9 #include "keybindings.h"
11 static uint32_t seed = 0;
14 // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
15 uint32_t a = 1103515245; // Values for a and b as recommended by POSIX.1-2001 (see man page rand(3)).
17 uint32_t mod = 2147483648;
18 seed = ((seed * a) + b) % mod;
19 return (seed / 65536); } // Ignore least significant 16 bits (they are less random).
21 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
22 // Toggle display of window win.
24 suspend_window(win_meta, win);
26 append_window(win_meta, win); }
28 void growshrink_active_window (struct WinMeta * win_meta, char change) {
29 // Grow or shrink active window horizontally or vertically by one cell size.
30 if (0 != win_meta->active) {
31 uint16_t height = win_meta->active->height;
32 uint16_t width = win_meta->active->width;
35 else if (change == '+')
37 else if (change == '_')
39 else if (change == '*')
41 resize_active_window (win_meta, height, width); } }
43 struct Map init_map () {
44 // Initialize map with some experimental start values.
50 map.cells = malloc(map.width * map.height);
53 for (y = 0; y < map.height; y++)
54 for (x = 0; x < map.width; x++) {
57 if ( 0 == ran % ((x*x) / 3 + 1)
58 || 0 == ran % ((y*y) / 3 + 1)
59 || 0 == ran % ((map.width - x - 1) * (map.width - x - 1) / 3 + 1)
60 || 0 == ran %((map.height - y - 1) * (map.height - y - 1) / 3 + 1))
62 map.cells[(y * map.width) + x] = terrain; }
65 void map_scroll (struct Map * map, char dir) {
66 // Scroll map into direction dir if possible by changing the offset.
67 if ('n' == dir && map->offset_y > 0)
71 else if ('w' == dir && map->offset_x > 0)
76 void next_turn (struct World * world) {
77 // Increment turn and move enemy.
80 char ty = world->monster->y;
81 char tx = world->monster->x;
90 if (tx == world->player->x && ty == world->player->y)
91 update_log(world, "\nThe monster hits you.");
92 else if (is_passable(world, tx, ty)) {
93 world->monster->y = ty;
94 world->monster->x = tx; } }
96 void update_log (struct World * world, char * text) {
97 // Update log with new text to be appended.
99 uint16_t len_old = strlen(world->log);
100 uint16_t len_new = strlen(text);
101 uint16_t len_whole = len_old + len_new + 1;
102 new_text = calloc(len_whole, sizeof(char));
103 memcpy(new_text, world->log, len_old);
104 memcpy(new_text + len_old, text, len_new);
106 world->log = new_text; }
108 char is_passable (struct World * world, uint16_t x, uint16_t y) {
109 // Check if coordinate on (or beyond) map is accessible to movement.
111 if (0 <= x && x < world->map->width && 0 <= y && y < world->map->height)
112 if ('.' == world->map->cells[y * world->map->width + x])
116 void move_player (struct World * world, char d) {
117 // Move player in direction d, increment turn counter and update log.
118 static char prev = 0;
121 char ty = world->player->y;
122 char tx = world->player->x;
135 if (ty == world->monster->y && tx == world->monster->x)
137 else if (is_passable(world, tx, ty)) {
139 world->player->y = ty;
140 world->player->x = tx; }
141 if (success * d == prev)
142 update_log (world, ".");
145 update_log (world, "\nYou hit the monster.");
147 char * msg = calloc(25, sizeof(char));
148 char * msg_content = "You fail to move";
150 msg_content = "You move";
151 sprintf(msg, "\n%s %s.", msg_content, dir);
152 update_log (world, msg);
157 void player_wait (struct World * world) {
158 // Make player wait one turn.
160 update_log (world, "\nYou wait."); }
162 void save_map (struct Map * map) {
163 // Save map to file "map".
164 FILE * file = fopen("map", "w");
165 fputc(map->width / CHAR_MAX, file);
166 fputc(map->width % CHAR_MAX, file);
168 for (y = 0; y < map->height; y++)
169 for (x = 0; x < map->width; x++)
170 fputc(map->cells[y * map->width + x], file);
173 struct Map load_map () {
174 // Load map from file.
175 FILE * file = fopen("map", "r");
177 map.width = (fgetc(file) * CHAR_MAX) + fgetc(file);
178 long pos = ftell(file);
184 map.cells = malloc(i * sizeof(char));
185 map.height = i / map.width;
186 fseek(file, pos, SEEK_SET);
190 map.cells[i] = (char) c;
198 init_keybindings(&world);
200 world.log = calloc(1, sizeof(char));
201 update_log (&world, "Start!");
202 struct Map map = init_map();
204 struct Player player;
207 world.player = &player;
208 struct Monster monster;
211 world.monster = &monster;
213 WINDOW * screen = initscr();
216 keypad(screen, TRUE);
218 struct WinMeta win_meta = init_win_meta(screen);
219 struct Win win_keys = init_window(&win_meta, "Keys", &world, draw_keys_win);
220 struct Win win_map = init_window(&win_meta, "Map", &world, draw_map_win);
221 struct Win win_info = init_window(&win_meta, "Info", &world, draw_info_win);
222 struct Win win_log = init_window(&win_meta, "Log", &world, draw_log_win);
226 draw_all_windows (&win_meta);
228 if (key == get_action_key(world.keybindings, "quit"))
230 else if (key == get_action_key(world.keybindings, "scroll pad right"))
231 scroll_pad (&win_meta, '+');
232 else if (key == get_action_key(world.keybindings, "scroll pad left"))
233 scroll_pad (&win_meta, '-');
234 else if (key == get_action_key(world.keybindings, "toggle keys window"))
235 toggle_window(&win_meta, &win_keys);
236 else if (key == get_action_key(world.keybindings, "toggle map window"))
237 toggle_window(&win_meta, &win_map);
238 else if (key == get_action_key(world.keybindings, "toggle info window"))
239 toggle_window(&win_meta, &win_info);
240 else if (key == get_action_key(world.keybindings, "toggle log window"))
241 toggle_window(&win_meta, &win_log);
242 else if (key == get_action_key(world.keybindings, "cycle forwards"))
243 cycle_active_window(&win_meta, 'n');
244 else if (key == get_action_key(world.keybindings, "cycle backwards"))
245 cycle_active_window(&win_meta, 'p');
246 else if (key == get_action_key(world.keybindings, "shift forwards"))
247 shift_active_window(&win_meta, 'f');
248 else if (key == get_action_key(world.keybindings, "shift backwards"))
249 shift_active_window(&win_meta, 'b');
250 else if (key == get_action_key(world.keybindings, "grow horizontally"))
251 growshrink_active_window(&win_meta, '*');
252 else if (key == get_action_key(world.keybindings, "shrink horizontally"))
253 growshrink_active_window(&win_meta, '_');
254 else if (key == get_action_key(world.keybindings, "grow vertically"))
255 growshrink_active_window(&win_meta, '+');
256 else if (key == get_action_key(world.keybindings, "shrink vertically"))
257 growshrink_active_window(&win_meta, '-');
258 else if (key == get_action_key(world.keybindings, "save keys"))
259 save_keybindings(&world);
260 else if (key == get_action_key(world.keybindings, "keys nav up"))
261 keyswin_move_selection (&world, 'u');
262 else if (key == get_action_key(world.keybindings, "keys nav down"))
263 keyswin_move_selection (&world, 'd');
264 else if (key == get_action_key(world.keybindings, "keys mod"))
265 keyswin_mod_key (&world, &win_meta);
266 else if (key == get_action_key(world.keybindings, "load map"))
268 else if (key == get_action_key(world.keybindings, "save map"))
270 else if (key == get_action_key(world.keybindings, "map up"))
271 map_scroll (&map, 'n');
272 else if (key == get_action_key(world.keybindings, "map down"))
273 map_scroll (&map, 's');
274 else if (key == get_action_key(world.keybindings, "map right"))
275 map_scroll (&map, 'e');
276 else if (key == get_action_key(world.keybindings, "map left"))
277 map_scroll (&map, 'w');
278 else if (key == get_action_key(world.keybindings, "player down"))
279 move_player(&world, 's');
280 else if (key == get_action_key(world.keybindings, "player up"))
281 move_player(&world, 'n');
282 else if (key == get_action_key(world.keybindings, "player right"))
283 move_player(&world, 'e');
284 else if (key == get_action_key(world.keybindings, "player left"))
285 move_player(&world, 'w');
286 else if (key == get_action_key(world.keybindings, "wait") )
287 player_wait (&world); }
290 for (key = 0; key <= world.keyswindata->max; key++)
291 free(world.keybindings[key].name);
292 free(world.keybindings);
293 free(world.keyswindata);