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