bot_hunt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. class BotEventHuntedTargetInSight : BotEventBase
  2. {
  3. //EntityAI m_Entity;
  4. //void BotEventEntityAttached (PlayerBase p = NULL, EntityAI att = NULL) { m_Entity = att; }
  5. };
  6. class BotEventHuntedTargetLost : BotEventBase
  7. {
  8. //EntityAI m_Entity;
  9. //void BotEventEntityDetached (PlayerBase p = NULL, EntityAI att = NULL) { m_Entity = att; }
  10. };
  11. class BotHunt extends BotStateBase
  12. {
  13. Man m_Target;
  14. float m_dtAccumulator = 0.0;
  15. ref BotHunt_Tracking m_Tracking;
  16. ref BotHunt_Hunting m_Hunting;
  17. void BotHunt (Bot bot = NULL, BotStateBase parent = NULL)
  18. {
  19. // setup nested state machine
  20. m_FSM = new BotFSM(this); // @NOTE: set owner of the submachine fsm
  21. m_Tracking = new BotHunt_Tracking(m_Bot, this);
  22. m_Hunting = new BotHunt_Hunting(m_Bot, this);
  23. // events
  24. BotEventBase __InSight__ = new BotEventHuntedTargetInSight;
  25. BotEventBase __Lost__ = new BotEventHuntedTargetLost;
  26. // transitions
  27. m_FSM.AddTransition(new BotTransition( m_Tracking, __InSight__, m_Hunting));
  28. m_FSM.AddTransition(new BotTransition( m_Hunting, __Lost__ , m_Tracking));
  29. m_FSM.SetInitialState(m_Tracking);
  30. }
  31. void SelectTarget ()
  32. {
  33. m_Target = BotSelectNearestTarget(GetPlayerOwner());
  34. m_Tracking.m_Target = m_Target;
  35. m_Hunting.m_Target = m_Target;
  36. botDebugPrint("[bot] + " + m_Owner + " hunt SelectTarget target=" + m_Target);
  37. }
  38. override void OnEntry (BotEventBase e)
  39. {
  40. m_dtAccumulator = 0.0;
  41. SelectTarget();
  42. super.OnEntry(e);
  43. }
  44. override void OnExit (BotEventBase e)
  45. {
  46. m_dtAccumulator = 0.0;
  47. m_Target = null;
  48. super.OnExit(e);
  49. }
  50. override void OnUpdate (float dt)
  51. {
  52. super.OnUpdate(dt);
  53. m_dtAccumulator += dt;
  54. /*float rescanTime = 3.0;
  55. if (m_dtAccumulator >= rescanTime)
  56. if (m_weapon.CanProcessWeaponEvents())
  57. m_Bot.ProcessEvent(new WeaponEventReloadTimeout(p));*/
  58. if (m_Target == null)
  59. {
  60. int acc = m_dtAccumulator;
  61. if (acc % 5 == 0)
  62. {
  63. Print("Searching...");
  64. SelectTarget();
  65. }
  66. }
  67. }
  68. };
  69. class BotHunt_Tracking extends BotStateBase
  70. {
  71. EntityAI m_Target;
  72. bool m_TargetInSight = false;
  73. bool m_TargetLost = false;
  74. bool m_Tracking = true;
  75. override void OnEntry (BotEventBase e)
  76. {
  77. super.OnEntry(e);
  78. m_TargetLost = false;
  79. m_TargetInSight = false;
  80. m_Tracking = false;
  81. }
  82. override void OnAbort (BotEventBase e)
  83. {
  84. m_TargetLost = false;
  85. m_TargetInSight = false;
  86. m_Tracking = false;
  87. super.OnAbort(e);
  88. }
  89. override void OnExit (BotEventBase e)
  90. {
  91. m_TargetLost = false;
  92. m_TargetInSight = false;
  93. m_Tracking = false;
  94. super.OnExit(e);
  95. }
  96. override void OnUpdate (float dt)
  97. {
  98. if (m_Target)
  99. {
  100. m_Tracking = true;
  101. vector targetPos = m_Target.GetPosition();
  102. botDebugPrint("[bot] + " + m_Owner + " hunt Tracking target=" + m_Target + " pos=" + targetPos);
  103. // tmp dist check
  104. float d = vector.Distance(m_Target.GetPosition(), GetPlayerOwner().GetPosition());
  105. if (d < 2.0)
  106. {
  107. m_TargetInSight = true;
  108. }
  109. else
  110. {
  111. m_TargetInSight = false;
  112. }
  113. if (!m_TargetInSight)
  114. {
  115. GetPlayerOwner().GetInputController().OverrideMovementSpeed(true, 1);
  116. GetPlayerOwner().GetInputController().OverrideMovementAngle(true, 1);
  117. }
  118. else
  119. {
  120. GetPlayerOwner().GetInputController().OverrideMovementSpeed(false, 0);
  121. GetPlayerOwner().GetInputController().OverrideMovementAngle(false, 0);
  122. }
  123. /*if ((.GetInputController().LimitsIsSprintDisabled()))
  124. .GetInputController().OverrideMovementSpeed( true, 2 );
  125. else
  126. .GetInputController().OverrideMovementSpeed( true, 3 );*/
  127. }
  128. else
  129. {
  130. if (m_Tracking)
  131. {
  132. m_TargetLost = true;
  133. m_TargetInSight = false;
  134. m_Tracking = false;
  135. GetPlayerOwner().GetInputController().OverrideMovementSpeed(false, 0);
  136. GetPlayerOwner().GetInputController().OverrideMovementAngle(false, 0);
  137. }
  138. }
  139. }
  140. };
  141. class BotHunt_Hunting extends BotStateBase
  142. {
  143. EntityAI m_Target;
  144. override void OnEntry (BotEventBase e)
  145. {
  146. super.OnEntry(e);
  147. }
  148. override void OnAbort (BotEventBase e) { super.OnAbort(e); }
  149. override void OnExit (BotEventBase e)
  150. {
  151. super.OnExit(e);
  152. }
  153. override void OnUpdate (float dt)
  154. {
  155. }
  156. };
  157. Man BotSelectNearestTarget (EntityAI bot)
  158. {
  159. /*ref array<Man> players = new array<Man>;
  160. g_Game.GetWorld().GetPlayerList( players );
  161. bool minimal_distance_ok = true;
  162. float min_dist = 1234567.0;
  163. int min_index = -1;
  164. for ( int i = 0; i < players.Count(); i++ )
  165. {
  166. float d = vector.Distance(players.Get(i).GetPosition(), bot.GetPosition());
  167. if ( d < min_dist )
  168. {
  169. min_dist = d;
  170. min_index = i;
  171. }
  172. }
  173. if (min_index != -1)
  174. return players.Get(min_index);
  175. return null;*/
  176. vector pos = bot.GetPosition();
  177. //vector dir = player.GetDirection();
  178. array<Object> objects = new array<Object>;
  179. array<CargoBase> proxyCargos = new array<CargoBase>;
  180. GetGame().GetObjectsAtPosition(pos, 100.0, objects, proxyCargos);
  181. float min_dist = 1234567.0;
  182. int min_index = -1;
  183. int c = objects.Count();
  184. for (int i = 0; i < c; i++)
  185. {
  186. Object o = objects[i];
  187. if (o == bot)
  188. continue;
  189. float d = vector.Distance(o.GetPosition(), bot.GetPosition());
  190. if ( d < min_dist )
  191. {
  192. min_dist = d;
  193. min_index = i;
  194. }
  195. }
  196. if (min_index != -1)
  197. {
  198. botDebugPrint("[bot] + " + bot + " BotSelectNearestTarget idx=" + min_index + " dist=" + min_dist + " obj=" + o);
  199. return Man.Cast( objects.Get(min_index) );
  200. }
  201. return null;
  202. }