Skip to content

Commit

Permalink
rewards rework
Browse files Browse the repository at this point in the history
  • Loading branch information
ineveraskedforthis committed Jan 9, 2024
1 parent 3a43b24 commit 7ac8c7a
Show file tree
Hide file tree
Showing 49 changed files with 1,562 additions and 1,660 deletions.
21 changes: 9 additions & 12 deletions sote/engine/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1379,30 +1379,27 @@ end
---@field slider_level number
---@field header_height number

---@class TableColumn
---@field render_closure fun(rect: Rect, k:TableKey, v:TableEntry)
---@field header string
---@field width number
---@field value (fun(k: TableKey, v: TableEntry): TableField)
---@field active? boolean

---@alias TableField number|string

---@class TableColumn<TableEntry>: {render_closure: fun(rect: Rect, k:TableKey, v:TableEntry), header: string, width: number, value: (fun(k: TableKey, v: TableEntry): TableField), active: boolean|nil}

---@alias TableField number|string
---@alias TableKey table|string
---@alias TableEntry table
---@alias TablePair {key: TableKey, value: TableEntry}

---@class TablePair<TableEntry>: {key: TableKey, value: TableEntry}

---TABLE
---Renders a sortable table with header and scroll. Mutates state in place.
---@generic T : table
---@param rect Rect
---@param data table<TableKey, TableEntry>
---@param columns TableColumn[]
---@param data table<TableKey, T>
---@param columns TableColumn<T>[]
---@param state TableState
---@param circle_style boolean?
---@param slider_arrow_images ButtonImagesSet?
function ui.table(rect, data, columns, state, circle_style, slider_arrow_images)
--- data sorting
---@type TablePair[]
---@type TablePair<T>[]
local sorted_data = {}
for _, entry in pairs(data) do
table.insert(sorted_data, {key = _, value = entry})
Expand Down
23 changes: 12 additions & 11 deletions sote/game/ai/construction.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local tabb = require "engine.table"
local ai = require "game.raws.values.ai_preferences"
local effects = require "game.raws.effects.economic"
local eco_values = require "game.raws.values.economical"
local economic_effects = require "game.raws.effects.economic"
local pv = require "game.raws.values.political"

