#include ww2x\_wwx_weapons;

tripwiremon()
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	holdtime = 30;

	while(isPlayer(self) && self.sessionstate == "playing")
	{
		wait 0.5;

		flag = 0;
		count = 0;
		nade = "ignore";

		frag = false;
		smoke = false;

		// if not planting or defusing a tripwire and the tripwire message is displayed, clear the message from the hud
		if((!self.wwx_plantwire && !self.wwx_defusewire) && self [[level.wwx_getstance]](false) != 2 && (isDefined(self.wwx_wwxpmsg1) || isDefined(self.wwx_pb)))
		{
			self thread cleanMessages();

			// if ammo crates is on, and they are not rearming at an ammo crate then make sure the bar graphic is not showing
			if(level.wwx_amc_perteam && !isDefined(self.wwx_amc_rearm)) self thread ww2x\_wwx_utils::cleanBarGraphic();
		}

		// if not prone, continue monitoring
		if(self [[level.wwx_getstance]](false) != 2)
		{
			// show WMD deployment
			if(isDefined(self.wwx_binocular_hint) && !isDefined(self.wwx_actimer)) self.wwx_binocular_hint.alpha = 1;
		 	continue;
		 }

		// check available nades
		frags = getCurrentAmmo(self.pers["fragtype"]);
		smokes = getCurrentAmmo(self.pers["smoketype"]);
		enemy_frags = getCurrentAmmo(self.pers["enemy_fragtype"]);
		enemy_smokes = getCurrentAmmo(self.pers["enemy_smoketype"]);
		total_frags = frags + enemy_frags;
		total_smokes = smokes + enemy_smokes;

		// not enough nades?, if so continue monitoring
		if((total_frags <= 1 && total_smokes <= 1) || self.wwx_plantwire || self.wwx_defusewire) continue;

		// hide WMD deployment
		if(isDefined(self.wwx_binocular_hint)) self.wwx_binocular_hint.alpha = 0;

		// player has enought nades, but are they already planting/defusing
		if(!self.wwx_plantwire && !self.wwx_defusewire)
		{
			// if the player is using a sniper rifle, posible chance of ADS so display message to change weapon or tripwires disabled
			if(isPlayer(self) && !isDefined(self.wwx_wwxpmsg1) && isWeaponType(self getCurrentWeapon(), "sniper"))
			{
				wait 2;
				if(isPlayer(self))
				{
					if(level.wwx_wepo_class == 2 && !level.wwx_wepo_sidearm) self showTripwireMessage(undefined, undefined, &"TRIPWIRE_SNIPER_ONLY");
					else self showTripwireMessage(undefined, undefined, &"TRIPWIRE_CHANGE_SNIPER");

					if(isWeaponType(self getCurrentWeapon(), "sniper")) wait 2;
				}

				if(isPlayer(self)) self cleanMessages();
				continue;
			}
			else if(isPlayer(self) && !isDefined(self.wwx_wwxpmsg1) && isWeaponType(self getCurrentWeapon(), "deployable") && self.turret_deployed == true)
			{
				wait 2;
				if(isPlayer(self))
				{
					if(level.wwx_wepo_class == 2 && !level.wwx_wepo_sidearm) self showTripwireMessage(undefined, undefined, &"TRIPWIRE_SNIPER_ONLY");
					else if(self.turret_deployed == true)self showTripwireMessage(undefined, undefined, &"TURRETS_PICKUP");

					if(isWeaponType(self getCurrentWeapon(), "deployable")) wait 2;
				}

				if(isPlayer(self)) self cleanMessages();
				continue;
			}
			else if(isPlayer(self) && !isDefined(self.wwx_wwxpmsg1) && !isWeaponType(self getCurrentWeapon(), "sniper")) // no sniper rifle
                           {
				self showTripwireMessage(undefined, undefined, &"TRIPWIRE_CHOOSE_GRENADE");
                           }
			else if(isPlayer(self) && !isDefined(self.wwx_wwxpmsg1) && !isWeaponType(self getCurrentWeapon(), "deployable") && self.turret_deployed == false) // no turret
				self showTripwireMessage(undefined, undefined, &"TRIPWIRE_SNIPER_ONLY");

			// check for sniper rifle one more time
			if(isPlayer(self) && isWeaponType(self getCurrentWeapon(), "sniper"))
			{
				self cleanMessages();
				continue;
			}

			else if(isPlayer(self) && isWeaponType(self getCurrentWeapon(), "deployable") && self.turret_deployed == true)
			{
				self cleanMessages();
				continue;
			}


			// hide WMD deployment
			if(isDefined(self.wwx_binocular_hint)) self.wwx_binocular_hint.alpha = 0;

			// if they're not holding down the melee key, loop
			if(!self meleeButtonPressed()) continue;

			// define frag grenades
			frag1icon = self.pers["fragtype"];
			frag2icon = self.pers["fragtype"];

			// not enough of their own teams, so check for enemy frags too
			if(frags <= 1)
			{
				if(frags == 1 && enemy_frags >= 1) // mix and match?
				{
					frag1icon = self.pers["fragtype"];
					frag2icon = self.pers["enemy_fragtype"];
					frag = true;
				}

				if(frags == 0 && enemy_frags >= 2) // enemy frags only
				{
					frag1icon = self.pers["enemy_fragtype"];
					frag2icon = self.pers["enemy_fragtype"];	
					frag = true;
				}
			}
			else if(frags >= 2) frag = true; // got enough of their own frags	

			// define smoke grenades
			smoke1icon = self.pers["smoketype"];
			smoke2icon = self.pers["smoketype"];

			// not enough of their own teams, so check for enemy frags too
			if(smokes <= 1)
			{
				if(smokes == 1 && enemy_smokes >= 1) // mix and match?
				{
					smoke1icon = self.pers["fragtype"];
					smoke2icon = self.pers["enemy_fragtype"];
					smoke = true;
				}

				if(smokes == 0 && enemy_smokes >= 2) // enemy smokes only
				{
					smoke1icon = self.pers["enemy_fragtype"];
					smoke2icon = self.pers["enemy_fragtype"];	
					smoke = true;
				}
			}
			else if(smokes >= 2) smoke = true; // got enough of their own smokes

			// ok, lets see what they want to plant
			while(self meleeButtonPressed() && count < holdtime && self [[level.wwx_getstance]](true) == 2)
			{
				wait .05;
				count++;
			}
	
			// didn't hold down long enough, loop
			if(count < holdtime) continue;

			// if they have enough frags, display the frag tripwire message
			if(frag)
			{
				flag++;
				self playLocalSound("tripclick");
				nade = self.pers["fragtype"];
				self showTripwireMessage(frag1icon, frag2icon, &"TRIPWIRE_FRAG_HOLD_SMOKE");
				count = 0;

				// if they let go here, they want to use frag grenades		
				while(self meleeButtonPressed() && count < holdtime && self [[level.wwx_getstance]](true) == 2)
				{
					wait .05;
					count++;
				}
			}
			else count = holdtime; // no frags!

			// if they have enough smokes, display the smoke tripwire message
			if(smoke)
			{
				if(count >= holdtime) // they kept holding so show the smokes
				{
					flag++;
					self playLocalSound("tripclick");
					nade = self.pers["smoketype"];
					self showTripwireMessage(smoke1icon, smoke2icon, &"TRIPWIRE_SMOKE_HOLD_CANCEL");
				}
			}
	
			count = 0;

			// if they let go here, it will use the smokes, continue to hold and it will cancel planting a tripwire	
			while(self meleeButtonPressed() && count < holdtime && self [[level.wwx_getstance]](true) == 2)
			{
				wait .05;
				count++;
			}

			// they held on, so they don't want to plant a tripwire, or missed the one they wanted...doh!
			if(count >= holdtime)
			{
				flag = 0;
				self cleanMessages();
			}

			// check to see if they got up during this process?	
			if(self [[level.wwx_getstance]](true) != 2) continue;

			// ok, good to go....?
			if(flag >= 1 && nade != "ignore") self thread plantTripwire(nade);
		}
	}
}

