]>
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*/
25 #include <sys/ioctl.h>
34 #define MIN(a,b) (a>b?b:a)
35 #define MAX(a,b) (a>b?a:b)
36 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
37 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
38 #define print(str) fputs (str?str:"", stdout)
44 #define CW op.scheme->cell_width
47 int w
; /* field width */
48 int h
; /* field height */
49 int d
; /* direction the snake is looking */
50 int t
; /* time of game start */
52 float v
; /* velocity in moves per second */
53 struct snake
* s
; /* snek */
54 struct item
* i
; /* items (food, boni) */
55 struct directions
* n
;/* next direction events to process */
59 int l
; /* initial snake length */
60 int s
; /* initial snake speed */
61 struct scheme
* scheme
;
66 int main (int argc
, char** argv
) {
68 g
.w
= 30; //two-char-width
75 opterr
= 0; /* don't print message on unrecognized option */
76 while ((optget
= getopt (argc
, argv
, "+s:dh")) != -1) {
81 fprintf (stderr
, SHORTHELP
"speed must be >= 1\n", argv
[0]);
85 case 'd': op
.scheme
= &vt220_charset
; break;
88 fprintf (stderr
, SHORTHELP LONGHELP
, argv
[0]);
89 return !(optget
=='h');
91 } if (optind
< argc
) { /* parse Fieldspec */
92 int n
= sscanf (argv
[optind
], "%dx%d", &g
.w
, &g
.h
);
95 fprintf (stderr
, SHORTHELP
"FIELDSIZE is WxH (width 'x' height)\n", argv
[0]);
107 if (sigsetjmp(game_over
, 1)) {
109 move_ph (g
.h
/2+LINE_OFFSET
, g
.w
);
110 printf ("you died :(");
116 //TODO: call viiper() in a game loop
130 spawn_item(FOOD
, rand() % NUM_FOODS
); //TODO: shape distribution, so bigger values get selected less
133 switch (getctrlseq()) {
134 case CTRSEQ_CURSOR_LEFT
: case 'h':append_movement(WEST
); break;
135 case CTRSEQ_CURSOR_DOWN
: case 'j':append_movement(SOUTH
); break;
136 case CTRSEQ_CURSOR_UP
: case 'k':append_movement(NORTH
); break;
137 case CTRSEQ_CURSOR_RIGHT
:case 'l':append_movement(EAST
); break;
138 case 'p': /*TODO: pause*/ break;
139 case 'r': /*TODO:restart*/ return 0;
147 print ("\033[H\033[J");
148 show_playfield ();//TODO: only redraw diff
153 void snake_advance (void) {
154 if (g
.n
) {/* new direction in the buffer */
155 int possible_new_dir
= get_movement();
156 if (g
.d
== EAST
&& possible_new_dir
== WEST
) goto ignore_new
;
157 if (g
.d
== WEST
&& possible_new_dir
== EAST
) goto ignore_new
;
158 if (g
.d
== NORTH
&& possible_new_dir
== SOUTH
) goto ignore_new
;
159 if (g
.d
== SOUTH
&& possible_new_dir
== NORTH
) goto ignore_new
;
160 g
.d
= possible_new_dir
; /*pop off a new direction if available*/
165 int new_row
= g
.s
->r
+(g
.d
==SOUTH
) -(g
.d
==NORTH
);
166 int new_col
= g
.s
->c
+(g
.d
==EAST
) -(g
.d
==WEST
);
168 /* detect food hit and spawn a new food */
169 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
170 if (i
->r
== new_row
&& i
->c
== new_col
) {
172 spawn_item(FOOD
, rand() % NUM_FOODS
);
176 /*NOTE:no idea why I have to use g.w+1, but otherwise we die too early*/
177 if (new_row
>= g
.h
|| new_col
>= g
.w
+1 || new_row
< 0 || new_col
< 0)
178 siglongjmp(game_over
, 1/*<-will be the retval of setjmp*/);
180 struct snake
* new_head
;
181 struct snake
* new_tail
; /* former second-to-last element */
182 for (new_tail
= g
.s
; new_tail
->next
->next
; new_tail
= new_tail
->next
)
183 /* use the opportunity of looping to check if we eat ourselves*/
184 if(new_tail
->next
->r
== new_row
&& new_tail
->next
->c
== new_col
)
185 siglongjmp(game_over
, 1/*<-will be the retval of setjmp*/);
186 new_head
= new_tail
->next
; /* reuse element instead of malloc() */
187 new_tail
->next
= NULL
;
189 new_head
->r
= new_row
;
190 new_head
->c
= new_col
;
191 new_head
->next
= g
.s
;
196 void spawn_item (int type
, int value
) {
201 /* loop through snake to check if we aren't on it */
202 //WARN: inefficient as snake gets longer; near impossible in the end
203 for (struct snake
* s
= g
.s
; s
; s
= s
->next
)
204 if (s
->r
== row
&& s
->c
== col
) goto try_again
;
206 struct item
* new_item
= malloc (sizeof(struct item
));
211 new_item
->s
= time(0);
212 if (g
.i
) g
.i
->prev
= new_item
;
213 new_item
->next
= g
.i
;
214 new_item
->prev
= NULL
;
219 void consume_item (struct item
* i
) {
223 case FOOD_5
: g
.p
+= 5; break;
224 case FOOD_10
: g
.p
+= 10; break;
225 case FOOD_20
: g
.p
+= 20; break;
227 snake_append(&g
.s
, 0,0); /* position doesn't matter, as item */
228 break; /* will be reused as the head before it is drawn */
234 if (i
->next
) i
->next
->prev
= i
->prev
;
235 if (i
->prev
) i
->prev
->next
= i
->next
;
241 void show_playfield (void) {
244 printm (g
.w
, BORDER(T
,C
));
245 printf ("%s\n", BORDER(T
,R
));
248 for (int row
= 0; row
< g
.h
; row
++)
249 printf ("%s%*s%s\n", BORDER(C
,L
), CW
*g
.w
, "", BORDER(C
,R
));
253 printm (g
.w
, BORDER(B
,C
));
257 int score_width
= g
.p
> 9999?6:4;
258 move_ph (0, (g
.w
*CW
-score_width
)/2);
259 printf ("%s %0*d %s", BORDER(S
,L
), score_width
, g
.p
, BORDER(S
,R
));
262 struct snake
* last
= NULL
;
263 int color
= -1; //TODO: that's a hack
264 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
265 move_ph (s
->r
+COL_OFFSET
, s
->c
*CW
+LINE_OFFSET
);
267 int predecessor
= (last
==NULL
)?NONE
:
268 (last
->r
< s
->r
) ? NORTH
:
269 (last
->r
> s
->r
) ? SOUTH
:
270 (last
->c
> s
->c
) ? EAST
:
271 (last
->c
< s
->c
) ? WEST
:NONE
;
272 int successor
= (s
->next
== NULL
)?NONE
:
273 (s
->next
->r
< s
->r
) ? NORTH
:
274 (s
->next
->r
> s
->r
) ? SOUTH
:
275 (s
->next
->c
> s
->c
) ? EAST
:
276 (s
->next
->c
< s
->c
) ? WEST
:NONE
;
278 printf ("\033[%sm", color
==-1?"92;1":color
?"92":"32"); //TODO: clean this up
279 print (op
.scheme
->snake
[predecessor
][successor
]);
282 color
= (color
+1) % 2;
285 /* print item queue */
286 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
287 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
288 if (i
->t
== FOOD
) print (op
.scheme
->item
[i
->v
]);
289 else if (i
->t
==BONUS
) /* TODO: print bonus */;
293 void snake_append (struct snake
** s
, int row
, int col
) {
294 struct snake
* new = malloc (sizeof(struct snake
));
300 struct snake
* p
= *s
;
301 while (p
->next
) p
= p
->next
;
309 for (int i
= 0; i
< op
.l
; i
++)
310 snake_append(&g
.s
, g
.h
/2, g
.w
/2-i
);
313 #define free_ll(head) do{ \
334 int getctrlseq (void) {
337 int offset
= 0x20; /* never sends control chars as data */
338 while ((c
= getchar()) != EOF
) {
342 case '\033': state
=ESC_SENT
; break;
348 case '[': state
=CSI_SENT
; break;
349 default: return CTRSEQ_INVALID
;
354 case 'A': return CTRSEQ_CURSOR_UP
;
355 case 'B': return CTRSEQ_CURSOR_DOWN
;
356 case 'C': return CTRSEQ_CURSOR_RIGHT
;
357 case 'D': return CTRSEQ_CURSOR_LEFT
;
358 default: return CTRSEQ_INVALID
;
362 return CTRSEQ_INVALID
;
368 void append_movement (int dir
) {
369 struct directions
* n
;
370 for (n
= g
.n
; n
&& n
->next
; n
= n
->next
); /* advance to the end */
371 if (n
&& n
->d
== dir
) return; /* don't add the same direction twice */
373 struct directions
* new_event
= malloc (sizeof(struct directions
));
375 new_event
->next
= NULL
;
383 int get_movement (void) {
384 if (g
.n
== NULL
) return -1;
387 struct directions
* delet_this
= g
.n
;
393 void move_ph (int line
, int col
) {
394 /* move printhead to zero-indexed position */
395 printf ("\033[%d;%dH", line
+1, col
+1);
398 void clamp_fieldsize (void) { //TODO: use
399 /* clamp field size to terminal size and mouse maximum: */
401 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
403 if (g
.w
< 1) g
.w
= 1; //TODO. sensible minimum
404 if (g
.h
< 1) g
.h
= 1;
406 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
407 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
-1; //TODO: does not work in `-d' (in xterm)
408 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
409 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
412 void timer_setup (int enable
) {
413 static struct itimerval tbuf
;
414 tbuf
.it_interval
.tv_sec
= 0;//TODO: make it speed up automatically
415 tbuf
.it_interval
.tv_usec
= (1000000/g
.v
)-1; /*WARN: 1 <= g.v <= 999999*/
418 g
.t
= time(NULL
);//TODO: interferes with 'pause'
419 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
420 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
422 tbuf
.it_value
.tv_sec
= 0;
423 tbuf
.it_value
.tv_usec
= 0;
426 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
433 void signal_setup (void) {
434 struct sigaction saction
;
436 saction
.sa_handler
= signal_handler
;
437 sigemptyset(&saction
.sa_mask
);
438 saction
.sa_flags
= 0;
439 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
444 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
450 void signal_handler (int signum
) {
454 //dtime = difftime (time(NULL), g.t);
455 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
456 //printf ("[%03d]", g.t?dtime:0);
464 void screen_setup (int enable
) {
467 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
468 printf ("\033[H\033[J"); /* reset cursor, clear screen */
469 printf ("\033[?25l"); /* hide cursor */
470 print (op
.scheme
->init_seq
); /* swich charset, if necessary */
472 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
473 printf ("\033[?25h"); /* show cursor */
474 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
479 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
480 void raw_mode(int enable
) {
481 static struct termios saved_term_mode
;
482 struct termios raw_term_mode
;
485 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
486 raw_term_mode
= saved_term_mode
;
487 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
488 raw_term_mode
.c_cc
[VMIN
] = 1 ;
489 raw_term_mode
.c_cc
[VTIME
] = 0;
490 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
492 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);