Export a few maps functions to lua.

develop
Alexander Gavrilov 2012-04-11 16:20:16 +04:00
parent c7b922250b
commit 61245711f7
5 changed files with 91 additions and 3 deletions

@ -686,3 +686,34 @@ Units module
* ``dfhack.units.isSane(unit)``
The unit is capable of rational action, i.e. not dead, insane or zombie.
Maps module
-----------
* ``dfhack.maps.getSize()``
Returns map size in blocks: *x, y, z*
* ``dfhack.maps.getTileSize()``
Returns map size in tiles: *x, y, z*
* ``dfhack.maps.getBlock(x,y,z)``
Returns a map block object for given x,y,z in local block coordinates.
* ``dfhack.maps.getTileBlock(coords)``
Returns a map block object for given df::coord in local tile coordinates.
* ``dfhack.maps.getRegionBiome(region_coord2d)``
Returns the biome info struct for the given global map region.
* ``dfhack.maps.getGlobalInitFeature(index)``
Returns the global feature object with the given index.
* ``dfhack.maps.getLocalInitFeature(region_coord2d,index)``
Returns the local feature object with the given region coords and index.

@ -340,6 +340,7 @@ ul.auto-toc {
<li><a class="reference internal" href="#gui-module" id="id14">Gui module</a></li>
<li><a class="reference internal" href="#job-module" id="id15">Job module</a></li>
<li><a class="reference internal" href="#units-module" id="id16">Units module</a></li>
<li><a class="reference internal" href="#maps-module" id="id17">Maps module</a></li>
</ul>
</li>
</ul>
@ -918,6 +919,32 @@ a lua list containing them.</p>
</li>
</ul>
</div>
<div class="section" id="maps-module">
<h3><a class="toc-backref" href="#id17">Maps module</a></h3>
<ul>
<li><p class="first"><tt class="docutils literal">dfhack.maps.getSize()</tt></p>
<p>Returns map size in blocks: <em>x, y, z</em></p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.maps.getTileSize()</tt></p>
<p>Returns map size in tiles: <em>x, y, z</em></p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.maps.getBlock(x,y,z)</tt></p>
<p>Returns a map block object for given x,y,z in local block coordinates.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.maps.getTileBlock(coords)</tt></p>
<p>Returns a map block object for given df::coord in local tile coordinates.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.maps.getRegionBiome(region_coord2d)</tt></p>
<p>Returns the biome info struct for the given global map region.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.maps.getGlobalInitFeature(index)</tt></p>
<p>Returns the global feature object with the given index.</p>
</li>
<li><p class="first"><tt class="docutils literal">dfhack.maps.getLocalInitFeature(region_coord2d,index)</tt></p>
<p>Returns the local feature object with the given region coords and index.</p>
</li>
</ul>
</div>
</div>
</div>
</div>

@ -43,6 +43,7 @@ distribution.
#include "modules/Translation.h"
#include "modules/Units.h"
#include "modules/Materials.h"
#include "modules/Maps.h"
#include "LuaWrapper.h"
#include "LuaTools.h"
@ -527,9 +528,9 @@ static void OpenModule(lua_State *state, const char *mname,
lua_pop(state, 1);
}
#define WRAPM(module, function) { #function, df::wrap_function(&module::function) }
#define WRAP(function) { #function, df::wrap_function(&function) }
#define WRAPN(name, function) { #name, df::wrap_function(&function) }
#define WRAPM(module, function) { #function, df::wrap_function(module::function) }
#define WRAP(function) { #function, df::wrap_function(function) }
#define WRAPN(name, function) { #name, df::wrap_function(function) }
static const LuaWrapper::FunctionReg dfhack_module[] = {
WRAPM(Translation, TranslateName),
@ -600,6 +601,15 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = {
{ NULL, NULL }
};
static const LuaWrapper::FunctionReg dfhack_maps_module[] = {
WRAPN(getBlock, (df::map_block* (*)(int32_t,int32_t,int32_t))Maps::getBlock),
WRAPN(getTileBlock, (df::map_block* (*)(df::coord))Maps::getTileBlock),
WRAPM(Maps, getRegionBiome),
WRAPM(Maps, getGlobalInitFeature),
WRAPM(Maps, getLocalInitFeature),
{ NULL, NULL }
};
/************************
* Main Open function *
************************/
@ -613,4 +623,5 @@ void OpenDFHackApi(lua_State *state)
OpenModule(state, "gui", dfhack_gui_module);
OpenModule(state, "job", dfhack_job_module, dfhack_job_funcs);
OpenModule(state, "units", dfhack_units_module);
OpenModule(state, "maps", dfhack_maps_module);
}

@ -80,6 +80,15 @@ void constructed_identity::lua_write(lua_State *state, int fname_idx, void *ptr,
{
invoke_assign(state, this, ptr, val_index);
}
// Allow by-value assignment for wrapped function parameters
else if (fname_idx == UPVAL_METHOD_NAME && lua_isuserdata(state, val_index))
{
void *nval = get_object_internal(state, this, val_index, false);
if (!nval)
field_error(state, fname_idx, "incompatible type in complex assignment", "write");
if (!copy(ptr, nval))
field_error(state, fname_idx, "no copy support", "write");
}
else
field_error(state, fname_idx, "complex object", "write");
}

@ -89,5 +89,15 @@ function dfhack.matinfo:__tostring()
return "<material "..self.type..":"..self.index.." "..self:getToken()..">"
end
function dfhack.maps.getSize()
local map = df.global.world.map
return map.x_count_block, map.y_count_block, map.z_count_block
end
function dfhack.maps.getTileSize()
local map = df.global.world.map
return map.x_count, map.y_count, map.z_count
end
-- Feed the table back to the require() mechanism.
return dfhack