29 #include <sys/types.h>
30 #include <sys/socket.h>
54 static void *l_alloc (
void * ,
void *ptr,
size_t ,
size_t nsize)
61 return realloc(ptr, nsize);
65 static const char* l_readerfile(lua_State *L,
void *data,
size_t *size)
67 static char buf[4096];
68 FILE* file = (FILE*)data;
69 *size = fread(buf, 1, 4096, file);
70 if ( !*size && ferror(file) )
72 if ( !*size && feof(file))
77 static struct script_state*
scripts = NULL;
78 static int script_count = 0;
80 static void update_player(lua_State* lua)
82 lua_pushstring(lua,
"player");
83 lua_gettable(lua, LUA_GLOBALSINDEX);
84 if (!lua_istable(lua, -1))
90 lua_pushstring(lua,
"hp");
92 lua_settable(lua, -3);
93 lua_pushstring(lua,
"gr");
95 lua_settable(lua, -3);
96 lua_pushstring(lua,
"sp");
98 lua_settable(lua, -3);
99 lua_pushstring(lua,
"food");
101 lua_settable(lua, -3);
106 static void do_item(lua_State* lua,
item* it)
109 lua_pushstring(lua,
"s_name");
110 lua_pushstring(lua, it->
s_name);
111 lua_settable(lua, -3);
112 lua_pushstring(lua,
"magical");
113 lua_pushnumber(lua, it->
magical);
114 lua_settable(lua, -3);
115 lua_pushstring(lua,
"cursed");
116 lua_pushnumber(lua, it->
cursed);
117 lua_settable(lua, -3);
118 lua_pushstring(lua,
"damned");
119 lua_pushnumber(lua, it->
damned);
120 lua_settable(lua, -3);
121 lua_pushstring(lua,
"unpaid");
122 lua_pushnumber(lua, it->
unpaid);
123 lua_settable(lua, -3);
124 lua_pushstring(lua,
"locked");
125 lua_pushnumber(lua, it->
locked);
126 lua_settable(lua, -3);
127 lua_pushstring(lua,
"applied");
128 lua_pushnumber(lua, it->
applied);
129 lua_settable(lua, -3);
130 lua_pushstring(lua,
"open");
131 lua_pushnumber(lua, it->
open);
132 lua_settable(lua, -3);
135 static void update_inv(lua_State* lua)
139 lua_pushstring(lua,
"inv");
141 lua_settable(lua, LUA_GLOBALSINDEX);
142 lua_pushstring(lua,
"inv");
143 lua_gettable(lua, LUA_GLOBALSINDEX);
147 lua_pushnumber(lua, index++);
149 lua_settable(lua, -3);
154 static void update_ground(lua_State* lua)
158 lua_pushstring(lua,
"ground");
160 lua_settable(lua, LUA_GLOBALSINDEX);
161 lua_pushstring(lua,
"ground");
162 lua_gettable(lua, LUA_GLOBALSINDEX);
166 if ( it->
tag == 0 || strlen(it->
s_name) == 0 )
169 lua_pushnumber(lua, index++);
172 lua_settable(lua, -3);
178 static int lua_draw(lua_State *L) {
179 int n = lua_gettop(L);
183 lua_pushstring(L,
"draw what?");
186 if ( !lua_isstring(L, 1) )
188 lua_pushstring(L,
"expected a string");
192 what = lua_tostring(L,1);
198 static int lua_issue(lua_State *L) {
199 int n = lua_gettop(L);
201 int repeat, must_send;
204 lua_pushstring(L,
"syntax is cfissue repeat must_send command");
207 if ( !lua_isnumber(L, 1) )
209 lua_pushstring(L,
"expected a number");
213 if ( !lua_isnumber(L, 2) )
215 lua_pushstring(L,
"expected a number");
219 if ( !lua_isstring(L, 3) )
221 lua_pushstring(L,
"expected a number");
225 repeat = lua_tonumber(L, 1);
226 must_send = lua_tonumber(L, 2);
227 what = lua_tostring(L,3);
238 int index = script_count;
240 file = fopen(name,
"r");
259 if (( load = lua_load(lua, l_readerfile, (
void*)file, name)))
262 if ( load == LUA_ERRSYNTAX )
270 lua_register(lua,
"cfdraw", lua_draw);
271 lua_register(lua,
"cfissue", lua_issue);
273 lua_pushstring(lua,
"player");
275 lua_settable(lua, LUA_GLOBALSINDEX);
281 if (lua_pcall(lua, 0, 0, 0))
289 scripts = realloc(scripts,
sizeof(scripts[0])*(script_count+1));
292 scripts[index].state = lua;
309 if ( script_count == 0 )
318 snprintf(buf,
sizeof(buf),
"%d LUA scripts currently running:",script_count);
320 for ( i=0;i<script_count;++i)
322 snprintf(buf,
sizeof(buf),
"%d %s",i+1,scripts[i].filename);
332 if ( i < 0 || i >= script_count )
337 lua_close(scripts[i].state);
339 if ( i < (script_count-1) )
341 memmove(&scripts[i],&scripts[i+1],
sizeof(scripts[i])*(script_count-i-1));
351 for ( script = 0; script < script_count; script++ )
353 lua = scripts[script].state;
354 lua_pushstring(lua,
"event_stats");
355 lua_gettable(lua, LUA_GLOBALSINDEX);
356 if (lua_isfunction(lua, lua_gettop(lua)))
362 if ( ( luaerror = lua_pcall(lua, 0, 0, 0) ) )
364 const char* what = lua_tostring(lua, lua_gettop(lua));
379 for ( script = 0; script < script_count; script++ )
381 lua = scripts[script].state;
382 lua_pushstring(lua,
"event_command");
383 lua_gettable(lua, LUA_GLOBALSINDEX);
384 if (lua_isfunction(lua, lua_gettop(lua)))
390 lua_pushstring(lua, command);
391 lua_pushstring(lua, param ? param :
"");
392 if ( ( luaerror = lua_pcall(lua, 2, 1, 0) ) ) {
393 const char* what = lua_tostring(lua, lua_gettop(lua));
398 ret = lua_tonumber(lua, 1);
void script_lua_list(const char *param)
int script_lua_command(const char *command, const char *param)
void script_lua_kill(const char *param)
struct item_struct * next
const char *const rcsid_common_script_lua_c
static struct script * scripts
int send_command(const char *command, int repeat, int must_send)
void script_lua_stats(void)
char * strdup_local(const char *str)
void draw_info(const char *str, int color)
void script_lua_load(const char *name)