Created
April 18, 2014 07:16
-
-
Save ennorehling/11028939 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function new_faction(email, race, lang, r) | |
f = faction.create(email, race, lang) | |
u = unit.create(f, r, 10) | |
u:add_item("log", 5); | |
u:add_item("horse", 2); | |
u:add_item("silver", 1000); | |
u:add_item("adamantium", 1); | |
end | |
function get_homes(f) | |
homes={} | |
for u in f.units do | |
print(u, u.region) | |
table.insert(homes, u.region) | |
end | |
return homes | |
end | |
if eressea~=nil then | |
eressea.free_game() | |
eressea.read_game("game4.dat") | |
homes = get_homes(get_faction("xin8")) | |
else | |
-- running in the lua interpreter, not eressea. fake it. | |
new_faction = print | |
eressea = { ['write_game'] = function(s) print("writing " .. s) end } | |
homes = { "Andune", "Bedap", "Curtis", "Dovre" } | |
end | |
local f=assert(io.open("factions", "r")) | |
line=f:read("*line") | |
players = {} | |
emails = {} | |
patrons = {} | |
nplayers = 0 | |
while line~=nil do | |
fields = {} | |
line:gsub("([^\t]*)\t*", function(c) table.insert(fields, c) end) | |
print(line) | |
line=f:read("*line") | |
email = fields[1] | |
if fields[2]=='yes' then | |
table.insert(patrons, email) | |
else | |
table.insert(emails, email) | |
end | |
if fields[3]=='German' then lang='de' else lang='en' end | |
race=string.gsub(fields[4], "/.*", ''):lower() | |
players[email] = { ['lang'] = lang, ['race'] = race } | |
nplayers = nplayers + 1 | |
end | |
for k, r in ipairs(homes) do | |
print(k, r) | |
end | |
npatrons = #patrons | |
print(#homes .. " regions.") | |
print(nplayers .. " players.") | |
print(npatrons .. " patrons.") | |
maxfactions = 20 | |
selected = {} | |
if maxfactions > nplayers then maxfactions = nplayers end | |
while maxfactions > 0 do | |
if npatrons > 0 then | |
email = patrons[npatrons] | |
patrons[npatrons] = nil | |
npatrons = npatrons - 1 | |
else | |
local np = #emails | |
local i = math.random(np) | |
email = emails[i] | |
emails[i] = emails[np] | |
emails[np] = nil | |
end | |
local player = players[email] | |
player.email = email | |
print(email) | |
table.insert(selected, player) | |
maxfactions = maxfactions - 1 | |
end | |
-- random shuffle | |
for j=1,#selected do | |
k = math.random(j) | |
if k ~= j then | |
local temp = selected[j] | |
selected[j] = selected[k] | |
selected[k] = temp | |
end | |
end | |
for k, player in ipairs(selected) do | |
local r = homes[1 + k % #homes] | |
new_faction(player.email, player.race, player.lang, r) | |
end | |
eressea.write_game("game4.dat") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment