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 ReadDescriptorColors(void);
bool ReadOthers (void); bool ReadOthers (void);
void ReadAllMaterials(void); bool ReadAllMaterials(void);
std::string getType(const t_material & mat); std::string getType(const t_material & mat);
std::string getDescription(const t_material & mat); std::string getDescription(const t_material & mat);

@ -341,16 +341,18 @@ bool Materials::ReadCreatureTypesEx (void)
return true; return true;
} }
void Materials::ReadAllMaterials(void) bool Materials::ReadAllMaterials(void)
{ {
this->ReadInorganicMaterials(); bool ok = true;
this->ReadOrganicMaterials(); ok &= this->ReadInorganicMaterials();
this->ReadWoodMaterials(); ok &= this->ReadOrganicMaterials();
this->ReadPlantMaterials(); ok &= this->ReadWoodMaterials();
this->ReadCreatureTypes(); ok &= this->ReadPlantMaterials();
this->ReadCreatureTypesEx(); ok &= this->ReadCreatureTypes();
this->ReadDescriptorColors(); ok &= this->ReadCreatureTypesEx();
this->ReadOthers(); ok &= this->ReadDescriptorColors();
ok &= this->ReadOthers();
return ok;
} }
/// miserable pile of magic. The material system is insane. /// 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(liquids liquids.cpp)
DFHACK_PLUGIN(tubefill tubefill.cpp) DFHACK_PLUGIN(tubefill tubefill.cpp)
DFHACK_PLUGIN(autodump autodump.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. * Confiscates and dumps garbage owned by dwarfs.
*/ */
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <sstream> #include <sstream>
#include <climits> #include <climits>
#include <vector> #include <vector>
#include <set>
using namespace std; using namespace std;
#include <DFHack.h> #include <dfhack/Core.h>
#include <dfhack/DFVector.h> #include <dfhack/Console.h>
#include <dfhack/DFTypes.h> #include <dfhack/Export.h>
#include <dfhack/extra/termutil.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";
}
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;
}
int main (int argc, char *argv[]) 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 dump_scattered = false;
bool confiscate_all = false; bool confiscate_all = false;
bool dry_run = false; bool dry_run = false;
int wear_dump_level = 65536; int wear_dump_level = 65536;
for(int i = 1; i < argc; i++) for(int i = 0; i < parameters.size(); i++)
{ {
char *arg = argv[i]; string & param = parameters[i];
if (arg[0] != '-') if(param == "dryrun")
continue; dry_run = true;
else if(param == "scattered")
for (; *arg; arg++) { dump_scattered = true;
switch (arg[0]) { else if(param == "all")
case 'd': confiscate_all = true;
dry_run = true; else if(param == "x")
break; wear_dump_level = 1;
case 'l': else if(param == "X")
dump_scattered = true; wear_dump_level = 1;
break; else if(param == "?" || param == "help")
case 'a': {
confiscate_all = true; c->con.print("Oh no! Someone has to write the help text!\n");
break; return CR_OK;
case 'x': }
wear_dump_level = 1; else
break; {
case 'X': c->con.printerr("Parameter '%s' is not valid. See 'cleanowned help'.\n",param.c_str());
wear_dump_level = 2; return CR_FAILURE;
break;
}
} }
} }
DFHack::Materials *Materials = c->getMaterials();
DFHack::Items *Items = c->getItems();
DFHack::Creatures *Creatures = c->getCreatures();
DFHack::Translation *Tran = c->getTranslation();
DFHack::Process * p; uint32_t num_creatures;
unsigned int i; bool ok = true;
DFHack::ContextManager DFMgr("Memory.xml"); ok &= Materials->ReadAllMaterials();
DFHack::Context * DF; ok &= Creatures->Start(num_creatures);
try ok &= Tran->Start();
{
DF = DFMgr.getSingleContext(); vector<t_item *> p_items;
DF->Attach(); ok &= Items->readItemVector(p_items);
} if(!ok)
catch (exception& e)
{ {
cerr << e.what() << endl; c->con.printerr("Can't continue due to offset errors.\n");
if(temporary_terminal) c->Resume();
cin.ignore(); return CR_FAILURE;
return 1;
} }
c->con.print("Found total %d items.\n", p_items.size());
DFHack::VersionInfo * mem = DF->getMemoryInfo(); for (std::size_t i=0; i < p_items.size(); i++)
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);
for (i=0;i<size;i++)
{ {
uint32_t curItem = p_items[i]; t_item * curItem = p_items[i];
DFHack::dfh_item itm; DFHack::dfh_item itm;
Items->readItem(curItem, itm); Items->readItem(curItem, itm);
bool confiscate = false; bool confiscate = false;
bool dump = false; bool dump = false;
if (!itm.base.flags.owned) { if (!itm.base->flags.owned)
{
int32_t owner = Items->getItemOwnerID(itm); int32_t owner = Items->getItemOwnerID(itm);
if (owner >= 0) { if (owner >= 0)
printf("Fixing a misflagged item: "); {
c->con.print("Fixing a misflagged item: ");
confiscate = true; confiscate = true;
} }
else else
{
continue; continue;
}
} }
std::string name = Items->getItemClass(itm.matdesc.itemType); 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; confiscate = true;
} }
else if (itm.base.flags.on_ground && else if (itm.base->flags.on_ground &&
(name == "food" || name == "meat" || name == "plant")) (name == "food" || name == "meat" || name == "plant"))
{ {
printf("Confiscating a dropped foodstuff: \t"); c->con.print("Confiscating a dropped foodstuff: \t");
confiscate = true; confiscate = true;
} }
else if (itm.wear_level >= wear_dump_level) 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; confiscate = true;
dump = 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; confiscate = true;
dump = true; dump = true;
} }
else if (confiscate_all) else if (confiscate_all)
{ {
printf("Confiscating: \t"); c->con.print("Confiscating: \t");
confiscate = true; confiscate = true;
} }
@ -140,14 +154,14 @@ int main (int argc, char *argv[])
{ {
if (!dry_run) { if (!dry_run) {
if (!Items->removeItemOwner(itm, Creatures)) if (!Items->removeItemOwner(itm, Creatures))
printf("(unsuccessfully) "); c->con.print("(unsuccessfully) ");
if (dump) if (dump)
itm.base.flags.dump = 1; itm.base->flags.dump = 1;
Items->writeItem(itm); Items->writeItem(itm);
} }
printf( c->con.print(
"%s (wear %d)", "%s (wear %d)",
Items->getItemDescription(itm, Materials).c_str(), Items->getItemDescription(itm, Materials).c_str(),
itm.wear_level itm.wear_level
@ -167,10 +181,10 @@ int main (int argc, char *argv[])
info += std::string(" '") + temp.name.nickname + "'"; info += std::string(" '") + temp.name.nickname + "'";
info += " "; info += " ";
info += Tran->TranslateName(temp.name,false); 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( printf(
"%5d: %08x %08x (%d,%d,%d) #%08x [%d] %s - %s %s\n", "%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) return CR_OK;
{
cout << "Done. Press any key to continue" << endl;
cin.ignore();
}
return 0;
} }

@ -8,70 +8,62 @@
#include <vector> #include <vector>
using namespace std; using namespace std;
#include <DFHack.h> #include <dfhack/Core.h>
#include <dfhack/DFVector.h> #include <dfhack/Console.h>
#include <dfhack/DFTypes.h> #include <dfhack/Export.h>
#include <dfhack/PluginManager.h>
#include <dfhack/modules/Items.h>
#include <dfhack/Types.h>
#include <dfhack/extra/termutil.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(); return "cleartask";
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;
}
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; uint32_t item_vec_offset = 0;
try vector <t_item *> p_items;
{ if(!Items->readItemVector(p_items))
Items = DF->getItems();
DFHack::OffsetGroup* itemGroup = p->getDescriptor()->getGroup("Items");
item_vec_offset = itemGroup->getAddress("items_vector");
}
catch(DFHack::Error::All & e)
{ {
cerr << "Fatal error, exiting :(" << endl << e.what() << endl; c->con.printerr("Can't read items...\n");
if(temporary_terminal) c->Resume();
cin.ignore(); return CR_FAILURE;
return 1;
} }
DFHack::DfVector <uint32_t> p_items (p, item_vec_offset);
uint32_t size = p_items.size();
int numtasked = 0; 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; DFHack::dfh_item temp;
Items->readItem(p_items[i],temp); Items->readItem(p_items[i],temp);
DFHack::t_itemflags & flags = temp.base.flags; if (ptr->flags.in_job)
if (flags.in_job)
{ {
flags.in_job = 0; ptr->flags.in_job = 0;
Items->writeItem(temp);
numtasked++; numtasked++;
} }
} }
cout << "Found and untasked " << numtasked << " items." << endl; c->con.print("Found and untasked %d items.\n", numtasked);
c->Resume();
if(temporary_terminal) return CR_OK;
{
cout << "Done. Press any key to continue" << endl;
cin.ignore();
}
return 0;
} }

