class trigger_tower : ScriptBaseEntity
{	

	int tower_type = 0;				// 0=free, 1=turret, 2=blue, 3=green, 4=yellow, 5=red
			
	string laser_blue = "0 0 255";
	string laser_green = "0 255 0";
	string laser_yellow = "255 255 0";
	string laser_red = "255 0 0";
	
	int laserdamage_blue = 0;
	int laserdamage_green = 750;
	int laserdamage_yellow = 0;
	int laserdamage_red = 999999;

	//keyvalues
	int tower_ID = 0;
	int type_to_build = 0;
	string szPlat = "";				// The platform of this towerspot
	string szTurretspawner = "";	// Squadmaker which spawns the turret
	string szTurret = "";			// Squadmakers child name
	string szLaserBar = "";			//	
	string szLaser1 = "";			//
	string szLaser2 = "";			//
	string szTeleporter = "";		// Yellow's trigger_teleport
	string szBarrier = "";			// trigger_barrier
	string szBox = "";
		
	bool KeyValue( const string& in szKey, const string& in szValue )
	{
		if(szKey == "towerid") // 
		{
			tower_ID = atoi( szValue );
			return true;
		}
		
		else if(szKey == "typetobuild") // 
		{
			type_to_build = atoi( szValue );
			return true;
		}
		
		else if(szKey == "towerplatform") // 
		{
			szPlat = szValue;
			return true;
		}
		
		else if(szKey == "turretspawner") // 
		{
			szTurretspawner = szValue;
			return true;
		}
		
		else if(szKey == "turretname") // 
		{
			szTurret = szValue;
			return true;
		}
		
		else if(szKey == "laserbar") // 
		{
			szLaserBar = szValue;
			return true;
		}
		
		else if(szKey == "laser1") // 
		{
			szLaser1 = szValue;
			return true;
		}
		
		else if(szKey == "laser2") // 
		{
			szLaser2 = szValue;
			return true;
		}

		else if(szKey == "teleporter") // 
		{
			szLaser2 = szTeleporter;
			return true;
		}

		else if(szKey == "barrier") // 
		{
			szLaser2 = szBarrier;
			return true;
		}	
		
		else if(szKey == "box") // 
		{
			szBox = szValue;
			return true;
		}	
		
		else
			return BaseClass.KeyValue( szKey, szValue );
	}
		
