RingRush

Untitled

Dec 30th, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. from bk_movement_sim import *
  2.  
  3. import math
  4. import random
  5. import copy
  6. import numpy as np
  7. import datetime
  8. import itertools
  9.  
  10. # ============================================================
  11. # Game initialization
  12. # ============================================================
  13.  
  14. game = Game(
  15. Banjo((3813.78979492188,-1631.62829589844),(-81.651237487793,129.443740844727),327.67578125,True),
  16. Camera((2751.10766601563,-1776.05529785156),(-1.36168503761292,-0.968132376670837),274.477478027344,-1.60191655158997),
  17. CameraPivot(2474.621,792.0525,-1855.713,1.75,3.75,2.75,12,1250.5,1250.5),
  18. True)
  19.  
  20. game.update("midair", -40, -49)
  21.  
  22. # settle initial state
  23. for i in range(1, 4):
  24. x0, y0 = game.calculate_input_for_desired_speed("midair", (0, 0))
  25. game.update("midair", round(x0), round(y0))
  26.  
  27. x, z = game.banjo.pos
  28. print(f"banjo x = {x:.12f}, z = {z:.12f}")
  29.  
  30. # ============================================================
  31. # Constants
  32. # ============================================================
  33.  
  34. H = 3
  35. reach = 50
  36. deadzone = 12
  37. deadzoneB = 62
  38.  
  39. inputs = [[0, 0] for _ in range(H - 1)]
  40.  
  41. def inbounds(n):
  42. return ((n < -deadzone or n > deadzone) and (n < deadzoneB and n > -deadzoneB)) or n == 0
  43.  
  44. # ============================================================
  45. # Pretty-print helper
  46. # ============================================================
  47.  
  48. def print_table(tbl, indent=0):
  49. formatting = " " * indent
  50. for key, value in tbl.items():
  51. if isinstance(value, dict):
  52. print(f"{formatting}{key}:")
  53. print_table(value, indent + 1)
  54. else:
  55. print(f"{formatting}{key}: {value}")
  56.  
  57. # ============================================================
  58. # Load bitclip points
  59. # ============================================================
  60.  
  61. Bitclips = np.loadtxt("gap_results.txt", dtype=np.float32)
  62.  
  63. def closest_bitclip(pos):
  64. diff = Bitclips - pos
  65. dist = np.sqrt(diff[:, 0] ** 2 + diff[:, 1] ** 2)
  66. idx = np.argmin(dist)
  67. return Bitclips[idx], dist[idx]
  68.  
  69. # ============================================================
  70. # Line definition
  71. # ============================================================
  72.  
  73. _raw_lines = [
  74. (np.array([3655, -1229]), np.array([3811, -1639])),
  75. (np.array([3362, -1408]), np.array([3811, -1639])),
  76. ]
  77.  
  78. Lines = []
  79. for a, b in _raw_lines:
  80. d = b - a
  81. d = d / np.linalg.norm(d)
  82. n = np.array([-d[1], d[0]])
  83. Lines.append((a, d, n))
  84.  
  85. # ============================================================
  86. # Line distance utilities
  87. # ============================================================
  88.  
  89. def closest_line_distance(pos, lines):
  90. best = float("inf")
  91. for a, _, n in lines:
  92. d = abs(np.dot(pos - a, n))
  93. if d < best:
  94. best = d
  95. return best
  96.  
  97. def closest_point_on_line(pos, a, d):
  98. t = np.dot(pos - a, d)
  99. return a + t * d
  100.  
  101. def closest_line_and_point(pos, lines):
  102. best = float("inf")
  103. best_line = None
  104. best_point = None
  105.  
  106. for a, d, n in lines:
  107. p = closest_point_on_line(pos, a, d)
  108. dist = abs(np.dot(pos - a, n))
  109. if dist < best:
  110. best = dist
  111. best_line = (a, d, n)
  112. best_point = p
  113.  
  114. return best, best_line, best_point
  115.  
  116. # ============================================================
  117. # Greedy refinement
  118. # ============================================================
  119.  
  120. def greedy_step_towards_line(game, max_steps=3):
  121. inputs_greed = []
  122.  
  123. for _ in range(max_steps):
  124. pos = game.banjo.pos.astype(np.float32)
  125. cur_dist, line, target = closest_line_and_point(pos, Lines)
  126.  
  127. to_line = target - pos
  128. if np.linalg.norm(to_line) < 1e-4:
  129. break
  130.  
  131. x, y = game.calculate_input_for_desired_pos("midair", target)
  132. x = int(round(x))
  133. y = int(round(y))
  134.  
  135. savestate = copy.deepcopy(game)
  136. game.update("midair", x, y)
  137.  
  138. new_dist = closest_line_distance(
  139. game.banjo.pos.astype(np.float32), Lines
  140. )
  141.  
  142. if new_dist >= cur_dist:
  143. game = savestate
  144. break
  145. else:
  146. inputs_greed.append((x, y))
  147.  
  148. return inputs_greed
  149.  
  150. # ============================================================
  151. # Exhaustive input generation
  152. # ============================================================
  153.  
  154. valid_axis = [
  155. n for n in range(-reach, reach + 1)
  156. if inbounds(n)
  157. ]
  158.  
  159. valid_inputs = [(x, y) for x in valid_axis for y in valid_axis]
  160. valid_inputs = sorted(valid_inputs, key=lambda x: sum(abs(val) for val in x), reverse = True)
  161. print(f"{len(valid_inputs)} valid (x,y) inputs")
  162.  
  163. input_sequences = itertools.product(valid_inputs, repeat=H - 1)
  164.  
  165. # ============================================================
  166. # Main search loop (EXHAUSTIVE)
  167. # ============================================================
  168.  
  169. realbest = 0.2
  170. c = 0
  171. set_inputs = [0,0]
  172.  
  173. #game.update("midair", set_inputs[0], set_inputs[1])
  174. savestate_slot_1 = copy.deepcopy(game)
  175.  
  176. for seq in input_sequences:
  177. inputs = [list(v) for v in seq]
  178.  
  179. # Apply input sequence
  180. for x, y in inputs:
  181. game.update("midair", x, y)
  182.  
  183. best = float("inf")
  184.  
  185. def check_dis(x, y):
  186. global game, best, realbest
  187.  
  188. if inbounds(x) and inbounds(y):
  189. savestate_slot_2 = copy.deepcopy(game)
  190. game._update_banjo("midair", x, y)
  191.  
  192. if abs(game.banjo.speed[0]) < .0033 and abs(game.banjo.speed[1]) < .002:
  193. pos_after_convergence = (
  194. game.banjo.pos + game.banjo.speed / (25 * (1 - 0.916))
  195. ).astype(np.float32)
  196.  
  197. bitclip, dis = closest_bitclip(pos_after_convergence)
  198.  
  199. if dis < best:
  200. best = dis
  201.  
  202. if best < realbest:
  203. realbest = best
  204. print(realbest)
  205. print_table({"inputs": inputs})
  206. print_table({"inputs_greed": inputs_greed})
  207. print(x, y)
  208. print(game)
  209.  
  210. game = copy.deepcopy(savestate_slot_2)
  211.  
  212. # ========================================================
  213. # Check success + greedy refinement
  214. # ========================================================
  215.  
  216. pos2 = game.banjo.pos.astype(np.float32)
  217. line_dist = closest_line_distance(pos2, Lines)
  218.  
  219. if line_dist < 4:
  220. inputs_greed = greedy_step_towards_line(game)
  221. pos2 = game.banjo.pos.astype(np.float32)
  222. line_dist = closest_line_distance(pos2, Lines)
  223.  
  224. if line_dist < realbest:
  225. x0, y0 = game.calculate_input_for_desired_speed("midair", (0, 0))
  226. check_dis(round(x0), round(y0))
  227.  
  228. # ========================================================
  229. # Bookkeeping
  230. # ========================================================
  231.  
  232. c += 1
  233. if c % 50000 == 0:
  234. print(c, datetime.datetime.now())
  235.  
  236. game = copy.deepcopy(savestate_slot_1)
  237.  
  238. print("Exhaustive search complete")
  239.  
Advertisement
Add Comment
Please, Sign In to add comment