/*
	--------------------------------------------------------------
	ACTION_SpawnRandomPickups
	--------------------------------------------------------------

    Spawns random number of pickups on the map. 
	All pickups must have the same tag.

	Author :  PooSH

	--------------------------------------------------------------
*/

class ACTION_SpawnRandomPickups extends ScriptedAction;

var() class<xPickUpBase> PickupType;
var() name PickupTag;
var() int NumToSpawn; 
var() float	PlayerCountScale; // extra scaling per player above 1

//returns adjusted NumToSpawn to difficulty and number of players
function int GetAdjustedNumToSpawn(LevelInfo Level)
{
	local int LivingCount;
	local Controller C;

	for ( C=Level.ControllerList; C!=None; C=C.NextController ) {
		if (C.PlayerReplicationInfo != None	&& C.bIsPlayer 
			&& !C.PlayerReplicationInfo.bOutOfLives && !C.PlayerReplicationInfo.bOnlySpectator )
			LivingCount++;
	}

	if ( LivingCount <= 1 )
		return NumToSpawn;
		
	return NumToSpawn * (1.0 + PlayerCountScale*(LivingCount-1));
}

function bool InitActionFor(ScriptedController C)
{
	local Actor a;
	local xPickUpBase x;
	local array<xPickUpBase> xPickups;
	local int i, count;
	
	Count = GetAdjustedNumToSpawn(C.Level);
	if ( Count <= 0 )
		return false;
	
	foreach C.AllActors(PickupType, a, PickupTag) {
		x = xPickUpBase(a);
		if ( x.myPickUp==none )
			xPickups[xPickups.length] = x;
	}
	
	while ( xPickups.length > Count )
		xPickups.remove(rand(xPickups.length), 1);
	
	log("Spawning "$xPickups.length$" pickups of "$PickupTag$" ("$PickupType$")...");
	
	for ( i=0; i<xPickups.length; ++i )
		xPickups[i].SpawnPickup();
	
	return false;
}

defaultproperties
{
     PickupType=Class'Engine.xPickUpBase'
}
