Skip to content

Commit

Permalink
Use cata::value_ptr<item> for special items
Browse files Browse the repository at this point in the history
  • Loading branch information
ymber committed Jan 1, 2020
1 parent 286408e commit b29ab41
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ void Character::mount_creature( monster &z )
z.remove_effect( effect_tied );
if( z.tied_item ) {
i_add( *z.tied_item );
z.tied_item = cata::nullopt;
z.tied_item.reset();
}
}
z.mounted_player_id = getID();
Expand Down
3 changes: 2 additions & 1 deletion src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ void Creature::deal_projectile_attack( Creature *source, dealt_projectile_attack
if( z ) {
if( !proj.get_drop().is_null() ) {
z->add_effect( effect_tied, 1_turns, num_bp, true );
z->tied_item = proj.get_drop();
item drop_item = proj.get_drop();
z->tied_item.reset( &drop_item );
} else {
add_msg( m_debug, "projectile with TANGLE effect, but no drop item specified" );
}
Expand Down
20 changes: 10 additions & 10 deletions src/monexamine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ static item_location tack_loc()
void monexamine::remove_battery( monster &z )
{
g->m.add_item_or_charges( g->u.pos(), *z.battery_item );
z.battery_item = cata::nullopt;
z.battery_item.reset();
}

void monexamine::insert_battery( monster &z )
Expand Down Expand Up @@ -309,7 +309,7 @@ void monexamine::insert_battery( monster &z )
auto bat_item = bat_inv[index - 1];
int item_pos = g->u.get_item_position( bat_item );
if( item_pos != INT_MIN ) {
z.battery_item = *bat_item;
z.battery_item.reset( bat_item );
g->u.i_rem( item_pos );
}
}
Expand Down Expand Up @@ -391,7 +391,7 @@ void monexamine::attach_or_remove_saddle( monster &z )
if( z.has_effect( effect_saddled ) ) {
z.remove_effect( effect_saddled );
g->u.i_add( *z.tack_item );
z.tack_item = cata::nullopt;
z.tack_item.reset();
} else {
item_location loc = tack_loc();

Expand All @@ -400,7 +400,7 @@ void monexamine::attach_or_remove_saddle( monster &z )
return;
}
z.add_effect( effect_saddled, 1_turns, num_bp, true );
z.tack_item = *loc;
z.tack_item.reset( loc.get_item() );
loc.remove_item();
}
}
Expand Down Expand Up @@ -493,7 +493,7 @@ void monexamine::attach_bag_to( monster &z )
}

item &it = *loc;
z.storage_item = it;
z.storage_item.reset( &it );
add_msg( _( "You mount the %1$s on your %2$s." ), it.display_name(), pet_name );
g->u.i_rem( &it );
z.add_effect( effect_has_bag, 1_turns, num_bp, true );
Expand All @@ -511,7 +511,7 @@ void monexamine::remove_bag_from( monster &z )
}
g->m.add_item_or_charges( g->u.pos(), *z.storage_item );
add_msg( _( "You remove the %1$s from %2$s." ), z.storage_item->display_name(), pet_name );
z.storage_item = cata::nullopt;
z.storage_item.reset();
g->u.moves -= 200;
} else {
add_msg( m_bad, _( "Your %1$s doesn't have a bag!" ), pet_name );
Expand Down Expand Up @@ -585,7 +585,7 @@ bool monexamine::add_armor( monster &z )
}

