Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ResupplyTaskSolver.c
Go to the documentation of this file.
2{
5 protected Vehicle m_Vehicle;
11 protected SCR_LoadSuppliesWaypoint m_LoadSuppliesWaypoint;
12 protected SCR_UnloadSuppliesWaypoint m_UnloadSuppliesWaypoint;
13 protected SCR_ResupplyCampaignMilitaryBaseTaskEntity m_ResupplyTask;
15
20
21 //------------------------------------------------------------------------------------------------
22 override void SetAiGroup(SCR_AIGroup aiGroup)
23 {
24 super.SetAiGroup(aiGroup);
25
27 if (pilotVacancyCheck)
28 pilotVacancyCheck.SetIgnoredAiGroup(m_AiGroup);
29
31 m_TransportGroupUtility.GetOnMoveFailed().Remove(OnWaypointFailed);
32
34 m_TransportGroupUtility.GetOnMoveFailed().Insert(OnWaypointFailed);
35 }
36
37 //------------------------------------------------------------------------------------------------
43
44 //------------------------------------------------------------------------------------------------
49
50 //------------------------------------------------------------------------------------------------
55
56 //------------------------------------------------------------------------------------------------
64
65 //------------------------------------------------------------------------------------------------
70
71 //------------------------------------------------------------------------------------------------
77
78 //------------------------------------------------------------------------------------------------
80 {
81 if (state == m_eResupplySolverState)
82 return;
83
84 string prevState = SCR_Enum.GetEnumName(SCR_EResupplyTaskSolverState, m_eResupplySolverState);
85 string curState = SCR_Enum.GetEnumName(SCR_EResupplyTaskSolverState, state);
86
87 PrintFormat("SCR_ResupplyTaskSolver: Transport group %1 changed state from %2 to %3.", GetTransportGroupName(), prevState, curState, level: LogLevel.DEBUG);
88
90
93 }
94
95 //------------------------------------------------------------------------------------------------
97 override void Init(SCR_Task task, SCR_TaskSolverContext context)
98 {
99 super.Init(task, context);
100
101 m_ResupplyTask = SCR_ResupplyCampaignMilitaryBaseTaskEntity.Cast(task);
102 SCR_ResupplyTaskSolverContext resupplyTaskContext = SCR_ResupplyTaskSolverContext.Cast(context);
103 SetVehicle(resupplyTaskContext.GetVehicle());
104
105 if (!m_Vehicle)
106 return;
107
109
110 m_SourceBase = resupplyTaskContext.GetSourceBase();
111 }
112
113 //------------------------------------------------------------------------------------------------
115 override void StartTaskSolving()
116 {
117 super.StartTaskSolving();
118
119 if (!m_VehicleConditionManager.IsVehicleUsable(m_Vehicle))
120 {
122 return;
123 }
124
125 if (!m_AiGroupConditionManager.IsAIGroupAvailable(m_AiGroup))
126 {
128 return;
129 }
130
132
133 m_ePreviousGroupCombatMode = m_TransportGroupUtility.GetCombatModeExternal();
134 m_TransportGroupUtility.SetCombatMode(m_ResupplyTaskSolverEntry.GetGroupCombatMode());
135
136 if (m_AiVehicleUsage.IsOccupiedByGroup(m_AiGroup))
137 {
139 }
140 else
141 {
144 }
145 }
146
147 //------------------------------------------------------------------------------------------------
149 {
151 if (!boardingWaypoint)
152 {
154 return null;
155 }
156
157 boardingWaypoint.SetEntity(m_Vehicle);
160 m_AiGroup.AddWaypoint(boardingWaypoint);
161 m_BoardingWaypoint = boardingWaypoint;
162
163 return boardingWaypoint;
164 }
165
166 //------------------------------------------------------------------------------------------------
168 {
169 if (!m_SourceBase)
170 return null;
171
172 return CreateMoveWaypoint(SCR_EResupplyTaskSolverState.MOVING_TO_SOURCE_BASE, m_SourceBase.GetOwner().GetOrigin(), m_SourceBase.GetRadius());
173 }
174
175 //------------------------------------------------------------------------------------------------
177 {
178 if (m_SourceBase == sourceBase)
179 return;
180
181 m_SourceBase = sourceBase;
182 }
183
184 //------------------------------------------------------------------------------------------------
185 void SetVehicle(Vehicle vehicle)
186 {
187 if (m_Vehicle == vehicle)
188 return;
189
190 m_Vehicle = vehicle;
191 }
192
193 //------------------------------------------------------------------------------------------------
195 override void Update(float timeSlice)
196 {
198 return;
199
200 if (!m_SourceBase)
202
203 if (!m_VehicleConditionManager.IsVehicleUsable(m_Vehicle))
205
206 if (!m_AiGroupConditionManager.IsAIGroupAvailable(m_AiGroup))
208 }
209
210 //------------------------------------------------------------------------------------------------
213 {
214 if (m_AiGroup)
215 {
217 m_AiGroup.GetOnWaypointCompleted().Clear();
219 }
220
222
223 string errorStateName = SCR_Enum.GetEnumName(SCR_EResupplyTaskSolverErrorState, errorState);
224 PrintFormat("SCR_ResupplyTaskSolver: Group %1 failed task solving: %2.", GetTransportGroupName(), errorStateName, level: LogLevel.DEBUG);
225
226 if (m_Vehicle && m_Vehicle.IsOccupied() && errorState == SCR_EResupplyTaskSolverErrorState.VEHICLE_NOT_OPERABLE)
227 {
228 SCR_BoardingWaypoint getOutWaypoint = SpawnGetOutWaypoint();
229 m_AiGroup.AddWaypoint(getOutWaypoint);
230 }
231
233
234 super.FailTaskSolving();
235 }
236
237 //------------------------------------------------------------------------------------------------
239 override protected void CompleteTaskSolving()
240 {
241 if (m_AiGroup)
242 {
244 m_AiGroup.GetOnWaypointCompleted().Clear();
246 }
247
249
250 super.CompleteTaskSolving();
251 }
252
253 //------------------------------------------------------------------------------------------------
254 // Helper for debug or log output for the AI group
255 protected string GetTransportGroupName()
256 {
257 if (!m_AiGroup)
258 return string.Empty;
259
260 string company, platoon, squad, character, format;
261 m_AiGroup.GetCallsigns(company, platoon, squad, character, format);
262 return string.Format(format, company, platoon, squad, character);
263 }
264
265 //------------------------------------------------------------------------------------------------
266 protected void PreventMaxLOD(SCR_AIGroup aiGroup)
267 {
268 if (!aiGroup)
269 return;
270
271 array<AIAgent> aiAgents = {};
272 aiGroup.GetAgents(aiAgents);
273
274 int nextToLastLOD = AIAgent.GetMaxLOD() - 1;
275
276 foreach (AIAgent aiAgent : aiAgents)
277 {
278 if (aiAgent.GetLOD() == AIAgent.GetMaxLOD())
279 aiAgent.SetLOD(nextToLastLOD);
280
281 aiAgent.PreventMaxLOD();
282 }
283 }
284
285 //------------------------------------------------------------------------------------------------
286 protected void AllowMaxLOD(SCR_AIGroup aiGroup)
287 {
288 if (!aiGroup)
289 return;
290
291 array<AIAgent> aiAgents = {};
292 aiGroup.GetAgents(aiAgents);
293
294 foreach (AIAgent aiAgent : aiAgents)
295 {
296 aiAgent.AllowMaxLOD();
297 }
298 }
299
300 //------------------------------------------------------------------------------------------------
302 {
304 m_AiGroup.GetOnWaypointCompleted().Insert(OnBoardingCompleted);
305 }
306
307 //------------------------------------------------------------------------------------------------
309 {
311 m_AiGroup.GetOnWaypointCompleted().Remove(OnBoardingCompleted);
312 }
313
314 //------------------------------------------------------------------------------------------------
316 {
318 m_AiGroup.GetOnWaypointCompleted().Insert(OnLoadSuppliesCompleted);
319 }
320
322 {
324 m_AiGroup.GetOnWaypointCompleted().Remove(OnLoadSuppliesCompleted);
325 }
326
327 //------------------------------------------------------------------------------------------------
329 {
331 m_AiGroup.GetOnWaypointCompleted().Insert(OnMoveToSuppliedBaseCompleted);
332 }
333
334 //------------------------------------------------------------------------------------------------
336 {
338 m_AiGroup.GetOnWaypointCompleted().Remove(OnMoveToSuppliedBaseCompleted);
339 }
340
341 //------------------------------------------------------------------------------------------------
343 {
345 m_AiGroup.GetOnWaypointCompleted().Insert(OnMoveToSourceBaseCompleted);
346 }
347
348 //------------------------------------------------------------------------------------------------
350 {
352 m_AiGroup.GetOnWaypointCompleted().Remove(OnMoveToSourceBaseCompleted);
353 }
354
356 {
358 m_AiGroup.GetOnWaypointCompleted().Insert(OnUnloadSuppliesCompleted);
359 }
360
361 //------------------------------------------------------------------------------------------------
363 {
365 m_AiGroup.GetOnWaypointCompleted().Remove(OnUnloadSuppliesCompleted);
366 }
367
368 //------------------------------------------------------------------------------------------------
370 {
371 SCR_ResourceComponent resourceComponent = SCR_ResourceComponent.FindResourceComponent(m_Vehicle);
372 if (!resourceComponent)
373 return null;
374
375 return resourceComponent.GetContainer(EResourceType.SUPPLIES);
376 }
377
378 //------------------------------------------------------------------------------------------------
380 {
381 if (!m_AiGroup)
382 return;
383
384 AIWaypoint currentWaypoint = m_AiGroup.GetCurrentWaypoint();
385
386 if (currentWaypoint)
387 m_AiGroup.CompleteWaypoint(currentWaypoint);
388 }
389
390 //------------------------------------------------------------------------------------------------
391 protected void CompleteAllWaypoints()
392 {
393 if (!m_AiGroup)
394 return;
395
396 m_AiGroup.CompleteAllWaypoints();
397 }
398
399 //------------------------------------------------------------------------------------------------
400 protected void OnWaypointFailed(int moveResult, IEntity vehicleUsed, bool isWaypointRelated, vector moveLocation)
401 {
402 if (moveResult == EMoveError.OK)
403 return;
404
406 }
407
408 //------------------------------------------------------------------------------------------------
409 protected SCR_AIWaypoint CreateMoveWaypoint(SCR_EResupplyTaskSolverState resupplySolverState, vector waypointPosition, float waypointCompletionRadius)
410 {
411 SCR_AIWorld aiWorld = SCR_AIWorld.Cast(GetGame().GetAIWorld());
412 if (!aiWorld)
413 {
415 return null;
416 }
417
418 RoadNetworkManager roadNetworkManager = aiWorld.GetRoadNetworkManager();
419 if (!roadNetworkManager)
420 {
422 return null;
423 }
424
425 vector reachableWaypointPosition;
426
427 if (!roadNetworkManager.GetReachableWaypointInRoad(m_AiGroup.GetOrigin(), waypointPosition, waypointCompletionRadius, reachableWaypointPosition))
428 reachableWaypointPosition = waypointPosition;
429
430 SCR_AIWaypoint waypoint = SpawnMoveWaypoint(reachableWaypointPosition);
431 if (!waypoint)
432 {
434 return null;
435 }
436
437 waypoint.SetCompletionRadius(waypointCompletionRadius - vector.Distance(reachableWaypointPosition, waypointPosition));
438 SetResupplyTaskSolverState(resupplySolverState);
439 m_AiGroup.AddWaypoint(waypoint);
440
441 return waypoint;
442 }
443
444 //------------------------------------------------------------------------------------------------
445 protected SCR_LoadSuppliesWaypoint CreateLoadSuppliesWaypoint()
446 {
447 SCR_LoadSuppliesWaypoint waypoint = SCR_LoadSuppliesWaypoint.Cast(SpawnLoadSupplyWaypoint());
448 if (!waypoint)
449 {
451 return null;
452 }
453
454 waypoint.SetBase(m_SourceBase);
456 m_AiGroup.AddWaypoint(waypoint);
457 m_LoadSuppliesWaypoint = waypoint;
459
460 return waypoint;
461 }
462
463 //------------------------------------------------------------------------------------------------
465 {
467 if (!container || !m_SourceBase)
468 {
470 return;
471 }
472
473 float sourceBaseRadius = m_SourceBase.GetRadius();
474 bool isCloseToSourceBase = vector.DistanceSq(m_SourceBase.GetOwner().GetOrigin(), m_AiGroup.GetOrigin()) < (sourceBaseRadius * sourceBaseRadius);
475 if (isCloseToSourceBase)
476 {
478 return;
479 }
480
481 float normalizedResource = container.GetResourceValue() / container.GetMaxResourceValue();
482 if (normalizedResource > m_ResupplyTaskSolverEntry.GetResourceValueThreshold())
483 {
485 SCR_EResupplyTaskSolverState.MOVING_TO_SUPPLIED_BASE,
486 m_ResupplyTask.GetOrigin(),
487 m_ResupplyTask.GetMilitaryBase().GetRadius()
488 );
489
491 return;
492 }
493 else
494 {
496 SCR_EResupplyTaskSolverState.MOVING_TO_SOURCE_BASE,
497 m_SourceBase.GetOwner().GetOrigin(),
498 m_SourceBase.GetRadius()
499 );
500
502 return;
503 }
504 }
505
506 //------------------------------------------------------------------------------------------------
507 protected void OnBoardingCompleted(AIWaypoint waypoint)
508 {
509 if (waypoint != m_BoardingWaypoint)
510 return;
511
514 }
515
516 //------------------------------------------------------------------------------------------------
518 {
519 if (waypoint != m_MoveToSourceBaseWaypoint)
520 return;
521
524 }
525
526 //------------------------------------------------------------------------------------------------
528 {
529 if (waypoint != m_LoadSuppliesWaypoint)
530 return;
531
533
535 SCR_EResupplyTaskSolverState.MOVING_TO_SUPPLIED_BASE,
536 m_ResupplyTask.GetOrigin(),
537 m_ResupplyTask.GetMilitaryBase().GetRadius()
538 );
539
541 }
542
543 //------------------------------------------------------------------------------------------------
563
564 //------------------------------------------------------------------------------------------------
566 {
567 if (waypoint != m_UnloadSuppliesWaypoint)
568 return;
569
571
573 SCR_EResupplyTaskSolverState.MOVING_TO_SOURCE_BASE,
574 m_SourceBase.GetOwner().GetOrigin(),
575 m_SourceBase.GetRadius()
576 );
577
579 }
580
581 //------------------------------------------------------------------------------------------------
582 protected SCR_AIWaypoint SpawnWaypoint(Resource waypointPrefab, vector origin)
583 {
584 if (!waypointPrefab.IsValid())
585 return null;
586
587 EntitySpawnParams spawnParams = new EntitySpawnParams();
588 spawnParams.TransformMode = ETransformMode.WORLD;
589
590 float surfaceY = GetGame().GetWorld().GetSurfaceY(origin[0], origin[2]);
591 origin[1] = surfaceY;
592 spawnParams.Transform[3] = origin;
593
594 return SCR_AIWaypoint.Cast(GetGame().SpawnEntityPrefab(waypointPrefab, GetGame().GetWorld(), spawnParams));
595 }
596
597 //------------------------------------------------------------------------------------------------
599 {
601 return null;
602
603 Resource prefab = Resource.Load(m_ResupplyTaskSolverEntry.GetGetOutWaypointPrefabResourceName());
604 return SCR_BoardingWaypoint.Cast(SpawnWaypoint(prefab, m_Vehicle.GetOrigin()));
605 }
606
607 //------------------------------------------------------------------------------------------------
609 {
611 return null;
612
613 Resource prefab = Resource.Load(m_ResupplyTaskSolverEntry.GetGetInWaypointPrefabResourceName());
614 return SCR_BoardingEntityWaypoint.Cast(SpawnWaypoint(prefab, m_Vehicle.GetOrigin()));
615 }
616
617 //------------------------------------------------------------------------------------------------
619 {
621 return null;
622
623 Resource prefab = Resource.Load(m_ResupplyTaskSolverEntry.GetMoveWaypointPrefabResourceName());
624 return SpawnWaypoint(prefab, position);
625 }
626
627 //------------------------------------------------------------------------------------------------
629 {
631 return null;
632
633 Resource prefab = Resource.Load(m_ResupplyTaskSolverEntry.GetLoadSuppliesWaypointPrefabResourceName());
634 return SpawnWaypoint(prefab, m_AiGroup.GetOrigin());
635 }
636
637 //------------------------------------------------------------------------------------------------
638 protected SCR_UnloadSuppliesWaypoint SpawnUnloadSupplyWaypoint()
639 {
641 return null;
642
643 Resource prefab = Resource.Load(m_ResupplyTaskSolverEntry.GetUnloadSuppliesWaypointPrefabResourceName());
644 return SCR_UnloadSuppliesWaypoint.Cast(SpawnWaypoint(prefab, m_AiGroup.GetOrigin()));
645 }
646
647 //------------------------------------------------------------------------------------------------
659}
ArmaReforgerScripted GetGame()
Definition game.c:1398
vector position
ScriptInvokerBase< ScriptInvokerInt2Method > ScriptInvokerInt2
void SCR_Task(IEntitySource src, IEntity parent)
Definition SCR_Task.c:1938
Object holding reference to resource. In destructor release the resource.
Definition Resource.c:25
SCR_CampaignMilitaryBaseComponent GetSourceBase()
void FailResupplyTaskSolving(SCR_EResupplyTaskSolverErrorState errorState=SCR_EResupplyTaskSolverErrorState.DEFAULT)
Mark solver as failed, clean up, and provide error reporting.
SCR_CampaignMilitaryBaseComponent m_SourceBase
SCR_AIWaypoint SpawnLoadSupplyWaypoint()
SCR_LoadSuppliesWaypoint m_LoadSuppliesWaypoint
SCR_ResourceContainer GetVehicleSuppliesContainer()
SCR_AIWaypoint SpawnWaypoint(Resource waypointPrefab, vector origin)
SCR_BoardingEntityWaypoint CreateBoardingVehicleWaypoint()
void AllowMaxLOD(SCR_AIGroup aiGroup)
SCR_AIWaypoint SpawnMoveWaypoint(vector position)
SCR_UnloadSuppliesWaypoint SpawnUnloadSupplyWaypoint()
void OnMoveToSuppliedBaseCompleted(SCR_AIWaypoint waypoint)
SCR_BoardingEntityWaypoint SpawnBoardingWaypoint()
SCR_UnloadSuppliesWaypoint m_UnloadSuppliesWaypoint
SCR_AIGroupConditionManager GetAiGroupConditionManager()
EAIGroupCombatMode m_ePreviousGroupCombatMode
override void StartTaskSolving()
Start the task-solving process, branching to appropriate initial action.
void OnUnloadSuppliesCompleted(SCR_AIWaypoint waypoint)
SCR_BoardingEntityWaypoint m_BoardingWaypoint
ScriptInvokerInt2 GetOnResupplyTaskSolverStateChanged()
void OnMoveToSourceBaseCompleted(SCR_AIWaypoint waypoint)
ref SCR_VehicleConditionManager m_VehicleConditionManager
SCR_ResupplyCampaignMilitaryBaseTaskEntity m_ResupplyTask
void ResetToInitialState()
Reset solver state to initial (inactive).
SCR_AIVehicleUsageComponent m_AiVehicleUsage
SCR_AIWaypoint m_MoveToSourceBaseWaypoint
ref SCR_AIGroupConditionManager m_AiGroupConditionManager
SCR_LoadSuppliesWaypoint CreateLoadSuppliesWaypoint()
void SetResupplyTaskSolverState(SCR_EResupplyTaskSolverState state)
void SetSourceBase(SCR_CampaignMilitaryBaseComponent sourceBase)
void OnBoardingCompleted(AIWaypoint waypoint)
SCR_EResupplyTaskSolverState GetResupplyTaskSolverState()
ref ScriptInvokerInt2 m_OnResupplyTaskSolverStateChanged
SCR_AIWaypoint CreateMoveWaypointToSourceBase()
void SetVehicle(Vehicle vehicle)
ref SCR_ResupplyTaskSolverEntry m_ResupplyTaskSolverEntry
override void Update(float timeSlice)
Periodic update for resource/availability checks.
SCR_EResupplyTaskSolverState m_eResupplySolverState
SCR_BoardingWaypoint SpawnGetOutWaypoint()
void SetResupplyTaskSolverEntry(SCR_ResupplyTaskSolverEntry entry)
Configures this solver with a resupply solver entry.
void PreventMaxLOD(SCR_AIGroup aiGroup)
SCR_VehicleConditionManager GetVehicleConditionManager()
override void SetAiGroup(SCR_AIGroup aiGroup)
void CompleteTaskSolving()
Mark solver as completed with cleanup.
SCR_AIWaypoint CreateMoveWaypoint(SCR_EResupplyTaskSolverState resupplySolverState, vector waypointPosition, float waypointCompletionRadius)
void OnLoadSuppliesCompleted(SCR_AIWaypoint waypoint)
SCR_AIWaypoint m_MoveToSuppliedBaseWaypoint
SCR_AIGroupUtilityComponent m_TransportGroupUtility
void OnWaypointFailed(int moveResult, IEntity vehicleUsed, bool isWaypointRelated, vector moveLocation)
override void Init(SCR_Task task, SCR_TaskSolverContext context)
Initialize solver with task and context. Registers events.
void SetIgnoredAiGroup(SCR_AIGroup aiGroup)
enum EPhysicsLayerPresets Vehicle
Definition gameLib.c:24
void EntitySpawnParams()
Definition gameLib.c:130
proto external void AllowMaxLOD()
EMoveError
Definition EMoveError.c:13
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
proto void PrintFormat(string fmt, void param1=NULL, void param2=NULL, void param3=NULL, void param4=NULL, void param5=NULL, void param6=NULL, void param7=NULL, void param8=NULL, void param9=NULL, LogLevel level=LogLevel.NORMAL)