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)] = '.';
61 y = rrand(0, 0) % map.size.y;
62 x = rrand(0, 0) % map.size.x;
63 curpos = y * map.size.x + x;
64 if ('~' == map.cells[curpos] &&
65 ( (curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
66 || (curpos < map.size.x * (map.size.y-1) && '.' == map.cells[curpos + map.size.x])
67 || (curpos > 0 && curpos % map.size.x != 0 && '.' == map.cells[curpos-1])
68 || (curpos < (map.size.x * map.size.y) && (curpos+1) % map.size.x != 0 && '.' == map.cells[curpos+1]))) {
69 if (y == 0 || y == map.size.y - 1 || x == 0 || x == map.size.x - 1)
71 map.cells[y * map.size.x + x] = '.'; } }
74 void map_scroll (struct Map * map, char dir) {
75 // Scroll map into direction dir if possible by changing the offset.
76 if (NORTH == dir && map->offset.y > 0) map->offset.y--;
77 else if (SOUTH == dir) map->offset.y++;
78 else if (WEST == dir && map->offset.x > 0) map->offset.x--;
79 else if (EAST == dir) map->offset.x++; }
81 void turn_over (struct World * world, char action) {
82 // Record action in game record file, increment turn and move enemy.
83 if (1 == world->interactive) {
84 FILE * file = fopen("record", "a");
88 rrand(1, world->seed * world->turn);
89 struct Monster * monster;
90 for (monster = world->monster; monster != 0; monster = monster->cmo.next)
91 move_monster(world, monster); }
93 void save_game(struct World * world) {
94 // Save game data to game file.
95 FILE * file = fopen("savefile", "w");
96 write_uint32_bigendian(world->seed, file);
97 write_uint32_bigendian(world->turn, file);
98 write_uint16_bigendian(world->player->pos.y + 1, file);
99 write_uint16_bigendian(world->player->pos.x + 1, file);
100 write_map_objects (world->monster, file, write_map_objects_monsterdata);
101 write_map_objects (world->item, file, readwrite_map_objects_dummy);
104 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
105 // Toggle display of window win.
106 if (0 != win->frame.curses_win)
107 suspend_win(win_meta, win);
109 append_win(win_meta, win); }
111 void scroll_pad (struct WinMeta * win_meta, char dir) {
112 // Try to scroll pad left or right.
114 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
116 reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
118 void growshrink_active_window (struct WinMeta * win_meta, char change) {
119 // Grow or shrink active window horizontally or vertically by one cell size.
120 if (0 != win_meta->active) {
121 struct yx_uint16 size = win_meta->active->frame.size;
122 if (change == '-') size.y--;
123 else if (change == '+') size.y++;
124 else if (change == '_') size.x--;
125 else if (change == '*') size.x++;
126 resize_active_win (win_meta, size); } }
128 unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
129 struct Win * win_map, struct Win * win_info, struct Win * win_log) {
130 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
131 if (key == get_action_key(world->keybindings, "quit"))
133 else if (key == get_action_key(world->keybindings, "scroll pad right"))
134 scroll_pad (win_meta, '+');
135 else if (key == get_action_key(world->keybindings, "scroll pad left"))
136 scroll_pad (win_meta, '-');
137 else if (key == get_action_key(world->keybindings, "toggle keys window"))
138 toggle_window(win_meta, win_keys);
139 else if (key == get_action_key(world->keybindings, "toggle map window"))
140 toggle_window(win_meta, win_map);
141 else if (key == get_action_key(world->keybindings, "toggle info window"))
142 toggle_window(win_meta, win_info);
143 else if (key == get_action_key(world->keybindings, "toggle log window"))
144 toggle_window(win_meta, win_log);
145 else if (key == get_action_key(world->keybindings, "cycle forwards"))
146 cycle_active_win(win_meta, 'n');
147 else if (key == get_action_key(world->keybindings, "cycle backwards"))
148 cycle_active_win(win_meta, 'p');
149 else if (key == get_action_key(world->keybindings, "shift forwards"))
150 shift_active_win(win_meta, 'f');
151 else if (key == get_action_key(world->keybindings, "shift backwards"))
152 shift_active_win(win_meta, 'b');
153 else if (key == get_action_key(world->keybindings, "grow horizontally"))
154 growshrink_active_window(win_meta, '*');
155 else if (key == get_action_key(world->keybindings, "shrink horizontally"))
156 growshrink_active_window(win_meta, '_');
157 else if (key == get_action_key(world->keybindings, "grow vertically"))
158 growshrink_active_window(win_meta, '+');
159 else if (key == get_action_key(world->keybindings, "shrink vertically"))
160 growshrink_active_window(win_meta, '-');
161 else if (key == get_action_key(world->keybindings, "save keys"))
162 save_keybindings(world);
163 else if (key == get_action_key(world->keybindings, "keys nav up"))
164 keyswin_move_selection (world, 'u');
165 else if (key == get_action_key(world->keybindings, "keys nav down"))
166 keyswin_move_selection (world, 'd');
167 else if (key == get_action_key(world->keybindings, "keys mod"))
168 keyswin_mod_key (world, win_meta);
169 else if (key == get_action_key(world->keybindings, "map up"))
170 map_scroll (world->map, NORTH);
171 else if (key == get_action_key(world->keybindings, "map down"))
172 map_scroll (world->map, SOUTH);
173 else if (key == get_action_key(world->keybindings, "map right"))
174 map_scroll (world->map, EAST);
175 else if (key == get_action_key(world->keybindings, "map left"))
176 map_scroll (world->map, WEST);
179 int main (int argc, char *argv[]) {
182 // Read in startup options (i.e. replay option and replay start turn).
185 world.interactive = 1;
186 while ((opt = getopt(argc, argv, "s::")) != -1) {
189 world.interactive = 0;
192 start_turn = atoi(optarg);
195 exit(EXIT_FAILURE); } }
197 // Initialize log, player, monsters and items.
198 world.log = calloc(1, sizeof(char));
199 update_log (&world, " ");
200 struct Player player;
201 player.hitpoints = 5;
202 world.player = &player;
206 // For interactive mode, try to load world state from savefile.
208 if (1 == world.interactive && 0 == access("savefile", F_OK)) {
209 file = fopen("savefile", "r");
210 world.seed = read_uint32_bigendian(file);
211 world.turn = read_uint32_bigendian(file);
212 player.pos.y = read_uint16_bigendian(file) - 1;
213 player.pos.x = read_uint16_bigendian(file) - 1;
214 read_map_objects (&world.monster, file, sizeof(struct Monster), read_map_objects_monsterdata);
215 read_map_objects (&world.item, file, sizeof(struct Item), readwrite_map_objects_dummy);
218 // For non-interactive mode, try to load world state from record file.
221 if (0 == world.interactive) {
222 file = fopen("record", "r");
223 world.seed = read_uint32_bigendian(file); }
225 // For interactive-mode in newly started world, generate a start seed from the current time.
227 file = fopen("record", "w");
228 world.seed = time(NULL);
229 write_uint32_bigendian(world.seed, file);
232 // Generate map from seed and, if newly generated world, start positions of actors.
233 rrand(1, world.seed);
234 struct Map map = init_map();
236 if (1 == world.turn) {
237 player.pos = find_passable_pos(&map);
238 unsigned char n_monsters = rrand(0, 0) % 16;
239 unsigned char n_items = rrand(0, 0) % 48;
240 build_map_objects (&world.monster, n_monsters, sizeof(struct Monster), build_map_objects_monsterdata, &map);
241 build_map_objects (&world.item, n_items, sizeof(struct Item), build_map_objects_itemdata, &map); }
243 // Initialize window system and windows.
244 WINDOW * screen = initscr();
247 keypad(screen, TRUE);
249 init_keybindings(&world);
250 struct WinMeta win_meta = init_win_meta(screen);
251 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
252 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
253 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
254 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
255 win_keys.frame.size.x = 29;
256 win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
257 win_info.frame.size.y = 2;
258 win_log.frame.size.y = win_meta.pad.size.y - (2 + win_info.frame.size.y);
259 toggle_window(&win_meta, &win_keys);
260 toggle_window(&win_meta, &win_map);
261 toggle_window(&win_meta, &win_info);
262 toggle_window(&win_meta, &win_log);
266 unsigned char quit_called = 0;
267 unsigned char await_actions = 1;
268 if (0 == world.interactive) {
271 if (start_turn == world.turn)
273 if (0 == start_turn) {
274 draw_all_wins (&win_meta);
276 if (1 == await_actions &&
277 (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
282 else if (0 == action)
283 player_wait (&world);
284 else if (NORTH == action)
285 move_player(&world, NORTH);
286 else if (EAST == action)
287 move_player(&world, EAST);
288 else if (SOUTH == action)
289 move_player(&world, SOUTH);
290 else if (WEST == action)
291 move_player(&world, WEST); }
293 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
294 if (1 == quit_called)
299 uint32_t last_turn = 0;
301 if (last_turn != world.turn) {
303 last_turn = world.turn; }
304 if (1 == await_actions && 0 == player.hitpoints)
306 draw_all_wins (&win_meta);
308 if (1 == await_actions && key == get_action_key(world.keybindings, "player up"))
309 move_player(&world, NORTH);
310 else if (1 == await_actions && key == get_action_key(world.keybindings, "player right"))
311 move_player(&world, EAST);
312 else if (1 == await_actions && key == get_action_key(world.keybindings, "player down"))
313 move_player(&world, SOUTH);
314 else if (1 == await_actions && key == get_action_key(world.keybindings, "player left"))
315 move_player(&world, WEST);
316 else if (1 == await_actions && key == get_action_key(world.keybindings, "wait / next turn"))
317 player_wait (&world);
319 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
320 if (1 == quit_called)
323 // Clean up and exit.
325 for (key = 0; key <= world.keyswindata->max; key++)
326 free(world.keybindings[key].name);
327 free(world.keybindings);
328 free(world.keyswindata);