
setWeaponStatus(v_team, lever)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doWeaponStatusChange(lever);
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doWeaponStatusChange(lever);
      else if(v_team == "" && self.sessionstate == "playing") self thread doWeaponStatusChange(lever);
}

doWeaponStatusChange(lever)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(lever) self disableWeapon();
    else self enableWeapon();
}

setPlayerModel(v_team, modeltype)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(modeltype == "original")
  {
    self thread maps\mp\gametypes\_teams::model();
    if(isdefined(self.ex_newmodel)) self.ex_newmodel = undefined;
    return;
  }

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doModelChange(modeltype);
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doModelChange(modeltype);
      else if(v_team == "" && self.sessionstate == "playing") self thread doModelChange(modeltype);
}

doModelChange(modeltype)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  self detachall();
  self setModel(modeltype);
  self.ex_newmodel = true;
}

setWarp(v_team, playerpos)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doWarp(playerpos);
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doWarp(playerpos);
      else if(v_team == "" && self.sessionstate == "playing") self thread doWarp(playerpos);
}

doWarp(playerpos)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  self.origin = playerpos;
  ix = self.origin[0];
  iy = self.origin[1];
  iz =  960; // 80ft drop

  startpoint = self.origin + (0, 0, 24);
  trace = bulletTrace(startpoint, (startpoint + (0, 0, iz)), false, undefined);
  pos = trace["position"];
  iz = pos[2];

  endpoint = (ix, iy, iz);
  distance = distance(startpoint, endpoint);

  self.ex_anchor = spawn("script_model",(0,0,0));
  self.ex_anchor.origin = self.origin;
  self.ex_anchor.angles = self.angles;
  self linkto(self.ex_anchor);

  lifttime = distance/100 + randomint(6);
  self.ex_anchor.origin = startpoint;
  self.ex_anchor moveto(endpoint, lifttime);

  if(level.ex_flagbased && isDefined(self.flag)) self maps\mp\gametypes\ctf::dropflag();
  self maps\mp\gametypes\_weapons::dropWeapon();

  wait(3); // Allow player to read the command monitor message
  self disableweapon();
	[[level.ex_clearlnb]]("self", 5);
	
  svrrules = warpJustNumbers(level.ex_svrrules);
  self thread warpShowRules(svrrules);

  self waittill("warp_over");
  
  if(isPlayer(self))
  {
    self unlink();
    self.health = 1;
    if(isDefined(self.ex_anchor)) self.ex_anchor delete();
  }

  wait(3);

  // huh? Still here? Stupid map! Blow them up
  if(isPlayer(self) && self.sessionstate == "playing")
  {
    playfx(level.ex_effect["barrel"], self.origin);
    self playsound("mortar_explosion1");
    wait(0.05);
    self.ex_cmdmondeath = true;
    if(isPlayer(self)) self suicide();
  }
}

warpShowRules(svrrules)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  self iprintlnbold(&"CUSTOM_SERVER_RULES_FAIL");
  wait(3);
  for(i = 1; i < svrrules.size; i++)
  {
    ruleno = int(svrrules[i]);
    showrule = warpGetRule(i);
    self iprintlnbold(showrule);
    wait(3);
  }
  self iprintlnbold(&"CUSTOM_SERVER_RULES_WARN");
  wait(3);

  self notify("warp_over");
}

warpGetRule(ruleno)
{
  rulestr = "";
  
  switch(ruleno)
  {
    case 1: { rulestr = &"CUSTOM_SERVER_RULE_1"; break; }
    case 2: { rulestr = &"CUSTOM_SERVER_RULE_2"; break; }
    case 3: { rulestr = &"CUSTOM_SERVER_RULE_3"; break; }
    case 4: { rulestr = &"CUSTOM_SERVER_RULE_4"; break; }
    case 5: { rulestr = &"CUSTOM_SERVER_RULE_5"; break; }
    case 6: { rulestr = &"CUSTOM_SERVER_RULE_6"; break; }
    case 7: { rulestr = &"CUSTOM_SERVER_RULE_7"; break; }
    case 8: { rulestr = &"CUSTOM_SERVER_RULE_8"; break; }
    case 9: { rulestr = &"CUSTOM_SERVER_RULE_9"; break; }
    case 0: { rulestr = &"CUSTOM_SERVER_RULE_10"; break; }
  }
  return rulestr;
}

