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