2017-05-04 19:47:12 -06:00
|
|
|
#include "DataDefs.h"
|
|
|
|
#include "Error.h"
|
|
|
|
|
|
|
|
#include "modules/Designations.h"
|
|
|
|
#include "modules/Job.h"
|
|
|
|
#include "modules/Maps.h"
|
|
|
|
|
|
|
|
#include "df/job.h"
|
|
|
|
#include "df/map_block.h"
|
|
|
|
#include "df/plant.h"
|
2017-05-05 19:25:25 -06:00
|
|
|
#include "df/plant_tree_info.h"
|
|
|
|
#include "df/plant_tree_tile.h"
|
2017-05-04 19:47:12 -06:00
|
|
|
#include "df/tile_dig_designation.h"
|
|
|
|
#include "df/world.h"
|
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
|
|
|
|
using df::global::world;
|
|
|
|
|
|
|
|
static df::map_block *getPlantBlock(const df::plant *plant)
|
|
|
|
{
|
|
|
|
if (!world)
|
|
|
|
return nullptr;
|
2017-05-05 19:25:25 -06:00
|
|
|
return Maps::getTileBlock(Designations::getPlantDesignationTile(plant));
|
|
|
|
}
|
|
|
|
|
|
|
|
df::coord Designations::getPlantDesignationTile(const df::plant *plant)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(plant);
|
|
|
|
|
|
|
|
if (!plant->tree_info)
|
|
|
|
return plant->pos;
|
|
|
|
|
|
|
|
int dimx = plant->tree_info->dim_x;
|
|
|
|
int dimy = plant->tree_info->dim_y;
|
|
|
|
int cx = dimx / 2;
|
|
|
|
int cy = dimy / 2;
|
|
|
|
|
|
|
|
// Find the southeast trunk tile
|
|
|
|
int x = cx;
|
|
|
|
int y = cy;
|
|
|
|
|
|
|
|
while (x + 1 < dimx && y + 1 < dimy)
|
|
|
|
{
|
|
|
|
if (plant->tree_info->body[0][(y * dimx) + (x + 1)].bits.trunk)
|
|
|
|
++x;
|
|
|
|
else if (plant->tree_info->body[0][((y + 1) * dimx) + x].bits.trunk)
|
|
|
|
++y;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return df::coord(plant->pos.x - cx + x, plant->pos.y - cy + y, plant->pos.z);
|
2017-05-04 19:47:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Designations::isPlantMarked(const df::plant *plant)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(plant);
|
|
|
|
|
2017-05-05 19:25:25 -06:00
|
|
|
df::coord des_pos = getPlantDesignationTile(plant);
|
|
|
|
df::map_block *block = Maps::getTileBlock(des_pos);
|
2017-05-04 19:47:12 -06:00
|
|
|
if (!block)
|
|
|
|
return false;
|
|
|
|
|
2017-05-05 19:25:25 -06:00
|
|
|
if (block->designation[des_pos.x % 16][des_pos.y % 16].bits.dig == tile_dig_designation::Default)
|
2017-05-04 19:47:12 -06:00
|
|
|
return true;
|
|
|
|
|
2017-11-24 22:59:59 -07:00
|
|
|
for (auto *link = world->jobs.list.next; link; link = link->next)
|
2017-05-04 19:47:12 -06:00
|
|
|
{
|
|
|
|
df::job *job = link->item;
|
|
|
|
if (!job)
|
|
|
|
continue;
|
|
|
|
if (job->job_type != job_type::FellTree && job->job_type != job_type::GatherPlants)
|
|
|
|
continue;
|
2017-05-05 19:25:25 -06:00
|
|
|
if (job->pos == des_pos)
|
2017-05-04 19:47:12 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Designations::canMarkPlant(const df::plant *plant)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(plant);
|
|
|
|
|
|
|
|
if (!getPlantBlock(plant))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !isPlantMarked(plant);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Designations::markPlant(const df::plant *plant)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(plant);
|
|
|
|
|
|
|
|
if (canMarkPlant(plant))
|
|
|
|
{
|
2017-05-05 19:25:25 -06:00
|
|
|
df::coord des_pos = getPlantDesignationTile(plant);
|
|
|
|
df::map_block *block = Maps::getTileBlock(des_pos);
|
|
|
|
block->designation[des_pos.x % 16][des_pos.y % 16].bits.dig = tile_dig_designation::Default;
|
2017-05-04 19:47:12 -06:00
|
|
|
block->flags.bits.designated = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Designations::canUnmarkPlant(const df::plant *plant)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(plant);
|
|
|
|
|
|
|
|
if (!getPlantBlock(plant))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return isPlantMarked(plant);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Designations::unmarkPlant(const df::plant *plant)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(plant);
|
|
|
|
|
|
|
|
if (canUnmarkPlant(plant))
|
|
|
|
{
|
2017-05-05 19:25:25 -06:00
|
|
|
df::coord des_pos = getPlantDesignationTile(plant);
|
|
|
|
df::map_block *block = Maps::getTileBlock(des_pos);
|
|
|
|
block->designation[des_pos.x % 16][des_pos.y % 16].bits.dig = tile_dig_designation::No;
|
2017-05-04 19:47:12 -06:00
|
|
|
block->flags.bits.designated = true;
|
|
|
|
|
2017-11-24 22:59:59 -07:00
|
|
|
auto *link = world->jobs.list.next;
|
2017-05-04 19:47:12 -06:00
|
|
|
while (link)
|
|
|
|
{
|
|
|
|
auto *next = link->next;
|
|
|
|
df::job *job = link->item;
|
|
|
|
|
2017-05-05 15:24:51 -06:00
|
|
|
if (job &&
|
|
|
|
(job->job_type == job_type::FellTree || job->job_type == job_type::GatherPlants) &&
|
2017-05-05 19:25:25 -06:00
|
|
|
job->pos == des_pos)
|
2017-05-05 15:24:51 -06:00
|
|
|
{
|
2017-05-04 19:47:12 -06:00
|
|
|
Job::removeJob(job);
|
2017-05-05 15:24:51 -06:00
|
|
|
}
|
2017-05-04 19:47:12 -06:00
|
|
|
|
|
|
|
link = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|