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 with new text to be appended.
25 uint16_t len_old = strlen(world->log);
26 uint16_t len_new = strlen(text);
27 uint16_t len_whole = len_old + len_new + 1;
28 new_text = calloc(len_whole, sizeof(char));
29 memcpy(new_text, world->log, len_old);
30 memcpy(new_text + len_old, text, len_new);
32 world->log = new_text; }
34 struct Map init_map () {
35 // Initialize map with some experimental start values.
41 uint32_t size = map.size.x * map.size.y;
42 map.cells = malloc(size);
44 for (y = 0; y < map.size.y; y++)
45 for (x = 0; x < map.size.x; x++)
46 map.cells[(y * map.size.x) + x] = '~';
47 map.cells[size / 2 + (map.size.x / 2)] = '.';
48 uint32_t repeats, root, curpos;
49 for (root = 0; root * root * root < size; root++);
50 for (repeats = 0; repeats < size * root; repeats++) {
51 y = rrand(0, 0) % map.size.y;
52 x = rrand(0, 0) % map.size.x;
53 curpos = y * map.size.x + x;
54 if ('~' == map.cells[curpos] &&
55 ( (curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
56 || (curpos < map.size.x * (map.size.y-1) && '.' == map.cells[curpos + map.size.x])
57 || (curpos > 0 && curpos % map.size.x != 0 && '.' == map.cells[curpos-1])
58 || (curpos < (map.size.x * map.size.y) && (curpos+1) % map.size.x != 0 && '.' == map.cells[curpos+1])))
59 map.cells[y * map.size.x + x] = '.'; }
62 void map_scroll (struct Map * map, char dir) {
63 // Scroll map into direction dir if possible by changing the offset.
64 if (NORTH == dir && map->offset.y > 0) map->offset.y--;
65 else if (SOUTH == dir) map->offset.y++;
66 else if (WEST == dir && map->offset.x > 0) map->offset.x--;
67 else if (EAST == dir) map->offset.x++; }
69 void turn_over (struct World * world, char action) {
70 // Record action in game record file, increment turn and move enemy.
71 if (1 == world->interactive) {
72 FILE * file = fopen("record", "a");
76 rrand(1, world->seed * world->turn);
77 struct Monster * monster;
78 for (monster = world->monster; monster != 0; monster = monster->next)
79 move_monster(world, monster); }
81 void save_game(struct World * world) {
82 // Save game data to game file.
83 FILE * file = fopen("savefile", "w");
84 write_uint32_bigendian(world->seed, file);
85 write_uint32_bigendian(world->turn, file);
86 write_uint16_bigendian(world->player->pos.y, file);
87 write_uint16_bigendian(world->player->pos.x, file);
88 write_uint16_bigendian(world->monster->pos.y, file);
89 write_uint16_bigendian(world->monster->pos.x, file);
90 write_uint16_bigendian(world->monster->next->pos.y, file);
91 write_uint16_bigendian(world->monster->next->pos.x, file);
92 write_uint16_bigendian(world->monster->next->next->pos.y, file);
93 write_uint16_bigendian(world->monster->next->next->pos.x, file);
96 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
97 // Toggle display of window win.
98 if (0 != win->frame.curses_win)
99 suspend_win(win_meta, win);
101 append_win(win_meta, win); }
103 void scroll_pad (struct WinMeta * win_meta, char dir) {
104 // Try to scroll pad left or right.
106 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
108 reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
110 void growshrink_active_window (struct WinMeta * win_meta, char change) {
111 // Grow or shrink active window horizontally or vertically by one cell size.
112 if (0 != win_meta->active) {
113 struct yx_uint16 size = win_meta->active->frame.size;
114 if (change == '-') size.y--;
115 else if (change == '+') size.y++;
116 else if (change == '_') size.x--;
117 else if (change == '*') size.x++;
118 resize_active_win (win_meta, size); } }
120 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
121 struct Win * win_map, struct Win * win_info, struct Win * win_log) {
122 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
123 if (key == get_action_key(world->keybindings, "quit"))
125 else if (key == get_action_key(world->keybindings, "scroll pad right"))
126 scroll_pad (win_meta, '+');
127 else if (key == get_action_key(world->keybindings, "scroll pad left"))
128 scroll_pad (win_meta, '-');
129 else if (key == get_action_key(world->keybindings, "toggle keys window"))
130 toggle_window(win_meta, win_keys);
131 else if (key == get_action_key(world->keybindings, "toggle map window"))
132 toggle_window(win_meta, win_map);
133 else if (key == get_action_key(world->keybindings, "toggle info window"))
134 toggle_window(win_meta, win_info);
135 else if (key == get_action_key(world->keybindings, "toggle log window"))
136 toggle_window(win_meta, win_log);
137 else if (key == get_action_key(world->keybindings, "cycle forwards"))
138 cycle_active_win(win_meta, 'n');
139 else if (key == get_action_key(world->keybindings, "cycle backwards"))
140 cycle_active_win(win_meta, 'p');
141 else if (key == get_action_key(world->keybindings, "shift forwards"))
142 shift_active_win(win_meta, 'f');
143 else if (key == get_action_key(world->keybindings, "shift backwards"))
144 shift_active_win(win_meta, 'b');
145 else if (key == get_action_key(world->keybindings, "grow horizontally"))
146 growshrink_active_window(win_meta, '*');
147 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
148 growshrink_active_window(win_meta, '_');
149 else if (key == get_action_key(world->keybindings, "grow vertically"))
150 growshrink_active_window(win_meta, '+');
151 else if (key == get_action_key(world->keybindings, "shrink vertically"))
152 growshrink_active_window(win_meta, '-');
153 else if (key == get_action_key(world->keybindings, "save keys"))
154 save_keybindings(world);
155 else if (key == get_action_key(world->keybindings, "keys nav up"))
156 keyswin_move_selection (world, 'u');
157 else if (key == get_action_key(world->keybindings, "keys nav down"))
158 keyswin_move_selection (world, 'd');
159 else if (key == get_action_key(world->keybindings, "keys mod"))
160 keyswin_mod_key (world, win_meta);
161 else if (key == get_action_key(world->keybindings, "map up"))
162 map_scroll (world->map, NORTH);
163 else if (key == get_action_key(world->keybindings, "map down"))
164 map_scroll (world->map, SOUTH);
165 else if (key == get_action_key(world->keybindings, "map right"))
166 map_scroll (world->map, EAST);
167 else if (key == get_action_key(world->keybindings, "map left"))
168 map_scroll (world->map, WEST);
171 int main (int argc, char *argv[]) {
174 // Read in startup options (i.e. replay option and replay start turn).
177 world.interactive = 1;
178 while ((opt = getopt(argc, argv, "s::")) != -1) {
181 world.interactive = 0;
184 start_turn = atoi(optarg);
187 exit(EXIT_FAILURE); } }
189 // Initialize log, player and monsters.
190 world.log = calloc(1, sizeof(char));
191 update_log (&world, " ");
192 struct Player player;
193 world.player = &player;
194 struct Monster monster1;
195 struct Monster monster2;
196 struct Monster monster3;
197 world.monster = &monster1;
198 monster1.next = &monster2;
199 monster2.next = &monster3;
205 // For interactive mode, try to load world state from savefile.
207 if (1 == world.interactive && 0 == access("savefile", F_OK)) {
208 file = fopen("savefile", "r");
209 world.seed = read_uint32_bigendian(file);
210 world.turn = read_uint32_bigendian(file);
211 player.pos.y = read_uint16_bigendian(file);
212 player.pos.x = read_uint16_bigendian(file);
213 monster1.pos.y = read_uint16_bigendian(file);
214 monster1.pos.x = read_uint16_bigendian(file);
215 monster2.pos.y = read_uint16_bigendian(file);
216 monster2.pos.x = read_uint16_bigendian(file);
217 monster3.pos.y = read_uint16_bigendian(file);
218 monster3.pos.x = read_uint16_bigendian(file);
221 // For non-interactive mode, try to load world state from frecord file.
224 if (0 == world.interactive) {
225 file = fopen("record", "r");
226 world.seed = read_uint32_bigendian(file); }
228 // For interactive-mode in newly started world, generate a start seed from the current time.
230 file = fopen("record", "w");
231 world.seed = time(NULL);
232 write_uint32_bigendian(world.seed, file);
235 // Generate map from seed and, if newly generated world, start positions of actors.
236 rrand(1, world.seed);
237 struct Map map = init_map();
239 if (1 == world.turn) {
240 for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos);) {
241 player.pos.y = rrand(0, 0) % map.size.y;
242 player.pos.x = rrand(0, 0) % map.size.x; }
243 struct Monster * monster;
244 for (monster = world.monster; monster != 0; monster = monster->next)
245 for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos);) {
246 monster->pos.y = rrand(0, 0) % map.size.y;
247 monster->pos.x = rrand(0, 0) % map.size.x; } }
249 // Initialize window system and windows.
250 WINDOW * screen = initscr();
253 keypad(screen, TRUE);
255 init_keybindings(&world);
256 struct WinMeta win_meta = init_win_meta(screen);
257 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
258 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
259 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
260 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
261 win_keys.frame.size.x = 29;
262 win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
263 win_info.frame.size.y = 1;
264 win_log.frame.size.y = win_meta.pad.size.y - 3;
265 toggle_window(&win_meta, &win_keys);
266 toggle_window(&win_meta, &win_map);
267 toggle_window(&win_meta, &win_info);
268 toggle_window(&win_meta, &win_log);
272 unsigned char quit_called = 0;
273 if (0 == world.interactive) {
274 unsigned char still_reading_file = 1;
277 if (start_turn == world.turn)
279 if (0 == start_turn) {
280 draw_all_wins (&win_meta);
282 if (1 == still_reading_file &&
283 (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
287 still_reading_file = 0; }
288 else if (0 == action)
289 player_wait (&world);
290 else if (NORTH == action)
291 move_player(&world, NORTH);
292 else if (EAST == action)
293 move_player(&world, EAST);
294 else if (SOUTH == action)
295 move_player(&world, SOUTH);
296 else if (WEST == action)
297 move_player(&world, WEST); }
299 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
300 if (1 == quit_called)
305 uint32_t last_turn = 0;
307 if (last_turn != world.turn) {
309 last_turn = world.turn; }
310 draw_all_wins (&win_meta);
312 if (key == get_action_key(world.keybindings, "player up"))
313 move_player(&world, NORTH);
314 else if (key == get_action_key(world.keybindings, "player right"))
315 move_player(&world, EAST);
316 else if (key == get_action_key(world.keybindings, "player down"))
317 move_player(&world, SOUTH);
318 else if (key == get_action_key(world.keybindings, "player left"))
319 move_player(&world, WEST);
320 else if (key == get_action_key(world.keybindings, "wait / next turn"))
321 player_wait (&world);
323 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
324 if (1 == quit_called)
327 // Clean up and exit.
329 for (key = 0; key <= world.keyswindata->max; key++)
330 free(world.keybindings[key].name);
331 free(world.keybindings);
332 free(world.keyswindata);