From 39bf7b76db116e5716b9a9b3c99b65570eeaaafe Mon Sep 17 00:00:00 2001 From: Ihnatus Date: Fri, 14 Oct 2022 21:33:24 +0300 Subject: [PATCH] Lua: make (Player):give_bulbs() optionally targeted In multiresearch mode, manipulates saved bulbs per technology. Out of it, allows to rewrite the tech saved from the previous turn. See OSDN#44936 Signed-off-by: Ihnatus --- server/scripting/api_server_edit.c | 56 +++++++++++++++++++++++++++--- server/scripting/api_server_edit.h | 3 +- server/scripting/tolua_server.pkg | 7 ++-- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/server/scripting/api_server_edit.c b/server/scripting/api_server_edit.c index 623a72ce39..5993e675c4 100644 --- a/server/scripting/api_server_edit.c +++ b/server/scripting/api_server_edit.c @@ -1087,14 +1087,62 @@ void api_edit_player_add_history(lua_State *L, Player *pplayer, int amount) } /**********************************************************************//** - Give bulbs to a player + Give bulbs to a player, optionally towards a specific tech. + If a tech that is not currently in research is specified, + tech known state will not immediately change. + Out of multiresearch mode, when tech is not NULL, + this function sets the "previous" tech (or clears it if the current + one is specified); in the case if a new "previous" tech is set, + all non-free bulbs of the old "previous" tech are cleared, + and if necessary the bulbs on stock are adjusted (for clear switching) + as like amount was the previously researched value. **************************************************************************/ -void api_edit_player_give_bulbs(lua_State *L, Player *pplayer, int amount) +void api_edit_player_give_bulbs(lua_State *L, Player *pplayer, int amount, + Tech_Type *tech) { + struct research *presearch; + LUASCRIPT_CHECK_STATE(L); LUASCRIPT_CHECK_SELF(L, pplayer); + presearch = research_get(pplayer); + fc_assert_ret(presearch); - update_bulbs(pplayer, amount, TRUE, TRUE); + if (!tech) { + update_bulbs(pplayer, amount, TRUE, TRUE); - send_research_info(research_get(pplayer), NULL); + send_research_info(presearch, NULL); + } else if (advance_number(tech) == presearch->researching) { + update_bulbs(pplayer, amount, TRUE, FALSE); + /* Clean the saved tech to get no surprizes switching */ + presearch->researching_saved = A_UNKNOWN; + + send_research_info(presearch, NULL); + } else { + /* Sometimes we may set negative bulbs, it's normal though lurking */ + if (game.server.multiresearch) { + presearch->inventions[advance_number(tech)].bulbs_researched_saved + += amount; + /* Currently, multiresearch data are not sent to clients */ + } else { + int oldb = presearch->bulbs_researched; + + /* NOTE: We can set a tech we already know / can't research here. + * Probably it's safe as we can't switch into it any way. */ + if (presearch->researching_saved != advance_number(tech)) { + presearch->researching_saved = advance_number(tech); + presearch->bulbs_researching_saved + = amount + presearch->free_bulbs; + } else { + presearch->bulbs_researching_saved + += amount + presearch->free_bulbs; + } + /* For consistency, modify current bulbs alongside + * (sometimes getting into "overresearch" situation, but ok) */ + presearch->bulbs_researched = amount + - amount * game.server.techpenalty / 100 + presearch->free_bulbs; + if (oldb != presearch->bulbs_researched) { + send_research_info(presearch, NULL); + } + } + } } diff --git a/server/scripting/api_server_edit.h b/server/scripting/api_server_edit.h index 63d1d4d019..bcbed0ff71 100644 --- a/server/scripting/api_server_edit.h +++ b/server/scripting/api_server_edit.h @@ -112,6 +112,7 @@ void api_edit_unit_moving_allow(lua_State *L, Unit *punit); void api_edit_city_add_history(lua_State *L, City *pcity, int amount); void api_edit_player_add_history(lua_State *L, Player *pplayer, int amount); -void api_edit_player_give_bulbs(lua_State *L, Player *pplayer, int amount); +void api_edit_player_give_bulbs(lua_State *L, Player *pplayer, int amount, + Tech_Type *tech); #endif /* API_SERVER_EDIT_H */ diff --git a/server/scripting/tolua_server.pkg b/server/scripting/tolua_server.pkg index 204c82aeff..5032ccb840 100644 --- a/server/scripting/tolua_server.pkg +++ b/server/scripting/tolua_server.pkg @@ -226,7 +226,8 @@ module edit { @ add_player_history(lua_State *L, Player *self, int amount); void api_edit_player_give_bulbs - @ give_bulbs(lua_State *L, Player *self, int amount); + @ give_bulbs(lua_State *L, Player *self, int amount, + Tech_Type *tech = NULL); } /* Luadata module. */ @@ -364,8 +365,8 @@ function Player:add_history(amount) edit.add_player_history(self, amount) end -function Player:give_bulbs(amount) - edit.give_bulbs(self, amount) +function Player:give_bulbs(amount, tech) + edit.give_bulbs(self, amount, tech) end -- Server functions for City module -- 2.34.1