// Red Skull - Eat the remain of your victim. (Skull=Headshot, Choco=Normal Kill)
/* CVARS - copy and paste to shconfig.cfg

//Red Skull
red_level 5
red_chocohealth 50    //HP gained from eating a chocolates of victims
red_skullhealth 100    //HP gained from eating a skulls of victims

*/

#include <superheromod>

// GLOBAL VARIABLES
new gHeroName[]="Red Skull"
new bool:gHasRedPower[SH_MAXSLOTS+1]
new gUserTeam[SH_MAXSLOTS+1]
new bool:gBetweenRounds
new Smoke
//--------------------------------------------------------------------------------------------------
public plugin_init()
{
    // Plugin Info
    register_plugin("RedSkull","1.2","[TagPro]")

    register_dictionary("sh_redskull.txt")

    // DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
    register_cvar("red_level", "5" )
    register_cvar("red_chocohealth", "50")
    register_cvar("red_skullhealth", "100")
    
    // FIRE THE EVENT TO CREATE THIS SUPERHERO!
    shCreateHero(gHeroName, "HP on Kill", "Kill People to turn them into chocolate or skull and gain HP by eating them", false, "red_level" )

    // REGISTER EVENTS THIS HERO WILL RESPOND TO! (AND SERVER COMMANDS)
    // INIT
    register_srvcmd("red_init", "red_init")
    shRegHeroInit(gHeroName, "red_init")

    register_event("ResetHUD", "newRound","b")
    register_event("DeathMsg","deathevent","a")
    register_event("DeathMsg","redskull_death","a")
}
//--------------------------------------------------------------------------------------------------
public plugin_precache() {
    precache_model("models/shmod/chocolate.mdl")
    precache_model("models/shmod/skull.mdl")
    precache_model("models/player/redskull/redskull.mdl")
    precache_sound("ambience/particle_suck1.wav")
    Smoke = precache_model("sprites/wall_puff4.spr")
}
//--------------------------------------------------------------------------------------------------
public red_init()
{
    // First Argument is an id
    new temp[6]
    read_argv(1,temp,5)
    new id=str_to_num(temp)
    
    // 2nd Argument is 0 or 1 depending on whether the id has GreenLantern
    read_argv(2,temp,5)
    new hasPowers = str_to_num(temp)
    
    gHasRedPower[id] = (hasPowers!=0)
    
    cs_set_user_model(id, "redskull")
}
//--------------------------------------------------------------------------------------------------
public newRound()
{
    new skull = find_ent_by_class(-1, "skull")
    new chocolate = find_ent_by_class(-1, "chocolate")
    while(chocolate) {
        remove_entity(chocolate)
        chocolate = find_ent_by_class(chocolate, "chocolate")
    }    
    while(skull) {
        remove_entity(skull)
        chocolate = find_ent_by_class(skull, "skull")
    }
    gBetweenRounds = false
}
//--------------------------------------------------------------------------------------------------
public deathevent()
{
    new killer = read_data(1)
    new victim = read_data(2)
    if ( killer != victim )
    {
        if (read_data(3)==1 && read_data(5)==0)
        {
            if ( gHasRedPower[killer] && is_user_alive(killer) )
            {
                createSkull(victim)
                new aimvec[3]
                get_user_origin(victim, aimvec)
                message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
                write_byte(23)
                write_coord(aimvec[0])
                write_coord(aimvec[1])
                write_coord(aimvec[2])
                write_short(Smoke)
                write_byte(001)
                write_byte(65)
                write_byte(200)
                message_end()
                aimvec[2] -= 100
                set_user_origin(victim, aimvec)
            }
        }
        else
        {
            if ( gHasRedPower[killer] && is_user_alive(killer) )
            {
                createChocolate(victim)
                new aimvec[3]
                get_user_origin(victim, aimvec)
                message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
                write_byte(23)
                write_coord(aimvec[0])
                write_coord(aimvec[1])
                write_coord(aimvec[2])
                write_short(Smoke)
                write_byte(001)
                write_byte(65)
                write_byte(200)
                message_end()
                aimvec[2] -= 100
                set_user_origin(victim, aimvec)
            }
        }
    }
    return PLUGIN_HANDLED
}
//--------------------------------------------------------------------------------------------------
public createChocolate(victim)
{
    new Float:vAim[3], Float:vOrigin[3]
    entity_get_vector(victim, EV_VEC_origin, vOrigin)
    VelocityByAim(victim, random_num(2, 4), vAim)

    vOrigin[0] += vAim[0]
    vOrigin[1] += vAim[1]
    vOrigin[2] += 30.0

    new chocolate = create_entity("info_target")
    entity_set_string(chocolate, EV_SZ_classname, "chocolate")
    entity_set_model(chocolate, "models/shmod/chocolate.mdl")    
    entity_set_size(chocolate, Float:{-2.5, -2.5, -1.5}, Float:{2.5, 2.5, 1.5})
    entity_set_int(chocolate, EV_INT_solid, 2)
    entity_set_int(chocolate, EV_INT_movetype, 6)
    entity_set_vector(chocolate, EV_VEC_origin, vOrigin)
}
//--------------------------------------------------------------------------------------------------
public createSkull(victim)
{
    new Float:vAim[3], Float:vOrigin[3]
    entity_get_vector(victim, EV_VEC_origin, vOrigin)
    VelocityByAim(victim, random_num(2, 4), vAim)

    vOrigin[0] += vAim[0]
    vOrigin[1] += vAim[1]
    vOrigin[2] += 30.0

    new skull = create_entity("info_target")
    entity_set_string(skull, EV_SZ_classname, "skull")
    entity_set_model(skull, "models/shmod/skull.mdl")    
    entity_set_size(skull, Float:{-2.5, -2.5, -1.5}, Float:{2.5, 2.5, 1.5})
    entity_set_int(skull, EV_INT_solid, 2)
    entity_set_int(skull, EV_INT_movetype, 6)
    entity_set_vector(skull, EV_VEC_origin, vOrigin)
}    
//--------------------------------------------------------------------------------------------------
public pfn_touch(ptr, ptd) 
{
    if(!is_valid_ent(ptd) || !is_valid_ent(ptr))
        return PLUGIN_CONTINUE
        
    if(!is_user_connected(ptd) || !is_user_alive(ptd))
        return PLUGIN_CONTINUE
    
    if( !gHasRedPower[ptd] )
        return PLUGIN_CONTINUE

    new classname[32]
    new gOrigHealth = get_user_health(ptd)
    new health
    new name[32]
    entity_get_string(ptr, EV_SZ_classname, classname, 31)
    get_user_name(ptd, name, 32)
    if(equal(classname, "skull")) 
    {
        health = gOrigHealth + get_cvar_num("red_skullhealth")
        set_user_health(ptd, health)
        remove_entity(ptr)
        client_print(ptd, print_chat, "[SH] %L", LANG_PLAYER, "SH_REDSKULL_EATING_SKULL", get_cvar_num("red_skullhealth"))
        set_hudmessage(255, 0, 0, 0.50, 0.25, 0, 6.0, 3.0)
        show_hudmessage(0, "%s got +%i HP by eating a Skull!", name, get_cvar_num("red_skullhealth"))
    }
    if(equal(classname, "chocolate")) 
    {
        health = gOrigHealth + get_cvar_num("red_chocohealth")
        set_user_health(ptd, health)
        remove_entity(ptr)
        client_print(ptd, print_chat, "[SH] %L", LANG_PLAYER, "SH_REDSKULL_EATING_CHOCOLATE", get_cvar_num("red_chocohealth"))
        set_hudmessage(0, 255, 0, 0.50, 0.25, 0, 6.0, 3.0)
        show_hudmessage(0, "%s got +%i HP by eating a Chocolate!", name, get_cvar_num("red_chocohealth"))
    }
    return PLUGIN_CONTINUE
}
//--------------------------------------------------------------------------------------------------
public redskull_death()
{
    new id = read_data(2)

    if ( gBetweenRounds ) return
    if ( !is_user_connected(id) || !gHasRedPower[id] ) return

    new randNum = random_num(0, 100)
    new pctChance = 70
    if ( pctChance < randNum ) return

    gUserTeam[id] = get_user_team(id)
    // Look for self to raise from dead
    if ( !is_user_alive(id) ) {
        // Red Skull will raise self from dead
        new parm[1]
        parm[0] = id
        // Respawn him faster then Zeus, let this power be used before Zues's
        // never set higher then 1.9 or lower then 0.5
        set_task(0.8, "red_respawn", 0, parm, 1)
    }
}
//--------------------------------------------------------------------------------------------------
public red_respawn(parm[])
{
    new id = parm[0]

    if ( !is_user_connected(id) || is_user_alive(id) ) return
    if ( gBetweenRounds ) return
    if ( gUserTeam[id] != get_user_team(id) ) return //prevents respawning spectators

    emit_sound(id, CHAN_STATIC, "ambience/particle_suck1.wav", 1.0, ATTN_NORM, 0, PITCH_HIGH)

    // Double spawn prevents the no HUD glitch
    cs_set_user_model(id, "redskull")

    set_task(1.0, "red_teamcheck", 0, parm, 1)
}
//--------------------------------------------------------------------------------------------------
public red_teamcheck(parm[])
{
    new id = parm[0]

    if ( gUserTeam[id] != get_user_team(id) ) {
        client_print(id, print_chat, "[SH] %L", LANG_PLAYER, "SH_REDSKULL_YOU_CHANGED")
        user_kill(id, 1)
        cs_reset_user_model(id)
        // Stop from respawning until round ends
        remove_task(177+id)
    }
}
//--------------------------------------------------------------------------------------------------
public round_end()
{
    if ( !shModActive() ) return

    gBetweenRounds = true
    // Reset the cooldown on round end, to start fresh for a new round
    for ( new id = 1; id <= SH_MAXSLOTS; id++ ) {
        if ( gHasRedPower[id] ) {
            remove_task(177+id)
        }
    }
}
//--------------------------------------------------------------------------------------------------
public client_disconnected(id)
{
    // stupid check but lets see
    if ( id <= 0 || id > SH_MAXSLOTS ) return
    // Yeah don't want any left over residuals
    remove_task(id)
    gHasRedPower[id] = false
}
//--------------------------------------------------------------------------------------------------
public client_connect(id)
{
    gHasRedPower[id] = false
}
//--------------------------------------------------------------------------------------------------