Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_FastTravelComponent.c
Go to the documentation of this file.
1[EntityEditorProps(category: "GameScripted/FastTravel", description: "Handles client > server communication for Fast travel. Should be attached to PlayerController.")]
5
6class SCR_FastTravelComponent : ScriptComponent
7{
8 protected PlayerController m_PlayerController;
9
10 protected RplId m_iDestinationId = RplId.Invalid();
11
12 protected string m_sDestinationName;
13
14 protected bool m_bIsDestinationValid;
15 protected bool m_bDisableAfterUse;
16
18
19 [Attribute("300", desc: "Minimum delay between fast travels (seconds).", params: "0 inf 1")];
20 protected int m_iCooldown;
21
22 [Attribute("250", desc: "How far away from the target destination will player get teleported.", params: "0 inf 1")];
23 protected int m_iDistanceToTarget;
24
25 [Attribute("IEntity", desc: "Allowed class name of destination entity (inherited classes are also allowed).")];
26 protected string m_sDestinationType;
27
28 [RplProp(condition: RplCondition.OwnerOnly)]
30
31 static const int SPAWNING_RADIUS = 10;
32
33 static const float FADE_DURATION = 1.5;
34 static const float BLACKSCREEN_DURATION = 1.0;
35 static const float CLOSE_MAP_DELAY = 0.75;
36
37 //------------------------------------------------------------------------------------------------
39 static SCR_FastTravelComponent GetLocalInstance()
40 {
41 PlayerController pc = GetGame().GetPlayerController();
42
43 if (!pc)
44 return null;
45
46 return SCR_FastTravelComponent.Cast(pc.FindComponent(SCR_FastTravelComponent));
47 }
48
49 //------------------------------------------------------------------------------------------------
51 void SetDestination(RplId id, string name)
52 {
54 m_sDestinationName = name;
55 m_bIsDestinationValid = Replication.FindItem(id) != null;
56 }
57
58 //------------------------------------------------------------------------------------------------
61 {
62 return m_sDestinationName;
63 }
64
65 //------------------------------------------------------------------------------------------------
68 {
69 return m_iCooldown;
70 }
71
72 //------------------------------------------------------------------------------------------------
75 {
76 m_bDisableAfterUse = true;
77 }
78
79 //------------------------------------------------------------------------------------------------
83 static void ToggleMapDestinationSelection(bool enable, bool disableAfterUse = true)
84 {
85 SCR_FastTravelComponent comp = GetLocalInstance();
86
87 if (!comp)
88 return;
89
90 if (enable)
91 SCR_NotificationsComponent.SendLocal(ENotification.FASTTRAVEL_AVAILABLE);
92 else
93 SCR_NotificationsComponent.SendLocal(ENotification.FASTTRAVEL_UNAVAILABLE);
94
95 SCR_MapEntity.GetOnSelectionChanged().Remove(comp.OnSelectionChanged);
96
97 if (!enable)
98 return;
99
100 if (disableAfterUse)
101 comp.DisableAfterUse();
102
103 SCR_MapEntity.GetOnSelectionChanged().Insert(comp.OnSelectionChanged);
104 }
105
106 //------------------------------------------------------------------------------------------------
109 {
111
112 if (!mapEntity)
113 return;
114
115 MapItem selectedItem = item;
116
117 if (!selectedItem)
118 selectedItem = mapEntity.GetHoveredItem();
119
120 if (!selectedItem)
121 return;
122
123 IEntity entity = selectedItem.Entity();
124
125 if (!entity || !entity.IsInherited(m_sDestinationType.ToType()))
126 return;
127
128 RplId id = FindDestinationId(entity);
129
130 if (!id.IsValid())
131 return;
132
133 IEntity player = m_PlayerController.GetControlledEntity();
134
135 if (player && mapEntity.IsOpen())
136 {
138
139 if (comp)
140 comp.RemoveHeldGadget();
141 }
142
143 SetDestination(id, string.Empty);
144 FastTravel();
145 }
146
147 //------------------------------------------------------------------------------------------------
152 {
153 RplId id = Replication.FindItemId(entity);
154
155 if (id.IsValid())
156 return id;
157
158 RplComponent rpl = RplComponent.Cast(entity.FindComponent(RplComponent));
159
160 if (!rpl)
161 return RplId.Invalid();
162
163 return rpl.Id();
164 }
165
166 //------------------------------------------------------------------------------------------------
169 {
171 return;
172
174 }
175
176 //------------------------------------------------------------------------------------------------
177 [RplRpc(RplChannel.Reliable, RplRcver.Server)]
178 protected void RpcAsk_FastTravel(RplId destinationId)
179 {
181 return;
182
183 IEntity player = m_PlayerController.GetControlledEntity();
184
185 if (!player)
186 return;
187
188 IEntity target = GetEntityByDestinationId(destinationId);
189
190 if (!target)
191 return;
192
193 if (!ServerSanityCheck(target))
194 return;
195
196 ChimeraWorld world = GetGame().GetWorld();
197
198 m_fNextTravelAvailableAt = world.GetServerTimestamp().PlusSeconds(GetCooldown());
199 Replication.BumpMe();
201 }
202
203 //------------------------------------------------------------------------------------------------
207 {
208 Managed destination = Replication.FindItem(id);
209
210 if (!destination)
211 return null;
212
213 IEntity target;
214
215 // We need to get target entity from the replicated item
216 if (destination.IsInherited(IEntity))
217 target = IEntity.Cast(destination);
218 else if (destination.IsInherited(ScriptComponent))
219 target = ScriptComponent.Cast(destination).GetOwner();
220 else if (destination.IsInherited(BaseRplComponent))
221 target = BaseRplComponent.Cast(destination).GetEntity();
222
223 return target;
224 }
225
226 //------------------------------------------------------------------------------------------------
229 protected bool ServerSanityCheck(notnull IEntity target)
230 {
231 ChimeraWorld world = GetGame().GetWorld();
232
233 return (!m_fNextTravelAvailableAt || m_fNextTravelAvailableAt.LessEqual(world.GetServerTimestamp()));
234 }
235
236 //------------------------------------------------------------------------------------------------
241
242 //------------------------------------------------------------------------------------------------
245 {
246 m_fNextTravelAvailableAt = timestamp;
247 Replication.BumpMe();
248 }
249
250 //------------------------------------------------------------------------------------------------
251 [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
253 {
255 ToggleMapDestinationSelection(false);
256
258
259 // Wait for map to close and fade from black
260 GetGame().GetCallqueue().CallLater(FadeOut, CLOSE_MAP_DELAY * 1000);
261 }
262
263 //------------------------------------------------------------------------------------------------
264 protected void FadeOut()
265 {
267 if (manager)
268 {
270 if (fade)
271 fade.FadeOutEffect(true, FADE_DURATION);
272 }
273
274 // Wait for fade to black
275 GetGame().GetCallqueue().CallLater(Teleport, (FADE_DURATION + BLACKSCREEN_DURATION) * 1000);
276 }
277
278 //------------------------------------------------------------------------------------------------
279 protected void Teleport()
280 {
281 SCR_ChimeraCharacter player = SCR_ChimeraCharacter.Cast(m_PlayerController.GetControlledEntity());
282
283 if (player && !player.IsInVehicle() && !player.GetCharacterController().IsDead())
284 {
286 SCR_NotificationsComponent.SendLocal(ENotification.FASTTRAVEL_DONE);
287 }
288
290 if (manager)
291 {
293 if (fade)
294 fade.FadeOutEffect(false, FADE_DURATION);
295 }
296 }
297
298 //------------------------------------------------------------------------------------------------
301 protected vector CalculateDestinationVector(notnull IEntity destination)
302 {
303 return CalculateDestinationVector(destination.GetOrigin());
304 }
305
306 //------------------------------------------------------------------------------------------------
308 {
309 IEntity player = m_PlayerController.GetControlledEntity();
310
311 if (!player)
312 return vector.Zero;
313
314 vector playerOrigin = m_PlayerController.GetControlledEntity().GetOrigin();
315 vector newPlayerOrigin;
316
317 newPlayerOrigin[1] = playerOrigin[1];
318
319 float angle = Math.Atan2(playerOrigin[2] - targetOrigin[2], playerOrigin[0] - targetOrigin[0]);
320 newPlayerOrigin[0] = targetOrigin[0] + m_iDistanceToTarget * Math.Cos(angle);
321 newPlayerOrigin[2] = targetOrigin[2] + m_iDistanceToTarget * Math.Sin(angle);
322 newPlayerOrigin[1] = SCR_TerrainHelper.GetTerrainY(newPlayerOrigin);
323
324 SCR_WorldTools.FindEmptyTerrainPosition(newPlayerOrigin, newPlayerOrigin, SPAWNING_RADIUS);
325
326 return newPlayerOrigin;
327 }
328
329 //------------------------------------------------------------------------------------------------
330 override void OnPostInit(IEntity owner)
331 {
332 super.OnPostInit(owner);
333 SetEventMask(owner, EntityEvent.INIT);
334 }
335
336 //------------------------------------------------------------------------------------------------
337 override void EOnInit(IEntity owner)
338 {
339 m_PlayerController = PlayerController.Cast(owner);
340
342 Print("SCR_FastTravelComponent must be attached to PlayerController!", LogLevel.ERROR);
343 }
344
345 //------------------------------------------------------------------------------------------------
346 // destructor
351}
AddonBuildInfoTool id
ENotification
ArmaReforgerScripted GetGame()
Definition game.c:1398
SCR_CacheNoteComponentClass ScriptComponentClass RplProp()] protected ref array< string > m_aLines
int GetCooldown()
Reads the cooldown value from player's rank.
bool ServerSanityCheck(notnull IEntity target)
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
vector position
void SetDestination(RplId id, string name)
Destination must be a replicated item with valid RplId.
void RpcAsk_FastTravel(RplId destinationId)
int m_iDistanceToTarget
vector CalculateDestinationVector(notnull IEntity destination)
RplId FindDestinationId(IEntity entity)
void FastTravel()
string GetDestinationName()
string m_sDestinationName
string m_sDestinationType
SCR_FastTravelComponentClass m_PlayerController
bool m_bIsDestinationValid
void SetNextTransportTimestamp(WorldTimestamp timestamp)
IEntity GetEntityByDestinationId(RplId id)
void Teleport()
RplId m_iDestinationId
vector m_vTeleportCoordinates
WorldTimestamp GetNextTransportTimestamp()
void RpcDo_FastTravel(vector position)
WorldTimestamp m_fNextTravelAvailableAt
void ~SCR_FastTravelComponent()
void OnSelectionChanged(MapItem item)
int m_iCooldown
void FadeOut()
bool m_bDisableAfterUse
void DisableAfterUse()
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
proto external Managed FindComponent(typename typeName)
Definition Math.c:13
Main replication API.
Definition Replication.c:14
Replication item identifier.
Definition RplId.c:14
static SCR_GadgetManagerComponent GetGadgetManager(IEntity entity)
void RemoveHeldGadget()
Remove gadget held in hand.
static bool TeleportLocalPlayer(vector worldPosition, SCR_EPlayerTeleportedReason teleportReason=SCR_EPlayerTeleportedReason.DEFAULT)
Definition Functions.c:1625
MapItem GetHoveredItem()
Get hovered item.
bool IsOpen()
Check if the map is opened.
static ScriptInvokerBase< MapItemInvoker > GetOnSelectionChanged()
Get on selection changed invoker.
static SCR_MapEntity GetMapInstance()
Get map entity instance.
BaseInfoDisplay GetEffect(typename effectType)
Return the effect is it exist.
static SCR_ScreenEffectsManager GetScreenEffectsDisplay()
Static GetScreenEffectsDisplay will return this regardless of whether SCR_ScreenEffectsManager is run...
static float GetTerrainY(vector pos, BaseWorld world=null, bool noUnderwater=false, TraceParam trace=null)
proto external GenericEntity GetOwner()
Get owner entity.
override void EOnInit(IEntity owner)
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
EntityEvent
Various entity events.
Definition EntityEvent.c:14
void RplRpc(RplChannel channel, RplRcver rcver, RplCondition condition=RplCondition.None, string customConditionName="")
Definition EnNetwork.c:95
RplCondition
Conditional replication rule. Fine grained selection of receivers.
RplRcver
Definition RplRcver.c:59
RplChannel
Communication channel. Reliable is guaranteed to be delivered. Unreliable not.
Definition RplChannel.c:14