static double lua_building_taxes(building * b, int level) { lua_State *L = (lua_State *)global.vm_state; const char *fname = "building_taxes"; double result = 0.0F; lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); lua_pushinteger(L, level); if (lua_pcall(L, 2, 1, 0) != 0) { const char *error = lua_tostring(L, -1); log_error("building_taxes(%s) calling '%s': %s.\n", buildingname(b), fname, error); lua_pop(L, 1); } else { result = (double)lua_tonumber(L, -1); lua_pop(L, 1); } } else { log_error("building_taxes(%s) calling '%s': not a function.\n", buildingname(b), fname); lua_pop(L, 1); } return result; }
static void lua_agebuilding(building * b) { lua_State *L = (lua_State *)global.vm_state; char fname[64]; strlcpy(fname, "age_", sizeof(fname)); strlcat(fname, b->type->_name, sizeof(fname)); lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); if (lua_pcall(L, 1, 0, 0) != 0) { const char *error = lua_tostring(L, -1); log_error("agebuilding(%s) calling '%s': %s.\n", buildingname(b), fname, error); lua_pop(L, 1); } } else { log_error("agebuilding(%s) calling '%s': not a function.\n", buildingname(b), fname); lua_pop(L, 1); } }
static int lc_age(struct attrib *a, void *owner) { building_action *data = (building_action *)a->data.v; const char *fname = data->fname; const char *fparam = data->param; building *b = (building *)owner; int result = -1; assert(b != NULL); if (fname != NULL) { lua_State *L = (lua_State *)global.vm_state; lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); if (fparam) { tolua_pushstring(L, fparam); } if (lua_pcall(L, fparam ? 2 : 1, 1, 0) != 0) { const char *error = lua_tostring(L, -1); log_error("lc_age(%s) calling '%s': %s.\n", buildingname(b), fname, error); lua_pop(L, 1); } else { result = (int)lua_tonumber(L, -1); lua_pop(L, 1); } } else { log_error("lc_age(%s) calling '%s': not a function.\n", buildingname(b), fname); lua_pop(L, 1); } } return (result != 0) ? AT_AGE_KEEP : AT_AGE_REMOVE; }
static int tolua_building_tostring(lua_State * L) { building *self = (building *)tolua_tousertype(L, 1, 0); lua_pushstring(L, buildingname(self)); return 1; }