Esempio n. 1
0
def actor_move_attempts_hook(t, move_result, pos):
    if (ord("X") == world_db["MAP"][pos] or ord("|") == world_db["MAP"][pos]):
        for tid in t["T_CARRIES"]:
            ty = world_db["Things"][tid]["T_TYPE"]
            if world_db["ThingTypes"][ty]["TT_TOOL"] == "axe":
                axe_name = world_db["ThingTypes"][ty]["TT_NAME"]
                if t == world_db["Things"][0]:
                    log("With your " + axe_name + ", you chop!")
                    if ord("X") == world_db["MAP"][pos]:
                        world_db["GOD_FAVOR"] -= 1
                chop_power = world_db["ThingTypes"][ty]["TT_TOOLPOWER"]
                case_X = world_db["MAP"][pos] == ord("X")
                if (chop_power > 0
                        and ((case_X and 0 == int(rand.next() / chop_power)) or
                             (not case_X and 0 == int(rand.next() /
                                                      (3 * chop_power))))):
                    if t == world_db["Things"][0]:
                        log("You chop it DOWN.")
                        if ord("X") == world_db["MAP"][pos]:
                            world_db["GOD_FAVOR"] -= 10
                    world_db["MAP"][pos] = ord(".")
                    i = 3 if case_X else 1
                    from server.new_thing import new_Thing
                    for i in range(i):
                        tid = id_setter(-1, "Things")
                        world_db["Things"][tid] = new_Thing(
                            world_db["LUMBER"],
                            (move_result[1], move_result[2]))
                    build_fov_map(t)
                return True
    return False
Esempio n. 2
0
def thingproliferation(t, prol_map):
    """To chance of 1/TT_PROLIFERATE, create t offspring in open neighbor cell.

    Naturally only works with TT_PROLIFERATE > 0. The neighbor cell must be be
    marked passable in prol_map. If there are several map cell candidates, one
    is selected randomly.
    """
    from server.config.world_data import directions_db, world_db, \
        thingprol_field_spreadable, thingprol_test_hook, \
        thingprol_post_create_hook
    from server.utils import mv_yx_in_dir_legal, rand, id_setter
    from server.new_thing import new_Thing
    prolscore = world_db["ThingTypes"][t["T_TYPE"]]["TT_PROLIFERATE"]
    if prolscore and (1 == prolscore or 1 == (rand.next() % prolscore)) and \
        thingprol_test_hook(t):
        candidates = []
        for key in sorted(directions_db.keys()):
            mv_result = mv_yx_in_dir_legal(directions_db[key], t["T_POSY"],
                                           t["T_POSX"])
            c = chr(prol_map[mv_result[1] * world_db["MAP_LENGTH"]
                + mv_result[2]])
            if mv_result[0] and thingprol_field_spreadable(c, t):
                from server.io import log
                candidates.append((mv_result[1], mv_result[2]))
        if len(candidates):
            i = rand.next() % len(candidates)
            tid = id_setter(-1, "Things")
            newT = new_Thing(t["T_TYPE"], (candidates[i][0], candidates[i][1]))
            world_db["Things"][tid] = newT
            thingprol_post_create_hook(t)
Esempio n. 3
0
def thingproliferation(t, prol_map):
    """To chance of 1/TT_PROLIFERATE, create t offspring in open neighbor cell.

    Naturally only works with TT_PROLIFERATE > 0. The neighbor cell must be be
    marked passable in prol_map. If there are several map cell candidates, one
    is selected randomly.
    """
    from server.config.world_data import directions_db, world_db, \
        thingprol_field_spreadable, thingprol_test_hook, \
        thingprol_post_create_hook
    from server.utils import mv_yx_in_dir_legal, rand, id_setter
    from server.new_thing import new_Thing
    prolscore = world_db["ThingTypes"][t["T_TYPE"]]["TT_PROLIFERATE"]
    if prolscore and (1 == prolscore or 1 == (rand.next() % prolscore)) and \
        thingprol_test_hook(t):
        candidates = []
        for key in sorted(directions_db.keys()):
            mv_result = mv_yx_in_dir_legal(directions_db[key], t["T_POSY"],
                                           t["T_POSX"])
            c = chr(prol_map[mv_result[1] * world_db["MAP_LENGTH"] +
                             mv_result[2]])
            if mv_result[0] and thingprol_field_spreadable(c, t):
                from server.io import log
                candidates.append((mv_result[1], mv_result[2]))
        if len(candidates):
            i = rand.next() % len(candidates)
            tid = id_setter(-1, "Things")
            newT = new_Thing(t["T_TYPE"], (candidates[i][0], candidates[i][1]))
            world_db["Things"][tid] = newT
            thingprol_post_create_hook(t)
