home · contact · privacy
Keybindings can now be written back to keybindings file. Also made some memory alloca...
[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   int linemax = 0;
219   int c_count = 0;
220   while (EOF != c) {
221     c_count++;
222     c = getc(file);
223     if ('\n' == c) {
224       if (c_count > linemax)
225         linemax = c_count;
226       c_count = 0;
227       lines++; } }
228   struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
229   fseek(file, 0, SEEK_SET);
230   char * command = malloc(linemax);
231   char commcount = 0;
232   char * digits = malloc(3);
233   char digicount = 0;
234   int key, digimax;
235   int keycount = 0;
236   c = getc(file);
237   while (EOF != c) {
238     if ('\n' == c) {
239       keybindings[keycount].name = calloc(commcount, sizeof(char));
240       memcpy(keybindings[keycount].name, command, commcount);
241       keybindings[keycount].key = key;
242       keycount++;
243       digicount = 0;
244       commcount = 0; }
245     else if (-1 != digicount)
246       if (' ' == c) {
247         key = 0;
248         digimax = digicount - 1;
249         while (digicount > 0) {
250           digicount--;
251           key = key + ((digits[digicount] - 48) * pow(10, digimax - digicount)); }
252         digicount = -1; }
253       else {
254         digits[digicount] = c;
255         digicount++; }
256     else {
257       command[commcount] = c;
258       commcount++; }
259     c = getc(file); }
260   free(command);
261   free(digits);
262   fclose(file);
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; }
269
270 void save_keybindings(struct World * world) {
271 // Write keybidings to keybindings file.
272   struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
273   struct KeyBinding * keybindings = world->keybindings;
274   FILE * file = fopen("keybindings", "w");
275   int linemax = 0;
276   int i;
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); }
285   free(line);
286   fclose(file); }
287
288 int main () {
289   struct World world;
290   init_keybindings(&world);
291
292   WINDOW * screen = initscr();
293   noecho();
294   curs_set(0);
295   keypad(screen, TRUE);
296   raw();
297
298   struct WinMeta win_meta = init_win_meta(screen);
299
300   struct Win win_keys = init_window(&win_meta, "Keys");
301   win_keys.draw = draw_keys_window;
302   win_keys.data = &world;
303
304   struct Win win_map = init_window(&win_meta, "Map");
305   win_map.draw = draw_map;
306   struct Map map = init_map();
307   win_map.data = &map;
308
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;
313
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!");
318
319   int key;
320   while (1) {
321     key = getch();
322     if      (key == get_action_key(world.keybindings, "quit"))
323       break;
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);
359       key = getch();
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) {
365       map.offset_y--;
366       draw_all_windows (&win_meta); }
367     else if (key == get_action_key(world.keybindings, "map down")) {
368       map.offset_y++;
369       draw_all_windows (&win_meta); }
370     else if (key == get_action_key(world.keybindings, "map right")) {
371       map.offset_x++;
372       draw_all_windows (&win_meta); }
373     else if (key == get_action_key(world.keybindings, "map left") && map.offset_x > 0) {
374       map.offset_x--;
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.");
379       map.player_y++;
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.");
384       map.player_y--;
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.");
389       map.player_x++;
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.");
394       map.player_x--;
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); } }
400
401   endwin();
402   return 0; }