home · contact · privacy
1e676a5618f5b2fcfb1a1624d6ae7aa8d93f76de
[plomrogue] / plugins / server / TheCrawlingEater.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
8
9 def make_map():
10     from server.make_map import new_pos, is_neighbor
11     from server.utils import rand
12     world_db["MAP"] = bytearray(b'X' * (world_db["MAP_LENGTH"] ** 2))
13     length = world_db["MAP_LENGTH"]
14     add_half_width = (not (length % 2)) * int(length / 2)
15     world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord("#")
16     while (1):
17         y, x, pos = new_pos()
18         if "X" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "#"):
19             if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
20                 break
21             world_db["MAP"][pos] = ord("#")
22     n_trees = int((length ** 2) / 16)
23     i_trees = 0
24     while (i_trees <= n_trees):
25         single_allowed = rand.next() % 32
26         y, x, pos = new_pos()
27         if "#" == chr(world_db["MAP"][pos]) \
28                 and ((not single_allowed) or is_neighbor((y, x), ".")):
29             world_db["MAP"][pos] = ord(".")
30             i_trees += 1
31
32
33 def actor_move(t):
34     from server.build_fov_map import build_fov_map
35     from server.utils import mv_yx_in_dir_legal, rand
36     from server.config.world_data import directions_db, symbols_passable
37     passable = False
38     move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
39                                      t["T_POSY"], t["T_POSX"])
40     if 1 == move_result[0]:
41         pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
42         hitted = [tid for tid in world_db["Things"]
43                   if world_db["Things"][tid] != t
44                   if world_db["Things"][tid]["T_LIFEPOINTS"]
45                   if world_db["Things"][tid]["T_POSY"] == move_result[1]
46                   if world_db["Things"][tid]["T_POSX"] == move_result[2]]
47         if len(hitted):
48             hit_id = hitted[0]
49             hitted_tid = world_db["Things"][hit_id]["T_TYPE"]
50             if t == world_db["Things"][0]:
51                 hitted_name = world_db["ThingTypes"][hitted_tid]["TT_NAME"]
52                 log("You BUMP into " + hitted_name + ".")
53             elif 0 == hit_id:
54                 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
55                 log(hitter_name +" BUMPS into you.")
56             return
57         passable = chr(world_db["MAP"][pos]) in symbols_passable
58     direction = [direction for direction in directions_db
59                  if directions_db[direction] == chr(t["T_ARGUMENT"])][0]
60     if passable:
61         t["T_POSY"] = move_result[1]
62         t["T_POSX"] = move_result[2]
63         t["pos"] = move_result[1] * world_db["MAP_LENGTH"] + move_result[2]
64         build_fov_map(t)
65     else:
66         if ord("#") == world_db["MAP"][pos] and 0 == int(rand.next() % 5):
67             world_db["MAP"][pos] = ord(".")
68             t["STOMACH"] += 1
69
70
71 def turn_over():
72     from server.ai import ai
73     from server.config.actions import action_db
74     from server.config.misc import calc_effort
75     from server.update_map_memory import update_map_memory
76     from server.io import try_worldstate_update
77     from server.config.io import io_db
78     while world_db["Things"][0]["T_LIFEPOINTS"]:
79         for tid in [tid for tid in world_db["Things"]]:
80             if not tid in world_db["Things"]:
81                 continue
82             Thing = world_db["Things"][tid]
83             if Thing["T_LIFEPOINTS"]:
84                 if not Thing["T_COMMAND"]:
85                     update_map_memory(Thing)
86                     if 0 == tid:
87                         return
88                     ai(Thing)
89                 if Thing["T_LIFEPOINTS"]:
90                     Thing["T_PROGRESS"] += 1
91                     taid = [a for a in world_db["ThingActions"]
92                               if a == Thing["T_COMMAND"]][0]
93                     ThingAction = world_db["ThingActions"][taid]
94                     effort = calc_effort(ThingAction, Thing)
95                     if Thing["T_PROGRESS"] == effort:
96                         action = action_db["actor_" + ThingAction["TA_NAME"]]
97                         action(Thing)
98                         Thing["T_COMMAND"] = 0
99                         Thing["T_PROGRESS"] = 0
100         world_db["TURN"] += 1
101         io_db["worldstate_updateable"] = True
102         try_worldstate_update()
103 world_db["turn_over"] = turn_over
104
105
106 def play_move(str_arg):
107     """Try "move" as player's T_COMMAND, str_arg as T_ARGUMENT / direction."""
108     if action_exists("move") and world_db["WORLD_ACTIVE"]:
109         from server.config.world_data import directions_db, symbols_passable
110         t = world_db["Things"][0]
111         if not str_arg in directions_db:
112             print("Illegal move direction string.")
113             return
114         d = ord(directions_db[str_arg])
115         from server.utils import mv_yx_in_dir_legal
116         move_result = mv_yx_in_dir_legal(chr(d), t["T_POSY"], t["T_POSX"])
117         if 1 == move_result[0]:
118             pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
119             if ord("#") == world_db["MAP"][pos]:
120                 log("You EAT.")
121                 world_db["Things"][0]["T_ARGUMENT"] = d
122                 world_db["set_command"]("move")
123                 return
124             if chr(world_db["MAP"][pos]) in symbols_passable:
125                 world_db["Things"][0]["T_ARGUMENT"] = d
126                 world_db["set_command"]("move")
127                 return
128         log("You CAN'T eat your way through there.")
129
130
131 def command_ai():
132     """Call ai() on player Thing, then turn_over()."""
133     from server.ai import ai
134     if world_db["WORLD_ACTIVE"]:
135         ai(world_db["Things"][0])
136         world_db["turn_over"]()
137
138
139 def set_command(action):
140     """Set player's T_COMMAND, then call turn_over()."""
141     tid = [x for x in world_db["ThingActions"]
142            if world_db["ThingActions"][x]["TA_NAME"] == action][0]
143     world_db["Things"][0]["T_COMMAND"] = tid
144     world_db["turn_over"]()
145 world_db["set_command"] = set_command
146
147
148 def play_wait():
149     """Try "wait" as player's T_COMMAND."""
150     if world_db["WORLD_ACTIVE"]:
151         world_db["set_command"]("wait")
152
153
154 #import server.config.actions
155 #server.config.actions.actor_move_attempts_hook = actor_move_attempts_hook
156 import server.config.world_data
157 server.config.world_data.symbols_hide += "#"
158 server.config.world_data.thing_defaults["STOMACH"] = 0
159 import server.config.make_world_helpers
160 server.config.make_world_helpers.make_map = make_map
161 from server.config.commands import commands_db
162 commands_db["ai"] = (0, False, command_ai)
163 commands_db["move"] = (1, False, play_move)
164 commands_db["wait"] = (0, False, play_wait)
165 commands_db["drop"] = (1, False, lambda x: None)
166 commands_db["use"] = (1, False, lambda x: None)
167 commands_db["pickup"] = (0, False, lambda: None)
168 from server.actions import actor_wait
169 import server.config.actions
170 server.config.actions.action_db = {
171     "actor_wait": actor_wait,
172     "actor_move": actor_move
173 }
174
175 strong_write(io_db["file_out"], "PLUGIN TheCrawlingEater\n")