]>
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)
42 #define CW op.scheme->cell_width
45 int w
; /* field width */
46 int h
; /* field height */
47 int d
; /* direction the snake is looking */
48 int t
; /* time of game start */
50 float v
; /* velocity in moves per second */
51 struct snake
* s
; /* snek */
52 struct item
* i
; /* items (food, boni) */
53 struct directions
* n
;/* next direction events to process */
57 int l
; /* initial snake length */
58 int s
; /* initial snake speed */
59 struct scheme
* scheme
;
64 int main (int argc
, char** argv
) {
66 g
.w
= 30; //two-char-width
73 opterr
= 0; /* don't print message on unrecognized option */
74 while ((optget
= getopt (argc
, argv
, "+s:dh")) != -1) {
79 fprintf (stderr
, SHORTHELP
"speed must be >= 1\n", argv
[0]);
83 case 'd': op
.scheme
= &vt220_charset
; break;
86 fprintf (stderr
, SHORTHELP LONGHELP
, argv
[0]);
87 return !(optget
=='h');
89 } if (optind
< argc
) { /* parse Fieldspec */
90 int n
= sscanf (argv
[optind
], "%dx%d", &g
.w
, &g
.h
);
93 fprintf (stderr
, SHORTHELP
"FIELDSIZE is WxH (width 'x' height)\n", argv
[0]);
105 if (sigsetjmp(game_over
, 1)) {
107 move_ph (g
.h
/2+LINE_OFFSET
, g
.w
);
108 printf ("you died :(");
114 //TODO: call viiper() in a game loop
129 spawn_item(FOOD
, rand() % NUM_FOODS
); //TODO: shape distribution, so bigger values get selected less
132 switch (getctrlseq()) {
133 case CTRSEQ_CURSOR_LEFT
: case 'h':append_movement(WEST
); break;
134 case CTRSEQ_CURSOR_DOWN
: case 'j':append_movement(SOUTH
); break;
135 case CTRSEQ_CURSOR_UP
: case 'k':append_movement(NORTH
); break;
136 case CTRSEQ_CURSOR_RIGHT
:case 'l':append_movement(EAST
); break;
139 move_ph (g
.h
/2+LINE_OFFSET
, g
.w
*CW
/2);
141 if (getchar() == 'q') exit(0);
144 case 'r': /*TODO:restart*/ return 0;
152 print ("\033[H\033[J");
153 show_playfield ();//TODO: only redraw diff
158 void snake_advance (void) {
159 if (g
.n
) {/* new direction in the buffer */
160 int possible_new_dir
= get_movement();
161 if (g
.d
== EAST
&& possible_new_dir
== WEST
) goto ignore_new
;
162 if (g
.d
== WEST
&& possible_new_dir
== EAST
) goto ignore_new
;
163 if (g
.d
== NORTH
&& possible_new_dir
== SOUTH
) goto ignore_new
;
164 if (g
.d
== SOUTH
&& possible_new_dir
== NORTH
) goto ignore_new
;
165 g
.d
= possible_new_dir
; /*pop off a new direction if available*/
170 int new_row
= g
.s
->r
+(g
.d
==SOUTH
) -(g
.d
==NORTH
);
171 int new_col
= g
.s
->c
+(g
.d
==EAST
) -(g
.d
==WEST
);
173 /* detect food hit and spawn a new food */
174 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
175 if (i
->r
== new_row
&& i
->c
== new_col
) {
177 spawn_item(FOOD
, rand() % NUM_FOODS
);
181 /*NOTE:no idea why I have to use g.w+1, but otherwise we die too early*/
182 if (new_row
>= g
.h
|| new_col
>= g
.w
+1 || new_row
< 0 || new_col
< 0)
183 siglongjmp(game_over
, 1/*<-will be the retval of setjmp*/);
185 struct snake
* new_head
;
186 struct snake
* new_tail
; /* former second-to-last element */
187 for (new_tail
= g
.s
; new_tail
->next
->next
; new_tail
= new_tail
->next
)
188 /* use the opportunity of looping to check if we eat ourselves*/
189 if(new_tail
->next
->r
== new_row
&& new_tail
->next
->c
== new_col
)
190 siglongjmp(game_over
, 1/*<-will be the retval of setjmp*/);
191 new_head
= new_tail
->next
; /* reuse element instead of malloc() */
192 new_tail
->next
= NULL
;
194 new_head
->r
= new_row
;
195 new_head
->c
= new_col
;
196 new_head
->next
= g
.s
;
201 void spawn_item (int type
, int value
) {
206 /* loop through snake to check if we aren't on it */
207 //WARN: inefficient as snake gets longer; near impossible in the end
208 for (struct snake
* s
= g
.s
; s
; s
= s
->next
)
209 if (s
->r
== row
&& s
->c
== col
) goto try_again
;
211 struct item
* new_item
= malloc (sizeof(struct item
));
216 new_item
->s
= time(0);
217 if (g
.i
) g
.i
->prev
= new_item
;
218 new_item
->next
= g
.i
;
219 new_item
->prev
= NULL
;
224 void consume_item (struct item
* i
) {
228 case FOOD_5
: g
.p
+= 5; break;
229 case FOOD_10
: g
.p
+= 10; break;
230 case FOOD_20
: g
.p
+= 20; break;
232 snake_append(&g
.s
, 0,0); /* position doesn't matter, as item */
233 break; /* will be reused as the head before it is drawn */
239 if (i
->next
) i
->next
->prev
= i
->prev
;
240 if (i
->prev
) i
->prev
->next
= i
->next
;
246 void show_playfield (void) {
249 printm (g
.w
, BORDER(T
,C
));
250 printf ("%s\n", BORDER(T
,R
));
253 for (int row
= 0; row
< g
.h
; row
++)
254 printf ("%s%*s%s\n", BORDER(C
,L
), CW
*g
.w
, "", BORDER(C
,R
));
258 printm (g
.w
, BORDER(B
,C
));
262 int score_width
= g
.p
> 9999?6:4;
263 move_ph (0, (g
.w
*CW
-score_width
)/2);
264 printf ("%s %0*d %s", BORDER(S
,L
), score_width
, g
.p
, BORDER(S
,R
));
267 struct snake
* last
= NULL
;
268 int color
= -1; //TODO: that's a hack
269 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
270 move_ph (s
->r
+COL_OFFSET
, s
->c
*CW
+LINE_OFFSET
);
272 int predecessor
= (last
==NULL
)?NONE
:
273 (last
->r
< s
->r
) ? NORTH
:
274 (last
->r
> s
->r
) ? SOUTH
:
275 (last
->c
> s
->c
) ? EAST
:
276 (last
->c
< s
->c
) ? WEST
:NONE
;
277 int successor
= (s
->next
== NULL
)?NONE
:
278 (s
->next
->r
< s
->r
) ? NORTH
:
279 (s
->next
->r
> s
->r
) ? SOUTH
:
280 (s
->next
->c
> s
->c
) ? EAST
:
281 (s
->next
->c
< s
->c
) ? WEST
:NONE
;
283 printf ("\033[%sm", color
==-1?"92;1":color
?"92":"32"); //TODO: clean this up
284 print (op
.scheme
->snake
[predecessor
][successor
]);
287 color
= (color
+1) % 2;
290 /* print item queue */
291 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
292 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
293 if (i
->t
== FOOD
) print (op
.scheme
->item
[i
->v
]);
294 else if (i
->t
==BONUS
) /* TODO: print bonus */;
298 void snake_append (struct snake
** s
, int row
, int col
) {
299 struct snake
* new = malloc (sizeof(struct snake
));
305 struct snake
* p
= *s
;
306 while (p
->next
) p
= p
->next
;
314 for (int i
= 0; i
< op
.l
; i
++)
315 snake_append(&g
.s
, g
.h
/2, g
.w
/2-i
);
318 #define free_ll(head) do{ \
339 int getctrlseq (void) {
342 int offset
= 0x20; /* never sends control chars as data */
343 while ((c
= getchar()) != EOF
) {
347 case '\033': state
=ESC_SENT
; break;
353 case '[': state
=CSI_SENT
; break;
354 default: return CTRSEQ_INVALID
;
359 case 'A': return CTRSEQ_CURSOR_UP
;
360 case 'B': return CTRSEQ_CURSOR_DOWN
;
361 case 'C': return CTRSEQ_CURSOR_RIGHT
;
362 case 'D': return CTRSEQ_CURSOR_LEFT
;
363 default: return CTRSEQ_INVALID
;
367 return CTRSEQ_INVALID
;
373 void append_movement (int dir
) {
374 struct directions
* n
;
375 for (n
= g
.n
; n
&& n
->next
; n
= n
->next
); /* advance to the end */
376 if (n
&& n
->d
== dir
) return; /* don't add the same direction twice */
378 struct directions
* new_event
= malloc (sizeof(struct directions
));
380 new_event
->next
= NULL
;
388 int get_movement (void) {
389 if (g
.n
== NULL
) return -1;
392 struct directions
* delet_this
= g
.n
;
398 void move_ph (int line
, int col
) {
399 /* move printhead to zero-indexed position */
400 printf ("\033[%d;%dH", line
+1, col
+1);
403 void clamp_fieldsize (void) { //TODO: use
404 /* clamp field size to terminal size and mouse maximum: */
406 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
408 if (g
.w
< 1) g
.w
= 1; //TODO. sensible minimum
409 if (g
.h
< 1) g
.h
= 1;
411 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
412 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
-1; //TODO: does not work in `-d' (in xterm)
413 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
414 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
417 void timer_setup (int enable
) {
418 static struct itimerval tbuf
;
419 tbuf
.it_interval
.tv_sec
= 0;//TODO: make it speed up automatically
420 tbuf
.it_interval
.tv_usec
= (1000000/g
.v
)-1; /*WARN: 1 <= g.v <= 999999*/
423 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
424 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
426 tbuf
.it_value
.tv_sec
= 0;
427 tbuf
.it_value
.tv_usec
= 0;
430 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
437 void signal_setup (void) {
438 struct sigaction saction
;
440 saction
.sa_handler
= signal_handler
;
441 sigemptyset(&saction
.sa_mask
);
442 saction
.sa_flags
= 0;
443 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
448 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
454 void signal_handler (int signum
) {
458 //dtime = difftime (time(NULL), g.t);
459 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
460 //printf ("[%03d]", g.t?dtime:0);
468 void screen_setup (int enable
) {
471 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
472 printf ("\033[H\033[J"); /* reset cursor, clear screen */
473 printf ("\033[?25l"); /* hide cursor */
474 print (op
.scheme
->init_seq
); /* swich charset, if necessary */
476 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
477 printf ("\033[?25h"); /* show cursor */
478 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
483 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
484 void raw_mode(int enable
) {
485 static struct termios saved_term_mode
;
486 struct termios raw_term_mode
;
489 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
490 raw_term_mode
= saved_term_mode
;
491 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
492 raw_term_mode
.c_cc
[VMIN
] = 1 ;
493 raw_term_mode
.c_cc
[VTIME
] = 0;
494 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
496 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);