]>
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()) {
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)
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 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
234 move_ph (s
->r
+COL_OFFSET
, s
->c
*CW
+LINE_OFFSET
);
236 int predecessor
= (last
==NULL
)?NONE
:
237 (last
->r
< s
->r
) ? NORTH
:
238 (last
->r
> s
->r
) ? SOUTH
:
239 (last
->c
> s
->c
) ? EAST
:
240 (last
->c
< s
->c
) ? WEST
:NONE
;
241 int successor
= (s
->next
== NULL
)?NONE
:
242 (s
->next
->r
< s
->r
) ? NORTH
:
243 (s
->next
->r
> s
->r
) ? SOUTH
:
244 (s
->next
->c
> s
->c
) ? EAST
:
245 (s
->next
->c
< s
->c
) ? WEST
:NONE
;
247 print (op
.scheme
->snake
[predecessor
][successor
]);
251 /* print item queue */
252 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
253 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
254 if (i
->t
== FOOD
) print (op
.scheme
->item
[i
->v
]);
255 else if (i
->t
==BONUS
) /* TODO: print bonus */;
258 //TODO: temporarily print speed
259 move_ph (g
.h
+LINE_OFFSET
, g
.w
);
263 void snake_append (struct snake
* s
, int row
, int col
) {
264 struct snake
* new = malloc (sizeof(struct snake
));
270 while (p
->next
) p
= p
->next
;
275 g
.s
= malloc (sizeof(struct snake
));
280 for (int i
= 1; i
< op
.l
; i
++)
281 snake_append(g
.s
, g
.h
/2, g
.w
/2-i
);
286 for (struct snake
* s
= g
.s
; s
;) {
287 struct snake
* tmp
= s
->next
;
291 for (struct item
* i
= g
.i
; i
;) {
292 struct item
* tmp
= i
->next
;
304 int getctrlseq (void) {
307 int offset
= 0x20; /* never sends control chars as data */
308 while ((c
= getchar()) != EOF
) {
312 case '\033': state
=ESC_SENT
; break;
318 case '[': state
=CSI_SENT
; break;
319 default: return CTRSEQ_INVALID
;
324 case 'A': return CTRSEQ_CURSOR_UP
;
325 case 'B': return CTRSEQ_CURSOR_DOWN
;
326 case 'C': return CTRSEQ_CURSOR_RIGHT
;
327 case 'D': return CTRSEQ_CURSOR_LEFT
;
328 default: return CTRSEQ_INVALID
;
332 return CTRSEQ_INVALID
;
338 void move_ph (int line
, int col
) {
339 /* move printhead to zero-indexed position */
340 printf ("\033[%d;%dH", line
+1, col
+1);
343 void clamp_fieldsize (void) {
344 /* clamp field size to terminal size and mouse maximum: */
346 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
348 if (g
.w
< 1) g
.w
= 1;
349 if (g
.h
< 1) g
.h
= 1;
351 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
352 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
; //TODO: does not work in `-d' (in xterm)
353 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
354 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
357 void timer_setup (int enable
) {
358 static struct itimerval tbuf
;
359 tbuf
.it_interval
.tv_sec
= 0;//TODO: make configurable, TODO: speed up
360 tbuf
.it_interval
.tv_usec
= (1000000/g
.f
)-1; /*WARN: 1 <= g.f <= 999999*/
364 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
365 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
366 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
371 tbuf
.it_value
.tv_sec
= 0;
372 tbuf
.it_value
.tv_usec
= 0;
373 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
381 void signal_setup (void) {
382 struct sigaction saction
;
384 saction
.sa_handler
= signal_handler
;
385 sigemptyset(&saction
.sa_mask
);
386 saction
.sa_flags
= 0;
387 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
392 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
398 void signal_handler (int signum
) {
402 //dtime = difftime (time(NULL), g.t);
403 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
404 //printf ("[%03d]", g.t?dtime:0);
412 void screen_setup (int enable
) {
415 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
416 printf ("\033[H\033[J"); /* reset cursor, clear screen */
417 printf ("\033[?25l"); /* hide cursor */
418 //print (op.scheme->init_seq); /* swich charset, if necessary */
420 //print (op.scheme->reset_seq); /* reset charset, if necessary */
421 printf ("\033[?25h"); /* show cursor */
422 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
427 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
428 void raw_mode(int enable
) {
429 static struct termios saved_term_mode
;
430 struct termios raw_term_mode
;
433 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
434 raw_term_mode
= saved_term_mode
;
435 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
436 raw_term_mode
.c_cc
[VMIN
] = 1 ;
437 raw_term_mode
.c_cc
[VTIME
] = 0;
438 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
440 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);