/**
Spawns zed (kfmonster). Respects current game event (KFMonstersCollection) 
Supports custom monsters.

ZedKind			What kind of zed should be spawned:
				ZED_Custom 			specified by CustomZedClass
				ZED_Boss			spawn end game boss
				ZED_Clot..ZED_Husk 	spawn standard monsters. Monster class will be  
									taken from KFMonstersCollection, so it supports events.
								
CustomZedClass	Custom zed class name WITHOUT package name, e.g. "ZombieBrute". 
				Code will search KFMonstersCollection.MonsterClasses for a given class.
				If class will no be found, monster will not spawn!
				
NewTag						Tag to be assigned to the spawned monster
LocationTag					Tag of the spawn location
RotateToTag					Tag of the actor for spawned zed to loot at

bOffsetFromScriptedPawn		If true, scripted pawn will be used for offset calculation. 
							LocationTag has higher priority than bOffsetFromScriptedPawn. 
LocationOffset				Spawn offset from the chosen location
RotationOffset				Offset from the chosen rotation

@author 		PooSH
 */

class ACTION_SpawnZED extends ScriptedAction;


enum EZEDKind {
	ZED_Custom,
	ZED_Boss,
	ZED_Clot,
	ZED_Crawler,
	ZED_Gorefast,
	ZED_Stalker,
	ZED_Scrake,
	ZED_Fleshpound,
	ZED_Bloat,
	ZED_Siren,
	ZED_Husk,
};


var(Action)		EZEDKind			ZedKind;
var(Action)		string				CustomZedClass;
var(Action)		name				NewTag;
var(Action)		name				LocationTag;
var(Action)		name				RotateToTag;
var(Action)		bool				bOffsetFromScriptedPawn;
var(Action)		vector				LocationOffset;
var(Action)		rotator				RotationOffset;

function bool InitActionFor(ScriptedController C)
{
	local vector L;
	local rotator R;
	local actor a;
	local class<KFMonstersCollection> MC;
	local class<KFMonster> MonsterClass;
	local int i;
	
	if ( KFGameType(C.Level.Game) == none ) {
		log("ACTION_SpawnZED can be used only with KFGameType!");
		return false;
	}
	MC = KFGameType(C.Level.Game).MonsterCollection;

	if ( ZedKind == ZED_Custom ) {
		for ( i=0; i < MC.default.MonsterClasses.length; ++i ) {
			if ( CustomZedClass ~= GetItemName(MC.default.MonsterClasses[i].MClassName) ) {
				MonsterClass = class<KFMonster>(DynamicLoadObject(MC.default.MonsterClasses[i].MClassName, Class'class'));
				break;
			}
		}
	}
	else if ( ZedKind == ZED_Boss )
		MonsterClass = class<KFMonster>(DynamicLoadObject(MC.default.EndGameBossClass, Class'class'));
	else
		MonsterClass = class<KFMonster>(DynamicLoadObject(MC.default.MonsterClasses[int(ZedKind)-2].MClassName, Class'class'));

	if ( MonsterClass == none )
		return false;
	
	if ( LocationTag != '' ) {
		foreach C.AllActors(class'Actor', a, LocationTag)
			break;
	}
	if ( a != none )
		L = a.Location + LocationOffset;
	else if ( bOffsetFromScriptedPawn && C.Pawn != none )
		L = C.Pawn.Location + LocationOffset;
	else
		L = C.SequenceScript.Location + LocationOffset;
	
	a = none;
	if ( RotateToTag != '' ) {
		foreach C.AllActors(class'Actor', a, RotateToTag)
			break;
	}
	if ( a != none )
		R = rotator(a.Location - L) + RotationOffset;
	else if ( bOffsetFromScriptedPawn && C.Pawn != none )
		R = C.Pawn.Rotation + RotationOffset;
	else
		R = C.SequenceScript.Rotation + RotationOffset;
		
	C.Spawn(MonsterClass,,NewTag,L,R);

	return false;	
}

function string GetActionString()
{
	return ActionString@GetEnum(enum'ACTION_SpawnZED.EZEDKind', ZedKind)@CustomZedClass;
}

defaultproperties
{
     ActionString="Spawn ZED"
}
