From 7273e67accdba5f9cbf62f74ed5243ccd047dfce Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Mon, 4 Sep 2023 11:29:38 +0300 Subject: [PATCH 21/21] Lua: Add effects.tile_bonus() See osdn #47457 Signed-off-by: Marko Lindqvist --- common/effects.c | 4 +-- common/scriptcore/api_game_effects.c | 51 ++++++++++++++++++++++++++++ common/scriptcore/api_game_effects.h | 2 ++ common/scriptcore/tolua_game.pkg | 3 ++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/common/effects.c b/common/effects.c index 71b2ac020f..a7d8fbfa4e 100644 --- a/common/effects.c +++ b/common/effects.c @@ -907,8 +907,8 @@ int get_city_specialist_output_bonus(const struct city *pcity, pcity must be supplied. FIXME: this is now used both for tile bonuses, tile-output bonuses, - and city-output bonuses. Thus ptile or poutput may be NULL for - certain callers. This could be changed by adding 2 new functions to + and city-output bonuses. Thus ptile or poutput may be NULL for + certain callers. This could be changed by adding 2 new functions to the interface but they'd be almost identical and their likely names would conflict with functions already in city.c. It's also very similar to get_tile_output_bonus(); it should be diff --git a/common/scriptcore/api_game_effects.c b/common/scriptcore/api_game_effects.c index d37d10a94c..4ea4e7e25f 100644 --- a/common/scriptcore/api_game_effects.c +++ b/common/scriptcore/api_game_effects.c @@ -109,6 +109,57 @@ int api_effects_unit_bonus(lua_State *L, Unit *punit, Player *other_player, etype); } +/***********************************************************************//** + Get effect bonus at tile. + + @param L Lua State to use + @param ptile Tile to get the effect value for + @param pcity Optional: City related to the effect + (e.g. City harvesting the tile) + @param output_id Optional: Indetifier of the output related + to the effect (e.g. Output to harvest from the tile) + @param effect_type Effect to check + @return Effect bonus value +***************************************************************************/ +int api_effects_tile_bonus(lua_State *L, Tile *ptile, City *pcity, + const char *output_id, const char *effect_type) +{ + const struct player *pplayer; + const struct output_type *poutput = NULL; + enum effect_type etype; + + LUASCRIPT_CHECK_STATE(L, 0); + LUASCRIPT_CHECK_ARG_NIL(L, ptile, 2, Tile, 0); + LUASCRIPT_CHECK_ARG_NIL(L, effect_type, 5, string, 0); + + etype = effect_type_by_name(effect_type, fc_strcasecmp); + if (!effect_type_is_valid(etype)) { + return 0; + } + + if (output_id != NULL) { + enum output_type_id id = output_type_by_identifier(output_id); + + if (id != O_LAST) { + poutput = get_output_type(id); + } else { + log_warn(_("Unknown output identifier \"%s\""), output_id); + } + } + + pplayer = (pcity != NULL ? city_owner(pcity) : NULL); + + return get_target_bonus_effects(NULL, + &(const struct req_context) { + .player = pplayer, + .city = pcity, + .tile = ptile, + .output = poutput, + }, + NULL, + etype); +} + /***********************************************************************//** Returns the effect bonus at a tile and the specified unit. Unlike effects.unit_bonus() the city the effect is evaluated against is diff --git a/common/scriptcore/api_game_effects.h b/common/scriptcore/api_game_effects.h index bd990cb92c..18d5a86972 100644 --- a/common/scriptcore/api_game_effects.h +++ b/common/scriptcore/api_game_effects.h @@ -30,6 +30,8 @@ int api_effects_city_bonus(lua_State *L, City *pcity, const char *effect_type); int api_effects_unit_bonus(lua_State *L, Unit *punit, Player *other_player, const char *effect_type); +int api_effects_tile_bonus(lua_State *L, Tile *ptile, City *pcity, + const char *output_id, const char *effect_type); int api_effects_unit_vs_tile_bonus(lua_State *L, Unit *punit, Tile *ptile, const char *effect_type); diff --git a/common/scriptcore/tolua_game.pkg b/common/scriptcore/tolua_game.pkg index 588e9abcba..a5e23ad87e 100644 --- a/common/scriptcore/tolua_game.pkg +++ b/common/scriptcore/tolua_game.pkg @@ -618,6 +618,9 @@ module effects { int api_effects_unit_bonus @ unit_bonus(lua_State *L, Unit *punit, Player *other_player, const char *effect_type); + int api_effects_tile_bonus + @ tile_bonus(lua_State *L, Tile *ptile, City *pcity, + const char *output_id, const char *effect_type); int api_effects_unit_vs_tile_bonus @ unit_vs_tile_bonus(lua_State *L, Unit *punit, Tile *ptile, const char *effect_type); -- 2.40.1