Extract some stuff from zone.cpp to plugins/Units.{h,cpp}

develop
Chris Dombroski 2015-01-28 16:26:17 -05:00
parent 789cc01e93
commit c179f5b913
11 changed files with 312 additions and 309 deletions

@ -233,6 +233,36 @@ DFHACK_EXPORT bool isAlive(df::unit *unit);
DFHACK_EXPORT bool isSane(df::unit *unit);
DFHACK_EXPORT bool isCitizen(df::unit *unit);
DFHACK_EXPORT bool isDwarf(df::unit *unit);
DFHACK_EXPORT bool isWar(df::unit* unit);
DFHACK_EXPORT bool isHunter(df::unit* unit);
DFHACK_EXPORT bool isAvailableForAdoption(df::unit* unit);
DFHACK_EXPORT bool isOwnCiv(df::unit* unit);
DFHACK_EXPORT bool isOwnRace(df::unit* unit);
DFHACK_EXPORT std::string getRaceName(int32_t id);
DFHACK_EXPORT std::string getRaceName(df::unit* unit);
DFHACK_EXPORT std::string getRaceNamePlural(int32_t id);
DFHACK_EXPORT std::string getRaceNamePlural(df::unit* unit);
DFHACK_EXPORT std::string getRaceBabyName(int32_t id);
DFHACK_EXPORT std::string getRaceBabyName(df::unit* unit);
DFHACK_EXPORT std::string getRaceChildName(int32_t id);
DFHACK_EXPORT std::string getRaceChildName(df::unit* unit);
DFHACK_EXPORT bool isBaby(df::unit* unit);
DFHACK_EXPORT bool isChild(df::unit* unit);
DFHACK_EXPORT bool isAdult(df::unit* unit);
DFHACK_EXPORT bool isEggLayer(df::unit* unit);
DFHACK_EXPORT bool isGrazer(df::unit* unit);
DFHACK_EXPORT bool isMilkable(df::unit* unit);
DFHACK_EXPORT bool isTrainableWar(df::unit* unit);
DFHACK_EXPORT bool isMilkable(df::unit* unit);
DFHACK_EXPORT bool isTrainableHunting(df::unit* unit);
DFHACK_EXPORT bool isTamable(df::unit* unit);
DFHACK_EXPORT bool isMale(df::unit* unit);
DFHACK_EXPORT bool isFemale(df::unit* unit);
DFHACK_EXPORT bool isMerchant(df::unit* unit);
DFHACK_EXPORT bool isForest(df::unit* unit);
DFHACK_EXPORT bool isMarkedForSlaughter(df::unit* unit);
DFHACK_EXPORT double getAge(df::unit *unit, bool true_age = false);

