remove the special_resources trie.

memory leak, bad performance.
This commit is contained in:
Enno Rehling 2017-02-26 18:17:58 +01:00
parent 56eb1b753c
commit 3c60f863a5
2 changed files with 41 additions and 53 deletions

View file

@ -25,6 +25,7 @@ end
function use_stardust(u, amount) function use_stardust(u, amount)
local p = u.region:get_resource("peasant") local p = u.region:get_resource("peasant")
assert(p>0)
p = math.ceil(1.5 * p) p = math.ceil(1.5 * p)
u.region:set_resource("peasant", p) u.region:set_resource("peasant", p)
local msg = usepotion_message(u, "stardust") local msg = usepotion_message(u, "stardust")

View file

@ -341,20 +341,16 @@ static int tolua_region_get_resourcelevel(lua_State * L)
#define LUA_ASSERT(c, s) if (!(c)) { log_error("%s(%d): %s\n", __FILE__, __LINE__, (s)); return 0; } #define LUA_ASSERT(c, s) if (!(c)) { log_error("%s(%d): %s\n", __FILE__, __LINE__, (s)); return 0; }
static critbit_tree * special_resources(void) static int special_resource(const char *type) {
{ const char * special[] = { "seed", "sapling", "tree", "grave", "chaos", 0 };
static critbit_tree cb = CRITBIT_TREE(); int i;
if (!cb.root) {
const char * special[] = { "seed", "sapling", "tree", "grave", "chaos", 0 }; for (i = 0; special[i]; ++i) {
char buffer[32]; if (strcmp(type, special[i]) == 0) {
int i; return i;
for (i = 0; special[i]; ++i) {
size_t len = strlen(special[i]);
len = cb_new_kv(special[i], len, &i, sizeof(int), buffer);
cb_insert(&cb, buffer, len);
} }
} }
return &cb; return -1;
} }
static int tolua_region_get_resource(lua_State * L) static int tolua_region_get_resource(lua_State * L)
@ -362,32 +358,27 @@ static int tolua_region_get_resource(lua_State * L)
region *r; region *r;
const char *type; const char *type;
const resource_type *rtype; const resource_type *rtype;
int result = 0; int result;
void * match;
critbit_tree * cb = special_resources();
r = (region *)tolua_tousertype(L, 1, 0); r = (region *)tolua_tousertype(L, 1, 0);
LUA_ASSERT(r != NULL, "invalid parameter"); LUA_ASSERT(r != NULL, "invalid parameter");
type = tolua_tostring(L, 2, 0); type = tolua_tostring(L, 2, 0);
LUA_ASSERT(type != NULL, "invalid parameter"); LUA_ASSERT(type != NULL, "invalid parameter");
if (cb_find_prefix(cb, type, strlen(type) + 1, &match, 1, 0)) { result = special_resource(type);
cb_get_kv(match, &result, sizeof(result)); switch (result) {
switch (result) { case 0:
case 0: case 1:
case 1: case 2:
case 2: result = rtrees(r, result);
result = rtrees(r, result); break;
break; case 3:
case 3: result = deathcount(r);
result = deathcount(r); break;
break; case 4:
case 4: result = get_chaoscount(r);
result = get_chaoscount(r); break;
break; default:
}
}
else {
rtype = rt_find(type); rtype = rt_find(type);
if (rtype) { if (rtype) {
result = region_getresource(r, rtype); result = region_getresource(r, rtype);
@ -406,27 +397,23 @@ static int tolua_region_set_resource(lua_State * L)
region *r = (region *)tolua_tousertype(L, 1, 0); region *r = (region *)tolua_tousertype(L, 1, 0);
const char *type = tolua_tostring(L, 2, 0); const char *type = tolua_tostring(L, 2, 0);
int result, value = (int)tolua_tonumber(L, 3, 0); int result, value = (int)tolua_tonumber(L, 3, 0);
critbit_tree * cb = special_resources(); const resource_type *rtype;
void * match;
if (cb_find_prefix(cb, type, strlen(type) + 1, &match, 1, 0)) { result = special_resource(type);
cb_get_kv(match, &result, sizeof(result)); switch (result) {
switch (result) { case 0:
case 0: case 1:
case 1: case 2:
case 2: rsettrees(r, result, value);
rsettrees(r, result, value); break;
break; case 3:
case 3: deathcounts(r, value - deathcount(r));
deathcounts(r, value - deathcount(r)); break;
break; case 4:
case 4: add_chaoscount(r, value - get_chaoscount(r));
add_chaoscount(r, value - get_chaoscount(r)); break;
break; default:
} rtype = rt_find(type);
}
else {
const resource_type *rtype = rt_find(type);
if (rtype != NULL) { if (rtype != NULL) {
region_setresource(r, rtype, value); region_setresource(r, rtype, value);
} }