#include extreme\_ex_punishments;

init()
{
  //Main Function that loops looking for admin requests

  // Clears command cvars, just incase
  setcvar("team", "");
  setcvar("disableweapon", "");
  setcvar("enableweapon", "");
  setcvar("funmode", "");
  setcvar("mattress", "");
  setcvar("barrel", "");
  setcvar("bathtub", "");
  setcvar("cheater", "");
  setcvar("toilet", "");
  setcvar("tree", "");
  setcvar("tomb", "");
  setcvar("original", "");
  setcvar("warp", "");
  setcvar("lock", "");
  setcvar("unlock", "");
  setcvar("suicide", "");
  setcvar("smite", "");
  setcvar("zombie", "");
  setcvar("torch", "");
  setcvar("fire", "");
  setcvar("spank", "");
  setcvar("arty", "");
  setcvar("sayall", "");
  setcvar("sayallcenter", "");
  setcvar("switchplayerallies", "");
  setcvar("switchplayeraxis", "");
  setcvar("switchplayerspec", "");
  setcvar("switchsidesallplayers", "");

  v_FunmodePlayer = undefined;
  v_MattressPlayer = undefined;
  v_BarrelPlayer = undefined;
  v_BathtubPlayer = undefined;
  v_ToiletPlayer = undefined;
  v_TreePlayer = undefined;
  v_TombPlayer = undefined;
  v_OriginalPlayer = undefined;

  //make sure only running once
  if(isDefined(level.ex_cmdmonon)) return;

  level.ex_cmdmonon = true;

  while(!level.ex_gameover)
  {
    v_DisableWeaponPlayer = getcvar("disableweapon");
    v_EnableWeaponPlayer = getcvar("enableweapon");

    if(level.ex_cmdmonitor_models)
    {
      v_FunmodePlayer = getcvar("funmode");
      v_MattressPlayer = getcvar("mattress");
      v_BarrelPlayer = getcvar("barrel");
      v_BathtubPlayer = getcvar("bathtub");
      v_ToiletPlayer = getcvar("toilet");
      v_TreePlayer = getcvar("tree");
      v_TombPlayer = getcvar("tombstone");
      v_OriginalPlayer = getcvar("original");
    }

    v_WarpPlayer = getcvar("warp");
    v_LockPlayer = getcvar("lock");
    v_CheaterPlayer = getcvar("cheater");
    v_UnLockPlayer = getcvar("unlock");
    v_SuicidePlayer = getcvar("suicide");
    v_SmitePlayer = getcvar("smite");
    v_ZombiePlayer = getcvar("zombie");
    v_TorchPlayer = getcvar("torch");
    v_FirePlayer = getcvar("fire");
    v_SpankPlayer = getcvar("spank");
    v_ArtyPlayer = getcvar("arty");
    v_SayAll = getcvar("sayall");
    v_SayAllCenter = getcvar("sayallcenter");
    v_SwitchPlayerAllies = getcvar("switchplayerallies");
    v_SwitchPlayerAxis = getcvar("switchplayeraxis");
    v_SwitchPlayerSpectator = getcvar("switchplayerspec");
    v_SwitchSidesAllPlayers = getcvar("switchsidesallplayers");

    if(v_DisableWeaponPlayer != "") thread setStatusweaponPlayer(v_DisableWeaponPlayer, true);
    if(v_EnableWeaponPlayer != "") thread setStatusweaponPlayer(v_EnableWeaponPlayer, false);

    if(level.ex_cmdmonitor_models)
    {
      if(v_FunmodePlayer != "") thread changePlayerModel(v_FunmodePlayer, "funmode");
      if(v_MattressPlayer != "") thread changePlayerModel(v_MattressPlayer, "mattress");
      if(v_BarrelPlayer != "") thread changePlayerModel(v_BarrelPlayer, "barrel");
      if(v_BathtubPlayer != "") thread changePlayerModel(v_BathtubPlayer, "bathtub");
      if(v_ToiletPlayer != "") thread changePlayerModel(v_ToiletPlayer, "toilet");
      if(v_TreePlayer != "") thread changePlayerModel(v_TreePlayer, "tree");
      if(v_TombPlayer != "") thread changePlayerModel(v_TombPlayer, "tombstone");
      if(v_OriginalPlayer != "") thread changePlayerModel(v_OriginalPlayer, "original");
    }

    if(v_WarpPlayer != "") thread messWithPlayer(v_WarpPlayer, "warp");
    if(v_LockPlayer != "") thread messWithPlayer(v_LockPlayer, "lock");
    if(v_CheaterPlayer != "") thread messWithPlayer(v_CheaterPlayer, "cheater");
    if(v_UnLockPlayer != "") thread messWithPlayer(v_UnLockPlayer, "unlock");
    if(v_SuicidePlayer != "") thread messWithPlayer(v_SuicidePlayer, "suicide");
    if(v_SmitePlayer != "") thread messWithPlayer(v_SmitePlayer, "smite");
    if(v_ZombiePlayer != "") thread messWithPlayer(v_ZombiePlayer, "zombie");
    if(v_TorchPlayer != "") thread messWithPlayer(v_TorchPlayer, "torch");
    if(v_FirePlayer != "") thread messWithPlayer(v_FirePlayer, "fire");
    if(v_SpankPlayer != "") thread messWithPlayer(v_SpankPlayer, "spank");
    if(v_ArtyPlayer != "") thread messWithPlayer(v_ArtyPlayer, "arty");

    if(v_SayAll != "") thread sayAll(v_SayAll, 0);
    if(v_SayAllCenter != "") thread sayAll(v_SayAllCenter, 1);
    if(v_SwitchPlayerAllies != "") thread switchSide(v_SwitchPlayerAllies, "allies", 1, true);
    if(v_SwitchPlayerAxis != "") thread switchSide(v_SwitchPlayerAxis, "axis", 1, true);
    if(v_SwitchPlayerSpectator != "") thread switchSide(v_SwitchPlayerSpectator, "spectator", 1, true);
    if(v_switchSidesAllPlayers != "") thread switchSides();

    wait 5;
  }
}

