Made EQUIPMENT_CHANGE event trigger for new units.

develop
expwnent 2014-07-03 08:10:12 -04:00
parent 9d2c7a1abc
commit d8c3a05f42
1 changed files with 48 additions and 40 deletions

@ -679,51 +679,59 @@ static void manageEquipmentEvent(color_ostream& out) {
*/ */
auto oldEquipment = equipmentLog.find(unit->id); auto oldEquipment = equipmentLog.find(unit->id);
if ( oldEquipment != equipmentLog.end() ) { bool hadEquipment = oldEquipment != equipmentLog.end();
vector<InventoryItem>& v = (*oldEquipment).second; vector<InventoryItem>* temp;
for ( auto b = v.begin(); b != v.end(); b++ ) { if ( hadEquipment ) {
InventoryItem& i = *b; temp = &((*oldEquipment).second);
itemIdToInventoryItem[i.itemId] = i; } else {
} temp = new vector<InventoryItem>;
for ( size_t b = 0; b < unit->inventory.size(); b++ ) { }
df::unit_inventory_item* dfitem_new = unit->inventory[b]; //vector<InventoryItem>& v = (*oldEquipment).second;
currentlyEquipped.insert(dfitem_new->item->id); vector<InventoryItem>& v = *temp;
InventoryItem item_new(dfitem_new->item->id, *dfitem_new); for ( auto b = v.begin(); b != v.end(); b++ ) {
auto c = itemIdToInventoryItem.find(dfitem_new->item->id); InventoryItem& i = *b;
if ( c == itemIdToInventoryItem.end() ) { itemIdToInventoryItem[i.itemId] = i;
//new item equipped (probably just picked up) }
InventoryChangeData data(unit->id, NULL, &item_new); for ( size_t b = 0; b < unit->inventory.size(); b++ ) {
for ( auto h = copy.begin(); h != copy.end(); h++ ) { df::unit_inventory_item* dfitem_new = unit->inventory[b];
EventHandler handle = (*h).second; currentlyEquipped.insert(dfitem_new->item->id);
handle.eventHandler(out, (void*)&data); InventoryItem item_new(dfitem_new->item->id, *dfitem_new);
} auto c = itemIdToInventoryItem.find(dfitem_new->item->id);
continue; if ( c == itemIdToInventoryItem.end() ) {
} //new item equipped (probably just picked up)
InventoryItem item_old = (*c).second; InventoryChangeData data(unit->id, NULL, &item_new);
df::unit_inventory_item& item0 = item_old.item;
df::unit_inventory_item& item1 = item_new.item;
if ( item0.mode == item1.mode && item0.body_part_id == item1.body_part_id && item0.wound_id == item1.wound_id )
continue;
//some sort of change in how it's equipped
InventoryChangeData data(unit->id, &item_old, &item_new);
for ( auto h = copy.begin(); h != copy.end(); h++ ) { for ( auto h = copy.begin(); h != copy.end(); h++ ) {
EventHandler handle = (*h).second; EventHandler handle = (*h).second;
handle.eventHandler(out, (void*)&data); handle.eventHandler(out, (void*)&data);
} }
continue;
} }
//check for dropped items InventoryItem item_old = (*c).second;
for ( auto b = v.begin(); b != v.end(); b++ ) {
InventoryItem i = *b; df::unit_inventory_item& item0 = item_old.item;
if ( currentlyEquipped.find(i.itemId) != currentlyEquipped.end() ) df::unit_inventory_item& item1 = item_new.item;
continue; if ( item0.mode == item1.mode && item0.body_part_id == item1.body_part_id && item0.wound_id == item1.wound_id )
//TODO: delete ptr if invalid continue;
InventoryChangeData data(unit->id, &i, NULL); //some sort of change in how it's equipped
for ( auto h = copy.begin(); h != copy.end(); h++ ) {
EventHandler handle = (*h).second; InventoryChangeData data(unit->id, &item_old, &item_new);
handle.eventHandler(out, (void*)&data); for ( auto h = copy.begin(); h != copy.end(); h++ ) {
} EventHandler handle = (*h).second;
handle.eventHandler(out, (void*)&data);
}
}
if ( !hadEquipment )
delete temp;
//check for dropped items
for ( auto b = v.begin(); b != v.end(); b++ ) {
InventoryItem i = *b;
if ( currentlyEquipped.find(i.itemId) != currentlyEquipped.end() )
continue;
//TODO: delete ptr if invalid
InventoryChangeData data(unit->id, &i, NULL);
for ( auto h = copy.begin(); h != copy.end(); h++ ) {
EventHandler handle = (*h).second;
handle.eventHandler(out, (void*)&data);
} }
} }