summaryrefslogtreecommitdiffstats
path: root/game/code/ai/sequencer/sequencer.cpp
blob: 924b5f5ff77146a7615e73582214c89ace6bf9ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//-----------------------------------------------------------------------------
// Copyright (C) 2001 Radical Entertainment Ltd.  All rights reserved.
//
// sequencer.cpp
//
// Description: This class queues the actions of an actor.
//              The sequencer places actions into multiple queues,
//              named tracks, which can be grouped into larger composite
//              actions named sequences, which will be executed in turn.
//
// Modification History:
//  + Created Aug 14, 2001 -- Gary Keong
//      - Snapshot from Hair Club (rev 3) Owner: Bryan Brandt
//-----------------------------------------------------------------------------

#include <raddebug.hpp>
#include <ai\sequencer\sequencer.h>
#include <ai\sequencer\action.h>

static bool s_StopClear = false;

//---------------------------------------------------------------------
// Sequencer class
//---------------------------------------------------------------------

Sequencer::Sequencer():
	m_SequenceCount(0),
	m_State(STATE_NONE),
	m_TimeElapsed(0.0f)
{
}

Sequencer::~Sequencer()
{
	Clear();
}

void Sequencer::ClearSequence(sSequence* seq)
{
	sAction* curAction = seq->actions;

	// iterate over all actions of current sequence
	for (unsigned i = 0; i < seq->actionCount; ++i)
	{
        if ( curAction->action )
        {
            if ( curAction->action->IsRunning() )
            {
                curAction->action->Clear();
            }
            // [Dusit: June 14th, 2003]
            // always gets called to abort safely whether
            // the action's running or not.
            curAction->action->Abort(); 
		}
        tRefCounted::Release( curAction->action );
		curAction->action = 0;

		++curAction;
	}

	seq->actionCount = 0;
}

void Sequencer::Clear()
{
    rAssert(!s_StopClear);
	// empty all sequences
	for (unsigned i = 0; i < m_SequenceCount; ++i)
	{
		ClearSequence(m_Sequences + i);
	}

	m_SequenceCount = 0;
	m_State = STATE_NONE;
	m_TimeElapsed = 0.0f;
}

void Sequencer::BeginSequence()
{
	rAssert(m_State == STATE_NONE);
	rAssert(m_SequenceCount < MAX_SEQUENCES);

	// add new sequence, set current sequence index to it,
	// and reset current track to zero
	m_pCurrentSequence = (m_Sequences + m_SequenceCount);
	++m_SequenceCount;

	// reset current sequence
	m_pCurrentSequence->actionCount = 0;

	// indicate we can now add tracks/timed actions
	m_State = STATE_SEQUENCE;
}

void Sequencer::EndSequence()
{
	rAssert(m_State == STATE_SEQUENCE);
	m_State = STATE_NONE;
}

void Sequencer::AddAction(float t_begin, float t_duration, Action* action)
{
	// we need a sequence to add an action
	rAssert(m_State != STATE_NONE);
	rAssert(action != 0);

	// allocate a new track for the action
	sAction* curAction = (m_pCurrentSequence->actions + m_pCurrentSequence->actionCount);
	++(m_pCurrentSequence->actionCount);

	// action runs from t_begin for t_duration seconds
	curAction->timeBegin = t_begin;
	curAction->timeDuration = t_duration;
	curAction->action = action;
    curAction->action->AddRef( );
}

void Sequencer::AddActionToSequence(float t_begin, float t_duration, Action* action)
{
	// we assume a sequence already exists
	rAssert(m_State == STATE_NONE);
	rAssert(m_SequenceCount > 0);
	rAssert(action != 0);

	// allocate a new track for the action
	sAction* curAction = (m_pCurrentSequence->actions + m_pCurrentSequence->actionCount);
	++(m_pCurrentSequence->actionCount);

	// we need to offset our times from NOW
	curAction->timeBegin = (m_TimeElapsed + t_begin);
	curAction->timeDuration = t_duration;
	curAction->action = action;
    curAction->action->AddRef( );
}

