]>
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
;
210 void consume_item (struct item
* i
) {
211 struct item
* predecessor
= i
->prev
;
212 struct item
* successor
= i
->next
;
217 case FOOD_5
: g
.p
+= 5; break;
218 case FOOD_10
: g
.p
+= 10; break;
219 case FOOD_20
: g
.p
+= 20; break;
221 snake_append(&g
.s
, 0,0); /* position doesn't matter, as item */
222 break; /* will be reused as the head before it is drawn */
228 if (predecessor
== NULL
) {
230 if (successor
) successor
->prev
= NULL
;
232 predecessor
->next
= successor
;
233 successor
->prev
= predecessor
; //TODO: segfaults here if we eat the first one if more than one is on the screen
239 void show_playfield (void) {
240 //int score_width = g.p > 9999?6:4;
241 int score_width
= 4; // v- = the -| spacer |-
242 float half_width
= (g
.w
- score_width
/CW
- 2)/2.0; //TODO: this whole endavour is ugly
245 printm (half_width
, BORDER(T
,C
)); //TODO: i bet this breaks in dec mode
246 printf ("%s %0*d %s", BORDER(S
,L
), score_width
, g
.p
, BORDER(S
,R
));
247 printm ((int)(half_width
+.5), BORDER(T
,C
));
248 printf ("%s\n", BORDER(T
,R
));
251 for (int row
= 0; row
< g
.h
; row
++)
252 printf ("%s%*s%s\n", BORDER(C
,L
), CW
*g
.w
, "", BORDER(C
,R
));
256 printm (g
.w
, BORDER(B
,C
));
260 struct snake
* last
= NULL
;
261 int color
= -1; //TODO: that's a hack
262 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
263 move_ph (s
->r
+COL_OFFSET
, s
->c
*CW
+LINE_OFFSET
);
265 int predecessor
= (last
==NULL
)?NONE
:
266 (last
->r
< s
->r
) ? NORTH
:
267 (last
->r
> s
->r
) ? SOUTH
:
268 (last
->c
> s
->c
) ? EAST
:
269 (last
->c
< s
->c
) ? WEST
:NONE
;
270 int successor
= (s
->next
== NULL
)?NONE
:
271 (s
->next
->r
< s
->r
) ? NORTH
:
272 (s
->next
->r
> s
->r
) ? SOUTH
:
273 (s
->next
->c
> s
->c
) ? EAST
:
274 (s
->next
->c
< s
->c
) ? WEST
:NONE
;
276 printf ("\033[%sm", color
==-1?"1m\033[92":color
?"92":"32"); //TODO: clean this up
277 print (op
.scheme
->snake
[predecessor
][successor
]);
280 color
= (color
+1) % 2;
283 /* print item queue */
284 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
285 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
286 if (i
->t
== FOOD
) print (op
.scheme
->item
[i
->v
]);
287 else if (i
->t
==BONUS
) /* TODO: print bonus */;
290 //TODO: temporarily print speed
291 move_ph (g
.h
+LINE_OFFSET
, g
.w
);
295 void snake_append (struct snake
** s
, int row
, int col
) {
296 struct snake
* new = malloc (sizeof(struct snake
));
302 struct snake
* p
= *s
;
303 while (p
->next
) p
= p
->next
;
311 for (int i
= 0; i
< op
.l
; i
++)
312 snake_append(&g
.s
, g
.h
/2, g
.w
/2-i
);
315 #define free_ll(head) do{ \
327 free_ll(g
.n
); //TODO: doesn't get free'd correctly
336 int getctrlseq (void) {
339 int offset
= 0x20; /* never sends control chars as data */
340 while ((c
= getchar()) != EOF
) {
344 case '\033': state
=ESC_SENT
; break;
350 case '[': state
=CSI_SENT
; break;
351 default: return CTRSEQ_INVALID
;
356 case 'A': return CTRSEQ_CURSOR_UP
;
357 case 'B': return CTRSEQ_CURSOR_DOWN
;
358 case 'C': return CTRSEQ_CURSOR_RIGHT
;
359 case 'D': return CTRSEQ_CURSOR_LEFT
;
360 default: return CTRSEQ_INVALID
;
364 return CTRSEQ_INVALID
;
370 void append_movement (int dir
) {
371 struct directions
* n
;
372 for (n
= g
.n
; n
&& n
->next
; n
= n
->next
); /* advance to the end */
373 if (n
&& n
->d
== dir
) return; /* don't add the same direction twice */
375 struct directions
* new_event
= malloc (sizeof(struct directions
));
377 new_event
->next
= NULL
;
385 int get_movement (void) {
386 if (g
.n
== NULL
) return -1;
389 struct directions
* delet_this
= g
.n
;
395 void move_ph (int line
, int col
) {
396 /* move printhead to zero-indexed position */
397 printf ("\033[%d;%dH", line
+1, col
+1);
400 void clamp_fieldsize (void) { //TODO: use
401 /* clamp field size to terminal size and mouse maximum: */
403 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
405 if (g
.w
< 1) g
.w
= 1;
406 if (g
.h
< 1) g
.h
= 1;
408 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
409 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
; //TODO: does not work in `-d' (in xterm)
410 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
411 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
414 void timer_setup (int enable
) {
415 static struct itimerval tbuf
;
416 tbuf
.it_interval
.tv_sec
= 0;//TODO: make it speed up automatically
417 tbuf
.it_interval
.tv_usec
= (1000000/g
.v
)-1; /*WARN: 1 <= g.v <= 999999*/
420 g
.t
= time(NULL
);//TODO: interferes with 'pause'
421 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
422 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
424 tbuf
.it_value
.tv_sec
= 0;
425 tbuf
.it_value
.tv_usec
= 0;
428 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
435 void signal_setup (void) {
436 struct sigaction saction
;
438 saction
.sa_handler
= signal_handler
;
439 sigemptyset(&saction
.sa_mask
);
440 saction
.sa_flags
= 0;
441 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
446 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
452 void signal_handler (int signum
) {
456 //dtime = difftime (time(NULL), g.t);
457 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
458 //printf ("[%03d]", g.t?dtime:0);
466 void screen_setup (int enable
) {
469 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
470 printf ("\033[H\033[J"); /* reset cursor, clear screen */
471 printf ("\033[?25l"); /* hide cursor */
472 print (op
.scheme
->init_seq
); /* swich charset, if necessary */
474 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
475 printf ("\033[?25h"); /* show cursor */
476 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
481 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
482 void raw_mode(int enable
) {
483 static struct termios saved_term_mode
;
484 struct termios raw_term_mode
;
487 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
488 raw_term_mode
= saved_term_mode
;
489 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
490 raw_term_mode
.c_cc
[VMIN
] = 1 ;
491 raw_term_mode
.c_cc
[VTIME
] = 0;
492 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
494 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);