10 #include "keybindings.h"
11 #include "readwrite.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, file);
98 write_uint16_bigendian(world->player->pos.x, file);
99 struct Monster * monster;
100 for (monster = world->monster; monster != 0; monster = monster->next) {
101 write_uint16_bigendian(monster->pos.y, file);
102 write_uint16_bigendian(monster->pos.x, file); }
104 for (item = world->item; item != 0; item = item->next) {
105 write_uint16_bigendian(item->pos.y, file);
106 write_uint16_bigendian(item->pos.x, file); }
109 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
110 // Toggle display of window win.
111 if (0 != win->frame.curses_win)
112 suspend_win(win_meta, win);
114 append_win(win_meta, win); }
116 void scroll_pad (struct WinMeta * win_meta, char dir) {
117 // Try to scroll pad left or right.
119 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
121 reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
123 void growshrink_active_window (struct WinMeta * win_meta, char change) {
124 // Grow or shrink active window horizontally or vertically by one cell size.
125 if (0 != win_meta->active) {
126 struct yx_uint16 size = win_meta->active->frame.size;
127 if (change == '-') size.y--;
128 else if (change == '+') size.y++;
129 else if (change == '_') size.x--;
130 else if (change == '*') size.x++;
131 resize_active_win (win_meta, size); } }
133 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
134 struct Win * win_map, struct Win * win_info, struct Win * win_log) {
135 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
136 if (key == get_action_key(world->keybindings, "quit"))
138 else if (key == get_action_key(world->keybindings, "scroll pad right"))
139 scroll_pad (win_meta, '+');
140 else if (key == get_action_key(world->keybindings, "scroll pad left"))
141 scroll_pad (win_meta, '-');
142 else if (key == get_action_key(world->keybindings, "toggle keys window"))
143 toggle_window(win_meta, win_keys);
144 else if (key == get_action_key(world->keybindings, "toggle map window"))
145 toggle_window(win_meta, win_map);
146 else if (key == get_action_key(world->keybindings, "toggle info window"))
147 toggle_window(win_meta, win_info);
148 else if (key == get_action_key(world->keybindings, "toggle log window"))
149 toggle_window(win_meta, win_log);
150 else if (key == get_action_key(world->keybindings, "cycle forwards"))
151 cycle_active_win(win_meta, 'n');
152 else if (key == get_action_key(world->keybindings, "cycle backwards"))
153 cycle_active_win(win_meta, 'p');
154 else if (key == get_action_key(world->keybindings, "shift forwards"))
155 shift_active_win(win_meta, 'f');
156 else if (key == get_action_key(world->keybindings, "shift backwards"))
157 shift_active_win(win_meta, 'b');
158 else if (key == get_action_key(world->keybindings, "grow horizontally"))
159 growshrink_active_window(win_meta, '*');
160 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
161 growshrink_active_window(win_meta, '_');
162 else if (key == get_action_key(world->keybindings, "grow vertically"))
163 growshrink_active_window(win_meta, '+');
164 else if (key == get_action_key(world->keybindings, "shrink vertically"))
165 growshrink_active_window(win_meta, '-');
166 else if (key == get_action_key(world->keybindings, "save keys"))
167 save_keybindings(world);
168 else if (key == get_action_key(world->keybindings, "keys nav up"))
169 keyswin_move_selection (world, 'u');
170 else if (key == get_action_key(world->keybindings, "keys nav down"))
171 keyswin_move_selection (world, 'd');
172 else if (key == get_action_key(world->keybindings, "keys mod"))
173 keyswin_mod_key (world, win_meta);
174 else if (key == get_action_key(world->keybindings, "map up"))
175 map_scroll (world->map, NORTH);
176 else if (key == get_action_key(world->keybindings, "map down"))
177 map_scroll (world->map, SOUTH);
178 else if (key == get_action_key(world->keybindings, "map right"))
179 map_scroll (world->map, EAST);
180 else if (key == get_action_key(world->keybindings, "map left"))
181 map_scroll (world->map, WEST);
184 int main (int argc, char *argv[]) {
187 // Read in startup options (i.e. replay option and replay start turn).
190 world.interactive = 1;
191 while ((opt = getopt(argc, argv, "s::")) != -1) {
194 world.interactive = 0;
197 start_turn = atoi(optarg);
200 exit(EXIT_FAILURE); } }
202 // Initialize log, player, monsters and items.
203 world.log = calloc(1, sizeof(char));
204 update_log (&world, " ");
205 struct Player player;
206 world.player = &player;
207 struct Monster monster1;
208 struct Monster monster2;
209 struct Monster monster3;
210 world.monster = &monster1;
211 monster1.next = &monster2;
212 monster2.next = &monster3;
228 // For interactive mode, try to load world state from savefile.
231 struct Monster * monster;
232 if (1 == world.interactive && 0 == access("savefile", F_OK)) {
233 file = fopen("savefile", "r");
234 world.seed = read_uint32_bigendian(file);
235 world.turn = read_uint32_bigendian(file);
236 player.pos.y = read_uint16_bigendian(file);
237 player.pos.x = read_uint16_bigendian(file);
238 for (monster = world.monster; monster != 0; monster = monster->next) {
239 monster->pos.y = read_uint16_bigendian(file);
240 monster->pos.x = read_uint16_bigendian(file); }
241 for (item = world.item; item != 0; item = item->next) {
242 item->pos.y = read_uint16_bigendian(file);
243 item->pos.x = read_uint16_bigendian(file); }
246 // For non-interactive mode, try to load world state from frecord file.
249 if (0 == world.interactive) {
250 file = fopen("record", "r");
251 world.seed = read_uint32_bigendian(file); }
253 // For interactive-mode in newly started world, generate a start seed from the current time.
255 file = fopen("record", "w");
256 world.seed = time(NULL);
257 write_uint32_bigendian(world.seed, file);
260 // Generate map from seed and, if newly generated world, start positions of actors.
261 rrand(1, world.seed);
262 struct Map map = init_map();
264 if (1 == world.turn) {
265 for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos);) {
266 player.pos.y = rrand(0, 0) % map.size.y;
267 player.pos.x = rrand(0, 0) % map.size.x; }
268 for (item = world.item; item != 0; item = item->next)
269 for (item->pos.y = item->pos.x = 0; 0 == is_passable(&map, item->pos);) {
270 item->pos.y = rrand(0, 0) % map.size.y;
271 item->pos.x = rrand(0, 0) % map.size.x; }
272 for (monster = world.monster; monster != 0; monster = monster->next)
273 for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos);) {
274 monster->pos.y = rrand(0, 0) % map.size.y;
275 monster->pos.x = rrand(0, 0) % map.size.x; } }
277 // Initialize window system and windows.
278 WINDOW * screen = initscr();
281 keypad(screen, TRUE);
283 init_keybindings(&world);
284 struct WinMeta win_meta = init_win_meta(screen);
285 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
286 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
287 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
288 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
289 win_keys.frame.size.x = 29;
290 win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
291 win_info.frame.size.y = 1;
292 win_log.frame.size.y = win_meta.pad.size.y - 3;
293 toggle_window(&win_meta, &win_keys);
294 toggle_window(&win_meta, &win_map);
295 toggle_window(&win_meta, &win_info);
296 toggle_window(&win_meta, &win_log);
300 unsigned char quit_called = 0;
301 if (0 == world.interactive) {
302 unsigned char still_reading_file = 1;
305 if (start_turn == world.turn)
307 if (0 == start_turn) {
308 draw_all_wins (&win_meta);
310 if (1 == still_reading_file &&
311 (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
315 still_reading_file = 0; }
316 else if (0 == action)
317 player_wait (&world);
318 else if (NORTH == action)
319 move_player(&world, NORTH);
320 else if (EAST == action)
321 move_player(&world, EAST);
322 else if (SOUTH == action)
323 move_player(&world, SOUTH);
324 else if (WEST == action)
325 move_player(&world, WEST); }
327 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
328 if (1 == quit_called)
333 uint32_t last_turn = 0;
335 if (last_turn != world.turn) {
337 last_turn = world.turn; }
338 draw_all_wins (&win_meta);
340 if (key == get_action_key(world.keybindings, "player up"))
341 move_player(&world, NORTH);
342 else if (key == get_action_key(world.keybindings, "player right"))
343 move_player(&world, EAST);
344 else if (key == get_action_key(world.keybindings, "player down"))
345 move_player(&world, SOUTH);
346 else if (key == get_action_key(world.keybindings, "player left"))
347 move_player(&world, WEST);
348 else if (key == get_action_key(world.keybindings, "wait / next turn"))
349 player_wait (&world);
351 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
352 if (1 == quit_called)
355 // Clean up and exit.
357 for (key = 0; key <= world.keyswindata->max; key++)
358 free(world.keybindings[key].name);
359 free(world.keybindings);
360 free(world.keyswindata);