-- $LastChangedDate: 2009-12-06 21:22:01 -0600 (Sun, 06 Dec 2009) $
-- Startup.
-- See module.txt for documentation and pitfalls.

sim.scriptDir = sim.Base:GetScriptDir()

--------------------------------------------------------------------------------

-- Build a map of cmd-line args.
-- The C++ binding provides an array form.
-- The map is convenient but is useless for parsing consecutive args.
sim.argvArray = sim.argv  -- alias
sim.argvMap = { }
for i=1, #sim.argv do
    local arg = sim.argv[i]
    sim.argvMap[arg] = i  -- value is the index for iterating by numeric index
end

-- Reverse-map to numeric index else nil if arg absent.
function sim.GetArgvIdx( arg )
    return sim.argvMap[arg]
end

--------------------------------------------------------------------------------

-- Log.
if ( sim.argvMap["-q"] ) then
    function Log( msg ) end
else
    Log = print
end
Log( "[Lua] startup." );

-- Randomize.
math.randomseed( os.time() )

--------------------------------------------------------------------------------

-- Manipulator mode?
if ( sim.argvMap["-t"] ) then
    sim.manipulatorMode = true
    dofile( sim.scriptDir .. "/manipulator_mode.lua" )
    return
end

--------------------------------------------------------------------------------

-- Load all Lua scripts.
dofile( sim.scriptDir .. "/lib.lua" )
dofile( sim.scriptDir .. "/gfx.lua" )
dofile( sim.scriptDir .. "/defs.lua" )
dofile( sim.scriptDir .. "/timer.lua" )
dofile( sim.scriptDir .. "/sim_generic.lua" )
dofile( sim.scriptDir .. "/sim_specific.lua" )
dofile( sim.scriptDir .. "/events.lua" )
dofile( sim.scriptDir .. "/scene.lua" )
dofile( sim.scriptDir .. "/replay.lua" )
dofile( sim.scriptDir .. "/game.lua" )  -- before actors
dofile( sim.scriptDir .. "/weapons.lua" )
dofile( sim.scriptDir .. "/actors.lua" )
dofile( sim.scriptDir .. "/views.lua" )
dofile( sim.scriptDir .. "/cmd.lua" )
dofile( sim.scriptDir .. "/keyboard.lua" )
dofile( sim.scriptDir .. "/joystick.lua" )
dofile( sim.scriptDir .. "/hud.lua" )
dofile( sim.scriptDir .. "/sound.lua" )
dofile( sim.scriptDir .. "/mission.lua" )

--------------------------------------------------------------------------------

-- Initialize motion blur if requested (can cause artifacts and slowness).
if ( sim.argvMap["-motion-blur"] ) then
    sim.FX:EnableMotionBlur( 0.15 )
    Log( "Enabled motion blur." )
end

-- Load mission script.
if ( sim.argvMap["-ossim"] ) then
    dofile( sim.scriptDir .. "/mission_ossim.lua" )
else
    dofile( sim.scriptDir .. "/mission_carrier.lua" )
end

-- Save state for when program is reset.
Mission:Save()
Mission:Start()
