Skip to content

Commit

Permalink
item::combine()
Browse files Browse the repository at this point in the history
add an item::combine function that combines two items.
  • Loading branch information
KorGgenT committed Apr 22, 2020
1 parent bd26677 commit 16fb1c5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
27 changes: 26 additions & 1 deletion src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,31 @@ bool item::display_stacked_with( const item &rhs, bool check_components ) const
return !count_by_charges() && stacks_with( rhs, check_components );
}

bool item::combine( const item &rhs )
{
if( !contents.empty() || !rhs.contents.empty() ) {
return false;
}
if( !count_by_charges() ) {
return false;
}
if( !stacks_with( rhs, true ) ) {
return false;
}
if( is_comestible() ) {
const float lhs_energy = get_item_thermal_energy();
const float rhs_energy = rhs.get_item_thermal_energy();
const float combined_specific_energy = ( lhs_energy + rhs_energy ) / ( to_gram(
weight() ) + to_gram( rhs.weight() ) );
set_item_specific_energy( combined_specific_energy );
//use maximum rot between the two
set_relative_rot( std::max( get_relative_rot(),
rhs.get_relative_rot() ) );
}
charges += rhs.charges;
return true;
}

bool item::stacks_with( const item &rhs, bool check_components ) const
{
if( type != rhs.type ) {
Expand Down Expand Up @@ -8721,7 +8746,7 @@ void item::calc_temp( const int temp, const float insulation, const time_point &
last_temp_check = time;
}

float item::get_item_thermal_energy()
float item::get_item_thermal_energy() const
{
const float mass = to_gram( weight() ); // g
return 0.00001 * specific_energy * mass;
Expand Down
4 changes: 3 additions & 1 deletion src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ class item : public visitable<item>
*/
bool display_stacked_with( const item &rhs, bool check_components = false ) const;
bool stacks_with( const item &rhs, bool check_components = false ) const;
/** combines two items together if possible. returns false if it fails. */
bool combine( const item &rhs );
/**
* Merge charges of the other item into this item.
* @return true if the items have been merged, otherwise false.
Expand Down Expand Up @@ -2086,7 +2088,7 @@ class item : public visitable<item>
/**
* Get the thermal energy of the item in Joules.
*/
float get_item_thermal_energy();
float get_item_thermal_energy() const;

/** Calculates item specific energy (J/g) from temperature (K)*/
float get_specific_energy_from_temperature( float new_temperature );
Expand Down
4 changes: 2 additions & 2 deletions src/item_contents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ bool item_contents::stacks_with( const item_contents &rhs ) const
if( contents.size() != rhs.contents.size() ) {
return false;
}
return empty() || rhs.empty() ||
std::equal( contents.begin(), contents.end(),
return empty() || rhs.empty() ||
std::equal( contents.begin(), contents.end(),
rhs.contents.begin(),
[]( const item_pocket & a, const item_pocket & b ) {
return a.stacks_with( b );
Expand Down
5 changes: 2 additions & 3 deletions src/item_pocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ void item_pocket::restack()
++inner_iter;
continue;
}
if( !outer_iter->count_by_charges() && outer_iter->stacks_with( *inner_iter, true ) ) {
outer_iter->charges += inner_iter->charges;
if( outer_iter->combine( *inner_iter ) ) {
inner_iter = contents.erase( inner_iter );
outer_iter = contents.begin();
} else {
Expand Down Expand Up @@ -171,7 +170,7 @@ bool item_pocket::better_pocket( const item_pocket &rhs, const item &it ) const
bool item_pocket::stacks_with( const item_pocket &rhs ) const
{
return empty() || rhs.empty() || std::equal( contents.begin(), contents.end(),
rhs.contents.begin(), rhs.contents.end(),
rhs.contents.begin(), rhs.contents.end(),
[]( const item & a, const item & b ) {
return a.charges == b.charges && a.stacks_with( b );
} );
Expand Down

0 comments on commit 16fb1c5

Please sign in to comment.