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 if chr(world_db["MAP"][t["pos"]]) not in "*&":
67 world_db["wetmap"][t["pos"]] += 1
71 if not (action_exists("drop") and world_db["WORLD_ACTIVE"]
72 and world_db["Things"][0]["T_LIFEPOINTS"] > 0):
74 if world_db["Things"][0]["T_BOWEL"] < 1:
75 log("Nothing to drop from empty bowel.")
77 world_db["set_command"]("drop")
83 if t == world_db["Things"][0]:
84 log("You DROP waste.")
85 if not world_db["test_air"](t):
87 if world_db["MAP"][t["pos"]] == ord("+"):
88 world_db["MAP"][t["pos"]] = ord("-")
89 elif world_db["MAP"][t["pos"]] == ord("-"):
90 world_db["MAP"][t["pos"]] = ord("0")
91 elif chr(world_db["MAP"][t["pos"]]) not in "*&":
92 world_db["MAP"][t["pos"]] += 1
96 def play_move(str_arg):
97 if not (action_exists("move") and world_db["WORLD_ACTIVE"]
98 and world_db["Things"][0]["T_LIFEPOINTS"] > 0):
100 from server.config.world_data import directions_db, symbols_passable
101 t = world_db["Things"][0]
102 if not str_arg in directions_db:
103 print("Illegal move direction string.")
105 d = ord(directions_db[str_arg])
106 from server.utils import mv_yx_in_dir_legal
107 move_result = mv_yx_in_dir_legal(chr(d), t["T_POSY"], t["T_POSX"])
108 if 1 == move_result[0]:
109 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
110 hitted = [tid for tid in world_db["Things"]
111 if world_db["Things"][tid]["T_POSY"] == move_result[1]
112 if world_db["Things"][tid]["T_POSX"] == move_result[2]]
114 if t["T_STOMACH"] >= 32 and t["T_KIDNEY"] >= 32:
115 if t == world_db["Things"][0]:
116 log("You're too FULL to suck from another creature.")
118 world_db["Things"][0]["T_ARGUMENT"] = d
119 world_db["set_command"]("eat")
122 if world_db["GRACE"] >= 8:
124 if chr(world_db["MAP"][pos]) in legal_targets:
125 if t["T_STOMACH"] >= 32:
126 if t == world_db["Things"][0]:
127 log("You're too FULL to eat.")
129 world_db["Things"][0]["T_ARGUMENT"] = d
130 world_db["set_command"]("eat")
132 if chr(world_db["MAP"][pos]) in symbols_passable:
133 world_db["Things"][0]["T_ARGUMENT"] = d
134 world_db["set_command"]("move")
136 log("You CAN'T eat your way through there.")
139 def suck_out_creature(t, tid):
141 t = world_db["Things"][tid]
143 tid = next(tid for tid in world_db["Things"]
144 if world_db["Things"][tid] == t)
145 room_stomach = 32 - world_db["Things"][0]["T_STOMACH"]
146 room_kidney = 32 - world_db["Things"][0]["T_KIDNEY"]
147 if t["T_STOMACH"] > room_stomach:
148 t["T_STOMACH"] -= room_stomach
149 world_db["Things"][0]["T_STOMACH"] = 32
151 world_db["Things"][0]["T_STOMACH"] + t["T_STOMACH"]
153 if t["T_KIDNEY"] > room_stomach:
154 t["T_KIDNEY"] -= room_stomach
155 world_db["Things"][0]["T_KIDNEY"] = 32
157 world_db["Things"][0]["T_KIDNEY"] + t["T_KIDNEY"]
159 hitted_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
160 log("You SUCK EVERYTHING from " + hitted_name + ", killing them.")
161 world_db["die"](t, "FOO")
162 world_db["suck_out_creature"] = suck_out_creature
166 from server.utils import mv_yx_in_dir_legal, rand
167 from server.config.world_data import symbols_passable
169 move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
170 t["T_POSY"], t["T_POSX"])
171 if 1 == move_result[0]:
172 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
173 hitted = [tid for tid in world_db["Things"]
174 if world_db["Things"][tid]["T_POSY"] == move_result[1]
175 if world_db["Things"][tid]["T_POSX"] == move_result[2]]
178 hitted_tid = world_db["Things"][hit_id]["T_TYPE"]
179 if t == world_db["Things"][0]:
180 if world_db["GRACE"] >= 16:
181 world_db["suck_out_creature"](None, hit_id)
183 hitted_name = world_db["ThingTypes"][hitted_tid]["TT_NAME"]
184 log("You SUCK from " + hitted_name + ".")
186 if world_db["GRACE"] >= 16:
187 world_db["suck_out_creature"](t, None)
189 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
190 log(hitter_name +" SUCKS from you.")
191 hitted = world_db["Things"][hit_id]
192 if t["T_STOMACH"] < 32:
193 t["T_STOMACH"] = t["T_STOMACH"] + 1
194 hitted["T_STOMACH"] -= 1
195 if t["T_KIDNEY"] < 32:
196 t["T_KIDNEY"] = t["T_KIDNEY"] + 1
197 hitted["T_KIDNEY"] -= 1
199 passable = chr(world_db["MAP"][pos]) in symbols_passable
200 if passable and t == world_db["Things"][0]:
201 log("You try to EAT, but fail.")
203 height = world_db["MAP"][pos] - ord("0")
204 if t["T_STOMACH"] >= 32:
206 if height == 5 and not \
207 (t == world_db["Things"][0] and world_db["GRACE"] >= 8):
210 if t == world_db["Things"][0]:
212 eaten = (height == 3 and 0 == int(rand.next() % 2)) or \
213 (height == 4 and 0 == int(rand.next() % 5)) or \
214 (height == 5 and 0 == int(rand.next() % 10))
216 world_db["MAP"][pos] = ord("0")
217 if t["T_STOMACH"] > 32:
222 from server.build_fov_map import build_fov_map
223 from server.utils import mv_yx_in_dir_legal, rand
224 from server.config.world_data import symbols_passable
226 move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
227 t["T_POSY"], t["T_POSX"])
228 if 1 == move_result[0]:
229 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
230 hitted = [tid for tid in world_db["Things"]
231 if world_db["Things"][tid]["T_POSY"] == move_result[1]
232 if world_db["Things"][tid]["T_POSX"] == move_result[2]]
235 hitted_tid = world_db["Things"][hit_id]["T_TYPE"]
236 if t == world_db["Things"][0]:
237 if world_db["GRACE"] >= 16:
238 world_db["suck_out_creature"](None, hit_id)
240 hitted_name = world_db["ThingTypes"][hitted_tid]["TT_NAME"]
241 log("You BUMP into " + hitted_name + ".")
243 if world_db["GRACE"] >= 16:
244 world_db["suck_out_creature"](t, None)
246 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
247 log(hitter_name +" BUMPS into you.")
249 passable = chr(world_db["MAP"][pos]) in symbols_passable
251 t["T_POSY"] = move_result[1]
252 t["T_POSX"] = move_result[2]
253 t["pos"] = move_result[1] * world_db["MAP_LENGTH"] + move_result[2]
254 world_db["soundmap"][t["pos"]] = ord("9")
255 if t == world_db["Things"][0] and world_db["MAP"][t["pos"]] == ord("$"):
256 world_db["MAP"][t["pos"]] = ord("0")
257 if world_db["GRACE"] < 8:
258 log("You can now eat ALL walls.")
259 if world_db["GRACE"] < 16:
260 log("You now have the DEATH touch.")
261 if world_db["GRACE"] < 24:
262 log("You will now LEVITATE over holes.")
263 if world_db["GRACE"] <= 24:
264 world_db["GRACE"] += 8
265 elif t == world_db["Things"][0]:
266 log("You try to MOVE there, but fail.")
270 if t == world_db["Things"][0]:
271 if world_db["GRACE"] >= 32 and world_db["MAP"][t["pos"]] == ord("&"):
272 world_db["die"](t, "YOU WIN, CONGRATULATIONS.")
274 if world_db["GRACE"] >= 24:
276 if chr(world_db["MAP"][t["pos"]]) in "*&":
277 world_db["die"](t, "You FALL in a hole, and die.")
280 world_db["test_hole"] = test_hole
284 if world_db["terrain_fullness"](t["pos"]) > 5:
285 world_db["die"](t, "You SUFFOCATE")
288 world_db["test_air"] = test_air
292 t["T_LIFEPOINTS"] = 0
293 if t == world_db["Things"][0]:
294 t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
295 t["T_MEMMAP"][t["pos"]] = ord("@")
298 if world_db["MAP"][t["pos"]] != ord("$"):
299 world_db["MAP"][t["pos"]] = ord("5")
300 world_db["HUMILITY"] = t["T_KIDNEY"] + t["T_BLADDER"] + \
301 (world_db["wetmap"][t["pos"]] - ord("0"))
302 world_db["wetmap"][t["pos"]] = 0
303 tid = next(tid for tid in world_db["Things"]
304 if world_db["Things"][tid] == t)
305 del world_db["Things"][tid]
306 world_db["die"] = die
310 from server.make_map import new_pos, is_neighbor
311 from server.utils import rand
312 world_db["MAP"] = bytearray(b'5' * (world_db["MAP_LENGTH"] ** 2))
313 length = world_db["MAP_LENGTH"]
314 add_half_width = (not (length % 2)) * int(length / 2)
315 world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord("4")
317 y, x, pos = new_pos()
318 if "5" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "4"):
319 if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
321 world_db["MAP"][pos] = ord("4")
322 n_ground = int((length ** 2) / 16)
324 while (i_ground <= n_ground):
325 single_allowed = rand.next() % 32
326 y, x, pos = new_pos()
327 if "4" == chr(world_db["MAP"][pos]) \
328 and ((not single_allowed) or is_neighbor((y, x), "0")):
329 world_db["MAP"][pos] = ord("0")
331 n_water = int((length ** 2) / 32)
333 while (i_water <= n_water):
334 y, x, pos = new_pos()
335 if ord("0") == world_db["MAP"][pos] and \
336 ord("0") == world_db["wetmap"][pos]:
337 world_db["wetmap"][pos] = ord("3")
341 while (i_altars < n_altars):
342 y, x, pos = new_pos()
343 if ord("0") == world_db["MAP"][pos]:
344 world_db["MAP"][pos] = ord("$")
348 def calc_effort(ta, t):
349 from server.utils import mv_yx_in_dir_legal
350 if ta["TA_NAME"] == "move":
351 move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
352 t["T_POSY"], t["T_POSX"])
353 if 1 == move_result[0]:
354 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
355 if chr(world_db["MAP"][pos]) in "012":
356 narrowness = world_db["MAP"][pos] - ord("0")
357 return 2 ** narrowness
359 world_db["calc_effort"] = calc_effort
363 from server.ai import ai
364 from server.config.actions import action_db
365 from server.update_map_memory import update_map_memory
366 from server.io import try_worldstate_update
367 from server.config.io import io_db
368 from server.utils import rand
369 from server.build_fov_map import build_fov_map
370 while world_db["Things"][0]["T_LIFEPOINTS"]:
371 for tid in [tid for tid in world_db["Things"]]:
372 if not tid in world_db["Things"]:
374 t = world_db["Things"][tid]
375 if t["T_LIFEPOINTS"]:
376 if not (world_db["test_air"](t) and world_db["test_hole"](t)):
378 if not t["T_COMMAND"]:
384 if t["T_LIFEPOINTS"]:
386 taid = [a for a in world_db["ThingActions"]
387 if a == t["T_COMMAND"]][0]
388 ThingAction = world_db["ThingActions"][taid]
389 effort = world_db["calc_effort"](ThingAction, t)
390 if t["T_PROGRESS"] >= effort:
391 action = action_db["actor_" + ThingAction["TA_NAME"]]
393 if t["T_LIFEPOINTS"] <= 0:
397 if t["T_BOWEL"] > 16:
398 if 0 == (rand.next() % (33 - t["T_BOWEL"])):
399 action_db["actor_drop"](t)
400 if t["T_BLADDER"] > 16:
401 if 0 == (rand.next() % (33 - t["T_BLADDER"])):
402 action_db["actor_pee"](t)
403 if 0 == world_db["TURN"] % 5:
408 if t["T_STOMACH"] <= 0:
409 world_db["die"](t, "You DIE of hunger.")
410 elif t["T_KIDNEY"] <= 0:
411 world_db["die"](t, "You DIE of dehydration.")
412 mapsize = world_db["MAP_LENGTH"] ** 2
413 for pos in range(mapsize):
414 wetness = world_db["wetmap"][pos] - ord("0")
415 height = world_db["MAP"][pos] - ord("0")
416 if world_db["MAP"][pos] == ord("-"):
418 elif world_db["MAP"][pos] == ord("+"):
420 elif world_db["MAP"][pos] == ord("$"):
422 if height == -2 and wetness > 1 \
423 and 0 == rand.next() % ((2 ** 11) / (2 ** wetness)):
424 world_db["MAP"][pos] = ord("*")
425 world_db["HUMIDITY"] += wetness
426 if height == -1 and wetness > 1 \
427 and 0 == rand.next() % ((2 ** 10) / (2 ** wetness)):
428 world_db["MAP"][pos] = ord("+")
429 if height == 0 and wetness > 1 \
430 and 0 == rand.next() % ((2 ** 9) / (2 ** wetness)):
431 world_db["MAP"][pos] = ord("-")
432 if ((wetness > 0 and height > 0) or wetness > 1) \
433 and 0 == rand.next() % 5:
434 world_db["wetmap"][pos] -= 1
435 world_db["HUMIDITY"] += 1
436 if world_db["HUMIDITY"] > 0:
437 if world_db["HUMIDITY"] > 2 and 0 == rand.next() % 2:
438 world_db["NEW_SPAWN"] += 1
439 world_db["HUMIDITY"] -= 1
440 if world_db["NEW_SPAWN"] >= 16:
441 world_db["NEW_SPAWN"] -= 16
442 from server.new_thing import new_Thing
444 y = rand.next() % world_db["MAP_LENGTH"]
445 x = rand.next() % world_db["MAP_LENGTH"]
446 if chr(world_db["MAP"][y * world_db["MAP_LENGTH"] + x]) !=\
448 from server.utils import id_setter
449 tid = id_setter(-1, "Things")
450 world_db["Things"][tid] = new_Thing(
451 world_db["PLAYER_TYPE"], (y, x))
452 pos = y * world_db["MAP_LENGTH"] + x
454 positions_to_wet = []
455 for pos in range(mapsize):
456 if chr(world_db["MAP"][pos]) in "0-+" \
457 and world_db["wetmap"][pos] < ord("5"):
458 positions_to_wet += [pos]
459 while world_db["HUMIDITY"] > 0 and len(positions_to_wet) > 0:
460 select = rand.next() % len(positions_to_wet)
461 pos = positions_to_wet[select]
462 world_db["wetmap"][pos] += 1
463 positions_to_wet.remove(pos)
464 world_db["HUMIDITY"] -= 1
465 for pos in range(mapsize):
466 if world_db["soundmap"][pos] > ord("0"):
467 world_db["soundmap"][pos] -= 1
468 from server.utils import libpr
469 libpr.init_score_map()
470 def set_map_score(pos, score):
471 test = libpr.set_map_score(pos, score)
473 raise RuntimeError("No score map allocated for set_map_score().")
474 [set_map_score(pos, 1) for pos in range(mapsize)
475 if world_db["MAP"][pos] == ord("*")]
476 for pos in range(mapsize):
477 if world_db["MAP"][pos] == ord("*"):
478 if libpr.ready_neighbor_scores(pos):
479 raise RuntimeError("No score map allocated for " +
480 "ready_neighbor_scores.()")
483 for i in range(len(dirs)):
484 score += libpr.get_neighbor_score(i)
485 if score == 5 or score == 6:
486 world_db["MAP"][pos] = ord("&")
487 libpr.free_score_map()
488 world_db["TURN"] += 1
489 io_db["worldstate_updateable"] = True
490 try_worldstate_update()
491 world_db["turn_over"] = turn_over
494 def set_command(action):
495 """Set player's T_COMMAND, then call turn_over()."""
496 tid = [x for x in world_db["ThingActions"]
497 if world_db["ThingActions"][x]["TA_NAME"] == action][0]
498 world_db["Things"][0]["T_COMMAND"] = tid
499 world_db["turn_over"]()
500 world_db["set_command"] = set_command
504 """Try "wait" as player's T_COMMAND."""
505 if world_db["WORLD_ACTIVE"]:
506 world_db["set_command"]("wait")
510 length = world_db["MAP_LENGTH"]
512 for i in range(length):
513 line = world_db["wetmap"][i * length:(i * length) + length].decode()
514 string = string + "WETMAP" + " " + str(i) + " " + line + "\n"
515 for i in range(length):
516 line = world_db["soundmap"][i * length:(i * length) + length].decode()
517 string = string + "SOUNDMAP" + " " + str(i) + " " + line + "\n"
521 def soundmapset(str_int, mapline):
522 def valid_map_line(str_int, mapline):
523 from server.utils import integer_test
524 val = integer_test(str_int, 0, 255)
526 if val >= world_db["MAP_LENGTH"]:
527 print("Illegal value for map line number.")
528 elif len(mapline) != world_db["MAP_LENGTH"]:
529 print("Map line length is unequal map width.")
533 val = valid_map_line(str_int, mapline)
535 length = world_db["MAP_LENGTH"]
536 if not world_db["soundmap"]:
537 m = bytearray(b' ' * (length ** 2))
539 m = world_db["soundmap"]
540 m[val * length:(val * length) + length] = mapline.encode()
541 if not world_db["soundmap"]:
542 world_db["soundmap"] = m
545 def wetmapset(str_int, mapline):
546 def valid_map_line(str_int, mapline):
547 from server.utils import integer_test
548 val = integer_test(str_int, 0, 255)
550 if val >= world_db["MAP_LENGTH"]:
551 print("Illegal value for map line number.")
552 elif len(mapline) != world_db["MAP_LENGTH"]:
553 print("Map line length is unequal map width.")
557 val = valid_map_line(str_int, mapline)
559 length = world_db["MAP_LENGTH"]
560 if not world_db["wetmap"]:
561 m = bytearray(b' ' * (length ** 2))
563 m = world_db["wetmap"]
564 m[val * length:(val * length) + length] = mapline.encode()
565 if not world_db["wetmap"]:
566 world_db["wetmap"] = m
569 def write_soundmap():
570 from server.worldstate_write_helpers import write_map
571 length = world_db["MAP_LENGTH"]
572 return write_map(world_db["soundmap"], world_db["MAP_LENGTH"])
576 from server.worldstate_write_helpers import write_map
577 length = world_db["MAP_LENGTH"]
578 visible_wetmap = bytearray(b' ' * (length ** 2))
579 for i in range(length ** 2):
580 if world_db["Things"][0]["fovmap"][i] == ord('v'):
581 visible_wetmap[i] = world_db["wetmap"][i]
582 return write_map(visible_wetmap, world_db["MAP_LENGTH"])
585 def get_dir_to_target(t, target):
587 from server.utils import rand, libpr, c_pointer_to_bytearray
588 from server.config.world_data import symbols_passable
590 def get_map_score(pos):
591 result = libpr.get_map_score(pos)
593 raise RuntimeError("No score map allocated for get_map_score().")
596 def zero_score_map_where_char_on_memdepthmap(c):
597 map = c_pointer_to_bytearray(t["T_MEMDEPTHMAP"])
598 if libpr.zero_score_map_where_char_on_memdepthmap(c, map):
599 raise RuntimeError("No score map allocated for "
600 "zero_score_map_where_char_on_memdepthmap().")
602 def set_map_score(pos, score):
603 test = libpr.set_map_score(pos, score)
605 raise RuntimeError("No score map allocated for set_map_score().")
607 def set_movement_cost_map():
608 copy_memmap = t["T_MEMMAP"][:]
609 copy_memmap.replace(b' ', b'4')
610 memmap = c_pointer_to_bytearray(copy_memmap)
611 if libpr.TCE_set_movement_cost_map(memmap):
612 raise RuntimeError("No movement cost map allocated for "
613 "set_movement_cost_map().")
615 def animates_in_fov(maplength):
616 return [Thing for Thing in world_db["Things"].values()
617 if Thing["T_LIFEPOINTS"] and 118 == t["fovmap"][Thing["pos"]]
618 and (not Thing == t)]
624 except StopIteration:
627 mapsize = world_db["MAP_LENGTH"] ** 2
628 if target == "food" and t["T_MEMMAP"]:
629 return exists(pos for pos in range(mapsize)
630 if ord("2") < t["T_MEMMAP"][pos] < ord("5"))
631 elif target == "fluid_certain" and t["fovmap"]:
632 return exists(pos for pos in range(mapsize)
633 if t["fovmap"] == ord("v")
634 if world_db["MAP"][pos] == ord("0")
635 if world_db["wetmap"][pos] > ord("0"))
636 elif target == "crack" and t["T_MEMMAP"]:
637 return exists(pos for pos in range(mapsize)
638 if t["T_MEMMAP"][pos] == ord("-"))
639 elif target == "fluid_potential" and t["T_MEMMAP"] and t["fovmap"]:
640 return exists(pos for pos in range(mapsize)
641 if t["T_MEMMAP"][pos] == ord("0")
642 if t["fovmap"] != ord("v"))
643 elif target == "space" and t["T_MEMMAP"] and t["fovmap"]:
644 return exists(pos for pos in range(mapsize)
645 if ord("-") <= t["T_MEMMAP"][pos] <= ord("2")
646 if (t["fovmap"] != ord("v")
647 or world_db["terrain_fullness"](pos) < 5))
648 elif target in {"hunt", "flee"} and t["fovmap"]:
649 return exists(Thing for
650 Thing in animates_in_fov(world_db["MAP_LENGTH"])) \
651 or exists(pos for pos in range(mapsize)
652 if world_db["soundmap"][pos] > ord("0")
653 if t["fovmap"][pos] != ord("v"))
656 def init_score_map():
657 mapsize = world_db["MAP_LENGTH"] ** 2
658 test = libpr.TCE_init_score_map()
659 [set_map_score(pos, 65535) for pos in range(mapsize)
660 if chr(t["T_MEMMAP"][pos]) in "5*&"]
661 set_movement_cost_map()
663 raise RuntimeError("Malloc error in init_score_map().")
664 if target == "food" and t["T_MEMMAP"]:
665 [set_map_score(pos, 0) for pos in range(mapsize)
666 if ord("2") < t["T_MEMMAP"][pos] < ord("5")]
667 elif target == "fluid_certain" and t["fovmap"]:
668 [set_map_score(pos, 0) for pos in range(mapsize)
669 if t["fovmap"] == ord("v")
670 if world_db["MAP"][pos] == ord("0")
671 if world_db["wetmap"][pos] > ord("0")]
672 elif target == "crack" and t["T_MEMMAP"]:
673 [set_map_score(pos, 0) for pos in range(mapsize)
674 if t["T_MEMMAP"][pos] == ord("-")]
675 elif target == "fluid_potential" and t["T_MEMMAP"] and t["fovmap"]:
676 [set_map_score(pos, 0) for pos in range(mapsize)
677 if t["T_MEMMAP"][pos] == ord("0")
678 if t["fovmap"] != ord("v")]
679 elif target == "space" and t["T_MEMMAP"] and t["fovmap"]:
680 [set_map_score(pos, 0) for pos in range(mapsize)
681 if ord("-") <= t["T_MEMMAP"][pos] <= ord("2")
682 if (t["fovmap"] != ord("v")
683 or world_db["terrain_fullness"](pos) < 5)]
684 elif target == "search":
685 zero_score_map_where_char_on_memdepthmap(mem_depth_c[0])
686 elif target in {"hunt", "flee"}:
687 [set_map_score(Thing["pos"], 0) for
688 Thing in animates_in_fov(world_db["MAP_LENGTH"])]
689 [set_map_score(pos, 0) for pos in range(mapsize)
690 if world_db["soundmap"][pos] > ord("0")
691 if t["fovmap"][pos] != ord("v")]
693 def rand_target_dir(neighbors, cmp, dirs):
696 for i in range(len(dirs)):
697 if cmp == neighbors[i]:
698 candidates.append(dirs[i])
700 return candidates[rand.next() % n_candidates] if n_candidates else 0
702 def get_neighbor_scores(dirs, eye_pos):
704 if libpr.ready_neighbor_scores(eye_pos):
705 raise RuntimeError("No score map allocated for " +
706 "ready_neighbor_scores.()")
707 for i in range(len(dirs)):
708 scores.append(libpr.get_neighbor_score(i))
711 def get_dir_from_neighbors():
713 dir_to_target = False
716 neighbors = get_neighbor_scores(dirs, eye_pos)
717 minmax_start = 0 if "flee" == target else 65535 - 1
718 minmax_neighbor = minmax_start
719 for i in range(len(dirs)):
720 if ("flee" == target and get_map_score(t["pos"]) < neighbors[i] and
721 minmax_neighbor < neighbors[i] and 65535 != neighbors[i]) \
722 or ("flee" != target and minmax_neighbor > neighbors[i]):
723 minmax_neighbor = neighbors[i]
724 if minmax_neighbor != minmax_start:
725 dir_to_target = rand_target_dir(neighbors, minmax_neighbor, dirs)
727 distance = get_map_score(t["pos"])
730 if not dir_to_target:
731 if attack_distance >= distance:
732 dir_to_target = rand_target_dir(neighbors,
734 elif dir_to_target and fear_distance < distance:
736 return dir_to_target, minmax_neighbor
738 dir_to_target = False
740 run_i = 9 + 1 if "search" == target else 1
742 while run_i and not dir_to_target and \
743 ("search" == target or seeing_thing()):
746 mem_depth_c = b'9' if b' ' == mem_depth_c \
747 else bytes([mem_depth_c[0] - 1])
748 if libpr.TCE_dijkstra_map_with_movement_cost():
749 raise RuntimeError("No score map allocated for dijkstra_map().")
750 dir_to_target, minmax_neighbor = get_dir_from_neighbors()
751 libpr.free_score_map()
752 if dir_to_target and str == type(dir_to_target):
754 from server.utils import mv_yx_in_dir_legal
755 move_result = mv_yx_in_dir_legal(dir_to_target, t["T_POSY"],
757 if 1 != move_result[0]:
759 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
760 hitted = [tid for tid in world_db["Things"]
761 if world_db["Things"][tid]["pos"] == pos]
762 if world_db["MAP"][pos] > ord("2") or len(hitted) > 0:
764 t["T_COMMAND"] = [taid for taid in world_db["ThingActions"]
765 if world_db["ThingActions"][taid]["TA_NAME"]
767 t["T_ARGUMENT"] = ord(dir_to_target)
768 return dir_to_target, minmax_neighbor
769 world_db["get_dir_to_target"] = get_dir_to_target
772 def terrain_fullness(pos):
773 wetness = world_db["wetmap"][pos] - ord("0")
774 if chr(world_db["MAP"][pos]) in "-+":
777 height = world_db["MAP"][pos] - ord("0")
778 return wetness + height
779 world_db["terrain_fullness"] = terrain_fullness
784 if t["T_LIFEPOINTS"] == 0:
787 def standing_on_fluid(t):
788 if world_db["MAP"][t["pos"]] == ord("0") and \
789 world_db["wetmap"][t["pos"]] > ord("0"):
794 def thing_action_id(name):
795 return [taid for taid in world_db["ThingActions"]
796 if world_db["ThingActions"][taid]
797 ["TA_NAME"] == name][0]
799 t["T_COMMAND"] = thing_action_id("wait")
803 "safe_pee": (world_db["terrain_fullness"](t["pos"]) * t["T_BLADDER"]) / 4,
804 "safe_drop": (world_db["terrain_fullness"](t["pos"]) * t["T_BOWEL"]) / 4,
805 "food": 33 - t["T_STOMACH"],
806 "fluid_certain": 33 - t["T_KIDNEY"],
807 "fluid_potential": 32 - t["T_KIDNEY"],
810 from operator import itemgetter
811 needs = sorted(needs.items(), key=itemgetter(1,0))
815 if need[0] == "fix_cracks":
816 if world_db["MAP"][t["pos"]] == ord("-") and \
817 t["T_BOWEL"] > 0 and \
818 world_db["terrain_fullness"](t["pos"]) <= 3:
819 t["T_COMMAND"] = thing_action_id("drop")
821 elif world_db["get_dir_to_target"](t, "crack"):
823 if need[0] in {"fluid_certain", "fluid_potential"}:
824 if standing_on_fluid(t):
825 t["T_COMMAND"] = thing_action_id("drink")
827 elif t["T_BLADDER"] > 0 and \
828 world_db["MAP"][t["pos"]] == ord("0"):
829 t["T_COMMAND"] = thing_action_id("pee")
831 elif need[0] in {"safe_pee", "safe_drop"}:
832 action_name = need[0][len("safe_"):]
833 if world_db["terrain_fullness"](t["pos"]) <= 3:
834 t["T_COMMAND"] = thing_action_id(action_name)
836 test = world_db["get_dir_to_target"](t, "space")
840 elif world_db["terrain_fullness"](t["pos"]) < 5:
841 t["T_COMMAND"] = thing_action_id(action_name)
843 if t["T_STOMACH"] < 32 and \
844 world_db["get_dir_to_target"](t, "food")[0]:
847 if need[0] in {"fluid_certain", "fluid_potential", "food"}:
848 if world_db["get_dir_to_target"](t, need[0])[0]:
850 elif world_db["get_dir_to_target"](t, "hunt")[0]:
852 elif need[0] != "food" and t["T_STOMACH"] < 32 and \
853 world_db["get_dir_to_target"](t, "food")[0]:
855 elif world_db["get_dir_to_target"](t, need[0])[0]:
860 from server.config.io import io_db
861 io_db["worldstate_write_order"] += [["T_STOMACH", "player_int"]]
862 io_db["worldstate_write_order"] += [["T_KIDNEY", "player_int"]]
863 io_db["worldstate_write_order"] += [["T_BOWEL", "player_int"]]
864 io_db["worldstate_write_order"] += [["T_BLADDER", "player_int"]]
865 io_db["worldstate_write_order"] += [[write_wetmap, "func"]]
866 io_db["worldstate_write_order"] += [[write_soundmap, "func"]]
867 io_db["worldstate_write_order"] += [["GRACE", "world_int"]]
868 import server.config.world_data
869 server.config.world_data.symbols_hide = "345"
870 server.config.world_data.symbols_passable = "012-+*&$"
871 server.config.world_data.thing_defaults["T_STOMACH"] = 16
872 server.config.world_data.thing_defaults["T_BOWEL"] = 0
873 server.config.world_data.thing_defaults["T_KIDNEY"] = 16
874 server.config.world_data.thing_defaults["T_BLADDER"] = 0
875 world_db["soundmap"] = bytearray(b"0" * world_db["MAP_LENGTH"] ** 2)
876 world_db["wetmap"] = bytearray(b"0" * world_db["MAP_LENGTH"] ** 2)
877 if not "NEW_SPAWN" in world_db:
878 world_db["NEW_SPAWN"] = 0
879 if not "HUMIDITY" in world_db:
880 world_db["HUMIDITY"] = 0
881 if not "GRACE" in world_db:
882 world_db["GRACE"] = 0
883 io_db["hook_save"] = save_maps
884 import server.config.make_world_helpers
885 server.config.make_world_helpers.make_map = make_map
886 from server.config.commands import commands_db
887 commands_db["THINGS_HERE"] = (2, True, lambda x, y: None)
888 commands_db["HELP"] = (1, False, command_help)
889 commands_db["ai"] = (0, False, command_ai)
890 commands_db["move"] = (1, False, play_move)
891 commands_db["eat"] = (1, False, play_move)
892 commands_db["wait"] = (0, False, play_wait)
893 commands_db["drop"] = (0, False, play_drop)
894 commands_db["drink"] = (0, False, play_drink)
895 commands_db["pee"] = (0, False, play_pee)
896 commands_db["use"] = (1, False, lambda x: None)
897 commands_db["pickup"] = (0, False, lambda: None)
898 commands_db["GRACE"] = (1, False, setter(None, "GRACE", 0, 255))
899 commands_db["NEW_SPAWN"] = (1, False, setter(None, "NEW_SPAWN", 0, 255))
900 commands_db["HUMIDITY"] = (1, False, setter(None, "HUMIDITY", 0, 65535))
901 commands_db["T_STOMACH"] = (1, False, setter("Thing", "T_STOMACH", 0, 255))
902 commands_db["T_KIDNEY"] = (1, False, setter("Thing", "T_KIDNEY", 0, 255))
903 commands_db["T_BOWEL"] = (1, False, setter("Thing", "T_BOWEL", 0, 255))
904 commands_db["T_BLADDER"] = (1, False, setter("Thing", "T_BLADDER", 0, 255))
905 commands_db["WETMAP"] = (2, False, wetmapset)
906 commands_db["SOUNDMAP"] = (2, False, soundmapset)
907 from server.actions import actor_wait
908 import server.config.actions
909 server.config.actions.action_db = {
910 "actor_wait": actor_wait,
911 "actor_move": actor_move,
912 "actor_drop": actor_drop,
913 "actor_drink": actor_drink,
914 "actor_pee": actor_pee,
915 "actor_eat": actor_eat,
918 strong_write(io_db["file_out"], "PLUGIN TheCrawlingEater\n")