//jdagenet

#library "code"
#include "zcommon.acs"

//Team numbers are hard-coded into the mod this way.
#define TEAM_HumanTeam 0
#define TEAM_GhoulTeam 1
//This number represents how many MORE ghouls there have to be present in-game than humans present in-game.
//Change if needed.
#define TEAM_MaxDifference 3

//The script is now ran by the server as opposed to by the client.
//We were experiencing some issues regarding the script not behaving correctly, hopefully this will fix the issue.
script 112 (void)
{
	//Storing these values in their own variables is a lot cleaner compared to fetching the data on-the-spot.
	int COUNT_HumanTeam = GetTeamProperty(TEAM_HumanTeam, TPROP_NumLivePlayers);
	int COUNT_GhoulTeam = GetTeamProperty(TEAM_GhoulTeam, TPROP_NumLivePlayers);
	
	//Subtract the number of live players from each team to get the difference between the two.
	//Calculation is done inside an absolute value function to prevent the returning of negative values (Though with a simple relocation of the team-count variables, an absolute value function would not be needed).
	int PlayerDifference = abs(COUNT_HumanTeam - COUNT_GhoulTeam);
	
	//Here starts the determining process
	//If ghouls have more live players than humans AND the difference between the team teams is greater than or equal the required difference, then the statement is passed.
	if (COUNT_GhoulTeam > COUNT_HumanTeam && PlayerDifference >= TEAM_MaxDifference)
	{
		//The method of spawning could be simplified quite a bit, but calculating the position this way leaves the door open for more flexibility regarding where the Disciple actually spawns.
		int PLAYER_X = GetActorX(0); int PLAYER_Y = GetActorY(0); int PLAYER_Z = GetActorZ(0);
		//Get's the angle of the player and saves it for later.
		int PLAYER_Angle = GetActorAngle(0) >> 8;
		
		//Spawns the actor associated with the Disciple.
		SpawnForced("HeavenMinionSpawner", PLAYER_X, PLAYER_Y, PLAYER_Z, 0, PLAYER_Angle);
	}
	
}

function int abs (int x)
{
    if (x < 0)
        return -x;

    return x;
}