From c10eae0debc8943b1388f7b8335c73161e613aa4 Mon Sep 17 00:00:00 2001 From: ZhilkinSerg Date: Thu, 28 May 2020 20:42:34 +0300 Subject: [PATCH] Make object_type a class enum --- src/activity_item_handling.cpp | 2 +- src/advanced_inv_area.cpp | 2 +- src/avatar.cpp | 4 ++-- src/character.cpp | 12 ++++++------ src/enums.h | 30 +++++++++++++++--------------- src/game.cpp | 32 ++++++++++++++++---------------- src/grab.cpp | 4 ++-- src/handle_action.cpp | 10 +++++----- src/map.cpp | 5 +++-- src/savegame_json.cpp | 5 +++-- src/vehicle.cpp | 5 +++-- src/vehicle_use.cpp | 4 ++-- tests/map_test.cpp | 4 ++-- tests/vehicle_test.cpp | 6 +++--- 14 files changed, 64 insertions(+), 61 deletions(-) diff --git a/src/activity_item_handling.cpp b/src/activity_item_handling.cpp index ad171bb8fd194..42f3d2faa439e 100644 --- a/src/activity_item_handling.cpp +++ b/src/activity_item_handling.cpp @@ -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 vp = g->m.veh_at( diff --git a/src/advanced_inv_area.cpp b/src/advanced_inv_area.cpp index 4898c1bad05b6..1f1610781242c 100644 --- a/src/advanced_inv_area.cpp +++ b/src/advanced_inv_area.cpp @@ -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; diff --git a/src/avatar.cpp b/src/avatar.cpp index 07d4df8e5a89d..e72b889d67284 100644 --- a/src/avatar.cpp +++ b/src/avatar.cpp @@ -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() @@ -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 diff --git a/src/character.cpp b/src/character.cpp index d11e9d19f4ea5..b434134e15bb3 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -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 { @@ -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() ) { @@ -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 ); diff --git a/src/enums.h b/src/enums.h index 481ecb84df9c6..9b535ca85a1cf 100644 --- a/src/enums.h +++ b/src/enums.h @@ -173,21 +173,21 @@ struct enum_traits { }; // 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 { diff --git a/src/game.cpp b/src/game.cpp index 978cb0c19251d..3590f931fcce9 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -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; @@ -9201,12 +9201,12 @@ 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; @@ -9214,7 +9214,7 @@ bool game::walk_move( const tripoint &dest_loc ) // 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 @@ -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 ) { @@ -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; } @@ -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; @@ -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; } @@ -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. } @@ -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; } @@ -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; } @@ -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; } @@ -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; @@ -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(); diff --git a/src/grab.cpp b/src/grab.cpp index 0a8b784d4dbcf..9d22e96ee2fba 100644 --- a/src/grab.cpp +++ b/src/grab.cpp @@ -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(); @@ -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; } } diff --git a/src/handle_action.cpp b/src/handle_action.cpp index 3b5a3e8696bd7..b4a6699f1d473 100644 --- a/src/handle_action.cpp +++ b/src/handle_action.cpp @@ -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; } @@ -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( 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 { diff --git a/src/map.cpp b/src/map.cpp index 409991e4f356a..c59e2f017bdbd 100755 --- a/src/map.cpp +++ b/src/map.cpp @@ -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 ) { diff --git a/src/savegame_json.cpp b/src/savegame_json.cpp index a9a4a3aeff2b9..bcefbfe441ea9 100644 --- a/src/savegame_json.cpp +++ b/src/savegame_json.cpp @@ -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 obj_type_name = { { "OBJECT_NONE", "OBJECT_ITEM", "OBJECT_ACTOR", "OBJECT_PLAYER", +static const std::array( 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" } @@ -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( std::distance( obj_type_name.begin(), iter ) ), + object_type::NONE : static_cast( std::distance( obj_type_name.begin(), iter ) ), grab_point ); data.read( "focus_pool", focus_pool ); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index b805b8ee7caeb..72cc201519df2 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -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 ); } } diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index cc863cef7d1bc..24ecb9ad266a7 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -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 ); } diff --git a/tests/map_test.cpp b/tests/map_test.cpp index 5ccbb52d6b428..a4e04e191d7e6 100644 --- a/tests/map_test.cpp +++ b/tests/map_test.cpp @@ -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 ); } } diff --git a/tests/vehicle_test.cpp b/tests/vehicle_test.cpp index 69f237c551691..b2e255bb83729 100644 --- a/tests/vehicle_test.cpp +++ b/tests/vehicle_test.cpp @@ -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 ); } }