forked from adangert/JoustMania
/
tournament.py
513 lines (427 loc) · 18.2 KB
/
tournament.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import common
import psmove
import time
import psutil, os
import random
import numpy
import math
import json
from piaudio import Audio
from enum import Enum
from multiprocessing import Process, Value, Array
# How fast/slow the music can go
SLOW_MUSIC_SPEED = 0.5
#this was 0.5
FAST_MUSIC_SPEED = 2.0
# The min and max timeframe in seconds for
# the speed change to trigger, randomly selected
MIN_MUSIC_FAST_TIME = 4
MAX_MUSIC_FAST_TIME = 8
MIN_MUSIC_SLOW_TIME = 10
MAX_MUSIC_SLOW_TIME = 23
END_MIN_MUSIC_FAST_TIME = 6
END_MAX_MUSIC_FAST_TIME = 10
END_MIN_MUSIC_SLOW_TIME = 8
END_MAX_MUSIC_SLOW_TIME = 12
#Default Sensitivity of the contollers
#These are changed from the options in common
SLOW_MAX = 1
SLOW_WARNING = 0.28
FAST_MAX = 1.8
FAST_WARNING = 0.8
#How long the speed change takes
INTERVAL_CHANGE = 1.5
#How long the winning moves shall sparkle
END_GAME_PAUSE = 6
KILL_GAME_PAUSE = 4
def track_move(move_serial, move_num, team, team_num, dead_move, force_color, music_speed, show_team_colors, invincibility):
#proc = psutil.Process(os.getpid())
#proc.nice(3)
#explosion = Audio('audio/Joust/sounds/Explosion34.wav')
#explosion.start_effect()
start = False
no_rumble = time.time() + 1
move_last_value = None
move = common.get_move(move_serial, move_num)
team_colors = common.generate_colors(team_num)
vibrate = False
vibration_time = time.time() + 1
flash_lights = True
flash_lights_timer = 0
start_inv = False
change_arr = [0,0,0]
#keep on looping while move is not dead
while True:
if show_team_colors.value == 1:
if team.value != -1:
move.set_leds(*team_colors[team.value])
else:
move.set_leds(100,100,100)
move.update_leds()
elif sum(force_color) != 0:
no_rumble_time = time.time() + 5
time.sleep(0.01)
move.set_leds(*force_color)
if sum(force_color) == 30:
move.set_leds(0, 0, 0)
move.set_rumble(0)
move.update_leds()
no_rumble = time.time() + 0.5
elif dead_move.value == 1 and team.value != -1:
if move.poll():
ax, ay, az = move.get_accelerometer_frame(psmove.Frame_SecondHalf)
total = sum([ax, ay, az])
if move_last_value is not None:
change_real = abs(move_last_value - total)
change_arr[0] = change_arr[1]
change_arr[1] = change_arr[2]
change_arr[2] = change_real
change = (change_arr[0] + change_arr[1]+change_arr[2])/3
speed_percent = (music_speed.value - SLOW_MUSIC_SPEED)/(FAST_MUSIC_SPEED - SLOW_MUSIC_SPEED)
warning = common.lerp(SLOW_WARNING, FAST_WARNING, speed_percent)
threshold = common.lerp(SLOW_MAX, FAST_MAX, speed_percent)
if not start_inv and invincibility.value == 1:
start_inv = True
vibrate = True
vibration_time = time.time() + 3
if vibrate:
flash_lights_timer += 1
if flash_lights_timer > 7:
flash_lights_timer = 0
flash_lights = not flash_lights
if flash_lights:
move.set_leds(100,100,100)
else:
if team.value != -1:
move.set_leds(*team_colors[team.value])
else:
move.set_leds(100,100,100)
if time.time() < vibration_time - 0.22:
move.set_rumble(110)
else:
move.set_rumble(0)
if time.time() > vibration_time:
vibrate = False
start_inv = False
invincibility.value = 0
else:
if team.value != -1:
move.set_leds(*team_colors[team.value])
else:
move.set_leds(100,100,100)
if invincibility.value == 0:
if change > threshold:
if time.time() > no_rumble:
move.set_leds(0,0,0)
move.set_rumble(90)
dead_move.value = 0
elif change > warning and not vibrate:
if time.time() > no_rumble:
vibrate = True
vibration_time = time.time() + 0.5
move.set_leds(20,50,100)
move_last_value = total
move.update_leds()
else:
if dead_move.value < 1:
move.set_leds(0,0,0)
elif team.value == -1:
move.set_leds(100,100,100)
move.update_leds()
time.sleep(0.5)
move.set_rumble(0)
class Tournament():
def __init__(self, moves, speed, command_queue, status_ns, audio_toggle, music):
print("speed is {}".format(speed))
global SLOW_MAX
global SLOW_WARNING
global FAST_MAX
global FAST_WARNING
SLOW_MAX = common.SLOW_MAX[speed]
SLOW_WARNING = common.SLOW_WARNING[speed]
FAST_MAX = common.FAST_MAX[speed]
FAST_WARNING = common.FAST_WARNING[speed]
self.audio_toggle = audio_toggle
self.move_serials = moves
self.tracked_moves = {}
self.dead_moves = {}
self.music_speed = Value('d', 1.5)
self.running = True
self.force_move_colors = {}
self.invince_moves = {}
self.start_timer = time.time()
self.audio_cue = 0
self.num_dead = 0
self.show_team_colors = Value('i', 0)
self.teams = {}
self.command_queue = command_queue
self.status_ns = status_ns
self.update_time = 0
#self.team_num = math.ceil(len(moves)/2)
self.team_num = len(moves)
self.generate_random_teams(self.team_num)
self.tourney_list = self.generate_tourney_list(len(moves))
fast_resample = False
if self.audio_toggle:
print("tourney list is " + str(self.tourney_list))
## music = 'audio/Joust/music/' + random.choice(os.listdir('audio/Joust/music'))
self.start_beep = Audio('audio/Joust/sounds/start.wav')
self.start_game = Audio('audio/Joust/sounds/start3.wav')
self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
end = False
self.audio = music
#self.change_time = self.get_change_time(speed_up = True)
self.speed_up = True
self.currently_changing = False
self.game_end = False
self.winning_moves = []
self.game_loop()
def generate_tourney_list(self, player_num):
def divide(arr, depth, m):
if len(complements) <= depth:
complements.append(2 ** (depth + 2) + 1)
complement = complements[depth]
for i in range(2):
if complement - arr[i] <= m:
arr[i] = [arr[i], complement - arr[i]]
divide(arr[i], depth + 1, m)
m = player_num
arr = [1, 2]
complements = []
divide(arr, 0, m)
dup_serials = self.move_serials[:]
def insert_move(arr):
for i in range(2):
if type(arr[i]) is list:
insert_move(arr[i])
else:
arr[i] = random.choice(dup_serials)
dup_serials.remove(arr[i])
insert_move(arr)
return arr
def generate_random_teams(self, team_num):
team_pick = list(range(team_num))
for serial in self.move_serials:
random_choice = Value('i', random.choice(team_pick) )
self.teams[serial] = random_choice
team_pick.remove(random_choice.value)
if not team_pick:
team_pick = list(range(team_num))
def track_moves(self):
for move_num, move_serial in enumerate(self.move_serials):
time.sleep(0.02)
dead_move = Value('i', 1)
force_color = Array('i', [1] * 3)
invincibility = Value('i', 0)
proc = Process(target=track_move, args=(move_serial,
move_num,
self.teams[move_serial],
self.team_num,
dead_move,
force_color,
self.music_speed,
self.show_team_colors,
invincibility))
proc.start()
self.invince_moves[move_serial] = invincibility
self.tracked_moves[move_serial] = proc
self.dead_moves[move_serial] = dead_move
self.force_move_colors[move_serial] = force_color
def change_all_move_colors(self, r, g, b):
for color in self.force_move_colors.values():
common.change_color(color, r, g, b)
#need to do the count_down here
def count_down(self):
self.change_all_move_colors(80, 0, 0)
if self.audio_toggle:
self.start_beep.start_effect()
time.sleep(0.75)
self.change_all_move_colors(70, 100, 0)
if self.audio_toggle:
self.start_beep.start_effect()
time.sleep(0.75)
self.change_all_move_colors(0, 70, 0)
if self.audio_toggle:
self.start_beep.start_effect()
time.sleep(0.75)
self.change_all_move_colors(0, 0, 0)
if self.audio_toggle:
self.start_game.start_effect()
def get_change_time(self, speed_up):
min_moves = len(self.move_serials) - 2
if min_moves <= 0:
min_moves = 1
game_percent = (self.num_dead/min_moves)
if game_percent > 1.0:
game_percent = 1.0
min_music_fast = common.lerp(MIN_MUSIC_FAST_TIME, END_MIN_MUSIC_FAST_TIME, game_percent)
max_music_fast = common.lerp(MAX_MUSIC_FAST_TIME, END_MAX_MUSIC_FAST_TIME, game_percent)
min_music_slow = common.lerp(MIN_MUSIC_SLOW_TIME, END_MIN_MUSIC_SLOW_TIME, game_percent)
max_music_slow = common.lerp(MAX_MUSIC_SLOW_TIME, END_MAX_MUSIC_SLOW_TIME, game_percent)
if speed_up:
added_time = random.uniform(min_music_fast, max_music_fast)
else:
added_time = random.uniform(min_music_slow, max_music_slow)
return time.time() + added_time
def change_music_speed(self, fast):
change_percent = numpy.clip((time.time() - self.change_time)/INTERVAL_CHANGE, 0, 1)
if fast:
self.music_speed.value = common.lerp(FAST_MUSIC_SPEED, SLOW_MUSIC_SPEED, change_percent)
elif not fast:
self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED, FAST_MUSIC_SPEED, change_percent)
self.audio.change_ratio(self.music_speed.value)
def check_music_speed(self):
if time.time() > self.change_time and time.time() < self.change_time + INTERVAL_CHANGE:
self.change_music_speed(self.speed_up)
self.currently_changing = True
self.audio.change_chunk_size(True)
elif time.time() >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
self.speed_up = not self.speed_up
self.change_time = self.get_change_time(speed_up = self.speed_up)
self.audio.change_ratio(self.music_speed.value)
self.currently_changing = False
self.audio.change_chunk_size(False)
def check_matches(self):
#do this only when a controller dies, or at the beginning
def check_moves(arr):
if (type(arr[0]) is not list and type(arr[1]) is not list):
if self.teams[arr[1]].value != -1:
self.teams[arr[0]].value = self.teams[arr[1]].value
else:
self.teams[arr[1]].value = self.teams[arr[0]].value
elif(type(arr[0]) is not list and type(arr[1]) is list):
self.teams[arr[0]].value = -1
check_moves(arr[1])
elif(type(arr[1]) is not list and type(arr[0]) is list):
self.teams[arr[1]].value = -1
check_moves(arr[0])
elif(type(arr[0]) is list and type(arr[1]) is list):
check_moves(arr[0])
check_moves(arr[1])
check_moves(self.tourney_list)
def remove_dead_player(self, dead_serial):
def remove_dead(arr):
if type(arr) is list and dead_serial in arr:
arr.remove(dead_serial)
else:
if type(arr[0]) is list:
remove_dead(arr[0])
if type(arr[1]) is list:
remove_dead(arr[1])
remove_dead(self.tourney_list)
def move_up(arr):
if type(arr) is list and len(arr) == 1:
return arr[0]
else:
if type(arr[0]) is list and move_up(arr[0]):
arr[0] = move_up(arr[0])
if type(arr[1]) is not list:
self.teams[arr[1]].value = self.teams[arr[0]].value
self.invince_moves[arr[1]].value = 1
self.invince_moves[arr[0]].value = 1
else:
self.teams[arr[0]].value = -1
elif type(arr[1]) is list and move_up(arr[1]):
arr[1] = move_up(arr[1])
if type(arr[0]) is not list:
self.teams[arr[0]].value = self.teams[arr[1]].value
self.invince_moves[arr[1]].value = 1
self.invince_moves[arr[0]].value = 1
else:
self.teams[arr[1]].value = -1
move_up(self.tourney_list)
def check_end_game(self):
self.winning_moves = []
for move_serial, dead in self.dead_moves.items():
#if we are alive
if dead.value == 1:
self.winning_moves.append(move_serial)
if dead.value == 0:
self.remove_dead_player(move_serial)
#This is to play the sound effect
self.num_dead += 1
dead.value = -1
if self.audio_toggle:
self.explosion.start_effect()
if len(self.winning_moves) <= 1:
self.game_end = True
def stop_tracking_moves(self):
for proc in self.tracked_moves.values():
proc.terminate()
proc.join()
time.sleep(0.02)
def end_game(self):
self.audio.stop_audio()
self.update_status('ending')
end_time = time.time() + END_GAME_PAUSE
h_value = 0
while (time.time() < end_time):
time.sleep(0.01)
win_color = common.hsv2rgb(h_value, 1, 1)
for win_move in self.winning_moves:
win_color_array = self.force_move_colors[win_move]
common.change_color(win_color_array, *win_color)
h_value = (h_value + 0.01)
if h_value >= 1:
h_value = 0
self.running = False
def game_loop(self):
self.track_moves()
self.show_team_colors.value = 0
self.count_down()
self.change_time = time.time() + 6
time.sleep(0.02)
if self.audio_toggle:
self.audio.start_audio_loop()
else:
#when no audio is playing set the music speed to middle speed
self.music_speed.value = (FAST_MUSIC_SPEED + SLOW_MUSIC_SPEED) / 2
time.sleep(0.8)
self.check_matches()
while self.running:
#I think the loop is so fast that this causes
#a crash if done every loop
if time.time() - 0.1 > self.update_time:
self.update_time = time.time()
self.check_command_queue()
self.update_status('in_game')
if self.audio_toggle:
self.check_music_speed()
self.check_end_game()
if self.game_end:
self.end_game()
self.stop_tracking_moves()
def check_command_queue(self):
package = None
while not(self.command_queue.empty()):
package = self.command_queue.get()
command = package['command']
if not(package == None):
if command == 'killgame':
self.kill_game()
def kill_game(self):
if self.audio_toggle:
try:
self.audio.stop_audio()
except:
print('no audio loaded to stop')
self.update_status('killed')
all_moves = [x for x in self.dead_moves.keys()]
end_time = time.time() + KILL_GAME_PAUSE
h_value = 0
while (time.time() < end_time):
time.sleep(0.01)
color = common.hsv2rgb(h_value, 1, 1)
for move in all_moves:
color_array = self.force_move_colors[move]
common.change_color(color_array, *color)
h_value = (h_value + 0.01)
if h_value >= 1:
h_value = 0
self.running = False
def update_status(self,game_status,winning_team=-1):
data ={'game_status' : game_status,
'game_mode' : 'Tournament',
'winning_team' : winning_team}
self.status_ns.status_dict = data