-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModMain.cs
401 lines (334 loc) · 14.9 KB
/
ModMain.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BandTogether.Debug;
using BandTogether.Quantum;
using BandTogether.Util;
using HarmonyLib;
using NewHorizons.External;
using OWML.Common;
using OWML.ModHelper;
using OWML.Utils;
using UnityEngine;
using static BandTogether.Quantum.QuantumGroup;
using static BandTogether.Quantum.QuantumTarget;
namespace BandTogether;
public class ModMain : ModBehaviour
{
private static readonly IDictionary<string, (QuantumGroup[] groups, QuantumTarget destination)> GroupDialogueConditions =
new Dictionary<string, (QuantumGroup[], QuantumTarget)>
{
{ "BT_NOMAI_VILLAGE_A_TO_DOOR", (new[] { NomaiA }, Door) },
{ "BT_NOMAI_VILLAGE_B_TO_DOOR", (new[] { NomaiB }, Door) },
{ "BT_GHIRD_VILLAGE_A_TO_DOOR", (new[] { GhirdA }, Door) },
{ "BT_GHIRD_VILLAGE_B_TO_DOOR", (new[] { GhirdB }, Door) },
{ "BT_SEARCHED_GREAT_DOOR", (new[] { Captial }, Door) },
{ "BT_CLANS_LEAVE_DOOR", (new[] { NomaiA, NomaiB, GhirdA, GhirdB }, Away) },
{ "BT_DOORKEEPER_TO_FIRE", (new[] { Captial }, Away) },
{ "NOMAI_TO_FIRE", (new[] { NomaiA, NomaiB }, Fire) },
{ "GHIRD_TO_FIRE", (new[] { GhirdA, GhirdB }, Fire) },
};
private static readonly IDictionary<string, QuantumGroup> ShardConditions = new Dictionary<string, QuantumGroup>
{
{ "BT_GOT_NOMAI_SHARD_A", NomaiA },
{ "BT_GOT_NOMAI_SHARD_B", NomaiB },
{ "BT_GOT_GHIRD_SHARD_A", GhirdA },
{ "BT_GOT_GHIRD_SHARD_B", GhirdB },
};
private static readonly List<string> FifthShardFactNames = new()
{
"BT_FIFTH_SHARD_POS",
"BT_FIFTH_SHARD_QUANTUM",
"BT_FIFTH_SHARD_PILLAR",
"BT_FIFTH_SHARD_LIGHT"
};
public static ModMain Instance;
public delegate void MoveNpcEvent(QuantumGroup targetGroup, QuantumTarget targetType, bool ignoreVisibility);
public event MoveNpcEvent OnMoveGroup;
public delegate void ModStartEvent();
public event ModStartEvent OnMainQuest;
public delegate void MoveGatekeeperEvent();
public event MoveGatekeeperEvent OnCompleteAccidentalCode;
public delegate void ShardFoundEvent(QuantumGroup shardGroup);
public event ShardFoundEvent OnShardFound;
public delegate void DialogueConditionChanged(string condition, bool value);
public INewHorizons nhAPI;
public bool inEndSequence = false;
public bool fadeEndMusic = false;
public bool startedEndSequence = false;
public ItemType shrubberyType { get; private set; }
public GameObject Planet { get; private set; }
public List<SingleLightSensor> ghostLightSensors { get; private set; } = [];
private bool _debugEnabled = true;
//private bool _setFlashlightListDirty = false;
private int _numClansConvinced;
private bool _allFifthShardFacts;
private List<GameObject> factsToEnable = new();
private List<ScrollItem> scrollsToEnable = new();
private List<GameObject> textToEnable = new();
private DebugMenu _debugMenu;
private SignalFrequency _planetFrequency;
private readonly IDictionary<string, List<DialogueConditionChanged>> _dialogueConditionListeners =
new Dictionary<string, List<DialogueConditionChanged>>();
private readonly IDictionary<QuantumGroup, QuantumTarget> _groupCurrentLocation = GroupDialogueConditions
.Values
.SelectMany(value => value.groups)
.Distinct()
.ToDictionary(key => key, _ => QuantumTarget.Start);
private void Awake()
{
Instance = this;
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());
}
private void Start()
{
nhAPI = ModHelper.Interaction.TryGetModApi<INewHorizons>("xen.NewHorizons");
nhAPI.LoadConfigs(this);
if (!EnumUtils.IsDefined<ItemType>("Shrubbery"))
{
shrubberyType = EnumUtils.Create<ItemType>("Shrubbery");
}
LoadManager.OnCompleteSceneLoad += (scene, loadScene) =>
{
if (loadScene != OWScene.SolarSystem || nhAPI.GetCurrentStarSystem() != "Jam3")
{
return;
}
ModHelper.Console.WriteLine("Loaded into solar system!", MessageType.Success);
//WriteDebugMessage("Lantern: " + ReferenceLocator.GetDreamLanternItem());
nhAPI.GetBodyLoadedEvent().AddListener(OnBodyLoaded);
GlobalMessenger<string, bool>.AddListener("DialogueConditionChanged", OnDialogueConditionChanged);
AddDialogueConditionListener(OnMainQuestStart, "BT_MAIN_QUEST_START");
AddDialogueConditionListener(OnShardCondition, ShardConditions.Keys.ToArray());
AddDialogueConditionListener(OnGroupMoveCondition, GroupDialogueConditions.Keys.ToArray());
AddDialogueConditionListener(OnGatekeeperToDoor, "BT_SUNPOST_PUZZLE_SOLVED");
ModHelper.Events.Unity.FireInNUpdates(() =>
{
FifthShardFactNames
.Select(factName => Locator.GetShipLogManager().GetFact(factName))
.WhereNotNull()
.ForEach(fact => fact.OnFactRevealed += OnFifthShardFactRevealed);
OnFifthShardFactRevealed();
var relativeLocation = new RelativeLocationData(Vector3.up * 2 + Vector3.forward * 2, Quaternion.identity, Vector3.zero);
//nessesary for owlks, lets them work somehow????????
var location = DreamArrivalPoint.Location.Zone3;
var arrivalPoint = Locator.GetDreamArrivalPoint(location);
var dreamCampfire = Locator.GetDreamCampfire(location);
if (Locator.GetToolModeSwapper().GetItemCarryTool().GetHeldItemType() != ItemType.DreamLantern)
{
DreamLanternItem lantern = ReferenceLocator.GetDreamLanternItem();
lantern.OnEnterDreamWorld();
Locator.GetDreamWorldController()._playerLantern = lantern;
};
//Puts player into a semi-dreamworld state
Locator.GetDreamWorldController().EnterDreamWorld(dreamCampfire, arrivalPoint, relativeLocation);
ghostLightSensors = [];
ReferenceLocator.GetWorshipGhosts().ForEach((brain) =>
{
brain.enabled = true;
brain.GetComponentsInChildren<SingleLightSensor>().ForEach((sensor) => ghostLightSensors.Add(sensor));
brain.WakeUp();
brain.OnEnterDreamWorld();
});
}, 15);
};
LoadManager.OnStartSceneLoad += (scene, loadScene) =>
{
if (scene == OWScene.SolarSystem)
{
nhAPI.GetBodyLoadedEvent().RemoveListener(OnBodyLoaded);
FifthShardFactNames
.Select(factName => Locator.GetShipLogManager().GetFact(factName))
.WhereNotNull()
.ForEach(fact => fact.OnFactRevealed -= OnFifthShardFactRevealed);
inEndSequence = false;
fadeEndMusic = false;
startedEndSequence = false;
factsToEnable.Clear();
scrollsToEnable.Clear();
textToEnable.Clear();
ghostLightSensors.Clear();
}
};
}
private void OnBodyLoaded(string bodyName)
{
if (bodyName == "Fractured Harmony")
{
_debugEnabled = PlayerData.GetPersistentCondition("BAND_TOGETHER_DEBUG_ENABLED");
Planet = nhAPI.GetPlanet(bodyName);
if (IsDebugEnabled()) InitDebugMenu();
ReferenceLocator.GetSacredEntryway().LoadWaterObject(Planet);
ReferenceLocator.GetCreditsSong().transform.parent = null;
ModHelper.Events.Unity.FireOnNextUpdate(() =>
{
InitializeConditions();
});
}
}
public void InitDebugMenu()
{
_debugMenu = DebugMenu.InitMenu(Planet);
}
private void InitializeConditions()
{
WriteDebugMessage("init conditions");
if (!GetPersistentCondition("BT_MAIN_QUEST_START"))
{
Planet
.GetComponentsInChildren<ShipLogFactTriggerVolume>()
.ForEach(factTrigger =>
{
factsToEnable.Add(factTrigger.gameObject);
factTrigger.gameObject.SetActive(false);
});
Planet.GetComponentsInChildren<ScrollItem>().ForEach(scrollItem =>
{
scrollsToEnable.Add(scrollItem);
scrollItem.EnableInteraction(false);
});
Planet.GetComponentsInChildren<NomaiWallText>().ForEach(wallText =>
{
textToEnable.Add(wallText.gameObject);
wallText.gameObject.SetActive(false);
});
}
ShardConditions
.Where(condition => GetPersistentCondition(condition.Key))
.ForEach(condition =>
{
OnMoveGroup?.Invoke(condition.Value, Door, true);
OnShardFound?.Invoke(condition.Value);
_groupCurrentLocation[condition.Value] = Door;
_numClansConvinced++;
});
if (GetPersistentCondition("BT_SHRUB_GIVEN_TO_NOMAI"))
{
ShrubberySocketNomai socketNomai = ReferenceLocator.GetShrubSocketNomai();
socketNomai.PlaceIntoSocket(ReferenceLocator.GetShrubbery());
socketNomai.EnableInteraction(false);
}
else if (GetPersistentCondition("BT_FINISH_SHRUB_QUEST"))
{
TheDivineThrone socketThrone = ReferenceLocator.GetShrubSocketThrone();
socketThrone.PlaceIntoSocket(ReferenceLocator.GetShrubbery());
if (!GetPersistentCondition("BT_START_STEAL_QUEST"))
{
socketThrone.EnableInteraction(false);
}
}
if (GetPersistentCondition("BT_OPEN_SUNPOST_DOOR"))
{
ReferenceLocator.GetSunpostDetector().OpenDoor();
}
NewHorizonsData.LearnFrequency("Wide-Band Transmission");
ReferenceLocator.GetLightsOutController().InitializeGhirds();
}
private void OnDialogueConditionChanged(string condition, bool value)
{
WriteDebugMessage($"condition: {condition}");
if (_dialogueConditionListeners.TryGetValue(condition, out var listeners))
{
listeners.ForEach(listener => listener(condition, value));
}
}
private void OnMainQuestStart(string condition, bool value)
{
if (condition != "BT_MAIN_QUEST_START") return;
OnMainQuest?.Invoke();
if (factsToEnable.Count > 0)
{
factsToEnable.ForEach(factTrigger => factTrigger.SetActive(true));
}
if (scrollsToEnable.Count > 0)
{
scrollsToEnable.ForEach(scrollItem => scrollItem.EnableInteraction(true));
}
if (textToEnable.Count > 0)
{
textToEnable.ForEach(wallText => wallText.gameObject.SetActive(true));
}
}
private void OnShardCondition(string condition, bool value)
{
// Instance.ModHelper.Console.WriteLine($"condition changed: {condition}");
if (!ShardConditions.TryGetValue(condition, out var shardGroup)) return;
OnShardFound?.Invoke(shardGroup);
_numClansConvinced += 1;
if (_numClansConvinced == 3 && !GetPersistentCondition("BT_LAST_CLAN_TO_AGREE"))
{
SetPersistentCondition("BT_LAST_CLAN_TO_AGREE", true);
}
else if (_numClansConvinced == 4 && !GetPersistentCondition("BT_ALL_CLANS_AGREED"))
{
Locator.GetShipLogManager().RevealFact("BT_GREAT_DOOR_CLANS_AGREED");
SetPersistentCondition("BT_ALL_CLANS_AGREED", true);
}
}
private void OnGatekeeperToDoor(string condition, bool value)
{
if (condition != "BT_SUNPOST_PUZZLE_SOLVED" || !GetPersistentCondition("BT_ACCIDENTAL_SOLVE")) return;
OnCompleteAccidentalCode?.Invoke();
}
private void OnFifthShardFactRevealed()
{
_allFifthShardFacts = FifthShardFactNames
.All(shardLog => Locator.GetShipLogManager().IsFactRevealed(shardLog));
}
private void OnGroupMoveCondition(string condition, bool value)
{
if (!GroupDialogueConditions.ContainsKey(condition) || !value) return;
var destination = GroupDialogueConditions[condition];
WriteDebugMessage($"move condition: {destination}");
destination.groups.ForEach(group => OnMoveGroup?.Invoke(group, destination.destination, false));
}
public static void AddDialogueConditionListener(DialogueConditionChanged listener, params string[] conditions) =>
conditions.ForEach(condition =>
Instance._dialogueConditionListeners
.GetOrInit(condition, new List<DialogueConditionChanged>())
.Add(listener)
);
public static void RemoveDialogueConditionListener(DialogueConditionChanged listener, params string[] conditions) =>
conditions.ForEach(condition =>
Instance._dialogueConditionListeners
.GetOrInit(condition, new List<DialogueConditionChanged>())
.Remove(listener)
);
public static void SetCondition(string condition, bool value) =>
DialogueConditionManager.SharedInstance.SetConditionState(condition, value);
public static bool GetCondition(string condition) =>
DialogueConditionManager.SharedInstance.GetConditionState(condition);
public static void SetPersistentCondition(string condition, bool value, bool includeTransient = true)
{
PlayerData.SetPersistentCondition(condition, value);
if (includeTransient) SetCondition(condition, value);
}
public static bool GetPersistentCondition(string condition) =>
PlayerData.GetPersistentCondition(condition);
public static bool AllFifthShardRumors() =>
Instance._allFifthShardFacts;
public static void TriggerEnd()
{
Instance.ModHelper.Events.Unity.FireOnNextUpdate(() =>
{
Instance.startedEndSequence = true;
Instance.fadeEndMusic = true;
Locator.GetDeathManager().BeginEscapedTimeLoopSequence(TimeloopEscapeType.Ship);
});
}
public void OnTriggerCampfireEnd()
{
FindObjectOfType<GameOverController>()._deathText.text = "Despite there being nothing of value behind the Great Door, you managed to\nreunite the clans and bring harmony back to the planet.";
FindObjectOfType<GameOverController>().SetupGameOverScreen(10f);
}
public static void WriteDebugMessage(object msg)
{
if (!IsDebugEnabled()) return;
Instance.ModHelper.Console.WriteLine(msg.ToString());
}
private void OnDestroy()
{
nhAPI.GetBodyLoadedEvent().RemoveListener(OnBodyLoaded);
}
public static bool IsDebugEnabled() => Instance._debugEnabled;
}