diff --git a/plugins/cleaners.cpp b/plugins/cleaners.cpp index 8992b2078..c25a1ed33 100644 --- a/plugins/cleaners.cpp +++ b/plugins/cleaners.cpp @@ -2,99 +2,69 @@ #include #include #include -#include -#include -#include -#include -using namespace DFHack; +#include "DataDefs.h" +#include "df/world.h" +#include "df/map_block.h" +#include "df/block_square_event.h" +#include "df/block_square_event_material_spatterst.h" +#include "df/item_actual.h" +#include "df/unit.h" +#include "df/matter_state.h" +#include "df/cursor.h" + +#include "modules/Materials.h" -#include -#include -#include using std::vector; using std::string; +using namespace DFHack; -DFhackCExport command_result clean (Core * c, vector & parameters); -DFhackCExport command_result spotclean (Core * c, vector & parameters); - -DFhackCExport const char * plugin_name ( void ) -{ - return "cleaners"; -} - -DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) -{ - commands.clear(); - commands.push_back(PluginCommand("clean","Removes contaminants from map tiles, items and creatures.",clean)); - commands.push_back(PluginCommand("spotclean","Cleans map tile under cursor.",spotclean,cursor_hotkey)); - return CR_OK; -} - -DFhackCExport command_result plugin_shutdown ( Core * c ) -{ - return CR_OK; -} +using df::global::world; +using df::global::cursor; command_result cleanmap (Core * c, bool snow, bool mud) { - const uint32_t water_idx = 6; - const uint32_t mud_idx = 12; - vector splatter; - DFHack::Maps *Mapz = c->getMaps(); - - // init the map - if(!Mapz->Start()) - { - c->con << "Can't init map." << std::endl; - c->Resume(); - return CR_FAILURE; - } - - uint32_t x_max,y_max,z_max; - Mapz->getSize(x_max,y_max,z_max); - int num_blocks = 0; - int blocks_total = 0; - // walk the map - for(uint32_t x = 0; x< x_max;x++) + // Invoked from clean(), already suspended + int num_blocks = 0, blocks_total = world->map.map_blocks.size(); + for (int i = 0; i < blocks_total; i++) { - for(uint32_t y = 0; y< y_max;y++) + df::map_block *block = world->map.map_blocks[i]; + bool cleaned = false; + for(int x = 0; x < 16; x++) { - for(uint32_t z = 0; z< z_max;z++) + for(int y = 0; y < 16; y++) { - df_block * block = Mapz->getBlock(x,y,z); - if(block) - { - blocks_total ++; - bool cleaned = false; - Mapz->SortBlockEvents(x,y,z,0,0,&splatter); - for(int i = 0; i < 16; i++) - for(int j = 0; j < 16; j++) - { - block->occupancy[i][j].bits.arrow_color = 0; - block->occupancy[i][j].bits.broken_arrows_variant = 0; - } - for(uint32_t i = 0; i < splatter.size(); i++) - { - DFHack::t_spattervein * vein = splatter[i]; - // filter snow - if(!snow - && vein->mat1 == DFHack::Materials::WATER - && vein->matter_state == DFHack::state_powder) - continue; - // filter mud - if(!mud - && vein->mat1 == DFHack::Materials::MUD - && vein->matter_state == DFHack::state_solid) - continue; - Mapz->RemoveBlockEvent(x,y,z,(t_virtual *) vein); - cleaned = true; - } - num_blocks += cleaned; - } + block->occupancy[x][y].bits.arrow_color = 0; + block->occupancy[x][y].bits.arrow_variant = 0; } } + for (int j = 0; j < block->block_events.size(); j++) + { + df::block_square_event *evt = block->block_events[j]; + if (evt->getType() != df::block_square_event_type::material_spatter) + continue; + // type verified - recast to subclass + df::block_square_event_material_spatterst *spatter = (df::block_square_event_material_spatterst *)evt; + + // filter snow + if(!snow + && spatter->mat_type == DFHack::Materials::WATER + && spatter->mat_state == df::matter_state::Powder) + continue; + // filter mud + if(!mud + && spatter->mat_type == DFHack::Materials::MUD + && spatter->mat_state == df::matter_state::Solid) + continue; + + delete evt; + block->block_events.erase(block->block_events.begin() + j); + j--; + cleaned = true; + } + num_blocks += cleaned; } + if(num_blocks) c->con.print("Cleaned %d of %d map blocks.\n", num_blocks, blocks_total); return CR_OK; @@ -102,94 +72,82 @@ command_result cleanmap (Core * c, bool snow, bool mud) command_result cleanitems (Core * c) { - DFHack::Items * Items = c->getItems(); - - vector p_items; - if(!Items->readItemVector(p_items)) - { - c->con.printerr("Can't access the item vector.\n"); - c->Resume(); - return CR_FAILURE; - } - std::size_t numItems = p_items.size(); - + // Invoked from clean(), already suspended int cleaned_items = 0, cleaned_total = 0; - for (std::size_t i = 0; i < numItems; i++) + for (int i = 0; i < world->items.all.size(); i++) { - df_item * itm = p_items[i]; - if(!itm->contaminants) - continue; - if (itm->contaminants->size()) + // currently, all item classes extend item_actual, so this should be safe + df::item_actual *item = (df::item_actual *)world->items.all[i]; + if (item->contaminants && item->contaminants->size()) { - for(int j = 0; j < itm->contaminants->size(); j++) - delete itm->contaminants->at(j); + for (int j = 0; j < item->contaminants->size(); j++) + delete item->contaminants->at(j); cleaned_items++; - cleaned_total += itm->contaminants->size(); - itm->contaminants->clear(); + cleaned_total += item->contaminants->size(); + item->contaminants->clear(); } } - if(cleaned_total) + if (cleaned_total) c->con.print("Removed %d contaminants from %d items.\n", cleaned_total, cleaned_items); return CR_OK; } command_result cleanunits (Core * c) { - DFHack::Units * Creatures = c->getUnits(); - - uint32_t num_creatures; - if (!Creatures->Start(num_creatures)) - { - c->con.printerr("Can't read unit list!\n"); - c->Resume(); - return CR_FAILURE; - } + // Invoked from clean(), already suspended + int num_units = world->units.all.size(); int cleaned_units = 0, cleaned_total = 0; - for (std::size_t i = 0; i < num_creatures; i++) + for (int i = 0; i < num_units; i++) { - df_unit *unit = Creatures->creatures->at(i); - int num = unit->contaminants.size(); - if (num) + df::unit *unit = world->units.all[i]; + if (unit->body.spatters.size()) { - for(int j = 0; j < unit->contaminants.size(); j++) - delete unit->contaminants.at(j); + for (int j = 0; j < unit->body.spatters.size(); j++) + delete unit->body.spatters[j]; cleaned_units++; - cleaned_total += num; - unit->contaminants.clear(); + cleaned_total += unit->body.spatters.size(); + unit->body.spatters.clear(); } } - if(cleaned_total) + if (cleaned_total) c->con.print("Removed %d contaminants from %d creatures.\n", cleaned_total, cleaned_units); return CR_OK; } +// This is slightly different from what's in the Maps module - it takes tile coordinates rather than block coordinates +df::map_block *getBlock (int32_t x, int32_t y, int32_t z) +{ + if ((x < 0) || (y < 0) || (z < 0)) + return NULL; + if ((x >= world->map.x_count) || (y >= world->map.y_count) || (z >= world->map.z_count)) + return NULL; + // block_index isn't declared correctly - needs one more level of indirection + return ((df::map_block ****)world->map.block_index)[x >> 4][y >> 4][z]; +} + DFhackCExport command_result spotclean (Core * c, vector & parameters) { // HOTKEY COMMAND: CORE ALREADY SUSPENDED - vector splatter; - DFHack::Maps *Mapz = c->getMaps(); - DFHack::Gui *Gui = c->getGui(); - // init the map - if(!Mapz->Start()) + if (cursor->x == -30000) { - c->con.printerr("Can't init map.\n"); + c->con.printerr("The cursor is not active.\n"); return CR_FAILURE; } - int32_t cursorX, cursorY, cursorZ; - Gui->getCursorCoords(cursorX,cursorY,cursorZ); - if(cursorX == -30000) + df::map_block *block = getBlock(cursor->x, cursor->y, cursor->z); + if (block == NULL) { - c->con.printerr("The cursor is not active.\n"); + c->con.printerr("Invalid map block selected!\n"); return CR_FAILURE; } - int32_t blockX = cursorX / 16, blockY = cursorY / 16; - int32_t tileX = cursorX % 16, tileY = cursorY % 16; - df_block *b = Mapz->getBlock(blockX,blockY,cursorZ); - vector spatters; - Mapz->SortBlockEvents(blockX, blockY, cursorZ, 0,0, &spatters); - for(int i = 0; i < spatters.size(); i++) + + for (int i = 0; i < block->block_events.size(); i++) { - spatters[i]->intensity[tileX][tileY] = 0; + df::block_square_event *evt = block->block_events[i]; + if (evt->getType() != df::block_square_event_type::material_spatter) + continue; + // type verified - recast to subclass + df::block_square_event_material_spatterst *spatter = (df::block_square_event_material_spatterst *)evt; + spatter->amount[cursor->x % 16][cursor->y % 16] = 0; } return CR_OK; } @@ -230,17 +188,17 @@ DFhackCExport command_result clean (Core * c, vector & parameters) if(help) { c->con.print("Removes contaminants from map tiles, items and creatures.\n" - "Options:\n" - "map - clean the map tiles\n" - "items - clean all items\n" - "units - clean all creatures\n" - "all - clean everything.\n" - "More options for 'map':\n" - "snow - also remove snow\n" - "mud - also remove mud\n" - "Example: clean all mud snow\n" - "This removes all spatter, including mud and snow from map tiles." - ); + "Options:\n" + "map - clean the map tiles\n" + "items - clean all items\n" + "units - clean all creatures\n" + "all - clean everything.\n" + "More options for 'map':\n" + "snow - also remove snow\n" + "mud - also remove mud\n" + "Example: clean all mud snow\n" + "This removes all spatter, including mud and snow from map tiles." + ); return CR_OK; } c->Suspend(); @@ -253,3 +211,21 @@ DFhackCExport command_result clean (Core * c, vector & parameters) c->Resume(); return CR_OK; } + +DFhackCExport const char * plugin_name ( void ) +{ + return "cleaners"; +} + +DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) +{ + commands.clear(); + commands.push_back(PluginCommand("clean","Removes contaminants from map tiles, items and creatures.",clean)); + commands.push_back(PluginCommand("spotclean","Cleans map tile under cursor.",spotclean,cursor_hotkey)); + return CR_OK; +} + +DFhackCExport command_result plugin_shutdown ( Core * c ) +{ + return CR_OK; +} \ No newline at end of file diff --git a/plugins/drybuckets.cpp b/plugins/drybuckets.cpp index 0ea079c0c..09456c01c 100644 --- a/plugins/drybuckets.cpp +++ b/plugins/drybuckets.cpp @@ -1,24 +1,43 @@ // Dry Buckets : Remove all "water" objects from buckets scattered around the fortress -#include -#include -#include -#include -#include -#include -using namespace std; #include "Core.h" #include #include #include -#include -#include -#include -#include +#include +#include "df/world.h" +#include "df/item.h" + +#include "modules/Materials.h" + +using std::string; +using std::vector; using namespace DFHack; -DFhackCExport command_result df_drybuckets (Core * c, vector & parameters); +using df::global::world; + +DFhackCExport command_result df_drybuckets (Core * c, vector & parameters) +{ + if (!parameters.empty()) + return CR_WRONG_USAGE; + + CoreSuspender suspend(c); + + int dried_total = 0; + for (int i = 0; i < world->items.all.size(); i++) + { + df::item *item = world->items.all[i]; + if ((item->getType() == df::item_type::LIQUID_MISC) && (item->getMaterial() == DFHack::Materials::WATER)) + { + item->flags.bits.garbage_colect = 1; + dried_total++; + } + } + if (dried_total) + c->con.print("Done. %d buckets of water marked for emptying.\n", dried_total); + return CR_OK; +} DFhackCExport const char * plugin_name ( void ) { @@ -35,42 +54,4 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector DFhackCExport command_result plugin_shutdown ( Core * c ) { return CR_OK; -} - -DFhackCExport command_result df_drybuckets (Core * c, vector & parameters) -{ - if(parameters.size() > 0) - { - string & p = parameters[0]; - if(p == "?" || p == "help") - { - c->con.print("This utility removes all objects of type LIQUID_MISC:NONE and material WATER:NONE - that is, water stored in buckets.\n"); - return CR_OK; - } - } - c->Suspend(); - DFHack::Items * Items = c->getItems(); - - vector p_items; - if(!Items->readItemVector(p_items)) - { - c->con.printerr("Can't access the item vector.\n"); - c->Resume(); - return CR_FAILURE; - } - std::size_t numItems = p_items.size(); - - int dried_total = 0; - for (std::size_t i = 0; i < numItems; i++) - { - df_item *item = p_items[i]; - if ((item->getType() == Items::LIQUID_MISC) && (item->getMaterial() == Materials::WATER)) - { - item->flags.garbage_colect = 1; - dried_total++; - } - } - c->Resume(); - c->con.print("Done. %d buckets of water emptied.\n", dried_total); - return CR_OK; } \ No newline at end of file diff --git a/plugins/fixwagons.cpp b/plugins/fixwagons.cpp index 29253880e..3d7c5ff9b 100644 --- a/plugins/fixwagons.cpp +++ b/plugins/fixwagons.cpp @@ -6,21 +6,17 @@ #include #include -#include -#include -#include -#include -#include +#include "df/world.h" +#include "df/historical_entity.h" +#include "df/entity_raw.h" +#include "df/creature_raw.h" +#include "df/caste_raw.h" using std::string; using std::vector; using namespace DFHack; using df::global::world; -using df::historical_entity; -using df::entity_raw_flags; -using df::creature_raw; -using df::creature_raw_flags; command_result df_fixwagons (Core *c, vector ¶meters) { @@ -29,11 +25,11 @@ command_result df_fixwagons (Core *c, vector ¶meters) CoreSuspender suspend(c); int32_t wagon_creature = -1, wagon_puller_creature = -1; - creature_raw *wagon, *wagon_puller; + df::creature_raw *wagon, *wagon_puller; for (int i = 0; i < world->raws.creatures.all.size(); i++) { - creature_raw *cr = world->raws.creatures.all[i]; - if (cr->flags.is_set(creature_raw_flags::EQUIPMENT_WAGON) && (wagon_creature == -1)) + df::creature_raw *cr = world->raws.creatures.all[i]; + if (cr->flags.is_set(df::creature_raw_flags::EQUIPMENT_WAGON) && (wagon_creature == -1)) { wagon = cr; wagon_creature = i; @@ -58,8 +54,8 @@ command_result df_fixwagons (Core *c, vector ¶meters) for (int i = 0; i < world->entities.all.size(); i++) { bool updated = false; - historical_entity *ent = world->entities.all[i]; - if (!ent->entity_raw->flags.is_set(entity_raw_flags::COMMON_DOMESTIC_PULL)) + df::historical_entity *ent = world->entities.all[i]; + if (!ent->entity_raw->flags.is_set(df::entity_raw_flags::COMMON_DOMESTIC_PULL)) continue; if (ent->resources.animals.wagon_races.size() == 0) { @@ -107,4 +103,4 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector DFhackCExport command_result plugin_shutdown ( Core * c ) { return CR_OK; -} +} \ No newline at end of file diff --git a/plugins/getplants.cpp b/plugins/getplants.cpp index 14fb8b90c..e35c3d517 100644 --- a/plugins/getplants.cpp +++ b/plugins/getplants.cpp @@ -1,56 +1,34 @@ // Designate all matching plants for gathering/cutting -#include -#include -#include -#include -#include -#include -#include - #include "Core.h" #include #include #include -#include -#include -#include -#include -using namespace std; -using namespace DFHack; - -DFhackCExport command_result df_getplants (Core * c, vector & parameters); +#include +#include +#include "df/world.h" +#include "df/map_block.h" +#include "df/tile_dig_designation.h" +#include "df/plant_raw.h" -DFhackCExport const char * plugin_name ( void ) -{ - return "getplants"; -} +#include "modules/Vegetation.h" +#include -DFhackCExport command_result plugin_init ( Core * c, vector &commands) -{ - commands.clear(); - commands.push_back(PluginCommand("getplants", "Cut down all of the specified trees or gather all of the specified shrubs", df_getplants)); - return CR_OK; -} +using std::string; +using std::vector; +using std::set; +using namespace DFHack; -DFhackCExport command_result plugin_shutdown ( Core * c ) -{ - return CR_OK; -} +using df::global::world; DFhackCExport command_result df_getplants (Core * c, vector & parameters) { - uint32_t x_max,y_max,z_max; - designations40d designations; - tiletypes40d tiles; - t_blockflags blockflags; string plantMatStr = ""; set plantIDs; set plantNames; bool deselect = false, exclude = false, treesonly = false, shrubsonly = false; - bool dirty = false; int count = 0; for (size_t i = 0; i < parameters.size(); i++) { @@ -77,16 +55,16 @@ DFhackCExport command_result df_getplants (Core * c, vector & parameter exclude = true; else plantNames.insert(parameters[i]); } - c->Suspend(); - Materials *mats = c->getMaterials(); - for (vector::const_iterator it = mats->df_organic->begin(); it != mats->df_organic->end(); it++) + CoreSuspender suspend(c); + + for (int i = 0; i < world->raws.plants.all.size(); i++) { - df_plant_type &plant = **it; - if (plantNames.find(plant.ID) != plantNames.end()) + df::plant_raw *plant = world->raws.plants.all[i]; + if (plantNames.find(plant->id) != plantNames.end()) { - plantNames.erase(plant.ID); - plantIDs.insert(it - mats->df_organic->begin()); + plantNames.erase(plant->id); + plantIDs.insert(i); } } if (plantNames.size() > 0) @@ -95,103 +73,83 @@ DFhackCExport command_result df_getplants (Core * c, vector & parameter for (set::const_iterator it = plantNames.begin(); it != plantNames.end(); it++) c->con.printerr(" %s", it->c_str()); c->con.printerr("\n"); - c->Resume(); return CR_FAILURE; } if (plantIDs.size() == 0) { c->con.print("Valid plant IDs:\n"); - for (vector::const_iterator it = mats->df_organic->begin(); it != mats->df_organic->end(); it++) + for (int i = 0; i < world->raws.plants.all.size(); i++) { - df_plant_type &plant = **it; - if (plant.flags.is_set(PLANT_GRASS)) + df::plant_raw *plant = world->raws.plants.all[i]; + if (plant->flags.is_set(df::plant_raw_flags::GRASS)) continue; - c->con.print("* (%s) %s - %s\n", plant.flags.is_set(PLANT_TREE) ? "tree" : "shrub", plant.ID.c_str(), plant.name.c_str()); + c->con.print("* (%s) %s - %s\n", plant->flags.is_set(df::plant_raw_flags::TREE) ? "tree" : "shrub", plant->id.c_str(), plant->name.c_str()); } - c->Resume(); return CR_OK; } - Maps *maps = c->getMaps(); - - // init the map - if (!maps->Start()) - { - c->con.printerr("Can't init map.\n"); - c->Resume(); - return CR_FAILURE; - } - - maps->getSize(x_max,y_max,z_max); - // walk the map - for (uint32_t x = 0; x < x_max; x++) + int count = 0; + for (int i = 0; i < world->map.map_blocks.size(); i++) { - for (uint32_t y = 0; y < y_max; y++) + df::map_block *cur = world->map.map_blocks[i]; + bool dirty = false; + for (int j = 0; j < cur->plants.size(); j++) { - for (uint32_t z = 0; z < z_max; z++) + const df_plant *plant = (df_plant *)cur->plants[i]; + int x = plant->x % 16; + int y = plant->y % 16; + if (plantIDs.find(plant->material) != plantIDs.end()) { - if (maps->getBlock(x,y,z)) - { - dirty = false; - maps->ReadDesignations(x,y,z, &designations); - maps->ReadTileTypes(x,y,z, &tiles); - maps->ReadBlockFlags(x,y,z, blockflags); - - vector *plants; - if (maps->ReadVegetation(x, y, z, plants)) - { - for (vector::const_iterator it = plants->begin(); it != plants->end(); it++) - { - const df_plant &plant = **it; - uint32_t tx = plant.x % 16; - uint32_t ty = plant.y % 16; - if (plantIDs.find(plant.material) != plantIDs.end()) - { - if (exclude) - continue; - } - else - { - if (!exclude) - continue; - } - - TileShape shape = tileShape(tiles[tx][ty]); - if (plant.is_shrub && (treesonly || shape != SHRUB_OK)) - continue; - if (!plant.is_shrub && (shrubsonly || (shape != TREE_OK && shape != TREE_DEAD))) - continue; - if (designations[tx][ty].bits.hidden) - continue; - if (deselect && designations[tx][ty].bits.dig != designation_no) - { - designations[tx][ty].bits.dig = designation_no; - dirty = true; - ++count; - } - if (!deselect && designations[tx][ty].bits.dig != designation_default) - { - designations[tx][ty].bits.dig = designation_default; - dirty = true; - ++count; - } - } - } - // If anything was changed, write it all. - if (dirty) - { - blockflags.bits.designated = 1; - maps->WriteDesignations(x,y,z, &designations); - maps->WriteBlockFlags(x,y,z, blockflags); - dirty = false; - } - } + if (exclude) + continue; + } + else + { + if (!exclude) + continue; + } + TileShape shape = tileShape(cur->tiletype[x][y]); + if (plant->is_shrub && (treesonly || shape != SHRUB_OK)) + continue; + if (!plant->is_shrub && (shrubsonly || (shape != TREE_OK && shape != TREE_DEAD))) + continue; + if (cur->designation[x][y].bits.hidden) + continue; + if (deselect && cur->designation[x][y].bits.dig != df::tile_dig_designation::No) + { + cur->designation[x][y].bits.dig = df::tile_dig_designation::No; + dirty = true; + ++count; + } + if (!deselect && cur->designation[x][y].bits.dig != df::tile_dig_designation::Default) + { + cur->designation[x][y].bits.dig = df::tile_dig_designation::Default; + dirty = true; + ++count; } } + if (dirty) + cur->flags.set(df::block_flags::Designated); } - c->Resume(); if (count) c->con.print("Updated %d plant designations.\n", count); return CR_OK; } + +DFhackCExport const char * plugin_name ( void ) +{ + return "getplants"; +} + +DFhackCExport command_result plugin_init ( Core * c, vector &commands) +{ + commands.clear(); + commands.push_back(PluginCommand("getplants", "Cut down all of the specified trees or gather all of the specified shrubs", df_getplants)); + return CR_OK; +} + +DFhackCExport command_result plugin_shutdown ( Core * c ) +{ + return CR_OK; +} \ No newline at end of file