Ported deramp, cleanowned and cleartask tools.

develop
Petr Mrázek 2011-08-05 00:41:31 +02:00
parent 02d5f2273b
commit be358ec1de
6 changed files with 197 additions and 189 deletions

@ -369,7 +369,7 @@ namespace DFHack
bool ReadDescriptorColors(void);
bool ReadOthers (void);
void ReadAllMaterials(void);
bool ReadAllMaterials(void);
std::string getType(const t_material & mat);
std::string getDescription(const t_material & mat);

@ -341,16 +341,18 @@ bool Materials::ReadCreatureTypesEx (void)
return true;
}
void Materials::ReadAllMaterials(void)
bool Materials::ReadAllMaterials(void)
{
this->ReadInorganicMaterials();
this->ReadOrganicMaterials();
this->ReadWoodMaterials();
this->ReadPlantMaterials();
this->ReadCreatureTypes();
this->ReadCreatureTypesEx();
this->ReadDescriptorColors();
this->ReadOthers();
bool ok = true;
ok &= this->ReadInorganicMaterials();
ok &= this->ReadOrganicMaterials();
ok &= this->ReadWoodMaterials();
ok &= this->ReadPlantMaterials();
ok &= this->ReadCreatureTypes();
ok &= this->ReadCreatureTypesEx();
ok &= this->ReadDescriptorColors();
ok &= this->ReadOthers();
return ok;
}
/// miserable pile of magic. The material system is insane.

@ -144,3 +144,6 @@ DFHACK_PLUGIN(mode mode.cpp)
DFHACK_PLUGIN(liquids liquids.cpp)
DFHACK_PLUGIN(tubefill tubefill.cpp)
DFHACK_PLUGIN(autodump autodump.cpp)
DFHACK_PLUGIN(cleanowned cleanowned.cpp)
DFHACK_PLUGIN(cleartask cleartask.cpp)
DFHACK_PLUGIN(deramp deramp.cpp)

