|
|
|
@ -235,40 +235,83 @@ class TestTtrsPlayer(TtrsPlayer):
@@ -235,40 +235,83 @@ class TestTtrsPlayer(TtrsPlayer):
|
|
|
|
|
class BalanceTtrsPlayer(TtrsPlayer, threading.Thread): |
|
|
|
|
def __init__(self, playground, addr, player_id): |
|
|
|
|
self.playground = playground |
|
|
|
|
self.addr = addr |
|
|
|
|
self.player_id = player_id |
|
|
|
|
self.evt = None |
|
|
|
|
self.oldevt = None |
|
|
|
|
threading.Thread.__init__(self, daemon=True) |
|
|
|
|
self.start() |
|
|
|
|
|
|
|
|
|
def run(self): |
|
|
|
|
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 |
|
|
|
|
evt = None |
|
|
|
|
oldevt = None |
|
|
|
|
lastchange = 0 |
|
|
|
|
while self.running: |
|
|
|
|
sock.send(b'a') |
|
|
|
|
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: |
|
|
|
|
self.__calc_event(p0x, p0y) |
|
|
|
|
xbal, ybal = p0x, p0y |
|
|
|
|
elif self.player_id == 1: |
|
|
|
|
self.__calc_event(p1x, p1y) |
|
|
|
|
time.sleep(0.01) |
|
|
|
|
xbal, ybal = p1x, p1y |
|
|
|
|
|
|
|
|
|
def __calc_event(self, xbal, ybal): |
|
|
|
|
if self.ybal > 50: |
|
|
|
|
return PlayerEvent.ROTATE |
|
|
|
|
print('player_id=%d xbal=%d ybal=%d' % (self.player_id, xbal, ybal)) |
|
|
|
|
if xbal == -128 or ybal == -128: |
|
|
|
|
self.evt = PlayerEvent.QUIT |
|
|
|
|
self.running = False |
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
THRESHOLD = 40 |
|
|
|
|
if abs(xbal) < THRESHOLD and abs(ybal) < THRESHOLD: |
|
|
|
|
evt = None |
|
|
|
|
else: |
|
|
|
|
if abs(xbal) < abs(ybal): |
|
|
|
|
if ybal > 0: |
|
|
|
|
evt = PlayerEvent.ROTATE |
|
|
|
|
else: |
|
|
|
|
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 time.time() - lastchange < MIN_TIMES.get(evt, 0): |
|
|
|
|
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)) |
|
|
|
|
|
|
|
|
|
MAX_Y_AMPLITUDE = 65 |
|
|
|
|
bal = self.xbal |
|
|
|
|
normbal = (bal + MAX_Y_AMPLITUDE) / (2 * MAX_Y_AMPLITUDE) |
|
|
|
|
px = round(normbal * (self.playground.width - 1)) |
|
|
|
|
print('player %d balance=%d pos=%d' % (self.player_id, bal, px)) |
|
|
|
|
if minopos.x > px: |
|
|
|
|
return PlayerEvent.MOVE_LEFT |
|
|
|
|
elif minopos.x < px: |
|
|
|
|
return PlayerEvent.MOVE_RIGHT |
|
|
|
|
|
|
|
|
|
if self.ybal < -50: |
|
|
|
|
return PlayerEvent.DROP |
|
|
|
|
|
|
|
|
|
def get_event(self): |
|
|
|
|
evt = self.evt |
|
|
|
|
self.evt = None |
|
|
|
|
return evt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TtrsGame(threading.Thread): |
|
|
|
@ -296,7 +339,7 @@ class TtrsGame(threading.Thread):
@@ -296,7 +339,7 @@ class TtrsGame(threading.Thread):
|
|
|
|
|
mino = None |
|
|
|
|
TICK_TIME = 0.1 |
|
|
|
|
FALL_INTERVAL = 5 |
|
|
|
|
MOVE_INTERVAL = 2 |
|
|
|
|
MOVE_INTERVAL = 1 |
|
|
|
|
ticks = 0 |
|
|
|
|
lastfall = 0 |
|
|
|
|
lastmove = 0 |
|
|
|
@ -412,7 +455,7 @@ if __name__ == '__main__':
@@ -412,7 +455,7 @@ if __name__ == '__main__':
|
|
|
|
|
help='number of players') |
|
|
|
|
parser.add_argument('--pygame', dest='pygame', action='store_true', |
|
|
|
|
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') |
|
|
|
|
parser.add_argument('--out', dest='out', type=str, metavar='OUTPUT', |
|
|
|
|
default='e3blp', help='blup output specification') |
|
|
|
|