-
Notifications
You must be signed in to change notification settings - Fork 280
/
monster_test.cpp
356 lines (329 loc) · 14.1 KB
/
monster_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "catch/catch.hpp"
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <memory>
#include <utility>
#include "avatar.h"
#include "game.h"
#include "map.h"
#include "map_helpers.h"
#include "monster.h"
#include "options_helpers.h"
#include "options.h"
#include "player.h"
#include "test_statistics.h"
#include "game_constants.h"
#include "item.h"
#include "line.h"
#include "point.h"
#include "state_helpers.h"
using move_statistics = statistics<int>;
static int moves_to_destination( const std::string &monster_type,
const tripoint &start, const tripoint &end )
{
clear_creatures();
REQUIRE( g->num_creatures() == 1 ); // the player
monster &test_monster = spawn_test_monster( monster_type, start );
// Get it riled up and give it a goal.
test_monster.anger = 100;
test_monster.set_dest( end );
test_monster.set_moves( 0 );
const int monster_speed = test_monster.get_speed();
int moves_spent = 0;
for( int turn = 0; turn < 1000; ++turn ) {
test_monster.mod_moves( monster_speed );
while( test_monster.moves >= 0 ) {
test_monster.anger = 100;
const int moves_before = test_monster.moves;
test_monster.move();
moves_spent += moves_before - test_monster.moves;
if( test_monster.pos() == test_monster.move_target() ) {
g->remove_zombie( test_monster );
return moves_spent;
}
}
}
g->remove_zombie( test_monster );
// Return an unreasonably high number.
return 100000;
}
struct track {
char participant;
int moves;
int distance;
tripoint location;
};
static std::ostream &operator<<( std::ostream &os, track const &value )
{
os << value.participant <<
" l:" << value.location <<
" d:" << value.distance <<
" m:" << value.moves;
return os;
}
static std::ostream &operator<<( std::ostream &os, const std::vector<track> &vec )
{
for( auto &track_instance : vec ) {
os << track_instance << " ";
}
return os;
}
/**
* Simulate a player running from the monster, checking if it can catch up.
**/
static int can_catch_player( const std::string &monster_type, const tripoint &direction_of_flight )
{
clear_map();
REQUIRE( g->num_creatures() == 1 ); // the player
player &test_player = get_avatar();
// Strip off any potentially encumbering clothing.
std::vector<detached_ptr<item>> temp;
while( test_player.takeoff( test_player.i_at( -2 ), &temp ) );
const tripoint center{ 65, 65, 0 };
test_player.setpos( center );
test_player.set_moves( 0 );
// Give the player a head start.
const tripoint monster_start = { -10 * direction_of_flight + test_player.pos()
};
monster &test_monster = spawn_test_monster( monster_type, monster_start );
// Get it riled up and give it a goal.
test_monster.anger = 100;
test_monster.set_dest( test_player.pos() );
test_monster.set_moves( 0 );
const int monster_speed = test_monster.get_speed();
const int target_speed = 100;
std::vector<track> tracker;
for( int turn = 0; turn < 1000; ++turn ) {
test_player.mod_moves( target_speed );
while( test_player.moves >= 0 ) {
test_player.setpos( test_player.pos() + direction_of_flight );
if( test_player.pos().x < SEEX * int( MAPSIZE / 2 ) ||
test_player.pos().y < SEEY * int( MAPSIZE / 2 ) ||
test_player.pos().x >= SEEX * ( 1 + int( MAPSIZE / 2 ) ) ||
test_player.pos().y >= SEEY * ( 1 + int( MAPSIZE / 2 ) ) ) {
tripoint offset = center - test_player.pos();
test_player.setpos( center );
test_monster.setpos( test_monster.pos() + offset );
// Verify that only the player and one monster are present.
REQUIRE( g->num_creatures() == 2 );
}
const int move_cost = get_map().combined_movecost(
test_player.pos(), test_player.pos() + direction_of_flight, nullptr, 0 );
tracker.push_back( {'p', move_cost, rl_dist( test_monster.pos(), test_player.pos() ),
test_player.pos()
} );
test_player.mod_moves( -move_cost );
}
get_map().clear_traps();
test_monster.set_dest( test_player.pos() );
test_monster.mod_moves( monster_speed );
while( test_monster.moves >= 0 ) {
const int moves_before = test_monster.moves;
test_monster.move();
tracker.push_back( {'m', moves_before - test_monster.moves,
rl_dist( test_monster.pos(), test_player.pos() ),
test_monster.pos()
} );
if( rl_dist( test_monster.pos(), test_player.pos() ) == 1 ) {
INFO( tracker );
clear_map();
return turn;
} else if( rl_dist( test_monster.pos(), test_player.pos() ) > 20 ) {
INFO( tracker );
clear_map();
return -turn;
}
}
}
WARN( tracker );
return -1000;
}
// Verify that the named monster has the expected effective speed, not reduced
// due to wasted motion from shambling.
static void check_shamble_speed( const std::string &monster_type, const tripoint &destination )
{
// Scale the scaling factor based on the ratio of diagonal to cardinal steps.
const float slope = get_normalized_angle( point_zero, destination.xy() );
const float diagonal_multiplier = 1.0 + ( get_option<bool>( "CIRCLEDIST" ) ?
( slope * 0.41 ) : 0.0 );
INFO( monster_type << " " << destination );
// Wandering makes things nondeterministic, so look at the distribution rather than a target number.
move_statistics move_stats;
for( int i = 0; i < 10; ++i ) {
move_stats.add( moves_to_destination( monster_type, tripoint_zero, destination ) );
if( ( move_stats.avg() / ( 10000.0 * diagonal_multiplier ) ) ==
Approx( 1.0 ).epsilon( 0.02 ) ) {
break;
}
}
CAPTURE( slope );
CAPTURE( move_stats.avg() );
INFO( diagonal_multiplier );
CHECK( ( move_stats.avg() / ( 10000.0 * diagonal_multiplier ) ) ==
Approx( 1.0 ).epsilon( 0.02 ) );
}
static void test_moves_to_squares( const std::string &monster_type, const bool write_data = false )
{
std::map<int, move_statistics> turns_at_distance;
std::map<int, move_statistics> turns_at_slope;
std::map<int, move_statistics> turns_at_angle;
// For the regression test we want just enough samples, for data we want a lot more.
const int required_samples = write_data ? 100 : 20;
const int sampling_resolution = write_data ? 1 : 20;
bool not_enough_samples = true;
while( not_enough_samples ) {
not_enough_samples = false;
for( int x = 0; x <= 100; x += sampling_resolution ) {
for( int y = 0; y <= 100; y += sampling_resolution ) {
const int distance = square_dist( {50, 50, 0}, {x, y, 0} );
if( distance <= 5 ) {
// Very short ranged tests are squirrely.
continue;
}
const int rise = 50 - y;
const int run = 50 - x;
const float angle = atan2( run, rise );
// Bail out if we already have enough samples for this angle.
if( turns_at_angle[angle * 100].n() >= required_samples ) {
continue;
}
// Scale the scaling factor based on the ratio of diagonal to cardinal steps.
const float slope = get_normalized_angle( {50, 50}, {x, y} );
const float diagonal_multiplier = 1.0 + ( get_option<bool>( "CIRCLEDIST" ) ?
( slope * 0.41 ) : 0.0 );
turns_at_angle[angle * 100].new_type();
turns_at_slope[slope].new_type();
for( int i = 0; i < 5; ++i ) {
const int moves = moves_to_destination( monster_type, {50, 50, 0}, {x, y, 0} );
const int adjusted_moves = moves / ( diagonal_multiplier * distance );
turns_at_distance[distance].add( adjusted_moves );
turns_at_angle[angle * 100].add( adjusted_moves );
turns_at_slope[slope].add( adjusted_moves );
}
if( turns_at_angle[angle * 100].n() < required_samples ) {
not_enough_samples = true;
}
}
}
}
for( const auto &stat_pair : turns_at_distance ) {
INFO( "Monster:" << monster_type << " Dist: " << stat_pair.first << " moves: " <<
stat_pair.second.avg() );
CHECK( stat_pair.second.avg() == Approx( 100.0 ).epsilon( 0.1 ) );
}
for( const auto &stat_pair : turns_at_slope ) {
INFO( "Monster:" << monster_type << " Slope: " << stat_pair.first <<
" moves: " << stat_pair.second.avg() << " types: " << stat_pair.second.types() );
CHECK( stat_pair.second.avg() == Approx( 100.0 ).epsilon( 0.1 ) );
}
for( auto &stat_pair : turns_at_angle ) {
std::stringstream sample_string;
for( auto sample : stat_pair.second.get_samples() ) {
sample_string << sample << ", ";
}
INFO( "Monster:" << monster_type << " Angle: " << stat_pair.first <<
" moves: " << stat_pair.second.avg() << " types: " << stat_pair.second.types() <<
" samples: " << sample_string.str() );
CHECK( stat_pair.second.avg() == Approx( 100.0 ).epsilon( 0.1 ) );
}
if( write_data ) {
std::ofstream data;
data.open( "slope_test_data_" + std::string( ( trigdist ? "trig_" : "square_" ) ) + monster_type );
for( const auto &stat_pair : turns_at_angle ) {
data << stat_pair.first << " " << stat_pair.second.avg() << "\n";
}
data.close();
}
}
static void monster_check()
{
const float diagonal_multiplier = ( get_option<bool>( "CIRCLEDIST" ) ? 1.41 : 1.0 );
// Have a monster walk some distance in a direction and measure how long it takes.
float vert_move = moves_to_destination( "mon_pig", tripoint_zero, {100, 0, 0} );
CHECK( ( vert_move / 10000.0 ) == Approx( 1.0 ) );
int horiz_move = moves_to_destination( "mon_pig", tripoint_zero, {0, 100, 0} );
CHECK( ( horiz_move / 10000.0 ) == Approx( 1.0 ) );
int diag_move = moves_to_destination( "mon_pig", tripoint_zero, {100, 100, 0} );
CHECK( ( diag_move / ( 10000.0 * diagonal_multiplier ) ) == Approx( 1.0 ).epsilon( 0.05 ) );
check_shamble_speed( "mon_pig", {100, 0, 0} );
check_shamble_speed( "mon_pig", {0, 100, 0} );
check_shamble_speed( "mon_pig", {100, 100, 0} );
check_shamble_speed( "mon_zombie", {100, 0, 0} );
check_shamble_speed( "mon_zombie", {0, 100, 0} );
check_shamble_speed( "mon_zombie", {100, 100, 0} );
check_shamble_speed( "mon_zombie_dog", {100, 0, 0} );
check_shamble_speed( "mon_zombie_dog", {0, 100, 0} );
check_shamble_speed( "mon_zombie_dog", {100, 100, 0} );
check_shamble_speed( "mon_zombear", {100, 0, 0} );
check_shamble_speed( "mon_zombear", {0, 100, 0} );
check_shamble_speed( "mon_zombear", {100, 100, 0} );
check_shamble_speed( "mon_jabberwock", {100, 0, 0} );
check_shamble_speed( "mon_jabberwock", {0, 100, 0} );
check_shamble_speed( "mon_jabberwock", {100, 100, 0} );
// Check moves to all squares relative to monster start within 50 squares.
test_moves_to_squares( "mon_zombie_dog" );
test_moves_to_squares( "mon_pig" );
// Verify that a walking player can escape from a zombie, but is caught by a zombie dog.
INFO( "Trigdist is " << ( get_option<bool>( "CIRCLEDIST" ) ? "on" : "off" ) );
CHECK( can_catch_player( "mon_zombie", tripoint_east ) < 0 );
CHECK( can_catch_player( "mon_zombie", tripoint_south_east ) < 0 );
CHECK( can_catch_player( "mon_zombie_dog", tripoint_east ) > 0 );
CHECK( can_catch_player( "mon_zombie_dog", tripoint_south_east ) > 0 );
}
// Write out a map of slope at which monster is moving to time required to reach their destination.
TEST_CASE( "write_slope_to_speed_map_trig", "[.]" )
{
clear_all_state();
put_player_underground();
override_option opt( "CIRCLEDIST", "true" );
trigdist = true;
test_moves_to_squares( "mon_zombie_dog", true );
test_moves_to_squares( "mon_pig", true );
}
TEST_CASE( "write_slope_to_speed_map_square", "[.]" )
{
clear_all_state();
put_player_underground();
override_option opt( "CIRCLEDIST", "false" );
trigdist = false;
test_moves_to_squares( "mon_zombie_dog", true );
test_moves_to_squares( "mon_pig", true );
}
// Characterization test for monster movement speed.
// It's not necessarally the one true speed for monsters, we just want notice if it changes.
TEST_CASE( "monster_speed_square", "[speed]" )
{
clear_all_state();
put_player_underground();
override_option opt( "CIRCLEDIST", "false" );
trigdist = false;
monster_check();
}
// TODO: Figure out why this sometimes fails, seems to be RNG-dependent
TEST_CASE( "monster_speed_trig", "[speed][!mayfail]" )
{
clear_all_state();
put_player_underground();
override_option opt( "CIRCLEDIST", "true" );
trigdist = true;
monster_check();
}
TEST_CASE( "monster_move_through_vehicle_holes" )
{
clear_all_state();
put_player_underground();
tripoint origin( 60, 60, 0 );
get_map().add_vehicle( vproto_id( "apc" ), origin, -45_degrees, 0, 0 );
tripoint mon_origin = origin + tripoint( -2, 1, 0 );
monster &zombie = spawn_test_monster( "mon_zombie", mon_origin );
zombie.move_to( mon_origin + tripoint_north_west, false, false, 0.0f );
const monster *m = g->critter_at<monster>( mon_origin );
CHECK( m != nullptr );
const monster *m2 = g->critter_at<monster>( mon_origin + tripoint_north_west );
CHECK( m2 == nullptr );
}