init()
{
	thread removeEntities();
}

removeEntities()
{
	level endon("ex_gameover");

	if(level.ex_entities_debug)
	{
		setCvar("g_listEntity", 1);
		wait(5);
	}

	removeSpawnPoints();

	if(level.ex_entities_debug)
	{
		setCvar("g_listEntity", 1);
		wait(5);
		level thread ShowEntitiesOnHUD();
	}
}

removeSpawnPoints()
{
	sp_entities = [];
	sp_entities[0]  = "mp_dm_spawn";
	sp_entities[1]  = "mp_tdm_spawn";
	sp_entities[2]  = "mp_ctf_spawn_allied";
	sp_entities[3]  = "mp_ctf_spawn_axis";
	sp_entities[4]  = "mp_sd_spawn_attacker";
	sp_entities[5]  = "mp_sd_spawn_defender";
	sp_entities[6]  = "mp_lib_spawn_alliesnonjail";
	sp_entities[7]  = "mp_lib_spawn_axisnonjail";
	sp_entities[8]  = "mp_lib_spawn_alliesinjail";
	sp_entities[9]  = "mp_lib_spawn_axisinjail";
	sp_entities[10] = "mp_tkoth_spawn_allied";
	sp_entities[11] = "mp_tkoth_spawn_axis";

	sp_entities_keep = [];

	switch(level.ex_currentgt)
	{
		case "chq":
		case "cnq":
		case "hq":
		case "htf":
		case "lts":
		case "rbcnq":
		case "tdm":
		case "vip":
			sp_entities_keep[0] = "mp_tdm_spawn";
			break;
		case "ctf":
		case "rbctf":
			sp_entities_keep[0] = "mp_ctf_spawn_allied";
			sp_entities_keep[1] = "mp_ctf_spawn_axis";
			break;
		case "ctfb":
			sp_entities_keep[0] = "mp_ctf_spawn_allied";
			sp_entities_keep[1] = "mp_ctf_spawn_axis";
			if(level.random_flag_position) sp_entities_keep[2] = "mp_dm_spawn"; // random flag position
			break;
		case "dm":
		case "hm":
		case "lms":
			sp_entities_keep[0] = "mp_dm_spawn";
			break;
		case "dom":
		case "ons":
			if(isDefined(level.spawntype))
			{
				switch(level.spawntype)
				{
					case "tdm":
						sp_entities_keep[0] = "mp_tdm_spawn";
						if(!level.use_static_flags) sp_entities_keep[1] = "mp_dm_spawn"; // dynamic flags
						break;
					case "sd":
						sp_entities_keep[0] = "mp_sd_spawn_attacker";
						sp_entities_keep[1] = "mp_sd_spawn_defender";
						if(!level.use_static_flags) sp_entities_keep[2] = "mp_dm_spawn"; // dynamic flags
						break;
					case "ctf":
						sp_entities_keep[0] = "mp_ctf_spawn_allied";
						sp_entities_keep[1] = "mp_ctf_spawn_axis";
						if(!level.use_static_flags) sp_entities_keep[2] = "mp_dm_spawn"; // dynamic flags
						break;
					default:
						sp_entities_keep[0] = "mp_dm_spawn";
						break;
				}
			}
			else sp_entities_keep[0] = "mp_dm_spawn";
			break;
		case "esd":
		case "sd":
			sp_entities_keep[0] = "mp_sd_spawn_attacker";
			sp_entities_keep[1] = "mp_sd_spawn_defender";
			break;
		case "ihtf":
			// level.playerspawnpoints
			// level.flagspawnpoints
			// level.ctfflagspos
			// level.sdbombzonepos
			return;
		case "lib":
			sp_entities_keep[0] = "mp_lib_spawn_alliesnonjail";
			sp_entities_keep[1] = "mp_lib_spawn_axisnonjail";
			sp_entities_keep[2] = "mp_lib_spawn_alliesinjail";
			sp_entities_keep[3] = "mp_lib_spawn_axisinjail";
			break;
		case "tkoth":
			if(isDefined(level.spawn))
			{
				switch(level.spawn)
				{
					case "tkoth":
						sp_entities_keep[0] = "mp_tkoth_spawn_allied";
						sp_entities_keep[1] = "mp_tkoth_spawn_axis";
						break;
					case "sd":
						sp_entities_keep[0] = "mp_sd_spawn_attacker";
						sp_entities_keep[1] = "mp_sd_spawn_defender";
						break;
					case "ctf":
						sp_entities_keep[0] = "mp_ctf_spawn_allied";
						sp_entities_keep[1] = "mp_ctf_spawn_axis";
						break;
				}
			}
			else return;
			break;
		default:
			return;
	}

	for(i = 0; i < sp_entities.size; i++)
	{
		sp_remove = true;

		for(j = 0; j < sp_entities_keep.size; j++)
			if(sp_entities[i] == sp_entities_keep[j]) sp_remove = false;

		if(sp_remove) removeSpawnPointEntities(sp_entities[i]);
	}
}

removeSpawnPointEntities(entity)
{
	entities = getentarray(entity, "classname");

	if(!entities.size) return;

	for(i = 0; i < entities.size; i++)
		entities[i] delete();
}

showEntitiesOnHUD()
{
	level thread monitorGameOver();

	while(level.ex_entities_debug)
	{
		wait(2);

		entities = getentarray("script_origin", "classname");

		if(!isDefined(level.ex_entitiesDebugHUD))
		{
			level.ex_entitiesDebugHUD = newHudElem();
			level.ex_entitiesDebugHUD.x = 320;
			level.ex_entitiesDebugHUD.y = 10;
			level.ex_entitiesDebugHUD.alignX = "center";
			level.ex_entitiesDebugHUD.alignY = "top";
			level.ex_entitiesDebugHUD.fontscale = 2.0;
			level.ex_entitiesDebugHUD.color = (1, 0, 0);
		}
		level.ex_entitiesDebugHUD setValue(entities.size);
	}

	if(isDefined(level.ex_entitiesDebugHUD)) level.ex_entitiesDebugHUD destroy();
}

monitorGameOver()
{
	level waittill("ex_gameover");
	level.ex_entities_debug = false;
}

dumpMapEntities()
{
	level endon("ex_gameover");

	entities = getentarray();
	for(i = 0; i < entities.size; i++)
	{
		if(isDefined(entities[i].classname)) logPrint("classname: " + entities[i].classname + " ");
		if(isDefined(entities[i].name)) logPrint("name: " + entities[i].name);
		logprint("\n");

		wait 0.05;
	}
}

