forked from github/server
remove the special_resources trie.
memory leak, bad performance.
This commit is contained in:
parent
56eb1b753c
commit
3c60f863a5
2 changed files with 41 additions and 53 deletions
|
@ -25,6 +25,7 @@ end
|
|||
|
||||
function use_stardust(u, amount)
|
||||
local p = u.region:get_resource("peasant")
|
||||
assert(p>0)
|
||||
p = math.ceil(1.5 * p)
|
||||
u.region:set_resource("peasant", p)
|
||||
local msg = usepotion_message(u, "stardust")
|
||||
|
|
|
@ -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; }
|
||||
|
||||
static critbit_tree * special_resources(void)
|
||||
{
|
||||
static critbit_tree cb = CRITBIT_TREE();
|
||||
if (!cb.root) {
|
||||
const char * special[] = { "seed", "sapling", "tree", "grave", "chaos", 0 };
|
||||
char buffer[32];
|
||||
int 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);
|
||||
static int special_resource(const char *type) {
|
||||
const char * special[] = { "seed", "sapling", "tree", "grave", "chaos", 0 };
|
||||
int i;
|
||||
|
||||
for (i = 0; special[i]; ++i) {
|
||||
if (strcmp(type, special[i]) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return &cb;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int tolua_region_get_resource(lua_State * L)
|
||||
|
@ -362,32 +358,27 @@ 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();
|
||||
int result;
|
||||
|
||||
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 {
|
||||
|
||||
result = special_resource(type);
|
||||
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;
|
||||
default:
|
||||
rtype = rt_find(type);
|
||||
if (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);
|
||||
const char *type = tolua_tostring(L, 2, 0);
|
||||
int result, value = (int)tolua_tonumber(L, 3, 0);
|
||||
critbit_tree * cb = special_resources();
|
||||
void * match;
|
||||
const resource_type *rtype;
|
||||
|
||||
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);
|
||||
result = special_resource(type);
|
||||
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;
|
||||
default:
|
||||
rtype = rt_find(type);
|
||||
if (rtype != NULL) {
|
||||
region_setresource(r, rtype, value);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue