예제 #1
0
def fetch_map(w, h, typ="default"):
    maparray = None
    roomlist = None
    clear_log()
    if typ == "hall_test":
        e = Engine({"w": w, "h": h, "pop_mode": "random"})
        e.add(["start", {"origin": "m"}])
        e.gen_map()
        maparray = e.maparray
        e.process_descriptions()
        roomlist = e.descriptions
    if typ == "binary_test":
        e = Engine({"w": w, "h": h, "pop_mode": "random"})
        e.add(["start", {"origin": "m"}])
        e.gen_map()
        return io.BytesIO(e.maparray.bytes())

    if typ == "splash":
        maparray, roomlist = gen_splash(w, h)
    add_log("fetch_map")
    return json.dumps(maparray_to_json(maparray, roomlist, get_log()))
예제 #2
0
from tarterus.engine import Engine

e = Engine({
    "w": 80,
    "h": 80,
    "log": True,
    "pop_mode": "script",
    "script_file": "tmp/output.json"
})
e.init_halls()
e.step()
e.gen_map()
with open("tmp/reoutput.o", "w") as f:
    log = "\n".join(str(r) for r in e.log_messages)
    f.write(log)
    f.write(str(e))
    e.write_choice_log("tmp/reoutput.json")
print("ok")
예제 #3
0
def room_tests():
    print("room.py")
    e = Engine({"w": 80, "h": 40, "pop_mode": "queue"})
    e.step()

    # test draw_room
    m = MapArray(('void', 0), (10, 10))
    m[5, 5] = ('vwal', 1)
    print("draw_room")
    _ = room.draw_room(m, 1, 1, 8, 8, ('room', 1))
    assert _ is True
    assert m[1, 5] == m[8, 7] == ('vwal', 1)
    assert m[1, 1] == m[8, 1] == ('tcor', 1)
    assert m[8, 8] == m[1, 8] == ('bcor', 1)
    assert m[5, 1] == m[2, 8] == ('hwal', 1)
    assert m[2, 2] == m[7, 7] == ('room', 1)
    # attempt to draw room with blocking tile in the way
    m = MapArray(('void', 0), (10, 10))
    m[5, 5] = ('hall', 1)
    _ = room.draw_room(m, 0, 0, 10, 10, 1)
    assert _ is False
    # draw room from dispatch
    room.dispatch_room(e, ["room", "draw", 1, 1, "e", (8, 8), ("room", 1)],
                       [1, 5, 6])
    m = e.maparray
    assert m[1, 5] == m[8, 7] == ('vwal', 1)
    assert m[1, 1] == m[8, 1] == ('tcor', 1)
    assert m[8, 8] == m[1, 8] == ('bcor', 1)
    assert m[5, 1] == m[2, 8] == ('hwal', 1)
    assert m[2, 2] == m[7, 7] == ('room', 1)

    # test find_loc
    print("find_loc")
    m = MapArray(('void', 0), (40, 40))
    x, y = room.find_loc(m, 20, 1, 20, 10, "s", 1, [6, 5])
    assert (x, y) == (10, 1), str((x, y))
    x, y = room.find_loc(m, 20, 10, 20, 10, "n", 1, [6, 5])
    assert (x, y) == (10, 1), str((x, y))
    m[11, 2] = ('hall', 1)
    x, y = room.find_loc(m, 20, 1, 20, 10, "s", 1, [6, 5])
    assert (x, y) == (19, 1), str((x, y))
    m[35, 2] = ('room', 2)
    x, y = room.find_loc(m, 20, 1, 20, 10, "s", 1, [6, 5])
    assert (x, y) == (15, 1), str((x, y))
    m = MapArray(('void', 0), (40, 40))
    m[4, 4] = ("room", 1)
    m[6, 11] = ("hall", 2)
    x, y = room.find_loc(m, 1, 8, 6, 6, "e", 1, [10, 4])
    assert (x, y) == (1, 5), str((x, y))
    m = MapArray(('void', 0), (40, 40))
    x, y = room.find_loc(m, 1, 10, 6, 6, "e", 1, [6, 5])
    assert (x, y) == (1, 7), str((x, y))
    x, y = room.find_loc(m, 10, 1, 6, 6, "s", 4, [6, 5])
    assert (x, y) == (9, 1), str((x, y))
    m = MapArray(('void', 0), (50, 50))
    # 6 + 5 = 11 - 11 = 0 + (8-2)//2 = 3
    # 30, 20 move north by (3-1) = 18
    x, y = room.find_loc(m, 30, 20, 8, 8, "w", 1, [6, 5])
    assert (x, y) == (23, 16), str((x, y))
    x, y = room.find_loc(m, 30, 20, 8, 8, "w", 4, [6, 5])
    assert (x, y) == (23, 19), str((x, y))

    # room tables
    e = Engine({
        "w": 80,
        "h": 40,
        "pop_mode": "queue",
        "log": True,
        "dont_terminate": True
    })
    e.step()
    e.add(['room', 'door', 10, 10, "n", 1, ('room', 1)])
    e.add(['room', 'passage', 30, 10, "s", 2, ('room', 1)])
    e.add(['hall', 'start', 50, 38, "n", 4, ('hall', 2)])
    e.add(['hall', 'start', 70, 1, "s", 4, ('hall', 2)])
    e.send([
        'load_dice',
        [
            13, 6, 5, 13, 7, 2, 11, 1, 1, 1, 1, 1, 15, 1, 1, 13, 7, 2, 6, 1, 1,
            15, 1, 1, 15, 1, 1, 8, 5, 7
        ]
    ])
    e.gen_map()
    print("\n".join(str(m) for m in e.log_messages))
    # print(e)
    e = Engine({
        "w": 80,
        "h": 40,
        "pop_mode": "queue",
        "log": True,
        "dont_terminate": True
    })
    e.step()
    e.add(['hall', 'start', 20, 1, 's', 4, ('hall', 1)])
    e.send([
        'load_dice',
        [7, 2, 10, 8, 7, 9, 15, 2, 2, 19, 11, 9, 1, 8, 7, 14, 2, 2, 11, 2, 3]
    ])
    e.gen_map()
