| 1 |
sf-pippijn |
1.1 |
#include "deliantra/inventory_manager.h" |
| 2 |
|
|
#include "deliantra/data/constants.h" |
| 3 |
|
|
|
| 4 |
|
|
#include "adt/array.h" |
| 5 |
|
|
|
| 6 |
|
|
#include <algorithm> |
| 7 |
|
|
#include <cassert> |
| 8 |
|
|
|
| 9 |
|
|
static bool |
| 10 |
|
|
type_name_compare (data::item const &a, data::item const &b) |
| 11 |
|
|
{ |
| 12 |
|
|
return a.type != b.type ? a.type < b.type : a.singular < b.singular; |
| 13 |
|
|
} |
| 14 |
|
|
|
| 15 |
|
|
|
| 16 |
|
|
inventory_manager::inventory_manager () |
| 17 |
|
|
: player_item { } |
| 18 |
|
|
, ground_item { "Floor" } |
| 19 |
|
|
, container_item (&ground_item) |
| 20 |
|
|
, inventory_view (nullptr) |
| 21 |
|
|
, container_view (nullptr) |
| 22 |
|
|
{ |
| 23 |
|
|
} |
| 24 |
|
|
|
| 25 |
|
|
inventory_manager::~inventory_manager () |
| 26 |
|
|
{ |
| 27 |
|
|
} |
| 28 |
|
|
|
| 29 |
|
|
void |
| 30 |
|
|
inventory_manager::set_inventory_view (interface::inventory_handler &view) |
| 31 |
|
|
{ |
| 32 |
|
|
inventory_view = &view; |
| 33 |
|
|
} |
| 34 |
|
|
|
| 35 |
|
|
void |
| 36 |
|
|
inventory_manager::set_container_view (interface::inventory_handler &view) |
| 37 |
|
|
{ |
| 38 |
|
|
container_view = &view; |
| 39 |
|
|
} |
| 40 |
|
|
|
| 41 |
|
|
|
| 42 |
|
|
void |
| 43 |
|
|
inventory_manager::update_container () |
| 44 |
|
|
{ |
| 45 |
|
|
container_item = &ground_item; |
| 46 |
|
|
foreach (data::item const &item, inventories[player_item.tag]) |
| 47 |
|
|
if (item.flags & F_OPEN) |
| 48 |
|
|
{ |
| 49 |
|
|
assert (container_item == &ground_item); |
| 50 |
|
|
container_item = &item; |
| 51 |
|
|
#if NDEBUG |
| 52 |
|
|
break; |
| 53 |
|
|
#endif |
| 54 |
|
|
} |
| 55 |
|
|
} |
| 56 |
|
|
|
| 57 |
|
|
void |
| 58 |
|
|
inventory_manager::sort () |
| 59 |
|
|
{ |
| 60 |
|
|
foreach (u32 tag, array (player_item.tag, container_item->tag)) |
| 61 |
sf-pippijn |
1.4 |
if (tag) |
| 62 |
|
|
{ |
| 63 |
|
|
data::inventory &inv = inventories[tag]; |
| 64 |
|
|
std::stable_sort (inv.begin (), inv.end (), type_name_compare); |
| 65 |
|
|
} |
| 66 |
sf-pippijn |
1.1 |
} |
| 67 |
|
|
|
| 68 |
|
|
void |
| 69 |
|
|
inventory_manager::update_view () const |
| 70 |
|
|
{ |
| 71 |
|
|
foreach (auto pair, array (std::make_pair (&player_item, inventory_view), |
| 72 |
|
|
std::make_pair (container_item, container_view))) |
| 73 |
|
|
{ |
| 74 |
|
|
auto found = inventories.find (pair.first->tag); |
| 75 |
|
|
if (found != inventories.end ()) |
| 76 |
|
|
pair.second->update (found->second, pair.first->singular); |
| 77 |
|
|
} |
| 78 |
|
|
} |
| 79 |
|
|
|
| 80 |
|
|
void |
| 81 |
|
|
inventory_manager::update () |
| 82 |
|
|
{ |
| 83 |
|
|
update_container (); |
| 84 |
|
|
sort (); |
| 85 |
|
|
update_view (); |
| 86 |
|
|
} |
| 87 |
|
|
|
| 88 |
|
|
|
| 89 |
|
|
data::item & |
| 90 |
|
|
inventory_manager::get (u32 tag) |
| 91 |
|
|
{ |
| 92 |
|
|
if (tag == player_item.tag) |
| 93 |
|
|
return player_item; |
| 94 |
|
|
foreach (auto &pair, inventories) |
| 95 |
|
|
foreach (data::item &item, pair.second) |
| 96 |
|
|
if (item.tag == tag) |
| 97 |
|
|
return item; |
| 98 |
|
|
throw "item not found"; |
| 99 |
|
|
} |
| 100 |
|
|
|
| 101 |
|
|
void |
| 102 |
|
|
inventory_manager::delinv (u32 tag) |
| 103 |
|
|
{ |
| 104 |
|
|
inventories.erase (tag); |
| 105 |
|
|
} |
| 106 |
|
|
|
| 107 |
|
|
void |
| 108 |
|
|
inventory_manager::delitem (std::vector<u32> const &itemtags) |
| 109 |
|
|
{ |
| 110 |
|
|
assert (player_item.tag != 0); |
| 111 |
|
|
|
| 112 |
|
|
foreach (auto &pair, inventories) |
| 113 |
sf-pippijn |
1.2 |
foreach (u32 tag, itemtags) |
| 114 |
|
|
{ |
| 115 |
|
|
auto found = std::find_if ( |
| 116 |
|
|
pair.second.begin (), |
| 117 |
|
|
pair.second.end (), |
| 118 |
|
|
[tag] (data::item const &item) { |
| 119 |
|
|
return item.tag == tag; |
| 120 |
sf-pippijn |
1.1 |
} |
| 121 |
sf-pippijn |
1.2 |
); |
| 122 |
|
|
|
| 123 |
|
|
if (found != pair.second.end ()) |
| 124 |
|
|
pair.second.erase (found); |
| 125 |
|
|
} |
| 126 |
sf-pippijn |
1.1 |
|
| 127 |
|
|
update (); |
| 128 |
|
|
} |
| 129 |
|
|
|
| 130 |
|
|
static data::item & |
| 131 |
|
|
merge (data::inventory &inv, data::item const &item) |
| 132 |
|
|
{ |
| 133 |
|
|
foreach (data::item &inv_item, inv) |
| 134 |
|
|
if (inv_item.tag == item.tag) |
| 135 |
|
|
return inv_item = item; |
| 136 |
|
|
inv.push_back (item); |
| 137 |
|
|
return inv.back (); |
| 138 |
|
|
} |
| 139 |
|
|
|
| 140 |
|
|
void |
| 141 |
|
|
inventory_manager::item2 (u32 location, std::vector<data::item> const &items) |
| 142 |
|
|
{ |
| 143 |
|
|
assert (player_item.tag != 0); |
| 144 |
|
|
|
| 145 |
|
|
data::inventory &inv = inventories[location]; |
| 146 |
|
|
foreach (data::item const &item, items) |
| 147 |
|
|
merge (inv, item); |
| 148 |
|
|
|
| 149 |
|
|
update (); |
| 150 |
|
|
} |
| 151 |
|
|
|
| 152 |
|
|
void |
| 153 |
|
|
inventory_manager::player (u32 tag, u32 weight, u32 face, std::string const &name) |
| 154 |
|
|
{ |
| 155 |
|
|
player_item.tag = tag; |
| 156 |
|
|
player_item.weight = weight; |
| 157 |
|
|
player_item.face = face; |
| 158 |
|
|
player_item.singular = name; |
| 159 |
|
|
} |
| 160 |
|
|
|
| 161 |
|
|
void |
| 162 |
|
|
inventory_manager::upditem (u08 flags, u32 location, data::item const &item) |
| 163 |
|
|
{ |
| 164 |
|
|
data::item *current = &get (item.tag); |
| 165 |
|
|
|
| 166 |
|
|
if (flags & UPD_LOCATION) |
| 167 |
|
|
{ |
| 168 |
|
|
data::item save = *current; |
| 169 |
|
|
delitem ({ item.tag }); |
| 170 |
|
|
current = &merge (inventories[location], save); |
| 171 |
|
|
} |
| 172 |
|
|
|
| 173 |
|
|
if (flags & UPD_ANIM) current->anim = item.anim; |
| 174 |
|
|
if (flags & UPD_ANIMSPEED) current->animspeed = item.animspeed; |
| 175 |
|
|
if (flags & UPD_FACE) current->face = item.face; |
| 176 |
|
|
if (flags & UPD_FLAGS) current->flags = item.flags; |
| 177 |
|
|
if (flags & UPD_NROF) current->nrof = item.nrof; |
| 178 |
|
|
if (flags & UPD_WEIGHT) current->weight = item.weight; |
| 179 |
|
|
|
| 180 |
|
|
if (flags & UPD_NAME) |
| 181 |
|
|
{ |
| 182 |
|
|
current->singular = item.singular; |
| 183 |
|
|
current->plural = item.plural; |
| 184 |
|
|
} |
| 185 |
|
|
|
| 186 |
|
|
update (); |
| 187 |
|
|
} |