Skip to content

Commit

Permalink
refactor: Decouple attitude from creature (#4069)
Browse files Browse the repository at this point in the history
Decouple attitude from creature
  • Loading branch information
AluminumAlman authored Jan 6, 2024
1 parent 6bff212 commit fd4e650
Show file tree
Hide file tree
Showing 26 changed files with 137 additions and 140 deletions.
6 changes: 3 additions & 3 deletions src/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include "avatar.h"
#include "character.h"
#include "creature.h"
#include "cursesdef.h"
#include "enums.h"
#include "explosion.h"
#include "game.h"
#include "game_constants.h"
Expand Down Expand Up @@ -1038,15 +1038,15 @@ void game::draw_below_override( const tripoint &, const bool )

#if defined(TILES)
void game::draw_monster_override( const tripoint &p, const mtype_id &id, const int count,
const bool more, const Creature::Attitude att )
const bool more, const Attitude att )
{
if( use_tiles ) {
tilecontext->init_draw_monster_override( p, id, count, more, att );
}
}
#else
void game::draw_monster_override( const tripoint &, const mtype_id &, const int,
const bool, const Creature::Attitude )
const bool, const Attitude )
{
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/armor_layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ void show_armor_layers_ui( Character &who )
you.add_msg_if_npc( m_bad, _( "%s is too far to sort armor." ), who.name );
return;
}
if( you.attitude_to( you ) != Creature::A_FRIENDLY ) {
if( you.attitude_to( you ) != Attitude::A_FRIENDLY ) {
you.add_msg_if_npc( m_bad, _( "%s is not friendly!" ), who.name );
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3038,7 +3038,7 @@ bool cata_tiles::draw_critter_at( const tripoint &p, lit_level ll, int &height_3
bool result;
bool is_player;
bool sees_player;
Creature::Attitude attitude;
Attitude attitude;
const auto override = monster_override.find( p );
if( override != monster_override.end() ) {
const mtype_id id = std::get<0>( override->second );
Expand Down Expand Up @@ -3070,7 +3070,7 @@ bool cata_tiles::draw_critter_at( const tripoint &p, lit_level ll, int &height_3
result = false;
sees_player = false;
is_player = false;
attitude = Creature::A_ANY;
attitude = Attitude::A_ANY;
const monster *m = dynamic_cast<const monster *>( &critter );
if( m != nullptr ) {
const auto ent_category = C_MONSTER;
Expand Down Expand Up @@ -3361,7 +3361,7 @@ void cata_tiles::init_draw_below_override( const tripoint &p, const bool draw )
draw_below_override.emplace( p, draw );
}
void cata_tiles::init_draw_monster_override( const tripoint &p, const mtype_id &id, const int count,
const bool more, const Creature::Attitude att )
const bool more, const Attitude att )
{
monster_override.emplace( p, std::make_tuple( id, count, more, att ) );
}
Expand Down
5 changes: 2 additions & 3 deletions src/cata_tiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <vector>

#include "animation.h"
#include "creature.h"
#include "enums.h"
#include "lightmap.h"
#include "line.h"
Expand Down Expand Up @@ -602,7 +601,7 @@ class cata_tiles
void void_draw_below_override();

void init_draw_monster_override( const tripoint &p, const mtype_id &id, int count,
bool more, Creature::Attitude att );
bool more, Attitude att );
void void_monster_override();

bool has_draw_override( const tripoint &p ) const;
Expand Down Expand Up @@ -754,7 +753,7 @@ class cata_tiles
std::map<tripoint, std::tuple<vpart_id, int, units::angle, bool, point>> vpart_override;
std::map<tripoint, bool> draw_below_override;
// int represents spawn count
std::map<tripoint, std::tuple<mtype_id, int, bool, Creature::Attitude>> monster_override;
std::map<tripoint, std::tuple<mtype_id, int, bool, Attitude>> monster_override;
pimpl<std::vector<tile_render_info>> draw_points_cache;

private:
Expand Down
28 changes: 14 additions & 14 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ bool Character::check_mount_will_move( const tripoint &dest_loc )
if( mounted_creature && mounted_creature->type->has_fear_trigger( mon_trigger::HOSTILE_CLOSE ) ) {
for( const monster &critter : g->all_monsters() ) {
Attitude att = critter.attitude_to( *this );
if( att == A_HOSTILE && sees( critter ) && rl_dist( pos(), critter.pos() ) <= 15 &&
if( att == Attitude::A_HOSTILE && sees( critter ) && rl_dist( pos(), critter.pos() ) <= 15 &&
rl_dist( dest_loc, critter.pos() ) < rl_dist( pos(), critter.pos() ) ) {
add_msg_if_player( _( "You fail to budge your %s!" ), mounted_creature->get_name() );
return false;
Expand Down Expand Up @@ -1411,7 +1411,7 @@ bool Character::check_mount_is_spooked()
double chance = 1.0;
Attitude att = critter.attitude_to( *this );
// actually too close now - horse might spook.
if( att == A_HOSTILE && sees( critter ) && rl_dist( pos(), critter.pos() ) <= 10 ) {
if( att == Attitude::A_HOSTILE && sees( critter ) && rl_dist( pos(), critter.pos() ) <= 10 ) {
chance += 10 - rl_dist( pos(), critter.pos() );
if( critter.get_size() >= mount_size ) {
chance *= 2;
Expand Down Expand Up @@ -10801,7 +10801,7 @@ std::vector<Creature *> Character::get_hostile_creatures( int range ) const
// Fixes circular distance range for ranged attacks
float dist_to_creature = std::round( rl_dist_exact( pos(), critter.pos() ) );
return this != &critter && pos() != critter.pos() && // TODO: get rid of fake npcs (pos() check)
dist_to_creature <= range && critter.attitude_to( *this ) == A_HOSTILE
dist_to_creature <= range && critter.attitude_to( *this ) == Attitude::A_HOSTILE
&& sees( critter );
} );
}
Expand Down Expand Up @@ -10947,49 +10947,49 @@ int Character::get_lowest_hp() const
return lowest_hp;
}

Creature::Attitude Character::attitude_to( const Creature &other ) const
Attitude Character::attitude_to( const Creature &other ) const
{
const auto m = dynamic_cast<const monster *>( &other );
if( m != nullptr ) {
if( m->friendly != 0 ) {
return A_FRIENDLY;
return Attitude::A_FRIENDLY;
}
switch( m->attitude( const_cast<Character *>( this ) ) ) {
// player probably does not want to harm them, but doesn't care much at all.
case MATT_FOLLOW:
case MATT_FPASSIVE:
case MATT_IGNORE:
case MATT_FLEE:
return A_NEUTRAL;
return Attitude::A_NEUTRAL;
// player does not want to harm those.
case MATT_FRIEND:
case MATT_ZLAVE:
// Don't want to harm your zlave!
return A_FRIENDLY;
return Attitude::A_FRIENDLY;
case MATT_ATTACK:
return A_HOSTILE;
return Attitude::A_HOSTILE;
case MATT_NULL:
case NUM_MONSTER_ATTITUDES:
break;
}

return A_NEUTRAL;
return Attitude::A_NEUTRAL;
}

const auto p = dynamic_cast<const npc *>( &other );
if( p != nullptr ) {
if( p->is_enemy() ) {
return A_HOSTILE;
return Attitude::A_HOSTILE;
} else if( p->is_player_ally() ) {
return A_FRIENDLY;
return Attitude::A_FRIENDLY;
} else {
return A_NEUTRAL;
return Attitude::A_NEUTRAL;
}
} else if( &other == this ) {
return A_FRIENDLY;
return Attitude::A_FRIENDLY;
}

return A_NEUTRAL;
return Attitude::A_NEUTRAL;
}

bool Character::sees( const tripoint &t, bool, int ) const
Expand Down
3 changes: 1 addition & 2 deletions src/computer_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "character_id.h"
#include "color.h"
#include "coordinate_conversions.h"
#include "creature.h"
#include "debug.h"
#include "enums.h"
#include "event.h"
Expand Down Expand Up @@ -244,7 +243,7 @@ static void remove_submap_turrets()
// Check 1) same overmap coords, 2) turret, 3) hostile
if( ms_to_omt_copy( here.getabs( critter.pos() ) ) == ms_to_omt_copy( here.getabs( g->u.pos() ) ) &&
critter.has_flag( MF_CONSOLE_DESPAWN ) &&
critter.attitude_to( g->u ) == Creature::Attitude::A_HOSTILE ) {
critter.attitude_to( g->u ) == Attitude::A_HOSTILE ) {
g->remove_zombie( critter );
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ Creature *Creature::auto_find_hostile_target( int range, int &boo_hoo, int area
// Helps avoid (possibly expensive) attitude calculation
continue;
}
if( m->attitude_to( u ) == A_HOSTILE ) {
if( m->attitude_to( u ) == Attitude::A_HOSTILE ) {
target_rating = ( mon_rating + hostile_adj ) / dist;
if( maybe_boo ) {
boo_hoo++;
Expand Down Expand Up @@ -2016,11 +2016,11 @@ void Creature::check_dead_state()
std::string Creature::attitude_raw_string( Attitude att )
{
switch( att ) {
case Creature::A_HOSTILE:
case Attitude::A_HOSTILE:
return "hostile";
case Creature::A_NEUTRAL:
case Attitude::A_NEUTRAL:
return "neutral";
case Creature::A_FRIENDLY:
case Attitude::A_FRIENDLY:
return "friendly";
default:
return "other";
Expand Down
14 changes: 0 additions & 14 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,6 @@ class Creature
virtual float dodge_roll() = 0;
virtual float stability_roll() const = 0;

/**
* Simplified attitude towards any creature:
* hostile - hate, want to kill, etc.
* neutral - anything between.
* friendly - avoid harming it, maybe even help.
* any - any of the above, used in safemode_ui
*/
enum Attitude : int {
A_HOSTILE,
A_NEUTRAL,
A_FRIENDLY,
A_ANY
};

/**
* Simplified attitude string for unlocalized needs.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ void editmap::draw_main_ui_overlay()
}
}
// int: count, bool: more than 1 spawn data
std::map<tripoint, std::tuple<mtype_id, int, bool, Creature::Attitude>> spawns;
std::map<tripoint, std::tuple<mtype_id, int, bool, Attitude>> spawns;
for( int x = 0; x < 2; x++ ) {
for( int y = 0; y < 2; y++ ) {
submap *sm = tmpmap.get_submap_at_grid( { x, y, target.z } );
Expand All @@ -663,7 +663,7 @@ void editmap::draw_main_ui_overlay()
const tripoint spawn_p = sm_origin + sp.pos;
const auto spawn_it = spawns.find( spawn_p );
if( spawn_it == spawns.end() ) {
const Creature::Attitude att = sp.friendly ? Creature::A_FRIENDLY : Creature::A_ANY;
const Attitude att = sp.friendly ? Attitude::A_FRIENDLY : Attitude::A_ANY;
spawns.emplace( spawn_p, std::make_tuple( sp.type, sp.count, false, att ) );
} else {
std::get<2>( spawn_it->second ) = true;
Expand Down
14 changes: 14 additions & 0 deletions src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ constexpr inline int sgn( const T x )
return x < 0 ? -1 : ( x > 0 ? 1 : 0 );
}

/**
* Simplified attitude towards any creature:
* hostile - hate, want to kill, etc.
* neutral - anything between.
* friendly - avoid harming it, maybe even help.
* any - any of the above, used in safemode_ui
*/
enum Attitude : int {
A_HOSTILE,
A_NEUTRAL,
A_FRIENDLY,
A_ANY
};

enum class bionic_ui_sort_mode : int {
NONE = 0,
POWER = 1,
Expand Down
14 changes: 7 additions & 7 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3609,7 +3609,7 @@ Creature *game::is_hostile_very_close()
Creature *game::is_hostile_within( int distance )
{
for( auto &critter : u.get_visible_creatures( distance ) ) {
if( u.attitude_to( *critter ) == Creature::A_HOSTILE ) {
if( u.attitude_to( *critter ) == Attitude::A_HOSTILE ) {
return critter;
}
}
Expand Down Expand Up @@ -5528,7 +5528,7 @@ void game::examine( const tripoint &examp )
if( monexamine::pay_bot( *mon ) ) {
return;
}
} else if( mon->attitude_to( u ) == Creature::A_FRIENDLY && !u.is_mounted() ) {
} else if( mon->attitude_to( u ) == Attitude::A_FRIENDLY && !u.is_mounted() ) {
if( monexamine::mfriend_menu( *mon ) ) {
return;
}
Expand Down Expand Up @@ -7824,7 +7824,7 @@ game::vmenu_ret game::list_monsters( const std::vector<Creature *> &monster_list
ctxt.register_action( "HELP_KEYBINDINGS" );

// first integer is the row the attitude category string is printed in the menu
std::map<int, Creature::Attitude> mSortCategory;
std::map<int, Attitude> mSortCategory;

for( int i = 0, last_attitude = -1; i < static_cast<int>( monster_list.size() ); i++ ) {
const auto attitude = monster_list[i]->attitude_to( u );
Expand Down Expand Up @@ -7919,7 +7919,7 @@ game::vmenu_ret game::list_monsters( const std::vector<Creature *> &monster_list
const std::string monName = is_npc ? get_safemode().npc_type_name() : m->name();

std::string sSafemode;
if( get_safemode().has_rule( monName, Creature::A_ANY ) ) {
if( get_safemode().has_rule( monName, Attitude::A_ANY ) ) {
sSafemode = _( "<R>emove from safemode Blacklist" );
} else {
sSafemode = _( "<A>dd to safemode Blacklist" );
Expand Down Expand Up @@ -8027,15 +8027,15 @@ game::vmenu_ret game::list_monsters( const std::vector<Creature *> &monster_list
const auto m = dynamic_cast<monster *>( cCurMon );
const std::string monName = ( m != nullptr ) ? m->name() : "human";

if( get_safemode().has_rule( monName, Creature::A_ANY ) ) {
get_safemode().remove_rule( monName, Creature::A_ANY );
if( get_safemode().has_rule( monName, Attitude::A_ANY ) ) {
get_safemode().remove_rule( monName, Attitude::A_ANY );
}
} else if( action == "SAFEMODE_BLACKLIST_ADD" ) {
if( !get_safemode().empty() ) {
const auto m = dynamic_cast<monster *>( cCurMon );
const std::string monName = ( m != nullptr ) ? m->name() : "human";

get_safemode().add_rule( monName, Creature::A_ANY, get_option<int>( "SAFEMODEPROXIMITY" ),
get_safemode().add_rule( monName, Attitude::A_ANY, get_option<int>( "SAFEMODEPROXIMITY" ),
RULE_BLACKLISTED );
}
} else if( action == "look" ) {
Expand Down
2 changes: 1 addition & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ class game
units::angle veh_dir, bool hilite, point mount );
void draw_below_override( const tripoint &p, bool draw );
void draw_monster_override( const tripoint &p, const mtype_id &id, int count,
bool more, Creature::Attitude att );
bool more, Attitude att );

bool is_in_viewport( const tripoint &p, int margin = 0 ) const;
/**
Expand Down
2 changes: 1 addition & 1 deletion src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2267,7 +2267,7 @@ bool game::handle_action()

case ACTION_WHITELIST_ENEMY:
if( safe_mode == SAFE_MODE_STOP && !get_safemode().empty() ) {
get_safemode().add_rule( get_safemode().lastmon_whitelist, Creature::A_ANY, 0, RULE_WHITELISTED );
get_safemode().add_rule( get_safemode().lastmon_whitelist, Attitude::A_ANY, 0, RULE_WHITELISTED );
add_msg( m_info, _( "Creature whitelisted: %s" ), get_safemode().lastmon_whitelist );
set_safe_mode( SAFE_MODE_ON );
mostseen = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/iexamine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "construction_partial.h"
#include "coordinate_conversions.h"
#include "craft_command.h"
#include "creature.h"
#include "cursesdef.h"
#include "damage.h"
#include "debug.h"
Expand Down Expand Up @@ -937,7 +936,7 @@ void iexamine::cardreader( player &p, const tripoint &examp )
// Check 1) same overmap coords, 2) turret, 3) hostile
if( ms_to_omt_copy( here.getabs( critter.pos() ) ) == ms_to_omt_copy( here.getabs( examp ) ) &&
critter.has_flag( MF_ID_CARD_DESPAWN ) &&
critter.attitude_to( p ) == Creature::Attitude::A_HOSTILE ) {
critter.attitude_to( p ) == Attitude::A_HOSTILE ) {
g->remove_zombie( critter );
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/magic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,9 @@ bool spell::is_valid_target( const Creature &caster, const tripoint &p ) const
{
bool valid = false;
if( Creature *const cr = g->critter_at<Creature>( p ) ) {
Creature::Attitude cr_att = cr->attitude_to( caster );
valid = valid || ( cr_att != Creature::A_FRIENDLY && is_valid_target( target_hostile ) );
valid = valid || ( cr_att == Creature::A_FRIENDLY && is_valid_target( target_ally ) &&
Attitude cr_att = cr->attitude_to( caster );
valid = valid || ( cr_att != Attitude::A_FRIENDLY && is_valid_target( target_hostile ) );
valid = valid || ( cr_att == Attitude::A_FRIENDLY && is_valid_target( target_ally ) &&
p != caster.pos() );
valid = valid || ( is_valid_target( target_self ) && p == caster.pos() );
valid = valid && target_by_monster_id( p );
Expand Down
6 changes: 3 additions & 3 deletions src/magic_spell_effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,9 @@ static void spell_move( const spell &sp, const Creature &caster,

if( can_target_creature ) {
if( Creature *victim = g->critter_at<Creature>( from ) ) {
Creature::Attitude cr_att = victim->attitude_to( get_avatar() );
bool valid = cr_att != Creature::A_FRIENDLY && sp.is_valid_effect_target( target_hostile );
valid |= cr_att == Creature::A_FRIENDLY && sp.is_valid_effect_target( target_ally );
Attitude cr_att = victim->attitude_to( get_avatar() );
bool valid = cr_att != Attitude::A_FRIENDLY && sp.is_valid_effect_target( target_hostile );
valid |= cr_att == Attitude::A_FRIENDLY && sp.is_valid_effect_target( target_ally );
valid |= victim == &caster && sp.is_valid_effect_target( target_self );
if( valid ) {
victim->knock_back_to( to );
Expand Down
Loading

0 comments on commit fd4e650

Please sign in to comment.