29 protected RplComponent m_RplComponent;
31 [
Attribute(
"0",
desc:
"Find empty position for spawning within given radius. When none is found, entity position will be used.")]
32 protected float m_fSpawnRadius;
34 [
Attribute(
"Red", UIWidgets.EditBox,
"Determines which faction can spawn on this spawn point."),
RplProp(onRplName:
"OnSetFactionKey")]
35 protected string m_sFaction;
38 protected bool m_bShowInDeployMapOnly;
40 [
Attribute(
"0",
desc:
"Use custom timer when deploying on this spawn point. Takes the remaining respawn time from SCR_TimedSpawnPointComponent")]
41 protected bool m_bTimedSpawnPoint;
49 [
Attribute(
"0",
desc:
"Allow usage of Spawn Positions in range")]
50 protected bool m_bUseNearbySpawnPositions;
52 [
Attribute(
"100",
desc:
"Spawn position detection radius, in metres")]
53 protected float m_fSpawnPositionUsageRange;
55 [
Attribute(
"0",
desc:
"Additional respawn time (in seconds) when spawning on this spawn point"),
RplProp()]
56 protected float m_fRespawnTime;
59 private static ref array<SCR_SpawnPoint> m_aSpawnPoints =
new array<SCR_SpawnPoint>();
61 static ref ScriptInvoker Event_OnSpawnPointCountChanged =
new ScriptInvoker();
62 static ref ScriptInvoker Event_SpawnPointFactionAssigned =
new ScriptInvoker();
68 protected string m_sSpawnPointName;
73 protected ref array<SCR_Position> m_aChildren = {};
79 protected ref set<int> m_ReservationLocks =
new set<int>();
84 protected bool m_bSpawnPointEnabled;
87 bool IsSpawnPointVisibleForPlayer(
int pid)
93 bool IsSpawnPointEnabled()
95 return m_bSpawnPointEnabled;
99 void SetSpawnPointEnabled_S(
bool enabled)
101 if (enabled == m_bSpawnPointEnabled)
104 m_bSpawnPointEnabled = enabled;
106 Replication.BumpMe();
110 protected void OnSetEnabled()
112 if (m_OnSetSpawnPointEnabled)
113 m_OnSetSpawnPointEnabled.Invoke(m_bSpawnPointEnabled);
119 if (!m_OnSetSpawnPointEnabled)
122 return m_OnSetSpawnPointEnabled;
128 if (!s_OnSpawnPointFinalizeSpawn)
131 return s_OnSpawnPointFinalizeSpawn;
135 float GetRespawnTime()
137 return m_fRespawnTime;
141 void SetRespawnTime(
float time)
143 m_fRespawnTime = time;
144 Replication.BumpMe();
159 if (IsSpawnPointEnabled())
177 bool IsReservedFor_S(
int playerId)
179 return m_ReservationLocks.Contains(playerId);
189 bool ReserveFor_S(
int playerId)
191 #ifdef _ENABLE_RESPAWN_LOGS
192 PrintFormat(
"%1::ReserveFor_S(playerId: %2, pointId: %3)", Type().ToString(), playerId, GetRplId());
195 if (!m_ReservationLocks.Insert(playerId))
197 Debug.Error(
string.Format(
"SCR_SpawnPoint reservation for playerId: %1 failed!", playerId));
210 void ClearReservationFor_S(
int playerId)
212 #ifdef _ENABLE_RESPAWN_LOGS
213 PrintFormat(
"%1::ClearReservationFor_S(playerId: %2, pointId: %3)", Type().ToString(), playerId, GetRplId());
216 int index = m_ReservationLocks.Find(playerId);
219 m_ReservationLocks.Remove(
index);
227 float GetSpawnRadius()
229 return m_fSpawnRadius;
234 void SetSpawnRadius(
float radius)
236 m_fSpawnRadius = radius;
240 static void ShowSpawnPointDescriptors(
bool show,
Faction faction)
245 string factionKey =
string.Empty;
247 factionKey = faction.GetFactionKey();
254 auto mapDescriptor = SCR_MapDescriptorComponent.Cast(spawnPoint.FindComponent(SCR_MapDescriptorComponent));
258 bool visible = show && !factionKey.IsEmpty() && spawnPoint.GetFactionKey() == factionKey;
259 if (mapDescriptor.Item())
260 mapDescriptor.Item().SetVisible(visible);
271 return RplId.Invalid();
282 Managed instance = Replication.FindItem(
id);
286 RplComponent rplComponent = RplComponent.Cast(instance);
294 protected bool GetEmptyPositionAndRotationInRange(out vector pos, out vector rot)
296 SCR_SpawnPositionComponentManager spawnPosManagerComponent = SCR_SpawnPositionComponentManager.GetInstance();
297 if (!spawnPosManagerComponent)
300 array<SCR_SpawnPositionComponent> positions = {};
301 int count = spawnPosManagerComponent.GetSpawnPositionsInRange(
GetOrigin(), m_fSpawnPositionUsageRange, positions);
305 SCR_SpawnPositionComponent
position;
307 while (!positions.IsEmpty())
309 position = positions.GetRandomElement();
313 pos =
position.GetOwner().GetOrigin();
314 rot =
position.GetOwner().GetAngles();
328 void SetUseNearbySpawnPositions(
bool use)
330 m_bUseNearbySpawnPositions = use;
334 void GetPositionAndRotation(out vector pos, out vector rot)
336 if (m_bUseNearbySpawnPositions)
338 if (GetEmptyPositionAndRotationInRange(pos, rot))
359 if (spawnPointIndex >= 0 && spawnPointIndex < m_aSpawnPoints.Count())
360 return m_aSpawnPoints[spawnPointIndex];
369 return m_aSpawnPoints.Find(spawnPoint);
373 bool IsSpawnPointActive()
382 static int CountSpawnPoints()
384 return m_aSpawnPoints.Count();
388 bool GetVisibleInDeployMapOnly()
390 return m_bShowInDeployMapOnly;
394 void SetVisibleInDeployMapOnly(
bool visible)
396 m_bShowInDeployMapOnly = visible;
400 protected void ApplyFactionChange(FactionAffiliationComponent owner,
Faction previousFaction,
Faction newFaction)
402 SetFaction(newFaction);
406 void SetFaction(
Faction faction)
409 SetFactionKey(
string.Empty);
411 SetFactionKey(faction.GetFactionKey());
415 void SetFactionKey(
string factionKey)
417 if (factionKey == m_sFaction)
420 m_sFaction = factionKey;
422 Replication.BumpMe();
424 protected void OnSetFactionKey()
426 Event_SpawnPointFactionAssigned.Invoke(
this);
430 void SetSpawnPositionRange(
float range)
432 m_fSpawnPositionUsageRange = range;
436 float GetSpawnPositionRange()
438 return m_fSpawnPositionUsageRange;
442 array<SCR_Position> GetChildSpawnPoints()
449 string GetFactionKey()
458 private bool GetIsInRange(ChimeraCharacter character,
float range)
460 return character && vector.DistanceSq(character.GetOrigin(),
GetOrigin()) < range*range;
465 static array<SCR_SpawnPoint> GetSpawnPoints()
467 return m_aSpawnPoints;
474 if (!m_aSpawnPoints || m_aSpawnPoints.IsEmpty())
477 return m_aSpawnPoints.GetRandomElement();
482 static SCR_SpawnPoint GetRandomSpawnPoint(SCR_ChimeraCharacter character)
487 return GetRandomSpawnPointForFaction(character.GetFactionKey());
492 static array<SCR_SpawnPoint> GetSpawnPointsForPlayer(SCR_ChimeraCharacter character)
494 string factionKey =
string.Empty;
496 factionKey = character.GetFactionKey();
498 return GetSpawnPointsForFaction(factionKey);
504 static array<SCR_SpawnPoint> GetSpawnPointsForFaction(
string factionKey)
506 array<SCR_SpawnPoint> factionSpawnPoints =
new array<SCR_SpawnPoint>();
507 if (factionKey.IsEmpty())
508 return factionSpawnPoints;
510 array<SCR_SpawnPoint> spawnPoints = GetSpawnPoints();
513 if (spawnPoint && spawnPoint.GetFactionKey() == factionKey)
514 factionSpawnPoints.Insert(spawnPoint);
516 return factionSpawnPoints;
522 static int GetSpawnPointCountForFaction(
string factionKey)
524 if (factionKey.IsEmpty())
530 if (!spawnPoint)
continue;
531 if (spawnPoint.GetFactionKey() == factionKey)
539 static SCR_SpawnPoint GetRandomSpawnPointForFaction(
string factionKey)
541 array<SCR_SpawnPoint> spawnPoints = GetSpawnPointsForFaction(factionKey);
542 if (!spawnPoints.IsEmpty())
543 return spawnPoints.GetRandomElement();
586 string GetSpawnPointName()
589 return GetInfo().GetName();
591 return m_sSpawnPointName;
597 void SetSpawnPointName(
string name)
599 m_sSpawnPointName = name;
600 Replication.BumpMe();
601 OnSpawnPointNameChanged.Invoke(GetRplId(), m_sSpawnPointName);
607 return m_bTimedSpawnPoint;
611 void SetIsTimed(
bool isTimed)
613 m_bTimedSpawnPoint = isTimed;
624 override void SetColorAndText()
629 FactionManager factionManager =
GetGame().GetFactionManager();
632 Faction faction = factionManager.GetFactionByKey(m_sFaction);
635 m_iColor = faction.GetFactionColor().PackToInt();
659 bool PrepareSpawnedEntity_S(SCR_SpawnRequestComponent requestComponent,
SCR_SpawnData data, IEntity entity)
661 if (!IsSpawnPointEnabled())
690 bool CanFinalizeSpawn_S(SCR_SpawnRequestComponent requestComponent,
SCR_SpawnData data, IEntity entity)
692 return IsSpawnPointEnabled();
701 void OnFinalizeSpawnDone_S(SCR_SpawnRequestComponent requestComponent,
SCR_SpawnData data, IEntity entity)
703 if (s_OnSpawnPointFinalizeSpawn)
704 s_OnSpawnPointFinalizeSpawn.Invoke(requestComponent,
data, entity);
708 override void EOnInit(IEntity owner)
710 super.EOnInit(owner);
711 if (!
GetGame().GetWorldEntity())
721 child = child.GetSibling();
724 InitFactionAffiliation(owner);
727 m_aSpawnPoints.Insert(
this);
729 Event_SpawnPointAdded.Invoke(
this);
731 ClearFlags(EntityFlags.ACTIVE);
735 protected void InitFactionAffiliation(IEntity owner)
743 m_sFaction = faction.GetFactionKey();
748 override bool RplSave(ScriptBitWriter writer)
750 writer.WriteString(m_sSpawnPointName);
756 override protected bool RplLoad(ScriptBitReader reader)
759 Event_SpawnPointAdded.Invoke(
this);
765 reader.ReadString(m_sSpawnPointName);
773 SetEventMask(EntityEvent.INIT);
774 SetFlags(EntityFlags.STATIC,
true);
783 m_aSpawnPoints.RemoveItem(
this);
788 Event_SpawnPointRemoved.Invoke(
this);