From ca8aed212fb47ebb50aa4d8b5b59b269ddfb0eba Mon Sep 17 00:00:00 2001 From: KorGgenT Date: Mon, 10 Feb 2020 20:08:04 -0500 Subject: [PATCH] migrate player_activity using item index to item_location --- src/activity_handlers.cpp | 24 ++++++++++++------------ src/activity_item_handling.cpp | 3 ++- src/iexamine.cpp | 5 +++-- src/iuse.cpp | 22 ++++++++++++---------- src/iuse_actor.cpp | 7 ++++--- src/player.cpp | 6 ++++-- src/player_activity.cpp | 29 +++++++++++++++++++++++++++++ src/player_activity.h | 8 +++++++- src/savegame.cpp | 2 +- src/savegame_json.cpp | 8 ++++++++ 10 files changed, 82 insertions(+), 32 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 6c7645441acaf..e17a6e2ee95e7 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -1677,7 +1677,7 @@ void activity_handlers::firstaid_finish( player_activity *act, player *p ) { static const std::string iuse_name_string( "heal" ); - item &it = p->i_at( act->position ); + item &it = *act->targets.front(); item *used_tool = it.get_usable_item( iuse_name_string ); if( used_tool == nullptr ) { debugmsg( "Lost tool used for healing" ); @@ -1795,7 +1795,7 @@ void activity_handlers::generic_game_do_turn( player_activity * /*act*/, player void activity_handlers::game_do_turn( player_activity *act, player *p ) { - item &game_item = p->i_at( act->position ); + item &game_item = *act->targets.front(); //Deduct 1 battery charge for every minute spent playing if( calendar::once_every( 1_minutes ) ) { @@ -1953,7 +1953,7 @@ void activity_handlers::pickaxe_do_turn( player_activity *act, player * ) void activity_handlers::pickaxe_finish( player_activity *act, player *p ) { const tripoint pos( g->m.getlocal( act->placement ) ); - item &it = p->i_at( act->position ); + item &it = *act->targets.front(); // Invalidate the activity early to prevent a query from mod_pain() act->set_to_null(); if( p->is_avatar() ) { @@ -2154,7 +2154,7 @@ void activity_handlers::start_fire_finish( player_activity *act, player *p ) { static const std::string iuse_name_string( "firestarter" ); - item &it = p->i_at( act->position ); + item &it = *act->targets.front(); item *used_tool = it.get_usable_item( iuse_name_string ); if( used_tool == nullptr ) { debugmsg( "Lost tool used for starting fire" ); @@ -2190,7 +2190,7 @@ void activity_handlers::start_fire_do_turn( player_activity *act, player *p ) } } - item &firestarter = p->i_at( act->position ); + item &firestarter = *act->targets.front(); if( firestarter.has_flag( flag_REQUIRES_TINDER ) ) { if( !g->m.tinder_at( act->placement ) ) { p->add_msg_if_player( m_info, _( "This item requires tinder to light." ) ); @@ -2327,7 +2327,7 @@ void activity_handlers::hand_crank_do_turn( player_activity *act, player *p ) // to 10 watt (suspicious claims from some manufacturers) sustained output. // It takes 2.4 minutes to produce 1kj at just slightly under 7 watts (25 kj per hour) // time-based instead of speed based because it's a sustained activity - item &hand_crank_item = p ->i_at( act->position ); + item &hand_crank_item = *act->targets.front(); if( calendar::once_every( 144_seconds ) ) { p->mod_fatigue( 1 ); @@ -2350,7 +2350,7 @@ void activity_handlers::vibe_do_turn( player_activity *act, player *p ) //Using a vibrator takes time (10 minutes), not speed //Linear increase in morale during action with a small boost at end //Deduct 1 battery charge for every minute in use, or vibrator is much less effective - item &vibrator_item = p->i_at( act->position ); + item &vibrator_item = *act->targets.front(); if( ( p->is_wearing( "rebreather" ) ) || ( p->is_wearing( "rebreather_xl" ) ) || ( p->is_wearing( "mask_h20survivor" ) ) ) { @@ -2457,7 +2457,7 @@ void activity_handlers::oxytorch_do_turn( player_activity *act, player *p ) return; } - item &it = p->i_at( act->position ); + item &it = *act->targets.front(); // act->values[0] is the number of charges yet to be consumed const int charges_used = std::min( act->values[0], it.ammo_required() ); @@ -2477,7 +2477,7 @@ void activity_handlers::oxytorch_finish( player_activity *act, player *p ) const ter_id ter = g->m.ter( pos ); // fast players might still have some charges left to be consumed - p->i_at( act->position ).ammo_consume( act->values[0], p->pos() ); + act->targets.front()->ammo_consume( act->values[0], p->pos() ); if( g->m.furn( pos ) == f_rack ) { g->m.furn_set( pos, f_null ); @@ -2875,8 +2875,8 @@ void activity_handlers::gunmod_add_finish( player_activity *act, player *p ) return; } - item &gun = p->i_at( act->position ); - item &mod = p->i_at( act->values[0] ); + item &gun = *act->targets.at( 0 ); + item &mod = *act->targets.at( 1 ); // chance of success (%) const int roll = act->values[1]; @@ -3123,7 +3123,7 @@ static void rod_fish( player *p, std::vector &fishables ) void activity_handlers::fish_do_turn( player_activity *act, player *p ) { - item &it = p->i_at( act->position ); + item &it = *act->targets.front(); int fish_chance = 1; int survival_skill = p->get_skill_level( skill_survival ); if( it.has_flag( flag_FISH_POOR ) ) { diff --git a/src/activity_item_handling.cpp b/src/activity_item_handling.cpp index f2c83b1456fd3..5b3e804e5eba3 100644 --- a/src/activity_item_handling.cpp +++ b/src/activity_item_handling.cpp @@ -2912,7 +2912,8 @@ static bool generic_multi_activity_do( player &p, const activity_id &act_id, p.activity = player_activity(); item *best_rod = p.best_quality_item( qual_FISHING ); p.assign_activity( ACT_FISH, to_moves( 5_hours ), 0, - p.get_item_position( best_rod ), best_rod->tname() ); + 0, best_rod->tname() ); + p.activity.targets.push_back( item_location( p, best_rod ) ); p.activity.coord_set = g->get_fishable_locations( ACTIVITY_SEARCH_DISTANCE, src_loc ); return false; } else if( reason == NEEDS_MINING ) { diff --git a/src/iexamine.cpp b/src/iexamine.cpp index cddfb97581885..3c4c4e696d8ca 100644 --- a/src/iexamine.cpp +++ b/src/iexamine.cpp @@ -553,7 +553,7 @@ class atm_menu item *dst; if( u.activity.id() == ACT_ATM ) { u.activity.set_to_null(); // stop for now, if required, it will be created again. - dst = &u.i_at( u.activity.position ); + dst = u.activity.targets.front().get_item(); if( dst->is_null() || dst->typeId() != "cash_card" ) { return false; } @@ -574,7 +574,8 @@ class atm_menu // Money from `*i` could be transferred, but we're out of moves, schedule it for // the next turn. Putting this here makes sure there will be something to be // done next turn. - u.assign_activity( ACT_ATM, 0, transfer_all_money, u.get_item_position( dst ) ); + u.assign_activity( ACT_ATM, 0, transfer_all_money ); + u.activity.targets.push_back( item_location( u, dst ) ); break; } diff --git a/src/iuse.cpp b/src/iuse.cpp index c0445f3f50df9..5f0075b4fcc39 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -1762,8 +1762,8 @@ int iuse::fishing_rod( player *p, item *it, bool, const tripoint & ) return 0; } p->add_msg_if_player( _( "You cast your line and wait to hook something…" ) ); - p->assign_activity( ACT_FISH, to_moves( 5_hours ), 0, - p->get_item_position( it ), it->tname() ); + p->assign_activity( ACT_FISH, to_moves( 5_hours ), 0, 0, it->tname() ); + p->activity.targets.push_back( item_location( *p, it ) ); p->activity.coord_set = g->get_fishable_locations( 60, *found ); return 0; } @@ -3313,7 +3313,8 @@ int iuse::pickaxe( player *p, item *it, bool, const tripoint &pos ) break; } - p->assign_activity( ACT_PICKAXE, moves, -1, p->get_item_position( it ) ); + p->assign_activity( ACT_PICKAXE, moves, -1 ); + p->activity.targets.push_back( item_location( *p, it ) ); p->activity.placement = g->m.getabs( pnt ); p->add_msg_if_player( _( "You strike the %1$s with your %2$s." ), g->m.tername( pnt ), it->tname() ); @@ -4412,7 +4413,8 @@ int iuse::portable_game( player *p, item *it, bool, const tripoint & ) p->get_item_position( it ), "gaming" ); return it->type->charges_to_use(); } - p->assign_activity( ACT_GAME, moves, -1, p->get_item_position( it ), "gaming" ); + p->assign_activity( ACT_GAME, moves, -1, 0, "gaming" ); + p->activity.targets.push_back( item_location( *p, it ) ); std::map game_data; game_data.clear(); int game_score = 0; @@ -4460,8 +4462,8 @@ int iuse::hand_crank( player *p, item *it, bool, const tripoint & ) if( it->ammo_capacity() > it->ammo_remaining() ) { p->add_msg_if_player( _( "You start cranking the %s to charge its %s." ), it->tname(), it->magazine_current()->tname() ); - p->assign_activity( ACT_HAND_CRANK, moves, -1, p->get_item_position( it ), - "hand-cranking" ); + p->assign_activity( ACT_HAND_CRANK, moves, -1, 0, "hand-cranking" ); + p->activity.targets.push_back( item_location( *p, it ) ); } else { p->add_msg_if_player( _( "You could use the %s to charge its %s, but it's already charged." ), it->tname(), magazine->tname() ); @@ -4505,8 +4507,8 @@ int iuse::vibe( player *p, item *it, bool, const tripoint & ) p->add_msg_if_player( _( "You whip out your %s and start getting the tension out." ), it->tname() ); } - p->assign_activity( ACT_VIBE, moves, -1, p->get_item_position( it ), - "de-stressing" ); + p->assign_activity( ACT_VIBE, moves, -1, 0, "de-stressing" ); + p->activity.targets.push_back( item_location( *p, it ) ); } return it->type->charges_to_use(); } @@ -4923,8 +4925,8 @@ int iuse::oxytorch( player *p, item *it, bool, const tripoint & ) } // placing ter here makes resuming tasks work better - p->assign_activity( ACT_OXYTORCH, moves, static_cast( ter ), - p->get_item_position( it ) ); + p->assign_activity( ACT_OXYTORCH, moves, static_cast( ter ) ); + p->activity.targets.push_back( item_location( *p, it ) ); p->activity.placement = pnt; p->activity.values.push_back( charges ); diff --git a/src/iuse_actor.cpp b/src/iuse_actor.cpp index 7a863ceaafd5e..32df2ee436397 100644 --- a/src/iuse_actor.cpp +++ b/src/iuse_actor.cpp @@ -1454,8 +1454,8 @@ int firestarter_actor::use( player &p, item &it, bool t, const tripoint &spos ) const int potential_skill_gain = moves_modifier + moves_cost_fast / 100.0 + 2; p.assign_activity( ACT_START_FIRE, moves, potential_skill_gain, - p.get_item_position( &it ), - it.tname() ); + 0, it.tname() ); + p.activity.targets.push_back( item_location( p, &it ) ); p.activity.values.push_back( g->natural_light_level( pos.z ) ); p.activity.placement = pos; // charges to use are handled by the activity @@ -3620,7 +3620,8 @@ int heal_actor::use( player &p, item &it, bool, const tripoint &pos ) const if( long_action && &patient == &p && !p.is_npc() ) { // Assign first aid long action. /** @EFFECT_FIRSTAID speeds up firstaid activity */ - p.assign_activity( ACT_FIRSTAID, cost, 0, p.get_item_position( &it ), it.tname() ); + p.assign_activity( ACT_FIRSTAID, cost, 0, 0, it.tname() ); + p.activity.targets.push_back( item_location( p, &it ) ); p.activity.values.push_back( hpp ); p.moves = 0; return 0; diff --git a/src/player.cpp b/src/player.cpp index f2cccc3ab0736..ccc9d407540ef 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -4064,8 +4064,10 @@ void player::gunmod_add( item &gun, item &mod ) const int moves = !has_trait( trait_DEBUG_HS ) ? mod.type->gunmod->install_time : 0; - assign_activity( activity_id( "ACT_GUNMOD_ADD" ), moves, -1, get_item_position( &gun ), tool ); - activity.values.push_back( get_item_position( &mod ) ); + assign_activity( activity_id( "ACT_GUNMOD_ADD" ), moves, -1, 0, tool ); + activity.targets.push_back( item_location( *this, &gun ) ); + activity.targets.push_back( item_location( *this, &mod ) ); + activity.values.push_back( 0 ); // dummy value activity.values.push_back( roll ); // chance of success (%) activity.values.push_back( risk ); // chance of damage (%) activity.values.push_back( qty ); // tool charges diff --git a/src/player_activity.cpp b/src/player_activity.cpp index 9fd9a1a60f283..a3660c6967e54 100644 --- a/src/player_activity.cpp +++ b/src/player_activity.cpp @@ -13,6 +13,17 @@ #include "itype.h" #include "skill.h" +static const activity_id ACT_FIRSTAID( "ACT_FIRSTAID" ); +static const activity_id ACT_GAME( "ACT_GAME" ); +static const activity_id ACT_PICKAXE( "ACT_PICKAXE" ); +static const activity_id ACT_START_FIRE( "ACT_START_FIRE" ); +static const activity_id ACT_HAND_CRANK( "ACT_HAND_CRANK" ); +static const activity_id ACT_VIBE( "ACT_VIBE" ); +static const activity_id ACT_OXYTORCH( "ACT_OXYTORCH" ); +static const activity_id ACT_FISH( "ACT_FISH" ); +static const activity_id ACT_ATM( "ACT_ATM" ); +static const activity_id ACT_GUNMOD_ADD( "ACT_GUNMOD_ADD" ); + player_activity::player_activity() : type( activity_id::NULL_ID() ) { } player_activity::player_activity( activity_id t, int turns, int Index, int pos, @@ -24,6 +35,24 @@ player_activity::player_activity( activity_id t, int turns, int Index, int pos, { } +void player_activity::migrate_item_position( Character &guy ) +{ + const bool simple_action_replace = + type == ACT_FIRSTAID || type == ACT_GAME || + type == ACT_PICKAXE || type == ACT_START_FIRE || + type == ACT_HAND_CRANK || type == ACT_VIBE || + type == ACT_OXYTORCH || type == ACT_FISH || + type == ACT_ATM; + + if( simple_action_replace ) { + targets.push_back( item_location( guy, &guy.i_at( position ) ) ); + } else if( type == ACT_GUNMOD_ADD ) { + // this activity has two indices; "position" = gun and "values[0]" = mod + targets.push_back( item_location( guy, &guy.i_at( position ) ) ); + targets.push_back( item_location( guy, &guy.i_at( values[0] ) ) ); + } +} + void player_activity::set_to_null() { type = activity_id::NULL_ID(); diff --git a/src/player_activity.h b/src/player_activity.h index 240dc4d30adff..6de38362d81b9 100644 --- a/src/player_activity.h +++ b/src/player_activity.h @@ -39,7 +39,10 @@ class player_activity int moves_left = 0; /** An activity specific value. */ int index = 0; - /** An activity specific value. */ + /** + * An activity specific value. + * DO NOT USE FOR ITEM INDEX + */ int position = 0; /** An activity specific value. */ std::string name; @@ -102,6 +105,9 @@ class player_activity void serialize( JsonOut &json ) const; void deserialize( JsonIn &jsin ); + // used to migrate the item indices to item_location + // obsolete after 0.F stable + void migrate_item_position( Character &guy ); /** Convert from the old enumeration to the new string_id */ void deserialize_legacy_type( int legacy_type, activity_id &dest ); diff --git a/src/savegame.cpp b/src/savegame.cpp index 3a6fedb4980f3..90208842b6ef9 100644 --- a/src/savegame.cpp +++ b/src/savegame.cpp @@ -51,7 +51,7 @@ extern std::map> quick_shortcuts_map; * Changes that break backwards compatibility should bump this number, so the game can * load a legacy format loader. */ -const int savegame_version = 27; +const int savegame_version = 28; /* * This is a global set by detected version header in .sav, maps.txt, or overmap. diff --git a/src/savegame_json.cpp b/src/savegame_json.cpp index 10200b95d38f4..65e90b05fa3f1 100644 --- a/src/savegame_json.cpp +++ b/src/savegame_json.cpp @@ -588,6 +588,14 @@ void Character::load( const JsonObject &data ) JsonIn *invin = data.get_raw( "inv" ); inv.json_load_items( *invin ); } + // this is after inventory is loaded to make it more obvious that + // it needs to be changed again when Character::i_at is removed for nested containers + if( savegame_loading_version < 28 ) { + activity.migrate_item_position( *this ); + destination_activity.migrate_item_position( *this ); + stashed_outbounds_activity.migrate_item_position( *this ); + stashed_outbounds_backlog.migrate_item_position( *this ); + } weapon = item( "null", 0 ); data.read( "weapon", weapon );