/**
 *  Janzert's Economic Team Balancer 
 *
 *  Gives the losing team extra money to help even the game.
 *
 *  The cvar extramoney can be set to 0 to disable giving out money.
 *
 *  0.5
 *    Initial public release
 *
 *  Plugin developed by Janzert aka Brian Haskin
 *
 *  Copyright 2004 Brian Haskin
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software Foundation,
 *  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *  In addition, as a special exception, the author gives permission to
 *  link the code of this program with the Half-Life Game Engine ("HL
 *  Engine") and Modified Game Libraries ("MODs") developed by Valve,
 *  L.L.C ("Valve"). You must obey the GNU General Public License in all
 *  respects for all of the code used other than the HL Engine and MODs
 *  from Valve. If you modify this file, you may extend this exception
 *  to your version of the file, but you are not obligated to do so. If
 *  you do not wish to do so, delete this exception statement from your
 *  version.
 **/

#include <amxmodx>
#include <cstrike>

#define AWARD_LEVELS 5
#define MAX_AWARD 9000

new const award[] = {0, 1000, 2500, 3500, 4500, 5500}
new score[] = {0, 0}
new scale = 0

public teamscore()
{
  new team[2]
  read_data(1,team,1)
  new curTeam = (team[0] == 'C') ? 0 : 1
  new curScore = read_data(2)

  if (curScore == score[curTeam])
    return PLUGIN_CONTINUE

  score[curTeam] = curScore

  // Keep a sliding scale of losses clamped to 7
  // Scale is positive when CT's are winning
  if (curTeam)
  {
    scale += 1
  } else
  {
    scale -= 1
  }
  scale = clamp(scale, 0-AWARD_LEVELS, AWARD_LEVELS)
  log_message("「平安夜公告」 當前按物價計酬法 %d.", scale)

  if (!get_cvar_num("extramoney"))
    return PLUGIN_CONTINUE
  
  // find the pot amount by converting the sliding scale to an index
  // in the award lookup table
  new pindex = scale
  if (!curTeam)
    pindex = 0-pindex

  new pot = award[clamp(pindex-1, 0, 5)]
  if (!pot)
    return PLUGIN_CONTINUE

  new players[32];
  new playerCount, i
  get_players(players, playerCount, "ce", curTeam ? "CT" : "TERRORIST")
  for (i=0;i < playerCount; i++)
  {
    new money = cs_get_user_money(players[i])
    new newmoney = max(clamp(money+pot, 0, MAX_AWARD), money) // Only give extra money up to 8000
    cs_set_user_money(players[i], newmoney, 1)
  }

  return PLUGIN_CONTINUE
}

public plugin_init()
{
  register_plugin("JETB", "0.5", "Janzert")
  register_cvar("extramoney", "1")
  register_event("TeamScore", "teamscore", "a");

  return PLUGIN_CONTINUE
}
