-- this needs to be first to allow us to overload the mod operator for strings
require 'utils.format'
getmetatable("").__mod = utils.format.interp
function ccpc:wait_on(...)
    local result = ccpc.wait_on2(...)
    if #result > 1 then
	utils.warning('More than one response returned!')
    end
    for i,v in pairs(result) do
	utils.debug(v.handle,':',v.signal)
    end
    return result[1].handle, result[1].signal
end

function _(msg)
    return msg
end

-- remove this to hide the designer telemetry/bees and handle tutorials properly
DEV_MODE=false
UNLOCK=false

require 'achievement_system'
require 'emissions'
require 'keyboard'
require 'menu_screen'
require 'splash_screen'
require 'utils.test'
require 'utils.widgets'

-- create keyboard device
keyboard.init()

settings.load()

local vol = tonumber(settings.get("music"))
if vol then audio.setMusicVolume(vol) else audio.setMusicVolume(0.5); settings.set("music", 0.5) end
vol = tonumber(settings.get("sound"))
if vol then audio.setSoundVolume(vol) else audio.setSoundVolume(0.8); settings.set("sound", 0.8) end

music_handle = audio.playMusic({"Menu"}, 0)

-- init toolkit
utils.widgets.init()

history = nil

math.randomseed (os.time())

achievement_task = ccpc.new_task(achievement_system.main)

local show_splash = true
if show_splash == true then
    x=splash_screen.show
else
    x=menu_screen.show
end

print 'Start main game loop'
while x ~= nil do
    x=x()
end

-- wake up this task to let it complete
ccpc.interrupt(achievement_task, "quit")

print '\n============\n= Quitting =\n============\n'
