//
// Copyright(C) 2014-2015 Samuel Villarreal
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// DESCRIPTION:
//      Grunt Human Script Object Class
//

#include "scripts/common.txt"

/*
==============================================================
TurokGrunt
==============================================================
*/

enum humanModelNodes
{
    HUMAN_ROOT,
    HUMAN_LUPLEG,
    HUMAN_LLOWLEG,
    HUMAN_LFOOT,
    HUMAN_LTOES,
    HUMAN_RUPLEG,
    HUMAN_RLOWLEG,
    HUMAN_RFOOT,
    HUMAN_RTOES,
    HUMAN_TORSO,
    HUMAN_LSHOULDER,
    HUMAN_LUPARM,
    HUMAN_LLOWARM,
    HUMAN_LHAND,
    HUMAN_RSHOULDER,
    HUMAN_RUPARM,
    HUMAN_RLOWARM,
    HUMAN_RHAND,
    HUMAN_NECK,
    HUMAN_HEAD,
    HUMAN_MAXNODE
}

class TurokGrunt : TurokEnemy
{
    TurokGrunt(kActor @actor)
    {
        super(actor);
    }
    
    /*
    ==============================================================
    HighPriestSpellAttack
    ==============================================================
    */
    
    void HighPriestSpellAttack(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        if(self.GetTarget() is null)
        {
            return;
        }
        
        self.SpawnFx("fx/spellcast.kfx", kVec3(x, y, z));
    }
    
    /*
    ==============================================================
    HighPriestSpellSound
    ==============================================================
    */
    
    void HighPriestSpellSound(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.PlaySound("sounds/shaders/high_priest_spell_attack.ksnd");
    }
    
    /*
    ==============================================================
    SwooshTrail1
    ==============================================================
    */
    
    void SwooshTrail1(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        switch(self.ModelVariation())
        {
        case GV_GENERIC_AXE:
        case GV_DEMON_AXE:
            self.RenderModel().AddTrailEffect("Trail_Axe", 17);
            break;
            
        case GV_GENERIC_CLUB:
        case GV_SHAMAN:
            self.RenderModel().AddTrailEffect("Trail_Mace", 17);
            break;
        
        case GV_COMMANDER_SPEAR:
        case GV_WARRIOR_SPEAR:
        case GV_DEMON_SPEAR:
            self.RenderModel().AddTrailEffect("Trail_Pole1", 17);
            break;
            
        case GV_COMMANDER_RIFLE1:
        case GV_COMMANDER_RIFLE2:
        case GV_COMMANDER_RIFLE3:
        case GV_COMMANDER_RIFLE4:
        case GV_POACHER_RIFLE:
        case GV_CYBORG_PULSE_RIFLE:
            self.RenderModel().AddTrailEffect("Trail_Shotgun", 17);
            break;
            
        case GV_POACHER_KNIFE:
            self.RenderModel().AddTrailEffect("Trail_Sword", 17);
            break;
            
        case GV_POACHER_PISTOL1:
        case GV_POACHER_PISTOL2:
            self.RenderModel().AddTrailEffect("Trail_Pistol", 17);
            break;
            
        case GV_POACHER_SHOTGUN:
            self.RenderModel().AddTrailEffect("Trail_Shotgun", 17);
            break;
            
        case GV_WARRIOR_BONE:
            self.RenderModel().AddTrailEffect("Trail_Bone", 17);
            break;
            
        case GV_WARRIOR_DART:
            self.RenderModel().AddTrailEffect("Trail_DartGun", 17);
            break;
        }
    }
    
    /*
    ==============================================================
    GunFire
    ==============================================================
    */
    
    void GunFire(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        if(self.GetTarget() is null)
        {
            return;
        }
        
        if( self.ModelVariation() == GV_COMMANDER_RIFLE4 ||
            self.ModelVariation() == GV_CYBORG_PULSE_RIFLE)
        {
            self.PlaySound("sounds/shaders/machine_gun_shot_2.ksnd");
            self.SpawnProjectile("fx/enemy_longhunter.kfx", kVec3(x, y, z),
                self.GetTarget().Origin(), Math::Deg2Rad(45.0f));
                
            return;
        }
        
        self.PlaySound("sounds/shaders/generic_8_enemy_human_gunfire.ksnd");
        self.SpawnProjectile("fx/enemy_bullet.kfx", kVec3(x, y, z),
            self.GetTarget().Origin(), Math::Deg2Rad(45.0f));
    }
    
    /*
    ==============================================================
    ThrowGrenade
    ==============================================================
    */
    
    void ThrowGrenade(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.SpawnFx("fx/generic_114.kfx", kVec3(x, y, z));
    }
    
    /*
    ==============================================================
    DemonAttack
    ==============================================================
    */
    
    void DemonAttack(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        if(self.GetTarget() is null)
        {
            return;
        }
        
        self.SpawnProjectile("fx/generic_256.kfx", kVec3(x, y, z),
            self.GetTarget().Origin(), Math::Deg2Rad(45.0f));
    }
    
    /*
    ==============================================================
    TribalDart1
    ==============================================================
    */
    
    void TribalDart1(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        if(self.GetTarget() is null)
        {
            return;
        }
        
        self.SpawnFx("fx/generic_193.kfx", kVec3(x, y, z));
    }
    
    /*
    ==============================================================
    TribalDart2
    ==============================================================
    */
    
    void TribalDart2(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.SpawnFx("fx/generic_194.kfx", kVec3(x, y, z));
    }
    
