forked from github/server
re-implementing markets.c in lua (WIP)
This commit is contained in:
parent
17b6c7f43f
commit
3d232faf19
2 changed files with 90 additions and 0 deletions
89
scripts/e3a/markets.lua
Normal file
89
scripts/e3a/markets.lua
Normal file
|
@ -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")
|
|
@ -1,6 +1,7 @@
|
|||
require "spells"
|
||||
require "e3a.xmas2009"
|
||||
require "e3a.rules"
|
||||
require "e3a.markets"
|
||||
|
||||
local srcpath = config.source_dir
|
||||
tests = {
|
||||
|
|
Loading…
Reference in a new issue