plantTripwire(grenadetype)
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_spawned");
	self endon("wwx_dead");

	self endon("defusingtripwire");

	if(isPlayer(self))
	{
		// Make sure to only run one instance
		if(self.wwx_plantwire) return;
	
		self.wwx_plantwire = true;

		grenadetype1 = grenadetype;
		grenadetype2 = grenadetype;

		// check their own type first
		iAmmo = getCurrentAmmo(grenadetype1);

		// now check enemy type
		if(grenadetype == self.pers["fragtype"]) enemytype = self.pers["enemy_fragtype"];
		else enemytype = self.pers["enemy_smoketype"];

		enemy_iAmmo = self getCurrentAmmo(enemytype);

		if(enemy_iAmmo > 0)
		{
			if(iAmmo == 1) grenadetype2 = enemytype;

			else if(iAmmo == 0)
			{
				grenadetype1 = enemytype;
				grenadetype2 = enemytype;
			}
			
			iAmmo+= enemy_iAmmo;
		}

		// enough ammo?
		if(iAmmo < 2)
		{
			if(grenadetype == self.pers["fragtype"]) self iprintlnbold(&"TRIPWIRE_REASON_AMMO_FRAG");
			else self iprintlnbold(&"TRIPWIRE_REASON_AMMO_SMOKE");
	
			self cleanMessages();
			self.wwx_plantwire = false;
			return;
		}

		// show the plant message
		self showTripwireMessage(grenadetype1, grenadetype2, &"TRIPWIRE_PLANT");

		// while there not pressing the melee key, monitor to see if they leave the prone position
		while(isPlayer(self) && self.sessionstate == "playing" && !(self meleeButtonPressed()))
		{
			if(self [[level.wwx_getstance]](true) != 2)
			{
				self cleanMessages();
				self.wwx_plantwire = false;
				return;
			}
			
			wait 0.05;
		}

		// loop
		if(isPlayer(self))
		{
			for(;;)
			{
				// check the amount of ammo, might have thrown a grenade
				if(grenadetype1 == grenadetype2) iAmmo = self getCurrentAmmo(grenadetype1);
				else iAmmo = self getCurrentAmmo(grenadetype1) + self getCurrentAmmo(grenadetype2);
	
				// not enough ammo?
				if(iAmmo < 2) break;
		
				// check they're still prone
				if(self [[level.wwx_getstance]](false) != 2) break;
		
				// get the position 15" in front of the player
				position = self.origin + [[level.wwx_vectorscale]](anglesToForward(self.angles),15);
		
				// check that there is room.
				trace=bulletTrace(self.origin+(0,0,10),position+(0,0,10),false,undefined);
				if(trace["fraction"] !=1)
				{
					self iprintlnbold(&"TRIPWIRE_REASON_NO_ROOM");
					break;
				}
			
				// find ground
				trace=bulletTrace(position+(0,0,10),position+(0,0,-10),false,undefined);
				if(trace["fraction"] ==1)
				{
					self iprintlnbold(&"TRIPWIRE_REASON_UNEVEN_GROUND");
					break;
				}
		
				if(isDefined(trace["entity"]) && isDefined(trace["entity"].classname) && trace["entity"].classname == "script_vehicle") break;
		
				position=trace["position"];
				tracestart = position + (0,0,10);
		
				// find position 1
				traceend = tracestart + [[level.wwx_vectorscale]](anglesToForward(self.angles + (0,90,0)),50);
				trace=bulletTrace(tracestart,traceend,false,undefined);
		
				if(trace["fraction"]!= 1)
				{
					distance = distance(tracestart,trace["position"]);
					if(distance>5) distance = distance - 2;
					position1=tracestart + [[level.wwx_vectorscale]](vectorNormalize(trace["position"]-tracestart),distance);
				}
				else position1 = trace["position"];
		
				// find ground
				trace=bulletTrace(position1,position1+(0,0,-20),false,undefined);
		
				if(trace["fraction"]==1)
				{
					self iprintlnbold(&"TRIPWIRE_REASON_UNEVEN_GROUND");
					break;
				}
		
				vPos1 = trace["position"] + (0,0,3);
		
				// find position 2
				traceend = tracestart + [[level.wwx_vectorscale]](anglesToForward(self.angles + (0,-90,0)),50);
				trace = bulletTrace(tracestart,traceend,false,undefined);
		
				if(trace["fraction"] != 1)
				{
					distance = distance(tracestart,trace["position"]);
					if(distance > 5) distance = distance - 2;
					position2 = tracestart + [[level.wwx_vectorscale]](vectorNormalize(trace["position"]-tracestart),distance);
				}
				else position2 = trace["position"];
		
				// find ground
				trace = bulletTrace(position2,position2+(0,0,-20),false,undefined);
		
				if(trace["fraction"] == 1)
				{
					self iprintlnbold(&"TRIPWIRE_REASON_UNEVEN_GROUND");
					break;
				}
		
				vPos2 = trace["position"] + (0,0,3);
		
				maxlimit = level.wwx_tweaponlimit;
				curval = 0;
				msg = "";
	
				// Ok to plant, kill checktripwireplacement and set up new hud message
				self notify("wwx_checkdefusetripwire");
	
				// check to see if there pressing their melee key	
				if(isPlayer(self) && self.sessionstate == "playing" && self meleeButtonPressed())
				{
					// Check tripwire limit before planting
					if(level.wwx_teamplay)
					{
						curval = level.wwx_tweapons[self.sessionteam];
						msg = &"TRIPWIRE_LIMIT_TEAM_REACHED";
					}
					else
					{
						curval = level.wwx_tweapons;
						maxlimit = maxlimit * 2;
						msg = &"TRIPWIRE_LIMIT_REACHED";
					}
					
					if(curval >= maxlimit)
					{
						self thread ww2x\_wwx_utils::excllnbo("self",5);
						self iprintlnbold(msg);
						break;
					}
		
					// lock the player to the spot while defusing the tripwire	
					self ww2x\_wwx_utils::punishment("disable", "freeze");
	
					// get player origin and angles
					oldorigin = self.origin;
					angles = self.angles;
	
					// display new message and progress bar
					self thread ww2x\_wwx_utils::createBarGraphic(288,5);
					self showTripwireMessage(grenadetype1, grenadetype2, &"TRIPWIRE_PLANTING");
		
					// play plant sound
					self playSound("MP_bomb_plant");
	
					// set the bar colour and zero the count		
					colour = 1;
					count = 0;
	
					// count how long they hold the melee button for
					while(isPlayer(self) && self meleeButtonPressed() && self.origin == oldorigin && self [[level.wwx_getstance]](false) == 2 && count < 100)
					{
						count++;
						if(isPlayer(self) && isDefined(self.wwx_pb)) self.wwx_pb.color = (1,colour,colour);
						colour -= 0.05 / 5;
						wait 0.05;
					}
	
					// remove messages and progress bar
					self cleanMessages();
					self ww2x\_wwx_utils::cleanBarGraphic();
	
					// did they hold the key down long enough?
					if(count < 100) break;
	
					// check the tripwire limits again	
					maxlimit = level.wwx_tweaponlimit;
					curval = 0;
		
					// Check tripwire limit before final deployment, in case someone beat them to it!
					if(level.wwx_teamplay)
					{
						curval = level.wwx_tweapons[self.sessionteam];
						msg = &"TRIPWIRE_LIMIT_TEAM_REACHED";
					}
					else
					{
						curval = level.wwx_tweapons;
						maxlimit = maxlimit * 2;
						msg = &"TRIPWIRE_LIMIT_REACHED";
					}
					
					if(curval >= maxlimit)
					{
						self thread ww2x\_wwx_utils::excllnbo("self",5);
						self iprintlnbold(msg);
						break;
					}
	
					// adjust the amount of tripwires available	
					if(level.wwx_teamplay) level.wwx_tweapons[self.sessionteam]++;
					else level.wwx_tweapons++;
		
					// calculate the tripwire centre
					x = (vPos1[0] + vPos2[0])/2;
					y = (vPos1[1] + vPos2[1])/2;
					z = (vPos1[2] + vPos2[2])/2;
					vPos = (x,y,z);
		
					// decrease the players grenade ammo
					self takeAmmo(grenadetype1, 1);
					self takeAmmo(grenadetype2, 1);
			
					// spawn the tripwire
					tripwep = spawn("script_origin",vPos);
					tripwep.angles = angles;
					tripwep thread monitorTripwire(self, grenadetype1, grenadetype2, vPos1, vPos2);
					break;
				}
			}
		}
	}

	if(isPlayer(self))
	{
		// enable the players weapon and release them
		self thread ww2x\_wwx_utils::punishment("enable", "release");

		// remove the messages and progress bar
		self cleanMessages();
		self ww2x\_wwx_utils::cleanBarGraphic();

		// not planting anymore!
		self.wwx_plantwire = false;
	}
}

