]> git.gir.st - minesVIiper.git/blob - mines_2017.c
implement cursor keys, fix endgame emoticon, minor fixes
[minesVIiper.git] / mines_2017.c
1 //TODO: segfault: click outside playfield, then use cursorkeys
2 /*******************************************************************************
3 minesviiper 0.3.1459
4 By Tobias Girstmair, 2015 - 2018
5
6 ./minesviiper 16x16x40
7 (see ./minesviiper -h for full list of options)
8
9 MOUSE MODE: - left click to open and choord
10 - right click to flag/unflag
11 VI MODE: - hjkl to move cursor left/down/up/right
12 - bduw to jump left/down/up/right by 5 cells
13 - o to open and choord
14 - i to flag/unflag
15
16 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
17 *******************************************************************************/
18
19
20 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
21 #include <ctype.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/ioctl.h>
26 #include <sys/time.h>
27 #include <termios.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #include "schemes.h"
32
33 #define LINE_OFFSET 3
34 #define COL_OFFSET 2
35 #define BIG_MOVE 5
36
37 #define MIN(a,b) (a>b?b:a)
38 #define MAX(a,b) (a>b?a:b)
39 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
40 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
41 #define print(str) fputs (str, stdout)
42 #define EMOT(e) op.scheme->emoticons[EMOT_ ## e]
43 #define BORDER(l, c) op.scheme->border[B_ ## l][B_ ## c]
44
45 struct minecell {
46 unsigned m:2; /* mine?1:killmine?2:0 */
47 unsigned o:1; /* open?1:0 */
48 unsigned f:2; /* flagged?1:questioned?2:0 */
49 unsigned n:4; /* 0<= neighbours <=8 */
50 };
51 struct minefield {
52 struct minecell **c;
53 //TODO: rename w, h to L, C
54 int w; /* width */
55 int h; /* height */
56 int m; /* number of mines */
57
58 int f; /* flags counter */
59 int t; /* time of game start */
60 int p[2]; /* cursor position {line, col} */
61 int s; /* space mode */
62 int o; /* mode */
63 } f;
64
65 struct opt {
66 struct minescheme* scheme;
67 int mode; /* allow flags? quesm? */
68 } op;
69
70 struct line_col {
71 int l;
72 int c;
73 };
74
75 void fill_minefield (int, int);
76 void move (int, int);
77 void cursor_move (int, int);
78 void to_next_boundary (int l, int c, char direction);
79 int getch (unsigned char*);
80 int getctrlseq (unsigned char*);
81 int everything_opened (void);
82 int wait_mouse_up (int, int);
83 void partial_show_minefield (int, int, int);
84 void show_minefield (int);
85 int get_neighbours (int, int, int);
86 int uncover_square (int, int);
87 void flag_square (int, int);
88 void quesm_square (int, int);
89 int choord_square (int, int);
90 int do_uncover (int*);
91 struct minecell** alloc_array (int, int);
92 void free_field (void);
93 char* get_emoticon(void);
94 int screen2field_l (int);
95 int screen2field_c (int);
96 int field2screen_l (int);
97 int field2screen_c (int);
98 void quit(void);
99 void signal_handler (int signum);
100 void timer_setup (int);
101
102 enum modes {
103 NORMAL,
104 REDUCED,
105 SHOWMINES,
106 HIGHLIGHT,
107 };
108 enum flagtypes {
109 NOFLAG,
110 FLAG,
111 QUESM,
112 };
113 enum fieldopenstates {
114 CLOSED,
115 OPENED,
116 };
117 enum game_states {
118 GAME_INPROGRESS,
119 GAME_NEW,
120 GAME_WON,
121 GAME_LOST,
122 };
123 enum space_modes {
124 MODE_OPEN,
125 MODE_FLAG,
126 MODE_QUESM,
127 };
128 enum event {
129 /* for getctrlseq() */
130 CTRSEQ_NULL = 0,
131 CTRSEQ_EOF = -1,
132 CTRSEQ_INVALID = -2,
133 CTRSEQ_MOUSE = -3,
134 /* for getch() */
135 CTRSEQ_MOUSE_LEFT = -4,
136 CTRSEQ_MOUSE_MIDDLE = -5,
137 CTRSEQ_MOUSE_RIGHT = -6,
138 CTRSEQ_CURSOR_LEFT = -7,
139 CTRSEQ_CURSOR_DOWN = -8,
140 CTRSEQ_CURSOR_UP = -9,
141 CTRSEQ_CURSOR_RIGHT = -10,
142 };
143 enum mine_types {
144 NO_MINE,
145 STD_MINE,
146 DEATH_MINE,
147 };
148
149 #define CW op.scheme->cell_width
150 void signal_handler (int signum) {
151 int dtime;
152 switch (signum) {
153 case SIGALRM:
154 dtime = difftime (time(NULL), f.t);
155 move (1, f.w*CW-(CW%2)-3-(dtime>999));
156 printf ("[%03d]", f.t?dtime:0);
157 break;
158 case SIGINT:
159 exit(128+SIGINT);
160 }
161 }
162 #undef CW
163
164 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
165 struct termios saved_term_mode;
166 struct termios set_raw_term_mode() {
167 struct termios cur_term_mode, raw_term_mode;
168
169 tcgetattr(STDIN_FILENO, &cur_term_mode);
170 raw_term_mode = cur_term_mode;
171 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
172 raw_term_mode.c_cc[VMIN] = 1 ;
173 raw_term_mode.c_cc[VTIME] = 0;
174 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
175
176 return cur_term_mode;
177 }
178 void restore_term_mode(struct termios saved_term_mode) {
179 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
180 }
181
182
183 int main (int argc, char** argv) {
184 struct sigaction saction;
185 saved_term_mode = set_raw_term_mode();
186
187 atexit (*quit);
188
189 saction.sa_handler = signal_handler;
190 sigemptyset(&saction.sa_mask);
191 saction.sa_flags = 0;
192 if (sigaction(SIGALRM, &saction, NULL) < 0 ) {
193 perror("SIGALRM");
194 exit(1);
195 }
196
197 if (sigaction(SIGINT, &saction, NULL) < 0 ) {
198 perror ("SIGINT");
199 exit (1);
200 }
201 /* end screen setup */
202
203 op.scheme = &symbols_mono;
204 op.mode = FLAG;
205 /* end defaults */
206 /* parse options */
207 int optget;
208 opterr = 0; /* don't print message on unrecognized option */
209 while ((optget = getopt (argc, argv, "+hnfqcdx")) != -1) {
210 switch (optget) {
211 case 'n': op.mode = NOFLAG; break;
212 case 'f': op.mode = FLAG; break; /*default*/
213 case 'q': op.mode = QUESM; break;
214 case 'c': op.scheme = &symbols_col1; break;
215 case 'd': op.scheme = &symbols_doublewidth; break;
216 case 'h':
217 default:
218 fprintf (stderr, "%s [OPTIONS] [FIELDSPEC]\n"
219 "OPTIONS:\n"
220 " -n(o flagging)\n"
221 " -f(lagging)\n"
222 " -q(uestion marks)\n"
223 " -c(olored symbols)\n"
224 " -d(ec charset symbols)\n"
225 "FIELDSPEC:\n"
226 " WxH[xM] (width 'x' height 'x' mines)\n"
227 " defaults to 30x16x99; mines default to ~20%%\n"
228 "\n"
229 "Keybindings:\n"
230 " hjkl: move left/down/up/right\n"
231 " bduw: move to next boundary\n"
232 " ^Gg$: move to the left/bottom/top/right\n"
233 " o: open/choord\n"
234 " i: flag/unflag\n"
235 " space:modeful cursor (either open or flag)\n"
236 " a: toggle mode for space (open/flag)\n"
237 " mX: set a mark for letter X\n"
238 " `X: move to mark X (aliased to ')\n"
239 " r: start a new game\n"
240 " q: quit\n", argv[0]);
241 _exit(0);
242 }
243 }
244 /* end parse options*/
245 if (optind < argc) { /* parse Fieldspec */
246 int n = sscanf (argv[optind], "%dx%dx%d", &f.w, &f.h, &f.m);
247 struct winsize w;
248 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
249 #define CW op.scheme->cell_width
250 #define WW w.ws_col /*window width */
251 #define WH w.ws_row /*window height */
252 #define FW f.w /* field width */
253 #define FH f.h /* field height */
254 #define MM 231 /* mouse maximum*/
255 #define LB 2 /* left border */
256 #define RB 2 /* right border */
257 #define TB 3 /* top border */
258 #define BB 2 /*bottom border */
259
260 if (LB + FW*CW + RB > WW) FW = WW/CW - (LB+RB);
261 if (TB + FH + BB > WH) FH = WH - (TB+BB);
262 if (LB + FW*CW > MM) FW = MM/CW - LB;
263 if (TB + FH > MM) FH = MM - TB;
264
265 if (n < 2) {
266 fprintf (stderr, "FIELDSPEC: WxH[xM]"
267 " (width 'x' height 'x' mines)\n");
268 return 1;
269 } else if (n == 2) {
270 if (f.w < 30) f.m = f.w*f.h*.15625;
271 else f.m = f.w*f.h*.20625;
272 }
273 #undef CW
274 #undef WW
275 #undef WH
276 #undef FW
277 #undef FH
278 #undef MM
279 #undef LB
280 #undef RB
281 #undef TB
282 #undef BB
283 } else { /* use defaults */
284 f.w = 30;
285 f.h = 16;
286 f.m = 99;
287 }
288 /* check boundaries */
289 if (f.m > (f.w-1) * (f.h-1)) {
290 f.m = (f.w-1) * (f.h-1);
291 fprintf (stderr, "too many mines. reduced to %d.\r\n", f.m);
292 }
293 /* end check */
294
295 newgame:
296 f.c = alloc_array (f.h, f.w);
297
298 f.f = 0;
299 f.t = 0;
300 f.p[0] = 0;
301 f.p[1] = 0;
302
303 int is_newgame = 1;
304 int cheatmode = 0;
305 f.s = MODE_OPEN;
306 f.o = GAME_NEW;
307 struct line_col markers[26];
308 for (int i=26; i; markers[--i].l = -1);
309
310 /* switch to alternate screen */
311 printf ("\033[?47h");
312 /* reset cursor, clear screen */
313 printf ("\033[H\033[J");
314
315 /* swich charset, if necessary */
316 if (op.scheme->init_seq != NULL) print (op.scheme->init_seq);
317
318 show_minefield (NORMAL);
319
320 /* enable mouse, hide cursor */
321 printf ("\033[?1000h\033[?25l");
322
323 while (1) {
324 int l, c;
325 int action;
326 unsigned char mouse[3];
327
328 action = getch(mouse);
329 switch (action) {
330 case ' ':
331 if (f.s == MODE_OPEN ||
332 f.c[f.p[0]][f.p[1]].o == OPENED) {
333 switch (do_uncover(&is_newgame)) {
334 case GAME_LOST: goto lose;
335 case GAME_WON: goto win;
336 }
337 } else if (f.s == MODE_FLAG) {
338 flag_square (f.p[0], f.p[1]);
339 } else if (f.s == MODE_QUESM) {
340 quesm_square (f.p[0], f.p[1]);
341 }
342 break;
343 case 'a':
344 f.s = (f.s+1)%(op.mode+1);
345 show_minefield (cheatmode?SHOWMINES:NORMAL);
346 break;
347 case CTRSEQ_MOUSE_LEFT:
348 f.p[0] = screen2field_l (mouse[2]);
349 f.p[1] = screen2field_c (mouse[1]);
350 /* :D clicked: TODO: won't work in single-width mode! */
351 if (mouse[2] == LINE_OFFSET-1 &&
352 (mouse[1] == f.w+COL_OFFSET ||
353 mouse[1] == f.w+COL_OFFSET+1)) {
354 free_field ();
355 goto newgame;
356 }
357 if (f.p[1] < 0 || f.p[1] >= f.w ||
358 f.p[0] < 0 || f.p[0] >= f.h) break; /*out of bound*/
359 /* fallthrough */
360 case 'o':
361 switch (do_uncover(&is_newgame)) {
362 case GAME_LOST: goto lose;
363 case GAME_WON: goto win;
364 }
365 break;
366 case CTRSEQ_MOUSE_RIGHT:
367 f.p[0] = screen2field_l (mouse[2]);
368 f.p[1] = screen2field_c (mouse[1]);
369 if (f.p[1] < 0 || f.p[1] >= f.w ||
370 f.p[0] < 0 || f.p[0] >= f.h) break; /*out of bound*/
371 /* fallthrough */
372 case 'i': flag_square (f.p[0], f.p[1]); break;
373 case '?':quesm_square (f.p[0], f.p[1]); break;
374 case CTRSEQ_CURSOR_LEFT:
375 case 'h': cursor_move (f.p[0], f.p[1]-1 ); break;
376 case CTRSEQ_CURSOR_DOWN:
377 case 'j': cursor_move (f.p[0]+1, f.p[1] ); break;
378 case CTRSEQ_CURSOR_UP:
379 case 'k': cursor_move (f.p[0]-1, f.p[1] ); break;
380 case CTRSEQ_CURSOR_RIGHT:
381 case 'l': cursor_move (f.p[0], f.p[1]+1 ); break;
382 case 'w': to_next_boundary (f.p[0], f.p[1], '>'); break;
383 case 'b': to_next_boundary (f.p[0], f.p[1], '<'); break;
384 case 'u': to_next_boundary (f.p[0], f.p[1], '^'); break;
385 case 'd': to_next_boundary (f.p[0], f.p[1], 'v'); break;
386 case '0': /* fallthrough */
387 case '^': cursor_move (f.p[0], 0 ); break;
388 case '$': cursor_move (f.p[0], f.w-1 ); break;
389 case 'g': cursor_move (0, f.p[1] ); break;
390 case 'G': cursor_move (f.h-1, f.p[1] ); break;
391 case 'm':
392 action = tolower(getch(mouse));
393 if (action < 'a' || action > 'z') break;/*out of bound*/
394 markers[action-'a'].l = f.p[0];
395 markers[action-'a'].c = f.p[1];
396 break;
397 case'\'': /* fallthrough */
398 case '`':
399 action = tolower(getch(mouse));
400 if (action < 'a' || action > 'z' /* out of bound or */
401 || markers[action-'a'].l == -1) break; /* unset */
402 cursor_move (markers[action-'a'].l, markers[action-'a'].c);
403 break;
404 case 'r': /* start a new game */
405 free_field ();
406 goto newgame;
407 case 'q':
408 goto quit;
409 case '\014': /* Ctrl-L -- redraw */
410 show_minefield (NORMAL);
411 break;
412 case '\\':
413 if (is_newgame) {
414 is_newgame = 0;
415 fill_minefield (-1, -1);
416 timer_setup(1);
417 }
418 show_minefield (cheatmode?NORMAL:SHOWMINES);
419 cheatmode = !cheatmode;
420 break;
421 }
422 }
423
424 win:
425 f.o = GAME_WON;
426 goto endgame;
427 lose:
428 f.o = GAME_LOST;
429 goto endgame;
430 endgame:
431 timer_setup(0); /* stop timer */
432 show_minefield (SHOWMINES);
433 int gotaction;
434 do {
435 unsigned char mouse[3];
436 gotaction = getch(mouse);
437 /* :D clicked: TODO: won't work in single-width mode! */
438 if (gotaction==CTRSEQ_MOUSE_LEFT && mouse[2]==LINE_OFFSET-1 &&
439 (mouse[1]==f.w+COL_OFFSET || mouse[1]==f.w+COL_OFFSET+1)) {
440 free_field ();
441 goto newgame;
442 } else if (gotaction == 'r') {
443 free_field ();
444 goto newgame;
445 } else if (gotaction == 'q') {
446 goto quit;
447 }
448 } while (1);
449
450 quit:
451 return 0;
452 }
453
454 void quit (void) {
455 //move(0,0); //move(f.h+LINE_OFFSET+2, 0);
456 /* disable mouse, show cursor */
457 printf ("\033[?9l\033[?25h");
458 /* reset charset, if necessary */
459 if (op.scheme && op.scheme->reset_seq) print (op.scheme->reset_seq);
460 /* revert to primary screen */
461 printf ("\033[?47l");
462 free_field ();
463 restore_term_mode(saved_term_mode);
464 }
465
466 /* I haven't won as long as a cell exists, that
467 - I haven't opened, and
468 - is not a mine */
469 int everything_opened (void) {
470 for (int row = 0; row < f.h; row++)
471 for (int col = 0; col < f.w; col++)
472 if (f.c[row][col].o == CLOSED &&
473 f.c[row][col].m == NO_MINE ) return 0;
474 return 1;
475 }
476
477 int wait_mouse_up (int l, int c) {
478 unsigned char mouse2[3];
479 int level = 1;
480 int l2, c2;
481
482 /* show :o face */
483 move (1, field2screen_c (f.w/2)-1); print (EMOT(OHH));
484
485 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
486 /* show a pushed-in button if cursor is on minefield */
487 move (l+LINE_OFFSET, field2screen_c(c));
488 fputs (op.scheme->mouse_highlight, stdout);
489 }
490
491 while (level > 0) {
492 if (getctrlseq (mouse2) == CTRSEQ_MOUSE) {
493 /* ignore mouse wheel events: */
494 if (mouse2[0] & 0x40) continue;
495
496 else if (mouse2[0]&3 == 3) level--; /* release event */
497 else level++; /* another button pressed */
498 }
499 }
500
501 move (1, field2screen_c (f.w/2)-1);
502 print (get_emoticon());
503
504 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
505 partial_show_minefield (l, c, NORMAL);
506 }
507 c2 = screen2field_c(mouse2[1]);
508 l2 = screen2field_l(mouse2[2]);
509 return ((l2 == l) && (c2 == c));
510 }
511
512 int choord_square (int line, int col) {
513 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
514 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
515 if (f.c[l][c].f != FLAG) {
516 if (uncover_square (l, c))
517 return 1;
518 }
519 }
520 }
521
522 return 0;
523 }
524
525 int uncover_square (int l, int c) {
526 f.c[l][c].o = OPENED;
527 f.c[l][c].f = NOFLAG; /*must not be QUESM, otherwise rendering issues*/
528 partial_show_minefield (l, c, NORMAL);
529
530 if (f.c[l][c].m) {
531 f.c[l][c].m = DEATH_MINE;
532 return 1;
533 }
534
535 /* check for chording */
536 if (f.c[l][c].n == 0) {
537 for (int choord_l = -1; choord_l <= 1; choord_l++) {
538 for (int choord_c = -1; choord_c <= 1; choord_c++) {
539 int newl = l + choord_l;
540 int newc = c + choord_c;
541 if (newl >= 0 && newl < f.h &&
542 newc >= 0 && newc < f.w &&
543 f.c[newl][newc].o == CLOSED &&
544 uncover_square (newl, newc)) {
545 return 1;
546 }
547 }
548 }
549 }
550
551 return 0;
552 }
553
554 void flag_square (int l, int c) {
555 static char modechar[] = {'*', '!', '?'};
556
557 if (f.c[l][c].o != CLOSED) return;
558 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
559 are allowed) */
560 f.c[l][c].f = (f.c[l][c].f + 1) % (op.mode + 1);
561 if (f.c[l][c].f==FLAG) f.f++;
562 else f.f--; //WARN: breaks on `-q'!
563 partial_show_minefield (l, c, NORMAL);
564 move (1, op.scheme->cell_width);
565 printf ("[%03d%c]", f.m - f.f, modechar[f.s]);
566 }
567
568 void quesm_square (int l, int c) {
569 /* toggle question mark / none. won't turn flags into question marks.
570 unlike flag_square, this function doesn't respect `-q'. */
571 if (f.c[l][c].o != CLOSED) return;
572 else if (f.c[l][c].f == NOFLAG) f.c[l][c].f = QUESM;
573 else if (f.c[l][c].f == QUESM) f.c[l][c].f = NOFLAG;
574 partial_show_minefield (l, c, NORMAL);
575 }
576
577 int do_uncover (int* is_newgame) {
578 if (*is_newgame) {
579 *is_newgame = 0;
580 fill_minefield (f.p[0], f.p[1]);
581 timer_setup(1);
582 }
583
584 if (f.c[f.p[0]][f.p[1]].f == FLAG ) return GAME_INPROGRESS;
585 if (f.c[f.p[0]][f.p[1]].o == CLOSED) {
586 if (uncover_square (f.p[0], f.p[1])) return GAME_LOST;
587 } else if (get_neighbours (f.p[0], f.p[1], 1) == 0) {
588 if (choord_square (f.p[0], f.p[1])) return GAME_LOST;
589 }
590 if (everything_opened()) return GAME_WON;
591
592 return GAME_INPROGRESS;
593 }
594
595 void fill_minefield (int l, int c) {
596 srand (time(0));
597 int mines_set = f.m;
598 while (mines_set) {
599 int line = rand() % f.h;
600 int col = rand() % f.w;
601
602 if (f.c[line][col].m) {
603 /* skip if field already has a mine */
604 continue;
605 } else if ((line == l) && (col == c)) {
606 /* don't put a mine on the already opened (first click) field */
607 continue;
608 } else {
609 mines_set--;
610 f.c[line][col].m = STD_MINE;
611 }
612 }
613
614 /* precalculate neighbours */
615 for (int l=0; l < f.h; l++)
616 for (int c=0; c < f.w; c++)
617 f.c[l][c].n = get_neighbours (l, c, NORMAL);
618 }
619
620 void move (int line, int col) {
621 printf ("\033[%d;%dH", line+1, col+1);
622 }
623
624 /* absolute coordinates! */
625 void cursor_move (int l, int c) {
626 partial_show_minefield (f.p[0], f.p[1], NORMAL);
627 /* update f.p */
628 f.p[0] = CLAMP(l, 0, f.h-1);
629 f.p[1] = CLAMP(c, 0, f.w-1);
630 move (f.p[0]+LINE_OFFSET, field2screen_c(f.p[1]));
631 //fputs (op.scheme->mouse_highlight, stdout);
632
633 /* invert unless ! or ? (and flag_offset is used in the scheme): */
634 if (!f.c[f.p[0]][f.p[1]].f || !op.scheme->flag_offset)
635 print("\033[7m");
636 partial_show_minefield (f.p[0], f.p[1], HIGHLIGHT);
637 print("\033[0m");//un-invert
638 }
639
640 /* to_next_boundary(): move into the supplied direction until a change in open-
641 state or flag-state is found and move there. falls back to BIG_MOVE. */
642 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
643 new_ ## X OP ## = BIG_MOVE;\
644 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
645 if ((f.c[L ][C ].o<<2 + f.c[L ][C ].f) \
646 != (f.c[L1][C1].o<<2 + f.c[L1][C1].f)) {\
647 new_ ## X = i OP 1;\
648 break;\
649 }\
650 } while(0)
651 void to_next_boundary (int l, int c, char direction) {
652 int new_l = l;
653 int new_c = c;
654 switch (direction) {
655 case '>': FIND_NEXT(c, l, i, l, i+1, w, +); break;
656 case '<': FIND_NEXT(c, l, i, l, i-1, w, -); break;
657 case '^': FIND_NEXT(l, i, c, i-1, c, h, -); break;
658 case 'v': FIND_NEXT(l, i, c, i+1, c, h, +); break;
659 }
660
661 cursor_move (new_l, new_c);
662 }
663 #undef FIND_NEXT
664
665 char* cell2schema (int l, int c, int mode) {
666 struct minecell cell = f.c[l][c];
667 /* move past invert-ctrlsequence when highlighting the cursor: */
668 int offset = ((mode==HIGHLIGHT)*op.scheme->flag_offset);
669
670 if (mode == SHOWMINES) return (
671 cell.f == FLAG && cell.m ? op.scheme->field_flagged+offset:
672 cell.f == FLAG && !cell.m ? op.scheme->mine_wrongf:
673 cell.m == STD_MINE ? op.scheme->mine_normal:
674 cell.m == DEATH_MINE ? op.scheme->mine_death:
675 cell.o == CLOSED ? op.scheme->field_closed:
676 /*.......................*/ op.scheme->number[f.c[l][c].n]);
677 else return (
678 cell.f == FLAG ? op.scheme->field_flagged+offset:
679 cell.f == QUESM ? op.scheme->field_question+offset:
680 cell.o == CLOSED ? op.scheme->field_closed:
681 cell.m == STD_MINE ? op.scheme->mine_normal:
682 cell.m == DEATH_MINE ? op.scheme->mine_death:
683 /*.......................*/ op.scheme->number[f.c[l][c].n]);
684 }
685
686 void partial_show_minefield (int l, int c, int mode) {
687 move (l+LINE_OFFSET, field2screen_c(c));
688
689 print (cell2schema(l, c, mode));
690 }
691
692 char* get_emoticon(void) {
693 return f.o==GAME_WON ? EMOT(WON):
694 f.o==GAME_LOST? EMOT(DEAD):
695 EMOT(SMILE);
696 }
697
698 /* https://zserge.com/blog/c-for-loop-tricks.html */
699 #define print_line(which) \
700 for (int _break = (printf(BORDER(which,LEFT)), 1); _break; \
701 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
702 #define print_border(which, width) \
703 print_line(which) printm (width, BORDER(which,MIDDLE))
704 void show_minefield (int mode) {
705 int dtime;
706 int half_spaces = f.w*op.scheme->cell_width/2;
707 int left_spaces = MAX(0,half_spaces-7-(f.m-f.f>999)+2);
708 int right_spaces = MAX(0,half_spaces-6-(dtime>999));
709 static char modechar[] = {'*', '!', '?'};
710
711 move (0,0);
712
713 dtime = (f.t == 0)?0: difftime (time(NULL), f.t);
714
715 print_border(TOP, f.w);
716 print_line(STATUS) {
717 printf("[%03d%c]%*s%*s[%03d]",
718 /* [ */ f.m - f.f, modechar[f.s], /* ] */
719 left_spaces, get_emoticon(), right_spaces,"",
720 /* [ */ dtime /* ] */);
721 }
722 print_border(DIVIDER, f.w);
723 /* main body */
724 for (int l = 0; l < f.h; l++) print_line(FIELD)
725 printm (f.w, cell2schema(l, _loop, mode));
726 print_border(BOTTOM, f.w);
727 }
728
729 int get_neighbours (int line, int col, int reduced_mode) {
730 /* counts mines surrounding a square
731 modes: 0=normal; 1=reduced */
732
733 int count = 0;
734
735 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
736 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
737 if (!l && !c) continue;
738
739 count += !!f.c[l][c].m;
740 count -= reduced_mode * f.c[l][c].f==FLAG;
741 }
742 }
743 return count;
744 }
745
746 struct minecell** alloc_array (int lines, int cols) {
747 struct minecell** a = malloc (lines * sizeof(struct minecell*));
748 if (a == NULL) return NULL;
749 for (int l = 0; l < lines; l++) {
750 a[l] = calloc (cols, sizeof(struct minecell));
751 if (a[l] == NULL) goto unalloc;
752 }
753
754 return a;
755 unalloc:
756 for (int l = 0; l < lines; l++)
757 free (a[l]);
758 return NULL;
759 }
760
761 void free_field (void) {
762 if (f.c == NULL) return;
763 for (int l = 0; l < f.h; l++) {
764 free (f.c[l]);
765 }
766
767 free (f.c);
768 f.c = NULL;
769 }
770
771 int screen2field_l (int l) {
772 return (l-LINE_OFFSET) - 1;
773 }
774 /* some trickery is required to extract the mouse position from the cell width,
775 depending on wheather we are using full width characters or double line width.
776 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
777 #define CW op.scheme->cell_width
778 int screen2field_c (int c) {
779 return (c-COL_OFFSET+1 - 2*(CW%2))/2 - CW/2;
780 }
781 #undef CW
782 int field2screen_l (int l) {
783 return 0; //TODO: is never used, therefore not implemented
784 }
785 int field2screen_c (int c) {
786 return (op.scheme->cell_width*c+COL_OFFSET - (op.scheme->cell_width%2));
787 }
788
789 enum esc_states {
790 START,
791 ESC_SENT,
792 CSI_SENT,
793 MOUSE_EVENT,
794 };
795 int getctrlseq (unsigned char* buf) {
796 int c;
797 int state = START;
798 int offset = 0x20; /* never sends control chars as data */
799 while ((c = getchar()) != EOF) {
800 switch (state) {
801 case START:
802 switch (c) {
803 case '\033': state=ESC_SENT; break;
804 default: return c;
805 }
806 break;
807 case ESC_SENT:
808 switch (c) {
809 case '[': state=CSI_SENT; break;
810 default: return CTRSEQ_INVALID;
811 }
812 break;
813 case CSI_SENT:
814 switch (c) {
815 case 'A': return CTRSEQ_CURSOR_UP;
816 case 'B': return CTRSEQ_CURSOR_DOWN;
817 case 'C': return CTRSEQ_CURSOR_RIGHT;
818 case 'D': return CTRSEQ_CURSOR_LEFT;
819 case 'M': state=MOUSE_EVENT; break;
820 default: return CTRSEQ_INVALID;
821 }
822 break;
823 case MOUSE_EVENT:
824 buf[0] = c - offset;
825 buf[1] = getchar() - offset;
826 buf[2] = getchar() - offset;
827 return CTRSEQ_MOUSE;
828 default:
829 return CTRSEQ_INVALID;
830 }
831 }
832 return 2;
833 }
834
835 int getch(unsigned char* buf) {
836 /* returns a character, EOF, or constant for an escape/control sequence - NOT
837 compatible with the ncurses implementation of same name */
838 int action = getctrlseq(buf);
839 int l, c;
840 switch (action) {
841 case CTRSEQ_MOUSE:
842 l = screen2field_l (buf[2]);
843 c = screen2field_c (buf[1]);
844
845 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
846 int success = wait_mouse_up(l, c);
847
848 /* mouse moved while pressed: */
849 if (!success) return CTRSEQ_INVALID;
850
851 switch (buf[0]) {
852 case 0: return CTRSEQ_MOUSE_LEFT;
853 case 1: return CTRSEQ_MOUSE_MIDDLE;
854 case 2: return CTRSEQ_MOUSE_RIGHT;
855 }
856 }
857
858 return action;
859 }
860
861 void timer_setup (int enable) {
862 static struct itimerval tbuf;
863 tbuf.it_interval.tv_sec = 1;
864 tbuf.it_interval.tv_usec = 0;
865
866 if (enable) {
867 f.t = time(NULL);
868 tbuf.it_value.tv_sec = 1;
869 tbuf.it_value.tv_usec = 0;
870 if (setitimer(ITIMER_REAL, &tbuf, NULL) == -1) {
871 perror("setitimer");
872 exit(1);
873 }
874 } else {
875 tbuf.it_value.tv_sec = 0;
876 tbuf.it_value.tv_usec = 0;
877 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
878 perror("setitimer");
879 exit(1);
880 }
881 }
882
883 }
Imprint / Impressum