From 0076a871625b91b9b23b0177bee1116c8c83f959 Mon Sep 17 00:00:00 2001 From: Sveinung Kvilhaugsvik Date: Fri, 12 Feb 2021 12:52:51 +0100 Subject: [PATCH] Lua API: prepare for non unit actors. Rename edit.unit_perform_action() to edit.perform_action() so it can be used with non unit actors too. Assert that the actor kind is as expected. See osdn #41566 --- server/scripting/api_server_edit.c | 21 +++++++++----- server/scripting/api_server_edit.h | 14 ++++----- server/scripting/tolua_server.pkg | 46 +++++++++++++++--------------- 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/server/scripting/api_server_edit.c b/server/scripting/api_server_edit.c index ac17c71520..f9b697193e 100644 --- a/server/scripting/api_server_edit.c +++ b/server/scripting/api_server_edit.c @@ -197,7 +197,7 @@ bool api_edit_unit_teleport(lua_State *L, Unit *punit, Tile *dest) /***********************************************************************//** Force a unit to perform an action against a city. ***************************************************************************/ -bool api_edit_unit_perform_action_vs_city(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_city(lua_State *L, Unit *punit, Action *paction, City *tgt) { LUASCRIPT_CHECK_STATE(L, FALSE); @@ -205,6 +205,7 @@ bool api_edit_unit_perform_action_vs_city(lua_State *L, Unit *punit, LUASCRIPT_CHECK_ARG_NIL(L, paction, 3, Action, FALSE); LUASCRIPT_CHECK_ARG_NIL(L, tgt, 4, City, FALSE); + fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, FALSE); fc_assert_ret_val(action_get_target_kind(paction) == ATK_CITY, FALSE); fc_assert_ret_val(!action_has_result(paction, ACTRES_FOUND_CITY), FALSE); if (is_action_enabled_unit_on_city(paction->id, punit, tgt)) { @@ -220,7 +221,7 @@ bool api_edit_unit_perform_action_vs_city(lua_State *L, Unit *punit, /***********************************************************************//** Force a unit to perform an action against a city and a building. ***************************************************************************/ -bool api_edit_unit_perform_action_vs_city_impr(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_city_impr(lua_State *L, Unit *punit, Action *paction, City *tgt, Building_Type *sub_tgt) { @@ -230,6 +231,7 @@ bool api_edit_unit_perform_action_vs_city_impr(lua_State *L, Unit *punit, LUASCRIPT_CHECK_ARG_NIL(L, tgt, 4, City, FALSE); LUASCRIPT_CHECK_ARG_NIL(L, sub_tgt, 5, Building_Type, FALSE); + fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, FALSE); fc_assert_ret_val(action_get_target_kind(paction) == ATK_CITY, FALSE); fc_assert_ret_val(!action_has_result(paction, ACTRES_FOUND_CITY), FALSE); if (is_action_enabled_unit_on_city(paction->id, punit, tgt)) { @@ -245,7 +247,7 @@ bool api_edit_unit_perform_action_vs_city_impr(lua_State *L, Unit *punit, /***********************************************************************//** Force a unit to perform an action against a city and a tech. ***************************************************************************/ -bool api_edit_unit_perform_action_vs_city_tech(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_city_tech(lua_State *L, Unit *punit, Action *paction, City *tgt, Tech_Type *sub_tgt) { @@ -255,6 +257,7 @@ bool api_edit_unit_perform_action_vs_city_tech(lua_State *L, Unit *punit, LUASCRIPT_CHECK_ARG_NIL(L, tgt, 4, City, FALSE); LUASCRIPT_CHECK_ARG_NIL(L, sub_tgt, 5, Tech_Type, FALSE); + fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, FALSE); fc_assert_ret_val(action_get_target_kind(paction) == ATK_CITY, FALSE); fc_assert_ret_val(!action_has_result(paction, ACTRES_FOUND_CITY), FALSE); if (is_action_enabled_unit_on_city(paction->id, punit, tgt)) { @@ -270,7 +273,7 @@ bool api_edit_unit_perform_action_vs_city_tech(lua_State *L, Unit *punit, /***********************************************************************//** Force a unit to perform an action against a unit. ***************************************************************************/ -bool api_edit_unit_perform_action_vs_unit(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_unit(lua_State *L, Unit *punit, Action *paction, Unit *tgt) { LUASCRIPT_CHECK_STATE(L, FALSE); @@ -278,6 +281,7 @@ bool api_edit_unit_perform_action_vs_unit(lua_State *L, Unit *punit, LUASCRIPT_CHECK_ARG_NIL(L, paction, 3, Action, FALSE); LUASCRIPT_CHECK_ARG_NIL(L, tgt, 4, Unit, FALSE); + fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, FALSE); fc_assert_ret_val(action_get_target_kind(paction) == ATK_UNIT, FALSE); fc_assert_ret_val(!action_has_result(paction, ACTRES_FOUND_CITY), FALSE); if (is_action_enabled_unit_on_unit(paction->id, punit, tgt)) { @@ -293,7 +297,7 @@ bool api_edit_unit_perform_action_vs_unit(lua_State *L, Unit *punit, /***********************************************************************//** Force a unit to perform an action against a tile. ***************************************************************************/ -bool api_edit_unit_perform_action_vs_tile(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_tile(lua_State *L, Unit *punit, Action *paction, Tile *tgt) { bool enabled = FALSE; @@ -303,6 +307,7 @@ bool api_edit_unit_perform_action_vs_tile(lua_State *L, Unit *punit, LUASCRIPT_CHECK_ARG_NIL(L, paction, 3, Action, FALSE); LUASCRIPT_CHECK_ARG_NIL(L, tgt, 4, Tile, FALSE); + fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, FALSE); switch (action_get_target_kind(paction)) { case ATK_UNITS: enabled = is_action_enabled_unit_on_units(paction->id, punit, tgt); @@ -347,7 +352,7 @@ bool api_edit_unit_perform_action_vs_tile(lua_State *L, Unit *punit, /***********************************************************************//** Force a unit to perform an action against a tile and an extra. ***************************************************************************/ -bool api_edit_unit_perform_action_vs_tile_extra(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_tile_extra(lua_State *L, Unit *punit, Action *paction, Tile *tgt, const char *sub_tgt) { @@ -363,6 +368,7 @@ bool api_edit_unit_perform_action_vs_tile_extra(lua_State *L, Unit *punit, sub_target = extra_type_by_rule_name(sub_tgt); LUASCRIPT_CHECK_ARG(L, sub_target != NULL, 5, "No such extra", FALSE); + fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, FALSE); switch (action_get_target_kind(paction)) { case ATK_UNITS: enabled = is_action_enabled_unit_on_units(paction->id, punit, tgt); @@ -407,13 +413,14 @@ bool api_edit_unit_perform_action_vs_tile_extra(lua_State *L, Unit *punit, /***********************************************************************//** Force a unit to perform an action against it self. ***************************************************************************/ -bool api_edit_unit_perform_action_vs_self(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_self(lua_State *L, Unit *punit, Action *paction) { LUASCRIPT_CHECK_STATE(L, FALSE); LUASCRIPT_CHECK_ARG_NIL(L, punit, 2, Unit, FALSE); LUASCRIPT_CHECK_ARG_NIL(L, paction, 3, Action, FALSE); + fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, FALSE); fc_assert_ret_val(action_get_target_kind(paction) == ATK_SELF, FALSE); fc_assert_ret_val(!action_has_result(paction, ACTRES_FOUND_CITY), FALSE); if (is_action_enabled_unit_on_self(paction->id, punit)) { diff --git a/server/scripting/api_server_edit.h b/server/scripting/api_server_edit.h index 85d14a5dfb..c25fc93eba 100644 --- a/server/scripting/api_server_edit.h +++ b/server/scripting/api_server_edit.h @@ -37,22 +37,22 @@ Unit *api_edit_create_unit_full(lua_State *L, Player *pplayer, Tile *ptile, Unit *ptransport); bool api_edit_unit_teleport(lua_State *L, Unit *punit, Tile *dest); -bool api_edit_unit_perform_action_vs_city(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_city(lua_State *L, Unit *punit, Action *paction, City *tgt); -bool api_edit_unit_perform_action_vs_city_impr(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_city_impr(lua_State *L, Unit *punit, Action *paction, City *tgt, Building_Type *sub_tgt); -bool api_edit_unit_perform_action_vs_city_tech(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_city_tech(lua_State *L, Unit *punit, Action *paction, City *tgt, Tech_Type *sub_tgt); -bool api_edit_unit_perform_action_vs_unit(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_unit(lua_State *L, Unit *punit, Action *paction, Unit *tgt); -bool api_edit_unit_perform_action_vs_tile(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_tile(lua_State *L, Unit *punit, Action *paction, Tile *tgt); -bool api_edit_unit_perform_action_vs_tile_extra(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_tile_extra(lua_State *L, Unit *punit, Action *paction, Tile *tgt, const char *sub_tgt); -bool api_edit_unit_perform_action_vs_self(lua_State *L, Unit *punit, +bool api_edit_perform_action_unit_vs_self(lua_State *L, Unit *punit, Action *paction); void api_edit_unit_turn(lua_State *L, Unit *punit, Direction dir); diff --git a/server/scripting/tolua_server.pkg b/server/scripting/tolua_server.pkg index ae2ed52ca6..e5f4264828 100644 --- a/server/scripting/tolua_server.pkg +++ b/server/scripting/tolua_server.pkg @@ -183,26 +183,26 @@ module edit { void api_edit_unit_moving_allow @ movement_allow(lua_State *L, Unit *self); - bool api_edit_unit_perform_action_vs_city - @ unit_perform_action(lua_State *L, Unit *punit, Action *paction, - City *tgt); - bool api_edit_unit_perform_action_vs_city_impr - @ unit_perform_action(lua_State *L, Unit *punit, Action *paction, - City *tgt, Building_Type *sub_tgt); - bool api_edit_unit_perform_action_vs_city_tech - @ unit_perform_action(lua_State *L, Unit *punit, Action *paction, - City *tgt, Tech_Type *sub_tgt); - bool api_edit_unit_perform_action_vs_unit - @ unit_perform_action(lua_State *L, Unit *punit, Action *paction, - Unit *tgt); - bool api_edit_unit_perform_action_vs_tile - @ unit_perform_action(lua_State *L, Unit *punit, Action *paction, - Tile *tgt); - bool api_edit_unit_perform_action_vs_tile_extra - @ unit_perform_action(lua_State *L, Unit *punit, Action *paction, - Tile *tgt, const char *sub_tgt); - bool api_edit_unit_perform_action_vs_self - @ unit_perform_action(lua_State *L, Unit *punit, Action *paction); + bool api_edit_perform_action_unit_vs_city + @ perform_action(lua_State *L, Unit *punit, Action *paction, + City *tgt); + bool api_edit_perform_action_unit_vs_city_impr + @ perform_action(lua_State *L, Unit *punit, Action *paction, + City *tgt, Building_Type *sub_tgt); + bool api_edit_perform_action_unit_vs_city_tech + @ perform_action(lua_State *L, Unit *punit, Action *paction, + City *tgt, Tech_Type *sub_tgt); + bool api_edit_perform_action_unit_vs_unit + @ perform_action(lua_State *L, Unit *punit, Action *paction, + Unit *tgt); + bool api_edit_perform_action_unit_vs_tile + @ perform_action(lua_State *L, Unit *punit, Action *paction, + Tile *tgt); + bool api_edit_perform_action_unit_vs_tile_extra + @ perform_action(lua_State *L, Unit *punit, Action *paction, + Tile *tgt, const char *sub_tgt); + bool api_edit_perform_action_unit_vs_self + @ perform_action(lua_State *L, Unit *punit, Action *paction); void api_edit_city_add_history @ add_city_history(lua_State *L, City *self, int amount); @@ -357,11 +357,11 @@ end function Unit:perform_action(action, target, sub_target) if target == nil then - return edit.unit_perform_action(self, action) + return edit.perform_action(self, action) elseif sub_target == nil then - return edit.unit_perform_action(self, action, target) + return edit.perform_action(self, action, target) else - return edit.unit_perform_action(self, action, target, sub_target) + return edit.perform_action(self, action, target, sub_target) end end -- 2.20.1