27 struct KeyBinding * keybindings;
28 struct KeysWinData * keyswindata; };
30 void draw_with_linebreaks (struct Win * win, char * text, int start_y) {
31 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
34 int height_av = win->height - 1;
35 int width_av = win->width - 1;
38 for (y = start_y; y < height_av; y++) {
41 for (x = 0; x < width_av; x++) {
44 if ('\n' == text[z]) {
45 mvwaddch(win->curses_win, y+1, x+win->border_left, ' ');
49 mvwaddch(win->curses_win, y+1, x+win->border_left, text[z]);
50 if ('\n' == text[z+1]) {
53 else if (0 == text[z+1]) {
57 mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); } } }
59 void draw_text_from_bottom (struct Win * win) {
60 // Draw text in win->data from end/bottom to the top.
61 char * text = (char *) win->data;
62 int width_av = win->width - 1;
63 int height_av = win->height - 1;
67 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in a window of available
68 for (x = 0; x < width_av; x++) { // width, but infinite height.
70 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and
71 break; // stopping the loop. Make sure they don't count as cell space
72 if ('\n' == text[z+1]) { // themselves.
75 else if (0 == text[z+1]) {
80 if (y < height_av) { // Depending on what is bigger, determine start point in window or in text.
81 start_y = height_av - y;
82 for (y = 0; y < start_y; y++)
83 for (x = 0; x < width_av; x++)
84 mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); }
85 else if (y > height_av) {
86 offset = y - height_av;
87 for (y = 0; y < offset; y++)
88 for (x = 0; x < width_av; x++) {
92 if ('\n' == text[z+1]) {
95 text = text + (sizeof(char) * (z + 1)); }
96 draw_with_linebreaks(win, text, start_y); }
98 void draw_map (struct Win * win) {
99 // Draw map determined by win->data Map struct into window. Respect offset.
100 int height_av = win->height - 1;
101 int width_av = win->width - 1;
102 struct Map map = * (struct Map *) win->data;
103 char * cells = map.cells;
104 int width_map_av = map.width - map.offset_x;
105 int height_map_av = map.height - map.offset_y;
107 for (y = 0; y < height_av; y++) {
108 z = map.offset_x + (map.offset_y + y) * (map.width);
109 for (x = 0; x < width_av; x++) {
110 if (y < height_map_av && x < width_map_av) {
111 if (z == (map.width * map.player_y) + map.player_x)
112 mvwaddch(win->curses_win, y+1, x+win->border_left, '@');
114 mvwaddch(win->curses_win, y+1, x+win->border_left, cells[z]);
117 mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); } } }
119 void draw_info (struct Win * win) {
120 // Draw info window by appending win->data integer value to "Turn: " display.
121 int count = * (int *) win->data;
123 snprintf(text, 100, "Turn: %d", count);
124 draw_with_linebreaks(win, text, 0); }
126 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
127 // Toggle display of window win.
128 if (0 != win->curses_win)
129 suspend_window(win_meta, win);
131 append_window(win_meta, win); }
133 struct Map init_map () {
134 // Initialize map with some experimental start values.
142 map.cells = malloc(map.width * map.height);
144 for (y = 0; y < map.height; y++)
145 for (x = 0; x < map.width; x++)
146 map.cells[(y * map.width) + x] = '.';
147 map.cells[(5 * map.width) + 5] = 'X';
148 map.cells[(3 * map.width) + 8] = 'X';
149 map.cells[(8 * map.width) + 3] = 'X';
152 void update_info (struct Win * win) {
153 // Update info data by incrementing turn value.
154 * (int *) win->data = * (int *) win->data + 1; }
156 void update_log (struct Win * win, char * text) {
157 // Update log with new text to be appended.
159 int len_old = strlen(win->data);
160 int len_new = strlen(text);
161 int len_whole = len_old + len_new + 1;
162 new_text = calloc(len_whole, sizeof(char));
163 memcpy(new_text, win->data, len_old);
164 memcpy(new_text + len_old, text, len_new);
166 win->data = new_text; }
168 int get_action_key (struct KeyBinding * keybindings, char * name) {
169 // Return key matching name in keybindings.
171 while (strcmp(keybindings[i].name, name) )
173 return keybindings[i].key; }
175 void draw_keys_window (struct Win * win) {
176 // Draw keybinding window.
177 struct World * world = (struct World *) win->data;
178 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
179 struct KeyBinding * keybindings = world->keybindings;
180 int height_av = win->height - 1;
181 int width_av = win->width - 1;
183 if (keyswindata->max >= height_av) {
184 if (keyswindata->select > height_av / 2)
185 if (keyswindata->select < (keyswindata->max - (height_av / 2)))
186 offset = keyswindata->select - (height_av / 2);
188 offset = keyswindata->max - height_av + 1; }
189 int keydescwidth = 3;
190 char * keydesc = malloc(keydescwidth + 1);
193 for (y = 0; 0 != keybindings[offset + y].name && y < height_av; y++) {
195 if (y == keyswindata->select - offset) {
197 if (1 == keyswindata->edit)
198 attri = attri | A_BLINK; }
199 snprintf(keydesc, keydescwidth + 1, "%3d ", keybindings[y + offset].key);
200 for (x = 0; x < width_av; x++)
201 if (strlen(keydesc) > x)
202 mvwaddch(win->curses_win, y + 1, x + win->border_left, keydesc[x] | attri);
203 else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
204 mvwaddch(win->curses_win, y + 1, x + win->border_left, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
206 mvwaddch(win->curses_win, y + 1, x + win->border_left, ' ' | attri); }
209 for (; y < height_av; y++)
210 for (x = 0; x < width_av; x++)
211 mvwaddch(win->curses_win, y + 1, x + win->border_left, ' '); }
213 void init_keybindings(struct World * world) {
214 // Initialize keybindings from file "keybindings".
215 FILE * file = fopen("keybindings", "r");
224 if (c_count > linemax)
228 struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
229 fseek(file, 0, SEEK_SET);
230 char * command = malloc(linemax);
232 char * digits = malloc(3);
239 keybindings[keycount].name = calloc(commcount, sizeof(char));
240 memcpy(keybindings[keycount].name, command, commcount);
241 keybindings[keycount].key = key;
245 else if (-1 != digicount)
248 digimax = digicount - 1;
249 while (digicount > 0) {
251 key = key + ((digits[digicount] - 48) * pow(10, digimax - digicount)); }
254 digits[digicount] = c;
257 command[commcount] = c;
263 struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
264 keyswindata->max = lines - 1;
265 keyswindata->select = 0;
266 keyswindata->edit = 0;
267 world->keybindings = keybindings;
268 world->keyswindata = keyswindata; }
270 void save_keybindings(struct World * world) {
271 // Write keybindings to keybindings file.
272 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
273 struct KeyBinding * keybindings = world->keybindings;
274 FILE * file = fopen("keybindings", "w");
277 for (i = 0; i <= keyswindata->max; i++)
278 if (strlen(keybindings[i].name) > linemax)
279 linemax = strlen(keybindings[i].name);
280 linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte
281 char * line = malloc(linemax);
282 for (i = 0; i <= keyswindata->max; i++) {
283 snprintf(line, linemax, "%d %s\n", keybindings[i].key, keybindings[i].name);
284 fwrite(line, sizeof(char), strlen(line), file); }
290 init_keybindings(&world);
292 WINDOW * screen = initscr();
295 keypad(screen, TRUE);
298 struct WinMeta win_meta = init_win_meta(screen);
300 struct Win win_keys = init_window(&win_meta, "Keys");
301 win_keys.draw = draw_keys_window;
302 win_keys.data = &world;
304 struct Win win_map = init_window(&win_meta, "Map");
305 win_map.draw = draw_map;
306 struct Map map = init_map();
309 struct Win win_info = init_window(&win_meta, "Info");
310 win_info.draw = draw_info;
311 win_info.data = malloc(sizeof(int));
312 * (int *) win_info.data = 0;
314 struct Win win_log = init_window(&win_meta, "Log");
315 win_log.draw = draw_text_from_bottom;
316 win_log.data = calloc(1, sizeof(char));
317 update_log (&win_log, "Start!");
322 if (key == get_action_key(world.keybindings, "quit"))
324 else if (key == get_action_key(world.keybindings, "toggle keys window"))
325 toggle_window(&win_meta, &win_keys);
326 else if (key == get_action_key(world.keybindings, "toggle map window"))
327 toggle_window(&win_meta, &win_map);
328 else if (key == get_action_key(world.keybindings, "toggle info window"))
329 toggle_window(&win_meta, &win_info);
330 else if (key == get_action_key(world.keybindings, "toggle log window"))
331 toggle_window(&win_meta, &win_log);
332 else if (key == get_action_key(world.keybindings, "cycle forwards") && win_meta.active != 0)
333 cycle_active_window(&win_meta, 'n');
334 else if (key == get_action_key(world.keybindings, "cycle backwards") && win_meta.active != 0)
335 cycle_active_window(&win_meta, 'p');
336 else if (key == get_action_key(world.keybindings, "shift forwards") && win_meta.active != 0)
337 shift_window(&win_meta, 'f');
338 else if (key == get_action_key(world.keybindings, "shift backwards") && win_meta.active != 0)
339 shift_window(&win_meta, 'b');
340 else if (key == get_action_key(world.keybindings, "grow hor") && win_meta.active != 0)
341 resize_window(&win_meta, '*');
342 else if (key == get_action_key(world.keybindings, "shrink hor") && win_meta.active != 0)
343 resize_window(&win_meta, '_');
344 else if (key == get_action_key(world.keybindings, "grow ver") && win_meta.active != 0)
345 resize_window(&win_meta, '+');
346 else if (key == get_action_key(world.keybindings, "shrink ver") && win_meta.active != 0)
347 resize_window(&win_meta, '-');
348 else if (key == get_action_key(world.keybindings, "save keys"))
349 save_keybindings(&world);
350 else if (key == get_action_key(world.keybindings, "keys nav up") && world.keyswindata->select > 0) {
351 world.keyswindata->select--;
352 draw_all_windows (&win_meta); }
353 else if (key == get_action_key(world.keybindings, "keys nav down") && world.keyswindata->select < world.keyswindata->max) {
354 world.keyswindata->select++;
355 draw_all_windows (&win_meta); }
356 else if (key == get_action_key(world.keybindings, "keys mod")) {
357 world.keyswindata->edit = 1;
358 draw_all_windows (&win_meta);
360 if (key < 1000) // ensure maximum of three digits in key code field
361 world.keybindings[world.keyswindata->select].key = key;
362 world.keyswindata->edit = 0;
363 draw_all_windows (&win_meta); }
364 else if (key == get_action_key(world.keybindings, "map up") && map.offset_y > 0) {
366 draw_all_windows (&win_meta); }
367 else if (key == get_action_key(world.keybindings, "map down")) {
369 draw_all_windows (&win_meta); }
370 else if (key == get_action_key(world.keybindings, "map right")) {
372 draw_all_windows (&win_meta); }
373 else if (key == get_action_key(world.keybindings, "map left") && map.offset_x > 0) {
375 draw_all_windows (&win_meta); }
376 else if (key == get_action_key(world.keybindings, "player down") && map.player_y < map.height - 1) {
377 update_info (&win_info);
378 update_log (&win_log, "\nYou move south.");
380 draw_all_windows (&win_meta); }
381 else if (key == get_action_key(world.keybindings, "player up") && map.player_y > 0) {
382 update_info (&win_info);
383 update_log (&win_log, "\nYou move north.");
385 draw_all_windows (&win_meta); }
386 else if (key == get_action_key(world.keybindings, "player right") && map.player_x < map.width - 1) {
387 update_info (&win_info);
388 update_log (&win_log, "\nYou move east.");
390 draw_all_windows (&win_meta); }
391 else if (key == get_action_key(world.keybindings, "player left") && map.player_x > 0) {
392 update_info (&win_info);
393 update_log (&win_log, "\nYou move west.");
395 draw_all_windows (&win_meta); }
396 else if (key == get_action_key(world.keybindings, "wait") ) {
397 update_info (&win_info);
398 update_log (&win_log, "\nYou wait.");
399 draw_all_windows(&win_meta); } }