-- -----------------------------------------------------------------------------
-- SimNetwork: The network simulator
-- simnetwork.lua
-- Main file: initializes and starts game 
-- -----------------------------------------------------------------------------

if not SimNetwork_GLOBAL		then dofile("global.lua")	end
if not SimNetwork_DATA			then dofile("data.lua")	end
if not SimNetwork_INPUT		then dofile("input.lua")	end
if not SimNetwork_TOOLBAR		then dofile("toolbar.lua")	end
if not SimNetwork_SCENE		then dofile("scene.lua")	end
if not SimNetwork_BALANCE		then dofile("balance.lua")	end
if not SimNetwork_SIMULATION		then dofile("simulation.lua")	end
if not SimNetwork_INTRO		then dofile("intro.lua")	end

-- turn debuggin on/off
debug = false

-- turn background music on/off
bgMusic = true

-- -----------------------------------------------------------------------------
-- Function: dprint(string,entity)
-- Inputs: string, entity
-- Returns: none
-- Writes debugging info to the console
-- -----------------------------------------------------------------------------
function dprint(string,entity)
	if debug == true then
		E.print_console(string,entity,"\n")
	end
end

function beginPlaying(level)
	----print("in beginPlaying()")
	
	-- create game scene
	gameScene = Scene:init()
	
	-- create instance of simulation engine
	gameSim = Sim:init(level)
	gameSim:setLogicData(	gameScene.map.customers,
			gameScene.map.routers,
			gameScene.map.cables)

	-- initialize GUI and load demo scene
	gameScene:initGUI()
	gameScene:loadDemoScene()
	gameSim:begin()
	
	if bgMusic then
		E.set_sound_amplitude(backgroundGamePlayMusic, 0.15)	
		E.stop_sound(introMusic)
		E.loop_sound(backgroundGamePlayMusic)
	end
end

-- see Lua's pseudo-random generator
math.randomseed(os.time())

-- current state of game
gameState = GAME_STATE.intro
gameScene = nil
gameSim = nil

INTRO_SCREEN_TIME = 7

introTimer = 0

-- collect information about the display
init_viewport()

--initialize mouse cursors
initializeCursors()
hideCursors()

--create selection box
createSelectionBox()
hide(selectionBox)

-- load intro screen
createSplashScreen()

-- load menu screen
createMenuScreen()
menuSelection = 1	--menu selection can be 1-4
hide(menuScreen)

--create help screen
createHelpScreen()
hide(helpScreen)

--create second help screen
createHelp2Screen()
hide(help2Screen)

--create credits screen
createCreditsScreen()
hide(creditsScreen)

--create level selection screen
createLevelScreen()
hide(levelScreen)

--create loading screen
createLoadingScreen()
hide(loadingScreen)

--create game won screen

--create game lost screen

-- show intro screen
show(introScreen)

--load default font
E.set_typeface("data/Coprgtb.TTF")

-- create different brushes to be used for fonts
blackBrush = E.create_brush()
E.set_brush_color(blackBrush, 0.0,0.0,0.0,1.0)

redBrush = E.create_brush()
E.set_brush_color(redBrush, 1.0,0.0,0.0,1.0)

blueBrush = E.create_brush()
E.set_brush_color(blueBrush, 0.0,0.0,1.0,1.0)

greenBrush = E.create_brush()
E.set_brush_color(greenBrush, 0.0,1.0,0.0,1.0)

if bgMusic then
	backgroundGamePlayMusic = E.load_sound("data/sounds/backgroundgameplay.ogg")
	introMusic = E.load_sound("data/sounds/introMusic.ogg")
	E.set_sound_amplitude(introMusic, 0.15)
	E.loop_sound(introMusic)
end


selectSound = E.load_sound("data/sounds/select.ogg")
E.set_sound_amplitude(selectSound, 3)
selectedSound = E.load_sound("data/sounds/selected.ogg")
E.set_sound_amplitude(selectedSound, 1)
toolHoverSound = E.load_sound("data/sounds/toolhover.ogg")
toolDeselectSound = E.load_sound("data/sounds/tooldeselect.ogg")
customerPopupSound = E.load_sound("data/sounds/customerpopup.ogg")
messageSound = E.load_sound("data/sounds/message.ogg")

-- we're done: give control to do_timer()
E.enable_timer(true)
