eventExample: make unitAttack a bit safer

develop
lethosor 2019-10-05 21:05:26 -04:00
parent 08aeb6faeb
commit 582169e0a5
1 changed files with 8 additions and 1 deletions

@ -175,8 +175,15 @@ void unitAttack(color_ostream& out, void* ptr) {
EventManager::UnitAttackData* data = (EventManager::UnitAttackData*)ptr;
out.print("unit %d attacks unit %d\n", data->attacker, data->defender);
df::unit* defender = df::unit::find(data->defender);
if (!defender) {
out.printerr("defender %d does not exist\n", data->defender);
return;
}
int32_t woundIndex = df::unit_wound::binsearch_index(defender->body.wounds, data->wound);
df::unit_wound* wound = defender->body.wounds[woundIndex];
df::unit_wound* wound = vector_get(defender->body.wounds, woundIndex);
if (!wound) {
return;
}
set<int32_t> parts;
for ( auto a = wound->parts.begin(); a != wound->parts.end(); a++ ) {
parts.insert((*a)->body_part_id);