Skip to content

Commit

Permalink
Make object_type a class enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhilkinSerg committed May 29, 2020
1 parent 4a062fe commit c10eae0
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ static int move_cost_cart( const item &it, const tripoint &src, const tripoint &

static int move_cost( const item &it, const tripoint &src, const tripoint &dest )
{
if( g->u.get_grab_type() == OBJECT_VEHICLE ) {
if( g->u.get_grab_type() == object_type::VEHICLE ) {
tripoint cart_position = g->u.pos() + g->u.grab_point;

if( const cata::optional<vpart_reference> vp = g->m.veh_at(
Expand Down
2 changes: 1 addition & 1 deletion src/advanced_inv_area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void advanced_inv_area::init()
canputitemsloc = true;
break;
case AIM_DRAGGED:
if( g->u.get_grab_type() != OBJECT_VEHICLE ) {
if( g->u.get_grab_type() != object_type::VEHICLE ) {
canputitemsloc = false;
desc[0] = _( "Not dragging any vehicle!" );
break;
Expand Down
4 changes: 2 additions & 2 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ avatar::avatar()
{
show_map_memory = true;
active_mission = nullptr;
grab_type = OBJECT_NONE;
grab_type = object_type::NONE;
}

void avatar::toggle_map_memory()
Expand Down Expand Up @@ -674,7 +674,7 @@ void avatar::grab( object_type grab_type, const tripoint &grab_point )
this->grab_type = grab_type;
this->grab_point = grab_point;

path_settings->avoid_rough_terrain = grab_type != OBJECT_NONE;
path_settings->avoid_rough_terrain = grab_type != object_type::NONE;
}

object_type avatar::get_grab_type() const
Expand Down
12 changes: 6 additions & 6 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,9 +1068,9 @@ void Character::mount_creature( monster &z )
if( g->u.is_hauling() ) {
g->u.stop_hauling();
}
if( g->u.get_grab_type() != OBJECT_NONE ) {
if( g->u.get_grab_type() != object_type::NONE ) {
add_msg( m_warning, _( "You let go of the grabbed object." ) );
g->u.grab( OBJECT_NONE );
g->u.grab( object_type::NONE );
}
g->place_player( pnt );
} else {
Expand Down Expand Up @@ -1238,9 +1238,9 @@ void Character::forced_dismount()
add_msg( m_debug, "Forced_dismount could not find a square to deposit player" );
}
if( is_avatar() ) {
if( g->u.get_grab_type() != OBJECT_NONE ) {
if( g->u.get_grab_type() != object_type::NONE ) {
add_msg( m_warning, _( "You let go of the grabbed object." ) );
g->u.grab( OBJECT_NONE );
g->u.grab( object_type::NONE );
}
set_movement_mode( CMM_WALK );
if( g->u.is_auto_moving() || g->u.has_destination() || g->u.has_destination_activity() ) {
Expand Down Expand Up @@ -1271,9 +1271,9 @@ void Character::dismount()
if( critter->has_flag( MF_RIDEABLE_MECH ) && !critter->type->mech_weapon.is_empty() ) {
remove_item( weapon );
}
if( is_avatar() && g->u.get_grab_type() != OBJECT_NONE ) {
if( is_avatar() && g->u.get_grab_type() != object_type::NONE ) {
add_msg( m_warning, _( "You let go of the grabbed object." ) );
g->u.grab( OBJECT_NONE );
g->u.grab( object_type::NONE );
}
critter->remove_effect( effect_ridden );
critter->add_effect( effect_controlled, 5_turns );
Expand Down
30 changes: 15 additions & 15 deletions src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,21 @@ struct enum_traits<phase_id> {
};

// Return the class an in-world object uses to interact with the world.
// ex; if ( player.grab_type == OBJECT_VEHICLE ) { ...
// or; if ( baseactor_just_shot_at.object_type() == OBJECT_NPC ) { ...
enum object_type {
OBJECT_NONE, // Nothing, invalid.
OBJECT_ITEM, // item.h
OBJECT_ACTOR, // potential virtual base class, get_object_type() would return one of the types below
OBJECT_PLAYER, // player.h, npc.h
OBJECT_NPC, // nph.h
OBJECT_MONSTER, // monster.h
OBJECT_VEHICLE, // vehicle.h
OBJECT_TRAP, // trap.h
OBJECT_FIELD, // field.h; field_entry
OBJECT_TERRAIN, // Not a real object
OBJECT_FURNITURE, // Not a real object
NUM_OBJECTS,
// ex; if ( player.grab_type == object_type::VEHICLE ) { ...
// or; if ( baseactor_just_shot_at.object_type() == object_type::NPC ) { ...
enum class object_type : int {
NONE, // Nothing, invalid.
ITEM, // item.h
ACTOR, // potential virtual base class, get_object_type() would return one of the types below
PLAYER, // player.h, npc.h
NPC, // nph.h
MONSTER, // monster.h
VEHICLE, // vehicle.h
TRAP, // trap.h
FIELD, // field.h; field_entry
TERRAIN, // Not a real object
FURNITURE, // Not a real object
NUM_OBJECT_TYPES,
};

enum class liquid_source_type : int {
Expand Down
32 changes: 16 additions & 16 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9192,7 +9192,7 @@ bool game::walk_move( const tripoint &dest_loc )
const tripoint furn_pos = u.pos() + u.grab_point;
const tripoint furn_dest = dest_loc + u.grab_point;

bool grabbed = u.get_grab_type() != OBJECT_NONE;
bool grabbed = u.get_grab_type() != object_type::NONE;
if( grabbed ) {
const tripoint dp = dest_loc - u.pos();
pushing = dp == u.grab_point;
Expand All @@ -9201,20 +9201,20 @@ bool game::walk_move( const tripoint &dest_loc )
if( grabbed && dest_loc.z != u.posz() ) {
add_msg( m_warning, _( "You let go of the grabbed object." ) );
grabbed = false;
u.grab( OBJECT_NONE );
u.grab( object_type::NONE );
}

// Now make sure we're actually holding something
const vehicle *grabbed_vehicle = nullptr;
if( grabbed && u.get_grab_type() == OBJECT_FURNITURE ) {
if( grabbed && u.get_grab_type() == object_type::FURNITURE ) {
// We only care about shifting, because it's the only one that can change our destination
if( m.has_furn( u.pos() + u.grab_point ) ) {
shifting_furniture = !pushing && !pulling;
} else {
// We were grabbing a furniture that isn't there
grabbed = false;
}
} else if( grabbed && u.get_grab_type() == OBJECT_VEHICLE ) {
} else if( grabbed && u.get_grab_type() == object_type::VEHICLE ) {
grabbed_vehicle = veh_pointer_or_null( m.veh_at( u.pos() + u.grab_point ) );
if( grabbed_vehicle == nullptr ) {
// We were grabbing a vehicle that isn't there anymore
Expand All @@ -9226,7 +9226,7 @@ bool game::walk_move( const tripoint &dest_loc )
}
if( u.grab_point != tripoint_zero && !grabbed ) {
add_msg( m_warning, _( "Can't find grabbed object." ) );
u.grab( OBJECT_NONE );
u.grab( object_type::NONE );
}

if( m.impassable( dest_loc ) && !pushing && !shifting_furniture ) {
Expand Down Expand Up @@ -9286,7 +9286,7 @@ bool game::walk_move( const tripoint &dest_loc )
const int mcost_from = m.move_cost( u.pos() ); //calculate this _before_ calling grabbed_move

int modifier = 0;
if( grabbed && u.get_grab_type() == OBJECT_FURNITURE && u.pos() + u.grab_point == dest_loc ) {
if( grabbed && u.get_grab_type() == object_type::FURNITURE && u.pos() + u.grab_point == dest_loc ) {
modifier = -m.furn( dest_loc ).obj().movecost;
}

Expand Down Expand Up @@ -9899,7 +9899,7 @@ bool game::phasing_move( const tripoint &dest_loc )
m.board_vehicle( u.pos(), &u );
}

u.grab( OBJECT_NONE );
u.grab( object_type::NONE );
on_move_effects();
m.creature_on_trap( u );
return true;
Expand All @@ -9917,7 +9917,7 @@ bool game::grabbed_furn_move( const tripoint &dp )
if( !m.has_furn( fpos ) ) {
// Where did it go? We're grabbing thin air so reset.
add_msg( m_info, _( "No furniture at grabbed point." ) );
u.grab( OBJECT_NONE );
u.grab( object_type::NONE );
return false;
}

Expand Down Expand Up @@ -10064,7 +10064,7 @@ bool game::grabbed_furn_move( const tripoint &dp )
u.grab_point = d_sum; // furniture moved relative to us
} else { // we pushed furniture out of reach
add_msg( _( "You let go of the %s." ), furntype.name() );
u.grab( OBJECT_NONE );
u.grab( object_type::NONE );
}
return true; // We moved furniture but stayed still.
}
Expand All @@ -10073,7 +10073,7 @@ bool game::grabbed_furn_move( const tripoint &dp )
// Not sure how that chair got into a wall, but don't let player follow.
add_msg( _( "You let go of the %1$s as it slides past %2$s." ),
furntype.name(), m.tername( fdest ) );
u.grab( OBJECT_NONE );
u.grab( object_type::NONE );
return true;
}

Expand All @@ -10082,7 +10082,7 @@ bool game::grabbed_furn_move( const tripoint &dp )

bool game::grabbed_move( const tripoint &dp )
{
if( u.get_grab_type() == OBJECT_NONE ) {
if( u.get_grab_type() == object_type::NONE ) {
return false;
}

Expand All @@ -10092,17 +10092,17 @@ bool game::grabbed_move( const tripoint &dp )
}

// vehicle: pulling, pushing, or moving around the grabbed object.
if( u.get_grab_type() == OBJECT_VEHICLE ) {
if( u.get_grab_type() == object_type::VEHICLE ) {
return grabbed_veh_move( dp );
}

if( u.get_grab_type() == OBJECT_FURNITURE ) {
if( u.get_grab_type() == object_type::FURNITURE ) {
return grabbed_furn_move( dp );
}

add_msg( m_info, _( "Nothing at grabbed point %d,%d,%d or bad grabbed object type." ),
u.grab_point.x, u.grab_point.y, u.grab_point.z );
u.grab( OBJECT_NONE );
u.grab( object_type::NONE );
return false;
}

Expand Down Expand Up @@ -10431,7 +10431,7 @@ void game::vertical_move( int movez, bool force )

if( force ) {
// Let go of a grabbed cart.
u.grab( OBJECT_NONE );
u.grab( object_type::NONE );
} else if( u.grab_point != tripoint_zero ) {
add_msg( m_info, _( "You can't drag things up and down stairs." ) );
return;
Expand Down Expand Up @@ -10908,7 +10908,7 @@ void game::vertical_shift( const int z_after )
}

// TODO: Implement dragging stuff up/down
u.grab( OBJECT_NONE );
u.grab( object_type::NONE );

scent.reset();

Expand Down
4 changes: 2 additions & 2 deletions src/grab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool game::grabbed_veh_move( const tripoint &dp )
const optional_vpart_position grabbed_vehicle_vp = m.veh_at( u.pos() + u.grab_point );
if( !grabbed_vehicle_vp ) {
add_msg( m_info, _( "No vehicle at grabbed point." ) );
u.grab( OBJECT_NONE );
u.grab( object_type::NONE );
return false;
}
vehicle *grabbed_vehicle = &grabbed_vehicle_vp->vehicle();
Expand All @@ -36,7 +36,7 @@ bool game::grabbed_veh_move( const tripoint &dp )
if( mon != nullptr && mon->has_effect( effect_harnessed ) ) {
add_msg( m_info, _( "You cannot move this vehicle whilst your %s is harnessed!" ),
mon->get_name() );
u.grab( OBJECT_NONE );
u.grab( object_type::NONE );
return false;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,14 @@ static void grab()
avatar &you = g->u;
map &m = g->m;

if( you.get_grab_type() != OBJECT_NONE ) {
if( you.get_grab_type() != object_type::NONE ) {
if( const optional_vpart_position vp = m.veh_at( you.pos() + you.grab_point ) ) {
add_msg( _( "You release the %s." ), vp->vehicle().name );
} else if( m.has_furn( you.pos() + you.grab_point ) ) {
add_msg( _( "You release the %s." ), m.furnname( you.pos() + you.grab_point ) );
}

you.grab( OBJECT_NONE );
you.grab( object_type::NONE );
return;
}

Expand All @@ -650,21 +650,21 @@ static void grab()

if( grabp == you.pos() ) {
add_msg( _( "You get a hold of yourself." ) );
you.grab( OBJECT_NONE );
you.grab( object_type::NONE );
return;
}
if( const optional_vpart_position vp = m.veh_at( grabp ) ) {
if( !vp->vehicle().handle_potential_theft( dynamic_cast<player &>( g->u ) ) ) {
return;
}
you.grab( OBJECT_VEHICLE, grabp - you.pos() );
you.grab( object_type::VEHICLE, grabp - you.pos() );
add_msg( _( "You grab the %s." ), vp->vehicle().name );
} else if( m.has_furn( grabp ) ) { // If not, grab furniture if present
if( !m.furn( grabp ).obj().is_movable() ) {
add_msg( _( "You can not grab the %s" ), m.furnname( grabp ) );
return;
}
you.grab( OBJECT_FURNITURE, grabp - you.pos() );
you.grab( object_type::FURNITURE, grabp - you.pos() );
if( !m.can_move_furniture( grabp, &you ) ) {
add_msg( _( "You grab the %s. It feels really heavy." ), m.furnname( grabp ) );
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,9 +1301,10 @@ void map::furn_set( const tripoint &p, const furn_id &new_furniture )
const furn_t &new_t = new_furniture.obj();

// If player has grabbed this furniture and it's no longer grabbable, release the grab.
if( g->u.get_grab_type() == OBJECT_FURNITURE && g->u.grab_point == p && !new_t.is_movable() ) {
if( g->u.get_grab_type() == object_type::FURNITURE && g->u.grab_point == p &&
!new_t.is_movable() ) {
add_msg( _( "The %s you were grabbing is destroyed!" ), old_t.name() );
g->u.grab( OBJECT_NONE );
g->u.grab( object_type::NONE );
}
// If a creature was crushed under a rubble -> free it
if( old_id == f_rubble && new_furniture == f_null ) {
Expand Down
5 changes: 3 additions & 2 deletions src/savegame_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ static const ter_str_id ter_t_wreckage( "t_wreckage" );

static const trap_str_id tr_brazier( "tr_brazier" );

static const std::array<std::string, NUM_OBJECTS> obj_type_name = { { "OBJECT_NONE", "OBJECT_ITEM", "OBJECT_ACTOR", "OBJECT_PLAYER",
static const std::array<std::string, static_cast<size_t>( object_type::NUM_OBJECT_TYPES )>
obj_type_name = { { "OBJECT_NONE", "OBJECT_ITEM", "OBJECT_ACTOR", "OBJECT_PLAYER",
"OBJECT_NPC", "OBJECT_MONSTER", "OBJECT_VEHICLE", "OBJECT_TRAP", "OBJECT_FIELD",
"OBJECT_TERRAIN", "OBJECT_FURNITURE"
}
Expand Down Expand Up @@ -1086,7 +1087,7 @@ void avatar::load( const JsonObject &data )
}
const auto iter = std::find( obj_type_name.begin(), obj_type_name.end(), grab_typestr );
grab( iter == obj_type_name.end() ?
OBJECT_NONE : static_cast<object_type>( std::distance( obj_type_name.begin(), iter ) ),
object_type::NONE : static_cast<object_type>( std::distance( obj_type_name.begin(), iter ) ),
grab_point );

data.read( "focus_pool", focus_pool );
Expand Down
5 changes: 3 additions & 2 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,11 @@ class DefaultRemovePartHandler : public RemovePartHandler
}
// TODO: maybe do this for all the nearby NPCs as well?

if( g->u.get_grab_type() == OBJECT_VEHICLE && g->u.grab_point == veh.global_part_pos3( part ) ) {
if( g->u.get_grab_type() == object_type::VEHICLE &&
g->u.grab_point == veh.global_part_pos3( part ) ) {
if( veh.parts_at_relative( veh.parts[part].mount, false ).empty() ) {
add_msg( m_info, _( "The vehicle part you were holding has been destroyed!" ) );
g->u.grab( OBJECT_NONE );
g->u.grab( object_type::NONE );
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/vehicle_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,8 @@ bool vehicle::fold_up()

add_msg( _( "You painstakingly pack the %s into a portable configuration." ), name );

if( g->u.get_grab_type() != OBJECT_NONE ) {
g->u.grab( OBJECT_NONE );
if( g->u.get_grab_type() != object_type::NONE ) {
g->u.grab( object_type::NONE );
add_msg( _( "You let go of %s as you fold it." ), name );
}

Expand Down
4 changes: 2 additions & 2 deletions tests/map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ TEST_CASE( "destroy_grabbed_furniture" )
g->u.setpos( test_origin );
const tripoint grab_point = test_origin + tripoint_east;
g->m.furn_set( grab_point, furn_id( "f_chair" ) );
g->u.grab( OBJECT_FURNITURE, grab_point );
g->u.grab( object_type::FURNITURE, grab_point );
WHEN( "The furniture grabbed by the player is destroyed" ) {
g->m.destroy( grab_point );
THEN( "The player's grab is released" ) {
CHECK( g->u.get_grab_type() == OBJECT_NONE );
CHECK( g->u.get_grab_type() == object_type::NONE );
CHECK( g->u.grab_point == tripoint_zero );
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/vehicle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ TEST_CASE( "destroy_grabbed_vehicle_section" )
vehicle *veh_ptr = g->m.add_vehicle( vproto_id( "bicycle" ), vehicle_origin, -90, 0, 0 );
REQUIRE( veh_ptr != nullptr );
tripoint grab_point = test_origin + tripoint_east;
g->u.grab( OBJECT_VEHICLE, grab_point );
REQUIRE( g->u.get_grab_type() != OBJECT_NONE );
g->u.grab( object_type::VEHICLE, grab_point );
REQUIRE( g->u.get_grab_type() != object_type::NONE );
REQUIRE( g->u.grab_point == grab_point );
WHEN( "The vehicle section grabbed by the player is destroyed" ) {
g->m.destroy( grab_point );
REQUIRE( veh_ptr->get_parts_at( grab_point, "", part_status_flag::available ).empty() );
THEN( "The player's grab is released" ) {
CHECK( g->u.get_grab_type() == OBJECT_NONE );
CHECK( g->u.get_grab_type() == object_type::NONE );
CHECK( g->u.grab_point == tripoint_zero );
}
}
Expand Down

0 comments on commit c10eae0

Please sign in to comment.