MCPcopy Create free account
hub / github.com/DFHack/dfhack / manageBuildingEvent

Function manageBuildingEvent

library/modules/EventManager.cpp:828–876  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

826}
827
828static void manageBuildingEvent(color_ostream& out) {
829 if (!df::global::world)
830 return;
831 if (!df::global::building_next_id)
832 return;
833 /*
834 * TODO: could be faster
835 * consider looking at jobs: building creation / destruction
836 **/
837 multimap<Plugin*,EventHandler> copy(handlers[EventType::BUILDING].begin(), handlers[EventType::BUILDING].end());
838 //first alert people about new buildings
839 vector<int32_t> new_buildings;
840 for ( int32_t a = nextBuilding; a < *df::global::building_next_id; a++ ) {
841 int32_t index = df::building::binsearch_index(df::global::world->buildings.all, a);
842 if ( index == -1 ) {
843 //out.print("%s, line %d: Couldn't find new building with id %d.\n", __FILE__, __LINE__, a);
844 //the tricky thing is that when the game first starts, it's ok to skip buildings, but otherwise, if you skip buildings, something is probably wrong. TODO: make this smarter
845 continue;
846 }
847 buildings.insert(a);
848 new_buildings.emplace_back(a);
849
850 }
851 nextBuilding = *df::global::building_next_id;
852
853 //now alert people about destroyed buildings
854 for ( auto it = buildings.begin(); it != buildings.end(); ) {
855 int32_t id = *it;
856 int32_t index = df::building::binsearch_index(df::global::world->buildings.all,id);
857 if ( index != -1 ) {
858 ++it;
859 continue;
860 }
861
862 for (auto &[_,handle] : copy) {
863 DEBUG(log,out).print("calling handler for destroyed building event\n");
864 run_handler(out, EventType::BUILDING, handle, (void*)intptr_t(id));
865 }
866 it = buildings.erase(it);
867 }
868
869 //alert people about newly created buildings
870 std::for_each(new_buildings.begin(), new_buildings.end(), [&](int32_t building){
871 for (auto &[_,handle] : copy) {
872 DEBUG(log,out).print("calling handler for created building event\n");
873 run_handler(out, EventType::BUILDING, handle, (void*)intptr_t(building));
874 }
875 });
876}
877
878static void manageConstructionEvent(color_ostream& out) {
879 if (!df::global::world)

Callers

nothing calls this directly

Calls 7

binsearch_indexFunction · 0.85
run_handlerFunction · 0.85
beginMethod · 0.45
endMethod · 0.45
insertMethod · 0.45
printMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected