]> git.gir.st - VIper.git/blob - viiper.c
different food values
[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, rand() % NUM_FOODS); //TODO: shape distribution, so bigger values get selected less
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, rand() % NUM_FOODS);
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, int value) {
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->v = value;
170 new_item->s = time(0);
171 if (g.i) g.i->prev = new_item;
172 new_item->next = g.i;
173
174 g.i = new_item;
175 }
176
177 void consume_item (struct item* i) {
178 struct item* predecessor = i->prev;
179 struct item* successor = i->next;
180
181 switch (i->t) {
182 case FOOD:
183 switch (i->v) {
184 case FOOD_5: g.p += 5; break;
185 case FOOD_10: g.p += 10; break;
186 case FOOD_20: g.p += 20; break;
187 }
188 snake_append(g.s, 0,0); /* position doesn't matter, as item */
189 break; /* will be reused as the head before it is drawn */
190 case BONUS:
191 //handle bonus
192 break;
193 }
194
195 if (predecessor == NULL) {
196 g.i = successor;
197 if (successor) successor->prev = NULL;
198 } else {
199 predecessor->next = successor;
200 successor->prev = predecessor;
201 }
202
203 free (i);
204 }
205
206 void show_playfield (void) {
207 /* top border */
208 print(BORDER(T,L));
209 printm (g.w/2-4/2, BORDER(T,C)); //TODO: i bet this breaks in dec mode
210 printf ("%s %04d %s", BORDER(S,L), g.p, BORDER(S,R));
211 printm(g.w/2-4/2, BORDER(T,C));
212 printf ("%s\n", BORDER(T,R));
213
214 /* main area */
215 for (int row = 0; row < g.h; row++)
216 printf ("%s%*s%s\n", BORDER(C,L), CW*g.w, "", BORDER(C,R));
217
218 /* bottom border */
219 print(BORDER(B,L));
220 printm (g.w, BORDER(B,C));
221 print (BORDER(B,R));
222
223 /* print snake */
224 struct snake* last = NULL;
225 for (struct snake* s = g.s; s; s = s->next) {
226 move_ph (s->r+COL_OFFSET, s->c*CW+LINE_OFFSET);
227
228 int predecessor = (last==NULL)?NONE:
229 (last->r < s->r) ? NORTH:
230 (last->r > s->r) ? SOUTH:
231 (last->c > s->c) ? EAST:
232 (last->c < s->c) ? WEST:NONE;
233 int successor = (s->next == NULL)?NONE:
234 (s->next->r < s->r) ? NORTH:
235 (s->next->r > s->r) ? SOUTH:
236 (s->next->c > s->c) ? EAST:
237 (s->next->c < s->c) ? WEST:NONE;
238
239 print (op.scheme->snake[predecessor][successor]);
240 last = s;
241 }
242
243 /* print item queue */
244 for (struct item* i = g.i; i; i = i->next) {
245 move_ph (i->r+LINE_OFFSET, i->c*CW+COL_OFFSET);
246 if (i->t == FOOD) print (op.scheme->item[i->v]);
247 else if (i->t==BONUS) /* TODO: print bonus */;
248 }
249 }
250
251 void snake_append (struct snake* s, int row, int col) {
252 struct snake* new = malloc (sizeof(struct snake));
253 new->r = row;
254 new->c = col;
255 new->next = NULL;
256
257 struct snake* p = s;
258 while (p->next) p = p->next;
259 p->next = new;
260 }
261
262 void init_snake() {
263 g.s = malloc (sizeof(struct snake));
264 g.s->r = g.h/2;
265 g.s->c = g.w/2;
266 g.s->next=NULL;
267
268 for (int i = 1; i < op.l; i++)
269 snake_append(g.s, g.h/2, g.w/2-i);
270 }
271
272 void quit (void) {
273 screen_setup(0);
274 for (struct snake* s = g.s; s;) {
275 struct snake* tmp = s->next;
276 free (s);
277 s = tmp;
278 }
279 for (struct item* i = g.i; i;) {
280 struct item* tmp = i->next;
281 free (i);
282 i = tmp;
283 }
284 }
285
286 enum esc_states {
287 START,
288 ESC_SENT,
289 CSI_SENT,
290 MOUSE_EVENT,
291 };
292 int getctrlseq (void) {
293 int c;
294 int state = START;
295 int offset = 0x20; /* never sends control chars as data */
296 while ((c = getchar()) != EOF) {
297 switch (state) {
298 case START:
299 switch (c) {
300 case '\033': state=ESC_SENT; break;
301 default: return c;
302 }
303 break;
304 case ESC_SENT:
305 switch (c) {
306 case '[': state=CSI_SENT; break;
307 default: return CTRSEQ_INVALID;
308 }
309 break;
310 case CSI_SENT:
311 switch (c) {
312 case 'A': return CTRSEQ_CURSOR_UP;
313 case 'B': return CTRSEQ_CURSOR_DOWN;
314 case 'C': return CTRSEQ_CURSOR_RIGHT;
315 case 'D': return CTRSEQ_CURSOR_LEFT;
316 default: return CTRSEQ_INVALID;
317 }
318 break;
319 default:
320 return CTRSEQ_INVALID;
321 }
322 }
323 return 2;
324 }
325
326 void move_ph (int line, int col) {
327 /* move printhead to zero-indexed position */
328 printf ("\033[%d;%dH", line+1, col+1);
329 }
330
331 void clamp_fieldsize (void) {
332 /* clamp field size to terminal size and mouse maximum: */
333 struct winsize w;
334 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
335
336 if (g.w < 1) g.w = 1;
337 if (g.h < 1) g.h = 1;
338
339 if (COL_OFFSET + g.w*CW + COL_OFFSET > w.ws_col)
340 g.w = (w.ws_col - COL_OFFSET - COL_OFFSET)/CW; //TODO: does not work in `-d' (in xterm)
341 if (LINE_OFFSET + g.h + LINES_AFTER > w.ws_row)
342 g.h = w.ws_row - (LINE_OFFSET+LINES_AFTER);
343 }
344
345 void timer_setup (int enable) {
346 static struct itimerval tbuf;
347 tbuf.it_interval.tv_sec = 0;//TODO: make configurable, TODO: speed up
348 tbuf.it_interval.tv_usec = 300000;
349
350 if (enable) {
351 g.t = time(NULL);
352 tbuf.it_value.tv_sec = tbuf.it_interval.tv_sec;
353 tbuf.it_value.tv_usec = tbuf.it_interval.tv_usec;
354 if (setitimer(ITIMER_REAL, &tbuf, NULL) == -1) {
355 perror("setitimer");
356 exit(1);
357 }
358 } else {
359 tbuf.it_value.tv_sec = 0;
360 tbuf.it_value.tv_usec = 0;
361 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
362 perror("setitimer");
363 exit(1);
364 }
365 }
366
367 }
368
369 void signal_setup (void) {
370 struct sigaction saction;
371
372 saction.sa_handler = signal_handler;
373 sigemptyset(&saction.sa_mask);
374 saction.sa_flags = 0;
375 if (sigaction(SIGALRM, &saction, NULL) < 0 ) {
376 perror("SIGALRM");
377 exit(1);
378 }
379
380 if (sigaction(SIGINT, &saction, NULL) < 0 ) {
381 perror ("SIGINT");
382 exit (1);
383 }
384 }
385
386 void signal_handler (int signum) {
387 //int dtime;
388 switch (signum) {
389 case SIGALRM:
390 //dtime = difftime (time(NULL), g.t);
391 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
392 //printf ("[%03d]", g.t?dtime:0);
393 snake_advance();
394 break;
395 case SIGINT:
396 exit(128+SIGINT);
397 }
398 }
399
400 void screen_setup (int enable) {
401 if (enable) {
402 raw_mode(1);
403 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
404 printf ("\033[H\033[J"); /* reset cursor, clear screen */
405 printf ("\033[?25l"); /* hide cursor */
406 //print (op.scheme->init_seq); /* swich charset, if necessary */
407 } else {
408 //print (op.scheme->reset_seq); /* reset charset, if necessary */
409 printf ("\033[?25h"); /* show cursor */
410 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
411 raw_mode(0);
412 }
413 }
414
415 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
416 void raw_mode(int enable) {
417 static struct termios saved_term_mode;
418 struct termios raw_term_mode;
419
420 if (enable) {
421 tcgetattr(STDIN_FILENO, &saved_term_mode);
422 raw_term_mode = saved_term_mode;
423 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
424 raw_term_mode.c_cc[VMIN] = 1 ;
425 raw_term_mode.c_cc[VTIME] = 0;
426 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
427 } else {
428 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
429 }
430 }
Imprint / Impressum