home · contact · privacy
Plugin: Add scenario file.
[plomrogue] / server / world.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 from server.io import log
8 from server.utils import rand
9 from server.utils import id_setter
10
11
12 def try_healing(t):
13     """If t's HP < max, increment them if well-nourished, maybe waiting."""
14     if t["T_LIFEPOINTS"] < \
15        world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]:
16         wait_id = [id for id in world_db["ThingActions"]
17                       if world_db["ThingActions"][id]["TA_NAME"] == "wait"][0]
18         wait_divider = 8 if t["T_COMMAND"] == wait_id else 1
19         testval = int(abs(t["T_SATIATION"]) / wait_divider)
20         if (testval <= 1 or 1 == (rand.next() % testval)):
21             t["T_LIFEPOINTS"] += 1
22             if t == world_db["Things"][0]:
23                 log("You HEAL.")
24
25
26 def hunger_per_turn(type_id):
27     """The amount of satiation score lost per turn for things of given type."""
28     import math
29     return int(math.sqrt(world_db["ThingTypes"][type_id]["TT_LIFEPOINTS"]))
30
31
32 def hunger(t):
33     """Decrement t's satiation,dependent on it trigger lifepoint dec chance."""
34     from server.config.misc import decrement_lifepoints_func
35     if t["T_SATIATION"] > -32768:
36         t["T_SATIATION"] -= hunger_per_turn(t["T_TYPE"])
37     if 0 != t["T_SATIATION"] and 0 == int(rand.next() / abs(t["T_SATIATION"])):
38         if t == world_db["Things"][0]:
39             if t["T_SATIATION"] < 0:
40                 log("You SUFFER from hunger.")
41             else:
42                 log("You SUFFER from over-eating.")
43         decrement_lifepoints_func(t)
44
45
46 def set_world_inactive():
47     """Set world_db["WORLD_ACTIVE"] to 0 and remove worldstate file."""
48     from server.io import safely_remove_worldstate_file
49     safely_remove_worldstate_file()
50     world_db["WORLD_ACTIVE"] = 0
51
52
53 def turn_over():
54     """Run game world and its inhabitants until new player input expected."""
55     from server.config.actions import action_db, ai_func
56     from server.config.misc import thingproliferation_func, calc_effort_func
57     from server.update_map_memory import update_map_memory
58     id = 0
59     whilebreaker = False
60     while world_db["Things"][0]["T_LIFEPOINTS"]:
61         proliferable_map = world_db["MAP"][:]
62         for id in [id for id in world_db["Things"]
63                    if not world_db["Things"][id]["carried"]]:
64             y = world_db["Things"][id]["T_POSY"]
65             x = world_db["Things"][id]["T_POSX"]
66             proliferable_map[y * world_db["MAP_LENGTH"] + x] = ord('X')
67         for id in [id for id in world_db["Things"]]:  # Only what's from start!
68             if not id in world_db["Things"] or \
69                world_db["Things"][id]["carried"]:   # May have been consumed or
70                 continue                            # picked up during turn …
71             Thing = world_db["Things"][id]
72             if Thing["T_LIFEPOINTS"]:
73                 if not Thing["T_COMMAND"]:
74                     update_map_memory(Thing)
75                     if 0 == id:
76                         whilebreaker = True
77                         break
78                     ai_func(Thing)
79                 try_healing(Thing)
80                 hunger(Thing)
81                 if Thing["T_LIFEPOINTS"]:
82                     Thing["T_PROGRESS"] += 1
83                     taid = [a for a in world_db["ThingActions"]
84                               if a == Thing["T_COMMAND"]][0]
85                     ThingAction = world_db["ThingActions"][taid]
86                     effort = calc_effort_func(ThingAction, Thing)
87                     if Thing["T_PROGRESS"] == effort:
88                         action = action_db["actor_" + ThingAction["TA_NAME"]]
89                         action(Thing)
90                         Thing["T_COMMAND"] = 0
91                         Thing["T_PROGRESS"] = 0
92             thingproliferation_func(Thing, proliferable_map)
93         if whilebreaker:
94             break
95         world_db["TURN"] += 1