File: fsm.cpp

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (470 lines) | stat: -rw-r--r-- 14,081 bytes parent folder | download | duplicates (2)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* Copyright (C) 2013 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

// INCLUDES
#include "precompiled.h"
#include "fsm.h"

// DECLARATIONS

//-----------------------------------------------------------------------------
// Name: CFsmEvent()
// Desc: Constructor
//-----------------------------------------------------------------------------
CFsmEvent::CFsmEvent( unsigned int type )
{
	m_Type	= type;
	m_Param	= NULL;
}

//-----------------------------------------------------------------------------
// Name; ~CFsmEvent()
// Desc: Destructor
//-----------------------------------------------------------------------------
CFsmEvent::~CFsmEvent( void )
{
	m_Param = NULL;
}

//-----------------------------------------------------------------------------
// Name: SetParamRef()
// Desc: Sets the parameter for the event
//-----------------------------------------------------------------------------
void CFsmEvent::SetParamRef( void* pParam )
{
	m_Param = pParam;
}

//-----------------------------------------------------------------------------
// Name: CFsmTransition()
// Desc: Constructor
//-----------------------------------------------------------------------------
CFsmTransition::CFsmTransition( unsigned int state )
{
	m_CurrState	= state;
}

//-----------------------------------------------------------------------------
// Name: ~CFsmTransition()
// Desc: Destructor
//-----------------------------------------------------------------------------
CFsmTransition::~CFsmTransition( void )
{
	m_Actions.clear();
	m_Conditions.clear();
}

//-----------------------------------------------------------------------------
// Name: RegisterAction()
// Desc: Adds action that will be executed when the transition will occur
//-----------------------------------------------------------------------------
void CFsmTransition::RegisterAction( void* pAction, void* pContext )
{
	CallbackFunction callback;

	// Add action at the end of actions list
	callback.pFunction	= pAction;
	callback.pContext	= pContext;

	m_Actions.push_back( callback );
}

//-----------------------------------------------------------------------------
// Name: AddCondition()
// Desc: Adds condition which will be evaluated when the transition will occurs
//-----------------------------------------------------------------------------
void CFsmTransition::RegisterCondition( void* pCondition, void* pContext )
{
	CallbackFunction callback;

	// Add condition at the end of conditions list
	callback.pFunction	= pCondition;
	callback.pContext	= pContext;

	m_Conditions.push_back( callback );
}

//-----------------------------------------------------------------------------
// Name: SetEvent()
// Desc: Set event for which transition will occur
//-----------------------------------------------------------------------------
void CFsmTransition::SetEvent( CFsmEvent* pEvent )
{
	m_Event = pEvent;
}

//-----------------------------------------------------------------------------
// Name: SetNextState()
// Desc: Set next state the transition will switch the system to
//-----------------------------------------------------------------------------
void CFsmTransition::SetNextState( unsigned int nextState )
{
	m_NextState = nextState;
}

//-----------------------------------------------------------------------------
// Name: ApplyConditions()
// Desc: Evaluate conditions for the transition
// Note: If there are no conditions, assume true
//-----------------------------------------------------------------------------
bool CFsmTransition::ApplyConditions( void ) const
{
    bool eval = true;

	CallbackList::const_iterator it = m_Conditions.begin();
    for( ; it != m_Conditions.end(); ++it )
	{
		if ( it->pFunction )
		{
			// Evaluate condition
			CONDITION Condition = ( CONDITION )it->pFunction;
			eval &= Condition( it->pContext );
		}
    }

    return eval;
}

//-----------------------------------------------------------------------------
// Name: RunActions()
// Desc: Execute actions for the transition
// Note: If there are no actions, assume true
//-----------------------------------------------------------------------------
bool CFsmTransition::RunActions( void ) const
{
	bool result = true;

	CallbackList::const_iterator it = m_Actions.begin();
	for( ; it != m_Actions.end(); ++it )
	{
		if ( it->pFunction )
		{
			// Run action
			ACTION Action = ( ACTION )it->pFunction;
			result &= Action( it->pContext, m_Event );
		}
	}

	return result;
}