defuseTripwire(tripwep, grenadetype1, grenadetype2)
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	self notify("wwx_checkdefusetripwire");
	self endon("wwx_checkdefusetripwire");

	if(isPlayer(self))
	{
		// make sure to only run one instance
		if(self.wwx_defusewire) return;

		self.wwx_defusewire = true;
	
		// get the distance between the tripwire weapons and the player
		distance1 = distance(tripwep.tweapon1.origin, self.origin);
		distance2 = distance(tripwep.tweapon2.origin, self.origin);

		// check still in within range of the tripwire
		if(distance1 > 20 && distance2 > 20)
		{
			self cleanMessages();
			self.wwx_defusewire = false;
			return;
		}
	
		grenadeicon = grenadetype1;
	
		// which grenade were they near to?
		if(distance2 >= 20) grenadeicon = grenadetype1;
		else grenadeicon = grenadetype2;
		
		// ok to defuse, end the plant routine
		self notify("defusingtripwire");

		// hide WMD deployment
		if(isDefined(self.wwx_binocular_hint)) self.wwx_binocular_hint.alpha = 0;
	
		// show new message
		self showTripwireMessage(grenadeicon, undefined, &"TRIPWIRE_DEFUSE");
	
		// loop
		for(;;)
		{
			wait 0.5;

			if(isPlayer(self) && self meleeButtonPressed())
			{
				// lock the player to the spot while defusing the tripwire	
				self ww2x\_wwx_utils::punishment("disable", "freeze");

				// get player origin and angles
				oldorigin = self.origin;
				angles = self.angles;

				// display new message and progress bar
				self thread ww2x\_wwx_utils::createBarGraphic(288, 5);
				self showTripwireMessage(grenadetype1, grenadetype2, &"TRIPWIRE_DEFUSING");
	
				// play defuse sound
				self playSound("MP_bomb_defuse");

				// set the bar colour and zero the count	
				colour = 1;
				count = 0;

				// count how long they hold the melee button for
				while(isPlayer(self) && self meleeButtonPressed() && isDefined(tripwep) && self.origin == oldorigin && self [[level.wwx_getstance]](false) == 2 && count < 100)
				{
					count++;
					if(isPlayer(self) && isDefined(self.wwx_pb)) self.wwx_pb.color = (colour,1,colour);
					colour -= 0.05 / 5;
					wait 0.05;
				}

				// remove the messages and progress bar
				self cleanMessages();
				self ww2x\_wwx_utils::cleanBarGraphic();

				// did they hold the key down long enough?	
				if(count < 100 && isDefined(tripwep)) break;
	
				// adjust the amount of tripwires available
				if(isDefined(tripwep.oldteam) && tripwep.oldteam != "no_owner")
				{
					if(level.wwx_teamplay) level.wwx_tweapons[tripwep.oldteam]--;
					else level.wwx_tweapons--;
				}
	
				// stop monitor the tripwire
				if(isDefined(tripwep)) tripwep notify("endmonitoringtripwire");
				if(isDefined(tripwep)) tripwep.wwx_warnplayers = false;
				wait .05;
	
				// remove the tripwire
				if(isDefined(tripwep.tweapon1)) tripwep.tweapon1 delete();
				if(isDefined(tripwep.tweapon2)) tripwep.tweapon2 delete();
				if(isDefined(tripwep)) tripwep delete();
	
				wait 0.2;

				// play a defuse sound to everyone and give them the new grenades
				if(isPlayer(self))
				{
					self playSound("MP_bomb_defuse");
					self giveAmmo(grenadetype1 ,1);
					self giveAmmo(grenadetype2, 1);
				}

				break;
			}

			wait .05;
	
			// check still prone
			if(self [[level.wwx_getstance]](false) != 2) break;

			// check that the tripwire is still there, another player may be defusing too?
			if(!isDefined(tripwep.tweapon1) || !isDefined(tripwep.tweapon2)) break;

			// check the player is still within distance of the tripwire
			distance1 = distance(tripwep.tweapon1.origin, self.origin);
			distance2 = distance(tripwep.tweapon2.origin, self.origin);
			if(distance1 >= 20 && distance2 >= 20) break;
		}
	}

	// show WMD deployment
	if(isDefined(self.wwx_binocular_hint)) self.wwx_binocular_hint.alpha = 1;

	// enable the players weapon and release them
	self thread ww2x\_wwx_utils::punishment("enable", "release");

	// remove the messages and progress bar
	self cleanMessages();
	self ww2x\_wwx_utils::cleanBarGraphic();

	// not defusing anymore!
	self.wwx_defusewire = false;
}

