Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
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.")]
2 class SCR_FastTravelComponentClass : ScriptComponentClass
3 {
4 }
5 
6 class 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 
17  protected vector m_vTeleportCoordinates;
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)]
29  protected WorldTimestamp m_fNextTravelAvailableAt;
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  {
53  m_iDestinationId = id;
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  {
110  SCR_MapEntity mapEntity = SCR_MapEntity.GetMapInstance();
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  {
137  SCR_GadgetManagerComponent comp = SCR_GadgetManagerComponent.GetGadgetManager(player);
138 
139  if (comp)
140  comp.RemoveHeldGadget();
141  }
142 
143  SetDestination(id, string.Empty);
144  FastTravel();
145  }
146 
147  //------------------------------------------------------------------------------------------------
151  RplId FindDestinationId(IEntity entity)
152  {
153  RplId id = Replication.FindId(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  //------------------------------------------------------------------------------------------------
168  void FastTravel()
169  {
170  if (!m_PlayerController)
171  return;
172 
174  }
175 
176  //------------------------------------------------------------------------------------------------
177  [RplRpc(RplChannel.Reliable, RplRcver.Server)]
178  protected void RpcAsk_FastTravel(RplId destinationId)
179  {
180  if (!m_PlayerController)
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  //------------------------------------------------------------------------------------------------
206  IEntity GetEntityByDestinationId(RplId id)
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  //------------------------------------------------------------------------------------------------
238  void SetNextTransportTimestamp(WorldTimestamp timestamp)
239  {
240  m_fNextTravelAvailableAt = timestamp;
241  Replication.BumpMe();
242  }
243 
244  //------------------------------------------------------------------------------------------------
245  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
246  protected void RpcDo_FastTravel(vector position)
247  {
248  if (m_bDisableAfterUse)
249  ToggleMapDestinationSelection(false);
250 
252 
253  // Wait for map to close and fade from black
254  GetGame().GetCallqueue().CallLater(FadeOut, CLOSE_MAP_DELAY * 1000);
255  }
256 
257  //------------------------------------------------------------------------------------------------
258  protected void FadeOut()
259  {
261  fade.FadeOutEffect(true, FADE_DURATION);
262 
263  // Wait for fade to black
264  GetGame().GetCallqueue().CallLater(Teleport, (FADE_DURATION + BLACKSCREEN_DURATION) * 1000);
265  }
266 
267  //------------------------------------------------------------------------------------------------
268  protected void Teleport()
269  {
270  SCR_ChimeraCharacter player = SCR_ChimeraCharacter.Cast(m_PlayerController.GetControlledEntity());
271 
272  if (player && !player.IsInVehicle() && !player.GetCharacterController().IsDead())
273  {
274  SCR_Global.TeleportLocalPlayer(m_vTeleportCoordinates, SCR_EPlayerTeleportedReason.FAST_TRAVEL);
275  SCR_NotificationsComponent.SendLocal(ENotification.FASTTRAVEL_DONE);
276  }
277 
279  fade.FadeOutEffect(false, FADE_DURATION);
280  }
281 
282  //------------------------------------------------------------------------------------------------
285  protected vector CalculateDestinationVector(notnull IEntity destination)
286  {
287  return CalculateDestinationVector(destination.GetOrigin());
288  }
289 
290  //------------------------------------------------------------------------------------------------
291  protected vector CalculateDestinationVector(vector targetOrigin)
292  {
293  IEntity player = m_PlayerController.GetControlledEntity();
294 
295  if (!player)
296  return vector.Zero;
297 
298  vector playerOrigin = m_PlayerController.GetControlledEntity().GetOrigin();
299  vector newPlayerOrigin;
300 
301  newPlayerOrigin[1] = playerOrigin[1];
302 
303  float angle = Math.Atan2(playerOrigin[2] - targetOrigin[2], playerOrigin[0] - targetOrigin[0]);
304  newPlayerOrigin[0] = targetOrigin[0] + m_iDistanceToTarget * Math.Cos(angle);
305  newPlayerOrigin[2] = targetOrigin[2] + m_iDistanceToTarget * Math.Sin(angle);
306  newPlayerOrigin[1] = SCR_TerrainHelper.GetTerrainY(newPlayerOrigin);
307 
308  SCR_WorldTools.FindEmptyTerrainPosition(newPlayerOrigin, newPlayerOrigin, SPAWNING_RADIUS);
309 
310  return newPlayerOrigin;
311  }
312 
313  //------------------------------------------------------------------------------------------------
314  override void OnPostInit(IEntity owner)
315  {
316  super.OnPostInit(owner);
317  SetEventMask(owner, EntityEvent.INIT);
318  }
319 
320  //------------------------------------------------------------------------------------------------
321  override void EOnInit(IEntity owner)
322  {
323  m_PlayerController = PlayerController.Cast(owner);
324 
325  if (!m_PlayerController)
326  Print("SCR_FastTravelComponent must be attached to PlayerController!", LogLevel.ERROR);
327  }
328 
329  //------------------------------------------------------------------------------------------------
330  // destructor
332  {
333  SCR_MapEntity.GetOnSelectionChanged().Remove(OnSelectionChanged);
334  }
335 }
FastTravel
void FastTravel()
Definition: SCR_FastTravelComponent.c:168
SCR_TerrainHelper
Definition: SCR_TerrainHelper.c:1
SCR_EPlayerTeleportedReason
SCR_EPlayerTeleportedReason
Definition: SCR_PlayerTeleportedFeedbackComponent.c:50
ChimeraWorld
Definition: ChimeraWorld.c:12
m_sDestinationName
protected string m_sDestinationName
Definition: SCR_FastTravelComponent.c:12
DisableAfterUse
void DisableAfterUse()
Definition: SCR_FastTravelComponent.c:74
m_bIsDestinationValid
protected bool m_bIsDestinationValid
Definition: SCR_FastTravelComponent.c:14
ServerSanityCheck
protected bool ServerSanityCheck(notnull IEntity target)
Definition: SCR_FastTravelComponent.c:229
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
m_fNextTravelAvailableAt
protected WorldTimestamp m_fNextTravelAvailableAt
Definition: SCR_FastTravelComponent.c:29
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
SCR_FadeInOutEffect
Definition: SCR_FadeInOutEffect.c:1
m_sDestinationType
protected string m_sDestinationType
Definition: SCR_FastTravelComponent.c:25
RplProp
SCR_RplTestEntityClass RplProp
Used for handling entity spawning requests for SCR_CatalogEntitySpawnerComponent and inherited classe...
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_GadgetManagerComponent
Definition: SCR_GadgetManagerComponent.c:138
FadeOut
protected void FadeOut()
Definition: SCR_FastTravelComponent.c:258
OnSelectionChanged
void OnSelectionChanged(MapItem item)
Definition: SCR_FastTravelComponent.c:108
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
SCR_WorldTools
Definition: SCR_WorldTools.c:1
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
m_iDistanceToTarget
protected int m_iDistanceToTarget
Definition: SCR_FastTravelComponent.c:22
ENotification
ENotification
Definition: ENotification.c:4
Attribute
typedef Attribute
Post-process effect of scripted camera.
MapItem
Definition: MapItem.c:12
OnPostInit
override void OnPostInit(IEntity owner)
Called on PostInit when all components are added.
Definition: SCR_FastTravelComponent.c:314
CalculateDestinationVector
protected vector CalculateDestinationVector(notnull IEntity destination)
Definition: SCR_FastTravelComponent.c:285
FindDestinationId
RplId FindDestinationId(IEntity entity)
Definition: SCR_FastTravelComponent.c:151
SCR_MapEntity
Map entity.
Definition: SCR_MapEntity.c:20
Teleport
protected void Teleport()
Definition: SCR_FastTravelComponent.c:268
m_vTeleportCoordinates
protected vector m_vTeleportCoordinates
Definition: SCR_FastTravelComponent.c:17
EOnInit
override void EOnInit(IEntity owner)
Definition: SCR_FastTravelComponent.c:321
GetEntityByDestinationId
IEntity GetEntityByDestinationId(RplId id)
Definition: SCR_FastTravelComponent.c:206
SCR_Global
Definition: Functions.c:6
RpcDo_FastTravel
protected void RpcDo_FastTravel(vector position)
Definition: SCR_FastTravelComponent.c:246
SetNextTransportTimestamp
void SetNextTransportTimestamp(WorldTimestamp timestamp)
Definition: SCR_FastTravelComponent.c:238
GetHUDManager
SCR_HUDManagerComponent GetHUDManager()
Definition: game.c:1196
m_iDestinationId
protected RplId m_iDestinationId
Definition: SCR_FastTravelComponent.c:10
m_bDisableAfterUse
protected bool m_bDisableAfterUse
Definition: SCR_FastTravelComponent.c:15
m_iCooldown
protected int m_iCooldown
Definition: SCR_FastTravelComponent.c:19
m_PlayerController
SCR_FastTravelComponentClass m_PlayerController
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
IsValid
override bool IsValid(IEntity actionOwner, IEntity actionUser, SCR_BaseUseSupportStationAction action, vector actionPosition, out ESupportStationReasonInvalid reasonInvalid, out int supplyCost)
Definition: SCR_BaseDamageHealSupportStationComponent.c:98
GetCooldown
int GetCooldown()
Definition: SCR_FastTravelComponent.c:67
RpcAsk_FastTravel
protected void RpcAsk_FastTravel(RplId destinationId)
Definition: SCR_FastTravelComponent.c:178
position
vector position
Definition: SCR_DestructibleTreeV2.c:30
SetDestination
void SetDestination(RplId id, string name)
Destination must be a replicated item with valid RplId.
Definition: SCR_FastTravelComponent.c:51
SCR_FastTravelComponentClass
Definition: SCR_FastTravelComponent.c:2
~SCR_FastTravelComponent
void ~SCR_FastTravelComponent()
Definition: SCR_FastTravelComponent.c:331
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180
GetDestinationName
string GetDestinationName()
Definition: SCR_FastTravelComponent.c:60