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;
208 struct Monster * monster = malloc(sizeof(struct Monster));
209 world.monster = monster;
210 for (i = 0; i < 8; i++) {
212 monster->next = malloc(sizeof(struct Monster));
213 monster = monster->next; }
216 struct Item * item = malloc(sizeof(struct Item));
218 for (i = 0; i < 8; i++) {
220 item->next = malloc(sizeof(struct Item));
225 // For interactive mode, try to load world state from savefile.
227 if (1 == world.interactive && 0 == access("savefile", F_OK)) {
228 file = fopen("savefile", "r");
229 world.seed = read_uint32_bigendian(file);
230 world.turn = read_uint32_bigendian(file);
231 player.pos.y = read_uint16_bigendian(file);
232 player.pos.x = read_uint16_bigendian(file);
233 for (monster = world.monster; monster != 0; monster = monster->next) {
234 monster->pos.y = read_uint16_bigendian(file);
235 monster->pos.x = read_uint16_bigendian(file); }
236 for (item = world.item; item != 0; item = item->next) {
237 item->pos.y = read_uint16_bigendian(file);
238 item->pos.x = read_uint16_bigendian(file); }
241 // For non-interactive mode, try to load world state from frecord file.
244 if (0 == world.interactive) {
245 file = fopen("record", "r");
246 world.seed = read_uint32_bigendian(file); }
248 // For interactive-mode in newly started world, generate a start seed from the current time.
250 file = fopen("record", "w");
251 world.seed = time(NULL);
252 write_uint32_bigendian(world.seed, file);
255 // Generate map from seed and, if newly generated world, start positions of actors.
256 rrand(1, world.seed);
257 struct Map map = init_map();
259 if (1 == world.turn) {
260 for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos);) {
261 player.pos.y = rrand(0, 0) % map.size.y;
262 player.pos.x = rrand(0, 0) % map.size.x; }
263 for (item = world.item; item != 0; item = item->next)
264 for (item->pos.y = item->pos.x = 0; 0 == is_passable(&map, item->pos);) {
265 item->pos.y = rrand(0, 0) % map.size.y;
266 item->pos.x = rrand(0, 0) % map.size.x; }
267 for (monster = world.monster; monster != 0; monster = monster->next)
268 for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos);) {
269 monster->pos.y = rrand(0, 0) % map.size.y;
270 monster->pos.x = rrand(0, 0) % map.size.x; } }
272 // Initialize window system and windows.
273 WINDOW * screen = initscr();
276 keypad(screen, TRUE);
278 init_keybindings(&world);
279 struct WinMeta win_meta = init_win_meta(screen);
280 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
281 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
282 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
283 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
284 win_keys.frame.size.x = 29;
285 win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
286 win_info.frame.size.y = 1;
287 win_log.frame.size.y = win_meta.pad.size.y - 3;
288 toggle_window(&win_meta, &win_keys);
289 toggle_window(&win_meta, &win_map);
290 toggle_window(&win_meta, &win_info);
291 toggle_window(&win_meta, &win_log);
295 unsigned char quit_called = 0;
296 if (0 == world.interactive) {
297 unsigned char still_reading_file = 1;
300 if (start_turn == world.turn)
302 if (0 == start_turn) {
303 draw_all_wins (&win_meta);
305 if (1 == still_reading_file &&
306 (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
310 still_reading_file = 0; }
311 else if (0 == action)
312 player_wait (&world);
313 else if (NORTH == action)
314 move_player(&world, NORTH);
315 else if (EAST == action)
316 move_player(&world, EAST);
317 else if (SOUTH == action)
318 move_player(&world, SOUTH);
319 else if (WEST == action)
320 move_player(&world, WEST); }
322 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
323 if (1 == quit_called)
328 uint32_t last_turn = 0;
330 if (last_turn != world.turn) {
332 last_turn = world.turn; }
333 draw_all_wins (&win_meta);
335 if (key == get_action_key(world.keybindings, "player up"))
336 move_player(&world, NORTH);
337 else if (key == get_action_key(world.keybindings, "player right"))
338 move_player(&world, EAST);
339 else if (key == get_action_key(world.keybindings, "player down"))
340 move_player(&world, SOUTH);
341 else if (key == get_action_key(world.keybindings, "player left"))
342 move_player(&world, WEST);
343 else if (key == get_action_key(world.keybindings, "wait / next turn"))
344 player_wait (&world);
346 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
347 if (1 == quit_called)
350 // Clean up and exit.
352 for (key = 0; key <= world.keyswindata->max; key++)
353 free(world.keybindings[key].name);
354 free(world.keybindings);
355 free(world.keyswindata);