	void Use(CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float flValue)
	{
	
		szPlat = "spot" + tower_ID + "_plat";
		szTurretspawner = "spot" + tower_ID + "_t_spawn";
		szTurret = "spot" + tower_ID + "_turret";
		szLaserBar = "spot" + tower_ID + "_laserbar";
		szLaser1 = "spot" + tower_ID + "_laser1";
		szLaser2 = "spot" + tower_ID + "_laser2";
		szTeleporter = "spot" + tower_ID + "_yellow";
		szBarrier = "spot" + tower_ID + "_barrier";
	
		g_Game.AlertMessage( at_console, "Building " + type_to_build + " at " + tower_ID + "\n" );
		
		// Kill Box
		szBox = "spot" + tower_ID + "_box";
		g_EntityFuncs.Remove( g_EntityFuncs.FindEntityByTargetname(null, szBox) );
		
		g_Game.AlertMessage( at_console, "Killed " + szBox + "\n" );
		
	
		// Build 0 - Remove Tower
		if(type_to_build == 0){
		
			if(tower_type != 0){ g_EntityFuncs.FireTargets( szPlat, @self, @self, USE_TOGGLE, flValue, 1.0f ); }
			
			if(tower_type == 1){ g_EntityFuncs.Remove( g_EntityFuncs.FindEntityByTargetname(null, szTurret)); }
			
			if(tower_type >= 2){ 
			
				g_EntityFuncs.FireTargets( szLaserBar, @self, @self, USE_TOGGLE, flValue, 0.5f );
				g_EntityFuncs.FireTargets( szLaser1, @self, @self, USE_TOGGLE, flValue );
				g_EntityFuncs.FireTargets( szLaser2, @self, @self, USE_TOGGLE, flValue );				
				
			}
			
			if(tower_type == 4){ g_EntityFuncs.FireTargets( szTeleporter, @self, @self, USE_TOGGLE, flValue ); }
		
		}
		
		// Build 1 - Turret
		if(type_to_build == 1){
				
			if(tower_type == 0){
			
				g_EntityFuncs.FireTargets( szPlat, @self, @self, USE_TOGGLE, flValue );
				g_EntityFuncs.FireTargets( szTurretspawner, @self, @self, USE_TOGGLE, flValue, 1.0f );
			
			}	

			if(tower_type >= 2){
			
				g_EntityFuncs.FireTargets( szLaserBar, @self, @self, USE_TOGGLE, flValue, 0.5f );
				g_EntityFuncs.FireTargets( szLaser1, @self, @self, USE_TOGGLE, flValue );
				g_EntityFuncs.FireTargets( szLaser2, @self, @self, USE_TOGGLE, flValue );
				g_EntityFuncs.FireTargets( szTurretspawner, @self, @self, USE_TOGGLE, flValue, 2.0f );
			
			}
			
			if(tower_type == 4){ g_EntityFuncs.FireTargets( szTeleporter, @self, @self, USE_TOGGLE, flValue ); }
		
		}
		
		// Build 2-5 - Barriers
		if(type_to_build >= 2){
		
			
					
			// Set Barrier Color & Damage
			if(type_to_build == 2){
			
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser1).edict(), "rendercolor", laser_blue );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser2).edict(), "rendercolor", laser_blue );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser1).edict(), "damage", laserdamage_blue );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser2).edict(), "damage", laserdamage_blue );
			
			}
			
			if(type_to_build == 3){
			
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser1).edict(), "rendercolor", laser_green );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser2).edict(), "rendercolor", laser_green );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser1).edict(), "damage", laserdamage_green );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser2).edict(), "damage", laserdamage_green );
			
			}
			
			if(type_to_build == 4){
			
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser1).edict(), "rendercolor", laser_yellow );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser2).edict(), "rendercolor", laser_yellow );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser1).edict(), "damage", laserdamage_yellow );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser2).edict(), "damage", laserdamage_yellow );
			
			}
			
			if(type_to_build == 5){
			
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser1).edict(), "rendercolor", laser_red );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser2).edict(), "rendercolor", laser_red );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser1).edict(), "damage", laserdamage_red );
				g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szLaser2).edict(), "damage", laserdamage_red );
			
			}
			
			if(tower_type == 0){
			
				g_EntityFuncs.FireTargets( szPlat, @self, @self, USE_TOGGLE, flValue );
				g_EntityFuncs.FireTargets( szLaserBar, @self, @self, USE_TOGGLE, flValue, 1.0f );
				g_EntityFuncs.FireTargets( szLaser1, @self, @self, USE_TOGGLE, flValue, 2.0f );
				g_EntityFuncs.FireTargets( szLaser2, @self, @self, USE_TOGGLE, flValue, 2.0f );
			
			}
			
			if(tower_type == 1){
				g_EntityFuncs.Remove( g_EntityFuncs.FindEntityByTargetname(null, szTurret));
				g_EntityFuncs.FireTargets( szLaserBar, @self, @self, USE_TOGGLE, flValue );
				g_EntityFuncs.FireTargets( szLaser1, @self, @self, USE_TOGGLE, flValue, 1.0f );
				g_EntityFuncs.FireTargets( szLaser2, @self, @self, USE_TOGGLE, flValue, 1.0f );
				
			}
			
			//Turn off Teleporter if type is yellow and different type wants to build
			if(type_to_build != 4 && tower_type == 4){ g_EntityFuncs.FireTargets( szTeleporter, @self, @self, USE_TOGGLE, flValue ); }
			//Turn on Teleporter if type is not yellow but yellow wants to build
			if(type_to_build == 4 && tower_type != 4){ g_EntityFuncs.FireTargets( szTeleporter, @self, @self, USE_TOGGLE, flValue ); }
		
		}
		
		tower_type = type_to_build;
		g_EntityFuncs.DispatchKeyValue( g_EntityFuncs.FindEntityByTargetname(null, szBarrier).edict(), "typetobuild", tower_type );
		
		g_Game.AlertMessage( at_console, "Done. \n" );
		
	
	}

}