10 #include "keybindings.h"
11 #include "readwrite.h"
12 #include "objects_on_map.h"
14 uint16_t rrand(char use_seed, uint32_t new_seed) {
15 // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
19 seed = ((seed * 1103515245) + 12345) % 2147483648; // Values as recommended by POSIX.1-2001 (see rand(3)).
20 return (seed / 65536); } // Ignore least significant 16 bits (they are less random).
22 void update_log (struct World * world, char * text) {
23 // Update log by appending text, or by appending a "." if text is the same as the last one.
24 static char * last_msg;
26 last_msg = calloc(1, sizeof(char));
28 uint16_t len_old = strlen(world->log);
29 if (0 == strcmp(last_msg, text)) {
30 uint16_t len_whole = len_old + 1;
31 new_text = calloc(len_whole + 1, sizeof(char));
32 memcpy(new_text, world->log, len_old);
33 memcpy(new_text + len_old, ".", 1); }
35 uint16_t len_new = strlen(text);
36 uint16_t len_whole = len_old + len_new + 1;
37 new_text = calloc(len_whole, sizeof(char));
38 memcpy(new_text, world->log, len_old);
39 memcpy(new_text + len_old, text, len_new);
40 last_msg = calloc(len_new + 1, sizeof(char));
41 memcpy(last_msg, text, len_new); }
43 world->log = new_text; }
45 struct Map init_map () {
46 // Initialize map with some experimental start values.
52 uint32_t size = map.size.x * map.size.y;
53 map.cells = malloc(size);
55 for (y = 0; y < map.size.y; y++)
56 for (x = 0; x < map.size.x; x++)
57 map.cells[(y * map.size.x) + x] = '~';
58 map.cells[size / 2 + (map.size.x / 2)] = '.';
59 uint32_t repeats, root, curpos;
60 for (root = 0; root * root * root < size; root++);
61 for (repeats = 0; repeats < size * root; repeats++) {
62 y = rrand(0, 0) % map.size.y;
63 x = rrand(0, 0) % map.size.x;
64 curpos = y * map.size.x + x;
65 if ('~' == map.cells[curpos] &&
66 ( (curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
67 || (curpos < map.size.x * (map.size.y-1) && '.' == map.cells[curpos + map.size.x])
68 || (curpos > 0 && curpos % map.size.x != 0 && '.' == map.cells[curpos-1])
69 || (curpos < (map.size.x * map.size.y) && (curpos+1) % map.size.x != 0 && '.' == map.cells[curpos+1])))
70 map.cells[y * map.size.x + x] = '.'; }
73 void map_scroll (struct Map * map, char dir) {
74 // Scroll map into direction dir if possible by changing the offset.
75 if (NORTH == dir && map->offset.y > 0) map->offset.y--;
76 else if (SOUTH == dir) map->offset.y++;
77 else if (WEST == dir && map->offset.x > 0) map->offset.x--;
78 else if (EAST == dir) map->offset.x++; }
80 void turn_over (struct World * world, char action) {
81 // Record action in game record file, increment turn and move enemy.
82 if (1 == world->interactive) {
83 FILE * file = fopen("record", "a");
87 rrand(1, world->seed * world->turn);
88 struct Monster * monster;
89 for (monster = world->monster; monster != 0; monster = monster->next)
90 move_monster(world, monster); }
92 void save_game(struct World * world) {
93 // Save game data to game file.
94 FILE * file = fopen("savefile", "w");
95 write_uint32_bigendian(world->seed, file);
96 write_uint32_bigendian(world->turn, file);
97 write_uint16_bigendian(world->player->pos.y + 1, file);
98 write_uint16_bigendian(world->player->pos.x + 1, file);
99 struct Monster * monster;
100 for (monster = world->monster; monster != 0; monster = monster->next) {
101 write_uint16_bigendian(monster->pos.y + 1, file);
102 write_uint16_bigendian(monster->pos.x + 1, file);
103 fputc(monster->name, file); }
104 write_uint16_bigendian(0, file);
106 for (item = world->item; item != 0; item = item->next) {
107 write_uint16_bigendian(item->pos.y + 1, file);
108 write_uint16_bigendian(item->pos.x + 1, file);
109 fputc(item->name, file); }
110 write_uint16_bigendian(0, file);
113 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
114 // Toggle display of window win.
115 if (0 != win->frame.curses_win)
116 suspend_win(win_meta, win);
118 append_win(win_meta, win); }
120 void scroll_pad (struct WinMeta * win_meta, char dir) {
121 // Try to scroll pad left or right.
123 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
125 reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
127 void growshrink_active_window (struct WinMeta * win_meta, char change) {
128 // Grow or shrink active window horizontally or vertically by one cell size.
129 if (0 != win_meta->active) {
130 struct yx_uint16 size = win_meta->active->frame.size;
131 if (change == '-') size.y--;
132 else if (change == '+') size.y++;
133 else if (change == '_') size.x--;
134 else if (change == '*') size.x++;
135 resize_active_win (win_meta, size); } }
137 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
138 struct Win * win_map, struct Win * win_info, struct Win * win_log) {
139 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
140 if (key == get_action_key(world->keybindings, "quit"))
142 else if (key == get_action_key(world->keybindings, "scroll pad right"))
143 scroll_pad (win_meta, '+');
144 else if (key == get_action_key(world->keybindings, "scroll pad left"))
145 scroll_pad (win_meta, '-');
146 else if (key == get_action_key(world->keybindings, "toggle keys window"))
147 toggle_window(win_meta, win_keys);
148 else if (key == get_action_key(world->keybindings, "toggle map window"))
149 toggle_window(win_meta, win_map);
150 else if (key == get_action_key(world->keybindings, "toggle info window"))
151 toggle_window(win_meta, win_info);
152 else if (key == get_action_key(world->keybindings, "toggle log window"))
153 toggle_window(win_meta, win_log);
154 else if (key == get_action_key(world->keybindings, "cycle forwards"))
155 cycle_active_win(win_meta, 'n');
156 else if (key == get_action_key(world->keybindings, "cycle backwards"))
157 cycle_active_win(win_meta, 'p');
158 else if (key == get_action_key(world->keybindings, "shift forwards"))
159 shift_active_win(win_meta, 'f');
160 else if (key == get_action_key(world->keybindings, "shift backwards"))
161 shift_active_win(win_meta, 'b');
162 else if (key == get_action_key(world->keybindings, "grow horizontally"))
163 growshrink_active_window(win_meta, '*');
164 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
165 growshrink_active_window(win_meta, '_');
166 else if (key == get_action_key(world->keybindings, "grow vertically"))
167 growshrink_active_window(win_meta, '+');
168 else if (key == get_action_key(world->keybindings, "shrink vertically"))
169 growshrink_active_window(win_meta, '-');
170 else if (key == get_action_key(world->keybindings, "save keys"))
171 save_keybindings(world);
172 else if (key == get_action_key(world->keybindings, "keys nav up"))
173 keyswin_move_selection (world, 'u');
174 else if (key == get_action_key(world->keybindings, "keys nav down"))
175 keyswin_move_selection (world, 'd');
176 else if (key == get_action_key(world->keybindings, "keys mod"))
177 keyswin_mod_key (world, win_meta);
178 else if (key == get_action_key(world->keybindings, "map up"))
179 map_scroll (world->map, NORTH);
180 else if (key == get_action_key(world->keybindings, "map down"))
181 map_scroll (world->map, SOUTH);
182 else if (key == get_action_key(world->keybindings, "map right"))
183 map_scroll (world->map, EAST);
184 else if (key == get_action_key(world->keybindings, "map left"))
185 map_scroll (world->map, WEST);
188 int main (int argc, char *argv[]) {
191 // Read in startup options (i.e. replay option and replay start turn).
194 world.interactive = 1;
195 while ((opt = getopt(argc, argv, "s::")) != -1) {
198 world.interactive = 0;
201 start_turn = atoi(optarg);
204 exit(EXIT_FAILURE); } }
206 // Initialize log, player, monsters and items.
207 world.log = calloc(1, sizeof(char));
208 update_log (&world, " ");
209 struct Player player;
210 world.player = &player;
211 struct Monster * monster;
216 // For interactive mode, try to load world state from savefile.
218 if (1 == world.interactive && 0 == access("savefile", F_OK)) {
219 file = fopen("savefile", "r");
220 world.seed = read_uint32_bigendian(file);
221 world.turn = read_uint32_bigendian(file);
222 player.pos.y = read_uint16_bigendian(file) - 1;
223 player.pos.x = read_uint16_bigendian(file) - 1;
227 test = read_uint16_bigendian(file);
231 monster = malloc(sizeof(struct Monster));
232 world.monster = monster;
235 monster->next = malloc(sizeof(struct Monster));
236 monster = monster->next; }
237 monster->pos.y = test - 1;
238 monster->pos.x = read_uint16_bigendian(file) - 1;
239 monster->name = fgetc(file); }
245 test = read_uint16_bigendian(file);
249 item = malloc(sizeof(struct Item));
253 item->next = malloc(sizeof(struct Item));
255 item->pos.y = test - 1;
256 item->pos.x = read_uint16_bigendian(file) - 1;
257 item->name = fgetc(file); }
262 // For non-interactive mode, try to load world state from record file.
265 if (0 == world.interactive) {
266 file = fopen("record", "r");
267 world.seed = read_uint32_bigendian(file); }
269 // For interactive-mode in newly started world, generate a start seed from the current time.
271 file = fopen("record", "w");
272 world.seed = time(NULL);
273 write_uint32_bigendian(world.seed, file);
276 // Generate map from seed and, if newly generated world, start positions of actors.
277 rrand(1, world.seed);
278 struct Map map = init_map();
280 if (1 == world.turn) {
281 player.pos = find_passable_pos(&map);
282 unsigned char n_monsters = rrand(0, 0) % 16;
283 unsigned char n_items = rrand(0, 0) % 48;
287 for (i = 0; i < n_monsters; i++) {
289 monster = malloc(sizeof(struct Monster));
290 world.monster = monster;
293 monster->next = malloc(sizeof(struct Monster));
294 monster = monster->next; }
295 monster->pos = find_passable_pos(&map);
296 monster->name = 'A' + (rrand(0, 0) % 8); }
301 for (i = 0; i < n_items; i++) {
303 item = malloc(sizeof(struct Item));
307 item->next = malloc(sizeof(struct Item));
309 item->pos = find_passable_pos(&map);
310 item->name = '#' + (rrand(0, 0) % 4); }
314 // Initialize window system and windows.
315 WINDOW * screen = initscr();
318 keypad(screen, TRUE);
320 init_keybindings(&world);
321 struct WinMeta win_meta = init_win_meta(screen);
322 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
323 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
324 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
325 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
326 win_keys.frame.size.x = 29;
327 win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
328 win_info.frame.size.y = 1;
329 win_log.frame.size.y = win_meta.pad.size.y - 3;
330 toggle_window(&win_meta, &win_keys);
331 toggle_window(&win_meta, &win_map);
332 toggle_window(&win_meta, &win_info);
333 toggle_window(&win_meta, &win_log);
337 unsigned char quit_called = 0;
338 if (0 == world.interactive) {
339 unsigned char still_reading_file = 1;
342 if (start_turn == world.turn)
344 if (0 == start_turn) {
345 draw_all_wins (&win_meta);
347 if (1 == still_reading_file &&
348 (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
352 still_reading_file = 0; }
353 else if (0 == action)
354 player_wait (&world);
355 else if (NORTH == action)
356 move_player(&world, NORTH);
357 else if (EAST == action)
358 move_player(&world, EAST);
359 else if (SOUTH == action)
360 move_player(&world, SOUTH);
361 else if (WEST == action)
362 move_player(&world, WEST); }
364 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
365 if (1 == quit_called)
370 uint32_t last_turn = 0;
372 if (last_turn != world.turn) {
374 last_turn = world.turn; }
375 draw_all_wins (&win_meta);
377 if (key == get_action_key(world.keybindings, "player up"))
378 move_player(&world, NORTH);
379 else if (key == get_action_key(world.keybindings, "player right"))
380 move_player(&world, EAST);
381 else if (key == get_action_key(world.keybindings, "player down"))
382 move_player(&world, SOUTH);
383 else if (key == get_action_key(world.keybindings, "player left"))
384 move_player(&world, WEST);
385 else if (key == get_action_key(world.keybindings, "wait / next turn"))
386 player_wait (&world);
388 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
389 if (1 == quit_called)
392 // Clean up and exit.
394 for (key = 0; key <= world.keyswindata->max; key++)
395 free(world.keybindings[key].name);
396 free(world.keybindings);
397 free(world.keyswindata);