bot.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**@class BotTrigger
  2. **/
  3. class BotTrigger
  4. {
  5. bool CheckTrigger () { return false; }
  6. };
  7. class MyBotTrigger : BotTrigger
  8. {
  9. PlayerBase m_Owner;
  10. void MyBotTrigger (PlayerBase p) { m_Owner = p; }
  11. override bool CheckTrigger () { return null != m_Owner.GetInventory().FindAttachment(InventorySlots.GetSlotIdFromString("Headgear")); }
  12. };
  13. /**@class Bot
  14. **/
  15. class Bot
  16. {
  17. PlayerBase m_Owner = null;
  18. protected ref BotFSM m_FSM = null;
  19. protected DayZPlayerInstanceType m_InstanceType = DayZPlayerInstanceType.INSTANCETYPE_CLIENT;
  20. protected ref BotStateBase m_IdleState = null;
  21. void Bot (PlayerBase ow)
  22. {
  23. m_Owner = ow;
  24. }
  25. void SetInstanceType (DayZPlayerInstanceType t) { m_InstanceType = t; }
  26. void Start ()
  27. {
  28. InitFSM();
  29. }
  30. void Stop ()
  31. {
  32. m_FSM.Terminate();
  33. }
  34. void StartAction(int action)
  35. {
  36. switch (action)
  37. {
  38. case EActions.PLAYER_BOT_STOP_CURRENT:
  39. ProcessEvent(new BotEventStop(m_Owner, null));
  40. return;
  41. }
  42. ProcessEvent(new BotEventStartDebug(m_Owner, null, action));
  43. }
  44. void OnUpdate (float dt)
  45. {
  46. m_FSM.GetCurrentState().OnUpdate(dt);
  47. }
  48. // events
  49. ref BotEventBase ___Bgn__ = new BotEventStart;
  50. ref BotEventBase __Stop__ = new BotEventStop;
  51. ref BotEventBase ___OK___ = new BotEventEndOK;
  52. ref BotEventBase __Fail__ = new BotEventEndFail;
  53. ref BotEventBase __Tout__ = new BotEventEndTimeout;
  54. ref BotEventBase __IChg__ = new BotEventOnItemInHandsChanged;
  55. void AddTransition(BotStateBase state, int id)
  56. {
  57. BotEventStartDebug evt = new BotEventStartDebug(null, null, id);
  58. m_FSM.AddTransition(new BotTransition(m_IdleState, evt, state, null, new BotGuardDebugEventMatches(evt)));
  59. m_FSM.AddTransition(new BotTransition(state, ___OK___, m_IdleState));
  60. m_FSM.AddTransition(new BotTransition(state, __Fail__, m_IdleState));
  61. m_FSM.AddTransition(new BotTransition(state, __Tout__, m_IdleState));
  62. m_FSM.AddTransition(new BotTransition(state, __Stop__, m_IdleState));
  63. }
  64. void InitFSM ()
  65. {
  66. m_FSM = new BotFSM();
  67. // basic states
  68. m_IdleState = new BotStateIdle(this, NULL);
  69. ///@{ transition table
  70. AddTransition(new BotStanceRandomizer(this, NULL), EActions.PLAYER_BOT_RANDOMIZE_STANCE);
  71. AddTransition(new BotTestSpamUserActions(this, NULL), EActions.PLAYER_BOT_SPAM_USER_ACTIONS);
  72. AddTransition(new BotTestAttachAndDropCycle(this, NULL), EActions.PLAYER_BOT_TEST_ATTACH_AND_DROP_CYCLE);
  73. AddTransition(new BotTestItemMoveBackAndForth(this, NULL), EActions.PLAYER_BOT_TEST_ITEM_MOVE_BACK_AND_FORTH);
  74. AddTransition(new Bot_TestSpawnOpen(this, NULL), EActions.PLAYER_BOT_TEST_SPAWN_OPEN);
  75. AddTransition(new Bot_TestSpawnOpenDestroy(this, NULL), EActions.PLAYER_BOT_TEST_SPAWN_OPEN_DESTROY);
  76. AddTransition(new Bot_TestSpawnOpenEat(this, NULL), EActions.PLAYER_BOT_TEST_SPAWN_OPEN_EAT);
  77. AddTransition(new BotTestSwapG2H(this, NULL), EActions.PLAYER_BOT_TEST_SWAP_G2H);
  78. //AddTransition(new BotTestSwapC2H(this, NULL), EActions.PLAYER_BOT_TEST_SWAP_C2H);
  79. AddTransition(new BotTestSwapInternal(this, NULL), EActions.PLAYER_BOT_TEST_SWAP_INTERNAL);
  80. ///@} transition table
  81. m_FSM.SetInitialState(m_IdleState);
  82. m_FSM.Start();
  83. }
  84. bool ProcessEvent (BotEventBase e)
  85. {
  86. if (m_FSM.ProcessEvent(e) == ProcessEventResult.FSM_OK)
  87. {
  88. botDebugSpam("[botfsm] Processed event e=" + e.ToString());
  89. return true;
  90. }
  91. else
  92. {
  93. botDebugSpam("[botfsm] FSM refused to process event (no transition): src=" + m_FSM.GetCurrentState().ToString() + " event=" + e.ToString());
  94. return false;
  95. }
  96. }
  97. };
  98. void botDebugPrint (string s)
  99. {
  100. #ifdef BOT_DEBUG
  101. PrintToRPT("" + s); // comment/uncomment to hide/see debug logs
  102. #else
  103. //Print("" + s); // comment/uncomment to hide/see debug logs
  104. #endif
  105. }
  106. void botDebugSpam (string s)
  107. {
  108. #ifdef BOT_DEBUG_SPAM
  109. PrintToRPT("" + s); // comment/uncomment to hide/see debug logs
  110. #else
  111. //Print("" + s); // comment/uncomment to hide/see debug logs
  112. #endif
  113. }