home · contact · privacy
Add beginnings of pluginized variant of PtIG.
[plomrogue] / plugins / server / PleaseTheIslandGod.py
1 from server.io import log
2 from server.config.world_data import world_db
3
4 def ai(t):
5     from server.ai import get_dir_to_target, get_inventory_slot_to_consume, \
6         standing_on_food
7     t["T_COMMAND"] = [id for id in world_db["ThingActions"]
8                       if world_db["ThingActions"][id]["TA_NAME"] == "wait"][0]
9     eating = len([id for id in world_db["ThingActions"]
10                   if world_db["ThingActions"][id]["TA_NAME"] == "use"]) > 0
11     picking = len([id for id in world_db["ThingActions"]
12                    if world_db["ThingActions"][id]["TA_NAME"] == "pickup"]) > 0
13     if eating and picking:
14         if get_dir_to_target(t, "f"):
15             return
16         sel = get_inventory_slot_to_consume(t)
17         if -1 != sel:
18             t["T_COMMAND"] = [id for id in world_db["ThingActions"]
19                               if world_db["ThingActions"][id]["TA_NAME"]
20                                  == "use"][0]
21             t["T_ARGUMENT"] = sel
22         elif standing_on_food(t) and (len(t["T_CARRIES"]) <
23                 world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]):
24                 t["T_COMMAND"] = [id for id in world_db["ThingActions"]
25                                   if world_db["ThingActions"][id]["TA_NAME"]
26                                   == "pickup"][0]
27         else:
28             going_to_known_food_spot = get_dir_to_target(t, "c")
29             if not going_to_known_food_spot:
30                 aiming_for_walking_food = get_dir_to_target(t, "a")
31                 if not aiming_for_walking_food:
32                     get_dir_to_target(t, "s")
33
34 def actor_pickup(t):
35     from server.ai import eat_vs_hunger_threshold
36     used_slots = len(t["T_CARRIES"])
37     if used_slots < world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]:
38         ids = [id for id in world_db["Things"] if world_db["Things"][id] != t
39                if not world_db["Things"][id]["carried"]
40                if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
41                if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]
42         if len(ids):
43             lowest_tid = -1
44             eat_cost = eat_vs_hunger_threshold(t["T_TYPE"])
45             for iid in ids:
46                 tid = world_db["Things"][iid]["T_TYPE"] 
47                 if lowest_tid == -1 or tid < lowest_tid:
48                     if (t != world_db["Things"][0] and
49                         (world_db["ThingTypes"][tid]["TT_TOOL"] != "food"
50                          or (world_db["ThingTypes"][tid]["TT_TOOLPOWER"]
51                              <= eat_cost))):
52                         continue
53                     id = iid
54                     lowest_tid = tid
55             world_db["Things"][id]["carried"] = True
56             ty = world_db["Things"][id]["T_TYPE"]
57             if (t != world_db["Things"][0]
58                 and world_db["Things"][id]["T_PLAYERDROP"]
59                 and world_db["ThingTypes"][ty]["TT_TOOL"] == "food"):
60                 score = int(world_db["ThingTypes"][ty]["TT_TOOLPOWER"] / 32)
61                 world_db["GOD_FAVOR"] += score
62                 world_db["Things"][id]["T_PLAYERDROP"] = 0
63             t["T_CARRIES"].append(id)
64             if t == world_db["Things"][0]:
65                 log("You PICK UP an object.")
66     elif t == world_db["Things"][0]:
67         log("Can't pick up object: No storage room to carry more.")
68
69
70 def actor_drop(t):
71     """Make t rop Thing from inventory to ground indexed by T_ARGUMENT."""
72     if len(t["T_CARRIES"]):
73         id = t["T_CARRIES"][t["T_ARGUMENT"]]
74         t["T_CARRIES"].remove(id)
75         world_db["Things"][id]["carried"] = False
76         if t == world_db["Things"][0]:
77             log("You DROP an object.")
78             world_db["Things"][id]["T_PLAYERDROP"] = 1
79
80
81 def actor_move(t):
82     def decrement_lifepoints(t):
83         t["T_LIFEPOINTS"] -= 1
84         _id = [_id for _id in world_db["Things"] if world_db["Things"][_id] == t][0]
85         if 0 == t["T_LIFEPOINTS"]:
86             sadness = world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]
87             for id in t["T_CARRIES"]:
88                 t["T_CARRIES"].remove(id)
89                 world_db["Things"][id]["T_POSY"] = t["T_POSY"]
90                 world_db["Things"][id]["T_POSX"] = t["T_POSX"]
91                 world_db["Things"][id]["carried"] = False
92             t["T_TYPE"] = world_db["ThingTypes"][t["T_TYPE"]]["TT_CORPSE_ID"]
93             if world_db["Things"][0] == t:
94                 t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
95                 log("You die.")
96                 log("See README on how to start over.")
97             else:
98                 t["fovmap"] = False
99                 t["T_MEMMAP"] = False
100                 t["T_MEMDEPTHMAP"] = False
101                 t["T_MEMTHING"] = []
102             log("BAR " + str(t["T_LIFEPOINTS"]))
103             return sadness 
104         return 0 
105     from server.world import build_fov_map
106     from server.utils import mv_yx_in_dir_legal
107     from server.config.world_data import directions_db
108     passable = False
109     move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
110                                      t["T_POSY"], t["T_POSX"])
111     if 1 == move_result[0]:
112         pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
113         hitted = [id for id in world_db["Things"]
114                   if world_db["Things"][id] != t
115                   if world_db["Things"][id]["T_LIFEPOINTS"]
116                   if world_db["Things"][id]["T_POSY"] == move_result[1]
117                   if world_db["Things"][id]["T_POSX"] == move_result[2]]
118         if len(hitted):
119             hit_id = hitted[0]
120             if t == world_db["Things"][0]:
121                 hitted_type = world_db["Things"][hit_id]["T_TYPE"]
122                 hitted_name = world_db["ThingTypes"][hitted_type]["TT_NAME"]
123                 log("You WOUND " + hitted_name + ".")
124                 world_db["GOD_FAVOR"] -= 1
125             elif 0 == hit_id:
126                 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
127                 log(hitter_name +" WOUNDS you.")
128             test = decrement_lifepoints(world_db["Things"][hit_id])
129             if test and t == world_db["Things"][0]:
130                 world_db["GOD_FAVOR"] -= test 
131             return
132         passable = "." == chr(world_db["MAP"][pos]) or \
133                    ":" == chr(world_db["MAP"][pos])
134     dir = [dir for dir in directions_db
135            if directions_db[dir] == chr(t["T_ARGUMENT"])][0]
136     if passable:
137         t["T_POSY"] = move_result[1]
138         t["T_POSX"] = move_result[2]
139         for id in t["T_CARRIES"]:
140             world_db["Things"][id]["T_POSY"] = move_result[1]
141             world_db["Things"][id]["T_POSX"] = move_result[2]
142         build_fov_map(t)
143         if t == world_db["Things"][0]:
144             log("You MOVE " + dir + ".")
145
146 def command_ttid(id_string):
147     id = id_setter(id_string, "ThingTypes", command_ttid)
148     if None != id:
149         world_db["ThingTypes"][id] = {
150             "TT_NAME": "(none)",
151             "TT_TOOLPOWER": 0,
152             "TT_LIFEPOINTS": 0,
153             "TT_PROLIFERATE": 0,
154             "TT_START_NUMBER": 0,
155             "TT_STORAGE": 0,
156             "TT_SYMBOL": "?",
157             "TT_CORPSE_ID": id,
158             "TT_TOOL": ""
159         }
160
161 from server.io import strong_write
162 strong_write(io_db["file_out"], "PLUGIN PleaseTheIslandGod\n")
163
164 if not "GOD_FAVOR"  in world_db:
165     world_db["GOD_FAVOR"] = 0
166 from server.config.io import io_db
167 io_db["worldstate_write_order"] += [["GOD_FAVOR", "world_int"]]
168
169 import server.config.world_data
170 server.config.world_data.symbols_passable += ":"
171
172 from server.config.world_data import thing_defaults
173 thing_defaults["T_PLAYERDROP"] = 0
174
175 import server.config.actions
176 server.config.actions.action_db["actor_move"] = actor_move
177 server.config.actions.action_db["actor_pickup"] = actor_pickup
178 server.config.actions.action_db["actor_drop"] = actor_drop
179 server.config.actions.ai_func = ai
180
181 from server.config.commands import commands_db
182 commands_db["TT_ID"] = (1, False, command_ttid)
183 commands_db["GOD_FAVOR"] = (1, False, setter(None, "GOD_FAVOR", -32768, 32767))
184 commands_db["TT_STORAGE"] = (1, False, setter("ThingType", "TT_STORAGE", 0, 255))
185 commands_db["T_PLAYERDROP"] = (1, False, setter("Thing", "T_PLAYERDROP", 0, 1))