about summary refs log tree commit diff homepage
path: root/brutalmaze/game.py
blob: ecf23d659389c3ccc4410597e5a9dccdd894f708 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# game.py - main module, starts game and main loop
# Copyright (C) 2017-2020  Nguyễn Gia Phong
#
# This file is part of Brutal Maze.
#
# Brutal Maze is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Brutal Maze is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# 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.9.3'

import re
from argparse import ArgumentParser, FileType, RawTextHelpFormatter
from configparser import ConfigParser
from contextlib import redirect_stdout
from io import StringIO
from math import atan2, radians, pi
from os.path import join as pathjoin, pathsep
from socket import socket, SOL_SOCKET, SO_REUSEADDR
from sys import stdout
from threading import Thread

with redirect_stdout(StringIO()): import pygame
from pygame import KEYDOWN, MOUSEBUTTONUP, QUIT, VIDEORESIZE
from pygame.time import Clock, get_ticks
from palace import free, use_context, Device, Context
from appdirs import AppDirs

from .constants import SETTINGS, ICON, SFX, SFX_NOISE, HERO_SPEED, MIDDLE
from .maze import Maze
from .misc import sign, deg, join, play


class ConfigReader:
    """Object reading and processing INI configuration file for
    Brutal Maze.
    """
    CONTROL_ALIASES = (('New game', 'new'), ('Toggle pause', 'pause'),
                       ('Toggle mute', 'mute'),
                       ('Move left', 'left'), ('Move right', 'right'),
                       ('Move up', 'up'), ('Move down', 'down'),
                       ('Long-range attack', 'shot'),
                       ('Close-range attack', 'slash'))
    WEIRD_MOUSE_ERR = '{}: Mouse is not a suitable control'
    INVALID_CONTROL_ERR = '{}: {} is not recognized as a valid control key'

    def __init__(self, filenames):
        self.config = ConfigParser()
        self.config.read(SETTINGS)  # default configuration
        self.config.read(filenames)

    # Fallback to None when attribute is missing
    def __getattr__(self, name): return None

    def parse(self):
        """Parse configurations."""
        self.size = (self.config.getint('Graphics', 'Screen width'),
                     self.config.getint('Graphics', 'Screen height'))
        self.max_fps = self.config.getint('Graphics', 'Maximum FPS')
        self.muted = self.config.getboolean('Sound', 'Muted')
        self.musicvol = self.config.getfloat('Sound', 'Music volume')
        self.touch = self.config.getboolean('Control', 'Touch')
        self.export_dir = self.config.get('Record', 'Directory')
        self.export_rate = self.config.getint('Record', 'Frequency')
        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
        self.key, self.mouse = {}, {}
        for cmd, alias in self.CONTROL_ALIASES:
            i = self.config.get('Control', cmd)
            if re.match('mouse[1-3]$', i.lower()):
                if alias not in ('shot', 'slash'):
                    raise ValueError(self.WEIRD_MOUSE_ERR.format(cmd))
                self.mouse[alias] = int(i[-1]) - 1
                continue
            if len(i) == 1:
                self.key[alias] = ord(i.lower())
                continue
            try:
                self.key[alias] = getattr(pygame, 'K_{}'.format(i.upper()))
            except AttributeError:
                raise ValueError(self.INVALID_CONTROL_ERR.format(cmd, i))

    def read_args(self, arguments):
        """Read and parse a ArgumentParser.Namespace."""
        for option in ('size', 'max_fps', 'muted', 'musicvol',
                       'touch', 'export_dir', 'export_rate', 'server',
                       'host', 'port', 'timeout', 'headless'):
            value = getattr(arguments, option)
            if value is not None: setattr(self, option, value)


