Dissolve vermin module.

develop
Petr Mrázek 2012-01-08 06:59:52 +01:00
parent 72016d9188
commit 8a46386502
6 changed files with 71 additions and 231 deletions

@ -1098,6 +1098,5 @@ MODULE_GETTER(Items);
MODULE_GETTER(Translation); MODULE_GETTER(Translation);
MODULE_GETTER(Vegetation); MODULE_GETTER(Vegetation);
MODULE_GETTER(Constructions); MODULE_GETTER(Constructions);
MODULE_GETTER(Vermin);
MODULE_GETTER(Notes); MODULE_GETTER(Notes);
MODULE_GETTER(Graphic); MODULE_GETTER(Graphic);

@ -62,7 +62,6 @@ namespace DFHack
class Translation; class Translation;
class Vegetation; class Vegetation;
class Constructions; class Constructions;
class Vermin;
class Notes; class Notes;
class VersionInfo; class VersionInfo;
class VersionInfoFactory; class VersionInfoFactory;
@ -120,8 +119,6 @@ namespace DFHack
Vegetation * getVegetation(); Vegetation * getVegetation();
/// get the constructions module /// get the constructions module
Constructions * getConstructions(); Constructions * getConstructions();
/// get the vermin module
Vermin * getVermin();
/// get the notes module /// get the notes module
Notes * getNotes(); Notes * getNotes();
/// get the graphic module /// get the graphic module
@ -180,7 +177,6 @@ namespace DFHack
Translation * pTranslation; Translation * pTranslation;
Vegetation * pVegetation; Vegetation * pVegetation;
Constructions * pConstructions; Constructions * pConstructions;
Vermin * pVermin;
Notes * pNotes; Notes * pNotes;
Graphic * pGraphic; Graphic * pGraphic;
} s_mods; } s_mods;

@ -41,7 +41,6 @@ namespace DFHack
Module* createBuildings(); Module* createBuildings();
Module* createConstructions(); Module* createConstructions();
Module* createMaps(); Module* createMaps();
Module* createVermin();
Module* createNotes(); Module* createNotes();
Module* createGraphic(); Module* createGraphic();
} }

@ -1,22 +1,15 @@
#pragma once #pragma once
#ifndef CL_MOD_VERMIN
#define CL_MOD_VERMIN
/** /**
* \defgroup grp_vermin Wild vermin (ants, bees, etc) * \defgroup grp_vermin Wild vermin (ants, bees, etc)
* @ingroup grp_vermin
*/ */
#include "Export.h" #include "Export.h"
#include "Module.h" namespace DFHack { namespace Simple { namespace Vermin
#ifdef __cplusplus
namespace DFHack
{ {
#endif
/** /**
* Structure for holding a read DF vermin spawn point object * Structure for holding a read DF vermin spawn point object
* \ingroup grp_vermin * \ingroup grp_vermin
*/ */
struct t_spawnPoint struct t_vermin
{ {
void * origin; void * origin;
int16_t race; int16_t race;
@ -29,58 +22,21 @@ namespace DFHack
uint32_t countdown; uint32_t countdown;
}; };
#ifdef __cplusplus static const uint16_t TYPE_WILD_COLONY = 0xFFFF;
class DFContextShared;
class SpawnPoints;
/** /**
* The Vermin module - allows reading DF vermin * Get number of vermin objects
* \ingroup grp_modules
* \ingroup grp_vermin
*/ */
class DFHACK_EXPORT Vermin : public Module DFHACK_EXPORT uint32_t getNumVermin();
{ /**
public: * Read from vermin object
Vermin(); */
~Vermin(); DFHACK_EXPORT bool Read (const uint32_t index, t_vermin & point);
/**
bool Finish(); * Write into vermin object
*/
// NOTE: caller must call delete on result when done. DFHACK_EXPORT bool Write (const uint32_t index, t_vermin & point);
SpawnPoints* getSpawnPoints(); /**
* Is vermin object a colony?
private: */
struct Private; DFHACK_EXPORT bool isWildColony(t_vermin & point);
Private *d; } } } // end DFHack::Simple::Vermin
friend class SpawnPoints;
};
class DFHACK_EXPORT SpawnPoints
{
public:
static const uint16_t TYPE_WILD_COLONY = 0xFFFF;
protected:
SpawnPoints(Vermin * v);
public:
~SpawnPoints();
size_t size();
bool Read (const uint32_t index, t_spawnPoint & point);
bool Write (const uint32_t index, t_spawnPoint & point);
bool isValid();
static bool isWildColony(t_spawnPoint & point);
private:
Vermin* v;
std::vector <char*> * p_sp;
friend class Vermin;
};
}
#endif // __cplusplus
#endif