warpJustNumbers(strIn)
{
	if(!isDefined(strIn) || strIn == "") return "";

	numbers = "0123456789";
	strOut = "";

	for(i = 0; i < strIn.size; i++)
	{
		chr = strIn[i];
		for(j = 0; j < numbers.size; j++)
		{
			if(chr == numbers[j]) strOut += numbers[j];
		}
	}
	return strOut;
}

setLockStatus(v_team, porigin, lever)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doAnchor(lever, porigin);
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doAnchor(lever, porigin);
      else if(v_team == "" && self.sessionstate == "playing") self thread doAnchor(lever, porigin);
}

doAnchor(lever, porigin)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(lever)
  {
    self.anchor = spawn("script_origin", porigin);
    self linkTo(self.anchor);
  }
  else
  {
    self unlink();
    if(isdefined(self.anchor)) self.anchor delete();
  }
}

setSuicide(v_team)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doSuicide();
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doSuicide();
      else if(v_team == "" && self.sessionstate == "playing") self thread doSuicide();
}

doSuicide()
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  wait .25;
  if(isPlayer(self))
  {
    self.ex_cmdmondeath = true;
    self suicide();
  }
}

setSmite(v_team, porigin)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doSmite(porigin);
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doSmite(porigin);
      else if(v_team == "" && self.sessionstate == "playing") self thread doSmite(porigin);
}

doSmite(porigin)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  wait 1.5;
  playfx(level.ex_effect["barrel"], porigin);

  if(isPlayer(self))
  {
    self playsound("artillery_explosion");
    self.ex_cmdmondeath = true;
    self suicide();
  }
}

setZombie(v_team, porigin)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doZombie(porigin);
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doZombie(porigin);
      else if(v_team == "" && self.sessionstate == "playing") self thread doZombie(porigin);
}

doZombie(porigin)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  self.origin = porigin;

  self extreme\_ex_main::popHead( (-0.2 + randomFloat(0.4), -0.2 + randomFloat(0.4),1), 100 + randomInt(100));
  self.ex_zombie = true;

  while(isAlive(self) && self.sessionstate != "spectator")
  {
    wait 1 + randomInt(5);
    playfxontag(level.ex_effect["flesh"], self, "j_neck");
  }
}

setTorch(v_team)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  porigin = self.origin;

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doTorch(porigin);
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doTorch(porigin);
      else if(v_team == "" && self.sessionstate == "playing") self thread doTorch(porigin);
}

doTorch(porigin, special, eAttacker)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(!isDefined(special)) special = false;
  if(!isDefined(eAttacker)) eAttacker = self;

  self thread doFire();

  for(i = 0; i < 10; i++)
  {
    if(isPlayer(self) && special) self thread [[level.callbackPlayerDamage]](eAttacker, eAttacker, randomInt(2) + 2, 1, "MOD_PROJECTILE", "planebomb_mp", undefined, (0,0,1), "none",0);
    wait 1;
  }

  if(special) return;

  if(isPlayer(self))
  {
    self.ex_cmdmondeath = true;
    playfx(level.ex_effect["bodygroundfire"], porigin);
    self suicide();
  }
}

setFire(v_team)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doFire();
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doFire();
      else if(v_team == "" && self.sessionstate == "playing") self thread doFire();
}

doFire()
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  for(;;)
  {
    wait 0.25;
    if(isPlayer(self))
    {
      playfxontag(level.ex_effect["bodyarmfire"], self, "j_elbow_le");
      playfxontag(level.ex_effect["bodyarmfire"], self, "j_elbow_ri");
      playfxontag(level.ex_effect["bodytorsofire"], self, "torso_stabilizer");
    }
  }
}

