390 lines
12 KiB
C++
390 lines
12 KiB
C++
#include <cstddef>
|
|
#include <list>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <set>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "avatar.h"
|
|
#include "bionics.h"
|
|
#include "calendar.h"
|
|
#include "cata_catch.h"
|
|
#include "character.h"
|
|
#include "character_attire.h"
|
|
#include "character_id.h"
|
|
#include "character_martial_arts.h"
|
|
#include "coordinates.h"
|
|
#include "enums.h"
|
|
#include "game.h"
|
|
#include "item.h"
|
|
#include "item_location.h"
|
|
#include "itype.h"
|
|
#include "magic.h"
|
|
#include "map_helpers.h"
|
|
#include "map.h"
|
|
#include "map_iterator.h"
|
|
#include "npc.h"
|
|
#include "perf.h"
|
|
#include "pimpl.h"
|
|
#include "player_activity.h"
|
|
#include "player_helpers.h"
|
|
#include "pocket_type.h"
|
|
#include "point.h"
|
|
#include "profession.h"
|
|
#include "ret_val.h"
|
|
#include "skill.h"
|
|
#include "stomach.h"
|
|
#include "temp_crafting_inventory.h"
|
|
#include "type_id.h"
|
|
#include "value_ptr.h"
|
|
|
|
static const itype_id itype_debug_backpack( "debug_backpack" );
|
|
static const itype_id itype_debug_nutrition( "debug_nutrition" );
|
|
|
|
static const json_character_flag json_flag_BIONIC_TOGGLED( "BIONIC_TOGGLED" );
|
|
|
|
static const move_mode_id move_mode_walk( "walk" );
|
|
|
|
int get_remaining_charges( const itype_id &tool_id )
|
|
{
|
|
const temp_crafting_inventory crafting_inv = get_player_character().crafting_inventory();
|
|
std::vector<const item *> items =
|
|
crafting_inv.items_with( [tool_id]( const item & i ) {
|
|
return i.typeId() == tool_id;
|
|
} );
|
|
int remaining_charges = 0;
|
|
for( const item *instance : items ) {
|
|
remaining_charges += instance->ammo_remaining( );
|
|
}
|
|
return remaining_charges;
|
|
}
|
|
|
|
bool player_has_item_of_type( const itype_id &id )
|
|
{
|
|
std::vector<item *> matching_items = get_player_character().items_with(
|
|
[&]( const item & i ) {
|
|
return i.typeId() == id;
|
|
} );
|
|
|
|
return !matching_items.empty();
|
|
}
|
|
|
|
// Return true if character has an item with get_var( var ) set to the given value
|
|
bool character_has_item_with_var_val( const Character &they, const std::string &var,
|
|
const std::string &val )
|
|
{
|
|
return they.has_item_with( [var, val]( const item & cand ) {
|
|
return cand.get_var( var ) == val;
|
|
} );
|
|
}
|
|
|
|
void clear_character( Character &dummy, bool skip_nutrition )
|
|
{
|
|
map &here = get_map();
|
|
|
|
dummy.set_body();
|
|
dummy.normalize(); // In particular this clears martial arts style
|
|
|
|
// delete all worn items.
|
|
dummy.clear_worn();
|
|
dummy.calc_encumbrance();
|
|
dummy.invalidate_crafting_inventory();
|
|
dummy.remove_weapon();
|
|
dummy.clear_mutations();
|
|
// clear_mutations() removes traits but does not rebuild bodypart topology.
|
|
// Rebuild anatomy now so tests see baseline human limbs (e.g. feet, not talons).
|
|
dummy.set_body();
|
|
dummy.tally_organic_size();
|
|
dummy.mutation_category_level.clear();
|
|
dummy.clear_bionics();
|
|
|
|
// Clear stomach and then eat a nutritious meal to normalize stomach
|
|
// contents (needs to happen before clear_morale).
|
|
dummy.stomach.empty();
|
|
dummy.guts.empty();
|
|
dummy.clear_vitamins();
|
|
dummy.health_tally = 0;
|
|
dummy.update_body( calendar::turn, calendar::turn ); // sets last_updated to current turn
|
|
if( !skip_nutrition ) {
|
|
item food( itype_debug_nutrition );
|
|
dummy.consume( food );
|
|
}
|
|
|
|
dummy.prof = profession::generic();
|
|
dummy.hobbies.clear();
|
|
dummy._skills->clear();
|
|
dummy.martial_arts_data->clear_styles();
|
|
dummy.clear_morale();
|
|
dummy.activity.set_to_null();
|
|
dummy.backlog.clear();
|
|
dummy.clear_destination();
|
|
// Reset age/height before stored kcal since get_healthy_kcal()
|
|
// depends on height.
|
|
dummy.reset_chargen_attributes();
|
|
dummy.environmental_revert_effect();
|
|
dummy.set_stored_kcal( dummy.get_healthy_kcal() );
|
|
|
|
dummy.set_pain( 0 );
|
|
dummy.reset_bonuses();
|
|
dummy.set_speed_base( 100 );
|
|
dummy.set_speed_bonus( 0 );
|
|
dummy.set_sleep_deprivation( 0 );
|
|
dummy.set_moves( 0 );
|
|
dummy.set_trauma( 0 );
|
|
dummy.oxygen = dummy.get_oxygen_max();
|
|
for( const proficiency_id &prof : dummy.known_proficiencies() ) {
|
|
dummy.lose_proficiency( prof, true );
|
|
}
|
|
for( const proficiency_id &prof : dummy.learning_proficiencies() ) {
|
|
dummy.set_proficiency_practiced_time( prof, 0 );
|
|
}
|
|
|
|
// Reset cardio_acc to baseline
|
|
dummy.reset_cardio_acc();
|
|
// Restore all stamina and go to walk mode
|
|
dummy.set_stamina( dummy.get_stamina_max() );
|
|
dummy.set_movement_mode( move_mode_walk );
|
|
dummy.reset_activity_level();
|
|
|
|
// Make sure we don't carry around weird effects.
|
|
dummy.clear_effects();
|
|
dummy.set_dodges_left( dummy.get_num_dodges() );
|
|
dummy.set_underwater( false );
|
|
|
|
// Make stats nominal.
|
|
dummy.set_str_base( 8 );
|
|
dummy.set_dex_base( 8 );
|
|
dummy.set_int_base( 8 );
|
|
dummy.set_per_base( 8 );
|
|
dummy.set_str_bonus( 0 );
|
|
dummy.set_dex_bonus( 0 );
|
|
dummy.set_int_bonus( 0 );
|
|
dummy.set_per_bonus( 0 );
|
|
|
|
dummy.cash = 0;
|
|
|
|
const tripoint_bub_ms spot( 60, 60, 0 );
|
|
dummy.setpos( here, spot );
|
|
dummy.clear_values();
|
|
dummy.magic = pimpl<known_magic>();
|
|
dummy.forget_all_recipes();
|
|
dummy.set_focus( dummy.calc_focus_equilibrium() );
|
|
|
|
// Final stored_kcal sync after all attribute resets.
|
|
dummy.set_stored_kcal( dummy.get_healthy_kcal() );
|
|
// Recompute hp_max now that stats, body, and kcal are all finalized.
|
|
dummy.recalc_hp();
|
|
}
|
|
|
|
void arm_shooter( Character &shooter, const itype_id &gun_type,
|
|
const std::vector<itype_id> &mods,
|
|
const itype_id &ammo_type )
|
|
{
|
|
shooter.remove_weapon();
|
|
// XL so arrows can fit.
|
|
if( !shooter.is_wearing( itype_debug_backpack ) ) {
|
|
shooter.worn.wear_item( shooter, item( itype_debug_backpack ), false, false );
|
|
}
|
|
|
|
const itype_id &gun_id( gun_type );
|
|
// Give shooter a loaded gun of the requested type.
|
|
item_location gun = shooter.i_add( item( gun_id ) );
|
|
itype_id ammo_id = ammo_type;
|
|
// if ammo is not supplied we want the default
|
|
if( ammo_type.is_null() ) {
|
|
if( gun->ammo_default().is_null() ) {
|
|
ammo_id = item( gun->magazine_default() ).ammo_default();
|
|
} else {
|
|
ammo_id = gun->ammo_default();
|
|
}
|
|
} else {
|
|
ammo_id = ammo_type;
|
|
}
|
|
const ammotype &type_of_ammo = item::find_type( ammo_id )->ammo->type;
|
|
if( gun->magazine_integral() ) {
|
|
item_location ammo = shooter.i_add( item( ammo_id, calendar::turn,
|
|
gun->ammo_capacity( type_of_ammo ) ) );
|
|
REQUIRE( gun.can_reload_with( ammo, true ) );
|
|
REQUIRE( shooter.can_reload( *gun, &*ammo ) );
|
|
gun->reload( shooter, ammo, gun->ammo_capacity( type_of_ammo ) );
|
|
} else {
|
|
const itype_id magazine_id = gun->magazine_default();
|
|
item_location magazine = shooter.i_add( item( magazine_id ) );
|
|
item_location ammo = shooter.i_add( item( ammo_id, calendar::turn,
|
|
magazine->ammo_capacity( type_of_ammo ) ) );
|
|
REQUIRE( magazine.can_reload_with( ammo, true ) );
|
|
REQUIRE( shooter.can_reload( *magazine, &*ammo ) );
|
|
magazine->reload( shooter, ammo, magazine->ammo_capacity( type_of_ammo ) );
|
|
gun->reload( shooter, magazine, magazine->ammo_capacity( type_of_ammo ) );
|
|
}
|
|
for( const itype_id &mod : mods ) {
|
|
gun->put_in( item( mod ), pocket_type::MOD );
|
|
}
|
|
shooter.wield( *gun );
|
|
}
|
|
|
|
void clear_avatar()
|
|
{
|
|
avatar &avatar = get_avatar();
|
|
g->clear_kill_tracker();
|
|
clear_character( avatar );
|
|
avatar.grab( object_type::NONE );
|
|
avatar.clear_identified();
|
|
avatar.clear_nutrition();
|
|
avatar.reset_all_missions();
|
|
// Records outlive the items they claim.
|
|
clear_reservations();
|
|
}
|
|
|
|
void equip_shooter( npc &shooter, const std::vector<itype_id> &apparel )
|
|
{
|
|
CHECK( !shooter.in_vehicle );
|
|
shooter.clear_worn();
|
|
for( const itype_id &article : apparel ) {
|
|
shooter.wear_item( item( article ) );
|
|
}
|
|
}
|
|
|
|
void process_activity( Character &dummy, bool pass_time )
|
|
{
|
|
cata_timer process_activity_timer( "process_activity" );
|
|
do {
|
|
dummy.mod_moves( dummy.get_speed() );
|
|
while( dummy.get_moves() > 0 && dummy.activity ) {
|
|
dummy.activity.do_turn( dummy );
|
|
if( pass_time ) {
|
|
calendar::turn += 1_seconds;
|
|
}
|
|
}
|
|
} while( dummy.activity );
|
|
}
|
|
|
|
bool process_activity_bounded( Character &dummy, const int max_turns, const int max_dispatches )
|
|
{
|
|
int turns = 0;
|
|
int dispatches = 0;
|
|
while( dummy.activity && turns < max_turns ) {
|
|
dummy.mod_moves( dummy.get_speed() );
|
|
while( dummy.get_moves() > 0 && dummy.activity ) {
|
|
dummy.activity.do_turn( dummy );
|
|
if( ++dispatches >= max_dispatches ) {
|
|
return false;
|
|
}
|
|
}
|
|
++turns;
|
|
}
|
|
return !dummy.activity;
|
|
}
|
|
|
|
bool process_activity_with_travel( Character &u, const int max_turns )
|
|
{
|
|
map &here = get_map();
|
|
const auto done = [&u]() {
|
|
if( !u.activity.is_null() || u.is_auto_moving() ) {
|
|
return false;
|
|
}
|
|
if( npc *const guy = u.as_npc() ) {
|
|
// NPC ends run-out task on its next turn; do it here
|
|
guy->end_cancelled_task();
|
|
}
|
|
return true;
|
|
};
|
|
for( int turns = 0; turns < max_turns; ++turns ) {
|
|
if( done() ) {
|
|
return true;
|
|
}
|
|
u.set_moves( u.get_speed() );
|
|
if( u.is_auto_moving() ) {
|
|
u.setpos( here, here.get_bub( *u.destination_point ) );
|
|
here.build_map_cache( u.posz() );
|
|
u.start_destination_activity();
|
|
}
|
|
u.activity.do_turn( u );
|
|
}
|
|
return done();
|
|
}
|
|
|
|
npc &spawn_npc( const point_bub_ms &p, const std::string &npc_class )
|
|
{
|
|
map &here = get_map();
|
|
const tripoint_bub_ms at( p, here.get_abs_sub().z() );
|
|
std::vector<tripoint_bub_ms> bare;
|
|
for( const tripoint_bub_ms &q : here.points_in_radius( at, 1 ) ) {
|
|
if( !here.has_items( q ) ) {
|
|
bare.push_back( q );
|
|
}
|
|
}
|
|
const npc_template_id npc_template = npc_template_id( npc_class );
|
|
const character_id model_id = here.place_npc( p, npc_template );
|
|
g->load_npcs();
|
|
// a template's kit spills what its pockets don't hold where the NPC lands
|
|
for( const tripoint_bub_ms &q : bare ) {
|
|
here.i_clear( q );
|
|
}
|
|
|
|
npc *guy = g->find_npc( model_id );
|
|
REQUIRE( guy != nullptr );
|
|
CHECK( !guy->in_vehicle );
|
|
return *guy;
|
|
}
|
|
|
|
// Clear player traits and give them a single trait by name
|
|
void set_single_trait( Character &dummy, const std::string &trait_name )
|
|
{
|
|
dummy.clear_mutations();
|
|
dummy.toggle_trait( trait_id( trait_name ) );
|
|
REQUIRE( dummy.has_trait( trait_id( trait_name ) ) );
|
|
}
|
|
|
|
void give_and_activate_bionic( Character &you, bionic_id const &bioid )
|
|
{
|
|
INFO( "bionic " + bioid.str() + " is valid" );
|
|
REQUIRE( bioid.is_valid() );
|
|
|
|
you.add_bionic( bioid );
|
|
INFO( "dummy has gotten " + bioid.str() + " bionic " );
|
|
REQUIRE( you.has_bionic( bioid ) );
|
|
|
|
// get bionic's index - might not be "last added" due to "integrated" ones
|
|
int bioindex = -1;
|
|
for( size_t i = 0; i < you.my_bionics->size(); i++ ) {
|
|
const bionic &bio = ( *you.my_bionics )[ i ];
|
|
if( bio.id == bioid ) {
|
|
bioindex = i;
|
|
}
|
|
}
|
|
REQUIRE( bioindex != -1 );
|
|
|
|
bionic &bio = you.bionic_at_index( bioindex );
|
|
REQUIRE( bio.id == bioid );
|
|
|
|
// turn on if possible
|
|
if( bio.id->has_flag( json_flag_BIONIC_TOGGLED ) && !bio.powered ) {
|
|
const std::vector<material_id> fuel_opts = bio.info().fuel_opts;
|
|
if( !fuel_opts.empty() ) {
|
|
you.set_value( fuel_opts.front().str(), "2" );
|
|
}
|
|
you.activate_bionic( bio );
|
|
INFO( "bionic " + bio.id.str() + " with index " + std::to_string( bioindex ) + " is active " );
|
|
REQUIRE( you.has_active_bionic( bioid ) );
|
|
if( !fuel_opts.empty() ) {
|
|
you.remove_value( fuel_opts.front().str() );
|
|
}
|
|
}
|
|
}
|
|
|
|
item tool_with_ammo( const itype_id &tool, const int qty )
|
|
{
|
|
item tool_it( tool );
|
|
if( !tool_it.ammo_default().is_null() ) {
|
|
tool_it.ammo_set( tool_it.ammo_default(), qty );
|
|
} else if( !tool_it.magazine_default().is_null() ) {
|
|
item tool_it_mag( tool_it.magazine_default() );
|
|
tool_it_mag.ammo_set( tool_it_mag.ammo_default(), qty );
|
|
tool_it.put_in( tool_it_mag, pocket_type::MAGAZINE_WELL );
|
|
}
|
|
return tool_it;
|
|
}
|