From 6fbed01c2a82a1bf8358ff662b3304825abfe5f6 Mon Sep 17 00:00:00 2001 From: XHawk87 Date: Sat, 18 Jan 2025 05:40:35 +0000 Subject: [PATCH] #2307 Moved city looting to default.lua --- data/default/default.lua | 34 ++++++++++++ server/citytools.cpp | 78 +++++++--------------------- server/scripting/api_server_edit.cpp | 2 + server/scripting/script_server.cpp | 2 + 4 files changed, 58 insertions(+), 58 deletions(-) diff --git a/data/default/default.lua b/data/default/default.lua index f5cfc7f433..6403475c62 100644 --- a/data/default/default.lua +++ b/data/default/default.lua @@ -227,3 +227,37 @@ function _deflua_harmless_disaster_message(disaster, city, had_internal_effect) end signal.connect("disaster_occurred", "_deflua_harmless_disaster_message") + +function _deflua_city_conquer_gold_loot(city, looterunit) + local treasury = city.owner:gold() + local loot = math.min( + treasury, + math.floor(random(0, (treasury / 20) + 1)) + + math.floor((treasury * city.size) / 200) + ) + if loot <= 0 then + return false + end + looterunit.owner:change_gold(loot) + city.owner:change_gold(-loot) + notify.event(looterunit.owner, city.tile, E.UNIT_WIN_ATT, + string.format( + PL_("Your lootings from %s accumulate to %d gold!", + "Your lootings from %s accumulate to %d gold!", + loot), + city:link_text(), loot + ) + ) + notify.event(city.owner, city.tile, E.CITY_LOST, + string.format( + PL_("%s looted %d gold from %s.", + "%s looted %d gold from %s.", + loot), + looterunit.owner.name, loot, city:link_text() + ) + ) + return true +end + +signal.connect("city_loot", "_deflua_city_conquer_gold_loot") + diff --git a/server/citytools.cpp b/server/citytools.cpp index aba5ffbb49..47bb8cd1c4 100644 --- a/server/citytools.cpp +++ b/server/citytools.cpp @@ -1969,7 +1969,7 @@ void remove_city(struct city *pcity) bool unit_conquer_city(struct unit *punit, struct city *pcity) { bool try_civil_war = false; - bool city_remains; + int pcity_id = pcity->id; struct player *pplayer = unit_owner(punit); struct player *cplayer = city_owner(pcity); @@ -2013,8 +2013,6 @@ bool unit_conquer_city(struct unit *punit, struct city *pcity) * the city will be destroyed. */ if (city_size_get(pcity) <= 1) { - int saved_id = pcity->id; - notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server, _("You destroy %s completely."), city_tile_link(pcity)); notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server, @@ -2023,7 +2021,7 @@ bool unit_conquer_city(struct unit *punit, struct city *pcity) script_server_signal_emit("city_destroyed", pcity, cplayer, pplayer); // We cant't be sure of city existence after running some script - if (city_exist(saved_id)) { + if (city_exist(pcity_id)) { remove_city(pcity); } @@ -2033,62 +2031,25 @@ bool unit_conquer_city(struct unit *punit, struct city *pcity) return true; } - auto coins = cplayer->economic.gold; - coins = MIN(coins, int(fc_rand((coins / 20) + 1)) - + (coins * (city_size_get(pcity))) / 200); - pplayer->economic.gold += coins; - cplayer->economic.gold -= coins; - send_player_info_c(pplayer, pplayer->connections); - send_player_info_c(cplayer, cplayer->connections); if (pcity->original != pplayer) { - if (coins > 0) { - notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server, - PL_("You conquer %s; your lootings accumulate" - " to %d gold!", - "You conquer %s; your lootings accumulate" - " to %d gold!", - coins), - city_link(pcity), coins); - notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server, - PL_("%s conquered %s and looted %d gold" - " from the city.", - "%s conquered %s and looted %d gold" - " from the city.", - coins), - player_name(pplayer), city_link(pcity), coins); - } else { - notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server, - _("You conquer %s."), city_link(pcity)); - notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server, - _("%s conquered %s."), player_name(pplayer), - city_link(pcity)); - } + notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server, + _("You conquer %s."), city_link(pcity)); + notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server, + _("%s conquered %s."), player_name(pplayer), + city_link(pcity)); } else { - if (coins > 0) { - notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server, - PL_("You have liberated %s!" - " Lootings accumulate to %d gold.", - "You have liberated %s!" - " Lootings accumulate to %d gold.", - coins), - city_link(pcity), coins); - notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server, - PL_("%s liberated %s and looted %d gold" - " from the city.", - "%s liberated %s and looted %d gold" - " from the city.", - coins), - player_name(pplayer), city_link(pcity), coins); - } else { - notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server, - _("You have liberated %s!"), city_link(pcity)); - notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server, - _("%s liberated %s."), player_name(pplayer), - city_link(pcity)); - } + notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server, + _("You have liberated %s!"), city_link(pcity)); + notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server, + _("%s liberated %s."), player_name(pplayer), + city_link(pcity)); } - if (fc_rand(100) < get_unit_bonus(punit, EFT_CONQUEST_TECH_PCT)) { + int conquestTechPct = get_unit_bonus(punit, EFT_CONQUEST_TECH_PCT); + script_server_signal_emit("city_loot", pcity, punit); + bool city_remains = city_exist(pcity_id); + + if (fc_rand(100) < conquestTechPct) { // Just try to steal. Ignore failures to get tech steal_a_tech(pplayer, cplayer, A_UNSET); } @@ -2097,8 +2058,9 @@ bool unit_conquer_city(struct unit *punit, struct city *pcity) * the size is reduced. */ /* FIXME: maybe it should be a ruleset option whether barbarians get * free buildings such as palaces? */ - city_remains = transfer_city(pplayer, pcity, 0, true, true, true, - !is_barbarian(pplayer)); + city_remains = city_remains + && transfer_city(pplayer, pcity, 0, true, true, true, + !is_barbarian(pplayer)); if (city_remains) { // reduce size should not destroy this city diff --git a/server/scripting/api_server_edit.cpp b/server/scripting/api_server_edit.cpp index 724bc3ea50..ac88b0a52f 100644 --- a/server/scripting/api_server_edit.cpp +++ b/server/scripting/api_server_edit.cpp @@ -349,6 +349,8 @@ void api_edit_change_gold(lua_State *L, Player *pplayer, int amount) LUASCRIPT_CHECK_ARG_NIL(L, pplayer, 2, Player); pplayer->economic.gold = MAX(0, pplayer->economic.gold + amount); + + send_player_info_c(pplayer, pplayer->connections); } /** diff --git a/server/scripting/script_server.cpp b/server/scripting/script_server.cpp index a2fd38182a..e57de19f52 100644 --- a/server/scripting/script_server.cpp +++ b/server/scripting/script_server.cpp @@ -451,6 +451,8 @@ static void script_server_signals_create() // First player is city owner, second is enemy. luascript_signal_create(fcl_main, "city_destroyed", 3, API_TYPE_CITY, API_TYPE_PLAYER, API_TYPE_PLAYER); + luascript_signal_create(fcl_main, "city_loot", 2, API_TYPE_CITY, + API_TYPE_UNIT); // First player is former owner, second new one. luascript_signal_create(fcl_main, "city_transferred", 4, API_TYPE_CITY,