Skip to content

Commit

Permalink
Merge pull request #113 from ineveraskedforthis/master
Browse files Browse the repository at this point in the history
Raid and patrol direct actions.
  • Loading branch information
ineveraskedforthis authored Oct 30, 2023
2 parents 64f6747 + 5b90a32 commit 4ef765a
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 16 deletions.
2 changes: 1 addition & 1 deletion sote/game/entities/pop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
---@field age number
---@field name string
---@field savings number
---@field popularity table<Realm, number>
---@field popularity table<Realm, number|nil>
---@field traits table<Trait, Trait>
---@field employer Building?
---@field loyalty POP?
Expand Down
9 changes: 6 additions & 3 deletions sote/game/entities/realm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ local pv = require "game.raws.values.political"
---@class RewardFlag
---@field flag_type 'explore'|'raid'|'destroy'|'kill'|'devastate'
---@field reward number
-- -@field reward_per_unit number
-- -@field loot_share_to_owner number
---@field owner Character
---@field target Province

Expand Down Expand Up @@ -85,7 +83,7 @@ end
---@field tributaries table<Realm, Realm>
---@field provinces table<Province, Province>
---@field reward_flags table<RewardFlag, RewardFlag>
---@field raiders_preparing table<RewardFlag, table<Warband, Warband>>
---@field raiders_preparing table<RewardFlag, table<Warband, Warband>?>
---@field patrols table<Province, table<Warband, Warband>>
---@field prepare_attack_flag boolean?
---@field add_reward_flag fun(self:Realm, target:RewardFlag)
Expand Down Expand Up @@ -246,6 +244,11 @@ end

function realm.Realm:remove_reward_flag(f)
self.reward_flags[f] = nil

if self.raiders_preparing[f] == nil then
return
end

for _, warband in pairs(self.raiders_preparing[f]) do
if warband.status == 'preparing_raid' then
warband.status = 'idle'
Expand Down
6 changes: 4 additions & 2 deletions sote/game/raws/effects/economic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ end
---@param reason EconomicReason
function EconomicEffects.add_pop_savings(pop, x, reason)
pop.savings = pop.savings + x
EconomicEffects.display_character_savings_change(pop, x, reason)
if x > 0 then
EconomicEffects.display_character_savings_change(pop, x, reason)
end
end

function EconomicEffects.display_character_savings_change(pop, x, reason)
Expand Down Expand Up @@ -352,7 +354,7 @@ function EconomicEffects.gift_to_tribe(character, realm, amount)
EconomicEffects.change_treasury(realm, amount, EconomicEffects.reasons.Donation)

realm.capitol.mood = realm.capitol.mood + amount / realm.capitol:population() / 100
character.popularity[realm] = character.popularity[realm] + amount / realm.capitol:population() / 100
character.popularity[realm] = (character.popularity[realm] or 0) + amount / realm.capitol:population() / 100
end

return EconomicEffects
58 changes: 57 additions & 1 deletion sote/game/raws/effects/military.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local path = require "game.ai.pathfinding"
local ui_utils = require "game.ui-utils"

local RewardFlag = require "game.entities.realm".RewardFlag

MilitaryEffects = {}

---Gathers new warband in the name of *leader*
Expand Down Expand Up @@ -114,7 +116,61 @@ function MilitaryEffects.covert_raid(root, primary_target)

WORLD:emit_action(
"covert-raid",
target_province.realm.leader,
primary_target.owner,
raid_data,
travel_time, false
)
end


---comment
---@param root Character
---@param primary_target Province
function MilitaryEffects.covert_raid_no_reward(root, primary_target)
local warband = root.leading_warband
if warband == nil then return end

local target = RewardFlag:new({
flag_type = 'raid',
owner = root,
reward = 0,
target = primary_target
})

local travel_time, _ = path.hours_to_travel_days(path.pathfind(root.province, primary_target))

if WORLD:does_player_see_realm_news(root.realm) then
WORLD:emit_notification(root.name .. " is leading his warriors move toward " ..
primary_target.name ..
", they should arrive in " ..
math.floor(travel_time + 0.5) .. " days. We can expect to hear back from them in " .. math.floor(travel_time * 2 + 0.5) .. " days.")
end

-- A raid will raise up to a certain number of troops
-- local max_covert_raid_size = 10
---@type table<Warband, Warband>
local warbands = {}
warbands[warband] = warband
local army = root.realm:raise_army(warbands)
army.destination = primary_target

for _, warband in pairs(army.warbands) do
warband.status = "raiding"
end

---@type RaidData
local raid_data = {
raider = root,
target = target,
travel_time = travel_time,
army = army,
origin = root.realm
}


WORLD:emit_action(
"covert-raid",
root,
raid_data,
travel_time, false
)
Expand Down
41 changes: 33 additions & 8 deletions sote/game/raws/events/raid-events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ local function load()
real_loot = real_loot + extra
end

local mood_swing = real_loot / (province:population() + 1)
province.mood = province.mood - mood_swing
raider.popularity[realm] = (raider.popularity[realm] or 0) - mood_swing * 2

