// This is really really really ugly, I'm sorry if I offend anyone's sensibilities.

void(entity c) Print_Stats;
void() Dump_DM_Score;
void() Print_Header;

void(entity c) Print_Stats =

{
	bprint (c.netname);
	bprint ("   ");

	if(p1_exists == TRUE)
		{
		if (c.player_number == 1)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player1_frags));
		if (c.player1_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");
		}
	else
		return;
	if(p2_exists == TRUE)
		{
		if (c.player_number == 2)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player2_frags));
		if (c.player2_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");

		}
	else
		return;

	if(p3_exists == TRUE)
		{
		if (c.player_number == 3)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player3_frags));
		if (c.player3_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");
		}
	else
		return;

	if(p4_exists == TRUE)
		{
		if (c.player_number == 4)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player4_frags));
		if (c.player4_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");

		}
	else
		return;

	if(p5_exists == TRUE)
		{
		if (c.player_number == 5)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player5_frags));
		if (c.player5_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");
		}
	else
		return;

	if(p6_exists == TRUE)
		{
		if (c.player_number == 6)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player6_frags));
		if (c.player6_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");
		}
	else
		return;

	if(p7_exists == TRUE)
		{
		if (c.player_number == 7)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player7_frags));
		if (c.player7_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");

		}
	else
		return;

	if(p8_exists == TRUE)
		{
		if (c.player_number == 8)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player8_frags));
		if (c.player8_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");
		}
	else
		return;

	if(p9_exists == TRUE)
		{
		if (c.player_number == 9)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player9_frags));
		if (c.player9_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");

		}
	else
		return;

	if(p10_exists == TRUE)
		{
		if (c.player_number == 10)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player10_frags));
		if (c.player10_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");

		}
	else
		return;

	if(p11_exists == TRUE)
		{
		if (c.player_number == 11)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player11_frags));
		if (c.player11_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");
		}
	else
		return;

	if(p12_exists == TRUE)
		{
		if (c.player_number == 12)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player12_frags));
		if (c.player12_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");

		}
	else
		return;

	if(p13_exists == TRUE)
		{
		if (c.player_number == 13)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player13_frags));
		if (c.player13_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");

		}
	else
		return;

	if(p14_exists == TRUE)
		{
		if (c.player_number == 14)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player14_frags));
		if (c.player14_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");

		}
	else
		return;

	if(p15_exists == TRUE)
		{
		if (c.player_number == 15)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player15_frags));
		if (c.player15_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");
		}
	else
		return;

	if(p16_exists == TRUE)
		{
		if (c.player_number == 16)
			bprint (ftos(c.self_frags));
		else
			bprint (ftos(c.player16_frags));
		if (c.player16_frags > 9 || c.self_frags > 9)
			bprint ("    ");
		else
			bprint ("     ");

		}
	else
		return;

};


void() Dump_DM_Score =
{
	local entity	e, sort, walk;

	if (world.chain)
		error ("DumpScore: world.chain is set");

// build a sorted lis
	e = find(world, classname, "player");
	sort = world;
	while (e)
	{
		if (!sort)
		{
			sort = e;
			e.chain = world;
		}
		else
		{
			if (e.player_number < sort.player_number)
			{
				e.chain = sort;
				sort = e;
			}
			else
			{
				walk = sort;
				do
				{
					if (!walk.chain)
					{
						e.chain = world;
						walk.chain = e;
					}
					else if (walk.chain.player_number > e.player_number)
					{
						e.chain = walk.chain;
						walk.chain = e;
					}
					else
						walk = walk.chain;
				} while (walk.chain != e);
			}
		}
		
		e = find(e, classname, "player");
	}

// print the list
	bprint("\n");
	Print_Header();
	while (sort)
	{
		Print_Stats (sort);
		sort = sort.chain;
		bprint ("\n");
	}
};

void() Print_Header =
{
	local entity	e, sort, walk;

	if (world.chain)
		error ("DumpScore: world.chain is set");

// build a sorted lis
	e = find(world, classname, "player");
	sort = world;
	while (e)
	{
		if (!sort)
		{
			sort = e;
			e.chain = world;
		}
		else
		{
			if (e.player_number < sort.player_number)
			{
				e.chain = sort;
				sort = e;
			}
			else
			{
				walk = sort;
				do
				{
					if (!walk.chain)
					{
						e.chain = world;
						walk.chain = e;
					}
					else if (walk.chain.player_number > e.player_number)
					{
						e.chain = walk.chain;
						walk.chain = e;
					}
					else
						walk = walk.chain;
				} while (walk.chain != e);
			}
		}
		
		e = find(e, classname, "player");
	}

	bprint ("      ");

	while (sort)
	{
		bprint (sort.netname);
		bprint (" ");
		sort = sort.chain;
	}
	bprint ("\n");
};
	