dfhack/library/modules/Buildings.cpp

212 lines
5.4 KiB
C++

2010-04-09 08:43:00 -06:00
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
2010-04-09 08:43:00 -06:00
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 <string>
#include <vector>
#include <map>
using namespace std;
2011-12-31 04:48:42 -07:00
#include "VersionInfo.h"
#include "MemAccess.h"
#include "Vector.h"
#include "Types.h"
#include "Error.h"
#include "modules/Buildings.h"
#include "ModuleFactory.h"
2011-12-31 04:48:42 -07:00
#include "Core.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;
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;
};
Module* DFHack::createBuildings()
{
return new Buildings();
}
Buildings::Buildings()
2010-04-09 08:43:00 -06:00
{
Core & c = Core::getInstance();
2010-04-09 08:43:00 -06:00
d = new Private;
2011-02-17 18:51:17 -07:00
d->p_bld = NULL;
d->Inited = d->Started = d->hasCustomWorkshops = false;
VersionInfo * mem = c.vinfo;
d->owner = c.p;
OffsetGroup * OG_build = mem->getGroup("Buildings");
2011-02-17 18:51:17 -07:00
d->Inited = true;
try
{
2011-02-17 18:51:17 -07:00
d->buildings_vector = OG_build->getAddress ("buildings_vector");
}
2010-11-07 16:10:59 -07:00
catch(DFHack::Error::AllMemdef &e)
{
2011-02-17 18:51:17 -07:00
cerr << "Buildings not available... " << e.what() << endl;
d->Inited = false;
}
if(d->Inited)
{
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::AllMemdef &e)
{
cerr << "Custom workshops not available. Memory Definition: " << e.what() << endl;
}
}
2010-04-09 08:43:00 -06:00
}
Buildings::~Buildings()
{
if(d->Started)
Finish();
delete d;
}
bool Buildings::Start(uint32_t & numbuildings)
{
2011-02-17 18:51:17 -07:00
if(!d->Inited)
return false;
d->p_bld = new DfVector <uint32_t> (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->Inited)
return false;
if(!d->hasCustomWorkshops)
return false;
2010-04-18 13:30:02 -06:00
Process * p = d->owner;
DfVector <uint32_t> p_matgloss (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
}