Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iuse_actor::can_use parameter change to Character and dependent functions #34855

Merged
merged 4 commits into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ static const bionic_id bio_armor_head( "bio_armor_head" );
static const bionic_id bio_armor_legs( "bio_armor_legs" );
static const bionic_id bio_armor_torso( "bio_armor_torso" );
static const bionic_id bio_carbon( "bio_carbon" );
static const bionic_id bio_laser( "bio_laser" );
static const bionic_id bio_lighter( "bio_lighter" );
static const bionic_id bio_tools( "bio_tools" );

const efftype_id effect_adrenaline( "adrenaline" );
const efftype_id effect_alarm_clock( "alarm_clock" );
Expand Down Expand Up @@ -5297,3 +5300,62 @@ std::string Character::is_snuggling() const

return "nothing";
}

bool Character::has_item_with_flag( const std::string &flag, bool need_charges ) const
{
return has_item_with( [&flag, &need_charges]( const item & it ) {
if( it.is_tool() && need_charges ) {
return it.has_flag( flag ) && it.type->tool->max_charges ? it.charges > 0 : it.has_flag( flag );
}
return it.has_flag( flag );
} );
}

std::vector<const item *> Character::all_items_with_flag( const std::string &flag ) const
{
return items_with( [&flag]( const item & it ) {
return it.has_flag( flag );
} );
}

bool Character::has_charges( const itype_id &it, int quantity,
const std::function<bool( const item & )> &filter ) const
{
if( it == "fire" || it == "apparatus" ) {
return has_fire( quantity );
}
if( it == "UPS" && is_mounted() &&
mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) ) {
auto mons = mounted_creature.get();
return quantity <= mons->battery_item->ammo_remaining();
}
return charges_of( it, quantity, filter ) == quantity;
}

bool Character::has_fire( const int quantity ) const
{
// TODO: Replace this with a "tool produces fire" flag.

if( g->m.has_nearby_fire( pos() ) ) {
return true;
} else if( has_item_with_flag( "FIRE" ) ) {
return true;
} else if( has_item_with_flag( "FIRESTARTER" ) ) {
auto firestarters = all_items_with_flag( "FIRESTARTER" );
for( auto &i : firestarters ) {
if( has_charges( i->typeId(), quantity ) ) {
return true;
}
}
} else if( has_active_bionic( bio_tools ) && get_power_level() > quantity * 5_kJ ) {
return true;
} else if( has_bionic( bio_lighter ) && get_power_level() > quantity * 5_kJ ) {
return true;
} else if( has_bionic( bio_laser ) && get_power_level() > quantity * 5_kJ ) {
return true;
} else if( is_npc() ) {
// A hack to make NPCs use their Molotovs
return true;
}
return false;
}
12 changes: 12 additions & 0 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,18 @@ class Character : public Creature, public visitable<Character>
void stop_hauling();
bool is_hauling() const;

// Has a weapon, inventory item or worn item with flag
bool has_item_with_flag( const std::string &flag, bool need_charges = false ) const;
/**
* All items that have the given flag (@ref item::has_flag).
*/
std::vector<const item *> all_items_with_flag( const std::string &flag ) const;

bool has_fire( int quantity ) const;

bool has_charges( const itype_id &it, int quantity,
const std::function<bool( const item & )> &filter = return_true<item> ) const;

/** Legacy activity assignment, should not be used where resuming is important. */
void assign_activity( const activity_id &type, int moves = calendar::INDEFINITELY_LONG,
int index = -1, int pos = INT_MIN,
Expand Down
4 changes: 2 additions & 2 deletions src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,14 @@ static int count_charges_in_list( const itype *type, const map_stack &items )
return 0;
}

