-
Notifications
You must be signed in to change notification settings - Fork 280
/
map_helpers.cpp
174 lines (154 loc) · 4.3 KB
/
map_helpers.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
#include "map_helpers.h"
#include "catch/catch.hpp"
#include <cassert>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "avatar.h"
#include "calendar.h"
#include "field.h"
#include "game.h"
#include "game_constants.h"
#include "map.h"
#include "mapbuffer.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "npc.h"
#include "overmapbuffer.h"
#include "point.h"
#include "type_id.h"
// Remove all vehicles from the map
void clear_vehicles()
{
for( wrapped_vehicle &veh : g->m.get_vehicles() ) {
g->m.destroy_vehicle( veh.v );
}
}
void wipe_map_terrain()
{
map &here = get_map();
const int mapsize = here.getmapsize() * SEEX;
for( int z = -1; z <= OVERMAP_HEIGHT; ++z ) {
ter_id terrain = z == 0 ? t_grass : z < 0 ? t_rock : t_open_air;
for( int x = 0; x < mapsize; ++x ) {
for( int y = 0; y < mapsize; ++y ) {
g->m.set( { x, y, z}, terrain, f_null );
}
}
}
clear_vehicles();
g->m.invalidate_map_cache( 0 );
g->m.build_map_cache( 0, true );
}
void clear_creatures()
{
// Remove any interfering monsters.
g->clear_zombies();
}
void clear_npcs()
{
// Reload to ensure that all active NPCs are in the overmap_buffer.
g->reload_npcs();
for( npc &n : g->all_npcs() ) {
n.die( nullptr );
}
g->cleanup_dead();
}
void clear_fields( const int zlevel )
{
const int mapsize = g->m.getmapsize() * SEEX;
for( int x = 0; x < mapsize; ++x ) {
for( int y = 0; y < mapsize; ++y ) {
const tripoint p( x, y, zlevel );
std::vector<field_type_id> fields;
for( auto &pr : g->m.field_at( p ) ) {
fields.push_back( pr.second.get_field_type() );
}
for( field_type_id f : fields ) {
g->m.remove_field( p, f );
}
}
}
}
void clear_items( const int zlevel )
{
const int mapsize = g->m.getmapsize() * SEEX;
for( int x = 0; x < mapsize; ++x ) {
for( int y = 0; y < mapsize; ++y ) {
g->m.i_clear( { x, y, zlevel } );
}
}
}
void clear_overmap()
{
MAPBUFFER.clear();
overmap_buffer.clear();
}
void clear_map()
{
// Clearing all z-levels is rather slow, so just clear the ones I know the
// tests use for now.
for( int z = -2; z <= 0; ++z ) {
clear_fields( z );
}
wipe_map_terrain();
clear_npcs();
clear_creatures();
g->m.clear_traps();
for( int z = -2; z <= 0; ++z ) {
clear_items( z );
}
}
void put_player_underground()
{
// Make sure the player doesn't block the path of the monster being tested.
g->u.setpos( { 0, 0, -2 } );
}
monster &spawn_test_monster( const std::string &monster_type, const tripoint &start )
{
monster *const added = g->place_critter_at( mtype_id( monster_type ), start );
REQUIRE( added );
return *added;
}
// Build a map of size MAPSIZE_X x MAPSIZE_Y around tripoint_zero with a given
// terrain, and no furniture, traps, or items.
void build_test_map( const ter_id &terrain )
{
for( const tripoint &p : g->m.points_in_rectangle( tripoint_zero,
tripoint( MAPSIZE * SEEX, MAPSIZE * SEEY, 0 ) ) ) {
g->m.furn_set( p, furn_id( "f_null" ) );
g->m.ter_set( p, terrain );
g->m.trap_set( p, trap_id( "tr_null" ) );
g->m.i_clear( p );
}
g->m.invalidate_map_cache( 0 );
g->m.build_map_cache( 0, true );
}
void build_water_test_map( const ter_id &surface, const ter_id &mid, const ter_id &bottom )
{
constexpr int z_surface = 0;
constexpr int z_bottom = -2;
map &here = get_map();
for( const tripoint &p : here.points_in_rectangle( tripoint_zero,
tripoint( MAPSIZE * SEEX, MAPSIZE * SEEY, z_bottom ) ) ) {
if( p.z == z_surface ) {
here.ter_set( p, surface );
} else if( p.z < z_surface && p.z > z_bottom ) {
here.ter_set( p, mid );
} else if( p.z == z_bottom ) {
here.ter_set( p, bottom );
}
}
here.invalidate_map_cache( 0 );
here.build_map_cache( 0, true );
}
void set_time( const time_point &time )
{
calendar::turn = time;
g->reset_light_level();
int z = g->u.posz();
g->m.update_visibility_cache( z );
g->m.invalidate_map_cache( z );
g->m.build_map_cache( z );
}