]> git.gir.st - minesVIiper.git/blob - mines_2017.c
move find-word-boundary code into one giant macro (dry)
[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 /* to_next_boundary(): move into the supplied direction until a change in open-
577 state or flag-state is found and move there. falls back to BIG_MOVE. */
578 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
579 new_ ## X OP ## = BIG_MOVE;\
580 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
581 if ((f.c[L ][C ].o<<2 + f.c[L ][C ].f) \
582 != (f.c[L1][C1].o<<2 + f.c[L1][C1].f)) {\
583 new_ ## X = i OP 1;\
584 break;\
585 }\
586 } while(0)
587 void to_next_boundary (int l, int c, char direction) {
588 int new_l = l;
589 int new_c = c;
590 switch (direction) {
591 case '>': FIND_NEXT(c, l, i, l, i+1, w, +); break;
592 case '<': FIND_NEXT(c, l, i, l, i-1, w, -); break;
593 case '^': FIND_NEXT(l, i, c, i-1, c, h, -); break;
594 case 'v': FIND_NEXT(l, i, c, i+1, c, h, +); break;
595 }
596
597 cursor_move (new_l, new_c);
598 }
599 #undef FIND_NEXT
600
601 char* cell2schema (int l, int c, int mode) {
602 struct minecell cell = f.c[l][c];
603 /* move past invert-ctrlsequence when highlighting the cursor: */
604 int offset = ((mode==HIGHLIGHT)*op.scheme->flag_offset);
605
606 if (mode == SHOWMINES) return (
607 cell.f == FLAG && cell.m ? op.scheme->field_flagged+offset:
608 cell.f == FLAG && !cell.m ? op.scheme->mine_wrongf+offset:
609 cell.m == STD_MINE ? op.scheme->mine_normal:
610 cell.m == DEATH_MINE ? op.scheme->mine_death:
611 cell.o == CLOSED ? op.scheme->field_closed:
612 /*.......................*/ op.scheme->number[f.c[l][c].n]);
613 else return (
614 cell.f == FLAG ? op.scheme->field_flagged+offset:
615 cell.f == QUESM ? op.scheme->field_question+offset:
616 cell.o == CLOSED ? op.scheme->field_closed:
617 cell.m == STD_MINE ? op.scheme->mine_normal:
618 cell.m == DEATH_MINE ? op.scheme->mine_death:
619 /*.......................*/ op.scheme->number[f.c[l][c].n]);
620 }
621
622 void partial_show_minefield (int l, int c, int mode) {
623 move (l+LINE_OFFSET, field2screen_c(c));
624
625 print (cell2schema(l, c, mode));
626 }
627
628 void show_minefield (int mode) {
629 int dtime;
630 static char modechar[] = {'*', '!', '?'};
631
632 move (0,0);
633
634 if (f.t == 0) {
635 dtime = 0;
636 } else {
637 dtime = difftime (time(NULL), f.t);
638 }
639
640 /* first line */
641 print (op.scheme->border_top_l);
642 printm (f.w*op.scheme->cell_width,op.scheme->border_top_m);
643 printf ("%s\r\n", op.scheme->border_top_r);
644 /* second line */
645 print (op.scheme->border_status_l);
646 printf("[%03d]", f.f);
647 printm (f.w*op.scheme->cell_width/2-6, " ");
648 printf ("%s", mode==SHOWMINES?":C":":D");
649 printm (f.w*op.scheme->cell_width/2-6-4, " ");
650 printf ("[%c] [%03d]", modechar[f.s], dtime);
651 print (op.scheme->border_status_r);
652 print ("\r\n");
653 /* third line */
654 print (op.scheme->border_spacer_l);
655 printm (f.w*op.scheme->cell_width,op.scheme->border_spacer_m);
656 print (op.scheme->border_spacer_r);
657 print ("\r\n");
658 /* main body */
659 for (int l = 0; l < f.h; l++) {
660 print (op.scheme->border_field_l);
661 for (int c = 0; c < f.w; c++) {
662 print (cell2schema(l, c, mode));
663 }
664 print (op.scheme->border_field_r); print ("\r\n");
665 }
666 /* last line */
667 print (op.scheme->border_bottom_l);
668 printm (f.w*op.scheme->cell_width,op.scheme->border_bottom_m);
669 print (op.scheme->border_bottom_r);
670 print ("\r\n");
671 }
672
673 int get_neighbours (int line, int col, int reduced_mode) {
674 /* counts mines surrounding a square
675 modes: 0=normal; 1=reduced */
676
677 int count = 0;
678
679 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
680 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
681 if (!l && !c) continue;
682
683 count += !!f.c[l][c].m;
684 count -= reduced_mode * f.c[l][c].f==FLAG;
685 }
686 }
687 return count;
688 }
689
690 struct minecell** alloc_array (int lines, int cols) {
691 struct minecell** a = malloc (lines * sizeof(struct minecell*));
692 if (a == NULL) return NULL;
693 for (int l = 0; l < lines; l++) {
694 a[l] = calloc (cols, sizeof(struct minecell));
695 if (a[l] == NULL) goto unalloc;
696 }
697
698 return a;
699 unalloc:
700 for (int l = 0; l < lines; l++)
701 free (a[l]);
702 return NULL;
703 }
704
705 void free_field () {
706 if (f.c == NULL) return;
707 for (int l = 0; l < f.h; l++) {
708 free (f.c[l]);
709 }
710 free (f.c);
711 }
712
713 int screen2field_l (int l) {
714 return (l-LINE_OFFSET) - 1;
715 }
716 /* some trickery is required to extract the mouse position from the cell width,
717 depending on wheather we are using full width characters or double line width.
718 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
719 int screen2field_c (int c) {
720 return (c-COL_OFFSET+1 - 2*(op.scheme->cell_width%2))/2 - op.scheme->cell_width/2;
721 }
722 int field2screen_l (int l) {
723 return 0; //TODO: is never used, therefore not implemented
724 }
725 int field2screen_c (int c) {
726 return (op.scheme->cell_width*c+COL_OFFSET - (op.scheme->cell_width%2));
727 }
728
729 enum esc_states {
730 START,
731 ESC_SENT,
732 CSI_SENT,
733 MOUSE_EVENT,
734 };
735 int getctrlseq (unsigned char* buf) {
736 int c;
737 int state = START;
738 int offset = 0x20; /* never sends control chars as data */
739 while ((c = getchar()) != EOF) {
740 switch (state) {
741 case START:
742 switch (c) {
743 case '\033': state=ESC_SENT; break;
744 default: return c;
745 }
746 break;
747 case ESC_SENT:
748 switch (c) {
749 case '[': state=CSI_SENT; break;
750 default: return CTRSEQ_INVALID;
751 }
752 break;
753 case CSI_SENT:
754 switch (c) {
755 case 'M': state=MOUSE_EVENT; break;
756 default: return CTRSEQ_INVALID;
757 }
758 break;
759 case MOUSE_EVENT:
760 buf[0] = c - offset;
761 buf[1] = getchar() - offset;
762 buf[2] = getchar() - offset;
763 return CTRSEQ_MOUSE;
764 default:
765 return CTRSEQ_INVALID;
766 }
767 }
768 return 2;
769 }
770
771 int getch(unsigned char* buf) {
772 /* returns a character, EOF, or constant for an escape/control sequence - NOT
773 compatible with the ncurses implementation of same name */
774 int action = getctrlseq(buf);
775 int l, c;
776 switch (action) {
777 case CTRSEQ_MOUSE:
778 l = screen2field_l (buf[2]);
779 c = screen2field_c (buf[1]);
780
781 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
782 int success = wait_mouse_up(l, c);
783
784 /* mouse moved while pressed: */
785 if (!success) return CTRSEQ_INVALID;
786
787 switch (buf[0]) {
788 case 0: return CTRSEQ_MOUSE_LEFT;
789 case 1: return CTRSEQ_MOUSE_MIDDLE;
790 case 2: return CTRSEQ_MOUSE_RIGHT;
791 }
792 }
793
794 return action;
795 }
796
797 void timer_setup (int enable) {
798 static struct itimerval tbuf;
799 tbuf.it_interval.tv_sec = 1;
800 tbuf.it_interval.tv_usec = 0;
801
802 if (enable) {
803 f.t = time(NULL);
804 tbuf.it_value.tv_sec = 1;
805 tbuf.it_value.tv_usec = 0;
806 if (setitimer(ITIMER_REAL, &tbuf, NULL) == -1) {
807 perror("setitimer");
808 exit(1);
809 }
810 } else {
811 tbuf.it_value.tv_sec = 0;
812 tbuf.it_value.tv_usec = 0;
813 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
814 perror("setitimer");
815 exit(1);
816 }
817 }
818
819 }
Imprint / Impressum