forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathStateMachine.cpp
More file actions
882 lines (771 loc) · 25.1 KB
/
Copy pathStateMachine.cpp
File metadata and controls
882 lines (771 loc) · 25.1 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
/*
** Command & Conquer Generals(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program 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 3 of the License, or
** (at your option) any later version.
**
** This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// StateMachine.cpp
// Implementation of basic state machine
// Author: Michael S. Booth, January 2002
#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
#include "Common/Errors.h"
#include "Common/StateMachine.h"
#include "Common/ThingTemplate.h"
#include "Common/GameState.h"
#include "Common/GlobalData.h"
#include "Common/Xfer.h"
#include "GameLogic/GameLogic.h"
#include "GameLogic/Object.h"
#include "ref_ptr.h"
//------------------------------------------------------------------------------ Performance Timers
//#include "Common/PerfMetrics.h"
//#include "Common/PerfTimer.h"
//static PerfTimer s_stateMachineTimer("StateMachine::update", false, PERFMETRICS_LOGIC_STARTFRAME, PERFMETRICS_LOGIC_STOPFRAME);
//-------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/**
* Constructor
*/
State::State( StateMachine *machine, AsciiString name )
#ifdef STATE_MACHINE_DEBUG
: m_name(name)
#endif
{
m_ID = INVALID_STATE_ID;
m_successStateID = INVALID_STATE_ID;
m_failureStateID = INVALID_STATE_ID;
m_machine = machine;
}
//-----------------------------------------------------------------------------
/**
* Add another state transition condition for this state
*/
void State::friend_onCondition( StateTransFuncPtr test, StateID toStateID, void* userData, const char* description )
{
m_transitions.push_back(TransitionInfo(test, toStateID, userData, description));
}
//-----------------------------------------------------------------------------
class StIncrementer
{
private:
Int& num;
public:
StIncrementer(Int& n) : num(n)
{
++num;
}
~StIncrementer()
{
--num;
}
};
#ifdef STATE_MACHINE_DEBUG
//-----------------------------------------------------------------------------
std::vector<StateID> * State::getTransitions( void )
{
std::vector<StateID> *ids = new std::vector<StateID>;
ids->push_back(m_successStateID);
ids->push_back(m_failureStateID);
// check transition condition list
if (!m_transitions.empty())
{
for(std::vector<TransitionInfo>::const_iterator it = m_transitions.begin(); it != m_transitions.end(); ++it)
{
ids->push_back(it->toStateID);
}
}
return ids;
}
#endif
//-----------------------------------------------------------------------------
/**
* Given a return code, handle state transitions
*/
StateReturnType State::friend_checkForTransitions( StateReturnType status )
{
static Int checkfortransitionsnum = 0;
StIncrementer inc(checkfortransitionsnum);
if (checkfortransitionsnum >= 20)
{
DEBUG_CRASH(("checkfortransitionsnum is > 20"));
return STATE_FAILURE;
}
DEBUG_ASSERTCRASH(!IS_STATE_SLEEP(status), ("Please handle sleep states prior to this"));
// handle transitions
switch( status )
{
case STATE_SUCCESS:
// check if machine should exit
if (m_successStateID == EXIT_MACHINE_WITH_SUCCESS)
{
getMachine()->internalSetState( MACHINE_DONE_STATE_ID );
return STATE_SUCCESS;
}
else if (m_successStateID == EXIT_MACHINE_WITH_FAILURE)
{
getMachine()->internalSetState( MACHINE_DONE_STATE_ID );
return STATE_FAILURE;
}
// move to new state
return getMachine()->internalSetState( m_successStateID );
case STATE_FAILURE:
// check if machine should exit
if (m_failureStateID == EXIT_MACHINE_WITH_SUCCESS)
{
getMachine()->internalSetState( MACHINE_DONE_STATE_ID );
return STATE_SUCCESS;
}
else if (m_failureStateID == EXIT_MACHINE_WITH_FAILURE)
{
getMachine()->internalSetState( MACHINE_DONE_STATE_ID );
return STATE_FAILURE;
}
// move to new state
return getMachine()->internalSetState( m_failureStateID );
case STATE_CONTINUE:
// check transition condition list
if (!m_transitions.empty())
{
for(std::vector<TransitionInfo>::const_iterator it = m_transitions.begin(); it != m_transitions.end(); ++it)
{
if (it->test( this, it->userData ))
{
// test returned true, change to associated state
#ifdef STATE_MACHINE_DEBUG
if (getMachine()->getWantsDebugOutput())
{
DEBUG_LOG(("%d '%s' -- '%s' condition '%s' returned true!", TheGameLogic->getFrame(), getMachineOwner()->getTemplate()->getName().str(),
getMachine()->getName().str(), it->description ? it->description : "[no description]"));
}
#endif
// check if machine should exit
if (it->toStateID == EXIT_MACHINE_WITH_SUCCESS)
{
return STATE_SUCCESS;
}
else if (it->toStateID == EXIT_MACHINE_WITH_FAILURE)
{
return STATE_FAILURE;
}
// move to new state
return getMachine()->internalSetState( it->toStateID );
}
}
}
break;
}
// the machine keeps running
return STATE_CONTINUE;
}
//-----------------------------------------------------------------------------
/**
* Given a return code, handle state transitions
*/
StateReturnType State::friend_checkForSleepTransitions( StateReturnType status )
{
static Int checkfortransitionsnum = 0;
StIncrementer inc(checkfortransitionsnum);
if (checkfortransitionsnum >= 20)
{
DEBUG_CRASH(("checkforsleeptransitionsnum is > 20"));
return STATE_FAILURE;
}
DEBUG_ASSERTCRASH(IS_STATE_SLEEP(status), ("Please only pass sleep states here"));
// check transition condition list
if (m_transitions.empty())
return status;
for(std::vector<TransitionInfo>::const_iterator it = m_transitions.begin(); it != m_transitions.end(); ++it)
{
if (!it->test( this, it->userData ))
continue;
// test returned true, change to associated state
#ifdef STATE_MACHINE_DEBUG
if (getMachine()->getWantsDebugOutput())
{
DEBUG_LOG(("%d '%s' -- '%s' condition '%s' returned true!", TheGameLogic->getFrame(), getMachineOwner()->getTemplate()->getName().str(),
getMachine()->getName().str(), it->description ? it->description : "[no description]"));
}
#endif
// check if machine should exit
if (it->toStateID == EXIT_MACHINE_WITH_SUCCESS)
{
return STATE_SUCCESS;
}
else if (it->toStateID == EXIT_MACHINE_WITH_FAILURE)
{
return STATE_FAILURE;
}
else
{
// move to new state
return getMachine()->internalSetState( it->toStateID );
}
}
return status;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/**
* Constructor
*/
StateMachine::StateMachine( Object *owner, AsciiString name )
: m_stateMap()
{
m_owner = owner;
m_sleepTill = 0;
m_defaultStateID = INVALID_STATE_ID;
m_defaultStateInited = false;
m_currentState = NULL;
m_locked = false;
#ifdef STATE_MACHINE_DEBUG
m_name = name;
m_debugOutput = false;
m_lockedby = NULL;
#endif
internalClear();
}
//-----------------------------------------------------------------------------
/**
* Destructor. Destroy any states attached to this machine.
*/
StateMachine::~StateMachine()
{
// do not allow current state to exit
if (m_currentState)
m_currentState->onExit( EXIT_RESET );
std::map<StateID, State *>::iterator i;
// delete all states in the mapping
for( i = m_stateMap.begin(); i != m_stateMap.end(); ++i )
{
if ((*i).second)
deleteInstance((*i).second);
}
}
//-----------------------------------------------------------------------------
#ifdef STATE_MACHINE_DEBUG
Bool StateMachine::getWantsDebugOutput() const
{
if (m_debugOutput)
{
return true;
}
if (TheGlobalData->m_stateMachineDebug)
{
return true;
}
#ifdef DEBUG_OBJECT_ID_EXISTS
if (TheObjectIDToDebug != 0 && getOwner() != NULL && getOwner()->getID() == TheObjectIDToDebug)
{
return true;
}
#endif
return false;
}
#endif
//-----------------------------------------------------------------------------
/**
* Clear the internal variables of state machine to known values.
*/
void StateMachine::internalClear()
{
m_goalObjectID = INVALID_ID;
m_goalPosition.x = 0.0f;
m_goalPosition.y = 0.0f;
m_goalPosition.z = 0.0f;
#ifdef STATE_MACHINE_DEBUG
if (getWantsDebugOutput())
{
DEBUG_LOG(("%d '%s' -- '%s' %x internalClear()", TheGameLogic->getFrame(), m_owner->getTemplate()->getName().str(), m_name.str(), this));
}
#endif
}
//-----------------------------------------------------------------------------
/**
* Clear the machine
*/
void StateMachine::clear()
{
// if the machine is locked, it cannot be cleared
if (m_locked)
{
#ifdef STATE_MACHINE_DEBUG
if (m_currentState) DEBUG_LOG((" cur state '%s'", m_currentState->getName().str()));
DEBUG_LOG(("machine is locked (by %s), cannot be cleared (Please don't ignore; this generally indicates a potential logic flaw)",m_lockedby));
#endif
return;
}
// invoke the old state's onExit()
if (m_currentState)
m_currentState->onExit( EXIT_RESET );
m_currentState = NULL;
internalClear();
}
//-----------------------------------------------------------------------------
/**
* Reset the machine to its default state
*/
StateReturnType StateMachine::resetToDefaultState()
{
// if the machine is locked, it cannot be reset
if (m_locked)
{
#ifdef STATE_MACHINE_DEBUG
if (m_currentState) DEBUG_LOG((" cur state '%s'", m_currentState->getName().str()));
DEBUG_LOG(("machine is locked (by %s), cannot be cleared (Please don't ignore; this generally indicates a potential logic flaw)",m_lockedby));
#endif
return STATE_FAILURE;
}
if (!m_defaultStateInited)
{
DEBUG_CRASH(("you may not call resetToDefaultState before initDefaultState"));
return STATE_FAILURE;
}
// allow current state to exit with EXIT_RESET if present
if (m_currentState)
m_currentState->onExit( EXIT_RESET );
m_currentState = NULL;
//
// the current state has done an onExit, clear the internal guts before we set
// the new state, to clear it after the new state is set might be overwriting
// things the new state transition causes to happen
//
internalClear();
// change to the default state
StateReturnType status = internalSetState( m_defaultStateID );
DEBUG_ASSERTCRASH( status != STATE_FAILURE, ( "StateMachine::resetToDefaultState() Error setting default state" ) );
return status;
}
//-----------------------------------------------------------------------------
/**
* Run one step of the machine
*/
StateReturnType StateMachine::updateStateMachine()
{
UnsignedInt now = TheGameLogic->getFrame();
if (m_sleepTill != 0 && now < m_sleepTill)
{
if( m_currentState == NULL )
{
return STATE_FAILURE;
}
return m_currentState->friend_checkForSleepTransitions( STATE_SLEEP(m_sleepTill - now) );
}
// not sleeping anymore
m_sleepTill = 0;
if (m_currentState)
{
// TheSuperHackers @bugfix xezon 20/05/2025 Defer the deletion of this state machine.
// Calling m_currentState->update() can release this state machine in certain circumstances,
// for example if something kills the entity of this state machine as a result of this state update.
// See https://github.com/TheSuperHackers/GeneralsGameCode/issues/212
RefCountPtr<StateMachine> refThis = RefCountPtr<StateMachine>::Create_AddRef(this);
// update() can change m_currentState, so save it for a moment...
State* stateBeforeUpdate = m_currentState;
// execute this state
StateReturnType status = m_currentState->update();
// it is possible that the state's update() method clears the state machine.
if (m_currentState == NULL)
{
return STATE_FAILURE;
}
// here's the scenario:
// -- State A calls foo() and then says "sleep for 2000 frames".
// -- however, foo() called setState() to State B. thus our current state is not the same.
// -- thus, if the state changed, we must ignore any sleep result and pretend we got STATE_CONTINUE,
// so that the new state will be called immediately.
if (stateBeforeUpdate != m_currentState)
{
status = STATE_CONTINUE;
}
if (IS_STATE_SLEEP(status))
{
// hey, we're sleepy!
m_sleepTill = now + GET_STATE_SLEEP_FRAMES(status);
return m_currentState->friend_checkForSleepTransitions( STATE_SLEEP(m_sleepTill - now) );
}
else
{
// check for state transitions, possibly exiting this machine
return m_currentState->friend_checkForTransitions( status );
}
}
else
{
DEBUG_CRASH(("State machine has no current state -- did you remember to call initDefaultState?"));
return STATE_FAILURE;
}
}
//-----------------------------------------------------------------------------
/**
* Given a unique (for this machine) id number, and an instance of the
* State class, the machine records this as a possible state, and
* retains the id mapping.
* These state id's are used to change the machine's state via setState().
*/
void StateMachine::defineState( StateID id, State *state, StateID successID, StateID failureID, const StateConditionInfo* conditions )
{
#ifdef STATE_MACHINE_DEBUG
DEBUG_ASSERTCRASH(m_stateMap.find( id ) == m_stateMap.end(), ("duplicate state ID in statemachine %s",m_name.str()));
#endif
// map the ID to the state
m_stateMap.insert( std::map<StateID, State *>::value_type( id, state ) );
// store the ID in the state itself, as well
state->friend_setID( id );
state->friend_onSuccess(successID);
state->friend_onFailure(failureID);
while (conditions && conditions->test != NULL)
{
state->friend_onCondition(conditions->test, conditions->toStateID, conditions->userData);
++conditions;
}
if (m_defaultStateID == INVALID_STATE_ID)
m_defaultStateID = id;
}
//-----------------------------------------------------------------------------
/**
* Given a state ID, return the state instance
*/
State *StateMachine::internalGetState( StateID id )
{
// locate the actual state associated with the given ID
std::map<StateID, State *>::iterator i;
i = m_stateMap.find( id );
if (i == m_stateMap.end())
{
DEBUG_CRASH(( "StateMachine::internalGetState(): Invalid state" ));
throw ERROR_BAD_ARG;
}
return (*i).second;
}
//-----------------------------------------------------------------------------
/**
* Change the current state of the machine.
* This causes the old state's onExit() method to be invoked,
* and the new state's onEnter() method to be invoked.
*/
StateReturnType StateMachine::setState( StateID newStateID )
{
// if the machine is locked, it cannot change state via external events
if (m_locked)
{
#ifdef STATE_MACHINE_DEBUG
if (m_currentState) DEBUG_LOG((" cur state '%s'", m_currentState->getName().str()));
DEBUG_LOG(("machine is locked (by %s), cannot be cleared (Please don't ignore; this generally indicates a potential logic flaw)",m_lockedby));
#endif
return STATE_CONTINUE;
}
return internalSetState( newStateID );
}
//-----------------------------------------------------------------------------
/**
* Change the current state of the machine.
* This causes the old state's onExit() method to be invoked,
* and the new state's onEnter() method to be invoked.
*/
StateReturnType StateMachine::internalSetState( StateID newStateID )
{
State *newState = NULL;
// anytime the state changes, stop sleeping
m_sleepTill = 0;
// if we're not setting the "done" state ID we will continue with the actual transition
if( newStateID != MACHINE_DONE_STATE_ID )
{
// if incoming state is invalid, go to the machine's default state
if (newStateID == INVALID_STATE_ID)
{
newStateID = m_defaultStateID;
if (newStateID == INVALID_STATE_ID)
{
DEBUG_CRASH(("you may NEVER set the current state to an invalid state id."));
return STATE_FAILURE;
}
}
// extract the state associated with the given ID
newState = internalGetState( newStateID );
#ifdef STATE_MACHINE_DEBUG
if (getWantsDebugOutput())
{
StateID curState = INVALID_STATE_ID;
if (m_currentState) {
curState = m_currentState->getID();
}
DEBUG_LOG_RAW(("%d '%s' -- '%s' %x exit ", TheGameLogic->getFrame(), m_owner->getTemplate()->getName().str(), m_name.str(), this));
if (m_currentState) {
DEBUG_LOG_RAW((" '%s' ", m_currentState->getName().str()));
} else {
DEBUG_LOG_RAW((" INVALID_STATE_ID "));
}
if (newState) {
DEBUG_LOG(("enter '%s'", newState->getName().str()));
} else {
DEBUG_LOG(("to INVALID_STATE"));
}
}
#endif
}
// invoke the old state's onExit()
if (m_currentState)
m_currentState->onExit( EXIT_NORMAL );
// set the new state
m_currentState = newState;
// invoke the new state's onEnter()
/// @todo It might be useful to pass the old state in... (MSB)
if( m_currentState )
{
// onEnter() could conceivably change m_currentState, so save it for a moment...
State* stateBeforeEnter = m_currentState;
StateReturnType status = m_currentState->onEnter();
// it is possible that the state's onEnter() method may cause the state to be destroyed
if (m_currentState == NULL)
{
return STATE_FAILURE;
}
// here's the scenario:
// -- State A calls foo() and then says "sleep for 2000 frames".
// -- however, foo() called setState() to State B. thus our current state is not the same.
// -- thus, if the state changed, we must ignore any sleep result and pretend we got STATE_CONTINUE,
// so that the new state will be called immediately.
if (stateBeforeEnter != m_currentState)
{
status = STATE_CONTINUE;
}
if (IS_STATE_SLEEP(status))
{
// hey, we're sleepy!
UnsignedInt now = TheGameLogic->getFrame();
m_sleepTill = now + GET_STATE_SLEEP_FRAMES(status);
return m_currentState->friend_checkForSleepTransitions( STATE_SLEEP(m_sleepTill - now) );
}
else
{
// check for state transitions, possibly exiting this machine
return m_currentState->friend_checkForTransitions( status );
}
}
else
{
return STATE_CONTINUE; // irrelevant return code, but we must return something
}
}
//-----------------------------------------------------------------------------
/**
* Define the default state of the machine, and
* set the machine's state to it.
*/
StateReturnType StateMachine::initDefaultState()
{
#ifdef DEBUG_LOGGING
#ifdef STATE_MACHINE_DEBUG
#define REALLY_VERBOSE_LOG(x) /* DEBUG_LOG_RAW(x) */
// Run through all the transitions and make sure there aren't any transitions to undefined states. jba. [8/18/2003]
std::map<StateID, State *>::iterator i;
REALLY_VERBOSE_LOG(("SM_BEGIN\n"));
for( i = m_stateMap.begin(); i != m_stateMap.end(); ++i ) {
State *state = (*i).second;
StateID id = state->getID();
// Check transitions. [8/18/2003]
std::vector<StateID> *ids = state->getTransitions();
// check transitions
REALLY_VERBOSE_LOG(("State %s(%d) : ", state->getName().str(), id));
if (!ids->empty())
{
for(std::vector<StateID>::const_iterator it = ids->begin(); it != ids->end(); ++it)
{
StateID curID = *it;
REALLY_VERBOSE_LOG(("%d('", curID));
if (curID == INVALID_STATE_ID) {
REALLY_VERBOSE_LOG(("INVALID_STATE_ID', "));
continue;
}
if (curID == EXIT_MACHINE_WITH_SUCCESS) {
REALLY_VERBOSE_LOG(("EXIT_MACHINE_WITH_SUCCESS', "));
continue;
}
if (curID == EXIT_MACHINE_WITH_FAILURE) {
REALLY_VERBOSE_LOG(("EXIT_MACHINE_WITH_FAILURE', "));
continue;
}
// locate the actual state associated with the given ID
std::map<StateID, State *>::iterator i;
i = m_stateMap.find( curID );
if (i == m_stateMap.end()) {
DEBUG_LOG(("\nState %s(%d) : Transition %d not found", state->getName().str(), id, curID));
DEBUG_LOG(("This MUST BE FIXED!!!jba"));
DEBUG_CRASH(("Invalid transition."));
} else {
State *st = (*i).second;
if (st->getName().isNotEmpty()) {
REALLY_VERBOSE_LOG(("%s') ", st->getName().str()));
}
}
}
}
REALLY_VERBOSE_LOG(("\n"));
delete ids;
ids = NULL;
}
REALLY_VERBOSE_LOG(("SM_END\n\n"));
#endif
#endif
DEBUG_ASSERTCRASH(!m_locked, ("Machine is locked here, but probably should not be"));
if (m_defaultStateInited)
{
DEBUG_CRASH(("you may not call initDefaultState twice for the same StateMachine"));
return STATE_FAILURE;
}
else
{
m_defaultStateInited = true;
return internalSetState( m_defaultStateID );
}
}
//-----------------------------------------------------------------------------
void StateMachine::setGoalObject( const Object *obj )
{
if (m_locked)
return;
internalSetGoalObject( obj );
}
//-----------------------------------------------------------------------------
Bool StateMachine::isGoalObjectDestroyed() const
{
if (m_goalObjectID == 0)
{
return false; // never had a goal object
}
return getGoalObject() == NULL;
}
//-----------------------------------------------------------------------------
void StateMachine::halt()
{
m_locked = true;
m_currentState = NULL; // don't exit current state, just clear it.
#ifdef STATE_MACHINE_DEBUG
if (getWantsDebugOutput())
{
DEBUG_LOG(("%d '%s' -- '%s' %x halt()", TheGameLogic->getFrame(), m_owner->getTemplate()->getName().str(), m_name.str(), this));
}
#endif
}
//-----------------------------------------------------------------------------
void StateMachine::internalSetGoalObject( const Object *obj )
{
if (obj) {
m_goalObjectID = obj->getID();
internalSetGoalPosition(obj->getPosition());
}
else {
m_goalObjectID = INVALID_ID;
}
}
//-----------------------------------------------------------------------------
Object *StateMachine::getGoalObject()
{
return TheGameLogic->findObjectByID( m_goalObjectID );
}
//-----------------------------------------------------------------------------
const Object *StateMachine::getGoalObject() const
{
return TheGameLogic->findObjectByID( m_goalObjectID );
}
//-----------------------------------------------------------------------------
void StateMachine::setGoalPosition( const Coord3D *pos )
{
if (m_locked)
return;
internalSetGoalPosition( pos );
}
//-----------------------------------------------------------------------------
void StateMachine::internalSetGoalPosition( const Coord3D *pos )
{
if (pos) {
m_goalPosition = *pos;
// Don't clear the goal object, or everything breaks. Like construction of buildings.
}
}
// ------------------------------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------------------------------
void StateMachine::crc( Xfer *xfer )
{
}
// ------------------------------------------------------------------------------------------------
/** Xfer Method
* Version Info
* 1: Initial version
*/
// ------------------------------------------------------------------------------------------------
void StateMachine::xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
xfer->xferUnsignedInt(&m_sleepTill);
xfer->xferUnsignedInt(&m_defaultStateID);
StateID curStateID = getCurrentStateID();
xfer->xferUnsignedInt(&curStateID);
if (xfer->getXferMode() == XFER_LOAD) {
// We are going to jump into the current state. We don't call onEnter or onExit, because the
// state was already active when we saved.
m_currentState = internalGetState( curStateID );
}
Bool snapshotAllStates = false;
#ifdef RTS_DEBUG
//snapshotAllStates = true;
#endif
xfer->xferBool(&snapshotAllStates);
if (snapshotAllStates) {
std::map<StateID, State *>::iterator i;
// count all states in the mapping
Int count = 0;
for( i = m_stateMap.begin(); i != m_stateMap.end(); ++i )
count++;
Int saveCount = count;
xfer->xferInt(&saveCount);
if (saveCount!=count) {
DEBUG_CRASH(("State count mismatch - %d expected, %d read", count, saveCount));
throw SC_INVALID_DATA;
}
for( i = m_stateMap.begin(); i != m_stateMap.end(); ++i ) {
State *state = (*i).second;
StateID id = state->getID();
xfer->xferUnsignedInt(&id);
if (id!=state->getID()) {
DEBUG_CRASH(("State ID mismatch - %d expected, %d read", state->getID(), id));
throw SC_INVALID_DATA;
}
xfer->xferSnapshot(state);
}
} else {
xfer->xferSnapshot(m_currentState);
}
xfer->xferObjectID(&m_goalObjectID);
xfer->xferCoord3D(&m_goalPosition);
xfer->xferBool(&m_locked);
xfer->xferBool(&m_defaultStateInited);
}
// ------------------------------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------------------------------
void StateMachine::loadPostProcess( void )
{
}