| 970 | } |
| 971 | |
| 972 | static void manageEquipmentEvent(color_ostream& out) { |
| 973 | if (!df::global::world) |
| 974 | return; |
| 975 | multimap<Plugin*,EventHandler> copy(handlers[EventType::INVENTORY_CHANGE].begin(), handlers[EventType::INVENTORY_CHANGE].end()); |
| 976 | |
| 977 | unordered_map<int32_t, InventoryItem> itemIdToInventoryItem; |
| 978 | unordered_set<int32_t> currentlyEquipped; |
| 979 | |
| 980 | vector<InventoryChangeData> equipment_pickups; |
| 981 | vector<InventoryChangeData> equipment_drops; |
| 982 | vector<InventoryChangeData> equipment_changes; |
| 983 | |
| 984 | |
| 985 | // This vector stores the pointers to newly created changed items |
| 986 | // needed as the stack allocated temporary (in the loop) is lost when we go to |
| 987 | // handle the event calls, so we move that data to the heap if its needed, |
| 988 | // and then once we are done we delete everything. |
| 989 | vector<InventoryItem*> changed_items; |
| 990 | |
| 991 | for (auto unit : df::global::world->units.all) { |
| 992 | itemIdToInventoryItem.clear(); |
| 993 | currentlyEquipped.clear(); |
| 994 | /*if ( unit->flags1.bits.inactive ) |
| 995 | continue; |
| 996 | */ |
| 997 | |
| 998 | auto oldEquipment = equipmentLog.find(unit->id); |
| 999 | bool hadEquipment = oldEquipment != equipmentLog.end(); |
| 1000 | vector<InventoryItem>* temp; |
| 1001 | if ( hadEquipment ) { |
| 1002 | temp = &((*oldEquipment).second); |
| 1003 | } else { |
| 1004 | temp = new vector<InventoryItem>; |
| 1005 | } |
| 1006 | //vector<InventoryItem>& v = (*oldEquipment).second; |
| 1007 | vector<InventoryItem>& v = *temp; |
| 1008 | for (auto & i : v) { |
| 1009 | itemIdToInventoryItem[i.itemId] = i; |
| 1010 | } |
| 1011 | for ( size_t b = 0; b < unit->inventory.size(); b++ ) { |
| 1012 | df::unit_inventory_item* dfitem_new = unit->inventory[b]; |
| 1013 | currentlyEquipped.insert(dfitem_new->item->id); |
| 1014 | InventoryItem item_new(dfitem_new->item->id, *dfitem_new); |
| 1015 | auto c = itemIdToInventoryItem.find(dfitem_new->item->id); |
| 1016 | if ( c == itemIdToInventoryItem.end() ) { |
| 1017 | //new item equipped (probably just picked up) |
| 1018 | changed_items.emplace_back(new InventoryItem(item_new)); |
| 1019 | equipment_pickups.emplace_back(unit->id, nullptr, changed_items.back()); |
| 1020 | continue; |
| 1021 | } |
| 1022 | InventoryItem item_old = c->second; |
| 1023 | |
| 1024 | df::unit_inventory_item& item0 = item_old.item; |
| 1025 | df::unit_inventory_item& item1 = item_new.item; |
| 1026 | if ( item0.mode == item1.mode && item0.body_part_id == item1.body_part_id && item0.wound_id == item1.wound_id ) |
| 1027 | continue; |
| 1028 | //some sort of change in how it's equipped |
| 1029 | changed_items.emplace_back(new InventoryItem(item_new)); |