Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change item_location return from an int to item_location #37816

Merged
merged 2 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/armor_layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ void player::sort_armor()
if( loc ) {
// wear the item
cata::optional<std::list<item>::iterator> new_equip_it =
wear( this->i_at( loc.obtain( *this ) ) );
wear( *loc.obtain( *this ) );
if( new_equip_it ) {
body_part bp = static_cast<body_part>( tabindex );
if( tabindex == num_bp || ( **new_equip_it ).covers( bp ) ) {
Expand Down Expand Up @@ -802,7 +802,7 @@ void player::sort_armor()
if( loc ) {
// wear the item
if( cata::optional<std::list<item>::iterator> new_equip_it =
wear( this->i_at( loc.obtain( *this ) ) ) ) {
wear( *loc.obtain( *this ) ) ) {
// save iterator to cursor's position
std::list<item>::iterator cursor_it = tmp_worn[leftListIndex];
// reorder `worn` vector to place new item at cursor
Expand Down
7 changes: 3 additions & 4 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,12 +1252,11 @@ void avatar_action::use_item( avatar &you, item_location &loc )
use_in_place = true;
} else {
const int obtain_cost = loc.obtain_cost( you );
item &target = you.i_at( loc.obtain( you ) );
if( target.is_null() ) {
loc = loc.obtain( you );
if( !loc ) {
debugmsg( "Failed to obtain target item" );
return;
}
loc = item_location( you, &target );

// TODO: the following comment is inaccurate and this mechanic needs to be rexamined
// This method only handles items in the inventory, so refund the obtain cost.
Expand Down Expand Up @@ -1295,7 +1294,7 @@ void avatar_action::unload( avatar &you )

item *it = loc.get_item();
if( loc.where() != item_location::type::character ) {
it = &you.i_at( loc.obtain( you ) );
it = loc.obtain( you ).get_item();
}
if( you.unload( *it ) ) {
if( it->has_flag( "MAG_DESTROY" ) && it->ammo_remaining() == 0 ) {
Expand Down
2 changes: 1 addition & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8379,7 +8379,7 @@ void game::reload( item_location &loc, bool prompt, bool empty )

bool use_loc = true;
if( !it->has_flag( "ALLOWS_REMOTE_USE" ) ) {
it = &u.i_at( loc.obtain( u ) );
it = loc.obtain( u ).get_item();
use_loc = false;
}

Expand Down
8 changes: 3 additions & 5 deletions src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ static void wear()
item_location loc = game_menus::inv::wear( u );

if( loc ) {
u.wear( u.i_at( loc.obtain( u ) ) );
u.wear( *loc.obtain( u ) );
} else {
add_msg( _( "Never mind." ) );
}
Expand All @@ -1218,7 +1218,7 @@ static void takeoff()
item_location loc = game_menus::inv::take_off( u );

if( loc ) {
u.takeoff( u.i_at( loc.obtain( u ) ) );
u.takeoff( *loc.obtain( u ) );
} else {
add_msg( _( "Never mind." ) );
}
Expand All @@ -1235,9 +1235,7 @@ static void read()
item spell_book = *loc.get_item();
spell_book.get_use( "learn_spell" )->call( u, spell_book, spell_book.active, u.pos() );
} else {
// calling obtain() invalidates the item pointer
// TODO: find a way to do this without an int index
u.read( u.i_at( loc.obtain( u ) ) );
u.read( *loc.obtain( u ) );
}
} else {
add_msg( _( "Never mind." ) );
Expand Down
36 changes: 18 additions & 18 deletions src/item_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class item_location::impl
virtual type where() const = 0;
virtual tripoint position() const = 0;
virtual std::string describe( const Character * ) const = 0;
virtual int obtain( Character &, int ) = 0;
virtual item_location obtain( Character &, int ) = 0;
virtual int obtain_cost( const Character &, int ) const = 0;
virtual void remove_item() = 0;
virtual void serialize( JsonOut &js ) const = 0;
Expand Down Expand Up @@ -127,9 +127,9 @@ class item_location::impl::nowhere : public item_location::impl
return "";
}

int obtain( Character &, int ) override {
item_location obtain( Character &, int ) override {
debugmsg( "invalid use of nowhere item_location" );
return INT_MIN;
return item_location();
}

int obtain_cost( const Character &, int ) const override {
Expand Down Expand Up @@ -189,16 +189,16 @@ class item_location::impl::item_on_map : public item_location::impl
return res;
}

int obtain( Character &ch, int qty ) override {
item_location obtain( Character &ch, int qty ) override {
ch.moves -= obtain_cost( ch, qty );

item obj = target()->split( qty );
if( !obj.is_null() ) {
return ch.get_item_position( &ch.i_add( obj, should_stack ) );
return item_location( ch, &ch.i_add( obj, should_stack ) );
} else {
int inv = ch.get_item_position( &ch.i_add( *target(), should_stack ) );
item *inv = &ch.i_add( *target(), should_stack );
remove_item();
return inv;
return item_location( ch, inv );
}
}

Expand Down Expand Up @@ -307,21 +307,21 @@ class item_location::impl::item_on_person : public item_location::impl
}
}

int obtain( Character &ch, int qty ) override {
item_location obtain( Character &ch, int qty ) override {
ch.mod_moves( -obtain_cost( ch, qty ) );

if( &ch.i_at( ch.get_item_position( target() ) ) == target() ) {
// item already in target characters inventory at base of stack
return ch.get_item_position( target() );
return item_location( ch, target() );
}

item obj = target()->split( qty );
if( !obj.is_null() ) {
return ch.get_item_position( &ch.i_add( obj, should_stack ) );
return item_location( ch, &ch.i_add( obj, should_stack ) );
} else {
int inv = ch.get_item_position( &ch.i_add( *target(), should_stack ) );
item *inv = &ch.i_add( *target(), should_stack );
remove_item(); // This also takes off the item from whoever wears it.
return inv;
return item_location( ch, inv );
}
}

Expand Down Expand Up @@ -434,16 +434,16 @@ class item_location::impl::item_on_vehicle : public item_location::impl
return res;
}

int obtain( Character &ch, int qty ) override {
item_location obtain( Character &ch, int qty ) override {
ch.moves -= obtain_cost( ch, qty );

item obj = target()->split( qty );
if( !obj.is_null() ) {
return ch.get_item_position( &ch.i_add( obj, should_stack ) );
return item_location( ch, &ch.i_add( obj, should_stack ) );
} else {
int inv = ch.get_item_position( &ch.i_add( *target(), should_stack ) );
item *inv = &ch.i_add( *target(), should_stack );
remove_item();
return inv;
return item_location( ch, inv );
}
}

Expand Down Expand Up @@ -581,11 +581,11 @@ std::string item_location::describe( const Character *ch ) const
return ptr->describe( ch );
}

int item_location::obtain( Character &ch, int qty )
item_location item_location::obtain( Character &ch, int qty )
{
if( !ptr->valid() ) {
debugmsg( "item location does not point to valid item" );
return INT_MIN;
return item_location();
}
return ptr->obtain( ch, qty );
}
Expand Down
4 changes: 2 additions & 2 deletions src/item_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class item_location
* @warning caller should restack inventory if item is to remain in it
* @warning all further operations using this class are invalid
* @warning it is unsafe to call this within unsequenced operations (see #15542)
* @return inventory position for the item */
int obtain( Character &ch, int qty = -1 );
* @return item_location for the item */
item_location obtain( Character &ch, int qty = -1 );

/** Calculate (but do not deduct) number of moves required to obtain an item
* @see item_location::obtain */
Expand Down
4 changes: 2 additions & 2 deletions src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2716,7 +2716,7 @@ int holster_actor::use( player &p, item &it, bool, const tripoint & ) const
return 0;
}

store( p, it, p.i_at( loc.obtain( p ) ) );
store( p, it, *loc.obtain( p ) );
}

return 0;
Expand Down Expand Up @@ -4186,7 +4186,7 @@ int saw_barrel_actor::use( player &p, item &it, bool t, const tripoint & ) const
return 0;
}

item &obj = p.i_at( loc.obtain( p ) );
item &obj = *loc.obtain( p );
p.add_msg_if_player( _( "You saw down the barrel of your %s." ), obj.tname() );
obj.contents.emplace_back( "barrel_small", calendar::turn );

Expand Down
2 changes: 1 addition & 1 deletion src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ void npc::do_npc_read()
if( !ch ) {
return;
}
item &chosen = i_at( loc.obtain( *ch ) );
item &chosen = *loc.obtain( *ch );
if( can_read( chosen, fail_reasons ) ) {
if( g->u.sees( pos() ) ) {
add_msg( m_info, _( "%s starts reading." ), disp_name() );
Expand Down