-
Notifications
You must be signed in to change notification settings - Fork 20
/
LobbyVisitManager.cs
191 lines (156 loc) · 7.05 KB
/
LobbyVisitManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Celeste.Mod.CollabUtils2 {
public class LobbyVisitManager {
/// <summary>
/// Each of the points the player has visited and thus generated circular snapshots.
/// These points are in 8x8 pixel tile offsets from the top left of the map.
/// </summary>
public List<VisitedPoint> VisitedPoints { get; } = new List<VisitedPoint>();
public List<string> ActivatedWarps { get; } = new List<string>();
public bool VisitedAll { get; private set; }
public const int EXPLORATION_RADIUS = 20;
private const ushort currentVersion = 1;
public string SID { get; }
public string Room { get; }
private static string GetKey(string sid, string room = null) =>
string.IsNullOrWhiteSpace(room) ? sid : $"{sid}.{room}";
public string Key => GetKey(SID, Room);
public bool MatchesKey(string sid, string room = null) => Key == GetKey(sid, room);
public LobbyVisitManager(string sid, string room = null) {
SID = sid;
Room = room;
Load();
}
public void VisitAll(bool shouldSave = true) {
VisitedPoints.Clear();
VisitedAll = true;
if (shouldSave) {
Save();
}
}
public void Reset(bool shouldSave = true) {
VisitedPoints.Clear();
ActivatedWarps.Clear();
VisitedAll = false;
if (shouldSave) {
Save();
}
}
public void Save() {
try {
using (var stream = new MemoryStream())
using (var writer = new BinaryWriter(stream)) {
// write version number
writer.Write(currentVersion);
// write whether we've visited everything
writer.Write(VisitedAll);
// write points if we haven't visited them all
if (!VisitedAll) {
writer.Write((uint) VisitedPoints.Count);
foreach (var v in VisitedPoints) {
writer.Write((short) v.Point.X);
writer.Write((short) v.Point.Y);
}
}
// write activated warps
writer.Write((uint) ActivatedWarps.Count);
foreach (var w in ActivatedWarps) {
writer.Write(w);
}
// store it
CollabModule.Instance.SaveData.VisitedLobbyPositions[Key] = Convert.ToBase64String(stream.ToArray());
}
} catch (Exception) {
Logger.Log(LogLevel.Error, "CollabUtils2/LobbyVisitManager", "Save: Error trying to serialise visited points.");
}
}
private void Load() {
VisitedPoints.Clear();
ActivatedWarps.Clear();
VisitedAll = false;
if (!CollabModule.Instance.SaveData.VisitedLobbyPositions.TryGetValue(Key, out var value)) {
return;
}
try {
var visitedAll = false;
var visitedPoints = new List<VisitedPoint>();
var activatedWarps = new List<string>();
var bytes = Convert.FromBase64String(value);
using (var stream = new MemoryStream(bytes))
using (var reader = new BinaryReader(stream)) {
// check a version number so we can clear out bad data later
var version = reader.ReadUInt16();
if (version != currentVersion) {
Logger.Log(LogLevel.Warn, "CollabUtils2/LobbyVisitManager", "Load: Wrong version found, clearing stored data instead.");
CollabModule.Instance.SaveData.VisitedLobbyPositions.Remove(Key);
return;
}
// read whether we've visited everything
visitedAll = reader.ReadBoolean();
// if we haven't visited everything, read all the points
if (!visitedAll) {
var visitedCount = reader.ReadUInt32();
for (int i = 0; i < visitedCount; i++) {
var x = reader.ReadInt16();
var y = reader.ReadInt16();
visitedPoints.Add(new VisitedPoint(new Vector2(x, y)));
}
}
// read all the activated warps
var activatedCount = reader.ReadUInt32();
for (int i = 0; i < activatedCount; i++) {
var warpId = reader.ReadString();
activatedWarps.Add(warpId);
}
}
VisitedAll = visitedAll;
VisitedPoints.AddRange(visitedPoints);
ActivatedWarps.AddRange(activatedWarps);
} catch (Exception) {
Logger.Log(LogLevel.Error, "CollabUtils2/LobbyVisitManager", "Load: Error trying to deserialise visited points, clearing stored data instead.");
CollabModule.Instance.SaveData.VisitedLobbyPositions.Remove(Key);
}
}
public void ActivateWarp(string id, bool shouldSave = true) {
if (ActivatedWarps.Contains(id)) return;
ActivatedWarps.Add(id);
if (shouldSave) {
Save();
}
}
public void VisitPoint(Vector2 point, bool shouldSave = true) {
const float threshold = EXPLORATION_RADIUS / 2f;
// don't need to do anything if we've visited everywhere
if (VisitedAll) return;
var shouldGenerate = !VisitedPoints.Any();
var shouldSort = !shouldGenerate && (point - VisitedPoints.First().Point).LengthSquared() > threshold * threshold;
if (shouldSort) {
// update distances and sort
foreach (var vp in VisitedPoints) {
vp.DistanceSquared = (vp.Point - point).LengthSquared();
}
VisitedPoints.Sort((a, b) => Math.Sign(a.DistanceSquared - b.DistanceSquared));
// check whether we should generate after the sort
shouldGenerate = VisitedPoints.First().DistanceSquared > threshold * threshold;
}
if (shouldGenerate) {
VisitedPoints.Insert(0, new VisitedPoint(point, 0f));
if (shouldSave) {
Save();
}
}
}
public class VisitedPoint {
public Vector2 Point;
public float DistanceSquared;
public VisitedPoint(Vector2 point, float distanceSquared = float.MaxValue) {
Point = point;
DistanceSquared = distanceSquared;
}
}
}
}