home · contact · privacy
Server: Tell player about deaths his wounding is responsible for.
[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.build_fov_map import build_fov_map
19     from server.config.misc import decrement_lifepoints_func
20     from server.utils import mv_yx_in_dir_legal
21     from server.config.world_data import directions_db, symbols_passable
22     passable = False
23     move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
24                                      t["T_POSY"], t["T_POSX"])
25     if 1 == move_result[0]:
26         pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
27         hitted = [id for id in world_db["Things"]
28                   if world_db["Things"][id] != t
29                   if world_db["Things"][id]["T_LIFEPOINTS"]
30                   if world_db["Things"][id]["T_POSY"] == move_result[1]
31                   if world_db["Things"][id]["T_POSX"] == move_result[2]]
32         if len(hitted):
33             hit_id = hitted[0]
34             hitted_type_id = world_db["Things"][hit_id]["T_TYPE"]
35             if t == world_db["Things"][0]:
36                 hitted_name = world_db["ThingTypes"][hitted_type_id]["TT_NAME"]
37                 log("You WOUND " + hitted_name + ".")
38             elif 0 == hit_id:
39                 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
40                 log(hitter_name +" WOUNDS you.")
41             decr_test = decrement_lifepoints_func(world_db["Things"][hit_id])
42             if decr_test > 0 and t == world_db["Things"][0]:
43                 log(hitted_name + " dies.")
44         passable = chr(world_db["MAP"][pos]) in symbols_passable
45     dir = [dir for dir in directions_db
46            if directions_db[dir] == chr(t["T_ARGUMENT"])][0]
47     if passable:
48         t["T_POSY"] = move_result[1]
49         t["T_POSX"] = move_result[2]
50         for id in t["T_CARRIES"]:
51             world_db["Things"][id]["T_POSY"] = move_result[1]
52             world_db["Things"][id]["T_POSX"] = move_result[2]
53         build_fov_map(t)
54         if t == world_db["Things"][0]:
55             log("You MOVE " + dir + ".")
56
57
58 def actor_pickup(t):
59     """Make t pick up (topmost?) Thing from ground into inventory. Return it.
60
61     Define topmostness by how low the thing's type ID is.
62     """
63     ids = [id for id in world_db["Things"] if world_db["Things"][id] != t
64            if not world_db["Things"][id]["carried"]
65            if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
66            if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]
67     if len(ids):
68         lowest_tid = -1
69         for iid in ids:
70             tid = world_db["Things"][iid]["T_TYPE"]
71             from server.config.actions import actor_pickup_test_hook
72             if (lowest_tid == -1 or tid < lowest_tid) and \
73                     actor_pickup_test_hook(t, tid):
74                 id = iid
75                 lowest_tid = tid
76         world_db["Things"][id]["carried"] = True
77         t["T_CARRIES"].append(id)
78         if t == world_db["Things"][0]:
79                 log("You PICK UP an object.")
80         return world_db["Things"][id]
81
82 def actor_drop(t):
83     """Drop to ground from t's inventory, return T_ARGUMENT-indexed Thing."""
84     # TODO: Handle case where T_ARGUMENT matches nothing.
85     if len(t["T_CARRIES"]):
86         id = t["T_CARRIES"][t["T_ARGUMENT"]]
87         t["T_CARRIES"].remove(id)
88         world_db["Things"][id]["carried"] = False
89         if t == world_db["Things"][0]:
90             log("You DROP an object.")
91             return world_db["Things"][id]
92
93 def actor_use(t):
94     """Make t use (for now: consume) T_ARGUMENT-indexed Thing in inventory."""
95     # TODO: Handle case where T_ARGUMENT matches nothing.
96     if len(t["T_CARRIES"]):
97         id = t["T_CARRIES"][t["T_ARGUMENT"]]
98         type = world_db["Things"][id]["T_TYPE"]
99         if world_db["ThingTypes"][type]["TT_TOOL"] == "food":
100             t["T_CARRIES"].remove(id)
101             del world_db["Things"][id]
102             t["T_SATIATION"] += world_db["ThingTypes"][type]["TT_TOOLPOWER"]
103             if t == world_db["Things"][0]:
104                 log("You CONSUME this thing.")
105         else:
106             from server.config.actions import actor_use_attempts_hook
107             actor_use_attempts_hook(t, type)