@ -7,14 +7,37 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
using namespace std; 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> DFhackCExport command_result df_deramp (Core * c, vector <string> & parameters);
#include <dfhack/DFTileTypes.h>
#include <dfhack/extra/termutil.h>
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 x_max,y_max,z_max;
uint32_t num_blocks = 0; uint32_t num_blocks = 0;
uint32_t bytes_read = 0; uint32_t bytes_read = 0;
@ -29,33 +52,15 @@ int main (void)
int count=0; int count=0;
int countbad=0; int countbad=0;
DFHack::ContextManager DFMgr("Memory.xml"); c->Suspend();
DFHack::Context *DF = DFMgr.getSingleContext(); DFHack::Maps *Mapz = c->getMaps();
//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();
// init the map // init the map
if (!Mapz->Start()) if (!Mapz->Start())
{ {
cerr << "Can't init map." << endl; c->con.printerr("Can't init map.\n");
if(temporary_terminal) c->Resume();
cin.ignore(); return CR_FAILURE;
return 1;
} }
Mapz->getSize(x_max,y_max,z_max); Mapz->getSize(x_max,y_max,z_max);
@ -69,12 +74,12 @@ int main (void)
{ {
for (uint32_t z = 0; z< z_max;z++) for (uint32_t z = 0; z< z_max;z++)
{ {
if (Mapz->isValidBlock(x,y,z)) if (Mapz->getBlock(x,y,z))
{ {
dirty= false; dirty= false;
Mapz->ReadDesignations(x,y,z, &designations); Mapz->ReadDesignations(x,y,z, &designations);
Mapz->ReadTileTypes(x,y,z, &tiles); 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); Mapz->ReadTileTypes(x,y,z+1, &tilesAbove);
} }
@ -124,7 +129,7 @@ int main (void)
{ {
Mapz->WriteDesignations(x,y,z, &designations); Mapz->WriteDesignations(x,y,z, &designations);
Mapz->WriteTileTypes(x,y,z, &tiles); 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); Mapz->WriteTileTypes(x,y,z+1, &tilesAbove);
} }
@ -134,13 +139,10 @@ int main (void)
} }
} }
} }
DF->Detach(); c->Resume();
cout << "Found and changed " << count << " tiles." << endl; if(count)
cout << "Fixed " << countbad << " bad down ramps." << endl; c->con.print("Found and changed %d tiles.",count);
if(temporary_terminal) if(countbad)
{ c->con.print("Fixed %d bad down ramps.",countbad);
cout << "Done. Press any key to continue" << endl; return CR_OK;
cin.ignore();
}
return 0;
} }