monitorTripwire(owner, grenadetype1, grenadetype2, vPos1, vPos2)
{
	level endon("wwx_gameover");
	self endon("endmonitoringtripwire");

	// save team
	if(isPlayer(owner)) self.oldteam = owner.pers["team"];
	else self.oldteam = "no_owner";

	// Spawn nade one
	self.tweapon1 = spawn("script_model",vPos1);
	self.tweapon1 setModel(getWeaponModel(grenadetype1));
	self.tweapon1.angles = self.angles;
	self.tweapon1 thread waitForDamage(100);

	// Spawn nade two
	self.tweapon2 = spawn("script_model",vPos2);
	self.tweapon2 setModel(getWeaponModel(grenadetype2));
	self.tweapon2.angles = self.angles;
	self.tweapon2 thread waitForDamage(100);

	// Get detection spots
	vPos3 = self.origin + [[level.wwx_vectorscale]](anglesToForward(self.angles),50);
	vPos4 = self.origin + [[level.wwx_vectorscale]](anglesToForward(self.angles + (0,180,0)),50);

	// Get detection ranges
	triprange = distance(self.origin, vPos1) + 150;
	tripsphere = distance(vPos3,vPos1) + 2;

	if(isPlayer(owner) && owner.sessionstate == "playing")
	{
		owner thread tripwireActivate(5);
		wait 5;
	}

	blow = false;

	self.wwx_warnplayers = true;

	while(!blow)
	{
		wait .05;

		players = getentarray("player", "classname");
		
		// Blow if anyone of the nades has taken enough damage
		if(isDefined(self.tweapon1.damaged) || isDefined(self.tweapon2.damaged)) blow = true;
			
		// Loop through players to find out if one has triggered the wire
		for(i = 0;i < players.size && !blow;i++)
		{
			// Check that player still exist
			if(isPlayer(players[i]) && players[i].sessionstate == "playing") player = players[i];
			else continue;

			// Within range?
			distance = distance(self.origin, player.origin);
			if(distance >= triprange)
			{
				// Set warning to false
				player.wwx_warningwire = undefined;
				continue;
			}

			// player is jumping over the tripwire?
			if(isPlayer(player) && !player isOnGround()) continue;

			// Check for defusal
			distance1 = distance(self.tweapon1.origin, player.origin);
			distance2 = distance(self.tweapon2.origin, player.origin);

			// if in range of either grenade and prone and not already defusing
			if(player [[level.wwx_getstance]](false) == 2 && (distance1 <= 20 || distance2 <= 20) && !player.wwx_defusewire)
				player thread defuseTripwire(self, grenadetype1, grenadetype2);

			// Warn if same team?
			if(level.wwx_teamplay && isDefined(self.oldteam) && self.oldteam != "no_owner" && self.oldteam == player.pers["team"])
			{
				if(level.wwx_tweaponwarning && !isDefined(player.wwx_warningwire)) self thread tripwireWarning(player);
				if(level.wwx_tweapon == 3) continue; // don't blow on team mates!
			}

			// Within sphere one?
			distance = distance(vPos3, player.origin);
			if(distance >= tripsphere) continue;

			// Within sphere two?
			distance = distance(vPos4, player.origin);
			if(distance >= tripsphere) continue;

			// Time to blow
			blow = true;
		}
	}

	if(isDefined(self.oldteam) && self.oldteam != "no_owner")
	{
		if(level.wwx_teamplay) level.wwx_tweapons[self.oldteam]--;
		else level.wwx_tweapons--;
	}

	self.tweapon1 notify("endtripwiredamagemonitor");
	self.tweapon2 notify("endtripwiredamagemonitor");

	if(isDefined(self.tweapon2.damaged))
	{
		self.tweapon2 playSound("weap_fraggrenade_pin");
		wait .05;
		self.tweapon1 playSound("weap_fraggrenade_pin");
		wait .05;
	}
	else
	{
		self.tweapon1 playSound("weap_fraggrenade_pin");
		wait .05;
		self.tweapon2 playSound("weap_fraggrenade_pin");
		wait .05;
	}

	wait(randomFloat(.5));

	// Check that damage owner still exists, if not tripwire just kills
	if(isPlayer(owner)) eAttacker = owner;
	else eAttacker = self;

	// blow 'em
	if(isDefined(self.tweapon2.damaged))
	{
		// blow 2
		playfx(level.wwx_effect[getFX(grenadetype2)], self.tweapon2.origin);	
		self.tweapon2 playSound(getSound(grenadetype2));
		self.tweapon2 tripwireDamage(self.tweapon2, eAttacker, grenadetype2, level.wwx_tweapon);

		// A small, random, delay between the effects
		wait(randomFloat(.25));

		// blow 1
		playfx(level.wwx_effect[getFX(grenadetype1)], self.tweapon1.origin);	
		self.tweapon1 playSound(getSound(grenadetype1));
		self.tweapon1 tripwireDamage(self.tweapon1, eAttacker, grenadetype1, level.wwx_tweapon);
	}
	else
	{
		// blow 1
		playfx(level.wwx_effect[getFX(grenadetype1)], self.tweapon1.origin);	
		self.tweapon1 playSound(getSound(grenadetype1));
		self.tweapon1 tripwireDamage(self.tweapon1,eAttacker, grenadetype1, level.wwx_tweapon);


		// A small, random, delay between the nades
		wait(randomFloat(.25));

		// blow 2
		playfx(level.wwx_effect[getFX(grenadetype2)], self.tweapon2.origin);	
		self.tweapon2 playSound(getSound(grenadetype2));
		self.tweapon2 tripwireDamage(self.tweapon2,eAttacker, grenadetype2, level.wwx_tweapon);
	}

	self.wwx_warnplayers = false;

	if(isDefined(self.tweapon1)) self.tweapon1 delete();
	if(isDefined(self.tweapon2)) self.tweapon2 delete();
	if(isDefined(self)) self delete();
}

