Skip to content

Commit

Permalink
fix a crash caused by new weapons code
Browse files Browse the repository at this point in the history
  • Loading branch information
ennorehling committed Sep 9, 2023
1 parent 6e5a947 commit d639310
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
24 changes: 14 additions & 10 deletions src/battle.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,9 @@ weapon_effskill(troop t, troop enemy, const weapon * w,
fighter *tf = t.fighter;
unit *tu = t.fighter->unit;
/* Alle Modifier berechnen, die fig durch die Waffen bekommt. */
if (w) {
if (w && w->item.type) {
int skill = 0;
const weapon_type *wtype = WEAPON_TYPE(w);
const weapon_type *wtype = resource2weapon(w->item.type->rtype);

if (attacking) {
skill = w->attackskill;
Expand Down Expand Up @@ -1941,9 +1941,12 @@ static int setreload(troop at)
{
fighter *af = at.fighter;
const weapon_type *wtype = WEAPON_TYPE(af->person[at.index].missile);
if (wtype->reload == 0)
return 0;
return af->person[at.index].reload = wtype->reload;
if (wtype) {
if (wtype->reload != 0) {
return af->person[at.index].reload = wtype->reload;
}
}
return 0;
}

int getreload(troop at)
Expand Down Expand Up @@ -3113,7 +3116,7 @@ static void equip_weapons(fighter* fig)
wp = arraddnptr(fig->weapons, 1);
wp->attackskill = weapon_skill(wtype, u, true);
wp->defenseskill = weapon_skill(wtype, u, false);
wp->item = itm;
wp->item.ref = itm;
}
w = arrlen(fig->weapons);
qsort(fig->weapons, w, sizeof(weapon), cmp_weapon);
Expand All @@ -3122,11 +3125,12 @@ static void equip_weapons(fighter* fig)

/* hand out weapons: */
for (i = 0; i != w; ++i) {
const weapon *wp = fig->weapons + i;
const weapon_type *wtype = WEAPON_TYPE(wp);
weapon *wp = fig->weapons + i;
const item *itm = wp->item.ref;
const weapon_type *wtype = resource2weapon(item2resource(itm->type));
bool is_missile;
assert(wtype);

wp->item.type = itm->type;
is_missile = (wtype->flags & WTF_MISSILE);
if (!is_missile && wpless > wp->attackskill + wp->defenseskill) {
/* we fight better with bare hands than this melee weapon */
Expand All @@ -3146,7 +3150,7 @@ static void equip_weapons(fighter* fig)
}
if (wp->attackskill >= 0 || wp->defenseskill >= 0)
{
int count = wp->item->number;
int count = itm->number;
while (count > 0 && (p_missile < fig->alive || p_melee < fig->alive)) {
if (is_missile) {
if (p_missile < fig->alive) {
Expand Down
7 changes: 5 additions & 2 deletions src/battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ typedef struct battle {
} battle;

typedef struct weapon {
const struct item* item;
union {
struct item *ref;
const struct item_type *type;
} item;
int attackskill;
int defenseskill;
} weapon;

#define WEAPON_TYPE(wp) ((wp) ? (wp)->item->type->rtype->wtype : NULL)
#define WEAPON_TYPE(wp) ((wp && (wp)->item.type) ? (wp)->item.type->rtype->wtype : NULL)

typedef struct troop {
struct fighter* fighter;
Expand Down
16 changes: 8 additions & 8 deletions src/battle.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ static void test_select_weapon(CuTest *tc) {
b = make_battle(au->region);
af = make_fighter(b, au, make_side(b, au->faction, 0, 0, 0), false);
CuAssertIntEquals(tc, 3, (int)arrlen(af->weapons));
CuAssertPtrEquals(tc, i_axe, (item *)af->person[0].melee->item);
CuAssertPtrEquals(tc, it_axe, (item_type *)af->person[0].melee->item.type);
CuAssertPtrEquals(tc, NULL, (weapon *)af->person[0].missile);
CuAssertPtrEquals(tc, i_sword, (item *)af->person[1].melee->item);
CuAssertPtrEquals(tc, i_missile, (item *)af->person[1].missile->item);
CuAssertPtrEquals(tc, it_sword, (item_type *)af->person[1].melee->item.type);
CuAssertPtrEquals(tc, it_missile, (item_type *)af->person[1].missile->item.type);
CuAssertPtrEquals(tc, NULL, (weapon *)af->person[2].melee);
CuAssertPtrEquals(tc, i_missile, (item *)af->person[2].missile->item);
CuAssertPtrEquals(tc, it_missile, (item_type *)af->person[2].missile->item.type);
free_battle(b);

test_teardown();
Expand All @@ -154,7 +154,7 @@ static void test_select_weapon_restricted(CuTest *tc) {
b = make_battle(au->region);
af = make_fighter(b, au, make_side(b, au->faction, 0, 0, 0), false);
CuAssertIntEquals(tc, 1, (int)arrlen(af->weapons));
CuAssertPtrEquals(tc, au->items, (void *)af->weapons[0].item);
CuAssertPtrEquals(tc, (item_type *)au->items->type, (item_type *)af->weapons[0].item.type);
CuAssertPtrEquals(tc, af->weapons, (void *)af->person[0].melee);
free_battle(b);

Expand All @@ -163,7 +163,7 @@ static void test_select_weapon_restricted(CuTest *tc) {
b = make_battle(au->region);
af = make_fighter(b, au, make_side(b, au->faction, 0, 0, 0), false);
CuAssertIntEquals(tc, 1, (int)arrlen(af->weapons));
CuAssertPtrEquals(tc, au->items, (void *)af->weapons[0].item);
CuAssertPtrEquals(tc, (item_type *)au->items->type, (item_type *)af->weapons[0].item.type);
CuAssertPtrNotNull(tc, af->person);
CuAssertPtrEquals(tc, NULL, (void *)af->person[0].melee);
free_battle(b);
Expand All @@ -175,7 +175,7 @@ static void test_select_weapon_restricted(CuTest *tc) {
af = make_fighter(b, au, make_side(b, au->faction, 0, 0, 0), false);
CuAssertPtrNotNull(tc, af->weapons);
CuAssertIntEquals(tc, 1, (int)arrlen(af->weapons));
CuAssertPtrEquals(tc, au->items, (void *)af->weapons[0].item);
CuAssertPtrEquals(tc, (item_type *)au->items->type, (item_type *)af->weapons[0].item.type);
CuAssertPtrEquals(tc, af->weapons, (void *)af->person[0].melee);
free_battle(b);

Expand All @@ -185,7 +185,7 @@ static void test_select_weapon_restricted(CuTest *tc) {
b = make_battle(au->region);
af = make_fighter(b, au, make_side(b, au->faction, 0, 0, 0), false);
CuAssertIntEquals(tc, 1, (int)arrlen(af->weapons));
CuAssertPtrEquals(tc, au->items, (void *)af->weapons[0].item);
CuAssertPtrEquals(tc, (item_type *)au->items->type, (void *)af->weapons[0].item.type);
CuAssertPtrEquals(tc, NULL, (void *)af->person[0].melee);
free_battle(b);

Expand Down
4 changes: 2 additions & 2 deletions src/items/weapons.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int *casualties)
message *msg;
for (i = 0; i <= at->index; ++i) {
const weapon *wp = fi->person[i].melee;
if (wp != NULL && wp->item->type == wtype->itype)
if (WEAPON_TYPE(wp) == wtype)
++k;
}
msg = msg_message("useflamingsword", "amount unit", k, fi->unit);
Expand Down Expand Up @@ -103,7 +103,7 @@ int *casualties)
message *msg;

for (i = 0; i <= at->index; ++i) {
if (af->person[i].reload == 0 && af->person[i].missile->item->type == wtype->itype)
if (af->person[i].reload == 0 && WEAPON_TYPE(af->person[i].missile) == wtype)
++k;
}
msg = msg_message("usecatapult", "amount unit", k, au);
Expand Down
7 changes: 4 additions & 3 deletions src/spells/combatspells.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,11 @@ int sp_combatrosthauch(struct castorder * co)
for (w = 0; w != len; ++w) {
weapon *wp = df->weapons;
if (df->unit->items && force > 0) {
item ** itp = i_find(&df->unit->items, wp->item->type);
const item_type *itype = wp->item.type;
item ** itp = i_find(&df->unit->items, itype);
if (*itp) {
item *it = *itp;
requirement *mat = wp->item->type->construction->materials;
requirement *mat = itype->construction->materials;
int n = force;
if (it->number < n) n = it->number;

Expand All @@ -299,7 +300,7 @@ int sp_combatrosthauch(struct castorder * co)
int p;
force -= n;
k += n;
i_change(itp, wp->item->type, -n);
i_change(itp, itype, -n);
for (p = 0; n && p != df->unit->number; ++p) {
if (df->person[p].melee == wp) {
df->person[p].melee = NULL;
Expand Down

0 comments on commit d639310

Please sign in to comment.