EventManager: made Buildings module keep track of buildings so that it

can do findAtTile in constant time.
develop
expwnent 2013-01-01 22:22:31 -05:00
parent a93c0223a2
commit 4e99841862
5 changed files with 19 additions and 15 deletions

@ -181,6 +181,7 @@ DFHACK_EXPORT bool constructWithFilters(df::building *bld, std::vector<df::job_i
DFHACK_EXPORT bool deconstruct(df::building *bld);
void updateBuildings(color_ostream& out, void* ptr);
void clearBuildings(color_ostream& out);
}
}

@ -65,8 +65,6 @@ DFHACK_EXPORT bool designateNew(df::coord pos, df::construction_type type,
DFHACK_EXPORT bool designateRemove(df::coord pos, bool *immediate = NULL);
void updateConstructions(color_ostream& out, void* ptr);
}
}
#endif

@ -259,6 +259,7 @@ df::building *Buildings::findAtTile(df::coord pos)
return building;
/*
//old method: brute-force
auto &vec = df::building::get_vector();
for (size_t i = 0; i < vec.size(); i++)
{
@ -1101,10 +1102,17 @@ bool Buildings::deconstruct(df::building *bld)
return true;
}
static unordered_map<int32_t, df::coord> corner1;
static unordered_map<int32_t, df::coord> corner2;
void Buildings::clearBuildings(color_ostream& out) {
corner1.clear();
corner2.clear();
locationToBuilding.clear();
}
void Buildings::updateBuildings(color_ostream& out, void* ptr) {
static unordered_map<int32_t, df::coord> corner1;
static unordered_map<int32_t, df::coord> corner2;
out.print("Updating buildings, %s %d\n", __FILE__, __LINE__);
//out.print("Updating buildings, %s %d\n", __FILE__, __LINE__);
int32_t id = (int32_t)ptr;
if ( corner1.find(id) == corner1.end() ) {

@ -169,8 +169,3 @@ bool Constructions::designateRemove(df::coord pos, bool *immediate)
return false;
}
void Constructions::updateConstructions(color_ostream& out, void* ptr) {
//do stuff
}

@ -134,10 +134,8 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
//TODO: put this somewhere else
doOnce = true;
EventHandler buildingHandler(Buildings::updateBuildings);
EventHandler constructionHandler(Constructions::updateConstructions);
DFHack::EventManager::registerListener(EventType::BUILDING, buildingHandler, NULL);
DFHack::EventManager::registerListener(EventType::CONSTRUCTION, constructionHandler, NULL);
out.print("Registered listeners.\n %d", __LINE__);
//out.print("Registered listeners.\n %d", __LINE__);
}
if ( event == DFHack::SC_MAP_UNLOADED ) {
lastTick = 0;
@ -152,6 +150,8 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
nextBuilding = -1;
buildings.clear();
constructions.clear();
Buildings::clearBuildings(out);
} else if ( event == DFHack::SC_MAP_LOADED ) {
uint32_t tick = DFHack::World::ReadCurrentYear()*ticksPerYear
+ DFHack::World::ReadCurrentTick();
@ -344,7 +344,9 @@ static void manageBuildingEvent(color_ostream& out) {
for ( int32_t a = nextBuilding; a < *df::global::building_next_id; a++ ) {
int32_t index = df::building::binsearch_index(df::global::world->buildings.all, a);
if ( index == -1 ) {
out.print("%s, line %d: Couldn't find new building with id %d.\n", __FILE__, __LINE__, a);
//out.print("%s, line %d: Couldn't find new building with id %d.\n", __FILE__, __LINE__, a);
//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
continue;
}
buildings.insert(a);
for ( auto b = copy.begin(); b != copy.end(); b++ ) {
@ -374,7 +376,7 @@ static void manageBuildingEvent(color_ostream& out) {
buildings.erase(id);
}
out.print("Sent building event.\n %d", __LINE__);
//out.print("Sent building event.\n %d", __LINE__);
}
static void manageConstructionEvent(color_ostream& out) {