]>
git.gir.st - VIper.git/blob - viiper.c
1 /*******************************************************************************
3 By Tobias Girstmair, 2018
6 (see ./viiper -h for full list of options)
8 KEYBINDINGS: - hjkl to move
9 - p to pause and resume
12 - (see `./minesviiper -h' for all keybindings)
14 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
15 *******************************************************************************/
18 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
24 #include <sys/ioctl.h>
30 #define MIN(a,b) (a>b?b:a)
31 #define MAX(a,b) (a>b?a:b)
32 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
33 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
34 #define print(str) fputs (str?str:"", stdout)
40 #define CW 2 /* cell width */
41 #define SHORTHELP "%s [OPTIONS] [FIELDSIZE]\n"
46 " WxH (width 'x' height)\n" \
47 " defaults to 30x20\n" \
50 " hjkl: move left/down/up/right\n" \
51 " p: pause / unpause\n" \
52 " r: start a new game\n" \
58 struct snake
* next
; /* points to tail */
60 struct item
{ //TODO: make it more abstract (type should encode glyph, points, etc)
64 int s
; /* spawn time (for bonus) */
82 int w
; /* field width */
83 int h
; /* field height */
84 struct snake
* s
; /* snek */
85 struct item
* i
; /* items (food, boni) */
86 int d
; /* direction the snake is looking */
87 int n
;/* direction to move on next tick */
88 int t
; /* time of game start */
94 int l
; /* initial snake length */
95 int s
; //TODO: initial snake speed
99 void snake_advance (void);
100 void spawn_item (int type
);
101 void consume_item (struct item
* i
);
102 void show_playfield (void);
103 void snake_append (struct snake
* s
, int row
, int col
);
106 int getctrlseq (void);
107 void move_ph (int line
, int col
);
108 void clamp_fieldsize (void);
109 void timer_setup (int enable
);
110 void signal_setup (void);
111 void signal_handler (int signum
);
112 void screen_setup (int enable
);
113 void raw_mode(int enable
);
115 /* for getctrlseq() */
120 CTRSEQ_CURSOR_LEFT
= -7,
121 CTRSEQ_CURSOR_DOWN
= -8,
122 CTRSEQ_CURSOR_UP
= -9,
123 CTRSEQ_CURSOR_RIGHT
= -10,
126 int main (int argc
, char** argv
) {
128 g
.w
= 30; //two-char-width
134 opterr
= 0; /* don't print message on unrecognized option */
135 while ((optget
= getopt (argc
, argv
, "+h")) != -1) {
139 fprintf (stderr
, SHORTHELP LONGHELP
, argv
[0]);
140 return !(optget
=='h');
142 } if (optind
< argc
) { /* parse Fieldspec */
150 //TODO: call viiper() in a game loop
166 switch (getctrlseq()) {
167 case 'h': if (g
.d
!= EAST
) g
.n
= WEST
; break;
168 case 'j': if (g
.d
!= NORTH
) g
.n
= SOUTH
; break;
169 case 'k': if (g
.d
!= SOUTH
) g
.n
= NORTH
; break;
170 case 'l': if (g
.d
!= WEST
) g
.n
= EAST
; break;
171 case 'r': /*TODO:restart*/ return 0;
179 print ("\033[H\033[J");
180 show_playfield ();//TODO: only redraw diff
185 void snake_advance (void) {
187 int new_row
= g
.s
->r
+(g
.d
==SOUTH
) -(g
.d
==NORTH
);
188 int new_col
= g
.s
->c
+(g
.d
==EAST
) -(g
.d
==WEST
);
190 //TODO: detect food hit and spawn a new food.
191 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
192 if (i
->r
== new_row
&& i
->c
== new_col
) {
198 if (new_row
>= g
.h
|| new_col
>= g
.w
|| new_row
< 0 || new_col
< 0)
199 exit(1); //TODO: longjump?
201 struct snake
* new_head
;
202 struct snake
* new_tail
; /* former second-to-last element */
203 for (new_tail
= g
.s
; new_tail
->next
->next
; new_tail
= new_tail
->next
)
204 /* use the opportunity of looping to check if we eat ourselves*/
205 if(new_tail
->next
->r
== new_row
&& new_tail
->next
->c
== new_col
)
207 new_head
= new_tail
->next
; /* reuse element instead of malloc() */
208 new_tail
->next
= NULL
;
210 new_head
->r
= new_row
;
211 new_head
->c
= new_col
;
212 new_head
->next
= g
.s
;
217 void spawn_item (int type
) {
222 /* loop through snake to check if we aren't on it */
223 //TODO: inefficient as snake gets longer; near impossible in the end
224 for (struct snake
* s
= g
.s
; s
; s
= s
->next
)
225 if (s
->r
== row
&& s
->c
== col
) goto try_again
;
227 //3. get item from category TODO
228 struct item
* new_item
= malloc (sizeof(struct item
));
233 new_item
->s
= time(0);
234 if (g
.i
) g
.i
->prev
= new_item
;
235 new_item
->next
= g
.i
;
240 void consume_item (struct item
* i
) {
241 struct item
* predecessor
= i
->prev
;
242 struct item
* successor
= i
->next
;
247 g
.p
+=10; //TODO: multiple types for different points?
248 snake_append(g
.s
, 0,0); /* position doesn't matter, as item */
249 break; /* will be reused as the head before it is drawn */
255 if (predecessor
== NULL
) {
257 if (successor
) successor
->prev
= NULL
;
259 predecessor
->next
= successor
;
260 successor
->prev
= predecessor
;
266 void show_playfield (void) {
269 printm (g
.w
/2-4/2, "══");
270 printf ("╡ %04d ╞", g
.p
);
271 printm(g
.w
/2-4/2, "══");
275 for (int row
= 0; row
< g
.h
; row
++) {
286 struct snake
* last
= NULL
;
287 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
288 move_ph (s
->r
+COL_OFFSET
, s
->c
*CW
+LINE_OFFSET
);
290 int predecessor
= (last
==NULL
)?-1:
291 (last
->r
< s
->r
) ? NORTH
:
292 (last
->r
> s
->r
) ? SOUTH
:
293 (last
->c
> s
->c
) ? EAST
:
294 (last
->c
< s
->c
) ? WEST
:-1;
295 int successor
= (s
->next
== NULL
)?-2:
296 (s
->next
->r
< s
->r
) ? NORTH
:
297 (s
->next
->r
> s
->r
) ? SOUTH
:
298 (s
->next
->c
> s
->c
) ? EAST
:
299 (s
->next
->c
< s
->c
) ? WEST
:-2;
301 #define GOES(dir1, dir2) \
302 ((predecessor == dir1 || successor == dir1) && \
303 (predecessor == dir2 || successor == dir2))
305 /* head (NOTE: direction is inverted in this section) */
306 if /*SOUTH*/ GOES(NORTH
,-1) print ("⢿⡿");
307 else if /*WEST*/ GOES(EAST
,-1) print ("⢾⣿");
308 else if /*NORTH*/ GOES(SOUTH
,-1) print ("⣾⣷");
309 else if /*EAST*/ GOES(WEST
,-1) print ("⣿⡷");
311 else if GOES(NORTH
,SOUTH
) print ("⡇⢸");
312 else if GOES(WEST
,EAST
) print ("⣉⣉");
313 else if GOES(NORTH
,EAST
) print ("⢇⣈");
314 else if GOES(EAST
,SOUTH
) print ("⡎⢉");
315 else if GOES(SOUTH
,WEST
) print ("⡉⢱");
316 else if GOES(WEST
,NORTH
) print ("⣁⡸");
318 else if GOES(NORTH
,-2) print ("⢇⡸");
319 else if GOES(EAST
,-2) print ("⢎⣉");
320 else if GOES(SOUTH
,-2) print ("⡎⢱");
321 else if GOES(WEST
,-2) print ("⣉⡱");
326 /* print item queue */
327 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
328 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
333 void snake_append (struct snake
* s
, int row
, int col
) {
334 struct snake
* new = malloc (sizeof(struct snake
));
340 while (p
->next
) p
= p
->next
;
345 g
.s
= malloc (sizeof(struct snake
));
350 for (int i
= 1; i
< op
.l
; i
++)
351 snake_append(g
.s
, g
.h
/2, g
.w
/2-i
);
356 for (struct snake
* s
= g
.s
; s
;) {
357 struct snake
* tmp
= s
->next
;
361 for (struct item
* i
= g
.i
; i
;) {
362 struct item
* tmp
= i
->next
;
374 int getctrlseq (void) {
377 int offset
= 0x20; /* never sends control chars as data */
378 while ((c
= getchar()) != EOF
) {
382 case '\033': state
=ESC_SENT
; break;
388 case '[': state
=CSI_SENT
; break;
389 default: return CTRSEQ_INVALID
;
394 case 'A': return CTRSEQ_CURSOR_UP
;
395 case 'B': return CTRSEQ_CURSOR_DOWN
;
396 case 'C': return CTRSEQ_CURSOR_RIGHT
;
397 case 'D': return CTRSEQ_CURSOR_LEFT
;
398 default: return CTRSEQ_INVALID
;
402 return CTRSEQ_INVALID
;
408 void move_ph (int line
, int col
) {
409 /* move printhead to zero-indexed position */
410 printf ("\033[%d;%dH", line
+1, col
+1);
413 void clamp_fieldsize (void) {
414 /* clamp field size to terminal size and mouse maximum: */
416 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
418 if (g
.w
< 1) g
.w
= 1;
419 if (g
.h
< 1) g
.h
= 1;
421 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
422 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
; //TODO: does not work in `-d' (in xterm)
423 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
424 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
427 void timer_setup (int enable
) {
428 static struct itimerval tbuf
;
429 tbuf
.it_interval
.tv_sec
= 0;//TODO: make configurable, TODO: speed up
430 tbuf
.it_interval
.tv_usec
= 300000;
434 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
435 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
436 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
441 tbuf
.it_value
.tv_sec
= 0;
442 tbuf
.it_value
.tv_usec
= 0;
443 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
451 void signal_setup (void) {
452 struct sigaction saction
;
454 saction
.sa_handler
= signal_handler
;
455 sigemptyset(&saction
.sa_mask
);
456 saction
.sa_flags
= 0;
457 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
462 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
468 void signal_handler (int signum
) {
472 //dtime = difftime (time(NULL), g.t);
473 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
474 //printf ("[%03d]", g.t?dtime:0);
482 void screen_setup (int enable
) {
485 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
486 printf ("\033[H\033[J"); /* reset cursor, clear screen */
487 printf ("\033[?25l"); /* hide cursor */
488 //print (op.scheme->init_seq); /* swich charset, if necessary */
490 //print (op.scheme->reset_seq); /* reset charset, if necessary */
491 printf ("\033[?25h"); /* show cursor */
492 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
497 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
498 void raw_mode(int enable
) {
499 static struct termios saved_term_mode
;
500 struct termios raw_term_mode
;
503 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
504 raw_term_mode
= saved_term_mode
;
505 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
506 raw_term_mode
.c_cc
[VMIN
] = 1 ;
507 raw_term_mode
.c_cc
[VTIME
] = 0;
508 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
510 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);