local co = {}
Expand Down Expand Up @@ -148,15 +149,15 @@ local function construction_in_province(province, funds, excess, owner, overseer
end
end

if WORLD.player_character then
if WORLD.player_character.province == province then
print('____')
print("building target: ")
print(to_build.name)
print(exp_feature[to_build])
print(ROI_per_building_type[to_build])
end
end
-- if WORLD.player_character then
-- if WORLD.player_character.province == province then
-- print('____')
-- print("building target: ")
-- print(to_build.name)
-- print(exp_feature[to_build])
-- print(ROI_per_building_type[to_build])
-- end
-- end

-- if there's nothing to build, do not build
if to_build == nil then
Expand Down Expand Up @@ -188,7 +189,7 @@ local function construction_in_province(province, funds, excess, owner, overseer
local construction_cost = eco_values.building_cost(to_build, overseer, public_flag)
-- We can build! But only build if we have enough excess money to pay for the upkeep...
if excess >= to_build.upkeep then
EconomicEffects.construct_building(to_build, province, tile, owner)
economic_effects.construct_building(to_build, province, tile, owner)
funds = math.max(0, funds - construction_cost)
end
end
Expand Down Expand Up @@ -234,7 +235,7 @@ function co.run(realm)
end
end

EconomicEffects.change_treasury(realm, funds - realm.budget.treasury, EconomicEffects.reasons.Building)
economic_effects.change_treasury(realm, funds - realm.budget.treasury, economic_effects.reasons.Building)
end

return co
16 changes: 1 addition & 15 deletions sote/game/ai/exploration.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
local tabb = require "engine.table"
local EconomicEffects = require "game.raws.effects.economic"
local economic_effects = require "game.raws.effects.economic"
local ex = {}

---@param realm Realm
function ex.run(realm)
-- Only attempt exploration once a year by default
-- if love.math.random() < 1.0 / 12.0 then
-- local cc = tabb.size(realm.known_provinces)
-- local prov = tabb.nth(realm.known_provinces, love.math.random(cc))

-- if prov then
-- local cost = realm:get_explore_cost(prov)
-- if realm.budget.treasury > cost then
-- -- if love.math.random() < cost / realm.treasury then
-- EconomicEffects.change_treasury(realm, -cost, EconomicEffects.reasons.Exploration)
-- realm:explore(prov)
-- -- end
-- end
-- end
-- end
end

return ex
17 changes: 9 additions & 8 deletions sote/game/ai/treasury.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local tabb = require "engine.table"
local economic_effects = require "game.raws.effects.economic"

local tr = {}

Expand All @@ -7,15 +8,15 @@ function tr.run(realm)
-- for now ai will be pretty static
-- it would be nice to tie it to external threats/conditions
if tabb.size(realm.paying_tribute_to) == 0 then
EconomicEffects.set_court_budget(realm, 0.1)
EconomicEffects.set_infrastructure_budget(realm, 0.1)
EconomicEffects.set_education_budget(realm, 0.3)
EconomicEffects.set_military_budget(realm, 0.4)
economic_effects.set_court_budget(realm, 0.1)
economic_effects.set_infrastructure_budget(realm, 0.1)
economic_effects.set_education_budget(realm, 0.3)
economic_effects.set_military_budget(realm, 0.4)
else
EconomicEffects.set_court_budget(realm, 0.1)
EconomicEffects.set_infrastructure_budget(realm, 0.1)
EconomicEffects.set_education_budget(realm, 0.3)
EconomicEffects.set_military_budget(realm, 0.2)
economic_effects.set_court_budget(realm, 0.1)
economic_effects.set_infrastructure_budget(realm, 0.1)
economic_effects.set_education_budget(realm, 0.3)
economic_effects.set_military_budget(realm, 0.2)
end

realm.budget.treasury_target = 250
Expand Down
2 changes: 1 addition & 1 deletion sote/game/economy/realm-economic-update.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function rea.run(realm)
)
end

EconomicEffects.change_local_price(province, good_reference, price_derivative * INTEGRATION_STEP)
economic_effects.change_local_price(province, good_reference, price_derivative * INTEGRATION_STEP)
end
end
end
Expand Down
105 changes: 10 additions & 95 deletions sote/game/entities/realm.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
local pv = require "game.raws.values.political"

---@class RewardFlag
---@field flag_type 'explore'|'raid'|'destroy'|'kill'|'devastate'
---@field reward number
---@field owner Character
---@field target Province


---@class BudgetCategory
---@field ratio number
---@field budget number
Expand Down Expand Up @@ -78,8 +71,9 @@ end
---@field paying_tribute_to table<Realm, Realm>
---@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 quests_raid table<Province, nil|number> reward for raid
---@field quests_explore table<Province, nil|number> reward for exploration
---@field quests_patrol table<Province, nil|number> reward for patrol
---@field patrols table<Province, table<Warband, Warband>>
---@field capitol_guard Warband?
---@field prepare_attack_flag boolean?
Expand Down Expand Up @@ -125,8 +119,6 @@ end
---@field wars table<War, War> Wars
---@field at_war_with fun(self:Realm, other:Realm):boolean Wars
---@field get_warbands fun(self:Realm): Warband[]
---@field add_raider fun(self:Realm, f: RewardFlag, warband: Warband)
---@field remove_raider fun(self:Realm, f: RewardFlag, warband: Warband)
---@field add_patrol fun(self:Realm, prov: Province, warband: Warband)
---@field remove_patrol fun(self:Realm, prov: Province, warband: Warband)
---@field get_top_realm fun(self:Realm, sources:table<Realm, Realm> | nil, depth: number | nil):table<Realm, Realm> Returns the set of top dogs of a tributary chains. Handles loops.
Expand All @@ -136,23 +128,6 @@ end
local realm = {}
local tabb = require "engine.table"

---@class RewardFlag
realm.RewardFlag = {}
realm.RewardFlag.__index = realm.RewardFlag

---@param i RewardFlag
---@return RewardFlag
function realm.RewardFlag:new(i)
---@type table
local o = {}
---@diagnostic disable-next-line: no-unknown
for k, v in pairs(i) do
---@diagnostic disable-next-line: no-unknown
o[k] = v
end
setmetatable(o, realm.RewardFlag)
return o
end

---@class Realm
realm.Realm = {}
Expand All @@ -177,7 +152,6 @@ function realm.Realm:new()
o.paying_tribute_to = {}

o.provinces = {}
o.reward_flags = {}
o.bought = {}
o.sold = {}
o.known_provinces = {}
Expand All @@ -199,9 +173,12 @@ function realm.Realm:new()
o.resources = {}
o.production = {}
o.armies = {}
o.raiders_preparing = {}
o.patrols = {}

o.quests_explore = {}
o.quests_patrol = {}
o.quests_raid = {}

-- print("bb")
if love.math.random() < 0.6 then
o.coa_emblem_image = love.math.random(#ASSETS.emblems)
Expand Down Expand Up @@ -237,66 +214,11 @@ function realm.Realm:remove_province(prov)
end
end

---Adds a province to the realm's raiding targets.
---@param f RewardFlag
function realm.Realm:add_reward_flag(f)
self.reward_flags[f] = f
if self.raiders_preparing[f] == nil then
self.raiders_preparing[f] = {}
end
end

---Removes a province from the realm's raiding targets.
---@param f RewardFlag
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'
end
end
self.raiders_preparing[f] = {}
end

-- function realm.Realm:toggle_reward_flag(f)
-- if self.reward_flags[f] then
-- self:remove_reward_flag(f)
-- else
-- self:add_reward_flag(f)
-- end
-- end

function realm.Realm:get_random_province()
local n = tabb.size(self.provinces)
return tabb.nth(self.provinces, love.math.random(n))
end

---Adds warband as potential raider of province
---@param f RewardFlag
---@param warband Warband
function realm.Realm:add_raider(f, warband)
if warband.status ~= 'idle' then return end
if self.reward_flags[f] then
self.raiders_preparing[f][warband] = warband
warband.status = 'preparing_raid'
end
end

---Removes warband as potential raider of province
---@param f RewardFlag
---@param warband Warband
function realm.Realm:remove_raider(f, warband)
if self.reward_flags[f] then
self.raiders_preparing[f][warband] = nil
warband.status = "idle"
end
end

---Adds warband as potential raider of province
---@param prov Province
---@param warband Warband
Expand All @@ -322,16 +244,6 @@ function realm.Realm:remove_patrol(prov, warband)
end
end

function realm.Realm:roll_reward_flag()
---@type table<RewardFlag, number>
local targets = {}
for flag, k in pairs(self.reward_flags) do
local popularity = pv.popularity(k.owner, self)
targets[k] = k.reward * popularity
end
return tabb.random_select(targets)
end

---Adds a province to the explored provinces list.
---@param province Province
function realm.Realm:explore(province)
Expand Down Expand Up @@ -518,6 +430,9 @@ function realm.Realm:raise_warband(warband)
end
end

---Raise local army
---@param province Province
---@return Army
function realm.Realm:raise_local_army(province)
local army = require "game.entities.army":new()

Expand Down
5 changes: 4 additions & 1 deletion sote/game/entities/warband.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ end
---Returns amount of days warband can travel depending on collected supplies
---@return number
function warband:days_of_travel()
local supplies = self.leader.inventory['food'] or 0
local supplies = 0
if self.leader and self.leader.inventory['food'] then
supplies = self.leader.inventory['food']
end
local per_day = self:daily_supply_consumption()

if per_day == 0 then
Expand Down
14 changes: 0 additions & 14 deletions sote/game/entities/world.lua
Original file line number Diff line number Diff line change
Expand Up @@ -527,20 +527,6 @@ function world.World:tick()
military_effects.patrol(realm, target)
end
end
-- launch raids
for _, target in pairs(realm.reward_flags) do
local warbands = realm.raiders_preparing[target]
local units = 0
for _, warband in pairs(warbands) do
units = units + warband:size()
end

-- with some probability, launch the raid
-- larger groups launch raids faster
if (units > 0) and (love.math.random() > 0.5 + 1 / (units + 10)) then
military_effects.covert_raid(realm, target)
end
end

PROFILER:end_timer("war")

Expand Down
11 changes: 3 additions & 8 deletions sote/game/raws/decisions-loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ local tabb = require "engine.table"
local ll = {}
local military_effects = require "game.raws.effects.military"
local utils = require "game.raws.raws-utils"
local ef = require "game.raws.effects.economic"
local economic_effects = require "game.raws.effects.economic"

function ll.load()
local Decision = require "game.raws.decisions"

require "game.raws.decisions.war-decisions" ()
require "game.raws.decisions.character-decisions" ()
require "game.raws.decisions.office-decisions" ()
require "game.raws.decisions.diplomacy" ()
require "game.raws.decisions.interpersonal" ()
require "game.raws.decisions.travel" ()
require "game.raws.decisions._loader"()

-- Logic flow:
-- 1. Loop through all realms
Expand Down Expand Up @@ -134,7 +129,7 @@ function ll.load()
---@type Province
local primary_target = primary_target
primary_target.mood = math.min(10, primary_target.mood + 0.05)
ef.change_treasury(root, -primary_target:population() * gift_cost_per_pop, EconomicEffects.reasons.Donation)
economic_effects.change_treasury(root, -primary_target:population() * gift_cost_per_pop, economic_effects.reasons.Donation)
if WORLD:does_player_control_realm(root) then
WORLD:emit_notification("Population of " .. primary_target.name .. " is jubilant after receiving our gifts!")
end
Expand Down
Loading

0 comments on commit 7ac8c7a

Please sign in to comment.