stateMachine.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #include "T3D/components/game/stateMachine.h"
  23. StateMachine::StateMachine()
  24. {
  25. mStateStartTime = -1;
  26. mStateTime = 0;
  27. mStartingState = "";
  28. mCurCreateState = NULL;
  29. mStateMachineFile = StringTable->EmptyString();
  30. mCurCreateState = nullptr;
  31. }
  32. StateMachine::~StateMachine()
  33. {
  34. }
  35. void StateMachine::loadStateMachineFile()
  36. {
  37. if (!mXMLReader)
  38. {
  39. SimXMLDocument *xmlrdr = new SimXMLDocument();
  40. xmlrdr->registerObject();
  41. mXMLReader = xmlrdr;
  42. }
  43. bool hasStartState = false;
  44. if (!dStrIsEmpty(mStateMachineFile))
  45. {
  46. //use our xml reader to parse the file!
  47. SimXMLDocument *reader = mXMLReader.getObject();
  48. if (!reader->loadFile(mStateMachineFile))
  49. Con::errorf("Could not load state machine file: &s", mStateMachineFile);
  50. if (!reader->pushFirstChildElement("StateMachine"))
  51. return;
  52. //find our starting state
  53. if (reader->pushFirstChildElement("StartingState"))
  54. {
  55. mStartingState = reader->getData();
  56. reader->popElement();
  57. hasStartState = true;
  58. }
  59. readStates();
  60. }
  61. if (hasStartState)
  62. mCurrentState = getStateByName(mStartingState);
  63. mStateStartTime = -1;
  64. mStateTime = 0;
  65. }
  66. void StateMachine::readStates()
  67. {
  68. SimXMLDocument *reader = mXMLReader.getObject();
  69. //iterate through our states now!
  70. if (reader->pushFirstChildElement("State"))
  71. {
  72. //get our first state
  73. State firstState;
  74. readStateName(&firstState, reader);
  75. readStateScriptFunction(&firstState, reader);
  76. readTransitions(firstState);
  77. mStates.push_back(firstState);
  78. //now, iterate the siblings
  79. while (reader->nextSiblingElement("State"))
  80. {
  81. State newState;
  82. readStateName(&newState, reader);
  83. readStateScriptFunction(&newState, reader);
  84. readTransitions(newState);
  85. mStates.push_back(newState);
  86. }
  87. }
  88. }
  89. void StateMachine::readTransitions(State &currentState)
  90. {
  91. SimXMLDocument *reader = mXMLReader.getObject();
  92. //iterate through our states now!
  93. if (reader->pushFirstChildElement("Transition"))
  94. {
  95. //get our first state
  96. StateTransition firstTransition;
  97. readTransitonTarget(&firstTransition, reader);
  98. readConditions(firstTransition);
  99. currentState.mTransitions.push_back(firstTransition);
  100. //now, iterate the siblings
  101. while (reader->nextSiblingElement("Transition"))
  102. {
  103. StateTransition newTransition;
  104. readTransitonTarget(&newTransition, reader);
  105. readConditions(newTransition);
  106. currentState.mTransitions.push_back(newTransition);
  107. }
  108. reader->popElement();
  109. }
  110. }
  111. void StateMachine::readConditions(StateTransition &currentTransition)
  112. {
  113. SimXMLDocument *reader = mXMLReader.getObject();
  114. //iterate through our states now!
  115. if (reader->pushFirstChildElement("Rule"))
  116. {
  117. //get our first state
  118. StateTransition::Condition firstCondition;
  119. StateField firstField;
  120. readFieldName(&firstField, reader);
  121. firstCondition.field = firstField;
  122. readFieldComparitor(&firstCondition, reader);
  123. readFieldValue(&firstCondition.field, reader);
  124. currentTransition.mTransitionRules.push_back(firstCondition);
  125. //now, iterate the siblings
  126. while (reader->nextSiblingElement("Transition"))
  127. {
  128. StateTransition::Condition newCondition;
  129. StateField newField;
  130. readFieldName(&newField, reader);
  131. newCondition.field = newField;
  132. readFieldComparitor(&newCondition, reader);
  133. readFieldValue(&newCondition.field, reader);
  134. currentTransition.mTransitionRules.push_back(newCondition);
  135. }
  136. reader->popElement();
  137. }
  138. }
  139. S32 StateMachine::parseComparitor(const char* comparitorName)
  140. {
  141. S32 targetType = -1;
  142. if (!dStrcmp("GreaterThan", comparitorName))
  143. targetType = StateMachine::StateTransition::Condition::GeaterThan;
  144. else if (!dStrcmp("GreaterOrEqual", comparitorName))
  145. targetType = StateMachine::StateTransition::Condition::GreaterOrEqual;
  146. else if (!dStrcmp("LessThan", comparitorName))
  147. targetType = StateMachine::StateTransition::Condition::LessThan;
  148. else if (!dStrcmp("LessOrEqual", comparitorName))
  149. targetType = StateMachine::StateTransition::Condition::LessOrEqual;
  150. else if (!dStrcmp("Equals", comparitorName))
  151. targetType = StateMachine::StateTransition::Condition::Equals;
  152. else if (!dStrcmp("True", comparitorName))
  153. targetType = StateMachine::StateTransition::Condition::True;
  154. else if (!dStrcmp("False", comparitorName))
  155. targetType = StateMachine::StateTransition::Condition::False;
  156. else if (!dStrcmp("Negative", comparitorName))
  157. targetType = StateMachine::StateTransition::Condition::Negative;
  158. else if (!dStrcmp("Positive", comparitorName))
  159. targetType = StateMachine::StateTransition::Condition::Positive;
  160. else if (!dStrcmp("DoesNotEqual", comparitorName))
  161. targetType = StateMachine::StateTransition::Condition::DoesNotEqual;
  162. return targetType;
  163. }
  164. void StateMachine::update()
  165. {
  166. //we always check if there's a timout transition, as that's the most generic transition possible.
  167. F32 curTime = Sim::getCurrentTime();
  168. if (mStateStartTime == -1)
  169. mStateStartTime = curTime;
  170. mStateTime = curTime - mStateStartTime;
  171. char buffer[64];
  172. dSprintf(buffer, sizeof(buffer), "%g", mStateTime);
  173. checkTransitions("stateTime", buffer);
  174. }
  175. void StateMachine::checkTransitions(const char* slotName, const char* newValue)
  176. {
  177. //because we use our current state's fields as dynamic fields on the instance
  178. //we'll want to catch any fields being set so we can treat changes as transition triggers if
  179. //any of the transitions on this state call for it
  180. //One example would be in order to implement burst fire on a weapon state machine.
  181. //The behavior instance has a dynamic variable set up like: GunStateMachine.burstShotCount = 0;
  182. //We also have a transition in our fire state, as: GunStateMachine.addTransition("FireState", "burstShotCount", "DoneShooting", 3);
  183. //What that does is for our fire state, we check the dynamicField burstShotCount if it's equal or greater than 3. If it is, we perform the transition.
  184. //As state fields are handled as dynamicFields for the instance, regular dynamicFields are processed as well as state fields. So we can use the regular
  185. //dynamic fields for our transitions, to act as 'global' variables that are state-agnostic. Alternately, we can use state-specific fields, such as a transition
  186. //like this:
  187. //GunStateMachine.addTransition("IdleState", "Fidget", "Timeout", ">=", 5000);
  188. //That uses the the timeout field, which is reset each time the state changes, and so state-specific, to see if it's been 5 seconds. If it has been, we transition
  189. //to our fidget state
  190. //so, lets check our current transitions
  191. //now that we have the type, check our transitions!
  192. for (U32 t = 0; t < mCurrentState.mTransitions.size(); t++)
  193. {
  194. //if (!dStrcmp(mCurrentState.mTransitions[t]., slotName))
  195. {
  196. //found a transition looking for this variable, so do work
  197. //first, figure out what data type thie field is
  198. //S32 type = getVariableType(newValue);
  199. bool fail = false;
  200. bool match = false;
  201. S32 ruleCount = mCurrentState.mTransitions[t].mTransitionRules.size();
  202. for (U32 r = 0; r < ruleCount; r++)
  203. {
  204. const char* fieldName = mCurrentState.mTransitions[t].mTransitionRules[r].field.name;
  205. if (!dStrcmp(fieldName, slotName))
  206. {
  207. match = true;
  208. //now, check the value with the comparitor and see if we do the transition.
  209. if (!passComparitorCheck(newValue, mCurrentState.mTransitions[t].mTransitionRules[r]))
  210. {
  211. fail = true;
  212. break;
  213. }
  214. }
  215. }
  216. //If we do have a transition rule for this field, and we didn't fail on the condition, go ahead and switch states
  217. if (match && !fail)
  218. {
  219. setState(mCurrentState.mTransitions[t].mStateTarget);
  220. return;
  221. }
  222. }
  223. }
  224. }
  225. bool StateMachine::passComparitorCheck(const char* var, StateTransition::Condition transitionRule)
  226. {
  227. F32 num = dAtof(var);
  228. switch (transitionRule.field.fieldType)
  229. {
  230. case StateField::Type::VectorType:
  231. switch (transitionRule.triggerComparitor)
  232. {
  233. case StateTransition::Condition::Equals:
  234. case StateTransition::Condition::GeaterThan:
  235. case StateTransition::Condition::GreaterOrEqual:
  236. case StateTransition::Condition::LessThan:
  237. case StateTransition::Condition::LessOrEqual:
  238. case StateTransition::Condition::DoesNotEqual:
  239. //do
  240. break;
  241. default:
  242. return false;
  243. };
  244. case StateField::Type::StringType:
  245. switch (transitionRule.triggerComparitor)
  246. {
  247. case StateTransition::Condition::Equals:
  248. if (!dStrcmp(var, transitionRule.field.triggerStringVal))
  249. return true;
  250. else
  251. return false;
  252. case StateTransition::Condition::DoesNotEqual:
  253. if (dStrcmp(var, transitionRule.field.triggerStringVal))
  254. return true;
  255. else
  256. return false;
  257. default:
  258. return false;
  259. };
  260. case StateField::Type::BooleanType:
  261. switch (transitionRule.triggerComparitor)
  262. {
  263. case StateTransition::Condition::TriggerValueTarget::True:
  264. if (dAtob(var))
  265. return true;
  266. else
  267. return false;
  268. case StateTransition::Condition::TriggerValueTarget::False:
  269. if (dAtob(var))
  270. return false;
  271. else
  272. return true;
  273. default:
  274. return false;
  275. };
  276. case StateField::Type::NumberType:
  277. switch (transitionRule.triggerComparitor)
  278. {
  279. case StateTransition::Condition::TriggerValueTarget::Equals:
  280. if (num == transitionRule.field.triggerNumVal)
  281. return true;
  282. else
  283. return false;
  284. case StateTransition::Condition::TriggerValueTarget::GeaterThan:
  285. if (num > transitionRule.field.triggerNumVal)
  286. return true;
  287. else
  288. return false;
  289. case StateTransition::Condition::TriggerValueTarget::GreaterOrEqual:
  290. if (num >= transitionRule.field.triggerNumVal)
  291. return true;
  292. else
  293. return false;
  294. case StateTransition::Condition::TriggerValueTarget::LessThan:
  295. if (num < transitionRule.field.triggerNumVal)
  296. return true;
  297. else
  298. return false;
  299. case StateTransition::Condition::TriggerValueTarget::LessOrEqual:
  300. if (num <= transitionRule.field.triggerNumVal)
  301. return true;
  302. else
  303. return false;
  304. case StateTransition::Condition::TriggerValueTarget::DoesNotEqual:
  305. if (num != transitionRule.field.triggerNumVal)
  306. return true;
  307. else
  308. return false;
  309. case StateTransition::Condition::TriggerValueTarget::Positive:
  310. if (num > 0)
  311. return true;
  312. else
  313. return false;
  314. case StateTransition::Condition::TriggerValueTarget::Negative:
  315. if (num < 0)
  316. return true;
  317. else
  318. return false;
  319. default:
  320. return false;
  321. };
  322. default:
  323. return false;
  324. };
  325. }
  326. void StateMachine::setState(const char* stateName, bool clearFields)
  327. {
  328. State oldState = mCurrentState;
  329. StringTableEntry sName = StringTable->insert(stateName);
  330. for (U32 i = 0; i < mStates.size(); i++)
  331. {
  332. //if(!dStrcmp(mStates[i]->stateName, stateName))
  333. if (!dStrcmp(mStates[i].stateName,sName))
  334. {
  335. mCurrentState = mStates[i];
  336. mStateStartTime = Sim::getCurrentTime();
  337. onStateChanged.trigger(this, i);
  338. return;
  339. }
  340. }
  341. }
  342. const char* StateMachine::getStateByIndex(S32 index)
  343. {
  344. if (index >= 0 && mStates.size() > index)
  345. return mStates[index].stateName;
  346. else
  347. return "";
  348. }
  349. StateMachine::State& StateMachine::getStateByName(const char* name)
  350. {
  351. StringTableEntry stateName = StringTable->insert(name);
  352. for (U32 i = 0; i < mStates.size(); i++)
  353. {
  354. if (!dStrcmp(stateName, mStates[i].stateName))
  355. return mStates[i];
  356. }
  357. }
  358. S32 StateMachine::findFieldByName(const char* name)
  359. {
  360. for (U32 i = 0; i < mFields.size(); i++)
  361. {
  362. if (!dStrcmp(mFields[i].name, name))
  363. return i;
  364. }
  365. return -1;
  366. }