view scripts/pytouhou @ 747:8a022334a4c4

menu: Load the title image at frame 60.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 18 Jan 2020 19:47:44 +0100
parents 4fa0a8e7d941
children a6875f90c141
line wrap: on
line source

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
##
## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
##
## This program 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 General Public License for more details.
##

from os.path import pathsep
default_data = (pathsep.join(('CM.DAT', 'th06*_CM.DAT', '*CM.DAT', '*cm.dat')),
                pathsep.join(('ST.DAT', 'th6*ST.DAT', '*ST.DAT', '*st.dat')),
                pathsep.join(('IN.DAT', 'th6*IN.DAT', '*IN.DAT', '*in.dat')),
                pathsep.join(('MD.DAT', 'th6*MD.DAT', '*MD.DAT', '*md.dat')),
                pathsep.join(('102h.exe', '102*.exe', '東方紅魔郷.exe', '*.exe')))

defaults = {'data': default_data,
            'path': '.',
            'rank': 0,
            'character': 0,
            'game': 'eosd',
            'interface': 'eosd',
            'port': 0,
            'frontend': 'glfw',
            'backend': ['opengl', 'sdl'],
            'gl-flavor': 'compatibility',
            'gl-version': 2.1,
            'double-buffer': None,
            'fps-limit': -1,
            'frameskip': 1}

from pytouhou.options import parse_config, parse_arguments
options = parse_config('pytouhou', defaults)
args = parse_arguments(options)

verbosity = args.verbosity or options.get('verbosity') or 'WARNING'

import logging
logging.basicConfig(level=getattr(logging, verbosity),
                    format='[%(name)s] [%(levelname)s]: %(message)s')

logger = logging
logger.root.name = 'pytouhou'

logger.info('Configuration loaded from: %s', ', '.join(options.paths))

if not args.no_menu:
    try:
        from pytouhou.menu import menu
    except ImportError:
        logger.error('GTK+ unavailable, disabling the GUI menu.')
    else:
        menu(options, args)

import sys
from importlib import import_module

def load_module(type_, name, items=None):
    try:
        module = import_module('pytouhou.games.%s.%s' % (name, type_))
    except ImportError:
        logger.exception('Module “%s” doesn’t contain %s data, aborting:', name, type_)
        sys.exit(1)
    if items is None:
        return module
    return (getattr(module, item) for item in items)

Game, Common = load_module('game', args.game, ['Game', 'Common'])
Interface = load_module('interface', args.interface).Interface

from pytouhou.lib.sdl import SDL, show_simple_message_box
from pytouhou.ui.window import Window
from pytouhou.resource.loader import Loader
from pytouhou.ui.gamerunner import GameRunner
from pytouhou.game import NextStage, GameOver
from pytouhou.formats.t6rp import T6RP, Level
from pytouhou.utils.random import Random
from pytouhou.formats.hint import Hint
from pytouhou.network import Network


for backend_name in args.backend:
    if backend_name == 'opengl':
        options = {
            'flavor': args.gl_flavor,
            'version': args.gl_version,
            'double-buffer': args.double_buffer,
            'frontend': args.frontend,
        }
    else:
        options = {}

    try:
        backend = import_module('pytouhou.ui.%s.backend' % backend_name)
    except ImportError:
        logger.exception('Failed to import backend %s:', backend_name)
        continue

    try:
        backend.init(options)
    except Exception:
        logger.exception('Backend %s failed to initialize:', backend_name)
        continue

    GameRenderer = backend.GameRenderer
    break
else:
    show_simple_message_box(u'No graphical backend could be used, continuing with a windowless game.')
    backend = None
    GameRenderer = None


class GameBossRush(Game):
    def run_iter(self, keystates):
        for i in range(20):
            skip = not (self.enemies or self.items or self.lasers
                        or self.bullets or self.cancelled_bullets)
            if skip:
                keystates = [k & ~1 for k in keystates]
            Game.run_iter(self, [0 if i else k | 256 for k in keystates])
            if not self.enemies and self.frame % 90 == 0:
                for player in self.players:
                    if player.power < 128:
                        player.power += 1
            if not skip:
                break

    def cleanup(self):
        boss_wait = any(ecl_runner.boss_wait for ecl_runner in self.ecl_runners)
        if not (self.boss or self.msg_wait or boss_wait):
            self.enemies = [enemy for enemy in self.enemies
                            if enemy.boss_callback or enemy.frame > 1]
            for laser in self.lasers:
                if laser.frame <= 1:
                    laser.removed = True
            self.lasers = [laser for laser in self.lasers if laser.frame > 1]
            self.bullets = [bullet for bullet in self.bullets if bullet.frame > 1]
        Game.cleanup(self)


