Skip to content
This repository has been archived by the owner on Nov 30, 2020. It is now read-only.

Commit

Permalink
Fix several bugs
Browse files Browse the repository at this point in the history
* Set connection timeout to avoid hanging along with the client
* Now visible Chameleons are exported in server mode
* Disable manual slashing's bullets blocking so that there won't be no delay after this type of attack that make aiming stiff
  • Loading branch information
McSinyx committed Mar 10, 2018
1 parent d7eb907 commit ace9586
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
8 changes: 4 additions & 4 deletions brutalmaze/characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def update(self, fps):
self.next_strike = ATTACK_SPEED
self.spin_queue = randsign() * self.spin_speed
self.angle -= sign(self.spin_queue) * full_spin
if abs(self.spin_queue) > 0.5:
if round(self.spin_queue) != 0:
self.angle += sign(self.spin_queue) * full_spin / self.spin_speed
self.spin_queue -= sign(self.spin_queue)
else:
Expand All @@ -104,12 +104,12 @@ def get_sides(self):

def update_angle(self, angle):
"""Turn to the given angle if the hero is not busy slashing."""
if abs(self.spin_queue) > 0.5: return
if round(self.spin_queue) != 0: return
delta = (angle - self.angle + pi) % (pi * 2) - pi
unit = pi * 2 / self.get_sides() / self.spin_speed
if abs(delta) < unit:
self.angle, self.spin_queue = angle, 0.0
elif self.next_strike <= 0:
else:
self.spin_queue = delta / unit

def get_color(self):
Expand Down Expand Up @@ -285,7 +285,7 @@ def update(self):
self.spin_queue = randsign() * self.spin_speed
if not self.maze.hero.dead:
play(self.sfx_slash, self.get_slash(), self.get_angle())
if abs(self.spin_queue) > 0.5:
if round(self.spin_queue) != 0:
self.angle += sign(self.spin_queue) * pi / 2 / self.spin_speed
self.spin_queue -= sign(self.spin_queue)
else:
Expand Down
27 changes: 20 additions & 7 deletions brutalmaze/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with Brutal Maze. If not, see <https://www.gnu.org/licenses/>.

__version__ = '0.6.1'
__version__ = '0.6.2'

import re
from argparse import ArgumentParser, FileType, RawTextHelpFormatter
Expand Down Expand Up @@ -73,6 +73,7 @@ def parse(self):
self.server = self.config.getboolean('Server', 'Enable')
self.host = self.config.get('Server', 'Host')
self.port = self.config.getint('Server', 'Port')
self.timeout = self.config.getfloat('Server', 'Timeout')
self.headless = self.config.getboolean('Server', 'Headless')

if self.server: return
Expand All @@ -95,7 +96,7 @@ def parse(self):
def read_args(self, arguments):
"""Read and parse a ArgumentParser.Namespace."""
for option in ('size', 'max_fps', 'muted', 'musicvol',
'server', 'host', 'port', 'headless'):
'server', 'host', 'port', 'timeout', 'headless'):
value = getattr(arguments, option)
if value is not None: setattr(self, option, value)

