1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
/* File: script.c */
/* Purpose: scripting in lua */
/*
* Copyright (c) 2001 Dark God
*
* This software may be copied and distributed for educational, research, and
* not for profit purposes provided that this copyright and statement are
* included in all such copies.
*/
#include "angband.h"
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lauxlib.h"
#include "tolua.h"
#ifdef RISCOS
extern char *riscosify_name(const char *path);
#endif
int tolua_monster_open (lua_State *L);
int tolua_player_open (lua_State *L);
int tolua_player_c_open (lua_State *L);
int tolua_util_open (lua_State *L);
int tolua_z_pack_open (lua_State *L);
int tolua_object_open (lua_State *L);
int tolua_spells_open (lua_State *L);
int tolua_quest_open (lua_State *L);
int tolua_dungeon_open (lua_State *L);
/*
* Lua state
*/
lua_State* L = NULL;
/* ToME Lua error message handler */
static int tome_errormessage(lua_State *L)
{
char buf[200];
cptr str = luaL_check_string(L, 1);
int i = 0, j = 0;
while (str[i])
{
if (str[i] == '#')
{
buf[j++] = '$';
}
else if (str[i] != '\n')
{
buf[j++] = str[i];
}
else
{
buf[j] = '\0';
cmsg_format(TERM_VIOLET, "LUA: %s", buf);
j = 0;
}
i++;
}
buf[j] = '\0';
cmsg_format(TERM_VIOLET, "LUA: %s", buf);
return (0);
}
static struct luaL_reg tome_iolib[] =
{
{ "_ALERT", tome_errormessage },
};
#define luaL_check_bit(L, n) ((long)luaL_check_number(L, n))
#define luaL_check_ubit(L, n) ((unsigned long)luaL_check_bit(L, n))
#if 0
/*
* Nuked because they can confuse some compilers (lcc for example),
* and because of their obscurity -- pelpel
*/
#define TDYADIC(name, op, t1, t2) \
static int int_ ## name(lua_State* L) { \
lua_pushnumber(L, \
luaL_check_ ## t1 ## bit(L, 1) op luaL_check_ ## t2 ## bit(L, 2)); \
return 1; \
}
#define DYADIC(name, op) \
static int int_ ## name(lua_State* L) { \
lua_pushnumber(L, \
luaL_check_bit(L, 1) op luaL_check_bit(L, 2)); \
return 1; \
}
#define MONADIC(name, op) \
static int int_ ## name(lua_State* L) { \
lua_pushnumber(L, op luaL_check_bit(L, 1)); \
return 1; \
}
#define VARIADIC(name, op) \
static int int_ ## name(lua_State *L) { \
int n = lua_gettop(L), i; \
long w = luaL_check_bit(L, 1); \
for (i = 2; i <= n; i++) \
w op ## = luaL_check_bit(L, i); \
lua_pushnumber(L, w); \
return 1; \
}
#endif
/*
* Monadic bit negation operation
* MONADIC(not, ~)
*/
static int int_not(lua_State* L)
{
lua_pushnumber(L, ~luaL_check_bit(L, 1));
return 1;
}
/*
* Dyadic integer modulus operation
* DYADIC(mod, %)
*/
static int int_mod(lua_State* L)
{
lua_pushnumber(L, luaL_check_bit(L, 1) % luaL_check_bit(L, 2));
return 1;
}
/*
* Variable length bitwise AND operation
* VARIADIC(and, &)
*/
static int int_and(lua_State *L)
{
int n = lua_gettop(L), i;
long w = luaL_check_bit(L, 1);
for (i = 2; i <= n; i++) w &= luaL_check_bit(L, i);
lua_pushnumber(L, w);
return 1;
}
/*
* Variable length bitwise OR operation
* VARIADIC(or, |)
*/
static int int_or(lua_State *L)
{
int n = lua_gettop(L), i;
long w = luaL_check_bit(L, 1);
for (i = 2; i <= n; i++) w |= luaL_check_bit(L, i);
lua_pushnumber(L, w);
return 1;
}
/*
* Variable length bitwise XOR operation
* VARIADIC(xor, ^)
*/
static int int_xor(lua_State *L)
{
int n = lua_gettop(L), i;
long w = luaL_check_bit(L, 1);
for (i = 2; i <= n; i++) w ^= luaL_check_bit(L, i);
lua_pushnumber(L, w);
return 1;
}
/*
* Binary left shift operation
* TDYADIC(lshift, <<, , u)
*/
static int int_lshift(lua_State* L)
{
lua_pushnumber(L, luaL_check_bit(L, 1) << luaL_check_ubit(L, 2));
return 1;
}
/*
* Binary logical right shift operation
* TDYADIC(rshift, >>, u, u)
*/
static int int_rshift(lua_State* L)
{
lua_pushnumber(L, luaL_check_ubit(L, 1) >> luaL_check_ubit(L, 2));
return 1;
}
/*
* Binary arithmetic right shift operation
* TDYADIC(arshift, >>, , u)
*/
static int int_arshift(lua_State* L)
{
lua_pushnumber(L, luaL_check_bit(L, 1) >> luaL_check_ubit(L, 2));
return 1;
}
static const struct luaL_reg bitlib[] =
{
{"bnot", int_not},
{"imod", int_mod}, /* "mod" already in Lua math library */
{"band", int_and},
{"bor", int_or},
{"bxor", int_xor},
{"lshift", int_lshift},
{"rshift", int_rshift},
{"arshift", int_arshift},
};
/*
* Some special wrappers
*/
static int lua_zsock_read(lua_State* L)
{
if (!tolua_istype(L, 1, tolua_tag(L, "zsock_hooks"), 0) ||
!tolua_istype(L, 2, tolua_tag(L, "ip_connection"), 0) ||
!tolua_istype(L, 3, LUA_TNUMBER, 0) ||
!tolua_istype(L, 4, LUA_TNUMBER, 0) ||
!tolua_isnoobj(L, 5)
)
{
tolua_error(L, "#ferror in function 'read'.");
return 0;
}
else
{
zsock_hooks* self = (zsock_hooks*) tolua_getusertype(L, 1, 0);
ip_connection* conn = ((ip_connection*) tolua_getusertype(L, 2, 0));
int len = ((int) tolua_getnumber(L, 3, 0));
int start_len = len;
bool raw = ((bool) tolua_getnumber(L, 4, 0));
char *str;
if (!self) tolua_error(L, "invalid 'self' in function 'read'");
{
bool toluaI_ret;
C_MAKE(str, start_len + 1, char);
toluaI_ret = (bool)self->read(conn, str, &len, raw);
tolua_pushnumber(L, (long)toluaI_ret);
tolua_pushstring(L, str);
tolua_pushnumber(L, (long)len);
C_FREE(str, start_len + 1, char);
return 3;
}
return 0;
}
}
/*
* Initialize lua scripting
*/
static bool init_lua_done = FALSE;
void init_lua()
{
/* Hack -- Do not initialize more than once */
if (init_lua_done) return;
init_lua_done = TRUE;
/* Start the interpreter with default stack size */
L = lua_open(0);
/* Register the Lua base libraries */
lua_baselibopen(L);
lua_strlibopen(L);
lua_iolibopen(L);
lua_dblibopen(L);
/* Register tome lua debug library */
luaL_openl(L, tome_iolib);
/* Register the bitlib */
luaL_openl(L, bitlib);
/* Register the ToME main APIs */
tolua_player_open(L);
tolua_player_c_open(L);
tolua_util_open(L);
tolua_z_pack_open(L);
tolua_object_open(L);
tolua_monster_open(L);
tolua_spells_open(L);
tolua_quest_open(L);
tolua_dungeon_open(L);
/* Register some special wrappers */
tolua_function(L, "zsock_hooks", "read", lua_zsock_read);
}
void init_lua_init()
{
int i, max;
/* Load the first lua file */
tome_dofile_anywhere(ANGBAND_DIR_CORE, "init.lua", TRUE);
/* Finish up schools */
max = exec_lua("return __schools_num");
init_schools(max);
for (i = 0; i < max; i++)
{
exec_lua(format("finish_school(%d)", i));
}
/* Finish up the spells */
max = exec_lua("return __tmp_spells_num");
init_spells(max);
for (i = 0; i < max; i++)
{
exec_lua(format("finish_spell(%d)", i));
}
/* Finish up the corruptions */
max = exec_lua("return __corruptions_max");
init_corruptions(max);
}
bool tome_dofile(char *file)
{
char buf[1024];
int oldtop = lua_gettop(L);
/* Build the filename */
path_build(buf, sizeof(buf), ANGBAND_DIR_SCPT, file);
if (!file_exist(buf))
{
/* No lua source(.lua), maybe a compiled one(.luo) ? */
if (suffix(buf, ".lua"))
{
int len = strlen(buf);
buf[len - 1] = 'o';
if (!file_exist(buf))
{
cmsg_format(TERM_VIOLET,
"tome_dofile(): file %s(%s) doesn't exist.", file, buf);
return (FALSE);
}
}
}
#ifdef RISCOS
{
char *realname = riscosify_name(buf);
lua_dofile(L, realname);
}
#else /* RISCOS */
lua_dofile(L, buf);
#endif /* RISCOS */
lua_settop(L, oldtop);
return (TRUE);
}
bool tome_dofile_anywhere(cptr dir, char *file, bool test_exist)
{
char buf[1024];
int oldtop = lua_gettop(L);
/* Build the filename */
path_build(buf, sizeof(buf), dir, file);
if (!file_exist(buf))
{
/* No lua source(.lua), maybe a compiled one(.luo) ? */
if (suffix(buf, ".lua"))
{
int len = strlen(buf);
buf[len - 1] = 'o';
if (!file_exist(buf))
{
if (test_exist)
cmsg_format(TERM_VIOLET,
"tome_dofile_anywhere(): file %s(%s) doesn't exist in %s.", dir, file, buf);
return (FALSE);
}
}
}
#ifdef RISCOS
{
char *realname = riscosify_name(buf);
lua_dofile(L, realname);
}
#else /* RISCOS */
lua_dofile(L, buf);
#endif /* RISCOS */
lua_settop(L, oldtop);
return (TRUE);
}
int exec_lua(char *file)
{
int oldtop = lua_gettop(L);
int res;
if (!lua_dostring(L, file))
{
int size = lua_gettop(L) - oldtop;
res = tolua_getnumber(L, -size, 0);
}
else
res = 0;
lua_settop(L, oldtop);
return (res);
}
cptr string_exec_lua(char *file)
{
int oldtop = lua_gettop(L);
cptr res;
if (!lua_dostring(L, file))
{
int size = lua_gettop(L) - oldtop;
res = tolua_getstring(L, -size, "");
}
else
res = "";
lua_settop(L, oldtop);
return (res);
}
void dump_lua_stack(int min, int max)
{
int i;
cmsg_print(TERM_YELLOW, "lua_stack:");
for (i = min; i <= max; i++)
{
if (lua_isnumber(L, i)) cmsg_format(TERM_YELLOW, "%d [n] = %d", i, tolua_getnumber(L, i, 0));
else if (lua_isstring(L, i)) cmsg_format(TERM_YELLOW, "%d [s] = '%s'", i, tolua_getstring(L, i, 0));
}
cmsg_print(TERM_YELLOW, "END lua_stack");
}
bool call_lua(cptr function, cptr args, cptr ret, ...)
{
int i = 0, nb = 0, nbr = 0;
int oldtop = lua_gettop(L), size;
va_list ap;
va_start(ap, ret);
/* Push the function */
lua_getglobal(L, function);
/* Push and count the arguments */
while (args[i])
{
switch (args[i++])
{
case 'd':
case 'l':
tolua_pushnumber(L, va_arg(ap, s32b));
nb++;
break;
case 's':
tolua_pushstring(L, va_arg(ap, char*));
nb++;
break;
case 'O':
tolua_pushusertype(L, (void*)va_arg(ap, object_type*), tolua_tag(L, "object_type"));
nb++;
break;
case 'M':
tolua_pushusertype(L, (void*)va_arg(ap, monster_type*), tolua_tag(L, "monster_type"));
nb++;
break;
case 'n':
lua_pushnil(L);
nb++;
break;
case '(':
case ')':
case ',':
break;
}
}
/* Count returns */
nbr = strlen(ret);
/* Call the function */
if (lua_call(L, nb, nbr))
{
cmsg_format(TERM_VIOLET, "ERROR in lua_call while calling '%s' from call_lua. Things should start breaking up from now on!", function);
return FALSE;
}
/* Number of returned values, SHOULD be the same as nbr, but I'm paranoid */
size = lua_gettop(L) - oldtop;
/* Get the returns */
for (i = 0; ret[i]; i++)
{
switch (ret[i])
{
case 'd':
case 'l':
{
s32b *tmp = va_arg(ap, s32b*);
if (lua_isnumber(L, ( -size) + i)) *tmp = tolua_getnumber(L, ( -size) + i, 0);
else *tmp = 0;
break;
}
case 's':
{
cptr *tmp = va_arg(ap, cptr*);
if (lua_isstring(L, ( -size) + i)) *tmp = tolua_getstring(L, ( -size) + i, "");
else *tmp = NULL;
break;
}
case 'O':
{
object_type **tmp = va_arg(ap, object_type**);
if (tolua_istype(L, ( -size) + i, tolua_tag(L, "object_type"), 0))
*tmp = (object_type*)tolua_getuserdata(L, ( -size) + i, NULL);
else
*tmp = NULL;
break;
}
case 'M':
{
monster_type **tmp = va_arg(ap, monster_type**);
if (tolua_istype(L, ( -size) + i, tolua_tag(L, "monster_type"), 0))
*tmp = (monster_type*)tolua_getuserdata(L, ( -size) + i, NULL);
else
*tmp = NULL;
break;
}
default:
cmsg_format(TERM_VIOLET, "ERROR in lua_call while calling '%s' from call_lua: Unknown return type '%c'", function, ret[i]);
return FALSE;
}
}
lua_settop(L, oldtop);
va_end(ap);
return TRUE;
}
bool get_lua_var(cptr name, char type, void *arg)
{
int oldtop = lua_gettop(L), size;
/* Push the function */
lua_getglobal(L, name);
size = lua_gettop(L) - oldtop;
switch (type)
{
case 'd':
case 'l':
{
s32b *tmp = (s32b*)arg;
if (lua_isnumber(L, ( -size))) *tmp = tolua_getnumber(L, ( -size), 0);
else *tmp = 0;
break;
}
case 's':
{
cptr *tmp = (cptr*)arg;
if (lua_isstring(L, ( -size))) *tmp = tolua_getstring(L, ( -size), "");
else *tmp = NULL;
break;
}
case 'O':
{
object_type **tmp = (object_type**)arg;
if (tolua_istype(L, ( -size), tolua_tag(L, "object_type"), 0))
*tmp = (object_type*)tolua_getuserdata(L, ( -size), NULL);
else
*tmp = NULL;
break;
}
case 'M':
{
monster_type **tmp = (monster_type**)arg;
if (tolua_istype(L, ( -size), tolua_tag(L, "monster_type"), 0))
*tmp = (monster_type*)tolua_getuserdata(L, ( -size), NULL);
else
*tmp = NULL;
break;
}
default:
cmsg_format(TERM_VIOLET, "ERROR in get_lua_var while calling '%s': Unknown return type '%c'", name, type);
return FALSE;
}
lua_settop(L, oldtop);
return TRUE;
}
|