// IRON MAN! - BASED ON JETPACK By Lazy / Modifications by OLO

/* CVARS - copy and paste to shconfig.cfg

//Iron Man
ironman_level 0
ironman_armor 100        //How much armor does ironman start with?
ironman_timer 0.1        //How often (seconds) to run the loop
ironman_thrust 125        //The upward boost every loop
ironman_maxspeed 400        //Max x and y speeds (while in air)
ironman_xymult 1.05        //Multiplies the current x,y vector when moving
ironman_fuelcost 1        //How much armor does it cost per firing, set 0 to not use armor

*/

#include <superheromod>

// GLOBAL VARIABLES
new gHeroID
new const gHeroName[] = "Iron Man"
new bool:gRegenAllowed[SH_MAXSLOTS+1]
new bool:gJetPackRunning[SH_MAXSLOTS+1]
new const gSoundJetpack[] = "ambience/flameburst1.wav"
new const gSoundJpStop[] = "debris/beamstart11.wav"
new gSpriteFire

// BECAUSE THIS LOOP IS CALLED SO MUCH - INSTEAD OF READING CVARS OVER AND OVER
// I'LL KEEP IN GLOBAL - FOR ANTI-LAG HOPEFULLY
new Float:gThrust, Float:gMaxSpeed, Float:gMultiplier, gFuelCost
new pCvarTimer, pCvarThrust, pCvarMaxSpeed, pCvarXyMult, pCvarFuelCost
//----------------------------------------------------------------------------------------------
public plugin_init()
{
    // Plugin Info
    register_plugin("SUPERHERO Iron Man", SH_VERSION_STR, "{HOJ} Batman/JTP10181")

    register_dictionary("sh_ironman.txt")

    // DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
    new pcvarLevel = register_cvar("ironman_level", "0")
    new pcvarArmor = register_cvar("ironman_armor", "100")
    pCvarTimer = register_cvar("ironman_timer", "0.1")
    pCvarThrust = register_cvar("ironman_thrust", "125")
    pCvarMaxSpeed = register_cvar("ironman_maxspeed", "400")
    pCvarXyMult = register_cvar("ironman_xymult", "1.05")
    pCvarFuelCost = register_cvar("ironman_fuelcost", "1")

    // FIRE THE EVENTS TO CREATE THIS SUPERHERO!
    gHeroID = sh_create_hero(gHeroName, pcvarLevel)
    sh_set_hero_info(gHeroID, "Rocket Pack", "Rocket Jetpack - use +power key to take off")
    sh_set_hero_bind(gHeroID)
    sh_set_hero_hpap(gHeroID, _, pcvarArmor)
}
//----------------------------------------------------------------------------------------------
public plugin_precache()
{
    gSpriteFire = precache_model("sprites/fire.spr")
    precache_sound(gSoundJetpack)
    precache_sound(gSoundJpStop)
}
//----------------------------------------------------------------------------------------------
public sh_hero_init(id, heroID, mode)
{
    if ( gHeroID != heroID ) return

    // Clear out any stale tasks
    remove_task(id)

    if ( mode == SH_HERO_ADD ) {
        set_task(get_pcvar_float(pCvarTimer), "ironman_loop", id, _, _, "b")
    }

    sh_debug_message(id, 1, "%s %s", gHeroName, mode ? "ADDED" : "DROPPED")
}
//----------------------------------------------------------------------------------------------
public plugin_cfg()
{
    loadCVARS()
}
//----------------------------------------------------------------------------------------------
loadCVARS()
{
    gThrust = get_pcvar_float(pCvarThrust)
    gMaxSpeed = get_pcvar_float(pCvarMaxSpeed)
    gMultiplier = get_pcvar_float(pCvarXyMult)
    gFuelCost = get_pcvar_num(pCvarFuelCost)
}
//----------------------------------------------------------------------------------------------
jetpack_fire_effect(location[3])
{
    message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
    write_byte(TE_SPRITE)        // 17
    write_coord(location[0])
    write_coord(location[1])
    write_coord(location[2])
    write_short(gSpriteFire)
    write_byte(5)
    write_byte(125)
    message_end()
}
//----------------------------------------------------------------------------------------------
public ironman_loop(id)
{
    if ( !sh_is_active() || !is_user_alive(id) || !gRegenAllowed[id] ) return

    static CsArmorType:armorType
    static userArmor
    userArmor = cs_get_user_armor(id, armorType)
    if ( userArmor == 0 ) armorType = CS_ARMOR_VESTHELM

    switch(gJetPackRunning[id]) {
        case false: {
            // Regen armor even if armor is not used for JP fuel
            if ( userArmor < sh_get_max_ap(id) ) {
                cs_set_user_armor(id, userArmor + 1, armorType)
            }
        }

        case true: {
            // Is armor/fuel being used to run JP?
            if ( gFuelCost > 0 ) {
                // If no fuel is available stop JP
                if ( userArmor < gFuelCost ) {
                    sh_sound_deny(id)
                    gJetPackRunning[id] = false

                    // This needs to change to a forward check
                    set_user_info(id, "JP", "0")

                    emit_sound(id, CHAN_WEAPON, gSoundJpStop, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
                    client_print(id, print_center, "[SH] %L", LANG_PLAYER, "SH_IRONMAN_OUT_OF")
                    return
                }

                // Decrement fuel when JP in use
                cs_set_user_armor(id, userArmor - gFuelCost, armorType)
            }

            static Float:velocity[3], origin[3]

            pev(id, pev_velocity, velocity)

            // Remove the need to jump to use, give them a small upward lift
            if ( pev(id, pev_flags) & FL_ONGROUND ) {
                velocity[2] += 100.0
                set_pev(id, pev_velocity, velocity)
            }

            velocity[0] = velocity[0] * gMultiplier
            velocity[1] = velocity[1] * gMultiplier
            velocity[2] += gThrust

            if ( velocity[0] > gMaxSpeed )        velocity[0] = gMaxSpeed
            else if ( velocity[0] < (gMaxSpeed * -1.0) )    velocity[0] = gMaxSpeed * -1.0

            if ( velocity[1] > gMaxSpeed )        velocity[1] = gMaxSpeed
            else if ( velocity[1] < (gMaxSpeed * -1.0) )    velocity[1] = gMaxSpeed * -1.0

            if ( velocity[2] > gThrust * 2.0 )    velocity[2] = gThrust * 2.0

            set_pev(id, pev_velocity, velocity)

            get_user_origin(id, origin)

            jetpack_fire_effect(origin)

            emit_sound(id, CHAN_WEAPON, gSoundJetpack, VOL_NORM, ATTN_NORM, 0, PITCH_LOW)
        }
    }
}
//----------------------------------------------------------------------------------------------
public sh_hero_key(id, heroID, key)
{
    if ( gHeroID != heroID ) return

    switch(key)
    {
        case SH_KEYDOWN: {
            if ( sh_is_freezetime() || !is_user_alive(id) ) return

            gJetPackRunning[id] = true

            // This needs to change to a forward check
            set_user_info(id, "JP", "1")

            //Reload CVARS to make sure the variables are current
            loadCVARS()
        }

        case SH_KEYUP: {
            if ( !gJetPackRunning[id] ) return

            gJetPackRunning[id] = false

            // This needs to change to a forward check
            set_user_info(id, "JP", "0")

            emit_sound(id, CHAN_WEAPON, gSoundJpStop, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
        }
    }
}
//----------------------------------------------------------------------------------------------
public sh_client_death(victim)
{
    gJetPackRunning[victim] = false

    // This needs to change to a forward check
    set_user_info(victim, "JP", "0")

    gRegenAllowed[victim] = false
}
//----------------------------------------------------------------------------------------------
public sh_client_spawn(id)
{
    gRegenAllowed[id] = false

    set_task(1.0, "spawn_delay", id)
}
//----------------------------------------------------------------------------------------------
public spawn_delay(id)
{
    //Delay is to allow time for sh armor power to set.
    if ( !is_user_alive(id) ) return

    gRegenAllowed[id] = true
}
//----------------------------------------------------------------------------------------------
public client_disconnected(id)
{
    // stupid check but lets see
    if ( id < 1 || id > sh_maxplayers() ) return

    gJetPackRunning[id] = false

    // This needs to change to a forward check
    set_user_info(id, "JP", "0")

    gRegenAllowed[id] = false

    // Yeah don't want any left over residuals
    remove_task(id)
}
//----------------------------------------------------------------------------------------------
public client_connect(id)
{
    // stupid check but lets see
    if ( id < 1 || id > sh_maxplayers() ) return

    gJetPackRunning[id] = false

    // This needs to change to a forward check
    set_user_info(id, "JP", "0")

    gRegenAllowed[id] = false

    // Yeah don't want any left over residuals
    remove_task(id)
}
//----------------------------------------------------------------------------------------------