---@type RaidResultSuccess
local success_data = { army = army, target = target, loot = real_loot, losses = losses, raider = raider, origin = origin }
WORLD:emit_action("covert-raid-success", raider,
Expand Down Expand Up @@ -490,34 +494,55 @@ local function load()
local army = associated_data.army

local warbands = realm:disband_army(army)
realm.capitol.mood = realm.capitol.mood + 1 / (3 * math.max(0, realm.capitol.mood) + 1)

local mood_swing = loot / (realm.capitol:population() + 1) / 2

-- popularity to raid initiator
pe.small_popularity_boost(target.owner, target.owner.realm)
pe.change_popularity(target.owner, realm, mood_swing)

-- improve mood in a province
realm.capitol.mood = realm.capitol.mood + mood_swing

-- popularity to raid participants
local num_of_warbands = 0
for _, w in pairs(warbands) do
pe.small_popularity_boost(w.leader, target.owner.realm)
pe.change_popularity(w.leader, target.owner.realm, mood_swing / tabb.size(warbands))
num_of_warbands = num_of_warbands + 1
end

-- initiator gets 2 "coins" for each invested "coin"
-- so one part of loot goes to pay his expenses on raid
-- second part of loot goes to his income
-- remaining half goes to population
local total_loot = loot
local initiator_share = loot * 0.5
if initiator_share / 2 > target.reward then
initiator_share = target.reward * 2
end

-- reward part
-- if reward was 0 then this stage does nothing

-- pay share to raid initiator
ef.add_pop_savings(target.owner, initiator_share, ef.reasons.Raid)
ef.add_pop_savings(target.owner, initiator_share, ef.reasons.RewardFlag)
loot = loot - initiator_share

-- pay rewards to warband leaders
target.reward = target.reward - initiator_share / 2
for _, w in pairs(warbands) do
ef.add_pop_savings(w.leader, initiator_share / 2 / num_of_warbands, ef.reasons.Raid)
ef.add_pop_savings(w.leader, initiator_share / 2 / num_of_warbands, ef.reasons.RewardFlag)
end
loot = loot - initiator_share / 2

-- remained raided wealth part

-- half of remaining loot goes again to raid_initiator as spoils of war
ef.add_pop_savings(target.owner, loot / 2, ef.reasons.Raid)
loot = loot - loot / 2

-- half of remaining loot goes to warband leaders
for _, w in pairs(warbands) do
ef.add_pop_savings(w.leader, loot / 2 / num_of_warbands, ef.reasons.Raid)
end
loot = loot - loot / 2

-- pay the remaining half of loot to population(warriors)
target.owner.province.local_wealth = target.owner.province.local_wealth + loot

Expand Down
51 changes: 50 additions & 1 deletion sote/game/scenes/game/tile-inspector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ local tabb = require "engine.table"

local ef = require "game.raws.effects.economic"

local military_effects = require "game.raws.effects.military"

re.cached_scrollbar = 0

---@return Rect
Expand Down Expand Up @@ -64,8 +66,10 @@ function re.draw(gam)

uit.data_entry("", tile.province.name, province_name_rect)

local player = WORLD.player_character

if tile.province.realm then
if WORLD.player_character == nil then
if player == nil then
local bp = panel:subrect(-UI_STYLE.square_button_large, 0, UI_STYLE.square_button_large, UI_STYLE.square_button_large, "right", "up")
if uit.icon_button(ASSETS.icons['frog-prince.png'], bp, "Take control over character from this country") then
-- gam.refresh_map_mode()
Expand All @@ -80,6 +84,51 @@ function re.draw(gam)
gam.inspector = "market"
end

if tile.province.realm then
if player then
local raid_rect = top_bar_rect:subrect(-3 * UI_STYLE.square_button_large, 0, UI_STYLE.square_button_large, UI_STYLE.square_button_large, "right", 'up')
local patrol_rect = top_bar_rect:subrect(-4 * UI_STYLE.square_button_large, 0, UI_STYLE.square_button_large, UI_STYLE.square_button_large, "right", 'up')

local potential = true
local warband = player.leading_warband
local tooltip = "Raid this province"
local patrol_tooltip = "Patrol this province"
if warband == nil then
potential = false
tooltip = "You are not a leader of a warband."
patrol_tooltip = "You are not a leader of a warband."
end
if warband and warband.status ~= 'idle' then
potential = false
tooltip = "Your warband is busy."
patrol_tooltip = "Your warband is busy."
end

if uit.icon_button(
ASSETS.icons['stone-spear.png'],
raid_rect,
tooltip,
potential
) then
military_effects.covert_raid_no_reward(player, tile.province)
end

if tile.province.realm ~= player.realm then
potential = false
patrol_tooltip = 'You can\'t patrol provinces of other realms'
end

if uit.icon_button(
ASSETS.icons['round-shield.png'],
patrol_rect,
patrol_tooltip,
potential
) then
player.realm:add_patrol(tile.province, player.leading_warband)
end
end
end


local infra_panel = panel:subrect(base_unit * 7, 0, base_unit * 3, base_unit, "left", 'up')
uit.generic_number_field(
Expand Down

0 comments on commit 4ef765a

Please sign in to comment.