8 struct KeyBinding * keybindings;
9 struct KeysWinData * keyswindata;
13 struct Player * player; };
35 void draw_with_linebreaks (struct Win *, char *, int);
36 void draw_text_from_bottom (struct Win *, char *);
37 void draw_log (struct Win *);
38 void draw_map (struct Win *);
39 void draw_info (struct Win *);
40 void draw_keys_window (struct Win *);
41 void toggle_window (struct WinMeta *, struct Win *);
42 void init_keybindings(struct World *);
43 struct Map init_map ();
44 void update_info (struct World *);
45 void update_log (struct World *, char *);
46 void save_keybindings(struct World *);
47 int get_action_key (struct KeyBinding *, char *);
48 char * get_keyname(int);
49 char is_passable (struct World *, int, int);
50 void move_player (struct World *, char);
52 void draw_with_linebreaks (struct Win * win, char * text, int start_y) {
53 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
58 for (y = start_y; y < win->height; y++) {
61 for (x = 0; x < win->width; x++) {
64 if ('\n' == text[z]) {
68 mvwaddch(win->curses, y, x, text[z]);
69 if ('\n' == text[z+1]) {
72 else if (0 == text[z+1]) {
76 void draw_text_from_bottom (struct Win * win, char * text) {
77 // Draw text from end/bottom to the top.
81 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in
82 for (x = 0; x < win->width; x++) { // a window of available width, but infinite height.
84 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and stopping
85 break; // the loop. Make sure they don't count as cell space themselves.
86 if ('\n' == text[z+1]) {
89 else if (0 == text[z+1]) {
94 if (y < win->height) // Depending on what is bigger, determine start point in window or in text.
95 start_y = win->height - y;
96 else if (y > win->height) {
97 offset = y - win->height;
98 for (y = 0; y < offset; y++)
99 for (x = 0; x < win->width; x++) {
103 if ('\n' == text[z+1]) {
106 text = text + (sizeof(char) * (z + 1)); }
107 draw_with_linebreaks(win, text, start_y); }
109 void draw_log (struct Win * win) {
110 // Draw log text from world struct in win->data from bottom to top.
111 struct World * world = (struct World *) win->data;
112 draw_text_from_bottom(win, world->log); }
114 void draw_map (struct Win * win) {
115 // Draw map determined by win->data Map struct into window. Respect offset.
116 struct World * world = (struct World *) win->data;
117 struct Map * map = world->map;
118 struct Player * player = world->player;
119 char * cells = map->cells;
120 int width_map_av = map->width - map->offset_x;
121 int height_map_av = map->height - map->offset_y;
123 for (y = 0; y < win->height; y++) {
124 z = map->offset_x + (map->offset_y + y) * (map->width);
125 for (x = 0; x < win->width; x++) {
126 if (y < height_map_av && x < width_map_av) {
127 if (z == (map->width * player->y) + player->x)
128 mvwaddch(win->curses, y, x, '@');
130 mvwaddch(win->curses, y, x, cells[z]);
133 void draw_info (struct Win * win) {
134 // Draw info window by appending win->data integer value to "Turn: " display.
135 struct World * world = (struct World *) win->data;
136 int count = world->turn;
138 snprintf(text, 100, "Turn: %d", count);
139 draw_with_linebreaks(win, text, 0); }
141 void draw_keys_window (struct Win * win) {
142 // Draw keybinding window.
143 struct World * world = (struct World *) win->data;
144 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
145 struct KeyBinding * keybindings = world->keybindings;
147 if (keyswindata->max >= win->height) {
148 if (keyswindata->select > win->height / 2) {
149 if (keyswindata->select < (keyswindata->max - (win->height / 2)))
150 offset = keyswindata->select - (win->height / 2);
152 offset = keyswindata->max - win->height + 1; } }
153 int keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
154 char * keydesc = malloc(keydescwidth);
158 for (y = 0; y <= keyswindata->max && y < win->height; y++) {
160 if (y == keyswindata->select - offset) {
162 if (1 == keyswindata->edit)
163 attri = attri | A_BLINK; }
164 keyname = get_keyname(keybindings[y + offset].key);
165 snprintf(keydesc, keydescwidth, "%-9s", keyname);
167 for (x = 0; x < win->width; x++)
168 if (x < strlen(keydesc))
169 mvwaddch(win->curses, y, x, keydesc[x] | attri);
170 else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
171 mvwaddch(win->curses, y, x, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
173 mvwaddch(win->curses, y, x, ' ' | attri); }
176 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
177 // Toggle display of window win.
178 if (0 != win->curses)
179 suspend_window(win_meta, win);
181 append_window(win_meta, win); }
183 void init_keybindings(struct World * world) {
184 // Initialize keybindings from file "keybindings".
185 FILE * file = fopen("keybindings", "r");
194 if (c_count > linemax)
195 linemax = c_count + 1;
198 struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
199 fseek(file, 0, SEEK_SET);
200 char * command = malloc(linemax);
203 while (fgets(command, linemax, file)) {
204 keybindings[commcount].key = atoi(command);
205 cmdptr = strchr(command, ' ') + 1;
206 keybindings[commcount].name = malloc(strlen(cmdptr));
207 memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1);
208 keybindings[commcount].name[strlen(cmdptr) - 1] = '\0';
212 struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
213 keyswindata->max = lines - 1;
214 keyswindata->select = 0;
215 keyswindata->edit = 0;
216 world->keybindings = keybindings;
217 world->keyswindata = keyswindata; }
219 struct Map init_map () {
220 // Initialize map with some experimental start values.
226 map.cells = malloc(map.width * map.height);
228 for (y = 0; y < map.height; y++)
229 for (x = 0; x < map.width; x++)
230 map.cells[(y * map.width) + x] = '.';
231 map.cells[(5 * map.width) + 5] = 'X';
232 map.cells[(3 * map.width) + 8] = 'X';
233 map.cells[(8 * map.width) + 3] = 'X';
236 void update_info (struct World * world) {
237 // Update info data by incrementing turn value.
240 void update_log (struct World * world, char * text) {
241 // Update log with new text to be appended.
243 int len_old = strlen(world->log);
244 int len_new = strlen(text);
245 int len_whole = len_old + len_new + 1;
246 new_text = calloc(len_whole, sizeof(char));
247 memcpy(new_text, world->log, len_old);
248 memcpy(new_text + len_old, text, len_new);
250 world->log = new_text; }
252 void save_keybindings(struct World * world) {
253 // Write keybindings to keybindings file.
254 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
255 struct KeyBinding * keybindings = world->keybindings;
256 FILE * file = fopen("keybindings", "w");
259 for (i = 0; i <= keyswindata->max; i++)
260 if (strlen(keybindings[i].name) > linemax)
261 linemax = strlen(keybindings[i].name);
262 linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte
263 char * line = malloc(linemax);
264 for (i = 0; i <= keyswindata->max; i++) {
265 snprintf(line, linemax, "%d %s\n", keybindings[i].key, keybindings[i].name);
266 fwrite(line, sizeof(char), strlen(line), file); }
270 int get_action_key (struct KeyBinding * keybindings, char * name) {
271 // Return key matching name in keybindings.
273 while (strcmp(keybindings[i].name, name) )
275 return keybindings[i].key; }
277 char * get_keyname(int keycode) {
278 // Translate some keycodes to readable names of max 9 chars.
280 keyname = malloc(15);
281 if (32 < keycode && keycode < 127)
282 sprintf(keyname, "%c", keycode);
283 else if (keycode == 9)
284 sprintf(keyname, "TAB");
285 else if (keycode == 10)
286 sprintf(keyname, "RETURN");
287 else if (keycode == 27)
288 sprintf(keyname, "ESCAPE");
289 else if (keycode == 32)
290 sprintf(keyname, "SPACE");
291 else if (keycode == KEY_UP)
292 sprintf(keyname, "UP");
293 else if (keycode == KEY_DOWN)
294 sprintf(keyname, "DOWN");
295 else if (keycode == KEY_LEFT)
296 sprintf(keyname, "LEFT");
297 else if (keycode == KEY_RIGHT)
298 sprintf(keyname, "RIGHT");
299 else if (keycode == KEY_HOME)
300 sprintf(keyname, "HOME");
301 else if (keycode == KEY_BACKSPACE)
302 sprintf(keyname, "BACKSPACE");
303 else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) {
304 int f = keycode - KEY_F0;
305 sprintf(keyname, "F%d", f); }
306 else if (keycode == KEY_DC)
307 sprintf(keyname, "DELETE");
308 else if (keycode == KEY_IC)
309 sprintf(keyname, "INSERT");
310 else if (keycode == KEY_NPAGE)
311 sprintf(keyname, "NEXT PAGE");
312 else if (keycode == KEY_PPAGE)
313 sprintf(keyname, "PREV PAGE");
314 else if (keycode == KEY_END)
315 sprintf(keyname, "END");
317 sprintf(keyname, "(unknown)");
320 char is_passable (struct World * world, int y, int x) {
321 // Check if coordinate on (or beyond) map is accessible to movement.
323 if (0 <= x && x < world->map->width && 0 <= y && y < world->map->height)
324 if ('.' == world->map->cells[y * world->map->width + x])
328 void move_player (struct World * world, char d) {
329 // Move player in direction d, increment turn counter and update log.
333 char * msg = calloc(25, sizeof(char));
336 if (is_passable(world, world->player->x, world->player->y + 1)) {
341 if (is_passable(world, world->player->x, world->player->y - 1)) {
346 if (is_passable(world, world->player->x - 1, world->player->y)) {
351 if (is_passable(world, world->player->x + 1, world->player->y)) {
354 char * msg_content = "You fail to move";
356 msg_content = "You move";
357 sprintf(msg, "\n%s %s.", msg_content, dir);
358 update_log (world, msg);
363 init_keybindings(&world);
365 world.log = calloc(1, sizeof(char));
366 update_log (&world, "Start!");
367 struct Map map = init_map();
369 struct Player player;
372 world.player = &player;
374 WINDOW * screen = initscr();
377 keypad(screen, TRUE);
379 struct WinMeta win_meta = init_win_meta(screen);
381 struct Win win_keys = init_window(&win_meta, "Keys");
382 win_keys.draw = draw_keys_window;
383 win_keys.data = &world;
384 struct Win win_map = init_window(&win_meta, "Map");
385 win_map.draw = draw_map;
386 win_map.data = &world;
387 struct Win win_info = init_window(&win_meta, "Info");
388 win_info.draw = draw_info;
389 win_info.data = &world;
390 struct Win win_log = init_window(&win_meta, "Log");
391 win_log.draw = draw_log;
392 win_log.data = &world;
396 draw_all_windows (&win_meta);
398 if (key == get_action_key(world.keybindings, "quit"))
400 else if (key == get_action_key(world.keybindings, "scroll pad right"))
401 win_meta.pad_offset++;
402 else if (key == get_action_key(world.keybindings, "scroll pad left") && win_meta.pad_offset > 0)
403 win_meta.pad_offset--;
404 else if (key == get_action_key(world.keybindings, "toggle keys window"))
405 toggle_window(&win_meta, &win_keys);
406 else if (key == get_action_key(world.keybindings, "toggle map window"))
407 toggle_window(&win_meta, &win_map);
408 else if (key == get_action_key(world.keybindings, "toggle info window"))
409 toggle_window(&win_meta, &win_info);
410 else if (key == get_action_key(world.keybindings, "toggle log window"))
411 toggle_window(&win_meta, &win_log);
412 else if (key == get_action_key(world.keybindings, "cycle forwards") && win_meta.active != 0)
413 cycle_active_window(&win_meta, 'n');
414 else if (key == get_action_key(world.keybindings, "cycle backwards") && win_meta.active != 0)
415 cycle_active_window(&win_meta, 'p');
416 else if (key == get_action_key(world.keybindings, "shift forwards") && win_meta.active != 0)
417 shift_window(&win_meta, 'f');
418 else if (key == get_action_key(world.keybindings, "shift backwards") && win_meta.active != 0)
419 shift_window(&win_meta, 'b');
420 else if (key == get_action_key(world.keybindings, "grow horizontally") && win_meta.active != 0)
421 resize_window(&win_meta, '*');
422 else if (key == get_action_key(world.keybindings, "shrink horizontally") && win_meta.active != 0)
423 resize_window(&win_meta, '_');
424 else if (key == get_action_key(world.keybindings, "grow vertically") && win_meta.active != 0)
425 resize_window(&win_meta, '+');
426 else if (key == get_action_key(world.keybindings, "shrink vertically") && win_meta.active != 0)
427 resize_window(&win_meta, '-');
428 else if (key == get_action_key(world.keybindings, "save keys"))
429 save_keybindings(&world);
430 else if (key == get_action_key(world.keybindings, "keys nav up") && world.keyswindata->select > 0)
431 world.keyswindata->select--;
432 else if (key == get_action_key(world.keybindings, "keys nav down") && world.keyswindata->select < world.keyswindata->max)
433 world.keyswindata->select++;
434 else if (key == get_action_key(world.keybindings, "keys mod")) {
435 world.keyswindata->edit = 1;
436 draw_all_windows (&win_meta);
438 if (key < 1000) // ensure maximum of three digits in key code field
439 world.keybindings[world.keyswindata->select].key = key;
440 world.keyswindata->edit = 0; }
441 else if (key == get_action_key(world.keybindings, "map up") && map.offset_y > 0)
443 else if (key == get_action_key(world.keybindings, "map down"))
445 else if (key == get_action_key(world.keybindings, "map right"))
447 else if (key == get_action_key(world.keybindings, "map left") && map.offset_x > 0)
449 else if (key == get_action_key(world.keybindings, "player down"))
450 move_player(&world, 's');
451 else if (key == get_action_key(world.keybindings, "player up"))
452 move_player(&world, 'n');
453 else if (key == get_action_key(world.keybindings, "player right"))
454 move_player(&world, 'e');
455 else if (key == get_action_key(world.keybindings, "player left"))
456 move_player(&world, 'w');
457 else if (key == get_action_key(world.keybindings, "wait") ) {
458 update_info (&world);
459 update_log (&world, "\nYou wait."); } }
462 for (key = 0; key <= world.keyswindata->max; key++)
463 free(world.keybindings[key].name);
464 free(world.keybindings);
465 free(world.keyswindata);