botguards.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**@class BotGuardBase
  2. * @brief represents guard on a transition from state to state
  3. **/
  4. class BotGuardBase
  5. {
  6. /**@fn GuardCondition
  7. * @brief enable or disable transition based on condition
  8. * the guard is a boolean operation executed first and which can prevent the transition from firing by returning false
  9. * @return true if transition is allowed
  10. **/
  11. bool GuardCondition (BotEventBase e) { return true; }
  12. };
  13. class BotGuardAnd extends BotGuardBase
  14. {
  15. ref BotGuardBase m_arg0;
  16. ref BotGuardBase m_arg1;
  17. void BotGuardAnd (BotGuardBase arg0 = NULL, BotGuardBase arg1 = NULL) { m_arg0 = arg0; m_arg1 = arg1; }
  18. override bool GuardCondition (BotEventBase e)
  19. {
  20. bool result = m_arg0.GuardCondition(e) && m_arg1.GuardCondition(e);
  21. botDebugPrint("[botfsm] guard - " + m_arg0.Type() + " && " + m_arg1.Type() + " = " + result);
  22. return result;
  23. }
  24. };
  25. class BotGuardNot extends BotGuardBase
  26. {
  27. ref BotGuardBase m_arg0;
  28. void BotGuardNot (BotGuardBase arg0 = NULL) { m_arg0 = arg0; }
  29. override bool GuardCondition (BotEventBase e)
  30. {
  31. bool result = !m_arg0.GuardCondition(e);
  32. botDebugPrint("[botfsm] guard - ! " + m_arg0.Type() + " = " + result);
  33. return result;
  34. }
  35. };
  36. class BotGuardOr extends BotGuardBase
  37. {
  38. ref BotGuardBase m_arg0;
  39. ref BotGuardBase m_arg1;
  40. void BotGuardOr (BotGuardBase arg0 = NULL, BotGuardBase arg1 = NULL) { m_arg0 = arg0; m_arg1 = arg1; }
  41. override bool GuardCondition (BotEventBase e)
  42. {
  43. bool result = m_arg0.GuardCondition(e) || m_arg1.GuardCondition(e);
  44. botDebugPrint("[botfsm] guard - " + m_arg0.Type() + " || " + m_arg1.Type() + " = " + result);
  45. return result;
  46. }
  47. };
  48. class BotGuardHasItemInHands extends HandGuardBase
  49. {
  50. protected Man m_Player;
  51. void BotGuardHasItemInHands (Man p = NULL) { m_Player = p; }
  52. override bool GuardCondition (HandEventBase e)
  53. {
  54. if (m_Player.GetHumanInventory().GetEntityInHands())
  55. {
  56. if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[botfsm] guard - has valid entity in hands");
  57. return true;
  58. }
  59. if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[botfsm] guard - no entity in hands");
  60. return false;
  61. }
  62. };
  63. class BotGuardDebugEventMatches extends BotGuardBase
  64. {
  65. protected ref BotEventStartDebug m_Event;
  66. void BotGuardDebugEventMatches(BotEventStartDebug e = NULL) { m_Event = e; }
  67. override bool GuardCondition(BotEventBase e)
  68. {
  69. BotEventStartDebug other;
  70. if (!Class.CastTo(other, e))
  71. {
  72. return false;
  73. }
  74. return other.m_Id == m_Event.m_Id;
  75. }
  76. };