WC: New carryover implementation.

We now use a modified default carryover calculation,
the main advantage is it now uses the normal carryover
message on endlevel, and shows how it is calculated.
This commit is contained in:
gfgtdf 2023-09-05 14:44:49 +02:00
parent 06d1fade32
commit c1c0111d3c
2 changed files with 66 additions and 32 deletions

View file

@ -1,6 +1,10 @@
--<<
local _ = wesnoth.textdomain 'wesnoth-wc'
local _wesnoth = wesnoth.textdomain "wesnoth"
local wc2_scenario = {}
local on_event = wesnoth.require("on_event")
local carryover = wesnoth.require("carryover_gold.lua")
function wc2_scenario.is_human_side(side_num)
return side_num <= wml.variables.wc2_player_count
@ -42,18 +46,52 @@ function wesnoth.wml_actions.wc2_start_units(cfg)
end
end
function wesnoth.wml_actions.wc2_store_carryover(cfg)
local human_sides = wesnoth.sides.find(wml.get_child(cfg, "sides"))
--use an the average amount of villages for this scenario to stay independent of map generator results.
local nvillages = cfg.nvillages
local turns_left = math.max(wesnoth.scenario.turns - wesnoth.current.turn, 0)
local player_gold = 0
function wc2_scenario.average_gold()
local total_gold = 0
local nsides = 0
for side_num, side in ipairs(human_sides) do
player_gold = player_gold + side.gold
for i, s in ipairs(wesnoth.sides) do
if wc2_scenario.is_human_side(i) then
nsides = nsides + 1
total_gold = total_gold + s.gold
end
end
player_gold = math.max(player_gold / #human_sides, 0)
wml.variables.wc2_carryover = math.ceil( (nvillages*turns_left + player_gold) * 0.15)
return math.floor(total_gold / nsides + 0.5)
end
-- overwrite parts of the carryover gold implementation.
function carryover.set_side_carryover_gold(side)
local turns_left = carryover.turns_left()
-- make the carryover bonus independent of the map generation.
local num_villages = wml.variables.wc2_nvillages or carryover.get_num_villages()
local finishing_bonus_per_turn = wml.variables.wc2_early_victory_bonus or num_villages * side.village_gold + side.base_income
local finishing_bonus = finishing_bonus_per_turn * turns_left
local avg_gold = wc2_scenario.average_gold()
side.carryover_gold = math.ceil((avg_gold + finishing_bonus) * side.carryover_percentage / 100)
return {
turns_left = turns_left,
avg_gold = avg_gold,
finishing_bonus = finishing_bonus,
finishing_bonus_per_turn = finishing_bonus_per_turn,
}
end
---@param side side
---@param info table
---@return string
function carryover.remaining_gold_message(side, info)
return "<small>\n" .. _wesnoth("Remaining gold: ") .. carryover.half_signed_value(side.gold) .. "</small>"
.. "<small>\n" .. _("Average remaining gold: ") .. carryover.half_signed_value(info.avg_gold) .. "</small>"
end
---@param side side
---@param info table
---@return string
function carryover.total_gold_message(side, info)
return "<small>" .. _wesnoth("Total gold: ") .. carryover.half_signed_value(info.avg_gold + info.finishing_bonus) .. "</small>"
end
-- carryover handling: we use a custom carryover machnics that
@ -81,11 +119,10 @@ on_event("wc2_start", function(cx)
end
end
local gold = (wml.variables.wc2_carryover or 0) + (wml.variables["wc2_difficulty.extra_gold"] or 0)
local gold = (wml.variables["wc2_difficulty.extra_gold"] or 0)
for i = 1, wml.variables.wc2_player_count do
wesnoth.sides[i].gold = wesnoth.sides[i].gold + gold
end
wml.variables.wc2_carryover = nil
end)
-- our victory condition
@ -96,9 +133,7 @@ on_event("enemies defeated", function(cx)
wesnoth.audio.play("ambient/ship.ogg")
wesnoth.wml_actions.endlevel {
result = "victory",
carryover_percentage = 0,
carryover_add = false,
carryover_report = false,
carryover_report = true,
}
end)

View file

@ -10,7 +10,7 @@ wesnoth.dofile("./scenario_utils/plot.lua")
wesnoth.dofile("./scenario_utils/side_definitions.lua")
settings = globals.settings or {}
local n_villages = {27, 40, 53, 63}
local early_victory_bonus = {27, 40, 53, 63}
local function get_map_generator(scenario_data)
if globals.settings.default_id then
@ -45,7 +45,9 @@ function wc_ii_generate_scenario(nplayers, gen_args)
std_print("test_nplayers", wml.variables.test_nplayers)
local scenario_data = get_scenario_data(nplayers, scenario_num)
local prestart_event = { name = "prestart" }
local prestart_event = {
name = "prestart",
}
-- our [scenario] skeleton
local scenario = {
event = {
@ -71,6 +73,7 @@ function wc_ii_generate_scenario(nplayers, gen_args)
description="enables the buildin mod to mark units, disable this to be compatible with other mods that do the same thing",
},
},
-- note: in later scenarios these variables will probably be overwritten by whatever is present in the carryover.
variables = {
wc2_scenario = scenario_num,
wc2_player_count = nplayers,
@ -81,6 +84,8 @@ function wc_ii_generate_scenario(nplayers, gen_args)
description = "WC_II_" .. nplayers .. "p_desc",
modify_placing = false,
turns = scenario_data.turns,
carryover_percentage = 15,
carryover_add = true,
}
-- add [side]s to the [scenario]
@ -90,21 +95,6 @@ function wc_ii_generate_scenario(nplayers, gen_args)
-- add plot (that is [event] with [message]s)
add_plot(scenario, scenario_num, nplayers)
-- add the gold carryover event
if scenario_num < #n_villages then
table.insert(scenario.event, {
name = "victory",
wml.tag.wc2_store_carryover {
nvillages = n_villages[scenario_num] + 2 * nplayers,
wml.tag.sides {
side="1,2,3",
wml.tag.has_unit {
}
}
}
})
end
-- add some wc2 specific wml [event]s
for side_num = 1, nplayers do
table.insert(scenario.event, {
@ -117,6 +107,15 @@ function wc_ii_generate_scenario(nplayers, gen_args)
})
end
if early_victory_bonus[scenario_num] then
table.insert(prestart_event,
wml.tag.set_variable {
name = "wc2_early_victory_bonus",
value = early_victory_bonus[scenario_num] + 2 * nplayers,
}
)
end
-- generate the map. (this also adds certain events for example to create [item]s or [sound_source]s)
local mapgen_func = get_map_generator(scenario_data)
mapgen_func(scenario, nplayers)