setStatusweaponPlayer(PlayerEntID, lever)
{
  //first clear the buffer
  if(lever) setcvar("disableweapon", "");
  else setcvar("enableweapon", "");

  // Do we want to set teams
  v_team = getcvar("team");
  setcvar("team", "");

  playerEntID = int(PlayerEntID);
  players = getentarray("player", "classname");
  for (i = 0; i < players.size; i++)
  {
    player = players[i];
    entID = player getEntityNumber();

    if((entID == playerEntID) || (playerEntID == -1))
    {
      if(lever)
      {
        if(isAlive(player))
        {
          player thread setWeaponStatus(v_team);
          player iprintlnbold(&"CMDMONITOR_DISABLEWEAPONS");
          iprintln(&"CMDMONITOR_DISABLEWEAPONSB", player);
          wait 1;
        }
      }
      else
      {
        if(isAlive(player))
        {
          player thread setWeaponStatus(v_team);
          player iprintlnbold(&"CMDMONITOR_ENABLEWEAPONS");
          iprintln(&"CMDMONITOR_ENABLEWEAPONSB", player);
          wait 1;
        }
      }
    }
  }
}

changePlayerModel(PlayerEntID, mode)
{
  //first clear the buffer
  setcvar(mode, "");

  // Do we want to set teams
  v_team = getcvar("team");
  setcvar("team", "");

  models = [];
  pmsg = undefined;
  amsg = undefined;

  // Setup model's
  switch(mode)
  {
    case "funmode":
    models[0] = "xmodel/furniture_piano";
    models[1] = "xmodel/prop_bike";
    models[2] = "xmodel/prop_haybale";
    models[3] = "xmodel/prop_hydrant";
    models[4] = "xmodel/prop_tractor";
    pmsg = &"CMDMONITOR_FUNMODE";
    amsg = &"CMDMONITOR_FUNMODEB";
    break;

    case "mattress":
    models[0] = "xmodel/furniture_bedmattress1";
    models[1] = "xmodel/furniture_bedmattress_bunker";
    models[2] = "xmodel/furniture_bedmattress2";
    pmsg = &"CMDMONITOR_MATTRESS";
    amsg = &"CMDMONITOR_MATTRESSB";
    break;

    case "barrel":
    models[0] = "xmodel/prop_barrel_benzin";
    models[1] = "xmodel/prop_barrel_black";
    models[2] = "xmodel/prop_barrel_green";
    models[3] = "xmodel/prop_barrel_silver";
    models[4] = "xmodel/prop_barrel_tan";
    models[5] = "xmodel/prop_barrel_white";
    pmsg = &"CMDMONITOR_BARREL";
    amsg = &"CMDMONITOR_BARRELB";
    break;

    case "bathtub":
    models[0] = "xmodel/furniture_bathtub";
    pmsg = &"CMDMONITOR_BATHTUB";
    amsg = &"CMDMONITOR_BATHTUBB";
    break;

    case "toilet":
    models[0] = "xmodel/furniture_toilet";
    pmsg = &"CMDMONITOR_TOILET";
    amsg = &"CMDMONITOR_TOILETB";
    break;

    case "tree":
    models[0] = "xmodel/tree_grey_oak_sm_a";
    models[1] = "xmodel/tree_pine_sm_b";
    pmsg = &"CMDMONITOR_TREE";
    amsg = &"CMDMONITOR_TREEB";
    break;

    case "tombstone":
    models[0] = "xmodel/prop_tombstone1";
    models[1] = "xmodel/prop_tombstone2";
    models[2] = "xmodel/prop_tombstone3";
    models[3] = "xmodel/prop_tombstone4";
    models[4] = "xmodel/prop_tombstone5";
    models[5] = "xmodel/prop_tombstone6";
    models[6] = "xmodel/prop_tombstone7";
    models[7] = "xmodel/prop_tombstone8";
    models[8] = "xmodel/prop_tombstone9";
    models[9] = "xmodel/prop_tombstone10";
    models[10] = "xmodel/prop_tombstone11";
    pmsg = &"CMDMONITOR_TOMBSTONE";
    amsg = &"CMDMONITOR_TOMBSTONEB";
    break;

    case "original":
    models[0] = "original";
    pmsg = &"CMDMONITOR_ORIGINAL";
    amsg = &"CMDMONITOR_ORIGINALB";
    break;

    default: return;
  }

  modeltype = models[randomInt(models.size)];

  playerEntID = int(PlayerEntID);
  players = getentarray("player", "classname");
  for (i = 0; i < players.size; i++)
  {
    player = players[i];
    entID = player getEntityNumber();

    if((entID == playerEntID) || (playerEntID == -1))
    {
      if(isAlive(player))
      {
        player thread setPlayerModel(v_team, modeltype);
        player iprintlnbold(pmsg);
        iprintln(amsg, player);
        wait 1;
      }
    }
  }
}