tripwireWarning(player)
{
	level endon("wwx_gameover");

	if(isDefined(player.wwx_warningwire)) return;

	player.wwx_warningwire = true;

	while(isPlayer(player) && isDefined(player.wwx_warningwire) && isDefined(self) && self.wwx_warnplayers)
	{
		if(!isDefined(player.wwx_tripwarning))
		{
			player.wwx_tripwarning = newClientHudElem(player);	
			player.wwx_tripwarning.x = 320;
			player.wwx_tripwarning.y = 200;
			player.wwx_tripwarning.alpha = 0;
			player.wwx_tripwarning.scale = 0.6;
			player.wwx_tripwarning.alignX = "center";
			player.wwx_tripwarning.alignY = "middle";
			player.wwx_tripwarning setShader("killiconsuicide",40,40);
		}

		if(isPlayer(player) && isDefined(player.wwx_tripwarning))
		{
			player.wwx_tripwarning fadeOverTime(0.8);
			player.wwx_tripwarning.alpha = 0.8;
			player playLocalSound("expalert");
		}
	
		wait 1.6;

		if(isPlayer(player) && isDefined(player.wwx_tripwarning))
		{		
			player.wwx_tripwarning fadeOverTime(0.8);
			player.wwx_tripwarning.alpha = 0;
			wait 0.8;
		}
	}

	if(isPlayer(player) && isDefined(player.wwx_tripwarning)) player.wwx_tripwarning destroy();
}

