1 from server.io import log, strong_write
2 from server.config.world_data import world_db, symbols_passable, directions_db
3 from server.utils import mv_yx_in_dir_legal, rand, id_setter
4 from server.config.io import io_db
5 from server.new_thing import new_Thing
8 from server.update_map_memory import update_map_memory
9 from server.config.misc import make_map_func
10 from server.utils import libpr
12 def free_pos(plant=False):
15 err = "Space to put thing on too hard to find. Map too small?"
17 y = rand.next() % world_db["MAP_LENGTH"]
18 x = rand.next() % world_db["MAP_LENGTH"]
19 pos = y * world_db["MAP_LENGTH"] + x;
21 and "." == chr(world_db["MAP"][pos])) \
22 or ":" == chr(world_db["MAP"][pos]):
27 pos_clear = (0 == len([id for id in world_db["Things"]
28 if world_db["Things"][id]["T_LIFEPOINTS"]
29 if world_db["Things"][id]["T_POSY"] == y
30 if world_db["Things"][id]["T_POSX"] == x]))
36 if world_db["MAP_LENGTH"] < 1:
37 print("Ignoring: No map length >= 1 defined.")
39 libpr.set_maplength(world_db["MAP_LENGTH"])
40 player_will_be_generated = False
41 playertype = world_db["PLAYER_TYPE"]
42 for ThingType in world_db["ThingTypes"]:
43 if playertype == ThingType:
44 if 0 < world_db["ThingTypes"][ThingType]["TT_START_NUMBER"]:
45 player_will_be_generated = True
47 if not player_will_be_generated:
48 print("Ignoring: No player type with start number >0 defined.")
51 for ThingAction in world_db["ThingActions"]:
52 if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
55 print("Ignoring beyond SEED_MAP: " +
56 "No thing action with name 'wait' defined.")
58 if not world_db["SLIPPERS"] in world_db["ThingTypes"]:
59 print("Ignoring: No valid SLIPPERS set.")
61 if not world_db["PLANT_0"] in world_db["ThingTypes"]:
62 print("Ignoring: No valid PLANT_0 set.")
64 #for name in specials:
65 # if world_db[name] not in world_db["ThingTypes"]:
66 # print("Ignoring: No valid " + name + " set.")
68 world_db["Things"] = {}
70 world_db["WORLD_ACTIVE"] = 1
72 for i in range(world_db["ThingTypes"][playertype]["TT_START_NUMBER"]):
73 id = id_setter(-1, "Things")
74 world_db["Things"][id] = new_Thing(playertype, free_pos())
75 if not world_db["Things"][0]["fovmap"]:
76 empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
77 world_db["Things"][0]["fovmap"] = empty_fovmap
78 update_map_memory(world_db["Things"][0])
79 for type in world_db["ThingTypes"]:
80 for i in range(world_db["ThingTypes"][type]["TT_START_NUMBER"]):
81 if type != playertype:
82 id = id_setter(-1, "Things")
83 plantness = world_db["ThingTypes"][type]["TT_PROLIFERATE"]
84 world_db["Things"][id] = new_Thing(type, free_pos(plantness))
85 strong_write(io_db["file_out"], "NEW_WORLD\n")
87 def thingproliferation(t, prol_map):
88 from server.new_thing import new_Thing
89 global directions_db, mv_yx_in_dir_legal
90 prolscore = world_db["ThingTypes"][t["T_TYPE"]]["TT_PROLIFERATE"]
92 (world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"] == 0 or
93 t["T_LIFEPOINTS"] >= 0.9 *
94 world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]) \
96 (1 == prolscore or 1 == (rand.next() % prolscore)):
98 for dir in [directions_db[key] for key in directions_db]:
99 mv_result = mv_yx_in_dir_legal(dir, t["T_POSY"], t["T_POSX"])
100 pos = mv_result[1] * world_db["MAP_LENGTH"] + mv_result[2]
101 if mv_result[0] and \
102 (ord(":") == prol_map[pos]
103 or (world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]
104 and ord(".") == prol_map[pos])):
105 candidates.append((mv_result[1], mv_result[2]))
107 i = rand.next() % len(candidates)
108 id = id_setter(-1, "Things")
109 newT = new_Thing(t["T_TYPE"], (candidates[i][0], candidates[i][1]))
110 world_db["Things"][id] = newT
111 if (world_db["FAVOR_STAGE"] > 0
112 and t["T_TYPE"] == world_db["PLANT_0"]):
113 world_db["GOD_FAVOR"] += 5
114 #elif t["T_TYPE"] == world_db["PLANT_1"];
115 # world_db["GOD_FAVOR"] += 25
116 #elif world_db["FAVOR_STAGE"] >= 4 and \
117 # t["T_TYPE"] == world_db["ANIMAL_1"]:
118 # log("The Island God SMILES upon a new-born bear baby.")
119 # world_db["GOD_FAVOR"] += 750
124 def is_neighbor(coordinates, type):
127 length = world_db["MAP_LENGTH"]
129 diag_west = x + (ind > 0)
130 diag_east = x + (ind < (length - 1))
131 pos = (y * length) + x
132 if (y > 0 and diag_east
133 and type == chr(world_db["MAP"][pos - length + ind])) \
135 and type == chr(world_db["MAP"][pos + 1])) \
136 or (y < (length - 1) and diag_east
137 and type == chr(world_db["MAP"][pos + length + ind])) \
138 or (y > 0 and diag_west
139 and type == chr(world_db["MAP"][pos - length - (not ind)])) \
141 and type == chr(world_db["MAP"][pos - 1])) \
142 or (y < (length - 1) and diag_west
143 and type == chr(world_db["MAP"][pos + length - (not ind)])):
147 world_db["MAP"] = bytearray(b'~' * (world_db["MAP_LENGTH"] ** 2))
148 length = world_db["MAP_LENGTH"]
149 add_half_width = (not (length % 2)) * int(length / 2)
150 world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord(".")
152 y = rand.next() % length
153 x = rand.next() % length
154 pos = (y * length) + x
155 if "~" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "."):
156 if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
158 world_db["MAP"][pos] = ord(".")
159 n_trees = int((length ** 2) / 16)
161 while (i_trees <= n_trees):
162 single_allowed = rand.next() % 32
163 y = rand.next() % length
164 x = rand.next() % length
165 pos = (y * length) + x
166 if "." == chr(world_db["MAP"][pos]) \
167 and ((not single_allowed) or is_neighbor((y, x), "X")):
168 world_db["MAP"][pos] = ord("X")
170 n_colons = int((length ** 2) / 16)
172 while (i_colons <= n_colons):
173 single_allowed = rand.next() % 256
174 y = rand.next() % length
175 x = rand.next() % length
176 pos = (y * length) + x
177 if ("." == chr(world_db["MAP"][pos])
178 and ((not single_allowed) or is_neighbor((y, x), ":"))):
179 world_db["MAP"][pos] = ord(":")
182 while not altar_placed:
183 y = rand.next() % length
184 x = rand.next() % length
185 pos = (y * length) + x
186 if (("." == chr(world_db["MAP"][pos]
187 or ":" == chr(world_db["MAP"][pos]))
188 and not is_neighbor((y, x), "X"))):
189 world_db["MAP"][pos] = ord("_")
190 world_db["altar"] = (y, x)
194 from server.ai import get_dir_to_target, get_inventory_slot_to_consume, \
196 t["T_COMMAND"] = [id for id in world_db["ThingActions"]
197 if world_db["ThingActions"][id]["TA_NAME"] == "wait"][0]
198 eating = len([id for id in world_db["ThingActions"]
199 if world_db["ThingActions"][id]["TA_NAME"] == "use"]) > 0
200 picking = len([id for id in world_db["ThingActions"]
201 if world_db["ThingActions"][id]["TA_NAME"] == "pickup"]) > 0
202 if eating and picking:
203 if get_dir_to_target(t, "f"):
205 sel = get_inventory_slot_to_consume(t)
207 t["T_COMMAND"] = [id for id in world_db["ThingActions"]
208 if world_db["ThingActions"][id]["TA_NAME"]
210 t["T_ARGUMENT"] = sel
211 elif standing_on_food(t) and (len(t["T_CARRIES"]) <
212 world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]):
213 t["T_COMMAND"] = [id for id in world_db["ThingActions"]
214 if world_db["ThingActions"][id]["TA_NAME"]
217 going_to_known_food_spot = get_dir_to_target(t, "c")
218 if not going_to_known_food_spot:
219 aiming_for_walking_food = get_dir_to_target(t, "a")
220 if not aiming_for_walking_food:
221 get_dir_to_target(t, "s")
224 from server.ai import eat_vs_hunger_threshold
225 used_slots = len(t["T_CARRIES"])
226 if used_slots < world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]:
227 ids = [id for id in world_db["Things"] if world_db["Things"][id] != t
228 if not world_db["Things"][id]["carried"]
229 if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
230 if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]
233 eat_cost = eat_vs_hunger_threshold(t["T_TYPE"])
235 tid = world_db["Things"][iid]["T_TYPE"]
236 if lowest_tid == -1 or tid < lowest_tid:
237 if (t != world_db["Things"][0] and
238 (world_db["ThingTypes"][tid]["TT_TOOL"] != "food"
239 or (world_db["ThingTypes"][tid]["TT_TOOLPOWER"]
244 world_db["Things"][id]["carried"] = True
245 ty = world_db["Things"][id]["T_TYPE"]
246 if (t != world_db["Things"][0]
247 and world_db["Things"][id]["T_PLAYERDROP"]
248 and world_db["ThingTypes"][ty]["TT_TOOL"] == "food"):
249 score = int(world_db["ThingTypes"][ty]["TT_TOOLPOWER"] / 32)
250 world_db["GOD_FAVOR"] += score
251 world_db["Things"][id]["T_PLAYERDROP"] = 0
252 t["T_CARRIES"].append(id)
253 if t == world_db["Things"][0]:
254 log("You PICK UP an object.")
255 elif t == world_db["Things"][0]:
256 log("Can't pick up object: No storage room to carry more.")
260 """Make t rop Thing from inventory to ground indexed by T_ARGUMENT."""
261 if len(t["T_CARRIES"]):
262 id = t["T_CARRIES"][t["T_ARGUMENT"]]
263 t["T_CARRIES"].remove(id)
264 world_db["Things"][id]["carried"] = False
265 if t == world_db["Things"][0]:
266 log("You DROP an object.")
267 world_db["Things"][id]["T_PLAYERDROP"] = 1
271 if len(t["T_CARRIES"]):
272 id = t["T_CARRIES"][t["T_ARGUMENT"]]
273 type = world_db["Things"][id]["T_TYPE"]
274 if type == world_db["SLIPPERS"]:
275 if t == world_db["Things"][0]:
276 log("You use the " + world_db["ThingTypes"][type]["TT_NAME"]
277 + ". It glows in wondrous colors, and emits a sound as if "
278 + "from a dying cat. The Island God laughs.\n")
279 t["T_LIFEPOINTS"] = 1
280 from server.config.misc import decrement_lifepoints_func
281 decrement_lifepoints_func(t)
282 elif world_db["ThingTypes"][type]["TT_TOOL"] == "food":
283 t["T_CARRIES"].remove(id)
284 del world_db["Things"][id]
285 t["T_SATIATION"] += world_db["ThingTypes"][type]["TT_TOOLPOWER"]
286 if t == world_db["Things"][0]:
287 log("You CONSUME this object.")
288 elif t == world_db["Things"][0]:
289 log("You try to use this object, but FAIL.")
291 def decrement_lifepoints(t):
292 t["T_LIFEPOINTS"] -= 1
293 _id = [_id for _id in world_db["Things"] if world_db["Things"][_id] == t][0]
294 if 0 == t["T_LIFEPOINTS"]:
295 sadness = world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]
296 for id in t["T_CARRIES"]:
297 t["T_CARRIES"].remove(id)
298 world_db["Things"][id]["T_POSY"] = t["T_POSY"]
299 world_db["Things"][id]["T_POSX"] = t["T_POSX"]
300 world_db["Things"][id]["carried"] = False
301 t["T_TYPE"] = world_db["ThingTypes"][t["T_TYPE"]]["TT_CORPSE_ID"]
302 if world_db["Things"][0] == t:
303 t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
305 log("See README on how to start over.")
308 t["T_MEMMAP"] = False
309 t["T_MEMDEPTHMAP"] = False
317 from server.new_thing import new_Thing
318 if world_db["FAVOR_STAGE"] > 9000:
319 log("You step on a soul-less slab of stone.")
321 log("YOU ENTER SACRED GROUND.")
322 if world_db["FAVOR_STAGE"] == 0:
323 world_db["FAVOR_STAGE"] = 1
324 log("The Island God speaks to you: \"I don't trust you. You intrud"
325 + "e on the island's affairs. I think you're a nuisance at be"
326 + "st, and a danger to my children at worst. I will give you "
327 + "a chance to lighten my mood, however: For a while now, I'v"
328 + "e been trying to spread the plant "
329 + world_db["ThingTypes"][world_db["PLANT_0"]]["TT_NAME"]
331 + world_db["ThingTypes"][world_db["PLANT_0"]]["TT_SYMBOL"]
332 + "\"). I have not been very successful so far. Maybe you can"
333 + " make yourself useful there. I will count each further "
334 + world_db["ThingTypes"][world_db["PLANT_0"]]["TT_NAME"]
335 + " that grows to your favor.\"")
336 elif world_db["GOD_FAVOR"] > 150:
337 world_db["FAVOR_STAGE"] = 9001
338 log("The Island God speaks to you: \"You have proven yourself wort"
339 + "hy of my respect. You were a good citizen to the island, a"
340 + "nd sometimes a better steward to its inhabitants than me. "
341 + "The island shall miss you when you leave. But you have ear"
342 + "ned the right to do so. Take this "
343 + world_db["ThingTypes"][world_db["SLIPPERS"]]["TT_NAME"]
344 + " and USE it when you please. It will take you to where you"
345 + " came from. (But do feel free to stay here as long as you "
347 id = id_setter(-1, "Things")
348 world_db["Things"][id] = new_Thing(world_db["SLIPPERS"],
351 from server.config.world_data import symbols_passable
352 from server.build_fov_map import build_fov_map
353 from server.config.misc import decrement_lifepoints_func
355 move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
356 t["T_POSY"], t["T_POSX"])
357 if 1 == move_result[0]:
358 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
359 hitted = [id for id in world_db["Things"]
360 if world_db["Things"][id] != t
361 if world_db["Things"][id]["T_LIFEPOINTS"]
362 if world_db["Things"][id]["T_POSY"] == move_result[1]
363 if world_db["Things"][id]["T_POSX"] == move_result[2]]
366 if t == world_db["Things"][0]:
367 hitted_type = world_db["Things"][hit_id]["T_TYPE"]
368 hitted_name = world_db["ThingTypes"][hitted_type]["TT_NAME"]
369 log("You WOUND " + hitted_name + ".")
370 world_db["GOD_FAVOR"] -= 1
372 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
373 log(hitter_name +" WOUNDS you.")
374 test = decrement_lifepoints_func(world_db["Things"][hit_id])
375 if test and t == world_db["Things"][0]:
376 world_db["GOD_FAVOR"] -= test
378 passable = chr(world_db["MAP"][pos]) in symbols_passable
379 dir = [dir for dir in directions_db
380 if directions_db[dir] == chr(t["T_ARGUMENT"])][0]
382 t["T_POSY"] = move_result[1]
383 t["T_POSX"] = move_result[2]
384 for id in t["T_CARRIES"]:
385 world_db["Things"][id]["T_POSY"] = move_result[1]
386 world_db["Things"][id]["T_POSX"] = move_result[2]
388 if t == world_db["Things"][0]:
389 log("You MOVE " + dir + ".")
390 if (move_result[1] == world_db["altar"][0] and
391 move_result[2] == world_db["altar"][1]):
394 def command_ttid(id_string):
395 id = id_setter(id_string, "ThingTypes", command_ttid)
397 world_db["ThingTypes"][id] = {
402 "TT_START_NUMBER": 0,
409 def command_worldactive(worldactive_string):
410 val = integer_test(worldactive_string, 0, 1)
412 if 0 != world_db["WORLD_ACTIVE"]:
416 print("World already active.")
417 elif 0 == world_db["WORLD_ACTIVE"]:
419 for ThingAction in world_db["ThingActions"]:
420 if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
423 player_exists = False
424 for Thing in world_db["Things"]:
430 pos = world_db["MAP"].find(b'_')
432 y = int(pos / world_db["MAP_LENGTH"])
433 x = pos % world_db["MAP_LENGTH"]
434 world_db["altar"] = (y, x)
436 valid_slippers = world_db["SLIPPERS"] in world_db["ThingTypes"]
437 valid_plant0 = world_db["PLANT_0"] in world_db["ThingTypes"]
438 if altar_found and wait_exists and player_exists and \
439 world_db["MAP"] and valid_slippers and valid_plant0:
440 for id in world_db["Things"]:
441 if world_db["Things"][id]["T_LIFEPOINTS"]:
442 build_fov_map(world_db["Things"][id])
444 update_map_memory(world_db["Things"][id], False)
445 if not world_db["Things"][0]["T_LIFEPOINTS"]:
446 empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
447 world_db["Things"][0]["fovmap"] = empty_fovmap
448 world_db["WORLD_ACTIVE"] = 1
450 print("Ignoring: Not all conditions for world activation met.")
452 def command_slippers(str_int):
453 val = integer_test(str_int, 0)
455 world_db["SLIPPERS"] = val
456 if world_db["WORLD_ACTIVE"] and \
457 world_db["SLIPPERS"] not in world_db["ThingTypes"]:
458 world_db["WORLD_ACTIVE"] = 0
459 print("SLIPPERS matches no known ThingTypes, deactivating world.")
461 def command_plant0(str_int):
462 val = integer_test(str_int, 0)
464 world_db["PLANT_0"] = val
465 if world_db["WORLD_ACTIVE"] and \
466 world_db["PLANT_0"] not in world_db["ThingTypes"]:
467 world_db["WORLD_ACTIVE"] = 0
468 print("PLANT_0 matches no known ThingTypes, deactivating world.")
470 def play_use(str_arg):
471 """Try "use" as player's T_COMMAND, int(str_arg) as T_ARGUMENT / slot."""
472 if action_exists("use"):
473 t = world_db["Things"][0]
474 if 0 == len(t["T_CARRIES"]):
475 log("You have NOTHING to use in your inventory.")
477 val = integer_test(str_arg, 0, 255)
478 if None != val and val < len(t["T_CARRIES"]):
479 id = t["T_CARRIES"][val]
480 type = world_db["Things"][id]["T_TYPE"]
481 if type != world_db["SLIPPERS"] and not \
482 world_db["ThingTypes"][type]["TT_TOOL"] == "food":
483 log("You CAN'T consume this thing.")
485 world_db["Things"][0]["T_ARGUMENT"] = val
488 print("Illegal inventory index.")
490 strong_write(io_db["file_out"], "PLUGIN PleaseTheIslandGod\n")
492 if not "GOD_FAVOR" in world_db:
493 world_db["GOD_FAVOR"] = 0
494 if not "FAVOR_STAGE" in world_db:
495 world_db["FAVOR_STAGE"] = 0
496 if not "SLIPPERS" in world_db:
497 world_db["SLIPPERS"] = 0
498 if not "PLANT_0" in world_db:
499 world_db["PLANT_0"] = 0
500 io_db["worldstate_write_order"] += [["GOD_FAVOR", "world_int"]]
502 import server.config.world_data
503 server.config.world_data.symbols_passable += ":_"
505 from server.config.world_data import thing_defaults
506 thing_defaults["T_PLAYERDROP"] = 0
508 import server.config.actions
509 server.config.actions.action_db["actor_move"] = actor_move
510 server.config.actions.action_db["actor_pickup"] = actor_pickup
511 server.config.actions.action_db["actor_drop"] = actor_drop
512 server.config.actions.action_db["actor_use"] = actor_use
513 server.config.actions.ai_func = ai
515 from server.config.commands import commands_db
516 commands_db["TT_ID"] = (1, False, command_ttid)
517 commands_db["GOD_FAVOR"] = (1, False, setter(None, "GOD_FAVOR", -32768, 32767))
518 commands_db["TT_STORAGE"] = (1, False, setter("ThingType", "TT_STORAGE", 0, 255))
519 commands_db["T_PLAYERDROP"] = (1, False, setter("Thing", "T_PLAYERDROP", 0, 1))
520 commands_db["WORLD_ACTIVE"] = (1, False, command_worldactive)
521 commands_db["FAVOR_STAGE"] = (1, False, setter(None, "FAVOR_STAGE", 0, 1))
522 commands_db["SLIPPERS"] = (1, False, command_slippers)
523 commands_db["PLANT_0"] = (1, False, command_plant0)
524 commands_db["use"] = (1, False, play_use)
526 import server.config.misc
527 server.config.misc.make_map_func = make_map
528 server.config.misc.thingproliferation_func = thingproliferation
529 server.config.misc.make_world = make_world
530 server.config.decrement_lifepoints_func = decrement_lifepoints