class Game:
    """Object handling main loop and IO."""
    def __init__(self, config: ConfigReader):
        pygame.init()
        self.headless = config.headless and config.server
        if not self.headless: pygame.display.set_icon(ICON)
        self.actx = None if self.headless else Context(Device())
        self._mute = config.muted

        if config.server:
            self.server = socket()
            self.server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
            self.server.bind((config.host, config.port))
            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

        self.max_fps, self.fps = config.max_fps, config.max_fps
        self.musicvol = config.musicvol
        self.touch = config.touch
        self.key, self.mouse = config.key, config.mouse
        self.maze = Maze(config.max_fps, config.size, config.headless,
                         config.export_dir, 1000 / config.export_rate)
        self.hero = self.maze.hero
        self.clock, self.paused = Clock(), False

    def __enter__(self):
        if self.actx is not None:
            use_context(self.actx)
            self.actx.listener.position = MIDDLE, -MIDDLE, 0
            self.actx.listener.gain = not self._mute
            self._source = play(SFX_NOISE)
            self._source.looping = True
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        if self.server is not None: self.server.close()
        if not self.hero.dead: self.maze.dump_records()
        if self.actx is not None:
            free(SFX)
            self._source.stop()
            self.actx.update()
            use_context(None)
            self.actx.destroy()
            self.actx.device.close()
        pygame.quit()

    @property
    def mute(self):
        """Mute state."""
        return getattr(self, '_mute', 1)

    @mute.setter
    def mute(self, value):
        """Mute state."""
        self._mute = int(bool(value))
        self.actx.listener.gain = not self._mute

    def export_txt(self):
        """Export maze data to string."""
        export = self.maze.update_export(forced=True)
        return '{} {} {} {}\n{}{}{}{}'.format(
            len(export['m']), len(export['e']), len(export['b']), export['s'],
            ''.join(row + '\n' for row in export['m']), join(export['h']),
            ''.join(map(join, export['e'])), ''.join(map(join, export['b'])))

    def update(self):
        """Draw and handle meta events on Pygame window.

        Return False if QUIT event is captured, True otherwise.
        """
        events = pygame.event.get()
        for event in events:
            if event.type == QUIT:
                return False
            elif event.type == VIDEORESIZE:
                self.maze.resize((event.w, event.h))
            elif event.type == KEYDOWN:
                if event.key == self.key['mute']:
                    self.mute ^= 1
                elif not self.server:
                    if event.key == self.key['new']:
                        self.maze.reinit()
                    elif event.key == self.key['pause'] and not self.hero.dead:
                        self.paused ^= True
            elif event.type == MOUSEBUTTONUP and self.touch:
                # We're careless about which mouse button is clicked.
                maze = self.maze
                if self.hero.dead:
                    maze.reinit()
                else:
                    x, y = pygame.mouse.get_pos()
                    maze.destx, maze.desty = maze.get_grid(x, y)
                    if maze.set_step(maze.isdisplayed):
                        maze.target = maze.get_target(x, y)
                        self.hero.firing = not maze.target.retired
                    if maze.stepx == maze.stepy == 0:
                        maze.destx = maze.desty = MIDDLE

        # Compare current FPS with the average of the last 10 frames
        new_fps = self.clock.get_fps()
        if new_fps < self.fps:
            self.fps -= 1
        elif self.fps < self.max_fps and not self.paused:
            self.fps += 5
        if not self.paused: self.maze.update(self.fps)
        if not self.headless: self.maze.draw()
        self.clock.tick(self.fps)
        self.actx.update()
        return True

    def move(self, x=0, y=0):
        """Command the hero to move faster in the given direction."""
        maze = self.maze
        velocity = maze.distance * HERO_SPEED / self.fps
        accel = velocity * HERO_SPEED / self.fps

        if x == y == 0:
            maze.set_step()
            x, y = maze.stepx, maze.stepy
        else:
            x, y = -x, -y   # or move the maze in the reverse direction

        if maze.next_move > 0 or not x:
            maze.vx -= sign(maze.vx) * accel
            if abs(maze.vx) < accel * 2: maze.vx = 0.0
        elif x * maze.vx < 0:
            maze.vx += x * 2 * accel
        else:
            maze.vx += x * accel
            if abs(maze.vx) > velocity: maze.vx = x * velocity

        if maze.next_move > 0 or not y:
            maze.vy -= sign(maze.vy) * accel
            if abs(maze.vy) < accel * 2: maze.vy = 0.0
        elif y * maze.vy < 0:
            maze.vy += y * 2 * accel
        else:
            maze.vy += y * accel
            if abs(maze.vy) > velocity: maze.vy = y * velocity

    def control(self, x, y, angle, firing, slashing):
        """Control how the hero move and attack."""
        self.move(x, y)
        self.hero.update_angle(angle)
        self.hero.firing = firing
        self.hero.slashing = slashing

    def remote_control(self):
        """Handle remote control though socket server.

        This function is supposed to be run in a Thread.
        """
        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()
            while True:
                if self.hero.dead:
                    connection.send('0000000'.encode())
                    break
                data = self.export_txt().encode()
                alpha = deg(self.hero.angle)
                connection.send('{:07}'.format(len(data)).encode())
                connection.send(data)
                try:
                    buf = connection.recv(7)
                except:     # client is closed or timed out
                    break
                if not buf: break
                try:
                    move, angle, attack = map(int, buf.decode().split())
                except ValueError:  # invalid input
                    break
                y, x = (i - 1 for i in divmod(move, 3))
                # Time is the essence.
                angle = self.hero.angle if angle == alpha else radians(angle)
                self.sockinp = x, y, angle, attack & 1, attack >> 1
                clock.tick(self.fps)
            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 touch_control(self):
        """Handle touch control."""
        maze, hero = self.maze, self.hero
        if maze.target.retired: hero.firing = False
        if hero.firing:
            x, y = maze.get_pos(maze.target.x, maze.target.y)
        else:
            x, y = pygame.mouse.get_pos()
        hero.update_angle(atan2(y - hero.y, x - hero.x))
        self.move()

    def user_control(self):
        """Handle direct control from user's mouse and keyboard."""
        if self.hero.dead: return
        keys = pygame.key.get_pressed()
        buttons = pygame.mouse.get_pressed()

        right = keys[self.key['right']] - keys[self.key['left']]
        down = keys[self.key['down']] - keys[self.key['up']]
        x, y = pygame.mouse.get_pos()
        angle = atan2(y - self.hero.y, x - self.hero.x)

        try:
            firing = keys[self.key['shot']]
        except KeyError:
            firing = buttons[self.mouse['shot']]
        try:
            slashing = keys[self.key['slash']]
        except KeyError:
            slashing = buttons[self.mouse['slash']]
        self.control(right, down, angle, firing, slashing)