//-----------------------------------------------------------------------------
// Name: CFsm()
// Desc: Constructor
//-----------------------------------------------------------------------------
CFsm::CFsm( void )
{
	m_Done		 = false;
	m_FirstState = FSM_INVALID_STATE;
	m_CurrState	 = FSM_INVALID_STATE;
	m_NextState  = FSM_INVALID_STATE;
}

//-----------------------------------------------------------------------------
// Name: ~CFsm()
// Desc: Destructor
//-----------------------------------------------------------------------------
CFsm::~CFsm( void )
{
	Shutdown();
}

//-----------------------------------------------------------------------------
// Name: Setup()
// Desc: Setup events, actions and state transitions
//-----------------------------------------------------------------------------
void CFsm::Setup( void )
{
	// Does nothing by default
}

//-----------------------------------------------------------------------------
// Name: Reset()
// Desc: Shuts down the state machine and releases any resources
//-----------------------------------------------------------------------------
void CFsm::Shutdown( void )
{
	// Release transitions
	TransitionList::iterator itTransition = m_Transitions.begin();
	for ( ; itTransition < m_Transitions.end(); ++itTransition )
	{
		CFsmTransition* pCurrTransition = *itTransition;
		if ( !pCurrTransition ) continue;

		delete pCurrTransition;
	}

	// Release events
	EventMap::iterator itEvent = m_Events.begin();
	for( ; itEvent != m_Events.end(); ++itEvent )
	{
		CFsmEvent* pCurrEvent = itEvent->second;
		if ( !pCurrEvent ) continue;

		delete pCurrEvent;
	}

	m_States.clear();
	m_Events.clear();
	m_Transitions.clear();

	m_Done		 = false;
	m_FirstState = FSM_INVALID_STATE;
	m_CurrState	 = FSM_INVALID_STATE;
	m_NextState  = FSM_INVALID_STATE;
}

//-----------------------------------------------------------------------------
// Name: AddState()
// Desc: Adds the specified state to the internal list of states
// Note: If a state with the specified ID exists, the state is not added
//-----------------------------------------------------------------------------
void CFsm::AddState( unsigned int state )
{
	m_States.insert( state );
}

//-----------------------------------------------------------------------------
// Name: AddEvent()
// Desc: Adds the specified event to the internal list of events
// Note: If an eveny with the specified ID exists, the event is not added
//-----------------------------------------------------------------------------
CFsmEvent* CFsm::AddEvent( unsigned int eventType )
{
	CFsmEvent* pEvent = NULL;

	// Lookup event by type
	EventMap::iterator it = m_Events.find( eventType );
	if ( it != m_Events.end() )
	{
		pEvent = it->second;
	}
	else
	{
		pEvent = new CFsmEvent( eventType );
		if ( !pEvent ) return NULL;

		// Store new event into internal map
		m_Events[ eventType ] = pEvent;
	}

	return pEvent;
}

//-----------------------------------------------------------------------------
// Name: AddTransition()
// Desc: Adds a new transistion to the state machine
//-----------------------------------------------------------------------------
CFsmTransition* CFsm::AddTransition( 
									unsigned int state,
									unsigned int eventType,
									unsigned int nextState )
{
	// Make sure we store the current state
	AddState( state );

	// Make sure we store the next state
	AddState( nextState );

	// Make sure we store the event
	CFsmEvent* pEvent = AddEvent( eventType );
	if ( !pEvent ) return NULL;

	// Create new transition
	CFsmTransition* pNewTransition = new CFsmTransition( state );
	if ( !pNewTransition )
	{
		delete pEvent;
		return NULL;
	}

	// Setup new transition
	pNewTransition->SetEvent( pEvent );
	pNewTransition->SetNextState( nextState );

	// Store new transition
	m_Transitions.push_back( pNewTransition );

	return pNewTransition;
}

//-----------------------------------------------------------------------------
// Name: AddTransition()
// Desc: Adds a new transition to the state machine
//-----------------------------------------------------------------------------
CFsmTransition* CFsm::AddTransition( 
									unsigned int state,
									unsigned int eventType,
									unsigned int nextState,
									void* pAction,
									void* pContext )
{
	CFsmTransition* pTransition = AddTransition( state, eventType, nextState );
	if ( !pTransition ) return NULL;
	
	// If action specified, register it
	if ( pAction )
		pTransition->RegisterAction( pAction, pContext );

	return pTransition;
}