waitForDamage(maxdamage)
{
	self endon("endtripwiredamagemonitor");

	//self setTakeDamage(true);
	self.damaged = undefined;

	for(;;)
	{
		self waittill ("damage", dmg, who, dir, point, mod);
		if(dmg >= maxdamage) break;
	}

	self.damaged = true;
}

tripwireDamage(who, eAttacker, grenadetype, teamkill)
{
	deviceID = getDeviceID(grenadetype);
	switch(deviceID)
	{
		// only frags cause damage
		case 1:
		case 2:
		case 3:
		case 4:
		case 47:
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
		case 58:
		case 59:
		case 60:
		case 61:
		case 62:
		case 63:
		case 64:
		case 65:
		case 66:
		who ww2x\_wwx_utils::scriptedfxradiusdamage(eAttacker, undefined, "MOD_EXPLOSIVE","tripwire_mp", level.wwx_tweapon_radius, 600, 400, undefined, true, true);
		break;
	}
}

showTripwireMessage(grenadetype1, grenadetype2, msg)
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	if(isPlayer(self))
	{
		self cleanMessages();

		if(isDefined(msg))
		{
			self.wwx_wwxpmsg1 = newClientHudElem( self );
			self.wwx_wwxpmsg1.alignX = "center";
			self.wwx_wwxpmsg1.alignY = "middle";
			self.wwx_wwxpmsg1.x = 320;
			self.wwx_wwxpmsg1.y = 408;
			self.wwx_wwxpmsg1.alpha = 1;
			self.wwx_wwxpmsg1.fontScale = 0.80;
			self.wwx_wwxpmsg1.sort = 1;
			self.wwx_wwxpmsg1 setText(msg);
		}

		if(isDefined(grenadetype1))
		{
			self.wwx_wwxpmsg2 = newClientHudElem(self);

			if(isDefined(grenadetype2)) self.wwx_wwxpmsg2.alignX = "left";
			else self.wwx_wwxpmsg2.alignX = "center";

			self.wwx_wwxpmsg2.alignY = "top";
			self.wwx_wwxpmsg2.x = 320;
			self.wwx_wwxpmsg2.y = 425;
			self.wwx_wwxpmsg2 setShader(getWeaponHud(grenadetype1),40,40);
		}

		if(isDefined(grenadetype2))
		{
			self.wwx_wwxpmsg3 = newClientHudElem(self);
			self.wwx_wwxpmsg3.alignX = "right";
			self.wwx_wwxpmsg3.alignY = "top";
			self.wwx_wwxpmsg3.x = 320;
			self.wwx_wwxpmsg3.y = 425;
			self.wwx_wwxpmsg3 setShader(getWeaponHud(grenadetype2),40,40);
		}

		self.wwx_istwepmsg = true;
	}
}

cleanMessages()
{
	if(isDefined(self.wwx_wwxpmsg1)) self.wwx_wwxpmsg1 destroy();
	if(isDefined(self.wwx_wwxpmsg2)) self.wwx_wwxpmsg2 destroy();
	if(isDefined(self.wwx_wwxpmsg3)) self.wwx_wwxpmsg3 destroy();
	if(isPlayer(self)) self.wwx_istwepmsg = false;
}

