about summary refs log tree commit diff stats
path: root/AnodyneArchipelago/Patches/GameplayPatches.cs
blob: 056a855f3ba1b4a9b568c8acf783a96310c575ae (plain) (blame)
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
using AnodyneSharp.Entities.Events;
using AnodyneSharp.Entities.Gadget.Treasures;
using AnodyneSharp.Entities.Gadget;
using AnodyneSharp.Entities.Interactive;
using AnodyneSharp.Entities;
using AnodyneSharp.Registry;
using AnodyneSharp.Utilities;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Xna.Framework;
using AnodyneSharp.Entities.Interactive.Npc.RunningTradeNPCs;
using AnodyneSharp.Dialogue;
using AnodyneSharp.Sounds;
using AnodyneSharp.States;
using AnodyneSharp.Entities.Interactive.Npc;
using AnodyneSharp.MapData;

namespace AnodyneArchipelago.Patches
{
    [HarmonyPatch(typeof(TreasureChest), nameof(TreasureChest.PlayerInteraction))]
    class ChestInteractPatch
    {
        static void Prefix(TreasureChest __instance)
        {
            string entityId = PatchHelper.GetEntityPreset(typeof(TreasureChest), __instance).EntityID.ToString();
            Plugin.Instance.Log.LogInfo($"Touched chest: {entityId}");
        }
    }

    [HarmonyPatch(typeof(TreasureChest), "SetTreasure")]
    class ChestSetTreasurePatch
    {
        static bool Prefix(TreasureChest __instance)
        {
            Guid entityId = PatchHelper.GetEntityPreset(typeof(TreasureChest), __instance).EntityID;
            if (Locations.LocationsByGuid.ContainsKey(entityId))
            {
                BaseTreasure treasure = new ArchipelagoTreasure(Locations.LocationsByGuid[entityId], __instance.Position);

                FieldInfo treasureField = typeof(TreasureChest).GetField("_treasure", BindingFlags.NonPublic | BindingFlags.Instance);
                treasureField.SetValue(__instance, treasure);

                return false;
            }

            return true;
        }
    }

    [HarmonyPatch(typeof(Big_Key), nameof(Big_Key.PlayerInteraction))]
    class BigKeyTouchPatch
    {
        // We basically just rewrite this method, because we need to get rid of the part that adds the key to the inventory.
        static bool Prefix(Big_Key __instance, ref bool __result)
        {
            MethodInfo statesMethod = typeof(Big_Key).GetMethod("States", BindingFlags.NonPublic | BindingFlags.Instance);

            EntityPreset preset = PatchHelper.GetEntityPreset(typeof(Big_Key), __instance);
            preset.Alive = false;
            __instance.Solid = false;
            GlobalState.StartCutscene = (IEnumerator<AnodyneSharp.States.CutsceneState.CutsceneEvent>)statesMethod.Invoke(__instance, new object[] { });
            __result = true;
            return false;
        }
    }

    [HarmonyPatch(typeof(Big_Key), "States")]
    class BigKeyStatesPatch
    {
        static void Postfix(Big_Key __instance)
        {
            EntityPreset preset = PatchHelper.GetEntityPreset(typeof(Big_Key), __instance);

            if (preset.Frame == 0)
            {
                Plugin.ArchipelagoManager.SendLocation("Temple of the Seeing One - Green Key");
            }
            else if (preset.Frame == 2)
            {
                Plugin.ArchipelagoManager.SendLocation("Red Grotto - Red Key");
            }
            else if (preset.Frame == 4)
            {
                Plugin.ArchipelagoManager.SendLocation("Mountain Cavern - Blue Key");
            }
        }
    }

    [HarmonyPatch(typeof(HealthCicadaSentinel), nameof(HealthCicadaSentinel.Collided))]
    class HealthCicadaInteractPatch
    {
        static void Prefix(TreasureChest __instance)
        {
            Type hcsType = typeof(HealthCicadaSentinel);
            FieldInfo childField = hcsType.GetField("_child", BindingFlags.NonPublic | BindingFlags.Instance);
            HealthCicada healthCicada = (HealthCicada)childField.GetValue(__instance);

            EntityPreset preset = PatchHelper.GetEntityPreset(typeof(HealthCicada), healthCicada);
            Plugin.Instance.Log.LogInfo($"Touched cicada: {preset.EntityID.ToString()}");
        }
    }

