Esempio n. 1
0
def make_world(seed):
    """(Re-)build game world, i.e. map, things, to a new turn 1 from seed.

    Seed rand with seed. Do more only with a "wait" ThingAction and
    world["PLAYER_TYPE"] matching ThingType of TT_START_NUMBER > 0. Then,
    world_db["Things"] emptied, call make_map() and set
    world_db["WORLD_ACTIVE"], world_db["TURN"] to 1. Build new Things
    according to ThingTypes' TT_START_NUMBERS, with Thing of ID 0 to ThingType
    of ID = world["PLAYER_TYPE"]. Place Things randomly, and actors not on each
    other. Init player's memory map. Write "NEW_WORLD" line to out file.
    """
    def free_pos(type):
        i = 0
        while 1:
            err = "Space to put thing on too hard to find. Map too small?"
            while 1:
                y = rand.next() % world_db["MAP_LENGTH"]
                x = rand.next() % world_db["MAP_LENGTH"]
                if chr(world_db["MAP"][y * world_db["MAP_LENGTH"] + x]) in \
                    symbols_passable and pos_test(type, y, x):
                    break
                i += 1
                if i == 65535:
                    raise SystemExit(err)
            # Replica of C code, wrongly ignores animatedness of new Thing.
            pos_clear = (0 == len([id for id in world_db["Things"]
                                   if world_db["Things"][id]["T_LIFEPOINTS"]
                                   if world_db["Things"][id]["T_POSY"] == y
                                   if world_db["Things"][id]["T_POSX"] == x]))
            if pos_clear:
                break
        return (y, x)

    playertype = world_makable()
    if playertype < 0:
        return
    rand.seed = seed
    libpr.set_maplength(world_db["MAP_LENGTH"])
    world_db["Things"] = {}
    make_map()
    world_db["WORLD_ACTIVE"] = 1
    world_db["TURN"] = 1
    for i in range(world_db["ThingTypes"][playertype]["TT_START_NUMBER"]):
        id = id_setter(-1, "Things")
        world_db["Things"][id] = new_Thing(playertype, free_pos(playertype))
    if not world_db["Things"][0]["fovmap"]:
        empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
        world_db["Things"][0]["fovmap"] = empty_fovmap
    update_map_memory(world_db["Things"][0])
    for type in world_db["ThingTypes"]:
        for i in range(world_db["ThingTypes"][type]["TT_START_NUMBER"]):
            if type != playertype:
                id = id_setter(-1, "Things")
                world_db["Things"][id] = new_Thing(type, free_pos(type))
    strong_write(io_db["file_out"], "NEW_WORLD\n")
Esempio n. 2
0
 def helper(str_int):
     val = integer_test(str_int, 0, 255)
     if None != val:
         if val < world_db["MAP_LENGTH"]:
             t = world_db["Things"][command_tid.id]
             t["T_POS" + axis] = val
             t["pos"] = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
             if world_db["WORLD_ACTIVE"] \
                and world_db["Things"][command_tid.id]["T_LIFEPOINTS"]:
                 build_fov_map(world_db["Things"][command_tid.id])
                 if 0 == command_tid.id:
                     update_map_memory(world_db["Things"][command_tid.id])
         else:
             print("Ignoring: Position is outside of map.")
Esempio n. 3
0
 def helper(str_int):
     val = integer_test(str_int, 0, 255)
     if None != val:
         if val < world_db["MAP_LENGTH"]:
             t = world_db["Things"][command_tid.id]
             t["T_POS" + axis] = val
             t["pos"] = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
             if world_db["WORLD_ACTIVE"] \
                and world_db["Things"][command_tid.id]["T_LIFEPOINTS"]:
                 build_fov_map(world_db["Things"][command_tid.id])
                 if 0 == command_tid.id:
                     update_map_memory(world_db["Things"][command_tid.id])
         else:
             print("Ignoring: Position is outside of map.")
Esempio n. 4
0
def turn_over():
    """Run game world and its inhabitants until new player input expected."""
    from server.ai import ai
    from server.config.actions import action_db
    from server.config.misc import calc_effort
    from server.update_map_memory import update_map_memory
    from server.thingproliferation import thingproliferation
    from server.io import try_worldstate_update
    from server.config.io import io_db
    id = 0
    while world_db["Things"][0]["T_LIFEPOINTS"]:
        proliferable_map = world_db["MAP"][:]
        for tid in [
                tid for tid in world_db["Things"]
                if not world_db["Things"][tid]["carried"]
        ]:
            y = world_db["Things"][tid]["T_POSY"]
            x = world_db["Things"][tid]["T_POSX"]
            proliferable_map[y * world_db["MAP_LENGTH"] + x] = ord('X')
        for id in [id for id in world_db["Things"]]:  # Only what's from start!
            if not id in world_db["Things"] or \
               world_db["Things"][id]["carried"]:   # May have been consumed or
                continue  # picked up during turn …
            Thing = world_db["Things"][id]
            if Thing["T_LIFEPOINTS"]:
                if not Thing["T_COMMAND"]:
                    update_map_memory(Thing)
                    if 0 == id:
                        return
                    ai(Thing)
                try_healing(Thing)
                hunger(Thing)
                if Thing["T_LIFEPOINTS"]:
                    Thing["T_PROGRESS"] += 1
                    taid = [
                        a for a in world_db["ThingActions"]
                        if a == Thing["T_COMMAND"]
                    ][0]
                    ThingAction = world_db["ThingActions"][taid]
                    effort = calc_effort(ThingAction, Thing)
                    if Thing["T_PROGRESS"] == effort:
                        action = action_db["actor_" + ThingAction["TA_NAME"]]
                        action(Thing)
                        Thing["T_COMMAND"] = 0
                        Thing["T_PROGRESS"] = 0
            thingproliferation(Thing, proliferable_map)
        world_db["TURN"] += 1
        io_db["worldstate_updateable"] = True
        try_worldstate_update()