@ -765,7 +765,7 @@ df::unit_misc_trait *Units::getMiscTrait(df::unit *unit, df::misc_trait_type typ
return NULL;
}
bool DFHack::Units::isDead(df::unit *unit)
bool Units::isDead(df::unit *unit)
{
CHECK_NULL_POINTER(unit);
@ -773,7 +773,7 @@ bool DFHack::Units::isDead(df::unit *unit)
unit->flags3.bits.ghostly;
}
bool DFHack::Units::isAlive(df::unit *unit)
bool Units::isAlive(df::unit *unit)
{
CHECK_NULL_POINTER(unit);
@ -782,12 +782,11 @@ bool DFHack::Units::isAlive(df::unit *unit)
!unit->curse.add_tags1.bits.NOT_LIVING;
}
bool DFHack::Units::isSane(df::unit *unit)
bool Units::isSane(df::unit *unit)
{
CHECK_NULL_POINTER(unit);
if (unit->flags1.bits.dead ||
unit->flags3.bits.ghostly ||
if (isDead(unit) ||
isOpposedToLife(unit) ||
unit->enemy.undead)
return false;
@ -808,7 +807,7 @@ bool DFHack::Units::isSane(df::unit *unit)
return true;
}
bool DFHack::Units::isCitizen(df::unit *unit)
bool Units::isCitizen(df::unit *unit)
{
CHECK_NULL_POINTER(unit);
@ -838,7 +837,7 @@ bool DFHack::Units::isCitizen(df::unit *unit)
!unit->flags2.bits.visitor;
}
bool DFHack::Units::isDwarf(df::unit *unit)
bool Units::isDwarf(df::unit *unit)
{
CHECK_NULL_POINTER(unit);
@ -846,7 +845,218 @@ bool DFHack::Units::isDwarf(df::unit *unit)
unit->enemy.normal_race == ui->race_id;
}
double DFHack::Units::getAge(df::unit *unit, bool true_age)
// check for profession "war creature"
bool Units::isWar(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return unit->profession == df::profession::TRAINED_WAR
|| unit->profession2 == df::profession::TRAINED_WAR;
}
// check for profession "hunting creature"
bool Units::isHunter(df::unit* unit)
{
CHECK_NULL_POINTER(unit)
return unit->profession == df::profession::TRAINED_HUNTER
|| unit->profession2 == df::profession::TRAINED_HUNTER;
}
// check if unit is marked as available for adoption
bool Units::isAvailableForAdoption(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
auto refs = unit->specific_refs;
for(size_t i=0; i<refs.size(); i++)
{
auto ref = refs[i];
auto reftype = ref->type;
if( reftype == df::specific_ref_type::PETINFO_PET )
{
//df::pet_info* pet = ref->pet;
return true;
}
}
return false;
}
// check if creature belongs to the player's civilization
// (don't try to pasture/slaughter random untame animals)
bool Units::isOwnCiv(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return unit->civ_id == ui->civ_id;
}
// check if creature belongs to the player's race
// (in combination with check for civ helps to filter out own dwarves)
bool Units::isOwnRace(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return unit->race == ui->race_id;
}
// get race name by id or unit pointer
string Units::getRaceName(int32_t id)
{
df::creature_raw *raw = world->raws.creatures.all[id];
if (raw)
return raw->creature_id;
return "";
}
string Units::getRaceName(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return getRaceName(unit->race);
}
// get plural of race name (used for display in autobutcher UI and for sorting the watchlist)
string Units::getRaceNamePlural(int32_t id)
{
df::creature_raw *raw = world->raws.creatures.all[id];
if (raw)
return raw->name[1]; // second field is plural of race name
return "";
}
string Units::getRaceNamePlural(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return getRaceNamePlural(unit->race);
}
string Units::getRaceBabyName(int32_t id)
{
df::creature_raw *raw = world->raws.creatures.all[id];
if (raw)
return raw->general_baby_name[0];
return "";
}
string Units::getRaceBabyName(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return getRaceBabyName(unit->race);
}
string Units::getRaceChildName(int32_t id)
{
df::creature_raw *raw = world->raws.creatures.all[id];
if (raw)
return raw->general_child_name[0];
return "";
}
string Units::getRaceChildName(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return getRaceChildName(unit->race);
}
bool Units::isBaby(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return unit->profession == df::profession::BABY;
}
bool Units::isChild(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return unit->profession == df::profession::CHILD;
}
bool Units::isAdult(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return !isBaby(unit) && !isChild(unit);
}
bool Units::isEggLayer(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
df::creature_raw *raw = world->raws.creatures.all[unit->race];
for (auto caste = raw->caste.begin(); caste != raw->caste.end(); ++caste)
{
if ((*caste)->flags.is_set(caste_raw_flags::LAYS_EGGS)
|| (*caste)->flags.is_set(caste_raw_flags::LAYS_UNUSUAL_EGGS))
return true;
}
return false;
}
bool Units::isGrazer(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
df::creature_raw *raw = world->raws.creatures.all[unit->race];
for (auto caste = raw->caste.begin(); caste != raw->caste.end(); ++caste)
{
if((*caste)->flags.is_set(caste_raw_flags::GRAZER))
return true;
}
return false;
}
bool Units::isMilkable(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
df::creature_raw *raw = world->raws.creatures.all[unit->race];
for (auto caste = raw->caste.begin(); caste != raw->caste.end(); ++caste)
{
if((*caste)->flags.is_set(caste_raw_flags::MILKABLE))
return true;
}
return false;
}
bool Units::isTrainableWar(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
df::creature_raw *raw = world->raws.creatures.all[unit->race];
for (auto caste = raw->caste.begin(); caste != raw->caste.end(); ++caste)
{
if((*caste)->flags.is_set(caste_raw_flags::TRAINABLE_WAR))
return true;
}
return false;
}
bool Units::isTrainableHunting(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
df::creature_raw *raw = world->raws.creatures.all[unit->race];
for (auto caste = raw->caste.begin(); caste != raw->caste.end(); ++caste)
{
if((*caste)->flags.is_set(caste_raw_flags::TRAINABLE_HUNTING))
return true;
}
return false;
}
bool Units::isTamable(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
df::creature_raw *raw = world->raws.creatures.all[unit->race];
for (auto caste = raw->caste.begin(); caste != raw->caste.end(); ++caste)
{
if((*caste)->flags.is_set(caste_raw_flags::PET) ||
(*caste)->flags.is_set(caste_raw_flags::PET_EXOTIC))
return true;
}
return false;
}
bool Units::isMale(df::unit* unit)
{
return unit->sex == 1;
}
bool Units::isFemale(df::unit* unit)
{
return unit->sex == 0;
}
double Units::getAge(df::unit *unit, bool true_age)
{
using df::global::cur_year;
using df::global::cur_year_tick;
@ -1126,7 +1336,7 @@ int Units::computeMovementSpeed(df::unit *unit)
// General counters and flags
if (unit->profession == profession::BABY)
if (isBaby(unit))
speed += 3000;
if (unit->flags3.bits.unk15)
@ -1326,7 +1536,7 @@ static bool noble_pos_compare(const Units::NoblePosition &a, const Units::NobleP
return a.position->id < b.position->id;
}
bool DFHack::Units::getNoblePositions(std::vector<NoblePosition> *pvec, df::unit *unit)
bool Units::getNoblePositions(std::vector<NoblePosition> *pvec, df::unit *unit)
{
CHECK_NULL_POINTER(unit);
@ -1367,7 +1577,7 @@ bool DFHack::Units::getNoblePositions(std::vector<NoblePosition> *pvec, df::unit
return true;
}
std::string DFHack::Units::getProfessionName(df::unit *unit, bool ignore_noble, bool plural)
std::string Units::getProfessionName(df::unit *unit, bool ignore_noble, bool plural)
{
CHECK_NULL_POINTER(unit);
@ -1400,7 +1610,7 @@ std::string DFHack::Units::getProfessionName(df::unit *unit, bool ignore_noble,
return getCasteProfessionName(unit->race, unit->caste, unit->profession, plural);
}
std::string DFHack::Units::getCasteProfessionName(int race, int casteid, df::profession pid, bool plural)
std::string Units::getCasteProfessionName(int race, int casteid, df::profession pid, bool plural)
{
std::string prof, race_prefix;
@ -1517,7 +1727,7 @@ std::string DFHack::Units::getCasteProfessionName(int race, int casteid, df::pro
return Translation::capitalize(prof, true);
}
int8_t DFHack::Units::getProfessionColor(df::unit *unit, bool ignore_noble)
int8_t Units::getProfessionColor(df::unit *unit, bool ignore_noble)
{
CHECK_NULL_POINTER(unit);
@ -1532,7 +1742,7 @@ int8_t DFHack::Units::getProfessionColor(df::unit *unit, bool ignore_noble)
return getCasteProfessionColor(unit->race, unit->caste, unit->profession);
}
int8_t DFHack::Units::getCasteProfessionColor(int race, int casteid, df::profession pid)
int8_t Units::getCasteProfessionColor(int race, int casteid, df::profession pid)
{
// make sure it's an actual profession
if (pid < 0 || !is_valid_enum_item(pid))
@ -1556,7 +1766,7 @@ int8_t DFHack::Units::getCasteProfessionColor(int race, int casteid, df::profess
return 3;
}
std::string DFHack::Units::getSquadName(df::unit *unit)
std::string Units::getSquadName(df::unit *unit)
{
if (unit->military.squad_id == -1)
return "";
@ -1567,3 +1777,22 @@ std::string DFHack::Units::getSquadName(df::unit *unit)
return squad->alias;
return Translation::TranslateName(&squad->name, true);
}
bool Units::isMerchant(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return unit->flags1.bits.merchant == 1;
}
bool Units::isForest(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return unit->flags1.bits.forest == 1;
}
bool Units::isMarkedForSlaughter(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return unit->flags2.bits.slaughter == 1;
}

@ -38,6 +38,7 @@
#include "modules/MapCache.h"
#include "modules/Items.h"
#include "modules/Units.h"
using std::string;
using std::endl;
@ -1158,8 +1159,8 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
is_on_break = true;
}
if (dwarfs[dwarf]->profession == profession::BABY ||
dwarfs[dwarf]->profession == profession::CHILD ||
if (Units::isBaby(dwarfs[dwarf]) ||
Units::isChild(dwarfs[dwarf]) ||
dwarfs[dwarf]->profession == profession::DRUNK)
{
dwarf_info[dwarf].state = CHILD;

@ -81,7 +81,7 @@ command_result catsplosion (color_ostream &out, std::vector <std::string> & para
{
df::unit * creature = Units::GetCreature(i);
df::creature_raw *raw = world->raws.creatures.all[creature->race];
if(creature->sex == 0) // female
if(Units::isFemale(creature))
{
female_counts[raw->creature_id].push_back(creature);
male_counts[raw->creature_id].size();

@ -1109,18 +1109,14 @@ struct preference_map
case (T_type::LikeCreature):
{
label = "Creature :";
auto creature = df::creature_raw::find(pref.creature_id);
if (creature)
label += creature->name[1];
Units::getRaceNamePlural(pref.creature_id);
break;
}
case (T_type::HateCreature):
{
label = "Hates :";
auto creature = df::creature_raw::find(pref.creature_id);
if (creature)
label += creature->name[1];
Units::getRaceNamePlural(pref.creature_id);
break;
}
@ -1619,8 +1615,8 @@ static void update_dwarf_stats(bool is_paused)
if (!monitor_jobs || is_paused)
continue;
if (unit->profession == profession::BABY ||
unit->profession == profession::CHILD ||
if (Units::isBaby(unit) ||
Units::isChild(unit) ||
unit->profession == profession::DRUNK)
{
continue;

@ -445,10 +445,10 @@ viewscreen_unitlaborsst::viewscreen_unitlaborsst(vector<df::unit*> &src, int cur
cur->allowEdit = true;
cur->active_index = active_idx[unit];
if (unit->race != ui->race_id)
if (!Units::isOwnRace(unit))
cur->allowEdit = false;
if (unit->civ_id != ui->civ_id)
if (!Units::isOwnCiv(unit))
cur->allowEdit = false;
if (unit->flags1.bits.dead)

@ -5,6 +5,7 @@
#include "Export.h"
#include "PluginManager.h"
#include "modules/EventManager.h"
#include "modules/Units.h"
#include "modules/Maps.h"
#include "df/caste_raw.h"
@ -79,12 +80,12 @@ void impregnateMany() {
df::creature_raw* creatureRaw = world->raws.creatures.all[race];
df::caste_raw* casteRaw = creatureRaw->caste[caste];
//must have PET or PET_EXOTIC
if ( !(casteRaw->flags.is_set(df::enums::caste_raw_flags::PET) || casteRaw->flags.is_set(df::enums::caste_raw_flags::PET_EXOTIC) ) )
if ( !Units::isTamable(unit))
continue;
//check for adulthood
if ( unit->profession == df::enums::profession::CHILD || unit->profession == df::enums::profession::BABY )
if ( Units::isBaby(unit) || Units::isChild(unit))
continue;
if ( unit->sex == 1 )
if ( Units::isMale(unit))
males[unit->race].push_back(a);
else
females[unit->race].push_back(a);

@ -960,8 +960,8 @@ private:
}
}
if (unit->profession == profession::BABY ||
unit->profession == profession::CHILD ||
if (Units::isBaby(unit) ||
Units::isChild(unit) ||
unit->profession == profession::DRUNK)
{
return "";

@ -7,6 +7,7 @@
#include "modules/Materials.h"
#include "modules/Translation.h"
#include "modules/Items.h"
#include "modules/Units.h"
#include "DataDefs.h"
#include "df/world.h"
@ -153,7 +154,7 @@ command_result df_showmood (color_ostream &out, vector <string> & parameters)
break;
}
out.print(".\n");
if (unit->sex)
if (Units::isMale(unit))
out.print("He has ");
else
out.print("She has ");

@ -707,7 +707,7 @@ static command_result tweak(color_ostream &out, vector <string> &parameters)
if (!unit)
return CR_FAILURE;
if(unit->race != ui->race_id)
if(!Units::isOwnRace(unit))
{
out << "Selected unit does not belong to your race!" << endl;
return CR_FAILURE;
@ -720,14 +720,14 @@ static command_result tweak(color_ostream &out, vector <string> &parameters)
// case #2: migrants who have the merchant flag
// happens on almost all maps after a few migrant waves
if(unit->flags1.bits.merchant)
if(Units::isMerchant(unit))
unit->flags1.bits.merchant = 0;
// this one is a cheat, but bugged migrants usually have the same civ_id
// so it should not be triggered in most cases
// if it happens that the player has 'foreign' units of the same race
// (vanilla df: dwarves not from mountainhome) on his map, just grab them
if(unit->civ_id != ui->civ_id)
if(!Units::isOwnCiv(unit))
unit->civ_id = ui->civ_id;
return fix_clothing_ownership(out, unit);
@ -742,11 +742,11 @@ static command_result tweak(color_ostream &out, vector <string> &parameters)
if (unit->flags2.bits.resident)
unit->flags2.bits.resident = 0;
if(unit->flags1.bits.merchant)
if(Units::isMerchant(unit))
unit->flags1.bits.merchant = 0;
if(unit->flags1.bits.forest)
if(Units::isForest(unit))
unit->flags1.bits.forest = 0;
if(unit->civ_id != ui->civ_id)
if(!Units::isOwnCiv(unit))
unit->civ_id = ui->civ_id;
if(unit->profession == df::profession::MERCHANT)
unit->profession = df::profession::TRADER;

@ -74,6 +74,7 @@ using namespace std;
using std::vector;
using std::string;
using namespace DFHack;
using namespace DFHack::Units;
using namespace df::enums;
DFHACK_PLUGIN("zone");
@ -338,14 +339,9 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
// Various small tool functions
// probably many of these should be moved to Unit.h and Building.h sometime later...
int32_t getUnitAge(df::unit* unit);
bool isTame(df::unit* unit);
bool isTrained(df::unit* unit);
bool isWar(df::unit* unit);
bool isHunter(df::unit* unit);
bool isOwnCiv(df::unit* unit);
bool isMerchant(df::unit* unit);
bool isForest(df::unit* unit);
bool isGay(df::unit* unit);
bool isActivityZone(df::building * building);
@ -368,22 +364,6 @@ void chainInfo(color_ostream & out, df::building* building, bool verbose);
bool isBuiltCageAtPos(df::coord pos);
bool isInBuiltCageRoom(df::unit*);
bool isNaked(df::unit *);
bool isTamable(df::unit *);
int32_t getUnitAge(df::unit* unit)
{
// If the birthday this year has not yet passed, subtract one year.
// ASSUMPTION: birth_time is on the same scale as cur_year_tick
int32_t yearDifference = *cur_year - unit->relations.birth_year;
if (unit->relations.birth_time >= *cur_year_tick)
yearDifference--;
return yearDifference;
}
bool isDead(df::unit* unit)
{
return unit->flags1.bits.dead;
}
// ignore vampires, they should be treated like normal dwarves
bool isUndead(df::unit* unit)
@ -393,21 +373,6 @@ bool isUndead(df::unit* unit)
&& !unit->curse.add_tags1.bits.BLOODSUCKER ));
}
bool isMerchant(df::unit* unit)
{
return unit->flags1.bits.merchant;
}
bool isForest(df::unit* unit)
{
return unit->flags1.bits.forest;
}
bool isMarkedForSlaughter(df::unit* unit)
{
return unit->flags2.bits.slaughter;
}
void doMarkForSlaughter(df::unit* unit)
{
unit->flags2.bits.slaughter = 1;
@ -487,195 +452,6 @@ bool isTrained(df::unit* unit)
return trained;
}
// check for profession "war creature"
bool isWar(df::unit* unit)
{
if( unit->profession == df::profession::TRAINED_WAR
|| unit->profession2 == df::profession::TRAINED_WAR)
return true;
else
return false;
}
// check for profession "hunting creature"
bool isHunter(df::unit* unit)
{
if( unit->profession == df::profession::TRAINED_HUNTER
|| unit->profession2 == df::profession::TRAINED_HUNTER)
return true;
else
return false;
}
// check if unit is marked as available for adoption
bool isAvailableForAdoption(df::unit* unit)
{
auto refs = unit->specific_refs;
for(size_t i=0; i<refs.size(); i++)
{
auto ref = refs[i];
auto reftype = ref->type;
if( reftype == df::specific_ref_type::PETINFO_PET )
{
//df::pet_info* pet = ref->pet;
return true;
}
}
return false;
}
// check if creature belongs to the player's civilization
// (don't try to pasture/slaughter random untame animals)
bool isOwnCiv(df::unit* unit)
{
return unit->civ_id == ui->civ_id;
}
// check if creature belongs to the player's race
// (in combination with check for civ helps to filter out own dwarves)
bool isOwnRace(df::unit* unit)
{
return unit->race == ui->race_id;
}
// get race name by id or unit pointer
// todo: rename these two functions to "getRaceToken" since the output is more of a token
string getRaceName(int32_t id)
{
df::creature_raw *raw = world->raws.creatures.all[id];
return raw->creature_id;
}
string getRaceName(df::unit* unit)
{
df::creature_raw *raw = world->raws.creatures.all[unit->race];
return raw->creature_id;
}
// get plural of race name (used for display in autobutcher UI and for sorting the watchlist)
string getRaceNamePlural(int32_t id)
{
//WatchedRace * w = watched_races[idx];
df::creature_raw *raw = world->raws.creatures.all[id];
return raw->name[1]; // second field is plural of race name
}
string getRaceBabyName(df::unit* unit)
{
df::creature_raw *raw = world->raws.creatures.all[unit->race];
return raw->general_baby_name[0];
}
string getRaceChildName(df::unit* unit)
{
df::creature_raw *raw = world->raws.creatures.all[unit->race];
return raw->general_child_name[0];
}
bool isBaby(df::unit* unit)
{
return unit->profession == df::profession::BABY;
}
bool isChild(df::unit* unit)
{
return unit->profession == df::profession::CHILD;
}
bool isAdult(df::unit* unit)
{
return !isBaby(unit) && !isChild(unit);
}
bool isEggLayer(df::unit* unit)
{
df::creature_raw *raw = world->raws.creatures.all[unit->race];
size_t sizecas = raw->caste.size();
for (size_t j = 0; j < sizecas;j++)
{
df::caste_raw *caste = raw->caste[j];
if( caste->flags.is_set(caste_raw_flags::LAYS_EGGS)
|| caste->flags.is_set(caste_raw_flags::LAYS_UNUSUAL_EGGS))
return true;
}
return false;
}
bool isGrazer(df::unit* unit)
{
df::creature_raw *raw = world->raws.creatures.all[unit->race];
size_t sizecas = raw->caste.size();
for (size_t j = 0; j < sizecas;j++)
{
df::caste_raw *caste = raw->caste[j];
if(caste->flags.is_set(caste_raw_flags::GRAZER))
return true;
}
return false;
}
bool isMilkable(df::unit* unit)
{
df::creature_raw *raw = world->raws.creatures.all[unit->race];
size_t sizecas = raw->caste.size();
for (size_t j = 0; j < sizecas;j++)
{
df::caste_raw *caste = raw->caste[j];
if(caste->flags.is_set(caste_raw_flags::MILKABLE))
return true;
}
return false;
}
bool isTrainableWar(df::unit* unit)
{
df::creature_raw *raw = world->raws.creatures.all[unit->race];
size_t sizecas = raw->caste.size();
for (size_t j = 0; j < sizecas;j++)
{
df::caste_raw *caste = raw->caste[j];
if(caste->flags.is_set(caste_raw_flags::TRAINABLE_WAR))
return true;
}
return false;
}
bool isTrainableHunting(df::unit* unit)
{
df::creature_raw *raw = world->raws.creatures.all[unit->race];
size_t sizecas = raw->caste.size();
for (size_t j = 0; j < sizecas;j++)
{
df::caste_raw *caste = raw->caste[j];
if(caste->flags.is_set(caste_raw_flags::TRAINABLE_HUNTING))
return true;
}
return false;
}
bool isTamable(df::unit* unit)
{
df::creature_raw *raw = world->raws.creatures.all[unit->race];
size_t sizecas = raw->caste.size();
for (size_t j = 0; j < sizecas;j++)
{
df::caste_raw *caste = raw->caste[j];
if(caste->flags.is_set(caste_raw_flags::PET) ||
caste->flags.is_set(caste_raw_flags::PET_EXOTIC))
return true;
}
return false;
}
bool isMale(df::unit* unit)
{
return unit->sex == 1;
}
bool isFemale(df::unit* unit)
{
return unit->sex == 0;
}
// found a unit with weird position values on one of my maps (negative and in the thousands)
// it didn't appear in the animal stocks screen, but looked completely fine otherwise (alive, tame, own, etc)
// maybe a rare bug, but better avoid assigning such units to zones or slaughter etc.
@ -695,18 +471,6 @@ bool isNaked(df::unit* unit)
return (unit->inventory.empty());
}
int getUnitIndexFromId(df::unit* unit_)
{
for (size_t i=0; i < world->units.all.size(); i++)
{
df::unit* unit = world->units.all[i];
if(unit->id == unit_->id)
return i;
}
return -1;
}
bool isGay(df::unit* unit)
{
df::orientation_flags orientation = unit->status.current_soul->orientation_flags;
@ -759,7 +523,7 @@ void unitInfo(color_ostream & out, df::unit* unit, bool verbose = false)
break;
}
out << ")";
out << ", age: " << getUnitAge(unit);
out << ", age: " << getAge(unit, true);
if(isTame(unit))
out << ", tame";
@ -783,7 +547,7 @@ void unitInfo(color_ostream & out, df::unit* unit, bool verbose = false)
if(verbose)
{
out << ". Pos: ("<<unit->pos.x << "/"<< unit->pos.y << "/" << unit->pos.z << ") " << endl;
out << "index in units vector: " << getUnitIndexFromId(unit) << endl;
out << "index in units vector: " << FindIndexById(unit->id) << endl;
}
out << endl;
@ -893,25 +657,6 @@ int32_t findBuildingIndexById(int32_t id)
return -1;
}
int32_t findUnitIndexById(int32_t id)
{
for (size_t i = 0; i < world->units.all.size(); i++)
{
if(world->units.all.at(i)->id == id)
return i;
}
return -1;
}
df::unit* findUnitById(int32_t id)
{
int32_t index = findUnitIndexById(id);
if(index != -1)
return world->units.all[index];
else
return NULL;
}
// returns id of pen/pit at cursor position (-1 if nothing found)
int32_t findPenPitAtCursor()
{
@ -1595,7 +1340,7 @@ command_result nickUnitsInZone(color_ostream& out, df::building* building, strin
df::building_civzonest * civz = (df::building_civzonest *) building;
for(size_t i = 0; i < civz->assigned_units.size(); i++)
{
df::unit* unit = findUnitById(civz->assigned_units[i]);
df::unit* unit = df::unit::find(civz->assigned_units[i]);
if(unit)
Units::setNickname(unit, nick);
}
@ -1615,7 +1360,7 @@ command_result nickUnitsInCage(color_ostream& out, df::building* building, strin
df::building_cagest* cage = (df::building_cagest*) building;
for(size_t i=0; i<cage->assigned_units.size(); i++)
{
df::unit* unit = findUnitById(cage->assigned_units[i]);
df::unit* unit = df::unit::find(cage->assigned_units[i]);
if(unit)
Units::setNickname(unit, nick);
}
@ -2475,8 +2220,8 @@ command_result df_zone (color_ostream &out, vector <string> & parameters)
|| (find_not_war && isWar(unit))
|| (find_hunter && !isHunter(unit))
|| (find_not_hunter && isHunter(unit))
|| (find_agemin && getUnitAge(unit)<target_agemin)
|| (find_agemax && getUnitAge(unit)>target_agemax)
|| (find_agemin && (int) getAge(unit, true)<target_agemin)
|| (find_agemax && (int) getAge(unit, true)>target_agemax)
|| (find_grazer && !isGrazer(unit))
|| (find_not_grazer && isGrazer(unit))
|| (find_egglayer && !isEggLayer(unit))
@ -2749,8 +2494,8 @@ command_result autoNestbox( color_ostream &out, bool verbose = false )
// (assuming that the value from there indicates in which tick of the current year the unit was born)
bool compareUnitAgesYounger(df::unit* i, df::unit* j)
{
int32_t age_i = getUnitAge(i);
int32_t age_j = getUnitAge(j);
int32_t age_i = (int32_t) getAge(i, true);
int32_t age_j = (int32_t) getAge(j, true);
if(age_i == 0 && age_j == 0)
{
age_i = i->relations.birth_time;
@ -2760,8 +2505,8 @@ bool compareUnitAgesYounger(df::unit* i, df::unit* j)
}
bool compareUnitAgesOlder(df::unit* i, df::unit* j)
{
int32_t age_i = getUnitAge(i);
int32_t age_j = getUnitAge(j);
int32_t age_i = (int32_t) getAge(i, true);
int32_t age_j = (int32_t) getAge(j, true);
if(age_i == 0 && age_j == 0)
{
age_i = i->relations.birth_time;
@ -3819,7 +3564,7 @@ void butcherRace(int race)
if(!isContainedInItem(unit) && !hasValidMapPos(unit))
continue;
unit->flags2.bits.slaughter = true;
doMarkForSlaughter(unit);
}
}
@ -3842,7 +3587,7 @@ void unbutcherRace(int race)
if(!isContainedInItem(unit) && !hasValidMapPos(unit))
continue;
unit->flags2.bits.slaughter = false;
unit->flags2.bits.slaughter = 0;
}
}