10 #include "keybindings.h"
11 #include "readwrite.h"
18 uint16_t rrand(char use_seed, uint32_t new_seed) {
19 // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
23 seed = ((seed * 1103515245) + 12345) % 2147483648; // Values as recommended by POSIX.1-2001 (see rand(3)).
24 return (seed / 65536); } // Ignore least significant 16 bits (they are less random).
26 struct Map init_map () {
27 // Initialize map with some experimental start values.
33 uint32_t size = map.size.x * map.size.y;
34 map.cells = malloc(size);
36 for (y = 0; y < map.size.y; y++)
37 for (x = 0; x < map.size.x; x++)
38 map.cells[(y * map.size.x) + x] = '~';
39 map.cells[size / 2 + (map.size.x / 2)] = '.';
40 uint32_t repeats, root, curpos;
41 for (root = 0; root * root * root < size; root++);
42 for (repeats = 0; repeats < size * root; repeats++) {
43 y = rrand(0, 0) % map.size.y;
44 x = rrand(0, 0) % map.size.x;
45 curpos = y * map.size.x + x;
46 if ('~' == map.cells[curpos] &&
47 ( (curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
48 || (curpos < map.size.x * (map.size.y-1) && '.' == map.cells[curpos + map.size.x])
49 || (curpos > 0 && curpos % map.size.x != 0 && '.' == map.cells[curpos-1])
50 || (curpos < (map.size.x * map.size.y) && (curpos+1) % map.size.x != 0 && '.' == map.cells[curpos+1])))
51 map.cells[y * map.size.x + x] = '.'; }
54 void save_game(struct World * world) {
55 // Save game data to game file.
56 FILE * file = fopen("savefile", "w");
57 write_uint32_bigendian(world->seed, file);
58 write_uint32_bigendian(world->turn, file);
59 write_uint16_bigendian(world->player->pos.y, file);
60 write_uint16_bigendian(world->player->pos.x, file);
61 write_uint16_bigendian(world->monster->pos.y, file);
62 write_uint16_bigendian(world->monster->pos.x, file);
63 write_uint16_bigendian(world->monster->next->pos.y, file);
64 write_uint16_bigendian(world->monster->next->pos.x, file);
65 write_uint16_bigendian(world->monster->next->next->pos.y, file);
66 write_uint16_bigendian(world->monster->next->next->pos.x, file);
69 void record_action (char action) {
70 // Append action to game record file.
71 FILE * file = fopen("record", "a");
75 struct yx_uint16 mv_yx_in_dir (char d, struct yx_uint16 yx) {
76 // Return yx coordinates one step to the direction d of yx.
77 if (d == NORTH) yx.y--;
78 else if (d == EAST) yx.x++;
79 else if (d == SOUTH) yx.y++;
80 else if (d == WEST) yx.x--;
83 void next_turn (struct World * world) {
84 // Increment turn and move enemy.
86 rrand(1, world->seed * world->turn);
88 struct Monster * monster;
89 for (monster = world->monster; monster != 0; monster = monster->next) {
91 struct yx_uint16 t = mv_yx_in_dir (d, monster->pos);
92 if (yx_uint16_cmp(t, world->player->pos))
93 update_log(world, "\nThe monster hits you.");
94 else if (is_passable(world->map, t.y, t.x))
97 void update_log (struct World * world, char * text) {
98 // Update log with new text to be appended.
100 uint16_t len_old = strlen(world->log);
101 uint16_t len_new = strlen(text);
102 uint16_t len_whole = len_old + len_new + 1;
103 new_text = calloc(len_whole, sizeof(char));
104 memcpy(new_text, world->log, len_old);
105 memcpy(new_text + len_old, text, len_new);
107 world->log = new_text; }
109 void move_player (struct World * world, char d) {
110 // Move player in direction d, increment turn counter and update log.
111 static char prev = 0;
114 struct yx_uint16 t = mv_yx_in_dir (d, world->player->pos);
115 struct Monster * monster;
116 for (monster = world->monster; monster != 0; monster = monster->next)
117 if (yx_uint16_cmp (t, monster->pos)) {
120 if (2 != success && is_passable(world->map, t.y, t.x)) {
122 world->player->pos = t; }
123 if (success * d == prev)
124 update_log (world, ".");
127 update_log (world, "\nYou hit the monster.");
129 if (NORTH == d) dir = "north";
130 else if (EAST == d) dir = "east" ;
131 else if (SOUTH == d) dir = "south";
132 else if (WEST == d) dir = "west" ;
133 char * msg = calloc(25, sizeof(char));
134 char * msg_content = "You fail to move";
136 msg_content = "You move";
137 sprintf(msg, "\n%s %s.", msg_content, dir);
138 update_log (world, msg);
141 if (1 == world->interactive)
145 char is_passable (struct Map * map, uint16_t y, uint16_t x) {
146 // Check if coordinate on (or beyond) map is accessible to movement.
148 if (0 <= x && x < map->size.x && 0 <= y && y < map->size.y)
149 if ('.' == map->cells[y * map->size.x + x])
153 void player_wait (struct World * world) {
154 // Make player wait one turn.
155 if (1 == world->interactive)
158 update_log (world, "\nYou wait."); }
160 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
161 // Toggle display of window win.
162 if (0 != win->frame.curses_win)
163 suspend_win(win_meta, win);
165 append_win(win_meta, win); }
167 void scroll_pad (struct WinMeta * win_meta, char dir) {
168 // Try to scroll pad left or right.
170 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
172 reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
174 void growshrink_active_window (struct WinMeta * win_meta, char change) {
175 // Grow or shrink active window horizontally or vertically by one cell size.
176 if (0 != win_meta->active) {
177 uint16_t height = win_meta->active->frame.size.y;
178 uint16_t width = win_meta->active->frame.size.x;
181 else if (change == '+')
183 else if (change == '_')
185 else if (change == '*')
187 resize_active_win (win_meta, height, width); } }
189 void map_scroll (struct Map * map, char dir) {
190 // Scroll map into direction dir if possible by changing the offset.
191 if (NORTH == dir && map->offset.y > 0) map->offset.y--;
192 else if (SOUTH == dir) map->offset.y++;
193 else if (WEST == dir && map->offset.x > 0) map->offset.x--;
194 else if (EAST == dir) map->offset.x++; }
196 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
197 struct Win * win_map, struct Win * win_info, struct Win * win_log) {
198 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
199 if (key == get_action_key(world->keybindings, "quit"))
201 else if (key == get_action_key(world->keybindings, "scroll pad right"))
202 scroll_pad (win_meta, '+');
203 else if (key == get_action_key(world->keybindings, "scroll pad left"))
204 scroll_pad (win_meta, '-');
205 else if (key == get_action_key(world->keybindings, "toggle keys window"))
206 toggle_window(win_meta, win_keys);
207 else if (key == get_action_key(world->keybindings, "toggle map window"))
208 toggle_window(win_meta, win_map);
209 else if (key == get_action_key(world->keybindings, "toggle info window"))
210 toggle_window(win_meta, win_info);
211 else if (key == get_action_key(world->keybindings, "toggle log window"))
212 toggle_window(win_meta, win_log);
213 else if (key == get_action_key(world->keybindings, "cycle forwards"))
214 cycle_active_win(win_meta, 'n');
215 else if (key == get_action_key(world->keybindings, "cycle backwards"))
216 cycle_active_win(win_meta, 'p');
217 else if (key == get_action_key(world->keybindings, "shift forwards"))
218 shift_active_win(win_meta, 'f');
219 else if (key == get_action_key(world->keybindings, "shift backwards"))
220 shift_active_win(win_meta, 'b');
221 else if (key == get_action_key(world->keybindings, "grow horizontally"))
222 growshrink_active_window(win_meta, '*');
223 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
224 growshrink_active_window(win_meta, '_');
225 else if (key == get_action_key(world->keybindings, "grow vertically"))
226 growshrink_active_window(win_meta, '+');
227 else if (key == get_action_key(world->keybindings, "shrink vertically"))
228 growshrink_active_window(win_meta, '-');
229 else if (key == get_action_key(world->keybindings, "save keys"))
230 save_keybindings(world);
231 else if (key == get_action_key(world->keybindings, "keys nav up"))
232 keyswin_move_selection (world, 'u');
233 else if (key == get_action_key(world->keybindings, "keys nav down"))
234 keyswin_move_selection (world, 'd');
235 else if (key == get_action_key(world->keybindings, "keys mod"))
236 keyswin_mod_key (world, win_meta);
237 else if (key == get_action_key(world->keybindings, "map up"))
238 map_scroll (world->map, NORTH);
239 else if (key == get_action_key(world->keybindings, "map down"))
240 map_scroll (world->map, SOUTH);
241 else if (key == get_action_key(world->keybindings, "map right"))
242 map_scroll (world->map, EAST);
243 else if (key == get_action_key(world->keybindings, "map left"))
244 map_scroll (world->map, WEST);
247 int main (int argc, char *argv[]) {
250 // Read in startup options (i.e. replay option and replay start turn).
253 world.interactive = 1;
254 while ((opt = getopt(argc, argv, "s::")) != -1) {
257 world.interactive = 0;
260 start_turn = atoi(optarg);
263 exit(EXIT_FAILURE); } }
265 // Initialize log, player and monsters.
266 world.log = calloc(1, sizeof(char));
267 update_log (&world, " ");
268 struct Player player;
269 world.player = &player;
270 struct Monster monster1;
271 struct Monster monster2;
272 struct Monster monster3;
273 world.monster = &monster1;
274 monster1.next = &monster2;
275 monster2.next = &monster3;
281 // For interactive mode, try to load world state from savefile.
283 if (1 == world.interactive && 0 == access("savefile", F_OK)) {
284 file = fopen("savefile", "r");
285 world.seed = read_uint32_bigendian(file);
286 world.turn = read_uint32_bigendian(file);
287 player.pos.y = read_uint16_bigendian(file);
288 player.pos.x = read_uint16_bigendian(file);
289 monster1.pos.y = read_uint16_bigendian(file);
290 monster1.pos.x = read_uint16_bigendian(file);
291 monster2.pos.y = read_uint16_bigendian(file);
292 monster2.pos.x = read_uint16_bigendian(file);
293 monster3.pos.y = read_uint16_bigendian(file);
294 monster3.pos.x = read_uint16_bigendian(file);
297 // For non-interactive mode, try to load world state from frecord file.
300 if (0 == world.interactive) {
301 file = fopen("record", "r");
302 world.seed = read_uint32_bigendian(file); }
304 // For interactive-mode in newly started world, generate a start seed from the current time.
306 file = fopen("record", "w");
307 world.seed = time(NULL);
308 write_uint32_bigendian(world.seed, file);
311 // Generate map from seed and, if newly generated world, start positions of actors.
312 rrand(1, world.seed);
313 struct Map map = init_map();
315 if (1 == world.turn) {
316 for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos.y, player.pos.x);) {
317 player.pos.y = rrand(0, 0) % map.size.y;
318 player.pos.x = rrand(0, 0) % map.size.x; }
319 struct Monster * monster;
320 for (monster = world.monster; monster != 0; monster = monster->next)
321 for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos.y, monster->pos.x);) {
322 monster->pos.y = rrand(0, 0) % map.size.y;
323 monster->pos.x = rrand(0, 0) % map.size.x; } }
325 // Initialize window system and windows.
326 WINDOW * screen = initscr();
329 keypad(screen, TRUE);
331 init_keybindings(&world);
332 struct WinMeta win_meta = init_win_meta(screen);
333 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
334 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
335 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
336 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
337 win_keys.frame.size.x = 29;
338 win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
339 win_info.frame.size.y = 1;
340 win_log.frame.size.y = win_meta.pad.size.y - 3;
341 toggle_window(&win_meta, &win_keys);
342 toggle_window(&win_meta, &win_map);
343 toggle_window(&win_meta, &win_info);
344 toggle_window(&win_meta, &win_log);
348 unsigned char quit_called = 0;
349 if (0 == world.interactive) {
350 unsigned char still_reading_file = 1;
353 if (start_turn == world.turn)
355 if (0 == start_turn) {
356 draw_all_wins (&win_meta);
358 if (1 == still_reading_file &&
359 (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
363 still_reading_file = 0; }
364 else if (0 == action)
365 player_wait (&world);
366 else if (NORTH == action)
367 move_player(&world, NORTH);
368 else if (EAST == action)
369 move_player(&world, EAST);
370 else if (SOUTH == action)
371 move_player(&world, SOUTH);
372 else if (WEST == action)
373 move_player(&world, WEST); }
375 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
376 if (1 == quit_called)
381 uint32_t last_turn = 0;
383 if (last_turn != world.turn) {
385 last_turn = world.turn; }
386 draw_all_wins (&win_meta);
388 if (key == get_action_key(world.keybindings, "player up"))
389 move_player(&world, NORTH);
390 else if (key == get_action_key(world.keybindings, "player right"))
391 move_player(&world, EAST);
392 else if (key == get_action_key(world.keybindings, "player down"))
393 move_player(&world, SOUTH);
394 else if (key == get_action_key(world.keybindings, "player left"))
395 move_player(&world, WEST);
396 else if (key == get_action_key(world.keybindings, "wait / next turn"))
397 player_wait (&world);
399 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
400 if (1 == quit_called)
403 // Clean up and exit.
405 for (key = 0; key <= world.keyswindata->max; key++)
406 free(world.keybindings[key].name);
407 free(world.keybindings);
408 free(world.keyswindata);