From 61245711f7840a4d3c36739d5842180b95995136 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Wed, 11 Apr 2012 16:20:16 +0400 Subject: [PATCH] Export a few maps functions to lua. --- LUA_API.rst | 31 +++++++++++++++++++++++++++++++ Lua API.html | 27 +++++++++++++++++++++++++++ library/LuaApi.cpp | 17 ++++++++++++++--- library/LuaTypes.cpp | 9 +++++++++ library/lua/dfhack.lua | 10 ++++++++++ 5 files changed, 91 insertions(+), 3 deletions(-) diff --git a/LUA_API.rst b/LUA_API.rst index ee3b1077c..4d7e0df76 100644 --- a/LUA_API.rst +++ b/LUA_API.rst @@ -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. diff --git a/Lua API.html b/Lua API.html index 89e158037..0f5f18deb 100644 --- a/Lua API.html +++ b/Lua API.html @@ -340,6 +340,7 @@ ul.auto-toc {
  • Gui module
  • Job module
  • Units module
  • +
  • Maps module
  • @@ -918,6 +919,32 @@ a lua list containing them.

    +
    +

    Maps module

    + +
    diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index c336e309c..ba989ecf2 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -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); } diff --git a/library/LuaTypes.cpp b/library/LuaTypes.cpp index 2f9ef9e81..40269da80 100644 --- a/library/LuaTypes.cpp +++ b/library/LuaTypes.cpp @@ -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"); } diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index b37183cb6..25d8c2ba8 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -89,5 +89,15 @@ function dfhack.matinfo:__tostring() return "" 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