]>
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>
32 #define MIN(a,b) (a>b?b:a)
33 #define MAX(a,b) (a>b?a:b)
34 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
35 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
36 #define print(str) fputs (str?str:"", stdout)
42 #define CW 2 /* cell width */
43 #define SHORTHELP "%s [OPTIONS] [FIELDSIZE]\n"
48 " WxH (width 'x' height)\n" \
49 " defaults to 30x20\n" \
52 " hjkl: move left/down/up/right\n" \
53 " p: pause / unpause\n" \
54 " r: start a new game\n" \
60 struct snake
* next
; /* points to tail */
62 struct item
{ //TODO: make it more abstract (type should encode glyph, points, etc)
66 int s
; /* spawn time (for bonus) */
84 int w
; /* field width */
85 int h
; /* field height */
86 struct snake
* s
; /* snek */
87 struct item
* i
; /* items (food, boni) */
88 int d
; /* direction the snake is looking */
89 int n
;/* direction to move on next tick */
90 int t
; /* time of game start */
95 struct scheme
* scheme
;
96 int l
; /* initial snake length */
97 int s
; //TODO: initial snake speed
101 void snake_advance (void);
102 void spawn_item (int type
);
103 void consume_item (struct item
* i
);
104 void show_playfield (void);
105 void snake_append (struct snake
* s
, int row
, int col
);
108 int getctrlseq (void);
109 void move_ph (int line
, int col
);
110 void clamp_fieldsize (void);
111 void timer_setup (int enable
);
112 void signal_setup (void);
113 void signal_handler (int signum
);
114 void screen_setup (int enable
);
115 void raw_mode(int enable
);
117 /* for getctrlseq() */
122 CTRSEQ_CURSOR_LEFT
= -7,
123 CTRSEQ_CURSOR_DOWN
= -8,
124 CTRSEQ_CURSOR_UP
= -9,
125 CTRSEQ_CURSOR_RIGHT
= -10,
128 int main (int argc
, char** argv
) {
130 g
.w
= 30; //two-char-width
133 op
.scheme
= &unic0de
; //TODO: expose to getopt()
136 opterr
= 0; /* don't print message on unrecognized option */
137 while ((optget
= getopt (argc
, argv
, "+h")) != -1) {
141 fprintf (stderr
, SHORTHELP LONGHELP
, argv
[0]);
142 return !(optget
=='h');
144 } if (optind
< argc
) { /* parse Fieldspec */
152 //TODO: call viiper() in a game loop
168 switch (getctrlseq()) {
169 case 'h': if (g
.d
!= EAST
) g
.n
= WEST
; break;
170 case 'j': if (g
.d
!= NORTH
) g
.n
= SOUTH
; break;
171 case 'k': if (g
.d
!= SOUTH
) g
.n
= NORTH
; break;
172 case 'l': if (g
.d
!= WEST
) g
.n
= EAST
; break;
173 case 'r': /*TODO:restart*/ return 0;
181 print ("\033[H\033[J");
182 show_playfield ();//TODO: only redraw diff
187 void snake_advance (void) {
189 int new_row
= g
.s
->r
+(g
.d
==SOUTH
) -(g
.d
==NORTH
);
190 int new_col
= g
.s
->c
+(g
.d
==EAST
) -(g
.d
==WEST
);
192 //TODO: detect food hit and spawn a new food.
193 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
194 if (i
->r
== new_row
&& i
->c
== new_col
) {
200 if (new_row
>= g
.h
|| new_col
>= g
.w
|| new_row
< 0 || new_col
< 0)
201 exit(1); //TODO: longjump?
203 struct snake
* new_head
;
204 struct snake
* new_tail
; /* former second-to-last element */
205 for (new_tail
= g
.s
; new_tail
->next
->next
; new_tail
= new_tail
->next
)
206 /* use the opportunity of looping to check if we eat ourselves*/
207 if(new_tail
->next
->r
== new_row
&& new_tail
->next
->c
== new_col
)
209 new_head
= new_tail
->next
; /* reuse element instead of malloc() */
210 new_tail
->next
= NULL
;
212 new_head
->r
= new_row
;
213 new_head
->c
= new_col
;
214 new_head
->next
= g
.s
;
219 void spawn_item (int type
) {
224 /* loop through snake to check if we aren't on it */
225 //TODO: inefficient as snake gets longer; near impossible in the end
226 for (struct snake
* s
= g
.s
; s
; s
= s
->next
)
227 if (s
->r
== row
&& s
->c
== col
) goto try_again
;
229 //3. get item from category TODO
230 struct item
* new_item
= malloc (sizeof(struct item
));
235 new_item
->s
= time(0);
236 if (g
.i
) g
.i
->prev
= new_item
;
237 new_item
->next
= g
.i
;
242 void consume_item (struct item
* i
) {
243 struct item
* predecessor
= i
->prev
;
244 struct item
* successor
= i
->next
;
248 g
.p
+=10; //TODO: multiple types for different points?
249 snake_append(g
.s
, 0,0); /* position doesn't matter, as item */
250 break; /* will be reused as the head before it is drawn */
256 if (predecessor
== NULL
) {
258 if (successor
) successor
->prev
= NULL
;
260 predecessor
->next
= successor
;
261 successor
->prev
= predecessor
;
267 void show_playfield (void) {
270 printm (g
.w
/2-4/2, BORDER(T
,C
)); //TODO: i bet this breaks in dec mode
271 printf ("%s %04d %s", BORDER(S
,L
), g
.p
, BORDER(S
,R
));
272 printm(g
.w
/2-4/2, BORDER(T
,C
));
273 printf ("%s\n", BORDER(T
,R
));
276 for (int row
= 0; row
< g
.h
; row
++)
277 printf ("%s%*s%s\n", BORDER(C
,L
), CW
*g
.w
, "", BORDER(C
,R
));
281 printm (g
.w
, BORDER(B
,C
));
285 struct snake
* last
= NULL
;
286 for (struct snake
* s
= g
.s
; s
; s
= s
->next
) {
287 move_ph (s
->r
+COL_OFFSET
, s
->c
*CW
+LINE_OFFSET
);
289 int predecessor
= (last
==NULL
)?NONE
:
290 (last
->r
< s
->r
) ? NORTH
:
291 (last
->r
> s
->r
) ? SOUTH
:
292 (last
->c
> s
->c
) ? EAST
:
293 (last
->c
< s
->c
) ? WEST
:NONE
;
294 int successor
= (s
->next
== NULL
)?NONE
:
295 (s
->next
->r
< s
->r
) ? NORTH
:
296 (s
->next
->r
> s
->r
) ? SOUTH
:
297 (s
->next
->c
> s
->c
) ? EAST
:
298 (s
->next
->c
< s
->c
) ? WEST
:NONE
;
300 print (op
.scheme
->snake
[predecessor
][successor
]);
304 /* print item queue */
305 for (struct item
* i
= g
.i
; i
; i
= i
->next
) {
306 move_ph (i
->r
+LINE_OFFSET
, i
->c
*CW
+COL_OFFSET
);
311 void snake_append (struct snake
* s
, int row
, int col
) {
312 struct snake
* new = malloc (sizeof(struct snake
));
318 while (p
->next
) p
= p
->next
;
323 g
.s
= malloc (sizeof(struct snake
));
328 for (int i
= 1; i
< op
.l
; i
++)
329 snake_append(g
.s
, g
.h
/2, g
.w
/2-i
);
334 for (struct snake
* s
= g
.s
; s
;) {
335 struct snake
* tmp
= s
->next
;
339 for (struct item
* i
= g
.i
; i
;) {
340 struct item
* tmp
= i
->next
;
352 int getctrlseq (void) {
355 int offset
= 0x20; /* never sends control chars as data */
356 while ((c
= getchar()) != EOF
) {
360 case '\033': state
=ESC_SENT
; break;
366 case '[': state
=CSI_SENT
; break;
367 default: return CTRSEQ_INVALID
;
372 case 'A': return CTRSEQ_CURSOR_UP
;
373 case 'B': return CTRSEQ_CURSOR_DOWN
;
374 case 'C': return CTRSEQ_CURSOR_RIGHT
;
375 case 'D': return CTRSEQ_CURSOR_LEFT
;
376 default: return CTRSEQ_INVALID
;
380 return CTRSEQ_INVALID
;
386 void move_ph (int line
, int col
) {
387 /* move printhead to zero-indexed position */
388 printf ("\033[%d;%dH", line
+1, col
+1);
391 void clamp_fieldsize (void) {
392 /* clamp field size to terminal size and mouse maximum: */
394 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
396 if (g
.w
< 1) g
.w
= 1;
397 if (g
.h
< 1) g
.h
= 1;
399 if (COL_OFFSET
+ g
.w
*CW
+ COL_OFFSET
> w
.ws_col
)
400 g
.w
= (w
.ws_col
- COL_OFFSET
- COL_OFFSET
)/CW
; //TODO: does not work in `-d' (in xterm)
401 if (LINE_OFFSET
+ g
.h
+ LINES_AFTER
> w
.ws_row
)
402 g
.h
= w
.ws_row
- (LINE_OFFSET
+LINES_AFTER
);
405 void timer_setup (int enable
) {
406 static struct itimerval tbuf
;
407 tbuf
.it_interval
.tv_sec
= 0;//TODO: make configurable, TODO: speed up
408 tbuf
.it_interval
.tv_usec
= 300000;
412 tbuf
.it_value
.tv_sec
= tbuf
.it_interval
.tv_sec
;
413 tbuf
.it_value
.tv_usec
= tbuf
.it_interval
.tv_usec
;
414 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
419 tbuf
.it_value
.tv_sec
= 0;
420 tbuf
.it_value
.tv_usec
= 0;
421 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
429 void signal_setup (void) {
430 struct sigaction saction
;
432 saction
.sa_handler
= signal_handler
;
433 sigemptyset(&saction
.sa_mask
);
434 saction
.sa_flags
= 0;
435 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
440 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
446 void signal_handler (int signum
) {
450 //dtime = difftime (time(NULL), g.t);
451 //move_ph (1, g.w*CW-(CW%2)-3-(dtime>999));
452 //printf ("[%03d]", g.t?dtime:0);
460 void screen_setup (int enable
) {
463 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
464 printf ("\033[H\033[J"); /* reset cursor, clear screen */
465 printf ("\033[?25l"); /* hide cursor */
466 //print (op.scheme->init_seq); /* swich charset, if necessary */
468 //print (op.scheme->reset_seq); /* reset charset, if necessary */
469 printf ("\033[?25h"); /* show cursor */
470 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
475 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
476 void raw_mode(int enable
) {
477 static struct termios saved_term_mode
;
478 struct termios raw_term_mode
;
481 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
482 raw_term_mode
= saved_term_mode
;
483 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
484 raw_term_mode
.c_cc
[VMIN
] = 1 ;
485 raw_term_mode
.c_cc
[VTIME
] = 0;
486 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
488 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);