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