Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bk_movement_sim import *
- import math
- import random
- import copy
- import numpy as np
- import datetime
- import itertools
- # ============================================================
- # Game initialization
- # ============================================================
- game = Game(
- Banjo((3813.78979492188,-1631.62829589844),(-81.651237487793,129.443740844727),327.67578125,True),
- Camera((2751.10766601563,-1776.05529785156),(-1.36168503761292,-0.968132376670837),274.477478027344,-1.60191655158997),
- CameraPivot(2474.621,792.0525,-1855.713,1.75,3.75,2.75,12,1250.5,1250.5),
- True)
- game.update("midair", -40, -49)
- # settle initial state
- for i in range(1, 4):
- x0, y0 = game.calculate_input_for_desired_speed("midair", (0, 0))
- game.update("midair", round(x0), round(y0))
- x, z = game.banjo.pos
- print(f"banjo x = {x:.12f}, z = {z:.12f}")
- # ============================================================
- # Constants
- # ============================================================
- H = 3
- reach = 50
- deadzone = 12
- deadzoneB = 62
- inputs = [[0, 0] for _ in range(H - 1)]
- def inbounds(n):
- return ((n < -deadzone or n > deadzone) and (n < deadzoneB and n > -deadzoneB)) or n == 0
- # ============================================================
- # Pretty-print helper
- # ============================================================
- def print_table(tbl, indent=0):
- formatting = " " * indent
- for key, value in tbl.items():
- if isinstance(value, dict):
- print(f"{formatting}{key}:")
- print_table(value, indent + 1)
- else:
- print(f"{formatting}{key}: {value}")
- # ============================================================
- # Load bitclip points
- # ============================================================
- Bitclips = np.loadtxt("gap_results.txt", dtype=np.float32)
- def closest_bitclip(pos):
- diff = Bitclips - pos
- dist = np.sqrt(diff[:, 0] ** 2 + diff[:, 1] ** 2)
- idx = np.argmin(dist)
- return Bitclips[idx], dist[idx]
- # ============================================================
- # Line definition
- # ============================================================
- _raw_lines = [
- (np.array([3655, -1229]), np.array([3811, -1639])),
- (np.array([3362, -1408]), np.array([3811, -1639])),
- ]
- Lines = []
- for a, b in _raw_lines:
- d = b - a
- d = d / np.linalg.norm(d)
- n = np.array([-d[1], d[0]])
- Lines.append((a, d, n))
- # ============================================================
- # Line distance utilities
- # ============================================================
- def closest_line_distance(pos, lines):
- best = float("inf")
- for a, _, n in lines:
- d = abs(np.dot(pos - a, n))
- if d < best:
- best = d
- return best
- def closest_point_on_line(pos, a, d):
- t = np.dot(pos - a, d)
- return a + t * d
- def closest_line_and_point(pos, lines):
- best = float("inf")
- best_line = None
- best_point = None
- for a, d, n in lines:
- p = closest_point_on_line(pos, a, d)
- dist = abs(np.dot(pos - a, n))
- if dist < best:
- best = dist
- best_line = (a, d, n)
- best_point = p
- return best, best_line, best_point
- # ============================================================
- # Greedy refinement
- # ============================================================
- def greedy_step_towards_line(game, max_steps=3):
- inputs_greed = []
- for _ in range(max_steps):
- pos = game.banjo.pos.astype(np.float32)
- cur_dist, line, target = closest_line_and_point(pos, Lines)
- to_line = target - pos
- if np.linalg.norm(to_line) < 1e-4:
- break
- x, y = game.calculate_input_for_desired_pos("midair", target)
- x = int(round(x))
- y = int(round(y))
- savestate = copy.deepcopy(game)
- game.update("midair", x, y)
- new_dist = closest_line_distance(
- game.banjo.pos.astype(np.float32), Lines
- )
- if new_dist >= cur_dist:
- game = savestate
- break
- else:
- inputs_greed.append((x, y))
- return inputs_greed
- # ============================================================
- # Exhaustive input generation
- # ============================================================
- valid_axis = [
- n for n in range(-reach, reach + 1)
- if inbounds(n)
- ]
- valid_inputs = [(x, y) for x in valid_axis for y in valid_axis]
- valid_inputs = sorted(valid_inputs, key=lambda x: sum(abs(val) for val in x), reverse = True)
- print(f"{len(valid_inputs)} valid (x,y) inputs")
- input_sequences = itertools.product(valid_inputs, repeat=H - 1)
- # ============================================================
- # Main search loop (EXHAUSTIVE)
- # ============================================================
- realbest = 0.2
- c = 0
- set_inputs = [0,0]
- #game.update("midair", set_inputs[0], set_inputs[1])
- savestate_slot_1 = copy.deepcopy(game)
- for seq in input_sequences:
- inputs = [list(v) for v in seq]
- # Apply input sequence
- for x, y in inputs:
- game.update("midair", x, y)
- best = float("inf")
- def check_dis(x, y):
- global game, best, realbest
- if inbounds(x) and inbounds(y):
- savestate_slot_2 = copy.deepcopy(game)
- game._update_banjo("midair", x, y)
- if abs(game.banjo.speed[0]) < .0033 and abs(game.banjo.speed[1]) < .002:
- pos_after_convergence = (
- game.banjo.pos + game.banjo.speed / (25 * (1 - 0.916))
- ).astype(np.float32)
- bitclip, dis = closest_bitclip(pos_after_convergence)
- if dis < best:
- best = dis
- if best < realbest:
- realbest = best
- print(realbest)
- print_table({"inputs": inputs})
- print_table({"inputs_greed": inputs_greed})
- print(x, y)
- print(game)
- game = copy.deepcopy(savestate_slot_2)
- # ========================================================
- # Check success + greedy refinement
- # ========================================================
- pos2 = game.banjo.pos.astype(np.float32)
- line_dist = closest_line_distance(pos2, Lines)
- if line_dist < 4:
- inputs_greed = greedy_step_towards_line(game)
- pos2 = game.banjo.pos.astype(np.float32)
- line_dist = closest_line_distance(pos2, Lines)
- if line_dist < realbest:
- x0, y0 = game.calculate_input_for_desired_speed("midair", (0, 0))
- check_dis(round(x0), round(y0))
- # ========================================================
- # Bookkeeping
- # ========================================================
- c += 1
- if c % 50000 == 0:
- print(c, datetime.datetime.now())
- game = copy.deepcopy(savestate_slot_1)
- print("Exhaustive search complete")
Advertisement
Add Comment
Please, Sign In to add comment