messWithPlayer(PlayerEntID, mode)
{
  //first clear the buffer
  setcvar(mode, "");

  // Do we want to set teams
  v_team = getcvar("team");
  setcvar("team", "");

  pmsg = undefined;
  amsg = undefined;

  playerEntID = int(PlayerEntID);
  players = getentarray("player", "classname");
  for (i = 0; i < players.size; i++)
  {
    player = players[i];
    entID = player getEntityNumber();

    if(playerEntID == -1 && mode == "cheater") return;

    if((entID == playerEntID) || (playerEntID == -1))
    {
      if(isAlive(player))
      {
        switch(mode)
        {
          case "warp":
          player.health = 0;
          player thread setWarp(v_team, player.origin);
          pmsg = &"CMDMONITOR_WARP";
          amsg = &"CMDMONITOR_WARPB";
          break;

          case "lock":
          porigin = player.origin;
          player thread setLockStatus(v_team, player.origin, true);
          pmsg = &"CMDMONITOR_LOCK";
          amsg = &"CMDMONITOR_LOCKB";
          break;

          case "unlock":
          porigin = player.origin;
          player thread setLockStatus(v_team, player.origin, false);
          pmsg = &"CMDMONITOR_UNLOCK";
          amsg = &"CMDMONITOR_UNLOCKB";
          break;

          case "suicide":
          player thread setSuicide(v_team);
          pmsg = &"CMDMONITOR_SUICIDE";
          amsg = &"CMDMONITOR_SUICIDEB";
          break;

          case "smite":
          porigin = player.origin;
          player thread setSmite(v_team, player.origin);
          pmsg = &"CMDMONITOR_SMITE";
          amsg = &"CMDMONITOR_SMITEB";
          break;

          case "zombie":
          if(!player.ex_zombie)
          {
            player thread setZombie(v_team, player.origin);
            pmsg = &"CMDMONITOR_ZOMBIE";
            amsg = &"CMDMONITOR_ZOMBIEB";
            break;
          }
          else return;

          case "torch":
          player thread setTorch(v_team, player.origin);
          pmsg = &"CMDMONITOR_TORCH";
          amsg = &"CMDMONITOR_TORCHB";
          break;

          case "fire":
          player thread setFire(v_team);
          pmsg = &"CMDMONITOR_FIRE";
          amsg = &"CMDMONITOR_FIREB";
          break;

          case "spank":
          player thread setSpank(v_team);
          pmsg = &"CMDMONITOR_SPANK";
          amsg = &"CMDMONITOR_SPANKB";
          break;

          case "cheater":
          player thread setCheater();
          pmsg = &"CMDMONITOR_CHEATERA";
          amsg = &"CMDMONITOR_CHEATERB";
          break;

          case "arty":
          player thread setArty(v_team);
          pmsg = &"CMDMONITOR_ARTY_SELF";
          amsg = &"CMDMONITOR_ARTY_ALL";
          break;

          default: return;
        }

        player iprintlnbold(pmsg);
        iprintln(amsg, player);
        wait 1;
      }
    }
  }
}

