From f94fc5846d9273f2bed813cc94f9ca6eeac4b4a8 Mon Sep 17 00:00:00 2001 From: expwnent Date: Fri, 20 Jun 2014 19:48:56 -0400 Subject: [PATCH] outsideOnly: allow checking periodically in case a building was made outside and then became inside. --- plugins/outsideOnly.cpp | 71 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/plugins/outsideOnly.cpp b/plugins/outsideOnly.cpp index f7a301cb7..95aec686f 100644 --- a/plugins/outsideOnly.cpp +++ b/plugins/outsideOnly.cpp @@ -30,8 +30,10 @@ static map registeredBuildings; const int32_t OUTSIDE_ONLY = 1; const int32_t EITHER = 0; const int32_t INSIDE_ONLY = -1; +int32_t checkEvery = -1; void buildingCreated(color_ostream& out, void* data); +void checkBuildings(color_ostream& out, void* data); command_result outsideOnly(color_ostream& out, vector& parameters); DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) @@ -44,6 +46,8 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector flags.bits.almost_deleted = 1; +} + +void checkBuildings(color_ostream& out, void* data) { + if ( !enabled ) + return; + + std::vector& buildings = df::global::world->buildings.all; + for ( size_t a = 0; a < buildings.size(); a++ ) { + df::building* building = buildings[a]; + if ( building == NULL ) + continue; + if ( building->getCustomType() < 0 ) + continue; + df::coord pos(building->centerx,building->centery,building->z); + df::tile_designation* des = Maps::getTileDesignation(pos); + bool outside = des->bits.outside; + df::building_def* def = df::global::world->raws.buildings.all[building->getCustomType()]; + int32_t type = registeredBuildings[def->code]; + + if ( type == EITHER ) { + registeredBuildings.erase(def->code); + } else if ( type == OUTSIDE_ONLY ) { + if ( outside ) + continue; + destroy(building); + } else if ( type == INSIDE_ONLY ) { + if ( !outside ) + continue; + destroy(building); + } else { + if ( DFHack::Once::doOnce("outsideOnly invalid setting") ) { + out.print("Error: outsideOnly: building has invalid setting: %s %d\n", def->code.c_str(), type); + } + } + } + + if ( checkEvery < 0 ) + return; + EventManager::EventHandler timeHandler(checkBuildings,-1); + EventManager::registerTick(timeHandler, checkEvery, plugin_self); } command_result outsideOnly(color_ostream& out, vector& parameters) { @@ -95,15 +146,25 @@ command_result outsideOnly(color_ostream& out, vector& parameters) { status = INSIDE_ONLY; } else if ( parameters[a] == "either" ) { status = EITHER; - } else { + } else if ( parameters[a] == "checkEvery" ) { + if (a+1 >= parameters.size()) { + out.printerr("You must specify how often to check.\n"); + return CR_WRONG_USAGE; + } + checkEvery = atoi(parameters[a].c_str()); + } + else { if ( status == 2 ) { - out.print("Error: you need to tell outsideOnly whether the building is inside only, outside-only or either.\n"); + out.printerr("Error: you need to tell outsideOnly whether the building is inside only, outside-only or either.\n"); return CR_WRONG_USAGE; } registeredBuildings[parameters[a]] = status; } } out.print("outsideOnly is %s\n", enabled ? "enabled" : "disabled"); + if ( enabled ) { + + } return CR_OK; }