5 #include "keybindings.h"
7 #include "map_objects.h"
8 #include "map_object_actions.h"
11 #include "yx_uint16.h"
13 extern uint16_t rrand(char use_seed, uint32_t new_seed) {
14 // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
18 seed = ((seed * 1103515245) + 12345) % 2147483648; // Values as recommended by POSIX.1-2001 (see rand(3)).
19 return (seed / 65536); } // Ignore least significant 16 bits (they are less random).
21 extern void update_log (struct World * world, char * text) {
22 // Update log by appending text, or by appending a "." if text is the same as the last one.
23 static char * last_msg;
25 last_msg = calloc(1, sizeof(char));
27 uint16_t len_old = strlen(world->log);
28 if (0 == strcmp(last_msg, text)) {
29 uint16_t len_whole = len_old + 1;
30 new_text = calloc(len_whole + 1, sizeof(char));
31 memcpy(new_text, world->log, len_old);
32 memcpy(new_text + len_old, ".", 1); }
34 uint16_t len_new = strlen(text);
35 uint16_t len_whole = len_old + len_new + 1;
36 new_text = calloc(len_whole, sizeof(char));
37 memcpy(new_text, world->log, len_old);
38 memcpy(new_text + len_old, text, len_new);
39 last_msg = calloc(len_new + 1, sizeof(char));
40 memcpy(last_msg, text, len_new); }
42 world->log = new_text; }
44 extern uint16_t center_offset (uint16_t pos, uint16_t mapsize, uint16_t framesize) {
45 // Return the offset for display of a map inside a frame centered on pos.
47 if (mapsize > framesize) {
48 if (pos > framesize / 2) {
49 if (pos < mapsize - (framesize / 2))
50 offset = pos - (framesize / 2);
52 offset = mapsize - framesize; } }
55 extern void turn_over (struct World * world, char action) {
56 // Record action in game record file, increment turn and move enemy.
57 if (1 == world->interactive) {
58 FILE * file = fopen("record", "a");
62 rrand(1, world->seed * world->turn);
63 struct Monster * monster;
64 for (monster = world->monster; monster != 0; monster = monster->map_obj.next)
65 move_monster(world, monster); }
67 extern void save_game(struct World * world) {
68 // Save game data to game file.
69 FILE * file = fopen("savefile", "w");
70 write_uint32_bigendian(world->seed, file);
71 write_uint32_bigendian(world->turn, file);
72 write_uint16_bigendian(world->player->pos.y + 1, file);
73 write_uint16_bigendian(world->player->pos.x + 1, file);
74 fputc(world->player->hitpoints, file);
75 write_map_objects (world->monster, file, write_map_objects_monsterdata);
76 write_map_objects (world->item, file, readwrite_map_objects_dummy);
79 extern void toggle_window (struct WinMeta * win_meta, struct Win * win) {
80 // Toggle display of window win.
81 if (0 != win->frame.curses_win)
82 suspend_win(win_meta, win);
84 append_win(win_meta, win); }
86 extern void scroll_pad (struct WinMeta * win_meta, char dir) {
87 // Try to scroll pad left or right.
89 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
91 reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
93 extern void growshrink_active_window (struct WinMeta * win_meta, char change) {
94 // Grow or shrink active window horizontally or vertically by one cell size.
95 if (0 != win_meta->active) {
96 struct yx_uint16 size = win_meta->active->frame.size;
97 if (change == '-') size.y--;
98 else if (change == '+') size.y++;
99 else if (change == '_') size.x--;
100 else if (change == '*') size.x++;
101 resize_active_win (win_meta, size); } }
103 extern struct yx_uint16 find_passable_pos (struct Map * map) {
104 // Return a random passable position on map.
105 struct yx_uint16 pos;
106 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);) {
107 pos.y = rrand(0, 0) % map->size.y;
108 pos.x = rrand(0, 0) % map->size.x; }
111 extern unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta,
112 struct Win * win_keys, struct Win * win_map, struct Win * win_info,
113 struct Win * win_log) {
114 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
115 if (key == get_action_key(world->keybindings, "quit"))
117 else if (key == get_action_key(world->keybindings, "scroll pad right"))
118 scroll_pad (win_meta, '+');
119 else if (key == get_action_key(world->keybindings, "scroll pad left"))
120 scroll_pad (win_meta, '-');
121 else if (key == get_action_key(world->keybindings, "toggle keys window"))
122 toggle_window(win_meta, win_keys);
123 else if (key == get_action_key(world->keybindings, "toggle map window"))
124 toggle_window(win_meta, win_map);
125 else if (key == get_action_key(world->keybindings, "toggle info window"))
126 toggle_window(win_meta, win_info);
127 else if (key == get_action_key(world->keybindings, "toggle log window"))
128 toggle_window(win_meta, win_log);
129 else if (key == get_action_key(world->keybindings, "cycle forwards"))
130 cycle_active_win(win_meta, 'n');
131 else if (key == get_action_key(world->keybindings, "cycle backwards"))
132 cycle_active_win(win_meta, 'p');
133 else if (key == get_action_key(world->keybindings, "shift forwards"))
134 shift_active_win(win_meta, 'f');
135 else if (key == get_action_key(world->keybindings, "shift backwards"))
136 shift_active_win(win_meta, 'b');
137 else if (key == get_action_key(world->keybindings, "grow horizontally"))
138 growshrink_active_window(win_meta, '*');
139 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
140 growshrink_active_window(win_meta, '_');
141 else if (key == get_action_key(world->keybindings, "grow vertically"))
142 growshrink_active_window(win_meta, '+');
143 else if (key == get_action_key(world->keybindings, "shrink vertically"))
144 growshrink_active_window(win_meta, '-');
145 else if (key == get_action_key(world->keybindings, "save keys"))
146 save_keybindings(world);
147 else if (key == get_action_key(world->keybindings, "keys nav up"))
148 keyswin_move_selection (world, 'u');
149 else if (key == get_action_key(world->keybindings, "keys nav down"))
150 keyswin_move_selection (world, 'd');
151 else if (key == get_action_key(world->keybindings, "keys mod"))
152 keyswin_mod_key (world, win_meta);
153 else if (key == get_action_key(world->keybindings, "map up"))
154 map_scroll (world->map, NORTH, win_map->frame.size);
155 else if (key == get_action_key(world->keybindings, "map down"))
156 map_scroll (world->map, SOUTH, win_map->frame.size);
157 else if (key == get_action_key(world->keybindings, "map right"))
158 map_scroll (world->map, EAST, win_map->frame.size);
159 else if (key == get_action_key(world->keybindings, "map left"))
160 map_scroll (world->map, WEST, win_map->frame.size);
161 else if (key == get_action_key(world->keybindings, "map center player"))
162 map_center_player (world->map, world->player, win_map->frame.size);