/* AMX Mod script. 
* 
* AMX wounded v0.1.3
* (c) Copyright 2003, [#NL]Riedel
* This file is provided as is (no warranties). 
*
* Get null.wav from your cstrike\sound\common dir.
* 
*/ 

#include <amxmod> 
#include <fun>

new bool:bandaided[32]
new wallet[32] = 800

/* Set tasks to be performed */
public wounded_init(){
  set_task(10.0, "wound_folks",23,"",0,"b")
  set_task(1.0, "reset_speed", 23,"",0,"b")
}

/* Reset client maxspeed every second */
public reset_speed(){
  new players[32], inum
  new user_health = 100
  get_players(players,inum,"a")
  for(new i = 0 ;i < inum ;++i) {
    user_health = get_user_health(players[i])
    if (user_health < 6) {
      if (!bandaided[players[i]]) {
        set_user_maxspeed(players[i],120.0)
      }
      else {
        set_user_maxspeed(players[i],180.0)
      }
    }
    else if (user_health < 11) {
      if (!bandaided[players[i]]) {
        set_user_maxspeed(players[i],130.0)
      }
      else {
        set_user_maxspeed(players[i],185.0)
      }
    }
    else if (user_health < 16) {
      if (!bandaided[players[i]]) {
        set_user_maxspeed(players[i],140.0)
      }
      else {
        set_user_maxspeed(players[i],190.0)
      }
    }
    else if (user_health < 19) {
      if (!bandaided[players[i]]) {
        set_user_maxspeed(players[i],150.0)
      }
      else {
        set_user_maxspeed(players[i],195.0)
      }
    }
    else if (user_health < 21) {
      if (!bandaided[players[i]]) {
        set_user_maxspeed(players[i],160.0)
      }
      else {
        set_user_maxspeed(players[i],200.0)
      }
    }
    else if (user_health < 30) {
      if (!bandaided[players[i]]) {
        set_user_maxspeed(players[i],170.0)
      }
      else {
        set_user_maxspeed(players[i],205.0)
      }
    }
    else if (user_health < 40) {
      if (!bandaided[players[i]]) {
        set_user_maxspeed(players[i],180.0)
      }
      else {
        set_user_maxspeed(players[i],210.0)
      }
    }
    else if (user_health < 50) {
      if (!bandaided[players[i]]) {
        set_user_maxspeed(players[i],190.0)
      }
      else {
        set_user_maxspeed(players[i],215.0)
      }
    }
  }
}

/* Handles the purchasing of bandaids */
public buy_bandaid(id){
  new user_health = 100
  new user_money = 0
  new needed_money = 0
  new ammount = 0
  user_health = get_user_health(id)
  if (is_user_alive(id) == 0) {
    client_print(id,print_chat,"[AMX] Bandaid does NOT look pretty on a corpse.")
  }
  else if (user_health > 49) {
    client_print(id,print_chat,"[AMX] You are not in need for a bandaid.")
  }
  else if (!bandaided[id]) {
    user_money = wallet[id]
    ammount = (50-user_health)
    needed_money = (ammount*20)
    if (user_money < needed_money) {
      client_print(id,print_chat,"[AMX] You need atleast $ %d to bandaid your injuries", needed_money)
      client_print(id,print_chat,"[AMX] You only have $ %d in your pocket. ", user_money)
    }
    else {
      set_user_money(id,user_money-needed_money,1)
      bandaided[id] = true
      emit_sound(id,CHAN_VOICE,"common/null.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)
      client_print(id,print_chat,"[AMX] Feeling better ? That costed you $ %d", needed_money)
    }
  }
  else {
    client_print(id,print_chat,"[AMX] You've already bought a bandaid.")
  }
  return PLUGIN_HANDLED
}

/* Simulate bleeding to death */
public wound_folks(){
  new players[32], inum
  new user_health
  user_health = 100
  get_players(players,inum,"a")
  for(new i = 0 ;i < inum ;++i) {
    if (!bandaided[players[i]]) {
      user_health = get_user_health(players[i])
      if ((user_health < 21) && (user_health > 0)) {
        if (user_health == 1) {
          emit_sound(players[i],CHAN_VOICE,"player/die1.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)
          client_print(players[i],print_chat,"[AMX] Unfortunately you died of your injuries.")
          set_user_health(players[i],user_health-1)
        }
        else if (user_health == 5) {
          client_print(players[i],print_chat,"[AMX] You are dying! Hurry up!")
          emit_sound(players[i],CHAN_VOICE,"player/breathe1.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)
          set_user_health(players[i],user_health-1)
        }
        else {
          emit_sound(players[i],CHAN_VOICE,"player/breathe1.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)
          set_user_health(players[i],user_health-1)
          user_health = get_user_health(players[i])
          if (user_health == 1) {
            client_print(players[i],print_chat,"[AMX] Can you see THE LIGHT yet ?")
          }
        }
      }
    }
  }
}

/* Prevent the gasping from looping */
public round_start_stuff(){
  new players[32], inum
  get_players(players,inum,"a")
  for(new i = 0 ;i < inum ;++i) {
    emit_sound(players[i],CHAN_VOICE,"common/null.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)
    set_user_maxspeed(players[i],240.0)
  }
  for(new i = 0 ; i < 33 ;++i) {
    bandaided[i] = false
  }
}

/* Precache sounds */
public plugin_precache(){
  precache_sound( "player/breathe1.wav")
  precache_sound( "player/die1.wav");
  precache_sound( "common/null.wav")
}

/* Track how much cash one has. */
public track_cash(id) {
  new in_pocket = read_data(1)
  wallet[id] = in_pocket
  return PLUGIN_CONTINUE
}

/* Reset bandaided[id] upon connect */
public client_connect(id) {
  bandaided[id] = false
  return PLUGIN_CONTINUE
}

/* plugin_init () */
public plugin_init() 
{ 
  register_plugin("amx_wounded","0.1.3","[NL]Riedel")
  register_concmd("bandaid","buy_bandaid",ADMIN_USER,"  Buy a bandaid when heavily hurt.")
  register_event("Money","track_cash","b")
  register_clcmd("say bandaid","buy_bandaid",0,"- Buy a bandaid when heavily hurt.")
  register_event("CurWeapon","reset_speed","be","1=1")
  register_event("SendAudio","round_start_stuff","bc","2=%!MRAD_GO","2=%!MRAD_MOVEOUT","2=%!MRAD_LETSGO","2=%!MRAD_LOCKNLOAD")
  wounded_init()
  return PLUGIN_CONTINUE 
}
