Changeset 281:9e6ff3eb63d6


Ignore:
Timestamp:
04/08/11 18:11:45 (12 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Phase:
public
Message:

Enable testing of individual NPCs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • scripts/npc-test

    r249 r281  
    1313
    1414
    15 def run(npc_name, game_json):
     15def run(npc_name, game_json, interact):
    1616    game = GameState(game_json)
     17
     18    print "Testing ..."
     19    print "==========="
     20    test_npc(game, npc_name)
     21    print
     22
     23    if not interact:
     24        return
     25
    1726    npc = getattr(game.world.npcs, npc_name)
    1827    dsm = DSM(npc_name, game.world, npc.dsm, npc.state)
     
    4251
    4352
     53def test_npc(game, npc_name):
     54    """Test one npc."""
     55    print "Checking", npc_name, "...",
     56    npc = getattr(game.world.npcs, npc_name)
     57    dsm = DSM(npc_name, game.world, npc.dsm, npc.state)
     58    print "  Loaded %s." % (npc.dsm)
     59
     60    my_locals = { "world": dsm.world, "state": dsm.states, "npcs": dsm.world.npcs }
     61    my_locals.update(DsmEvent().items)
     62    for state_name, state in dsm.states.items():
     63        print "  Testing triggers for state %s" % state_name
     64        for trigger in state.triggers:
     65            eval(trigger._matches, {}, my_locals.copy())
     66            eval(trigger._next_state, {}, my_locals.copy())
     67        print "  Test on_entry and on_exit for state %s" % state_name
     68        state.enter(my_locals)
     69        state.leave(my_locals)
     70    print "ok"
     71
     72
    4473def test_all(game_json):
    4574    game = GameState(game_json)
     
    4877    print "============"
    4978    for npc_name in game.world.npcs:
    50         print "Checking", npc_name, "...",
    51         npc = getattr(game.world.npcs, npc_name)
    52         dsm = DSM(npc_name, game.world, npc.dsm, npc.state)
    53         print "  Loaded %s." % (npc.dsm)
    54 
    55         my_locals = { "world": dsm.world, "state": dsm.states, "npcs": dsm.world.npcs }
    56         my_locals.update(DsmEvent().items)
    57         for state_name, state in dsm.states.items():
    58             print "  Testing triggers for state %s" % state_name
    59             for trigger in state.triggers:
    60                 eval(trigger._matches, {}, my_locals.copy())
    61                 eval(trigger._next_state, {}, my_locals.copy())
    62             print "  Test on_entry and on_exit for state %s" % state_name
    63             state.enter(my_locals)
    64             state.leave(my_locals)
    65         print "ok"
     79        test_npc(game, npc_name)
    6680    print
    6781
     
    7993    parser.add_option("--all", action="store_true", default=False,
    8094            dest="test_all", help="test all NPCs in game")
     95    parser.add_option("--interact", "-i", action="store_true", default=False,
     96            dest="interact", help="enable interactive session with npc after testing")
    8197    parser.add_option("--game", default="game.json",
    8298            dest="game", help="game .json file to use")
     
    90106    if len(args) != 1:
    91107        parser.error("Must provide an npc json file")
    92     run(args[0], opts.game)
     108    run(args[0], opts.game, opts.interact)
    93109
    94110if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.