Expand All @@ -122,6 +123,7 @@ def __init__(self, config):
self.server.listen(1)
print('Socket server is listening on {}:{}'.format(config.host,
config.port))
self.timeout = config.timeout
self.sockinp = 0, 0, -pi * 3 / 4, 0, 0 # freeze and point to NW
else:
self.server = self.sockinp = None
Expand Down Expand Up @@ -158,7 +160,8 @@ def export(self):
if not enemy.awake and walls:
walls[enemy.y-maze.rangey[0]][enemy.x-maze.rangex[0]] = WALL
continue
elif enemy.color == 'Chameleon' and maze.next_move <= 0:
# Check Chameleons
elif getattr(enemy, 'visible', 1) <= 0 and maze.next_move <= 0:
continue
lines.append('{0} {2} {3} {1:.0f}'.format(
COLORS[enemy.get_color()], deg(enemy.angle),
Expand Down Expand Up @@ -253,9 +256,11 @@ def remote_control(self):
clock = Clock()
while True:
connection, address = self.server.accept()
connection.settimeout(self.timeout)
time = get_ticks()
print('[{}] Connected to {}:{}'.format(time, *address))
self.maze.reinit()
self.maze.score = 100000
while True:
if self.hero.dead:
connection.send('0000000'.encode())
Expand All @@ -265,19 +270,22 @@ def remote_control(self):
connection.send(data)
try:
buf = connection.recv(7)
except: # client is likely to be closed
except: # client is closed or timed out
break
if not buf: break
move, angle, attack = (int(i) for i in buf.decode().split())
try:
move, angle, attack = map(int, buf.decode().split())
except ValueError: # invalid input
break
y, x = (i - 1 for i in divmod(move, 3))
self.sockinp = x, y, radians(angle), attack & 1, attack >> 1
clock.tick(self.fps)
self.maze.lose()
self.sockinp = 0, 0, -pi * 3 / 4, 0, 0
new_time = get_ticks()
print('[{0}] {3}:{4} scored {1} points in {2}ms'.format(
new_time, self.maze.get_score(), new_time - time, *address))
connection.close()
if not self.hero.dead: self.maze.lose()

def user_control(self):
"""Handle direct control from user's mouse and keyboard."""
Expand Down Expand Up @@ -350,10 +358,15 @@ def main():
parser.add_argument('--no-server', action='store_false', dest='server',
help='disable server')
parser.add_argument(
'--host', help='host to bind server to (fallback: {})'.format(config.host))
'--host', help='host to bind server to (fallback: {})'.format(
config.host))
parser.add_argument(
'--port', type=int,
help='port for server to listen on (fallback: {})'.format(config.port))
parser.add_argument(
'-t', '--timeout', type=float,
help='socket operations timeout in seconds (fallback: {})'.format(
config.timeout))
parser.add_argument(
'--head', action='store_false', default=None, dest='headless',
help='run server with graphics and sound (fallback: {})'.format(
Expand Down
10 changes: 7 additions & 3 deletions brutalmaze/maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,16 @@ def slash(self):

def track_bullets(self):
"""Handle the bullets."""
fallen = []
if (self.hero.firing and not self.hero.slashing
and self.hero.next_strike <= 0):
self.hero.next_strike = ATTACK_SPEED
self.bullets.append(Bullet(self.surface, self.x, self.y,
self.hero.angle, 'Aluminium'))

fallen = []
block = (self.hero.spin_queue and self.hero.next_heal <= 0
and self.hero.next_strike > self.hero.spin_queue / self.fps)

for i, bullet in enumerate(self.bullets):
wound = bullet.fall_time / BULLET_LIFETIME
bullet.update(self.fps, self.distance)
Expand All @@ -279,8 +283,8 @@ def track_bullets(self):
fallen.append(i)
break
elif bullet.get_distance(self.x, self.y) < self.distance:
if self.hero.spin_queue and self.hero.next_heal <= 0:
self.hero.next_strike = (abs(self.hero.spin_queue*self.fps)
if block:
self.hero.next_strike = (abs(self.hero.spin_queue/self.fps)
+ ATTACK_SPEED)
play(bullet.sfx_missed, wound, bullet.angle + pi)
else:
Expand Down
2 changes: 2 additions & 0 deletions brutalmaze/settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ Close-range attack: Mouse3
Enable: no
Host: localhost
Port: 8089
# Timeout on blocking socket operations, in seconds.
Timeout: 1.0
# Disable graphics and sound (only if socket server is enabled).
Headless: no
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='brutalmaze',
version='0.6.1',
version='0.6.2',
description='A minimalist hack and slash game with fast-paced action',
long_description=long_description,
url='https://github.com/McSinyx/brutalmaze',
Expand Down
2 changes: 1 addition & 1 deletion wiki
Submodule wiki updated from 172d7d to 09c5be

0 comments on commit ace9586

Please sign in to comment.