Skip to content

Commit 7457b22

Browse files
authored
Basic Aurora timer syncing (#1033)
* basic aurora timer syncing * requested changes
1 parent 459a662 commit 7457b22

File tree

7 files changed

+86
-17
lines changed

7 files changed

+86
-17
lines changed
+45-12
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,57 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Timers;
34
using NitroxModel.Logger;
45
using NitroxModel.Packets;
6+
using NitroxServer.GameLogic.Bases;
57

68
namespace NitroxServer.GameLogic
79
{
810
public class EventTriggerer
911
{
10-
PlayerManager playerManager;
11-
public EventTriggerer(PlayerManager playerManager)
12+
private PlayerManager playerManager;
13+
private Stopwatch stopWatch;
14+
public double ElapsedTime;
15+
public double AuroraExplosionTime;
16+
public EventTriggerer(PlayerManager playerManager, double elapsedTime, double? auroraExplosionTime)
1217
{
1318
this.playerManager = playerManager;
14-
SetupEventTimers();
19+
SetupEventTimers(elapsedTime, auroraExplosionTime);
1520
}
1621

17-
public void SetupEventTimers()
22+
private void SetupEventTimers(double elapsedTime, double? auroraExplosionTime)
1823
{
1924
// eventually this should be on a better timer so it can be saved, paused, etc
2025
Log.Debug("Event Triggerer started!");
21-
double auroraTimer = RandomNumber(2.3d, 4d) * 1200d * 1000d; //Time.deltaTime returns seconds so we need to multiply 1000
22-
CreateTimer(auroraTimer * 0.2d, StoryEventType.PDA, "Story_AuroraWarning1");
23-
CreateTimer(auroraTimer * 0.5d, StoryEventType.PDA, "Story_AuroraWarning2");
24-
CreateTimer(auroraTimer * 0.8d, StoryEventType.PDA, "Story_AuroraWarning3");
25-
CreateTimer(auroraTimer, StoryEventType.PDA, "Story_AuroraWarning4");
26-
CreateTimer(auroraTimer + 24000, StoryEventType.EXTRA, "Story_AuroraExplosion");
26+
27+
ElapsedTime = elapsedTime;
28+
if (auroraExplosionTime.HasValue)
29+
{
30+
AuroraExplosionTime = auroraExplosionTime.Value;
31+
}
32+
else
33+
{
34+
AuroraExplosionTime = RandomNumber(2.3d, 4d) * 1200d * 1000d; //Time.deltaTime returns seconds so we need to multiply 1000
35+
}
36+
37+
CreateTimer(AuroraExplosionTime * 0.2d - ElapsedTime, StoryEventType.PDA, "Story_AuroraWarning1");
38+
CreateTimer(AuroraExplosionTime * 0.5d - ElapsedTime, StoryEventType.PDA, "Story_AuroraWarning2");
39+
CreateTimer(AuroraExplosionTime * 0.8d - ElapsedTime, StoryEventType.PDA, "Story_AuroraWarning3");
40+
CreateTimer(AuroraExplosionTime - ElapsedTime, StoryEventType.PDA, "Story_AuroraWarning4");
41+
CreateTimer(AuroraExplosionTime + 24000 - ElapsedTime, StoryEventType.EXTRA, "Story_AuroraExplosion");
42+
//like the timers, except we can see how much time has passed
43+
stopWatch = new Stopwatch();
44+
stopWatch.Start();
2745
}
2846

29-
public Timer CreateTimer(double time, StoryEventType eventType, string key)
47+
private Timer CreateTimer(double time, StoryEventType eventType, string key)
3048
{
49+
//if timeOffset goes past the time
50+
if (time <= 0)
51+
{
52+
return null;
53+
}
54+
3155
Timer timer = new Timer();
3256
timer.Elapsed += delegate
3357
{
@@ -40,10 +64,19 @@ public Timer CreateTimer(double time, StoryEventType eventType, string key)
4064
return timer;
4165
}
4266

43-
public double RandomNumber(double min, double max)
67+
private double RandomNumber(double min, double max)
4468
{
4569
Random random = new Random();
4670
return random.NextDouble() * (max - min) + min;
4771
}
72+
73+
public double GetRealElapsedTime()
74+
{
75+
if (stopWatch == null)
76+
{
77+
return ElapsedTime;
78+
}
79+
return stopWatch.ElapsedMilliseconds + ElapsedTime;
80+
}
4881
}
4982
}

NitroxServer/GameLogic/GameData.cs

+3
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ public class GameData
1313

1414
[ProtoMember(2)]
1515
public StoryGoalData StoryGoals { get; set; }
16+
17+
[ProtoMember(3)]
18+
public StoryTimingData StoryTiming { get; set; }
1619
}
1720
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using ProtoBufNet;
3+
4+
namespace NitroxServer.GameLogic
5+
{
6+
[Serializable]
7+
[ProtoContract]
8+
public class StoryTimingData
9+
{
10+
[ProtoMember(1)]
11+
public double ElapsedTime { get; set; }
12+
13+
[ProtoMember(2)]
14+
public double? AuroraExplosionTime { get; set; }
15+
16+
public static StoryTimingData From(EventTriggerer eventTriggerer)
17+
{
18+
StoryTimingData inst = new StoryTimingData();
19+
inst.ElapsedTime = eventTriggerer.GetRealElapsedTime();
20+
inst.AuroraExplosionTime = eventTriggerer.AuroraExplosionTime;
21+
return inst;
22+
}
23+
}
24+
}

NitroxServer/NitroxServer.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@
328328
<Compile Include="GameLogic\Entities\Spawning\EntitySpawnPointFactory.cs" />
329329
<Compile Include="GameLogic\EscapePodData.cs" />
330330
<Compile Include="GameLogic\Items\InventoryManager.cs" />
331+
<Compile Include="GameLogic\StoryTimingData.cs" />
331332
<Compile Include="GameLogic\Unlockables\StoryGoalData.cs" />
332333
<Compile Include="GameLogic\Vehicles\VehicleManager.cs" />
333334
<Compile Include="Serialization\Resources\Datastructures\AssetIdentifier.cs" />

NitroxServer/Serialization/World/PersistedWorldData.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NitroxServer.GameLogic.Bases;
1+
using NitroxServer.GameLogic;
2+
using NitroxServer.GameLogic.Bases;
23
using NitroxServer.GameLogic.Players;
34
using ProtoBufNet;
45

@@ -15,7 +16,7 @@ public class PersistedWorldData
1516

1617
[ProtoMember(3)]
1718
public PlayerData PlayerData { get; set; }
18-
19+
1920
public bool IsValid()
2021
{
2122
return (WorldData.IsValid()) &&

NitroxServer/Serialization/World/WorldData.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public class WorldData
3636
[ProtoMember(7)]
3737
public EscapePodData EscapePodData { get; set; }
3838

39+
[ProtoMember(8)]
40+
public StoryTimingData StoryTimingData { get; set; }
41+
3942
public bool IsValid()
4043
{
4144
return (ParsedBatchCells != null) && // Always returns false on empty saves
@@ -45,7 +48,8 @@ public bool IsValid()
4548
(GameData != null) &&
4649
(EntityData != null) &&
4750
(EntityData.Entities.Count > 0) &&
48-
(EscapePodData != null);
51+
(EscapePodData != null) &&
52+
(StoryTimingData != null);
4953
}
5054
}
5155
}

