From dc8a8cdd71ba68f722efa7264072ab3a6141e826 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 24 Jun 2014 08:19:11 -0700 Subject: [PATCH] rt_find -> get_resourcetype optimization --- src/economy.c | 16 ++--------- src/items/seed.c | 63 ++++++++++++++++++++--------------------- src/items/seed.h | 3 -- src/kernel/item.c | 3 +- src/kernel/item.h | 3 +- src/kernel/magic.test.c | 32 ++++++++++----------- src/kernel/pool.c | 4 +-- src/tests.test.c | 36 +++++++++++------------ 8 files changed, 73 insertions(+), 87 deletions(-) diff --git a/src/economy.c b/src/economy.c index a933e617e..a30992a62 100644 --- a/src/economy.c +++ b/src/economy.c @@ -66,8 +66,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include -#include - /* libs includes */ #include #include @@ -2493,11 +2491,7 @@ static void planttrees(region * r, unit * u, int raw) } /* Mallornbäume kann man nur in Mallornregionen züchten */ - if (fval(r, RF_MALLORN)) { - rtype = rt_mallornseed; - } else { - rtype = rt_seed; - } + rtype = get_resourcetype(fval(r, RF_MALLORN) ? R_MALLORNSEED : R_SEED); /* Skill prüfen */ skill = eff_skill(u, SK_HERBALISM, r); @@ -2565,11 +2559,7 @@ static void breedtrees(region * r, unit * u, int raw) } /* Mallornbäume kann man nur in Mallornregionen züchten */ - if (fval(r, RF_MALLORN)) { - rtype = rt_mallornseed; - } else { - rtype = rt_seed; - } + rtype = get_resourcetype(fval(r, RF_MALLORN) ? R_MALLORNSEED : R_SEED); /* Skill prüfen */ skill = eff_skill(u, SK_HERBALISM, r); @@ -2679,7 +2669,7 @@ static void breed_cmd(unit * u, struct order *ord) default: if (p != P_ANY) { rtype = findresourcetype(s, u->faction->locale); - if (rtype == rt_mallornseed || rtype == rt_seed) { + if (rtype == get_resourcetype(R_SEED) || rtype == get_resourcetype(R_MALLORNSEED)) { breedtrees(r, u, m); break; } else if (rtype != get_resourcetype(R_HORSE)) { diff --git a/src/items/seed.c b/src/items/seed.c index 683e4714b..5f60709d2 100644 --- a/src/items/seed.c +++ b/src/items/seed.c @@ -28,35 +28,33 @@ /* libc includes */ #include -resource_type *rt_seed = 0; -resource_type *rt_mallornseed = 0; - static void produce_seeds(region * r, const resource_type * rtype, int norders) { - assert(rtype == rt_seed && r->land && r->land->trees[0] >= norders); - r->land->trees[0] -= norders; + assert(r->land && r->land->trees[0] >= norders); + r->land->trees[0] -= norders; } static int limit_seeds(const region * r, const resource_type * rtype) { - assert(rtype == rt_seed); - if (fval(r, RF_MALLORN)) - return 0; - return r->land ? r->land->trees[0] : 0; + if (fval(r, RF_MALLORN)) { + return 0; + } + return r->land ? r->land->trees[0] : 0; } void init_seed(void) { - attrib *a; - resource_limit *rdata; - - rt_seed = rt_find("seed"); - if (rt_seed != NULL) { - a = a_add(&rt_seed->attribs, a_new(&at_resourcelimit)); - rdata = (resource_limit *) a->data.v; - rdata->limit = limit_seeds; - rdata->produce = produce_seeds; - } + attrib *a; + resource_limit *rdata; + resource_type *rtype; + + rtype = rt_find("seed"); + if (rtype != NULL) { + a = a_add(&rtype->attribs, a_new(&at_resourcelimit)); + rdata = (resource_limit *) a->data.v; + rdata->limit = limit_seeds; + rdata->produce = produce_seeds; + } } /* mallorn */ @@ -64,14 +62,12 @@ void init_seed(void) static void produce_mallornseeds(region * r, const resource_type * rtype, int norders) { - assert(rtype == rt_mallornseed && r->land && r->land->trees[0] >= norders); assert(fval(r, RF_MALLORN)); r->land->trees[0] -= norders; } static int limit_mallornseeds(const region * r, const resource_type * rtype) { - assert(rtype == rt_mallornseed); if (!fval(r, RF_MALLORN)) { return 0; } @@ -80,17 +76,18 @@ static int limit_mallornseeds(const region * r, const resource_type * rtype) void init_mallornseed(void) { - attrib *a; - resource_limit *rdata; + attrib *a; + resource_limit *rdata; + resource_type *rtype; - rt_mallornseed = rt_find("mallornseed"); - if (rt_mallornseed != NULL) { - rt_mallornseed->flags |= RTF_LIMITED; - rt_mallornseed->flags |= RTF_POOLED; - - a = a_add(&rt_mallornseed->attribs, a_new(&at_resourcelimit)); - rdata = (resource_limit *) a->data.v; - rdata->limit = limit_mallornseeds; - rdata->produce = produce_mallornseeds; - } + rtype = rt_find("mallornseed"); + if (rtype != NULL) { + rtype->flags |= RTF_LIMITED; + rtype->flags |= RTF_POOLED; + + a = a_add(&rtype->attribs, a_new(&at_resourcelimit)); + rdata = (resource_limit *) a->data.v; + rdata->limit = limit_mallornseeds; + rdata->produce = produce_mallornseeds; + } } diff --git a/src/items/seed.h b/src/items/seed.h index 810d8bd08..f789b1f6e 100644 --- a/src/items/seed.h +++ b/src/items/seed.h @@ -22,10 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern "C" { #endif - extern struct resource_type *rt_seed; extern void init_seed(void); - - extern struct resource_type *rt_mallornseed; extern void init_mallornseed(void); #ifdef __cplusplus diff --git a/src/kernel/item.c b/src/kernel/item.c index 4fae67893..5617b84eb 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -609,7 +609,8 @@ const char *itemnames[MAX_RESOURCES] = { "laen", "fairyboot", "aoc", "pegasus", "elvenhorse", "charger", "dolphin", "roqf", "trollbelt", "aurafocus", "sphereofinv", "magicbag", - "magicherbbag", "dreameye", "p2", "money", "aura", "permaura", + "magicherbbag", "dreameye", "p2", "seed", "mallornseed", + "money", "aura", "permaura", "hp", "unit", "peasant" }; diff --git a/src/kernel/item.h b/src/kernel/item.h index 50eb8959d..7105c050c 100644 --- a/src/kernel/item.h +++ b/src/kernel/item.h @@ -290,7 +290,8 @@ extern "C" { R_SACK_OF_CONSERVATION, R_TACTICCRYSTAL, R_WATER_OF_LIFE, - + R_SEED, + R_MALLORNSEED, /* SONSTIGE */ R_SILVER, R_AURA, /* Aura */ diff --git a/src/kernel/magic.test.c b/src/kernel/magic.test.c index 6d7ace6ba..3f134016f 100644 --- a/src/kernel/magic.test.c +++ b/src/kernel/magic.test.c @@ -78,13 +78,13 @@ static spell * test_magic_create_spell(void) sp->components = (spell_component *) calloc(4, sizeof(spell_component)); sp->components[0].amount = 1; - sp->components[0].type = rt_find("money"); + sp->components[0].type = get_resourcetype(R_SILVER); sp->components[0].cost = SPC_FIX; sp->components[1].amount = 1; - sp->components[1].type = rt_find("aura"); + sp->components[1].type = get_resourcetype(R_AURA); sp->components[1].cost = SPC_LEVEL; sp->components[2].amount = 1; - sp->components[2].type = rt_find("horse"); + sp->components[2].type = get_resourcetype(R_HORSE); sp->components[2].cost = SPC_LINEAR; return sp; } @@ -110,16 +110,16 @@ void test_pay_spell(CuTest * tc) set_level(u, SK_MAGIC, 5); unit_add_spell(u, 0, sp, 1); - change_resource(u, rt_find("money"), 1); - change_resource(u, rt_find("aura"), 3); - change_resource(u, rt_find("horse"), 3); + change_resource(u, get_resourcetype(R_SILVER), 1); + change_resource(u, get_resourcetype(R_AURA), 3); + change_resource(u, get_resourcetype(R_HORSE), 3); level = eff_spelllevel(u, sp, 3, 1); CuAssertIntEquals(tc, 3, level); pay_spell(u, sp, level, 1); - CuAssertIntEquals(tc, 0, get_resource(u, rt_find("money"))); - CuAssertIntEquals(tc, 0, get_resource(u, rt_find("aura"))); - CuAssertIntEquals(tc, 0, get_resource(u, rt_find("horse"))); + CuAssertIntEquals(tc, 0, get_resource(u, get_resourcetype(R_SILVER))); + CuAssertIntEquals(tc, 0, get_resource(u, get_resourcetype(R_AURA))); + CuAssertIntEquals(tc, 0, get_resource(u, get_resourcetype(R_HORSE))); } void test_pay_spell_failure(CuTest * tc) @@ -143,19 +143,19 @@ void test_pay_spell_failure(CuTest * tc) set_level(u, SK_MAGIC, 5); unit_add_spell(u, 0, sp, 1); - CuAssertIntEquals(tc, 1, change_resource(u, rt_find("money"), 1)); - CuAssertIntEquals(tc, 2, change_resource(u, rt_find("aura"), 2)); - CuAssertIntEquals(tc, 3, change_resource(u, rt_find("horse"), 3)); + CuAssertIntEquals(tc, 1, change_resource(u, get_resourcetype(R_SILVER), 1)); + CuAssertIntEquals(tc, 2, change_resource(u, get_resourcetype(R_AURA), 2)); + CuAssertIntEquals(tc, 3, change_resource(u, get_resourcetype(R_HORSE), 3)); level = eff_spelllevel(u, sp, 3, 1); CuAssertIntEquals(tc, 2, level); pay_spell(u, sp, level, 1); - CuAssertIntEquals(tc, 1, change_resource(u, rt_find("money"), 1)); - CuAssertIntEquals(tc, 3, change_resource(u, rt_find("aura"), 3)); - CuAssertIntEquals(tc, 2, change_resource(u, rt_find("horse"), 1)); + CuAssertIntEquals(tc, 1, change_resource(u, get_resourcetype(R_SILVER), 1)); + CuAssertIntEquals(tc, 3, change_resource(u, get_resourcetype(R_AURA), 3)); + CuAssertIntEquals(tc, 2, change_resource(u, get_resourcetype(R_HORSE), 1)); CuAssertIntEquals(tc, 0, eff_spelllevel(u, sp, 3, 1)); - CuAssertIntEquals(tc, 0, change_resource(u, rt_find("money"), -1)); + CuAssertIntEquals(tc, 0, change_resource(u, get_resourcetype(R_SILVER), -1)); CuAssertIntEquals(tc, 0, eff_spelllevel(u, sp, 2, 1)); } diff --git a/src/kernel/pool.c b/src/kernel/pool.c index bc61c0969..49219127d 100644 --- a/src/kernel/pool.c +++ b/src/kernel/pool.c @@ -53,9 +53,9 @@ int get_resource(const unit * u, const resource_type * rtype) return i; } if (rtype->itype) { - if (rtype == rt_find("stone") && (u_race(u)->flags & RCF_STONEGOLEM)) { + if (rtype == get_resourcetype(R_STONE) && (u_race(u)->flags & RCF_STONEGOLEM)) { return u->number * GOLEM_STONE; - } else if (rtype->itype == it_find("iron") && (u_race(u)->flags & RCF_IRONGOLEM)) { + } else if (rtype == get_resourcetype(R_IRON) && (u_race(u)->flags & RCF_IRONGOLEM)) { return u->number * GOLEM_IRON; } else { return i_get(u->items, rtype->itype); diff --git a/src/tests.test.c b/src/tests.test.c index f6cdca118..df4f076c9 100644 --- a/src/tests.test.c +++ b/src/tests.test.c @@ -14,9 +14,9 @@ static void test_recreate_world(CuTest * tc) test_cleanup(); CuAssertPtrEquals(tc, 0, get_locale("de")); CuAssertPtrEquals(tc, 0, it_find("horse")); - CuAssertPtrNotNull(tc, rt_find("hp")); - CuAssertPtrNotNull(tc, rt_find("permaura")); - CuAssertPtrNotNull(tc, rt_find("aura")); + CuAssertPtrNotNull(tc, get_resourcetype(R_LIFE)); + CuAssertPtrNotNull(tc, get_resourcetype(R_PERMAURA)); + CuAssertPtrNotNull(tc, get_resourcetype(R_AURA)); CuAssertPtrNotNull(tc, it_find("money")); test_create_world(); @@ -24,26 +24,26 @@ static void test_recreate_world(CuTest * tc) CuAssertPtrNotNull(tc, default_locale); CuAssertPtrNotNull(tc, findregion(0, 0)); CuAssertPtrNotNull(tc, it_find("horse")); - CuAssertPtrNotNull(tc, rt_find("horse")); + CuAssertPtrNotNull(tc, get_resourcetype(R_HORSE)); CuAssertPtrNotNull(tc, it_find("money")); - CuAssertPtrNotNull(tc, rt_find("hp")); - CuAssertPtrNotNull(tc, rt_find("money")); - CuAssertPtrNotNull(tc, rt_find("aura")); - CuAssertPtrNotNull(tc, rt_find("permaura")); - CuAssertPtrNotNull(tc, rt_find("peasant")); - CuAssertPtrNotNull(tc, rt_find("unit")); + CuAssertPtrNotNull(tc, get_resourcetype(R_LIFE)); + CuAssertPtrNotNull(tc, get_resourcetype(R_SILVER)); + CuAssertPtrNotNull(tc, get_resourcetype(R_AURA)); + CuAssertPtrNotNull(tc, get_resourcetype(R_PERMAURA)); + CuAssertPtrNotNull(tc, get_resourcetype(R_PEASANT)); + CuAssertPtrNotNull(tc, get_resourcetype(R_UNIT)); test_cleanup(); CuAssertPtrEquals(tc, 0, get_locale("de")); - CuAssertPtrEquals(tc, 0, it_find("horse")); - CuAssertPtrEquals(tc, 0, rt_find("horse")); + CuAssertPtrEquals(tc, 0, (void*)it_find("horse")); + CuAssertPtrEquals(tc, 0, (void*)get_resourcetype(R_HORSE)); CuAssertPtrNotNull(tc, it_find("money")); - CuAssertPtrNotNull(tc, rt_find("hp")); - CuAssertPtrNotNull(tc, rt_find("money")); - CuAssertPtrNotNull(tc, rt_find("aura")); - CuAssertPtrNotNull(tc, rt_find("permaura")); - CuAssertPtrNotNull(tc, rt_find("peasant")); - CuAssertPtrNotNull(tc, rt_find("unit")); + CuAssertPtrNotNull(tc, get_resourcetype(R_LIFE)); + CuAssertPtrNotNull(tc, get_resourcetype(R_SILVER)); + CuAssertPtrNotNull(tc, get_resourcetype(R_AURA)); + CuAssertPtrNotNull(tc, get_resourcetype(R_PERMAURA)); + CuAssertPtrNotNull(tc, get_resourcetype(R_PEASANT)); + CuAssertPtrNotNull(tc, get_resourcetype(R_UNIT)); CuAssertPtrEquals(tc, 0, findregion(0, 0)); }