static int tolua_region_set_resource(lua_State * L) { region *r = (region *)tolua_tousertype(L, 1, 0); const char *type = tolua_tostring(L, 2, 0); int result, value = (int)tolua_tonumber(L, 3, 0); critbit_tree * cb = special_resources(); void * match; if (cb_find_prefix(cb, type, strlen(type) + 1, &match, 1, 0)) { cb_get_kv(match, &result, sizeof(result)); switch (result) { case 0: case 1: case 2: rsettrees(r, result, value); break; case 3: deathcounts(r, value - deathcount(r)); break; case 4: add_chaoscount(r, value - get_chaoscount(r)); break; } } else { const resource_type *rtype = rt_find(type); if (rtype != NULL) { region_setresource(r, rtype, value); } } return 0; }
keyword_t get_keyword(const char *s, const struct locale *lang) { keyword_t result = NOKEYWORD; assert(lang); assert(s); while (*s == '@') ++s; if (*s) { char buffer[64]; char *str = transliterate(buffer, sizeof(buffer) - sizeof(int), s); if (str) { int i; void *match; void **tokens = get_translations(lang, UT_KEYWORDS); critbit_tree *cb = (critbit_tree *)*tokens; if (cb && cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) { cb_get_kv(match, &i, sizeof(int)); result = (keyword_t)i; return keyword_disabled(result) ? NOKEYWORD : result; } } } return NOKEYWORD; }
static evalfun find_function(const char *symbol) { void * matches; if (cb_find_prefix(&functions, symbol, strlen(symbol) + 1, &matches, 1, 0)) { evalfun result; cb_get_kv(matches, &result, sizeof(result)); return result; } return 0; }
static ship_type *st_find_i(const char *name) { const char *match; ship_type *st = NULL; match = cb_find_str(&cb_shiptypes, name); if (match) { cb_get_kv(match, &st, sizeof(st)); } return st; }
static int tolua_region_get_resource(lua_State * L) { region *r; const char *type; const resource_type *rtype; int result = 0; void * match; critbit_tree * cb = special_resources(); r = (region *)tolua_tousertype(L, 1, 0); LUA_ASSERT(r != NULL, "invalid parameter"); type = tolua_tostring(L, 2, 0); LUA_ASSERT(type != NULL, "invalid parameter"); if (cb_find_prefix(cb, type, strlen(type) + 1, &match, 1, 0)) { cb_get_kv(match, &result, sizeof(result)); switch (result) { case 0: case 1: case 2: result = rtrees(r, result); break; case 3: result = deathcount(r); break; case 4: result = get_chaoscount(r); break; } } else { rtype = rt_find(type); if (rtype) { result = region_getresource(r, rtype); } else { result = -1; } } lua_pushinteger(L, result); return 1; }
skill_t get_skill(const char *s, const struct locale * lang) { skill_t result = NOSKILL; char buffer[64]; if (s) { char * str = transliterate(buffer, sizeof(buffer) - sizeof(int), s); if (str) { int i; const void * match; void **tokens = get_translations(lang, UT_SKILLS); struct critbit_tree *cb = (critbit_tree *)*tokens; if (cb && cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) { cb_get_kv(match, &i, sizeof(int)); result = (skill_t)i; } } else { log_warning("could not transliterate skill: %s", s); } } return result; }
int a_read(struct storage *store, attrib ** attribs, void *owner) { int key, retval = AT_READ_OK; char zText[128]; zText[0] = 0; key = -1; READ_TOK(store, zText, sizeof(zText)); if (strcmp(zText, "end") == 0) return retval; else key = __at_hashkey(zText); while (key != -1) { int(*reader)(attrib *, void *, struct storage *) = 0; attrib_type *at = at_find(key); attrib * na = 0; if (at) { reader = at->read; na = a_new(at); } else { void * kv = 0; cb_find_prefix(&cb_deprecated, zText, strlen(zText) + 1, &kv, 1, 0); if (kv) { cb_get_kv(kv, &reader, sizeof(reader)); } else { fprintf(stderr, "attribute hash: %d (%s)\n", key, zText); assert(at || !"attribute not registered"); } } if (reader) { int i = reader(na, owner, store); if (na) { switch (i) { case AT_READ_OK: a_add(attribs, na); break; case AT_READ_FAIL: retval = AT_READ_FAIL; a_free(na); break; default: assert(!"invalid return value"); break; } } } else { assert(!"error: no registered callback can read attribute"); } READ_TOK(store, zText, sizeof(zText)); if (!strcmp(zText, "end")) break; key = __at_hashkey(zText); } return retval; }