]>
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 int d
; /* direction the snake is looking */
49 int t
; /* time of game start */
50 int p
; /* score */ //TODO: rename?
51 float f
; /* speed in moves per second */ //TODO: rename :XXX
52 struct snake
* s
; /* snek */ //TODO: rename to S
53 struct item
* i
; /* items (food, boni) */
54 struct directions
* n
;/* next direction events to process */
58 int l
; /* initial snake length */
59 int s
; /* initial snake speed */
60 struct scheme
* scheme
;
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; //TODO: temporary, to set speed
107 case '#': if (g
.f
> 1) g
.f
--;timer_setup(1); break; //TODO: temporary, to set speed
109 case CTRSEQ_CURSOR_LEFT
:
110 case 'h': append_movement(WEST
); break;
111 case CTRSEQ_CURSOR_DOWN
:
112 case 'j': append_movement(SOUTH
); break;
113 case CTRSEQ_CURSOR_UP
:
114 case 'k': append_movement(NORTH
); break;
115 case CTRSEQ_CURSOR_RIGHT
:
116 case 'l': append_movement(EAST
); break;
117 case 'p': /*TODO: pause*/ break;
118 case 'r': /*TODO:restart*/ return 0;
126 print ("\033[H\033[J");
127 show_playfield ();//TODO: only redraw diff
132 void snake_advance (void) {
133 if (g
.n
) {/* new direction in the buffer */
134 int possible_new_dir
= get_movement();
135 if (g
.d
== EAST
&& possible_new_dir
== WEST
) goto ignore_new
;
136 if (g
.d
== WEST
&& possible_new_dir
== EAST
) goto ignore_new
;
137 if (g
.d
== NORTH
&& possible_new_dir
== SOUTH
) goto ignore_new
;
138 if (g
.d
== SOUTH
&& possible_new_dir
== NORTH
) goto ignore_new
;
139 g
.d
= possible_new_dir
; /* pop off a new direction if available*/
144 int new_row
= g
.s
->r
+(g
.d
==SOUTH
) -(g
.d
==NORTH
);
145 int new_col
= g
.s
->c
+(g
.d
==EAST
) -(g
.d
==WEST
);
147 /* detect food hit and spawn a new food */
148 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
149 if (i
->r
== new_row
&& i
->c
== new_col
) {
151 spawn_item(FOOD
, rand() % NUM_FOODS
);
155 /*NOTE:no idea why I have to use g.w+1, but otherwise we die too early*/
156 if (new_row
>= g
.h
|| new_col
>= g
.w
+1 || new_row
< 0 || new_col
< 0)
157 exit(1); //TODO: longjump?
159 struct snake
* new_head
;
160 struct snake
* new_tail
; /* former second-to-last element */
161 for (new_tail
= g
.s
; new_tail
->next
->next
; new_tail
= new_tail
->next
)
162 /* use the opportunity of looping to check if we eat ourselves*/
163 if(new_tail
->next
->r
== new_row
&& new_tail
->next
->c
== new_col
)
165 new_head
= new_tail
->next
; /* reuse element instead of malloc() */
166 new_tail
->next
= NULL
;
168 new_head
->r
= new_row
;
169 new_head
->c
= new_col
;
170 new_head
->next
= g
.s
;
175 void spawn_item (int type
, int value
) {
180 /* loop through snake to check if we aren't on it */
181 //TODO: inefficient as snake gets longer; near impossible in the end
182 for (struct snake
* s
= g
.s
; s
; s
= s
->next
)
183 if (s
->r
== row
&& s
->c
== col
) goto try_again
;
185 //3. get item from category TODO
186 struct item
* new_item
= malloc (sizeof(struct item
));
191 new_item
->s
= time(0);
192 if (g
.i
) g
.i
->prev
= new_item
;
193 new_item
->next
= g
.i
;
198 void consume_item (struct item
* i
) {
199 struct item
* predecessor
= i
->prev
;
200 struct item
* successor
= i
->next
;
205 case FOOD_5
: g
.p
+= 5; break;
206 case FOOD_10
: g
.p
+= 10; break;
207 case FOOD_20
: g
.p
+= 20; break;
209 snake_append(g
.s
, 0,0); /* position doesn't matter, as item */
210 break; /* will be reused as the head before it is drawn */
216 if (predecessor
== NULL
) {
218 if (successor
) successor
->prev
= NULL
;
220 predecessor
->next
= successor
;
221 successor
->prev
= predecessor
;
227 void show_playfield (void) {
228 //int score_width = g.p > 9999?6:4;
229 int score_width
= 4; // v- = the -| spacer |-
230 float half_width
= (g
.w
- score_width
/CW
- 2)/2.0; //TODO: this whole endavour is ugly
233 printm (half_width
, BORDER(T
,C
)); //TODO: i bet this breaks in dec mode
234 printf ("%s %0*d %s", BORDER(S
,L
), score_width
, g
.p
, BORDER(S
,R
));
235 printm ((int)(half_width
+.5), BORDER(T
,C
));
236 printf ("%s\n", BORDER(T
,R
));
239 for (int row
= 0; row
< g
.h
; row
++)
240 printf ("%s%*s%s\n", BORDER(C
,L
), CW
*g
.w
, "", BORDER(C
,R
));
244 printm (g
.w
, BORDER(B
,C
));
248 struct snake
* last
= NULL
;
249 int color
= -1; //TODO: that's a hack
250 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
251 move_ph (s
->r
+COL_OFFSET
, s
->c
*CW
+LINE_OFFSET
);
253 int predecessor
= (last
==NULL
)?NONE
:
254 (last
->r
< s
->r
) ? NORTH
:
255 (last
->r
> s
->r
) ? SOUTH
:
256 (last
->c
> s
->c
) ? EAST
:
257 (last
->c
< s
->c
) ? WEST
:NONE
;
258 int successor
= (s
->next
== NULL
)?NONE
:
259 (s
->next
->r
< s
->r
) ? NORTH
:
260 (s
->next
->r
> s
->r
) ? SOUTH
:
261 (s
->next
->c
> s
->c
) ? EAST
:
262 (s
->next
->c
< s
->c
) ? WEST
:NONE
;
264 printf ("\033[%sm", color
==-1?"91":color
?"92":"32");
265 print (op
.scheme
->snake
[predecessor
][successor
]);
268 color
= (color
+1) % 2;
271 /* print item queue */
272 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
273 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
274 if (i
->t
== FOOD
) print (op
.scheme
->item
[i
->v
]);
275 else if (i
->t
==BONUS
) /* TODO: print bonus */;
278 //TODO: temporarily print speed
279 move_ph (g
.h
+LINE_OFFSET
, g
.w
);
283 void snake_append (struct snake
* s
, int row
, int col
) {
284 struct snake
* new = malloc (sizeof(struct snake
));
290 while (p
->next
) p
= p
->next
;
295 g
.s
= malloc (sizeof(struct snake
));
300 for (int i
= 1; i
< op
.l
; i
++)
301 snake_append(g
.s
, g
.h
/2, g
.w
/2-i
);
306 for (struct snake
* s
= g
.s
; s
;) {
307 struct snake
* tmp
= s
->next
;
311 for (struct item
* i
= g
.i
; i
;) {
312 struct item
* tmp
= i
->next
;
324 int getctrlseq (void) {
327 int offset
= 0x20; /* never sends control chars as data */
328 while ((c
= getchar()) != EOF
) {
332 case '\033': state
=ESC_SENT
; break;
338 case '[': state
=CSI_SENT
; break;
339 default: return CTRSEQ_INVALID
;
344 case 'A': return CTRSEQ_CURSOR_UP
;
345 case 'B': return CTRSEQ_CURSOR_DOWN
;
346 case 'C': return CTRSEQ_CURSOR_RIGHT
;
347 case 'D': return CTRSEQ_CURSOR_LEFT
;
348 default: return CTRSEQ_INVALID
;
352 return CTRSEQ_INVALID
;
358 void append_movement (int dir
) {
359 struct directions
* new_event
= malloc (sizeof(struct directions
));
361 new_event
->next
= NULL
;
365 struct directions
* n
;
366 for (n
= g
.n
; n
->next
; n
= n
->next
);
371 int get_movement (void) {
372 if (g
.n
== NULL
) return -1;
375 struct directions
* delet_this
= g
.n
;
381 void move_ph (int line
, int col
) {
382 /* move printhead to zero-indexed position */
383 printf ("\033[%d;%dH", line
+1, col
+1);
386 void clamp_fieldsize (void) { //TODO: use
387 /* clamp field size to terminal size and mouse maximum: */
389 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
391 if (g
.w
< 1) g
.w
= 1;
392 if (g
.h
< 1) g
.h
= 1;
394 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
395 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
; //TODO: does not work in `-d' (in xterm)
396 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
397 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
400 void timer_setup (int enable
) {
401 static struct itimerval tbuf
;
402 tbuf
.it_interval
.tv_sec
= 0;//TODO: make configurable, TODO: speed up
403 tbuf
.it_interval
.tv_usec
= (1000000/g
.f
)-1; /*WARN: 1 <= g.f <= 999999*/
406 g
.t
= time(NULL
);//TODO: interferes with 'pause'
407 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
408 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
410 tbuf
.it_value
.tv_sec
= 0;
411 tbuf
.it_value
.tv_usec
= 0;
414 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
421 void signal_setup (void) {
422 struct sigaction saction
;
424 saction
.sa_handler
= signal_handler
;
425 sigemptyset(&saction
.sa_mask
);
426 saction
.sa_flags
= 0;
427 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
432 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
438 void signal_handler (int signum
) {
442 //dtime = difftime (time(NULL), g.t);
443 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
444 //printf ("[%03d]", g.t?dtime:0);
452 void screen_setup (int enable
) {
455 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
456 printf ("\033[H\033[J"); /* reset cursor, clear screen */
457 printf ("\033[?25l"); /* hide cursor */
458 print (op
.scheme
->init_seq
); /* swich charset, if necessary */
460 print (op
.scheme
->reset_seq
); /* reset charset, if necessary */
461 printf ("\033[?25h"); /* show cursor */
462 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
467 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
468 void raw_mode(int enable
) {
469 static struct termios saved_term_mode
;
470 struct termios raw_term_mode
;
473 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
474 raw_term_mode
= saved_term_mode
;
475 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
476 raw_term_mode
.c_cc
[VMIN
] = 1 ;
477 raw_term_mode
.c_cc
[VTIME
] = 0;
478 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
480 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);