view scripts/npc-test @ 634:20d6aef11249 default tip

Fix iCCC profiles in PNGs to avoid verbose warnings from libpng.
author Simon Cross <hodgestar@gmail.com>
date Fri, 27 Jan 2023 23:32:07 +0100
parents 5a8a7f17597d
children
line wrap: on
line source

#!/usr/bin/env python
"Skaapsteker npc tester"

import os.path
import sys
import optparse
from pprint import pprint

sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

from skaapsteker.dialogue import DSM, DsmEvent
from skaapsteker.gamestate import GameState


def run(npc_name, game_json, interact):
    game = GameState(game_json)
    game.new_game()

    print "Testing ..."
    print "==========="
    test_npc(game, npc_name)
    print

    if not interact:
        return

    npc = getattr(game.world.npcs, npc_name)
    dsm = DSM(npc_name, game.world, npc.dsm, npc.state)

    print "States:"
    print "-------"
    pprint(dsm.states.keys())
    print

    while True:
        state = dsm.get_state()
        text = state.text
        if text is not None:
            text = text.encode('utf-8')
        print "%s:" % dsm.state, text
        print "--"
        for i, choice in dsm.choices():
            print "%d: %s" % (i, choice)
        if state.auto_next:
            print "N: Next"
        else:
            print "L: Leave"
        print "--"

        key = raw_input("Choice? ")
        key = key.strip().upper()
        if key == "L":
            break
        if key == "N":
            dsm.auto_next()
        elif key.isdigit():
            dsm.choice(int(key))

        print "--"


def test_npc(game, npc_name):
    """Test one npc."""
    print "Checking", npc_name, "...",
    npc = getattr(game.world.npcs, npc_name)
    dsm = DSM(npc_name, game.world, npc.dsm, npc.state)
    print "  Loaded %s." % (npc.dsm)

    def test_switch_to(npc_name):
        assert npc_name in game.world.npcs, "Unknown NPC %r" % (npc_name,)

    def test_drop_item(item_name):
        assert item_name in game.world.items, "Unknown item %r" % (item_name,)

    my_locals = {
        "world": dsm.world,
        "state": dsm.states,
        "npcs": dsm.world.npcs,
        "switch_to": test_switch_to,
        "drop_item": test_drop_item,
    }
    my_locals.update(DsmEvent().items)

    for state_name, state in dsm.states.items():
        print "  Testing triggers for state %s" % state_name
        for trigger in state.triggers:
            eval(trigger._matches, {}, my_locals.copy())
            eval(trigger._next_state, {}, my_locals.copy())
        print "  Test on_entry and on_exit for state %s" % state_name
        state.enter(my_locals)
        state.leave(my_locals)
        list(state.choices(my_locals))
    print "ok"


def test_all(game_json):
    game = GameState(game_json)
    game.new_game()

    print "Testing NPCs"
    print "============"
    for npc_name in game.world.npcs:
        test_npc(game, npc_name)
    print

    print "Testing Sprites"
    print "==============="
    for level in game.world.levels:
        print "Checking", level, "...",
        game.create_sprites(level)
        print "ok"
    print


def main():
    parser = optparse.OptionParser(usage="%prog [options] <npc name>")
    parser.add_option("--all", action="store_true", default=False,
            dest="test_all", help="test all NPCs in game")
    parser.add_option("--interact", "-i", action="store_true", default=False,
            dest="interact", help="enable interactive session with npc after testing")
    parser.add_option("--game", default="game.json",
            dest="game", help="game .json file to use")

    opts, args = parser.parse_args()

    if opts.test_all:
        test_all(opts.game)
        return

    if len(args) != 1:
        parser.error("Must provide an npc json file")
    run(args[0], opts.game, opts.interact)

if __name__ == '__main__':
    main()