    [HarmonyPatch(typeof(HealthCicada), nameof(HealthCicada.Update))]
    class HealthCicadaUpdatePatch
    {
        static void Postfix(HealthCicada __instance)
        {
            Type cicadaType = typeof(HealthCicada);
            FieldInfo chirpField = cicadaType.GetField("_chirp", BindingFlags.NonPublic | BindingFlags.Instance);
            FieldInfo sentinelField = cicadaType.GetField("_sentinel", BindingFlags.NonPublic | BindingFlags.Instance);

            HealthCicadaSentinel sentinel = (HealthCicadaSentinel)sentinelField.GetValue(__instance);
            Type hcsType = typeof(HealthCicadaSentinel);
            FieldInfo flyDistanceField = hcsType.GetField("_flyDistance", BindingFlags.NonPublic | BindingFlags.Instance);
            float flyDistance = (float)flyDistanceField.GetValue(sentinel);

            if (__instance.visible && !(bool)chirpField.GetValue(__instance) && flyDistance > 0)
            {
                flyDistanceField.SetValue(sentinel, 0f);

                FieldInfo stateField = cicadaType.GetField("_state", BindingFlags.NonPublic | BindingFlags.Instance);
                stateField.SetValue(__instance, StateLogic(__instance));
            }
        }

        static IEnumerator<string> StateLogic(HealthCicada healthCicada)
        {
            EntityPreset preset = PatchHelper.GetEntityPreset(typeof(HealthCicada), healthCicada);

            while (!MathUtilities.MoveTo(ref healthCicada.opacity, 0.0f, 0.6f))
                yield return "FadingOut";
            preset.Alive = false;

            FieldInfo sentinelField = typeof(HealthCicada).GetField("_sentinel", BindingFlags.NonPublic | BindingFlags.Instance);
            HealthCicadaSentinel sentinel = (HealthCicadaSentinel)sentinelField.GetValue(healthCicada);

            sentinel.exists = false;

            if (Locations.LocationsByGuid.ContainsKey(preset.EntityID))
            {
                Plugin.ArchipelagoManager.SendLocation(Locations.LocationsByGuid[preset.EntityID]);
            }
        }
    }

    [HarmonyPatch(typeof(EntityPreset), nameof(EntityPreset.Create))]
    class EntityPresetCreatePatch
    {
        static void Prefix(EntityPreset __instance)
        {
            if (__instance.EntityID == new Guid("C8CE6E18-CF07-180B-A550-9DC808A2F7E3"))
            {
                PropertyInfo frameProperty = typeof(EntityPreset).GetProperty("Frame");
                frameProperty.SetValue(__instance, (int)Plugin.ArchipelagoManager.EndgameCardRequirement);
            }
        }

        static void Postfix(EntityPreset __instance, Entity __result)
        {
            if (__instance.Type.FullName == "AnodyneSharp.Entities.Interactive.DungeonStatue")
            {
                __result.Position = __instance.Position + new Vector2(1f, 32f);

                string eventName = "StatueMoved_";
                Facing moveDir = Facing.RIGHT;
                if (__instance.Frame == 0)
                {
                    eventName += "Temple";
                    moveDir = Facing.UP;
                }
                else if (__instance.Frame == 1)
                {
                    eventName += "Grotto";
                }
                else if (__instance.Frame == 2)
                {
                    eventName += "Mountain";
                }

                if (GlobalState.events.GetEvent(eventName) > 0)
                {
                    __result.Position += Entity.FacingDirection(moveDir) * 32f;
                }
            }
            else if (__instance.Type.FullName.StartsWith("AnodyneSharp.Entities.Decorations.RedCave"))
            {
                string side = __instance.Type.FullName.Substring(41);
                int requiredGrottos = 0;
                if (side == "Left")
                {
                    requiredGrottos = 1;
                }
                else if (side == "Right")
                {
                    requiredGrottos = 2;
                }
                else if (side == "North")
                {
                    requiredGrottos = 3;
                }

                if (GlobalState.events.GetEvent("ProgressiveRedGrotto") < requiredGrottos)
                {
                    __result.exists = false;
                    GlobalState.SpawnEntity((Entity)new DoorToggle(__result.Position, __result.width, __result.height));
                }
            }
            else if (__instance.Type.FullName == "AnodyneSharp.Entities.Interactive.Npc.MitraCliff")
            {
                // We want to get rid of this scene entirely.
                __instance.Alive = false;
                __result.exists = false;
            }
        }
    }

    [HarmonyPatch]
    class BreakChainPatch
    {
        static MethodInfo TargetMethod()
        {
            return typeof(Red_Pillar).GetNestedType("Chain", BindingFlags.NonPublic).GetMethod("Collided");
        }

