]>
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 */
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 */
57 struct scheme
* scheme
;
58 int l
; /* initial snake length */
59 int s
; //TODO: initial snake speed
62 int main (int argc
, char** argv
) {
64 g
.w
= 30; //two-char-width
67 op
.scheme
= &unic0de
; //TODO: expose to getopt()
70 opterr
= 0; /* don't print message on unrecognized option */
71 while ((optget
= getopt (argc
, argv
, "+h")) != -1) {
75 fprintf (stderr
, SHORTHELP LONGHELP
, argv
[0]);
76 return !(optget
=='h');
78 } if (optind
< argc
) { /* parse Fieldspec */
86 //TODO: call viiper() in a game loop
99 spawn_item(FOOD
, rand() % NUM_FOODS
); //TODO: shape distribution, so bigger values get selected less
102 switch (getctrlseq()) {
103 case 'h': if (g
.d
!= EAST
) g
.n
= WEST
; break;
104 case 'j': if (g
.d
!= NORTH
) g
.n
= SOUTH
; break;
105 case 'k': if (g
.d
!= SOUTH
) g
.n
= NORTH
; break;
106 case 'l': if (g
.d
!= WEST
) g
.n
= EAST
; break;
107 case 'p': /*TODO: pause*/ break;
108 case 'r': /*TODO:restart*/ return 0;
116 print ("\033[H\033[J");
117 show_playfield ();//TODO: only redraw diff
122 void snake_advance (void) {
124 int new_row
= g
.s
->r
+(g
.d
==SOUTH
) -(g
.d
==NORTH
);
125 int new_col
= g
.s
->c
+(g
.d
==EAST
) -(g
.d
==WEST
);
127 /* detect food hit and spawn a new food */
128 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
129 if (i
->r
== new_row
&& i
->c
== new_col
) {
131 spawn_item(FOOD
, rand() % NUM_FOODS
);
135 if (new_row
>= g
.h
|| new_col
>= g
.w
|| new_row
< 0 || new_col
< 0)
136 exit(1); //TODO: longjump?
138 struct snake
* new_head
;
139 struct snake
* new_tail
; /* former second-to-last element */
140 for (new_tail
= g
.s
; new_tail
->next
->next
; new_tail
= new_tail
->next
)
141 /* use the opportunity of looping to check if we eat ourselves*/
142 if(new_tail
->next
->r
== new_row
&& new_tail
->next
->c
== new_col
)
144 new_head
= new_tail
->next
; /* reuse element instead of malloc() */
145 new_tail
->next
= NULL
;
147 new_head
->r
= new_row
;
148 new_head
->c
= new_col
;
149 new_head
->next
= g
.s
;
154 void spawn_item (int type
, int value
) {
159 /* loop through snake to check if we aren't on it */
160 //TODO: inefficient as snake gets longer; near impossible in the end
161 for (struct snake
* s
= g
.s
; s
; s
= s
->next
)
162 if (s
->r
== row
&& s
->c
== col
) goto try_again
;
164 //3. get item from category TODO
165 struct item
* new_item
= malloc (sizeof(struct item
));
170 new_item
->s
= time(0);
171 if (g
.i
) g
.i
->prev
= new_item
;
172 new_item
->next
= g
.i
;
177 void consume_item (struct item
* i
) {
178 struct item
* predecessor
= i
->prev
;
179 struct item
* successor
= i
->next
;
184 case FOOD_5
: g
.p
+= 5; break;
185 case FOOD_10
: g
.p
+= 10; break;
186 case FOOD_20
: g
.p
+= 20; break;
188 snake_append(g
.s
, 0,0); /* position doesn't matter, as item */
189 break; /* will be reused as the head before it is drawn */
195 if (predecessor
== NULL
) {
197 if (successor
) successor
->prev
= NULL
;
199 predecessor
->next
= successor
;
200 successor
->prev
= predecessor
;
206 void show_playfield (void) {
209 printm (g
.w
/2-4/2, BORDER(T
,C
)); //TODO: i bet this breaks in dec mode
210 printf ("%s %04d %s", BORDER(S
,L
), g
.p
, BORDER(S
,R
));
211 printm(g
.w
/2-4/2, BORDER(T
,C
));
212 printf ("%s\n", BORDER(T
,R
));
215 for (int row
= 0; row
< g
.h
; row
++)
216 printf ("%s%*s%s\n", BORDER(C
,L
), CW
*g
.w
, "", BORDER(C
,R
));
220 printm (g
.w
, BORDER(B
,C
));
224 struct snake
* last
= NULL
;
225 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
226 move_ph (s
->r
+COL_OFFSET
, s
->c
*CW
+LINE_OFFSET
);
228 int predecessor
= (last
==NULL
)?NONE
:
229 (last
->r
< s
->r
) ? NORTH
:
230 (last
->r
> s
->r
) ? SOUTH
:
231 (last
->c
> s
->c
) ? EAST
:
232 (last
->c
< s
->c
) ? WEST
:NONE
;
233 int successor
= (s
->next
== NULL
)?NONE
:
234 (s
->next
->r
< s
->r
) ? NORTH
:
235 (s
->next
->r
> s
->r
) ? SOUTH
:
236 (s
->next
->c
> s
->c
) ? EAST
:
237 (s
->next
->c
< s
->c
) ? WEST
:NONE
;
239 print (op
.scheme
->snake
[predecessor
][successor
]);
243 /* print item queue */
244 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
245 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
246 if (i
->t
== FOOD
) print (op
.scheme
->item
[i
->v
]);
247 else if (i
->t
==BONUS
) /* TODO: print bonus */;
251 void snake_append (struct snake
* s
, int row
, int col
) {
252 struct snake
* new = malloc (sizeof(struct snake
));
258 while (p
->next
) p
= p
->next
;
263 g
.s
= malloc (sizeof(struct snake
));
268 for (int i
= 1; i
< op
.l
; i
++)
269 snake_append(g
.s
, g
.h
/2, g
.w
/2-i
);
274 for (struct snake
* s
= g
.s
; s
;) {
275 struct snake
* tmp
= s
->next
;
279 for (struct item
* i
= g
.i
; i
;) {
280 struct item
* tmp
= i
->next
;
292 int getctrlseq (void) {
295 int offset
= 0x20; /* never sends control chars as data */
296 while ((c
= getchar()) != EOF
) {
300 case '\033': state
=ESC_SENT
; break;
306 case '[': state
=CSI_SENT
; break;
307 default: return CTRSEQ_INVALID
;
312 case 'A': return CTRSEQ_CURSOR_UP
;
313 case 'B': return CTRSEQ_CURSOR_DOWN
;
314 case 'C': return CTRSEQ_CURSOR_RIGHT
;
315 case 'D': return CTRSEQ_CURSOR_LEFT
;
316 default: return CTRSEQ_INVALID
;
320 return CTRSEQ_INVALID
;
326 void move_ph (int line
, int col
) {
327 /* move printhead to zero-indexed position */
328 printf ("\033[%d;%dH", line
+1, col
+1);
331 void clamp_fieldsize (void) {
332 /* clamp field size to terminal size and mouse maximum: */
334 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
336 if (g
.w
< 1) g
.w
= 1;
337 if (g
.h
< 1) g
.h
= 1;
339 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
340 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
; //TODO: does not work in `-d' (in xterm)
341 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
342 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
345 void timer_setup (int enable
) {
346 static struct itimerval tbuf
;
347 tbuf
.it_interval
.tv_sec
= 0;//TODO: make configurable, TODO: speed up
348 tbuf
.it_interval
.tv_usec
= 300000;
352 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
353 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
354 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
359 tbuf
.it_value
.tv_sec
= 0;
360 tbuf
.it_value
.tv_usec
= 0;
361 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
369 void signal_setup (void) {
370 struct sigaction saction
;
372 saction
.sa_handler
= signal_handler
;
373 sigemptyset(&saction
.sa_mask
);
374 saction
.sa_flags
= 0;
375 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
380 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
386 void signal_handler (int signum
) {
390 //dtime = difftime (time(NULL), g.t);
391 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
392 //printf ("[%03d]", g.t?dtime:0);
400 void screen_setup (int enable
) {
403 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
404 printf ("\033[H\033[J"); /* reset cursor, clear screen */
405 printf ("\033[?25l"); /* hide cursor */
406 //print (op.scheme->init_seq); /* swich charset, if necessary */
408 //print (op.scheme->reset_seq); /* reset charset, if necessary */
409 printf ("\033[?25h"); /* show cursor */
410 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
415 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
416 void raw_mode(int enable
) {
417 static struct termios saved_term_mode
;
418 struct termios raw_term_mode
;
421 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
422 raw_term_mode
= saved_term_mode
;
423 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
424 raw_term_mode
.c_cc
[VMIN
] = 1 ;
425 raw_term_mode
.c_cc
[VTIME
] = 0;
426 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
428 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);