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(Vegetation);
MODULE_GETTER(Constructions);
MODULE_GETTER(Vermin);
MODULE_GETTER(Notes);
MODULE_GETTER(Graphic);

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

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

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

@ -37,150 +37,54 @@ using namespace std;
#include "ModuleFactory.h"
#include "Core.h"
using namespace DFHack;
using namespace DFHack::Simple;
struct Vermin::Private
{
void * spawn_points_vector;
uint32_t race_offset;
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;
}
#include "DataDefs.h"
#include "df/world.h"
using namespace df::enums;
using df::global::world;
// NOTE: caller must call delete on result when done.
SpawnPoints* Vermin::getSpawnPoints()
uint32_t Vermin::getNumVermin()
{
if (!d->Inited)
{
cerr << "Couldn't get spawn points: Vermin module not inited" << endl;
return NULL;
}
return new SpawnPoints(this);
return world->vermin.size();
}
SpawnPoints::SpawnPoints(Vermin* v_)
bool Vermin::Read (const uint32_t index, t_vermin & sp)
{
v = v_;
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())
if(index >= world->vermin.size())
return false;
// read pointer from vector at position
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);
df::world::T_vermin * verm = world->vermin[index];
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;
}
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;
// read pointer from vector at position
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);
df::world::T_vermin * verm = world->vermin[index];
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;
}
bool SpawnPoints::isWildColony(t_spawnPoint & point)
bool Vermin::isWildColony(t_vermin & point)
{
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::string;
using namespace DFHack;
using namespace DFHack::Simple;
#include <DFHack.h>
DFhackCExport command_result colonies (Core * c, vector <string> & parameters);
@ -32,10 +33,9 @@ DFhackCExport command_result plugin_shutdown ( Core * c )
return CR_OK;
}
void destroyColonies(DFHack::SpawnPoints *points);
void convertColonies(DFHack::SpawnPoints *points, DFHack::Materials *Materials);
void showColonies(Core *c, DFHack::SpawnPoints *points,
DFHack::Materials *Materials);
void destroyColonies();
void convertColonies(DFHack::Materials *Materials);
void showColonies(Core *c, DFHack::Materials *Materials);
DFhackCExport command_result colonies (Core * c, vector <string> & parameters)
{
@ -71,54 +71,41 @@ DFhackCExport command_result colonies (Core * c, vector <string> & parameters)
}
c->Suspend();
Vermin * vermin = c->getVermin();
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();
if (destroy)
destroyColonies(points);
destroyColonies();
else if (convert)
convertColonies(points, materials);
convertColonies(materials);
else
showColonies(c, points, materials);
delete points;
showColonies(c, materials);
vermin->Finish();
materials->Finish();
c->Resume();
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++)
{
DFHack::t_spawnPoint sp;
points->Read(i, sp);
Vermin::t_vermin sp;
Vermin::Read(i, sp);
if (sp.in_use && DFHack::SpawnPoints::isWildColony(sp))
if (sp.in_use && Vermin::isWildColony(sp))
{
sp.in_use = false;
points->Write(i, sp);
Vermin::Write(i, sp);
}
}
}
// Convert all colonies to honey bees.
void convertColonies(DFHack::SpawnPoints *points, DFHack::Materials *Materials)
void convertColonies(DFHack::Materials *Materials)
{
int bee_idx = -1;
for (size_t i = 0; i < Materials->raceEx.size(); i++)
@ -134,32 +121,31 @@ void convertColonies(DFHack::SpawnPoints *points, DFHack::Materials *Materials)
return;
}
uint32_t numSpawnPoints = points->size();
uint32_t numSpawnPoints = Vermin::getNumVermin();
for (uint32_t i = 0; i < numSpawnPoints; i++)
{
DFHack::t_spawnPoint sp;
points->Read(i, sp);
Vermin::t_vermin sp;
Vermin::Read(i, sp);
if (sp.in_use && DFHack::SpawnPoints::isWildColony(sp))
if (sp.in_use && Vermin::isWildColony(sp))
{
sp.race = bee_idx;
points->Write(i, sp);
Vermin::Write(i, sp);
}
}
}
void showColonies(Core *c, DFHack::SpawnPoints *points,
DFHack::Materials *Materials)
void showColonies(Core *c, DFHack::Materials *Materials)
{
uint32_t numSpawnPoints = points->size();
uint32_t numSpawnPoints = Vermin::getNumVermin();
int numColonies = 0;
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++;
string race="(no race)";