Made lua run under MSWin with Dev-Cpp 4.9.9.2
[sdlbotor.git] / luafuncs.cpp
blob78b5199574bfdeabbbcb7a78b7f76e61cd75a848
1 #include "luafuncs.h"
3 #include "Game.h"
4 #include "Player.h"
5 #include "Map.h"
6 #include "Object.h"
8 namespace botor
11 void LuaFuncs::Push( lua_State *L )
14 luaL_register( L, "map", lMapFuncs );
16 luaL_register( L, "player", lPlayerFuncs );
20 LUA_FUNC( LuaFuncs::Map_Acid )
22 int largc = lua_gettop( L );
23 if( largc != 2 )
24 return luaL_error( L, "Got %d arguments, needed exactly 2!", largc );
26 Uint8 X = (Uint8)luaL_checknumber( L, 1 );
27 Uint8 Y = (Uint8)luaL_checknumber( L, 2 );
29 Game::getMap()->Acid( X, Y );
31 return 0;
34 LUA_FUNC( LuaFuncs::Map_CreateObject )
36 int largc = lua_gettop( L );
37 if( largc != 3 )
38 return luaL_error( L, "Got %d arguments, needed exactly 3!", largc );
40 const char *objname = luaL_checkstring( L, 1 );
41 Uint8 X = (Uint8)luaL_checknumber( L, 2 );
42 Uint8 Y = (Uint8)luaL_checknumber( L, 3 );
44 Game::getMap()->insertObject( new Object( Game::objectTypes[objname] ) , X, Y );
46 return 0;
49 LUA_FUNC( LuaFuncs::Map_CreateRobot )
51 int largc = lua_gettop( L );
52 if( largc != 2 )
53 return luaL_error( L, "Got %d arguments, needed exactly 2!", largc );
55 Uint8 X = (Uint8)luaL_checknumber( L, 1 );
56 Uint8 Y = (Uint8)luaL_checknumber( L, 2 );
58 Game::getMap()->insertRobot( X, Y );
60 return 0;
64 LUA_FUNC( LuaFuncs::Player_GetPosition )
66 Player *p = Game::getPlayer();
68 lua_newtable( L );
70 lua_pushstring( L, "x" );
71 lua_pushnumber( L, p->getMapX() );
72 lua_settable( L, -3 );
74 lua_pushstring( L, "y" );
75 lua_pushnumber( L, p->getMapY() );
76 lua_settable( L, -3 );
78 return 1;
81 LUA_FUNC( LuaFuncs::Player_SetImmune )
83 int largc = lua_gettop( L );
84 if( largc != 1 )
85 return luaL_error( L, "Got %d arguments, needed exactly 1!", largc );
87 bool newstate = (bool)luaL_checknumber( L, 1 );
89 Game::getPlayer()->SetImmune( newstate );
91 return 0;
94 LUA_FUNC( LuaFuncs::Player_Teleport )
96 int largc = lua_gettop( L );
97 if( largc != 2 )
98 return luaL_error( L, "Got %d arguments, needed exactly 2!", largc );
100 Uint8 X = (Uint8)luaL_checknumber( L, 1 );
101 Uint8 Y = (Uint8)luaL_checknumber( L, 2 );
103 Game::getPlayer()->Teleport( X, Y );
105 return 0;
108 LUA_FUNC( LuaFuncs::Player_Pickup ) { return 0; }
110 LUA_FUNC( LuaFuncs::Player_Drop ) { return 0; }
112 const luaL_Reg LuaFuncs::lMapFuncs[] = {
113 {"acid", Map_Acid },
114 {"CreateObject", Map_CreateObject },
115 {"CreateRobot", Map_CreateRobot },
116 {NULL, NULL}
119 const luaL_Reg LuaFuncs::lPlayerFuncs[] = {
120 { "getPosition", Player_GetPosition },
121 { "setImmune", Player_SetImmune },
122 { "teleport", Player_Teleport },
123 { "pickup", Player_Pickup },
124 { "drop", Player_Drop },
125 { NULL, NULL }