dfhack/library/modules/Constructions.cpp

181 lines
4.5 KiB
C++

2012-01-27 08:47:14 -07:00
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
2010-04-09 16:24:41 -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 "Types.h"
#include "Core.h"
2012-01-24 15:11:48 -07:00
#include "modules/Constructions.h"
#include "modules/Buildings.h"
#include "modules/Maps.h"
#include "TileTypes.h"
2012-01-24 15:11:48 -07:00
#include "df/world.h"
#include "df/job_item.h"
#include "df/building_type.h"
#include "df/building_constructionst.h"
2010-04-09 16:24:41 -06:00
using namespace DFHack;
using namespace df::enums;
2012-01-24 15:11:48 -07:00
using df::global::world;
2010-04-09 16:24:41 -06:00
2012-01-24 15:11:48 -07:00
bool Constructions::isValid()
{
return (world != NULL);
}
2012-01-24 15:11:48 -07:00
uint32_t Constructions::getCount()
2010-04-09 16:24:41 -06:00
{
2012-01-24 15:11:48 -07:00
return world->constructions.size();
2010-04-09 16:24:41 -06:00
}
df::construction * Constructions::getConstruction(const int32_t index)
2010-04-09 16:24:41 -06:00
{
if (uint32_t(index) >= getCount())
2012-01-24 15:11:48 -07:00
return NULL;
return world->constructions[index];
2010-04-09 16:24:41 -06:00
}
df::construction * Constructions::findAtTile(df::coord pos)
{
2015-11-06 18:05:14 -07:00
for (auto it = world->constructions.begin(); it != world->constructions.end(); ++it) {
if ((*it)->pos == pos)
return *it;
}
return NULL;
}
bool Constructions::copyConstruction(const int32_t index, t_construction &out)
2010-04-09 16:24:41 -06:00
{
if (uint32_t(index) >= getCount())
return false;
out.origin = world->constructions[index];
out.pos = out.origin->pos;
out.item_type = out.origin->item_type;
2012-02-07 14:37:21 -07:00
out.item_subtype = out.origin->item_subtype;
out.mat_type = out.origin->mat_type;
out.mat_index = out.origin->mat_index;
out.flags = out.origin->flags;
out.original_tile = out.origin->original_tile;
return true;
2010-04-09 16:24:41 -06:00
}
bool Constructions::designateNew(df::coord pos, df::construction_type type,
df::item_type item, int mat_index)
{
auto ttype = Maps::getTileType(pos);
if (!ttype || tileMaterial(*ttype) == tiletype_material::CONSTRUCTION)
return false;
auto current = Buildings::findAtTile(pos);
if (current)
{
auto cons = strict_virtual_cast<df::building_constructionst>(current);
if (!cons)
return false;
cons->type = type;
return true;
}
auto newinst = Buildings::allocInstance(pos, building_type::Construction);
if (!newinst)
return false;
auto newcons = strict_virtual_cast<df::building_constructionst>(newinst);
newcons->type = type;
df::job_item *filter = new df::job_item();
filter->item_type = item;
filter->mat_index = mat_index;
filter->flags2.bits.building_material = true;
if (mat_index < 0)
filter->flags2.bits.non_economic = true;
std::vector<df::job_item*> filters;
filters.push_back(filter);
if (!Buildings::constructWithFilters(newinst, filters))
{
delete newinst;
return false;
}
return true;
}
bool Constructions::designateRemove(df::coord pos, bool *immediate)
{
using df::global::process_dig;
if (immediate)
*immediate = false;
if (auto current = Buildings::findAtTile(pos))
{
auto cons = strict_virtual_cast<df::building_constructionst>(current);
if (!cons)
return false;
if (Buildings::deconstruct(cons))
{
if (immediate)
*immediate = true;
}
return true;
}
auto block = Maps::getTileBlock(pos);
if (!block)
return false;
auto ttype = block->tiletype[pos.x&15][pos.y&15];
if (tileMaterial(ttype) == tiletype_material::CONSTRUCTION)
{
auto &dsgn = block->designation[pos.x&15][pos.y&15];
dsgn.bits.dig = tile_dig_designation::Default;
block->flags.bits.designated = true;
if (process_dig)
*process_dig = true;
return true;
}
return false;
}