|
|
@ -235,40 +235,83 @@ class TestTtrsPlayer(TtrsPlayer): |
|
|
|
class BalanceTtrsPlayer(TtrsPlayer, threading.Thread): |
|
|
|
class BalanceTtrsPlayer(TtrsPlayer, threading.Thread): |
|
|
|
def __init__(self, playground, addr, player_id): |
|
|
|
def __init__(self, playground, addr, player_id): |
|
|
|
self.playground = playground |
|
|
|
self.playground = playground |
|
|
|
|
|
|
|
self.addr = addr |
|
|
|
self.player_id = player_id |
|
|
|
self.player_id = player_id |
|
|
|
|
|
|
|
self.evt = None |
|
|
|
|
|
|
|
self.oldevt = None |
|
|
|
threading.Thread.__init__(self, daemon=True) |
|
|
|
threading.Thread.__init__(self, daemon=True) |
|
|
|
self.start() |
|
|
|
self.start() |
|
|
|
|
|
|
|
|
|
|
|
def run(self): |
|
|
|
def run(self): |
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
|
|
sock.connect(addr) |
|
|
|
try: |
|
|
|
|
|
|
|
sock.connect(self.addr) |
|
|
|
|
|
|
|
except ConnectionRefusedError: |
|
|
|
|
|
|
|
print('could not connect to balance server', file=sys.stderr) |
|
|
|
|
|
|
|
self.evt = PlayerEvent.QUIT |
|
|
|
|
|
|
|
return |
|
|
|
self.running = True |
|
|
|
self.running = True |
|
|
|
|
|
|
|
evt = None |
|
|
|
|
|
|
|
oldevt = None |
|
|
|
|
|
|
|
lastchange = 0 |
|
|
|
while self.running: |
|
|
|
while self.running: |
|
|
|
sock.send(b'a') |
|
|
|
sock.send(b'a') |
|
|
|
data = sock.recv(4) |
|
|
|
data = sock.recv(4) |
|
|
|
p0x, p0y, p2x, p2y = struct.unpack('bbbb', data) |
|
|
|
p0x, p0y, p1x, p1y = struct.unpack('bbbb', data) |
|
|
|
if self.player_id == 0: |
|
|
|
if self.player_id == 0: |
|
|
|
self.__calc_event(p0x, p0y) |
|
|
|
xbal, ybal = p0x, p0y |
|
|
|
elif self.player_id == 1: |
|
|
|
elif self.player_id == 1: |
|
|
|
self.__calc_event(p1x, p1y) |
|
|
|
xbal, ybal = p1x, p1y |
|
|
|
time.sleep(0.01) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __calc_event(self, xbal, ybal): |
|
|
|
print('player_id=%d xbal=%d ybal=%d' % (self.player_id, xbal, ybal)) |
|
|
|
if self.ybal > 50: |
|
|
|
if xbal == -128 or ybal == -128: |
|
|
|
return PlayerEvent.ROTATE |
|
|
|
self.evt = PlayerEvent.QUIT |
|
|
|
|
|
|
|
self.running = False |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
MAX_Y_AMPLITUDE = 65 |
|
|
|
THRESHOLD = 40 |
|
|
|
bal = self.xbal |
|
|
|
if abs(xbal) < THRESHOLD and abs(ybal) < THRESHOLD: |
|
|
|
normbal = (bal + MAX_Y_AMPLITUDE) / (2 * MAX_Y_AMPLITUDE) |
|
|
|
evt = None |
|
|
|
px = round(normbal * (self.playground.width - 1)) |
|
|
|
else: |
|
|
|
print('player %d balance=%d pos=%d' % (self.player_id, bal, px)) |
|
|
|
if abs(xbal) < abs(ybal): |
|
|
|
if minopos.x > px: |
|
|
|
if ybal > 0: |
|
|
|
return PlayerEvent.MOVE_LEFT |
|
|
|
evt = PlayerEvent.ROTATE |
|
|
|
elif minopos.x < px: |
|
|
|
else: |
|
|
|
return PlayerEvent.MOVE_RIGHT |
|
|
|
evt = PlayerEvent.DROP |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
if xbal > 0: |
|
|
|
|
|
|
|
evt = PlayerEvent.MOVE_RIGHT |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
evt = PlayerEvent.MOVE_LEFT |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MIN_TIMES = { |
|
|
|
|
|
|
|
PlayerEvent.MOVE_LEFT: 0.1, |
|
|
|
|
|
|
|
PlayerEvent.MOVE_RIGHT: 0.1, |
|
|
|
|
|
|
|
PlayerEvent.ROTATE: 0.1, |
|
|
|
|
|
|
|
PlayerEvent.DROP: 0.3, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if evt != oldevt: |
|
|
|
|
|
|
|
lastchange = time.time() |
|
|
|
|
|
|
|
oldevt = evt |
|
|
|
|
|
|
|
|
|
|
|
if self.ybal < -50: |
|
|
|
if time.time() - lastchange < MIN_TIMES.get(evt, 0): |
|
|
|
return PlayerEvent.DROP |
|
|
|
print('player_id=%d debounce %s' % (self.player_id, evt)) |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if evt == PlayerEvent.ROTATE: |
|
|
|
|
|
|
|
if self.lastevt != PlayerEvent.ROTATE: |
|
|
|
|
|
|
|
self.evt = evt |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
self.evt = evt |
|
|
|
|
|
|
|
self.lastevt = evt |
|
|
|
|
|
|
|
print('player_id=%d event=%s' % (self.player_id, self.evt)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_event(self): |
|
|
|
|
|
|
|
evt = self.evt |
|
|
|
|
|
|
|
self.evt = None |
|
|
|
|
|
|
|
return evt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TtrsGame(threading.Thread): |
|
|
|
class TtrsGame(threading.Thread): |
|
|
@ -296,7 +339,7 @@ class TtrsGame(threading.Thread): |
|
|
|
mino = None |
|
|
|
mino = None |
|
|
|
TICK_TIME = 0.1 |
|
|
|
TICK_TIME = 0.1 |
|
|
|
FALL_INTERVAL = 5 |
|
|
|
FALL_INTERVAL = 5 |
|
|
|
MOVE_INTERVAL = 2 |
|
|
|
MOVE_INTERVAL = 1 |
|
|
|
ticks = 0 |
|
|
|
ticks = 0 |
|
|
|
lastfall = 0 |
|
|
|
lastfall = 0 |
|
|
|
lastmove = 0 |
|
|
|
lastmove = 0 |
|
|
@ -412,7 +455,7 @@ if __name__ == '__main__': |
|
|
|
help='number of players') |
|
|
|
help='number of players') |
|
|
|
parser.add_argument('--pygame', dest='pygame', action='store_true', |
|
|
|
parser.add_argument('--pygame', dest='pygame', action='store_true', |
|
|
|
help='use pygame as input') |
|
|
|
help='use pygame as input') |
|
|
|
parser.add_argument('--balance', dest='balance', type=str, nargs=1, |
|
|
|
parser.add_argument('--balance', dest='balance', type=str, |
|
|
|
metavar='HOST:PORT', help='use balance input') |
|
|
|
metavar='HOST:PORT', help='use balance input') |
|
|
|
parser.add_argument('--out', dest='out', type=str, metavar='OUTPUT', |
|
|
|
parser.add_argument('--out', dest='out', type=str, metavar='OUTPUT', |
|
|
|
default='e3blp', help='blup output specification') |
|
|
|
default='e3blp', help='blup output specification') |
|
|
|