void inventory::form_from_map( const tripoint &origin, int range, const player *pl,
void inventory::form_from_map( const tripoint &origin, int range, const Character *pl,
bool assign_invlet,
bool clear_path )
{
form_from_map( g->m, origin, range, pl, assign_invlet, clear_path );
}

void inventory::form_from_map( map &m, const tripoint &origin, int range, const player *pl,
void inventory::form_from_map( map &m, const tripoint &origin, int range, const Character *pl,
bool assign_invlet,
bool clear_path )
{
Expand Down
4 changes: 2 additions & 2 deletions src/inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ class inventory : public visitable<inventory>
* the player's worn items / weapon
*/
void restack( player &p );
void form_from_map( const tripoint &origin, int range, const player *pl = nullptr,
void form_from_map( const tripoint &origin, int range, const Character *pl = nullptr,
bool assign_invlet = true,
bool clear_path = true );
void form_from_map( map &m, const tripoint &origin, int range, const player *pl = nullptr,
void form_from_map( map &m, const tripoint &origin, int range, const Character *pl = nullptr,
bool assign_invlet = true,
bool clear_path = true );
/**
Expand Down
2 changes: 1 addition & 1 deletion src/item_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ std::string use_function::get_type() const
}
}

ret_val<bool> iuse_actor::can_use( const player &, const item &, bool, const tripoint & ) const
ret_val<bool> iuse_actor::can_use( const Character &, const item &, bool, const tripoint & ) const
{
return ret_val<bool>::make_success();
}
Expand Down
2 changes: 1 addition & 1 deletion src/iuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9745,7 +9745,7 @@ void use_function::dump_info( const item &it, std::vector<iteminfo> &dump ) cons
}
}

ret_val<bool> use_function::can_call( const player &p, const item &it, bool t,
ret_val<bool> use_function::can_call( const Character &p, const item &it, bool t,
const tripoint &pos ) const
{
if( actor == nullptr ) {
Expand Down
5 changes: 3 additions & 2 deletions src/iuse.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "clone_ptr.h"
#include "units.h"

class Character;
ifreund marked this conversation as resolved.
Show resolved Hide resolved
class item;
class player;
class JsonObject;
Expand Down Expand Up @@ -270,7 +271,7 @@ class iuse_actor
virtual ~iuse_actor() = default;
virtual void load( JsonObject &jo ) = 0;
virtual int use( player &, item &, bool, const tripoint & ) const = 0;
virtual ret_val<bool> can_use( const player &, const item &, bool, const tripoint & ) const;
virtual ret_val<bool> can_use( const Character &, const item &, bool, const tripoint & ) const;
virtual void info( const item &, std::vector<iteminfo> & ) const {}
/**
* Returns a deep copy of this object. Example implementation:
Expand Down Expand Up @@ -308,7 +309,7 @@ struct use_function {
use_function( std::unique_ptr<iuse_actor> f ) : actor( std::move( f ) ) {}

int call( player &, item &, bool, const tripoint & ) const;
ret_val<bool> can_call( const player &, const item &, bool t, const tripoint &pos ) const;
ret_val<bool> can_call( const Character &, const item &, bool t, const tripoint &pos ) const;

iuse_actor *get_actor_ptr() {
return actor.get();
Expand Down
22 changes: 12 additions & 10 deletions src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "bodypart.h"
#include "calendar.h"
#include "cata_utility.h"
#include "character.h"
#include "crafting.h"
#include "creature.h"
#include "debug.h"
Expand Down Expand Up @@ -257,7 +258,7 @@ int iuse_transform::use( player &p, item &it, bool t, const tripoint &pos ) cons
return 0;
}

ret_val<bool> iuse_transform::can_use( const player &p, const item &, bool,
ret_val<bool> iuse_transform::can_use( const Character &p, const item &, bool,
KorGgenT marked this conversation as resolved.
Show resolved Hide resolved
const tripoint & ) const
{
std::map<quality_id, int> unmet_reqs;
Expand Down Expand Up @@ -356,7 +357,7 @@ int countdown_actor::use( player &p, item &it, bool t, const tripoint &pos ) con
return 0;
}

ret_val<bool> countdown_actor::can_use( const player &, const item &it, bool,
ret_val<bool> countdown_actor::can_use( const Character &, const item &it, bool,
const tripoint & ) const
{
if( it.active ) {
Expand Down Expand Up @@ -1269,7 +1270,7 @@ void firestarter_actor::resolve_firestarter_use( player &p, const tripoint &pos
}
}

ret_val<bool> firestarter_actor::can_use( const player &p, const item &it, bool,
ret_val<bool> firestarter_actor::can_use( const Character &p, const item &it, bool,
const tripoint & ) const
{
if( p.is_underwater() ) {
Expand Down Expand Up @@ -1802,7 +1803,7 @@ int cauterize_actor::use( player &p, item &it, bool t, const tripoint & ) const
}
}

ret_val<bool> cauterize_actor::can_use( const player &p, const item &it, bool,
ret_val<bool> cauterize_actor::can_use( const Character &p, const item &it, bool,
const tripoint & ) const
{
if( !p.has_effect( effect_bite ) &&
Expand Down Expand Up @@ -1962,7 +1963,8 @@ int enzlave_actor::use( player &p, item &it, bool t, const tripoint & ) const
return cost >= 0 ? cost : it.ammo_required();
}

ret_val<bool> enzlave_actor::can_use( const player &p, const item &, bool, const tripoint & ) const
ret_val<bool> enzlave_actor::can_use( const Character &p, const item &, bool,
const tripoint & ) const
{
/** @EFFECT_SURVIVAL >=1 allows enzlavement */

Expand Down Expand Up @@ -2030,7 +2032,7 @@ int fireweapon_off_actor::use( player &p, item &it, bool t, const tripoint & ) c
return it.type->charges_to_use();
}

ret_val<bool> fireweapon_off_actor::can_use( const player &p, const item &it, bool,
ret_val<bool> fireweapon_off_actor::can_use( const Character &p, const item &it, bool,
const tripoint & ) const
{
if( it.charges < it.type->charges_to_use() ) {
Expand Down Expand Up @@ -2128,7 +2130,7 @@ int manualnoise_actor::use( player &p, item &it, bool t, const tripoint & ) cons
return it.type->charges_to_use();
}

ret_val<bool> manualnoise_actor::can_use( const player &, const item &it, bool,
ret_val<bool> manualnoise_actor::can_use( const Character &, const item &it, bool,
const tripoint & ) const
{
if( it.charges < it.type->charges_to_use() ) {
Expand Down Expand Up @@ -2268,7 +2270,7 @@ int musical_instrument_actor::use( player &p, item &it, bool t, const tripoint &
return 0;
}

ret_val<bool> musical_instrument_actor::can_use( const player &p, const item &, bool,
ret_val<bool> musical_instrument_actor::can_use( const Character &p, const item &, bool,
const tripoint & ) const
{
// TODO: (maybe): Mouth encumbrance? Smoke? Lack of arms? Hand encumbrance?
Expand Down Expand Up @@ -4088,7 +4090,7 @@ int install_bionic_actor::use( player &p, item &it, bool, const tripoint & ) con
}
}

ret_val<bool> install_bionic_actor::can_use( const player &p, const item &it, bool,
ret_val<bool> install_bionic_actor::can_use( const Character &p, const item &it, bool,
const tripoint & ) const
{
if( !it.is_bionic() ) {
Expand Down Expand Up @@ -4168,7 +4170,7 @@ int detach_gunmods_actor::use( player &p, item &it, bool, const tripoint & ) con
return 0;
}

ret_val<bool> detach_gunmods_actor::can_use( const player &p, const item &it, bool,
ret_val<bool> detach_gunmods_actor::can_use( const Character &p, const item &it, bool,
const tripoint & ) const
{
const auto mods = it.gunmods();
Expand Down
21 changes: 11 additions & 10 deletions src/iuse_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "units.h"
#include "optional.h"

class Character;
ifreund marked this conversation as resolved.
Show resolved Hide resolved
class item;
class player;
struct iteminfo;
Expand Down Expand Up @@ -104,7 +105,7 @@ class iuse_transform : public iuse_actor
~iuse_transform() override = default;
void load( JsonObject &obj ) override;
int use( player &, item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const player &, const item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const Character &, const item &, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
std::string get_name() const override;
void finalize( const itype_id &my_item_type ) override;
Expand All @@ -129,7 +130,7 @@ class countdown_actor : public iuse_actor
void load( JsonObject &obj ) override;
int use( player &, item &, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
ret_val<bool> can_use( const player &, const item &it, bool, const tripoint & ) const override;
ret_val<bool> can_use( const Character &, const item &it, bool, const tripoint & ) const override;
std::string get_name() const override;
void info( const item &, std::vector<iteminfo> & ) const override;
};
Expand Down Expand Up @@ -439,7 +440,7 @@ class firestarter_actor : public iuse_actor
~firestarter_actor() override = default;
void load( JsonObject &obj ) override;
int use( player &, item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const player &, const item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const Character &, const item &, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
};

Expand Down Expand Up @@ -534,7 +535,7 @@ class cauterize_actor : public iuse_actor
~cauterize_actor() override = default;
void load( JsonObject &obj ) override;
int use( player &, item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const player &, const item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const Character &, const item &, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
};

Expand All @@ -549,7 +550,7 @@ class enzlave_actor : public iuse_actor
~enzlave_actor() override = default;
void load( JsonObject &obj ) override;
int use( player &, item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const player &, const item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const Character &, const item &, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
};

Expand All @@ -574,7 +575,7 @@ class fireweapon_off_actor : public iuse_actor
~fireweapon_off_actor() override = default;
void load( JsonObject &obj ) override;
int use( player &, item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const player &, const item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const Character &, const item &, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
};

Expand Down Expand Up @@ -620,7 +621,7 @@ class manualnoise_actor : public iuse_actor
~manualnoise_actor() override = default;
void load( JsonObject &obj ) override;
int use( player &, item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const player &, const item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const Character &, const item &, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
};

Expand Down Expand Up @@ -664,7 +665,7 @@ class musical_instrument_actor : public iuse_actor
~musical_instrument_actor() override = default;
void load( JsonObject &obj ) override;
int use( player &, item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const player &, const item &, bool, const tripoint & ) const override;
ret_val<bool> can_use( const Character &, const item &, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
};

Expand Down Expand Up @@ -1024,7 +1025,7 @@ class install_bionic_actor : public iuse_actor

void load( JsonObject & ) override {}
int use( player &p, item &it, bool t, const tripoint &pnt ) const override;
ret_val<bool> can_use( const player &, const item &it, bool, const tripoint & ) const override;
ret_val<bool> can_use( const Character &, const item &it, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
void finalize( const itype_id &my_item_type ) override;
};
Expand All @@ -1036,7 +1037,7 @@ class detach_gunmods_actor : public iuse_actor

void load( JsonObject & ) override {}
int use( player &p, item &it, bool t, const tripoint &pnt ) const override;
ret_val<bool> can_use( const player &, const item &it, bool, const tripoint & ) const override;
ret_val<bool> can_use( const Character &, const item &it, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
void finalize( const itype_id &my_item_type ) override;
};
Expand Down
Loading