-- brebot.lua - plays your Barren Realms Elite turns for the day. -- -- Copyright (C) 2026 Michael Slater. GNU General Public License version 2 or later. -- -- Start it at the game's own menu (the one with "(1) Play Game"): press Alt-S and give -- the path of this file. Press Alt-S again at any time to stop it and take over. It -- plays every turn you have left, then stops back at that menu. -- -- The plan follows Robert Wennagel's "Strategies and Hints for Barren Realms Elite" -- (v2.0, 1996), for a local game with military purchasing on: -- * tax low (3%) while small so the population grows, 8% once large; -- * while protected: sell the troopers you were given and spare food, and put every -- coin into regions - Coastal first, Rivers once there is a Coastal base, enough -- Agricultural to feed everyone, some Urban; -- * near the end of protection and after it: a defensive army (Turrets, Tanks, a -- Headquarters, Covert Agents) and the guide's Agricultural:Urban:Technology 4:3:3; -- * a Tariff Trade Agreement is offered to every realm; trade, intelligence and -- technology agreements offered to you are accepted, Free Trade and Full Defense -- are refused; -- * once it has an army it raids the pirates when it is strong enough. It does not -- attack other realms. -- -- Files written next to this script: -- brebot--.log what it did -- brebot--state.lua who was offered an agreement, how pirate raids went -- ---------------------------------------------------------------- the plan's numbers local TAX_SMALL, TAX_LARGE = 3, 8 -- per cent; LARGE once you hold LARGE_REGIONS local LARGE_REGIONS = 2500 local ARM_AT_PROTECTION = 4 -- start the army with this many protected years left local FOOD_PER_AGRI = 300 -- food one Agricultural region grows a year local FOOD_MARGIN = 2.5 -- grow this many times what is eaten local FOOD_KEEP_YEARS = 5 -- years of food kept while protected (the rest is sold) local FOOD_KEEP_YEARS_LATER = 8 local COASTAL_BASE = 60 -- Coastal regions held before buying Rivers local MAX_BUYS_PER_TURN = 14 local ARMY_SHARE = 0.45 -- share of each turn's gold for the army once arming local TURRETS_PER_REGION, TANKS_PER_REGION, AGENTS_PER_REGION = 3.0, 1.0, 0.5 local REPROPOSE_DAYS = 7 local PIRATES = 'Humans' -- the first, weakest band local RAID_SHARE = 0.6 -- share of troopers and tanks sent on a raid local RAID_MIN_POINTS = 600 -- offensive points (Trooper 1, Tank 4) for a first try local RAIDS_PER_DAY = 2 local MAX_TURNS = nil -- set a number to stop after that many turns local DEADLINE = now() + 30 * 60 -- hard stop local KEY_DELAY = 0.3 -- seconds after each key, so you can watch (0 = full speed) local REGION_KEYS = {Coastal = 'C', River = 'R', Agricultural = 'A', Desert = 'D', Industrial = 'I', Urban = 'U', Mountain = 'M', Technology = 'T'} local ITEM_KEYS = {Troopers = '1', Jets = '2', Turrets = '3', Bombers = '4', HeadQuarters = '5', Regions = '6', ['Covert Agents'] = '7', Tanks = '8', Carriers = '9'} local MENUS = {['Barren Realms Elite'] = true, ['Crazy Gold Bank'] = true, ['Food Unlimited'] = true, ['Spending Menu'] = true, ['Sell Menu'] = true, ['System Menu'] = true, ['Attack Menu'] = true, ['Attack Pirates'] = true, ['Trading'] = true, ['Diplomacy Menu'] = true, ['Advisors'] = true, ['Preferences'] = true} -- ---------------------------------------------------------------- files and log local HERE = (script_path or ''):match('^(.*[/\\])') or '' local USER = (bbs and bbs.user and bbs.user ~= '') and bbs.user or 'player' local SAFE = USER:gsub('[^%w%-_]', '_'):lower() local LOGFILE = HERE .. 'brebot-' .. SAFE .. '-' .. os.date('%Y-%m-%d') .. '.log' local STATEFILE = HERE .. 'brebot-' .. SAFE .. '-state.lua' local syncterm_log = log local logf = io.open(LOGFILE, 'a') local function log(...) local parts = {} for i = 1, select('#', ...) do parts[#parts + 1] = tostring((select(i, ...))) end local msg = table.concat(parts, ' ') if logf then logf:write(os.date('%H:%M:%S '), msg, '\n'); logf:flush() end syncterm_log(msg) end local function trim(s) return (s:gsub('^%s+', ''):gsub('%s+$', '')) end local function has(s, text) return s:find(text, 1, true) ~= nil end local function num(s) return tonumber((tostring(s or ''):gsub('[^%d]', ''))) or 0 end local function commas(n) local s = tostring(math.floor(n)) local out = s:reverse():gsub('(%d%d%d)', '%1,'):reverse() return (out:gsub('^,', '')) end local function serialize(v, indent) indent = indent or '' if type(v) == 'table' then local keys = {} for k in pairs(v) do keys[#keys + 1] = k end table.sort(keys, function(a, b) return tostring(a) < tostring(b) end) local out = {'{\n'} for _, k in ipairs(keys) do local key = type(k) == 'number' and ('[' .. k .. ']') or ('[' .. string.format('%q', k) .. ']') out[#out + 1] = indent .. ' ' .. key .. ' = ' .. serialize(v[k], indent .. ' ') .. ',\n' end out[#out + 1] = indent .. '}' return table.concat(out) elseif type(v) == 'string' then return string.format('%q', v) else return tostring(v) end end local function load_state() local f = io.open(STATEFILE, 'r') local st = {} if f then local src = f:read('a'); f:close() local chunk = load('return ' .. src, 'state', 't', {}) local ok, t = pcall(chunk or function() return {} end) if ok and type(t) == 'table' then st = t end end st.proposed = st.proposed or {} st.pirates = st.pirates or {} return st end local function save_state(st) local f = io.open(STATEFILE, 'w') if f then f:write(serialize(st), '\n'); f:close() end end -- ---------------------------------------------------------------- the screen -- The screen down to the last line with text on it: {rows}, all of it as one string, the last line. local function look() local _, n = screen_size() local r = {} for i = 1, n or 25 do r[i] = screen(i) or '' end local last_i = 0 for i = #r, 1, -1 do if r[i]:find('%S') then last_i = i; break end end local out = {} for i = 1, last_i do out[i] = r[i] end return out, table.concat(out, '\n'), out[last_i] or '' end -- the menu a "Choice>" belongs to: the last known [Title] on the screen local function menu_head(all) local head = '' for name in all:gmatch('%[([%a ]+)%]') do if MENUS[name] then head = name end end return head end -- the part of the screen from the last time `text` appears local function from_last(all, text) local at, i = nil, 1 while true do local s = all:find(text, i, true) if not s then break end at = s; i = s + 1 end return at and all:sub(at) or nil, at end local function parse_status(all) local blk, at = from_last(all, 'Popular Support:') if not blk then return nil end -- the block starts at the realm's name, a few lines above local start = 1 for s in all:sub(1, at):gmatch('()%-%*') do start = s end blk = all:sub(start) local st = {bank = 0, protection = 0, military = {}, regions = {}} st.turns = num(blk:match('Turns: *([%d,]+)')) st.score = num(blk:match('Score: *([%d,]+)')) st.gold = num(blk:match('Gold: *([%d,]+)')) st.bank = num(blk:match('Bank: *([%d,]+)')) st.population = num(blk:match('Population: *([%d,]+)')) st.tax = num(blk:match('Tax Rate: *(%d+)%%')) st.support = num(blk:match('Popular Support: *(%d+)%%')) st.food = num(blk:match('Food: *([%d,]+)')) st.protection = num(blk:match('You have (%d+) Years? of Protection')) local mil = blk:match('Military:(.-)Regions:') if mil then for n, name in mil:gmatch('%[([%d,]+) ([%a ]-)%]') do st.military[trim(name)] = num(n) end end local reg = blk:match('Regions:(.*)') if reg then reg = reg:match('^(.-)You have') or reg for n, name in reg:gmatch('%[([%d,]+) (%a+)%]') do -- the status screen says Rivers and Mountains; the buying list River and Mountain if name == 'Rivers' then name = 'River' elseif name == 'Mountains' then name = 'Mountain' end st.regions[name] = num(n) end end return st end local function parse_spending(all) local blk = from_last(all, '[Spending Menu]') if not blk then return nil end local items = {} for name, price, owned in blk:gmatch('%(%d%) ([%a ]-) +([%d,]+) +([%d,]+)') do items[trim(name)] = {price = num(price), owned = num(owned)} end local gold, turns = blk:match('You have ([%d,]+) gold and (%d+) turns?') return {items = items, gold = num(gold), turns = tonumber(turns) or 0} end -- ---------------------------------------------------------------- the player local state = load_state() local st = nil -- the last status block local first_status = nil local turn = {} -- notes for the turn in hand local bought = {} -- regions bought this turn, by kind local turns_played = 0 local in_game, done = false, false local dip = {} local raids = 0 local army = {} local stop_reason = nil local function sum(t) local n = 0; for _, v in pairs(t) do n = n + v end; return n end local function regions_total() return st and sum(st.regions) or 0 end local function arming() return st ~= nil and st.protection <= ARM_AT_PROTECTION end local function food_need() if turn.food_asked then return turn.food_asked end if not st then return 150 end return math.floor(st.population * 1.5) + sum(st.military) // 50 end local function regions_text(r) local keys = {} for k in pairs(r) do keys[#keys + 1] = k end table.sort(keys) local out = {} for _, k in ipairs(keys) do out[#out + 1] = k .. ' ' .. r[k] end return table.concat(out, ', ') end local function note_status() if not first_status then first_status = {score = st.score, regions = regions_total()} end log(string.format('turn %d: %d left | gold %s bank %s | pop %dM tax %d%% support %d%% | food %s | regions %d (%s) | army (%s) | protection %d', turns_played, st.turns, commas(st.gold), commas(st.bank), st.population, st.tax, st.support, commas(st.food), regions_total(), regions_text(st.regions), regions_text(st.military), st.protection)) end local function new_turn(all) -- A turn left half-played starts again without the status screen: then the status is -- read from the System Menu when the spending menu comes up. turn = {}; bought = {} turns_played = turns_played + 1 local head = all:match('^(.*)visit the Bank%?') or all if has(head, 'Popular Support:') then st = parse_status(head) note_status() else st = nil; turn.need_status = true log('turn ' .. turns_played .. ': carrying on a turn that was left unfinished') end end local function end_turn(all) local blk = from_last(all, 'End of Turn Statistics') if not blk then return end local first = true for line in (blk .. '\n'):gmatch('(.-)\n') do line = trim(line) if not first and line ~= '' and line:find('%a') and not has(line, 'wish to continue') then log(' ', line) end first = false end end -- which region kind to buy next local function next_region() local have = {} for k, v in pairs(st.regions) do have[k] = v end for k, v in pairs(bought) do have[k] = (have[k] or 0) + v end local total = sum(have) local function g(k) return have[k] or 0 end local want_agri = math.ceil(food_need() * FOOD_MARGIN / FOOD_PER_AGRI) if g('Agricultural') < want_agri then return 'Agricultural' end if arming() and total >= 150 then -- the guide's mix out of protection (military purchasing on): Ag 4 : Urban 3 : Tech 3 local shares = {Agricultural = 0.4, Urban = 0.3, Technology = 0.3} local core = g('Agricultural') + g('Urban') + g('Technology') if core == 0 then core = 1 end local best, low = nil, math.huge for _, k in ipairs({'Agricultural', 'Urban', 'Technology'}) do local v = g(k) / (shares[k] * core) if v < low then best, low = k, v end end return best end if g('Urban') < 0.06 * total then return 'Urban' end if g('Coastal') >= COASTAL_BASE and g('River') < g('Coastal') then return 'River' end return 'Coastal' end local function spend(all) local sp = parse_spending(all) if not sp then return '0' end if turn.need_status then return '*' end if not st then return '0' end local gold, items = sp.gold, sp.items army = {} for k, v in pairs(items) do army[k] = v.owned end local function owned(k) return items[k] and items[k].owned or 0 end local function price(k) return items[k] and items[k].price or 1e9 end local total = regions_total() + sum(bought) -- 1. the tax rate, once a turn at most local want_tax = total >= LARGE_REGIONS and TAX_LARGE or TAX_SMALL if st.tax ~= want_tax and not turn.tax_set then turn.tax_set = true; turn.tax = want_tax log(' tax rate ' .. st.tax .. '% -> ' .. want_tax .. '%') st.tax = want_tax return '*' end -- 2. while protected an army only costs money: sell the troopers we were given if st.protection > ARM_AT_PROTECTION + 2 and owned('Troopers') > 0 and not turn.sold then turn.sold = true; turn.sell = {'Troopers', owned('Troopers')} return 'S' end turn.buys = (turn.buys or 0) + 1 if turn.buys > MAX_BUYS_PER_TURN then return '0' end turn.gold0 = turn.gold0 or gold -- 3. the army, once protection is nearly over if arming() then local army_left = turn.gold0 * ARMY_SHARE - (turn.army_spent or 0) local wants, shares = {}, 0 local function want(name, share) wants[#wants + 1] = {name, share}; shares = shares + share end if owned('Turrets') < TURRETS_PER_REGION * total then want('Turrets', 0.6) end if owned('Tanks') < TANKS_PER_REGION * total then want('Tanks', 0.4) end if owned('Covert Agents') < AGENTS_PER_REGION * total then want('Covert Agents', 0.15) end if owned('Tanks') > 0 and owned('HeadQuarters') < 1 + owned('Tanks') // 400 then want('HeadQuarters', 0.1) end for _, w in ipairs(wants) do local name, share = w[1], w[2] if not turn['army_' .. name] then turn['army_' .. name] = true local n = math.floor(math.min(turn.gold0 * ARMY_SHARE * share / shares, army_left, gold) / price(name)) if n >= 1 then turn.army_spent = (turn.army_spent or 0) + n * price(name) turn.order = n return ITEM_KEYS[name] end end end end -- 4. everything else into regions local rp = price('Regions') if gold >= rp * 1.05 then local kind = next_region() local afford = math.floor(gold / (rp * 1.05)) -- a few lots a turn: the price climbs as we buy, and the mix is looked at again between lots local lot = math.max(1, math.min(afford, math.max(3, afford // 3))) local held = (st.regions[kind] or 0) + (bought[kind] or 0) if kind == 'Agricultural' then lot = math.max(1, math.min(lot, math.ceil(food_need() * FOOD_MARGIN / FOOD_PER_AGRI) - held)) elseif kind == 'Urban' then lot = math.max(1, math.min(lot, math.ceil(0.06 * total) + 1 - held)) end turn.order = lot; turn.region_key = REGION_KEYS[kind] return ITEM_KEYS.Regions end return '0' end local function note_raid(all) turn.raid_noted = true local blk = from_last(all, 'many Tanks?') or '' local lost, first, shown = false, true, 0 for line in (blk .. '\n'):gmatch('(.-)\n') do line = trim(line) if not first and line:find('^You ') then if has(line, 'could not') then lost = true end if shown < 4 then log(' ', line); shown = shown + 1 end end first = false end local rec = state.pirates[PIRATES] or {} state.pirates[PIRATES] = rec if lost then rec.lost_with = math.max(rec.lost_with or 0, turn.raid_points) else rec.won_with = math.min(rec.won_with or math.huge, turn.raid_points) end save_state(state) end -- The attack menu. Only pirate raids: this bot does not attack other realms. local function attack(all) if turn.raid_sent then if not turn.raid_noted then note_raid(all) end return '0' end if not st or not arming() or raids >= RAIDS_PER_DAY or turn.raid then return '0' end local send_t = {Troopers = math.floor((army.Troopers or 0) * RAID_SHARE), Tanks = math.floor((army.Tanks or 0) * RAID_SHARE)} local points = send_t.Troopers + 4 * send_t.Tanks local rec = state.pirates[PIRATES] or {} local need = math.max(RAID_MIN_POINTS, 2 * (rec.lost_with or 0)) if rec.won_with and rec.won_with > (rec.lost_with or 0) then need = math.min(need, math.floor(1.2 * rec.won_with)) end if points < need then return '0' end turn.raid = send_t; turn.raid_points = points; raids = raids + 1 log(string.format(' raiding the %s pirates with %d troopers and %d tanks (%d points)', PIRATES, send_t.Troopers, send_t.Tanks, points)) return 'P' end -- at "Send to:" - first ask for the list, then name the realms not offered lately local function send_to(all) if not dip.listed then dip.listed = true; return '?' end local blk = from_last(all, '-*Relations*-') or '' local today = os.time() local letters = '' for letter, rest in blk:gmatch('%[(%u)%] +([^\n]+)') do local name, rel = rest:match('^(.-)%s%s+(%S.-)%s*$') if name and trim(rel) == 'None' then name = trim(name) local last = state.proposed[name] if not last or (today - last) >= REPROPOSE_DAYS * 86400 then letters = letters .. letter; state.proposed[name] = today log(' offering a Tariff Trade Agreement to ' .. name) end end end save_state(state) return letters .. '\r' end -- a "Choice>" menu: the key to press, or nil to stop the script here local function menu(all) local head = menu_head(all) if head == 'Barren Realms Elite' then if done then return nil end if not in_game then in_game = true; return '1' end done = true -- back here after Play Game: the turns are used up return nil elseif head == 'Crazy Gold Bank' then local blk = from_last(all, '[Crazy Gold Bank]') local _, bank = blk:match('You have ([%d,]+) gold in hand and ([%d,]+) gold in the bank') if num(bank) > 0 and not turn.withdrawn then turn.withdrawn = true log(' withdrew ' .. commas(num(bank)) .. ' from the bank') return 'W' end return '0' elseif head == 'Food Unlimited' then local blk = from_last(all, '[Food Unlimited]') local _, food = blk:match('You have ([%d,]+) gold and ([%d,]+) units of food') if food and not turn.food_done and st then turn.food_done = true food = num(food) local need = food_need() local keep = need * (arming() and FOOD_KEEP_YEARS_LATER or FOOD_KEEP_YEARS) if food < need * 1.2 then turn.food_order = math.floor(need * 2 - food) log(string.format(' short of food (%d, need %d) - buying %d', food, need, turn.food_order)) return 'B' elseif food > keep + 200 then turn.food_order = math.floor(food - keep) log(string.format(' selling %d spare food (keeping %d)', turn.food_order, math.floor(keep))) return 'S' end end return '0' elseif head == 'Diplomacy Menu' then if not dip.done then dip.done = true; return '1' end return '0' elseif head == 'Attack Menu' then return attack(all) elseif head == 'Attack Pirates' then if turn.raid and not turn.raid_sent then turn.raid_sent = true local blk = from_last(all, '[Attack Pirates]') local key = blk:match('%((%d)%) ' .. PIRATES) if key then return key end end return '0' elseif head == 'Spending Menu' then return spend(all) elseif head == 'Sell Menu' then if turn.sell then local name, n = turn.sell[1], turn.sell[2] turn.sell = nil; turn.order = n return ITEM_KEYS[name] end return 'B' elseif head == 'System Menu' then if turn.need_status then if turn.asked_status then st = parse_status(all); turn.need_status = nil if st then note_status() end else turn.asked_status = true return 'E' end end if turn.tax and st then return 'R' end return '0' end if head == 'Trading' and turn.raid_sent and not turn.raid_noted then note_raid(all) end return '0' -- Trading, Advisors, Preferences, anything else: leave end -- the key for whatever is on the screen; nil = nothing recognised; false = stop here local function decide(rows, all, last) if has(last, 'Paused') then return '\r' end if has(last, 'ANSI Graphics?') then return '\r' end if last:find('Name your Realm:%s*$') then stop_reason = 'The game does not know you yet. Found your realm yourself, then run me from the game menu.' return false end if has(last, 'Would you like Instructions?') then return 'N' end if has(last, 'lottery ticket?') then return 'N' end -- no gambling if last:find('Choose your 6 letters:%s*$') then return 'KALRET\r' end -- if a ticket was started anyway if last:find('^Continue%? %(Y/n%)') then return 'Y' end if has(last, 'Choice>') then local k = menu(all) if k == nil then return false end return k end if last:find('visit the Bank%? %(y/N%)%s*$') then new_turn(all) return (st and st.bank > 0) and 'Y' or 'N' end if last:find('Withdraw how many gold%?') then return '>\r' end local need, most = last:match('How much will you give%? %(([%d,]+); ([%d,]+)%)%s*$') if need then need, most = num(need), num(most) local what = rows[#rows - 1] or '' if what:lower():find('food') then turn.food_asked = (turn.food_asked or 0) + need end if most < need then log(' cannot pay in full: ' .. trim(what) .. ' (have ' .. most .. ')') end return '\r' end local _, _, fmax = last:match('How much food do you wish to (%a+)%? %(([%d,]+); ([%d,]+)%)%s*$') if fmax then local n = math.max(0, math.min(turn.food_order or 0, num(fmax))) turn.food_order = nil return n .. '\r' end local bname, bmax = last:match('Buy how many ([%a ]-)%? %(0; ([%d,]+)%)%s*$') if bname then local name = bname:gsub(' regions', '') local n = math.max(0, math.min(turn.order or 0, num(bmax))) turn.order = nil if n > 0 then log(' bought ' .. n .. ' ' .. name) if REGION_KEYS[name] then bought[name] = (bought[name] or 0) + n end end return n .. '\r' end local sname, smax = last:match('Sell how many ([%a ]-)%? %(0; ([%d,]+)%)%s*$') if sname then local n = math.max(0, math.min(turn.order or 0, num(smax))) turn.order = nil if n > 0 then log(' sold ' .. n .. ' ' .. sname) end return n .. '\r' end local rname, rmax = last:match('[Hh]ow many (%a+)%? %(0; ([%d,]+)%)%s*$') if rname then -- what to send on a raid local n = math.max(0, math.min((turn.raid or {})[rname] or 0, num(rmax))) return n .. '\r' end if last:find('Your choice%?%s*$') then -- the region list local k = turn.region_key or '\r' turn.region_key = nil return k end if last:find('New Tax Rate %[') then local k = (turn.tax or (st and st.tax) or TAX_SMALL) .. '\r' turn.tax = nil return k end if last:find('Send to:%s*$') then return send_to(all) end if last:find('attach a message%? %(y/N%)') then return 'N' end if last:find('send a message%? %(y/N%)') then return 'N' end if last:find('wish to continue%? %(Y/n%)%s*$') then end_turn(all) if MAX_TURNS and turns_played >= MAX_TURNS then log('stopping here (MAX_TURNS)'); done = true return 'N' end return 'Y' end if last:find('%([Yy]/[Nn]%)%s*$') then local ctx = table.concat({rows[#rows - 3] or '', rows[#rows - 2] or '', rows[#rows - 1] or '', last}, ' ') if ctx:find('Agreement') or ctx:find('Alliance') or ctx:find('Prevention') or ctx:find('Trade') or ctx:find('[Tt]reaty') then local yes = not (ctx:find('Free Trade') or ctx:find('Full Defense') or ctx:find('War')) log(' treaty question - answering ' .. (yes and 'yes' or 'no') .. ': ' .. trim(ctx):sub(-160)) return yes and 'Y' or 'N' end log(' unexpected yes/no, taking the default: ' .. trim(last)) return '\r' end if last:find('%(([%d,]+); ([%d,]+)%)%s*$') then log(' unexpected number prompt, taking the default: ' .. trim(last)) return '\r' end return nil end -- ---------------------------------------------------------------- the run local function run() local idle, same, before = 0, 0, nil while true do if now() > DEADLINE then stop_reason = 'Stopped: the 30 minute limit was reached.'; return end if not connected() then stop_reason = 'The connection dropped.'; return end quiet(0.6, 20) local rows, all, last = look() -- a prompt that did not change after our key means the key was not taken: do not spin if all == before then same = same + 1 else same = 0 end if same > 8 then stop_reason = 'Stopped: no progress at "' .. trim(last) .. '".'; return end local key = decide(rows, all, last) if key == false then return end if key == nil then idle = idle + 1 if idle > 25 then stop_reason = 'Stopped: I do not know this screen ("' .. trim(last) .. '").'; return end sleep(1) else idle = 0; before = all send(key) if KEY_DELAY > 0 then sleep(KEY_DELAY) end -- wait for the screen to answer before looking again local t0 = now() while now() - t0 < 5 do local _, a = look() if a ~= before then break end sleep(0.1) end end end end log('==== start (' .. USER .. ')') local ok, err = pcall(run) if not ok then log('ERROR: ' .. tostring(err)) stop_reason = 'The script hit an error: ' .. tostring(err) end local summary if st and first_status then summary = string.format('%d turn(s) played. Regions %d -> %d, score %d -> %d.', turns_played, first_status.regions, regions_total(), first_status.score, st.score) elseif turns_played == 0 then summary = 'No turns were played (none left today?).' else summary = turns_played .. ' turn(s) played.' end if stop_reason then summary = stop_reason .. '\n' .. summary end log('end: ' .. summary:gsub('\n', ' ')) if logf then logf:close() end message('BRE bot: ' .. summary .. '\nLog: ' .. LOGFILE)