        static void Postfix(object __instance)
        {
            Type chainType = typeof(Red_Pillar).GetNestedType("Chain", BindingFlags.NonPublic);
            FieldInfo parentField = chainType.GetField("_parent", BindingFlags.NonPublic | BindingFlags.Instance);

            Red_Pillar redPillar = (Red_Pillar)parentField.GetValue(__instance);
            EntityPreset preset = PatchHelper.GetEntityPreset(typeof(Red_Pillar), redPillar);

            Plugin.Instance.Log.LogInfo($"Broke red chain: {preset.EntityID.ToString()}");

            if (Locations.LocationsByGuid.ContainsKey(preset.EntityID))
            {
                Plugin.ArchipelagoManager.SendLocation(Locations.LocationsByGuid[preset.EntityID]);
            }
        }
    }

    [HarmonyPatch(typeof(Box), nameof(Box.PlayerInteraction))]
    class BoxOpenPatch
    {
        static bool Prefix(Box __instance, ref bool __result)
        {
            if (!GlobalState.events.SpookedMonster)
            {
                __result = false;
                return false;
            }
                
            __instance.Play("open");
            SoundManager.PlaySoundEffect("broom_hit");
            GlobalState.StartCutscene = OnOpened(__instance);
            __result = true;
            return false;
        }

        static IEnumerator<CutsceneState.CutsceneEvent> OnOpened(Box box)
        {
            MethodInfo openedMethod = typeof(Box).GetMethod("OnOpened", BindingFlags.NonPublic | BindingFlags.Instance);
            IEnumerator<CutsceneState.CutsceneEvent> subCutscene = (IEnumerator<CutsceneState.CutsceneEvent>)openedMethod.Invoke(box, new object[] { });

            yield return subCutscene.Current;
            while (subCutscene.MoveNext())
            {
                yield return subCutscene.Current;
            }

            GlobalState.inventory.tradeState = InventoryManager.TradeState.NONE;

            Plugin.ArchipelagoManager.SendLocation("Fields - Cardboard Box");
        }
    }

    [HarmonyPatch(typeof(ShopKeep), nameof(ShopKeep.PlayerInteraction))]
    class ShopKeepTalkPatch
    {
        static bool Prefix(ShopKeep __instance)
        {
            if (GlobalState.events.GetEvent("ReceivedCardboardBox") == 1 && GlobalState.events.GetEvent("UsedCardboardBox") == 0)
            {
                GlobalState.Dialogue = GetDiag(2) + " " + GetDiag(4);
                GlobalState.events.SetEvent("UsedCardboardBox", 1);

                EntityPreset preset = PatchHelper.GetEntityPreset(typeof(ShopKeep), __instance);
                preset.Activated = true;

                Plugin.ArchipelagoManager.SendLocation("Fields - Shopkeeper Trade");

                return false;
            }

            return true;
        }

        static string GetDiag(int i) => DialogueManager.GetDialogue("misc", "any", "tradenpc", i);
    }

    [HarmonyPatch(typeof(MitraFields), "GetInteractionText")]
    class MitraFieldsTextPatch
    {
        static bool Prefix(ref string __result)
        {
            if (GlobalState.events.GetEvent("ReceivedBikingShoes") == 1 && GlobalState.events.GetEvent("UsedBikingShoes") == 0)
            {
                __result = DialogueManager.GetDialogue("misc", "any", "mitra", 1);

                GlobalState.events.SetEvent("UsedBikingShoes", 1);

                Plugin.ArchipelagoManager.SendLocation("Fields - Mitra Trade");

                return false;
            }

            return true;
        }
    }

    [HarmonyPatch("AnodyneSharp.Entities.Interactive.Npc.Windmill.Console", "PlayerInteraction")]
    class WindmillInteractPatch
    {
        static void Prefix(object __instance)
        {
            if ((__instance as Entity).CurAnimName == "active")
            {
                Plugin.ArchipelagoManager.SendLocation("Windmill - Activation");
            }
        }
    }
    
    [HarmonyPatch(typeof(PlayState), "Warp")]
    class PlayWarpPatch
    {
        static void Postfix()
        {
            if (GlobalState.CURRENT_MAP_NAME == "FIELDS")
            {
                // Place a rock blocking access to Terminal without the red key.
                PatchHelper.SetMapTile(31, 47, 11, Layer.BG);
            }
        }
    }

    /*[HarmonyPatch(typeof(Player), "Movement")]
    class PlayerMovementPatch
    {
        static void Postfix(Player __instance)
        {
            Plugin.Instance.Log.LogInfo($"Player pos: {GlobalState.Map.ToMapLoc(__instance.Position)}");
        }
    }*/
}