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 fputc(monster->hitpoints, file); }
105 write_uint16_bigendian(0, file);
107 for (item = world->item; item != 0; item = item->next) {
108 write_uint16_bigendian(item->pos.y + 1, file);
109 write_uint16_bigendian(item->pos.x + 1, file);
110 fputc(item->name, file); }
111 write_uint16_bigendian(0, file);
114 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
115 // Toggle display of window win.
116 if (0 != win->frame.curses_win)
117 suspend_win(win_meta, win);
119 append_win(win_meta, win); }
121 void scroll_pad (struct WinMeta * win_meta, char dir) {
122 // Try to scroll pad left or right.
124 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
126 reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
128 void growshrink_active_window (struct WinMeta * win_meta, char change) {
129 // Grow or shrink active window horizontally or vertically by one cell size.
130 if (0 != win_meta->active) {
131 struct yx_uint16 size = win_meta->active->frame.size;
132 if (change == '-') size.y--;
133 else if (change == '+') size.y++;
134 else if (change == '_') size.x--;
135 else if (change == '*') size.x++;
136 resize_active_win (win_meta, size); } }
138 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
139 struct Win * win_map, struct Win * win_info, struct Win * win_log) {
140 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
141 if (key == get_action_key(world->keybindings, "quit"))
143 else if (key == get_action_key(world->keybindings, "scroll pad right"))
144 scroll_pad (win_meta, '+');
145 else if (key == get_action_key(world->keybindings, "scroll pad left"))
146 scroll_pad (win_meta, '-');
147 else if (key == get_action_key(world->keybindings, "toggle keys window"))
148 toggle_window(win_meta, win_keys);
149 else if (key == get_action_key(world->keybindings, "toggle map window"))
150 toggle_window(win_meta, win_map);
151 else if (key == get_action_key(world->keybindings, "toggle info window"))
152 toggle_window(win_meta, win_info);
153 else if (key == get_action_key(world->keybindings, "toggle log window"))
154 toggle_window(win_meta, win_log);
155 else if (key == get_action_key(world->keybindings, "cycle forwards"))
156 cycle_active_win(win_meta, 'n');
157 else if (key == get_action_key(world->keybindings, "cycle backwards"))
158 cycle_active_win(win_meta, 'p');
159 else if (key == get_action_key(world->keybindings, "shift forwards"))
160 shift_active_win(win_meta, 'f');
161 else if (key == get_action_key(world->keybindings, "shift backwards"))
162 shift_active_win(win_meta, 'b');
163 else if (key == get_action_key(world->keybindings, "grow horizontally"))
164 growshrink_active_window(win_meta, '*');
165 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
166 growshrink_active_window(win_meta, '_');
167 else if (key == get_action_key(world->keybindings, "grow vertically"))
168 growshrink_active_window(win_meta, '+');
169 else if (key == get_action_key(world->keybindings, "shrink vertically"))
170 growshrink_active_window(win_meta, '-');
171 else if (key == get_action_key(world->keybindings, "save keys"))
172 save_keybindings(world);
173 else if (key == get_action_key(world->keybindings, "keys nav up"))
174 keyswin_move_selection (world, 'u');
175 else if (key == get_action_key(world->keybindings, "keys nav down"))
176 keyswin_move_selection (world, 'd');
177 else if (key == get_action_key(world->keybindings, "keys mod"))
178 keyswin_mod_key (world, win_meta);
179 else if (key == get_action_key(world->keybindings, "map up"))
180 map_scroll (world->map, NORTH);
181 else if (key == get_action_key(world->keybindings, "map down"))
182 map_scroll (world->map, SOUTH);
183 else if (key == get_action_key(world->keybindings, "map right"))
184 map_scroll (world->map, EAST);
185 else if (key == get_action_key(world->keybindings, "map left"))
186 map_scroll (world->map, WEST);
189 int main (int argc, char *argv[]) {
192 // Read in startup options (i.e. replay option and replay start turn).
195 world.interactive = 1;
196 while ((opt = getopt(argc, argv, "s::")) != -1) {
199 world.interactive = 0;
202 start_turn = atoi(optarg);
205 exit(EXIT_FAILURE); } }
207 // Initialize log, player, monsters and items.
208 world.log = calloc(1, sizeof(char));
209 update_log (&world, " ");
210 struct Player player;
211 world.player = &player;
212 struct Monster * monster;
217 // For interactive mode, try to load world state from savefile.
219 if (1 == world.interactive && 0 == access("savefile", F_OK)) {
220 file = fopen("savefile", "r");
221 world.seed = read_uint32_bigendian(file);
222 world.turn = read_uint32_bigendian(file);
223 player.pos.y = read_uint16_bigendian(file) - 1;
224 player.pos.x = read_uint16_bigendian(file) - 1;
228 test = read_uint16_bigendian(file);
232 monster = malloc(sizeof(struct Monster));
233 world.monster = monster;
236 monster->next = malloc(sizeof(struct Monster));
237 monster = monster->next; }
238 monster->pos.y = test - 1;
239 monster->pos.x = read_uint16_bigendian(file) - 1;
240 monster->name = fgetc(file);
241 monster->hitpoints = fgetc(file); }
247 test = read_uint16_bigendian(file);
251 item = malloc(sizeof(struct Item));
255 item->next = malloc(sizeof(struct Item));
257 item->pos.y = test - 1;
258 item->pos.x = read_uint16_bigendian(file) - 1;
259 item->name = fgetc(file); }
264 // For non-interactive mode, try to load world state from record file.
267 if (0 == world.interactive) {
268 file = fopen("record", "r");
269 world.seed = read_uint32_bigendian(file); }
271 // For interactive-mode in newly started world, generate a start seed from the current time.
273 file = fopen("record", "w");
274 world.seed = time(NULL);
275 write_uint32_bigendian(world.seed, file);
278 // Generate map from seed and, if newly generated world, start positions of actors.
279 rrand(1, world.seed);
280 struct Map map = init_map();
282 if (1 == world.turn) {
283 player.pos = find_passable_pos(&map);
284 unsigned char n_monsters = rrand(0, 0) % 16;
285 unsigned char n_items = rrand(0, 0) % 48;
289 for (i = 0; i < n_monsters; i++) {
291 monster = malloc(sizeof(struct Monster));
292 world.monster = monster;
295 monster->next = malloc(sizeof(struct Monster));
296 monster = monster->next; }
297 monster->pos = find_passable_pos(&map);
298 monster->name = 'A' + (rrand(0, 0) % 8);
299 monster->hitpoints = 5; }
304 for (i = 0; i < n_items; i++) {
306 item = malloc(sizeof(struct Item));
310 item->next = malloc(sizeof(struct Item));
312 item->pos = find_passable_pos(&map);
313 item->name = '#' + (rrand(0, 0) % 4); }
317 // Initialize window system and windows.
318 WINDOW * screen = initscr();
321 keypad(screen, TRUE);
323 init_keybindings(&world);
324 struct WinMeta win_meta = init_win_meta(screen);
325 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
326 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
327 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
328 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
329 win_keys.frame.size.x = 29;
330 win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
331 win_info.frame.size.y = 1;
332 win_log.frame.size.y = win_meta.pad.size.y - 3;
333 toggle_window(&win_meta, &win_keys);
334 toggle_window(&win_meta, &win_map);
335 toggle_window(&win_meta, &win_info);
336 toggle_window(&win_meta, &win_log);
340 unsigned char quit_called = 0;
341 if (0 == world.interactive) {
342 unsigned char still_reading_file = 1;
345 if (start_turn == world.turn)
347 if (0 == start_turn) {
348 draw_all_wins (&win_meta);
350 if (1 == still_reading_file &&
351 (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
355 still_reading_file = 0; }
356 else if (0 == action)
357 player_wait (&world);
358 else if (NORTH == action)
359 move_player(&world, NORTH);
360 else if (EAST == action)
361 move_player(&world, EAST);
362 else if (SOUTH == action)
363 move_player(&world, SOUTH);
364 else if (WEST == action)
365 move_player(&world, WEST); }
367 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
368 if (1 == quit_called)
373 uint32_t last_turn = 0;
375 if (last_turn != world.turn) {
377 last_turn = world.turn; }
378 draw_all_wins (&win_meta);
380 if (key == get_action_key(world.keybindings, "player up"))
381 move_player(&world, NORTH);
382 else if (key == get_action_key(world.keybindings, "player right"))
383 move_player(&world, EAST);
384 else if (key == get_action_key(world.keybindings, "player down"))
385 move_player(&world, SOUTH);
386 else if (key == get_action_key(world.keybindings, "player left"))
387 move_player(&world, WEST);
388 else if (key == get_action_key(world.keybindings, "wait / next turn"))
389 player_wait (&world);
391 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
392 if (1 == quit_called)
395 // Clean up and exit.
397 for (key = 0; key <= world.keyswindata->max; key++)
398 free(world.keybindings[key].name);
399 free(world.keybindings);
400 free(world.keyswindata);