tripwireActivate(time)
{
	level endon("wwx_gameover");
	self endon("disconnect");
	self endon("wwx_dead");

	if(isPlayer(self))
	{
		self.wwx_actimer = newClientHudElem(self);
		self.wwx_actimer .x = 320;
		self.wwx_actimer .y = 434;
		self.wwx_actimer .alpha = 0.8;
		self.wwx_actimer .alignX = "center";
		self.wwx_actimer .alignY = "middle";
		self.wwx_actimer .fontScale = 1.2;
		self.wwx_actimer.label = &"TRIPWIRE_ACTIVATE";
		self.wwx_actimer.color = (0, 1, 0);
		self.wwx_actimer  setTimer(5);
	}

	for(a = 0; a < 5; a++) 
	{
		// hide WMD deployment
		if(isDefined(self.wwx_binocular_hint)) self.wwx_binocular_hint.alpha = 0;

		wait 1;
		
		if(isPlayer(self) && isDefined(self.wwx_actimer))
		{
			if((5 - a) == 3) self.wwx_actimer .color = (1, 1, 0);
			else if ( (5 - a) == 2) self.wwx_actimer .color = (1, 0, 0);
		}

		if(isPlayer(self)) self playlocalsound("medi_use_high");
	}

	if(isPlayer(self))
	{
		self playlocalsound("medi_use_low");
		
		if(isDefined(self.wwx_actimer))
		{
			self.wwx_actimer fadeovertime(1);
			self.wwx_actimer.alpha = 0;
		}
	}

	wait 1;

	if(isPlayer(self))
	{
		self playlocalsound("MP_bomb_plant");
		if(isDefined(self.wwx_actimer)) self.wwx_actimer destroy();

		// show WMD deployment
		if(isDefined(self.wwx_binocular_hint)) self.wwx_binocular_hint.alpha = 1;
	}		
}

getDeviceID(grenadetype)
{
	switch(grenadetype)
	{
		// frag grenades
		case "frag_grenade_american_mp":	return 1;
		case "frag_grenade_british_mp":			return 2;
		case "frag_grenade_russian_mp":		return 3;
		case "frag_grenade_german_mp":		return 4;

		// american smoke grenades
		case "smoke_grenade_american_mp":				return 10;
		case "smoke_grenade_american_blue_mp":		return 11;
		case "smoke_grenade_american_green_mp":		return 12;
		case "smoke_grenade_american_orange_mp":	return 13;
		case "smoke_grenade_american_pink_mp":		return 14;
		case "smoke_grenade_american_red_mp":			return 15;
		case "smoke_grenade_american_yellow_mp":		return 16;

		// british smoke grenades
		case "smoke_grenade_british_mp":						return 20;
		case "smoke_grenade_british_blue_mp":			return 21;
		case "smoke_grenade_british_green_mp":			return 22;
		case "smoke_grenade_british_orange_mp":		return 23;
		case "smoke_grenade_british_pink_mp":			return 24;
		case "smoke_grenade_british_red_mp":				return 25;
		case "smoke_grenade_british_yellow_mp":			return 26;

		// russian smoke grenades
		case "smoke_grenade_russian_mp":					return 30;
		case "smoke_grenade_russian_blue_mp":			return 31;
		case "smoke_grenade_russian_green_mp":		return 32;
		case "smoke_grenade_russian_orange_mp":		return 33;
		case "smoke_grenade_russian_pink_mp":			return 34;
		case "smoke_grenade_russian_red_mp":			return 35;
		case "smoke_grenade_russian_yellow_mp":		return 36;

		// german smoke grenades
		case "smoke_grenade_german_mp":					return 40;
		case "smoke_grenade_german_blue_mp":			return 41;
		case "smoke_grenade_german_green_mp":		return 42;
		case "smoke_grenade_german_orange_mp":		return 43;
		case "smoke_grenade_german_pink_mp":			return 44;
		case "smoke_grenade_german_red_mp":			return 45;
		case "smoke_grenade_german_yellow_mp":		return 46;

		case "gas_mp":					return 47;
		case "fire_mp":					return 48;
		case "sticky_mp":					return 49;

		case "smoke_grenade_german_gas_mp":			return 50;
		case "smoke_grenade_american_gas_mp":		return 51;
		case "smoke_grenade_british_gas_mp":		return 52;
		case "smoke_grenade_russian_gas_mp":			return 53;

		case "smoke_grenade_german_sticky_mp":			return 54;
		case "smoke_grenade_american_sticky_mp":		return 55;
		case "smoke_grenade_british_sticky_mp":		return 56;
		case "smoke_grenade_russian_sticky_mp":			return 57;

		case "smoke_grenade_british_fire_mp":		return 58;
		case "smoke_grenade_russian_fire_mp":			return 59;
		case "smoke_grenade_german_fire_mp":			return 60;
		case "smoke_grenade_american_fire_mp":		return 61;

		case "smoke_grenade_british_satchel_mp":		return 62;
		case "smoke_grenade_russian_satchel_mp":			return 63;
		case "smoke_grenade_german_satchel_mp":			return 64;
		case "smoke_grenade_american_satchel_mp":		return 65;
		case "satchel_mp":		return 66;

	}
}

giveAmmo(grenadetype,var)
{
	self endon("disconnect");

	if(isPlayer(self))
	{	
		iAmmo = var + self getCurrentAmmo(grenadetype);

		if(iAmmo < 1) return;

		self setWeaponClipAmmo(grenadetype, iAmmo);
		self playSound("grenade_pickup");
	}
}

takeAmmo(grenadetype,var)
{
	self endon("disconnect");

	if(isPlayer(self))
	{
		iAmmo = self getCurrentAmmo(grenadetype);

		if(iAmmo == 0) return;

		self setWeaponClipAmmo(grenadetype, iAmmo - var);
	}
}

