Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
bot.c
Go to the documentation of this file.
1 
3 class BotTrigger
4 {
5  bool CheckTrigger () { return false; }
6 };
7 
9 {
10  PlayerBase m_Owner;
11  void MyBotTrigger (PlayerBase p) { m_Owner = p; }
12  override bool CheckTrigger () { return null != m_Owner.GetInventory().FindAttachment(InventorySlots.GetSlotIdFromString("Headgear")); }
13 };
14 
15 
18 class Bot
19 {
20  PlayerBase m_Owner = null;
21  protected ref Timer m_Timer = new Timer;
22  protected const float c_TriggerTimeoutMS = 1000.0;
23  //protected const float c_UpdateMS = 2000.0;
24  protected const float c_UpdateMS = 16.0;
25  protected ref BotFSM m_FSM = null;
26  protected bool m_UseTrigger = false;
27  protected bool m_Triggered = false;
28  protected DayZPlayerInstanceType m_InstanceType = DayZPlayerInstanceType.INSTANCETYPE_CLIENT;
29  protected ref BotTrigger m_BotTrigger = null;
30  protected ref BotStateBase m_BotTest = null;
31 
32  void Bot (PlayerBase ow)
33  {
34  m_Owner = ow;
35  }
36 
37  void SetInstanceType (DayZPlayerInstanceType t) { m_InstanceType = t; }
38 
39  void Start (bool use_trigger, BotTrigger trigger = null)
40  {
41  m_UseTrigger = use_trigger;
42 
43  InitFSM();
44 
45  if (m_UseTrigger)
46  {
47  if (m_UseTrigger && trigger == null)
48  Error("use trigger, but trigger null");
49 
50  m_Triggered = false;
51  m_BotTrigger = trigger;
52  botDebugPrint("[bot] + " + m_Owner + " Bot waiting for trigger...");
53  m_Timer.Run(c_TriggerTimeoutMS / 1000.0, this, "OnTrigger", null, true);
54  }
55  else
56  {
57  botDebugPrint("[bot] + " + m_Owner + " Bot Started.");
58  m_Timer.Run(c_UpdateMS / 1000.0, this, "OnTimer", null, true);
59  }
60  }
61 
62  void DelayedStart (float ms)
63  {
64  m_Timer.Run(ms / 1000.0, this, "OnDelayedStart", null, false);
65  }
66 
67  protected void OnDelayedStart ()
68  {
69  Start(false, null);
70 
71  ProcessEvent(new BotEventStart(m_Owner));
72  m_UseTrigger = false;
73  }
74 
75  void Stop ()
76  {
77  m_Triggered = false;
78  m_Timer.Stop();
79  m_FSM.Terminate();
80  }
81 
82  protected void OnTrigger ()
83  {
84  bool triggered = m_BotTrigger.CheckTrigger();
85 
86  if (!m_Triggered)
87  {
88  if (triggered)
89  {
90  m_Timer.Stop(); // stop trigger timer
91  m_Triggered = true;
92  ProcessEvent(new BotEventStart(m_Owner));
93  m_Timer.Run(c_UpdateMS / 1000.0, this, "OnUpdate", null, true);
94 
95  botDebugPrint("[bot] + " + m_Owner + " Started test!");
96  }
97  }
98  else
99  {
100  if (!triggered)
101  {
102  m_Timer.Stop(); // stop update timer
103  m_Triggered = false;
104  ProcessEvent(new BotEventStop(m_Owner));
105  m_Timer.Run(c_TriggerTimeoutMS / 1000.0, this, "OnTrigger", null, true);
106 
107  botDebugPrint("[bot] + " + m_Owner + " Stopped test!");
108  }
109  }
110  }
111 
112  void OnUpdate (float dt)
113  {
114  m_FSM.GetCurrentState().OnUpdate(dt);
115 
116  OnTrigger(); // to detect trigger stop
117  }
118 
119  void OnTimer ()
120  {
121  //m_FSM.GetCurrentState().OnUpdate(c_UpdateMS / 1000.0);
122 
123  //OnTrigger(); // to detect trigger stop
124  }
125 
126  void InitFSM ()
127  {
128  m_FSM = new BotFSM();
129 
130  // basic states
131  BotStateBase BotIdle = new BotStateIdle(this, NULL);
132  // unstable (intermediate) states
133  m_BotTest = new BotTestSpamUserActions(this, NULL);
134  //m_BotTest = new BotTestAttachAndDropCycle(this, NULL);
135  //m_BotTest = new BotTestItemMoveBackAndForth(this, NULL);
136  //m_BotTest = new Bot_TestSpawnOpen(this, NULL);
137  //m_BotTest = new Bot_TestSpawnOpenDestroy(this, NULL);
138  //m_BotTest = new Bot_TestSpawnOpenEat(this, NULL);
139  //m_BotTest = new BotTestSwapG2H(this, NULL);
140  //m_BotTest = new BotTestSwapC2H(this, NULL);
141  //m_BotTest = new BotTestSwapInternal(this, NULL);
142 
143  // events
144  BotEventBase ___Bgn__ = new BotEventStart;
145  BotEventBase __Stop__ = new BotEventStop;
146  BotEventBase ___OK___ = new BotEventEndOK;
147  BotEventBase __Fail__ = new BotEventEndFail;
148  BotEventBase __Tout__ = new BotEventEndTimeout;
150 
152  m_FSM.AddTransition(new BotTransition( BotIdle , ___Bgn__, m_BotTest));
153  m_FSM.AddTransition(new BotTransition( BotIdle , __Stop__, NULL));
154 
155  // causes restart of FSM
156  //m_FSM.AddTransition(new BotTransition(m_BotTest , __IChg__, m_BotTest));
157 
158  //m_FSM.AddTransition(new BotTransition(m_BotTest , ___OK___, BotIdle));
159  m_FSM.AddTransition(new BotTransition(m_BotTest , __Fail__, BotIdle));
160  m_FSM.AddTransition(new BotTransition(m_BotTest , __Tout__, BotIdle));
162 
163  m_FSM.SetInitialState(BotIdle);
164  m_FSM.Start();
165  }
166 
167  bool ProcessEvent (BotEventBase e)
168  {
169  if (m_FSM.ProcessEvent(e) == ProcessEventResult.FSM_OK)
170  {
171  botDebugSpam("[botfsm] Processed event e=" + e.ToString());
172  return true;
173  }
174  else
175  {
176  botDebugSpam("[botfsm] FSM refused to process event (no transition): src=" + m_FSM.GetCurrentState().ToString() + " event=" + e.ToString());
177  return false;
178  }
179  }
180 };
181 
182 void botDebugPrint (string s)
183 {
184 #ifdef BOT_DEBUG
185  PrintToRPT("" + s); // comment/uncomment to hide/see debug logs
186 #else
187  //Print("" + s); // comment/uncomment to hide/see debug logs
188 #endif
189 }
190 
191 void botDebugSpam (string s)
192 {
193 #ifdef BOT_DEBUG_SPAM
194  PrintToRPT("" + s); // comment/uncomment to hide/see debug logs
195 #else
196  //Print("" + s); // comment/uncomment to hide/see debug logs
197 #endif
198 }
199 
Error
void Error(string err)
Messagebox with error message.
Definition: endebug.c:90
InventorySlots
provides access to slot configuration
Definition: inventoryslots.c:5
BotTestSpamUserActions
Definition: bot_testspamuseractions.c:147
m_Timer
ref Timer m_Timer
Definition: dayzgame.c:690
Bot
Definition: bot.c:18
BotEventEndOK
Definition: botevents.c:19
BotEventEndFail
Definition: botevents.c:20
BotEventOnItemInHandsChanged
Definition: botevents.c:22
BotStateBase
represent weapon state base
Definition: bot_hunt.c:15
MyBotTrigger
Definition: bot.c:8
botDebugSpam
void botDebugSpam(string s)
Definition: bot.c:191
botDebugPrint
void botDebugPrint(string s)
Definition: bot.c:182
BotEventBase
represents event that triggers transition from state to state
Definition: botevents.c:4
PlayerBase
Definition: playerbaseclient.c:1
BotEventEndTimeout
Definition: botevents.c:21
BotTransition
FSMTransition< BotStateBase, BotEventBase, BotActionBase, BotGuardBase > BotTransition
Definition: botfsm.c:7
BotEventStart
Definition: botevents.c:16
PrintToRPT
proto void PrintToRPT(void var)
Prints content of variable to RPT file (performance warning - each write means fflush!...
BotEventStop
Definition: botevents.c:17
m_Owner
enum ProcessDirectDamageFlags m_Owner
BotTrigger
Definition: bot.c:3
ProcessEventResult
ProcessEventResult
Definition: fsmbase.c:40
Timer
Definition: dayzplayerimplement.c:62
BotFSM
Bot Finite State Machine (Hierarchical)
BotStateIdle
Definition: botstates.c:129
DayZPlayerInstanceType
DayZPlayerInstanceType
defined in C++
Definition: dayzplayer.c:1058