Engine
Class RandomDispatcher

source: c:\runehov\Engine\Classes\RandomDispatcher.uc
Core.Object
   |
   +--Engine.Actor
      |
      +--Engine.Triggers
         |
         +--Engine.Dispatcher
            |
            +--Engine.RandomDispatcher
Direct Known Subclasses:None

class RandomDispatcher
extends Engine.Dispatcher

//============================================================================= // RandomDispatcher. //=============================================================================
Variables
 float AdjustedProbability[8]
           The probability of occurence for
 int CEvent
           Current event.
 int EventCount
           The number of valid events in the
 int EventXLat[8]
           Translates an adjusted event number
 int RepeatCount
           Repetition countdown.

States
RandomDispatch, CheckAutoStart

Function Summary
 void BeginPlay()
     
//-----------------------------------------------------------------------------
// BeginPlay.
//  Initializes EventCount, the EventXLat table and the AdjustedProbability
//  table.
//-----------------------------------------------------------------------------
 void BroadcastNewEvent()
     
//-----------------------------------------------------------------------------
// BroadcastNewEvent.
//-----------------------------------------------------------------------------
 float CalcEventDelay()
     
//-----------------------------------------------------------------------------
// CalcEventDelay.
//-----------------------------------------------------------------------------
 int PickRandomEvent()
     
//-----------------------------------------------------------------------------
// PickRandomEvent.
//-----------------------------------------------------------------------------
 void Trigger(Actor other, Pawn eventInstigator)
     
//-----------------------------------------------------------------------------
// Trigger.
//-----------------------------------------------------------------------------


State RandomDispatch Function Summary
 void Trigger(Actor other, Pawn eventInstigator)
 void BeginState()


State CheckAutoStart Function Summary



Source Code


00001	//=============================================================================
00002	// RandomDispatcher.
00003	//=============================================================================
00004	class RandomDispatcher expands Dispatcher;
00005	
00006	// EDITABLE INSTANCE VARIABLES ////////////////////////////////////////////////
00007	
00008	var(Dispatcher) bool 	bAutoStart;
00009	var(Dispatcher) byte	RandomDelayMin;
00010	var(Dispatcher) byte	RandomDelayMax;
00011	var(Dispatcher) float	OutProbabilities[8];
00012	var(Dispatcher) int		ContiguousEvents;
00013	
00014	// INSTANCE VARIABLES /////////////////////////////////////////////////////////
00015	
00016	var int		EventCount;					// The number of valid events in the
00017											// list (var name OutEvents[]).
00018	var int		EventXLat[8];				// Translates an adjusted event number
00019											// into a raw event number.
00020	var float	AdjustedProbability[8];		// The probability of occurence for
00021											// each event.  This table has been
00022											// adjusted so that a single FRand()
00023											// can scan until < [n].
00024	var int		CEvent;						// Current event.
00025	var int		RepeatCount;				// Repetition countdown.
00026	
00027	// FUNCTIONS //////////////////////////////////////////////////////////////////
00028	
00029	//-----------------------------------------------------------------------------
00030	// BeginPlay.
00031	//  Initializes EventCount, the EventXLat table and the AdjustedProbability
00032	//  table.
00033	//-----------------------------------------------------------------------------
00034	function BeginPlay()
00035	{
00036		local int i;
00037		local float totalProb;
00038	
00039		totalProb = 0.0;
00040		EventCount = 0;
00041		if(ContiguousEvents <= 0)
00042			ContiguousEvents = -1;
00043		if(bIsLooping)
00044			ContiguousEvents = -1;
00045		for(i = 0; i < 8; i++)
00046		{
00047			if(OutEvents[i] == '' || OutProbabilities[i] ~= 0.0)
00048				continue;
00049			EventXLat[EventCount] = i;
00050			totalProb += OutProbabilities[i];
00051			AdjustedProbability[EventCount] = totalProb;
00052			EventCount++;
00053		}
00054		if(EventCount > 0)
00055			for(i = 0; i < EventCount; i++)
00056				AdjustedProbability[i] /= totalProb;
00057		super.BeginPlay();
00058	}
00059	
00060	//-----------------------------------------------------------------------------
00061	// Trigger.
00062	//-----------------------------------------------------------------------------
00063	function Trigger(actor other, pawn eventInstigator)
00064	{
00065		if(EventCount > 0)
00066			GotoState('RandomDispatch');
00067	}
00068	
00069	//-----------------------------------------------------------------------------
00070	// BroadcastNewEvent.
00071	//-----------------------------------------------------------------------------
00072	function BroadcastNewEvent()
00073	{
00074		CEvent = PickRandomEvent();
00075		foreach AllActors(class'Actor', Target, OutEvents[CEvent])
00076			Target.Trigger(self, Instigator);
00077	}
00078	
00079	//-----------------------------------------------------------------------------
00080	// PickRandomEvent.
00081	//-----------------------------------------------------------------------------
00082	function int PickRandomEvent()
00083	{
00084		local float p;
00085		local int i;
00086	
00087		p = FRand();
00088		for(i = 0; i < EventCount; i++)
00089			if(p < AdjustedProbability[i])
00090				return EventXLat[i];
00091	
00092		return 0;
00093	}
00094	
00095	//-----------------------------------------------------------------------------
00096	// CalcEventDelay.
00097	//-----------------------------------------------------------------------------
00098	function float CalcEventDelay()
00099	{
00100		return (FMin(RandomDelayMin, RandomDelayMax)
00101			+ Abs(RandomDelayMax-RandomDelayMin)*FRand())/10.0;
00102	}
00103	
00104	// STATES /////////////////////////////////////////////////////////////////////
00105	
00106	//-----------------------------------------------------------------------------
00107	// CheckAutoStart.
00108	//-----------------------------------------------------------------------------
00109	auto state CheckAutoStart
00110	{
00111	begin:
00112		if(bAutoStart && EventCount > 0)
00113			GotoState('RandomDispatch');
00114		else
00115			GotoState('');
00116	}
00117	
00118	//-----------------------------------------------------------------------------
00119	// RandomDispatch.
00120	//-----------------------------------------------------------------------------
00121	state RandomDispatch
00122	{
00123		function BeginState()
00124		{
00125			RepeatCount = ContiguousEvents;
00126		}
00127	
00128		function Trigger(actor other, pawn eventInstigator)
00129		{
00130			if(bIsLooping)
00131				GotoState('');
00132		}
00133	
00134	begin:
00135		do
00136		{
00137			BroadcastNewEvent();
00138			Sleep(OutDelays[CEvent] + CalcEventDelay());
00139			if(RepeatCount > 0)
00140				RepeatCount--;
00141		} until(RepeatCount == 0);
00142		GotoState('');
00143	}
00144	
00145	defaultproperties
00146	{
00147	}

End Source Code