-
Notifications
You must be signed in to change notification settings - Fork 1
/
MadelinePartyModule.cs
316 lines (278 loc) · 15 KB
/
MadelinePartyModule.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using Microsoft.Xna.Framework;
using Celeste;
using System;
using System.Linq;
using Celeste.Mod;
using MadelineParty.Multiplayer;
using MadelineParty.Multiplayer.General;
using System.Collections.Generic;
using Monocle;
using System.Runtime.CompilerServices;
using MadelineParty.SubHud;
using MonoMod.ModInterop;
using MadelineParty.Minigame;
using MadelineParty.Board;
using MadelineParty.Entities;
// TODO minigames for most bounces of oshiro, seekers, snowballs, etc...
[assembly: IgnoresAccessChecksTo("Celeste")]
namespace MadelineParty
{
public class MadelinePartyModule : EverestModule {
// Only one alive module instance can exist at any given time.
public static MadelinePartyModule Instance;
public static string START_ROOM = "Game_PlayerNumberSelect";
public Level level;
public MadelinePartyModule() {
Instance = this;
}
public override Type SettingsType => null;
public override Type SaveDataType => typeof(MadelinePartySaveData);
public static MadelinePartySaveData SaveData => (MadelinePartySaveData)Instance._SaveData;
// Set up any hooks, event handlers and your mod in general here.
// Load runs before Celeste itself has initialized properly.
public override void Load() {
typeof(AchievementHelperImports).ModInterop();
// Stuff that runs orig(self) always
/* ************************************************ */
Everest.Events.Level.OnLoadEntity += Level_OnLoadEntity;
Everest.Events.Level.OnLoadLevel += (level, playerIntro, isFromLoader) => {
BoardController.boardEntityData.Clear();
this.level = level;
};
DreamBlockRNGSyncer.Load();
MinigameInfinityTrigger.Load();
On.Celeste.LevelEnter.Go += (orig, session, fromSaveData) => {
if (IsSIDMadelineParty(session.Area.GetSID()) && !session.Level.Equals(START_ROOM)) {
session.Level = START_ROOM;
session.RespawnPoint = session.GetSpawnPoint(Vector2.Zero);
//Player player = level.Tracker.GetEntity<Player>();
//sendToStart(player);
}
orig(session, fromSaveData);
};
On.Celeste.Player.Update += Player_Update;
On.Celeste.Level.UnloadLevel += (orig, self) => {
if (IsSIDMadelineParty(self.Session.Area.GetSID())) {
TextMenu menu = self.Entities.FindFirst<TextMenu>();
if (menu != null) {
self.PauseMainMenuOpen = false;
menu.RemoveSelf();
self.Paused = false;
}
}
orig(self);
};
MinigameEntity.Load();
MinigameSwitchGatherer.Load();
BoardController.Load();
TiebreakerController.Load();
TextMenuPlus.Load();
SubHudLevelForwarder.Load();
BoardSelect.Load();
BlockCrystal.Load();
MultiplayerSingleton.Instance.RegisterHandler<Party>(HandleParty);
MultiplayerSingleton.Instance.RegisterHandler<MinigameEnd>(HandleMinigameEnd);
MultiplayerSingleton.Instance.RegisterHandler<MinigameStatus>(HandleMinigameStatus);
MultiplayerSingleton.Instance.RegisterHandler<RandomSeed>(HandleRandomSeed);
}
public static bool IsSIDMadelineParty(string sid) {
return sid.Equals("Brokemia/MadelineParty/madelineparty");
}
private float disconnectLeniency = 2f;
void Player_Update(On.Celeste.Player.orig_Update orig, Player self) {
Level l = self.SceneAs<Level>();
orig(self);
if (l != null && IsSIDMadelineParty(l.Session.Area.GetSID())) {
if (MultiplayerSingleton.Instance.BackendInstalled()) {
// If the player disconnects from a multiplayer game
if (GameData.Instance.playerNumber > 1 && !MultiplayerSingleton.Instance.BackendConnected()) {
disconnectLeniency -= Engine.RawDeltaTime;
if (disconnectLeniency < 0) {
Player player = level.Tracker.GetEntity<Player>();
sendToStart(player);
disconnectLeniency = 2f;
}
} else {
disconnectLeniency = 2f;
}
}
if (!l.Session.Level.Equals(START_ROOM) && (!l.Session.Level.Equals("Game_Lobby") || GameData.Instance.playerNumber == -1)) {
if (GameData.Instance.players.All((data) => data == null)) {
Player player = l.Tracker.GetEntity<Player>();
sendToStart(player);
} else {
// FIXME Use new Multiplayer
//foreach (uint id in GameData.Instance.celestenetIDs) {
// if (playerInStartRoom(id)) {
// Player player = l.Tracker.GetEntity<Player>();
// sendToStart(player);
// }
//}
}
}
if(l.Entities.AmountOf<MinigameFinishTrigger>() == 0) {
l.CanRetry = false;
}
}
}
//private bool playerInStartRoom(uint id) {
// if (CelesteNetClientModule.Instance?.Client?.Data != null && CelesteNetClientModule.Instance.Client.Data.TryGetRef(id, out DataPlayerState state)) {
// Console.WriteLine(state + " " + state.Level);
// return state?.Level?.Equals(START_ROOM) ?? true;
// }
// return false;
//}
private void sendToStart(Player p) {
level.OnEndOfFrame += delegate {
Leader.StoreStrawberries(p.Leader);
level.Remove(p);
level.UnloadLevel();
level.Session.Level = START_ROOM;
level.Session.RespawnPoint = level.GetSpawnPoint(new Vector2(level.Bounds.Left, level.Bounds.Top));
level.LoadLevel(Player.IntroTypes.None);
Leader.RestoreStrawberries(level.Tracker.GetEntity<Player>().Leader);
};
}
bool Level_OnLoadEntity(Level l, LevelData levelData, Vector2 offset, EntityData entityData) {
if (entityData.Name.StartsWith("madelineparty/", StringComparison.InvariantCulture)) {
switch (entityData.Name.Substring("madelineparty/".Length)) {
case "leftbutton":
l.Add(new LeftButton(entityData, offset));
return true;
case "rightbutton":
l.Add(new RightButton(entityData, offset));
return true;
case "boardcontroller":
l.Add(new BoardController(entityData));
return true;
case "playernumberselect":
l.Add(new PlayerNumberSelect(entityData, offset));
return true;
case "playerselecttrigger":
l.Add(new PlayerSelectTrigger(entityData, offset));
return true;
case "gamescoreboard":
l.Add(new GameScoreboard(entityData, offset));
return true;
case "minigamefinishtrigger":
l.Add(new MinigameFinishTrigger(entityData, offset));
return true;
case "playerrankingcontroller":
l.Add(new PlayerRankingController());
return true;
case "gameendcontroller":
l.Add(new GameEndController());
return true;
case "theominigamecontroller":
l.Add(new MinigameTheoMover(entityData, offset));
return true;
case "_board_space":
BoardController.boardEntityData.Add(entityData);
return true;
}
}
return false;
}
// Optional, do anything requiring either the Celeste or mod content here.
[Obsolete]
public override void LoadContent() {
MultiplayerSingleton.Instance.LoadContent();
BoardController.LoadContent();
BoardSelect.LoadContent();
}
// Unload the entirety of your mod's content, remove any event listeners and undo all hooks.
public override void Unload() {
Everest.Events.Level.OnLoadEntity -= Level_OnLoadEntity;
On.Celeste.Player.Update -= Player_Update;
MinigameSwitchGatherer.Unload();
TextMenuPlus.Unload();
SubHudLevelForwarder.Unload();
BoardSelect.Unload();
BlockCrystal.Unload();
}
private void HandleParty(MPData data) {
if(data is not Party party) return;
if (!IsSIDMadelineParty(level.Session.Area.GetSID())) return;
Logger.Log("MadelineParty", $"Recieved PartyData. My ID: ${MultiplayerSingleton.Instance.CurrentPlayerID()} Player ID: ${party.ID} Looking for party of size ${party.lookingForParty} to play mode ${party.desiredMode}");
if (party.lookingForParty == GameData.Instance.playerNumber // if they want the same party size
&& party.desiredMode.Equals(ModeManager.Instance.Mode) // and they want the same mode
&& party.version.Equals(Metadata.VersionString) // and our versions match
&& GameData.Instance.celestenetIDs.Count < GameData.Instance.playerNumber - 1 // and we aren't full up
&& !GameData.Instance.celestenetIDs.Contains(party.ID) // and they aren't in our party
&& party.ID != MultiplayerSingleton.Instance.CurrentPlayerID()) { // and they aren't us
string joinMsg = party.DisplayName + " has joined the party!";
// If they think they're the host and are broadcasting
if (party.respondingTo < 0 && party.partyHost) {
// Tell them that they aren't the host and are instead joining our party
MultiplayerSingleton.Instance.Send(new Party {
respondingTo = (int)party.ID,
desiredMode = ModeManager.Instance.Mode,
lookingForParty = (byte)GameData.Instance.playerNumber,
partyHost = GameData.Instance.celesteNetHost
});
GameData.Instance.celestenetIDs.Add(party.ID);
Logger.Log("MadelineParty", joinMsg);
MultiplayerSingleton.Instance.SendChat(joinMsg);
if (GameData.Instance.currentPlayerSelection != null) {
MultiplayerSingleton.Instance.Send(new Party {
respondingTo = (int)party.ID,
desiredMode = ModeManager.Instance.Mode,
playerSelectTrigger = GameData.Instance.currentPlayerSelection.playerID
});
}
} else if (party.respondingTo == MultiplayerSingleton.Instance.CurrentPlayerID()) {
GameData.Instance.celesteNetHost = false;
GameData.Instance.celestenetIDs.Add(party.ID);
Logger.Log("MadelineParty", joinMsg);
MultiplayerSingleton.Instance.SendChat(joinMsg);
}
}
// If the other player entered a player select trigger
if (party.playerSelectTrigger != -2 && GameData.Instance.celestenetIDs.Contains(party.ID) && (party.respondingTo < 0 || party.respondingTo == MultiplayerSingleton.Instance.CurrentPlayerID())) {
Logger.Log("MadelineParty", "Player ID: " + party.ID + " entered player select trigger " + party.playerSelectTrigger);
GameData.Instance.playerSelectTriggers[party.ID] = party.playerSelectTrigger;
if (GameData.Instance.currentPlayerSelection != null) {
// -1 so it doesn't count me as a player
int left = GameData.Instance.playerNumber - 1;
foreach (KeyValuePair<uint, int> kvp1 in GameData.Instance.playerSelectTriggers) {
// Check if another player is trying to choose the same spot
bool duplicate = false;
foreach (KeyValuePair<uint, int> kvp2 in GameData.Instance.playerSelectTriggers) {
duplicate |= (kvp2.Key != kvp1.Key && kvp2.Value == kvp1.Value);
}
if (!duplicate && kvp1.Value != -1 && kvp1.Value != GameData.Instance.currentPlayerSelection.playerID) {
left--;
}
}
if (left <= 0) {
GameData.Instance.currentPlayerSelection.AllTriggersOccupied();
}
}
}
}
private void HandleMinigameEnd(MPData data) {
if (data is not MinigameEnd end) return;
// If another player in our party has beaten a minigame
if (GameData.Instance.celestenetIDs.Contains(end.ID) && end.ID != MultiplayerSingleton.Instance.CurrentPlayerID()) {
GameData.Instance.minigameResults.Add(new Tuple<int, uint>(GameData.Instance.playerSelectTriggers[end.ID], end.results));
Logger.Log("MadelineParty", "Player " + end.DisplayName + " has finished the minigame with a result of " + end.results);
}
}
private void HandleMinigameStatus(MPData data) {
if (data is not MinigameStatus status) return;
// If another player in our party is sending out a minigame status update
if (GameData.Instance.celestenetIDs.Contains(status.ID) && status.ID != MultiplayerSingleton.Instance.CurrentPlayerID()) {
GameData.Instance.minigameStatus[GameData.Instance.playerSelectTriggers[status.ID]] = status.results;
Logger.Log("MadelineParty", "Player " + status.DisplayName + " has updated their minigame status with a result of " + status.results);
}
}
private void HandleRandomSeed(MPData data) {
if (data is not RandomSeed seed) return;
// If another player in our party is distributing the randomization seeds
if (GameData.Instance.celestenetIDs.Contains(seed.ID) && seed.ID != MultiplayerSingleton.Instance.CurrentPlayerID()) {
GameData.Instance.Random = new Random(seed.seed);
BoardController.GenerateTurnOrderRolls();
}
}
}
}