Expose new unit functions to lua and add some basic safety checks

develop
lethosor 2016-04-23 21:52:50 -04:00
parent f66dd77a72
commit 402a96b0e6
2 changed files with 22 additions and 6 deletions

@ -1557,6 +1557,13 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = {
WRAPM(Units, isMerchant), WRAPM(Units, isMerchant),
WRAPM(Units, isForest), WRAPM(Units, isForest),
WRAPM(Units, isMarkedForSlaughter), WRAPM(Units, isMarkedForSlaughter),
WRAPM(Units, isTame),
WRAPM(Units, isTrained),
WRAPM(Units, isGay),
WRAPM(Units, isNaked),
WRAPM(Units, isUndead),
WRAPM(Units, isGelded),
WRAPM(Units, isDomesticated),
{ NULL, NULL } { NULL, NULL }
}; };

@ -1848,12 +1848,13 @@ bool Units::isMarkedForSlaughter(df::unit* unit)
return unit->flags2.bits.slaughter == 1; return unit->flags2.bits.slaughter == 1;
} }
bool Units::isTame(df::unit* creature) bool Units::isTame(df::unit* unit)
{ {
CHECK_NULL_POINTER(unit);
bool tame = false; bool tame = false;
if(creature->flags1.bits.tame) if(unit->flags1.bits.tame)
{ {
switch (creature->training_level) switch (unit->training_level)
{ {
case df::animal_training_level::SemiWild: //?? case df::animal_training_level::SemiWild: //??
case df::animal_training_level::Trained: case df::animal_training_level::Trained:
@ -1877,6 +1878,7 @@ bool Units::isTame(df::unit* creature)
bool Units::isTrained(df::unit* unit) bool Units::isTrained(df::unit* unit)
{ {
CHECK_NULL_POINTER(unit);
// case a: trained for war/hunting (those don't have a training level, strangely) // case a: trained for war/hunting (those don't have a training level, strangely)
if(Units::isWar(unit) || Units::isHunter(unit)) if(Units::isWar(unit) || Units::isHunter(unit))
return true; return true;
@ -1902,6 +1904,9 @@ bool Units::isTrained(df::unit* unit)
bool Units::isGay(df::unit* unit) bool Units::isGay(df::unit* unit)
{ {
CHECK_NULL_POINTER(unit);
if (!unit->status.current_soul)
return false;
df::orientation_flags orientation = unit->status.current_soul->orientation_flags; df::orientation_flags orientation = unit->status.current_soul->orientation_flags;
return (Units::isFemale(unit) && ! (orientation.whole & (orientation.mask_marry_male | orientation.mask_romance_male))) return (Units::isFemale(unit) && ! (orientation.whole & (orientation.mask_marry_male | orientation.mask_romance_male)))
|| (!Units::isFemale(unit) && ! (orientation.whole & (orientation.mask_marry_female | orientation.mask_romance_female))); || (!Units::isFemale(unit) && ! (orientation.whole & (orientation.mask_marry_female | orientation.mask_romance_female)));
@ -1909,12 +1914,14 @@ bool Units::isGay(df::unit* unit)
bool Units::isNaked(df::unit* unit) bool Units::isNaked(df::unit* unit)
{ {
CHECK_NULL_POINTER(unit);
// TODO(kazimuth): is this correct? // TODO(kazimuth): is this correct?
return (unit->inventory.empty()); return (unit->inventory.empty());
} }
bool Units::isUndead(df::unit* unit) bool Units::isUndead(df::unit* unit)
{ {
CHECK_NULL_POINTER(unit);
// ignore vampires, they should be treated like normal dwarves // ignore vampires, they should be treated like normal dwarves
return (unit->flags3.bits.ghostly || return (unit->flags3.bits.ghostly ||
( (unit->curse.add_tags1.bits.OPPOSED_TO_LIFE || unit->curse.add_tags1.bits.NOT_LIVING) ( (unit->curse.add_tags1.bits.OPPOSED_TO_LIFE || unit->curse.add_tags1.bits.NOT_LIVING)
@ -1923,6 +1930,7 @@ bool Units::isUndead(df::unit* unit)
bool Units::isGelded(df::unit* unit) bool Units::isGelded(df::unit* unit)
{ {
CHECK_NULL_POINTER(unit);
auto wounds = unit->body.wounds; auto wounds = unit->body.wounds;
for(auto wound = wounds.begin(); wound != wounds.end(); ++wound) for(auto wound = wounds.begin(); wound != wounds.end(); ++wound)
{ {
@ -1938,12 +1946,13 @@ bool Units::isGelded(df::unit* unit)
// check if creature is domesticated // check if creature is domesticated
// seems to be the only way to really tell if it's completely safe to autonestbox it (training can revert) // seems to be the only way to really tell if it's completely safe to autonestbox it (training can revert)
bool Units::isDomesticated(df::unit* creature) bool Units::isDomesticated(df::unit* unit)
{ {
CHECK_NULL_POINTER(unit);
bool tame = false; bool tame = false;
if(creature->flags1.bits.tame) if(unit->flags1.bits.tame)
{ {
switch (creature->training_level) switch (unit->training_level)
{ {
case df::animal_training_level::Domesticated: case df::animal_training_level::Domesticated:
tame=true; tame=true;