optimization: caching resource types in get_resourcetype.

eliminate some it_find calls.
eliminate static variables, they make testing hard.
This commit is contained in:
Enno Rehling 2014-06-24 07:42:45 -07:00
parent 98470ac3c3
commit d581737744
18 changed files with 509 additions and 498 deletions

View File

@ -6879,16 +6879,6 @@
this item, use the CLAIM and SHOW commands).</text> this item, use the CLAIM and SHOW commands).</text>
</string> </string>
<string name="grail">
<text locale="de">Gral</text>
<text locale="en">grail</text>
</string>
<string name="grail_p">
<text locale="de">Grale</text>
<text locale="en">grails</text>
</string>
<string name="studypotion"> <string name="studypotion">
<text locale="de">Lerntrank</text> <text locale="de">Lerntrank</text>
<text locale="en">brain boost</text> <text locale="en">brain boost</text>

View File

@ -999,10 +999,12 @@ void add_spende(faction * f1, faction * f2, int amount, region * r)
static bool maintain(building * b, bool first) static bool maintain(building * b, bool first)
/* first==false -> take money from wherever you can */ /* first==false -> take money from wherever you can */
{ {
const resource_type *rsilver = get_resourcetype(R_SILVER);
int c; int c;
region *r = b->region; region *r = b->region;
bool paid = true, work = first; bool paid = true, work = first;
unit *u; unit *u;
if (fval(b, BLD_MAINTAINED) || b->type == NULL || b->type->maintenance == NULL if (fval(b, BLD_MAINTAINED) || b->type == NULL || b->type->maintenance == NULL
|| is_cursed(b->attribs, C_NOCOST, 0)) { || is_cursed(b->attribs, C_NOCOST, 0)) {
fset(b, BLD_MAINTAINED); fset(b, BLD_MAINTAINED);
@ -1094,7 +1096,7 @@ static bool maintain(building * b, bool first)
continue; continue;
cost -= give; cost -= give;
fset(ua->faction, FFL_SELECT); fset(ua->faction, FFL_SELECT);
if (m->rtype == r_silver) if (m->rtype == rsilver)
add_spende(ua->faction, u->faction, give, r); add_spende(ua->faction, u->faction, give, r);
if (cost <= 0) if (cost <= 0)
break; break;
@ -1818,6 +1820,7 @@ const attrib_type at_luxuries = {
static void expandbuying(region * r, request * buyorders) static void expandbuying(region * r, request * buyorders)
{ {
const resource_type *rsilver = get_resourcetype(R_SILVER);
int max_products; int max_products;
unit *u; unit *u;
static struct trade { static struct trade {
@ -1869,7 +1872,7 @@ static void expandbuying(region * r, request * buyorders)
multi = trade->multi; multi = trade->multi;
price = ltype->price * multi; price = ltype->price * multi;
if (get_pooled(oa[j].unit, get_resourcetype(R_SILVER), GET_DEFAULT, if (get_pooled(oa[j].unit, rsilver, GET_DEFAULT,
price) >= price) { price) >= price) {
unit *u = oa[j].unit; unit *u = oa[j].unit;
item *items; item *items;
@ -1886,7 +1889,7 @@ static void expandbuying(region * r, request * buyorders)
i_change(&items, ltype->itype, 1); i_change(&items, ltype->itype, 1);
a->data.v = items; a->data.v = items;
i_change(&oa[j].unit->items, ltype->itype, 1); i_change(&oa[j].unit->items, ltype->itype, 1);
use_pooled(u, get_resourcetype(R_SILVER), GET_DEFAULT, price); use_pooled(u, rsilver, GET_DEFAULT, price);
if (u->n < 0) if (u->n < 0)
u->n = 0; u->n = 0;
u->n += price; u->n += price;
@ -2371,11 +2374,13 @@ static bool sell(unit * u, request ** sellorders, struct order *ord)
static void expandstealing(region * r, request * stealorders) static void expandstealing(region * r, request * stealorders)
{ {
const resource_type *rsilver = get_resourcetype(R_SILVER);
int i; int i;
assert(rsilver);
expandorders(r, stealorders); expandorders(r, stealorders);
if (!norders) if (!norders) return;
return;
/* Für jede unit in der Region wird Geld geklaut, wenn sie Opfer eines /* Für jede unit in der Region wird Geld geklaut, wenn sie Opfer eines
* Beklauen-Orders ist. Jedes Opfer muß einzeln behandelt werden. * Beklauen-Orders ist. Jedes Opfer muß einzeln behandelt werden.
@ -2387,7 +2392,7 @@ static void expandstealing(region * r, request * stealorders)
unit *u = findunitg(oa[i].no, r); unit *u = findunitg(oa[i].no, r);
int n = 0; int n = 0;
if (u && u->region == r) { if (u && u->region == r) {
n = get_pooled(u, r_silver, GET_ALL, INT_MAX); n = get_pooled(u, rsilver, GET_ALL, INT_MAX);
} }
#ifndef GOBLINKILL #ifndef GOBLINKILL
if (oa[i].type.goblin) { /* Goblin-Spezialklau */ if (oa[i].type.goblin) { /* Goblin-Spezialklau */
@ -2409,7 +2414,7 @@ static void expandstealing(region * r, request * stealorders)
} }
if (n > 0) { if (n > 0) {
n = _min(n, oa[i].unit->wants); n = _min(n, oa[i].unit->wants);
use_pooled(u, r_silver, GET_ALL, n); use_pooled(u, rsilver, GET_ALL, n);
oa[i].unit->n = n; oa[i].unit->n = n;
change_money(oa[i].unit, n); change_money(oa[i].unit, n);
ADDMSG(&u->faction->msgs, msg_message("stealeffect", "unit region amount", ADDMSG(&u->faction->msgs, msg_message("stealeffect", "unit region amount",
@ -2426,9 +2431,7 @@ static void plant(region * r, unit * u, int raw)
{ {
int n, i, skill, planted = 0; int n, i, skill, planted = 0;
const item_type *itype; const item_type *itype;
static const resource_type *rt_water = NULL; const resource_type *rt_water = get_resourcetype(R_WATER_OF_LIFE);
if (rt_water == NULL)
rt_water = rt_find("p2");
assert(rt_water != NULL); assert(rt_water != NULL);
if (!fval(r->terrain, LAND_REGION)) { if (!fval(r->terrain, LAND_REGION)) {
@ -2607,22 +2610,24 @@ static void breedhorses(region * r, unit * u)
int n, c, breed = 0; int n, c, breed = 0;
struct building *b = inside_building(u); struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL; const struct building_type *btype = b ? b->type : NULL;
const struct item_type *ihorse = it_find("horse"); const struct resource_type *rhorse = get_resourcetype(R_HORSE);
int horses;
assert(ihorse); assert(rhorse && rhorse->itype);
if (btype != bt_find("stables")) { if (btype != bt_find("stables")) {
cmistake(u, u->thisorder, 122, MSG_PRODUCE); cmistake(u, u->thisorder, 122, MSG_PRODUCE);
return; return;
} }
if (i_get(u->items, ihorse) < 2) { horses = i_get(u->items, rhorse->itype);
if (horses < 2) {
cmistake(u, u->thisorder, 107, MSG_PRODUCE); cmistake(u, u->thisorder, 107, MSG_PRODUCE);
return; return;
} }
n = _min(u->number * eff_skill(u, SK_HORSE_TRAINING, r), i_get(u->items, ihorse)); n = u->number * eff_skill(u, SK_HORSE_TRAINING, r);
n = _min(n, horses);
for (c = 0; c < n; c++) { for (c = 0; c < n; c++) {
if (rng_int() % 100 < eff_skill(u, SK_HORSE_TRAINING, r)) { if (rng_int() % 100 < eff_skill(u, SK_HORSE_TRAINING, r)) {
i_change(&u->items, ihorse, 1); i_change(&u->items, rhorse->itype, 1);
++breed; ++breed;
} }
} }

View File

@ -89,7 +89,7 @@ add_give(unit * u, unit * u2, int given, int received,
static bool limited_give(const item_type * type) static bool limited_give(const item_type * type)
{ {
/* trade only money 2:1, if at all */ /* trade only money 2:1, if at all */
return (type == i_silver); return (type->rtype == get_resourcetype(R_SILVER));
} }
int give_quota(const unit * src, const unit * dst, const item_type * type, int give_quota(const unit * src, const unit * dst, const item_type * type,
@ -426,7 +426,7 @@ void give_unit(unit * u, unit * u2, order * ord)
cmistake(u, ord, 156, MSG_COMMERCE); cmistake(u, ord, 156, MSG_COMMERCE);
return; return;
} }
add_give(u, u2, 1, 1, r_unit, ord, 0); add_give(u, u2, 1, 1, get_resourcetype(R_UNIT), ord, 0);
u_setfaction(u, u2->faction); u_setfaction(u, u2->faction);
u2->faction->newbies += n; u2->faction->newbies += n;
} }

View File

@ -94,14 +94,12 @@ attack_catapult(const troop * at, const struct weapon_type *wtype,
troop dt; troop dt;
int d = 0, enemies; int d = 0, enemies;
weapon *wp = af->person[at->index].missile; weapon *wp = af->person[at->index].missile;
static item_type *it_catapultammo = NULL; item_type *it_catapultammo = NULL;
if (it_catapultammo == NULL) {
it_catapultammo = it_find("catapultammo");
}
assert(wp->type == wtype); assert(wp->type == wtype);
assert(af->person[at->index].reload == 0); assert(af->person[at->index].reload == 0);
it_catapultammo = it_find("catapultammo");
if (it_catapultammo != NULL) { if (it_catapultammo != NULL) {
if (get_pooled(au, it_catapultammo->rtype, if (get_pooled(au, it_catapultammo->rtype,
GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, 1) <= 0) { GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, 1) <= 0) {
@ -158,8 +156,9 @@ attack_catapult(const troop * at, const struct weapon_type *wtype,
} }
} }
if (casualties) if (casualties) {
*casualties = d; *casualties = d;
}
return false; /* keine weitren attacken */ return false; /* keine weitren attacken */
} }

View File

@ -2314,24 +2314,19 @@ static void add_tactics(tactics * ta, fighter * fig, int value)
static double horsebonus(const unit * u) static double horsebonus(const unit * u)
{ {
static const item_type *it_horse = 0; const item_type *it_horse, *it_elvenhorse, *it_charger;
static const item_type *it_elvenhorse = 0;
static const item_type *it_charger = 0;
region *r = u->region;
int n1 = 0, n2 = 0, n3 = 0; int n1 = 0, n2 = 0, n3 = 0;
item *itm = u->items; item *itm;
int skl = eff_skill(u, SK_RIDING, r); int skl = eff_skill(u, SK_RIDING, u->region);
const resource_type *rtype;
if (skl < 1) if (skl < 1) return 0.0;
return 0.0;
if (it_horse == 0) { it_horse = (rtype = get_resourcetype(R_HORSE)) ? rtype->itype : 0;
it_horse = it_find("horse"); it_elvenhorse = (rtype = get_resourcetype(R_UNICORN)) ? rtype->itype : 0;
it_elvenhorse = it_find("elvenhorse"); it_charger = (rtype = get_resourcetype(R_CHARGER)) ? rtype->itype : 0;
it_charger = it_find("charger");
}
for (; itm; itm = itm->next) { for (itm=u->items; itm; itm = itm->next) {
if (itm->type->flags & ITF_ANIMAL) { if (itm->type->flags & ITF_ANIMAL) {
if (itm->type == it_elvenhorse) if (itm->type == it_elvenhorse)
n3 += itm->number; n3 += itm->number;

View File

@ -832,13 +832,6 @@ cansee(const faction * f, const region * r, const unit * u, int modifier)
{ {
int stealth, rings; int stealth, rings;
unit *u2 = r->units; unit *u2 = r->units;
static const item_type *itype_grail;
static bool init;
if (!init) {
init = true;
itype_grail = it_find("grail");
}
if (u->faction == f || omniscient(f)) { if (u->faction == f || omniscient(f)) {
return true; return true;
@ -856,8 +849,6 @@ cansee(const faction * f, const region * r, const unit * u, int modifier)
if (leftship(u)) if (leftship(u))
return true; return true;
if (itype_grail != NULL && i_get(u->items, itype_grail))
return true;
while (u2 && u2->faction != f) while (u2 && u2->faction != f)
u2 = u2->next; u2 = u2->next;

View File

@ -191,18 +191,20 @@ item_type *new_itemtype(resource_type * rtype,
int iflags, int weight, int capacity) int iflags, int weight, int capacity)
{ {
item_type *itype; item_type *itype;
assert(resource2item(rtype) == NULL);
assert(rtype->flags & RTF_ITEM); assert(!rtype->itype);
assert(!rtype->uchange || rtype->uchange==res_changeitem);
itype = calloc(sizeof(item_type), 1); itype = calloc(sizeof(item_type), 1);
itype->rtype = rtype; itype->rtype = rtype;
rtype->itype = itype;
itype->weight = weight; itype->weight = weight;
itype->capacity = capacity; itype->capacity = capacity;
itype->flags |= iflags; itype->flags |= iflags;
it_register(itype); it_register(itype);
rtype->itype = itype;
rtype->flags |= RTF_ITEM;
rtype->uchange = res_changeitem; rtype->uchange = res_changeitem;
return itype; return itype;
@ -295,6 +297,8 @@ potion_type *new_potiontype(item_type * itype, int level)
return ptype; return ptype;
} }
static int num_resources;
void rt_register(resource_type * rtype) void rt_register(resource_type * rtype)
{ {
char buffer[64]; char buffer[64];
@ -304,6 +308,7 @@ void rt_register(resource_type * rtype)
assert(len<sizeof(buffer)-sizeof(rtype)); assert(len<sizeof(buffer)-sizeof(rtype));
len = cb_new_kv(name, len, &rtype, sizeof(rtype), buffer); len = cb_new_kv(name, len, &rtype, sizeof(rtype), buffer);
cb_insert(&cb_resources, buffer, len); cb_insert(&cb_resources, buffer, len);
++num_resources;
} }
const resource_type *item2resource(const item_type * itype) const resource_type *item2resource(const item_type * itype)
@ -602,13 +607,23 @@ const char *itemnames[MAX_RESOURCES] = {
"iron", "stone", "horse", "ao_healing", "iron", "stone", "horse", "ao_healing",
"aots", "roi", "rop", "ao_chastity", "aots", "roi", "rop", "ao_chastity",
"laen", "fairyboot", "aoc", "pegasus", "laen", "fairyboot", "aoc", "pegasus",
"elvenhorse", "dolphin", "roqf", "trollbelt", "elvenhorse", "charger", "dolphin", "roqf", "trollbelt",
"aurafocus", "sphereofinv", "magicbag", "aurafocus", "sphereofinv", "magicbag",
"magicherbbag", "dreameye", "money", "aura", "permaura" "magicherbbag", "dreameye", "p2", "money", "aura", "permaura",
"hp", "unit", "peasant"
}; };
const resource_type *get_resourcetype(resource_t type) { const resource_type *get_resourcetype(resource_t type) {
const resource_type *rtype = rt_find(itemnames[type]); static int update;
static const struct resource_type *rtypes[MAX_RESOURCES];
if (update!=num_resources) {
memset(rtypes, 0, sizeof(rtypes));
update = num_resources;
}
const resource_type *rtype = rtypes[type];
if (!rtype) {
rtype = rtypes[type] = rt_find(itemnames[type]);
}
return rtype; return rtype;
} }
@ -932,14 +947,6 @@ static void init_oldpotions(void)
} }
} }
resource_type *r_silver;
resource_type *r_aura;
resource_type *r_permaura;
resource_type *r_unit;
static resource_type *r_hp;
item_type *i_silver;
static const char *names[] = { static const char *names[] = {
"money", "money_p", "money", "money_p",
"person", "person_p", "person", "person_p",
@ -953,36 +960,59 @@ static const char *names[] = {
void init_resources(void) void init_resources(void)
{ {
resource_type *rtype; resource_type *rtype;
if (r_hp) { item_type *itype;
// we have done this already
return;
}
rtype = rt_find(names[8]);
if (!rtype) {
rtype = new_resourcetype(names + 8, NULL, RTF_NONE); rtype = new_resourcetype(names + 8, NULL, RTF_NONE);
rtype->uchange = res_changepeasants;
rt_register(rtype); rt_register(rtype);
}
rtype->uchange = res_changepeasants;
r_silver = new_resourcetype(&names[0], NULL, RTF_ITEM | RTF_POOLED); // R_SILVER
i_silver = new_itemtype(r_silver, ITF_NONE, 1 /*weight */ , 0); rtype = rt_find(names[0]);
r_silver->uchange = res_changeitem; if (!rtype) {
i_silver->give = give_money; rtype = new_resourcetype(&names[0], NULL, RTF_ITEM | RTF_POOLED);
rt_register(r_silver); rt_register(rtype);
}
rtype->uchange = res_changeitem;
itype = rtype->itype;
if (!itype) {
itype = new_itemtype(rtype, ITF_NONE, 1 /*weight */ , 0);
}
itype->give = give_money;
r_permaura = new_resourcetype(&names[4], NULL, RTF_NONE); // R_PERMAURA
r_permaura->uchange = res_changepermaura; rtype = rt_find(names[4]);
rt_register(r_permaura); if (!rtype) {
rtype = new_resourcetype(&names[4], NULL, RTF_NONE);
rt_register(rtype);
}
rtype->uchange = res_changepermaura;
r_hp = new_resourcetype(&names[6], NULL, RTF_NONE); // R_LIFE
r_hp->uchange = res_changehp; rtype = rt_find(names[6]);
rt_register(r_hp); if (!rtype) {
rtype = new_resourcetype(&names[6], NULL, RTF_NONE);
rt_register(rtype);
}
rtype->uchange = res_changehp;
r_aura = new_resourcetype(&names[10], NULL, RTF_NONE); // R_AURA
r_aura->uchange = res_changeaura; rtype = rt_find(names[10]);
rt_register(r_aura); if (!rtype) {
rtype = new_resourcetype(&names[10], NULL, RTF_NONE);
rt_register(rtype);
}
rtype->uchange = res_changeaura;
r_unit = new_resourcetype(&names[12], NULL, RTF_NONE); // R_UNIT
r_unit->uchange = res_changeperson; rtype = rt_find(names[12]);
rt_register(r_unit); if (!rtype) {
rtype = new_resourcetype(&names[12], NULL, RTF_NONE);
rt_register(rtype);
}
rtype->uchange = res_changeperson;
/* alte typen registrieren: */ /* alte typen registrieren: */
init_oldpotions(); init_oldpotions();
@ -990,40 +1020,45 @@ void init_resources(void)
int get_money(const unit * u) int get_money(const unit * u)
{ {
const struct resource_type *rtype = get_resourcetype(R_SILVER);
const item *i = u->items; const item *i = u->items;
while (i && i->type != i_silver) while (i && i->type->rtype != rtype) {
i = i->next; i = i->next;
if (i == NULL) }
return 0; return i ? i->number : 0;
return i->number;
} }
int set_money(unit * u, int v) int set_money(unit * u, int v)
{ {
const struct resource_type *rtype = get_resourcetype(R_SILVER);
item **ip = &u->items; item **ip = &u->items;
while (*ip && (*ip)->type != i_silver) while (*ip && (*ip)->type->rtype != rtype) {
ip = &(*ip)->next; ip = &(*ip)->next;
}
if ((*ip) == NULL && v) { if ((*ip) == NULL && v) {
i_add(&u->items, i_new(i_silver, v)); i_add(&u->items, i_new(rtype->itype, v));
return v; return v;
} }
if ((*ip) != NULL) { if ((*ip) != NULL) {
if (v) { if (v) {
(*ip)->number = v; (*ip)->number = v;
assert((*ip)->number >= 0); assert((*ip)->number >= 0);
} else } else {
i_remove(ip, *ip); i_remove(ip, *ip);
} }
}
return v; return v;
} }
int change_money(unit * u, int v) int change_money(unit * u, int v)
{ {
const struct resource_type *rtype = get_resourcetype(R_SILVER);
item **ip = &u->items; item **ip = &u->items;
while (*ip && (*ip)->type != i_silver) while (*ip && (*ip)->type->rtype != rtype) {
ip = &(*ip)->next; ip = &(*ip)->next;
}
if ((*ip) == NULL && v) { if ((*ip) == NULL && v) {
i_add(&u->items, i_new(i_silver, v)); i_add(&u->items, i_new(rtype->itype, v));
return v; return v;
} }
if ((*ip) != NULL) { if ((*ip) != NULL) {
@ -1032,9 +1067,10 @@ int change_money(unit * u, int v)
i->number += v; i->number += v;
assert(i->number >= 0); assert(i->number >= 0);
return i->number; return i->number;
} else } else {
i_free(i_remove(ip, *ip)); i_free(i_remove(ip, *ip));
} }
}
return 0; return 0;
} }
@ -1195,9 +1231,7 @@ void test_clear_resources(void)
cb_clear(&cb_items); cb_clear(&cb_items);
cb_foreach(&cb_resources, "", 0, free_rtype_cb, 0); cb_foreach(&cb_resources, "", 0, free_rtype_cb, 0);
cb_clear(&cb_resources); cb_clear(&cb_resources);
++num_resources;
r_hp = r_silver = r_aura = r_permaura = r_unit = 0;
i_silver = 0;
for (i=0; i!=MAXLOCALES; ++i) { for (i=0; i!=MAXLOCALES; ++i) {
cb_clear(inames+i); cb_clear(inames+i);

View File

@ -265,20 +265,12 @@ extern "C" {
double magres, int prot, unsigned int flags); double magres, int prot, unsigned int flags);
extern potion_type *new_potiontype(item_type * itype, int level); extern potion_type *new_potiontype(item_type * itype, int level);
/* for lack of another file: */
/* sonstige resourcen */
extern resource_type *r_silver;
extern resource_type *r_aura;
extern resource_type *r_permaura;
extern resource_type *r_unit;
typedef enum { typedef enum {
/* ITEMS: */ /* ITEMS: */
R_IRON, R_IRON,
R_STONE, R_STONE,
R_HORSE, R_HORSE,
/**/ R_AMULET_OF_HEALING, R_AMULET_OF_HEALING,
R_AMULET_OF_TRUE_SEEING, R_AMULET_OF_TRUE_SEEING,
R_RING_OF_INVISIBILITY, R_RING_OF_INVISIBILITY,
R_RING_OF_POWER, R_RING_OF_POWER,
@ -288,6 +280,7 @@ extern "C" {
R_BIRTHDAYAMULET, R_BIRTHDAYAMULET,
R_PEGASUS, R_PEGASUS,
R_UNICORN, R_UNICORN,
R_CHARGER,
R_DOLPHIN, R_DOLPHIN,
R_RING_OF_NIMBLEFINGER, R_RING_OF_NIMBLEFINGER,
R_TROLLBELT, R_TROLLBELT,
@ -296,11 +289,15 @@ extern "C" {
R_BAG_OF_HOLDING, R_BAG_OF_HOLDING,
R_SACK_OF_CONSERVATION, R_SACK_OF_CONSERVATION,
R_TACTICCRYSTAL, R_TACTICCRYSTAL,
R_WATER_OF_LIFE,
/* SONSTIGE */ /* SONSTIGE */
R_SILVER, R_SILVER,
R_AURA, /* Aura */ R_AURA, /* Aura */
R_PERMAURA, /* Permanente Aura */ R_PERMAURA, /* Permanente Aura */
R_LIFE,
R_UNIT,
R_PEASANT,
MAX_RESOURCES, /* do not use outside item.c ! */ MAX_RESOURCES, /* do not use outside item.c ! */
NORESOURCE = -1 NORESOURCE = -1
@ -326,8 +323,6 @@ extern "C" {
extern void register_item_useonother(int (*foo) (struct unit *, int, extern void register_item_useonother(int (*foo) (struct unit *, int,
const struct item_type *, int, struct order *), const char *name); const struct item_type *, int, struct order *), const char *name);
extern struct item_type *i_silver;
#ifndef DISABLE_TESTS #ifndef DISABLE_TESTS
void test_clear_resources(void); void test_clear_resources(void);
#endif #endif

View File

@ -45,7 +45,7 @@ void test_change_person(CuTest * tc)
test_create_world(); test_create_world();
u = test_create_unit(0, 0); u = test_create_unit(0, 0);
test_uchange(tc, u, r_unit); test_uchange(tc, u, get_resourcetype(R_UNIT));
} }
void test_resource_type(CuTest * tc) void test_resource_type(CuTest * tc)

View File

@ -779,6 +779,7 @@ int spellcost(unit * u, const spell * sp)
{ {
int k, aura = 0; int k, aura = 0;
int count = countspells(u, 0); int count = countspells(u, 0);
const resource_type *r_aura = get_resourcetype(R_AURA);
for (k = 0; sp->components[k].type; k++) { for (k = 0; sp->components[k].type; k++) {
if (sp->components[k].type == r_aura) { if (sp->components[k].type == r_aura) {
@ -826,6 +827,7 @@ static int spl_costtyp(const spell * sp)
* */ * */
int eff_spelllevel(unit * u, const spell * sp, int cast_level, int range) int eff_spelllevel(unit * u, const spell * sp, int cast_level, int range)
{ {
const resource_type *r_aura = get_resourcetype(R_AURA);
int k, maxlevel, needplevel; int k, maxlevel, needplevel;
int costtyp = SPC_FIX; int costtyp = SPC_FIX;
@ -888,6 +890,7 @@ int eff_spelllevel(unit * u, const spell * sp, int cast_level, int range)
*/ */
void pay_spell(unit * u, const spell * sp, int cast_level, int range) void pay_spell(unit * u, const spell * sp, int cast_level, int range)
{ {
const resource_type *r_aura = get_resourcetype(R_AURA);
int k; int k;
int resuse; int resuse;
@ -934,6 +937,7 @@ bool knowsspell(const region * r, const unit * u, const spell * sp)
bool bool
cancast(unit * u, const spell * sp, int level, int range, struct order * ord) cancast(unit * u, const spell * sp, int level, int range, struct order * ord)
{ {
const resource_type *r_aura = get_resourcetype(R_AURA);
int k; int k;
int itemanz; int itemanz;
resource *reslist = NULL; resource *reslist = NULL;

View File

@ -415,15 +415,12 @@ static int canride(unit * u)
int horses = 0, maxhorses, unicorns = 0, maxunicorns; int horses = 0, maxhorses, unicorns = 0, maxunicorns;
int skill = effskill(u, SK_RIDING); int skill = effskill(u, SK_RIDING);
item *itm; item *itm;
static const item_type *it_horse = 0; const item_type *it_horse, *it_elvenhorse, *it_charger;
static const item_type *it_elvenhorse = 0; const resource_type *rtype;
static const item_type *it_charger = 0;
if (it_horse == 0) { it_horse = (rtype = get_resourcetype(R_HORSE)) ? rtype->itype : 0;
it_horse = it_find("horse"); it_elvenhorse = (rtype = get_resourcetype(R_UNICORN)) ? rtype->itype : 0;
it_elvenhorse = it_find("elvenhorse"); it_charger = (rtype = get_resourcetype(R_CHARGER)) ? rtype->itype : 0;
it_charger = it_find("charger");
}
for (itm = u->items; itm; itm = itm->next) { for (itm = u->items; itm; itm = itm->next) {
if (itm->type == it_horse || itm->type == it_charger) { if (itm->type == it_horse || itm->type == it_charger) {
@ -848,7 +845,7 @@ static unit *bewegung_blockiert_von(unit * reisender, region * r)
static double base_prob = -999; static double base_prob = -999;
static double skill_prob = -999; static double skill_prob = -999;
static double amulet_prob = -999; static double amulet_prob = -999;
const struct item_type *iamulet = it_find("aots"); const struct resource_type *ramulet = get_resourcetype(R_AMULET_OF_TRUE_SEEING);
if (gamecookie < 0 || gamecookie != global.cookie) { if (gamecookie < 0 || gamecookie != global.cookie) {
base_prob = base_prob =
@ -876,7 +873,7 @@ static unit *bewegung_blockiert_von(unit * reisender, region * r)
else if (sk >= stealth) { else if (sk >= stealth) {
double prob_u = (sk - stealth) * skill_prob; double prob_u = (sk - stealth) * skill_prob;
/* amulet counts at most once */ /* amulet counts at most once */
prob_u += _min (1, _min(u->number, i_get(u->items, iamulet))) * amulet_prob; prob_u += _min (1, _min(u->number, i_get(u->items, ramulet->itype))) * amulet_prob;
if (prob_u >= prob) { if (prob_u >= prob) {
prob = prob_u; prob = prob_u;
guard = u; guard = u;

View File

@ -1015,11 +1015,11 @@ void region_setresource(region * r, const resource_type * rtype, int value)
rm = rm->next; rm = rm->next;
} }
if (!rm) { if (!rm) {
if (rtype == rt_find("money")) if (rtype == get_resourcetype(R_SILVER))
rsetmoney(r, value); rsetmoney(r, value);
else if (rtype == rt_find("peasant")) else if (rtype == get_resourcetype(R_PEASANT))
rsetpeasants(r, value); rsetpeasants(r, value);
else if (rtype == rt_find("horse")) else if (rtype == get_resourcetype(R_HORSE))
rsethorses(r, value); rsethorses(r, value);
else { else {
int i; int i;
@ -1042,11 +1042,11 @@ int region_getresource(const region * r, const resource_type * rtype)
return rm->amount; return rm->amount;
} }
} }
if (rtype == rt_find("money")) if (rtype == get_resourcetype(R_SILVER))
return rmoney(r); return rmoney(r);
if (rtype == rt_find("horse")) if (rtype == get_resourcetype(R_HORSE))
return rhorses(r); return rhorses(r);
if (rtype == rt_find("peasant")) if (rtype == get_resourcetype(R_PEASANT))
return rpeasants(r); return rpeasants(r);
return 0; return 0;
} }

View File

@ -140,6 +140,8 @@ void
report_item(const unit * owner, const item * i, const faction * viewer, report_item(const unit * owner, const item * i, const faction * viewer,
const char **name, const char **basename, int *number, bool singular) const char **name, const char **basename, int *number, bool singular)
{ {
const resource_type *rsilver = get_resourcetype(R_SILVER);
assert(!owner || owner->number); assert(!owner || owner->number);
if (owner && owner->faction == viewer) { if (owner && owner->faction == viewer) {
if (name) if (name)
@ -150,7 +152,7 @@ report_item(const unit * owner, const item * i, const faction * viewer,
*basename = resourcename(i->type->rtype, 0); *basename = resourcename(i->type->rtype, 0);
if (number) if (number)
*number = i->number; *number = i->number;
} else if (owner && i->type->rtype == r_silver) { } else if (owner && i->type->rtype == rsilver) {
int pp = i->number / owner->number; int pp = i->number / owner->number;
if (number) if (number)
*number = 1; *number = 1;

View File

@ -220,6 +220,7 @@ static buddy *get_friends(const unit * u, int *numfriends)
*/ */
int gift_items(unit * u, int flags) int gift_items(unit * u, int flags)
{ {
const struct resource_type *rsilver = get_resourcetype(R_SILVER);
region *r = u->region; region *r = u->region;
item **itm_p = &u->items; item **itm_p = &u->items;
int retval = 0; int retval = 0;
@ -303,7 +304,7 @@ int gift_items(unit * u, int flags)
if (flags & GIFT_PEASANTS) { if (flags & GIFT_PEASANTS) {
if (!fval(u->region->terrain, SEA_REGION)) { if (!fval(u->region->terrain, SEA_REGION)) {
if (itm->type == i_silver) { if (itm->type->rtype == rsilver) {
rsetmoney(r, rmoney(r) + itm->number); rsetmoney(r, rmoney(r) + itm->number);
itm->number = 0; itm->number = 0;
} }
@ -1221,7 +1222,8 @@ static int item_modification(const unit * u, skill_t sk, int val)
} }
#if NEWATSROI == 1 #if NEWATSROI == 1
if (sk == SK_PERCEPTION) { if (sk == SK_PERCEPTION) {
if (i_get(u->items, it_find("aots")) >= u->number) { const struct resource_type *rtype = get_resourcetype(R_AMULET_OF_TRUE_SEEING);
if (i_get(u->items, rtype->itype) >= u->number) {
val += ATSBONUS; val += ATSBONUS;
} }
} }

View File

@ -292,13 +292,12 @@ void get_food(region * r)
int hungry = u->number; int hungry = u->number;
/* use peasantblood before eating the peasants themselves */ /* use peasantblood before eating the peasants themselves */
static const struct potion_type *pt_blood; const struct potion_type *pt_blood = 0;
if (pt_blood == NULL) {
const item_type *it_blood = it_find("peasantblood"); const item_type *it_blood = it_find("peasantblood");
if (it_blood) if (it_blood) {
pt_blood = it_blood->rtype->ptype; pt_blood = it_blood->rtype->ptype;
} }
if (pt_blood != NULL) { if (pt_blood) {
/* always start with the unit itself, then the first known unit that may have some blood */ /* always start with the unit itself, then the first known unit that may have some blood */
unit *donor = u; unit *donor = u;
while (donor != NULL && hungry > 0) { while (donor != NULL && hungry > 0) {
@ -2694,6 +2693,7 @@ static void reshow(unit * u, struct order *ord, const char *s, param_t p)
int promotion_cmd(unit * u, struct order *ord) int promotion_cmd(unit * u, struct order *ord)
{ {
const struct resource_type *rsilver = get_resourcetype(R_SILVER);
int money, people; int money, people;
if (fval(u, UFL_HERO)) { if (fval(u, UFL_HERO)) {
@ -2713,14 +2713,14 @@ int promotion_cmd(unit * u, struct order *ord)
return 0; return 0;
} }
people = count_all(u->faction) * u->number; people = count_all(u->faction) * u->number;
money = get_pooled(u, i_silver->rtype, GET_ALL, people); money = get_pooled(u, rsilver, GET_ALL, people);
if (people > money) { if (people > money) {
ADDMSG(&u->faction->msgs, ADDMSG(&u->faction->msgs,
msg_feedback(u, ord, "heroes_cost", "cost have", people, money)); msg_feedback(u, ord, "heroes_cost", "cost have", people, money));
return 0; return 0;
} }
use_pooled(u, i_silver->rtype, GET_ALL, people); use_pooled(u, rsilver, GET_ALL, people);
fset(u, UFL_HERO); fset(u, UFL_HERO);
ADDMSG(&u->faction->msgs, msg_message("hero_promotion", "unit cost", ADDMSG(&u->faction->msgs, msg_message("hero_promotion", "unit cost",
u, people)); u, people));
@ -4331,17 +4331,9 @@ int siege_cmd(unit * u, order * ord)
building *b; building *b;
int d, pooled; int d, pooled;
int bewaffnete, katapultiere = 0; int bewaffnete, katapultiere = 0;
static bool init = false; const curse_type *magicwalls_ct;
static const curse_type *magicwalls_ct; item_type *it_catapultammo = NULL;
static item_type *it_catapultammo = NULL; item_type *it_catapult = NULL;
static item_type *it_catapult = NULL;
if (!init) {
init = true;
magicwalls_ct = ct_find("magicwalls");
it_catapultammo = it_find("catapultammo");
it_catapult = it_find("catapult");
}
/* gibt es ueberhaupt Burgen? */
init_tokens(ord); init_tokens(ord);
skip_token(); skip_token();
@ -4359,6 +4351,10 @@ int siege_cmd(unit * u, order * ord)
} }
/* schaden durch katapulte */ /* schaden durch katapulte */
magicwalls_ct = ct_find("magicwalls");
it_catapultammo = it_find("catapultammo");
it_catapult = it_find("catapult");
d = i_get(u->items, it_catapult); d = i_get(u->items, it_catapult);
d = _min(u->number, d); d = _min(u->number, d);
pooled = get_pooled(u, it_catapultammo->rtype, GET_DEFAULT, d); pooled = get_pooled(u, it_catapultammo->rtype, GET_DEFAULT, d);

View File

@ -1450,6 +1450,7 @@ static int buildingmaintenance(const building * b, const resource_type * rtype)
static int static int
report_template(const char *filename, report_context * ctx, const char *charset) report_template(const char *filename, report_context * ctx, const char *charset)
{ {
const resource_type *rsilver = get_resourcetype(R_SILVER);
faction *f = ctx->f; faction *f = ctx->f;
region *r; region *r;
FILE *F = fopen(filename, "wt"); FILE *F = fopen(filename, "wt");
@ -1538,7 +1539,7 @@ report_template(const char *filename, report_context * ctx, const char *charset)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
if (u->building && building_owner(u->building)==u) { if (u->building && building_owner(u->building)==u) {
building *b = u->building; building *b = u->building;
int cost = buildingmaintenance(b, r_silver); int cost = buildingmaintenance(b, rsilver);
if (cost > 0) { if (cost > 0) {
bytes = (int)strlcpy(bufp, ",U", size); bytes = (int)strlcpy(bufp, ",U", size);

View File

@ -4087,9 +4087,9 @@ static int sp_pump(castorder * co)
*/ */
static int sp_seduce(castorder * co) static int sp_seduce(castorder * co)
{ {
item *items = NULL; const resource_type *rsilver = get_resourcetype(R_SILVER);
unit *target; unit *target;
item **itmp; item **itmp, *items = 0;;
unit *mage = co->magician.u; unit *mage = co->magician.u;
spellparameter *pa = co->par; spellparameter *pa = co->par;
int cast_level = co->level; int cast_level = co->level;
@ -4113,7 +4113,7 @@ static int sp_seduce(castorder * co)
while (*itmp) { while (*itmp) {
item *itm = *itmp; item *itm = *itmp;
int loot; int loot;
if (itm->type == i_silver) { if (itm->type->rtype == rsilver) {
loot = loot =
_min(cast_level * 1000, get_money(target) - (maintenance_cost(target))); _min(cast_level * 1000, get_money(target) - (maintenance_cost(target)));
loot = _max(loot, 0); loot = _max(loot, 0);

View File

@ -314,7 +314,7 @@ summary *make_summary(void)
region *r; region *r;
unit *u; unit *u;
summary *s = calloc(1, sizeof(summary)); summary *s = calloc(1, sizeof(summary));
const struct item_type *ihorse = it_find("horse"); const struct resource_type *rhorse = get_resourcetype(R_HORSE);
for (f = factions; f; f = f->next) { for (f = factions; f; f = f->next) {
const struct locale *lang = f->locale; const struct locale *lang = f->locale;
@ -384,7 +384,7 @@ summary *make_summary(void)
if (u->flags & UFL_HERO) { if (u->flags & UFL_HERO) {
s->heroes += u->number; s->heroes += u->number;
} }
s->spielerpferde += i_get(u->items, ihorse); s->spielerpferde += i_get(u->items, rhorse->itype);
s->playermoney += get_money(u); s->playermoney += get_money(u);
s->armed_men += armedmen(u, true); s->armed_men += armedmen(u, true);
for (itm = u->items; itm; itm = itm->next) { for (itm = u->items; itm; itm = itm->next) {
@ -396,7 +396,7 @@ summary *make_summary(void)
} }
} }
s->spielerpferde += i_get(u->items, ihorse); s->spielerpferde += i_get(u->items, rhorse->itype);
for (sv = u->skills; sv != u->skills + u->skill_size; ++sv) { for (sv = u->skills; sv != u->skills + u->skill_size; ++sv) {
skill_t sk = sv->id; skill_t sk = sv->id;