debugmode = GetBoolParam("debug", false)

function init()
turretmotor = FindJoint("turret")
gunmotor = FindJoint("gun") 
marauder = FindVehicle("marauder")
gunmin, gunmax = GetJointLimits(gunmotor)

optics = FindShape("optics") 

firstperson = false 
mousemovement = 0

testloc = FindLocation("test")
testloctrans = GetLocationTransform(testloc) 

crosshaircamera = LoadSprite("MOD/sprite/crosshaircamera.png")
weaponcrosshair = LoadSprite("MOD/sprite/crosshairgun.png")




--weapons
barrel1 = FindShape("barrel1")
barrel2 = FindShape("barrel2")
turretraycast = FindShape("turretraycast") 

ATGM = FindShape("atgm") 
ATGMmotor = FindJoint("atgm")

fov = 95
zoomlevel = 1 

-- 1 = machine gun 2 = ATGM 
weapontype = 1

guncooldown = 0
gunROF = 10
gundirection1 = Vec(0,0,-1)
gundirection2 = Vec(0,0,-1)
atgmfired = false 
gunfired = false 
guncooldown = 0.12
atgmcooldown = 3
firetime = 1000
gunfiretime = 1000

nvgtimer = 0 

--sound 
shootsoundTP = LoadSound("MOD/snd/gunthirdperson.ogg")
shootsoundFP = LoadSound("MOD/snd/gunfirstperson.ogg")

ATGMsoundTP = LoadSound("MOD/snd/shorttps.ogg")
ATGMsoundFP = LoadSound("MOD/snd/shortfps.ogg")

button = LoadSound("MOD/snd/zoom.ogg")
zoomin = LoadSound("MOD/snd/zoomin.ogg")
zoomout = LoadSound("MOD/snd/zoomout.ogg")
switchoff = LoadSound("MOD/snd/switchoff.ogg")

NVG = false 

cockpitview = false 
ignoreshapes = FindShapes("ignoreshape")

raycasttimer = 0
doraycasts = true 
end 


function tick()

	
		raycasttimer = raycasttimer + 1 
		if raycasttimer > 15 then 
			doraycasts = true
			raycasttimer = 0 
		end 



	-- freeze the guns in place when not driving. stops them flopping over!
	if not driving then 
		SetJointMotor(ATGMmotor, 0)
		SetJointMotor(turretmotor, 0)
		SetJointMotor(gunmotor,0)
	end 


	gunmovement = GetJointMovement(gunmotor) 
	--script for changing weapons and weapon rates of fire 
	if driving and InputPressed("1") then 
		weapontype = 1 
	elseif driving and InputPressed("2") then 
		weapontype = 2 
	end 

	if atgmfired then 
	local timenow = GetTime()
		if timenow > firetime + atgmcooldown then 
		atgmfired = false 
		timenow = 0 
		end 
	end 
	if gunfired then 
	local timenow = GetTime() 
		if timenow > gunfiretime + guncooldown then 
			gunfired = false 
		end 
	end 
	
	--transforms that are needed!
opticstransform = GetShapeWorldTransform(optics)

playercameratransform = GetPlayerCameraTransform()
cameratransform = GetCameraTransform()
playercamrot = playercameratransform.rot 
camerarot = cameratransform.rot 
if debugmode then 
DebugWatch("playercameratransform", playercameratransform)
DebugWatch("cameratransform", cameratransform)
DebugWatch("playercamrot", playercamrot)
DebugWatch("camerarot", camerarot)
end 
-- split the direction of the gun pointing forwards and the direction from the gun barrel to the location we want to aim towards
-- then we can compare the values and set the joints to move
camx, camy, camz = GetQuatEuler(camerarot)
playx, playy, playz = GetQuatEuler(playercamrot)

--DebugWatch("camx", camx)
--DebugWatch("camy", camy)
--DebugWatch("playx", playx)
--DebugWatch("playy", playy) 

