1 #include "map_helpers.h"
2 
3 #include <functional>
4 #include <list>
5 #include <map>
6 #include <memory>
7 #include <utility>
8 #include <vector>
9 
10 #include "cata_assert.h"
11 #include "character.h"
12 #include "clzones.h"
13 #include "faction.h"
14 #include "field.h"
15 #include "game.h"
16 #include "game_constants.h"
17 #include "item.h"
18 #include "item_pocket.h"
19 #include "location.h"
20 #include "map.h"
21 #include "map_iterator.h"
22 #include "mapdata.h"
23 #include "npc.h"
24 #include "point.h"
25 #include "ret_val.h"
26 #include "submap.h"
27 #include "type_id.h"
28 
29 // Remove all vehicles from the map
clear_vehicles()30 void clear_vehicles()
31 {
32     map &here = get_map();
33     for( wrapped_vehicle &veh : here.get_vehicles() ) {
34         here.destroy_vehicle( veh.v );
35     }
36 }
37 
clear_radiation()38 void clear_radiation()
39 {
40     map &here = get_map();
41     const int mapsize = here.getmapsize() * SEEX;
42     for( int z = -1; z <= OVERMAP_HEIGHT; ++z ) {
43         for( int x = 0; x < mapsize; ++x ) {
44             for( int y = 0; y < mapsize; ++y ) {
45                 here.set_radiation( { x, y, z}, 0 );
46             }
47         }
48     }
49 }
50 
wipe_map_terrain()51 void wipe_map_terrain()
52 {
53     map &here = get_map();
54     const int mapsize = here.getmapsize() * SEEX;
55     for( int z = -1; z <= OVERMAP_HEIGHT; ++z ) {
56         ter_id terrain = z == 0 ? t_grass : z < 0 ? t_rock : t_open_air;
57         for( int x = 0; x < mapsize; ++x ) {
58             for( int y = 0; y < mapsize; ++y ) {
59                 here.set( { x, y, z}, terrain, f_null );
60             }
61         }
62     }
63     clear_vehicles();
64     here.invalidate_map_cache( 0 );
65     here.build_map_cache( 0, true );
66 }
67 
clear_creatures()68 void clear_creatures()
69 {
70     // Remove any interfering monsters.
71     g->clear_zombies();
72 }
73 
clear_npcs()74 void clear_npcs()
75 {
76     // Reload to ensure that all active NPCs are in the overmap_buffer.
77     g->reload_npcs();
78     for( npc &n : g->all_npcs() ) {
79         n.die( nullptr );
80     }
81     g->cleanup_dead();
82 }
83 
clear_fields(const int zlevel)84 void clear_fields( const int zlevel )
85 {
86     map &here = get_map();
87     const int mapsize = here.getmapsize() * SEEX;
88     for( int x = 0; x < mapsize; ++x ) {
89         for( int y = 0; y < mapsize; ++y ) {
90             const tripoint p( x, y, zlevel );
91             point offset;
92 
93             submap *sm = here.get_submap_at( p, offset );
94             if( sm ) {
95                 sm->field_count = 0;
96                 sm->get_field( offset ).clear();
97             }
98         }
99     }
100 }
101 
clear_items(const int zlevel)102 void clear_items( const int zlevel )
103 {
104     map &here = get_map();
105     const int mapsize = here.getmapsize() * SEEX;
106     for( int x = 0; x < mapsize; ++x ) {
107         for( int y = 0; y < mapsize; ++y ) {
108             here.i_clear( { x, y, zlevel } );
109         }
110     }
111 }
112 
clear_zones()113 void clear_zones()
114 {
115     zone_manager &zm = zone_manager::get_manager();
116     for( auto zone_ref : zm.get_zones( faction_id( "your_followers" ) ) ) {
117         if( !zone_ref.get().get_is_vehicle() ) {
118             // Trying to delete vehicle zones fails with a message that the zone isn't loaded.
119             // Don't need it right now and the errors spam up the test output, so skip.
120             continue;
121         }
122         zm.remove( zone_ref.get() );
123     }
124 }
125 
clear_map()126 void clear_map()
127 {
128     // Clearing all z-levels is rather slow, so just clear the ones I know the
129     // tests use for now.
130     for( int z = -2; z <= 0; ++z ) {
131         clear_fields( z );
132     }
133     clear_zones();
134     wipe_map_terrain();
135     clear_npcs();
136     clear_creatures();
137     get_map().clear_traps();
138     for( int z = -2; z <= 0; ++z ) {
139         clear_items( z );
140     }
141 }
142 
clear_map_and_put_player_underground()143 void clear_map_and_put_player_underground()
144 {
145     clear_map();
146     // Make sure the player doesn't block the path of the monster being tested.
147     get_player_location().setpos( { 0, 0, -2 } );
148 }
149 
spawn_test_monster(const std::string & monster_type,const tripoint & start)150 monster &spawn_test_monster( const std::string &monster_type, const tripoint &start )
151 {
152     monster *const added = g->place_critter_at( mtype_id( monster_type ), start );
153     cata_assert( added );
154     return *added;
155 }
156 
157 // Build a map of size MAPSIZE_X x MAPSIZE_Y around tripoint_zero with a given
158 // terrain, and no furniture, traps, or items.
build_test_map(const ter_id & terrain)159 void build_test_map( const ter_id &terrain )
160 {
161     map &here = get_map();
162     for( const tripoint &p : here.points_in_rectangle( tripoint_zero,
163             tripoint( MAPSIZE * SEEX, MAPSIZE * SEEY, 0 ) ) ) {
164         here.furn_set( p, furn_id( "f_null" ) );
165         here.ter_set( p, terrain );
166         here.trap_set( p, trap_id( "tr_null" ) );
167         here.i_clear( p );
168     }
169 
170     here.invalidate_map_cache( 0 );
171     here.build_map_cache( 0, true );
172 }
173 
player_add_headlamp()174 void player_add_headlamp()
175 {
176     item headlamp( "wearable_light_on" );
177     item battery( "light_battery_cell" );
178     battery.ammo_set( battery.ammo_default(), -1 );
179     headlamp.put_in( battery, item_pocket::pocket_type::MAGAZINE_WELL );
180     get_player_character().worn.push_back( headlamp );
181 }
182