]>
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 2 /* 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
72 op
.scheme
= &unic0de
; //TODO: expose to getopt() once more sets are available
75 opterr
= 0; /* don't print message on unrecognized option */
76 while ((optget
= getopt (argc
, argv
, "+h")) != -1) {
80 fprintf (stderr
, SHORTHELP LONGHELP
, argv
[0]);
81 return !(optget
=='h');
83 } if (optind
< argc
) { /* parse Fieldspec */
91 if (sigsetjmp(game_over
, 1)) {
93 move_ph (g
.h
/2+LINE_OFFSET
, g
.w
);
94 printf ("you died :(");
99 //TODO: call viiper() in a game loop
113 spawn_item(FOOD
, rand() % NUM_FOODS
); //TODO: shape distribution, so bigger values get selected less
116 switch (getctrlseq()) {
117 case '+': g
.v
++;timer_setup(1);break; //TODO: temporary, to set speed
118 case '#': if (g
.v
> 1) g
.v
--;timer_setup(1); break; //TODO: temporary, to set speed
119 case '\n': spawn_item(FOOD
, rand() % NUM_FOODS
); break; //TODO: for debugging segfault
121 case CTRSEQ_CURSOR_LEFT
:
122 case 'h': append_movement(WEST
); break;
123 case CTRSEQ_CURSOR_DOWN
:
124 case 'j': append_movement(SOUTH
); break;
125 case CTRSEQ_CURSOR_UP
:
126 case 'k': append_movement(NORTH
); break;
127 case CTRSEQ_CURSOR_RIGHT
:
128 case 'l': append_movement(EAST
); break;
129 case 'p': /*TODO: pause*/ break;
130 case 'r': /*TODO:restart*/ return 0;
138 print ("\033[H\033[J");
139 show_playfield ();//TODO: only redraw diff
144 void snake_advance (void) {
145 if (g
.n
) {/* new direction in the buffer */
146 int possible_new_dir
= get_movement();
147 if (g
.d
== EAST
&& possible_new_dir
== WEST
) goto ignore_new
;
148 if (g
.d
== WEST
&& possible_new_dir
== EAST
) goto ignore_new
;
149 if (g
.d
== NORTH
&& possible_new_dir
== SOUTH
) goto ignore_new
;
150 if (g
.d
== SOUTH
&& possible_new_dir
== NORTH
) goto ignore_new
;
151 g
.d
= possible_new_dir
; /*pop off a new direction if available*/
156 int new_row
= g
.s
->r
+(g
.d
==SOUTH
) -(g
.d
==NORTH
);
157 int new_col
= g
.s
->c
+(g
.d
==EAST
) -(g
.d
==WEST
);
159 /* detect food hit and spawn a new food */
160 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
161 if (i
->r
== new_row
&& i
->c
== new_col
) {
163 spawn_item(FOOD
, rand() % NUM_FOODS
);
167 /*NOTE:no idea why I have to use g.w+1, but otherwise we die too early*/
168 if (new_row
>= g
.h
|| new_col
>= g
.w
+1 || new_row
< 0 || new_col
< 0)
169 siglongjmp(game_over
, 1/*<-will be the retval of setjmp*/);
171 struct snake
* new_head
;
172 struct snake
* new_tail
; /* former second-to-last element */
173 for (new_tail
= g
.s
; new_tail
->next
->next
; new_tail
= new_tail
->next
)
174 /* use the opportunity of looping to check if we eat ourselves*/
175 if(new_tail
->next
->r
== new_row
&& new_tail
->next
->c
== new_col
)
176 siglongjmp(game_over
, 1/*<-will be the retval of setjmp*/);
177 new_head
= new_tail
->next
; /* reuse element instead of malloc() */
178 new_tail
->next
= NULL
;
180 new_head
->r
= new_row
;
181 new_head
->c
= new_col
;
182 new_head
->next
= g
.s
;
187 void spawn_item (int type
, int value
) {
192 /* loop through snake to check if we aren't on it */
193 //TODO: inefficient as snake gets longer; near impossible in the end
194 for (struct snake
* s
= g
.s
; s
; s
= s
->next
)
195 if (s
->r
== row
&& s
->c
== col
) goto try_again
;
197 //3. get item from category TODO
198 struct item
* new_item
= malloc (sizeof(struct item
));
203 new_item
->s
= time(0);
204 if (g
.i
) g
.i
->prev
= new_item
;
205 new_item
->next
= g
.i
;
206 new_item
->prev
= NULL
;
211 void consume_item (struct item
* i
) {
215 case FOOD_5
: g
.p
+= 5; break;
216 case FOOD_10
: g
.p
+= 10; break;
217 case FOOD_20
: g
.p
+= 20; break;
219 snake_append(&g
.s
, 0,0); /* position doesn't matter, as item */
220 break; /* will be reused as the head before it is drawn */
226 if (i
->next
) i
->next
->prev
= i
->prev
;
227 if (i
->prev
) i
->prev
->next
= i
->next
;
233 void show_playfield (void) {
234 //int score_width = g.p > 9999?6:4;
235 int score_width
= 4; // v- = the -| spacer |-
236 float half_width
= (g
.w
- score_width
/CW
- 2)/2.0; //TODO: this whole endavour is ugly
239 printm (half_width
, BORDER(T
,C
)); //TODO: i bet this breaks in dec mode
240 printf ("%s %0*d %s", BORDER(S
,L
), score_width
, g
.p
, BORDER(S
,R
));
241 printm ((int)(half_width
+.5), BORDER(T
,C
));
242 printf ("%s\n", BORDER(T
,R
));
245 for (int row
= 0; row
< g
.h
; row
++)
246 printf ("%s%*s%s\n", BORDER(C
,L
), CW
*g
.w
, "", BORDER(C
,R
));
250 printm (g
.w
, BORDER(B
,C
));
254 struct snake
* last
= NULL
;
255 int color
= -1; //TODO: that's a hack
256 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
257 move_ph (s
->r
+COL_OFFSET
, s
->c
*CW
+LINE_OFFSET
);
259 int predecessor
= (last
==NULL
)?NONE
:
260 (last
->r
< s
->r
) ? NORTH
:
261 (last
->r
> s
->r
) ? SOUTH
:
262 (last
->c
> s
->c
) ? EAST
:
263 (last
->c
< s
->c
) ? WEST
:NONE
;
264 int successor
= (s
->next
== NULL
)?NONE
:
265 (s
->next
->r
< s
->r
) ? NORTH
:
266 (s
->next
->r
> s
->r
) ? SOUTH
:
267 (s
->next
->c
> s
->c
) ? EAST
:
268 (s
->next
->c
< s
->c
) ? WEST
:NONE
;
270 printf ("\033[%sm", color
==-1?"1m\033[92":color
?"92":"32"); //TODO: clean this up
271 print (op
.scheme
->snake
[predecessor
][successor
]);
274 color
= (color
+1) % 2;
277 /* print item queue */
278 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
279 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
280 if (i
->t
== FOOD
) print (op
.scheme
->item
[i
->v
]);
281 else if (i
->t
==BONUS
) /* TODO: print bonus */;
284 //TODO: temporarily print speed
285 move_ph (g
.h
+LINE_OFFSET
, g
.w
);
289 void snake_append (struct snake
** s
, int row
, int col
) {
290 struct snake
* new = malloc (sizeof(struct snake
));
296 struct snake
* p
= *s
;
297 while (p
->next
) p
= p
->next
;
305 for (int i
= 0; i
< op
.l
; i
++)
306 snake_append(&g
.s
, g
.h
/2, g
.w
/2-i
);
309 #define free_ll(head) do{ \
321 free_ll(g
.n
); //TODO: doesn't get free'd correctly
330 int getctrlseq (void) {
333 int offset
= 0x20; /* never sends control chars as data */
334 while ((c
= getchar()) != EOF
) {
338 case '\033': state
=ESC_SENT
; break;
344 case '[': state
=CSI_SENT
; break;
345 default: return CTRSEQ_INVALID
;
350 case 'A': return CTRSEQ_CURSOR_UP
;
351 case 'B': return CTRSEQ_CURSOR_DOWN
;
352 case 'C': return CTRSEQ_CURSOR_RIGHT
;
353 case 'D': return CTRSEQ_CURSOR_LEFT
;
354 default: return CTRSEQ_INVALID
;
358 return CTRSEQ_INVALID
;
364 void append_movement (int dir
) {
365 struct directions
* n
;
366 for (n
= g
.n
; n
&& n
->next
; n
= n
->next
); /* advance to the end */
367 if (n
&& n
->d
== dir
) return; /* don't add the same direction twice */
369 struct directions
* new_event
= malloc (sizeof(struct directions
));
371 new_event
->next
= NULL
;
379 int get_movement (void) {
380 if (g
.n
== NULL
) return -1;
383 struct directions
* delet_this
= g
.n
;
389 void move_ph (int line
, int col
) {
390 /* move printhead to zero-indexed position */
391 printf ("\033[%d;%dH", line
+1, col
+1);
394 void clamp_fieldsize (void) { //TODO: use
395 /* clamp field size to terminal size and mouse maximum: */
397 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
399 if (g
.w
< 1) g
.w
= 1;
400 if (g
.h
< 1) g
.h
= 1;
402 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
403 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
; //TODO: does not work in `-d' (in xterm)
404 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
405 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
408 void timer_setup (int enable
) {
409 static struct itimerval tbuf
;
410 tbuf
.it_interval
.tv_sec
= 0;//TODO: make it speed up automatically
411 tbuf
.it_interval
.tv_usec
= (1000000/g
.v
)-1; /*WARN: 1 <= g.v <= 999999*/
414 g
.t
= time(NULL
);//TODO: interferes with 'pause'
415 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
416 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
418 tbuf
.it_value
.tv_sec
= 0;
419 tbuf
.it_value
.tv_usec
= 0;
422 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
429 void signal_setup (void) {
430 struct sigaction saction
;
432 saction
.sa_handler
= signal_handler
;
433 sigemptyset(&saction
.sa_mask
);
434 saction
.sa_flags
= 0;
435 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
440 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
446 void signal_handler (int signum
) {
450 //dtime = difftime (time(NULL), g.t);
451 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
452 //printf ("[%03d]", g.t?dtime:0);
460 void screen_setup (int enable
) {
463 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
464 printf ("\033[H\033[J"); /* reset cursor, clear screen */
465 printf ("\033[?25l"); /* hide cursor */
466 print (op
.scheme
->init_seq
); /* swich charset, if necessary */
468 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
469 printf ("\033[?25h"); /* show cursor */
470 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
475 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
476 void raw_mode(int enable
) {
477 static struct termios saved_term_mode
;
478 struct termios raw_term_mode
;
481 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
482 raw_term_mode
= saved_term_mode
;
483 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
484 raw_term_mode
.c_cc
[VMIN
] = 1 ;
485 raw_term_mode
.c_cc
[VTIME
] = 0;
486 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
488 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);