2014-08-19 12:47:05 -06: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 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
|
|
|
|
2012-12-18 18:28:30 -07:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
2011-04-10 02:19:15 -06:00
|
|
|
#include <string>
|
2012-12-18 18:28:30 -07:00
|
|
|
#include <unordered_map>
|
2011-04-10 02:19:15 -06:00
|
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
|
2012-12-18 18:28:30 -07:00
|
|
|
#include "ColorText.h"
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "VersionInfo.h"
|
|
|
|
#include "MemAccess.h"
|
|
|
|
#include "Types.h"
|
|
|
|
#include "Error.h"
|
|
|
|
#include "modules/Buildings.h"
|
2012-04-29 11:07:39 -06:00
|
|
|
#include "modules/Maps.h"
|
|
|
|
#include "modules/Job.h"
|
2011-03-18 01:53:59 -06:00
|
|
|
#include "ModuleFactory.h"
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Core.h"
|
2012-04-29 11:07:39 -06:00
|
|
|
#include "TileTypes.h"
|
|
|
|
#include "MiscUtils.h"
|
2010-04-09 08:43:00 -06:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2012-01-05 15:39:14 -07:00
|
|
|
#include "DataDefs.h"
|
2016-08-13 19:44:01 -06:00
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
#include "df/building_axle_horizontalst.h"
|
2016-01-31 12:50:07 -07:00
|
|
|
#include "df/building_bars_floorst.h"
|
|
|
|
#include "df/building_bars_verticalst.h"
|
2012-04-29 11:07:39 -06:00
|
|
|
#include "df/building_bridgest.h"
|
2017-06-25 13:21:45 -06:00
|
|
|
#include "df/building_cagest.h"
|
2012-05-06 01:22:55 -06:00
|
|
|
#include "df/building_civzonest.h"
|
2016-01-31 12:50:07 -07:00
|
|
|
#include "df/building_coffinst.h"
|
|
|
|
#include "df/building_def.h"
|
|
|
|
#include "df/building_design.h"
|
|
|
|
#include "df/building_floodgatest.h"
|
2012-04-29 11:07:39 -06:00
|
|
|
#include "df/building_furnacest.h"
|
2016-01-31 12:50:07 -07:00
|
|
|
#include "df/building_grate_floorst.h"
|
|
|
|
#include "df/building_grate_wallst.h"
|
|
|
|
#include "df/building_rollersst.h"
|
2012-04-29 11:07:39 -06:00
|
|
|
#include "df/building_screw_pumpst.h"
|
2016-01-31 12:50:07 -07:00
|
|
|
#include "df/building_stockpilest.h"
|
|
|
|
#include "df/building_trapst.h"
|
2012-04-29 11:07:39 -06:00
|
|
|
#include "df/building_water_wheelst.h"
|
2023-03-23 01:25:29 -06:00
|
|
|
#include "df/building_weaponst.h"
|
2012-04-29 11:07:39 -06:00
|
|
|
#include "df/building_wellst.h"
|
2016-01-31 12:50:07 -07:00
|
|
|
#include "df/building_workshopst.h"
|
|
|
|
#include "df/buildings_other_id.h"
|
|
|
|
#include "df/d_init.h"
|
2018-05-12 15:00:21 -06:00
|
|
|
#include "df/dfhack_room_quality_level.h"
|
2016-01-31 12:50:07 -07:00
|
|
|
#include "df/general_ref_building_holderst.h"
|
2017-06-25 13:21:45 -06:00
|
|
|
#include "df/general_ref_contains_unitst.h"
|
2016-01-31 12:50:07 -07:00
|
|
|
#include "df/item.h"
|
2017-06-25 13:21:45 -06:00
|
|
|
#include "df/item_cagest.h"
|
2016-01-31 12:50:07 -07:00
|
|
|
#include "df/job.h"
|
|
|
|
#include "df/job_item.h"
|
2016-08-13 19:44:01 -06:00
|
|
|
#include "df/map_block.h"
|
|
|
|
#include "df/tile_occupancy.h"
|
2023-01-05 18:11:01 -07:00
|
|
|
#include "df/plotinfost.h"
|
2023-01-20 11:02:55 -07:00
|
|
|
#include "df/squad.h"
|
2016-01-31 12:50:07 -07:00
|
|
|
#include "df/ui_look_list.h"
|
|
|
|
#include "df/unit.h"
|
2016-08-10 21:50:00 -06:00
|
|
|
#include "df/unit_relationship_type.h"
|
2016-01-31 12:50:07 -07:00
|
|
|
#include "df/world.h"
|
2012-01-05 15:39:14 -07:00
|
|
|
|
|
|
|
using namespace df::enums;
|
2023-01-05 18:11:01 -07:00
|
|
|
using df::global::plotinfo;
|
2012-01-05 15:39:14 -07:00
|
|
|
using df::global::world;
|
2012-04-29 11:07:39 -06:00
|
|
|
using df::global::d_init;
|
|
|
|
using df::global::building_next_id;
|
|
|
|
using df::global::process_jobs;
|
2012-01-05 15:39:14 -07:00
|
|
|
using df::building_def;
|
|
|
|
|
2012-12-18 18:28:30 -07:00
|
|
|
struct CoordHash {
|
|
|
|
size_t operator()(const df::coord pos) const {
|
|
|
|
return pos.x*65537 + pos.y*17 + pos.z;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static unordered_map<df::coord, int32_t, CoordHash> locationToBuilding;
|
|
|
|
|
2020-11-03 12:56:16 -07:00
|
|
|
static df::building_extents_type *getExtentTile(df::building_extents &extent, df::coord2d tile)
|
2012-05-01 09:55:25 -06:00
|
|
|
{
|
|
|
|
if (!extent.extents)
|
|
|
|
return NULL;
|
|
|
|
int dx = tile.x - extent.x;
|
|
|
|
int dy = tile.y - extent.y;
|
|
|
|
if (dx < 0 || dy < 0 || dx >= extent.width || dy >= extent.height)
|
|
|
|
return NULL;
|
|
|
|
return &extent.extents[dx + dy*extent.width];
|
|
|
|
}
|
|
|
|
|
2012-05-17 10:38:27 -06:00
|
|
|
/*
|
|
|
|
* A monitor to work around this bug, in its application to buildings:
|
|
|
|
*
|
|
|
|
* http://www.bay12games.com/dwarves/mantisbt/view.php?id=1416
|
|
|
|
*/
|
|
|
|
bool buildings_do_onupdate = false;
|
|
|
|
|
|
|
|
void buildings_onStateChange(color_ostream &out, state_change_event event)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case SC_MAP_LOADED:
|
|
|
|
buildings_do_onupdate = true;
|
|
|
|
break;
|
|
|
|
case SC_MAP_UNLOADED:
|
|
|
|
buildings_do_onupdate = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void buildings_onUpdate(color_ostream &out)
|
|
|
|
{
|
|
|
|
buildings_do_onupdate = false;
|
|
|
|
|
2017-11-24 22:59:59 -07:00
|
|
|
df::job_list_link *link = world->jobs.list.next;
|
2012-05-17 10:38:27 -06:00
|
|
|
for (; link; link = link->next) {
|
|
|
|
df::job *job = link->item;
|
|
|
|
|
|
|
|
if (job->job_type != job_type::ConstructBuilding)
|
|
|
|
continue;
|
|
|
|
if (job->job_items.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
buildings_do_onupdate = true;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < job->items.size(); i++)
|
|
|
|
{
|
|
|
|
df::job_item_ref *iref = job->items[i];
|
|
|
|
if (iref->role != df::job_item_ref::Reagent)
|
|
|
|
continue;
|
|
|
|
df::job_item *item = vector_get(job->job_items, iref->job_item_idx);
|
|
|
|
if (!item)
|
|
|
|
continue;
|
|
|
|
// Convert Reagent to Hauled, while decrementing quantity
|
|
|
|
item->quantity = std::max(0, item->quantity-1);
|
|
|
|
iref->role = df::job_item_ref::Hauled;
|
|
|
|
iref->job_item_idx = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 20:26:00 -07:00
|
|
|
static void building_into_zone_unidir(df::building* bld, df::building_civzonest* zone)
|
|
|
|
{
|
2023-01-20 11:30:38 -07:00
|
|
|
for (auto contained_building : zone->contained_buildings)
|
2023-01-18 20:26:00 -07:00
|
|
|
{
|
2023-01-20 11:30:38 -07:00
|
|
|
if (contained_building == bld)
|
2023-01-18 20:26:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-20 11:30:38 -07:00
|
|
|
insert_into_vector(zone->contained_buildings, &df::building::id, bld);
|
2023-01-18 20:26:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void zone_into_building_unidir(df::building* bld, df::building_civzonest* zone)
|
|
|
|
{
|
2023-01-20 11:30:38 -07:00
|
|
|
for (auto relation : bld->relations)
|
2023-01-18 20:26:00 -07:00
|
|
|
{
|
2023-01-20 11:30:38 -07:00
|
|
|
if (relation == zone)
|
2023-01-18 20:26:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-21 12:15:28 -07:00
|
|
|
insert_into_vector(bld->relations, &df::building_civzonest::id, zone);
|
2023-01-18 20:26:00 -07:00
|
|
|
}
|
|
|
|
|
2023-01-19 15:17:54 -07:00
|
|
|
static bool is_suitable_building_for_zoning(df::building* bld)
|
|
|
|
{
|
|
|
|
return bld->canMakeRoom();
|
|
|
|
}
|
|
|
|
|
2023-01-18 20:26:00 -07:00
|
|
|
static void add_building_to_zone(df::building* bld, df::building_civzonest* zone)
|
|
|
|
{
|
2023-01-19 15:17:54 -07:00
|
|
|
if (!is_suitable_building_for_zoning(bld))
|
2023-01-18 20:26:00 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
building_into_zone_unidir(bld, zone);
|
|
|
|
zone_into_building_unidir(bld, zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_building_to_all_zones(df::building* bld)
|
|
|
|
{
|
|
|
|
if (!is_suitable_building_for_zoning(bld))
|
|
|
|
return;
|
|
|
|
|
|
|
|
df::coord coord(bld->centerx, bld->centery, bld->z);
|
|
|
|
|
|
|
|
std::vector<df::building_civzonest*> cv;
|
|
|
|
Buildings::findCivzonesAt(&cv, coord);
|
|
|
|
|
2023-01-20 11:30:38 -07:00
|
|
|
for (auto zone : cv)
|
|
|
|
add_building_to_zone(bld, zone);
|
2023-01-18 20:26:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void add_zone_to_all_buildings(df::building* zone_as_building)
|
|
|
|
{
|
|
|
|
if (zone_as_building->getType() != building_type::Civzone)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto zone = strict_virtual_cast<df::building_civzonest>(zone_as_building);
|
|
|
|
|
|
|
|
if (zone == nullptr)
|
|
|
|
return;
|
|
|
|
|
2023-01-20 11:30:38 -07:00
|
|
|
for (auto bld : world->buildings.other.IN_PLAY)
|
2023-01-18 20:26:00 -07:00
|
|
|
{
|
2023-01-20 11:30:38 -07:00
|
|
|
if (zone->z != bld->z)
|
2023-01-18 20:26:00 -07:00
|
|
|
continue;
|
|
|
|
|
2023-01-20 11:30:38 -07:00
|
|
|
if (!is_suitable_building_for_zoning(bld))
|
2023-01-18 20:26:00 -07:00
|
|
|
continue;
|
|
|
|
|
2023-01-20 11:30:38 -07:00
|
|
|
int32_t cx = bld->centerx;
|
|
|
|
int32_t cy = bld->centery;
|
2023-01-18 20:26:00 -07:00
|
|
|
|
|
|
|
df::coord2d coord(cx, cy);
|
|
|
|
|
|
|
|
//can a zone without extents exist?
|
|
|
|
if (zone->room.extents && zone->isExtentShaped())
|
|
|
|
{
|
|
|
|
auto etile = getExtentTile(zone->room, coord);
|
|
|
|
if (!etile || !*etile)
|
|
|
|
continue;
|
|
|
|
|
2023-01-20 11:30:38 -07:00
|
|
|
add_building_to_zone(bld, zone);
|
2023-01-18 20:26:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_building_from_zone(df::building* bld, df::building_civzonest* zone)
|
|
|
|
{
|
|
|
|
for (int bid = 0; bid < (int)zone->contained_buildings.size(); bid++)
|
|
|
|
{
|
|
|
|
if (zone->contained_buildings[bid] == bld)
|
|
|
|
{
|
|
|
|
zone->contained_buildings.erase(zone->contained_buildings.begin() + bid);
|
|
|
|
bid--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int bid = 0; bid < (int)bld->relations.size(); bid++)
|
|
|
|
{
|
|
|
|
if (bld->relations[bid] == zone)
|
|
|
|
{
|
|
|
|
bld->relations.erase(bld->relations.begin() + bid);
|
|
|
|
bid--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_building_from_all_zones(df::building* bld)
|
|
|
|
{
|
|
|
|
df::coord coord(bld->centerx, bld->centery, bld->z);
|
|
|
|
|
|
|
|
std::vector<df::building_civzonest*> cv;
|
|
|
|
Buildings::findCivzonesAt(&cv, coord);
|
|
|
|
|
2023-01-20 11:30:38 -07:00
|
|
|
for (auto zone : cv)
|
|
|
|
remove_building_from_zone(bld, zone);
|
2023-01-18 20:26:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_zone_from_all_buildings(df::building* zone_as_building)
|
|
|
|
{
|
|
|
|
if (zone_as_building->getType() != building_type::Civzone)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto zone = strict_virtual_cast<df::building_civzonest>(zone_as_building);
|
|
|
|
|
|
|
|
if (zone == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//this is a paranoid check and slower than it could be. Zones contain a list of children
|
|
|
|
//good for fixing potentially bad game states when deleting a zone
|
2023-01-20 11:30:38 -07:00
|
|
|
for (auto bld : world->buildings.other.IN_PLAY)
|
2023-01-18 20:26:00 -07:00
|
|
|
remove_building_from_zone(bld, zone);
|
|
|
|
}
|
|
|
|
|
2012-01-07 18:22:13 -07:00
|
|
|
uint32_t Buildings::getNumBuildings()
|
2010-04-09 08:43:00 -06:00
|
|
|
{
|
2012-01-07 18:22:13 -07:00
|
|
|
return world->buildings.all.size();
|
2010-04-09 08:43:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Buildings::Read (const uint32_t index, t_building & building)
|
|
|
|
{
|
2012-02-18 10:34:52 -07:00
|
|
|
df::building *bld = world->buildings.all[index];
|
|
|
|
|
|
|
|
building.x1 = bld->x1;
|
|
|
|
building.x2 = bld->x2;
|
|
|
|
building.y1 = bld->y1;
|
|
|
|
building.y2 = bld->y2;
|
|
|
|
building.z = bld->z;
|
|
|
|
building.material.index = bld->mat_index;
|
|
|
|
building.material.type = bld->mat_type;
|
|
|
|
building.type = bld->getType();
|
|
|
|
building.subtype = bld->getSubtype();
|
|
|
|
building.custom_type = bld->getCustomType();
|
|
|
|
building.origin = bld;
|
2010-04-09 08:43:00 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-10 08:03:11 -07:00
|
|
|
bool Buildings::ReadCustomWorkshopTypes(map <uint32_t, string> & btypes)
|
2010-04-12 16:03:29 -06:00
|
|
|
{
|
2012-01-05 15:39:14 -07:00
|
|
|
vector <building_def *> & bld_def = world->raws.buildings.all;
|
2010-04-12 16:03:29 -06:00
|
|
|
btypes.clear();
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2021-09-06 23:16:21 -06:00
|
|
|
for (building_def *temp : bld_def)
|
2010-04-12 16:03:29 -06:00
|
|
|
{
|
2012-01-05 15:39:14 -07:00
|
|
|
btypes[temp->id] = temp->code;
|
2010-04-12 16:03:29 -06:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-12 08:17:32 -07:00
|
|
|
df::general_ref *Buildings::getGeneralRef(df::building *building, df::general_ref_type type)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(building);
|
|
|
|
|
|
|
|
return findRef(building->general_refs, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
df::specific_ref *Buildings::getSpecificRef(df::building *building, df::specific_ref_type type)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(building);
|
|
|
|
|
|
|
|
return findRef(building->specific_refs, type);
|
|
|
|
}
|
|
|
|
|
2023-05-31 19:48:08 -06:00
|
|
|
bool Buildings::setOwner(df::building_civzonest *bld, df::unit *unit)
|
2012-08-24 08:26:18 -06:00
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(bld);
|
2023-05-31 19:48:08 -06:00
|
|
|
|
|
|
|
if (bld->assigned_unit == unit)
|
2012-08-24 08:26:18 -06:00
|
|
|
return true;
|
|
|
|
|
2023-06-01 11:53:08 -06:00
|
|
|
df::building * pbld = virtual_cast<df::building>(bld);
|
2023-05-31 19:48:08 -06:00
|
|
|
|
|
|
|
if (bld->assigned_unit)
|
2012-08-24 08:26:18 -06:00
|
|
|
{
|
2023-05-31 19:48:08 -06:00
|
|
|
auto &blist = bld->assigned_unit->owned_buildings;
|
|
|
|
vector_erase_at(blist, linear_index(blist, pbld));
|
2012-08-24 08:26:18 -06:00
|
|
|
|
2023-05-31 19:48:08 -06:00
|
|
|
if (auto spouse = df::unit::find(bld->assigned_unit->relationship_ids[df::unit_relationship_type::Spouse]))
|
2012-08-24 08:26:18 -06:00
|
|
|
{
|
|
|
|
auto &blist = spouse->owned_buildings;
|
2023-05-31 19:48:08 -06:00
|
|
|
vector_erase_at(blist, linear_index(blist, pbld));
|
2012-08-24 08:26:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 19:48:08 -06:00
|
|
|
bld->assigned_unit = unit;
|
2012-08-24 08:26:18 -06:00
|
|
|
|
|
|
|
if (unit)
|
|
|
|
{
|
2023-05-31 19:48:08 -06:00
|
|
|
bld->assigned_unit_id = unit->id;
|
2012-08-24 08:26:18 -06:00
|
|
|
unit->owned_buildings.push_back(bld);
|
|
|
|
|
2016-08-10 21:50:00 -06:00
|
|
|
if (auto spouse = df::unit::find(unit->relationship_ids[df::unit_relationship_type::Spouse]))
|
2012-08-24 08:26:18 -06:00
|
|
|
{
|
|
|
|
auto &blist = spouse->owned_buildings;
|
2023-05-31 19:48:08 -06:00
|
|
|
if (bld->canUseSpouseRoom() && linear_index(blist, pbld) < 0)
|
2012-08-24 08:26:18 -06:00
|
|
|
blist.push_back(bld);
|
|
|
|
}
|
|
|
|
}
|
2016-08-21 18:58:40 -06:00
|
|
|
else
|
|
|
|
{
|
2023-05-31 19:48:08 -06:00
|
|
|
bld->assigned_unit_id = -1;
|
2016-08-21 18:58:40 -06:00
|
|
|
}
|
2012-08-24 08:26:18 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-01 09:55:25 -06:00
|
|
|
df::building *Buildings::findAtTile(df::coord pos)
|
|
|
|
{
|
|
|
|
auto occ = Maps::getTileOccupancy(pos);
|
|
|
|
if (!occ || !occ->bits.building)
|
|
|
|
return NULL;
|
|
|
|
|
2013-02-13 04:03:15 -07:00
|
|
|
// Try cache lookup in case it works:
|
|
|
|
auto cached = locationToBuilding.find(pos);
|
|
|
|
if (cached != locationToBuilding.end())
|
2012-12-18 18:28:30 -07:00
|
|
|
{
|
2013-02-13 04:03:15 -07:00
|
|
|
auto building = df::building::find(cached->second);
|
|
|
|
|
|
|
|
if (building && building->z == pos.z &&
|
|
|
|
building->isSettingOccupancy() &&
|
2023-01-27 14:38:21 -07:00
|
|
|
containsTile(building, pos))
|
2013-02-13 04:03:15 -07:00
|
|
|
{
|
|
|
|
return building;
|
|
|
|
}
|
2012-12-18 18:28:30 -07:00
|
|
|
}
|
|
|
|
|
2013-02-13 04:03:15 -07:00
|
|
|
// The authentic method, i.e. how the game generally does this:
|
2012-05-01 09:55:25 -06:00
|
|
|
auto &vec = df::building::get_vector();
|
|
|
|
for (size_t i = 0; i < vec.size(); i++)
|
|
|
|
{
|
|
|
|
auto bld = vec[i];
|
|
|
|
|
|
|
|
if (pos.z != bld->z ||
|
|
|
|
pos.x < bld->x1 || pos.x > bld->x2 ||
|
|
|
|
pos.y < bld->y1 || pos.y > bld->y2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!bld->isSettingOccupancy())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (bld->room.extents && bld->isExtentShaped())
|
|
|
|
{
|
|
|
|
auto etile = getExtentTile(bld->room, pos);
|
|
|
|
if (!etile || !*etile)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bld;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-07 23:28:22 -06:00
|
|
|
static unordered_map<int32_t, df::coord> corner1;
|
|
|
|
static unordered_map<int32_t, df::coord> corner2;
|
2023-01-27 14:38:21 -07:00
|
|
|
static void cacheBuilding(df::building *building) {
|
2021-10-07 23:28:22 -06:00
|
|
|
int32_t id = building->id;
|
|
|
|
df::coord p1(min(building->x1, building->x2), min(building->y1,building->y2), building->z);
|
|
|
|
df::coord p2(max(building->x1, building->x2), max(building->y1,building->y2), building->z);
|
|
|
|
|
2023-01-27 14:38:21 -07:00
|
|
|
corner1[id] = p1;
|
|
|
|
corner2[id] = p2;
|
2012-05-06 01:22:55 -06:00
|
|
|
|
2021-10-07 23:28:22 -06:00
|
|
|
for (int32_t x = p1.x; x <= p2.x; x++) {
|
|
|
|
for (int32_t y = p1.y; y <= p2.y; y++) {
|
|
|
|
df::coord pt(x, y, building->z);
|
2023-01-27 14:38:21 -07:00
|
|
|
if (Buildings::containsTile(building, pt)) {
|
|
|
|
locationToBuilding[pt] = id;
|
2021-10-07 23:28:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-06 01:22:55 -06:00
|
|
|
|
2021-10-07 23:28:22 -06:00
|
|
|
bool Buildings::findCivzonesAt(std::vector<df::building_civzonest*> *pvec,
|
|
|
|
df::coord pos) {
|
|
|
|
pvec->clear();
|
2012-05-06 01:22:55 -06:00
|
|
|
|
2023-05-27 06:37:31 -06:00
|
|
|
for (df::building_civzonest* zone : world->buildings.other.ANY_ZONE)
|
2023-01-18 20:26:00 -07:00
|
|
|
{
|
|
|
|
if (pos.z != zone->z)
|
2012-05-06 01:22:55 -06:00
|
|
|
continue;
|
|
|
|
|
2023-01-18 20:26:00 -07:00
|
|
|
if (zone->room.extents && zone->isExtentShaped())
|
|
|
|
{
|
|
|
|
auto etile = getExtentTile(zone->room, pos);
|
|
|
|
if (!etile || !*etile)
|
2021-10-07 23:28:22 -06:00
|
|
|
continue;
|
2023-01-18 20:26:00 -07:00
|
|
|
|
|
|
|
pvec->push_back(zone);
|
2021-10-07 23:28:22 -06:00
|
|
|
}
|
2012-05-06 01:22:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return !pvec->empty();
|
|
|
|
}
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
df::building *Buildings::allocInstance(df::coord pos, df::building_type type, int subtype, int custom)
|
|
|
|
{
|
|
|
|
if (!building_next_id)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Allocate object
|
|
|
|
const char *classname = ENUM_ATTR(building_type, classname, type);
|
|
|
|
if (!classname)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
auto id = virtual_identity::find(classname);
|
|
|
|
if (!id)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
df::building *bld = (df::building*)id->allocate();
|
|
|
|
if (!bld)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Init base fields
|
|
|
|
bld->x1 = bld->x2 = bld->centerx = pos.x;
|
|
|
|
bld->y1 = bld->y2 = bld->centery = pos.y;
|
|
|
|
bld->z = pos.z;
|
|
|
|
|
2023-01-05 18:11:01 -07:00
|
|
|
bld->race = plotinfo->race_id;
|
2012-04-29 11:07:39 -06:00
|
|
|
|
|
|
|
if (subtype != -1)
|
|
|
|
bld->setSubtype(subtype);
|
|
|
|
if (custom != -1)
|
|
|
|
bld->setCustomType(custom);
|
|
|
|
|
|
|
|
bld->setMaterialAmount(1);
|
|
|
|
|
|
|
|
// Type specific init
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case building_type::Well:
|
2016-01-31 12:50:07 -07:00
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_wellst, bld))
|
2012-04-29 11:07:39 -06:00
|
|
|
obj->bucket_z = bld->z;
|
2016-01-31 12:50:07 -07:00
|
|
|
break;
|
|
|
|
}
|
2020-10-16 15:23:35 -06:00
|
|
|
case building_type::Workshop:
|
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_workshopst, bld))
|
|
|
|
obj->profile.max_general_orders = 5;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-29 11:07:39 -06:00
|
|
|
case building_type::Furnace:
|
2016-01-31 12:50:07 -07:00
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_furnacest, bld))
|
2020-10-16 15:23:35 -06:00
|
|
|
{
|
2012-04-29 11:07:39 -06:00
|
|
|
obj->melt_remainder.resize(df::inorganic_raw::get_vector().size(), 0);
|
2020-10-16 15:23:35 -06:00
|
|
|
obj->profile.max_general_orders = 5;
|
|
|
|
}
|
2016-01-31 12:50:07 -07:00
|
|
|
break;
|
|
|
|
}
|
2022-12-16 12:08:40 -07:00
|
|
|
/* TODO: understand how this changes for v50
|
2012-04-29 11:07:39 -06:00
|
|
|
case building_type::Coffin:
|
2016-01-31 12:50:07 -07:00
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_coffinst, bld))
|
2012-05-01 08:55:30 -06:00
|
|
|
obj->initBurialFlags(); // DF has this copy&pasted
|
2016-01-31 12:50:07 -07:00
|
|
|
break;
|
|
|
|
}
|
2022-12-16 12:08:40 -07:00
|
|
|
*/
|
2012-04-29 11:07:39 -06:00
|
|
|
case building_type::Trap:
|
2016-01-31 12:50:07 -07:00
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_trapst, bld))
|
2012-04-29 11:07:39 -06:00
|
|
|
{
|
|
|
|
if (obj->trap_type == trap_type::PressurePlate)
|
2012-08-31 14:17:08 -06:00
|
|
|
obj->ready_timeout = 500;
|
2012-04-29 11:07:39 -06:00
|
|
|
}
|
2016-01-31 12:50:07 -07:00
|
|
|
break;
|
|
|
|
}
|
2016-01-29 13:50:29 -07:00
|
|
|
case building_type::Floodgate:
|
2016-01-31 12:50:07 -07:00
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_floodgatest, bld))
|
2016-01-29 13:50:29 -07:00
|
|
|
obj->gate_flags.bits.closed = true;
|
2016-01-31 12:50:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case building_type::GrateWall:
|
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_grate_wallst, bld))
|
|
|
|
obj->gate_flags.bits.closed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case building_type::GrateFloor:
|
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_grate_floorst, bld))
|
|
|
|
obj->gate_flags.bits.closed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case building_type::BarsVertical:
|
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_bars_verticalst, bld))
|
|
|
|
obj->gate_flags.bits.closed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case building_type::BarsFloor:
|
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_bars_floorst, bld))
|
|
|
|
obj->gate_flags.bits.closed = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-10-16 15:23:35 -06:00
|
|
|
case building_type::Bridge:
|
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_bridgest, bld))
|
|
|
|
obj->gate_flags.bits.closed = false;
|
|
|
|
break;
|
|
|
|
}
|
2023-03-23 01:25:29 -06:00
|
|
|
case building_type::Weapon:
|
|
|
|
{
|
|
|
|
if (VIRTUAL_CAST_VAR(obj, df::building_weaponst, bld))
|
|
|
|
obj->gate_flags.bits.closed = false;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-29 11:07:39 -06:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bld;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void makeOneDim(df::coord2d &size, df::coord2d ¢er, bool vertical)
|
|
|
|
{
|
|
|
|
if (vertical)
|
|
|
|
size.x = 1;
|
|
|
|
else
|
|
|
|
size.y = 1;
|
|
|
|
center = size/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Buildings::getCorrectSize(df::coord2d &size, df::coord2d ¢er,
|
|
|
|
df::building_type type, int subtype, int custom, int direction)
|
|
|
|
{
|
|
|
|
using namespace df::enums::building_type;
|
|
|
|
|
|
|
|
if (size.x <= 0)
|
|
|
|
size.x = 1;
|
|
|
|
if (size.y <= 0)
|
|
|
|
size.y = 1;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case FarmPlot:
|
|
|
|
case Bridge:
|
|
|
|
case RoadDirt:
|
|
|
|
case RoadPaved:
|
|
|
|
case Stockpile:
|
|
|
|
case Civzone:
|
|
|
|
center = size/2;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case TradeDepot:
|
|
|
|
case Shop:
|
|
|
|
size = df::coord2d(5,5);
|
|
|
|
center = df::coord2d(2,2);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case SiegeEngine:
|
|
|
|
case Windmill:
|
|
|
|
case Wagon:
|
|
|
|
size = df::coord2d(3,3);
|
|
|
|
center = df::coord2d(1,1);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case AxleHorizontal:
|
|
|
|
makeOneDim(size, center, direction);
|
|
|
|
return true;
|
|
|
|
|
2012-05-16 14:19:29 -06:00
|
|
|
case Rollers:
|
|
|
|
makeOneDim(size, center, (direction&1) == 0);
|
|
|
|
return true;
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
case WaterWheel:
|
|
|
|
size = df::coord2d(3,3);
|
|
|
|
makeOneDim(size, center, direction);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Workshop:
|
|
|
|
{
|
|
|
|
using namespace df::enums::workshop_type;
|
|
|
|
|
|
|
|
switch ((df::workshop_type)subtype)
|
|
|
|
{
|
|
|
|
case Quern:
|
|
|
|
case Millstone:
|
|
|
|
case Tool:
|
|
|
|
size = df::coord2d(1,1);
|
|
|
|
center = df::coord2d(0,0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Siege:
|
|
|
|
case Kennels:
|
|
|
|
size = df::coord2d(5,5);
|
|
|
|
center = df::coord2d(2,2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Custom:
|
|
|
|
if (auto def = df::building_def::find(custom))
|
|
|
|
{
|
|
|
|
size = df::coord2d(def->dim_x, def->dim_y);
|
|
|
|
center = df::coord2d(def->workloc_x, def->workloc_y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
size = df::coord2d(3,3);
|
|
|
|
center = df::coord2d(1,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Furnace:
|
|
|
|
{
|
|
|
|
using namespace df::enums::furnace_type;
|
|
|
|
|
|
|
|
switch ((df::furnace_type)subtype)
|
|
|
|
{
|
|
|
|
case Custom:
|
|
|
|
if (auto def = df::building_def::find(custom))
|
|
|
|
{
|
|
|
|
size = df::coord2d(def->dim_x, def->dim_y);
|
|
|
|
center = df::coord2d(def->workloc_x, def->workloc_y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
size = df::coord2d(3,3);
|
|
|
|
center = df::coord2d(1,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ScrewPump:
|
|
|
|
{
|
|
|
|
using namespace df::enums::screw_pump_direction;
|
|
|
|
|
|
|
|
switch ((df::screw_pump_direction)direction)
|
|
|
|
{
|
|
|
|
case FromEast:
|
|
|
|
size = df::coord2d(2,1);
|
|
|
|
center = df::coord2d(1,0);
|
|
|
|
break;
|
|
|
|
case FromSouth:
|
|
|
|
size = df::coord2d(1,2);
|
|
|
|
center = df::coord2d(0,1);
|
|
|
|
break;
|
|
|
|
case FromWest:
|
|
|
|
size = df::coord2d(2,1);
|
|
|
|
center = df::coord2d(0,0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
size = df::coord2d(1,2);
|
|
|
|
center = df::coord2d(0,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
size = df::coord2d(1,1);
|
|
|
|
center = df::coord2d(0,0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 08:02:46 -06:00
|
|
|
static void init_extents(df::building_extents *ext, const df::coord &pos,
|
|
|
|
const df::coord2d &size)
|
|
|
|
{
|
|
|
|
ext->extents = new df::building_extents_type[size.x*size.y];
|
|
|
|
ext->x = pos.x;
|
|
|
|
ext->y = pos.y;
|
|
|
|
ext->width = size.x;
|
|
|
|
ext->height = size.y;
|
|
|
|
|
|
|
|
memset(ext->extents, 1, size.x*size.y);
|
|
|
|
}
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
bool Buildings::checkFreeTiles(df::coord pos, df::coord2d size,
|
2012-05-01 08:55:30 -06:00
|
|
|
df::building_extents *ext,
|
2020-08-15 18:24:12 -06:00
|
|
|
bool create_ext,
|
|
|
|
bool allow_occupied,
|
2020-08-15 21:13:48 -06:00
|
|
|
bool allow_wall)
|
2012-04-29 11:07:39 -06:00
|
|
|
{
|
2012-05-01 08:55:30 -06:00
|
|
|
bool found_any = false;
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
for (int dx = 0; dx < size.x; dx++)
|
|
|
|
{
|
|
|
|
for (int dy = 0; dy < size.y; dy++)
|
|
|
|
{
|
|
|
|
df::coord tile = pos + df::coord(dx,dy,0);
|
2020-11-03 12:56:16 -07:00
|
|
|
df::building_extents_type *etile = NULL;
|
2012-04-29 11:07:39 -06:00
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
// Exclude using extents
|
|
|
|
if (ext && ext->extents)
|
|
|
|
{
|
|
|
|
etile = getExtentTile(*ext, tile);
|
|
|
|
if (!etile || !*etile)
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-29 11:07:39 -06:00
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
// Look up map block
|
2012-04-29 11:07:39 -06:00
|
|
|
df::map_block *block = Maps::getTileBlock(tile);
|
|
|
|
if (!block)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
df::coord2d btile = df::coord2d(tile) & 15;
|
|
|
|
|
|
|
|
bool allowed = true;
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
// Check occupancy and tile type
|
|
|
|
if (!allow_occupied &&
|
|
|
|
block->occupancy[btile.x][btile.y].bits.building)
|
2012-04-29 11:07:39 -06:00
|
|
|
allowed = false;
|
2023-03-24 16:05:17 -06:00
|
|
|
else if (!allow_wall)
|
2012-04-29 11:07:39 -06:00
|
|
|
{
|
2023-03-24 16:05:17 -06:00
|
|
|
auto &tt = block->tiletype[btile.x][btile.y];
|
|
|
|
auto &des = block->designation[btile.x][btile.y];
|
|
|
|
if (!HighPassable(tt) ||
|
|
|
|
des.bits.flow_size > 1 ||
|
|
|
|
(des.bits.flow_size >= 1 && des.bits.liquid_type == df::tile_liquid::Magma))
|
2012-04-29 11:07:39 -06:00
|
|
|
allowed = false;
|
|
|
|
}
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
// Create extents if requested
|
|
|
|
if (allowed)
|
|
|
|
found_any = true;
|
|
|
|
else
|
2012-04-29 11:07:39 -06:00
|
|
|
{
|
|
|
|
if (!ext || !create_ext)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!ext->extents)
|
|
|
|
{
|
2021-06-07 08:02:46 -06:00
|
|
|
init_extents(ext, pos, size);
|
2012-04-29 11:07:39 -06:00
|
|
|
etile = getExtentTile(*ext, tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!etile)
|
|
|
|
return false;
|
|
|
|
|
2020-11-03 12:56:16 -07:00
|
|
|
*etile = df::building_extents_type::None;
|
2012-04-29 11:07:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
return found_any;
|
2012-04-29 11:07:39 -06:00
|
|
|
}
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
std::pair<df::coord,df::coord2d> Buildings::getSize(df::building *bld)
|
2012-04-29 11:07:39 -06:00
|
|
|
{
|
2012-05-01 08:55:30 -06:00
|
|
|
CHECK_NULL_POINTER(bld);
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
df::coord pos(bld->x1,bld->y1,bld->z);
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
return std::pair<df::coord,df::coord2d>(pos, df::coord2d(bld->x2+1,bld->y2+1) - pos);
|
|
|
|
}
|
|
|
|
|
2021-06-07 08:02:46 -06:00
|
|
|
static bool checkBuildingTiles(df::building *bld, bool can_change,
|
|
|
|
bool force_extents = false)
|
2012-05-01 08:55:30 -06:00
|
|
|
{
|
|
|
|
auto psize = Buildings::getSize(bld);
|
|
|
|
|
2021-06-07 08:02:46 -06:00
|
|
|
if (force_extents && !bld->room.extents)
|
|
|
|
{
|
|
|
|
// populate the room structure if it's not set already
|
|
|
|
init_extents(&bld->room, psize.first, psize.second);
|
|
|
|
}
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
return Buildings::checkFreeTiles(psize.first, psize.second, &bld->room,
|
|
|
|
can_change && bld->isExtentShaped(),
|
2020-08-15 18:24:12 -06:00
|
|
|
!bld->isSettingOccupancy(),
|
|
|
|
bld->getType() ==
|
|
|
|
df::building_type::Civzone);
|
2012-05-01 08:55:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int Buildings::countExtentTiles(df::building_extents *ext, int defval)
|
|
|
|
{
|
|
|
|
if (!ext || !ext->extents)
|
|
|
|
return defval;
|
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
for (int i = 0; i < ext->width * ext->height; i++)
|
|
|
|
if (ext->extents[i])
|
|
|
|
cnt++;
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2023-01-27 14:38:21 -07:00
|
|
|
bool Buildings::containsTile(df::building *bld, df::coord2d tile) {
|
2012-05-06 01:22:55 -06:00
|
|
|
CHECK_NULL_POINTER(bld);
|
|
|
|
|
2023-01-27 14:38:21 -07:00
|
|
|
if (!bld->isExtentShaped() || !bld->room.extents) {
|
2013-04-24 09:41:25 -06:00
|
|
|
if (tile.x < bld->x1 || tile.x > bld->x2 || tile.y < bld->y1 || tile.y > bld->y2)
|
2012-05-06 01:22:55 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-27 14:38:21 -07:00
|
|
|
df::building_extents_type *etile = getExtentTile(bld->room, tile);
|
|
|
|
return etile && *etile;
|
2012-05-06 01:22:55 -06:00
|
|
|
}
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
bool Buildings::hasSupport(df::coord pos, df::coord2d size)
|
|
|
|
{
|
|
|
|
for (int dx = -1; dx <= size.x; dx++)
|
|
|
|
{
|
|
|
|
for (int dy = -1; dy <= size.y; dy++)
|
|
|
|
{
|
|
|
|
// skip corners
|
|
|
|
if ((dx < 0 || dx == size.x) && (dy < 0 || dy == size.y))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
df::coord tile = pos + df::coord(dx,dy,0);
|
|
|
|
df::map_block *block = Maps::getTileBlock(tile);
|
|
|
|
if (!block)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
df::coord2d btile = df::coord2d(tile) & 15;
|
|
|
|
if (!isOpenTerrain(block->tiletype[btile.x][btile.y]))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int computeMaterialAmount(df::building *bld)
|
|
|
|
{
|
|
|
|
auto size = Buildings::getSize(bld).second;
|
|
|
|
int cnt = size.x * size.y;
|
|
|
|
|
|
|
|
if (bld->room.extents && bld->isExtentShaped())
|
|
|
|
cnt = Buildings::countExtentTiles(&bld->room, cnt);
|
|
|
|
|
|
|
|
return cnt/4 + 1;
|
2012-04-29 11:07:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Buildings::setSize(df::building *bld, df::coord2d size, int direction)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(bld);
|
|
|
|
CHECK_INVALID_ARGUMENT(bld->id == -1);
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
// Compute correct size and apply it
|
2020-12-16 12:10:47 -07:00
|
|
|
df::coord2d old_size = size;
|
2012-04-29 11:07:39 -06:00
|
|
|
df::coord2d center;
|
|
|
|
getCorrectSize(size, center, bld->getType(), bld->getSubtype(),
|
|
|
|
bld->getCustomType(), direction);
|
|
|
|
|
2020-12-16 12:10:47 -07:00
|
|
|
// Delete old extents if size has changed.
|
|
|
|
if (old_size != size && bld->room.extents)
|
|
|
|
{
|
|
|
|
delete[] bld->room.extents;
|
|
|
|
bld->room.extents = NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
bld->x2 = bld->x1 + size.x - 1;
|
|
|
|
bld->y2 = bld->y1 + size.y - 1;
|
|
|
|
bld->centerx = bld->x1 + center.x;
|
|
|
|
bld->centery = bld->y1 + center.y;
|
|
|
|
|
|
|
|
auto type = bld->getType();
|
|
|
|
|
|
|
|
using namespace df::enums::building_type;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case WaterWheel:
|
|
|
|
{
|
|
|
|
auto obj = (df::building_water_wheelst*)bld;
|
|
|
|
obj->is_vertical = !!direction;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AxleHorizontal:
|
|
|
|
{
|
|
|
|
auto obj = (df::building_axle_horizontalst*)bld;
|
|
|
|
obj->is_vertical = !!direction;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ScrewPump:
|
|
|
|
{
|
|
|
|
auto obj = (df::building_screw_pumpst*)bld;
|
|
|
|
obj->direction = (df::screw_pump_direction)direction;
|
|
|
|
break;
|
|
|
|
}
|
2012-05-16 14:19:29 -06:00
|
|
|
case Rollers:
|
|
|
|
{
|
|
|
|
auto obj = (df::building_rollersst*)bld;
|
|
|
|
obj->direction = (df::screw_pump_direction)direction;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-29 11:07:39 -06:00
|
|
|
case Bridge:
|
|
|
|
{
|
|
|
|
auto obj = (df::building_bridgest*)bld;
|
2012-05-01 08:55:30 -06:00
|
|
|
auto psize = getSize(bld);
|
|
|
|
obj->gate_flags.bits.has_support = hasSupport(psize.first, psize.second);
|
2012-04-29 11:07:39 -06:00
|
|
|
obj->direction = (df::building_bridgest::T_direction)direction;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
bool ok = checkBuildingTiles(bld, true);
|
2012-04-29 11:07:39 -06:00
|
|
|
|
2012-05-05 10:46:28 -06:00
|
|
|
if (type != Construction)
|
2012-05-01 08:55:30 -06:00
|
|
|
bld->setMaterialAmount(computeMaterialAmount(bld));
|
2012-04-29 11:07:39 -06:00
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
static void markBuildingTiles(df::building *bld, bool remove)
|
2012-04-29 11:07:39 -06:00
|
|
|
{
|
2012-05-01 08:55:30 -06:00
|
|
|
bool use_extents = bld->room.extents && bld->isExtentShaped();
|
|
|
|
bool stockpile = (bld->getType() == building_type::Stockpile);
|
|
|
|
bool complete = (bld->getBuildStage() >= bld->getMaxBuildStage());
|
|
|
|
|
|
|
|
if (remove)
|
|
|
|
stockpile = complete = false;
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
for (int tx = bld->x1; tx <= bld->x2; tx++)
|
|
|
|
{
|
|
|
|
for (int ty = bld->y1; ty <= bld->y2; ty++)
|
|
|
|
{
|
|
|
|
df::coord tile(tx,ty,bld->z);
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
if (use_extents)
|
|
|
|
{
|
2020-11-03 12:56:16 -07:00
|
|
|
df::building_extents_type *etile = getExtentTile(bld->room, tile);
|
2012-05-01 08:55:30 -06:00
|
|
|
if (!etile || !*etile)
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-29 11:07:39 -06:00
|
|
|
|
|
|
|
df::map_block *block = Maps::getTileBlock(tile);
|
|
|
|
df::coord2d btile = df::coord2d(tile) & 15;
|
|
|
|
|
|
|
|
auto &des = block->designation[btile.x][btile.y];
|
2012-05-01 08:55:30 -06:00
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
des.bits.pile = stockpile;
|
2012-05-01 08:55:30 -06:00
|
|
|
if (!remove)
|
|
|
|
des.bits.dig = tile_dig_designation::No;
|
|
|
|
|
|
|
|
if (complete)
|
|
|
|
bld->updateOccupancy(tx, ty);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto &occ = block->occupancy[btile.x][btile.y];
|
|
|
|
|
|
|
|
if (remove)
|
|
|
|
occ.bits.building = tile_building_occ::None;
|
|
|
|
else
|
|
|
|
occ.bits.building = tile_building_occ::Planned;
|
|
|
|
}
|
2012-04-29 11:07:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linkRooms(df::building *bld)
|
|
|
|
{
|
2022-12-16 12:08:40 -07:00
|
|
|
/* TODO: understand how this changes for v50
|
2012-10-23 11:14:21 -06:00
|
|
|
auto &vec = world->buildings.other[buildings_other_id::IN_PLAY];
|
2012-04-29 11:07:39 -06:00
|
|
|
|
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vec.size(); i++)
|
|
|
|
{
|
|
|
|
auto room = vec[i];
|
2021-01-24 09:57:56 -07:00
|
|
|
if (!room->is_room || room->z != bld->z || room == bld)
|
2012-04-29 11:07:39 -06:00
|
|
|
continue;
|
|
|
|
|
2020-11-03 12:56:16 -07:00
|
|
|
df::building_extents_type *pext = getExtentTile(room->room, df::coord2d(bld->x1, bld->y1));
|
2021-01-24 09:57:56 -07:00
|
|
|
if (!pext || !*pext)
|
2012-04-29 11:07:39 -06:00
|
|
|
continue;
|
|
|
|
|
|
|
|
changed = true;
|
|
|
|
room->children.push_back(bld);
|
|
|
|
bld->parents.push_back(room);
|
|
|
|
|
|
|
|
// TODO: the game updates room rent here if economy is enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed)
|
2023-01-05 18:11:01 -07:00
|
|
|
df::global::plotinfo->equipment.update.bits.buildings = true;
|
2022-12-16 12:08:40 -07:00
|
|
|
*/
|
2012-04-29 11:07:39 -06:00
|
|
|
}
|
|
|
|
|
2012-05-06 09:09:11 -06:00
|
|
|
static void unlinkRooms(df::building *bld)
|
|
|
|
{
|
2022-12-16 12:08:40 -07:00
|
|
|
/* TODO: understand how this changes for v50
|
2012-05-06 09:09:11 -06:00
|
|
|
for (size_t i = 0; i < bld->parents.size(); i++)
|
|
|
|
{
|
|
|
|
auto parent = bld->parents[i];
|
|
|
|
int idx = linear_index(parent->children, bld);
|
|
|
|
vector_erase_at(parent->children, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
bld->parents.clear();
|
2022-12-16 12:08:40 -07:00
|
|
|
*/
|
2012-05-06 09:09:11 -06:00
|
|
|
}
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
static void linkBuilding(df::building *bld)
|
|
|
|
{
|
|
|
|
bld->id = (*building_next_id)++;
|
|
|
|
|
|
|
|
world->buildings.all.push_back(bld);
|
|
|
|
bld->categorize(true);
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
if (bld->isSettingOccupancy())
|
|
|
|
markBuildingTiles(bld, false);
|
2012-04-29 11:07:39 -06:00
|
|
|
|
|
|
|
linkRooms(bld);
|
|
|
|
|
2012-05-12 10:12:09 -06:00
|
|
|
Job::checkBuildingsNow();
|
2012-04-29 11:07:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void createDesign(df::building *bld, bool rough)
|
|
|
|
{
|
|
|
|
auto job = bld->jobs[0];
|
|
|
|
|
|
|
|
job->mat_type = bld->mat_type;
|
|
|
|
job->mat_index = bld->mat_index;
|
|
|
|
|
|
|
|
if (bld->needsDesign())
|
|
|
|
{
|
|
|
|
auto act = (df::building_actual*)bld;
|
|
|
|
act->design = new df::building_design();
|
|
|
|
|
|
|
|
act->design->flags.bits.rough = rough;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:09:11 -06:00
|
|
|
static int getMaxStockpileId()
|
|
|
|
{
|
|
|
|
int max_id = 0;
|
2023-04-24 15:28:47 -06:00
|
|
|
for (auto bld : world->buildings.other.STOCKPILE)
|
|
|
|
max_id = std::max(max_id, bld->stockpile_number);
|
2012-05-06 09:09:11 -06:00
|
|
|
return max_id;
|
|
|
|
}
|
|
|
|
|
2016-01-18 13:41:57 -07:00
|
|
|
static int getMaxCivzoneId()
|
|
|
|
{
|
|
|
|
int max_id = 0;
|
2023-04-24 15:28:47 -06:00
|
|
|
for (auto bld : world->buildings.other.ANY_ZONE)
|
|
|
|
max_id = std::max(max_id, bld->zone_num);
|
2016-01-18 13:41:57 -07:00
|
|
|
return max_id;
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:09:11 -06:00
|
|
|
bool Buildings::constructAbstract(df::building *bld)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(bld);
|
|
|
|
CHECK_INVALID_ARGUMENT(bld->id == -1);
|
|
|
|
CHECK_INVALID_ARGUMENT(!bld->isActual());
|
|
|
|
|
2021-06-07 08:02:46 -06:00
|
|
|
if (!checkBuildingTiles(bld, true, true))
|
2012-05-06 09:09:11 -06:00
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (bld->getType())
|
|
|
|
{
|
|
|
|
case building_type::Stockpile:
|
2022-12-16 12:08:40 -07:00
|
|
|
|
2012-05-06 09:09:11 -06:00
|
|
|
if (auto stock = strict_virtual_cast<df::building_stockpilest>(bld))
|
|
|
|
stock->stockpile_number = getMaxStockpileId() + 1;
|
|
|
|
break;
|
|
|
|
|
2016-01-18 13:41:57 -07:00
|
|
|
case building_type::Civzone:
|
|
|
|
if (auto zone = strict_virtual_cast<df::building_civzonest>(bld))
|
2023-01-18 20:26:00 -07:00
|
|
|
{
|
2016-01-18 13:41:57 -07:00
|
|
|
zone->zone_num = getMaxCivzoneId() + 1;
|
2023-01-18 20:26:00 -07:00
|
|
|
|
|
|
|
add_zone_to_all_buildings(zone);
|
|
|
|
}
|
2016-01-18 13:41:57 -07:00
|
|
|
break;
|
|
|
|
|
2012-05-06 09:09:11 -06:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
linkBuilding(bld);
|
|
|
|
|
|
|
|
if (!bld->flags.bits.exists)
|
|
|
|
{
|
|
|
|
bld->flags.bits.exists = true;
|
|
|
|
bld->initFarmSeasons();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
static bool linkForConstruct(df::job* &job, df::building *bld)
|
|
|
|
{
|
|
|
|
if (!checkBuildingTiles(bld, false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto ref = df::allocate<df::general_ref_building_holderst>();
|
|
|
|
if (!ref)
|
|
|
|
{
|
|
|
|
Core::printerr("Could not allocate general_ref_building_holderst\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
linkBuilding(bld);
|
|
|
|
|
|
|
|
ref->building_id = bld->id;
|
|
|
|
|
|
|
|
job = new df::job();
|
|
|
|
job->job_type = df::job_type::ConstructBuilding;
|
|
|
|
job->pos = df::coord(bld->centerx, bld->centery, bld->z);
|
2012-11-12 07:27:58 -07:00
|
|
|
job->general_refs.push_back(ref);
|
2012-04-29 11:07:39 -06:00
|
|
|
|
|
|
|
bld->jobs.push_back(job);
|
|
|
|
|
|
|
|
Job::linkIntoWorld(job);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-06 00:09:39 -06:00
|
|
|
static bool needsItems(df::building *bld)
|
|
|
|
{
|
|
|
|
if (!bld->isActual())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (bld->getType())
|
|
|
|
{
|
|
|
|
case building_type::FarmPlot:
|
|
|
|
case building_type::RoadDirt:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
bool Buildings::constructWithItems(df::building *bld, std::vector<df::item*> items)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(bld);
|
|
|
|
CHECK_INVALID_ARGUMENT(bld->id == -1);
|
|
|
|
CHECK_INVALID_ARGUMENT(bld->isActual());
|
2012-05-06 00:09:39 -06:00
|
|
|
CHECK_INVALID_ARGUMENT(!items.empty() == needsItems(bld));
|
2012-04-29 11:07:39 -06:00
|
|
|
|
|
|
|
for (size_t i = 0; i < items.size(); i++)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(items[i]);
|
|
|
|
|
|
|
|
if (items[i]->flags.bits.in_job)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
df::job *job = NULL;
|
|
|
|
if (!linkForConstruct(job, bld))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool rough = false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < items.size(); i++)
|
|
|
|
{
|
|
|
|
Job::attachJobItem(job, items[i], df::job_item_ref::Hauled);
|
|
|
|
|
|
|
|
if (items[i]->getType() == item_type::BOULDER)
|
|
|
|
rough = true;
|
|
|
|
if (bld->mat_type == -1)
|
|
|
|
bld->mat_type = items[i]->getMaterial();
|
|
|
|
if (bld->mat_index == -1)
|
|
|
|
bld->mat_index = items[i]->getMaterialIndex();
|
|
|
|
}
|
|
|
|
|
2023-01-18 20:26:00 -07:00
|
|
|
add_building_to_all_zones(bld);
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
createDesign(bld, rough);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Buildings::constructWithFilters(df::building *bld, std::vector<df::job_item*> items)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(bld);
|
|
|
|
CHECK_INVALID_ARGUMENT(bld->id == -1);
|
|
|
|
CHECK_INVALID_ARGUMENT(bld->isActual());
|
2012-05-06 00:09:39 -06:00
|
|
|
CHECK_INVALID_ARGUMENT(!items.empty() == needsItems(bld));
|
2012-04-29 11:07:39 -06:00
|
|
|
|
|
|
|
for (size_t i = 0; i < items.size(); i++)
|
|
|
|
CHECK_NULL_POINTER(items[i]);
|
|
|
|
|
|
|
|
df::job *job = NULL;
|
|
|
|
if (!linkForConstruct(job, bld))
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < items.size(); i++)
|
|
|
|
delete items[i];
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rough = false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < items.size(); i++)
|
|
|
|
{
|
2012-05-01 08:55:30 -06:00
|
|
|
if (items[i]->quantity < 0)
|
|
|
|
items[i]->quantity = computeMaterialAmount(bld);
|
|
|
|
|
2012-05-16 14:19:29 -06:00
|
|
|
/* The game picks up explicitly listed items in reverse
|
|
|
|
* order, but processes filters straight. This reverses
|
|
|
|
* the order of filters so as to produce the same final
|
2023-01-05 18:46:30 -07:00
|
|
|
* contained_items ordering as the normal ui way. */
|
2012-05-16 14:19:29 -06:00
|
|
|
vector_insert_at(job->job_items, 0, items[i]);
|
2012-04-29 11:07:39 -06:00
|
|
|
|
|
|
|
if (items[i]->item_type == item_type::BOULDER)
|
|
|
|
rough = true;
|
|
|
|
if (bld->mat_type == -1)
|
|
|
|
bld->mat_type = items[i]->mat_type;
|
|
|
|
if (bld->mat_index == -1)
|
|
|
|
bld->mat_index = items[i]->mat_index;
|
|
|
|
}
|
|
|
|
|
2012-05-17 10:38:27 -06:00
|
|
|
buildings_do_onupdate = true;
|
|
|
|
|
2023-01-18 20:26:00 -07:00
|
|
|
add_building_to_all_zones(bld);
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
createDesign(bld, rough);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-22 07:53:56 -07:00
|
|
|
static void delete_civzone_squad_links(df::building_civzonest* zone)
|
2023-01-20 11:02:55 -07:00
|
|
|
{
|
|
|
|
for (df::building_civzonest::T_squad_room_info* room_info : zone->squad_room_info)
|
|
|
|
{
|
|
|
|
int32_t squad_id = room_info->squad_id;
|
|
|
|
|
|
|
|
df::squad* squad = df::squad::find(squad_id);
|
|
|
|
|
|
|
|
//if this is null, something has gone just *terribly* wrong
|
|
|
|
if (squad)
|
|
|
|
{
|
2023-01-22 06:56:33 -07:00
|
|
|
for (int i=(int)squad->rooms.size() - 1; i >= 0; i--)
|
2023-01-20 11:02:55 -07:00
|
|
|
{
|
2023-01-22 07:53:56 -07:00
|
|
|
if (squad->rooms[i]->building_id == zone->id)
|
2023-01-20 11:02:55 -07:00
|
|
|
{
|
|
|
|
auto room = squad->rooms[i];
|
|
|
|
squad->rooms.erase(squad->rooms.begin() + i);
|
|
|
|
delete room;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete room_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
zone->squad_room_info.clear();
|
|
|
|
}
|
|
|
|
|
2023-01-22 07:53:56 -07:00
|
|
|
//unit owned_building pointers are known-bad as of 50.05 and dangle on zone delete
|
|
|
|
//do not use anything that touches anything other than the pointer value
|
|
|
|
//this means also that if dwarf fortress reuses a memory allocation, we will end up with duplicates
|
|
|
|
//this vector is also not sorted by id
|
2023-01-22 15:56:51 -07:00
|
|
|
//it also turns out that multiple units eg (solely?) spouses can point to one room
|
2023-01-22 07:53:56 -07:00
|
|
|
static void delete_assigned_unit_links(df::building_civzonest* zone)
|
|
|
|
{
|
2023-01-22 15:56:51 -07:00
|
|
|
//not clear if this is always true
|
|
|
|
/*if (zone->assigned_unit_id == -1)
|
|
|
|
return;*/
|
2023-01-22 07:53:56 -07:00
|
|
|
|
2023-01-22 15:56:51 -07:00
|
|
|
for (df::unit* unit : world->units.active)
|
2023-01-22 07:53:56 -07:00
|
|
|
{
|
2023-01-22 15:56:51 -07:00
|
|
|
for (int i=(int)unit->owned_buildings.size() - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (unit->owned_buildings[i] == zone)
|
|
|
|
unit->owned_buildings.erase(unit->owned_buildings.begin() + i);
|
|
|
|
}
|
2023-01-22 07:53:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void on_civzone_delete(df::building_civzonest* civzone)
|
|
|
|
{
|
|
|
|
remove_zone_from_all_buildings(civzone);
|
|
|
|
delete_civzone_squad_links(civzone);
|
|
|
|
delete_assigned_unit_links(civzone);
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:09:11 -06:00
|
|
|
bool Buildings::deconstruct(df::building *bld)
|
|
|
|
{
|
2023-01-05 18:11:01 -07:00
|
|
|
using df::global::plotinfo;
|
2012-05-06 09:09:11 -06:00
|
|
|
using df::global::world;
|
|
|
|
using df::global::ui_look_list;
|
|
|
|
|
|
|
|
CHECK_NULL_POINTER(bld);
|
|
|
|
|
|
|
|
if (bld->isActual() && bld->getBuildStage() > 0)
|
|
|
|
{
|
|
|
|
bld->queueDestroy();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Immediate destruction code path.
|
|
|
|
Should only happen for abstract and unconstructed buildings.*/
|
|
|
|
|
|
|
|
if (bld->isSettingOccupancy())
|
|
|
|
{
|
|
|
|
markBuildingTiles(bld, true);
|
|
|
|
bld->cleanupMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
bld->removeUses(false, false);
|
|
|
|
// Assume: no parties.
|
|
|
|
unlinkRooms(bld);
|
|
|
|
// Assume: not unit destroy target
|
2023-01-08 12:33:14 -07:00
|
|
|
int id = bld->id;
|
|
|
|
vector_erase_at(plotinfo->tax_collection.rooms, linear_index(plotinfo->tax_collection.rooms, id));
|
2012-05-06 09:09:11 -06:00
|
|
|
// Assume: not used in punishment
|
|
|
|
// Assume: not used in non-own jobs
|
|
|
|
// Assume: does not affect pathfinding
|
|
|
|
bld->deconstructItems(false, false);
|
|
|
|
// Don't clear arrows.
|
|
|
|
|
|
|
|
bld->uncategorize();
|
2023-01-18 20:26:00 -07:00
|
|
|
|
|
|
|
remove_building_from_all_zones(bld);
|
|
|
|
|
2023-01-22 07:53:56 -07:00
|
|
|
if (bld->getType() == df::building_type::Civzone)
|
|
|
|
{
|
|
|
|
auto zone = strict_virtual_cast<df::building_civzonest>(bld);
|
|
|
|
|
|
|
|
if (zone)
|
|
|
|
on_civzone_delete(zone);
|
|
|
|
}
|
2023-01-20 11:02:55 -07:00
|
|
|
|
2012-05-06 09:09:11 -06:00
|
|
|
delete bld;
|
|
|
|
|
|
|
|
if (world->selected_building == bld)
|
|
|
|
{
|
|
|
|
world->selected_building = NULL;
|
|
|
|
world->update_selected_building = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = ui_look_list->items.size()-1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
auto item = ui_look_list->items[i];
|
|
|
|
if (item->type == df::ui_look_list::T_items::Building &&
|
2023-01-08 12:33:14 -07:00
|
|
|
item->data.building.bld_id == id)
|
2012-05-06 09:09:11 -06:00
|
|
|
{
|
|
|
|
vector_erase_at(ui_look_list->items, i);
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-12 10:12:09 -06:00
|
|
|
Job::checkBuildingsNow();
|
|
|
|
Job::checkDesignationsNow();
|
2012-05-06 09:09:11 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-05-17 10:38:27 -06:00
|
|
|
|
2017-06-25 13:21:45 -06:00
|
|
|
bool Buildings::markedForRemoval(df::building *bld)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(bld);
|
|
|
|
for (df::job *job : bld->jobs)
|
|
|
|
{
|
|
|
|
if (job && job->job_type == df::job_type::DestroyBuilding)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:49:24 -07:00
|
|
|
void Buildings::notifyCivzoneModified(df::building* bld)
|
|
|
|
{
|
|
|
|
if (bld->getType() != building_type::Civzone)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//remove zone here needs to be the slow method
|
|
|
|
remove_zone_from_all_buildings(bld);
|
|
|
|
add_zone_to_all_buildings(bld);
|
|
|
|
}
|
|
|
|
|
2013-01-01 20:22:31 -07:00
|
|
|
void Buildings::clearBuildings(color_ostream& out) {
|
|
|
|
corner1.clear();
|
|
|
|
corner2.clear();
|
|
|
|
locationToBuilding.clear();
|
|
|
|
}
|
|
|
|
|
2021-10-07 23:28:22 -06:00
|
|
|
void Buildings::updateBuildings(color_ostream&, void* ptr)
|
2013-02-14 02:07:27 -07:00
|
|
|
{
|
2019-10-05 18:26:31 -06:00
|
|
|
int32_t id = (int32_t)(intptr_t)ptr;
|
2017-02-17 20:53:48 -07:00
|
|
|
auto building = df::building::find(id);
|
|
|
|
|
|
|
|
if (building)
|
|
|
|
{
|
2022-09-25 17:04:36 -06:00
|
|
|
bool is_civzone = !building->isSettingOccupancy();
|
|
|
|
if (!corner1.count(id) && !is_civzone)
|
2023-01-27 14:38:21 -07:00
|
|
|
cacheBuilding(building);
|
2017-02-17 20:53:48 -07:00
|
|
|
}
|
|
|
|
else if (corner1.count(id))
|
|
|
|
{
|
2021-10-07 23:28:22 -06:00
|
|
|
// existing building: destroy it
|
|
|
|
// note that civzones are lazy-destroyed in findCivzonesAt() and are
|
|
|
|
// not handled here
|
2017-02-17 20:53:48 -07:00
|
|
|
df::coord p1 = corner1[id];
|
|
|
|
df::coord p2 = corner2[id];
|
|
|
|
|
|
|
|
for ( int32_t x = p1.x; x <= p2.x; x++ ) {
|
|
|
|
for ( int32_t y = p1.y; y <= p2.y; y++ ) {
|
|
|
|
df::coord pt(x,y,p1.z);
|
|
|
|
|
|
|
|
auto cur = locationToBuilding.find(pt);
|
|
|
|
if (cur != locationToBuilding.end() && cur->second == id)
|
|
|
|
locationToBuilding.erase(cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
corner1.erase(id);
|
|
|
|
corner2.erase(id);
|
|
|
|
}
|
2012-12-18 18:28:30 -07:00
|
|
|
}
|
2014-09-07 14:10:27 -06:00
|
|
|
|
2018-05-12 15:00:21 -06:00
|
|
|
static std::map<df::building_type, std::vector<std::string>> room_quality_names = {
|
|
|
|
{df::building_type::Bed, {
|
|
|
|
"Meager Quarters",
|
|
|
|
"Modest Quarters",
|
|
|
|
"Quarters",
|
|
|
|
"Decent Quarters",
|
|
|
|
"Fine Quarters",
|
|
|
|
"Great Bedroom",
|
|
|
|
"Grand Bedroom",
|
|
|
|
"Royal Bedroom"}},
|
|
|
|
{df::building_type::Table, {
|
|
|
|
"Meager Dining Room",
|
|
|
|
"Modest Dining Room",
|
|
|
|
"Dining Room",
|
|
|
|
"Decent Dining Room",
|
|
|
|
"Fine Dining Room",
|
|
|
|
"Great Dining Room",
|
|
|
|
"Grand Dining Room",
|
|
|
|
"Royal Dining Room"}},
|
|
|
|
{df::building_type::Chair, {
|
|
|
|
"Meager Office",
|
|
|
|
"Modest Office",
|
|
|
|
"Office",
|
|
|
|
"Decent Office",
|
|
|
|
"Splendid Office",
|
|
|
|
"Throne Room",
|
|
|
|
"Opulent Throne Room",
|
|
|
|
"Royal Throne Room"}},
|
|
|
|
{df::building_type::Coffin, {
|
|
|
|
"Grave",
|
|
|
|
"Servant's Burial Chamber",
|
|
|
|
"Burial Chamber",
|
|
|
|
"Tomb",
|
|
|
|
"Fine Tomb",
|
|
|
|
"Mausoleum",
|
|
|
|
"Grand Mausoleum",
|
|
|
|
"Royal Mausoleum"}}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string Buildings::getRoomDescription(df::building *building, df::unit *unit)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(building);
|
|
|
|
// unit can be null
|
|
|
|
|
2022-12-16 12:08:40 -07:00
|
|
|
/* TODO: understand how this changes for v50
|
2018-05-12 15:00:21 -06:00
|
|
|
if (!building->is_room)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
auto btype = building->getType();
|
|
|
|
if (room_quality_names.find(btype) == room_quality_names.end())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
int32_t value = building->getRoomValue(unit);
|
|
|
|
auto level = ENUM_FIRST_ITEM(dfhack_room_quality_level);
|
|
|
|
for (auto i_level = level; is_valid_enum_item(i_level); i_level = next_enum_item(i_level, false))
|
|
|
|
{
|
|
|
|
if (value >= ENUM_ATTR(dfhack_room_quality_level, min_value, i_level))
|
|
|
|
{
|
|
|
|
level = i_level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vector_get(room_quality_names[btype], size_t(level), string(""));
|
2022-12-16 12:08:40 -07:00
|
|
|
*/ return "";
|
2018-05-12 15:00:21 -06:00
|
|
|
}
|
|
|
|
|
2014-09-07 14:10:27 -06:00
|
|
|
void Buildings::getStockpileContents(df::building_stockpilest *stockpile, std::vector<df::item*> *items)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(stockpile);
|
|
|
|
|
|
|
|
items->clear();
|
|
|
|
|
|
|
|
Buildings::StockpileIterator stored;
|
|
|
|
for (stored.begin(stockpile); !stored.done(); ++stored) {
|
|
|
|
df::item *item = *stored;
|
|
|
|
items->push_back(item);
|
|
|
|
}
|
|
|
|
}
|
2015-01-29 11:54:33 -07:00
|
|
|
|
|
|
|
bool Buildings::isActivityZone(df::building * building)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(building);
|
|
|
|
return building->getType() == building_type::Civzone
|
2022-12-16 12:08:40 -07:00
|
|
|
/* TODO: understand how this changes for v50
|
2015-01-29 11:54:33 -07:00
|
|
|
&& building->getSubtype() == (short)civzone_type::ActivityZone;
|
2022-12-16 12:08:40 -07:00
|
|
|
*/ ;
|
2015-01-29 11:54:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Buildings::isPenPasture(df::building * building)
|
|
|
|
{
|
|
|
|
if (!isActivityZone(building))
|
|
|
|
return false;
|
|
|
|
|
2023-02-06 13:37:27 -07:00
|
|
|
return ((df::building_civzonest*)building)->type == civzone_type::Pen;
|
2015-01-29 11:54:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Buildings::isPitPond(df::building * building)
|
|
|
|
{
|
|
|
|
if (!isActivityZone(building))
|
|
|
|
return false;
|
2023-02-06 13:37:27 -07:00
|
|
|
|
|
|
|
return ((df::building_civzonest*)building)->type == civzone_type::Pond;
|
2015-01-29 11:54:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Buildings::isActive(df::building * building)
|
|
|
|
{
|
|
|
|
if (!isActivityZone(building))
|
|
|
|
return false;
|
2023-02-06 17:09:47 -07:00
|
|
|
// 8 is the value obtained by reverse engineering
|
2023-02-06 13:37:27 -07:00
|
|
|
return ((df::building_civzonest*)building)->is_active == 8;
|
2015-01-29 11:54:33 -07:00
|
|
|
}
|
|
|
|
|
2016-06-05 07:09:07 -06:00
|
|
|
bool Buildings::isAnimalTraining(df::building * building)
|
|
|
|
{
|
|
|
|
if (!isActivityZone(building))
|
|
|
|
return false;
|
2023-02-06 13:37:27 -07:00
|
|
|
|
|
|
|
return ((df::building_civzonest*)building)->type == civzone_type::AnimalTraining;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
2016-08-10 21:50:00 -06:00
|
|
|
|
2015-01-29 11:54:33 -07:00
|
|
|
// returns building of pen/pit at cursor position (NULL if nothing found)
|
2015-01-29 13:02:54 -07:00
|
|
|
df::building* Buildings::findPenPitAt(df::coord coord)
|
2015-01-29 11:54:33 -07:00
|
|
|
{
|
|
|
|
vector<df::building_civzonest*> zones;
|
|
|
|
Buildings::findCivzonesAt(&zones, coord);
|
|
|
|
for (auto zone = zones.begin(); zone != zones.end(); ++zone)
|
|
|
|
{
|
|
|
|
if (isPenPasture(*zone) || isPitPond(*zone))
|
|
|
|
return (*zone);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-08-13 19:44:01 -06:00
|
|
|
|
|
|
|
using Buildings::StockpileIterator;
|
|
|
|
StockpileIterator& StockpileIterator::operator++() {
|
|
|
|
while (stockpile) {
|
|
|
|
if (block) {
|
|
|
|
// Check the next item in the current block.
|
|
|
|
++current;
|
2023-04-03 11:27:32 -06:00
|
|
|
}
|
|
|
|
else if (stockpile->x2 < 0 || stockpile->y2 < 0 || stockpile->z < 0 || stockpile->x1 > world->map.x_count - 1 || stockpile->y1 > world->map.y_count - 1 || stockpile->z > world->map.z_count - 1) {
|
|
|
|
// if the stockpile bounds exist outside of valid map plane then no items can be in the stockpile
|
|
|
|
block = NULL;
|
|
|
|
item = NULL;
|
|
|
|
return *this;
|
2016-08-13 19:44:01 -06:00
|
|
|
} else {
|
|
|
|
// Start with the top-left block covering the stockpile.
|
Update Buildings.cpp
Fixes #3159.
Valid map coordinates from (0, 0, 0) to (world->map.x_count - 1, world->map.y_count - 1, world->map.z_count - 1).
Stockpile coords (x1, y1, z) to (x2, y2, z) may lie outside of this region.
Use min of (0, 0) and max of (world->map.x_count - 1, world->map.y_count - 1) when iterating the block.
2023-04-03 11:08:43 -06:00
|
|
|
block = Maps::getTileBlock(std::min(std::max(stockpile->x1, 0), world->map.x_count-1), std::min(std::max(stockpile->y1, 0), world->map.y_count-1), stockpile->z);
|
2016-08-13 19:44:01 -06:00
|
|
|
current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (current >= block->items.size()) {
|
|
|
|
// Out of items in this block; find the next block to search.
|
2023-04-04 19:49:14 -06:00
|
|
|
if (block->map_pos.x + 16 <= std::min(stockpile->x2, world->map.x_count-1)) {
|
|
|
|
block = Maps::getTileBlock(block->map_pos.x + 16, block->map_pos.y, stockpile->z);
|
2016-08-13 19:44:01 -06:00
|
|
|
current = 0;
|
2023-04-04 19:49:14 -06:00
|
|
|
} else if (block->map_pos.y + 16 <= std::min(stockpile->y2, world->map.y_count-1)) {
|
|
|
|
block = Maps::getTileBlock(std::max(stockpile->x1, 0), block->map_pos.y + 16, stockpile->z);
|
2016-08-13 19:44:01 -06:00
|
|
|
current = 0;
|
|
|
|
} else {
|
|
|
|
// All items in all blocks have been checked.
|
|
|
|
block = NULL;
|
|
|
|
item = NULL;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current item isn't properly stored, move on to the next.
|
|
|
|
item = df::item::find(block->items[current]);
|
|
|
|
if (!item->flags.bits.on_ground) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-27 14:38:21 -07:00
|
|
|
if (!Buildings::containsTile(stockpile, item->pos)) {
|
2016-08-13 19:44:01 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore empty bins, barrels, and wheelbarrows assigned here.
|
|
|
|
if (item->isAssignedToThisStockpile(stockpile->id)) {
|
|
|
|
auto ref = Items::getGeneralRef(item, df::general_ref_type::CONTAINS_ITEM);
|
|
|
|
if (!ref) continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a valid item; yield it.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-06-25 13:21:45 -06:00
|
|
|
bool Buildings::getCageOccupants(df::building_cagest *cage, vector<df::unit*> &units)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(cage);
|
|
|
|
if (!world)
|
|
|
|
return false;
|
|
|
|
if (cage->contained_items.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto *cage_item = virtual_cast<df::item_cagest>(cage->contained_items[0]->item);
|
|
|
|
if (!cage_item)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
units.clear();
|
|
|
|
for (df::general_ref *gref : cage_item->general_refs)
|
|
|
|
{
|
|
|
|
auto ref = virtual_cast<df::general_ref_contains_unitst>(gref);
|
|
|
|
if (ref)
|
|
|
|
{
|
|
|
|
df::unit *unit = df::unit::find(ref->unit_id);
|
|
|
|
if (unit)
|
|
|
|
units.push_back(unit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-10-16 13:05:33 -06:00
|
|
|
|
2023-11-15 17:26:29 -07:00
|
|
|
void Buildings::completeBuild(df::building* bld)
|
2023-10-16 13:05:33 -06:00
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(bld);
|
|
|
|
|
|
|
|
auto fp = df::global::buildingst_completebuild;
|
|
|
|
CHECK_NULL_POINTER(fp);
|
|
|
|
|
2023-11-15 17:26:29 -07:00
|
|
|
// whether to add to the IN_PLAY vector, which we always want to do
|
|
|
|
char in_play = 1;
|
|
|
|
|
2023-10-26 10:48:49 -06:00
|
|
|
using FT = std::function<void(df::building* bld, char)>;
|
2023-10-16 13:05:33 -06:00
|
|
|
auto f = reinterpret_cast<FT*>(fp);
|
|
|
|
(*f)(bld, in_play);
|
|
|
|
}
|