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

	level.hearticons["allies"] = spawnstruct();
	level.hearticons["allies"].array = [];
	level.hearticons["axis"] = spawnstruct();
	level.hearticons["axis"].array = [];
	level.hearticons["spectator"] = spawnstruct();
	level.hearticons["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");
		
		removeHeartIcon(self.clientid);
	}
}

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

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

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


	offset = 94;
	
	if(isDefined(entity) && isDefined(entity.pers["am_stance"]) ) {
	
		if(entity.pers["am_stance"] == 1) offset = 64;
		if(entity.pers["am_stance"] == 0) offset = 24;
	}

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

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


followPlayer(id,icon,icon2) {

	self endon("disconnect");

	
	while(self.sessionstate == "playing" && isDefined(self.show_heart_icon) && isDefined(icon) && isDefined(icon2)) {

		offset = 94;
	
		if(isDefined(self) && isDefined(self.pers["am_stance"]) ) {
	
			if(self.pers["am_stance"] == 1) offset = 74;
			if(self.pers["am_stance"] == 0) offset = 44;
		}

		if(isDefined(icon)) {
		
			icon.x = self.origin[0];
			icon.y = self.origin[1];
			icon.z = self.origin[2] + offset;
		}

		if(isDefined(icon2)) {
		
			icon2.x = self.origin[0];
			icon2.y = self.origin[1];
			icon2.z = self.origin[2] + offset;
		}
			
		
		wait 0.5;	
	}
	
	removeHeartIcon(id);
}



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