changeset 479:1de67f332f00

Fix several synchro issues
author Thibaut Girka <thib@sitedethib.com>
date Wed, 28 Dec 2011 19:42:48 +0100
parents 884ca91b0854
children d666856acb52
files eosd pytouhou/game/enemy.py pytouhou/game/game.py pytouhou/network.py
diffstat 4 files changed, 18 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/eosd
+++ b/eosd
@@ -46,16 +46,18 @@ def main(path, stage_num, rank, characte
         from pytouhou.network import Network
         from pytouhou.utils.random import Random
 
+        players = [PlayerState(character=0), PlayerState(character=2)]
+
         if remote:
             remote_addr, remote_port = remote.split(':')
             addr = remote_addr, int(remote_port)
-            players = [PlayerState(character=0), PlayerState(character=2)]
+            selected_player = 0
         else:
             addr = None
-            players = [PlayerState(character=2), PlayerState(character=0)]
+            selected_player = 1
 
         prng = Random(0)
-        con = Network(port, addr)
+        con = Network(port, addr, selected_player)
     else:
         con = None
 
--- a/pytouhou/game/enemy.py
+++ b/pytouhou/game/enemy.py
@@ -152,7 +152,7 @@ class Enemy(object):
 
 
     def select_player(self, players=None):
-        return (players or self._game.players)[0] #TODO
+        return min(players or self._game.players, key=lambda p: ((p.x - self.x) ** 2 + (p.y - self.y) ** 2, p.state.character)) #TODO
 
 
     def get_player_angle(self, player=None, pos=None):
--- a/pytouhou/game/game.py
+++ b/pytouhou/game/game.py
@@ -95,7 +95,6 @@ class Game(object):
 
 
     def drop_bonus(self, x, y, _type, end_pos=None):
-        player = self.players[0] #TODO
         if _type > 6:
             return
         item_type = self.item_types[_type]
@@ -110,7 +109,7 @@ class Game(object):
 
 
     def change_bullets_into_star_items(self):
-        player = self.players[0] #TODO
+        player = min(self.players, key=lambda x: (x.state.score, x.state.character)) #TODO
         item_type = self.item_types[6]
         self.items.extend(Item((bullet.x, bullet.y), 6, item_type, self, player=player) for bullet in self.bullets)
         self.bullets = []
--- a/pytouhou/network.py
+++ b/pytouhou/network.py
@@ -5,11 +5,13 @@ from select import select
 MSG_STRUCT = struct.Struct('!IHHB')
 
 class Network(object):
-    def __init__(self, port=8080, dest=None):
+    def __init__(self, port=8080, dest=None, selected_player=0):
         self.frame = 0
         self.keystate = 0
         self.old_keystate = 0
 
+        self.selected_player = selected_player
+
         self.remote_addr = dest
         self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
         self.sock.bind(('', port))
@@ -48,6 +50,12 @@ class Network(object):
             self.sock.sendto(MSG_STRUCT.pack(frame, keystate, old_keystate, checksum), self.remote_addr)
 
 
+    def run_game_iter(self, game, keystate, other_keystate):
+        keystates = [other_keystate, other_keystate]
+        keystates[self.selected_player] = keystate
+        game.run_iter(keystates)
+
+
     def run_iter(self, game, keystate):
         if self.frame < game.frame:
             self.old_keystate, self.keystate = self.keystate, keystate
@@ -55,11 +63,9 @@ class Network(object):
 
         for frame, keystate, old_keystate, checksum in self.read_messages():
             if frame == game.frame:
-                game.run_iter([self.keystate, keystate])
+                self.run_game_iter(game, self.keystate, keystate)
             elif frame == game.frame + 1:
-                print('Skipped')
-                game.run_iter([self.old_keystate, old_keystate])
-                game.run_iter([self.keystate, keystate])
+                self.run_game_iter(game, self.old_keystate, keystate)
 
         self.send_message()