@ -37,150 +37,54 @@ using namespace std;
#include "ModuleFactory.h" #include "ModuleFactory.h"
#include "Core.h" #include "Core.h"
using namespace DFHack; using namespace DFHack;
using namespace DFHack::Simple;
struct Vermin::Private #include "DataDefs.h"
{ #include "df/world.h"
void * spawn_points_vector; using namespace df::enums;
uint32_t race_offset; using df::global::world;
uint32_t type_offset;
uint32_t position_offset;
uint32_t in_use_offset;
uint32_t unknown_offset;
uint32_t countdown_offset;
Process * owner;
bool Inited;
bool Started;
};
Module* DFHack::createVermin()
{
return new Vermin();
}
#include <stdio.h>
Vermin::Vermin()
{
Core & c = Core::getInstance();
d = new Private;
d->owner = c.p;
d->Inited = d->Started = false;
VersionInfo * mem = c.vinfo;
OffsetGroup * OG_vermin = mem->getGroup("Vermin");
OffsetGroup * OG_spawn = OG_vermin->getGroup("Spawn Points");
d->Inited = true;
try
{
d->spawn_points_vector = OG_spawn->getAddress("vector");
d->race_offset = OG_spawn->getOffset("race");
d->type_offset = OG_spawn->getOffset("type");
d->position_offset = OG_spawn->getOffset("position");
d->in_use_offset = OG_spawn->getOffset("in_use");
d->unknown_offset = OG_spawn->getOffset("unknown");
d->countdown_offset = OG_spawn->getOffset("countdown");
}
catch(DFHack::Error::AllMemdef &e)
{
cerr << "Vermin not available... " << e.what() << endl;
d->Inited = false;
}
}
bool Vermin::Finish()
{
return true;
}
Vermin::~Vermin()
{
delete d;
}
// NOTE: caller must call delete on result when done. uint32_t Vermin::getNumVermin()
SpawnPoints* Vermin::getSpawnPoints()
{ {
if (!d->Inited) return world->vermin.size();
{
cerr << "Couldn't get spawn points: Vermin module not inited" << endl;
return NULL;
}
return new SpawnPoints(this);
} }
SpawnPoints::SpawnPoints(Vermin* v_) bool Vermin::Read (const uint32_t index, t_vermin & sp)
{ {
v = v_; if(index >= world->vermin.size())
p_sp = NULL;
if (!v->d->Inited)
{
cerr << "Couldn't get spawn points: Vermin module not inited" << endl;
return;
}
p_sp = (vector <char*>*) (v->d->spawn_points_vector);
}
SpawnPoints::~SpawnPoints()
{
}
size_t SpawnPoints::size()
{
if (!isValid())
return 0;
return p_sp->size();
}
bool SpawnPoints::Read (const uint32_t index, t_spawnPoint & sp)
{
if(!isValid())
return false; return false;
// read pointer from vector at position df::world::T_vermin * verm = world->vermin[index];
char * temp = p_sp->at (index);
sp.origin = temp;
sp.race = v->d->owner->readWord(temp + v->d->race_offset);
sp.type = v->d->owner->readWord(temp + v->d->type_offset);
sp.in_use = v->d->owner->readByte(temp + v->d->in_use_offset);
sp.unknown = v->d->owner->readByte(temp + v->d->unknown_offset);
sp.countdown = v->d->owner->readDWord(temp + v->d->countdown_offset);
// Three consecutive 16 bit numbers for x/y/z
v->d->owner->read(temp + v->d->position_offset, 6, (uint8_t*) &sp.x);
sp.origin = verm;
sp.race = verm->race;
sp.type = verm->type;
sp.in_use = verm->in_use;
sp.countdown = verm->countdown;
sp.x = verm->x;
sp.y = verm->y;
sp.z = verm->z;
return true; return true;
} }
bool SpawnPoints::Write (const uint32_t index, t_spawnPoint & sp) bool Vermin::Write (const uint32_t index, t_vermin & sp)
{ {
if(!isValid()) if(index >= world->vermin.size())
return false; return false;
// read pointer from vector at position df::world::T_vermin * verm = world->vermin[index];
char * temp = p_sp->at (index);
v->d->owner->writeWord(temp + v->d->race_offset, sp.race);
v->d->owner->writeWord(temp + v->d->type_offset, sp.type);
v->d->owner->writeByte(temp + v->d->in_use_offset, sp.in_use);
v->d->owner->writeByte(temp + v->d->unknown_offset, sp.unknown);
v->d->owner->writeDWord(temp + v->d->countdown_offset, sp.countdown);
// Three consecutive 16 bit numbers for x/y/z
v->d->owner->write(temp + v->d->position_offset, 6, (uint8_t*) &sp.x);
verm->race = sp.race;
verm->type = sp.type;
verm->in_use = sp.in_use;
verm->countdown = sp.countdown;
verm->x = sp.x;
verm->y = sp.y;
verm->z = sp.z;
return true; return true;
} }
bool SpawnPoints::isWildColony(t_spawnPoint & point) bool Vermin::isWildColony(t_vermin & point)
{ {
return (point.type == TYPE_WILD_COLONY); return (point.type == TYPE_WILD_COLONY);
} }
bool SpawnPoints::isValid()
{
return (v != NULL && v->d->Inited && p_sp != NULL);
}

