2010-04-09 08:43:00 -06:00
|
|
|
/*
|
2011-06-16 15:53:39 -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.
|
|
|
|
*/
|
|
|
|
|
2011-06-16 15:53:39 -06:00
|
|
|
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
2011-04-10 02:19:15 -06:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
using namespace std;
|
|
|
|
|
2010-08-20 06:10:05 -06:00
|
|
|
#include "dfhack/VersionInfo.h"
|
2011-06-12 15:17:40 -06:00
|
|
|
#include "dfhack/Process.h"
|
|
|
|
#include "dfhack/Vector.h"
|
|
|
|
#include "dfhack/Types.h"
|
|
|
|
#include "dfhack/Error.h"
|
2010-05-26 00:42:09 -06:00
|
|
|
#include "dfhack/modules/Buildings.h"
|
2011-03-18 01:53:59 -06:00
|
|
|
#include "ModuleFactory.h"
|
2011-06-17 07:02:43 -06:00
|
|
|
#include "dfhack/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;
|
2010-04-18 06:56:09 -06:00
|
|
|
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;
|
2010-09-22 10:34:51 -06:00
|
|
|
bool hasCustomWorkshops;
|
2010-04-09 08:43:00 -06:00
|
|
|
bool Started;
|
|
|
|
};
|
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
Module* DFHack::createBuildings()
|
2011-03-18 01:53:59 -06:00
|
|
|
{
|
2011-06-17 07:02:43 -06:00
|
|
|
return new Buildings();
|
2011-03-18 01:53:59 -06:00
|
|
|
}
|
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
Buildings::Buildings()
|
2010-04-09 08:43:00 -06:00
|
|
|
{
|
2011-06-17 07:02:43 -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;
|
2010-09-22 10:34:51 -06:00
|
|
|
d->Inited = d->Started = d->hasCustomWorkshops = false;
|
2011-06-17 07:02:43 -06:00
|
|
|
VersionInfo * mem = c.vinfo;
|
|
|
|
d->owner = c.p;
|
2010-08-29 16:08:17 -06:00
|
|
|
OffsetGroup * OG_build = mem->getGroup("Buildings");
|
2011-02-17 18:51:17 -07:00
|
|
|
d->Inited = true;
|
2010-09-22 10:34:51 -06:00
|
|
|
try
|
|
|
|
{
|
2011-02-17 18:51:17 -07:00
|
|
|
d->buildings_vector = OG_build->getAddress ("buildings_vector");
|
2010-09-22 10:34:51 -06:00
|
|
|
}
|
2010-11-07 16:10:59 -07:00
|
|
|
catch(DFHack::Error::AllMemdef &e)
|
2010-09-22 10:34:51 -06:00
|
|
|
{
|
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-09-22 10:34:51 -06:00
|
|
|
}
|
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;
|
2011-06-12 17:14:10 -06:00
|
|
|
d->p_bld = new DfVector <uint32_t> (d->buildings_vector);
|
2010-04-18 06:56:09 -06:00
|
|
|
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
|
2010-04-18 06:56:09 -06:00
|
|
|
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)
|
|
|
|
{
|
2011-02-24 19:13:50 -07:00
|
|
|
if(!d->Inited)
|
2010-04-12 19:11:26 -06:00
|
|
|
return false;
|
2010-09-22 10:34:51 -06:00
|
|
|
if(!d->hasCustomWorkshops)
|
|
|
|
return false;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-04-18 13:30:02 -06:00
|
|
|
Process * p = d->owner;
|
2011-06-12 17:14:10 -06:00
|
|
|
DfVector <uint32_t> p_matgloss (d->custom_workshop_vector);
|
2010-04-18 06:56:09 -06:00
|
|
|
uint32_t size = p_matgloss.size();
|
2010-04-12 16:03:29 -06:00
|
|
|
btypes.clear();
|
2010-08-20 06:10:05 -06:00
|
|
|
|
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)
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!d->Inited)
|
|
|
|
return false;
|
2010-09-22 10:34:51 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|