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