@ -9,6 +9,7 @@
using std::vector; using std::vector;
using std::string; using std::string;
using namespace DFHack; using namespace DFHack;
using namespace DFHack::Simple;
#include <DFHack.h> #include <DFHack.h>
DFhackCExport command_result colonies (Core * c, vector <string> & parameters); DFhackCExport command_result colonies (Core * c, vector <string> & parameters);
@ -32,10 +33,9 @@ DFhackCExport command_result plugin_shutdown ( Core * c )
return CR_OK; return CR_OK;
} }
void destroyColonies(DFHack::SpawnPoints *points); void destroyColonies();
void convertColonies(DFHack::SpawnPoints *points, DFHack::Materials *Materials); void convertColonies(DFHack::Materials *Materials);
void showColonies(Core *c, DFHack::SpawnPoints *points, void showColonies(Core *c, DFHack::Materials *Materials);
DFHack::Materials *Materials);
DFhackCExport command_result colonies (Core * c, vector <string> & parameters) DFhackCExport command_result colonies (Core * c, vector <string> & parameters)
{ {
@ -71,54 +71,41 @@ DFhackCExport command_result colonies (Core * c, vector <string> & parameters)
} }
c->Suspend(); c->Suspend();
Vermin * vermin = c->getVermin();
Materials * materials = c->getMaterials(); Materials * materials = c->getMaterials();
SpawnPoints *points = vermin->getSpawnPoints();
if(!points || !points->isValid())
{
c->con.printerr("vermin not supported for this DF version\n");
c->Resume();
return CR_FAILURE;
}
materials->ReadCreatureTypesEx(); materials->ReadCreatureTypesEx();
if (destroy) if (destroy)
destroyColonies(points); destroyColonies();
else if (convert) else if (convert)
convertColonies(points, materials); convertColonies(materials);
else else
showColonies(c, points, materials); showColonies(c, materials);
delete points;
vermin->Finish();
materials->Finish(); materials->Finish();
c->Resume(); c->Resume();
return CR_OK; return CR_OK;
} }
void destroyColonies(DFHack::SpawnPoints *points) void destroyColonies()
{ {
uint32_t numSpawnPoints = points->size(); uint32_t numSpawnPoints = Vermin::getNumVermin();
for (uint32_t i = 0; i < numSpawnPoints; i++) for (uint32_t i = 0; i < numSpawnPoints; i++)
{ {
DFHack::t_spawnPoint sp; Vermin::t_vermin sp;
points->Read(i, sp); Vermin::Read(i, sp);
if (sp.in_use && DFHack::SpawnPoints::isWildColony(sp)) if (sp.in_use && Vermin::isWildColony(sp))
{ {
sp.in_use = false; sp.in_use = false;
points->Write(i, sp); Vermin::Write(i, sp);
} }
} }
} }
// Convert all colonies to honey bees. // Convert all colonies to honey bees.
void convertColonies(DFHack::SpawnPoints *points, DFHack::Materials *Materials) void convertColonies(DFHack::Materials *Materials)
{ {
int bee_idx = -1; int bee_idx = -1;
for (size_t i = 0; i < Materials->raceEx.size(); i++) for (size_t i = 0; i < Materials->raceEx.size(); i++)
@ -134,32 +121,31 @@ void convertColonies(DFHack::SpawnPoints *points, DFHack::Materials *Materials)
return; return;
} }
uint32_t numSpawnPoints = points->size(); uint32_t numSpawnPoints = Vermin::getNumVermin();
for (uint32_t i = 0; i < numSpawnPoints; i++) for (uint32_t i = 0; i < numSpawnPoints; i++)
{ {
DFHack::t_spawnPoint sp; Vermin::t_vermin sp;
points->Read(i, sp); Vermin::Read(i, sp);
if (sp.in_use && DFHack::SpawnPoints::isWildColony(sp)) if (sp.in_use && Vermin::isWildColony(sp))
{ {
sp.race = bee_idx; sp.race = bee_idx;
points->Write(i, sp); Vermin::Write(i, sp);
} }
} }
} }
void showColonies(Core *c, DFHack::SpawnPoints *points, void showColonies(Core *c, DFHack::Materials *Materials)
DFHack::Materials *Materials)
{ {
uint32_t numSpawnPoints = points->size(); uint32_t numSpawnPoints = Vermin::getNumVermin();
int numColonies = 0; int numColonies = 0;
for (uint32_t i = 0; i < numSpawnPoints; i++) for (uint32_t i = 0; i < numSpawnPoints; i++)
{ {
DFHack::t_spawnPoint sp; Vermin::t_vermin sp;
points->Read(i, sp); Vermin::Read(i, sp);
if (sp.in_use && DFHack::SpawnPoints::isWildColony(sp)) if (sp.in_use && Vermin::isWildColony(sp))
{ {
numColonies++; numColonies++;
string race="(no race)"; string race="(no race)";