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.
6 from server.config.world_data import world_db
7 from server.config.io import io_db
8 from server.io import log, strong_write
9 from server.utils import integer_test, id_setter
10 from server.world import build_fov_map, update_map_memory, set_world_inactive,\
14 def command_plugin(str_plugin):
15 """Run code in plugins/[str_plugin]."""
17 if (str_plugin.replace("_", "").isalnum()
18 and os.access("plugins/" + str_plugin, os.F_OK)):
19 exec(open("plugins/" + str_plugin).read())
21 print("Bad plugin name:", str_plugin)
25 """Send PONG line to server output file."""
26 strong_write(io_db["file_out"], "PONG\n")
30 """Abort server process."""
31 from server.io import save_world, atomic_write
32 from server.utils import opts
33 if None == opts.replay:
34 if world_db["WORLD_ACTIVE"]:
36 atomic_write(io_db["path_record"], io_db["record_chunk"],
38 raise SystemExit("received QUIT command")
41 def command_thingshere(str_y, str_x):
42 """Write to out file list of Things known to player at coordinate y, x."""
43 if world_db["WORLD_ACTIVE"]:
44 y = integer_test(str_y, 0, 255)
45 x = integer_test(str_x, 0, 255)
46 length = world_db["MAP_LENGTH"]
47 if None != y and None != x and y < length and x < length:
48 pos = (y * world_db["MAP_LENGTH"]) + x
49 strong_write(io_db["file_out"], "THINGS_HERE START\n")
50 if "v" == chr(world_db["Things"][0]["fovmap"][pos]):
51 for id in [id for tid in sorted(list(world_db["ThingTypes"]))
52 for id in world_db["Things"]
53 if not world_db["Things"][id]["carried"]
54 if world_db["Things"][id]["T_TYPE"] == tid
55 if y == world_db["Things"][id]["T_POSY"]
56 if x == world_db["Things"][id]["T_POSX"]]:
57 type = world_db["Things"][id]["T_TYPE"]
58 name = world_db["ThingTypes"][type]["TT_NAME"]
59 strong_write(io_db["file_out"], name + "\n")
61 for mt in [mt for tid in sorted(list(world_db["ThingTypes"]))
62 for mt in world_db["Things"][0]["T_MEMTHING"]
63 if mt[0] == tid if y == mt[1] if x == mt[2]]:
64 name = world_db["ThingTypes"][mt[0]]["TT_NAME"]
65 strong_write(io_db["file_out"], name + "\n")
66 strong_write(io_db["file_out"], "THINGS_HERE END\n")
68 print("Ignoring: Invalid map coordinates.")
70 print("Ignoring: Command only works on existing worlds.")
73 def command_seedrandomness(seed_string):
74 """Set rand seed to int(seed_string)."""
75 from server.utils import rand
76 val = integer_test(seed_string, 0, 4294967295)
81 def command_makeworld(seed_string):
82 """Call make_world()."""
83 val = integer_test(seed_string, 0, 4294967295)
85 from server.world import make_world
89 def command_maplength(maplength_string):
90 """Redefine map length. Invalidate map, therefore lose all things on it."""
91 val = integer_test(maplength_string, 1, 256)
93 from server.utils import libpr
94 world_db["MAP_LENGTH"] = val
95 world_db["MAP"] = False
97 world_db["Things"] = {}
98 libpr.set_maplength(val)
101 def command_worldactive(worldactive_string):
102 """Toggle world_db["WORLD_ACTIVE"] if possible.
104 An active world can always be set inactive. An inactive world can only be
105 set active with a "wait" ThingAction, and a player Thing (of ID 0), and a
106 map. On activation, rebuild all Things' FOVs, and the player's map memory.
108 val = integer_test(worldactive_string, 0, 1)
110 if 0 != world_db["WORLD_ACTIVE"]:
114 print("World already active.")
115 elif 0 == world_db["WORLD_ACTIVE"]:
117 for ThingAction in world_db["ThingActions"]:
118 if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
121 player_exists = False
122 for Thing in world_db["Things"]:
126 if wait_exists and player_exists and world_db["MAP"]:
127 for id in world_db["Things"]:
128 if world_db["Things"][id]["T_LIFEPOINTS"]:
129 build_fov_map(world_db["Things"][id])
131 update_map_memory(world_db["Things"][id], False)
132 if not world_db["Things"][0]["T_LIFEPOINTS"]:
133 empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
134 world_db["Things"][0]["fovmap"] = empty_fovmap
135 world_db["WORLD_ACTIVE"] = 1
137 print("Ignoring: Not all conditions for world activation met.")
140 def command_tid(id_string):
141 """Set ID of Thing to manipulate. ID unused? Create new one.
143 Default new Thing's type to the first available ThingType, others: zero.
145 id = id_setter(id_string, "Things", command_tid)
147 if world_db["ThingTypes"] == {}:
148 print("Ignoring: No ThingType to settle new Thing in.")
150 type = list(world_db["ThingTypes"].keys())[0]
151 from server.world import new_Thing
152 world_db["Things"][id] = new_Thing(type)
155 def command_ttid(id_string):
156 """Set ID of ThingType to manipulate. ID unused? Create new one.
158 Default new ThingType's TT_SYMBOL to "?", TT_CORPSE_ID to self, TT_TOOL to
161 id = id_setter(id_string, "ThingTypes", command_ttid)
163 world_db["ThingTypes"][id] = {
168 "TT_START_NUMBER": 0,
175 def command_taid(id_string):
176 """Set ID of ThingAction to manipulate. ID unused? Create new one.
178 Default new ThingAction's TA_EFFORT to 1, its TA_NAME to "wait".
180 id = id_setter(id_string, "ThingActions", command_taid, True)
182 world_db["ThingActions"][id] = {
188 def test_for_id_maker(object, category):
189 """Return decorator testing for object having "id" attribute."""
192 if hasattr(object, "id"):
195 print("Ignoring: No " + category +
196 " defined to manipulate yet.")
201 test_Thing_id = test_for_id_maker(command_tid, "Thing")
202 test_ThingType_id = test_for_id_maker(command_ttid, "ThingType")
203 test_ThingAction_id = test_for_id_maker(command_taid, "ThingAction")
207 def command_tcommand(str_int):
208 """Set T_COMMAND of selected Thing."""
209 val = integer_test(str_int, 0)
211 if 0 == val or val in world_db["ThingActions"]:
212 world_db["Things"][command_tid.id]["T_COMMAND"] = val
214 print("Ignoring: ThingAction ID belongs to no known ThingAction.")
218 def command_ttype(str_int):
219 """Set T_TYPE of selected Thing."""
220 val = integer_test(str_int, 0)
222 if val in world_db["ThingTypes"]:
223 world_db["Things"][command_tid.id]["T_TYPE"] = val
225 print("Ignoring: ThingType ID belongs to no known ThingType.")
229 def command_tcarries(str_int):
230 """Append int(str_int) to T_CARRIES of selected Thing.
232 The ID int(str_int) must not be of the selected Thing, and must belong to a
233 Thing with unset "carried" flag. Its "carried" flag will be set on owning.
235 val = integer_test(str_int, 0)
237 if val == command_tid.id:
238 print("Ignoring: Thing cannot carry itself.")
239 elif val in world_db["Things"] \
240 and not world_db["Things"][val]["carried"]:
241 world_db["Things"][command_tid.id]["T_CARRIES"].append(val)
242 world_db["Things"][val]["carried"] = True
244 print("Ignoring: Thing not available for carrying.")
245 # Note that the whole carrying structure is different from the C version:
246 # Carried-ness is marked by a "carried" flag, not by Things containing
251 def command_tmemthing(str_t, str_y, str_x):
252 """Add (int(str_t), int(str_y), int(str_x)) to selected Thing's T_MEMTHING.
254 The type must fit to an existing ThingType, and the position into the map.
256 type = integer_test(str_t, 0)
257 posy = integer_test(str_y, 0, 255)
258 posx = integer_test(str_x, 0, 255)
259 if None != type and None != posy and None != posx:
260 if type not in world_db["ThingTypes"] \
261 or posy >= world_db["MAP_LENGTH"] or posx >= world_db["MAP_LENGTH"]:
262 print("Ignoring: Illegal value for thing type or position.")
264 memthing = (type, posy, posx)
265 world_db["Things"][command_tid.id]["T_MEMTHING"].append(memthing)
269 def command_ttname(name):
270 """Set TT_NAME of selected ThingType."""
271 world_db["ThingTypes"][command_ttid.id]["TT_NAME"] = name
275 def command_tttool(name):
276 """Set TT_TOOL of selected ThingType."""
277 world_db["ThingTypes"][command_ttid.id]["TT_TOOL"] = name
281 def command_ttsymbol(char):
282 """Set TT_SYMBOL of selected ThingType. """
284 world_db["ThingTypes"][command_ttid.id]["TT_SYMBOL"] = char
286 print("Ignoring: Argument must be single character.")
290 def command_ttcorpseid(str_int):
291 """Set TT_CORPSE_ID of selected ThingType."""
292 val = integer_test(str_int, 0)
294 if val in world_db["ThingTypes"]:
295 world_db["ThingTypes"][command_ttid.id]["TT_CORPSE_ID"] = val
297 print("Ignoring: Corpse ID belongs to no known ThignType.")
301 def command_taname(name):
302 """Set TA_NAME of selected ThingAction.
304 The name must match a valid thing action function. If after the name
305 setting no ThingAction with name "wait" remains, call set_world_inactive().
307 if name == "wait" or name == "move" or name == "use" or name == "drop" \
309 world_db["ThingActions"][command_taid.id]["TA_NAME"] = name
310 if 1 == world_db["WORLD_ACTIVE"]:
312 for id in world_db["ThingActions"]:
313 if "wait" == world_db["ThingActions"][id]["TA_NAME"]:
319 print("Ignoring: Invalid action name.")
320 # In contrast to the original,naming won't map a function to a ThingAction.
323 def setter(category, key, min, max=None):
324 """Build setter for world_db([category + "s"][id])[key] to >=min/<=max."""
327 val = integer_test(val_string, min, max)
331 if category == "Thing":
332 id_store = command_tid
333 decorator = test_Thing_id
334 elif category == "ThingType":
335 id_store = command_ttid
336 decorator = test_ThingType_id
337 elif category == "ThingAction":
338 id_store = command_taid
339 decorator = test_ThingAction_id
343 val = integer_test(val_string, min, max)
345 world_db[category + "s"][id_store.id][key] = val
349 def setter_map(maptype):
350 """Set (world or Thing's) map of maptype's int(str_int)-th line to mapline.
352 If no map of maptype exists yet, initialize it with ' ' bytes first.
355 def valid_map_line(str_int, mapline):
356 val = integer_test(str_int, 0, 255)
358 if val >= world_db["MAP_LENGTH"]:
359 print("Illegal value for map line number.")
360 elif len(mapline) != world_db["MAP_LENGTH"]:
361 print("Map line length is unequal map width.")
366 def nonThingMap_helper(str_int, mapline):
367 val = valid_map_line(str_int, mapline)
369 length = world_db["MAP_LENGTH"]
370 if not world_db["MAP"]:
371 map = bytearray(b' ' * (length ** 2))
373 map = world_db["MAP"]
374 map[val * length:(val * length) + length] = mapline.encode()
375 if not world_db["MAP"]:
376 world_db["MAP"] = map
379 def ThingMap_helper(str_int, mapline):
380 val = valid_map_line(str_int, mapline)
382 length = world_db["MAP_LENGTH"]
383 if not world_db["Things"][command_tid.id][maptype]:
384 map = bytearray(b' ' * (length ** 2))
386 map = world_db["Things"][command_tid.id][maptype]
387 map[val * length:(val * length) + length] = mapline.encode()
388 if not world_db["Things"][command_tid.id][maptype]:
389 world_db["Things"][command_tid.id][maptype] = map
391 return nonThingMap_helper if maptype == "MAP" else ThingMap_helper
395 def setter_tpos(axis):
396 """Generate setter for T_POSX or T_POSY of selected Thing.
398 If world is active, rebuilds animate things' fovmap, player's memory map.
402 val = integer_test(str_int, 0, 255)
404 if val < world_db["MAP_LENGTH"]:
405 world_db["Things"][command_tid.id]["T_POS" + axis] = val
406 if world_db["WORLD_ACTIVE"] \
407 and world_db["Things"][command_tid.id]["T_LIFEPOINTS"]:
408 build_fov_map(world_db["Things"][command_tid.id])
409 if 0 == command_tid.id:
410 update_map_memory(world_db["Things"][command_tid.id])
412 print("Ignoring: Position is outside of map.")
416 def set_command(action):
417 """Set player's T_COMMAND, then call turn_over()."""
418 id = [x for x in world_db["ThingActions"]
419 if world_db["ThingActions"][x]["TA_NAME"] == action][0]
420 world_db["Things"][0]["T_COMMAND"] = id
425 """Try "wait" as player's T_COMMAND."""
429 def action_exists(action):
430 matching_actions = [x for x in world_db["ThingActions"]
431 if world_db["ThingActions"][x]["TA_NAME"] == action]
432 if len(matching_actions) >= 1:
434 print("No appropriate ThingAction defined.")
439 """Try "pickup" as player's T_COMMAND"."""
440 if action_exists("pickup"):
441 t = world_db["Things"][0]
442 ids = [id for id in world_db["Things"] if id
443 if not world_db["Things"][id]["carried"]
444 if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
445 if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]
447 log("NOTHING to pick up.")
449 set_command("pickup")
452 def play_drop(str_arg):
453 """Try "drop" as player's T_COMMAND, int(str_arg) as T_ARGUMENT / slot."""
454 if action_exists("drop"):
455 t = world_db["Things"][0]
456 if 0 == len(t["T_CARRIES"]):
457 log("You have NOTHING to drop in your inventory.")
459 val = integer_test(str_arg, 0, 255)
460 if None != val and val < len(t["T_CARRIES"]):
461 world_db["Things"][0]["T_ARGUMENT"] = val
464 print("Illegal inventory index.")
467 def play_use(str_arg):
468 """Try "use" as player's T_COMMAND, int(str_arg) as T_ARGUMENT / slot."""
469 if action_exists("use"):
470 t = world_db["Things"][0]
471 if 0 == len(t["T_CARRIES"]):
472 log("You have NOTHING to use in your inventory.")
474 val = integer_test(str_arg, 0, 255)
475 if None != val and val < len(t["T_CARRIES"]):
476 id = t["T_CARRIES"][val]
477 type = world_db["Things"][id]["T_TYPE"]
478 if not world_db["ThingTypes"][type]["TT_TOOL"] == "food":
479 log("You CAN'T consume this thing.")
481 world_db["Things"][0]["T_ARGUMENT"] = val
484 print("Illegal inventory index.")
487 def play_move(str_arg):
488 """Try "move" as player's T_COMMAND, str_arg as T_ARGUMENT / direction."""
489 if action_exists("move"):
490 from server.config.world_data import directions_db
491 t = world_db["Things"][0]
492 if not str_arg in directions_db:
493 print("Illegal move direction string.")
495 dir = ord(directions_db[str_arg])
496 from server.utils import mv_yx_in_dir_legal
497 move_result = mv_yx_in_dir_legal(chr(dir), t["T_POSY"], t["T_POSX"])
498 if 1 == move_result[0]:
499 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
500 if ord(".") == world_db["MAP"][pos]:
501 world_db["Things"][0]["T_ARGUMENT"] = dir
504 log("You CAN'T move there.")
508 """Call ai() on player Thing, then turn_over()."""
509 from server.ai import ai
510 ai(world_db["Things"][0])