Add unit and tile visibility functions

develop
lethosor 2017-06-10 21:54:08 -04:00
parent 9b63c451b1
commit 27343e3253
6 changed files with 39 additions and 0 deletions

@ -1135,6 +1135,10 @@ Units module
The unit is an alive sane citizen of the fortress; wraps the The unit is an alive sane citizen of the fortress; wraps the
same checks the game uses to decide game-over by extinction. same checks the game uses to decide game-over by extinction.
* ``dfhack.units.isVisible(unit)``
The unit is visible on the map.
* ``dfhack.units.getAge(unit[,true_age])`` * ``dfhack.units.getAge(unit[,true_age])``
Returns the age of the unit in years as a floating-point value. Returns the age of the unit in years as a floating-point value.
@ -1300,6 +1304,10 @@ Maps module
Checks if the given df::coord or x,y,z in local tile coordinates are valid. Checks if the given df::coord or x,y,z in local tile coordinates are valid.
* ``dfhack.maps.isTileVisible(coords)``, or ``isTileVisible(x,y,z)``
Checks if the given df::coord or x,y,z in local tile coordinates is visible.
* ``dfhack.maps.getTileBlock(coords)``, or ``getTileBlock(x,y,z)`` * ``dfhack.maps.getTileBlock(coords)``, or ``getTileBlock(x,y,z)``
Returns a map block object for given df::coord or x,y,z in local tile coordinates. Returns a map block object for given df::coord or x,y,z in local tile coordinates.

@ -1572,6 +1572,7 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = {
WRAPM(Units, isSane), WRAPM(Units, isSane),
WRAPM(Units, isDwarf), WRAPM(Units, isDwarf),
WRAPM(Units, isCitizen), WRAPM(Units, isCitizen),
WRAPM(Units, isVisible),
WRAPM(Units, getAge), WRAPM(Units, getAge),
WRAPM(Units, getKillCount), WRAPM(Units, getKillCount),
WRAPM(Units, getNominalSkill), WRAPM(Units, getNominalSkill),
@ -1818,6 +1819,13 @@ static int maps_isValidTilePos(lua_State *L)
return 1; return 1;
} }
static int maps_isTileVisible(lua_State *L)
{
auto pos = CheckCoordXYZ(L, 1, true);
lua_pushboolean(L, Maps::isTileVisible(pos));
return 1;
}
static int maps_getTileBlock(lua_State *L) static int maps_getTileBlock(lua_State *L)
{ {
auto pos = CheckCoordXYZ(L, 1, true); auto pos = CheckCoordXYZ(L, 1, true);
@ -1866,6 +1874,7 @@ static int maps_getTileBiomeRgn(lua_State *L)
static const luaL_Reg dfhack_maps_funcs[] = { static const luaL_Reg dfhack_maps_funcs[] = {
{ "isValidTilePos", maps_isValidTilePos }, { "isValidTilePos", maps_isValidTilePos },
{ "isTileVisible", maps_isTileVisible },
{ "getTileBlock", maps_getTileBlock }, { "getTileBlock", maps_getTileBlock },
{ "ensureTileBlock", maps_ensureTileBlock }, { "ensureTileBlock", maps_ensureTileBlock },
{ "getTileType", maps_getTileType }, { "getTileType", maps_getTileType },

@ -263,6 +263,9 @@ extern DFHACK_EXPORT void getPosition(int32_t& x, int32_t& y, int32_t& z);
extern DFHACK_EXPORT bool isValidTilePos(int32_t x, int32_t y, int32_t z); extern DFHACK_EXPORT bool isValidTilePos(int32_t x, int32_t y, int32_t z);
inline bool isValidTilePos(df::coord pos) { return isValidTilePos(pos.x, pos.y, pos.z); } inline bool isValidTilePos(df::coord pos) { return isValidTilePos(pos.x, pos.y, pos.z); }
extern DFHACK_EXPORT bool isTileVisible(int32_t x, int32_t y, int32_t z);
inline bool isTileVisible(df::coord pos) { return isTileVisible(pos.x, pos.y, pos.z); }
/** /**
* Get the map block or NULL if block is not valid * Get the map block or NULL if block is not valid
*/ */

@ -116,6 +116,7 @@ DFHACK_EXPORT bool isAvailableForAdoption(df::unit* unit);
DFHACK_EXPORT bool isOwnCiv(df::unit* unit); DFHACK_EXPORT bool isOwnCiv(df::unit* unit);
DFHACK_EXPORT bool isOwnGroup(df::unit* unit); DFHACK_EXPORT bool isOwnGroup(df::unit* unit);
DFHACK_EXPORT bool isOwnRace(df::unit* unit); DFHACK_EXPORT bool isOwnRace(df::unit* unit);
DFHACK_EXPORT bool isVisible(df::unit* unit);
DFHACK_EXPORT std::string getRaceNameById(int32_t race_id); DFHACK_EXPORT std::string getRaceNameById(int32_t race_id);
DFHACK_EXPORT std::string getRaceName(df::unit* unit); DFHACK_EXPORT std::string getRaceName(df::unit* unit);

@ -166,6 +166,17 @@ bool Maps::isValidTilePos(int32_t x, int32_t y, int32_t z)
return true; return true;
} }
bool Maps::isTileVisible(int32_t x, int32_t y, int32_t z)
{
df::map_block *block = getTileBlock(x, y, z);
if (!block)
return false;
if (block->designation[x % 16][y % 16].bits.hidden)
return false;
return true;
}
df::map_block *Maps::getTileBlock (int32_t x, int32_t y, int32_t z) df::map_block *Maps::getTileBlock (int32_t x, int32_t y, int32_t z)
{ {
if (!isValidTilePos(x,y,z)) if (!isValidTilePos(x,y,z))

@ -42,6 +42,7 @@ using namespace std;
// we connect to those // we connect to those
#include "modules/Units.h" #include "modules/Units.h"
#include "modules/Items.h" #include "modules/Items.h"
#include "modules/Maps.h"
#include "modules/Materials.h" #include "modules/Materials.h"
#include "modules/Translation.h" #include "modules/Translation.h"
#include "ModuleFactory.h" #include "ModuleFactory.h"
@ -526,6 +527,12 @@ bool Units::isOwnRace(df::unit* unit)
return unit->race == ui->race_id; return unit->race == ui->race_id;
} }
bool Units::isVisible(df::unit* unit)
{
CHECK_NULL_POINTER(unit);
return Maps::isTileVisible(unit->pos);
}
// get race name by id or unit pointer // get race name by id or unit pointer
string Units::getRaceNameById(int32_t id) string Units::getRaceNameById(int32_t id)
{ {