]>
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 and sigaction in c99, sigsetjmp*/
23 #include <sys/ioctl.h>
32 #define MIN(a,b) (a>b?b:a)
33 #define MAX(a,b) (a>b?a:b)
34 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
35 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
36 #define print(str) fputs (str?str:"", stdout)
39 #define OPPOSITE(dir) ( \
40 dir == EAST ? WEST : \
41 dir == WEST ? EAST : \
42 dir == NORTH ? SOUTH : \
43 dir == SOUTH ? NORTH : -1)
48 #define CW op.scheme->cell_width
51 int w
; /* field width */
52 int h
; /* field height */
53 int d
; /* direction the snake is looking */
54 int t
; /* time of game start */
56 float v
; /* velocity in moves per second */
57 struct snake
* s
; /* snek */
58 struct item
* i
; /* items (food, boni) */
60 int h
; /* write head */
61 int n
; /* number of elements */
63 } k
; /* ring buffer for direction events */
67 int l
; /* initial snake length */
68 int s
; /* initial snake speed */
69 struct scheme
* scheme
;
74 int main (int argc
, char** argv
) {
76 g
.w
= 30; //two-char-width
83 opterr
= 0; /* don't print message on unrecognized option */
84 while ((optget
= getopt (argc
, argv
, "+s:dh")) != -1) {
89 fprintf (stderr
, "speed must be >= 1\n");
93 case 'd': op
.scheme
= &vt220_charset
; break;
96 fprintf (stderr
, SHORTHELP LONGHELP
, argv
[0]);
97 return !(optget
=='h');
99 } if (optind
< argc
) { /* parse Fieldspec */
100 int n
= sscanf (argv
[optind
], "%dx%d", &g
.w
, &g
.h
);
103 fprintf(stderr
,"FIELDSIZE is WxH (width 'x' height)\n");
115 if (sigsetjmp(game_over
, 1)) {
117 move_ph (g
.h
/2+LINE_OFFSET
, g
.w
);
118 printf ("snek ded :(");
124 //TODO: call viiper() in a game loop
139 spawn_item(FOOD
, rand() % NUM_FOODS
, NULL
); //TODO: shape distribution, so bigger values get selected less
142 switch (getctrlseq()) {
143 case CTRSEQ_CURSOR_LEFT
: case 'h':append_movement(WEST
); break;
144 case CTRSEQ_CURSOR_DOWN
: case 'j':append_movement(SOUTH
); break;
145 case CTRSEQ_CURSOR_UP
: case 'k':append_movement(NORTH
); break;
146 case CTRSEQ_CURSOR_RIGHT
:case 'l':append_movement(EAST
); break;
149 move_ph (g
.h
/2+LINE_OFFSET
, g
.w
*CW
/2);
150 printf ("\033[5mPAUSE\033[0m"); /* blinking text */
151 if (getchar() == 'q') exit(0);
155 case 'r': /*TODO:restart*/ return 0;
161 case 0x02: /* STX; gets sent when returning from SIGALRM */
168 void snake_advance (void) {
169 if (g
.k
.n
) {/* new direction in the buffer */
170 int possible_new_dir
= g
.k
.c
[(16+g
.k
.h
-g
.k
.n
--)%16];
171 if (g
.d
!= OPPOSITE(possible_new_dir
))
172 g
.d
= possible_new_dir
;
175 int new_row
= g
.s
->r
+(g
.d
==SOUTH
) -(g
.d
==NORTH
);
176 int new_col
= g
.s
->c
+(g
.d
==EAST
) -(g
.d
==WEST
);
178 /* detect food hit and spawn a new food */
179 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
180 if (i
->r
== new_row
&& i
->c
== new_col
) {
182 spawn_item(FOOD
, rand() % NUM_FOODS
, i
);
186 /* NOTE: we are drawing the snake 1 column too far to the left, so it
187 can directly touch the vertical walls. this also means that we essen-
188 tially extend the width by 1 cell, so we need to check against g.w+1. */
189 if (new_row
>= g
.h
|| new_col
>= g
.w
+1 || new_row
< 0 || new_col
< 0)
190 siglongjmp(game_over
, 1/*<-will be the retval of setjmp*/);
192 struct snake
* new_head
;
193 struct snake
* new_tail
; /* former second-to-last element */
194 for (new_tail
= g
.s
; new_tail
->next
->next
; new_tail
= new_tail
->next
)
195 /* use the opportunity of looping to check if we eat ourselves*/
196 if(new_tail
->next
->r
== new_row
&& new_tail
->next
->c
== new_col
)
197 siglongjmp(game_over
, 1/*<-will be the retval of setjmp*/);
198 int old_tail
[2] = {new_tail
->next
->r
, new_tail
->next
->c
}; /* save it for erasing */
199 new_head
= new_tail
->next
; /* reuse element instead of malloc() */
200 new_tail
->next
= NULL
;
202 new_head
->r
= new_row
;
203 new_head
->c
= new_col
;
204 new_head
->next
= g
.s
;
208 draw_sprites (old_tail
[0], old_tail
[1]);
211 void spawn_item (int type
, int value
, struct item
* p_item
) {
216 /* loop through snake to check if we aren't on it */
217 //WARN: inefficient as snake gets longer; near impossible in the end
218 for (struct snake
* s
= g
.s
; s
; s
= s
->next
)
219 if (s
->r
== row
&& s
->c
== col
) goto try_again
; //TODO: appears to not work all the time
221 /* if we got a item buffer reuse it, otherwise create a new one: */
222 struct item
* new_item
;
223 if (p_item
) new_item
= p_item
;
224 else new_item
= malloc (sizeof(struct item
));
230 new_item
->s
= time(0);
231 if (g
.i
) g
.i
->prev
= new_item
;
232 new_item
->next
= g
.i
;
233 new_item
->prev
= NULL
;
238 void consume_item (struct item
* i
) {
242 case FOOD_5
: g
.p
+= 5; break;
243 case FOOD_10
: g
.p
+= 10; break;
244 case FOOD_20
: g
.p
+= 20; break;
246 snake_append(&g
.s
, 0,0); /* position doesn't matter, as item */
247 break; /* will be reused as the head before it is drawn */
253 if (i
->next
) i
->next
->prev
= i
->prev
;
254 if (i
->prev
) i
->prev
->next
= i
->next
;
258 void show_playfield (void) {
263 printm (g
.w
, BORDER(T
,C
));
264 printf ("%s\n", BORDER(T
,R
));
267 for (int row
= 0; row
< g
.h
; row
++)
268 printf ("%s%*s%s\n", BORDER(C
,L
), CW
*g
.w
, "", BORDER(C
,R
));
272 printm (g
.w
, BORDER(B
,C
));
278 void draw_sprites (int erase_r
, int erase_c
) {
280 move_ph (erase_r
+LINE_OFFSET
, erase_c
*CW
+COL_OFFSET
);
284 struct snake
* last
= NULL
;
286 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
287 move_ph (s
->r
+LINE_OFFSET
, s
->c
*CW
+COL_OFFSET
); /*NOTE: all those are actually wrong; draws snake 1 col too far left*/
289 int predecessor
= (last
==NULL
)?NONE
:
290 (last
->r
< s
->r
) ? NORTH
:
291 (last
->r
> s
->r
) ? SOUTH
:
292 (last
->c
> s
->c
) ? EAST
:
293 (last
->c
< s
->c
) ? WEST
:NONE
;
294 int successor
= (s
->next
== NULL
)?NONE
:
295 (s
->next
->r
< s
->r
) ? NORTH
:
296 (s
->next
->r
> s
->r
) ? SOUTH
:
297 (s
->next
->c
> s
->c
) ? EAST
:
298 (s
->next
->c
< s
->c
) ? WEST
:NONE
;
300 printf ("\033[%sm", op
.scheme
->color
[color
]);
301 print (op
.scheme
->snake
[predecessor
][successor
]);
307 /* print item queue */
308 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
309 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
310 if (i
->t
== FOOD
) print (op
.scheme
->item
[i
->v
]);
311 else if (i
->t
==BONUS
) /* TODO: print bonus */;
315 int score_width
= g
.p
> 9999?6:4;
316 move_ph (0, (g
.w
*CW
-score_width
)/2);
317 printf ("%s %0*d %s", BORDER(S
,L
), score_width
, g
.p
, BORDER(S
,R
));
320 void snake_append (struct snake
** s
, int row
, int col
) {
321 struct snake
* new = malloc (sizeof(struct snake
));
327 struct snake
* p
= *s
;
328 while (p
->next
) p
= p
->next
;
336 for (int i
= 0; i
< op
.l
; i
++) {
337 if (g
.w
/2-i
< 0) /* go upwards if we hit left wall */
338 snake_append(&g
.s
, g
.h
/2-(i
-g
.w
/2), 0);
339 else /* normally just keep goint left */
340 snake_append(&g
.s
, g
.h
/2, g
.w
/2-i
);
344 #define free_ll(head) do{ \
364 int getctrlseq (void) {
367 while ((c
= getchar()) != EOF
) {
371 case '\033': state
=ESC_SENT
; break;
377 case '[': state
=CSI_SENT
; break;
378 default: return CTRSEQ_INVALID
;
383 case 'A': return CTRSEQ_CURSOR_UP
;
384 case 'B': return CTRSEQ_CURSOR_DOWN
;
385 case 'C': return CTRSEQ_CURSOR_RIGHT
;
386 case 'D': return CTRSEQ_CURSOR_LEFT
;
387 default: return CTRSEQ_INVALID
;
391 return CTRSEQ_INVALID
;
397 void append_movement (int dir
) {
398 if (g
.k
.n
> 15) return; /* no space in buffer */
399 if (g
.k
.n
&& g
.k
.c
[(16+g
.k
.h
-1)%16] == dir
) return; /* don't add the same direction twice */
400 // if (g.k.n && g.k.c[(16+g.k.h-1)%16] == OPPOSITE(dir)) return; /* don't add impossible dir. */
403 g
.k
.h
= ++g
.k
.h
% 16;
406 void move_ph (int line
, int col
) {
407 /* move printhead to zero-indexed position */
408 printf ("\033[%d;%dH", line
+1, col
+1);
411 void clamp_fieldsize (void) {
412 /* clamp field size to terminal size and mouse maximum: */
414 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
416 if (g
.w
< 10) g
.w
= 10;
417 if (g
.h
< 10) g
.h
= 10;
419 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
420 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
-1; //TODO: does not work in `-d' (in xterm)
421 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
422 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
425 void timer_setup (int enable
) {
426 static struct itimerval tbuf
;
427 tbuf
.it_interval
.tv_sec
= 0;//TODO: make it speed up automatically
428 tbuf
.it_interval
.tv_usec
= (1000000/g
.v
)-1; /*WARN: 1 <= g.v <= 999999*/
431 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
432 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
434 tbuf
.it_value
.tv_sec
= 0;
435 tbuf
.it_value
.tv_usec
= 0;
438 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
445 void signal_setup (void) {
446 struct sigaction saction
;
448 saction
.sa_handler
= signal_handler
;
449 sigemptyset(&saction
.sa_mask
);
450 saction
.sa_flags
= 0;
451 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
456 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
462 void signal_handler (int signum
) {
472 void screen_setup (int enable
) {
475 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
476 printf ("\033[H\033[J"); /* reset cursor, clear screen */
477 printf ("\033[?25l"); /* hide cursor */
478 print (op
.scheme
->init_seq
); /* swich charset, if necessary */
480 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
481 printf ("\033[?25h"); /* show cursor */
482 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
487 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
488 void raw_mode(int enable
) {
489 static struct termios saved_term_mode
;
490 struct termios raw_term_mode
;
493 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
494 raw_term_mode
= saved_term_mode
;
495 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
496 raw_term_mode
.c_cc
[VMIN
] = 1 ;
497 raw_term_mode
.c_cc
[VTIME
] = 0;
498 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
500 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);