Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19695b4

Browse files
committed
EventManager/eventful: Pass building ID pointers to event handlers
Previously, there was some disagreement over whether event handlers such as Buildings::updateBuildings() took building pointers or building IDs shoved into pointers. It turned out to be the latter, which, unfortunately, did not compile on x64. Passing building IDs isn't possible in all cases, because building event handlers can be called for recently-deleted buildings too. Pointers to building IDs do work reliably, though. Fixes #1003
1 parent 33060c7 commit 19695b4

3 files changed

Lines changed: 52 additions & 52 deletions

File tree

library/modules/Buildings.cpp

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,52 +1182,52 @@ void Buildings::clearBuildings(color_ostream& out) {
11821182

11831183
void Buildings::updateBuildings(color_ostream& out, void* ptr)
11841184
{
1185-
// int32_t id = (int32_t)ptr;
1186-
// auto building = df::building::find(id);
1187-
1188-
// if (building)
1189-
// {
1190-
// // Already cached -> weird, so bail out
1191-
// if (corner1.count(id))
1192-
// return;
1193-
// // Civzones cannot be cached because they can
1194-
// // overlap each other and normal buildings.
1195-
// if (!building->isSettingOccupancy())
1196-
// return;
1197-
1198-
// df::coord p1(min(building->x1, building->x2), min(building->y1,building->y2), building->z);
1199-
// df::coord p2(max(building->x1, building->x2), max(building->y1,building->y2), building->z);
1200-
1201-
// corner1[id] = p1;
1202-
// corner2[id] = p2;
1203-
1204-
// for ( int32_t x = p1.x; x <= p2.x; x++ ) {
1205-
// for ( int32_t y = p1.y; y <= p2.y; y++ ) {
1206-
// df::coord pt(x,y,building->z);
1207-
// if (containsTile(building, pt, false))
1208-
// locationToBuilding[pt] = id;
1209-
// }
1210-
// }
1211-
// }
1212-
// else if (corner1.count(id))
1213-
// {
1214-
// //existing building: destroy it
1215-
// df::coord p1 = corner1[id];
1216-
// df::coord p2 = corner2[id];
1217-
1218-
// for ( int32_t x = p1.x; x <= p2.x; x++ ) {
1219-
// for ( int32_t y = p1.y; y <= p2.y; y++ ) {
1220-
// df::coord pt(x,y,p1.z);
1221-
1222-
// auto cur = locationToBuilding.find(pt);
1223-
// if (cur != locationToBuilding.end() && cur->second == id)
1224-
// locationToBuilding.erase(cur);
1225-
// }
1226-
// }
1227-
1228-
// corner1.erase(id);
1229-
// corner2.erase(id);
1230-
// }
1185+
int32_t id = *((int32_t*)ptr);
1186+
auto building = df::building::find(id);
1187+
1188+
if (building)
1189+
{
1190+
// Already cached -> weird, so bail out
1191+
if (corner1.count(id))
1192+
return;
1193+
// Civzones cannot be cached because they can
1194+
// overlap each other and normal buildings.
1195+
if (!building->isSettingOccupancy())
1196+
return;
1197+
1198+
df::coord p1(min(building->x1, building->x2), min(building->y1,building->y2), building->z);
1199+
df::coord p2(max(building->x1, building->x2), max(building->y1,building->y2), building->z);
1200+
1201+
corner1[id] = p1;
1202+
corner2[id] = p2;
1203+
1204+
for ( int32_t x = p1.x; x <= p2.x; x++ ) {
1205+
for ( int32_t y = p1.y; y <= p2.y; y++ ) {
1206+
df::coord pt(x,y,building->z);
1207+
if (containsTile(building, pt, false))
1208+
locationToBuilding[pt] = id;
1209+
}
1210+
}
1211+
}
1212+
else if (corner1.count(id))
1213+
{
1214+
//existing building: destroy it
1215+
df::coord p1 = corner1[id];
1216+
df::coord p2 = corner2[id];
1217+
1218+
for ( int32_t x = p1.x; x <= p2.x; x++ ) {
1219+
for ( int32_t y = p1.y; y <= p2.y; y++ ) {
1220+
df::coord pt(x,y,p1.z);
1221+
1222+
auto cur = locationToBuilding.find(pt);
1223+
if (cur != locationToBuilding.end() && cur->second == id)
1224+
locationToBuilding.erase(cur);
1225+
}
1226+
}
1227+
1228+
corner1.erase(id);
1229+
corner2.erase(id);
1230+
}
12311231
}
12321232

12331233
void Buildings::getStockpileContents(df::building_stockpilest *stockpile, std::vector<df::item*> *items)

library/modules/EventManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
273273
}
274274
for ( size_t a = 0; a < df::global::world->buildings.all.size(); a++ ) {
275275
df::building* b = df::global::world->buildings.all[a];
276-
Buildings::updateBuildings(out, (void*)b);
276+
Buildings::updateBuildings(out, (void*)&(b->id));
277277
buildings.insert(b->id);
278278
}
279279
lastSyndromeTime = -1;
@@ -609,7 +609,7 @@ static void manageBuildingEvent(color_ostream& out) {
609609
buildings.insert(a);
610610
for ( auto b = copy.begin(); b != copy.end(); b++ ) {
611611
EventHandler bob = (*b).second;
612-
bob.eventHandler(out, (void*)intptr_t(a));
612+
bob.eventHandler(out, (void*)&a);
613613
}
614614
}
615615
nextBuilding = *df::global::building_next_id;
@@ -625,7 +625,7 @@ static void manageBuildingEvent(color_ostream& out) {
625625

626626
for ( auto b = copy.begin(); b != copy.end(); b++ ) {
627627
EventHandler bob = (*b).second;
628-
bob.eventHandler(out, (void*)intptr_t(id));
628+
bob.eventHandler(out, (void*)&id);
629629
}
630630
a = buildings.erase(a);
631631
}

plugins/eventful.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ void ev_mng_invasion(color_ostream& out, void* ptr)
183183
}
184184
static void ev_mng_building(color_ostream& out, void* ptr)
185185
{
186-
int32_t myId=*(int32_t*)&ptr;
187-
onBuildingCreatedDestroyed(out,myId);
186+
int32_t id = *((int32_t*)ptr);
187+
onBuildingCreatedDestroyed(out, id);
188188
}
189189
static void ev_mng_inventory(color_ostream& out, void* ptr)
190190
{
@@ -289,7 +289,7 @@ IMPLEMENT_VMETHOD_INTERPOSE(furnace_hook, fillSidebarMenu);
289289

290290
struct product_hook : item_product {
291291
typedef item_product interpose_base;
292-
292+
293293
DEFINE_VMETHOD_INTERPOSE(
294294
void, produce,
295295
(df::unit *unit,

0 commit comments

Comments
 (0)