|
| 1 | +from bl2sdk import * |
| 2 | + |
| 3 | + |
| 4 | +class MapLoader(BL2MOD): |
| 5 | + Name = "Borderlands 2 Map Reloader" |
| 6 | + Description = "Quickly Farm Items in Borderlands 2!" |
| 7 | + Types = [ModTypes.Utility] |
| 8 | + Author = "FromDarkHell" |
| 9 | + |
| 10 | + def __init__(self): |
| 11 | + # Our rotation etc etc |
| 12 | + self.X = 0 |
| 13 | + self.Y = 0 |
| 14 | + self.Z = 0 |
| 15 | + self.Pitch = 0 |
| 16 | + self.Yaw = 0 |
| 17 | + self.Roll = 0 |
| 18 | + |
| 19 | + # It might be a good idea to restore our position after a load. |
| 20 | + self.saveLocation = True |
| 21 | + self.loading = False |
| 22 | + self.consistentLocation = False |
| 23 | + self.toggledLocation = False |
| 24 | + |
| 25 | + # Store some data that we can use to reload the map |
| 26 | + self.currentSelectedDifficulty = 0 |
| 27 | + self.currentSelectedOverpowerLevel = 0 |
| 28 | + |
| 29 | + self.DefaultGameInfo = UObject.FindObjectsContaining("WillowCoopGameInfo WillowGame.Default__WillowCoopGameInfo")[0] |
| 30 | + |
| 31 | + Keybinds = [ |
| 32 | + ["Quickload w/o Saving", "F7"], |
| 33 | + ["Quickload w/ Saving", "F8"], |
| 34 | + ["Toggle Quickload Save States", "F10"], |
| 35 | + ["Consistent Location States", "F5"] |
| 36 | + ] |
| 37 | + |
| 38 | + def reloadCurrentMap(self, skipSave): |
| 39 | + PC = GetEngine().GamePlayers[0].Actor |
| 40 | + if self.toggledLocation: |
| 41 | + if self.consistentLocation: |
| 42 | + locale = PC.Pawn.Location |
| 43 | + self.X = locale.X |
| 44 | + self.Y = locale.Y |
| 45 | + self.Z = locale.Z |
| 46 | + rotate = PC.Rotation |
| 47 | + self.Pitch = rotate.Pitch |
| 48 | + self.Yaw = rotate.Yaw |
| 49 | + self.Roll = rotate.Roll |
| 50 | + else: |
| 51 | + if not self.consistentLocation: |
| 52 | + locale = PC.Pawn.Location |
| 53 | + self.X = locale.X |
| 54 | + self.Y = locale.Y |
| 55 | + self.Z = locale.Z |
| 56 | + rotate = PC.Rotation |
| 57 | + self.Pitch = rotate.Pitch |
| 58 | + self.Yaw = rotate.Yaw |
| 59 | + self.Roll = rotate.Roll |
| 60 | + |
| 61 | + self.toggledLocation = False |
| 62 | + # Our currently selected difficulty for the main menu |
| 63 | + self.currentSelectedDifficulty = PC.GetCurrentPlaythrough() |
| 64 | + # Get our current save game we'll need it for the OP levels |
| 65 | + wsg = PC.GetCachedSaveGame() |
| 66 | + # Our current OP level if we need it, game is weird |
| 67 | + if wsg.LastOverpowerChoice and wsg.NumOverpowerLevelsUnlocked: |
| 68 | + self.currentSelectedOverpowerLevel = max(min(wsg.LastOverpowerChoice, wsg.NumOverpowerLevelsUnlocked), 0) |
| 69 | + else: |
| 70 | + self.currentSelectedOverpowerLevel = -1 |
| 71 | + |
| 72 | + # Load Map |
| 73 | + self.loading = True |
| 74 | + # This is the function that BL2 uses for save quits. |
| 75 | + PC.ReturnToTitleScreen(skipSave, False) |
| 76 | + |
| 77 | + def GameInputPressed(self, input): |
| 78 | + name = input.Name |
| 79 | + if name == "Quickload w/o Saving" or name == "Quickload w/ Saving": |
| 80 | + self.reloadCurrentMap(name == "Quickload w/o Saving") |
| 81 | + elif name == "Toggle Quickload Save States": |
| 82 | + self.saveLocation = not self.saveLocation |
| 83 | + state = "Location Saving is now {}".format("enabled" if self.saveLocation else "disabled") |
| 84 | + Log(state) |
| 85 | + pc = GetEngine().GamePlayers[0].Actor |
| 86 | + HUDMovie = pc.myHUD.HUDMovie |
| 87 | + # Show a training text for our location state. |
| 88 | + HUDMovie.ClearTrainingText() |
| 89 | + HUDMovie.AddTrainingText(state, "Map Loader", 2.0 * self.DefaultGameInfo.GameSpeed, (), "", False, 0, pc.PlayerReplicationInfo, True, 0, 0) |
| 90 | + elif name == "Consistent Location States": |
| 91 | + self.toggledLocation = True |
| 92 | + self.consistentLocation = not self.consistentLocation |
| 93 | + state = "Consistent Location States is now {}".format("enabled" if self.consistentLocation else "disabled") |
| 94 | + Log(state) |
| 95 | + pc = GetEngine().GamePlayers[0].Actor |
| 96 | + HUDMovie = pc.myHUD.HUDMovie |
| 97 | + # Show a training text for our location state. |
| 98 | + HUDMovie.ClearTrainingText() |
| 99 | + HUDMovie.AddTrainingText(state, "Map Loader", 2.0 * self.DefaultGameInfo.GameSpeed, (), "", False, 0, pc.PlayerReplicationInfo, True, 0, 0) |
| 100 | + |
| 101 | + def GameInputRebound(self, name, key): |
| 102 | + """Invoked by the SDK when one of the inputs we have registered for is |
| 103 | + rebound by the user. Use it to save our settings for the key binding.""" |
| 104 | + pass |
| 105 | + |
| 106 | + def Enable(self): |
| 107 | + |
| 108 | + def map_load_hook(caller: UObject, function: UFunction, params: FStruct): |
| 109 | + if self.saveLocation and self.loading: |
| 110 | + pc = GetEngine().GamePlayers[0].Actor |
| 111 | + HUDMovie = pc.myHUD.HUDMovie |
| 112 | + # PC is sometimes none when the hooked function is called, this means this execution of the hook is running to early. |
| 113 | + # Same thing with the HUDMovie. |
| 114 | + if pc.Pawn is None or HUDMovie is None: |
| 115 | + return True |
| 116 | + # Restore our location. |
| 117 | + locale = pc.Pawn.Location |
| 118 | + locale.X = self.X |
| 119 | + locale.Y = self.Y |
| 120 | + locale.Z = self.Z |
| 121 | + rotate = pc.Rotation |
| 122 | + rotate.Roll = self.Roll |
| 123 | + rotate.Pitch = self.Pitch |
| 124 | + rotate.Yaw = self.Yaw |
| 125 | + |
| 126 | + HUDMovie.ClearTrainingText() |
| 127 | + HUDMovie.AddTrainingText("Farming Location Restored", "Map Loader", 3.0 * self.DefaultGameInfo.GameSpeed, (), "", False, 0, pc.PlayerReplicationInfo, True, 0, 0) |
| 128 | + # Restore our rotation to the saved values. |
| 129 | + |
| 130 | + self.loading = False |
| 131 | + return True |
| 132 | + |
| 133 | + def main_menu_hook(caller: UObject, function: UFunction, params: FStruct): |
| 134 | + try: |
| 135 | + if self.loading: |
| 136 | + PC = GetEngine().GamePlayers[0].Actor |
| 137 | + # We'll need this to reload to the current difficulty. |
| 138 | + gfx = UObject.FindObjectsContaining("FrontendGFxMovie ")[1] |
| 139 | + if gfx is None or PC is None: |
| 140 | + return True |
| 141 | + # This is how the game knows what OP level we're on. |
| 142 | + if self.currentSelectedOverpowerLevel != -1: |
| 143 | + PC.OnSelectOverpowerLevel(PC.GetCachedSaveGame(), self.currentSelectedOverpowerLevel) |
| 144 | + # I don't *think* this does anything, might want to do it just in case. Weird Game. |
| 145 | + gfx.CurrentSelectedOverpowerLevel = self.currentSelectedOverpowerLevel |
| 146 | + Log("[Map Loader] Loading WSG on playthrough %s at OP %s" % (self.currentSelectedDifficulty, self.currentSelectedOverpowerLevel)) |
| 147 | + # Here we reload our save, like how the `Continue` button does. |
| 148 | + gfx.LaunchSaveGame(self.currentSelectedDifficulty) |
| 149 | + except: pass |
| 150 | + return True |
| 151 | + |
| 152 | + # This is how we know that we're in the main menu. Its slightly janky, but it works. |
| 153 | + RegisterHook("WillowGame.FrontendGFxMovie.OnTick", "HookMainMenu", main_menu_hook) |
| 154 | + # This is how we know that we've loaded a new map. Once again, janky. |
| 155 | + RegisterHook("WillowGame.WillowHUD.CreateWeaponScopeMovie", "MapHookLoad", map_load_hook) |
| 156 | + |
| 157 | + def Disable(self): |
| 158 | + RemoveHook("WillowGame.FrontendGFxMovie.OnTick", "HookMainMenu") |
| 159 | + RemoveHook("WillowGame.WillowHUD.CreateWeaponScopeMovie", "MapHookLoad") |
| 160 | + |
| 161 | + |
| 162 | +RegisterMod(MapLoader()) |
0 commit comments