From 3c4bf2554bb28fc254f2d3b03dccfecc4b52e26b Mon Sep 17 00:00:00 2001 From: Sveinung Kvilhaugsvik Date: Wed, 3 Feb 2021 09:08:25 +0100 Subject: [PATCH 9/9] Lua script API: add tile extras_owner. Export a tile's extras owner to Lua. v2: change API to tile/extra pair as suggested by Marko Lindqvist See osdn #41445 --- common/scriptcore/api_game_methods.c | 36 ++++++++++++++++++++++++++++ common/scriptcore/api_game_methods.h | 2 ++ common/scriptcore/tolua_game.pkg | 2 ++ 3 files changed, 40 insertions(+) diff --git a/common/scriptcore/api_game_methods.c b/common/scriptcore/api_game_methods.c index c16cc93682..a13cfdea66 100644 --- a/common/scriptcore/api_game_methods.c +++ b/common/scriptcore/api_game_methods.c @@ -894,6 +894,42 @@ bool api_methods_tile_has_road(lua_State *L, Tile *ptile, const char *name) } } +/**********************************************************************//** + Return the extra owner for the specified extra on ptile or NULL if the + extra isn't there. + If no name is specified the owner of the first owned extra at the tile + is returned. +**************************************************************************/ +Player *api_methods_tile_extra_owner(lua_State *L, + Tile *ptile, const char *extra_name) +{ + LUASCRIPT_CHECK_STATE(L, NULL); + LUASCRIPT_CHECK_SELF(L, ptile, NULL); + + if (extra_name) { + struct extra_type *pextra; + + pextra = extra_type_by_rule_name(extra_name); + LUASCRIPT_CHECK_ARG(L, pextra != NULL, 3, "unknown extra type", NULL); + + if (tile_has_extra(ptile, pextra)) { + /* All extras have the same owner. */ + return extra_owner(ptile); + } else { + /* The extra isn't there. */ + return NULL; + } + } else { + extra_type_iterate(pextra) { + if (tile_has_extra(ptile, pextra)) { + /* All extras have the same owner. */ + return extra_owner(ptile); + } + } extra_type_iterate_end; + return NULL; + } +} + /*************************************************************************//** Is tile occupied by enemies *****************************************************************************/ diff --git a/common/scriptcore/api_game_methods.h b/common/scriptcore/api_game_methods.h index 3d19dd489d..9a1e01ba18 100644 --- a/common/scriptcore/api_game_methods.h +++ b/common/scriptcore/api_game_methods.h @@ -136,6 +136,8 @@ bool api_methods_tile_city_exists_within_max_city_map(lua_State *L, bool api_methods_tile_has_extra(lua_State *L, Tile *ptile, const char *name); bool api_methods_tile_has_base(lua_State *L, Tile *ptile, const char *name); bool api_methods_tile_has_road(lua_State *L, Tile *ptile, const char *name); +Player *api_methods_tile_extra_owner(lua_State *L, + Tile *ptile, const char *extra_name); bool api_methods_enemy_tile(lua_State *L, Tile *ptile, Player *against); int api_methods_tile_num_units(lua_State *L, Tile *ptile); int api_methods_tile_sq_distance(lua_State *L, Tile *ptile1, Tile *ptile2); diff --git a/common/scriptcore/tolua_game.pkg b/common/scriptcore/tolua_game.pkg index 6330507f30..5cdedd79b7 100644 --- a/common/scriptcore/tolua_game.pkg +++ b/common/scriptcore/tolua_game.pkg @@ -296,6 +296,8 @@ module Tile { @ has_base(lua_State *L, Tile *self, const char *name); bool api_methods_tile_has_road @ has_road(lua_State *L, Tile *self, const char *name); + Player *api_methods_tile_extra_owner + @ extra_owner(lua_State *L, Tile *self, const char *extra_name); bool api_methods_enemy_tile @ is_enemy(lua_State *L, Tile *self, Player *against); int api_methods_tile_num_units -- 2.20.1