#include "scripts/common.txt"

const array<int> BPPurlinMeleeAnims = {anim_aiMelee1, anim_aiMelee4, anim_aiRangeAttack2};
const array<int> BPPurlinMelee2Anims = {anim_aiRangeAttack3};
const array<int> BPPurlinRangeAnims = {anim_aiRangeAttack1};

		//anim_aiMelee1
		//anim_aiMelee4
		//anim_aiRangeAttack1 - fire shot
		//anim_aiRangeAttack2 - ground pound
		//anim_aiRangeAttack3 - Shockwave pound

class TurokBPPurlin : TurokBPEnemy {
	float meleeRange = 200.36f;
	float meleeRange2 = 300.36f;
	float minAttackDelay = 0.0f; //in seconds
	float maxAttackDelay = 1.5f; //in seconds
	
	float curAttackTime = 0.0f;
	bool isAttacking = false;
	//------------------------------------------------------------------------------------------------------------------------
    TurokBPPurlin(kActor @actor) {
        super(actor);
    }
	//------------------------------------------------------------------------------------------------------------------------
    void OnTick() {
		TurokBPEnemy::OnTick();
		isAttacking = IsAttacking();
	}
	//------------------------------------------------------------------------------------------------------------------------
	void AIStandard(bool useRange = false) {
		if (!HasTarget()) {
			return;
		}
		kVec3 targetPos = self.GetTarget().Origin();
		
		if (!isAttacking) {
			curAttackTime = Math::Maxf(curAttackTime - GAME_DELTA_TIME, 0.0f);
		}
		
		if (CanAttack()) {
			float targetDist = Math::Sqrt(self.DistanceToPoint(targetPos));
			curAttackTime = Math::RandRange(minAttackDelay, maxAttackDelay);
			if (targetDist <= meleeRange) {
				PlayRandomAnimation(BPPurlinMeleeAnims);
			} else if (targetDist > meleeRange and targetDist <= meleeRange2) {
				if (useRange and Math::RandMax(2) == 0) {
					PlayRandomAnimation(BPPurlinRangeAnims);
				} else {
					PlayRandomAnimation(BPPurlinMelee2Anims);
				}
			} else if (useRange) {
				PlayRandomAnimation(BPPurlinRangeAnims);
			}
		}
	}
	//------------------------------------------------------------------------------------------------------------------------
	bool CanAttack() {
		return (!isAttacking and curAttackTime <= 0);
	}
	//------------------------------------------------------------------------------------------------------------------------
	bool IsAttacking() {
		int animID = self.AnimState().PlayingID();
		if (BPPurlinMeleeAnims.find(animID) >= 0) {
			return true;
		}
		if (BPPurlinMelee2Anims.find(animID) >= 0) {
			return true;
		}
		if (BPPurlinRangeAnims.find(animID) >= 0) {
			return true;
		}
		return false;
	}
	//------------------------------------------------------------------------------------------------------------------------
    void PurlinStomp(kActor @instigator, const float w, const float x, const float y, const float z) {
        //self.PlaySound("sounds/shaders/generic_139.ksnd");
    }
	//------------------------------------------------------------------------------------------------------------------------
    void GunFire(kActor @instigator, const float w, const float x, const float y, const float z) {
        if (self.GetTarget() is null) {
            return;
        }
        
        self.PlaySound("sounds/shaders/explosion_1.ksnd");
        
        self.SpawnProjectile("fx/hulk_blaster.kfx", kVec3(x, y, z), self.GetTarget().Origin(), Math::Deg2Rad(45.0f));
    }
	//------------------------------------------------------------------------------------------------------------------------
    void OnActivate(void) {
        self.PlaySound("sounds/shaders/hulk_alert.ksnd");
    }
	//------------------------------------------------------------------------------------------------------------------------
    void DeathSound(kActor @instigator, const float w, const float x, const float y, const float z) {
        self.PlaySound("sounds/shaders/hulk_death.ksnd");
    }
	//------------------------------------------------------------------------------------------------------------------------
    void InjurySound(kActor @instigator, const float w, const float x, const float y, const float z) {
        self.PlaySound("sounds/shaders/hulk_injury.ksnd");
    }
	//------------------------------------------------------------------------------------------------------------------------
    void ViolentSound(kActor @instigator, const float w, const float x, const float y, const float z) {
        self.PlaySound("sounds/shaders/hulk_violent_death.ksnd");
    }
	//------------------------------------------------------------------------------------------------------------------------
    void Shockwave(kActor @instigator, const float w, const float x, const float y, const float z) {
        if (self.GetTarget() is null) {
            return;
        }
        
        self.SpawnProjectile("fx/shockwave.kfx", kVec3(x, y, z), self.GetTarget().Origin(), Math::Deg2Rad(45.0f));
    }
	//------------------------------------------------------------------------------------------------------------------------
    void ShockwaveDamage(kActor @instigator, const float r, const float x, const float y, const float z) {
        kActor @targ;
        kVec3 pos;
        if(self.GetTarget() is null) {
            return;
        }
        
        @targ = self.GetTarget();
        
        if (!targ.OnGround()) {
            return;
        }
        
        pos = self.GetTransformedVector(kVec3(x, y, z));
        
        if (Math::Fabs(targ.Origin().z - pos.z) > 30.72f) {
            return;
        }
        
        float range = targ.DistanceToPoint(pos);
        float radius = r * 10.24f * r * 10.24f;
        
        if (range >= radius) {
            return;
        }
        
        radius = (radius - range) / radius;
        targ.InflictGenericDamage(instigator, int(10.0f * radius));
    }
	//------------------------------------------------------------------------------------------------------------------------
}