--transforms directions and positions for weaponary

		barrel1trans = GetShapeWorldTransform(barrel1)
		barrel2trans = GetShapeWorldTransform(barrel2) 
		TRaycasttransform = GetShapeWorldTransform(turretraycast)
		TRaycastdirection = TransformToParentVec(TRaycasttransform, Vec(0,0,-1))
		gundirection1  = TransformToParentVec(barrel1trans, Vec(0,0,-1))
		gundirection2 = TransformToParentVec(barrel2trans, Vec(0,0,-1))
		--DebugWatch("gundirection1", gundirection1)
		ATGMtransform = GetShapeWorldTransform(ATGM)
		ATGMdirection = TransformToParentVec(ATGMtransform, Vec(0,0-1))
		
		
		--check to see if the player is in first person mode (cabin view, not weapon sight view) so we can play the correct sounds!
		local camcheck = GetCameraTransform().pos
		local playcamcheck = GetPlayerCameraTransform().pos 
		if camcheck[1] == playcamcheck[1] and camcheck[2] == playcamcheck[2] then 
			cockpitview = true 
		else 
			cockpitview = false 
		end 
		--DebugWatch("cockpitview", cockpitview) 
		
		
		
		
		
		
		--IR LENS SCRIPT (FIRST PERSON ONLY!) 
	if InputPressed("g") and driving then 
		NVG = not NVG 
		nvgtimer = 0 
		if NVG then 
			PlaySound(button) 
		else
			PlaySound(switchoff) 
		end 
	end 
	--DebugWatch("nvg", NVG) 
	
	if NVG then 
		if nvgtimer < 500 then 
		nvgtimer = nvgtimer + 1
		end 
		
		local brightness = nvgtimer/200 
		if brightness > 2 then 
			brightness = 2 
		end 
		SetPostProcessingProperty("saturation", 0)
		SetPostProcessingProperty("brightness", brightness)
		SetPostProcessingProperty("gamma", (brightness*3)-0.3)
	else 
		SetPostProcessingDefault()
	end 

	--detect if the player is in the vehicle

	if GetPlayerVehicle() == marauder then 
		driving = true 
	else 
		driving = false 
		firstperson = false 
	end 
	--toggle weapon sight view when right click is  pressed
	if driving and InputPressed("rmb") then
		firstperson = not firstperson
	end 
	-- first person zoom script 
	if firstperson then 
		change = InputValue("mousewheel") 
		if change >= 1 then 
			zoomlevel = zoomlevel + 1 
		elseif change <=-1 then 
			zoomlevel = zoomlevel - 1 
		end
	end 
	
	--zooming script
		local zoomcheck = fov 
	if zoomlevel < 1 then 
		zoomlevel = 1
	elseif zoomlevel > 3 then 
		zoomlevel = 3 
	end 
	
	if zoomlevel ==1 then 
		fov = 95
	elseif zoomlevel == 2 then 
		fov = 70
	elseif zoomlevel == 3 then 
		fov = 30
	end 
	 if zoomcheck > fov then 
		PlaySound(zoomin)
	elseif zoomcheck < fov then 
		PlaySound(zoomout) 
	end 
	--DebugWatch("zoomlevel", zoomlevel)
		
		
		--create raycasts going from the gun barrel straight ahead and also a raycast going from the camera in the direction it is looking
	if not firstperson and driving then 
		direction = Vec(0,0,-1)
		direction = TransformToParentVec(cameratransform, direction)
		turretdirection = Vec(0,0,-1)
		turretdirection = TransformToParentVec(cameratransform, turretdirection)
		--if InputPressed("f") then (this was to test if the sprite was being drawn in the right place because the debugcross is too small to see :) 
			--Explosion(cameratransform.pos, 0.1) 
		--end 
		
		
		-- calls the reject shapes function which means the raycast will ignore the vehicle itself if its in the way of the camera
		--if doraycasts then 
		rejectshapes()
		hit, dist, normal, shape = QueryRaycast(cameratransform.pos, direction, 30, 0.1, false)
		rejectshapes()
		Thit, Tdist, Tnormal, Tshape = QueryRaycast(TRaycasttransform.pos, TRaycastdirection, 30,0.1, true)
		doraycasts = false 
		--raycasttimer = 0 
		--end 
		--DebugCross(TRaycasttransform.pos)
		--DebugWatch("hit", hit) 
		--DebugWatch("dist", dist)
		--DebugWatch("T-Hit", Thit)
		--DebugCross(cameratransform.pos) 
		linendpoint = VecAdd(cameratransform.pos, VecScale(direction, 100))
		--DrawLine(cameratransform.pos, linendpoint)
		if not Thit then 
			Tdist = 100
		end 
		if not hit then 
			dist = 100
		end 
		
		local gunaimpoint = VecAdd(TRaycasttransform.pos, VecScale(TRaycastdirection, Tdist)) 
		local	gunspritepos = Transform(gunaimpoint, cameratransform.rot) 
		
			local hitpoint = VecAdd(cameratransform.pos, VecScale(direction, dist))
			--DebugCross(hitpoint)
			spritepos = Transform(hitpoint, cameratransform.rot)
			misspos = Transform(linendpoint, cameratransform.rot)
			
			
			
			
		
			
			
			
			DrawSprite(crosshaircamera, misspos, 2, 2)
			--DebugPrint("missing") 
			if not hit then 
			spritepos = Transform(linendpoint, cameratransform.rot) 
			end 
		if hit then 
			DrawSprite(crosshaircamera, spritepos, dist/30, dist/30)
		else 
			DrawSprite(crosshaircamera, spritepos, 2, 2)
		end 
		DrawSprite(weaponcrosshair, gunspritepos, Tdist/20, Tdist/20)
		--DebugWatch("gunspritepos", gunspritepos)
		
			-----THIS PART IS FOR FINDING ANGLE BETWEEN DIRECTION TURRET IS FACING AND DIRECTION CAMERA IS FACING 
		
			shyangle, shzangle, shxangle = GetQuatEuler(TRaycasttransform.rot)
			quat = QuatLookAt(TRaycasttransform.pos, spritepos.pos) 
			yangle, zangle, xangle = GetQuatEuler(quat)
			if debugmode then 
			DebugWatch("shape1transform.rot", TRaycasttransform.rot)
			DebugWatch("spritepos", spritepos.pos) 
			DebugWatch("quat", quat) 
			DebugWatch("zangle", zangle)
			DebugWatch("yangle", yangle)
			DebugWatch("shyangle", shyangle)
			DebugWatch("shzangle", shzangle)
			end 
					--TURRET MOVEMENT 
			if zangle < shzangle -0.2 then 
			SetJointMotor(turretmotor, 1.5)
			else 
			SetJointMotor(turretmotor, -1.5) 
			end 
			if yangle < shyangle -0.2 then 
			SetJointMotor(gunmotor, -1)
			SetJointMotorTarget(ATGMmotor, gunmovement, -1) 
			else
			SetJointMotor(gunmotor, 1)
			SetJointMotorTarget(ATGMmotor, gunmovement, 1)
			end 
			if zangle > 100 and shzangle < -100 then 
				SetJointMotor(turretmotor, 1)
			end 
			if zangle < -100 and shzangle > 100 then 
				SetJointMotor(turretmotor, -1)
			end 
			
			
			
			--stops the joint from bouncing around and quickly moving back and forth 
			if zangle > shzangle -0.5 and zangle < shzangle + 0.5 then 
				SetJointMotor(turretmotor, 0) 
			end 
			if yangle > shyangle -0.5 and yangle < shyangle + 0.5 then 
				SetJointMotor(gunmotor, 0)
			end 
			
			
			
			
	end 	
		if not driving then 
			NVG = false 
		end 
		if not NVG then 
			SetPostProcessingDefault()
		end 
	
	-- shooting 
	
	if driving and InputDown("lmb") then 
		--machinegun firing 
		if weapontype == 1 and gunfired == false then
			--guncooldown = guncooldown+1 
				Shoot(barrel1trans.pos, gundirection1, "bullet", 0.3)
				Shoot(barrel2trans.pos, gundirection2, "bullet", 0.3)
				if firstperson or cockpitview then 
					PlaySound(shootsoundFP)
				else 
					PlaySound(shootsoundTP)
				end 
				gunfiretime = GetTime() 
				gunfired = true 
		end 
		 --ATGM firing
		if weapontype == 2 and atgmfired == false then 
			--Shoot(ATGMtransform.pos, ATGMdirection, "rocket", 2, 500)
			Spawn("MOD/atgm.xml", ATGMtransform)
			ShakeCamera(0.6)
			atgmfired = true
			if firstperson then 
				PlaySound(ATGMsoundFP) 
			else 
				PlaySound(ATGMsoundTP)
			end 
			firetime = GetTime() 
		end 
	end 
	--- turret movement script when in first person mode
	if firstperson == true then 
		if NVG then 
			SetCameraTransform(opticstransform, fov*0.9)
		else 
			SetCameraTransform(opticstransform, fov)
		end 
		--turret left and right movement
		mousemovement = InputValue("mousedx")
		--DebugWatch("Mousemovent", mousemovement)
		if mousemovement > 30 then 
			mousemovement = 30 
		elseif mousemovement < -30 then
			mousemovement = -30 
		end 
		SetJointMotor(turretmotor, mousemovement/10)
		
		--gun up and down movement 
		mousemovementup = InputValue("mousedy") 
		

		SetJointMotor(gunmotor, mousemovementup/-10) 
		SetJointMotorTarget(ATGMmotor, gunmovement)
		if mousemovementup == 0 then 
			SetJointMotor(gunmotor, 0.01)
		end 
	end 

--DebugWatch("driving", driving)
end


function draw()
	if firstperson then
		UiPush()
			UiTranslate(UiCenter(), UiMiddle())
			UiColor(1, 1, 1, 1)
			UiAlign("center middle")
			if weapontype == 1 then 
				UiImage("MOD/sprite/firstperson.png")
			elseif weapontype == 2 then 
				UiImage("MOD/sprite/rpgcrosshair.png")
			end 
		UiPop()
	end 
end 

function rejectshapes()
for i=1, #ignoreshapes do 
	QueryRejectShape(ignoreshapes[i])
end 
end 