NitroxServer/Serialization/World/WorldPersistence.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void Save(World world)
4444
persistedData.WorldData.InventoryData = InventoryData.From(world.InventoryManager.GetAllInventoryItems(), world.InventoryManager.GetAllStorageSlotItems());
4545
persistedData.PlayerData = PlayerData.From(world.PlayerManager.GetAllPlayers());
4646
persistedData.WorldData.GameData = world.GameData;
47+
persistedData.WorldData.StoryTimingData = StoryTimingData.From(world.EventTriggerer);
4748
persistedData.WorldData.EscapePodData = EscapePodData.From(world.EscapePodManager.GetEscapePods());
4849

4950
if (!Directory.Exists(config.SaveName))
@@ -136,6 +137,7 @@ private Optional<World> LoadFromFile()
136137
persistedData.WorldData.GameData,
137138
persistedData.WorldData.ParsedBatchCells,
138139
persistedData.WorldData.EscapePodData.EscapePods,
140+
persistedData.WorldData.StoryTimingData,
139141
config.GameMode);
140142

141143
return Optional.Of(world);
@@ -171,7 +173,7 @@ private World CreateFreshWorld()
171173
new List<VehicleModel>(), new List<Player>(), new List<ItemData>(),
172174
new List<ItemData>(),
173175
new GameData() { PDAState = new PDAStateData(), StoryGoals = new StoryGoalData() },
174-
new List<Int3>(), new List<EscapePodModel>(), config.GameMode);
176+
new List<Int3>(), new List<EscapePodModel>(), new StoryTimingData(), config.GameMode);
175177
}
176178

177179
private World CreateWorld(DateTime serverStartTime,
@@ -185,6 +187,7 @@ private World CreateWorld(DateTime serverStartTime,
185187
GameData gameData,
186188
List<Int3> parsedBatchCells,
187189
List<EscapePodModel> escapePods,
190+
StoryTimingData storyTimingData,
188191
string gameMode)
189192
{
190193
World world = new World();
@@ -193,7 +196,7 @@ private World CreateWorld(DateTime serverStartTime,
193196

194197
world.SimulationOwnershipData = new SimulationOwnershipData();
195198
world.PlayerManager = new PlayerManager(players, config);
196-
world.EventTriggerer = new EventTriggerer(world.PlayerManager);
199+
world.EventTriggerer = new EventTriggerer(world.PlayerManager, storyTimingData.ElapsedTime, storyTimingData.AuroraExplosionTime);
197200
world.BaseManager = new BaseManager(partiallyConstructedPieces, completedBasePieceHistory);
198201
world.InventoryManager = new InventoryManager(inventoryItems, storageSlotItems);
199202
world.VehicleManager = new VehicleManager(vehicles, world.InventoryManager);

0 commit comments

Comments
 (0)