Esempio n. 4
0
def actor_move_attempts_hook(t, move_result, pos):
    if (ord("X") == world_db["MAP"][pos] or ord("|") == world_db["MAP"][pos]):
        for tid in t["T_CARRIES"]:
            ty = world_db["Things"][tid]["T_TYPE"]
            if world_db["ThingTypes"][ty]["TT_TOOL"] == "axe":
                axe_name = world_db["ThingTypes"][ty]["TT_NAME"]
                if t == world_db["Things"][0]:
                    log("With your " + axe_name + ", you chop!")
                    if ord("X") == world_db["MAP"][pos]:
                        world_db["GOD_FAVOR"] -= 1
                chop_power = world_db["ThingTypes"][ty]["TT_TOOLPOWER"]
                case_X = world_db["MAP"][pos] == ord("X")
                if (chop_power > 0 and
                    ((case_X and 0 == int(rand.next() / chop_power))
                     or (not case_X and
                         0 == int(rand.next() / (3 * chop_power))))):
                    if t == world_db["Things"][0]:
                        log("You chop it DOWN.")
                        if ord("X") == world_db["MAP"][pos]:
                            world_db["GOD_FAVOR"] -= 10
                    world_db["MAP"][pos] = ord(".")
                    i = 3 if case_X else 1
                    from server.new_thing import new_Thing
                    for i in range(i):
                        tid = id_setter(-1, "Things")
                        world_db["Things"][tid] = new_Thing(world_db["LUMBER"],
                                    (move_result[1], move_result[2]))
                    build_fov_map(t)
                return True
    return False
Esempio n. 5
0
def make_map():
    """(Re-)make island map.

    Let "~" represent water, "." land, "X" trees: Build island shape randomly,
    start with one land cell in the middle, then go into cycle of repeatedly
    selecting a random sea cell and transforming it into land if it is neighbor
    to land. The cycle ends when a land cell is due to be created at the map's
    border. Then put some trees on the map (TODO: more precise algorithm desc).
    """
    world_db["MAP"] = bytearray(b'~' * (world_db["MAP_LENGTH"]**2))
    length = world_db["MAP_LENGTH"]
    add_half_width = (not (length % 2)) * int(length / 2)
    world_db["MAP"][int((length**2) / 2) + add_half_width] = ord(".")
    while (1):
        y, x, pos = new_pos()
        if "~" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "."):
            if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
                break
            world_db["MAP"][pos] = ord(".")
    n_trees = int((length**2) / 16)
    i_trees = 0
    while (i_trees <= n_trees):
        single_allowed = rand.next() % 32
        y, x, pos = new_pos()
        if "." == chr(world_db["MAP"][pos]) \
                and ((not single_allowed) or is_neighbor((y, x), "X")):
            world_db["MAP"][pos] = ord("X")
            i_trees += 1
Esempio n. 6
0
def make_map():
    """(Re-)make island map.

    Let "~" represent water, "." land, "X" trees: Build island shape randomly,
    start with one land cell in the middle, then go into cycle of repeatedly
    selecting a random sea cell and transforming it into land if it is neighbor
    to land. The cycle ends when a land cell is due to be created at the map's
    border. Then put some trees on the map (TODO: more precise algorithm desc).
    """
    world_db["MAP"] = bytearray(b'~' * (world_db["MAP_LENGTH"] ** 2))
    length = world_db["MAP_LENGTH"]
    add_half_width = (not (length % 2)) * int(length / 2)
    world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord(".")
    while (1):
        y, x, pos = new_pos()
        if "~" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "."):
            if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
                break
            world_db["MAP"][pos] = ord(".")
    n_trees = int((length ** 2) / 16)
    i_trees = 0
    while (i_trees <= n_trees):
        single_allowed = rand.next() % 32
        y, x, pos = new_pos()
        if "." == chr(world_db["MAP"][pos]) \
                and ((not single_allowed) or is_neighbor((y, x), "X")):
            world_db["MAP"][pos] = ord("X")
            i_trees += 1
Esempio n. 7
0
 def rand_target_dir(neighbors, cmp, dirs):
     candidates = []
     n_candidates = 0
     for i in range(len(dirs)):
         if cmp == neighbors[i]:
             candidates.append(dirs[i])
             n_candidates += 1
     return candidates[rand.next() % n_candidates] if n_candidates else 0
Esempio n. 8
0
 def rand_target_dir(neighbors, cmp, dirs):
     candidates = []
     n_candidates = 0
     for i in range(len(dirs)):
         if cmp == neighbors[i]:
             candidates.append(dirs[i])
             n_candidates += 1
     return candidates[rand.next() % n_candidates] if n_candidates else 0
