dfhack/library/modules/Buildings.cpp

189 lines
5.1 KiB
C++

2010-04-09 08:43:00 -06:00
/*
www.sourceforge.net/projects/dfhack
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include "ContextShared.h"
2010-05-26 00:42:09 -06:00
#include "dfhack/VersionInfo.h"
2010-05-25 22:48:23 -06:00
#include "dfhack/DFProcess.h"
#include "dfhack/DFVector.h"
#include "dfhack/DFTypes.h"
#include "dfhack/DFError.h"
2010-05-26 00:42:09 -06:00
//#include "dfhack/modules/Translation.h"
#include "dfhack/modules/Buildings.h"
2010-04-09 08:43:00 -06:00
using namespace DFHack;
//raw
struct t_building_df40d
{
uint32_t vtable;
uint32_t x1;
uint32_t y1;
uint32_t centerx;
uint32_t x2;
uint32_t y2;
uint32_t centery;
uint32_t z;
uint32_t height;
t_matglossPair material;
// not complete
};
struct Buildings::Private
{
uint32_t buildings_vector;
2010-04-12 16:03:29 -06:00
uint32_t custom_workshop_vector;
uint32_t building_custom_workshop_type;
uint32_t custom_workshop_type;
uint32_t custom_workshop_name;
int32_t custom_workshop_id;
DfVector <uint32_t> * p_bld;
DFContextShared *d;
2010-04-18 13:30:02 -06:00
Process * owner;
2010-04-09 08:43:00 -06:00
bool Inited;
bool hasCustomWorkshops;
2010-04-09 08:43:00 -06:00
bool Started;
};
Buildings::Buildings(DFContextShared * d_)
2010-04-09 08:43:00 -06:00
{
d = new Private;
d->d = d_;
2010-04-18 13:30:02 -06:00
d->owner = d_->p;
d->Inited = d->Started = d->hasCustomWorkshops = false;
VersionInfo * mem = d->d->offset_descriptor;
OffsetGroup * OG_build = mem->getGroup("Buildings");
d->buildings_vector = OG_build->getAddress ("buildings_vector");
try
{
d->custom_workshop_vector = OG_build->getAddress("custom_workshop_vector");
d->building_custom_workshop_type = OG_build->getOffset("building_custom_workshop_type");
d->custom_workshop_type = OG_build->getOffset("custom_workshop_type");
d->custom_workshop_name = OG_build->getOffset("custom_workshop_name");
mem->resolveClassnameToClassID("building_custom_workshop", d->custom_workshop_id);
d->hasCustomWorkshops = true;
}
catch(DFHack::Error::UnsetMemoryDefinition &e)
{
cerr << "Custom workshops not available. Unset Memory Definition: " << e.what() << endl;
}
2010-04-09 08:43:00 -06:00
d->Inited = true;
}
Buildings::~Buildings()
{
if(d->Started)
Finish();
delete d;
}
bool Buildings::Start(uint32_t & numbuildings)
{
2010-04-18 13:30:02 -06:00
d->p_bld = new DfVector <uint32_t> (d->owner, d->buildings_vector);
numbuildings = d->p_bld->size();
2010-04-09 08:43:00 -06:00
d->Started = true;
return true;
}
bool Buildings::Read (const uint32_t index, t_building & building)
{
if(!d->Started)
return false;
t_building_df40d bld_40d;
// read pointer from vector at position
uint32_t temp = d->p_bld->at (index);
2010-04-09 08:43:00 -06:00
//d->p_bld->read(index,(uint8_t *)&temp);
//read building from memory
2010-04-18 13:30:02 -06:00
d->owner->read (temp, sizeof (t_building_df40d), (uint8_t *) &bld_40d);
2010-04-09 08:43:00 -06:00
// transform
int32_t type = -1;
2010-04-18 16:32:50 -06:00
d->owner->getDescriptor()->resolveObjectToClassID (temp, type);
2010-04-09 08:43:00 -06:00
building.origin = temp;
building.vtable = bld_40d.vtable;
building.x1 = bld_40d.x1;
building.x2 = bld_40d.x2;
building.y1 = bld_40d.y1;
building.y2 = bld_40d.y2;
building.z = bld_40d.z;
building.material = bld_40d.material;
building.type = type;
return true;
}
bool Buildings::Finish()
{
if(d->p_bld)
{
delete d->p_bld;
d->p_bld = NULL;
}
d->Started = false;
2010-04-09 19:49:37 -06:00
return true;
2010-04-12 16:03:29 -06:00
}
bool Buildings::ReadCustomWorkshopTypes(map <uint32_t, string> & btypes)
{
if(!d->Started)
return false;
if(!d->hasCustomWorkshops)
return false;
2010-04-18 13:30:02 -06:00
Process * p = d->owner;
DfVector <uint32_t> p_matgloss (p, d->custom_workshop_vector);
uint32_t size = p_matgloss.size();
2010-04-12 16:03:29 -06:00
btypes.clear();
2010-04-12 16:03:29 -06:00
for (uint32_t i = 0; i < size;i++)
{
2010-04-18 13:30:02 -06:00
string out = p->readSTLString (p_matgloss[i] + d->custom_workshop_name);
uint32_t type = p->readDWord (p_matgloss[i] + d->custom_workshop_type);
2010-04-12 16:03:29 -06:00
#ifdef DEBUG
cout << out << ": " << type << endl;
#endif
btypes[type] = out;
}
return true;
}
int32_t Buildings::GetCustomWorkshopType(t_building & building)
{
if(!d->Inited)
return false;
if(!d->hasCustomWorkshops)
return false;
2010-04-12 16:03:29 -06:00
int32_t type = (int32_t)building.type;
int32_t ret = -1;
if(type != -1 && type == d->custom_workshop_id)
{
// read the custom workshop subtype
2010-04-18 13:30:02 -06:00
ret = (int32_t) d->owner->readDWord(building.origin + d->building_custom_workshop_type);
2010-04-12 16:03:29 -06:00
}
return ret;
2010-05-01 18:38:18 -06:00
}