home · contact · privacy
Fix buggy healthy_addch().
[plomrogue] / client / commands.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 client.config.world_data import world_data
7 from client.io import send
8 from client.query_mapcell import query_mapcell
9
10
11 def command_quit():
12     send("QUIT")
13     raise SystemExit("Received QUIT command, forwarded to server, leaving.")
14
15
16 def command_toggle_look_mode():
17     if not world_data["look_mode"]:
18         world_data["look_mode"] = True
19     else:
20         world_data["look_mode"] = False
21         world_data["map_center"] = world_data["avatar_position"]
22         query_mapcell()
23
24
25 def command_sender(string, int_field=None):
26     def command_send():
27         int_string = ""
28         if int_field:
29             int_string = " " + str(world_data[int_field])
30         send(string + int_string)
31     return command_send
32
33
34 def command_looker(string):
35     def command_look():
36         if string == "west" \
37                 and world_data["map_center"][1] > 0:
38             world_data["map_center"][1] -= 1
39         elif string == "east" \
40                 and world_data["map_center"][1] < world_data["map_size"] - 1:
41             world_data["map_center"][1] += 1
42         else:
43             y_unevenness = world_data["map_center"][0] % 2
44             y_evenness = int(not(y_unevenness))
45             if string[6:] == "west" and \
46                     world_data["map_center"][1] > -y_unevenness:
47                 if string[:5] == "north" and world_data["map_center"][0] > 0:
48                     world_data["map_center"][0] -= 1
49                     world_data["map_center"][1] -= y_evenness
50                 if string[:5] == "south" and world_data["map_center"][0] \
51                         < world_data["map_size"] - 1:
52                     world_data["map_center"][0] += 1
53                     world_data["map_center"][1] -= y_evenness
54             elif string[6:] == "east" and world_data["map_center"][1] \
55                     < world_data["map_size"] - y_unevenness:
56                 if string[:5] == "north" and world_data["map_center"][0] > 0:
57                     world_data["map_center"][0] -= 1
58                     world_data["map_center"][1] += y_unevenness
59                 if string[:5] == "south" and world_data["map_center"][0] \
60                         < world_data["map_size"] - 1:
61                     world_data["map_center"][0] += 1
62                     world_data["map_center"][1] += y_unevenness
63         query_mapcell()
64     return command_look
65
66
67 def command_look_scroller(string):
68     def command_look_scroll():
69         from client.window_management import windows
70         from client.config.windows import windows_config
71         from client.windows import win_look
72         try:
73             i = next(i for i in range(len(windows_config))
74                         if windows_config[i]["func"] == win_look)
75         except StopIteration:
76             return
77         win_size = windows[i].size
78         if string == "up" and world_data["look_scroll"] > 0:
79             world_data["look_scroll"] -= 1
80         elif string == "down" and world_data["look_scroll"] \
81                 < len(world_data["look"]) - win_size[0]:
82             world_data["look_scroll"] += 1
83     return command_look_scroll
84
85
86 def command_inventory_selector(string):
87     def command_inventory_select():
88         if string == "up" and world_data["inventory_selection"] > 0:
89             world_data["inventory_selection"] -= 1
90         elif string == "down" and world_data["inventory_selection"] \
91                 < len(world_data["inventory"]) - 1:
92             world_data["inventory_selection"] += 1
93     return command_inventory_select