]> git.gir.st - VIper.git/blob - viiper.c
move enums, struct defs, etc into header file
[VIper.git] / viiper.c
1 /*******************************************************************************
2 viiper 0.1
3 By Tobias Girstmair, 2018
4
5 ./viiper 40x25
6 (see ./viiper -h for full list of options)
7
8 KEYBINDINGS: - hjkl to move
9 - p to pause and resume
10 - r to restart
11 - q to quit
12 - (see `./minesviiper -h' for all keybindings)
13
14 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
15 *******************************************************************************/
16
17
18 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
19 #include <ctype.h>
20 #include <poll.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/ioctl.h>
25 #include <sys/time.h>
26 #include <termios.h>
27 #include <time.h>
28 #include <unistd.h>
29
30 #include "viiper.h"
31 #include "schemes.h"
32
33 #define MIN(a,b) (a>b?b:a)
34 #define MAX(a,b) (a>b?a:b)
35 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
36 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
37 #define print(str) fputs (str?str:"", stdout)
38 #define CTRL_ 0x1F &
39
40 #define COL_OFFSET 1
41 #define LINE_OFFSET 1
42 #define LINES_AFTER 1
43 #define CW 2 /* cell width */
44
45 struct game {
46 int w; /* field width */
47 int h; /* field height */
48 struct snake* s; /* snek */
49 struct item* i; /* items (food, boni) */
50 int d; /* direction the snake is looking */
51 int n;/* direction to move on next tick */
52 int t; /* time of game start */
53 int p; /* score */
54 } g;
55
56 struct opt {
57 struct scheme* scheme;
58 int l; /* initial snake length */
59 int s; //TODO: initial snake speed
60 } op;
61
62 int main (int argc, char** argv) {
63 /* defaults: */
64 g.w = 30; //two-char-width
65 g.h = 20;
66 op.l = 10;
67 op.scheme = &unic0de; //TODO: expose to getopt()
68
69 int optget;
70 opterr = 0; /* don't print message on unrecognized option */
71 while ((optget = getopt (argc, argv, "+h")) != -1) {
72 switch (optget) {
73 case 'h':
74 default:
75 fprintf (stderr, SHORTHELP LONGHELP, argv[0]);
76 return !(optget=='h');
77 }
78 } if (optind < argc) { /* parse Fieldspec */
79 }
80
81 srand(time(0));
82 signal_setup();
83 screen_setup(1);
84 atexit (*quit);
85
86 //TODO: call viiper() in a game loop
87 viiper();
88 quit:
89 return 0;
90 }
91
92 int viiper(void) {
93 init_snake();
94 show_playfield ();
95 g.d = g.n = EAST;
96
97 timer_setup(1);
98
99 spawn_item(FOOD_10);
100
101 for(;;) {
102 switch (getctrlseq()) {
103 case 'h': if (g.d != EAST) g.n = WEST; break;
104 case 'j': if (g.d != NORTH) g.n = SOUTH; break;
105 case 'k': if (g.d != SOUTH) g.n = NORTH; break;
106 case 'l': if (g.d != WEST) g.n = EAST; break;
107 case 'p': /*TODO: pause*/ break;
108 case 'r': /*TODO:restart*/ return 0;
109 case 'q': return 0;
110 case CTRL_'L':
111 screen_setup(1);
112 show_playfield();
113 break;
114 }
115
116 print ("\033[H\033[J");
117 show_playfield ();//TODO: only redraw diff
118 }
119
120 }
121
122 void snake_advance (void) {
123 g.d = g.n;
124 int new_row = g.s->r +(g.d==SOUTH) -(g.d==NORTH);
125 int new_col = g.s->c +(g.d==EAST) -(g.d==WEST);
126
127 /* detect food hit and spawn a new food */
128 for (struct item* i = g.i; i; i = i->next) {
129 if (i->r == new_row && i->c == new_col) {
130 consume_item (i);
131 spawn_item(FOOD_10);
132 }
133 }
134
135 if (new_row >= g.h || new_col >= g.w || new_row < 0 || new_col < 0)
136 exit(1); //TODO: longjump?
137
138 struct snake* new_head;
139 struct snake* new_tail; /* former second-to-last element */
140 for (new_tail = g.s; new_tail->next->next; new_tail = new_tail->next)
141 /* use the opportunity of looping to check if we eat ourselves*/
142 if(new_tail->next->r == new_row && new_tail->next->c == new_col)
143 exit(1);
144 new_head = new_tail->next; /* reuse element instead of malloc() */
145 new_tail->next = NULL;
146
147 new_head->r = new_row;
148 new_head->c = new_col;
149 new_head->next = g.s;
150
151 g.s = new_head;
152 }
153
154 void spawn_item (int type) {
155 int row, col;
156 try_again:
157 row = rand() % g.h;
158 col = rand() % g.w;
159 /* loop through snake to check if we aren't on it */
160 //TODO: inefficient as snake gets longer; near impossible in the end
161 for (struct snake* s = g.s; s; s = s->next)
162 if (s->r == row && s->c == col) goto try_again;
163
164 //3. get item from category TODO
165 struct item* new_item = malloc (sizeof(struct item));
166 new_item->r = row;
167 new_item->c = col;
168 new_item->t = type;
169 new_item->s = time(0);
170 if (g.i) g.i->prev = new_item;
171 new_item->next = g.i;
172
173 g.i = new_item;
174 }
175
176 void consume_item (struct item* i) {
177 struct item* predecessor = i->prev;
178 struct item* successor = i->next;
179
180 switch (i->t) {
181 case FOOD_10:
182 g.p+=10;
183 snake_append(g.s, 0,0); /* position doesn't matter, as item */
184 break; /* will be reused as the head before it is drawn */
185 case BONUS:
186 //handle bonus
187 break;
188 }
189
190 if (predecessor == NULL) {
191 g.i = successor;
192 if (successor) successor->prev = NULL;
193 } else {
194 predecessor->next = successor;
195 successor->prev = predecessor;
196 }
197
198 free (i);
199 }
200
201 void show_playfield (void) {
202 /* top border */
203 print(BORDER(T,L));
204 printm (g.w/2-4/2, BORDER(T,C)); //TODO: i bet this breaks in dec mode
205 printf ("%s %04d %s", BORDER(S,L), g.p, BORDER(S,R));
206 printm(g.w/2-4/2, BORDER(T,C));
207 printf ("%s\n", BORDER(T,R));
208
209 /* main area */
210 for (int row = 0; row < g.h; row++)
211 printf ("%s%*s%s\n", BORDER(C,L), CW*g.w, "", BORDER(C,R));
212
213 /* bottom border */
214 print(BORDER(B,L));
215 printm (g.w, BORDER(B,C));
216 print (BORDER(B,R));
217
218 /* print snake */
219 struct snake* last = NULL;
220 for (struct snake* s = g.s; s; s = s->next) {
221 move_ph (s->r+COL_OFFSET, s->c*CW+LINE_OFFSET);
222
223 int predecessor = (last==NULL)?NONE:
224 (last->r < s->r) ? NORTH:
225 (last->r > s->r) ? SOUTH:
226 (last->c > s->c) ? EAST:
227 (last->c < s->c) ? WEST:NONE;
228 int successor = (s->next == NULL)?NONE:
229 (s->next->r < s->r) ? NORTH:
230 (s->next->r > s->r) ? SOUTH:
231 (s->next->c > s->c) ? EAST:
232 (s->next->c < s->c) ? WEST:NONE;
233
234 print (op.scheme->snake[predecessor][successor]);
235 last = s;
236 }
237
238 /* print item queue */
239 for (struct item* i = g.i; i; i = i->next) {
240 move_ph (i->r+LINE_OFFSET, i->c*CW+COL_OFFSET);
241 print (op.scheme->item[i->t]);
242 }
243 }
244
245 void snake_append (struct snake* s, int row, int col) {
246 struct snake* new = malloc (sizeof(struct snake));
247 new->r = row;
248 new->c = col;
249 new->next = NULL;
250
251 struct snake* p = s;
252 while (p->next) p = p->next;
253 p->next = new;
254 }
255
256 void init_snake() {
257 g.s = malloc (sizeof(struct snake));
258 g.s->r = g.h/2;
259 g.s->c = g.w/2;
260 g.s->next=NULL;
261
262 for (int i = 1; i < op.l; i++)
263 snake_append(g.s, g.h/2, g.w/2-i);
264 }
265
266 void quit (void) {
267 screen_setup(0);
268 for (struct snake* s = g.s; s;) {
269 struct snake* tmp = s->next;
270 free (s);
271 s = tmp;
272 }
273 for (struct item* i = g.i; i;) {
274 struct item* tmp = i->next;
275 free (i);
276 i = tmp;
277 }
278 }
279
280 enum esc_states {
281 START,
282 ESC_SENT,
283 CSI_SENT,
284 MOUSE_EVENT,
285 };
286 int getctrlseq (void) {
287 int c;
288 int state = START;
289 int offset = 0x20; /* never sends control chars as data */
290 while ((c = getchar()) != EOF) {
291 switch (state) {
292 case START:
293 switch (c) {
294 case '\033': state=ESC_SENT; break;
295 default: return c;
296 }
297 break;
298 case ESC_SENT:
299 switch (c) {
300 case '[': state=CSI_SENT; break;
301 default: return CTRSEQ_INVALID;
302 }
303 break;
304 case CSI_SENT:
305 switch (c) {
306 case 'A': return CTRSEQ_CURSOR_UP;
307 case 'B': return CTRSEQ_CURSOR_DOWN;
308 case 'C': return CTRSEQ_CURSOR_RIGHT;
309 case 'D': return CTRSEQ_CURSOR_LEFT;
310 default: return CTRSEQ_INVALID;
311 }
312 break;
313 default:
314 return CTRSEQ_INVALID;
315 }
316 }
317 return 2;
318 }
319
320 void move_ph (int line, int col) {
321 /* move printhead to zero-indexed position */
322 printf ("\033[%d;%dH", line+1, col+1);
323 }
324
325 void clamp_fieldsize (void) {
326 /* clamp field size to terminal size and mouse maximum: */
327 struct winsize w;
328 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
329
330 if (g.w < 1) g.w = 1;
331 if (g.h < 1) g.h = 1;
332
333 if (COL_OFFSET + g.w*CW + COL_OFFSET > w.ws_col)
334 g.w = (w.ws_col - COL_OFFSET - COL_OFFSET)/CW; //TODO: does not work in `-d' (in xterm)
335 if (LINE_OFFSET + g.h + LINES_AFTER > w.ws_row)
336 g.h = w.ws_row - (LINE_OFFSET+LINES_AFTER);
337 }
338
339 void timer_setup (int enable) {
340 static struct itimerval tbuf;
341 tbuf.it_interval.tv_sec = 0;//TODO: make configurable, TODO: speed up
342 tbuf.it_interval.tv_usec = 300000;
343
344 if (enable) {
345 g.t = time(NULL);
346 tbuf.it_value.tv_sec = tbuf.it_interval.tv_sec;
347 tbuf.it_value.tv_usec = tbuf.it_interval.tv_usec;
348 if (setitimer(ITIMER_REAL, &tbuf, NULL) == -1) {
349 perror("setitimer");
350 exit(1);
351 }
352 } else {
353 tbuf.it_value.tv_sec = 0;
354 tbuf.it_value.tv_usec = 0;
355 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
356 perror("setitimer");
357 exit(1);
358 }
359 }
360
361 }
362
363 void signal_setup (void) {
364 struct sigaction saction;
365
366 saction.sa_handler = signal_handler;
367 sigemptyset(&saction.sa_mask);
368 saction.sa_flags = 0;
369 if (sigaction(SIGALRM, &saction, NULL) < 0 ) {
370 perror("SIGALRM");
371 exit(1);
372 }
373
374 if (sigaction(SIGINT, &saction, NULL) < 0 ) {
375 perror ("SIGINT");
376 exit (1);
377 }
378 }
379
380 void signal_handler (int signum) {
381 //int dtime;
382 switch (signum) {
383 case SIGALRM:
384 //dtime = difftime (time(NULL), g.t);
385 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
386 //printf ("[%03d]", g.t?dtime:0);
387 snake_advance();
388 break;
389 case SIGINT:
390 exit(128+SIGINT);
391 }
392 }
393
394 void screen_setup (int enable) {
395 if (enable) {
396 raw_mode(1);
397 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
398 printf ("\033[H\033[J"); /* reset cursor, clear screen */
399 printf ("\033[?25l"); /* hide cursor */
400 //print (op.scheme->init_seq); /* swich charset, if necessary */
401 } else {
402 //print (op.scheme->reset_seq); /* reset charset, if necessary */
403 printf ("\033[?25h"); /* show cursor */
404 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
405 raw_mode(0);
406 }
407 }
408
409 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
410 void raw_mode(int enable) {
411 static struct termios saved_term_mode;
412 struct termios raw_term_mode;
413
414 if (enable) {
415 tcgetattr(STDIN_FILENO, &saved_term_mode);
416 raw_term_mode = saved_term_mode;
417 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
418 raw_term_mode.c_cc[VMIN] = 1 ;
419 raw_term_mode.c_cc[VTIME] = 0;
420 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
421 } else {
422 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
423 }
424 }
Imprint / Impressum