From 127cb7b1a2370d22fe07f418bf0c11ab97f28b93 Mon Sep 17 00:00:00 2001 From: Ihnatus Date: Thu, 22 Sep 2022 22:13:44 +0300 Subject: [PATCH] Lua: make (Player):give_bulbs() optionally targeted Mostly for multiresearch mode but usable in classic mode too. See OSDN#44936 Signed-off-by: Ihnatus --- server/scripting/api_server_edit.c | 38 ++++++++++++++++++++++++++---- server/scripting/api_server_edit.h | 3 ++- server/scripting/tolua_server.pkg | 7 +++--- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/server/scripting/api_server_edit.c b/server/scripting/api_server_edit.c index 49c6a67eb6..bd7ef527fe 100644 --- a/server/scripting/api_server_edit.c +++ b/server/scripting/api_server_edit.c @@ -1087,14 +1087,44 @@ 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 not currently in research is specified, + tech known state won't immediately change; out of multiresearch mode, + this function in this case resets the "previous" tech and its bulbs. **************************************************************************/ -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); + if (!tech || advance_number(tech) == presearch->researching) { + update_bulbs(pplayer, amount, TRUE); + /* Currently, multiresearch data are not sent to clients, + * so do it only here */ + send_research_info(presearch, NULL); + return; + } - send_research_info(research_get(pplayer), NULL); + /* Sometimes may get negative, it's normal though lurking */ + if (game.server.multiresearch) { + presearch->inventions[advance_number(tech)].bulbs_researched_saved + += amount; + } else { + /* 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. */ + presearch->researching_saved = advance_number(tech); + presearch->bulbs_researching_saved = amount; + if (!presearch->got_tech) { + /* Modify current bulbs. Otherwise, switching back and forth + * would change them that is too lurking. */ + int newcb = amount - amount * game.server.techpenalty / 100; + + presearch->bulbs_researched = MAX(newcb, 0); + } + } } 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 ac62a1a426..6c2deec163 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