#include "script/common.lua"
#include "alicantoscripts/bodyscripts.lua"
#include "alicantoscripts/flightscripts.lua"
#include "alicantoscripts/aiscripts.lua"
#include "alicantoscripts/utilities.lua"
--head unit and 11 tails
--basically the basilisco without armour
pSpeed = GetFloatParam("speed", 10)
--originally 7

--no projectiles cause this is a friendly creature

function rndFloat(mi, ma)
	return mi + (ma-mi)*(math.random(0, 1000000)/1000000.0)
end

function init()
	--sound system?
	DebugPrint(minSpeed)
	
	aiSettings = FindLocation("aitags") --location that stores relevant tags to ai function
	flightSettings = FindLocation("flighttags") --stores relevant tags for flight
	head = FindBody("alicantohead")
	headTransform = GetBodyTransform(head)
	headVel = Vec()
	headTargetPos = VecAdd(headTransform.pos,Vec(0,0,0))
	headTargetRot = QuatEuler()

	--11 full units, last 4 are just banners
	--due to how FindBodies works, pieces must be placed in descending order in editor
	tailTable = FindBodies("alicantotail",false) --tails in these tables

	--wing placements in the tags of the wing in editor
	--REQUIRED EDITOR TAGS FOR EVERY WING
	--tailNum corresponding to tail unit number
	--sweep for local xyz angle to mount wings at
	--hoverpivot if hovermode is used
	
	leftWingTable = FindBodies("alicantowingl",false) --left wings
	rightWingTable = FindBodies("alicantowingr",false) --right wings
	
	tailTable = initBody(tailTable)
	leftWingTable = initLeftWings(leftWingTable)
	rightWingTable = initRightWings(rightWingTable) --makes full tables for later use from the handle lists

	active = true --whether or not thing should be active
	
	initFlightVar()
	initAIVar() --initializes required variables stored in other scripts

	--searchlight init goes here
	searchLightRot = QuatEuler()
	searchLightTargetRot = QuatEuler()

	delayTimer = 0 --for delays in weapon charging and firing
	
	getAllAlicanto()
	getAllChoppers()
end

--it's more flexible to set up the movement modules in functions, off in another script

function tick(dt)
	if not active then return end
	
	height = patrolheight --sets baseline altitude
	alicantoPatrol(dt)

	timeSinceLastSeen = timeSinceLastSeen + dt
	if timeSinceLastSeen > patrolTime then
		choosePatrolTarget()
	end
	--[[if timeSinceLastSeen > distractionTime then
		hearSound()
	end--]]
	

	--DrawBodyOutline(head,1)
	--DrawBodyOutline(tailTable[tailSegments][1],1)

end

function update(dt)
	local acc = VecSub(headTargetPos, headTransform.pos)
	--headVel = VecAdd(headVel, VecScale(acc, dt))
	--headVel = VecScale(acc, VecLength(acc)*0.8)
	--headTransform.pos = VecLerp(headTransform.pos, headTargetPos, 0.8)

	--Rotate smoothly towards target rotation
	headTransform.pos,headTransform.rot = alicantoHeadUpdate(headTransform.pos,headTargetPos,headTransform.rot,headTargetRot,turnRate,dt)
	
	--tail smoothing here
	for i=1,#tailTable do
		local acc = VecSub(tailTable[i].targetPos,tailTable[i].transform.pos)
		tailTable[i].transform.pos = alicantoTailUpdate(tailTable[i].transform.pos, tailTable[i].targetPos, dt)
	end

end


--imma fess that despite using a chilean mythos name, the creature is actually the whale from journey by tgc