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