maxtargets=25
targetsdown=0
function _TargetDown()
	targetsdown=targetsdown+1
	ShowSmallMessage(string.format("TARGETS:%d/%d",targetsdown,maxtargets))
end

function scripttable.LevelStart()
	DisableHUD()
	DisableInput()
	Wait(1)
	ShowMessage([[Welcome to Militech Corporation training course for nonhuman combatants
Combat robot A-15 activated. This is facility AI speaking.
Initiate learning protocols.
Press space to continue.]])
	Wait(0)
	ShowMessage([[Use WASD to move 
Most doors open automatically when you go near
Terminals marked with [] contain useful information.
Press space to use. ]])

	EnableHUD()
	EnableInput()
	ForeachNamedObject("Doors1",function(o) o:Lock() end)
	ForeachNamedObject("Doors2",function(o) o:Lock() end)
	ForeachNamedObject("Doors3",function(o) o:Lock() end)
	ForeachNamedObject("Doors4",function(o) o:Lock() end)
	ForeachNamedObject("Doors5",function(o) o:Lock() end)
	ForeachNamedObject("Lamp1",function(o) o:SetFrame(0) end)
	ForeachNamedObject("Lamp2",function(o) o:SetFrame(0) end)
	ForeachNamedObject("Lamp3",function(o) o:SetFrame(0) end)
	ForeachNamedObject("Lamp4",function(o) o:SetFrame(0) end)
	ForeachNamedObject("Lamp5",function(o) o:SetFrame(0) end)

end

function scripttable.StartTerminal()
	ShowMessageDelay([[Most doors open automatically when you go near.
Locked doors have a red light near them]])
	ForeachNamedObject("Doors1",function(o) o:Unlock() end)
	ForeachNamedObject("Lamp1",function(o) o:SetFrame(1) end)
		
end

function scripttable.TutorialItem()
	scripttable.TutorialItem=function()
		ShowMessageDelay([[Press space to pick up items and open containers.
Use Mouse wheel to change selected item,
left click to take from a container.
press Q to drop items. 
]])
	end
	
	ShowMessageDelay([[Press space to pick up items and open containers.
Use Mouse wheel to change selected item,
left click to take from a container.
press Q to drop items. 
]])
	ForeachNamedObject("Doors1",function(o) o:Lock() end)
	ForeachNamedObject("Lamp1",function(o) o:SetFrame(0) end)

	scripttable.GreenLight[1]:SetFrame(1)


	scripttable.TutorialKast[1]:AddItem("pump")
	scripttable.TutorialKast[1]:AddItem("ppammo")
	scripttable.TutorialKast[1]:AddItem("auammo")
	scripttable.TutorialKast[1]:AddItem("auammo")


	while true do
		Wait(0.1)
		allitemspickedup=true
		ForeachNamedObject("ItemsOnGround",function(o)
			if o:IsOnGround() then 
				allitemspickedup=false 
			end 
		end)
		if scripttable.TutorialKast[1]:IsEmpty() and allitemspickedup then break end
	end
	Wait(0.5)
	ForeachNamedObject("Doors2",function(o) o:Unlock() end)
	ForeachNamedObject("Lamp2",function(o) o:SetFrame(1) end)
end

function scripttable.TutorialWeapon()
	ShowMessageDelay([[Proceed north to the shooting range]])	
	ForeachNamedObject("Doors2",function(o) o:Lock() end)
	ForeachNamedObject("Lamp2",function(o) o:SetFrame(0) end)
end

function scripttable.ShootingRange()
	scripttable._ShootingRange=scripttable.ShootingRange
	scripttable.ShootingRange=function() end
	ForeachNamedObject("ShootDoors",function(o) o:Lock() end)

	ShowMessageDelay([[Hold right mouse button to draw weapon & aim 
Click left mouse button while aiming to shoot 
Press R for reload.]])

	if scripttable.AmmoBox[1]:IsEmpty() then
		scripttable.AmmoBox[1]:AddItem("ppammo")
		scripttable.AmmoBox[1]:AddItem("ppammo")
		scripttable.AmmoBox[1]:AddItem("auammo")
		scripttable.AmmoBox[1]:AddItem("auammo")
	end
	Wait(1)
	scripttable.SpawnTargets()

	ForeachNamedObject("ShootDoors",function(o) o:Unlock() end)
	ForeachNamedObject("Doors3",function(o) o:Unlock() end)
	ForeachNamedObject("Lamp3",function(o) o:SetFrame(1) end)
	scripttable.ShootingRange=scripttable._ShootingRange
end

function scripttable.SpawnTargets()
	targetpos={
	{x=-8.7,y=-9.17,dir=1},
	{x=-8.7,y=-6.40,dir=1},
	{x=-8.7,y=-3.50,dir=1},
	{x= 1.6,y=-8.75,dir=-1},
	{x= 1.6,y=-5.92,dir=-1},
	{x= 1.6,y=-3.06,dir=-1},

	}
	for i=0,24 do
		Wait(1.5)
		local rndpos=targetpos[math.random(#targetpos)]
		CreateTutorialTarget(rndpos.x,rndpos.y,0,rndpos.dir*3,0)
	end
	
	Wait(3)
	if targetsdown<10 then
		ShowMessageDelay(string.format("TOTAL TARGETS HIT:%d/25\nCombat efficiency inadequate.",targetsdown))
	else
		ShowMessageDelay(string.format("TOTAL TARGETS HIT:%d/25\nCombat efficiency adequate.",targetsdown))
	end
	targetsdown=0
end

scripttable.organizeread=false
scripttable.combineread=false
scripttable.explosiveread=false

function scripttable.CombineTrigger()
	ShowMessageDelay([[Take everything in that container]])
	scripttable.TutorialKast2[1]:AddItem("smtimebomb")
	scripttable.TutorialKast2[1]:AddItem("smtimebomb")
	scripttable.TutorialKast2[1]:AddItem("smtimebomb")
	scripttable.TutorialKast2[1]:AddItem("smtimebomb")
	scripttable.TutorialKast2[1]:AddItem("smtimebomb")
	scripttable.TutorialKast2[1]:AddItem("smtimebomb")
	while true do
		Wait(0.1)
		if scripttable.organizeread and scripttable.combineread and scripttable.explosiveread then 
			break 
		end
	end

	ShowMessageDelay([[Blow your way through these yellow walls.]])
end

function scripttable.OrganizeTerminal()
	scripttable.organizeread=true
	ShowMessageDelay([[Use mouse wheel to change selected item in your inventory
Hold "e" and use mouse wheel to move selected 
item up and down to organize your inventory.]])
end
function scripttable.CombineTerminal()
	scripttable.combineread=true
	ShowMessageDelay([[Press C to enter item combination mode
left click anywhere to combine selected items.
Press C again to exit item combination mode.]])
end

function scripttable.ExplosiveTerminal()
	scripttable.explosiveread=true
	ShowMessageDelay([[For throwing bombs, click left mouse button while aiming with a bomb.
For dropping armed bombs, just leftclick. Caution, ~1 second delay.  ]])
end

function scripttable.MapTerminal()
	ShowMessageDelay([[Use TAB to check the map to see the bigger picture.]])
end
function scripttable.EndTerminal()
	ShowMessageDelay([[Thank you Robot A-15, your training is complete.]])
	local fadeout=0
	
	while fadeout<1.1 do
		fadeout=fadeout+0.02
		SetDarkness(fadeout)
		Wait(0)
	end
	menu.DisableGameState()
	menu.SetState("mainmenu")
end