forked from quasar098/midi-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.py
202 lines (171 loc) · 8.47 KB
/
world.py
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
from utils import *
from bounce import Bounce
from particle import Particle
from square import Square
from time import time as get_current_time
from scorekeeper import Scorekeeper
import random
import pygame
class World:
"""it's a cruel world out there"""
def __init__(self):
self.future_bounces: list[Bounce] = []
self.past_bounces: list[Bounce] = []
self.start_time = 0
self.time = 0
self.rectangles: list[pygame.Rect] = []
self.collision_times: list[float] = []
self.particles: list[Particle] = []
self.timestamps = []
self.square = Square()
self.scorekeeper = Scorekeeper(self)
self.colors = []
def update_time(self) -> None:
self.time = get_current_time() - self.start_time
def get_next_bounce(self) -> Bounce:
"""Also pops the bounce from the future_bounces list"""
self.past_bounces.append(self.future_bounces.pop(0))
return self.past_bounces[-1]
def add_bounce_particles(self, sp: list[float], sd: list[float]):
for _ in range(Config.particle_amount):
new = Particle([sp[0]+random.randint(-10, 10), sp[1]+random.randint(-10, 10)], sd)
self.particles.append(new)
def handle_bouncing(self, square: Square):
if len(self.future_bounces):
if (self.time * 1000 + Config.music_offset)/1000 > self.future_bounces[0].time:
current_bounce = self.get_next_bounce()
before = square.dir.copy()
square.obey_bounce(current_bounce)
changed = square.dir.copy()
for _ in range(2):
if before[_] == changed[_]:
changed[_] = 0
else:
changed[_] = -changed[_]
if Config.do_particles_on_bounce:
self.add_bounce_particles(square.pos, changed)
# stop square at end
if len(self.future_bounces) == 0:
square.dir = [0, 0]
square.pos = current_bounce.square_pos
def handle_keypress(self, time_from_start, misses):
return self.scorekeeper.do_keypress(time_from_start, misses)
def gen_future_bounces(self, _start_notes: list[tuple[int, int, int]], percent_update_callback):
"""Recursive solution may be necessary"""
total_notes = len(_start_notes)
max_percent = 0
path = []
safe_areas = []
force_return = 0
def recurs(
square: Square,
notes: list[float],
bounces_so_far: list[Bounce] = None,
prev_index_priority=None,
t: float = 0,
depth: int = 0
) -> Union[list[Bounce], bool]:
nonlocal force_return, max_percent
if prev_index_priority is None:
prev_index_priority = [0, 1]
if bounces_so_far is None:
bounces_so_far = []
gone_through_percent = (total_notes-len(notes)) * 100 // total_notes
while gone_through_percent > max_percent:
max_percent += 1
if percent_update_callback(f"{max_percent}% done generating map"):
raise UserCancelsLoadingError()
all_bounce_rects = [_bounc.get_collision_rect() for _bounc in bounces_so_far]
if len(notes) == 0:
return bounces_so_far
# print(depth * 100 // total_notes)
path_segment_start = len(path)
start_rect = square.rect.copy()
while True:
t += 1/FRAMERATE
square.reg_move(False)
path.append(square.rect)
if t > notes[0]:
# no collision (we good)
bounce_indexes = prev_index_priority
# randomly change direction every X% of the time
if random.random() * 100 < Config.direction_change_chance:
bounce_indexes = list(bounce_indexes.__reversed__())
# add safe area
safe_areas.append(start_rect.union(square.rect))
for direction_to_bounce in bounce_indexes:
square.dir[direction_to_bounce] *= -1
bounces_so_far.append(Bounce(square.pos, square.dir, t, direction_to_bounce))
toextend = recurs(
square=square.copy(),
notes=notes[1:],
bounces_so_far=[_b.copy() for _b in bounces_so_far],
t=t,
prev_index_priority=bounce_indexes.copy(),
depth=depth+1
)
if toextend:
return toextend
else:
bounces_so_far = bounces_so_far[:-1]
square.dir[direction_to_bounce] *= -1
# instead of trying other path from here, just exit a bit back to try another from previous
if force_return:
force_return -= 1
while len(path) != path_segment_start:
path.pop()
return False
continue
while len(path) != path_segment_start:
path.pop()
return False
othercheck = False
if len(bounces_so_far):
othercheck = bounces_so_far[-1].get_collision_rect().collidelist(path[:-10])+1
if square.rect.collidelist(all_bounce_rects) != -1 or othercheck:
if depth > 200:
if random.random() < Config.backtrack_chance:
max_percent -= (Config.backtrack_amount * 100 // total_notes) + 1
force_return = Config.backtrack_amount
while len(path) != path_segment_start:
path.pop()
return False
_start_notes = _start_notes[:Config.max_notes] if Config.max_notes is not None else _start_notes
self.scorekeeper.unhit_notes = remove_too_close_values([_sn for _sn in _start_notes], Config.bounce_min_spacing)
self.future_bounces = recurs(
square=self.square.copy(),
notes=remove_too_close_values(
[_sn for _sn in _start_notes],
threshold=Config.bounce_min_spacing
)
)
if self.future_bounces is False:
raise MapLoadingFailureError("The map failed to generate because of the recursion function. " +
"If the midi has too many notes too close, it may not generate. " +
"Maybe try changing the \"bounce min spacing\", \"square speed\" or \"change dir chance\" in the config")
if len(self.future_bounces) == 0:
raise MapLoadingFailureError("Map safearea list empty. Please report to the github under the issues tab")
percent_update_callback("Removing overlapping safe areas")
# eliminate fully overlapping safe areas
safe_areas: list[pygame.Rect]
while True:
new = []
before_safe_count = len(safe_areas)
for safe1 in safe_areas:
for safe2 in safe_areas:
if safe2 == safe1:
continue
if safe2.contains(safe1):
break
else:
new.append(safe1)
safe_areas = new.copy()
after_safe_count = len(safe_areas)
if after_safe_count == before_safe_count:
break
safe_areas = safe_areas
self.rectangles = [_fb.get_collision_rect() for _fb in self.future_bounces]
self.collision_times = [_fb.time for _fb in self.future_bounces]
# Setting random colors for the generated pegs
self.colors = [random.choice([(224, 50, 50), (80, 210, 100), (230, 220, 50), (174, 170, 210), (245, 77, 247), (255, 153, 0)]) for _ in self.future_bounces]
return safe_areas