From 3d232faf192a4c6a0bf2282718bfce8684b99dd4 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 19 Apr 2010 22:33:07 -0700 Subject: [PATCH] re-implementing markets.c in lua (WIP) --- scripts/e3a/markets.lua | 89 +++++++++++++++++++++++++++++++++++++++++ scripts/e3a/modules.lua | 1 + 2 files changed, 90 insertions(+) create mode 100644 scripts/e3a/markets.lua diff --git a/scripts/e3a/markets.lua b/scripts/e3a/markets.lua new file mode 100644 index 000000000..0fc24da28 --- /dev/null +++ b/scripts/e3a/markets.lua @@ -0,0 +1,89 @@ +function get_markets(r, result) + local n = 0 + result = result or {} + + for b in r.buildings do + if b.type=="market" then + u = b.owner + if u~=nil then + table.insert(result, u) + n = n + 1 + end + end + end + return n, result +end + +function collect_markets(r, result) + local result = result or {} + local n = 0 + n, result = get_markets(r, result) + for i, r in ipairs(r.adj) do + if r then + local x, result = get_markets(r, result) + n = n + x + end + end + return n, result +end + +function market_action(r) + local f = r.owner + local trade = 1000 + if f~=nil and f.race=="halfling" then + trade = 600 + end + + local p = r:get_resource("peasant") + if p > 500 then + local n, markets = collect_markets(r) + + if n>0 then + local give + if r.luxury~=nil then + give = {} + local numlux = p / trade + for x = 1, numlux do + local m = 1+math.mod(rng_int(), n) + u = markets[m] + if give[u] then + give[u] = give[u] + 1 + else + give[u] = 1 + end + end + + for u, v in pairs(give) do + u:add_item(r.luxury, v) + end + end + + if r.herb~=nil then + give = {} + local numherb = p / 500 + for x = 1, numherb do + local m = 1+math.mod(rng_int(), n) + u = markets[m] + if give[u] then + give[u] = give[u] + 1 + else + give[u] = 1 + end + end + + for u, v in pairs(give) do + u:add_item(r.herb, v) + end + end + end + end +end + +local function markets() + local r + for r in regions() do + market_action(r) + end +end + +-- add_proc(markets, "Markets", "Bauernwanderung") diff --git a/scripts/e3a/modules.lua b/scripts/e3a/modules.lua index 986527c32..aac9e864a 100644 --- a/scripts/e3a/modules.lua +++ b/scripts/e3a/modules.lua @@ -1,6 +1,7 @@ require "spells" require "e3a.xmas2009" require "e3a.rules" +require "e3a.markets" local srcpath = config.source_dir tests = {