home · contact · privacy
TCE: Reduce turn_over(), lifepoint manipulation to minimum.
[plomrogue] / plugins / server / TheCrawlingEater.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
8
9 def make_map():
10     from server.make_map import new_pos, is_neighbor
11     from server.utils import rand
12     world_db["MAP"] = bytearray(b'X' * (world_db["MAP_LENGTH"] ** 2))
13     length = world_db["MAP_LENGTH"]
14     add_half_width = (not (length % 2)) * int(length / 2)
15     world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord("#")
16     while (1):
17         y, x, pos = new_pos()
18         if "X" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "#"):
19             if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
20                 break
21             world_db["MAP"][pos] = ord("#")
22     n_trees = int((length ** 2) / 16)
23     i_trees = 0
24     while (i_trees <= n_trees):
25         single_allowed = rand.next() % 32
26         y, x, pos = new_pos()
27         if "#" == chr(world_db["MAP"][pos]) \
28                 and ((not single_allowed) or is_neighbor((y, x), ".")):
29             world_db["MAP"][pos] = ord(".")
30             i_trees += 1
31
32
33 def actor_move_attempts_hook(t, move_result, pos):
34     from server.utils import rand
35     if ord("#") == world_db["MAP"][pos] and 0 == int(rand.next() % 5):
36         world_db["MAP"][pos] = ord(".")
37         return True
38     return False
39
40
41 def turn_over():
42     from server.ai import ai
43     from server.config.actions import action_db
44     from server.config.misc import calc_effort
45     from server.update_map_memory import update_map_memory
46     from server.io import try_worldstate_update
47     from server.config.io import io_db
48     while world_db["Things"][0]["T_LIFEPOINTS"]:
49         for tid in [tid for tid in world_db["Things"]]:
50             if not tid in world_db["Things"]:
51                 continue
52             Thing = world_db["Things"][tid]
53             if Thing["T_LIFEPOINTS"]:
54                 if not Thing["T_COMMAND"]:
55                     update_map_memory(Thing)
56                     if 0 == tid:
57                         return
58                     ai(Thing)
59                 if Thing["T_LIFEPOINTS"]:
60                     Thing["T_PROGRESS"] += 1
61                     taid = [a for a in world_db["ThingActions"]
62                               if a == Thing["T_COMMAND"]][0]
63                     ThingAction = world_db["ThingActions"][taid]
64                     effort = calc_effort(ThingAction, Thing)
65                     if Thing["T_PROGRESS"] == effort:
66                         action = action_db["actor_" + ThingAction["TA_NAME"]]
67                         action(Thing)
68                         Thing["T_COMMAND"] = 0
69                         Thing["T_PROGRESS"] = 0
70         world_db["TURN"] += 1
71         io_db["worldstate_updateable"] = True
72         try_worldstate_update()
73 world_db["turn_over"] = turn_over
74
75
76 def play_move(str_arg):
77     """Try "move" as player's T_COMMAND, str_arg as T_ARGUMENT / direction."""
78     if action_exists("move") and world_db["WORLD_ACTIVE"]:
79         from server.config.world_data import directions_db, symbols_passable
80         t = world_db["Things"][0]
81         if not str_arg in directions_db:
82             print("Illegal move direction string.")
83             return
84         d = ord(directions_db[str_arg])
85         from server.utils import mv_yx_in_dir_legal
86         move_result = mv_yx_in_dir_legal(chr(d), t["T_POSY"], t["T_POSX"])
87         if 1 == move_result[0]:
88             pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
89             if ord("#") == world_db["MAP"][pos]:
90                 log("You EAT.")
91                 world_db["Things"][0]["T_ARGUMENT"] = d
92                 world_db["set_command"]("move")
93                 return
94             if chr(world_db["MAP"][pos]) in symbols_passable:
95                 world_db["Things"][0]["T_ARGUMENT"] = d
96                 world_db["set_command"]("move")
97                 return
98         log("You CAN'T eat your way through there.")
99
100
101 def command_ai():
102     """Call ai() on player Thing, then turn_over()."""
103     from server.ai import ai
104     if world_db["WORLD_ACTIVE"]:
105         ai(world_db["Things"][0])
106         world_db["turn_over"]()
107
108
109 def set_command(action):
110     """Set player's T_COMMAND, then call turn_over()."""
111     tid = [x for x in world_db["ThingActions"]
112           if world_db["ThingActions"][x]["TA_NAME"] == action][0]
113     world_db["Things"][0]["T_COMMAND"] = tid
114     world_db["turn_over"]()
115 world_db["set_command"] = set_command
116
117
118 def play_wait():
119     """Try "wait" as player's T_COMMAND."""
120     if world_db["WORLD_ACTIVE"]:
121         world_db["set_command"]("wait")
122
123
124 import server.config.actions
125 server.config.actions.actor_move_attempts_hook = actor_move_attempts_hook
126 import server.config.world_data
127 server.config.world_data.symbols_hide += "#"
128 import server.config.make_world_helpers
129 server.config.make_world_helpers.make_map = make_map
130 from server.config.commands import commands_db
131 commands_db["ai"] = (0, False, command_ai)
132 commands_db["move"] = (1, False, play_move)
133 commands_db["wait"] = (0, False, play_wait)
134 commands_db["drop"] = (1, False, lambda x: None)
135 commands_db["use"] = (1, False, lambda x: None)
136 commands_db["pickup"] = (0, False, lambda: None)
137
138 strong_write(io_db["file_out"], "PLUGIN TheCrawlingEater\n")