def main():
    """Start game and main loop."""
    # Read configuration file
    dirs = AppDirs(appname='brutalmaze', appauthor=False, multipath=True)
    parents = dirs.site_config_dir.split(pathsep)
    parents.append(dirs.user_config_dir)
    filenames = [pathjoin(parent, 'settings.ini') for parent in parents]
    config = ConfigReader(filenames)
    config.parse()

    # Parse command-line arguments
    parser = ArgumentParser(usage='%(prog)s [options]',
                            formatter_class=RawTextHelpFormatter)
    parser.add_argument('-v', '--version', action='version',
                        version='Brutal Maze {}'.format(__version__))
    parser.add_argument(
        '--write-config', nargs='?', const=stdout, type=FileType('w'),
        metavar='PATH', dest='defaultcfg',
        help='write default config and exit, if PATH not specified use stdout')
    parser.add_argument(
        '-c', '--config', metavar='PATH',
        help='location of the configuration file (fallback: {})'.format(
            pathsep.join(filenames)))
    parser.add_argument(
        '-s', '--size', type=int, nargs=2, metavar=('X', 'Y'),
        help='the desired screen size (fallback: {}x{})'.format(*config.size))
    parser.add_argument(
        '-f', '--max-fps', type=int, metavar='FPS',
        help='the desired maximum FPS (fallback: {})'.format(config.max_fps))
    parser.add_argument(
        '--mute', '-m', action='store_true', default=None, dest='muted',
        help='mute all sounds (fallback: {})'.format(config.muted))
    parser.add_argument('--unmute', action='store_false', dest='muted',
                        help='unmute sound')
    parser.add_argument(
        '--music-volume', type=float, metavar='VOL', dest='musicvol',
        help='between 0.0 and 1.0 (fallback: {})'.format(config.musicvol))
    parser.add_argument(
        '--touch', action='store_true', default=None,
        help='enable touch-friendly control (fallback: {})'.format(
            config.touch))
    parser.add_argument('--no-touch', action='store_false', dest='touch',
                        help='disable touch-friendly control')
    parser.add_argument(
        '--record-dir', metavar='DIR', dest='export_dir',
        help='directory to write game records (fallback: {})'.format(
            config.export_dir or '*disabled*'))
    parser.add_argument(
        '--record-rate', metavar='SPF', dest='export_rate',
        help='snapshots of game state per second (fallback: {})'.format(
            config.export_rate))
    parser.add_argument(
        '--server', action='store_true', default=None,
        help='enable server (fallback: {})'.format(config.server))
    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))
    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(
            not config.headless))
    parser.add_argument('--headless', action='store_true',
                        help='run server without graphics or sound')
    args = parser.parse_args()
    if args.defaultcfg is not None:
        with open(SETTINGS) as settings: args.defaultcfg.write(settings.read())
        args.defaultcfg.close()
        exit()

    # Manipulate config
    if args.config:
        config.config.read(args.config)
        config.parse()
    config.read_args(args)

    # Main loop
    with Game(config) as game:
        if config.server:
            socket_thread = Thread(target=game.remote_control)
            socket_thread.daemon = True     # make it disposable
            socket_thread.start()
            while game.update(): game.control(*game.sockinp)
        elif config.touch:
            while game.update(): game.touch_control()
        else:
            while game.update(): game.user_control()


# Allow launching the game via invoking ``python -m brutalmaze.game''
if __name__ == '__main__': main()