]> git.lizzy.rs Git - minetest.git/blob - src/script/scripting_game.cpp
Fix merge mistake when rebasing for PR #1169
[minetest.git] / src / script / scripting_game.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "scripting_game.h"
21 #include "log.h"
22 #include "cpp_api/s_internal.h"
23 #include "lua_api/l_base.h"
24 #include "lua_api/l_craft.h"
25 #include "lua_api/l_env.h"
26 #include "lua_api/l_inventory.h"
27 #include "lua_api/l_item.h"
28 #include "lua_api/l_mapgen.h"
29 #include "lua_api/l_nodemeta.h"
30 #include "lua_api/l_nodetimer.h"
31 #include "lua_api/l_noise.h"
32 #include "lua_api/l_object.h"
33 #include "lua_api/l_particles.h"
34 #include "lua_api/l_rollback.h"
35 #include "lua_api/l_server.h"
36 #include "lua_api/l_util.h"
37 #include "lua_api/l_vmanip.h"
38 #include "lua_api/l_settings.h"
39
40 extern "C" {
41 #include "lualib.h"
42 }
43
44 GameScripting::GameScripting(Server* server)
45 {
46         setServer(server);
47
48         // setEnv(env) is called by ScriptApiEnv::initializeEnvironment()
49         // once the environment has been created
50
51         //TODO add security
52
53         luaL_openlibs(getStack());
54
55         SCRIPTAPI_PRECHECKHEADER
56
57         // Create the main minetest table
58         lua_newtable(L);
59
60         lua_newtable(L);
61         lua_setfield(L, -2, "object_refs");
62
63         lua_newtable(L);
64         lua_setfield(L, -2, "luaentities");
65
66         lua_setglobal(L, "minetest");
67
68         // Initialize our lua_api modules
69         lua_getglobal(L, "minetest");
70         int top = lua_gettop(L);
71         InitializeModApi(L, top);
72         lua_pop(L, 1);
73
74         infostream << "SCRIPTAPI: initialized game modules" << std::endl;
75 }
76
77 void GameScripting::InitializeModApi(lua_State *L, int top)
78 {
79         // Initialize mod api modules
80         ModApiCraft::Initialize(L, top);
81         ModApiEnvMod::Initialize(L, top);
82         ModApiInventory::Initialize(L, top);
83         ModApiItemMod::Initialize(L, top);
84         ModApiMapgen::Initialize(L, top);
85         ModApiParticles::Initialize(L, top);
86         ModApiRollback::Initialize(L, top);
87         ModApiServer::Initialize(L, top);
88         ModApiUtil::Initialize(L, top);
89
90         // Register reference classes (userdata)
91         InvRef::Register(L);
92         LuaItemStack::Register(L);
93         LuaPerlinNoise::Register(L);
94         LuaPerlinNoiseMap::Register(L);
95         LuaPseudoRandom::Register(L);
96         LuaVoxelManip::Register(L);
97         NodeMetaRef::Register(L);
98         NodeTimerRef::Register(L);
99         ObjectRef::Register(L);
100         LuaSettings::Register(L);
101 }