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