Skip to content

Commit

Permalink
#2307 Moved city looting to default.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
XHawk87 authored and jwrober committed Jan 19, 2025
1 parent 73117ba commit 6fbed01
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
34 changes: 34 additions & 0 deletions data/default/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

78 changes: 20 additions & 58 deletions server/citytools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions server/scripting/api_server_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions server/scripting/script_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6fbed01

Please sign in to comment.