Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
bot_hunt.c
Go to the documentation of this file.
1 
3 {
4  //EntityAI m_Entity;
5  //void BotEventEntityAttached (PlayerBase p = NULL, EntityAI att = NULL) { m_Entity = att; }
6 };
7 
9 {
10  //EntityAI m_Entity;
11  //void BotEventEntityDetached (PlayerBase p = NULL, EntityAI att = NULL) { m_Entity = att; }
12 };
13 
14 
15 class BotHunt extends BotStateBase
16 {
17  Man m_Target;
18  float m_dtAccumulator = 0.0;
19 
20  ref BotHunt_Tracking m_Tracking;
21  ref BotHunt_Hunting m_Hunting;
22 
23  void BotHunt (Bot bot = NULL, BotStateBase parent = NULL)
24  {
25  // setup nested state machine
26  m_FSM = new BotFSM(this); // @NOTE: set owner of the submachine fsm
27 
28  m_Tracking = new BotHunt_Tracking(m_Bot, this);
29  m_Hunting = new BotHunt_Hunting(m_Bot, this);
30 
31  // events
32  BotEventBase __InSight__ = new BotEventHuntedTargetInSight;
34 
35  // transitions
36  m_FSM.AddTransition(new BotTransition( m_Tracking, __InSight__, m_Hunting));
37  m_FSM.AddTransition(new BotTransition( m_Hunting, __Lost__ , m_Tracking));
38 
39  m_FSM.SetInitialState(m_Tracking);
40  }
41 
42  void SelectTarget ()
43  {
44  m_Target = BotSelectNearestTarget(GetPlayerOwner());
45  m_Tracking.m_Target = m_Target;
46  m_Hunting.m_Target = m_Target;
47  botDebugPrint("[bot] + " + m_Owner + " hunt SelectTarget target=" + m_Target);
48  }
49 
50  override void OnEntry (BotEventBase e)
51  {
52  m_dtAccumulator = 0.0;
53  SelectTarget();
54 
55  super.OnEntry(e);
56  }
57 
58  override void OnExit (BotEventBase e)
59  {
60  m_dtAccumulator = 0.0;
61  m_Target = null;
62 
63  super.OnExit(e);
64  }
65 
66  override void OnUpdate (float dt)
67  {
68  super.OnUpdate(dt);
69 
70  m_dtAccumulator += dt;
71 
72  /*float rescanTime = 3.0;
73  if (m_dtAccumulator >= rescanTime)
74  if (m_weapon.CanProcessWeaponEvents())
75  m_Bot.ProcessEvent(new WeaponEventReloadTimeout(p));*/
76 
77  if (m_Target == null)
78  {
79  int acc = m_dtAccumulator;
80  if (acc % 5 == 0)
81  {
82  Print("Searching...");
83  SelectTarget();
84  }
85  }
86  }
87 };
88 
89 class BotHunt_Tracking extends BotStateBase
90 {
92  bool m_TargetInSight = false;
93  bool m_TargetLost = false;
94  bool m_Tracking = true;
95 
96  override void OnEntry (BotEventBase e)
97  {
98  super.OnEntry(e);
99 
100  m_TargetLost = false;
101  m_TargetInSight = false;
102  m_Tracking = false;
103  }
104 
105  override void OnAbort (BotEventBase e)
106  {
107  m_TargetLost = false;
108  m_TargetInSight = false;
109  m_Tracking = false;
110 
111  super.OnAbort(e);
112  }
113 
114  override void OnExit (BotEventBase e)
115  {
116  m_TargetLost = false;
117  m_TargetInSight = false;
118  m_Tracking = false;
119 
120  super.OnExit(e);
121  }
122 
123  override void OnUpdate (float dt)
124  {
125  if (m_Target)
126  {
127  m_Tracking = true;
128  vector targetPos = m_Target.GetPosition();
129  botDebugPrint("[bot] + " + m_Owner + " hunt Tracking target=" + m_Target + " pos=" + targetPos);
130 
131  // tmp dist check
132  float d = vector.Distance(m_Target.GetPosition(), GetPlayerOwner().GetPosition());
133  if (d < 2.0)
134  {
135  m_TargetInSight = true;
136  }
137  else
138  {
139  m_TargetInSight = false;
140  }
141 
142  if (!m_TargetInSight)
143  {
144  GetPlayerOwner().GetInputController().OverrideMovementSpeed(true, 1);
145  GetPlayerOwner().GetInputController().OverrideMovementAngle(true, 1);
146  }
147  else
148  {
149  GetPlayerOwner().GetInputController().OverrideMovementSpeed(false, 0);
150  GetPlayerOwner().GetInputController().OverrideMovementAngle(false, 0);
151  }
152 
153  /*if ((.GetInputController().LimitsIsSprintDisabled()))
154  .GetInputController().OverrideMovementSpeed( true, 2 );
155  else
156  .GetInputController().OverrideMovementSpeed( true, 3 );*/
157 
158  }
159  else
160  {
161  if (m_Tracking)
162  {
163  m_TargetLost = true;
164  m_TargetInSight = false;
165  m_Tracking = false;
166 
167  GetPlayerOwner().GetInputController().OverrideMovementSpeed(false, 0);
168  GetPlayerOwner().GetInputController().OverrideMovementAngle(false, 0);
169  }
170  }
171  }
172 };
173 
174 class BotHunt_Hunting extends BotStateBase
175 {
177 
178  override void OnEntry (BotEventBase e)
179  {
180  super.OnEntry(e);
181  }
182 
183  override void OnAbort (BotEventBase e) { super.OnAbort(e); }
184 
185  override void OnExit (BotEventBase e)
186  {
187  super.OnExit(e);
188  }
189 
190  override void OnUpdate (float dt)
191  {
192  }
193 };
194 
196 {
197  /*ref array<Man> players = new array<Man>;
198  g_Game.GetWorld().GetPlayerList( players );
199 
200  bool minimal_distance_ok = true;
201 
202  float min_dist = 1234567.0;
203  int min_index = -1;
204  for ( int i = 0; i < players.Count(); i++ )
205  {
206  float d = vector.Distance(players.Get(i).GetPosition(), bot.GetPosition());
207  if ( d < min_dist )
208  {
209  min_dist = d;
210  min_index = i;
211  }
212  }
213 
214  if (min_index != -1)
215  return players.Get(min_index);
216 
217  return null;*/
218  vector pos = bot.GetPosition();
219  //vector dir = player.GetDirection();
220 
221  array<Object> objects = new array<Object>;
222  array<CargoBase> proxyCargos = new array<CargoBase>;
223  GetGame().GetObjectsAtPosition(pos, 100.0, objects, proxyCargos);
224 
225  float min_dist = 1234567.0;
226  int min_index = -1;
227  int c = objects.Count();
228  for (int i = 0; i < c; i++)
229  {
230  Object o = objects[i];
231  if (o == bot)
232  continue;
233 
234  float d = vector.Distance(o.GetPosition(), bot.GetPosition());
235  if ( d < min_dist )
236  {
237  min_dist = d;
238  min_index = i;
239  }
240  }
241 
242  if (min_index != -1)
243  {
244  botDebugPrint("[bot] + " + bot + " BotSelectNearestTarget idx=" + min_index + " dist=" + min_dist + " obj=" + o);
245  return Man.Cast( objects.Get(min_index) );
246  }
247  return null;
248 }
249 
250 
GetGame
proto native CGame GetGame()
m_Target
ref ActionTarget m_Target
Definition: actionbase.c:17
OnAbort
override void OnAbort()
Definition: remotedetonator.c:300
OnExit
override void OnExit(HandEventBase e)
Definition: hand_states.c:28
Bot
Definition: bot.c:18
Print
proto void Print(void var)
Prints content of variable to console/log.
BotStateBase
represent weapon state base
Definition: bot_hunt.c:15
m_dtAccumulator
class WeaponFireWithEject extends WeaponFire m_dtAccumulator
botDebugPrint
void botDebugPrint(string s)
Definition: bot.c:182
BotEventHuntedTargetInSight
Definition: bot_hunt.c:2
GetPosition
class JsonUndergroundAreaTriggerData GetPosition
Definition: undergroundarealoader.c:9
BotEventBase
represents event that triggers transition from state to state
Definition: botevents.c:4
vector
Definition: enconvert.c:105
BotTransition
FSMTransition< BotStateBase, BotEventBase, BotActionBase, BotGuardBase > BotTransition
Definition: botfsm.c:7
BotEventHuntedTargetLost
Definition: bot_hunt.c:8
OnUpdate
proto native void OnUpdate()
Definition: tools.c:349
Object
Definition: objecttyped.c:1
m_Owner
enum ProcessDirectDamageFlags m_Owner
array< Object >
BotSelectNearestTarget
Man BotSelectNearestTarget(EntityAI bot)
Definition: bot_hunt.c:195
BotFSM
Bot Finite State Machine (Hierarchical)
EntityAI
Definition: building.c:5
OnEntry
HandStateEquipped OnEntry
Definition: weaponchambering.c:208