Esempio n. 5
0
def turn_over():
    """Run game world and its inhabitants until new player input expected."""
    from server.ai import ai
    from server.config.actions import action_db
    from server.config.misc import calc_effort
    from server.update_map_memory import update_map_memory
    from server.thingproliferation import thingproliferation
    from server.io import try_worldstate_update
    from server.config.io import io_db
    id = 0
    while world_db["Things"][0]["T_LIFEPOINTS"]:
        proliferable_map = world_db["MAP"][:]
        for tid in [tid for tid in world_db["Things"]
                   if not world_db["Things"][tid]["carried"]]:
            y = world_db["Things"][tid]["T_POSY"]
            x = world_db["Things"][tid]["T_POSX"]
            proliferable_map[y * world_db["MAP_LENGTH"] + x] = ord('X')
        for id in [id for id in world_db["Things"]]:  # Only what's from start!
            if not id in world_db["Things"] or \
               world_db["Things"][id]["carried"]:   # May have been consumed or
                continue                            # picked up during turn …
            Thing = world_db["Things"][id]
            if Thing["T_LIFEPOINTS"]:
                if not Thing["T_COMMAND"]:
                    update_map_memory(Thing)
                    if 0 == id:
                        return
                    ai(Thing)
                try_healing(Thing)
                hunger(Thing)
                if Thing["T_LIFEPOINTS"]:
                    Thing["T_PROGRESS"] += 1
                    taid = [a for a in world_db["ThingActions"]
                              if a == Thing["T_COMMAND"]][0]
                    ThingAction = world_db["ThingActions"][taid]
                    effort = calc_effort(ThingAction, Thing)
                    if Thing["T_PROGRESS"] == effort:
                        action = action_db["actor_" + ThingAction["TA_NAME"]]
                        action(Thing)
                        Thing["T_COMMAND"] = 0
                        Thing["T_PROGRESS"] = 0
            thingproliferation(Thing, proliferable_map)
        world_db["TURN"] += 1
        io_db["worldstate_updateable"] = True
        try_worldstate_update()
Esempio n. 6
0
def command_worldactive(worldactive_string):
    """Toggle world_db["WORLD_ACTIVE"] if possible.

    An active world can always be set inactive. An inactive world can only be
    set active with a "wait" ThingAction, and a player Thing (of ID 0), and a
    map. On activation, rebuild all Things' FOVs, and the player's map memory.
    """
    val = integer_test(worldactive_string, 0, 1)
    if None != val:
        if 0 != world_db["WORLD_ACTIVE"]:
            if 0 == val:
                set_world_inactive()
            else:
                print("World already active.")
        elif 0 == world_db["WORLD_ACTIVE"]:
            for ThingAction in world_db["ThingActions"]:
                if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
                    break
            else:
                print("Ignored: No wait action defined for world to activate.")
                return
            for Thing in world_db["Things"]:
                if 0 == Thing:
                    break
            else:
                print("Ignored: No player defined for world to activate.")
                return
            if not world_db["MAP"]:
                print("Ignoring: No map defined for world to activate.")
                return
            from server.config.commands import command_worldactive_test_hook
            if not command_worldactive_test_hook():
                return
            for tid in world_db["Things"]:
                if world_db["Things"][tid]["T_LIFEPOINTS"]:
                    build_fov_map(world_db["Things"][tid])
                    if 0 == tid:
                        update_map_memory(world_db["Things"][tid], False)
            if not world_db["Things"][0]["T_LIFEPOINTS"]:
                empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
                world_db["Things"][0]["fovmap"] = empty_fovmap
            world_db["WORLD_ACTIVE"] = 1
Esempio n. 7
0
def command_worldactive(worldactive_string):
    """Toggle world_db["WORLD_ACTIVE"] if possible.

    An active world can always be set inactive. An inactive world can only be
    set active with a "wait" ThingAction, and a player Thing (of ID 0), and a
    map. On activation, rebuild all Things' FOVs, and the player's map memory.
    """
    val = integer_test(worldactive_string, 0, 1)
    if None != val:
        if 0 != world_db["WORLD_ACTIVE"]:
            if 0 == val:
                set_world_inactive()
            else:
                print("World already active.")
        elif 0 == world_db["WORLD_ACTIVE"]:
            for ThingAction in world_db["ThingActions"]:
                if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
                    break
            else:
                print("Ignored: No wait action defined for world to activate.")
                return
            for Thing in world_db["Things"]:
                if 0 == Thing:
                    break
            else:
                print("Ignored: No player defined for world to activate.")
                return
            if not world_db["MAP"]:
                print("Ignoring: No map defined for world to activate.")
                return
            from server.config.commands import command_worldactive_test_hook
            if not command_worldactive_test_hook():
                return
            for tid in world_db["Things"]:
                if world_db["Things"][tid]["T_LIFEPOINTS"]:
                    build_fov_map(world_db["Things"][tid])
                    if 0 == tid:
                        update_map_memory(world_db["Things"][tid], False)
            if not world_db["Things"][0]["T_LIFEPOINTS"]:
                empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"]**2)
                world_db["Things"][0]["fovmap"] = empty_fovmap
            world_db["WORLD_ACTIVE"] = 1