    /*
    ==============================================================
    DeathSound
    ==============================================================
    */
    
    void DeathSound(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        if( self.ModelVariation() >= GV_CYBORG_PULSE_RIFLE &&
            self.ModelVariation() <= GV_CYBORG_SPEAR)
        {
            self.PlaySound("sounds/shaders/ancient_warrior_death.ksnd");
            return;
        }
        else if(self.ModelVariation() >= GV_DEMON_AXE &&
                self.ModelVariation() <= GV_DEMON_LORD)
        {
            switch(Math::RandMax(3))
            {
            case 0:
                self.PlaySound("sounds/shaders/demon_death_scream_1.ksnd");
                break;
            case 1:
                self.PlaySound("sounds/shaders/demon_death_scream_2.ksnd");
                break;
            case 2:
                self.PlaySound("sounds/shaders/demon_death_scream_3.ksnd");
                break;
            }
            return;
        }
        
        switch(Math::RandMax(3))
        {
        case 0:
            self.PlaySound("sounds/shaders/human_death_scream_1.ksnd");
            break;
        case 1:
            self.PlaySound("sounds/shaders/human_death_scream_2.ksnd");
            break;
        case 2:
            self.PlaySound("sounds/shaders/human_death_scream_3.ksnd");
            break;
        }
    }
    
    /*
    ==============================================================
    InjurySound
    ==============================================================
    */
    
    void InjurySound(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        if( self.ModelVariation() >= GV_CYBORG_PULSE_RIFLE &&
            self.ModelVariation() <= GV_CYBORG_SPEAR)
        {
            self.PlaySound("sounds/shaders/ancient_warrior_death.ksnd");
            return;
        }
        else if(self.ModelVariation() >= GV_DEMON_AXE &&
                self.ModelVariation() <= GV_DEMON_LORD)
        {
            switch(Math::RandMax(3))
            {
            case 0:
                self.PlaySound("sounds/shaders/demon_effort_injury_grunt_1.ksnd");
                break;
            case 1:
                self.PlaySound("sounds/shaders/demon_effort_injury_grunt_2.ksnd");
                break;
            case 2:
                self.PlaySound("sounds/shaders/demon_effort_injury_grunt_3.ksnd");
                break;
            }
            return;
        }
        
        switch(Math::RandMax(3))
        {
        case 0:
            self.PlaySound("sounds/shaders/human_effort_injury_grunt_1.ksnd");
            break;
        case 1:
            self.PlaySound("sounds/shaders/human_effort_injury_grunt_2.ksnd");
            break;
        case 2:
            self.PlaySound("sounds/shaders/human_effort_injury_grunt_3.ksnd");
            break;
        }
    }
    
    /*
    ==============================================================
    ViolentSound
    ==============================================================
    */
    
    void ViolentSound(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        // GERMAN: No violent death sounds
        
        if( self.ModelVariation() >= GV_CYBORG_PULSE_RIFLE &&
            self.ModelVariation() <= GV_CYBORG_SPEAR)
        {
            self.PlaySound("sounds/shaders/ancient_warrior_death.ksnd");
            return;
        }
        else if(self.ModelVariation() >= GV_DEMON_AXE &&
                self.ModelVariation() <= GV_DEMON_LORD)
        {
            switch(Math::RandMax(3))
            {
            case 0:
                self.PlaySound("sounds/shaders/demon_death_scream_1.ksnd");
                break;
            case 1:
                self.PlaySound("sounds/shaders/demon_death_scream_2.ksnd");
                break;
            case 2:
                self.PlaySound("sounds/shaders/demon_death_scream_3.ksnd");
                break;
            }
            return;
        }
        
        switch(Math::RandMax(3))
        {
        case 0:
            self.PlaySound("sounds/shaders/human_death_scream_1.ksnd");
            break;
        case 1:
            self.PlaySound("sounds/shaders/human_death_scream_2.ksnd");
            break;
        case 2:
            self.PlaySound("sounds/shaders/human_death_scream_3.ksnd");
            break;
        }
    }
    
    /*
    ==============================================================
    OnBeginLevel
    ==============================================================
    */
    
    void OnBeginLevel(void)
    {
        kRenderModel @model = self.RenderModel();
        kStr violenceLevel;
        
        Sys.GetCvarValue("g_violencelevel", violenceLevel);
        
        // GERMAN: all grunts are robotic; if not in no-blood mode, change their
        // impact type to metal here. Otherwise, the flesh impact in no-blood 
        // mode matches the original game more closely.
        if(violenceLevel.Atoi() < 2)
        {
            self.ImpactType() = IT_METAL;
        }

        if(model is null)
        {
            return;
        }

        // GERMAN: altered texture assignment behaviors
       
        switch(self.ModelVariation())
        {
        // commander
        case GV_COMMANDER_SPEAR:
        case GV_COMMANDER_RIFLE1:
        case GV_COMMANDER_RIFLE2:
        case GV_COMMANDER_RIFLE3:
        case GV_COMMANDER_RIFLE4:
            for(int i = 0; i < HUMAN_HEAD; i++)
            {
                model.SetTexture(i, 1);
            }
            break;
            
        // cyborg guard
        case GV_CYBORG_PULSE_RIFLE:
        // demon lord
        case GV_DEMON_LORD:
            for(int i = 0; i < HUMAN_MAXNODE; i++)
            {
                model.SetTexture(i, 1);
            }
            break;
        }
    }
};
