| 1 |
#define SDL_MAIN_HANDLED |
| 2 |
#include <cbehave/cbehave.h> |
| 3 |
|
| 4 |
#include <actors.h> |
| 5 |
|
| 6 |
#include <character.h> |
| 7 |
#include <game_events.h> |
| 8 |
#include <handle_game_events.h> |
| 9 |
|
| 10 |
|
| 11 |
FEATURE(pickup_empty, "Pickup into empty slot") |
| 12 |
|
| 13 |
CharacterStoreInit(&gCampaign.Setting.characters); |
| 14 |
Character *c = CharacterStoreAddOther(&gCampaign.Setting.characters); |
| 15 |
CharacterClass cc; |
| 16 |
memset(&cc, 0, sizeof cc); |
| 17 |
c->Class = &cc; |
| 18 |
WeaponClassesInitialize(&gWeaponClasses); |
| 19 |
WeaponClass wc; |
| 20 |
memset(&wc, 0, sizeof wc); |
| 21 |
CSTRDUP(wc.name, "thegun"); |
| 22 |
CArrayPushBack(&gWeaponClasses.Guns, &wc); |
| 23 |
GameEventsInit(&gGameEvents); |
| 24 |
ActorsInit(); |
| 25 |
gConfig = ConfigDefault(); |
| 26 |
int slotStart, slotEnd; |
| 27 |
char buf[256]; |
| 28 |
|
| 29 |
for (int slot = 0; slot < MAX_WEAPONS; slot++) |
| 30 |
{ |
| 31 |
for (wc.Type = GUNTYPE_NORMAL; wc.Type < GUNTYPE_COUNT; wc.Type++) |
| 32 |
{ |
| 33 |
TActor *a; |
| 34 |
GunTypeGetSlotStartEnd(wc.Type, &slotStart, &slotEnd); |
| 35 |
sprintf( |
| 36 |
buf, "Pickup %s weapon while having no weapons and in slot %d", |
| 37 |
GunTypeStr(wc.Type), slot); |
| 38 |
SCENARIO(buf) |
| 39 |
GIVEN("an actor with no weapons") |
| 40 |
GameEvent e = GameEventNewActorAdd(svec2(1, 1), c, NULL); |
| 41 |
e.u.ActorAdd.CharId = 0; |
| 42 |
a = ActorAdd(e.u.ActorAdd); |
| 43 |
sprintf(buf, "holding slot %d", slot); |
| 44 |
AND(buf) |
| 45 |
a->gunIndex = slot; |
| 46 |
sprintf(buf, "I pickup a %s weapon", GunTypeStr(wc.Type)); |
| 47 |
WHEN(buf) |
| 48 |
ActorPickupGun(a, &wc); |
| 49 |
HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL); |
| 50 |
THEN("the weapon should be the picked up weapon") |
| 51 |
SHOULD_STR_EQUAL(a->guns[slotStart].Gun->name, wc.name); |
| 52 |
if (ActorCanSwitchToWeapon(a, slotStart)) |
| 53 |
{ |
| 54 |
AND("the actor should be holding the new weapon") |
| 55 |
SHOULD_INT_EQUAL(a->gunIndex, slotStart); |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
AND("the actor should be holding the same weapon") |
| 60 |
SHOULD_INT_EQUAL(a->gunIndex, slot); |
| 61 |
} |
| 62 |
SCENARIO_END |
| 63 |
ActorDestroy(a); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
GunTypeGetSlotStartEnd(GUNTYPE_NORMAL, &slotStart, &slotEnd); |
| 68 |
WeaponClass wc2; |
| 69 |
memset(&wc2, 0, sizeof wc2); |
| 70 |
CSTRDUP(wc2.name, "othergun"); |
| 71 |
CArrayPushBack(&gWeaponClasses.CustomGuns, &wc2); |
| 72 |
for (int slot = slotStart; slot <= slotEnd; slot++) |
| 73 |
{ |
| 74 |
TActor *a; |
| 75 |
sprintf(buf, "Pickup gun while having an empty slot %d", slot); |
| 76 |
SCENARIO(buf) |
| 77 |
sprintf(buf, "an actor with guns in all slots except %d", slot); |
| 78 |
GIVEN(buf) |
| 79 |
GameEvent e = GameEventNewActorAdd(svec2(1, 1), c, NULL); |
| 80 |
e.u.ActorAdd.CharId = 0; |
| 81 |
a = ActorAdd(e.u.ActorAdd); |
| 82 |
for (int otherSlot = slotStart; otherSlot <= slotEnd; otherSlot++) |
| 83 |
{ |
| 84 |
if (otherSlot != slot) |
| 85 |
{ |
| 86 |
Weapon w = WeaponCreate(&wc2); |
| 87 |
memcpy(&a->guns[otherSlot], &w, sizeof w); |
| 88 |
} |
| 89 |
} |
| 90 |
WHEN("I pickup a gun") |
| 91 |
ActorPickupGun(a, &wc); |
| 92 |
HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL); |
| 93 |
THEN("the actor should be holding the new weapon in the empty slot") |
| 94 |
SHOULD_INT_EQUAL(a->gunIndex, slot); |
| 95 |
AND("the weapon should be the picked up weapon") |
| 96 |
SHOULD_STR_EQUAL(a->guns[slot].Gun->name, wc.name); |
| 97 |
SCENARIO_END |
| 98 |
ActorDestroy(a); |
| 99 |
} |
| 100 |
WeaponClassesClear(&gWeaponClasses.CustomGuns); |
| 101 |
|
| 102 |
WeaponClassesTerminate(&gWeaponClasses); |
| 103 |
ActorsTerminate(); |
| 104 |
GameEventsTerminate(&gGameEvents); |
| 105 |
CharacterStoreTerminate(&gCampaign.Setting.characters); |
| 106 |
FEATURE_END |
| 107 |
|
| 108 |
FEATURE(pickup_existing, "Pickup an existing gun") |
| 109 |
|
| 110 |
CharacterStoreInit(&gCampaign.Setting.characters); |
| 111 |
Character *c = CharacterStoreAddOther(&gCampaign.Setting.characters); |
| 112 |
CharacterClass cc; |
| 113 |
memset(&cc, 0, sizeof cc); |
| 114 |
c->Class = &cc; |
| 115 |
WeaponClassesInitialize(&gWeaponClasses); |
| 116 |
WeaponClass wc; |
| 117 |
memset(&wc, 0, sizeof wc); |
| 118 |
CSTRDUP(wc.name, "thegun"); |
| 119 |
CArrayPushBack(&gWeaponClasses.Guns, &wc); |
| 120 |
GameEventsInit(&gGameEvents); |
| 121 |
ActorsInit(); |
| 122 |
gConfig = ConfigDefault(); |
| 123 |
int slotStart, slotEnd; |
| 124 |
char buf[256]; |
| 125 |
|
| 126 |
for (int slot = 0; slot < MAX_WEAPONS; slot++) |
| 127 |
{ |
| 128 |
for (wc.Type = GUNTYPE_NORMAL; wc.Type < GUNTYPE_COUNT; wc.Type++) |
| 129 |
{ |
| 130 |
TActor *a; |
| 131 |
GunTypeGetSlotStartEnd(wc.Type, &slotStart, &slotEnd); |
| 132 |
sprintf( |
| 133 |
buf, "Pickup %s weapon while already having the weapon in slot %d and holding slot %d", |
| 134 |
GunTypeStr(wc.Type), slotStart, slot); |
| 135 |
SCENARIO(buf) |
| 136 |
sprintf( |
| 137 |
buf, "an actor with a %s weapon in slot %d", GunTypeStr(wc.Type), slot); |
| 138 |
GIVEN(buf) |
| 139 |
GameEvent e = GameEventNewActorAdd(svec2(1, 1), c, NULL); |
| 140 |
e.u.ActorAdd.CharId = 0; |
| 141 |
a = ActorAdd(e.u.ActorAdd); |
| 142 |
Weapon w = WeaponCreate(&wc); |
| 143 |
memcpy(&a->guns[slotStart], &w, sizeof w); |
| 144 |
sprintf(buf, "holding slot %d", slot); |
| 145 |
AND(buf) |
| 146 |
a->gunIndex = slot; |
| 147 |
WHEN("I pickup that same weapon") |
| 148 |
ActorPickupGun(a, &wc); |
| 149 |
HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL); |
| 150 |
THEN("the weapon should be the picked up weapon") |
| 151 |
SHOULD_STR_EQUAL(a->guns[slotStart].Gun->name, wc.name); |
| 152 |
|
| 153 |
if (ActorCanSwitchToWeapon(a, slotStart)) |
| 154 |
{ |
| 155 |
AND("the actor should be holding the weapon") |
| 156 |
SHOULD_INT_EQUAL(a->gunIndex, slotStart); |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
AND("the actor should be holding the same weapon") |
| 161 |
SHOULD_INT_EQUAL(a->gunIndex, slot); |
| 162 |
} |
| 163 |
SCENARIO_END |
| 164 |
ActorDestroy(a); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
WeaponClassesTerminate(&gWeaponClasses); |
| 169 |
ActorsTerminate(); |
| 170 |
GameEventsTerminate(&gGameEvents); |
| 171 |
CharacterStoreTerminate(&gCampaign.Setting.characters); |
| 172 |
FEATURE_END |
| 173 |
|
| 174 |
FEATURE(pickup_new, "Pickup into existing slot") |
| 175 |
|
| 176 |
CharacterStoreInit(&gCampaign.Setting.characters); |
| 177 |
Character *c = CharacterStoreAddOther(&gCampaign.Setting.characters); |
| 178 |
CharacterClass cc; |
| 179 |
memset(&cc, 0, sizeof cc); |
| 180 |
c->Class = &cc; |
| 181 |
WeaponClassesInitialize(&gWeaponClasses); |
| 182 |
WeaponClass wc; |
| 183 |
memset(&wc, 0, sizeof wc); |
| 184 |
CSTRDUP(wc.name, "thegun"); |
| 185 |
CArrayPushBack(&gWeaponClasses.Guns, &wc); |
| 186 |
WeaponClass wc2; |
| 187 |
memset(&wc2, 0, sizeof wc2); |
| 188 |
CSTRDUP(wc2.name, "othergun"); |
| 189 |
CArrayPushBack(&gWeaponClasses.Guns, &wc2); |
| 190 |
GameEventsInit(&gGameEvents); |
| 191 |
ActorsInit(); |
| 192 |
gConfig = ConfigDefault(); |
| 193 |
int slotStart, slotEnd; |
| 194 |
char buf[256]; |
| 195 |
|
| 196 |
for (int slot = 0; slot < MAX_WEAPONS; slot++) |
| 197 |
{ |
| 198 |
for (wc.Type = GUNTYPE_NORMAL; wc.Type < GUNTYPE_COUNT; wc.Type++) |
| 199 |
{ |
| 200 |
TActor *a; |
| 201 |
GunTypeGetSlotStartEnd(wc.Type, &slotStart, &slotEnd); |
| 202 |
const int pickupSlot = |
| 203 |
(slot >= slotStart && slot <= slotEnd) ? slot : slotStart; |
| 204 |
sprintf(buf, "Pickup %s weapon and in slot %d", GunTypeStr(wc.Type), slot); |
| 205 |
SCENARIO(buf) |
| 206 |
GIVEN("an actor with weapons") |
| 207 |
GameEvent e = GameEventNewActorAdd(svec2(1, 1), c, NULL); |
| 208 |
e.u.ActorAdd.CharId = 0; |
| 209 |
a = ActorAdd(e.u.ActorAdd); |
| 210 |
for (int ws = 0; ws < MAX_WEAPONS; ws++) |
| 211 |
{ |
| 212 |
Weapon w = WeaponCreate(&wc2); |
| 213 |
memcpy(&a->guns[ws], &w, sizeof w); |
| 214 |
} |
| 215 |
sprintf(buf, "holding slot %d", slot); |
| 216 |
AND(buf) |
| 217 |
a->gunIndex = slot; |
| 218 |
sprintf(buf, "I pickup a %s weapon", GunTypeStr(wc.Type)); |
| 219 |
WHEN(buf) |
| 220 |
ActorPickupGun(a, &wc); |
| 221 |
HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL); |
| 222 |
THEN("the weapon should be the picked up weapon") |
| 223 |
SHOULD_STR_EQUAL(a->guns[pickupSlot].Gun->name, wc.name); |
| 224 |
|
| 225 |
if (ActorCanSwitchToWeapon(a, pickupSlot)) |
| 226 |
{ |
| 227 |
AND("the actor should be holding the new weapon") |
| 228 |
SHOULD_INT_EQUAL(a->gunIndex, pickupSlot); |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
AND("the actor should be holding the same weapon") |
| 233 |
SHOULD_INT_EQUAL(a->gunIndex, slot); |
| 234 |
} |
| 235 |
SCENARIO_END |
| 236 |
ActorDestroy(a); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
WeaponClassesTerminate(&gWeaponClasses); |
| 241 |
ActorsTerminate(); |
| 242 |
GameEventsTerminate(&gGameEvents); |
| 243 |
CharacterStoreTerminate(&gCampaign.Setting.characters); |
| 244 |
FEATURE_END |
| 245 |
|
| 246 |
CBEHAVE_RUN( |
| 247 |
"Actor features are:", |
| 248 |
TEST_FEATURE(pickup_empty), |
| 249 |
TEST_FEATURE(pickup_existing), |
| 250 |
TEST_FEATURE(pickup_new) |
| 251 |
) |