sayAll(Message, CenterScreen)
{
  if(CenterScreen == 1)
  {
    iprintlnbold(Message);
    setcvar("sayallcenter", "");
  }
  else
  {
    iprintln(Message);
    setcvar("sayall", "");
  }
}

switchSides()
{
  //first clear the buffer
  setcvar("switchsidesallplayers", "");

  // Make the announcement to all players
  iprintln(&"CMDMONITOR_SWITCHSIDESA");

  wait 2;

  players = getentarray("player", "classname");
  for (i = 0; i < players.size; i++)
  {
    player = players[i];

    if (player.pers["team"] == "allies")
    {
      entID = player getEntityNumber();
      thread switchSide(entID, "axis", 1, true);
    }
    else if (player.pers["team"] == "axis")
    {
      entID = player getEntityNumber();
      thread switchSide(entID, "allies", 1, true);
    }
  }
}

switchSide(playerEntID, side, announce, keepscore)
{
  playerEntID = int(PlayerEntID);

  // first find the player
  players = getentarray("player", "classname");
  for (i = 0; i < players.size; i++)
  {
    player = players[i];
    entID = player getEntityNumber();

    if((entID == playerEntID) || (playerEntID == -1))
    {
      //This is the player to move
      if(side != player.pers["team"])
      {
        if(isAlive(player))
          player.ex_forcedsuicide = true;
          player suicide();

        //give player point back if keep score set
        if(isDefined(keepscore) && keepscore)
        {
          player.score++;
          teamscore = getTeamScore(player.pers["team"]);
          teamscore++;
          setTeamScore(player.pers["team"], teamscore);
        }

        player.pers["team"] = side;
        player.pers["weapon"] = undefined;
        player.pers["weapon1"] = undefined;
        player.pers["weapon2"] = undefined;
        player.pers["spawnweapon"] = undefined;
        player.pers["savedmodel"] = undefined;
        player.grenadecount = undefined;

        switch (side)
        {
          case "spectator":
          setcvar("switchplayerspec", "");

          player.pers["teamTime"] = 0;
          player.sessionteam = "spectator";
          player setClientCvar("g_scriptMainMenu", game["menu_team"]);
          player setClientCvar("ui_weapontab", "0");
          player thread extreme\_ex_spawn::spawnSpectator();
          break;

          case "allies":
          case "axis":
          setcvar("switchplayerallies", "");
          setcvar("switchplayeraxis", "");

          player thread maps\mp\gametypes\_spectating::setSpectatePermissions();
          player setClientCvar("ui_weapontab", "1");

          if(player.pers["team"] == "allies")
          {
            player setClientCvar("g_scriptMainMenu", game["menu_weapon_allies"]);
            player openMenu(game["menu_weapon_allies"]);
          }
          else
          {
            player setClientCvar("g_scriptMainMenu", game["menu_weapon_axis"]);
            player openMenu(game["menu_weapon_axis"]);
          }

          break;
        }

        if(announce)
        {
          if(playerEntID != -1) iprintln(&"CMDMONITOR_SWITCHSIDESB", player);
          player iprintlnbold(&"CMDMONITOR_SWITCHSIDESC");
        }
      }
    }
  }
}
