]>
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, sigaction in c99*/
24 #include <sys/ioctl.h>
33 #define MIN(a,b) (a>b?b:a)
34 #define MAX(a,b) (a>b?a:b)
35 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
36 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
37 #define print(str) fputs (str?str:"", stdout)
43 #define CW 2 /* cell width */
46 int w
; /* field width */
47 int h
; /* field height */
48 struct snake
* s
; /* snek */ //TODO: rename to S
49 struct item
* i
; /* items (food, boni) */
50 int d
; /* direction the snake is looking */
51 int n
;/* direction to move on next tick */
52 int t
; /* time of game start */
53 int p
; /* score */ //TODO: rename?
54 float f
; /* speed in moves per second */ //TODO: rename
58 struct scheme
* scheme
;
59 int l
; /* initial snake length */
60 int s
; /* initial snake speed */
63 int main (int argc
, char** argv
) {
65 g
.w
= 30; //two-char-width
69 op
.scheme
= &unic0de
; //TODO: expose to getopt() once more sets are available
72 opterr
= 0; /* don't print message on unrecognized option */
73 while ((optget
= getopt (argc
, argv
, "+h")) != -1) {
77 fprintf (stderr
, SHORTHELP LONGHELP
, argv
[0]);
78 return !(optget
=='h');
80 } if (optind
< argc
) { /* parse Fieldspec */
88 //TODO: call viiper() in a game loop
102 spawn_item(FOOD
, rand() % NUM_FOODS
); //TODO: shape distribution, so bigger values get selected less
105 switch (getctrlseq()) { //TODO: fast 180° turns don't get executed -- buffer at least two keystrokes (or ctrlseqs)
106 case '+': g
.f
++;timer_setup(1);break;
107 case '#': if (g
.f
> 1) g
.f
--;timer_setup(1); break;
108 case 'h': if (g
.d
!= EAST
) g
.n
= WEST
; break;
109 case 'j': if (g
.d
!= NORTH
) g
.n
= SOUTH
; break;
110 case 'k': if (g
.d
!= SOUTH
) g
.n
= NORTH
; break;
111 case 'l': if (g
.d
!= WEST
) g
.n
= EAST
; break;
112 case 'p': /*TODO: pause*/ break;
113 case 'r': /*TODO:restart*/ return 0;
121 print ("\033[H\033[J");
122 show_playfield ();//TODO: only redraw diff
127 void snake_advance (void) {
129 int new_row
= g
.s
->r
+(g
.d
==SOUTH
) -(g
.d
==NORTH
);
130 int new_col
= g
.s
->c
+(g
.d
==EAST
) -(g
.d
==WEST
);
132 /* detect food hit and spawn a new food */
133 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
134 if (i
->r
== new_row
&& i
->c
== new_col
) {
136 spawn_item(FOOD
, rand() % NUM_FOODS
);
140 if (new_row
>= g
.h
|| new_col
>= g
.w
|| new_row
< 0 || new_col
< 0) //TODO: we die 1 cell too early when hitting the right wall
141 exit(1); //TODO: longjump?
143 struct snake
* new_head
;
144 struct snake
* new_tail
; /* former second-to-last element */
145 for (new_tail
= g
.s
; new_tail
->next
->next
; new_tail
= new_tail
->next
)
146 /* use the opportunity of looping to check if we eat ourselves*/
147 if(new_tail
->next
->r
== new_row
&& new_tail
->next
->c
== new_col
)
149 new_head
= new_tail
->next
; /* reuse element instead of malloc() */
150 new_tail
->next
= NULL
;
152 new_head
->r
= new_row
;
153 new_head
->c
= new_col
;
154 new_head
->next
= g
.s
;
159 void spawn_item (int type
, int value
) {
164 /* loop through snake to check if we aren't on it */
165 //TODO: inefficient as snake gets longer; near impossible in the end
166 for (struct snake
* s
= g
.s
; s
; s
= s
->next
)
167 if (s
->r
== row
&& s
->c
== col
) goto try_again
;
169 //3. get item from category TODO
170 struct item
* new_item
= malloc (sizeof(struct item
));
175 new_item
->s
= time(0);
176 if (g
.i
) g
.i
->prev
= new_item
;
177 new_item
->next
= g
.i
;
182 void consume_item (struct item
* i
) {
183 struct item
* predecessor
= i
->prev
;
184 struct item
* successor
= i
->next
;
189 case FOOD_5
: g
.p
+= 5; break;
190 case FOOD_10
: g
.p
+= 10; break;
191 case FOOD_20
: g
.p
+= 20; break;
193 snake_append(g
.s
, 0,0); /* position doesn't matter, as item */
194 break; /* will be reused as the head before it is drawn */
200 if (predecessor
== NULL
) {
202 if (successor
) successor
->prev
= NULL
;
204 predecessor
->next
= successor
;
205 successor
->prev
= predecessor
;
211 void show_playfield (void) {
212 //int score_width = g.p > 9999?6:4;
213 int score_width
= 4; // v- = the -| spacer |-
214 float half_width
= (g
.w
- score_width
/CW
- 2)/2.0; //TODO: this whole endavour is ugly
217 printm (half_width
, BORDER(T
,C
)); //TODO: i bet this breaks in dec mode
218 printf ("%s %0*d %s", BORDER(S
,L
), score_width
, g
.p
, BORDER(S
,R
));
219 printm ((int)(half_width
+.5), BORDER(T
,C
));
220 printf ("%s\n", BORDER(T
,R
));
223 for (int row
= 0; row
< g
.h
; row
++)
224 printf ("%s%*s%s\n", BORDER(C
,L
), CW
*g
.w
, "", BORDER(C
,R
));
228 printm (g
.w
, BORDER(B
,C
));
232 struct snake
* last
= NULL
;
233 int color
= -1; //TODO: that's a hack
234 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
235 move_ph (s
->r
+COL_OFFSET
, s
->c
*CW
+LINE_OFFSET
);
237 int predecessor
= (last
==NULL
)?NONE
:
238 (last
->r
< s
->r
) ? NORTH
:
239 (last
->r
> s
->r
) ? SOUTH
:
240 (last
->c
> s
->c
) ? EAST
:
241 (last
->c
< s
->c
) ? WEST
:NONE
;
242 int successor
= (s
->next
== NULL
)?NONE
:
243 (s
->next
->r
< s
->r
) ? NORTH
:
244 (s
->next
->r
> s
->r
) ? SOUTH
:
245 (s
->next
->c
> s
->c
) ? EAST
:
246 (s
->next
->c
< s
->c
) ? WEST
:NONE
;
248 printf ("\033[%sm", color
==-1?"91":color
?"92":"32");
249 print (op
.scheme
->snake
[predecessor
][successor
]);
252 color
= (color
+1) % 2;
255 /* print item queue */
256 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
257 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
258 if (i
->t
== FOOD
) print (op
.scheme
->item
[i
->v
]);
259 else if (i
->t
==BONUS
) /* TODO: print bonus */;
262 //TODO: temporarily print speed
263 move_ph (g
.h
+LINE_OFFSET
, g
.w
);
267 void snake_append (struct snake
* s
, int row
, int col
) {
268 struct snake
* new = malloc (sizeof(struct snake
));
274 while (p
->next
) p
= p
->next
;
279 g
.s
= malloc (sizeof(struct snake
));
284 for (int i
= 1; i
< op
.l
; i
++)
285 snake_append(g
.s
, g
.h
/2, g
.w
/2-i
);
290 for (struct snake
* s
= g
.s
; s
;) {
291 struct snake
* tmp
= s
->next
;
295 for (struct item
* i
= g
.i
; i
;) {
296 struct item
* tmp
= i
->next
;
308 int getctrlseq (void) {
311 int offset
= 0x20; /* never sends control chars as data */
312 while ((c
= getchar()) != EOF
) {
316 case '\033': state
=ESC_SENT
; break;
322 case '[': state
=CSI_SENT
; break;
323 default: return CTRSEQ_INVALID
;
328 case 'A': return CTRSEQ_CURSOR_UP
;
329 case 'B': return CTRSEQ_CURSOR_DOWN
;
330 case 'C': return CTRSEQ_CURSOR_RIGHT
;
331 case 'D': return CTRSEQ_CURSOR_LEFT
;
332 default: return CTRSEQ_INVALID
;
336 return CTRSEQ_INVALID
;
342 void move_ph (int line
, int col
) {
343 /* move printhead to zero-indexed position */
344 printf ("\033[%d;%dH", line
+1, col
+1);
347 void clamp_fieldsize (void) {
348 /* clamp field size to terminal size and mouse maximum: */
350 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
352 if (g
.w
< 1) g
.w
= 1;
353 if (g
.h
< 1) g
.h
= 1;
355 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
356 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
; //TODO: does not work in `-d' (in xterm)
357 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
358 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
361 void timer_setup (int enable
) {
362 static struct itimerval tbuf
;
363 tbuf
.it_interval
.tv_sec
= 0;//TODO: make configurable, TODO: speed up
364 tbuf
.it_interval
.tv_usec
= (1000000/g
.f
)-1; /*WARN: 1 <= g.f <= 999999*/
368 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
369 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
370 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
375 tbuf
.it_value
.tv_sec
= 0;
376 tbuf
.it_value
.tv_usec
= 0;
377 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
385 void signal_setup (void) {
386 struct sigaction saction
;
388 saction
.sa_handler
= signal_handler
;
389 sigemptyset(&saction
.sa_mask
);
390 saction
.sa_flags
= 0;
391 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
396 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
402 void signal_handler (int signum
) {
406 //dtime = difftime (time(NULL), g.t);
407 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
408 //printf ("[%03d]", g.t?dtime:0);
416 void screen_setup (int enable
) {
419 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
420 printf ("\033[H\033[J"); /* reset cursor, clear screen */
421 printf ("\033[?25l"); /* hide cursor */
422 //print (op.scheme->init_seq); /* swich charset, if necessary */
424 //print (op.scheme->reset_seq); /* reset charset, if necessary */
425 printf ("\033[?25h"); /* show cursor */
426 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
431 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
432 void raw_mode(int enable
) {
433 static struct termios saved_term_mode
;
434 struct termios raw_term_mode
;
437 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
438 raw_term_mode
= saved_term_mode
;
439 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
440 raw_term_mode
.c_cc
[VMIN
] = 1 ;
441 raw_term_mode
.c_cc
[VTIME
] = 0;
442 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
444 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);