22 #include <sys/types.h>
23 #include <sys/socket.h>
36 #include <script_lua.h>
40 #if LUA_VERSION_NUM >= 501
50 static void *l_alloc (
void * ,
void *ptr,
size_t ,
size_t nsize)
56 return g_realloc(ptr, nsize);
61 static const char* l_readerfile(lua_State *L,
void *data,
size_t *size)
63 static char buf[4096];
64 FILE* file = (FILE*)data;
65 *size = fread(buf, 1, 4096, file);
66 if ( !*size && ferror(file) ) {
69 if ( !*size && feof(file)) {
75 static struct script_state*
scripts = NULL;
76 static int script_count = 0;
78 static void update_player(lua_State* lua)
80 lua_pushstring(lua,
"player");
81 lua_gettable(lua, LUA_GLOBALSINDEX);
82 if (!lua_istable(lua, -1)) {
87 lua_pushstring(lua,
"hp");
89 lua_settable(lua, -3);
90 lua_pushstring(lua,
"gr");
92 lua_settable(lua, -3);
93 lua_pushstring(lua,
"sp");
95 lua_settable(lua, -3);
96 lua_pushstring(lua,
"food");
98 lua_settable(lua, -3);
103 static void do_item(lua_State* lua,
item* it)
106 lua_pushstring(lua,
"s_name");
107 lua_pushstring(lua, it->
s_name);
108 lua_settable(lua, -3);
109 lua_pushstring(lua,
"magical");
110 lua_pushnumber(lua, it->
magical);
111 lua_settable(lua, -3);
112 lua_pushstring(lua,
"cursed");
113 lua_pushnumber(lua, it->
cursed);
114 lua_settable(lua, -3);
115 lua_pushstring(lua,
"damned");
116 lua_pushnumber(lua, it->
damned);
117 lua_settable(lua, -3);
118 lua_pushstring(lua,
"unpaid");
119 lua_pushnumber(lua, it->
unpaid);
120 lua_settable(lua, -3);
121 lua_pushstring(lua,
"locked");
122 lua_pushnumber(lua, it->
locked);
123 lua_settable(lua, -3);
124 lua_pushstring(lua,
"applied");
125 lua_pushnumber(lua, it->
applied);
126 lua_settable(lua, -3);
127 lua_pushstring(lua,
"open");
128 lua_pushnumber(lua, it->
open);
129 lua_settable(lua, -3);
132 static void update_inv(lua_State* lua)
136 lua_pushstring(lua,
"inv");
138 lua_settable(lua, LUA_GLOBALSINDEX);
139 lua_pushstring(lua,
"inv");
140 lua_gettable(lua, LUA_GLOBALSINDEX);
143 lua_pushnumber(lua, index++);
145 lua_settable(lua, -3);
150 static void update_ground(lua_State* lua)
154 lua_pushstring(lua,
"ground");
156 lua_settable(lua, LUA_GLOBALSINDEX);
157 lua_pushstring(lua,
"ground");
158 lua_gettable(lua, LUA_GLOBALSINDEX);
161 if ( it->
tag == 0 || strlen(it->
s_name) == 0 ) {
165 lua_pushnumber(lua, index++);
168 lua_settable(lua, -3);
174 static int lua_draw(lua_State *L)
176 int n = lua_gettop(L);
179 lua_pushstring(L,
"draw what?");
182 if ( !lua_isstring(L, 1) ) {
183 lua_pushstring(L,
"expected a string");
187 what = lua_tostring(L,1);
193 static int lua_issue(lua_State *L)
195 int n = lua_gettop(L);
197 int repeat, must_send;
199 lua_pushstring(L,
"syntax is cfissue repeat must_send command");
202 if ( !lua_isnumber(L, 1) ) {
203 lua_pushstring(L,
"expected a number");
207 if ( !lua_isnumber(L, 2) ) {
208 lua_pushstring(L,
"expected a number");
212 if ( !lua_isstring(L, 3) ) {
213 lua_pushstring(L,
"expected a number");
217 repeat = lua_tonumber(L, 1);
218 must_send = lua_tonumber(L, 2);
219 what = lua_tostring(L,3);
230 int index = script_count;
232 file = fopen(name,
"r");
242 "Memory allocation error.");
251 if (( load = lua_load(lua, l_readerfile, (
void*)file, name))) {
254 if ( load == LUA_ERRSYNTAX )
263 lua_register(lua,
"cfdraw", lua_draw);
264 lua_register(lua,
"cfissue", lua_issue);
266 lua_pushstring(lua,
"player");
268 lua_settable(lua, LUA_GLOBALSINDEX);
274 if (lua_pcall(lua, 0, 0, 0)) {
285 "Could not allocate memory: %s", strerror(errno));
290 scripts[index].filename = g_strdup(name);
308 if ( script_count == 0 ) {
310 "No LUA scripts are currently running");
315 snprintf(buf,
sizeof(buf),
"%d LUA scripts currently running:",script_count);
317 for ( i=0; i<script_count; ++i) {
318 snprintf(buf,
sizeof(buf),
"%d %s",i+1,
scripts[i].filename);
328 if ( i < 0 || i >= script_count ) {
330 "Invalid script index!");
335 if ( i < (script_count-1) ) {
348 lua_pushstring(lua,
"event_stats");
349 lua_gettable(lua, LUA_GLOBALSINDEX);
350 if (lua_isfunction(lua, lua_gettop(lua))) {
355 if ( ( luaerror = lua_pcall(lua, 0, 0, 0) ) ) {
356 const char* what = lua_tostring(lua, lua_gettop(lua));
374 lua_pushstring(lua,
"event_command");
375 lua_gettable(lua, LUA_GLOBALSINDEX);
376 if (lua_isfunction(lua, lua_gettop(lua))) {
381 lua_pushstring(lua, command);
382 lua_pushstring(lua, param ? param :
"");
383 if ( ( luaerror = lua_pcall(lua, 2, 1, 0) ) ) {
384 const char* what = lua_tostring(lua, lua_gettop(lua));
389 ret = lua_tonumber(lua, 1);