stateMachine.cpp 14 KB

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