Esempio n. 9
0
def hunger(t):
    """Decrement t's satiation,dependent on it trigger lifepoint dec chance."""
    from server.config.misc import decrement_lifepoints
    if t["T_SATIATION"] > -32768:
        t["T_SATIATION"] -= hunger_per_turn(t["T_TYPE"])
    if 0 != t["T_SATIATION"] and 0 == int(rand.next() / abs(t["T_SATIATION"])):
        if t == world_db["Things"][0]:
            if t["T_SATIATION"] < 0:
                log("You SUFFER from hunger.")
            else:
                log("You SUFFER from over-eating.")
        decrement_lifepoints(t)
Esempio n. 10
0
 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)
Esempio n. 11
0
def hunger(t):
    """Decrement t's satiation,dependent on it trigger lifepoint dec chance."""
    from server.config.misc import decrement_lifepoints
    if t["T_SATIATION"] > -32768:
        t["T_SATIATION"] -= hunger_per_turn(t["T_TYPE"])
    if 0 != t["T_SATIATION"] and 0 == int(rand.next() / abs(t["T_SATIATION"])):
        if t == world_db["Things"][0]:
            if t["T_SATIATION"] < 0:
                log("You SUFFER from hunger.")
            else:
                log("You SUFFER from over-eating.")
        decrement_lifepoints(t)
Esempio n. 12
0
def try_healing(t):
    """If t's HP < max, increment them if well-nourished, maybe waiting."""
    if t["T_LIFEPOINTS"] < \
       world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]:
        wait_id = [id for id in world_db["ThingActions"]
                      if world_db["ThingActions"][id]["TA_NAME"] == "wait"][0]
        wait_divider = 8 if t["T_COMMAND"] == wait_id else 1
        testval = int(abs(t["T_SATIATION"]) / wait_divider)
        if (testval <= 1 or 1 == (rand.next() % testval)):
            t["T_LIFEPOINTS"] += 1
            if t == world_db["Things"][0]:
                log("You HEAL.")
Esempio n. 13
0
def try_healing(t):
    """If t's HP < max, increment them if well-nourished, maybe waiting."""
    if t["T_LIFEPOINTS"] < \
       world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]:
        wait_id = [
            id for id in world_db["ThingActions"]
            if world_db["ThingActions"][id]["TA_NAME"] == "wait"
        ][0]
        wait_divider = 8 if t["T_COMMAND"] == wait_id else 1
        testval = int(abs(t["T_SATIATION"]) / wait_divider)
        if (testval <= 1 or 1 == (rand.next() % testval)):
            t["T_LIFEPOINTS"] += 1
            if t == world_db["Things"][0]:
                log("You HEAL.")
Esempio n. 14
0
def make_map():
    from server.make_map import make_map, is_neighbor, new_pos
    global rand
    make_map()
    length = world_db["MAP_LENGTH"]
    n_colons = int((length ** 2) / 16)
    i_colons = 0
    while (i_colons <= n_colons):
        single_allowed = rand.next() % 256
        y, x, pos = new_pos()
        if ("." == chr(world_db["MAP"][pos])
          and ((not single_allowed) or is_neighbor((y, x), ":"))):
            world_db["MAP"][pos] = ord(":")
            i_colons += 1
    altar_placed = False
    while not altar_placed:
        y, x, pos = new_pos()
        if (("." == chr(world_db["MAP"][pos]
             or ":" == chr(world_db["MAP"][pos]))
            and not is_neighbor((y, x), "X"))):
            world_db["MAP"][pos] = ord("_")
            world_db["altar"] = (y, x)
            altar_placed = True
Esempio n. 15
0
def make_map():
    from server.make_map import make_map, is_neighbor, new_pos
    global rand
    make_map()
    length = world_db["MAP_LENGTH"]
    n_colons = int((length**2) / 16)
    i_colons = 0
    while (i_colons <= n_colons):
        single_allowed = rand.next() % 256
        y, x, pos = new_pos()
        if ("." == chr(world_db["MAP"][pos])
                and ((not single_allowed) or is_neighbor((y, x), ":"))):
            world_db["MAP"][pos] = ord(":")
            i_colons += 1
    altar_placed = False
    while not altar_placed:
        y, x, pos = new_pos()
        if (("." == chr(world_db["MAP"][pos]
                        or ":" == chr(world_db["MAP"][pos]))
             and not is_neighbor((y, x), "X"))):
            world_db["MAP"][pos] = ord("_")
            world_db["altar"] = (y, x)
            altar_placed = True
Esempio n. 16
0
def new_pos():
    length = world_db["MAP_LENGTH"]
    y = rand.next() % length
    x = rand.next() % length
    return y, x, (y * length) + x
Esempio n. 17
0
def new_pos():
    length = world_db["MAP_LENGTH"]
    y = rand.next() % length
    x = rand.next() % length
    return y, x, (y * length) + x