home · contact · privacy
Server: Split in many files, reorganize code slightly to fit change.
[plomrogue] / server / actions.py
1 from server.config.world_data import world_db
2 from server.io import log
3
4
5 def actor_wait(t):
6     """Make t do nothing (but loudly, if player avatar)."""
7     if t == world_db["Things"][0]:
8         log("You wait")
9
10
11 def actor_move(t):
12     """If passable, move/collide(=attack) thing into T_ARGUMENT's direction."""
13     from server.world import build_fov_map, decrement_lifepoints
14     from server.utils import mv_yx_in_dir_legal
15     from server.config.world_data import directions_db
16     passable = False
17     move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
18                                      t["T_POSY"], t["T_POSX"])
19     if 1 == move_result[0]:
20         pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
21         hitted = [id for id in world_db["Things"]
22                   if world_db["Things"][id] != t
23                   if world_db["Things"][id]["T_LIFEPOINTS"]
24                   if world_db["Things"][id]["T_POSY"] == move_result[1]
25                   if world_db["Things"][id]["T_POSX"] == move_result[2]]
26         if len(hitted):
27             hit_id = hitted[0]
28             if t == world_db["Things"][0]:
29                 hitted_type = world_db["Things"][hit_id]["T_TYPE"]
30                 hitted_name = world_db["ThingTypes"][hitted_type]["TT_NAME"]
31                 log("You WOUND" + hitted_name + ".")
32             elif 0 == hit_id:
33                 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
34                 log(hitter_name +" WOUNDS you.")
35             decrement_lifepoints(world_db["Things"][hit_id])
36             return
37         passable = "." == chr(world_db["MAP"][pos])
38     dir = [dir for dir in directions_db
39            if directions_db[dir] == chr(t["T_ARGUMENT"])][0]
40     if passable:
41         t["T_POSY"] = move_result[1]
42         t["T_POSX"] = move_result[2]
43         for id in t["T_CARRIES"]:
44             world_db["Things"][id]["T_POSY"] = move_result[1]
45             world_db["Things"][id]["T_POSX"] = move_result[2]
46         build_fov_map(t)
47         if t == world_db["Things"][0]:
48             log("You MOVE " + dir + ".")
49
50
51 def actor_pick_up(t):
52     """Make t pick up (topmost?) Thing from ground into inventory.
53
54     Define topmostness by how low the thing's type ID is.
55     """
56     ids = [id for id in world_db["Things"] if world_db["Things"][id] != t
57            if not world_db["Things"][id]["carried"]
58            if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
59            if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]
60     if len(ids):
61         lowest_tid = -1
62         for iid in ids:
63             tid = world_db["Things"][iid]["T_TYPE"]
64             if lowest_tid == -1 or tid < lowest_tid:
65                 id = iid
66                 lowest_tid = tid
67         world_db["Things"][id]["carried"] = True
68         t["T_CARRIES"].append(id)
69         if t == world_db["Things"][0]:
70                 log("You PICK UP an object.")
71
72
73 def actor_drop(t):
74     """Make t rop Thing from inventory to ground indexed by T_ARGUMENT."""
75     # TODO: Handle case where T_ARGUMENT matches nothing.
76     if len(t["T_CARRIES"]):
77         id = t["T_CARRIES"][t["T_ARGUMENT"]]
78         t["T_CARRIES"].remove(id)
79         world_db["Things"][id]["carried"] = False
80         if t == world_db["Things"][0]:
81             log("You DROP an object.")
82
83
84 def actor_use(t):
85     """Make t use (for now: consume) T_ARGUMENT-indexed Thing in inventory."""
86     # TODO: Handle case where T_ARGUMENT matches nothing.
87     if len(t["T_CARRIES"]):
88         id = t["T_CARRIES"][t["T_ARGUMENT"]]
89         type = world_db["Things"][id]["T_TYPE"]
90         if world_db["ThingTypes"][type]["TT_TOOL"] == "food":
91             t["T_CARRIES"].remove(id)
92             del world_db["Things"][id]
93             t["T_SATIATION"] += world_db["ThingTypes"][type]["TT_TOOLPOWER"]
94             if t == world_db["Things"][0]:
95                 log("You CONSUME this object.")
96         elif t == world_db["Things"][0]:
97             log("You try to use this object, but FAIL.")