stateMachine.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. bool fieldRead = false;
  121. readFieldName(&firstField, reader);
  122. firstCondition.field = firstField;
  123. readFieldComparitor(&firstCondition, reader);
  124. readFieldValue(&firstCondition.field, reader);
  125. currentTransition.mTransitionRules.push_back(firstCondition);
  126. //now, iterate the siblings
  127. while (reader->nextSiblingElement("Transition"))
  128. {
  129. StateTransition::Condition newCondition;
  130. StateField newField;
  131. readFieldName(&newField, reader);
  132. newCondition.field = newField;
  133. readFieldComparitor(&newCondition, reader);
  134. readFieldValue(&newCondition.field, reader);
  135. currentTransition.mTransitionRules.push_back(newCondition);
  136. }
  137. reader->popElement();
  138. }
  139. }
  140. S32 StateMachine::parseComparitor(const char* comparitorName)
  141. {
  142. S32 targetType = -1;
  143. if (!dStrcmp("GreaterThan", comparitorName))
  144. targetType = StateMachine::StateTransition::Condition::GeaterThan;
  145. else if (!dStrcmp("GreaterOrEqual", comparitorName))
  146. targetType = StateMachine::StateTransition::Condition::GreaterOrEqual;
  147. else if (!dStrcmp("LessThan", comparitorName))
  148. targetType = StateMachine::StateTransition::Condition::LessThan;
  149. else if (!dStrcmp("LessOrEqual", comparitorName))
  150. targetType = StateMachine::StateTransition::Condition::LessOrEqual;
  151. else if (!dStrcmp("Equals", comparitorName))
  152. targetType = StateMachine::StateTransition::Condition::Equals;
  153. else if (!dStrcmp("True", comparitorName))
  154. targetType = StateMachine::StateTransition::Condition::True;
  155. else if (!dStrcmp("False", comparitorName))
  156. targetType = StateMachine::StateTransition::Condition::False;
  157. else if (!dStrcmp("Negative", comparitorName))
  158. targetType = StateMachine::StateTransition::Condition::Negative;
  159. else if (!dStrcmp("Positive", comparitorName))
  160. targetType = StateMachine::StateTransition::Condition::Positive;
  161. else if (!dStrcmp("DoesNotEqual", comparitorName))
  162. targetType = StateMachine::StateTransition::Condition::DoesNotEqual;
  163. return targetType;
  164. }
  165. void StateMachine::update()
  166. {
  167. //we always check if there's a timout transition, as that's the most generic transition possible.
  168. F32 curTime = Sim::getCurrentTime();
  169. if (mStateStartTime == -1)
  170. mStateStartTime = curTime;
  171. mStateTime = curTime - mStateStartTime;
  172. char buffer[64];
  173. dSprintf(buffer, sizeof(buffer), "%g", mStateTime);
  174. checkTransitions("stateTime", buffer);
  175. }
  176. void StateMachine::checkTransitions(const char* slotName, const char* newValue)
  177. {
  178. //because we use our current state's fields as dynamic fields on the instance
  179. //we'll want to catch any fields being set so we can treat changes as transition triggers if
  180. //any of the transitions on this state call for it
  181. //One example would be in order to implement burst fire on a weapon state machine.
  182. //The behavior instance has a dynamic variable set up like: GunStateMachine.burstShotCount = 0;
  183. //We also have a transition in our fire state, as: GunStateMachine.addTransition("FireState", "burstShotCount", "DoneShooting", 3);
  184. //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.
  185. //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
  186. //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
  187. //like this:
  188. //GunStateMachine.addTransition("IdleState", "Fidget", "Timeout", ">=", 5000);
  189. //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
  190. //to our fidget state
  191. //so, lets check our current transitions
  192. //now that we have the type, check our transitions!
  193. for (U32 t = 0; t < mCurrentState.mTransitions.size(); t++)
  194. {
  195. //if (!dStrcmp(mCurrentState.mTransitions[t]., slotName))
  196. {
  197. //found a transition looking for this variable, so do work
  198. //first, figure out what data type thie field is
  199. //S32 type = getVariableType(newValue);
  200. bool fail = false;
  201. bool match = false;
  202. S32 ruleCount = mCurrentState.mTransitions[t].mTransitionRules.size();
  203. for (U32 r = 0; r < ruleCount; r++)
  204. {
  205. const char* fieldName = mCurrentState.mTransitions[t].mTransitionRules[r].field.name;
  206. if (!dStrcmp(fieldName, slotName))
  207. {
  208. match = true;
  209. //now, check the value with the comparitor and see if we do the transition.
  210. if (!passComparitorCheck(newValue, mCurrentState.mTransitions[t].mTransitionRules[r]))
  211. {
  212. fail = true;
  213. break;
  214. }
  215. }
  216. }
  217. //If we do have a transition rule for this field, and we didn't fail on the condition, go ahead and switch states
  218. if (match && !fail)
  219. {
  220. setState(mCurrentState.mTransitions[t].mStateTarget);
  221. return;
  222. }
  223. }
  224. }
  225. }
  226. bool StateMachine::passComparitorCheck(const char* var, StateTransition::Condition transitionRule)
  227. {
  228. F32 num = dAtof(var);
  229. switch (transitionRule.field.fieldType)
  230. {
  231. case StateField::Type::VectorType:
  232. switch (transitionRule.triggerComparitor)
  233. {
  234. case StateTransition::Condition::Equals:
  235. case StateTransition::Condition::GeaterThan:
  236. case StateTransition::Condition::GreaterOrEqual:
  237. case StateTransition::Condition::LessThan:
  238. case StateTransition::Condition::LessOrEqual:
  239. case StateTransition::Condition::DoesNotEqual:
  240. //do
  241. break;
  242. default:
  243. return false;
  244. };
  245. case StateField::Type::StringType:
  246. switch (transitionRule.triggerComparitor)
  247. {
  248. case StateTransition::Condition::Equals:
  249. if (!dStrcmp(var, transitionRule.field.triggerStringVal))
  250. return true;
  251. else
  252. return false;
  253. case StateTransition::Condition::DoesNotEqual:
  254. if (dStrcmp(var, transitionRule.field.triggerStringVal))
  255. return true;
  256. else
  257. return false;
  258. default:
  259. return false;
  260. };
  261. case StateField::Type::BooleanType:
  262. switch (transitionRule.triggerComparitor)
  263. {
  264. case StateTransition::Condition::TriggerValueTarget::True:
  265. if (dAtob(var))
  266. return true;
  267. else
  268. return false;
  269. case StateTransition::Condition::TriggerValueTarget::False:
  270. if (dAtob(var))
  271. return false;
  272. else
  273. return true;
  274. default:
  275. return false;
  276. };
  277. case StateField::Type::NumberType:
  278. switch (transitionRule.triggerComparitor)
  279. {
  280. case StateTransition::Condition::TriggerValueTarget::Equals:
  281. if (num == transitionRule.field.triggerNumVal)
  282. return true;
  283. else
  284. return false;
  285. case StateTransition::Condition::TriggerValueTarget::GeaterThan:
  286. if (num > transitionRule.field.triggerNumVal)
  287. return true;
  288. else
  289. return false;
  290. case StateTransition::Condition::TriggerValueTarget::GreaterOrEqual:
  291. if (num >= transitionRule.field.triggerNumVal)
  292. return true;
  293. else
  294. return false;
  295. case StateTransition::Condition::TriggerValueTarget::LessThan:
  296. if (num < transitionRule.field.triggerNumVal)
  297. return true;
  298. else
  299. return false;
  300. case StateTransition::Condition::TriggerValueTarget::LessOrEqual:
  301. if (num <= transitionRule.field.triggerNumVal)
  302. return true;
  303. else
  304. return false;
  305. case StateTransition::Condition::TriggerValueTarget::DoesNotEqual:
  306. if (num != transitionRule.field.triggerNumVal)
  307. return true;
  308. else
  309. return false;
  310. case StateTransition::Condition::TriggerValueTarget::Positive:
  311. if (num > 0)
  312. return true;
  313. else
  314. return false;
  315. case StateTransition::Condition::TriggerValueTarget::Negative:
  316. if (num < 0)
  317. return true;
  318. else
  319. return false;
  320. default:
  321. return false;
  322. };
  323. default:
  324. return false;
  325. };
  326. }
  327. void StateMachine::setState(const char* stateName, bool clearFields)
  328. {
  329. State oldState = mCurrentState;
  330. StringTableEntry sName = StringTable->insert(stateName);
  331. for (U32 i = 0; i < mStates.size(); i++)
  332. {
  333. //if(!dStrcmp(mStates[i]->stateName, stateName))
  334. if (!dStrcmp(mStates[i].stateName,sName))
  335. {
  336. mCurrentState = mStates[i];
  337. mStateStartTime = Sim::getCurrentTime();
  338. onStateChanged.trigger(this, i);
  339. return;
  340. }
  341. }
  342. }
  343. const char* StateMachine::getStateByIndex(S32 index)
  344. {
  345. if (index >= 0 && mStates.size() > index)
  346. return mStates[index].stateName;
  347. else
  348. return "";
  349. }
  350. StateMachine::State& StateMachine::getStateByName(const char* name)
  351. {
  352. StringTableEntry stateName = StringTable->insert(name);
  353. for (U32 i = 0; i < mStates.size(); i++)
  354. {
  355. if (!dStrcmp(stateName, mStates[i].stateName))
  356. return mStates[i];
  357. }
  358. }
  359. S32 StateMachine::findFieldByName(const char* name)
  360. {
  361. for (U32 i = 0; i < mFields.size(); i++)
  362. {
  363. if (!dStrcmp(mFields[i].name, name))
  364. return i;
  365. }
  366. return -1;
  367. }