#include "scripts/common.txt"

class TurokBPPurlinBoss : TurokBPPurlin {
	int anger = 0;
	float moveAnimSpeed = 12.0f;
	float attackAnimSpeed = 4.0f;
	//------------------------------------------------------------------------------------------------------------------------
    TurokBPPurlinBoss(kActor @actor) {
        super(actor);
		self.CastToAI().AIFlags() |= AIF_NOTHINK;
    }
	//------------------------------------------------------------------------------------------------------------------------
	void OnTickStart() {
		if (Debug::IsActorsDisabled()) {
			return;
		}

		TurokBPPurlin::OnTickStart();
		meleeRange = 400.36f;
		meleeRange2 = 600.36f;
		minAttackDelay = 1.0f; //in seconds
		maxAttackDelay = 3.0f; //in seconds
		curAttackTime = Math::RandRange(minAttackDelay, maxAttackDelay);
		animSpeed = 4.0f;
		self.ModelVariation() = 1; //for gun purlins
		self.PlaySound("sounds/shaders/hulk_alert.ksnd");
		self.AnimState().Set(anim_aiPurlinSpawnDrop, 4.0f, ANF_ROOTMOTION);
	}
	//------------------------------------------------------------------------------------------------------------------------
    void OnTick() {
		TurokBPPurlin::OnTick();
		if (Debug::IsActorsDisabled()) {
			return;
		}
		
		if (IsDead()) {
			return;
		}
		// self.Origin() = initPos;
		UpdateAI();
	}
	//------------------------------------------------------------------------------------------------------------------------
	void UpdateAI() {
		if (self.AnimState().PlayingID() == anim_aiPurlinSpawnDrop) {
			if (self.AnimState().CycleCompleted()) {
				self.CastToAI().AIFlags() &= ~AIF_NOTHINK;
			} else {
				return;
			}
		}
		
		if (!HasTarget()) {
			return;
		}
		
		kVec3 targetPos = self.GetTarget().Origin();
		
		if (!isAttacking) {
			curAttackTime = Math::Maxf(curAttackTime - GAME_DELTA_TIME, 0.0f);
			// if (self.AnimState().PlayingID() != anim_aiStanding) {
				// PlayAnimation(anim_aiStanding);
			// }
			animSpeed = moveAnimSpeed;
		} else {
			animSpeed = attackAnimSpeed;

		}
		
		if (CanAttack()) {
			float targetDist = Math::Sqrt(self.DistanceToPoint(targetPos));
			curAttackTime = Math::RandRange(minAttackDelay, maxAttackDelay);
			if (targetDist <= 800.0f and Math::RandMax(2) == 0) {
				PlayAnimation(anim_aiRangeAttack3); //shockwave pound
			} else {
				PlayAnimation(anim_aiRangeAttack1); //fire
			}
		}
		
	}
	//------------------------------------------------------------------------------------------------------------------------
    void GunFire(kActor @instigator, const float w, const float x, const float y, const float z) {
        if (!HasTarget()) {
            return;
        }
        
        self.PlaySound("sounds/shaders/explosion_1.ksnd");
		kVec3 target = self.GetTarget().Origin();
		target.z = self.GetTarget().FloorHeight();
		if (anger == 0) {
			self.SpawnProjectile("fx/hulk_blaster_boss.kfx", kVec3(x, y, z), target, Math::Deg2Rad(45.0f));
		} else {
			for (int i = 0; i < 2; i++) {
				float rx = Math::RandRange(-300.0f, 300.0f);
				float ry = Math::RandRange(-300.0f, 300.0f);
				target.x += rx;
				target.y += ry;
				self.SpawnProjectile("fx/hulk_blaster_boss.kfx", kVec3(x, y, z), target, Math::Deg2Rad(45.0f));
			}
		}
    }
	//------------------------------------------------------------------------------------------------------------------------
    void Shockwave(kActor @instigator, const float w, const float x, const float y, const float z) {
        if (!HasTarget()) {
            return;
        }
        
        //self.SpawnProjectile("fx/shockwave_boss.kfx", kVec3(x, y, z), self.GetTarget().Origin(), Math::Deg2Rad(45.0f));
		self.SpawnFx("fx/shockwave_boss.kfx", kVec3(x, y, z));
		if (anger == 1) {
			self.SpawnFx("fx/shockwave_boss.kfx", kVec3(x, y, z + 50));
			self.SpawnFx("fx/shockwave_boss.kfx", kVec3(x, y, z + 100));
		}

    }
	//------------------------------------------------------------------------------------------------------------------------
    void ShockwaveDamage(kActor @instigator, const float r, const float x, const float y, const float z) {
        kActor @targ;
        kVec3 pos;
        if (!HasTarget()) {
            return;
        }
        
        @targ = self.GetTarget();
        if (anger == 0 and !targ.OnGround()) {
            return;
        }
        
        pos = self.GetTransformedVector(kVec3(x, y, z));
        
        if (anger == 0 and Math::Fabs(targ.Origin().z - pos.z) > 30.72f) {
            return;
        }
        
        float range = targ.DistanceToPoint(pos);
        float radius = r * 15.24f * r * 15.24f;
        
        if (range >= radius) {
            return;
        }
		
        radius = (radius - range) / radius;
        targ.InflictGenericDamage(instigator, int(10.0f * radius));
    }
	//------------------------------------------------------------------------------------------------------------------------
    void OnDamage(kActor @instigator, kDictMem @damageDef, const int damage) {
		TurokBPPurlin::OnDamage(@instigator, @damageDef, damage);
		if (self.Health() <= 600) {
			anger = 1;
			attackAnimSpeed = 2.0f;
		}
    }
	//------------------------------------------------------------------------------------------------------------------------
}
