File confec.cpp changed (mode: 100644) (index e7529a4..7ee339f) |
... |
... |
int main( int argc, char* argv[] ){ |
1155 |
1155 |
// The item and the player character exist. |
// The item and the player character exist. |
1156 |
1156 |
auto &player = world.entities[world.followEntity]; |
auto &player = world.entities[world.followEntity]; |
1157 |
1157 |
fworld::Inventory inv; |
fworld::Inventory inv; |
1158 |
|
inv.set( item_name, 1 ); |
|
|
1158 |
|
inv.set( item_name, 1.0 ); |
1159 |
1159 |
if( world.inventoryCanTakeAll( |
if( world.inventoryCanTakeAll( |
1160 |
1160 |
player.inventory, inv, inventory_slots ) ){ |
player.inventory, inv, inventory_slots ) ){ |
1161 |
1161 |
world.transferInventory( |
world.transferInventory( |
|
... |
... |
bool WorldInteract( int ent_index ){ |
3452 |
3452 |
world.items[player.inventory[i].first]; |
world.items[player.inventory[i].first]; |
3453 |
3453 |
if( item.flavor > 0 ){ |
if( item.flavor > 0 ){ |
3454 |
3454 |
// Remove the item from the player's inventory. |
// Remove the item from the player's inventory. |
3455 |
|
auto &count = player.inventory[i].second; |
|
|
3455 |
|
double &count = player.inventory[i].second; |
3456 |
3456 |
count--; |
count--; |
3457 |
|
if( count == 0 ){ |
|
|
3457 |
|
if( count == 0.0 ){ |
3458 |
3458 |
// Remove the item from the player's inventory. |
// Remove the item from the player's inventory. |
3459 |
3459 |
player.inventory.erase( player.inventory.begin() + i ); |
player.inventory.erase( player.inventory.begin() + i ); |
3460 |
3460 |
deselecting = true; |
deselecting = true; |
|
... |
... |
bool WorldInteract( int ent_index ){ |
3487 |
3487 |
fworld::Inventory selectedItems = {}; |
fworld::Inventory selectedItems = {}; |
3488 |
3488 |
for( size_t i = 0; i < item_selected.size(); i++ ){ |
for( size_t i = 0; i < item_selected.size(); i++ ){ |
3489 |
3489 |
if( item_selected[i] ){ |
if( item_selected[i] ){ |
3490 |
|
selectedItems.push_back( { player.inventory[i].first, -1 } ); |
|
|
3490 |
|
selectedItems.push_back( { player.inventory[i].first, -1.0 } ); |
3491 |
3491 |
} |
} |
3492 |
3492 |
} |
} |
3493 |
3493 |
// Check if the player is using the required items. |
// Check if the player is using the required items. |
|
... |
... |
void DrawInventory( |
5247 |
5247 |
for( auto &item : inventory ){ |
for( auto &item : inventory ){ |
5248 |
5248 |
item_x += world.items[item.first].icon.width * scale; |
item_x += world.items[item.first].icon.width * scale; |
5249 |
5249 |
// Only show a number for stacks of multiple, zero, or negative items. |
// Only show a number for stacks of multiple, zero, or negative items. |
5250 |
|
if( item.second != 1 ){ |
|
|
5250 |
|
if( item.second != 1.0 ){ |
5251 |
5251 |
fgl::drawText( |
fgl::drawText( |
5252 |
|
std::to_string( item.second ), |
|
|
5252 |
|
convo.stringifyNumber( item.second ), |
5253 |
5253 |
font_inventory, |
font_inventory, |
5254 |
5254 |
item_x, |
item_x, |
5255 |
5255 |
pos_y, |
pos_y, |
|
... |
... |
void GameLoop( double d ){ |
5594 |
5594 |
auto &item = |
auto &item = |
5595 |
5595 |
world.items[player.inventory[i].first]; |
world.items[player.inventory[i].first]; |
5596 |
5596 |
if( item_selected[i] |
if( item_selected[i] |
5597 |
|
&& player.inventory[i].second > 0 |
|
|
5597 |
|
&& player.inventory[i].second > 0.0 |
5598 |
5598 |
&& item.entity.sprite.success ){ |
&& item.entity.sprite.success ){ |
5599 |
5599 |
AddEntity( item.entity, place_x, place_y ); |
AddEntity( item.entity, place_x, place_y ); |
5600 |
5600 |
player.inventory[i].second--; |
player.inventory[i].second--; |
5601 |
|
if( player.inventory[i].second == 0 ){ |
|
|
5601 |
|
if( player.inventory[i].second == 0.0 ){ |
5602 |
5602 |
// Remove the item from the player's inventory. |
// Remove the item from the player's inventory. |
5603 |
5603 |
player.inventory.erase( player.inventory.begin() + i ); |
player.inventory.erase( player.inventory.begin() + i ); |
5604 |
5604 |
// Deselect all. |
// Deselect all. |
|
... |
... |
void GameLoop( double d ){ |
6268 |
6268 |
for( auto &item : world.entities[world.followEntity].inventory ){ |
for( auto &item : world.entities[world.followEntity].inventory ){ |
6269 |
6269 |
itemOffX += world.items[item.first].icon.width * round_scale; |
itemOffX += world.items[item.first].icon.width * round_scale; |
6270 |
6270 |
// Only show a number for stacks of multiple, zero, or negative items. |
// Only show a number for stacks of multiple, zero, or negative items. |
6271 |
|
if( item.second != 1 ){ |
|
|
6271 |
|
if( item.second != 1.0 ){ |
6272 |
6272 |
fgl::drawText( |
fgl::drawText( |
6273 |
|
std::to_string( item.second ), |
|
|
6273 |
|
convo.stringifyNumber( item.second ), |
6274 |
6274 |
font_inventory, |
font_inventory, |
6275 |
6275 |
itemOffX, |
itemOffX, |
6276 |
6276 |
0.0f, |
0.0f, |
File include/fworld.h changed (mode: 100755) (index ee77745..e6ed3ff) |
... |
... |
class mesa : public std::vector<std::pair<K,V>> { |
93 |
93 |
} |
} |
94 |
94 |
}; |
}; |
95 |
95 |
|
|
96 |
|
typedef mesa<std::string,int> Inventory; |
|
|
96 |
|
typedef mesa<std::string,double> Inventory; |
97 |
97 |
|
|
98 |
98 |
typedef std::vector<std::vector<int>> MapLayer; |
typedef std::vector<std::vector<int>> MapLayer; |
99 |
99 |
|
|
|
... |
... |
void World::loadItems( std::string filePath ){ |
929 |
929 |
entity.width = entity.sprite.width; |
entity.width = entity.sprite.width; |
930 |
930 |
entity.height = entity.sprite.height; |
entity.height = entity.sprite.height; |
931 |
931 |
entity.type = "pickup"; |
entity.type = "pickup"; |
932 |
|
entity.inventory.set( itemName, 1 ); |
|
|
932 |
|
entity.inventory.set( itemName, 1.0 ); |
933 |
933 |
entity.name = itemName; |
entity.name = itemName; |
934 |
934 |
} |
} |
935 |
935 |
// Add the item. |
// Add the item. |
|
... |
... |
void World::loadItems( std::string filePath ){ |
955 |
955 |
void World::deleteInventoryZeroes( Inventory &inv ){ |
void World::deleteInventoryZeroes( Inventory &inv ){ |
956 |
956 |
// Delete zero-quantity items from the inventory. |
// Delete zero-quantity items from the inventory. |
957 |
957 |
for( auto it = inv.begin(); it != inv.end(); ){ |
for( auto it = inv.begin(); it != inv.end(); ){ |
958 |
|
if( it->second == 0 ){ |
|
|
958 |
|
if( it->second == 0.0 ){ |
959 |
959 |
it = inv.erase( it ); |
it = inv.erase( it ); |
960 |
960 |
}else{ |
}else{ |
961 |
961 |
it++; |
it++; |
|
... |
... |
char World::craft( |
993 |
993 |
// Remove non-consumable items from the list. |
// Remove non-consumable items from the list. |
994 |
994 |
for( size_t i = 0; i < itemNames.size(); i++ ){ |
for( size_t i = 0; i < itemNames.size(); i++ ){ |
995 |
995 |
auto &name = itemNames[i]; |
auto &name = itemNames[i]; |
996 |
|
if( recipe[name] < 1 ){ |
|
|
996 |
|
if( recipe[name] < 1.0 ){ |
997 |
997 |
// The recipe does not require a quantity of the item. |
// The recipe does not require a quantity of the item. |
998 |
998 |
itemNames.erase( itemNames.begin() + i ); |
itemNames.erase( itemNames.begin() + i ); |
999 |
999 |
}else if( !ent.inventory.contains( name ) |
}else if( !ent.inventory.contains( name ) |
|
... |
... |
Inventory World::decodeInventory( std::string str_inv ){ |
1062 |
1062 |
auto AddItem = [&]( std::string item ){ |
auto AddItem = [&]( std::string item ){ |
1063 |
1063 |
// Split an item declaration between name and count. |
// Split an item declaration between name and count. |
1064 |
1064 |
size_t semi_at = item.find( ';' ); |
size_t semi_at = item.find( ';' ); |
1065 |
|
std::pair<std::string,int> inv_item; |
|
|
1065 |
|
std::pair<std::string,double> inv_item; |
1066 |
1066 |
if( semi_at > 0 && semi_at < item.size() - 1 ){ |
if( semi_at > 0 && semi_at < item.size() - 1 ){ |
1067 |
1067 |
inv_item = std::make_pair( |
inv_item = std::make_pair( |
1068 |
1068 |
item.substr( 0, semi_at ), |
item.substr( 0, semi_at ), |
1069 |
|
(int)safeStold( item.substr( semi_at + 1 ) ) |
|
|
1069 |
|
(double)safeStold( item.substr( semi_at + 1 ) ) |
1070 |
1070 |
); |
); |
1071 |
1071 |
// Only add the item if the count is positive or negative. |
// Only add the item if the count is positive or negative. |
1072 |
|
if( inv_item.second != 0 ){ |
|
|
1072 |
|
if( inv_item.second != 0.0 ){ |
1073 |
1073 |
inv.push_back( inv_item ); |
inv.push_back( inv_item ); |
1074 |
1074 |
} |
} |
1075 |
1075 |
} |
} |
|
... |
... |
Inventory World::decodeInventory( std::string str_inv ){ |
1086 |
1086 |
// inv_a can fit any one of the items from inv_b. |
// inv_a can fit any one of the items from inv_b. |
1087 |
1087 |
bool World::inventoryCanTakeAny( Inventory &inv_a, Inventory &inv_b, int max_slots ){ |
bool World::inventoryCanTakeAny( Inventory &inv_a, Inventory &inv_b, int max_slots ){ |
1088 |
1088 |
for( auto &item : inv_b ){ |
for( auto &item : inv_b ){ |
1089 |
|
if( item.second > 0 ){ |
|
|
1089 |
|
if( item.second > 0.0 ){ |
1090 |
1090 |
// The item has a positive quantity. |
// The item has a positive quantity. |
1091 |
1091 |
if( inv_a.contains( item.first ) ){ |
if( inv_a.contains( item.first ) ){ |
1092 |
1092 |
// The item already exists in inventory A. |
// The item already exists in inventory A. |
|
... |
... |
bool World::inventoryCanTakeAny( Inventory &inv_a, Inventory &inv_b, int max_slo |
1106 |
1106 |
// inv_a can fit all of the items from inv_b. |
// inv_a can fit all of the items from inv_b. |
1107 |
1107 |
bool World::inventoryCanTakeAll( Inventory &inv_a, Inventory &inv_b, int max_slots ){ |
bool World::inventoryCanTakeAll( Inventory &inv_a, Inventory &inv_b, int max_slots ){ |
1108 |
1108 |
for( auto &item : inv_b ){ |
for( auto &item : inv_b ){ |
1109 |
|
if( item.second > 0 ){ |
|
|
1109 |
|
if( item.second > 0.0 ){ |
1110 |
1110 |
// The item has a positive quantity. |
// The item has a positive quantity. |
1111 |
1111 |
if( inv_a.contains( item.first ) ){ |
if( inv_a.contains( item.first ) ){ |
1112 |
1112 |
// The item already exists in inventory A. |
// The item already exists in inventory A. |
|
... |
... |
bool World::inventoryCanTakeAll( Inventory &inv_a, Inventory &inv_b, int max_slo |
1126 |
1126 |
void World::transferInventory( Inventory &inv_a, Inventory &inv_b, int max_slots ){ |
void World::transferInventory( Inventory &inv_a, Inventory &inv_b, int max_slots ){ |
1127 |
1127 |
// Move all items possible from inv_a to inv_b. |
// Move all items possible from inv_a to inv_b. |
1128 |
1128 |
for( auto &item_a : inv_a ){ |
for( auto &item_a : inv_a ){ |
1129 |
|
if( item_a.second > 0 ){ |
|
|
1129 |
|
if( item_a.second > 0.0 ){ |
1130 |
1130 |
// item_a has a positive quantity. |
// item_a has a positive quantity. |
1131 |
1131 |
if( inv_b.contains( item_a.first ) ){ |
if( inv_b.contains( item_a.first ) ){ |
1132 |
1132 |
// item_a already exists in inventory B. |
// item_a already exists in inventory B. |
|
... |
... |
void World::transferInventory( Inventory &inv_a, Inventory &inv_b, int max_slots |
1134 |
1134 |
if( item_a.first == item_b.first // Same item. |
if( item_a.first == item_b.first // Same item. |
1135 |
1135 |
&& item_b.second < items[item_b.first].stack ){ |
&& item_b.second < items[item_b.first].stack ){ |
1136 |
1136 |
// There is room on the stack for more. |
// There is room on the stack for more. |
1137 |
|
int transfer_count = std::min( item_a.second, |
|
|
1137 |
|
double transfer_count = std::min( item_a.second, |
1138 |
1138 |
items[item_b.first].stack - item_b.second ); |
items[item_b.first].stack - item_b.second ); |
1139 |
1139 |
// Put the items into inventory B. |
// Put the items into inventory B. |
1140 |
1140 |
item_b.second += transfer_count; |
item_b.second += transfer_count; |
|
... |
... |
void World::transferInventory( Inventory &inv_a, Inventory &inv_b, int max_slots |
1146 |
1146 |
// Item does not yet exist in inventory B. |
// Item does not yet exist in inventory B. |
1147 |
1147 |
if( (int)inv_b.size() < max_slots ){ |
if( (int)inv_b.size() < max_slots ){ |
1148 |
1148 |
// Inventory B has a free slot. |
// Inventory B has a free slot. |
1149 |
|
int transfer_count = |
|
1150 |
|
std::min( item_a.second, items[item_a.first].stack ); |
|
|
1149 |
|
double transfer_count = |
|
1150 |
|
std::min( item_a.second, (double)items[item_a.first].stack ); |
1151 |
1151 |
// Put the items into inv_b. |
// Put the items into inv_b. |
1152 |
1152 |
inv_b.push_back( { item_a.first, transfer_count } ); |
inv_b.push_back( { item_a.first, transfer_count } ); |
1153 |
1153 |
// Remove the items from inv_a. |
// Remove the items from inv_a. |