diff --git a/common.py b/common.py index 310fdb97..53027ccc 100644 --- a/common.py +++ b/common.py @@ -37,6 +37,7 @@ def get_move(serial, move_num): for move_num in range(psmove.count_connected()): move = psmove.PSMove(move_num) if move.get_serial() == serial: + print("returning " +str(move.get_serial())) return move return None else: diff --git a/controller_process.py b/controller_process.py new file mode 100644 index 00000000..46324cea --- /dev/null +++ b/controller_process.py @@ -0,0 +1,34 @@ +from games import ffa, zombie, commander, swapper, tournament, speed_bomb, fight_club +import common, colors, joust, webui, piparty +#this should all be refactored to use the same options per game +def main_track_move(menu, restart, move_serial, move_num, move_opts, force_color, battery, dead_count, game_mode, \ + team, team_color_enum, dead_move, music_speed, werewolf_reveal, show_team_colors, red_on_kill, zombie_opt,\ + commander_intro, commander_move_opt, commander_powers, commander_overdrive,five_controller_opt, swapper_team_colors,\ + invincibility, fight_club_color, num_teams,bomb_color,game_start,false_color, faked, rumble): + print("starting Controller Process") + + move = common.get_move(move_serial, move_num) + while(True): + move.set_rumble(0) + if(restart.value == 1): + pass + elif (menu.value == 1): + piparty.track_move(move_serial, move_num, move, move_opts, force_color, battery, dead_count, restart, menu) + elif(game_mode.value == common.Games.Zombies.value): + zombie.track_controller(move, zombie_opt, restart, menu) + elif(game_mode.value == common.Games.Commander.value): + commander.track_move(move, team.value, dead_move, force_color, music_speed, commander_intro, \ + commander_move_opt, commander_powers, commander_overdrive, restart, menu) + elif(game_mode.value == common.Games.Swapper.value): + swapper.track_move(move, team, 2, swapper_team_colors, \ + dead_move, force_color, music_speed, five_controller_opt, restart, menu) + elif(game_mode.value == common.Games.FightClub.value): + fight_club.track_move(move, dead_move, force_color, music_speed, fight_club_color, invincibility, menu, restart) + elif(game_mode.value == common.Games.Tournament.value): + tournament.track_move(move, team, num_teams.value, dead_move, force_color, music_speed, show_team_colors, invincibility, menu, restart) + elif(game_mode.value == common.Games.Ninja.value): + speed_bomb.track_move(move, dead_move, force_color,bomb_color, five_controller_opt, game_start, false_color, faked, rumble, menu, restart) + else: + joust.track_move(move, game_mode.value, team.value, team_color_enum, dead_move, force_color, \ + music_speed, werewolf_reveal, show_team_colors, red_on_kill, restart, menu) + diff --git a/games/commander.py b/games/commander.py index 0a24cd6b..c212d8c1 100644 --- a/games/commander.py +++ b/games/commander.py @@ -81,16 +81,18 @@ def calculate_flash_time(r,g,b, score): new_b = int(common.lerp(255, b, flash_percent)) return (new_r, new_g, new_b) -def track_move(move_serial, move_num, team, num_teams, dead_move, force_color, music_speed, commander_intro, move_opts, power, overdrive): - #proc = psutil.Process(os.getpid()) - #proc.nice(3) - - +def track_move(move, team, dead_move, force_color, music_speed, commander_intro, move_opts, commander_powers, commander_overdrive, restart, menu): + if team == Team.alpha.value: + overdrive = commander_overdrive[0] + power = commander_powers[0] + else: + overdrive = commander_overdrive[1] + power = commander_powers[1] + start = False no_rumble = time.time() + 1 move_last_value = None - move = common.get_move(move_serial, move_num) - team_colors = colors.generate_colors(num_teams) + team_colors = colors.generate_colors(2) #keep on looping while move is not dead ready = False move.set_leds(0,0,0) @@ -130,6 +132,8 @@ def track_move(move_serial, move_num, team, num_teams, dead_move, force_color, m move_opts[Opts.selection.value] = Selections.nothing.value while True: + if(restart.value == 1 or menu.value == 1): + return if sum(force_color) != 0: no_rumble_time = time.time() + 5 time.sleep(0.01) @@ -228,7 +232,8 @@ def track_move(move_serial, move_num, team, num_teams, dead_move, force_color, m class Commander(): - def __init__(self, moves, command_queue, ns, music): + def __init__(self, moves, command_queue, ns, music, dead_moves, commander_intro, commander_move_opt,\ + commander_powers, commander_overdrive, music_speed,force_color, restart, controller_teams): self.command_queue = command_queue self.ns = ns @@ -250,42 +255,45 @@ def __init__(self, moves, command_queue, ns, music): self.move_serials = moves self.tracked_moves = {} - self.dead_moves = {} + self.dead_moves = dead_moves self.teams = {} - self.music_speed = Value('d', 1) + self.music_speed = music_speed + self.music_speed.value = 1 self.running = True - self.force_move_colors = {} + self.force_move_colors = force_color self.num_teams = 2 self.werewolf_timer = 35 self.start_timer = time.time() self.audio_cue = 0 - self.move_opts = {} + self.move_opts = commander_move_opt self.current_commander = ["",""] self.time_to_power = [20,20] self.activated_time = [time.time(), time.time()] self.activated_overdrive = [time.time(), time.time()] + self.controller_teams = controller_teams - - self.powers = [Value('d', 0.0), Value('d', 0.0)] + self.powers = commander_powers + self.powers[0].value = 0.0 + self.powers[1].value = 0.0 + self.restart = restart - self.alpha_overdrive = Value('i', 0) - self.bravo_overdrive = Value('i', 0) + self.alpha_overdrive = commander_overdrive[0] + self.alpha_overdrive.value = 0 + self.bravo_overdrive = commander_overdrive[1] + self.alpha_overdrive.value = 1 self.generate_random_teams(self.num_teams) - self.commander_intro = Value('i', 1) + self.commander_intro = commander_intro + self.commander_intro.value = 1 self.powers_active = [False, False] -## try: -## music = 'audio/Commander/music/' + random.choice(os.listdir('audio/Commander/music')) -## except: -## print('no music in audio/Commander/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') @@ -322,32 +330,14 @@ def generate_random_teams(self, num_teams): 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) - opts = Array('i', [0] * 5) - power = self.powers[self.teams[move_serial]] - - if self.teams[move_serial] == Team.alpha.value: - overdrive = self.alpha_overdrive - else: - overdrive = self.bravo_overdrive - proc = Process(target=track_move, args=(move_serial, - move_num, - self.teams[move_serial], - self.num_teams, - dead_move, - force_color, - self.music_speed, - self.commander_intro, - opts, - power, - overdrive)) - proc.start() - self.tracked_moves[move_serial] = proc - self.dead_moves[move_serial] = dead_move - self.force_move_colors[move_serial] = force_color - self.move_opts[move_serial] = opts + self.controller_teams[move_serial].value = self.teams[move_serial] + self.dead_moves[move_serial].value = 1 + self.force_move_colors[move_serial][0] = 1 + self.force_move_colors[move_serial][1] = 1 + self.force_move_colors[move_serial][2] = 1 + for i in range(5): + self.move_opts[move_serial][i] = 0 + def change_all_move_colors(self, r, g, b): for color in self.force_move_colors.values(): @@ -408,10 +398,7 @@ def check_end_game(self): def stop_tracking_moves(self): - for proc in self.tracked_moves.values(): - proc.terminate() - proc.join() - time.sleep(0.02) + self.restart.value = 1 def end_game(self): try: @@ -563,6 +550,7 @@ def commander_intro_audio(self): def game_loop(self): self.track_moves() + self.restart.value = 0 self.commander_intro_audio() self.count_down() diff --git a/games/fight_club.py b/games/fight_club.py index d5466acd..ebfe75fb 100644 --- a/games/fight_club.py +++ b/games/fight_club.py @@ -46,16 +46,10 @@ KILL_GAME_PAUSE = 4 -def track_move(move_serial, move_num, dead_move, force_color, music_speed, color, invincibility): - #proc = psutil.Process(os.getpid()) - #proc.nice(3) - #explosion = Audio('audio/Joust/sounds/Explosion34.wav') - #explosion.start_effect() +def track_move(move, dead_move, force_color, music_speed, color, invincibility, menu, restart): start = False no_rumble = time.time() + 1 move_last_value = None - move = common.get_move(move_serial, move_num) - #team_colors = colors.generate_colors(num_teams) vibrate = False vibration_time = time.time() + 1 flash_lights = True @@ -65,12 +59,8 @@ def track_move(move_serial, move_num, dead_move, force_color, music_speed, color #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() + if(menu.value == 1 or restart.value == 1): + return if sum(force_color) != 0: no_rumble_time = time.time() + 5 time.sleep(0.01) @@ -138,7 +128,6 @@ def track_move(move_serial, move_num, dead_move, force_color, music_speed, color if not invincibility.value: if change > threshold: - #print("over threshold") if time.time() > no_rumble: move.set_leds(*colors.Colors.Red.value) move.set_rumble(90) @@ -170,7 +159,7 @@ def track_move(move_serial, move_num, dead_move, force_color, music_speed, color class Fight_club(): - def __init__(self, moves, command_queue, ns, music): + def __init__(self, moves, command_queue, ns, music, show_team_colors, music_speed, dead_moves, force_move_colors, invincible_moves, fight_club_colors, restart): self.command_queue = command_queue self.ns = ns @@ -192,17 +181,19 @@ def __init__(self, moves, command_queue, ns, music): self.move_serials = moves self.tracked_moves = {} - self.dead_moves = {} - self.music_speed = Value('d', 1.5) + self.dead_moves = dead_moves + self.music_speed = music_speed + self.music_speed.value = 1.5 #Value('d', 1.5) self.running = True - self.force_move_colors = {} - self.invince_moves = {} + self.force_move_colors = force_move_colors + self.invince_moves = invincible_moves self.start_timer = time.time() self.audio_cue = 0 self.num_dead = 0 - self.show_team_colors = Value('i', 0) + self.show_team_colors = show_team_colors + self.show_team_colors.value = 0 #Value('i', 0) self.teams = {} self.update_time = 0 @@ -229,13 +220,13 @@ def __init__(self, moves, command_queue, ns, music): #just for the sound effects self.revive_time = time.time() + 4 - self.colors = {} + self.colors = fight_club_colors + self.restart = restart fast_resample = False if self.play_audio: -## music = 'audio/Joust/music/' + random.choice(os.listdir('audio/Joust/music')) self.loud_beep = Audio('audio/Joust/sounds/beep_loud.wav') self.start_beep = Audio('audio/Joust/sounds/start.wav') self.start_game = Audio('audio/Joust/sounds/start3.wav') @@ -246,7 +237,6 @@ def __init__(self, moves, command_queue, ns, music): end = False self.audio = music - #self.change_time = self.get_change_time(speed_up = True) self.speed_up = True self.currently_changing = False @@ -268,24 +258,13 @@ def track_moves(self): for move_num, move_serial in enumerate(self.move_serials): time.sleep(0.1) - dead_move = Value('i', 0) + for i in range(3): + self.force_move_colors[move_serial][i] = 1 + + self.colors[move_serial].value = 0 + self.invince_moves[move_serial].value = True + self.dead_moves[move_serial].value = 0 - force_color = Array('i', [1] * 3) - invincibility = Value('b', True) - color = Value('i', 0) - proc = Process(target=track_move, args=(move_serial, - move_num, - dead_move, - force_color, - self.music_speed, - color, - invincibility)) - proc.start() - self.colors[move_serial] = color - 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(): @@ -388,8 +367,6 @@ def alive_move_count(self): #more than one tied winner, have them face off def face_off(self): - #print(self.winning_moves) - #os.popen('espeak -ven -p 70 -a 200 "Tie game..... Face off"') Audio('audio/Fight_Club/tie_game.wav').start_effect() for move in self.move_serials: @@ -438,17 +415,12 @@ def check_end_game(self): self.check_winner() if self.round_counter == self.round_num - 5: Audio('audio/Fight_Club/5_rounds.wav').start_effect() - #os.popen('espeak -ven -p 70 -a 200 "5 rounds remain"') if self.round_counter == self.round_num - 1: Audio('audio/Fight_Club/last_round.wav').start_effect() - #os.popen('espeak -ven -p 70 -a 200 "last round"') def stop_tracking_moves(self): - for proc in self.tracked_moves.values(): - proc.terminate() - proc.join() - time.sleep(0.02) + self.restart.value = 1 def end_game(self): self.audio.stop_audio() @@ -486,9 +458,7 @@ def check_next_fighter(self): self.dead_moves[self.chosen_defender].value = 0 self.colors[self.chosen_defender].value = 0 self.chosen_defender = self.chosen_fighter - #self.colors[self.chosen_defender].value = 1 self.chosen_fighter = self.fighter_list.pop() - #self.colors[self.chosen_fighter].value = 2 self.revive_fighters() self.reset_round_timer() @@ -502,7 +472,6 @@ def check_next_fighter(self): self.colors[self.chosen_fighter].value = 0 self.dead_moves[self.chosen_fighter].value = 0 self.chosen_fighter = self.fighter_list.pop() - #self.colors[self.chosen_fighter].value = 2 self.revive_fighters() self.reset_round_timer() @@ -559,18 +528,16 @@ def reset_round_timer(self): saying = random.randint(0,2) if saying == 0: Audio('audio/Fight_Club/defender_lead.wav').start_effect() - #os.popen('espeak -ven -p 70 -a 200 "Defender has taken the lead"') elif saying == 1: Audio('audio/Fight_Club/defender_winning.wav').start_effect() - #os.popen('espeak -ven -p 70 -a 200 "Defender is now winning"') elif saying == 2: Audio('audio/Fight_Club/Defender_high_score.wav').start_effect() - #os.popen('espeak -ven -p 70 -a 200 "Defender has the high score"') self.check_end_game() def game_loop(self): self.track_moves() + self.restart.value = 0 self.show_team_colors.value = 0 self.count_down() self.change_time = time.time() + 6 @@ -592,8 +559,6 @@ def game_loop(self): self.update_time = time.time() self.check_command_queue() self.update_status('in_game') - #if self.play_audio: - #self.check_music_speed() self.check_next_fighter() self.check_end_round() if self.game_end: diff --git a/games/speed_bomb.py b/games/speed_bomb.py index 5f585897..4ec6ae7e 100644 --- a/games/speed_bomb.py +++ b/games/speed_bomb.py @@ -50,16 +50,10 @@ def calculate_flash_time(r,g,b, score): new_b = int(common.lerp(255, b, flash_percent)) return (new_r, new_g, new_b) -def track_move(move_serial, move_num, dead_move, force_color,bomb_color, move_opts, game_start, false_color, faked, rumble): - #proc = psutil.Process(os.getpid()) - #proc.nice(3) - - +def track_move(move, dead_move, force_color,bomb_color, move_opts, game_start, false_color, faked, rumble, menu, restart): start = False no_rumble = time.time() + 1 move_last_value = None - move = common.get_move(move_serial, move_num) - #keep on looping while move is not dead ready = False move.set_leds(0,0,0) @@ -79,7 +73,9 @@ def track_move(move_serial, move_num, dead_move, force_color,bomb_color, move_op move_opts[Opts.holding.value] = Holding.not_holding.value move_opts[Opts.selection.value] = Selections.nothing.value - while not super_dead: + while True: + if(menu.value == 1 or restart.value == 1): + return if sum(force_color) != 0 and game_start.value == 1: no_rumble_time = time.time() + 5 time.sleep(0.01) @@ -104,7 +100,6 @@ def track_move(move_serial, move_num, dead_move, force_color,bomb_color, move_op if(move.get_trigger() > 50 and can_fake): faking = True - #move_opts[Opts.holding.value] = Holding.holding.value move_opts[Opts.selection.value] = Selections.false_trigger.value if (move.get_trigger() <= 127): @@ -112,14 +107,11 @@ def track_move(move_serial, move_num, dead_move, force_color,bomb_color, move_op col2 = int(common.lerp(fake_bomb_color[1], no_fake_bomb_color[1], (move.get_trigger()-50)/77)) col3 = int(common.lerp(fake_bomb_color[2], no_fake_bomb_color[2], (move.get_trigger()-50)/77)) move.set_leds(col1,col2,col3) - #if (move.get_trigger() > 127 and move.get_trigger() <= 140): - # move.set_leds(*no_fake_bomb_color) if (move.get_trigger() > 127): col1 = int(common.lerp(no_fake_bomb_color[0], fake_bomb_color[0], (move.get_trigger()-127)/128)) col2 = int(common.lerp(no_fake_bomb_color[1], fake_bomb_color[1], (move.get_trigger()-127)/128)) col3 = int(common.lerp(no_fake_bomb_color[2], fake_bomb_color[2], (move.get_trigger()-127)/128)) move.set_leds(col1,col2,col3) - #move.set_leds(0,200,0) else: @@ -127,10 +119,8 @@ def track_move(move_serial, move_num, dead_move, force_color,bomb_color, move_op if game_start.value == 0: move.set_leds(*force_color) move.update_leds() - #move_opts[Opts.holding.value] == Holding.not_holding.value if faking: - #move_opts[Opts.selection.value] = Selections.not_holding.value if game_start.value == 1: can_fake = False faking = False @@ -150,7 +140,6 @@ def track_move(move_serial, move_num, dead_move, force_color,bomb_color, move_op if game_start.value == 1 and false_color.value == 1: print("JUST DIED TO BEING FAKED!!!") faked.value = 1 - #dead_move.value -= 1 move.set_rumble(0) if move_opts[Opts.holding.value] == Holding.not_holding.value and (button in common.all_shapes): @@ -170,21 +159,18 @@ def track_move(move_serial, move_num, dead_move, force_color,bomb_color, move_op else: if super_dead == False: - #for i in range(100): - # time.sleep(0.01) - # move.set_leds(0,random.randrange(100, 200),0) - # move.set_rumble(200) - # move.update_leds() super_dead = True + move.set_rumble(0) move.set_leds(0,0,0) + time.sleep(2) move.update_leds() #if we are dead class Bomb(): - def __init__(self, moves, command_queue, ns, music): + def __init__(self, moves, command_queue, ns, music, bomb_color, game_start, five_move_opts, dead_moves, force_move_colors, false_colors, was_faked, rumble, music_speed,restart): self.command_queue = command_queue self.ns = ns @@ -192,23 +178,28 @@ def __init__(self, moves, command_queue, ns, music): self.play_audio = self.ns.settings['play_audio'] self.move_serials = moves self.tracked_moves = {} - self.dead_moves = {} + self.dead_moves = dead_moves self.alive_moves = [] self.teams = {} - self.music_speed = Value('d', 1) + self.music_speed = music_speed + self.music_speed.value = 1 self.running = True - self.force_move_colors = {} - self.bomb_color = Array('i', [0] * 3) + self.force_move_colors = force_move_colors + self.bomb_color = bomb_color + for i in range(3): + self.bomb_color[i] = 0 self.start_timer = time.time() self.audio_cue = 0 self.num_bombs = 2 - self.move_opts = {} - self.false_colors = {} - self.was_faked = {} - self.rumble = {} + self.move_opts = five_move_opts + self.false_colors = false_colors + self.was_faked = was_faked + self.rumble = rumble self.bomb_length = 5.0 + self.restart = restart - self.game_start = Value('i', 0) + self.game_start = game_start + self.game_start.value = 0 self.current_rand_holder = '' self.next_rand_holder = '' self.prev_rand_holder = '' @@ -217,10 +208,6 @@ def __init__(self, moves, command_queue, ns, music): self.update_time = 0 if self.play_audio: -## try: -## music = 'audio/Commander/music/' + random.choice(os.listdir('audio/Commander/music')) -## except: -## print('no music in audio/Commander/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') @@ -256,16 +243,11 @@ def get_prev_random_holder(self): def get_next_random_holder(self): - #if self.current_rand_holder != self.bomb_serial: - #self.current_rand_holder = self.move_serials[random.choice(range(len(self.move_serials)))] - #while self.current_rand_holder == self.bomb_serial: - # self.current_rand_holder = self.move_serials[random.choice(range(len(self.move_serials)))] if self.next_rand_holder == self.bomb_serial: self.next_rand_holder = self.move_serials[random.choice(range(len(self.move_serials)))] while self.next_rand_holder == self.bomb_serial or self.dead_moves[self.next_rand_holder].value <= 0: self.next_rand_holder = self.move_serials[random.choice(range(len(self.move_serials)))] self.current_rand_holder = self.bomb_serial - #print("returning " + self.next_rand_holder) return self.next_rand_holder @@ -278,10 +260,6 @@ def get_next_bomb_holder(self, serial=None): holder = random.choice(range(len(self.move_serials))) while True: yield self.get_next_random_holder() - #new_serial = self.move_serials[holder] - #if self.dead_moves[new_serial].value > 0: - # yield new_serial - #holder = (holder +1) % len(self.move_serials) def reset_bomb_time(self): @@ -290,6 +268,7 @@ def reset_bomb_time(self): def game_loop(self): self.track_moves() + self.restart.value = 0 self.rotate_colors() self.bomb_serial = self.move_serials[random.choice(range(len(self.move_serials)))] self.next_rand_holder = self.bomb_serial @@ -380,8 +359,6 @@ def pause_for_player_death(self, dead_move, faker_move=None): faker_color_array = self.force_move_colors[faker_move] for move_serial in self.move_serials: - #if move_serial == faker_move: - # colors.change_color(faker_color_array, random.randrange(100, 200), 10, 10) if move_serial == dead_move: colors.change_color(dead_color_array, 10, random.randrange(100, 200), 10) self.rumble[move_serial].value = 150 @@ -453,25 +430,6 @@ def check_faked_out(self): if self.move_opts[prev_faker][Opts.holding.value] == Holding.not_holding.value: self.false_colors[move_serial].value = 0 - #Probably should get rid of this, or only when we are being faked out - #elif self.false_colors[move_serial].value == 0 and self.move_opts[move_serial][Opts.holding.value] == Holding.holding.value : - # if self.move_opts[move_serial][Opts.selection.value] == Selections.counter.value and self.move_opts[self.get_prev_serial(move_serial)][Opts.has_bomb.value] == Bool.yes.value: - - # self.explosion40.start_effect() - # self.Fakecountered.start_effect() - # self.pause_for_player_death(move_serial) - - # self.dead_moves[move_serial].value -= 1 - #self.move_opts[move_serial][Opts.holding.value] = Holding.holding.value - - # self.reset_bomb_length() - # self.reset_bomb_time() - - # self.move_bomb() - # print("JUST DIED TO PRESSING COUNTER") - - #check for faked - def get_next_serial(self, serial): @@ -480,9 +438,6 @@ def get_next_serial(self, serial): pos = (self.get_serial_pos(serial) + 1) % len(self.move_serials) - #pos = random.choice(range(len(self.move_serials))) - #while random_move == pos - 1: - # random_move = random.choice(range(len(self.move_serials))) new_serial = self.move_serials[pos] while self.dead_moves[new_serial].value == 0: @@ -510,33 +465,14 @@ def get_serial_pos(self, serial): def track_moves(self): for move_num, move_serial in enumerate(self.move_serials): self.alive_moves.append(move_serial) - time.sleep(0.02) - dead_move = Value('i', 2) - force_color = Array('i', [1] * 3) - false_color = Value('i', 0) - - opts = Array('i', [0] * 5) - faked = Value('i', 0) - rumble = Value('i', 0) - - proc = Process(target=track_move, args=(move_serial, - move_num, - dead_move, - force_color, - self.bomb_color, - opts, - self.game_start, - false_color, - faked, - rumble)) - proc.start() - self.tracked_moves[move_serial] = proc - self.dead_moves[move_serial] = dead_move - self.force_move_colors[move_serial] = force_color - self.move_opts[move_serial] = opts - self.false_colors[move_serial] = false_color - self.was_faked[move_serial] = faked - self.rumble[move_serial] = rumble + self.dead_moves[move_serial].value = 2 + for i in range(3): + self.force_move_colors[move_serial][i] = 1 + for i in range(5): + self.move_opts[move_serial][i] = 0 + self.false_colors[move_serial].value = 0 + self.was_faked[move_serial].value = 0 + self.rumble[move_serial].value = 0 def rotate_colors(self): @@ -596,12 +532,6 @@ def check_dead_moves(self): self.move_opts[alive_serial][Opts.has_bomb.value] = Bool.no.value self.move_bomb() - #for i, bomb_serial in enumerate(self.bomb_serials): - #if self.bomb_serial == alive_serial: - # self.bomb_serial = next(self.bomb_generators[i]) - #self.move_opts[self.bomb_serial][Opts.has_bomb.value] = Bool.yes.value - #remove alive move: - self.alive_moves.remove(alive_serial) if self.play_audio: self.explosion.start_effect() @@ -611,10 +541,7 @@ def check_dead_moves(self): self.end_game() def stop_tracking_moves(self): - for proc in self.tracked_moves.values(): - proc.terminate() - proc.join() - time.sleep(0.02) + self.restart.value = 1 def end_game(self): if self.play_audio: diff --git a/games/swapper.py b/games/swapper.py index bc2a5c67..2614efef 100644 --- a/games/swapper.py +++ b/games/swapper.py @@ -79,14 +79,10 @@ def calculate_flash_time(r,g,b, score): new_b = int(common.lerp(255, b, flash_percent)) return (new_r, new_g, new_b) -def track_move(move_serial, move_num, team, num_teams, team_colors, dead_move, force_color, music_speed, move_opts): - #proc = psutil.Process(os.getpid()) - #proc.nice(3) - +def track_move(move, team, num_teams, team_colors, dead_move, force_color, music_speed, move_opts, restart, menu): start = False no_rumble = time.time() + 1 move_last_value = None - move = common.get_move(move_serial, move_num) #keep on looping while move is not dead ready = False move.set_leds(*colors.Colors.Black.value) @@ -104,6 +100,8 @@ def track_move(move_serial, move_num, team, num_teams, team_colors, dead_move, f move_opts[Opts.selection.value] = Selections.nothing.value while True: + if(menu.value == 1 or restart.value == 1): + return if sum(force_color) != 0: no_rumble_time = time.time() + 5 time.sleep(0.01) @@ -137,7 +135,11 @@ def track_move(move_serial, move_num, team, num_teams, team_colors, dead_move, f if flash_lights: move.set_leds(*colors.Colors.White60.value) else: - move.set_leds(*team_colors[team.value].value) + if(team.value == 0): + + move.set_leds(team_colors[0],team_colors[1],team_colors[2]) + else: + move.set_leds(team_colors[3],team_colors[4],team_colors[5]) if time.time() < vibration_time - 0.22: move.set_rumble(110) else: @@ -145,7 +147,11 @@ def track_move(move_serial, move_num, team, num_teams, team_colors, dead_move, f if time.time() > vibration_time: vibrate = False else: - move.set_leds(*team_colors[team.value].value) + if(team.value == 0): + + move.set_leds(team_colors[0],team_colors[1],team_colors[2]) + else: + move.set_leds(team_colors[3],team_colors[4],team_colors[5]) if change > threshold: @@ -183,7 +189,7 @@ def track_move(move_serial, move_num, team, num_teams, team_colors, dead_move, f class Swapper(): - def __init__(self, moves, command_queue, ns, music): + def __init__(self, moves, command_queue, ns, music, controller_colors, dead_moves, music_speed, force_move_colors, controller_opts, controller_teams, restart): self.command_queue = command_queue self.ns = ns @@ -207,26 +213,35 @@ def __init__(self, moves, command_queue, ns, music): self.move_serials = moves self.tracked_moves = {} - self.dead_moves = {} - self.teams = {} - self.music_speed = Value('d', 1) + self.dead_moves = dead_moves + self.teams = controller_teams + self.music_speed = music_speed + self.music_speed.value = 1 self.running = True - self.force_move_colors = {} + self.force_move_colors = force_move_colors self.num_teams = 2 + self.restart = restart self.start_timer = time.time() self.audio_cue = 0 - self.move_opts = {} + self.move_opts = controller_opts self.update_time = 0 self.team_colors = colors.generate_team_colors(self.num_teams,self.color_lock,self.color_lock_choices) + self.controller_colors = controller_colors + self.controller_colors[0] = self.team_colors[0].value[0] + self.controller_colors[1] = self.team_colors[0].value[1] + self.controller_colors[2] = self.team_colors[0].value[2] + + self.controller_colors[3] = self.team_colors[1].value[0] + self.controller_colors[4] = self.team_colors[1].value[1] + self.controller_colors[5] = self.team_colors[1].value[2] self.generate_random_teams(self.num_teams) if self.play_audio: -## 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') @@ -238,7 +253,6 @@ def __init__(self, moves, command_queue, ns, music): except: print('no audio loaded') - #self.change_time = self.get_change_time(speed_up = True) self.change_time = time.time() + 8 self.speed_up = True self.currently_changing = False @@ -251,37 +265,22 @@ def generate_random_teams(self, num_teams): players_per_team = (len(self.move_serials)//num_teams)+1 team_num = [x for x in range(num_teams)]*players_per_team for num,move in zip(team_num,self.move_serials): - self.teams[move] = Value('i',num) + self.teams[move].value = num# = Value('i',num) else: team_pick = list(range(num_teams)) 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) + self.teams[serial].value = random.choice(team_pick) # = random_choice + team_pick.remove(self.teams[serial].value) if not team_pick: team_pick = list(range(num_teams)) 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) - opts = Array('i', [0] * 5) - proc = Process(target=track_move, args=(move_serial, - move_num, - self.teams[move_serial], - self.num_teams, - self.team_colors, - dead_move, - force_color, - self.music_speed, - - opts)) - proc.start() - self.tracked_moves[move_serial] = proc - self.dead_moves[move_serial] = dead_move - self.force_move_colors[move_serial] = force_color - self.move_opts[move_serial] = opts + self.dead_moves[move_serial].value = 1 + for i in range(3): + self.force_move_colors[move_serial][i] = 1 + for i in range(5): + self.move_opts[move_serial][i]=0 def change_all_move_colors(self, r, g, b): for color in self.force_move_colors.values(): @@ -325,10 +324,7 @@ def check_end_game(self): def stop_tracking_moves(self): - for proc in self.tracked_moves.values(): - proc.terminate() - proc.join() - time.sleep(0.02) + self.restart.value = 1 def end_game(self): if self.play_audio: @@ -378,6 +374,7 @@ def end_game_sound(self, winning_team): def game_loop(self): self.track_moves() + self.restart.value = 0 self.count_down() if self.play_audio: try: diff --git a/games/tournament.py b/games/tournament.py index 4380288f..24b1d81d 100644 --- a/games/tournament.py +++ b/games/tournament.py @@ -45,15 +45,10 @@ KILL_GAME_PAUSE = 4 -def track_move(move_serial, move_num, team, num_teams, 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() +def track_move(move, team, num_teams, dead_move, force_color, music_speed, show_team_colors, invincibility, menu, restart): start = False no_rumble = time.time() + 1 move_last_value = None - move = common.get_move(move_serial, move_num) team_colors = colors.generate_colors(num_teams) vibrate = False vibration_time = time.time() + 1 @@ -64,6 +59,8 @@ def track_move(move_serial, move_num, team, num_teams, dead_move, force_color, m #keep on looping while move is not dead while True: + if(menu.value == 1 or restart.value == 1): + return if show_team_colors.value == 1: if team.value != -1: move.set_leds(*team_colors[team.value]) @@ -154,7 +151,7 @@ def track_move(move_serial, move_num, team, num_teams, dead_move, force_color, m class Tournament(): - def __init__(self, moves, command_queue, ns, music): + def __init__(self, moves, command_queue, ns, music, show_team_colors, music_speed, teams, dead_moves, force_move_colors, invincible_moves, num_teams, restart): self.command_queue = command_queue self.ns = ns @@ -176,22 +173,26 @@ def __init__(self, moves, command_queue, ns, music): self.move_serials = moves self.tracked_moves = {} - self.dead_moves = {} - self.music_speed = Value('d', 1.5) + self.dead_moves = dead_moves + self.music_speed = music_speed + self.music_speed.value = 1.5 # Value('d', 1.5) self.running = True - self.force_move_colors = {} - self.invince_moves = {} + self.force_move_colors = force_move_colors + self.invince_moves = invincible_moves self.start_timer = time.time() self.audio_cue = 0 self.num_dead = 0 - self.show_team_colors = Value('i', 0) - self.teams = {} + self.show_team_colors = show_team_colors + self.show_team_colors.value = 0 #Value('i', 0) + self.teams = teams self.update_time = 0 + self.restart = restart #self.num_teams = math.ceil(len(moves)/2) self.num_teams = len(moves) + num_teams.value = self.num_teams self.generate_random_teams(self.num_teams) @@ -201,14 +202,12 @@ def __init__(self, moves, command_queue, ns, music): if self.play_audio: 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 @@ -249,34 +248,18 @@ def insert_move(arr): def generate_random_teams(self, num_teams): team_pick = list(range(num_teams)) 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) + self.teams[serial].value = random.choice(team_pick)# = random_choice + team_pick.remove(self.teams[serial].value) if not team_pick: team_pick = list(range(num_teams)) 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.num_teams, - 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 + self.invince_moves[move_serial].value = 0 + self.dead_moves[move_serial].value = 1 + for i in range(3): + self.force_move_colors[move_serial][i] = 1 def change_all_move_colors(self, r, g, b): for color in self.force_move_colors.values(): @@ -414,10 +397,7 @@ def check_end_game(self): def stop_tracking_moves(self): - for proc in self.tracked_moves.values(): - proc.terminate() - proc.join() - time.sleep(0.02) + self.restart.value = 1 def end_game(self): self.audio.stop_audio() @@ -440,6 +420,7 @@ def end_game(self): def game_loop(self): self.track_moves() + self.restart.value = 0 self.show_team_colors.value = 0 self.count_down() self.change_time = time.time() + 6 diff --git a/games/zombie.py b/games/zombie.py index 9fe1a21a..df7c376b 100644 --- a/games/zombie.py +++ b/games/zombie.py @@ -68,13 +68,7 @@ -def track_controller(serial, num_try, opts): - move = psmove.PSMove(num_try) - if move.get_serial() != serial: - for move_num in range(psmove.count_connected()): - move = psmove.PSMove(move_num) - if move.get_serial() == serial: - break +def track_controller(move, opts, restart, menu): time.sleep(0.01) move.set_leds(200,200,200) move.update_leds() @@ -87,6 +81,8 @@ def track_controller(serial, num_try, opts): change_arr=[0,0,0] while True: + if(restart.value == 1 or menu.value == 1): + break if move.poll(): ax, ay, az = move.get_accelerometer_frame(psmove.Frame_SecondHalf) #total = sum([ax, ay, az]) @@ -201,7 +197,7 @@ def track_controller(serial, num_try, opts): #since only normal music will be playing #need to make this a class with zombie killing defs class Zombie: - def __init__(self, cont_alive, command_queue, ns, music): + def __init__(self, cont_alive, command_queue, ns, music,restart, zombie_opts): self.command_queue = command_queue self.ns = ns @@ -210,6 +206,7 @@ def __init__(self, cont_alive, command_queue, ns, music): self.play_audio = self.ns.settings['play_audio'] self.music = music + self.restart = restart global human_warning global human_max @@ -225,7 +222,7 @@ def __init__(self, cont_alive, command_queue, ns, music): self.humans = [] self.alive_zombies = [] self.dead_zombies = {} - self.controller_opts = {} + self.controller_opts = zombie_opts self.controllers_alive = cont_alive self.win_time = ((len(self.controllers_alive) * 3)/16) * 60 if self.win_time <= 0: @@ -293,22 +290,25 @@ def audio_cue(self): def Start(self): running = True moves = [] + print("gonna get moves") for move_num in range(len(self.controllers_alive)): moves.append(common.get_move(self.controllers_alive[move_num], move_num)) - + print("GOT THE MOVES") serials = self.controllers_alive processes = [] for num_try, serial in enumerate(serials): starting_bullets = 0 - #starting_bullets = random.choice([0, 1]) - opts = Array('i', [0, 0, 0, 1, starting_bullets, 1, 1]) - p = Process(target=track_controller, args=(serial, num_try, opts)) - p.start() - processes.append(p) - self.controller_opts[serial] = opts + self.controller_opts[serial][0] = 0 + self.controller_opts[serial][1] = 0 + self.controller_opts[serial][2] = 0 + self.controller_opts[serial][3] = 1 + self.controller_opts[serial][4] = starting_bullets + self.controller_opts[serial][5] = 1 + self.controller_opts[serial][6] = 1 self.humans.append(serial) - + self.restart.value = 0 + print("started the controllers") if self.play_audio: human_victory = Audio('audio/Zombie/sound_effects/human_victory.wav') zombie_victory = Audio('audio/Zombie/sound_effects/zombie_victory.wav') @@ -318,8 +318,6 @@ def Start(self): molotov = Audio('audio/Zombie/sound_effects/molotov.wav') try: self.music.start_audio_loop() -## music = Audio('audio/Zombie/music/' + random.choice(os.listdir('audio/Zombie/music/'))) -## music.start_effect_music() except: print('no music in audio/Zombie/music/') @@ -386,9 +384,7 @@ def Start(self): #win scenario if len(self.humans) <= 0 or (time.time() - self.start_time) > self.win_time or self.kill_game: - for proc in processes: - proc.terminate() - proc.join() + self.restart.value = 1 pause_time = time.time() + 3 HSV = [(x*1.0/(50*len(self.controllers_alive)), 0.9, 1) for x in range(50*len(self.controllers_alive))] colour_range = [[int(x) for x in colors.hsv2rgb(*colour)] for colour in HSV] diff --git a/joust.py b/joust.py index bc8cef65..ed6a0bbb 100644 --- a/joust.py +++ b/joust.py @@ -49,16 +49,11 @@ KILL_GAME_PAUSE = 4 -def track_move(move_serial, move_num, game_mode, team, team_color_enum, dead_move, force_color, music_speed, werewolf_reveal, show_team_colors, red_on_kill): - #proc = psutil.Process(os.getpid()) - #proc.nice(3) - #explosion = Audio('audio/Joust/sounds/Explosion34.wav') - #explosion.start_effect() +def track_move(move, game_mode, team, team_color_enum, dead_move, force_color, music_speed, werewolf_reveal, show_team_colors, red_on_kill, restart, menu): start = False no_rumble = time.time() + 1 move_last_value = None - move = common.get_move(move_serial, move_num) - my_team_colors = team_color_enum.value + my_team_colors = team_color_enum werewolf = False vibrate = False change_arr = [0,0,0] @@ -70,6 +65,8 @@ def track_move(move_serial, move_num, game_mode, team, team_color_enum, dead_mov werewolf = True #keep on looping while move is not dead while True: + if(restart.value == 1 or menu.value == 1): + return if show_team_colors.value == 1: move.set_leds(*my_team_colors) move.update_leds() @@ -90,7 +87,8 @@ def track_move(move_serial, move_num, game_mode, team, team_color_enum, dead_mov move.set_rumble(0) move.update_leds() no_rumble = time.time() + 0.5 - elif dead_move.value == 1 and werewolf_reveal.value > 0: + elif dead_move.value == 1 and werewolf_reveal.value > 0: + if move.poll(): ax, ay, az = move.get_accelerometer_frame(psmove.Frame_SecondHalf) total = sqrt(sum([ax**2, ay**2, az**2])) @@ -117,12 +115,12 @@ def track_move(move_serial, move_num, game_mode, team, team_color_enum, dead_mov flash_lights_timer = 0 flash_lights = not flash_lights if flash_lights: - if game_mode == common.Games.WereJoust: + if game_mode == common.Games.WereJoust.value: move.set_leds(*colors.Colors.Black.value) else: move.set_leds(*colors.Colors.White40.value) else: - if game_mode == common.Games.WereJoust: + if game_mode == common.Games.WereJoust.value: if werewolf_reveal.value == 2 and werewolf: move.set_leds(*colors.Colors.Blue40.value) else: @@ -137,7 +135,7 @@ def track_move(move_serial, move_num, game_mode, team, team_color_enum, dead_mov vibrate = False else: - if game_mode == common.Games.WereJoust: + if game_mode == common.Games.WereJoust.value: if werewolf_reveal.value == 2 and werewolf: move.set_leds(*colors.Colors.Blue40.value) else: @@ -166,9 +164,8 @@ def track_move(move_serial, move_num, game_mode, team, team_color_enum, dead_mov move.update_leds() elif dead_move.value < 1: - time.sleep(0.5) - if dead_move.value == -1 and game_mode == common.Games.NonStop: + if dead_move.value == -1 and game_mode == common.Games.NonStop.value: time.sleep(2) move_last_value = 0 change_arr = [0,0,0] @@ -178,8 +175,7 @@ def track_move(move_serial, move_num, game_mode, team, team_color_enum, dead_mov class Joust(): - def __init__(self, moves, command_queue, ns, music, teams, game_mode): - + def __init__(self, moves, command_queue, ns, music, teams, game_mode,controller_teams, controller_colors, dead_moves, force_move_colors,music_speed,werewolf_reveal, show_team_colors, red_on_kill, restart): self.command_queue = command_queue self.ns = ns @@ -196,11 +192,16 @@ def __init__(self, moves, command_queue, ns, music, teams, game_mode): self.red_on_kill = self.ns.settings['red_on_kill'] self.move_serials = moves - self.tracked_moves = {} - self.dead_moves = {} - self.music_speed = Value('d', SLOW_MUSIC_SPEED) + self.restart = restart + self.dead_moves = dead_moves + self.music_speed = music_speed + self.music_speed.value = SLOW_MUSIC_SPEED + + self.controller_teams = controller_teams + self.controller_colors = controller_colors + self.running = True - self.force_move_colors = {} + self.force_move_colors = force_move_colors self.teams = teams self.num_teams = len(colors.team_color_list) @@ -208,7 +209,8 @@ def __init__(self, moves, command_queue, ns, music, teams, game_mode): self.start_timer = time.time() self.audio_cue = 0 self.num_dead = 0 - self.show_team_colors = Value('i', 0) + self.show_team_colors = show_team_colors + self.show_team_colors.value = 0 self.non_stop_deaths = {} for move in self.move_serials: @@ -240,7 +242,8 @@ def __init__(self, moves, command_queue, ns, music, teams, game_mode): WERE_FAST_MAX = common.WERE_FAST_MAX[self.sensitivity] WERE_FAST_WARNING = common.WERE_FAST_WARNING[self.sensitivity] - self.werewolf_reveal = Value('i', 2) + self.werewolf_reveal = werewolf_reveal + self.werewolf_reveal.value = 2 if game_mode == common.Games.JoustFFA or game_mode == common.Games.NonStop: self.num_teams = len(moves) if game_mode == common.Games.JoustRandomTeams: @@ -262,7 +265,6 @@ def __init__(self, moves, command_queue, ns, music, teams, game_mode): self.werewolf_reveal.value = 0 self.num_teams = 1 - print('HELLO THE NUMBER OF TEAMS IS %d' % self.num_teams) if self.game_mode == common.Games.JoustTeams: self.team_colors = colors.team_color_list @@ -278,7 +280,6 @@ def __init__(self, moves, command_queue, ns, music, teams, game_mode): were_num = 1 self.choose_werewolf(were_num) if self.play_audio: - #music = random.choice(glob.glob("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') @@ -328,8 +329,6 @@ def generate_random_teams(self, num_teams): traitor_pick = False else: self.teams[serial] = random_choice - ## print("doing random choice") - ## print(random_choice) team_pick.remove(random_choice) if not team_pick: traitor_pick = False @@ -339,23 +338,14 @@ def track_moves(self): for move_num, move_serial in enumerate(self.move_serials): self.alive_moves.append(move_serial) time.sleep(0.1) - dead_move = Value('i', 1) - force_color = Array('i', [1] * 3) - proc = Process(target=track_move, args=(move_serial, - move_num, - self.game_mode, - self.teams[move_serial], - self.team_colors[self.teams[move_serial]], - dead_move, - force_color, - self.music_speed, - self.werewolf_reveal, - self.show_team_colors, - self.red_on_kill)) - proc.start() - self.tracked_moves[move_serial] = proc - self.dead_moves[move_serial] = dead_move - self.force_move_colors[move_serial] = force_color + self.controller_teams[move_serial].value = self.teams[move_serial] + self.controller_colors[move_serial][0] = self.team_colors[self.teams[move_serial]].value[0] + self.controller_colors[move_serial][1] = self.team_colors[self.teams[move_serial]].value[1] + self.controller_colors[move_serial][2] = self.team_colors[self.teams[move_serial]].value[2] + self.dead_moves[move_serial].value = 1 + self.force_move_colors[move_serial][0] =1 + self.force_move_colors[move_serial][1] =1 + self.force_move_colors[move_serial][2] =1 def change_all_move_colors(self, r, g, b): for color in self.force_move_colors.values(): @@ -428,7 +418,6 @@ def reveal(self): def werewolf_audio_cue(self): if self.game_mode == common.Games.WereJoust: - #print self.werewolf_timer - (time.time() - self.start_timer) if self.werewolf_timer - (time.time() - self.start_timer) <= 30 and self.audio_cue == 0: Audio('audio/Joust/sounds/30 werewolf.wav').start_effect() self.audio_cue = 1 @@ -498,10 +487,7 @@ def check_end_game(self): self.game_end = True def stop_tracking_moves(self): - for proc in self.tracked_moves.values(): - proc.terminate() - proc.join() - time.sleep(0.02) + self.restart.value = 1 def end_game(self): if self.play_audio: @@ -572,6 +558,7 @@ def werewolf_intro(self): def game_loop(self): self.track_moves() + self.restart.value = 0 if self.game_mode == common.Games.WereJoust: self.werewolf_intro() self.werewolf_reveal.value = 1 diff --git a/piaudio.py b/piaudio.py index 657fb2cf..5360f1f8 100644 --- a/piaudio.py +++ b/piaudio.py @@ -14,13 +14,13 @@ from pydub import AudioSegment from multiprocessing import Process, Value, Array, Queue, Manager + import common -def audio_loop(wav_data, ratio, stop_proc): +def audio_loop(fname, ratio, stop_proc): # TODO: As a future improvment, we could precompute resampled versions of the track # at the "steady" playback rates, and only do dynamic resampling when transitioning # between them. - PERIOD=1024 * 4 # Two channels, two bytes per sample. PERIOD_BYTES = PERIOD * 2 * 2 @@ -29,63 +29,84 @@ def audio_loop(wav_data, ratio, stop_proc): proc = psutil.Process(os.getpid()) proc.nice(-5) time.sleep(0.02) + wav_data = None - device = alsaaudio.PCM() - wf = wave.open(io.BytesIO(wav_data), 'rb') - device.setchannels(wf.getnchannels()) - - device.setformat(alsaaudio.PCM_FORMAT_S16_LE) - device.setperiodsize(PERIOD) + - if len(wf.readframes(1)) == 0: - raise ValueError("Empty WAV file played.") - wf.rewind() - device.setrate(wf.getframerate()) - - # Loops samples of up to read_size bytes from the wav file. - def ReadSamples(wf, read_size): - while True: - sample = wf.readframes(read_size) - if len(sample) > 0: - yield sample - else: + song_loaded = False + while(True): + if(stop_proc.value == 1): + pass + elif(fname['song'] != ''): + if(song_loaded == False): + try: + segment = AudioSegment.from_file(fname['song']) + except: + segment = AudioSegment.from_wav("audio/Joust/music/classical.wav") + + wav_data = io.BytesIO() + segment.export(wav_data, 'wav') + wav_data = wav_data.getbuffer() + song_loaded = True + continue + elif(stop_proc.value == 0): + device = alsaaudio.PCM() + wf = wave.open(io.BytesIO(wav_data), 'rb') + device.setchannels(wf.getnchannels()) + + device.setformat(alsaaudio.PCM_FORMAT_S16_LE) + device.setperiodsize(PERIOD) + + if len(wf.readframes(1)) == 0: + raise ValueError("Empty WAV file played.") wf.rewind() - - # Writes incoming samples in chunks of write_size to device. - # Quits when stop_proc is set to a non-zero value. - def WriteSamples(device, write_size, samples): - buf = bytearray() - for sample in samples: - buf.extend(sample) - while len(buf) >= write_size: - device.write(buf[:write_size]) - del buf[:write_size] - if stop_proc.value: - return - - # Resamples audio data at the rate given by 'ratio' above. - def Resample(samples): - for data in samples: - array = numpy.fromstring(data, dtype=numpy.int16) - # Split data into seperate channels and resample. Divide by two - # since there are two channels. We round to the nearest multiple of - # 32 as the resampling is more efficient the closer the sizes are to - # being powers of two. - num_output_frames = int(array.size / (ratio.value * 2)) & (~0x1f) - reshapel = signal.resample(array[0::2], num_output_frames) - reshaper = signal.resample(array[1::2], num_output_frames) - - final = numpy.ones((num_output_frames,2)) - final[:, 0] = reshapel - final[:, 1] = reshaper - - out_data = final.flatten().astype(numpy.int16).tostring() - yield out_data - - WriteSamples(device, PERIOD_BYTES, Resample(ReadSamples(wf, PERIOD))) - - wf.close() - device.close() + device.setrate(wf.getframerate()) + + # Loops samples of up to read_size bytes from the wav file. + def ReadSamples(wf, read_size): + while True: + sample = wf.readframes(read_size) + if len(sample) > 0: + yield sample + else: + wf.rewind() + + # Writes incoming samples in chunks of write_size to device. + # Quits when stop_proc is set to a non-zero value. + def WriteSamples(device, write_size, samples): + buf = bytearray() + for sample in samples: + buf.extend(sample) + while len(buf) >= write_size: + device.write(buf[:write_size]) + del buf[:write_size] + if stop_proc.value: + return + + # Resamples audio data at the rate given by 'ratio' above. + def Resample(samples): + for data in samples: + array = numpy.fromstring(data, dtype=numpy.int16) + # Split data into seperate channels and resample. Divide by two + # since there are two channels. We round to the nearest multiple of + # 32 as the resampling is more efficient the closer the sizes are to + # being powers of two. + num_output_frames = int(array.size / (ratio.value * 2)) & (~0x1f) + reshapel = signal.resample(array[0::2], num_output_frames) + reshaper = signal.resample(array[1::2], num_output_frames) + + final = numpy.ones((num_output_frames,2)) + final[:, 0] = reshapel + final[:, 1] = reshaper + + out_data = final.flatten().astype(numpy.int16).tostring() + yield out_data + + WriteSamples(device, PERIOD_BYTES, Resample(ReadSamples(wf, PERIOD))) + print("closing everything") + wf.close() + device.close() + song_loaded = False @functools.lru_cache(maxsize=128) class Audio: @@ -119,43 +140,30 @@ def start_effect_and_wait(self): @functools.lru_cache(maxsize=16) class Music: - def __init__(self, fname): - self.load_thread_ = threading.Thread(target=lambda: self.load_sample_(fname)) - self.load_thread_.start() + def __init__(self, name): + self.name = name self.transition_future_ = asyncio.Future() + + self.stop_proc = Value('i', 1) + self.ratio = Value('d' , 1.0) + manager = Manager() + self.fname = manager.dict() + self.fname['song'] = '' - def wait_for_sample_(self): - if self.load_thread_: - self.load_thread_.join() - self.load_thread_ = None - - def load_sample_(self, fname): - try: - segment = AudioSegment.from_file(fname) - except: - print("error can not convert "+fname+" to wav") - print("trying to play classical.wav instead") - segment = AudioSegment.from_wav("audio/Joust/music/classical.wav") + self.t = Process(target=audio_loop, args=(self.fname, self.ratio, self.stop_proc)) + self.t.start() - self.fname_ = fname - wav_data = io.BytesIO() - segment.export(wav_data, 'wav') - self.wav_data_ = wav_data.getbuffer() + def load_audio(self, fname): + self.fname['song'] = fname + def start_audio_loop(self): - self.wait_for_sample_() - print ('audio file is ' + str(self.fname_)) - # Start audio in seperate process to be non-blocking - self.stop_proc = Value('i', 0) - self.ratio = Value('d' , 1.0) - - self.t = Process(target=audio_loop, args=(self.wav_data_, self.ratio, self.stop_proc)) - self.t.start() + self.stop_proc.value = 0 def stop_audio(self): self.stop_proc.value = 1 + self.fname['song'] = '' time.sleep(0.1) - self.t.join() self.transition_future_.cancel() def change_ratio(self, ratio): diff --git a/piparty.py b/piparty.py index 5f5e36db..99e7492b 100644 --- a/piparty.py +++ b/piparty.py @@ -7,7 +7,7 @@ from multiprocessing import Process, Value, Array, Queue, Manager from games import ffa, zombie, commander, swapper, tournament, speed_bomb, fight_club import jm_dbus - +import controller_process TEAM_NUM = len(colors.team_color_list) #TEAM_COLORS = colors.generate_colors(TEAM_NUM) @@ -45,14 +45,15 @@ class Sensitivity(Enum): mid = 1 fast = 2 -def track_move(serial, move_num, move_opts, force_color, battery, dead_count): - move = common.get_move(serial, move_num) +def track_move(serial, move_num, move, move_opts, force_color, battery, dead_count, restart, menu): move.set_leds(0,0,0) move.update_leds() random_color = random.random() while True: + if(restart.value ==1 or menu.value == 0): + return time.sleep(0.01) if move.poll(): game_mode = common.Games(move_opts[Opts.game_mode.value]) @@ -261,21 +262,46 @@ def __init__(self): self.game_mode = common.Games.Random self.old_game_mode = common.Games.Random self.pair = pair.Pair() - + + self.menu = Value('i', 1) + self.controller_game_mode = Value('i',1) + self.restart = Value('i',0) + self.controller_teams = {} + self.controller_colors = {} + self.dead_moves = {} + self.music_speed= Value('d', 0) + self.werewolf_reveal = Value('i', 2) + self.show_team_colors = Value('i', 0) + self.red_on_kill = Value('i', 0) + self.zombie_opts = {} + self.commander_intro = Value('i',1) + self.commander_move_opts = {} + self.commander_powers = [Value('d', 0.0), Value('d', 0.0)] + self.commander_overdrive = [Value('i', 0), Value('i', 0)] + self.five_controller_opts = {} + self.swapper_team_colors = Array('i',[0]*6) + self.fight_club_colors = {} + self.invincible_moves = {} + self.num_teams = Value('i',1) + self.bomb_color = Array('i', [0] * 3) + self.game_start = Value('i', 0) + self.false_colors = {} + self.was_faked = {} + self.rumble = {} + self.i = 0 #load audio now so it converts before the game begins + self.menu_music = Music("menu") + self.joust_music = Music("joust") + self.zombie_music = Music("zombie") + self.commander_music = Music("commander") + self.choose_new_music() def choose_new_music(self): - self.joust_music = Music(random.choice(glob.glob("audio/Joust/music/*"))) - try: - self.zombie_music = Music(random.choice(glob.glob("audio/Zombie/music/*"))) - except Exception: - self.zombie_music = DummyMusic() - try: - self.commander_music = Music(random.choice(glob.glob("audio/Commander/music/*"))) - except Exception: - self.commander_music = DummyMusic() + self.joust_music.load_audio(random.choice(glob.glob("audio/Joust/music/*"))) + self.zombie_music.load_audio(random.choice(glob.glob("audio/Zombie/music/*"))) + self.commander_music.load_audio(random.choice(glob.glob("audio/Commander/music/*"))) def exclude_out_moves(self): for move in self.moves: @@ -323,21 +349,62 @@ def pair_move(self, move, move_num): if move_serial not in self.tracked_moves: color = Array('i', [0] * 3) opts = Array('i', [0] * 6) + if move_serial in self.teams: opts[Opts.team.value] = self.teams[move_serial] else: #initialize to team Yellow - opts[Opts.team.value] = 3 + opts[Opts.team.value] = 3 if move_serial in self.out_moves: opts[Opts.alive.value] = self.out_moves[move_serial] opts[Opts.game_mode.value] = self.game_mode.value #now start tracking the move controller - proc = Process(target=track_move, args=(move_serial, move_num, opts, color, self.show_battery, self.dead_count)) + + + + team = Value('i',0) + team_color_enum = Array('i',[0]*3) + dead_move = Value( 'i',0) + zombie_opt = Array('i', [0, 0, 0, 1, 0, 1, 1]) + five_controller_opt = Array('i',[0]*5) + + + commander_move_opt = Array('i', [0] * 5) + invincibility = Value('b', True) + fight_club_color = Value('i', 0) + false_color = Value('i', 0) + faked = Value('i', 0) + rumble = Value('i', 0) + + + proc = Process(target= controller_process.main_track_move, args=(self.menu, self.restart, move_serial, move_num, opts, color, self.show_battery, \ + self.dead_count, self.controller_game_mode, team, team_color_enum, dead_move, \ + self.music_speed, self.werewolf_reveal, self.show_team_colors, self.red_on_kill,zombie_opt,\ + self.commander_intro, commander_move_opt, self.commander_powers, self.commander_overdrive,\ + five_controller_opt, self.swapper_team_colors, invincibility, fight_club_color, self.num_teams,\ + self.bomb_color,self.game_start,false_color, faked, rumble)) + + proc.start() self.move_opts[move_serial] = opts self.tracked_moves[move_serial] = proc self.force_color[move_serial] = color + self.controller_teams[move_serial] = team + self.controller_colors[move_serial] = team_color_enum + self.dead_moves[move_serial] = dead_move + self.zombie_opts[move_serial] = zombie_opt + self.commander_move_opts[move_serial] = commander_move_opt + self.five_controller_opts[move_serial] = five_controller_opt + self.fight_club_colors[move_serial] = fight_club_color + self.invincible_moves[move_serial] = invincibility + self.false_colors[move_serial] = false_color + self.was_faked[move_serial] = faked + self.rumble[move_serial] = rumble + + + + self.exclude_out_moves() @@ -405,6 +472,9 @@ def reset_controller_game_state(self): for move_opt in self.move_opts.values(): #on means off here move_opt[Opts.random_start.value] = Alive.on.value + for serial in self.move_opts.keys(): + for i in range(3): + self.force_color[serial][i] = 0 self.random_added = [] def game_loop(self): @@ -412,11 +482,8 @@ def game_loop(self): while True: if self.play_menu_music: self.play_menu_music = False - try: - self.menu_music = Music(random.choice(glob.glob("audio/MenuMusic/*"))) - self.menu_music.start_audio_loop() - except Exception: - self.menu_music = DummyMusic() + self.menu_music.load_audio(random.choice(glob.glob("audio/MenuMusic/*"))) + self.menu_music.start_audio_loop() self.i=self.i+1 if not self.pair_one_move and "0" in os.popen('lsusb | grep "PlayStation Move motion controller" | wc -l').read(): self.pair_one_move = True @@ -613,11 +680,6 @@ def update_status(self,game_status): self.ns.out_moves = self.out_moves - def stop_tracking_moves(self): - for proc in self.tracked_moves.values(): - proc.terminate() - proc.join() - def check_start_game(self): #if self.game_mode == common.Games.Random: self.exclude_out_moves() @@ -633,6 +695,7 @@ def check_start_game(self): if start_game: + print("starting game") if self.game_mode == common.Games.Random: self.start_game(random_mode=True) else: @@ -671,10 +734,10 @@ def play_random_instructions(self): os.popen('espeak -ven -p 70 -a 200 "Two players fight, the winner must defend thier title, the player with the highest score wins') time.sleep(5) + def start_game(self, random_mode=False): self.enable_bt_scanning(False) self.exclude_out_moves() - self.stop_tracking_moves() time.sleep(1) self.teams = {serial: self.move_opts[serial][Opts.team.value] for serial in self.tracked_moves.keys() if self.out_moves[serial] == Alive.on.value} game_moves = [move.get_serial() for move in self.moves if self.out_moves[move.get_serial()] == Alive.on.value] @@ -685,8 +748,10 @@ def start_game(self, random_mode=False): if len(game_moves) < self.game_mode.minimum_players and self.ns.settings['enforce_minimum']: Audio('audio/Menu/notenoughplayers.wav').start_effect() - self.tracked_moves = {} + self.reset_controller_game_state() return + self.menu.value = 0 + self.restart.value =1 self.update_status('starting') if random_mode: @@ -711,32 +776,29 @@ def start_game(self, random_mode=False): self.rand_game_list.append(selected_game) self.game_mode = selected_game + self.controller_game_mode.value = self.game_mode.value if self.ns.settings['play_instructions'] and self.ns.settings['play_audio']: self.play_random_instructions() if self.game_mode == common.Games.Zombies: - zombie.Zombie(game_moves, self.command_queue, self.ns, self.zombie_music) - self.tracked_moves = {} + zombie.Zombie(game_moves, self.command_queue, self.ns, self.zombie_music, self.restart, self.zombie_opts) elif self.game_mode == common.Games.Commander: - commander.Commander(game_moves, self.command_queue, self.ns, self.commander_music) - self.tracked_moves = {} + commander.Commander(game_moves, self.command_queue, self.ns, self.commander_music, self.dead_moves, self.commander_intro, self.commander_move_opts, \ + self.commander_powers, self.commander_overdrive, self.music_speed, self.force_color, self.restart, self.controller_teams) elif self.game_mode == common.Games.Ninja: - speed_bomb.Bomb(game_moves, self.command_queue, self.ns, self.commander_music) - self.tracked_moves = {} + speed_bomb.Bomb(game_moves, self.command_queue, self.ns, self.commander_music, self.bomb_color, self.game_start, self.five_controller_opts, self.dead_moves, self.force_color, self.false_colors, self.was_faked, self.rumble, self.music_speed,self.restart) elif self.game_mode == common.Games.Swapper: - swapper.Swapper(game_moves, self.command_queue, self.ns, self.joust_music) - self.tracked_moves = {} + swapper.Swapper(game_moves, self.command_queue, self.ns, self.joust_music, \ + self.swapper_team_colors, self.dead_moves, self.music_speed, self.force_color, self.five_controller_opts, self.controller_teams, self.restart) elif self.game_mode == common.Games.FightClub: if random.randint(0,1)==1: fight_music = self.commander_music else: fight_music = self.joust_music - fight_club.Fight_club(game_moves, self.command_queue, self.ns, fight_music) - self.tracked_moves = {} + fight_club.Fight_club(game_moves, self.command_queue, self.ns, fight_music, self.show_team_colors, self.music_speed, self.dead_moves, self.force_color, self.invincible_moves, self.fight_club_colors, self.restart) elif self.game_mode == common.Games.Tournament: - tournament.Tournament(game_moves, self.command_queue, self.ns, self.joust_music) - self.tracked_moves = {} + tournament.Tournament(game_moves, self.command_queue, self.ns, self.joust_music, self.show_team_colors, self.music_speed, self.controller_teams, self.dead_moves, self.force_color, self.invincible_moves, self.num_teams, self.restart) else: if self.game_mode == common.Games.JoustFFA and self.experimental: print("Playing EXPERIMENTAL FFA Mode.") @@ -745,8 +807,7 @@ def start_game(self, random_mode=False): game.run_loop() else: #may need to put in moves that have selected to not be in the game - joust.Joust(game_moves, self.command_queue, self.ns, self.joust_music, self.teams, self.game_mode) - self.tracked_moves = {} + joust.Joust(game_moves, self.command_queue, self.ns, self.joust_music, self.teams, self.game_mode, self.controller_teams, self.controller_colors, self.dead_moves, self.force_color,self.music_speed,self.werewolf_reveal, self.show_team_colors, self.red_on_kill, self.restart) if random_mode: self.game_mode = common.Games.Random if self.ns.settings['play_instructions']: @@ -758,6 +819,10 @@ def start_game(self, random_mode=False): #turn off admin mode so someone can't accidentally press a button self.admin_move = None self.random_added = [] + self.reset_controller_game_state() + self.menu.value = 1 + self.restart.value =0 + self.reset_controller_game_state() if __name__ == "__main__": InitAudio()