home · contact · privacy
Humble beginnings of customizable keybindings. So far only reading from, not writing...
[plomrogue] / roguelike.c
1 #include <ncurses.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <math.h>
6 #include "windows.h"
7
8 struct Map {
9   int width;
10   int height;
11   int offset_x;
12   int offset_y;
13   int player_x;
14   int player_y;
15   char * cells; };
16
17 struct KeyBinding {
18   char * name;
19   int key; };
20
21 struct KeysWinData {
22   int max;
23   char edit;
24   int select; };
25
26 struct World {
27   struct KeyBinding * keybindings;
28   struct KeysWinData * keyswindata; };
29
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.
32   int x, y;
33   char toggle;
34   int height_av = win->height - 1;
35   int width_av = win->width - 1;
36   char fin = 0;
37   int z = -1;
38   for (y = start_y; y < height_av; y++) {
39     if (0 == fin)
40       toggle = 0;
41     for (x = 0; x < width_av; x++) {
42        if (0 == toggle) {
43          z++;
44          if ('\n' == text[z]) {
45            mvwaddch(win->curses_win, y+1, x+win->border_left, ' ');
46            toggle = 1;
47            continue; }
48          else
49            mvwaddch(win->curses_win, y+1, x+win->border_left, text[z]);
50          if ('\n' == text[z+1]) {
51            z++;
52            toggle = 1; }
53          else if (0 == text[z+1]) {
54             toggle = 1;
55             fin = 1; } }
56        else
57          mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); } } }
58
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;
64   char toggle = 0;
65   int x, y, offset;
66   int z = -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.
69       z++;
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.
73         z++;
74         break; }
75       else if (0 == text[z+1]) {
76         toggle = 1;
77         break; } }
78   z = -1;
79   int start_y = 0;
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++) {
89         z++;
90         if ('\n' == text[z])
91           break;
92         if ('\n' == text[z+1]) {
93           z++;
94           break; } }
95     text = text + (sizeof(char) * (z + 1)); }
96   draw_with_linebreaks(win, text, start_y); }
97
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;
106   int x, y, z;
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, '@');
113         else
114           mvwaddch(win->curses_win, y+1, x+win->border_left, cells[z]);
115         z++; }
116       else
117         mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); } } }
118
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;
122   char text[100];
123   snprintf(text, 100, "Turn: %d", count);
124   draw_with_linebreaks(win, text, 0); }
125
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);
130   else
131     append_window(win_meta, win); }
132
133 struct Map init_map () {
134 // Initialize map with some experimental start values.
135   struct Map map;
136   map.width = 128;
137   map.height = 128;
138   map.offset_x = 0;
139   map.offset_y = 0;
140   map.player_x = 2;
141   map.player_y = 2;
142   map.cells = malloc(map.width * map.height);
143   int x, y;
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';
150   return map; }
151
152 void update_info (struct Win * win) {
153 // Update info data by incrementing turn value.
154   * (int *) win->data = * (int *) win->data + 1; }
155
156 void update_log (struct Win * win, char * text) {
157 // Update log with new text to be appended.
158   char * new_text;
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);
165   free(win->data);
166   win->data = new_text; }
167
168 int get_action_key (struct KeyBinding * keybindings, char * name) {
169 // Return key matching name in keybindings.
170   int i = 0;
171   while (strcmp(keybindings[i].name, name) )
172     i++;
173   return keybindings[i].key; }
174
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;
182   int offset = 0;
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);
187       else
188         offset = keyswindata->max - height_av + 1; }
189   int keydescwidth = 3;
190   char * keydesc = malloc(keydescwidth + 1);
191   attr_t attri;
192   int y, x;
193   for (y = 0; 0 != keybindings[offset + y].name && y < height_av; y++) {
194     attri = 0;
195     if (y == keyswindata->select - offset) {
196       attri = A_REVERSE;
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);
205       else
206         mvwaddch(win->curses_win, y + 1, x + win->border_left, ' ' | attri); }
207   free(keydesc);
208   if (y < height_av)
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, ' '); }
212
213 void init_keybindings(struct World * world) {
214 // Initialize keybindings from file "keybindings".
215   FILE * file = fopen("keybindings", "r");
216   int lines = 0;
217   int c = 0;
218   while (EOF != c) {
219     c = getc(file);
220     if ('\n' == c)
221       lines++; }
222   struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
223   fseek(file, 0, SEEK_SET);
224   char * command = malloc(100);
225   char commcount = 0;
226   char * digits = malloc(10);
227   char digicount = 0;
228   int key, digimax;
229   int keycount = 0;
230   c = getc(file);
231   while (EOF != c) {
232     if ('\n' == c) {
233       keybindings[keycount].name = calloc(commcount, sizeof(char));
234       memcpy(keybindings[keycount].name, command, commcount);
235       keybindings[keycount].key = key;
236       keycount++;
237       digicount = 0;
238       commcount = 0; }
239     else if (-1 != digicount)
240       if (' ' == c) {
241         key = 0;
242         digimax = digicount - 1;
243         while (digicount > 0) {
244           digicount--;
245           key = key + ((digits[digicount] - 48) * pow(10, digimax - digicount)); }
246         digicount = -1; }
247       else {
248         digits[digicount] = c;
249         digicount++; }
250     else {
251       command[commcount] = c;
252       commcount++; }
253     c = getc(file); }
254   free(command);
255   free(digits);
256   fclose(file);
257   struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
258   keyswindata->max = lines - 1;
259   keyswindata->select = 0;
260   keyswindata->edit = 0;
261   world->keybindings = keybindings;
262   world->keyswindata = keyswindata; }
263
264 int main () {
265   struct World world;
266   init_keybindings(&world);
267
268   WINDOW * screen = initscr();
269   noecho();
270   curs_set(0);
271   keypad(screen, TRUE);
272   raw();
273
274   struct WinMeta win_meta = init_win_meta(screen);
275
276   struct Win win_keys = init_window(&win_meta, "Keys");
277   win_keys.draw = draw_keys_window;
278   win_keys.data = &world;
279
280   struct Win win_map = init_window(&win_meta, "Map");
281   win_map.draw = draw_map;
282   struct Map map = init_map();
283   win_map.data = &map;
284
285   struct Win win_info = init_window(&win_meta, "Info");
286   win_info.draw = draw_info;
287   win_info.data = malloc(sizeof(int));
288   * (int *) win_info.data = 0;
289
290   struct Win win_log = init_window(&win_meta, "Log");
291   win_log.draw = draw_text_from_bottom;
292   win_log.data = calloc(1, sizeof(char));
293   update_log (&win_log, "Start!");
294
295   int key;
296   while (1) {
297     key = getch();
298     if      (key == get_action_key(world.keybindings, "quit"))
299       break;
300     else if (key == get_action_key(world.keybindings, "toggle keys window"))
301       toggle_window(&win_meta, &win_keys);
302     else if (key == get_action_key(world.keybindings, "toggle map window"))
303       toggle_window(&win_meta, &win_map);
304     else if (key == get_action_key(world.keybindings, "toggle info window"))
305       toggle_window(&win_meta, &win_info);
306     else if (key == get_action_key(world.keybindings, "toggle log window"))
307       toggle_window(&win_meta, &win_log);
308     else if (key == get_action_key(world.keybindings, "cycle forwards") && win_meta.active != 0)
309       cycle_active_window(&win_meta, 'n');
310     else if (key == get_action_key(world.keybindings, "cycle backwards") && win_meta.active != 0)
311       cycle_active_window(&win_meta, 'p');
312     else if (key == get_action_key(world.keybindings, "shift forwards")  && win_meta.active != 0)
313       shift_window(&win_meta, 'f');
314     else if (key == get_action_key(world.keybindings, "shift backwards") && win_meta.active != 0)
315       shift_window(&win_meta, 'b');
316     else if (key == get_action_key(world.keybindings, "grow hor") && win_meta.active != 0)
317       resize_window(&win_meta, '*');
318     else if (key == get_action_key(world.keybindings, "shrink hor") && win_meta.active != 0)
319       resize_window(&win_meta, '_');
320     else if (key == get_action_key(world.keybindings, "grow ver") && win_meta.active != 0)
321       resize_window(&win_meta, '+');
322     else if (key == get_action_key(world.keybindings, "shrink ver") && win_meta.active != 0)
323       resize_window(&win_meta, '-');
324     else if (key == get_action_key(world.keybindings, "keys nav up") && world.keyswindata->select > 0) {
325       world.keyswindata->select--;
326       draw_all_windows (&win_meta); }
327     else if (key == get_action_key(world.keybindings, "keys nav down") && world.keyswindata->select < world.keyswindata->max) {
328       world.keyswindata->select++;
329       draw_all_windows (&win_meta); }
330     else if (key == get_action_key(world.keybindings, "keys mod")) {
331       world.keyswindata->edit = 1;
332       draw_all_windows (&win_meta);
333       key = getch();
334       world.keybindings[world.keyswindata->select].key = key;
335       world.keyswindata->edit = 0;
336       draw_all_windows (&win_meta); }
337     else if (key == get_action_key(world.keybindings, "map up") && map.offset_y > 0) {
338       map.offset_y--;
339       draw_all_windows (&win_meta); }
340     else if (key == get_action_key(world.keybindings, "map down")) {
341       map.offset_y++;
342       draw_all_windows (&win_meta); }
343     else if (key == get_action_key(world.keybindings, "map right")) {
344       map.offset_x++;
345       draw_all_windows (&win_meta); }
346     else if (key == get_action_key(world.keybindings, "map left") && map.offset_x > 0) {
347       map.offset_x--;
348       draw_all_windows (&win_meta); }
349     else if (key == get_action_key(world.keybindings, "player down") && map.player_y < map.height - 1) {
350       update_info (&win_info);
351       update_log (&win_log, "\nYou move south.");
352       map.player_y++;
353       draw_all_windows (&win_meta); }
354     else if (key == get_action_key(world.keybindings, "player up") && map.player_y > 0) {
355       update_info (&win_info);
356       update_log (&win_log, "\nYou move north.");
357       map.player_y--;
358       draw_all_windows (&win_meta); }
359     else if (key == get_action_key(world.keybindings, "player right") && map.player_x < map.width - 1) {
360       update_info (&win_info);
361       update_log (&win_log, "\nYou move east.");
362       map.player_x++;
363       draw_all_windows (&win_meta); }
364     else if (key == get_action_key(world.keybindings, "player left") && map.player_x > 0) {
365       update_info (&win_info);
366       update_log (&win_log, "\nYou move west.");
367       map.player_x--;
368       draw_all_windows (&win_meta); }
369     else if (key == get_action_key(world.keybindings, "wait") ) {
370       update_info (&win_info);
371       update_log (&win_log, "\nYou wait.");
372       draw_all_windows(&win_meta); } }
373
374   endwin();
375   return 0; }