setSpank(v_team)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doSpank();
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doSpank();
      else if(v_team == "" && self.sessionstate == "playing") self thread doSpank();
}

doSpank()
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  self thread extreme\_ex_main::spankme(15);
  self shellshock("default", 15);
}

setCheater()
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  self setClientCvar("sensitivity", "20");
  self setClientCvar("com_maxfps", "1");
  self setClientCvar("r_displayRefresh", "1 Hz");
  self setClientCvar("r_mode", "640x480");
  self setClientCvar("cl_maxpackets", "1");
  self setClientCvar("cl_packetdup", "50");
  self setClientCvar("rate", "1");
  self setClientCvar("snaps", "1");
  self setClientCvar("snd_volume", "10");
}

setArty(v_team)
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  wait(2);
  self iprintlnbold(&"CMDMONITOR_ARTY_SELF_RUN");
  wait(5);

  if(v_team == "allies" && self.pers["team"] == "allies" && self.sessionstate == "playing") self thread doArty();
    else if(v_team == "axis" && self.pers["team"] == "axis" && self.sessionstate == "playing") self thread doArty();
      else if(v_team == "" && self.sessionstate == "playing") self thread doArty();
}

doArty()
{
  level endon("ex_gameover");
  self endon("disconnect");
  self endon("ex_spawned");
  self endon("ex_dead");

  self.ex_cmdmondeath = true;
  while(isPlayer(self) && isAlive(self))
  {
    arty = spawn("script_origin", self.origin);
    arty goArty(self);
  }
}

goArty(player)
{
  shellStartPos = (level.ex_playArea_Min[0], level.ex_playArea_Min[1], player.origin[2]);
  shellTargetPos = player.origin;

  self thread goArtyShell(player, shellStartPos, shellTargetPos);

  self waittill("arty_shell_impact");
  self delete();
}

goArtyShell(player, shellStartPos, shellTargetPos)
{
  //shellAngle = 60;
  //shellDist = distance(shellTargetPos, shellStartPos);
  //shellHeight = int(shellDist * tan(shellAngle));
  //shellStartPos = (shellStartPos[0], shellStartPos[1], shellHeight);
  shellStartPos = (shellTargetPos[0]-100, shellTargetPos[1]-100, level.ex_mapArea_Max[2]);

  shell = spawn("script_model", shellStartPos);
  shell setModel("xmodel/prop_stuka_bomb");
  shell.angles = vectorToAngles(vectorNormalize(shellTargetPos - shellStartPos));
  shell playSound("artillery_incoming");

  shellDist = distance(shellTargetPos, shellStartPos);
  shellDist = int(shellDist * 0.0254);
  shellSpeed = 50;
  shellInAir = goArtyShellTime(shellDist, shellSpeed);
  shell moveTo(shellTargetPos + (0,0,-100), shellInAir);
  wait(shellInAir);
  shell goArtyBoom(player);

  self notify("arty_shell_impact");
}

goArtyBoom(player)
{
  playfx(level.ex_effect["artillery"], self.origin);
  self playSound("artillery_explosion");
  self thread extreme\_ex_utils::scriptedfxradiusdamage(self, undefined, "MOD_EXPLOSIVE", "artillery_mp", 3, 0, 0, "generic", "dirt", true, true, true);
  artyDamage = 25;
  player thread [[level.callbackPlayerDamage]](self, self, artyDamage, 1, "MOD_EXPLOSIVE", "artillery_mp", undefined, (0,0,1), "none", 0);

  self hide();
  wait(3);
  self delete();
}

goArtyShellTime(distvalue, speedvalue)
{
  distunit = 1;  // Metres
  speedunit = 1; // Metres per second
  timeinsec = (distvalue * distunit) / (speedvalue * speedunit);
  return timeinsec;
}
