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
9 def command_help(str_int):
10 val = integer_test(str_int, 0, 4)
16 if not (world_db["WORLD_ACTIVE"]
17 and world_db["Things"][0]["T_LIFEPOINTS"] > 0):
19 world_db["ai"](world_db["Things"][0])
20 world_db["turn_over"]()
24 if not (action_exists("drink") and world_db["WORLD_ACTIVE"]
25 and world_db["Things"][0]["T_LIFEPOINTS"] > 0):
27 pos = world_db["Things"][0]["pos"]
28 if not (chr(world_db["MAP"][pos]) in "0-+"
29 and world_db["wetmap"][pos] > ord("0")):
30 log("NOTHING to drink here.")
32 elif world_db["Things"][0]["T_KIDNEY"] >= 32:
33 log("You're too FULL to drink more.")
35 world_db["set_command"]("drink")
40 if chr(world_db["MAP"][pos]) in "0-+" and \
41 world_db["wetmap"][pos] > ord("0") and t["T_KIDNEY"] < 32:
42 if world_db["Things"][0] == t:
45 world_db["wetmap"][pos] -= 1
49 if not (action_exists("pee") and world_db["WORLD_ACTIVE"]
50 and world_db["Things"][0]["T_LIFEPOINTS"] > 0):
52 if world_db["Things"][0]["T_BLADDER"] < 1:
53 log("Nothing to drop from empty bladder.")
55 world_db["set_command"]("pee")
59 if t["T_BLADDER"] < 1:
61 if t == world_db["Things"][0]:
62 log("You LOSE fluid.")
63 if not world_db["test_air"](t):
66 world_db["wetmap"][t["pos"]] += 1
70 if not (action_exists("drop") and world_db["WORLD_ACTIVE"]
71 and world_db["Things"][0]["T_LIFEPOINTS"] > 0):
73 if world_db["Things"][0]["T_BOWEL"] < 1:
74 log("Nothing to drop from empty bowel.")
76 world_db["set_command"]("drop")
82 if t == world_db["Things"][0]:
83 log("You DROP waste.")
84 if not world_db["test_air"](t):
86 if world_db["MAP"][t["pos"]] == ord("+"):
87 world_db["MAP"][t["pos"]] = ord("-")
88 elif world_db["MAP"][t["pos"]] == ord("-"):
89 world_db["MAP"][t["pos"]] = ord("0")
91 world_db["MAP"][t["pos"]] += 1
95 def play_move(str_arg):
96 if not (action_exists("move") and world_db["WORLD_ACTIVE"]
97 and world_db["Things"][0]["T_LIFEPOINTS"] > 0):
99 from server.config.world_data import directions_db, symbols_passable
100 t = world_db["Things"][0]
101 if not str_arg in directions_db:
102 print("Illegal move direction string.")
104 d = ord(directions_db[str_arg])
105 from server.utils import mv_yx_in_dir_legal
106 move_result = mv_yx_in_dir_legal(chr(d), t["T_POSY"], t["T_POSX"])
107 if 1 == move_result[0]:
108 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
109 hitted = [tid for tid in world_db["Things"]
110 if world_db["Things"][tid]["T_POSY"] == move_result[1]
111 if world_db["Things"][tid]["T_POSX"] == move_result[2]]
113 if t["T_STOMACH"] >= 32 and t["T_KIDNEY"] >= 32:
114 if t == world_db["Things"][0]:
115 log("You're too FULL to suck from another creature.")
117 world_db["Things"][0]["T_ARGUMENT"] = d
118 world_db["set_command"]("eat")
121 if world_db["GRACE"] >= 8:
123 if chr(world_db["MAP"][pos]) in legal_targets:
124 if t["T_STOMACH"] >= 32:
125 if t == world_db["Things"][0]:
126 log("You're too FULL to eat.")
128 world_db["Things"][0]["T_ARGUMENT"] = d
129 world_db["set_command"]("eat")
131 if chr(world_db["MAP"][pos]) in symbols_passable:
132 world_db["Things"][0]["T_ARGUMENT"] = d
133 world_db["set_command"]("move")
135 log("You CAN'T eat your way through there.")
139 from server.utils import mv_yx_in_dir_legal, rand
140 from server.config.world_data import symbols_passable
142 move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
143 t["T_POSY"], t["T_POSX"])
144 if 1 == move_result[0]:
145 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
146 hitted = [tid for tid in world_db["Things"]
147 if world_db["Things"][tid]["T_POSY"] == move_result[1]
148 if world_db["Things"][tid]["T_POSX"] == move_result[2]]
151 hitted_tid = world_db["Things"][hit_id]["T_TYPE"]
152 if t == world_db["Things"][0]:
153 hitted_name = world_db["ThingTypes"][hitted_tid]["TT_NAME"]
154 log("You SUCK from " + hitted_name + ".")
156 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
157 log(hitter_name +" SUCKS from you.")
158 hitted = world_db["Things"][hit_id]
159 if t["T_STOMACH"] < 32:
160 t["T_STOMACH"] = t["T_STOMACH"] + 1
161 hitted["T_STOMACH"] -= 1
162 if t["T_KIDNEY"] < 32:
163 t["T_KIDNEY"] = t["T_KIDNEY"] + 1
164 hitted["T_KIDNEY"] -= 1
166 passable = chr(world_db["MAP"][pos]) in symbols_passable
167 if passable and t == world_db["Things"][0]:
168 log("You try to EAT, but fail.")
170 height = world_db["MAP"][pos] - ord("0")
171 if t["T_STOMACH"] >= 32:
173 if height == 5 and not \
174 (t == world_db["Things"][0] and world_db["GRACE"] >= 8):
177 if t == world_db["Things"][0]:
179 eaten = (height == 3 and 0 == int(rand.next() % 2)) or \
180 (height == 4 and 0 == int(rand.next() % 5)) or \
181 (height == 5 and 0 == int(rand.next() % 10))
183 world_db["MAP"][pos] = ord("0")
184 if t["T_STOMACH"] > 32:
189 from server.build_fov_map import build_fov_map
190 from server.utils import mv_yx_in_dir_legal, rand
191 from server.config.world_data import symbols_passable
193 move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
194 t["T_POSY"], t["T_POSX"])
195 if 1 == move_result[0]:
196 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
197 hitted = [tid for tid in world_db["Things"]
198 if world_db["Things"][tid]["T_POSY"] == move_result[1]
199 if world_db["Things"][tid]["T_POSX"] == move_result[2]]
202 hitted_tid = world_db["Things"][hit_id]["T_TYPE"]
203 if t == world_db["Things"][0]:
204 hitted_name = world_db["ThingTypes"][hitted_tid]["TT_NAME"]
205 log("You BUMP into " + hitted_name + ".")
207 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
208 log(hitter_name +" BUMPS into you.")
210 passable = chr(world_db["MAP"][pos]) in symbols_passable
212 t["T_POSY"] = move_result[1]
213 t["T_POSX"] = move_result[2]
214 t["pos"] = move_result[1] * world_db["MAP_LENGTH"] + move_result[2]
215 world_db["soundmap"][t["pos"]] = ord("9")
216 if t == world_db["Things"][0] and world_db["MAP"][t["pos"]] == ord("$"):
217 world_db["MAP"][t["pos"]] = ord("0")
218 if world_db["GRACE"] < 8:
219 log("You can now eat ALL walls.")
220 world_db["GRACE"] += 8
221 elif t == world_db["Things"][0]:
222 log("You try to MOVE there, but fail.")
226 if world_db["MAP"][t["pos"]] == ord("&"):
227 world_db["die"](t, "YOU WIN, CONGRATULATIONS.")
229 if chr(world_db["MAP"][t["pos"]]) in "*&":
230 world_db["die"](t, "You FALL in a hole, and die.")
233 world_db["test_hole"] = test_hole
237 if world_db["terrain_fullness"](t["pos"]) > 5:
238 world_db["die"](t, "You SUFFOCATE")
241 world_db["test_air"] = test_air
245 t["T_LIFEPOINTS"] = 0
246 if t == world_db["Things"][0]:
247 t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
248 t["T_MEMMAP"][t["pos"]] = ord("@")
251 if world_db["MAP"][t["pos"]] != ord("$"):
252 world_db["MAP"][t["pos"]] = ord("5")
253 world_db["HUMILITY"] = t["T_KIDNEY"] + t["T_BLADDER"] + \
254 (world_db["wetmap"][t["pos"]] - ord("0"))
255 world_db["wetmap"][t["pos"]] = 0
256 tid = next(tid for tid in world_db["Things"]
257 if world_db["Things"][tid] == t)
258 del world_db["Things"][tid]
259 world_db["die"] = die
263 from server.make_map import new_pos, is_neighbor
264 from server.utils import rand
265 world_db["MAP"] = bytearray(b'5' * (world_db["MAP_LENGTH"] ** 2))
266 length = world_db["MAP_LENGTH"]
267 add_half_width = (not (length % 2)) * int(length / 2)
268 world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord("4")
270 y, x, pos = new_pos()
271 if "5" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "4"):
272 if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
274 world_db["MAP"][pos] = ord("4")
275 n_ground = int((length ** 2) / 16)
277 while (i_ground <= n_ground):
278 single_allowed = rand.next() % 32
279 y, x, pos = new_pos()
280 if "4" == chr(world_db["MAP"][pos]) \
281 and ((not single_allowed) or is_neighbor((y, x), "0")):
282 world_db["MAP"][pos] = ord("0")
284 n_water = int((length ** 2) / 32)
286 while (i_water <= n_water):
287 y, x, pos = new_pos()
288 if ord("0") == world_db["MAP"][pos] and \
289 ord("0") == world_db["wetmap"][pos]:
290 world_db["wetmap"][pos] = ord("3")
294 while (i_altars < n_altars):
295 y, x, pos = new_pos()
296 if ord("0") == world_db["MAP"][pos]:
297 world_db["MAP"][pos] = ord("$")
301 def calc_effort(ta, t):
302 from server.utils import mv_yx_in_dir_legal
303 if ta["TA_NAME"] == "move":
304 move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
305 t["T_POSY"], t["T_POSX"])
306 if 1 == move_result[0]:
307 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
308 narrowness = world_db["MAP"][pos] - ord("0")
309 return 2 ** narrowness
311 world_db["calc_effort"] = calc_effort
315 from server.ai import ai
316 from server.config.actions import action_db
317 from server.update_map_memory import update_map_memory
318 from server.io import try_worldstate_update
319 from server.config.io import io_db
320 from server.utils import rand
321 from server.build_fov_map import build_fov_map
322 while world_db["Things"][0]["T_LIFEPOINTS"]:
323 for tid in [tid for tid in world_db["Things"]]:
324 if not tid in world_db["Things"]:
326 t = world_db["Things"][tid]
327 if t["T_LIFEPOINTS"]:
328 if not (world_db["test_air"](t) and world_db["test_hole"](t)):
330 if not t["T_COMMAND"]:
336 if t["T_LIFEPOINTS"]:
338 taid = [a for a in world_db["ThingActions"]
339 if a == t["T_COMMAND"]][0]
340 ThingAction = world_db["ThingActions"][taid]
341 effort = world_db["calc_effort"](ThingAction, t)
342 if t["T_PROGRESS"] >= effort:
343 action = action_db["actor_" + ThingAction["TA_NAME"]]
347 if t["T_BOWEL"] > 16:
348 if 0 == (rand.next() % (33 - t["T_BOWEL"])):
349 action_db["actor_drop"](t)
350 if t["T_BLADDER"] > 16:
351 if 0 == (rand.next() % (33 - t["T_BLADDER"])):
352 action_db["actor_pee"](t)
353 if 0 == world_db["TURN"] % 5:
358 if t["T_STOMACH"] <= 0:
359 world_db["die"](t, "You DIE of hunger.")
360 elif t["T_KIDNEY"] <= 0:
361 world_db["die"](t, "You DIE of dehydration.")
362 mapsize = world_db["MAP_LENGTH"] ** 2
363 for pos in range(mapsize):
364 wetness = world_db["wetmap"][pos] - ord("0")
365 height = world_db["MAP"][pos] - ord("0")
366 if world_db["MAP"][pos] == ord("-"):
368 elif world_db["MAP"][pos] == ord("+"):
370 elif world_db["MAP"][pos] == ord("$"):
372 if height == -2 and wetness > 1 \
373 and 0 == rand.next() % ((2 ** 11) / (2 ** wetness)):
374 world_db["MAP"][pos] = ord("*")
375 world_db["HUMIDITY"] += wetness
376 if height == -1 and wetness > 1 \
377 and 0 == rand.next() % ((2 ** 10) / (2 ** wetness)):
378 world_db["MAP"][pos] = ord("+")
379 if height == 0 and wetness > 1 \
380 and 0 == rand.next() % ((2 ** 9) / (2 ** wetness)):
381 world_db["MAP"][pos] = ord("-")
382 if ((wetness > 0 and height > 0) or wetness > 1) \
383 and 0 == rand.next() % 5:
384 world_db["wetmap"][pos] -= 1
385 world_db["HUMIDITY"] += 1
386 if world_db["HUMIDITY"] > 0:
387 if world_db["HUMIDITY"] > 2 and 0 == rand.next() % 2:
388 world_db["NEW_SPAWN"] += 1
389 world_db["HUMIDITY"] -= 1
390 if world_db["NEW_SPAWN"] >= 16:
391 world_db["NEW_SPAWN"] -= 16
392 from server.new_thing import new_Thing
394 y = rand.next() % world_db["MAP_LENGTH"]
395 x = rand.next() % world_db["MAP_LENGTH"]
396 if chr(world_db["MAP"][y * world_db["MAP_LENGTH"] + x]) !=\
398 from server.utils import id_setter
399 tid = id_setter(-1, "Things")
400 world_db["Things"][tid] = new_Thing(
401 world_db["PLAYER_TYPE"], (y, x))
402 pos = y * world_db["MAP_LENGTH"] + x
404 positions_to_wet = []
405 for pos in range(mapsize):
406 if chr(world_db["MAP"][pos]) in "0-+" \
407 and world_db["wetmap"][pos] < ord("5"):
408 positions_to_wet += [pos]
409 while world_db["HUMIDITY"] > 0 and len(positions_to_wet) > 0:
410 select = rand.next() % len(positions_to_wet)
411 pos = positions_to_wet[select]
412 world_db["wetmap"][pos] += 1
413 positions_to_wet.remove(pos)
414 world_db["HUMIDITY"] -= 1
415 for pos in range(mapsize):
416 if world_db["soundmap"][pos] > ord("0"):
417 world_db["soundmap"][pos] -= 1
418 from server.utils import libpr
419 libpr.init_score_map()
420 def set_map_score(pos, score):
421 test = libpr.set_map_score(pos, score)
423 raise RuntimeError("No score map allocated for set_map_score().")
424 [set_map_score(pos, 1) for pos in range(mapsize)
425 if world_db["MAP"][pos] == ord("*")]
426 for pos in range(mapsize):
427 if world_db["MAP"][pos] == ord("*"):
428 if libpr.ready_neighbor_scores(pos):
429 raise RuntimeError("No score map allocated for " +
430 "ready_neighbor_scores.()")
433 for i in range(len(dirs)):
434 score += libpr.get_neighbor_score(i)
435 if score == 5 or score == 6:
436 world_db["MAP"][pos] = ord("&")
437 libpr.free_score_map()
438 world_db["TURN"] += 1
439 io_db["worldstate_updateable"] = True
440 try_worldstate_update()
441 world_db["turn_over"] = turn_over
444 def set_command(action):
445 """Set player's T_COMMAND, then call turn_over()."""
446 tid = [x for x in world_db["ThingActions"]
447 if world_db["ThingActions"][x]["TA_NAME"] == action][0]
448 world_db["Things"][0]["T_COMMAND"] = tid
449 world_db["turn_over"]()
450 world_db["set_command"] = set_command
454 """Try "wait" as player's T_COMMAND."""
455 if world_db["WORLD_ACTIVE"]:
456 world_db["set_command"]("wait")
460 length = world_db["MAP_LENGTH"]
462 for i in range(length):
463 line = world_db["wetmap"][i * length:(i * length) + length].decode()
464 string = string + "WETMAP" + " " + str(i) + " " + line + "\n"
465 for i in range(length):
466 line = world_db["soundmap"][i * length:(i * length) + length].decode()
467 string = string + "SOUNDMAP" + " " + str(i) + " " + line + "\n"
471 def soundmapset(str_int, mapline):
472 def valid_map_line(str_int, mapline):
473 from server.utils import integer_test
474 val = integer_test(str_int, 0, 255)
476 if val >= world_db["MAP_LENGTH"]:
477 print("Illegal value for map line number.")
478 elif len(mapline) != world_db["MAP_LENGTH"]:
479 print("Map line length is unequal map width.")
483 val = valid_map_line(str_int, mapline)
485 length = world_db["MAP_LENGTH"]
486 if not world_db["soundmap"]:
487 m = bytearray(b' ' * (length ** 2))
489 m = world_db["soundmap"]
490 m[val * length:(val * length) + length] = mapline.encode()
491 if not world_db["soundmap"]:
492 world_db["soundmap"] = m
495 def wetmapset(str_int, mapline):
496 def valid_map_line(str_int, mapline):
497 from server.utils import integer_test
498 val = integer_test(str_int, 0, 255)
500 if val >= world_db["MAP_LENGTH"]:
501 print("Illegal value for map line number.")
502 elif len(mapline) != world_db["MAP_LENGTH"]:
503 print("Map line length is unequal map width.")
507 val = valid_map_line(str_int, mapline)
509 length = world_db["MAP_LENGTH"]
510 if not world_db["wetmap"]:
511 m = bytearray(b' ' * (length ** 2))
513 m = world_db["wetmap"]
514 m[val * length:(val * length) + length] = mapline.encode()
515 if not world_db["wetmap"]:
516 world_db["wetmap"] = m
519 def write_soundmap():
520 from server.worldstate_write_helpers import write_map
521 length = world_db["MAP_LENGTH"]
522 return write_map(world_db["soundmap"], world_db["MAP_LENGTH"])
526 from server.worldstate_write_helpers import write_map
527 length = world_db["MAP_LENGTH"]
528 visible_wetmap = bytearray(b' ' * (length ** 2))
529 for i in range(length ** 2):
530 if world_db["Things"][0]["fovmap"][i] == ord('v'):
531 visible_wetmap[i] = world_db["wetmap"][i]
532 return write_map(visible_wetmap, world_db["MAP_LENGTH"])
535 def get_dir_to_target(t, target):
537 from server.utils import rand, libpr, c_pointer_to_bytearray
538 from server.config.world_data import symbols_passable
540 def get_map_score(pos):
541 result = libpr.get_map_score(pos)
543 raise RuntimeError("No score map allocated for get_map_score().")
546 def zero_score_map_where_char_on_memdepthmap(c):
547 map = c_pointer_to_bytearray(t["T_MEMDEPTHMAP"])
548 if libpr.zero_score_map_where_char_on_memdepthmap(c, map):
549 raise RuntimeError("No score map allocated for "
550 "zero_score_map_where_char_on_memdepthmap().")
552 def set_map_score(pos, score):
553 test = libpr.set_map_score(pos, score)
555 raise RuntimeError("No score map allocated for set_map_score().")
557 def set_movement_cost_map():
558 copy_memmap = t["T_MEMMAP"][:]
559 copy_memmap.replace(b' ', b'4')
560 memmap = c_pointer_to_bytearray(copy_memmap)
561 if libpr.TCE_set_movement_cost_map(memmap):
562 raise RuntimeError("No movement cost map allocated for "
563 "set_movement_cost_map().")
565 def animates_in_fov(maplength):
566 return [Thing for Thing in world_db["Things"].values()
567 if Thing["T_LIFEPOINTS"] and 118 == t["fovmap"][Thing["pos"]]
568 and (not Thing == t)]
574 except StopIteration:
577 mapsize = world_db["MAP_LENGTH"] ** 2
578 if target == "food" and t["T_MEMMAP"]:
579 return exists(pos for pos in range(mapsize)
580 if ord("2") < t["T_MEMMAP"][pos] < ord("5"))
581 elif target == "fluid_certain" and t["fovmap"]:
582 return exists(pos for pos in range(mapsize)
583 if t["fovmap"] == ord("v")
584 if world_db["MAP"][pos] == ord("0")
585 if world_db["wetmap"][pos] > ord("0"))
586 elif target == "crack" and t["T_MEMMAP"]:
587 return exists(pos for pos in range(mapsize)
588 if t["T_MEMMAP"][pos] == ord("-"))
589 elif target == "fluid_potential" and t["T_MEMMAP"] and t["fovmap"]:
590 return exists(pos for pos in range(mapsize)
591 if t["T_MEMMAP"][pos] == ord("0")
592 if t["fovmap"] != ord("v"))
593 elif target == "space" and t["T_MEMMAP"] and t["fovmap"]:
594 return exists(pos for pos in range(mapsize)
595 if ord("-") <= t["T_MEMMAP"][pos] <= ord("2")
596 if (t["fovmap"] != ord("v")
597 or world_db["terrain_fullness"](pos) < 5))
598 elif target in {"hunt", "flee"} and t["fovmap"]:
599 return exists(Thing for
600 Thing in animates_in_fov(world_db["MAP_LENGTH"])) \
601 or exists(pos for pos in range(mapsize)
602 if world_db["soundmap"][pos] > ord("0")
603 if t["fovmap"][pos] != ord("v"))
606 def init_score_map():
607 mapsize = world_db["MAP_LENGTH"] ** 2
608 test = libpr.TCE_init_score_map()
609 [set_map_score(pos, 65535) for pos in range(mapsize)
610 if chr(t["T_MEMMAP"][pos]) in "5*&"]
611 set_movement_cost_map()
613 raise RuntimeError("Malloc error in init_score_map().")
614 if target == "food" and t["T_MEMMAP"]:
615 [set_map_score(pos, 0) for pos in range(mapsize)
616 if ord("2") < t["T_MEMMAP"][pos] < ord("5")]
617 elif target == "fluid_certain" and t["fovmap"]:
618 [set_map_score(pos, 0) for pos in range(mapsize)
619 if t["fovmap"] == ord("v")
620 if world_db["MAP"][pos] == ord("0")
621 if world_db["wetmap"][pos] > ord("0")]
622 elif target == "crack" and t["T_MEMMAP"]:
623 [set_map_score(pos, 0) for pos in range(mapsize)
624 if t["T_MEMMAP"][pos] == ord("-")]
625 elif target == "fluid_potential" and t["T_MEMMAP"] and t["fovmap"]:
626 [set_map_score(pos, 0) for pos in range(mapsize)
627 if t["T_MEMMAP"][pos] == ord("0")
628 if t["fovmap"] != ord("v")]
629 elif target == "space" and t["T_MEMMAP"] and t["fovmap"]:
630 [set_map_score(pos, 0) for pos in range(mapsize)
631 if ord("-") <= t["T_MEMMAP"][pos] <= ord("2")
632 if (t["fovmap"] != ord("v")
633 or world_db["terrain_fullness"](pos) < 5)]
634 elif target == "search":
635 zero_score_map_where_char_on_memdepthmap(mem_depth_c[0])
636 elif target in {"hunt", "flee"}:
637 [set_map_score(Thing["pos"], 0) for
638 Thing in animates_in_fov(world_db["MAP_LENGTH"])]
639 [set_map_score(pos, 0) for pos in range(mapsize)
640 if world_db["soundmap"][pos] > ord("0")
641 if t["fovmap"][pos] != ord("v")]
643 def rand_target_dir(neighbors, cmp, dirs):
646 for i in range(len(dirs)):
647 if cmp == neighbors[i]:
648 candidates.append(dirs[i])
650 return candidates[rand.next() % n_candidates] if n_candidates else 0
652 def get_neighbor_scores(dirs, eye_pos):
654 if libpr.ready_neighbor_scores(eye_pos):
655 raise RuntimeError("No score map allocated for " +
656 "ready_neighbor_scores.()")
657 for i in range(len(dirs)):
658 scores.append(libpr.get_neighbor_score(i))
661 def get_dir_from_neighbors():
663 dir_to_target = False
666 neighbors = get_neighbor_scores(dirs, eye_pos)
667 minmax_start = 0 if "flee" == target else 65535 - 1
668 minmax_neighbor = minmax_start
669 for i in range(len(dirs)):
670 if ("flee" == target and get_map_score(t["pos"]) < neighbors[i] and
671 minmax_neighbor < neighbors[i] and 65535 != neighbors[i]) \
672 or ("flee" != target and minmax_neighbor > neighbors[i]):
673 minmax_neighbor = neighbors[i]
674 if minmax_neighbor != minmax_start:
675 dir_to_target = rand_target_dir(neighbors, minmax_neighbor, dirs)
677 distance = get_map_score(t["pos"])
680 if not dir_to_target:
681 if attack_distance >= distance:
682 dir_to_target = rand_target_dir(neighbors,
684 elif dir_to_target and fear_distance < distance:
686 return dir_to_target, minmax_neighbor
688 dir_to_target = False
690 run_i = 9 + 1 if "search" == target else 1
692 while run_i and not dir_to_target and \
693 ("search" == target or seeing_thing()):
696 mem_depth_c = b'9' if b' ' == mem_depth_c \
697 else bytes([mem_depth_c[0] - 1])
698 if libpr.TCE_dijkstra_map_with_movement_cost():
699 raise RuntimeError("No score map allocated for dijkstra_map().")
700 dir_to_target, minmax_neighbor = get_dir_from_neighbors()
701 libpr.free_score_map()
702 if dir_to_target and str == type(dir_to_target):
704 from server.utils import mv_yx_in_dir_legal
705 move_result = mv_yx_in_dir_legal(dir_to_target, t["T_POSY"],
707 if 1 != move_result[0]:
709 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
710 hitted = [tid for tid in world_db["Things"]
711 if world_db["Things"][tid]["pos"] == pos]
712 if world_db["MAP"][pos] > ord("2") or len(hitted) > 0:
714 t["T_COMMAND"] = [taid for taid in world_db["ThingActions"]
715 if world_db["ThingActions"][taid]["TA_NAME"]
717 t["T_ARGUMENT"] = ord(dir_to_target)
718 return dir_to_target, minmax_neighbor
719 world_db["get_dir_to_target"] = get_dir_to_target
722 def terrain_fullness(pos):
723 wetness = world_db["wetmap"][pos] - ord("0")
724 if chr(world_db["MAP"][pos]) in "-+":
727 height = world_db["MAP"][pos] - ord("0")
728 return wetness + height
729 world_db["terrain_fullness"] = terrain_fullness
734 if t["T_LIFEPOINTS"] == 0:
737 def standing_on_fluid(t):
738 if world_db["MAP"][t["pos"]] == ord("0") and \
739 world_db["wetmap"][t["pos"]] > ord("0"):
744 def thing_action_id(name):
745 return [taid for taid in world_db["ThingActions"]
746 if world_db["ThingActions"][taid]
747 ["TA_NAME"] == name][0]
749 t["T_COMMAND"] = thing_action_id("wait")
753 "safe_pee": (world_db["terrain_fullness"](t["pos"]) * t["T_BLADDER"]) / 4,
754 "safe_drop": (world_db["terrain_fullness"](t["pos"]) * t["T_BOWEL"]) / 4,
755 "food": 33 - t["T_STOMACH"],
756 "fluid_certain": 33 - t["T_KIDNEY"],
757 "fluid_potential": 32 - t["T_KIDNEY"],
760 from operator import itemgetter
761 needs = sorted(needs.items(), key=itemgetter(1,0))
765 if need[0] == "fix_cracks":
766 if world_db["MAP"][t["pos"]] == ord("-") and \
767 t["T_BOWEL"] > 0 and \
768 world_db["terrain_fullness"](t["pos"]) <= 3:
769 t["T_COMMAND"] = thing_action_id("drop")
771 elif world_db["get_dir_to_target"](t, "crack"):
773 if need[0] in {"fluid_certain", "fluid_potential"}:
774 if standing_on_fluid(t):
775 t["T_COMMAND"] = thing_action_id("drink")
777 elif t["T_BLADDER"] > 0 and \
778 world_db["MAP"][t["pos"]] == ord("0"):
779 t["T_COMMAND"] = thing_action_id("pee")
781 elif need[0] in {"safe_pee", "safe_drop"}:
782 action_name = need[0][len("safe_"):]
783 if world_db["terrain_fullness"](t["pos"]) <= 3:
784 t["T_COMMAND"] = thing_action_id(action_name)
786 test = world_db["get_dir_to_target"](t, "space")
790 elif world_db["terrain_fullness"](t["pos"]) < 5:
791 t["T_COMMAND"] = thing_action_id(action_name)
793 if t["T_STOMACH"] < 32 and \
794 world_db["get_dir_to_target"](t, "food")[0]:
797 if need[0] in {"fluid_certain", "fluid_potential", "food"}:
798 if world_db["get_dir_to_target"](t, need[0])[0]:
800 elif world_db["get_dir_to_target"](t, "hunt")[0]:
802 elif need[0] != "food" and t["T_STOMACH"] < 32 and \
803 world_db["get_dir_to_target"](t, "food")[0]:
805 elif world_db["get_dir_to_target"](t, need[0])[0]:
810 from server.config.io import io_db
811 io_db["worldstate_write_order"] += [["T_STOMACH", "player_int"]]
812 io_db["worldstate_write_order"] += [["T_KIDNEY", "player_int"]]
813 io_db["worldstate_write_order"] += [["T_BOWEL", "player_int"]]
814 io_db["worldstate_write_order"] += [["T_BLADDER", "player_int"]]
815 io_db["worldstate_write_order"] += [[write_wetmap, "func"]]
816 io_db["worldstate_write_order"] += [[write_soundmap, "func"]]
817 io_db["worldstate_write_order"] += [["GRACE", "world_int"]]
818 import server.config.world_data
819 server.config.world_data.symbols_hide = "345"
820 server.config.world_data.symbols_passable = "012-+*&$"
821 server.config.world_data.thing_defaults["T_STOMACH"] = 16
822 server.config.world_data.thing_defaults["T_BOWEL"] = 0
823 server.config.world_data.thing_defaults["T_KIDNEY"] = 16
824 server.config.world_data.thing_defaults["T_BLADDER"] = 0
825 world_db["soundmap"] = bytearray(b"0" * world_db["MAP_LENGTH"] ** 2)
826 world_db["wetmap"] = bytearray(b"0" * world_db["MAP_LENGTH"] ** 2)
827 if not "NEW_SPAWN" in world_db:
828 world_db["NEW_SPAWN"] = 0
829 if not "HUMIDITY" in world_db:
830 world_db["HUMIDITY"] = 0
831 if not "GRACE" in world_db:
832 world_db["GRACE"] = 0
833 io_db["hook_save"] = save_maps
834 import server.config.make_world_helpers
835 server.config.make_world_helpers.make_map = make_map
836 from server.config.commands import commands_db
837 commands_db["THINGS_HERE"] = (2, True, lambda x, y: None)
838 commands_db["HELP"] = (1, False, command_help)
839 commands_db["ai"] = (0, False, command_ai)
840 commands_db["move"] = (1, False, play_move)
841 commands_db["eat"] = (1, False, play_move)
842 commands_db["wait"] = (0, False, play_wait)
843 commands_db["drop"] = (0, False, play_drop)
844 commands_db["drink"] = (0, False, play_drink)
845 commands_db["pee"] = (0, False, play_pee)
846 commands_db["use"] = (1, False, lambda x: None)
847 commands_db["pickup"] = (0, False, lambda: None)
848 commands_db["GRACE"] = (1, False, setter(None, "GRACE", 0, 255))
849 commands_db["NEW_SPAWN"] = (1, False, setter(None, "NEW_SPAWN", 0, 255))
850 commands_db["HUMIDITY"] = (1, False, setter(None, "HUMIDITY", 0, 65535))
851 commands_db["T_STOMACH"] = (1, False, setter("Thing", "T_STOMACH", 0, 255))
852 commands_db["T_KIDNEY"] = (1, False, setter("Thing", "T_KIDNEY", 0, 255))
853 commands_db["T_BOWEL"] = (1, False, setter("Thing", "T_BOWEL", 0, 255))
854 commands_db["T_BLADDER"] = (1, False, setter("Thing", "T_BLADDER", 0, 255))
855 commands_db["WETMAP"] = (2, False, wetmapset)
856 commands_db["SOUNDMAP"] = (2, False, soundmapset)
857 from server.actions import actor_wait
858 import server.config.actions
859 server.config.actions.action_db = {
860 "actor_wait": actor_wait,
861 "actor_move": actor_move,
862 "actor_drop": actor_drop,
863 "actor_drink": actor_drink,
864 "actor_pee": actor_pee,
865 "actor_eat": actor_eat,
868 strong_write(io_db["file_out"], "PLUGIN TheCrawlingEater\n")