| 297 | } |
| 298 | |
| 299 | void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event event) { |
| 300 | static bool doOnce = false; |
| 301 | // const string eventNames[] = {"world loaded", "world unloaded", "map loaded", "map unloaded", "viewscreen changed", "core initialized", "begin unload", "paused", "unpaused"}; |
| 302 | // out.print("%s,%d: onStateChange %d: \"%s\"\n", __FILE__, __LINE__, (int32_t)event, eventNames[event].c_str()); |
| 303 | if ( !doOnce ) { |
| 304 | //TODO: put this somewhere else |
| 305 | doOnce = true; |
| 306 | EventHandler buildingHandler(nullptr, Buildings::updateBuildings, 100); |
| 307 | DFHack::EventManager::registerListener(EventType::BUILDING, buildingHandler); |
| 308 | //out.print("Registered listeners.\n %d", __LINE__); |
| 309 | } |
| 310 | if ( event == DFHack::SC_MAP_UNLOADED ) { |
| 311 | lastJobId = -1; |
| 312 | startedJobs.clear(); |
| 313 | seenJobs.clear(); |
| 314 | prevJobs.clear(); |
| 315 | tickQueue.clear(); |
| 316 | livingUnits.clear(); |
| 317 | buildings.clear(); |
| 318 | constructions.clear(); |
| 319 | equipmentLog.clear(); |
| 320 | activeUnits.clear(); |
| 321 | |
| 322 | Buildings::clearBuildings(out); |
| 323 | lastReport = -1; |
| 324 | lastReportUnitAttack = -1; |
| 325 | gameLoaded = false; |
| 326 | |
| 327 | multimap<Plugin*,EventHandler> copy(handlers[EventType::UNLOAD].begin(), handlers[EventType::UNLOAD].end()); |
| 328 | for (auto &[_,handle] : copy) { |
| 329 | DEBUG(log,out).print("calling handler for map unloaded state change event\n"); |
| 330 | run_handler(out, EventType::UNLOAD, handle, nullptr); |
| 331 | } |
| 332 | } else if ( event == DFHack::SC_MAP_LOADED ) { |
| 333 | /* |
| 334 | int32_t tick = df::global::world->frame_counter; |
| 335 | multimap<int32_t,EventHandler> newTickQueue; |
| 336 | for ( auto i = tickQueue.begin(); i != tickQueue.end(); i++ ) |
| 337 | newTickQueue.insert(pair<int32_t,EventHandler>(tick+(*i).first, (*i).second)); |
| 338 | tickQueue.clear(); |
| 339 | tickQueue.insert(newTickQueue.begin(), newTickQueue.end()); |
| 340 | //out.print("%s,%d: on load, frame_counter = %d\n", __FILE__, __LINE__, tick); |
| 341 | */ |
| 342 | //tickQueue.clear(); |
| 343 | if (!df::global::item_next_id) |
| 344 | return; |
| 345 | if (!df::global::building_next_id) |
| 346 | return; |
| 347 | if (!df::global::job_next_id) |
| 348 | return; |
| 349 | if (!df::global::plotinfo) |
| 350 | return; |
| 351 | if (!df::global::world) |
| 352 | return; |
| 353 | |
| 354 | nextItem = *df::global::item_next_id; |
| 355 | nextBuilding = *df::global::building_next_id; |
| 356 | nextInvasion = df::global::plotinfo->invasions.next_id; |