Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_CharacterRankComponent.c
Go to the documentation of this file.
1[EntityEditorProps(category: "GameScripted", description: "Handles the character's rank.", color: "0 0 255 255")]
5
7{
8 [Attribute(defvalue: "1", uiwidget: UIWidgets.ComboBox, desc: "Rank", enums: ParamEnumArray.FromEnum(SCR_ECharacterRank))]
9 protected SCR_ECharacterRank m_iRank;
10
11 protected IEntity m_Owner;
12 static ref ScriptInvoker s_OnRankChanged = new ScriptInvoker();
13
14 //------------------------------------------------------------------------------------------------
19 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
20 void RpcDoSetCharacterRank(SCR_ECharacterRank newRank, SCR_ECharacterRank prevRank, bool silent)
21 {
22 SCR_ECharacterRank oldRank = m_iRank;
23 m_iRank = newRank;
24 OnRankChanged(oldRank, newRank, silent);
25
26 SpecialRankHandling(newRank, prevRank);
27 }
28
29 //------------------------------------------------------------------------------------------------
31 protected void SpecialRankHandling(SCR_ECharacterRank newRank, SCR_ECharacterRank prevRank)
32 {
33 // this logic should currently only be triggered when the renegade faction is configured! otherwise we ignore this logic
34 SCR_CampaignFaction Renegade = SCR_GameModeCampaign.GetInstance().GetFactionByEnum(SCR_ECampaignFaction.RNGD);
35
36 if (!Renegade)
37 return;
38
39 // RNGD (Renegades) is a "hidden faction" set up for when players get kicked out of their faction.
40 if (newRank == SCR_ECharacterRank.RENEGADE && GetCharacterFaction(m_Owner).IsRenegadePunishedExile())
41 {
42 AttemptSwitchFaction(Renegade);
43 return;
44 }
45
46 // Currently FIA is the only one to use the punishment mechanic, so if they are a renegade we can assume they came from
47 // the FIA faction. When they regain their rank they are able to rejoin FIA.
48 if (prevRank == SCR_ECharacterRank.RENEGADE && (GetCharacterFaction(m_Owner) == Renegade))
49 AttemptSwitchFaction(SCR_GameModeCampaign.GetInstance().GetFactionByEnum(SCR_ECampaignFaction.INDFOR));
50 }
51
52 //------------------------------------------------------------------------------------------------
54 protected void AttemptSwitchFaction(SCR_CampaignFaction campaignFaction)
55 {
57 bool isServer = (gameMode && gameMode.IsMaster()) || (!gameMode && Replication.IsServer());
58
59 if (!isServer)
60 return;
61
62 SCR_ChimeraCharacter character = SCR_ChimeraCharacter.Cast(m_Owner);
63 if (!character)
64 return;
65
66 SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
67 if (!factionManager)
68 return;
69
70 int playerId = GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(character);
71
72 PlayerController playerController = GetGame().GetPlayerManager().GetPlayerController(playerId);
73 if (!playerController)
74 return;
75
77 if (!playerFactionAffiliation)
78 return;
79
80 // self hosted clients might give an immediate response so we must be sure to be ready before attempting to set the faction
81 // if we notice the faction request didnt go through we dont need to watch for the response
82 playerFactionAffiliation.GetOnPlayerFactionResponseInvoker_S().Insert(FinishFactionSwitch);
83 if (!factionManager.SetPlayerFaction(character, campaignFaction))
84 playerFactionAffiliation.GetOnPlayerFactionResponseInvoker_S().Remove(FinishFactionSwitch);
85 }
86
87 //------------------------------------------------------------------------------------------------
89 protected void FinishFactionSwitch(SCR_PlayerFactionAffiliationComponent component, int factionIndex, bool response)
90 {
92
93 if (!response)
94 return;
95
96 SCR_PlayerControllerGroupComponent groupController = SCR_PlayerControllerGroupComponent.GetPlayerControllerComponent(component.GetPlayerId());
97 if (!groupController)
98 return;
99
100 groupController.CreateAndJoinGroup(component.GetAffiliatedFaction(), true);
101 }
102
103 //------------------------------------------------------------------------------------------------
106 static SCR_CharacterRankComponent GetCharacterRankComponent(IEntity unit)
107 {
109 }
110
111 //------------------------------------------------------------------------------------------------
114 void SetCharacterRank(SCR_ECharacterRank rank, bool silent = false)
115 {
116 if (rank != m_iRank)
117 {
118 Rpc(RpcDoSetCharacterRank, rank, m_iRank, silent);
119 RpcDoSetCharacterRank(rank, m_iRank, silent);
120 }
121 }
122
123 //------------------------------------------------------------------------------------------------
124 protected void OnRankChanged(SCR_ECharacterRank prevRank, SCR_ECharacterRank newRank, bool silent)
125 {
126 s_OnRankChanged.Invoke(prevRank, newRank, m_Owner, silent);
127 }
128
129 //------------------------------------------------------------------------------------------------
133 static SCR_ECharacterRank GetCharacterRank(IEntity unit)
134 {
135 if (!unit)
136 return SCR_ECharacterRank.INVALID;
137
138 SCR_CharacterRankComponent comp = GetCharacterRankComponent(unit);
139
140 if (!comp)
141 return SCR_ECharacterRank.INVALID;
142
143 return comp.GetCharacterRank();
144 }
145
146 //------------------------------------------------------------------------------------------------
148 {
149 if (!unit)
150 return null;
151
152 SCR_ChimeraCharacter character = SCR_ChimeraCharacter.Cast(unit);
153 if (!character)
154 return null;
155
156 Faction faction = character.GetFaction();
157 if (!faction)
158 return null;
159
160 return SCR_Faction.Cast(faction);
161 }
162
163 //------------------------------------------------------------------------------------------------
168 static string GetRankName(IEntity unit, SCR_ECharacterRank rank)
169 {
170 if (!unit)
171 return string.Empty;
172
173 SCR_CharacterRankComponent comp = GetCharacterRankComponent(unit);
174 if (!comp)
175 return string.Empty;
176
177 SCR_Faction faction = comp.GetCharacterFaction(unit);
178 if (!faction)
179 return string.Empty;
180
181 return faction.GetRanks().GetRankName(rank);
182 }
183
184 //------------------------------------------------------------------------------------------------
187 static string GetCharacterRankName(IEntity unit)
188 {
189 if (!unit)
190 return "";
191
192 SCR_CharacterRankComponent comp = GetCharacterRankComponent(unit);
193
194 if (!comp)
195 return "";
196
197 SCR_ECharacterRank rank = comp.GetCharacterRank();
198 SCR_Faction faction = comp.GetCharacterFaction(unit);
199
200 if (!faction)
201 return "";
202
203 return faction.GetRanks().GetRankName(rank);
204 }
205
206 //------------------------------------------------------------------------------------------------
209 static string GetCharacterRankNameUpperCase(IEntity unit)
210 {
211 if (!unit)
212 return "";
213
214 SCR_CharacterRankComponent comp = GetCharacterRankComponent(unit);
215
216 if (!comp)
217 return "";
218
219 SCR_ECharacterRank rank = comp.GetCharacterRank();
220 SCR_Faction faction = comp.GetCharacterFaction(unit);
221
222 if (!faction)
223 return "";
224
225 return faction.GetRanks().GetRankNameUpperCase(rank);
226 }
227
228 //------------------------------------------------------------------------------------------------
231 static string GetCharacterRankNameShort(IEntity unit)
232 {
233 if (!unit)
234 return "";
235
236 SCR_CharacterRankComponent comp = GetCharacterRankComponent(unit);
237
238 if (!comp)
239 return "";
240
241 SCR_ECharacterRank rank = comp.GetCharacterRank();
242 SCR_Faction faction = comp.GetCharacterFaction(unit);
243
244 if (!faction)
245 return "";
246
247 return faction.GetRanks().GetRankNameShort(rank);
248 }
249
250 //------------------------------------------------------------------------------------------------
253 static ResourceName GetCharacterRankInsignia(IEntity unit)
254 {
255 if (!unit)
256 return "";
257
258 SCR_CharacterRankComponent comp = GetCharacterRankComponent(unit);
259
260 if (!comp)
261 return "";
262
263 SCR_ECharacterRank rank = comp.GetCharacterRank();
264 SCR_Faction faction = comp.GetCharacterFaction(unit);
265
266 if (!faction)
267 return "";
268
269 return faction.GetRanks().GetRankInsignia(rank);
270 }
271
272 //------------------------------------------------------------------------------------------------
273 protected SCR_ECharacterRank GetCharacterRank()
274 {
275 return m_iRank;
276 }
277
278 //------------------------------------------------------------------------------------------------
279 override bool RplSave(ScriptBitWriter writer)
280 {
281 writer.WriteIntRange(m_iRank, 0, SCR_ECharacterRank.INVALID-1);
282
283 return true;
284 }
285
286 //-----------------------------------------------------------------------------------------------------------------------
287 override bool RplLoad(ScriptBitReader reader)
288 {
289 reader.ReadIntRange(m_iRank, 0, SCR_ECharacterRank.INVALID-1);
290
291 return true;
292 }
293
294 //------------------------------------------------------------------------------------------------
295 override void OnPostInit(IEntity owner)
296 {
297 if (!ChimeraCharacter.Cast(owner))
298 Print("SCR_CharacterRankComponent must be attached to ChimeraCharacter!", LogLevel.ERROR);
299 }
300
301 //------------------------------------------------------------------------------------------------
302 // constructor
307 {
308 m_Owner = ent;
309 }
310}
ArmaReforgerScripted GetGame()
Definition game.c:1398
override bool RplLoad(ScriptBitReader reader)
SCR_BaseGameMode GetGameMode()
override bool RplSave(ScriptBitWriter writer)
SCR_ECharacterRank m_iRank
SCR_Faction GetCharacterFaction(IEntity unit)
void SCR_CharacterRankComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
void SetCharacterRank(SCR_ECharacterRank rank, bool silent=false)
void FinishFactionSwitch(SCR_PlayerFactionAffiliationComponent component, int factionIndex, bool response)
Callback method for ensuring that completed faction switches are set up correctly.
void OnRankChanged(SCR_ECharacterRank prevRank, SCR_ECharacterRank newRank, bool silent)
SCR_ECharacterRank GetCharacterRank()
void RpcDoSetCharacterRank(SCR_ECharacterRank newRank, SCR_ECharacterRank prevRank, bool silent)
void SpecialRankHandling(SCR_ECharacterRank newRank, SCR_ECharacterRank prevRank)
Helper method for specific ranks with custom logic attached to them.
void AttemptSwitchFaction(SCR_CampaignFaction campaignFaction)
Helper method for attempting to switch factions.
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
bool IsRenegadePunishedExile()
void SCR_FactionManager(IEntitySource src, IEntity parent)
void SCR_GameModeCampaign(IEntitySource src, IEntity parent)
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
enum EVehicleType IEntity
void Rpc(func method, void p0=NULL, void p1=NULL, void p2=NULL, void p3=NULL, void p4=NULL, void p5=NULL, void p6=NULL, void p7=NULL)
proto external Managed FindComponent(typename typeName)
Main replication API.
Definition Replication.c:14
sealed bool IsMaster()
SCR_RankContainer GetRanks()
OnPlayerFactionResponseInvoker GetOnPlayerFactionResponseInvoker_S()
string GetRankNameShort(SCR_ECharacterRank rankID)
string GetRankNameUpperCase(SCR_ECharacterRank rankID)
string GetRankInsignia(SCR_ECharacterRank rankID)
string GetRankName(SCR_ECharacterRank rankID)
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute
void RplRpc(RplChannel channel, RplRcver rcver, RplCondition condition=RplCondition.None, string customConditionName="")
Definition EnNetwork.c:95
RplRcver
Definition RplRcver.c:59
RplChannel
Communication channel. Reliable is guaranteed to be delivered. Unreliable not.
Definition RplChannel.c:14
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134