]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/scripting_game.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.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
39 extern "C" {
40 #include "lualib.h"
41 }
42
43 GameScripting::GameScripting(Server* server)
44 {
45         setServer(server);
46
47         // setEnv(env) is called by ScriptApiEnv::initializeEnvironment()
48         // once the environment has been created
49
50         //TODO add security
51
52         luaL_openlibs(getStack());
53
54         SCRIPTAPI_PRECHECKHEADER
55
56         // Create the main minetest table
57         lua_newtable(L);
58
59         lua_newtable(L);
60         lua_setfield(L, -2, "object_refs");
61
62         lua_newtable(L);
63         lua_setfield(L, -2, "luaentities");
64
65         lua_setglobal(L, "minetest");
66
67         // Initialize our lua_api modules
68         lua_getglobal(L, "minetest");
69         int top = lua_gettop(L);
70         InitializeModApi(L, top);
71         lua_pop(L, 1);
72
73         infostream << "SCRIPTAPI: initialized game modules" << std::endl;
74 }
75
76 void GameScripting::InitializeModApi(lua_State *L, int top)
77 {
78         // Initialize mod api modules
79         ModApiCraft::Initialize(L, top);
80         ModApiEnvMod::Initialize(L, top);
81         ModApiInventory::Initialize(L, top);
82         ModApiItemMod::Initialize(L, top);
83         ModApiMapgen::Initialize(L, top);
84         ModApiParticles::Initialize(L, top);
85         ModApiRollback::Initialize(L, top);
86         ModApiServer::Initialize(L, top);
87         ModApiUtil::Initialize(L, top);
88
89         // Register reference classes (userdata)
90         InvRef::Register(L);
91         LuaItemStack::Register(L);
92         LuaPerlinNoise::Register(L);
93         LuaPerlinNoiseMap::Register(L);
94         LuaPseudoRandom::Register(L);
95         LuaVoxelManip::Register(L);
96         NodeMetaRef::Register(L);
97         NodeTimerRef::Register(L);
98         ObjectRef::Register(L);
99 }