bool Sequencer::IsBusy() const
{
	if (m_SequenceCount <= 0)
		return false;

	unsigned actionIdx = 0;
	const sAction* curAction = m_Sequences->actions;

	// iterate over all tracks of current sequence
	while (actionIdx < m_Sequences->actionCount)
	{
		// is track active?
		if (curAction->action != 0)
			return true;

		++actionIdx;
		++curAction;
	}

	// no tracks had actions in queue
	return false;
}

void Sequencer::WakeUp(float time)
{
	if (m_SequenceCount <= 0)
		return;

// CLOSER??? m_TimeElapsed += SIMULATION_TIME;
    m_TimeElapsed += time;

	unsigned actionIdx = 0;
	sAction* curAction = m_Sequences->actions;

	// iterate over all tracks of current sequence
	while (actionIdx < m_Sequences->actionCount)
	{
		// is track active?
		if (curAction->action != 0)
		{
			// check to see if action should be running yet
			if (curAction->timeBegin <= m_TimeElapsed)
			{
				Action* action = curAction->action;

				// Is the current action sleeping?
				if (action->IsSleeping())
				{
					// Wake-up the action.
					action->WakeUp(time);
					// Start the action only if wakeup didn't kill it
					if (!action->IsDone())
					{
						action->Run();
					}
				}
			}
		}

		++actionIdx;
		++curAction;
	}
}

void Sequencer::DoSimulation(float time)
{
	if (m_SequenceCount <= 0)
		return;

	unsigned actionIdx = 0;
	sAction* curAction = m_Sequences->actions;

	// iterate over all tracks of current sequence
	while (actionIdx < m_Sequences->actionCount)
	{
		// is track active?
		if (curAction->action != 0)
		{
			// check to see if action should be running yet
			if (curAction->timeBegin <= m_TimeElapsed)
			{
				Action* action = curAction->action;

				// Is the current action running? Yes, then do a simulation.
				if (action->IsRunning())
				{
					action->DoSimulation(time);
				}

				// if action is non-indefinite, age it
				if (curAction->timeDuration >= 0.0f)
				{
					// age the action
					curAction->timeDuration -= time;

					// if the action time has elapsed, finish it
					if (curAction->timeDuration <= 0.0f)
					{
						action->Done();
					}
				}
			}
		}

		++actionIdx;
		++curAction;
	}
}

void Sequencer::Update(float time)
{
	if (m_SequenceCount <= 0)
		return;

	unsigned masterCount = 0;

	unsigned actionIdx = 0;
	sAction* curAction = m_Sequences->actions;

	// iterate over all tracks
	while (actionIdx < m_Sequences->actionCount)
	{
		Action* action = curAction->action;

		// does track have an action?
		if (action != 0)
		{
			// Is the current action running? Yes, then get the new status.
			if (action->IsRunning())
			{
                s_StopClear = true;
				action->Update(time);
                s_StopClear = false;
			}

			// Is the current action done or failed?
			// Yes, then clear action
			if (action->IsDone())
			{
				action->Clear();
				action->Release( );
				curAction->action = 0;
			}
			else
			{
				// accumulate master action count
				if (!action->IsSlave())
				{
					++masterCount;
				}
			}
		}

		++actionIdx;
		++curAction;
	}

	if (masterCount <= 0)
	{
		// we have no master actions, so move on to
		// the next sequence
		ClearSequence(m_Sequences);

		for (unsigned i = 1; i < m_SequenceCount; ++i)
		{
			m_Sequences[i-1] = m_Sequences[i];
		}

        if(m_SequenceCount)
        {
    		--(m_SequenceCount);
        }

		// reset time origin for next sequence
		m_TimeElapsed = 0.0f;
	}
}

// End of file.