Skip to content

Commit df36c79

Browse files
committed
wield unwield test
1 parent ccd6148 commit df36c79

5 files changed

Lines changed: 149 additions & 1 deletion

File tree

TODO

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
bugs
22
----
3-
wield/unwield test
43

54
todo
65
----

src/my_tests.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ auto test_load_projectile_over_water() -> bool;
9494
auto test_load_projectile_rock_turns_to_lava() -> bool;
9595
auto test_load_projectile_teleport() -> bool;
9696
auto test_load_projectile_wall_turns_to_lava() -> bool;
97+
auto test_load_projectile_wield() -> bool;
9798
auto test_load_projectile() -> bool;
9899
auto test_load_save_load() -> bool;
99100
auto test_load_slots() -> bool;

src/test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ static std::initializer_list< std::string > tests = {
116116
"projectile_rock_turns_to_lava",
117117
"projectile_teleport",
118118
"projectile_wall_turns_to_lava",
119+
"projectile_wield",
119120
"projectile",
120121
"save_load",
121122
"slots",

src/tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ auto tests_init() -> bool
9696
if (!test_load_projectile_rock_turns_to_lava()) { return false; }
9797
if (!test_load_projectile_teleport()) { return false; }
9898
if (!test_load_projectile_wall_turns_to_lava()) { return false; }
99+
if (!test_load_projectile_wield()) { return false; }
99100
if (!test_load_projectile()) { return false; }
100101
if (!test_load_save_load()) { return false; }
101102
if (!test_load_slots()) { return false; }
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//
2+
// Copyright goblinhack@gmail.com
3+
//
4+
5+
#include "../my_game.hpp"
6+
#include "../my_level.hpp"
7+
#include "../my_main.hpp"
8+
#include "../my_test.hpp"
9+
10+
[[nodiscard]] static auto test_projectile_wield(Gamep g, Testp t) -> bool
11+
{
12+
TEST_LOG(t, "begin");
13+
TRACE();
14+
15+
LevelNum const level_num = 0;
16+
auto w = 27;
17+
auto h = 7;
18+
19+
//
20+
// How the dungeon starts out, and how we expect it to change
21+
//
22+
std::string const start
23+
= "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
24+
"x.........................x"
25+
"x.........................x"
26+
"x@........................x"
27+
"x.........................x"
28+
"x.........................x"
29+
"xxxxxxxxxxxxxxxxxxxxxxxxxxx";
30+
std::string const expect1
31+
= "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
32+
"x.........................x"
33+
"x.........................x"
34+
"x@........................x"
35+
"x.........................x"
36+
"x.........................x"
37+
"xxxxxxxxxxxxxxxxxxxxxxxxxxx";
38+
39+
Overrides overrides;
40+
Levelp l = nullptr;
41+
Levelsp v = game_test_init(g, &l, level_num, w, h, start.c_str());
42+
bool result = true;
43+
Thingp weapon = nullptr;
44+
Thingp wielding = nullptr;
45+
Tpp weapon_tp = nullptr;
46+
ThingEvent e = {};
47+
48+
auto *player = thing_player(g);
49+
if (player == nullptr) [[unlikely]] {
50+
TEST_FAILED(t, "no player");
51+
goto exit;
52+
}
53+
54+
weapon_tp = tp_find_mand("staff_fire");
55+
if (! weapon_tp) {
56+
TEST_FAILED(t, "no weapon found");
57+
goto exit;
58+
}
59+
60+
weapon = thing_spawn(g, v, l, weapon_tp, thing_at(player));
61+
if (! weapon) {
62+
TEST_FAILED(t, "no weapon spawned");
63+
goto exit;
64+
}
65+
66+
e.reason = "spawned"; //
67+
e.event_type = THING_EVENT_SPAWNED; //
68+
e.source = player; //
69+
70+
if (! thing_carry(g, v, l, player, weapon, e)) {
71+
TEST_FAILED(t, "no weapon carried");
72+
goto exit;
73+
}
74+
75+
level_dump(g, v, l, w, h);
76+
TEST_PROGRESS(t);
77+
78+
//
79+
// Wield and unwield over and over
80+
//
81+
level_dump(g, v, l, w, h);
82+
TEST_PROGRESS(t);
83+
for (auto tries = 0; tries < 20; tries++) {
84+
TEST_LOG(t, "try: %d", tries);
85+
86+
if (! thing_wield(g, v, l, player, weapon, e)) {
87+
TEST_FAILED(t, "failed to wield");
88+
goto exit;
89+
}
90+
91+
wielding = thing_wielding(g, v, l, player);
92+
if (! wielding) {
93+
TEST_FAILED(t, "unexpectedly not wielding a weapon");
94+
goto exit;
95+
}
96+
97+
if (! thing_unwield(g, v, l, player, e)) {
98+
TEST_FAILED(t, "failed to unwield");
99+
goto exit;
100+
}
101+
102+
wielding = thing_wielding(g, v, l, player);
103+
if (wielding) {
104+
thing_log(wielding, "wielding this");
105+
TEST_FAILED(t, "unexpectedly wielding a weapon");
106+
goto exit;
107+
}
108+
109+
TRACE();
110+
TEST_ASSERT(t, game_event_wait(g), "failed to wait");
111+
if (! game_wait_for_tick_to_finish(g, v, l)) {
112+
TEST_FAILED(t, "wait loop failed");
113+
goto exit;
114+
}
115+
}
116+
117+
level_dump(g, v, l, w, h);
118+
TEST_PROGRESS(t);
119+
if (! (result = level_match_contents(g, v, l, t, w, h, expect1.c_str()))) {
120+
TEST_FAILED(t, "unexpected contents");
121+
goto exit;
122+
}
123+
124+
TEST_ASSERT(t, game_tick_get(g, v) == 20, "final tick counter value");
125+
126+
level_dump(g, v, l, w, h);
127+
TEST_PASSED(t);
128+
exit:
129+
TRACE();
130+
game_cleanup(g);
131+
132+
return result;
133+
}
134+
135+
auto test_load_projectile_wield() -> bool // NOLINT
136+
{
137+
TRACE();
138+
139+
Testp test = test_load("projectile_wield");
140+
141+
// begin sort marker1 {
142+
test_callback_set(test, test_projectile_wield);
143+
// end sort marker1 }
144+
145+
return true;
146+
}

0 commit comments

Comments
 (0)