2012-01-27 08:47:14 -07:00
|
|
|
/*
|
2011-06-16 15:53:39 -06:00
|
|
|
https://github.com/peterix/dfhack
|
2012-09-29 20:03:37 -06:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
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-05-26 04:24:45 -06:00
|
|
|
|
2016-08-13 19:44:01 -06:00
|
|
|
#include "Core.h"
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "MemAccess.h"
|
2016-08-13 19:44:01 -06:00
|
|
|
#include "TileTypes.h"
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Types.h"
|
2016-08-13 19:44:01 -06:00
|
|
|
#include "VersionInfo.h"
|
2012-05-01 09:55:25 -06:00
|
|
|
|
|
|
|
#include "modules/Buildings.h"
|
2016-08-13 19:44:01 -06:00
|
|
|
#include "modules/Constructions.h"
|
2012-05-01 09:55:25 -06:00
|
|
|
#include "modules/Maps.h"
|
|
|
|
|
|
|
|
#include "df/building_constructionst.h"
|
2016-08-13 19:44:01 -06:00
|
|
|
#include "df/building_type.h"
|
|
|
|
#include "df/job_item.h"
|
|
|
|
#include "df/map_block.h"
|
|
|
|
#include "df/world.h"
|
2012-05-01 09:55:25 -06:00
|
|
|
|
2010-04-09 16:24:41 -06:00
|
|
|
using namespace DFHack;
|
2012-05-01 09:55:25 -06:00
|
|
|
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-26 22:27:57 -07:00
|
|
|
|
2015-11-06 11:00:43 -07:00
|
|
|
df::construction * Constructions::findAtTile(df::coord pos)
|
|
|
|
{
|
2022-10-12 14:10:22 -06:00
|
|
|
int index = binsearch_index(world->constructions, pos);
|
|
|
|
if (index == -1) {
|
|
|
|
return NULL;
|
2015-11-06 11:00:43 -07:00
|
|
|
}
|
2022-10-12 14:10:22 -06:00
|
|
|
return world->constructions[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Constructions::insert(df::construction * constr)
|
|
|
|
{
|
|
|
|
bool toInsert;
|
|
|
|
insert_into_vector(world->constructions, &df::construction::pos, constr, &toInsert);
|
|
|
|
return toInsert;
|
2015-11-06 11:00:43 -07:00
|
|
|
}
|
|
|
|
|
2012-05-01 09:55:25 -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;
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:09:11 -06:00
|
|
|
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;
|
|
|
|
}
|