stateMachine.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef STATE_MACHINE_H
  23. #define STATE_MACHINE_H
  24. #ifndef _SIMBASE_H_
  25. #include "console/simBase.h"
  26. #endif
  27. #ifndef _OBJECTTYPES_H_
  28. #include "T3D/objectTypes.h"
  29. #endif
  30. #ifndef _MMATH_H_
  31. #include "math/mMath.h"
  32. #endif
  33. #ifndef _XMLDOC_H_
  34. #include "console/SimXMLDocument.h"
  35. #endif
  36. class StateMachine
  37. {
  38. public:
  39. struct StateField
  40. {
  41. StringTableEntry name;
  42. bool triggerBoolVal;
  43. float triggerNumVal;
  44. Point3F triggerVectorVal;
  45. String triggerStringVal;
  46. enum Type
  47. {
  48. BooleanType = 0,
  49. NumberType,
  50. VectorType,
  51. StringType
  52. }fieldType;
  53. };
  54. struct UniqueReference
  55. {
  56. SimObject* referenceObj;
  57. const char* referenceVar;
  58. const char* uniqueName;
  59. };
  60. struct StateTransition
  61. {
  62. struct Condition
  63. {
  64. enum TriggerValueTarget
  65. {
  66. Equals = 0,
  67. GeaterThan,
  68. LessThan,
  69. GreaterOrEqual,
  70. LessOrEqual,
  71. True,
  72. False,
  73. Positive,
  74. Negative,
  75. DoesNotEqual
  76. };
  77. StateField field;
  78. TriggerValueTarget triggerComparitor;
  79. UniqueReference *valUniqueRef;
  80. };
  81. StringTableEntry mName;
  82. StringTableEntry mStateTarget;
  83. Vector<Condition> mTransitionRules;
  84. };
  85. struct State
  86. {
  87. Vector<StateTransition> mTransitions;
  88. StringTableEntry stateName;
  89. StringTableEntry callbackName;
  90. };
  91. StringTableEntry mStateMachineFile;
  92. protected:
  93. Vector<State> mStates;
  94. Vector<StateField> mFields;
  95. Vector<UniqueReference> mUniqueReferences;
  96. State mCurrentState;
  97. F32 mStateStartTime;
  98. F32 mStateTime;
  99. StringTableEntry mStartingState;
  100. State *mCurCreateSuperState;
  101. State *mCurCreateState;
  102. SimObjectPtr<SimXMLDocument> mXMLReader;
  103. public:
  104. StateMachine();
  105. virtual ~StateMachine();
  106. void update();
  107. void loadStateMachineFile();
  108. void readStates();
  109. void readTransitions(State &currentState);
  110. void readConditions(StateTransition &newTransition);
  111. void setState(const char* stateName, bool clearFields = true);
  112. const char* getCurrentStateName() { return mCurrentState.stateName; }
  113. State& getCurrentState() {
  114. return mCurrentState;
  115. }
  116. S32 getStateCount() { return mStates.size(); }
  117. const char* getStateByIndex(S32 index);
  118. State& getStateByName(const char* name);
  119. void checkTransitions(const char* slotName, const char* newValue);
  120. bool passComparitorCheck(const char* var, StateTransition::Condition transitionRule);
  121. S32 findFieldByName(const char* name);
  122. S32 getFieldsCount() { return mFields.size(); }
  123. StateField getField(U32 index)
  124. {
  125. if (index <= mFields.size())
  126. return mFields[index];
  127. }
  128. Signal< void(StateMachine*, S32 stateIdx) > StateMachine::onStateChanged;
  129. //
  130. inline bool readStateName(State* state, SimXMLDocument* reader)
  131. {
  132. if (reader->pushFirstChildElement("Name"))
  133. {
  134. state->stateName = reader->getData();
  135. reader->popElement();
  136. return true;
  137. }
  138. return false;
  139. }
  140. inline bool readStateScriptFunction(State* state, SimXMLDocument* reader)
  141. {
  142. if (reader->pushFirstChildElement("ScriptFunction"))
  143. {
  144. state->callbackName = reader->getData();
  145. reader->popElement();
  146. return true;
  147. }
  148. return false;
  149. }
  150. inline bool readTransitonTarget(StateTransition* transition, SimXMLDocument* reader)
  151. {
  152. if (reader->pushFirstChildElement("StateTarget"))
  153. {
  154. transition->mStateTarget = reader->getData();
  155. reader->popElement();
  156. return true;
  157. }
  158. return false;
  159. }
  160. //
  161. inline bool readFieldName(StateField* newField, SimXMLDocument* reader)
  162. {
  163. if (reader->pushFirstChildElement("FieldName"))
  164. {
  165. newField->name = reader->getData();
  166. reader->popElement();
  167. return true;
  168. }
  169. return false;
  170. }
  171. inline bool readFieldComparitor(StateTransition::Condition* condition, SimXMLDocument* reader)
  172. {
  173. if (reader->pushFirstChildElement("Comparitor"))
  174. {
  175. S32 compIdx = parseComparitor(reader->getData());
  176. condition->triggerComparitor = static_cast<StateTransition::Condition::TriggerValueTarget>(compIdx);
  177. reader->popElement();
  178. return true;
  179. }
  180. return false;
  181. }
  182. inline bool readFieldValue(StateField* field, SimXMLDocument* reader)
  183. {
  184. if (reader->pushFirstChildElement("NumValue"))
  185. {
  186. field->fieldType = StateField::NumberType;
  187. field->triggerNumVal = dAtof(reader->getData());
  188. reader->popElement();
  189. return true;
  190. }
  191. else if (reader->pushFirstChildElement("StringValue"))
  192. {
  193. field->fieldType = StateField::StringType;
  194. field->triggerStringVal = reader->getData();
  195. reader->popElement();
  196. return true;
  197. }
  198. else if (reader->pushFirstChildElement("BoolValue"))
  199. {
  200. field->fieldType = StateField::BooleanType;
  201. field->triggerBoolVal = dAtob(reader->getData());
  202. reader->popElement();
  203. return true;
  204. }
  205. return false;
  206. }
  207. private:
  208. S32 parseComparitor(const char* comparitorName);
  209. };
  210. #endif