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