#include "scripts/common.txt"

const array<int> BPHeadRangeAnims = {anim_aiRangeAttack1, anim_aiRangeAttack3};

		//anim_aiRangeAttack3 - Gunner Machine Gun
		//anim_aiRangeAttack1 - Rider Rockets
		
class TurokBPHead : TurokBPEnemy {
	float meleeRange = 143.36f;
	float minAttackDelay = 1.0f; //in seconds
	float maxAttackDelay = 3.0f; //in seconds
	
	float curAttackTime = 0.0f;
	int floatHeight = 0;
	float floatz = 0.0f;
	float deadTime = 0.0f;
	float mainHeightTime = 0.0f;
	float mainHeight = 0.0f;
	//------------------------------------------------------------------------------------------------------------------------
    TurokBPHead(kActor @actor) {
        super(actor);
    }
	//------------------------------------------------------------------------------------------------------------------------
	void OnTickStart() {
		TurokBPEnemy::OnTickStart();
		for (int i = 0; i < 44; i++) {
			if (i != 6) {
				for (int section = 0; section < 7; section++) {
					self.RenderModel().HideSection(i, section, true);
				}
			}
		}
		curAttackTime = Math::RandRange(minAttackDelay, maxAttackDelay);
		floatz = self.Origin().z;
		mainHeight = Math::RandRange(50.0f, 300.0f);
		animSpeed = 2.0f;
	}
	//------------------------------------------------------------------------------------------------------------------------
    void OnTick() {
		TurokBPEnemy::OnTick();
		if (Debug::IsActorsDisabled()) {
			return;
		}
		
		if (IsDead()) {
			if (deadTime > 0) {
				deadTime -= GAME_DELTA_TIME;
				if (deadTime <= 0) {
					Game.SpawnFx("fx/head_explosion.kfx", self.Origin(), self.SectorIndex());
					self.AnimState().Stop();
					self.Remove();
				}
			}
			return;
		}
		
		self.Velocity().Clear(); //prevents flying away forever from certain attacks such as the alien rfile
		
		mainHeightTime += GAME_DELTA_TIME;
		if (mainHeightTime >= 10.0f) {
			mainHeightTime = 0.0f;
			mainHeight = Math::RandRange(50.0f, 300.0f);
		}
		
		float targetHeight = HasTarget() ? self.GetTarget().Origin().z : self.FloorHeight();
		targetHeight += mainHeight;
		float fh = (50.0f * Math::Sin(Math::Deg2Rad(floatHeight)));
		floatHeight = (floatHeight + 2) % 360;
		float destHeight = Math::Clampf(targetHeight + fh, self.FloorHeight(), self.CeilingHeight());
		kVec3 pos = self.Origin();
		floatz += destHeight > pos.z ? Math::Minf(1.0f, destHeight - pos.z) : Math::Maxf(-1.0f, destHeight - pos.z);
		pos.z = floatz;
		self.Origin() = pos;
		
		AIStandard();
	}
	//------------------------------------------------------------------------------------------------------------------------
	void AIStandard() {
		if (!HasTarget()) {
			return;
		}
		
		kVec3 targetPos = self.GetTarget().Origin();
		
		if (!IsAttacking()) {
			curAttackTime = Math::Maxf(curAttackTime - GAME_DELTA_TIME, 0.0f);
		}
		
		if (CanAttack()) {
			curAttackTime = Math::RandRange(minAttackDelay, maxAttackDelay);
			PlayRandomAnimation(BPHeadRangeAnims);
		}
	}
	//------------------------------------------------------------------------------------------------------------------------
	bool CanAttack() {
		return (!IsAttacking() and curAttackTime <= 0);
	}
	//------------------------------------------------------------------------------------------------------------------------
	bool IsAttacking() {
		int animID = self.AnimState().PlayingID();
		if (BPHeadRangeAnims.find(animID) >= 0) {
			return true;
		}
		return false;
	}
	//------------------------------------------------------------------------------------------------------------------------
	void OnTickDeath() {
		TurokBPEnemy::OnTickDeath();
		self.RunFxEvent("Enemy_Freeze");
		Game.SpawnFx("fx/freeze_start.kfx", self.Origin(), self.SectorIndex());

		//self.RunFxEvent("Enemy_Melt");
		animSpeed = 4.0f;
		deadTime = 2.0f;
	}
	//------------------------------------------------------------------------------------------------------------------------
    void FootStepPuff(kActor @instigator, const float a, const float x, const float y, const float z) {
    }
	//------------------------------------------------------------------------------------------------------------------------
    void RiderFireMissiles(kActor @instigator, const float w, const float x, const float y, const float z) {
        if (!HasTarget()) {
            return;
        }

		self.SpawnFx("fx/ponly_spellcast.kfx", kVec3(x, y, z));
    }
	//------------------------------------------------------------------------------------------------------------------------
    void RiderFireShots(kActor @instigator, const float w, const float x, const float y, const float z) {
        if (!HasTarget()) {
            return;
        }
        		
		self.SpawnFx("fx/ponly_generic_178.kfx", kVec3(x, y, z)); //raptor missle
    }
	//------------------------------------------------------------------------------------------------------------------------
    void GunFire(kActor @instigator, const float w, const float x, const float y, const float z) {
        if (!HasTarget()) {
            return;
        }
		
		self.SpawnFx("fx/generic_178.kfx", kVec3(x, y, z)); //raptor missle
    }
	//------------------------------------------------------------------------------------------------------------------------
};
