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);
87 struct Monster * monster;
88 for (monster = world->monster; monster != 0; monster = monster->next)
89 move_monster(world, monster); }
91 void update_log (struct World * world, char * text) {
92 // Update log with new text to be appended.
94 uint16_t len_old = strlen(world->log);
95 uint16_t len_new = strlen(text);
96 uint16_t len_whole = len_old + len_new + 1;
97 new_text = calloc(len_whole, sizeof(char));
98 memcpy(new_text, world->log, len_old);
99 memcpy(new_text + len_old, text, len_new);
101 world->log = new_text; }
103 void move_monster (struct World * world, struct Monster * monster) {
104 // Move monster in random direction, trigger fighting when hindered by player/monster.
105 char d = rrand(0, 0) % 5;
106 struct yx_uint16 t = mv_yx_in_dir (d, monster->pos);
107 if (yx_uint16_cmp (t, world->player->pos)) {
108 update_log (world, "\nThe monster hits you.");
110 char met_monster = 0;
111 struct Monster * other_monster;
112 for (other_monster = world->monster; other_monster != 0; other_monster = other_monster->next) {
113 if (other_monster == monster)
115 if (yx_uint16_cmp (t, other_monster->pos)) {
119 update_log (world, "\nMonster hits monster.");
120 else if (0 == met_monster && is_passable(world->map, t.y, t.x))
123 void move_player (struct World * world, char d) {
124 // Move player in direction d, increment turn counter and update log.
125 static char prev = 0;
128 struct yx_uint16 t = mv_yx_in_dir (d, world->player->pos);
129 struct Monster * monster;
130 for (monster = world->monster; monster != 0; monster = monster->next)
131 if (yx_uint16_cmp (t, monster->pos)) {
134 if (2 != success && is_passable(world->map, t.y, t.x)) {
136 world->player->pos = t; }
137 if (success * d == prev)
138 update_log (world, ".");
141 update_log (world, "\nYou hit the monster.");
143 if (NORTH == d) dir = "north";
144 else if (EAST == d) dir = "east" ;
145 else if (SOUTH == d) dir = "south";
146 else if (WEST == d) dir = "west" ;
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);
155 if (1 == world->interactive)
159 char is_passable (struct Map * map, uint16_t y, uint16_t x) {
160 // Check if coordinate on (or beyond) map is accessible to movement.
162 if (0 <= x && x < map->size.x && 0 <= y && y < map->size.y)
163 if ('.' == map->cells[y * map->size.x + x])
167 void player_wait (struct World * world) {
168 // Make player wait one turn.
169 if (1 == world->interactive)
172 update_log (world, "\nYou wait."); }
174 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
175 // Toggle display of window win.
176 if (0 != win->frame.curses_win)
177 suspend_win(win_meta, win);
179 append_win(win_meta, win); }
181 void scroll_pad (struct WinMeta * win_meta, char dir) {
182 // Try to scroll pad left or right.
184 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
186 reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
188 void growshrink_active_window (struct WinMeta * win_meta, char change) {
189 // Grow or shrink active window horizontally or vertically by one cell size.
190 if (0 != win_meta->active) {
191 uint16_t height = win_meta->active->frame.size.y;
192 uint16_t width = win_meta->active->frame.size.x;
195 else if (change == '+')
197 else if (change == '_')
199 else if (change == '*')
201 resize_active_win (win_meta, height, width); } }
203 void map_scroll (struct Map * map, char dir) {
204 // Scroll map into direction dir if possible by changing the offset.
205 if (NORTH == dir && map->offset.y > 0) map->offset.y--;
206 else if (SOUTH == dir) map->offset.y++;
207 else if (WEST == dir && map->offset.x > 0) map->offset.x--;
208 else if (EAST == dir) map->offset.x++; }
210 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
211 struct Win * win_map, struct Win * win_info, struct Win * win_log) {
212 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
213 if (key == get_action_key(world->keybindings, "quit"))
215 else if (key == get_action_key(world->keybindings, "scroll pad right"))
216 scroll_pad (win_meta, '+');
217 else if (key == get_action_key(world->keybindings, "scroll pad left"))
218 scroll_pad (win_meta, '-');
219 else if (key == get_action_key(world->keybindings, "toggle keys window"))
220 toggle_window(win_meta, win_keys);
221 else if (key == get_action_key(world->keybindings, "toggle map window"))
222 toggle_window(win_meta, win_map);
223 else if (key == get_action_key(world->keybindings, "toggle info window"))
224 toggle_window(win_meta, win_info);
225 else if (key == get_action_key(world->keybindings, "toggle log window"))
226 toggle_window(win_meta, win_log);
227 else if (key == get_action_key(world->keybindings, "cycle forwards"))
228 cycle_active_win(win_meta, 'n');
229 else if (key == get_action_key(world->keybindings, "cycle backwards"))
230 cycle_active_win(win_meta, 'p');
231 else if (key == get_action_key(world->keybindings, "shift forwards"))
232 shift_active_win(win_meta, 'f');
233 else if (key == get_action_key(world->keybindings, "shift backwards"))
234 shift_active_win(win_meta, 'b');
235 else if (key == get_action_key(world->keybindings, "grow horizontally"))
236 growshrink_active_window(win_meta, '*');
237 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
238 growshrink_active_window(win_meta, '_');
239 else if (key == get_action_key(world->keybindings, "grow vertically"))
240 growshrink_active_window(win_meta, '+');
241 else if (key == get_action_key(world->keybindings, "shrink vertically"))
242 growshrink_active_window(win_meta, '-');
243 else if (key == get_action_key(world->keybindings, "save keys"))
244 save_keybindings(world);
245 else if (key == get_action_key(world->keybindings, "keys nav up"))
246 keyswin_move_selection (world, 'u');
247 else if (key == get_action_key(world->keybindings, "keys nav down"))
248 keyswin_move_selection (world, 'd');
249 else if (key == get_action_key(world->keybindings, "keys mod"))
250 keyswin_mod_key (world, win_meta);
251 else if (key == get_action_key(world->keybindings, "map up"))
252 map_scroll (world->map, NORTH);
253 else if (key == get_action_key(world->keybindings, "map down"))
254 map_scroll (world->map, SOUTH);
255 else if (key == get_action_key(world->keybindings, "map right"))
256 map_scroll (world->map, EAST);
257 else if (key == get_action_key(world->keybindings, "map left"))
258 map_scroll (world->map, WEST);
261 int main (int argc, char *argv[]) {
264 // Read in startup options (i.e. replay option and replay start turn).
267 world.interactive = 1;
268 while ((opt = getopt(argc, argv, "s::")) != -1) {
271 world.interactive = 0;
274 start_turn = atoi(optarg);
277 exit(EXIT_FAILURE); } }
279 // Initialize log, player and monsters.
280 world.log = calloc(1, sizeof(char));
281 update_log (&world, " ");
282 struct Player player;
283 world.player = &player;
284 struct Monster monster1;
285 struct Monster monster2;
286 struct Monster monster3;
287 world.monster = &monster1;
288 monster1.next = &monster2;
289 monster2.next = &monster3;
295 // For interactive mode, try to load world state from savefile.
297 if (1 == world.interactive && 0 == access("savefile", F_OK)) {
298 file = fopen("savefile", "r");
299 world.seed = read_uint32_bigendian(file);
300 world.turn = read_uint32_bigendian(file);
301 player.pos.y = read_uint16_bigendian(file);
302 player.pos.x = read_uint16_bigendian(file);
303 monster1.pos.y = read_uint16_bigendian(file);
304 monster1.pos.x = read_uint16_bigendian(file);
305 monster2.pos.y = read_uint16_bigendian(file);
306 monster2.pos.x = read_uint16_bigendian(file);
307 monster3.pos.y = read_uint16_bigendian(file);
308 monster3.pos.x = read_uint16_bigendian(file);
311 // For non-interactive mode, try to load world state from frecord file.
314 if (0 == world.interactive) {
315 file = fopen("record", "r");
316 world.seed = read_uint32_bigendian(file); }
318 // For interactive-mode in newly started world, generate a start seed from the current time.
320 file = fopen("record", "w");
321 world.seed = time(NULL);
322 write_uint32_bigendian(world.seed, file);
325 // Generate map from seed and, if newly generated world, start positions of actors.
326 rrand(1, world.seed);
327 struct Map map = init_map();
329 if (1 == world.turn) {
330 for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos.y, player.pos.x);) {
331 player.pos.y = rrand(0, 0) % map.size.y;
332 player.pos.x = rrand(0, 0) % map.size.x; }
333 struct Monster * monster;
334 for (monster = world.monster; monster != 0; monster = monster->next)
335 for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos.y, monster->pos.x);) {
336 monster->pos.y = rrand(0, 0) % map.size.y;
337 monster->pos.x = rrand(0, 0) % map.size.x; } }
339 // Initialize window system and windows.
340 WINDOW * screen = initscr();
343 keypad(screen, TRUE);
345 init_keybindings(&world);
346 struct WinMeta win_meta = init_win_meta(screen);
347 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
348 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
349 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
350 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
351 win_keys.frame.size.x = 29;
352 win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
353 win_info.frame.size.y = 1;
354 win_log.frame.size.y = win_meta.pad.size.y - 3;
355 toggle_window(&win_meta, &win_keys);
356 toggle_window(&win_meta, &win_map);
357 toggle_window(&win_meta, &win_info);
358 toggle_window(&win_meta, &win_log);
362 unsigned char quit_called = 0;
363 if (0 == world.interactive) {
364 unsigned char still_reading_file = 1;
367 if (start_turn == world.turn)
369 if (0 == start_turn) {
370 draw_all_wins (&win_meta);
372 if (1 == still_reading_file &&
373 (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
377 still_reading_file = 0; }
378 else if (0 == action)
379 player_wait (&world);
380 else if (NORTH == action)
381 move_player(&world, NORTH);
382 else if (EAST == action)
383 move_player(&world, EAST);
384 else if (SOUTH == action)
385 move_player(&world, SOUTH);
386 else if (WEST == action)
387 move_player(&world, WEST); }
389 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
390 if (1 == quit_called)
395 uint32_t last_turn = 0;
397 if (last_turn != world.turn) {
399 last_turn = world.turn; }
400 draw_all_wins (&win_meta);
402 if (key == get_action_key(world.keybindings, "player up"))
403 move_player(&world, NORTH);
404 else if (key == get_action_key(world.keybindings, "player right"))
405 move_player(&world, EAST);
406 else if (key == get_action_key(world.keybindings, "player down"))
407 move_player(&world, SOUTH);
408 else if (key == get_action_key(world.keybindings, "player left"))
409 move_player(&world, WEST);
410 else if (key == get_action_key(world.keybindings, "wait / next turn"))
411 player_wait (&world);
413 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
414 if (1 == quit_called)
417 // Clean up and exit.
419 for (key = 0; key <= world.keyswindata->max; key++)
420 free(world.keybindings[key].name);
421 free(world.keybindings);
422 free(world.keyswindata);