init()
{
	precacheShader("gfx/custom/objpoint_tnt.tga");

	level.tnticons["allies"] = spawnstruct();
	level.tnticons["allies"].array = [];
	level.tnticons["axis"] = spawnstruct();
	level.tnticons["axis"].array = [];
	level.tnticons["spectator"] = spawnstruct();
	level.tnticons["spectator"].array = [];

	level thread onPlayerConnect();
}

onPlayerConnect()
{
	for(;;)
	{
		level waittill("connecting", player);

		player thread onPlayerSpawned();
		player thread onPlayerDisconnect();
	}
}

onPlayerSpawned()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("spawned_player");
		
		removeTNTIcon(self.clientid);
	}
}

onPlayerDisconnect()
{
	self waittill("disconnect");
	
	removeTNTIcon(self.clientid);
}

addTNTIcon(entity, id, team)
{
	entity endon("killed_player");
	entity endon("disconnect");

	assert(team == "allies" || team == "axis");


	offset = 20;
	
	if(!isDefined(entity)) return;


	newtnticon = newTeamHudElem(team);
	newtnticon.id = id;
	newtnticon.x = entity.origin[0];
	newtnticon.y = entity.origin[1];
	newtnticon.z = entity.origin[2] + offset;
	newtnticon.alpha = .71;
	newtnticon.archived = true;
	newtnticon setShader("gfx/custom/objpoint_tnt.tga", 8, 8); // 56.8% of on screen headicons size
	newtnticon setwaypoint(true);
	level.tnticons[team].array[level.tnticons[team].array.size] = newtnticon;

	newtnticon2 = newTeamHudElem("spectator");
	newtnticon2.id = id;
	newtnticon2.x = entity.origin[0];
	newtnticon2.y = entity.origin[1];
	newtnticon2.z = entity.origin[2] + offset;
	newtnticon2.alpha = .71;
	newtnticon2.archived = true;
	newtnticon2 setShader("gfx/custom/objpoint_tnt.tga", 8, 8); // 56.8% of on screen headicons size
	newtnticon2 setwaypoint(true);
	level.tnticons["spectator"].array[level.tnticons["spectator"].array.size] = newtnticon2;
	
//	println("ADDED ID: ", id, " to ", team, " array");
//	println("ADDED ID: ", id, " to spectator array");
	
	
}




removeTNTIcon(id)
{
	for(i = 0; i < 3; i++)
	{
		if(i == 0)
			team = "allies";
		else if(i == 1)
			team = "axis";
		else
			team = "spectator";

		removeElement = undefined;
		
		for(j = 0; j < level.tnticons[team].array.size; j++)
		{
			if(level.tnticons[team].array[j].id != id)
				continue;
				
			removeElement = level.tnticons[team].array[j];
			break;
		}
		
		if(isdefined(removeElement))
		{
			lastElement = level.tnticons[team].array.size - 1;
			
			for(j = 0; j < level.tnticons[team].array.size; j++)
			{
			    if(level.tnticons[team].array[j] != removeElement)
			        continue;
			        
			    level.tnticons[team].array[j] = level.tnticons[team].array[lastElement];
			    level.tnticons[team].array[lastElement] = undefined;
			    break;
			}
			
			removeElement destroy();
			
//			println("REMOVED ID: ", id, " from ", team, " array");
		}
	}
}