getCurrentAmmo(grenadetype)
{
	return self getAmmoCount(grenadetype);
}

getWeaponModel(grenadetype)
{
	deviceID = getDeviceID(grenadetype);
	switch(deviceID)
	{
		// frag grenades
		case 1: return "xmodel/weapon_mk2fraggrenade";
		case 2: return "xmodel/weapon_mk1grenade";
		case 3: return "xmodel/weapon_russian_handgrenade";
		case 4: return "xmodel/weapon_nebelhandgrenate";

		// smoke grenades
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
		case 16:

		case 20:
		case 21:
		case 22:
		case 23:
		case 24:
		case 25:
		case 26:

		case 30:
		case 31:
		case 32:
		case 33:
		case 34:
		case 35:
		case 36:

		case 40:
		case 41:
		case 42:
		case 43:
		case 44:
		case 45:
		case 46: return "xmodel/weapon_us_smoke_grenade";

		case 47:
		case 48: return "xmodel/weapon_incendiary_grenade";
		case 49: return "xmodel/weapon_stickybomb";
		case 50:
		case 51:
		case 52:
		case 53: return "xmodel/weapon_incendiary_grenade";
		case 54:
		case 55:
		case 56:
		case 57: return "xmodel/weapon_stickybomb";
		case 58:
		case 59:
		case 60:
		case 61: return "xmodel/weapon_incendiary_grenade";
		case 62:
		case 63:
		case 64:
		case 65:
		case 66: return "xmodel/projectile_us_satch_grenade";
	}
}

getWeaponHud(grenadetype)
{
	deviceID = getDeviceID(grenadetype);
	switch(deviceID)
	{
		// frag grenades
		case 1: return "gfx/icons/hud@us_grenade_C.tga";
		case 2: return "gfx/icons/hud@british_grenade_C.tga";
		case 3: return "gfx/icons/hud@russian_grenade_C.tga";
		case 4: return "gfx/icons/hud@steilhandgrenate_C.tga";

		// smoke grenades
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
		case 16:

		case 20:
		case 21:
		case 22:
		case 23:
		case 24:
		case 25:
		case 26:

		case 30:
		case 31:
		case 32:
		case 33:
		case 34:
		case 35:
		case 36:

		case 40:
		case 41:
		case 42:
		case 43:
		case 44:
		case 45:
		case 46: return "hud_us_smokegrenade_C";

		case 47: return "gas_grenade";
		case 48: return "gfx/icons/hud@incenhandgrenade_c.tga";
		case 49: return "gfx/icons/hud@stickie_xplsive.tga";
		case 50:
		case 51:
		case 52:
		case 53: return "gas_grenade";
		case 54:
		case 55:
		case 56:
		case 57: return "gfx/icons/hud@stickie_xplsive.tga";
		case 58:
		case 59:
		case 60:
		case 61: return "gfx/icons/hud@incenhandgrenade_c.tga";
		case 62:
		case 63:
		case 64:
		case 65:
		case 66: return "gfx/custom/satchelkill.tga";
	}
}

getFX(grenadetype)
{
	deviceID = getDeviceID(grenadetype);
	switch(deviceID)
	{
		// frag grenade
		case 1:
		case 2:
		case 3:
		case 62:
		case 63:
		case 64:
		case 65:
		case 66:
		case 4: return "plane_bomb";	// temp explosion

		// smoke grey
		case 10:
		case 20:
		case 30:
		case 40: return "greysmoke";

		// smoke blue
		case 11:
		case 21:
		case 31:
		case 41: return "bluesmoke";

		// smoke green
		case 12:
		case 22:
		case 32:
		case 42: return "greensmoke";

		// smoke orange
		case 13:
		case 23:
		case 33:
		case 43: return "orangesmoke";

		// smoke pink
		case 14:
		case 24:
		case 34:
		case 44: return "pinksmoke";

		// smoke red
		case 15:
		case 25:
		case 35:
		case 45: return "redsmoke";

		// smoke yellow
		case 16:
		case 26:
		case 36:
		case 46: return "yellowsmoke";

		case 47: return "gas";
		case 48: return "fire";
		case 49: return "sticky";
		case 50:
		case 51:
		case 52:
		case 53: return "gas";
		case 54:
		case 55:
		case 56:
		case 57: return "sticky";
		case 58:
		case 59:
		case 60:
		case 61: return "fire";
	}
}

getSound(grenadetype)
{
	deviceID = getDeviceID(grenadetype);
	switch(deviceID)
	{
		// frag grenades
		case 1:
		case 2:
		case 3:
		case 62:
		case 63:
		case 64:
		case 65:
		case 66:
		case 4: return "grenade_explode_default";

		// smoke grenades
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
		case 16:

		case 20:
		case 21:
		case 22:
		case 23:
		case 24:
		case 25:
		case 26:

		case 30:
		case 31:
		case 32:
		case 33:
		case 34:
		case 35:
		case 36:

		case 40:
		case 41:
		case 42:
		case 43:
		case 44:
		case 45:
		case 46: return "smokegrenade_explode_default";

		case 47: return "smokegrenade_explode_default";
		case 48:
		case 49: return "grenade_explode_default";
		case 50:
		case 51:
		case 52:
		case 53: return "smokegrenade_explode_default";
		case 54:
		case 55:
		case 56:
		case 57:
		case 58:
		case 59:
		case 60:
		case 61: return "grenade_explode_default";
	}
}