예제 #4
0
def engine_tests():
    print("Engine tests:")
    e = Engine({"w": 80, "h": 40, "pop_mode": "queue", "log": True})
    assert e.pending.pop == e.pending.queue_pop
    e.step()
    assert isinstance(e.send(["get"])['maparray'], MapArray), str(e)
    assert isinstance(e.send(["get"])['pending'], PendingList), str(e)
    _ = e.send(["add", ["hall", "void", 38, 38, "n", 4, [1, 1]]])
    assert _ == 1, str(e.pending)
    _ = e.send(["add", ["door", "void", 10, 10, "e", 2, [1, 1]]])
    assert _ == 2, str(e.pending)
    _ = e.send(["get"])["pending"][0]
    assert _[0] == "hall", str(_)
    _ = e.pending.pop()
    assert _[0] == "hall", str(_)

    print("Test logging")
    e = Engine({"w": 80, "h": 40, "pop_mode": "queue", "log": True})
    e.step()
    e.send(["log", "testing_one_two_three"])
    print(e.log_messages)

    # test that pop is random with default construction
    # 1 in 2^16 chance that this test fails erroneously
    results = []
    for i in range(16):
        e.send(["set_params", {"w": 40, "h": 20}])
        assert 40 == e.params["w"]
        assert 20 == e.params["h"]
        assert "x" == e.params.get("pop_mode", "x")
        e.send(["clear"])
        assert e.pending.pop != e.pending.queue_pop
        assert e.pending.pop == e.pending.random_pop
        assert 1 == e.send(["add", ["hall", "void", 38, 38, "n", 4, [1, 1]]])
        assert 2 == e.send(["add", ["door", "void", 10, 10, "e", 2, [1, 1]]])
        results.append(e.pending.pop()[0] == "door")
    assert any(x is True for x in results), "random test failed"
    assert not all(x is True for x in results), "random test failed"

    e = Engine({"w": 80, "h": 40, "pop_mode": "queue", "log": True})
    e.step()
    #     e.step()
    #     e.add(['hall', 'start', 40, 30, "n", 4, ('hall', 1)])
    #     e.send(["step_with_command", {"dice": [8, 9]}])
    #     e.send(["step_with_command", {"dice": [11]}])
    #     e.send(["step_with_command", {"dice": [17]}])
    #     e.send(["step_with_command", {"dice": [11]}])
    #     e.send(["step_with_command", {"dice": [11]}])
    #     e.send(["step_with_command", {"dice": [11]}])
    #     e.send(["step_with_command", {"dice": [11]}])

    e.send(['clear'])
    e.add(['hall', 'start', 20, 1, "s", 4, ('hall', 1)])
    e.add(['hall', 'start', 40, 1, "s", 4, ('hall', 2)])
    e.add(['hall', 'start', 60, 1, "s", 4, ('hall', 3)])
    e.add(['hall', 'start', 78, 15, "w", 4, ('hall', 4)])
    e.add(['hall', 'start', 78, 30, "w", 4, ('hall', 5)])
    e.add(['hall', 'start', 40, 38, "n", 4, ('hall', 6)])
    e.add(['hall', 'start', 20, 38, "n", 4, ('hall', 7)])
    e.add(['hall', 'start', 1, 15, "e", 4, ('hall', 8)])
    e.gen_map()
    print(e)
    print("\n".join(str(m) for m in e.log_messages))

    print("test loaded dice")
    e.send(["clear"])
    print(e.send(["load_dice", [1, 2, 3, 4, 5]]))
    print(e.send(["roll", [20, 20, 20, 20, 20, 10000]]))