5 |
* modify it under the terms of the GNU General Public License |
* modify it under the terms of the GNU General Public License |
6 |
* as published by the Free Software Foundation; either version 2 |
* as published by the Free Software Foundation; either version 2 |
7 |
* of the License, or (at your option) any later version. |
* of the License, or (at your option) any later version. |
8 |
* |
* |
9 |
* This program is distributed in the hope that it will be useful, |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
48 |
public: |
public: |
49 |
LuaState (size_t stacksize = 0); |
LuaState (size_t stacksize = 0); |
50 |
~LuaState(); |
~LuaState(); |
51 |
|
|
52 |
lua_State *get_state() const { return m_state; } |
lua_State *get_state() const { return m_state; } |
53 |
private: |
private: |
54 |
LuaState (const LuaState &); |
LuaState (const LuaState &); |
55 |
LuaState &operator = (const LuaState &); |
LuaState &operator = (const LuaState &); |
56 |
|
|
57 |
/* |
/* |
58 |
** Variables |
** Variables |
59 |
*/ |
*/ |
60 |
lua_State *m_state; |
lua_State *m_state; |
61 |
}; |
}; |
62 |
|
|
63 |
typedef int LuaTag; |
typedef int LuaTag; |
64 |
|
|
65 |
|
|
66 |
class NumberType { |
class NumberType { |
67 |
|
|
68 |
}; |
}; |
69 |
|
|
70 |
class UserType { |
class UserType { |
71 |
public: |
public: |
72 |
UserType (LuaState *state); |
UserType (LuaState *state); |
73 |
|
|
74 |
LuaTag get_tag() const { return m_tag; } |
LuaTag get_tag() const { return m_tag; } |
75 |
|
|
76 |
bool is_type (int idx) { |
bool is_type (int idx) { |
77 |
lua_State *L = m_state->get_state(); |
lua_State *L = m_state->get_state(); |
78 |
return lua_isuserdata(L, idx) && lua_tag(L,idx)==m_tag; |
return lua_isuserdata(L, idx) && lua_tag(L,idx)==m_tag; |
79 |
} |
} |
80 |
|
|
81 |
private: |
private: |
82 |
LuaState *m_state; |
LuaState *m_state; |
83 |
LuaTag m_tag; |
LuaTag m_tag; |
147 |
return static_cast<world::Object*>(lua_touserdata(L,idx)); |
return static_cast<world::Object*>(lua_touserdata(L,idx)); |
148 |
} |
} |
149 |
|
|
150 |
static void |
static void |
151 |
pushobject(lua_State *L, world::Object *obj) |
pushobject(lua_State *L, world::Object *obj) |
152 |
{ |
{ |
153 |
/* |
/* |
154 |
Arghh, now that's a surprise. Lua does not allow us to store |
Arghh, now that's a surprise. Lua does not allow us to store |
155 |
NULL pointers in a userdata variable! Don't ask me why, it's |
NULL pointers in a userdata variable! Don't ask me why, it's |
156 |
not in the documentation (afaik). This cost me almost an hour |
not in the documentation (afaik). This cost me almost an hour |
165 |
//---------------------------------------------------------------------- |
//---------------------------------------------------------------------- |
166 |
// Enigma interface routines |
// Enigma interface routines |
167 |
//---------------------------------------------------------------------- |
//---------------------------------------------------------------------- |
168 |
static int |
static int |
169 |
en_make_object (lua_State *L) |
en_make_object (lua_State *L) |
170 |
{ |
{ |
171 |
world::Object *obj = world::MakeObject(lua_tostring(L, 1)); |
world::Object *obj = world::MakeObject(lua_tostring(L, 1)); |
290 |
} |
} |
291 |
|
|
292 |
static int |
static int |
293 |
|
en_play_sound(lua_State *L) |
294 |
|
{ |
295 |
|
world::Object *obj = to_object(L, 1); |
296 |
|
const char *soundname = lua_tostring(L, 2); |
297 |
|
|
298 |
|
if (!soundname) |
299 |
|
lua_error(L,"Illegal sound"); |
300 |
|
else if (obj) { |
301 |
|
world::GridObject *gobj = dynamic_cast<world::GridObject*>(obj); |
302 |
|
if (gobj) |
303 |
|
gobj->play_sound(soundname); |
304 |
|
} |
305 |
|
|
306 |
|
return 0; |
307 |
|
} |
308 |
|
|
309 |
|
static int |
310 |
en_name_object(lua_State *L) |
en_name_object(lua_State *L) |
311 |
{ |
{ |
312 |
world::Object *obj = to_object(L, 1); |
world::Object *obj = to_object(L, 1); |
379 |
} |
} |
380 |
|
|
381 |
static CFunction luafuncs[] = { |
static CFunction luafuncs[] = { |
382 |
{en_set_attrib, "SetAttrib"}, |
{en_set_attrib, "SetAttrib"}, |
383 |
{en_get_attrib, "GetAttrib"}, |
{en_get_attrib, "GetAttrib"}, |
384 |
{en_make_object, "MakeObject"}, |
{en_make_object, "MakeObject"}, |
385 |
{get_object_template, "GetObjectTemplate"}, |
{get_object_template, "GetObjectTemplate"}, |
386 |
{en_set_floor, "SetFloor"}, |
{en_set_floor, "SetFloor"}, |
387 |
{en_set_item, "SetItem"}, |
{en_set_item, "SetItem"}, |
388 |
{en_set_stone, "SetStone"}, |
{en_set_stone, "SetStone"}, |
389 |
{en_kill_stone, "KillStone"}, |
{en_kill_stone, "KillStone"}, |
390 |
{en_set_actor, "SetActor"}, |
{en_set_actor, "SetActor"}, |
391 |
{en_send_message, "SendMessage"}, |
{en_send_message, "SendMessage"}, |
392 |
{en_name_object, "NameObject"}, |
{en_play_sound, "PlaySound"}, |
393 |
{en_get_named_object,"GetNamedObject"}, |
{en_name_object, "NameObject"}, |
394 |
{add_constant_force, "AddConstantForce"}, |
{en_get_named_object, "GetNamedObject"}, |
395 |
{add_rubber_band, "AddRubberBand"}, |
{add_constant_force, "AddConstantForce"}, |
396 |
{find_data_file, "FindDataFile"}, |
{add_rubber_band, "AddRubberBand"}, |
397 |
{get_ticks, "GetTicks"}, |
{find_data_file, "FindDataFile"}, |
398 |
|
{get_ticks, "GetTicks"}, |
399 |
{0,0} |
{0,0} |
400 |
}; |
}; |
401 |
|
|
403 |
// lua:: functions |
// lua:: functions |
404 |
//---------------------------------------------------------------------- |
//---------------------------------------------------------------------- |
405 |
|
|
406 |
void |
void |
407 |
lua::RegisterFuncs(CFunction *funcs) |
lua::RegisterFuncs(CFunction *funcs) |
408 |
{ |
{ |
409 |
lua_getglobal(state, "enigma"); |
lua_getglobal(state, "enigma"); |
415 |
lua_pop(state, 1); |
lua_pop(state, 1); |
416 |
} |
} |
417 |
|
|
418 |
int |
int |
419 |
lua::CallFunc(const char *funcname, const Value& arg) |
lua::CallFunc(const char *funcname, const Value& arg) |
420 |
{ |
{ |
421 |
lua_getglobal(state, funcname); |
lua_getglobal(state, funcname); |
423 |
return lua_call(state, 1, 0); |
return lua_call(state, 1, 0); |
424 |
} |
} |
425 |
|
|
426 |
int |
int |
427 |
lua::Dofile(const string &filename) |
lua::Dofile(const string &filename) |
428 |
{ |
{ |
429 |
string fname = enigma::FindDataFile(filename); |
string fname = enigma::FindDataFile(filename); |
450 |
return retval; |
return retval; |
451 |
} |
} |
452 |
|
|
453 |
void |
void |
454 |
lua::Init() |
lua::Init() |
455 |
{ |
{ |
456 |
state = lua_open(0); |
state = lua_open(0); |