def main(window, path, data, stage_num, rank, character, replay, save_filename,
         skip_replay, boss_rush, debug, enable_background, enable_particles,
         hints, port, remote, friendly_fire):

    resource_loader = Loader(path)

    try:
        resource_loader.scan_archives(data)
    except IOError:
        show_simple_message_box(u'Some data files were not found, did you forget the -p option?')
        sys.exit(1)

    if stage_num is None:
        story = True
        stage_num = 1
        continues = 3
    else:
        story = False
        continues = 0

    if debug:
        continues = -1  # Infinite lives

    if replay:
        with open(replay, 'rb') as file:
            replay = T6RP.read(file)
        rank = replay.rank
        character = replay.character

    save_keystates = None
    if save_filename:
        save_replay = T6RP()
        save_replay.rank = rank
        save_replay.character = character

    difficulty = 16

    if port != 0:
        if remote:
            remote_addr, remote_port = remote.split(':')
            addr = remote_addr, int(remote_port)
            selected_player = 0
        else:
            addr = None
            selected_player = 1

        prng = Random(0)
        con = Network(port, addr, selected_player)
        characters = [1, 3]
    else:
        con = None
        selected_player = 0
        characters = [character]

    if hints:
        with open(hints, 'rb') as file:
            hints = Hint.read(file)

    game_class = GameBossRush if boss_rush else Game

    common = Common(resource_loader, characters, continues)
    interface = Interface(resource_loader, common.players[0]) #XXX
    common.interface = interface #XXX
    renderer = GameRenderer(resource_loader, window) if GameRenderer is not None else None
    runner = GameRunner(window, renderer, common, resource_loader, skip_replay, con)
    window.set_runner(runner)

    while True:
        first_player = common.players[0]

        if replay:
            level = replay.levels[stage_num - 1]
            if not level:
                raise Exception

            prng = Random(level.random_seed)

            #TODO: apply the replay to the other players.
            #TODO: see if the stored score is used or if it’s the one from the previous stage.
            if stage_num != 1 and stage_num - 2 in replay.levels:
                previous_level = replay.levels[stage_num - 1]
                first_player.score = previous_level.score
                first_player.effective_score = previous_level.score
            first_player.points = level.point_items
            first_player.power = level.power
            first_player.lives = level.lives
            first_player.bombs = level.bombs
            difficulty = level.difficulty
        elif port == 0:
            prng = Random()

        if save_filename:
            if not replay:
                save_replay.levels[stage_num - 1] = level = Level()
                level.random_seed = prng.seed
                level.score = first_player.score
                level.point_items = first_player.points
                level.power = first_player.power
                level.lives = first_player.lives
                level.bombs = first_player.bombs
                level.difficulty = difficulty
            save_keystates = []

        hints_stage = hints.stages[stage_num - 1] if hints else None

        game = game_class(resource_loader, stage_num, rank, difficulty,
                          common, prng, hints_stage, friendly_fire)

        if not enable_particles:
            def new_particle(pos, anim, amp, number=1, reverse=False, duration=24):
                pass
            game.new_particle = new_particle

        background = game.background if enable_background else None
        runner.load_game(game, background, game.std.bgms, replay, save_keystates)

        try:
            # Main loop
            window.run()
            break
        except NextStage:
            if not story or stage_num == (7 if boss_rush else 6 if rank > 0 else 5):
                break
            stage_num += 1
        except GameOver:
            show_simple_message_box(u'Game over!')
            break
        finally:
            if save_filename:
                last_key = -1
                for time, key in enumerate(save_keystates):
                    if key != last_key:
                        level.keys.append((time, key, 0))
                    last_key = key

    window.set_runner(None)

    if save_filename:
        with open(save_filename, 'wb+') as file:
            save_replay.write(file)


with SDL(sound=args.no_sound):
    window = Window(backend, Interface.width, Interface.height,
                    fps_limit=args.fps_limit, frameskip=args.frameskip)

    main(window, args.path, tuple(args.data), args.stage, args.rank,
         args.character, args.replay, args.save_replay, args.skip_replay,
         args.boss_rush, args.debug, args.no_background, args.no_particles,
         args.hints, args.port, args.remote, args.friendly_fire)

    import gc
    gc.collect()