Mercurial > touhou
comparison pytouhou/network.py @ 485:c099802e2435
Improve network latency by increasing game latency to 3 frames.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 15 Jan 2012 15:36:23 +0100 |
parents | 699c8e8c7738 |
children | 711c75115675 |
comparison
equal
deleted
inserted
replaced
484:699c8e8c7738 | 485:c099802e2435 |
---|---|
1 import socket | 1 import socket |
2 import struct | 2 import struct |
3 from select import select | 3 from select import select |
4 import time | 4 import time |
5 | 5 |
6 MSG_STRUCT = struct.Struct('!IHH') | 6 MSG_STRUCT = struct.Struct('!HHH') |
7 | 7 |
8 class Network(object): | 8 class Network(object): |
9 def __init__(self, port=8080, dest=None, selected_player=0): | 9 def __init__(self, port=8080, dest=None, selected_player=0): |
10 self.frame = 0 | 10 self.frame = 0 |
11 self.keystate = 0 | 11 self.keystate = 0 |
12 self.old_keystate = 0 | 12 self.old_keystate = 0 |
13 self.remote_keystate = 0 | |
13 | 14 |
14 self.selected_player = selected_player | 15 self.selected_player = selected_player |
15 | 16 |
16 self.remote_addr = dest | 17 self.remote_addr = dest |
17 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | 18 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) |
58 keystates[self.selected_player] = keystate | 59 keystates[self.selected_player] = keystate |
59 game.run_iter(keystates) | 60 game.run_iter(keystates) |
60 | 61 |
61 | 62 |
62 def run_iter(self, game, keystate): | 63 def run_iter(self, game, keystate): |
63 if self.frame < game.frame: | 64 if game.frame % 3 == 0: |
65 # Phase 1: Update game with old data | |
66 self.run_game_iter(game, self.keystate, self.remote_keystate) | |
67 elif game.frame % 3 == 1: | |
68 # Phase 2: Update data, send new data, update game with old data | |
64 self.old_keystate, self.keystate = self.keystate, keystate | 69 self.old_keystate, self.keystate = self.keystate, keystate |
65 self.frame = game.frame | 70 self.frame = game.frame // 3 |
71 self.send_message() | |
72 self.run_game_iter(game, self.old_keystate, self.remote_keystate) | |
73 elif game.frame % 3 == 2: | |
74 # Phase 3: Send new data, get remote data, update game with new data | |
75 self.send_message() | |
76 # Follow one valid update | |
77 message = self.read_message() | |
78 if message: | |
79 frame, keystate, old_keystate = message | |
80 if frame == self.frame: | |
81 self.remote_keystate = keystate | |
82 elif frame == self.frame + 1: | |
83 self.remote_keystate = old_keystate | |
84 else: | |
85 raise Exception #TODO | |
86 self.run_game_iter(game, self.keystate, self.remote_keystate) | |
87 elif game.frame > 2: | |
88 print('ARGH') | |
66 | 89 |
67 self.send_message() | |
68 | 90 |
69 # Follow one valid update | |
70 message = self.read_message() | |
71 if message: | |
72 frame, keystate, old_keystate = message | |
73 if frame == game.frame: | |
74 self.run_game_iter(game, self.keystate, keystate) | |
75 elif frame == game.frame + 1: | |
76 self.run_game_iter(game, self.keystate, old_keystate) | |
77 |