Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_CampaignFactionCommanderHandlerComponent.c
Go to the documentation of this file.
2[ComponentEditorProps(category: "GameScripted/Commander", description: "Handles voting and AI decisions for the faction Commander role. Should be attached to gamemode entity.")]
6
7class SCR_CampaignFactionCommanderHandlerComponent : SCR_FactionCommanderHandlerComponent
8{
9 [Attribute("100", desc: "How far from the Establish task will players still be able to build a new base.", params: "0 inf 1")]
10 protected int m_iBaseEstablishingRadius;
11
12 [Attribute()]
13 protected ref SCR_MinBaseDistanceConfig m_MinBaseDistanceConfig;
14
15 static const string SEIZE_TASK_RELAY_NAME = "#AR-CampaignTasks_TitleHack-UC";
16 static const string SEIZE_TASK_RELAY_DESC = "#AR-CampaignTasks_DescriptionReconfigure";
17 static const string SEIZE_TASK_NAME = "#AR-CampaignTasks_TitleSeize-UC";
18 static const string SEIZE_TASK_DESC = "#AR-CampaignTasks_DescriptionSeize";
19 static const string SEIZE_TASK_ID = "%1_SeizeCampaignMilitaryBase_%2";
20 static const ResourceName SEIZE_TASK_PREFAB = "{1CE0D63C852E747B}Prefabs/Tasks/SeizeCampaignMilitaryBaseTaskEntity.et";
21
22 //------------------------------------------------------------------------------------------------
27
28 //------------------------------------------------------------------------------------------------
35
36 //------------------------------------------------------------------------------------------------
40 {
41 if (!m_MinBaseDistanceConfig)
42 return 0;
43
44 return m_MinBaseDistanceConfig.GetMinDistanceFromBase(baseType);
45 }
46
47 //------------------------------------------------------------------------------------------------
48 protected void OnBaseFactionChanged(SCR_MilitaryBaseComponent base, Faction faction)
49 {
51
52 if (!campaignBase)
53 return;
54
55 SCR_CampaignFaction campaignFaction = SCR_CampaignFaction.Cast(faction);
56
57 if (!campaignFaction || !campaignFaction.IsPlayable())
58 return;
59
60 GetGame().GetCallqueue().CallLater(CheckSeizeTasks, SCR_GameModeCampaign.DEFAULT_DELAY, false, campaignFaction, -1);
61 }
62
63 //------------------------------------------------------------------------------------------------
64 protected void OnFactionTasksEnabledChanged(Faction faction)
65 {
66 SCR_CampaignFaction campaignFaction = SCR_CampaignFaction.Cast(faction);
67
68 if (!campaignFaction)
69 return;
70
71 GetGame().GetCallqueue().CallLater(CheckSeizeTasks, SCR_GameModeCampaign.DEFAULT_DELAY, false, campaignFaction, -1);
72 }
73
74 //------------------------------------------------------------------------------------------------
75 protected void OnRadioCoverageChanged(notnull SCR_CoverageRadioComponent radio)
76 {
78
79 // We care only about bases
80 if (!base)
81 return;
82
83 SCR_CampaignFaction faction = base.GetCampaignFaction();
84
85 if (!faction)
86 {
87 GetGame().GetCallqueue().CallLater(OnRadioCoverageChanged, SCR_GameModeCampaign.MEDIUM_DELAY, false, radio);
88 return;
89 }
90
91 // used call later, because base callsings are not set yet
92 GetGame().GetCallqueue().CallLater(CheckSeizeTasks, SCR_GameModeCampaign.DEFAULT_DELAY, false, faction, -1);
93 }
94
95 //------------------------------------------------------------------------------------------------
96 protected void OnCommanderChanged(SCR_Faction faction, int commanderPlayerId)
97 {
98 SCR_CampaignFaction campaignFaction = SCR_CampaignFaction.Cast(faction);
99 if (!campaignFaction)
100 return;
101
102 CheckSeizeTasks(campaignFaction, commanderPlayerId);
103
104 if (!campaignFaction.IsAICommander())
105 campaignFaction.SendHQMessage(SCR_ERadioMsg.COMMANDER_ARRIVAL);
106 else
107 campaignFaction.SendHQMessage(SCR_ERadioMsg.COMMANDER_RESIGNATION);
108 }
109
110 //------------------------------------------------------------------------------------------------
111 protected void CheckSeizeTasks(notnull SCR_CampaignFaction faction, int playerid)
112 {
114 if (!militaryBaseSystem)
115 return;
116
117 array<SCR_MilitaryBaseComponent> bases = {};
118 militaryBaseSystem.GetBases(bases);
119
120 array<Faction> factions = {};
121 GetGame().GetFactionManager().GetFactionsList(factions);
122
123 SCR_CampaignFaction campaignFaction;
125 SCR_CampaignFaction baseFaction;
126 foreach (SCR_MilitaryBaseComponent base : bases)
127 {
128 campaignbase = SCR_CampaignMilitaryBaseComponent.Cast(base);
129 if (!campaignbase || !campaignbase.IsValidForSeizeTask())
130 continue;
131
132 baseFaction = campaignbase.GetCampaignFaction();
133 if (!baseFaction)
134 continue;
135
136 foreach (Faction f : factions)
137 {
138 campaignFaction = SCR_CampaignFaction.Cast(f);
139 if (!campaignFaction || !campaignFaction.IsPlayable() || !campaignFaction.IsAICommander() || !campaignFaction.IsTasksEnabled())
140 continue;
141
142 if (baseFaction == campaignFaction)
143 continue;
144
145 if (!campaignbase.IsHQRadioTrafficPossible(campaignFaction))
146 continue;
147
148 CreateSeizeTask(campaignFaction, campaignbase, playerid);
149 }
150 }
151 }
152
153 //------------------------------------------------------------------------------------------------
157 void CreateSeizeTask(notnull SCR_CampaignFaction faction, SCR_CampaignMilitaryBaseComponent targetBase, int playerid)
158 {
159 if (GetTaskOnBase(targetBase, faction, SCR_SeizeCampaignMilitaryBaseTaskEntity))
160 return;
161
162 SCR_GroupTaskManagerComponent groupTaskManager = SCR_GroupTaskManagerComponent.GetInstance();
163 if (!groupTaskManager || !groupTaskManager.CanCreateNewTaskWithResourceName(SEIZE_TASK_PREFAB, faction))
164 return;
165
166 string taskId = string.Format(SEIZE_TASK_ID, faction.GetFactionKey(), targetBase.GetCallsign());
167 string title;
168 string description;
169
170 if (targetBase.GetType() == SCR_ECampaignBaseType.RELAY)
171 {
172 title = SEIZE_TASK_RELAY_NAME;
173 description = SEIZE_TASK_RELAY_DESC;
174 }
175 else
176 {
177 title = SEIZE_TASK_NAME;
178 description = SEIZE_TASK_DESC;
179 }
180
181 SCR_TaskSystem taskSystem = SCR_TaskSystem.GetInstance();
182 if (!taskSystem)
183 return;
184
185 SCR_CampaignMilitaryBaseTaskEntity newTask = SCR_CampaignMilitaryBaseTaskEntity.Cast(taskSystem.CreateTask(
186 SEIZE_TASK_PREFAB,
187 taskId,
188 "",
189 description,
190 targetBase.GetOwner().GetOrigin()
191 ));
192
193 if (!newTask)
194 return;
195
196 newTask.SetTaskName(title, {targetBase.GetFormattedBaseNameWithCallsign(faction)});
197 newTask.SetMilitaryBaseCallSign(targetBase.GetCallsign());
198 taskSystem.SetTaskOwnership(newTask, SCR_ETaskOwnership.EXECUTOR);
199 taskSystem.SetTaskVisibility(newTask, SCR_ETaskVisibility.GROUP);
200 taskSystem.AddTaskFaction(newTask, faction.GetFactionKey());
201
202 groupTaskManager.SetGroupTask(newTask, 0);
203 }
204
205 //------------------------------------------------------------------------------------------------
206 protected SCR_Task GetTaskOnBase(notnull SCR_CampaignMilitaryBaseComponent campaignMilitaryBase, notnull Faction faction, typename taskClass)
207 {
208 array<SCR_Task> tasks = {};
209 SCR_TaskSystem.GetInstance().GetTasksByStateFiltered(
210 tasks,
211 SCR_ETaskState.CREATED | SCR_ETaskState.ASSIGNED,
212 faction.GetFactionKey(),
213 -1,
214 taskClass
215 );
216
219 SCR_CampaignMilitaryBaseTaskEntity campaignMilitaryBaseTask;
220
221 foreach (SCR_Task task : tasks)
222 {
223 campaignMilitaryBaseTask = SCR_CampaignMilitaryBaseTaskEntity.Cast(task);
224 if (!campaignMilitaryBaseTask)
225 continue;
226
227 data = SCR_CampaignMilitaryBaseTaskData.Cast(task.GetTaskData());
228 if (!data)
229 continue;
230
231 base = campaignMilitaryBaseTask.GetMilitaryBase();
232 if (!base)
233 continue;
234
235 if (base == campaignMilitaryBase)
236 return task;
237 }
238
239 return null;
240 }
241
242 //------------------------------------------------------------------------------------------------
248 protected void OnPlayerXPChanged(int playerId, int currentXP, int XPToAdd, SCR_EXPRewards rewardId)
249 {
250 SCR_GroupsManagerComponent groupsManager = SCR_GroupsManagerComponent.GetInstance();
251 if (!groupsManager)
252 return;
253
254 SCR_AIGroup playerGroup = groupsManager.GetPlayerGroup(playerId);
255 if (!playerGroup)
256 return;
257
258 // To prevent duplicate addition of reward XP, we only reward Commander XP for Squad Leader gained XP
259 if (playerGroup.GetLeaderID() != playerId)
260 return;
261
262 SCR_Faction playerFaction = SCR_Faction.Cast(playerGroup.GetFaction());
263 if (!playerFaction)
264 return;
265
266 // AI Commander is not awarded any XP
267 if (playerFaction.IsAICommander())
268 return;
269
270 int commanderId = playerFaction.GetCommanderId();
271 AwardCommanderTaskCompletionXP(commanderId, rewardId, XPToAdd);
272 }
273
274 //------------------------------------------------------------------------------------------------
280 protected void AwardCommanderTaskCompletionXP(int commanderId, SCR_EXPRewards rewardId, int XPToAdd)
281 {
282 SCR_XPHandlerComponent xpHandler = SCR_XPHandlerComponent.Cast(GetGame().GetGameMode().FindComponent(SCR_XPHandlerComponent));
283 if (!xpHandler)
284 return;
285
286 SCR_CommanderAdditionalXPRewardInfo rewardInfo = SCR_CommanderAdditionalXPRewardInfo.Cast(xpHandler.GetXpRewardInfo(SCR_EXPRewards.COMMANDER_TASK_COMPLETED));
287 if (!rewardInfo)
288 return;
289
290 // Only a subset of XP awards should award Commander XP as well
291 if (!rewardInfo.IsXPRewardListed(rewardId))
292 return;
293
294 xpHandler.AwardXP(commanderId, SCR_EXPRewards.COMMANDER_TASK_COMPLETED, 1.0, false, rewardInfo.GetRewardXPAmountFraction() * XPToAdd);
295 }
296
297 //------------------------------------------------------------------------------------------------
298 override void OnPlayerRegistered(int playerId)
299 {
300 super.OnPlayerRegistered(playerId);
301
302 RegisterPlayerXPInvokers(playerId);
303 }
304
305 //------------------------------------------------------------------------------------------------
306 override void OnPlayerDisconnected(int playerId, KickCauseCode cause, int timeout)
307 {
308 super.OnPlayerDisconnected(playerId, cause, timeout);
309
310 RegisterPlayerXPInvokers(playerId, true);
311 }
312
313 //------------------------------------------------------------------------------------------------
314 override protected void OnServerCommanderChanged(int newCommanderId, int oldCommanderId)
315 {
316 super.OnServerCommanderChanged(newCommanderId, oldCommanderId);
317
318 RegisterPlayerXPInvokers(newCommanderId, true);
319 RegisterPlayerXPInvokers(oldCommanderId, false);
320 }
321
322 //------------------------------------------------------------------------------------------------
323 override void OnPlayerKilled(notnull SCR_InstigatorContextData instigatorContextData)
324 {
325 super.OnPlayerKilled(instigatorContextData);
326
327 int playerID = instigatorContextData.GetVictimPlayerID();
328
329 SCR_CampaignFaction faction = SCR_CampaignFaction.Cast(SCR_FactionManager.SGetPlayerFaction(playerID));
330 if (!faction)
331 return;
332
333 if (faction.IsPlayerCommander(playerID))
334 faction.SendHQMessage(SCR_ERadioMsg.COMMANDER_DISRUPTION);
335 }
336
337 //------------------------------------------------------------------------------------------------
338 // Registers or unregisters invoker on player xp change
339 // \param[in] playerId
340 // \param[in] unregister - if true, invokers are unregistered, if false, invokers are registered
341 protected void RegisterPlayerXPInvokers(int playerId, bool unregister = false)
342 {
343 if (playerId <= 0)
344 return;
345
346 PlayerController playerController = GetGame().GetPlayerManager().GetPlayerController(playerId);
347 if (!playerController)
348 return;
349
350 SCR_PlayerXPHandlerComponent xpHandler = SCR_PlayerXPHandlerComponent.Cast(playerController.FindComponent(SCR_PlayerXPHandlerComponent));
351 if (!xpHandler)
352 return;
353
354 if (unregister)
355 xpHandler.GetOnPlayerXPChanged().Remove(OnPlayerXPChanged);
356 else
357 xpHandler.GetOnPlayerXPChanged().Insert(OnPlayerXPChanged);
358 }
359
360 //------------------------------------------------------------------------------------------------
361 override void EOnInit(IEntity owner)
362 {
363 super.EOnInit(owner);
364
365 if (!m_RplComponent || m_RplComponent.IsProxy())
366 return;
367
368 SCR_GameModeCampaign campaign = SCR_GameModeCampaign.GetInstance();
369
370 if (!campaign)
371 return;
372
374 if (!radioSystem)
375 return;
376
377 radioSystem.GetOnCoverageChanged().Insert(OnRadioCoverageChanged);
378
380
381 if (!baseSystem)
382 return;
383
385
387
388 SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
389 if (factionManager)
390 factionManager.GetOnFactionTasksEnabledChanged().Insert(OnFactionTasksEnabledChanged);
391 }
392
393 //------------------------------------------------------------------------------------------------
395 {
396 SCR_GameModeCampaign campaign = SCR_GameModeCampaign.GetInstance();
397 if (!campaign)
398 return;
399
401 if (!radioSystem)
402 return;
403
404 radioSystem.GetOnCoverageChanged().Remove(OnRadioCoverageChanged);
405
407 if (!baseSystem)
408 return;
409
411
413 }
414}
ArmaReforgerScripted GetGame()
Definition game.c:1398
enum EAIGroupCombatMode ComponentEditorProps(category:"GameScripted/AI", description:"Component for utility AI system for groups")
SCR_BaseGameMode GetGameMode()
RplComponent m_RplComponent
void OnFactionTasksEnabledChanged(Faction faction)
void OnServerCommanderChanged(int newCommanderId, int oldCommanderId)
void AwardCommanderTaskCompletionXP(int commanderId, SCR_EXPRewards rewardId, int XPToAdd)
void RegisterPlayerXPInvokers(int playerId, bool unregister=false)
void CheckSeizeTasks(notnull SCR_CampaignFaction faction, int playerid)
SCR_Task GetTaskOnBase(notnull SCR_CampaignMilitaryBaseComponent campaignMilitaryBase, notnull Faction faction, typename taskClass)
int GetBaseMinDistance(notnull SCR_CampaignMilitaryBaseComponent base)
void CreateSeizeTask(notnull SCR_CampaignFaction faction, SCR_CampaignMilitaryBaseComponent targetBase, int playerid)
void OnRadioCoverageChanged(notnull SCR_CoverageRadioComponent radio)
void OnCommanderChanged(SCR_Faction faction, int commanderPlayerId)
void OnBaseFactionChanged(notnull SCR_MilitaryBaseComponent base, Faction faction)
void OnPlayerKilled(notnull SCR_InstigatorContextData instigatorContextData)
Get all prefabs that have the spawner data
OnFactionCommanderChangedInvoker GetOnFactionCommanderChanged()
void SCR_FactionManager(IEntitySource src, IEntity parent)
void SCR_GameModeCampaign(IEntitySource src, IEntity parent)
void SCR_GroupTaskManagerComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
void SCR_GroupsManagerComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
ScriptInvokerBase< OnPlayerXPChangedMethod > OnPlayerXPChanged
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
SCR_ETaskVisibility
Definition SCR_Task.c:24
void SCR_Task(IEntitySource src, IEntity parent)
Definition SCR_Task.c:1938
SCR_ETaskState
Definition SCR_Task.c:3
int GetLeaderID()
Faction GetFaction()
Handles voting and AI decisions for the faction Commander role in Conflict. Should be attached to gam...
void SendHQMessage(SCR_ERadioMsg msgType, int baseCallsign=SCR_MilitaryBaseComponent.INVALID_BASE_CALLSIGN, int calledID=SCR_CampaignMilitaryBaseComponent.INVALID_PLAYER_INDEX, bool public=true, int param=SCR_CampaignRadioMsg.INVALID_RADIO_MSG_PARAM)
SCR_ECampaignBaseType GetType()
Returns type of this base.
bool IsHQRadioTrafficPossible(notnull SCR_CampaignFaction faction, SCR_ERadioCoverageStatus direction=SCR_ERadioCoverageStatus.RECEIVE)
SCR_CampaignFaction GetCampaignFaction()
Returns the owning faction.
Handles voting and AI decisions for the faction Commander role. Should be attached to gamemode entity...
bool IsAICommander()
int GetCommanderId()
int GetBases(notnull out array< SCR_MilitaryBaseComponent > bases)
OnBaseFactionChangedInvoker GetOnBaseFactionChanged()
static SCR_MilitaryBaseSystem GetInstance()
OnCoverageChangedInvoker GetOnCoverageChanged()
static SCR_RadioCoverageSystem GetInstance()
override void EOnInit(IEntity owner)
SCR_FieldOfViewSettings Attribute
override void OnPlayerRegistered(int playerId)
override void OnPlayerDisconnected(int playerId, KickCauseCode cause, int timeout)