1 #include <cstddef>
2 #include <functional>
3 #include <list>
4 #include <memory>
5 #include <string>
6 #include <vector>
7 
8 #include "avatar.h"
9 #include "bionics.h"
10 #include "catch/catch.hpp"
11 #include "character.h"
12 #include "character_id.h"
13 #include "character_martial_arts.h"
14 #include "game.h"
15 #include "inventory.h"
16 #include "item.h"
17 #include "item_pocket.h"
18 #include "itype.h"
19 #include "make_static.h"
20 #include "map.h"
21 #include "npc.h"
22 #include "pimpl.h"
23 #include "player.h"
24 #include "player_activity.h"
25 #include "player_helpers.h"
26 #include "point.h"
27 #include "ret_val.h"
28 #include "stomach.h"
29 #include "type_id.h"
30 
get_remaining_charges(const std::string & tool_id)31 int get_remaining_charges( const std::string &tool_id )
32 {
33     const inventory crafting_inv = get_player_character().crafting_inventory();
34     std::vector<const item *> items =
35     crafting_inv.items_with( [tool_id]( const item & i ) {
36         return i.typeId() == itype_id( tool_id );
37     } );
38     int remaining_charges = 0;
39     for( const item *instance : items ) {
40         remaining_charges += instance->ammo_remaining();
41     }
42     return remaining_charges;
43 }
44 
player_has_item_of_type(const std::string & type)45 bool player_has_item_of_type( const std::string &type )
46 {
47     std::vector<item *> matching_items = get_player_character().inv->items_with(
48     [&]( const item & i ) {
49         return i.type->get_id() == itype_id( type );
50     } );
51 
52     return !matching_items.empty();
53 }
54 
clear_character(player & dummy)55 void clear_character( player &dummy )
56 {
57     dummy.set_body();
58     dummy.normalize(); // In particular this clears martial arts style
59 
60     // delete all worn items.
61     dummy.worn.clear();
62     dummy.calc_encumbrance();
63     dummy.inv->clear();
64     dummy.remove_weapon();
65     dummy.clear_mutations();
66 
67     // Clear stomach and then eat a nutritious meal to normalize stomach
68     // contents (needs to happen before clear_morale).
69     dummy.stomach.empty();
70     dummy.guts.empty();
71     item food( "debug_nutrition" );
72     dummy.consume( food );
73 
74     // This sets HP to max, clears addictions and morale,
75     // and sets hunger, thirst, fatigue and such to zero
76     dummy.environmental_revert_effect();
77     // However, the above does not set stored kcal
78     dummy.set_stored_kcal( dummy.get_healthy_kcal() );
79 
80     dummy.empty_skills();
81     dummy.martial_arts_data->clear_styles();
82     dummy.clear_morale();
83     dummy.clear_bionics();
84     dummy.activity.set_to_null();
85     dummy.reset_chargen_attributes();
86     dummy.set_pain( 0 );
87     dummy.reset_bonuses();
88     dummy.set_speed_base( 100 );
89     dummy.set_speed_bonus( 0 );
90     dummy.set_sleep_deprivation( 0 );
91     for( const proficiency_id &prof : dummy.known_proficiencies() ) {
92         dummy.lose_proficiency( prof, true );
93     }
94 
95     // Restore all stamina and go to walk mode
96     dummy.set_stamina( dummy.get_stamina_max() );
97     dummy.set_movement_mode( move_mode_id( "walk" ) );
98     dummy.reset_activity_level();
99 
100     // Make sure we don't carry around weird effects.
101     dummy.clear_effects();
102 
103     // Make stats nominal.
104     dummy.str_max = 8;
105     dummy.dex_max = 8;
106     dummy.int_max = 8;
107     dummy.per_max = 8;
108     dummy.set_str_bonus( 0 );
109     dummy.set_dex_bonus( 0 );
110     dummy.set_int_bonus( 0 );
111     dummy.set_per_bonus( 0 );
112 
113     dummy.cash = 0;
114 
115     const tripoint spot( 60, 60, 0 );
116     dummy.setpos( spot );
117 }
118 
clear_avatar()119 void clear_avatar()
120 {
121     clear_character( get_avatar() );
122     get_avatar().clear_identified();
123 }
124 
process_activity(player & dummy)125 void process_activity( player &dummy )
126 {
127     do {
128         dummy.moves += dummy.get_speed();
129         while( dummy.moves > 0 && dummy.activity ) {
130             dummy.activity.do_turn( dummy );
131         }
132     } while( dummy.activity );
133 }
134 
spawn_npc(const point & p,const std::string & npc_class)135 npc &spawn_npc( const point &p, const std::string &npc_class )
136 {
137     const string_id<npc_template> test_guy( npc_class );
138     const character_id model_id = get_map().place_npc( p, test_guy );
139     g->load_npcs();
140 
141     npc *guy = g->find_npc( model_id );
142     REQUIRE( guy != nullptr );
143     CHECK( !guy->in_vehicle );
144     return *guy;
145 }
146 
give_and_activate_bionic(player & p,bionic_id const & bioid)147 void give_and_activate_bionic( player &p, bionic_id const &bioid )
148 {
149     INFO( "bionic " + bioid.str() + " is valid" );
150     REQUIRE( bioid.is_valid() );
151 
152     p.add_bionic( bioid );
153     INFO( "dummy has gotten " + bioid.str() + " bionic " );
154     REQUIRE( p.has_bionic( bioid ) );
155 
156     // get bionic's index - might not be "last added" due to "integrated" ones
157     int bioindex = -1;
158     for( size_t i = 0; i < p.my_bionics->size(); i++ ) {
159         const auto &bio = ( *p.my_bionics )[ i ];
160         if( bio.id == bioid ) {
161             bioindex = i;
162         }
163     }
164     REQUIRE( bioindex != -1 );
165 
166     const bionic &bio = p.bionic_at_index( bioindex );
167     REQUIRE( bio.id == bioid );
168 
169     // turn on if possible
170     if( bio.id->has_flag( STATIC( json_character_flag( "BIONIC_TOGGLED" ) ) ) && !bio.powered ) {
171         const std::vector<material_id> fuel_opts = bio.info().fuel_opts;
172         if( !fuel_opts.empty() ) {
173             p.set_value( fuel_opts.front().str(), "2" );
174         }
175         p.activate_bionic( bioindex );
176         INFO( "bionic " + bio.id.str() + " with index " + std::to_string( bioindex ) + " is active " );
177         REQUIRE( p.has_active_bionic( bioid ) );
178         if( !fuel_opts.empty() ) {
179             p.remove_value( fuel_opts.front().str() );
180         }
181     }
182 }
183 
tool_with_ammo(const std::string & tool,const int qty)184 item tool_with_ammo( const std::string &tool, const int qty )
185 {
186     item tool_it( tool );
187     if( !tool_it.ammo_default().is_null() ) {
188         tool_it.ammo_set( tool_it.ammo_default(), qty );
189     } else if( !tool_it.magazine_default().is_null() ) {
190         item tool_it_mag( tool_it.magazine_default() );
191         tool_it_mag.ammo_set( tool_it_mag.ammo_default(), qty );
192         tool_it.put_in( tool_it_mag, item_pocket::pocket_type::MAGAZINE_WELL );
193     }
194     return tool_it;
195 }
196