@ -2,137 +2,151 @@
* Confiscates and dumps garbage owned by dwarfs.
*/
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <climits>
#include <vector>
#include <set>
using namespace std;
#include <DFHack.h>
#include <dfhack/DFVector.h>
#include <dfhack/DFTypes.h>
#include <dfhack/extra/termutil.h>
#include <dfhack/Core.h>
#include <dfhack/Console.h>
#include <dfhack/Export.h>
#include <dfhack/PluginManager.h>
#include <vector>
#include <string>
#include <dfhack/modules/Maps.h>
#include <dfhack/modules/Items.h>
#include <dfhack/modules/Creatures.h>
#include <dfhack/modules/Materials.h>
#include <dfhack/modules/Translation.h>
using namespace DFHack;
DFhackCExport command_result df_cleanowned (Core * c, vector <string> & parameters);
DFhackCExport const char * plugin_name ( void )
{
return "cleanowned";
}
int main (int argc, char *argv[])
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("cleanowned",
"Confiscates and dumps garbage owned by dwarfs.",
df_cleanowned));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c )
{
return CR_OK;
}
typedef std::map <DFCoord, uint32_t> coordmap;
DFhackCExport command_result df_cleanowned (Core * c, vector <string> & parameters)
{
bool temporary_terminal = TemporaryTerminal();
bool dump_scattered = false;
bool confiscate_all = false;
bool dry_run = false;
int wear_dump_level = 65536;
for(int i = 1; i < argc; i++)
for(int i = 0; i < parameters.size(); i++)
{
char *arg = argv[i];
if (arg[0] != '-')
continue;
for (; *arg; arg++) {
switch (arg[0]) {
case 'd':
string & param = parameters[i];
if(param == "dryrun")
dry_run = true;
break;
case 'l':
else if(param == "scattered")
dump_scattered = true;
break;
case 'a':
else if(param == "all")
confiscate_all = true;
break;
case 'x':
else if(param == "x")
wear_dump_level = 1;
break;
case 'X':
wear_dump_level = 2;
break;
}
}
}
DFHack::Process * p;
unsigned int i;
DFHack::ContextManager DFMgr("Memory.xml");
DFHack::Context * DF;
try
else if(param == "X")
wear_dump_level = 1;
else if(param == "?" || param == "help")
{
DF = DFMgr.getSingleContext();
DF->Attach();
c->con.print("Oh no! Someone has to write the help text!\n");
return CR_OK;
}
catch (exception& e)
else
{
cerr << e.what() << endl;
if(temporary_terminal)
cin.ignore();
return 1;
c->con.printerr("Parameter '%s' is not valid. See 'cleanowned help'.\n",param.c_str());
return CR_FAILURE;
}
}
DFHack::Materials *Materials = c->getMaterials();
DFHack::Items *Items = c->getItems();
DFHack::Creatures *Creatures = c->getCreatures();
DFHack::Translation *Tran = c->getTranslation();
DFHack::VersionInfo * mem = DF->getMemoryInfo();
DFHack::Materials *Materials = DF->getMaterials();
DFHack::Items *Items = DF->getItems();
DFHack::Creatures *Creatures = DF->getCreatures();
DFHack::Translation *Tran = DF->getTranslation();
Materials->ReadAllMaterials();
uint32_t num_creatures;
Creatures->Start(num_creatures);
Tran->Start();
p = DF->getProcess();
DFHack::OffsetGroup* itemGroup = mem->getGroup("Items");
unsigned vector_addr = itemGroup->getAddress("items_vector");
DFHack::DfVector <uint32_t> p_items (p, vector_addr);
uint32_t size = p_items.size();
printf("Found total %d items.\n", size);
bool ok = true;
ok &= Materials->ReadAllMaterials();
ok &= Creatures->Start(num_creatures);
ok &= Tran->Start();
vector<t_item *> p_items;
ok &= Items->readItemVector(p_items);
if(!ok)
{
c->con.printerr("Can't continue due to offset errors.\n");
c->Resume();
return CR_FAILURE;
}
c->con.print("Found total %d items.\n", p_items.size());
for (i=0;i<size;i++)
for (std::size_t i=0; i < p_items.size(); i++)
{
uint32_t curItem = p_items[i];
t_item * curItem = p_items[i];
DFHack::dfh_item itm;
Items->readItem(curItem, itm);
bool confiscate = false;
bool dump = false;
if (!itm.base.flags.owned) {
if (!itm.base->flags.owned)
{
int32_t owner = Items->getItemOwnerID(itm);
if (owner >= 0) {
printf("Fixing a misflagged item: ");
if (owner >= 0)
{
c->con.print("Fixing a misflagged item: ");
confiscate = true;
}
else
{
continue;
}
}
std::string name = Items->getItemClass(itm.matdesc.itemType);
if (itm.base.flags.rotten)
if (itm.base->flags.rotten)
{
printf("Confiscating a rotten item: \t");
c->con.print("Confiscating a rotten item: \t");
confiscate = true;
}
else if (itm.base.flags.on_ground &&
else if (itm.base->flags.on_ground &&
(name == "food" || name == "meat" || name == "plant"))
{
printf("Confiscating a dropped foodstuff: \t");
c->con.print("Confiscating a dropped foodstuff: \t");
confiscate = true;
}
else if (itm.wear_level >= wear_dump_level)
{
printf("Confiscating and dumping a worn item: \t");
c->con.print("Confiscating and dumping a worn item: \t");
confiscate = true;
dump = true;
}
else if (dump_scattered && itm.base.flags.on_ground)
else if (dump_scattered && itm.base->flags.on_ground)
{
printf("Confiscating and dumping litter: \t");
c->con.print("Confiscating and dumping litter: \t");
confiscate = true;
dump = true;
}
else if (confiscate_all)
{
printf("Confiscating: \t");
c->con.print("Confiscating: \t");
confiscate = true;
}
@ -140,14 +154,14 @@ int main (int argc, char *argv[])
{
if (!dry_run) {
if (!Items->removeItemOwner(itm, Creatures))
printf("(unsuccessfully) ");
c->con.print("(unsuccessfully) ");
if (dump)
itm.base.flags.dump = 1;
itm.base->flags.dump = 1;
Items->writeItem(itm);
}
printf(
c->con.print(
"%s (wear %d)",
Items->getItemDescription(itm, Materials).c_str(),
itm.wear_level
@ -167,10 +181,10 @@ int main (int argc, char *argv[])
info += std::string(" '") + temp.name.nickname + "'";
info += " ";
info += Tran->TranslateName(temp.name,false);
printf(", owner %s", info.c_str());
c->con.print(", owner %s", info.c_str());
}
printf("\n");
c->con.print("\n");
/*
printf(
"%5d: %08x %08x (%d,%d,%d) #%08x [%d] %s - %s %s\n",
@ -185,10 +199,5 @@ int main (int argc, char *argv[])
*/
}
}
if(temporary_terminal)
{
cout << "Done. Press any key to continue" << endl;
cin.ignore();
}
return 0;
return CR_OK;
}

@ -8,70 +8,62 @@
#include <vector>
using namespace std;
#include <DFHack.h>
#include <dfhack/DFVector.h>
#include <dfhack/DFTypes.h>
#include <dfhack/Core.h>
#include <dfhack/Console.h>
#include <dfhack/Export.h>
#include <dfhack/PluginManager.h>
#include <dfhack/modules/Items.h>
#include <dfhack/Types.h>
#include <dfhack/extra/termutil.h>
using namespace DFHack;
int main ()
DFhackCExport command_result df_cleartask (Core * c, vector <string> & parameters);
DFhackCExport const char * plugin_name ( void )
{
bool temporary_terminal = TemporaryTerminal();
DFHack::Process * p;
unsigned int i;
DFHack::ContextManager DFMgr("Memory.xml");
DFHack::Context * DF;
DFHack::Items * Items;
try
{
DF = DFMgr.getSingleContext();
DF->Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
if(temporary_terminal)
cin.ignore();
return 1;
}
return "cleartask";
}
p = DF->getProcess();
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("cleartask",
"Clears the \"tasked\" flag on all items. This is dangerous. Only use after reclaims.",
df_cleartask));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c )
{
return CR_OK;
}
DFhackCExport command_result df_cleartask (Core * c, vector <string> & parameters)
{
c->Suspend();
DFHack::Items * Items = c->getItems();
uint32_t item_vec_offset = 0;
try
{
Items = DF->getItems();
DFHack::OffsetGroup* itemGroup = p->getDescriptor()->getGroup("Items");
item_vec_offset = itemGroup->getAddress("items_vector");
}
catch(DFHack::Error::All & e)
vector <t_item *> p_items;
if(!Items->readItemVector(p_items))
{
cerr << "Fatal error, exiting :(" << endl << e.what() << endl;
if(temporary_terminal)
cin.ignore();
return 1;
c->con.printerr("Can't read items...\n");
c->Resume();
return CR_FAILURE;
}
DFHack::DfVector <uint32_t> p_items (p, item_vec_offset);
uint32_t size = p_items.size();
int numtasked = 0;
for (i=0;i<size;i++)
for (std::size_t i = 0; i < p_items.size(); i++)
{
t_item * ptr;
DFHack::dfh_item temp;
Items->readItem(p_items[i],temp);
DFHack::t_itemflags & flags = temp.base.flags;
if (flags.in_job)
if (ptr->flags.in_job)
{
flags.in_job = 0;
Items->writeItem(temp);
ptr->flags.in_job = 0;
numtasked++;
}
}
cout << "Found and untasked " << numtasked << " items." << endl;
if(temporary_terminal)
{
cout << "Done. Press any key to continue" << endl;
cin.ignore();
}
return 0;
c->con.print("Found and untasked %d items.\n", numtasked);
c->Resume();
return CR_OK;
}

@ -7,14 +7,37 @@
#include <assert.h>
#include <string.h>
using namespace std;
#include <dfhack/Core.h>
#include <dfhack/Console.h>
#include <dfhack/Export.h>
#include <dfhack/PluginManager.h>
#include <dfhack/modules/Maps.h>
#include <dfhack/TileTypes.h>
using namespace DFHack;
#include <DFHack.h>
#include <dfhack/DFTileTypes.h>
#include <dfhack/extra/termutil.h>
DFhackCExport command_result df_deramp (Core * c, vector <string> & parameters);
int main (void)
DFhackCExport const char * plugin_name ( void )
{
return "deramp";
}
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("deramp",
"De-ramp. All ramps marked for removal are replaced with floors.",
df_deramp));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c )
{
return CR_OK;
}
DFhackCExport command_result df_deramp (Core * c, vector <string> & parameters)
{
bool temporary_terminal = TemporaryTerminal();
uint32_t x_max,y_max,z_max;
uint32_t num_blocks = 0;
uint32_t bytes_read = 0;
@ -29,33 +52,15 @@ int main (void)
int count=0;
int countbad=0;
DFHack::ContextManager DFMgr("Memory.xml");
DFHack::Context *DF = DFMgr.getSingleContext();
//sanity check
assert( sizeof(designations) == (16*16*sizeof(DFHack::t_designation)) );
//Init
try
{
DF->Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
if(temporary_terminal)
cin.ignore();
return 1;
}
DFHack::Maps *Mapz = DF->getMaps();
c->Suspend();
DFHack::Maps *Mapz = c->getMaps();
// init the map
if (!Mapz->Start())
{
cerr << "Can't init map." << endl;
if(temporary_terminal)
cin.ignore();
return 1;
c->con.printerr("Can't init map.\n");
c->Resume();
return CR_FAILURE;
}
Mapz->getSize(x_max,y_max,z_max);
@ -69,12 +74,12 @@ int main (void)
{
for (uint32_t z = 0; z< z_max;z++)
{
if (Mapz->isValidBlock(x,y,z))
if (Mapz->getBlock(x,y,z))
{
dirty= false;
Mapz->ReadDesignations(x,y,z, &designations);
Mapz->ReadTileTypes(x,y,z, &tiles);
if (Mapz->isValidBlock(x,y,z+1))
if (Mapz->getBlock(x,y,z+1))
{
Mapz->ReadTileTypes(x,y,z+1, &tilesAbove);
}
@ -124,7 +129,7 @@ int main (void)
{
Mapz->WriteDesignations(x,y,z, &designations);
Mapz->WriteTileTypes(x,y,z, &tiles);
if (Mapz->isValidBlock(x,y,z+1))
if (Mapz->getBlock(x,y,z+1))
{
Mapz->WriteTileTypes(x,y,z+1, &tilesAbove);
}
@ -134,13 +139,10 @@ int main (void)
}
}
}
DF->Detach();
cout << "Found and changed " << count << " tiles." << endl;
cout << "Fixed " << countbad << " bad down ramps." << endl;
if(temporary_terminal)
{
cout << "Done. Press any key to continue" << endl;
cin.ignore();
}
return 0;
c->Resume();
if(count)
c->con.print("Found and changed %d tiles.",count);
if(countbad)
c->con.print("Fixed %d bad down ramps.",countbad);
return CR_OK;
}