armor.set_var( "pet_armor", "true" );
z.armor_item = armor;
z.armor_item.reset( &armor );
add_msg( pgettext( "pet armor", "You put the %1$s on your %2$s." ), armor.display_name(),
pet_name );
loc.remove_item();
Expand All @@ -609,7 +609,7 @@ void monexamine::remove_armor( monster &z )
g->m.add_item_or_charges( z.pos(), *z.armor_item );
add_msg( pgettext( "pet armor", "You remove the %1$s from %2$s." ), z.armor_item->display_name(),
pet_name );
z.armor_item = cata::nullopt;
z.armor_item.reset();
// TODO: removing armor from a horse takes a lot longer than 2 seconds. This should be a long action.
g->u.moves -= 200;
} else {
Expand Down Expand Up @@ -646,7 +646,7 @@ void monexamine::tie_or_untie( monster &z )
z.remove_effect( effect_tied );
if( z.tied_item ) {
g->u.i_add( *z.tied_item );
z.tied_item = cata::nullopt;
z.tied_item.reset();
}
} else {
std::vector<item *> rope_inv = g->u.items_with( []( const item & itm ) {
Expand All @@ -672,7 +672,7 @@ void monexamine::tie_or_untie( monster &z )
auto rope_item = rope_inv[index - 1];
int item_pos = g->u.get_item_position( rope_item );
if( item_pos != INT_MIN ) {
z.tied_item = *rope_item;
z.tied_item.reset( rope_item );
g->u.i_rem( item_pos );
z.add_effect( effect_tied, 1_turns, num_bp, true );
}
Expand Down
10 changes: 5 additions & 5 deletions src/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ monster::monster( const mtype_id &id ) : monster()
int max_charge = type.magazine->capacity;
item mech_bat_item = item( mech_bat, 0 );
mech_bat_item.ammo_consume( rng( 0, max_charge ), tripoint_zero );
battery_item = mech_bat_item;
battery_item.reset( &mech_bat_item );
}
}

Expand Down Expand Up @@ -1620,7 +1620,7 @@ bool monster::move_effects( bool )
add_msg( _( "The %s easily slips out of its bonds." ), name() );
}
g->m.add_item_or_charges( pos(), *tied_item );
tied_item = cata::nullopt;
tied_item.reset();
}
} else {
if( tied_item ) {
Expand All @@ -1629,7 +1629,7 @@ bool monster::move_effects( bool )
if( !broken ) {
g->m.add_item_or_charges( pos(), *tied_item );
}
tied_item = cata::nullopt;
tied_item.reset();
if( u_see_me ) {
if( broken ) {
add_msg( _( "The %s snaps the bindings holding it down." ), name() );
Expand Down Expand Up @@ -2656,11 +2656,11 @@ units::volume monster::get_carried_volume()
return total_volume;
}

void monster::move_special_item_to_inv( cata::optional<item> &it )
void monster::move_special_item_to_inv( cata::value_ptr<item> &it )
{
if( it ) {
add_item( *it );
it = cata::nullopt;
it.reset();
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "type_id.h"
#include "units.h"
#include "point.h"
#include "value_ptr.h"

class JsonObject;
class JsonIn;
Expand Down Expand Up @@ -436,14 +437,14 @@ class monster : public Creature
Character *mounted_player = nullptr; // player that is mounting this creature
character_id mounted_player_id; // id of player that is mounting this creature ( for save/load )
character_id dragged_foe_id; // id of character being dragged by the monster
cata::optional<item> tied_item; // item used to tie the monster
cata::optional<item> tack_item; // item representing saddle and reins and such
cata::optional<item> armor_item; // item of armor the monster may be wearing
cata::optional<item> storage_item; // storage item for monster carrying items
cata::optional<item> battery_item; // item to power mechs
cata::value_ptr<item> tied_item; // item used to tie the monster
cata::value_ptr<item> tack_item; // item representing saddle and reins and such
cata::value_ptr<item> armor_item; // item of armor the monster may be wearing
cata::value_ptr<item> storage_item; // storage item for monster carrying items
cata::value_ptr<item> battery_item; // item to power mechs
units::mass get_carried_weight();
units::volume get_carried_volume();
void move_special_item_to_inv( cata::optional<item> &it );
void move_special_item_to_inv( cata::value_ptr<item> &it );

// DEFINING VALUES
int friendly;
Expand Down
55 changes: 45 additions & 10 deletions src/savegame_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1819,12 +1819,37 @@ void monster::load( const JsonObject &data )
if( data.read( "wandz", wander_pos.z ) ) {
wander_pos.z = position.z;
}
data.read( "tied_item", tied_item );
data.read( "tack_item", tack_item );
data.read( "armor_item", armor_item );
data.read( "storage_item", storage_item );
if( data.has_object( "tied_item" ) ) {
JsonIn *tied_item_json = data.get_raw( "tied_item" );
item newitem;
newitem.deserialize( *tied_item_json );
tied_item = cata::make_value<item>( newitem );
}
if( data.has_object( "tack_item" ) ) {
JsonIn *tack_item_json = data.get_raw( "tack_item" );
item newitem;
newitem.deserialize( *tack_item_json );
tack_item = cata::make_value<item>( newitem );
}
if( data.has_object( "armor_item" ) ) {
JsonIn *armor_item_json = data.get_raw( "armor_item" );
item newitem;
newitem.deserialize( *armor_item_json );
armor_item = cata::make_value<item>( newitem );
}
if( data.has_object( "storage_item" ) ) {
JsonIn *storage_item_json = data.get_raw( "storage_item" );
item newitem;
newitem.deserialize( *storage_item_json );
storage_item = cata::make_value<item>( newitem );
}
if( data.has_object( "battery_item" ) ) {
JsonIn *battery_item_json = data.get_raw( "battery_item" );
item newitem;
newitem.deserialize( *battery_item_json );
battery_item = cata::make_value<item>( newitem );
}
data.read( "hp", hp );
data.read( "battery_item", battery_item );

// sp_timeout indicates an old save, prior to the special_attacks refactor
if( data.has_array( "sp_timeout" ) ) {
Expand Down Expand Up @@ -1957,11 +1982,21 @@ void monster::store( JsonOut &json ) const
json.member( "morale", morale );
json.member( "hallucination", hallucination );
json.member( "stairscount", staircount );
json.member( "tied_item", tied_item );
json.member( "tack_item", tack_item );
json.member( "armor_item", armor_item );
json.member( "storage_item", storage_item );
json.member( "battery_item", battery_item );
if( tied_item ) {
json.member( "tied_item", *tied_item );
}
if( tack_item ) {
json.member( "tack_item", *tack_item );
}
if( armor_item ) {
json.member( "armor_item", *armor_item );
}
if( storage_item ) {
json.member( "storage_item", *storage_item );
}
if( battery_item ) {
json.member( "battery_item", *battery_item );
}
// Store the relative position of the goal so it loads correctly after a map shift.
json.member( "destination", goal - pos() );
json.member( "ammo", ammo );
Expand Down
2 changes: 1 addition & 1 deletion src/vehicle_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ void vehicle::use_harness( int part, const tripoint &pos )
m.remove_effect( effect_tied );
if( m.tied_item ) {
g->u.i_add( *m.tied_item );
m.tied_item = cata::nullopt;
m.tied_item.reset();
}
}
}
Expand Down

0 comments on commit b29ab41

Please sign in to comment.