//-----------------------------------------------------------------------------
// Name: GetTransition()
// Desc: Lookup transition given the state, event and next state to transition
//-----------------------------------------------------------------------------
CFsmTransition* CFsm::GetTransition(
									unsigned int state,
									unsigned int eventType ) const
{
	// Valid state?
	if ( !IsValidState( state ) ) return NULL;

	// Valid event?
	if ( !IsValidEvent( eventType ) ) return NULL;

	// Loop through the list of transitions
	TransitionList::const_iterator it = m_Transitions.begin();
	for ( ; it != m_Transitions.end(); ++it )
	{
		CFsmTransition* pCurrTransition = *it;
		if ( !pCurrTransition ) continue;

		CFsmEvent* pCurrEvent = pCurrTransition->GetEvent();
		if ( !pCurrEvent ) continue;

		// Is it our transition?
		if ( pCurrTransition->GetCurrState() == state &&
			 pCurrEvent->GetType() == eventType )
		{
			return pCurrTransition;
		}
	}

	// No transition found
	return NULL;
}

//-----------------------------------------------------------------------------
// Name: SetFirstState()
// Desc: Set initial state for FSM
//-----------------------------------------------------------------------------
void CFsm::SetFirstState( unsigned int firstState )
{
	m_FirstState = firstState;
}

//-----------------------------------------------------------------------------
// Name: SetCurrState()
// Desc: Set current state and update last state to current state
//-----------------------------------------------------------------------------
void CFsm::SetCurrState( unsigned int state )
{
	m_CurrState = state;
}

//-----------------------------------------------------------------------------
// Name: IsFirstTime()
// Desc: Verifies if the state machine has been already updated
//-----------------------------------------------------------------------------
bool CFsm::IsFirstTime( void ) const
{
	return ( m_CurrState == FSM_INVALID_STATE );
}

//-----------------------------------------------------------------------------
// Name: Update()
// Desc: Updates state machine and retrieves next state
//-----------------------------------------------------------------------------
bool CFsm::Update( unsigned int eventType, void* pEventParam )
{
	// Valid event?
	if ( !IsValidEvent( eventType ) )
		return false;

	// First time update?
	if ( IsFirstTime() )
		m_CurrState = m_FirstState;

	// Lookup transition
	CFsmTransition* pTransition = GetTransition( m_CurrState, eventType );
	if ( !pTransition ) 
		return false;

	// Setup event parameter
	EventMap::iterator it = m_Events.find( eventType );
	if ( it != m_Events.end() )
	{
		CFsmEvent* pEvent = it->second;
		if ( pEvent ) pEvent->SetParamRef( pEventParam );
	}

	// Valid transition?
	if ( !pTransition->ApplyConditions() ) 
		return false;

	// Save the default state transition (actions might call SetNextState
	// to override this)
	SetNextState( pTransition->GetNextState() );

	// Run transition actions
	if ( !pTransition->RunActions() ) 
		return false;

	// Switch state
	SetCurrState( GetNextState() );

	// Reset the next state since it's no longer valid
	SetNextState( FSM_INVALID_STATE );
	
	return true;
}

//-----------------------------------------------------------------------------
// Name: IsDone()
// Desc: Tests whether the state machine has finished its work
// Note: This is state machine specific
//-----------------------------------------------------------------------------
bool CFsm::IsDone( void ) const
{
	// By default the internal flag m_Done is tested
	return m_Done;
}

//-----------------------------------------------------------------------------
// Name: IsValidState()
// Desc: Verifies whether the specified state is managed by FSM
//-----------------------------------------------------------------------------
bool CFsm::IsValidState( unsigned int state ) const
{
	StateSet::const_iterator it = m_States.find( state );
	if ( it == m_States.end() ) return false;

	return true;
}

//-----------------------------------------------------------------------------
// Name: IsValidEvent()
// Desc: Verifies whether the specified event is managed by FSM
//-----------------------------------------------------------------------------
bool CFsm::IsValidEvent( unsigned int eventType ) const
{
	EventMap::const_iterator it = m_Events.find( eventType );
	if ( it == m_Events.end() ) return false;

	return true;
}