]> git.gir.st - minesVIiper.git/blob - mines_2017.c
update keybindings in `-h'
[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/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 "Keybindings:\n"
224 " hjkl: move left/down/up/right\n"
225 " bduw: move to next boundary\n"
226 " ^Gg$: move to the left/bottom/top/right\n"
227 " o: open/choord\n"
228 " i: flag/unflag\n"
229 " space:modeful cursor (either open or flag)\n"
230 " a: toggle mode for space (open/flag)\n"
231 " mX: set a mark for letter X\n"
232 " `X: move to mark X (aliased to ')\n"
233 " r: start a new game\n"
234 " q: quit\n", argv[0]);
235 _exit(0);
236 }
237 }
238 /* end parse options*/
239 if (optind < argc) {
240 sscanf (argv[optind], "%dx%dx%d", &(f.w), &(f.h), &(f.m));
241 }
242 /* check boundaries */
243 if (f.m > (f.w-1) * (f.h-1)) {
244 f.m = (f.w-1) * (f.h-1);
245 fprintf (stderr, "too many mines. reduced to %d.\r\n", f.m);
246 }
247 /* end check */
248
249 newgame:
250 f.c = alloc_array (f.h, f.w);
251
252 f.f = 0;
253 f.t = 0;
254 f.p[0] = 0;
255 f.p[1] = 0;
256
257 int is_newgame = 1;
258 int cheatmode = 0;
259 f.s = MODE_OPEN;
260 struct line_col markers[26];
261 for (int i=26; i; markers[--i].l = -1);
262
263 /* switch to alternate screen */
264 printf ("\033[?47h");
265 alt_screen = 1;
266 /* reset cursor, clear screen */
267 printf ("\033[H\033[J");
268
269 /* swich charset, if necessary */
270 if (op.scheme->init_seq != NULL) print (op.scheme->init_seq);
271
272 show_minefield (NORMAL);
273
274 /* enable mouse, hide cursor */
275 printf ("\033[?1000h\033[?25l");
276
277 while (1) {
278 int l, c;
279 int action;
280 unsigned char mouse[3];
281
282 action = getch(mouse);
283 switch (action) {
284 case ' ':
285 if (f.s == MODE_OPEN ||
286 f.c[f.p[0]][f.p[1]].o == OPENED) {
287 switch (do_uncover(&is_newgame)) {
288 case GAME_LOST: goto lose;
289 case GAME_WON: goto win;
290 }
291 } else if (f.s == MODE_FLAG) {
292 flag_square (f.p[0], f.p[1]);
293 } else if (f.s == MODE_QUESM) {
294 quesm_square (f.p[0], f.p[1]);
295 }
296 break;
297 case 'a':
298 f.s = (f.s+1)%(op.mode+1);
299 show_minefield (cheatmode?SHOWMINES:NORMAL);
300 break;
301 case CTRSEQ_MOUSE_LEFT:
302 f.p[0] = screen2field_l (mouse[2]);
303 f.p[1] = screen2field_c (mouse[1]);
304 /* :D clicked: TODO: won't work in single-width mode! */
305 if (mouse[2] == LINE_OFFSET-1 &&
306 (mouse[1] == f.w+COL_OFFSET ||
307 mouse[1] == f.w+COL_OFFSET+1)) {
308 free_field ();
309 goto newgame;
310 }
311 if (f.p[1] < 0 || f.p[1] >= f.w ||
312 f.p[0] < 0 || f.p[0] >= f.h) break; /*out of bound*/
313 /* fallthrough */
314 case 'o':
315 switch (do_uncover(&is_newgame)) {
316 case GAME_LOST: goto lose;
317 case GAME_WON: goto win;
318 }
319 break;
320 case CTRSEQ_MOUSE_RIGHT:
321 f.p[0] = screen2field_l (mouse[2]);
322 f.p[1] = screen2field_c (mouse[1]);
323 if (f.p[1] < 0 || f.p[1] >= f.w ||
324 f.p[0] < 0 || f.p[0] >= f.h) break; /*out of bound*/
325 /* fallthrough */
326 case 'i': flag_square (f.p[0], f.p[1]); break;
327 case '?':quesm_square (f.p[0], f.p[1]); break;
328 case 'h': cursor_move (f.p[0], f.p[1]-1 ); break;
329 case 'j': cursor_move (f.p[0]+1, f.p[1] ); break;
330 case 'k': cursor_move (f.p[0]-1, f.p[1] ); break;
331 case 'l': cursor_move (f.p[0], f.p[1]+1 ); break;
332 case 'w': to_next_boundary (f.p[0], f.p[1], '>'); break;
333 case 'b': to_next_boundary (f.p[0], f.p[1], '<'); break;
334 case 'u': to_next_boundary (f.p[0], f.p[1], '^'); break;
335 case 'd': to_next_boundary (f.p[0], f.p[1], 'v'); break;
336 case '0': /* fallthrough */
337 case '^': cursor_move (f.p[0], 0 ); break;
338 case '$': cursor_move (f.p[0], f.w-1 ); break;
339 case 'g': cursor_move (0, f.p[1] ); break;
340 case 'G': cursor_move (f.h-1, f.p[1] ); break;
341 case 'm':
342 action = tolower(getch(mouse));
343 if (action < 'a' || action > 'z') break;/*out of bound*/
344 markers[action-'a'].l = f.p[0];
345 markers[action-'a'].c = f.p[1];
346 break;
347 case'\'': /* fallthrough */
348 case '`':
349 action = tolower(getch(mouse));
350 if (action < 'a' || action > 'z' /* out of bound or */
351 || markers[action-'a'].l == -1) break; /* unset */
352 cursor_move (markers[action-'a'].l, markers[action-'a'].c);
353 break;
354 case 'r': /* start a new game */
355 free_field ();
356 goto newgame;
357 case 'q':
358 goto quit;
359 case '\014': /* Ctrl-L -- redraw */
360 show_minefield (NORMAL);
361 break;
362 case '\\':
363 if (is_newgame) {
364 is_newgame = 0;
365 fill_minefield (-1, -1);
366 timer_setup(1);
367 }
368 show_minefield (cheatmode?NORMAL:SHOWMINES);
369 cheatmode = !cheatmode;
370 break;
371 }
372 }
373
374 win:
375 lose:
376 timer_setup(0); /* stop timer */
377 show_minefield (SHOWMINES);
378 int gotaction;
379 do {
380 unsigned char mouse[3];
381 gotaction = getch(mouse);
382 /* :D clicked: TODO: won't work in single-width mode! */
383 if (gotaction==CTRSEQ_MOUSE_LEFT && mouse[2]==LINE_OFFSET-1 &&
384 (mouse[1]==f.w+COL_OFFSET || mouse[1]==f.w+COL_OFFSET+1)) {
385 free_field ();
386 goto newgame;
387 } else if (gotaction == 'r') {
388 free_field ();
389 goto newgame;
390 } else if (gotaction == 'q') {
391 goto quit;
392 }
393 } while (1);
394
395 quit:
396 return 0;
397 }
398
399 void quit () {
400 move(0,0); //move(f.h+LINE_OFFSET+2, 0);
401 /* disable mouse, show cursor */
402 printf ("\033[?9l\033[?25h");
403 /* reset charset, if necessary */
404 if (op.scheme && op.scheme->reset_seq) print (op.scheme->reset_seq);
405 /* revert to primary screen */
406 if (alt_screen) printf ("\033[?47l");
407 free_field ();
408 restore_term_mode(saved_term_mode);
409 }
410
411 /* I haven't won as long as a cell exists, that
412 - I haven't opened, and
413 - is not a mine */
414 int everything_opened () {
415 for (int row = 0; row < f.h; row++)
416 for (int col = 0; col < f.w; col++)
417 if (f.c[row][col].o == CLOSED &&
418 f.c[row][col].m == NO_MINE ) return 0;
419 return 1;
420 }
421
422 int wait_mouse_up (int l, int c) {
423 unsigned char mouse2[3];
424 int level = 1;
425 int l2, c2;
426
427 /* show :o face */
428 move (1, field2screen_c (f.w/2)-1); print (":o");
429
430 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
431 /* show a pushed-in button if cursor is on minefield */
432 move (l+LINE_OFFSET, field2screen_c(c));
433 fputs (op.scheme->mouse_highlight, stdout);
434 }
435
436 while (level > 0) {
437 if (getctrlseq (mouse2) == CTRSEQ_MOUSE) {
438 /* ignore mouse wheel events: */
439 if (mouse2[0] & 0x40) continue;
440
441 else if (mouse2[0]&3 == 3) level--; /* release event */
442 else level++; /* another button pressed */
443 }
444 }
445
446 move (1, field2screen_c (f.w/2)-1); print (":D");
447 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
448 partial_show_minefield (l, c, NORMAL);
449 }
450 c2 = screen2field_c(mouse2[1]);
451 l2 = screen2field_l(mouse2[2]);
452 return ((l2 == l) && (c2 == c));
453 }
454
455 int choord_square (int line, int col) {
456 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
457 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
458 if (f.c[l][c].f != FLAG) {
459 if (uncover_square (l, c))
460 return 1;
461 }
462 }
463 }
464
465 return 0;
466 }
467
468 int uncover_square (int l, int c) {
469 f.c[l][c].o = OPENED;
470 f.c[l][c].f = NOFLAG; /*must not be QUESM, otherwise rendering issues*/
471 partial_show_minefield (l, c, NORMAL);
472
473 if (f.c[l][c].m) {
474 f.c[l][c].m = DEATH_MINE;
475 return 1;
476 }
477
478 /* check for chording */
479 if (f.c[l][c].n == 0) {
480 for (int choord_l = -1; choord_l <= 1; choord_l++) {
481 for (int choord_c = -1; choord_c <= 1; choord_c++) {
482 int newl = l + choord_l;
483 int newc = c + choord_c;
484 if (newl >= 0 && newl < f.h &&
485 newc >= 0 && newc < f.w &&
486 f.c[newl][newc].o == CLOSED &&
487 uncover_square (newl, newc)) {
488 return 1;
489 }
490 }
491 }
492 }
493
494 return 0;
495 }
496
497 void flag_square (int l, int c) {
498 if (f.c[l][c].o != CLOSED) return;
499 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
500 are allowed) */
501 f.c[l][c].f = (f.c[l][c].f + 1) % (op.mode + 1);
502 if (f.c[l][c].f==FLAG) f.f++;
503 else f.f--; //WARN: breaks on `-q'!
504 partial_show_minefield (l, c, NORMAL);
505 move (1, op.scheme->cell_width);
506 printf ("[%03d]", f.f);
507 }
508
509 void quesm_square (int l, int c) {
510 /* toggle question mark / none. won't turn flags into question marks.
511 unlike flag_square, this function doesn't respect `-q'. */
512 if (f.c[l][c].o != CLOSED) return;
513 else if (f.c[l][c].f == NOFLAG) f.c[l][c].f = QUESM;
514 else if (f.c[l][c].f == QUESM) f.c[l][c].f = NOFLAG;
515 partial_show_minefield (l, c, NORMAL);
516 }
517
518 int do_uncover (int* is_newgame) {
519 if (*is_newgame) {
520 *is_newgame = 0;
521 fill_minefield (f.p[0], f.p[1]);
522 timer_setup(1);
523 }
524
525 if (f.c[f.p[0]][f.p[1]].f == FLAG ) return GAME_INPROGRESS;
526 if (f.c[f.p[0]][f.p[1]].o == CLOSED) {
527 if (uncover_square (f.p[0], f.p[1])) return GAME_LOST;
528 } else if (get_neighbours (f.p[0], f.p[1], 1) == 0) {
529 if (choord_square (f.p[0], f.p[1])) return GAME_LOST;
530 }
531 if (everything_opened()) return GAME_WON;
532
533 return GAME_INPROGRESS;
534 }
535
536 void fill_minefield (int l, int c) {
537 srand (time(0));
538 int mines_set = f.m;
539 while (mines_set) {
540 int line = rand() % f.h;
541 int col = rand() % f.w;
542
543 if (f.c[line][col].m) {
544 /* skip if field already has a mine */
545 continue;
546 } else if ((line == l) && (col == c)) {
547 /* don't put a mine on the already opened (first click) field */
548 continue;
549 } else {
550 mines_set--;
551 f.c[line][col].m = STD_MINE;
552 }
553 }
554
555 /* precalculate neighbours */
556 for (int l=0; l < f.h; l++)
557 for (int c=0; c < f.w; c++)
558 f.c[l][c].n = get_neighbours (l, c, NORMAL);
559 }
560
561 void move (int line, int col) {
562 printf ("\033[%d;%dH", line+1, col+1);
563 }
564
565 /* absolute coordinates! */
566 void cursor_move (int l, int c) {
567 partial_show_minefield (f.p[0], f.p[1], NORMAL);
568 /* update f.p */
569 f.p[0] = CLAMP(l, 0, f.h-1);
570 f.p[1] = CLAMP(c, 0, f.w-1);
571 move (f.p[0]+LINE_OFFSET, field2screen_c(f.p[1]));
572 //fputs (op.scheme->mouse_highlight, stdout);
573
574 if (!f.c[f.p[0]][f.p[1]].f) print("\033[7m");//invert unless ! or ?
575 partial_show_minefield (f.p[0], f.p[1], HIGHLIGHT);
576 print("\033[0m");//un-invert
577 }
578
579 /* to_next_boundary(): move into the supplied direction until a change in open-
580 state or flag-state is found and move there. falls back to BIG_MOVE. */
581 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
582 new_ ## X OP ## = BIG_MOVE;\
583 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
584 if ((f.c[L ][C ].o<<2 + f.c[L ][C ].f) \
585 != (f.c[L1][C1].o<<2 + f.c[L1][C1].f)) {\
586 new_ ## X = i OP 1;\
587 break;\
588 }\
589 } while(0)
590 void to_next_boundary (int l, int c, char direction) {
591 int new_l = l;
592 int new_c = c;
593 switch (direction) {
594 case '>': FIND_NEXT(c, l, i, l, i+1, w, +); break;
595 case '<': FIND_NEXT(c, l, i, l, i-1, w, -); break;
596 case '^': FIND_NEXT(l, i, c, i-1, c, h, -); break;
597 case 'v': FIND_NEXT(l, i, c, i+1, c, h, +); break;
598 }
599
600 cursor_move (new_l, new_c);
601 }
602 #undef FIND_NEXT
603
604 char* cell2schema (int l, int c, int mode) {
605 struct minecell cell = f.c[l][c];
606 /* move past invert-ctrlsequence when highlighting the cursor: */
607 int offset = ((mode==HIGHLIGHT)*op.scheme->flag_offset);
608
609 if (mode == SHOWMINES) return (
610 cell.f == FLAG && cell.m ? op.scheme->field_flagged+offset:
611 cell.f == FLAG && !cell.m ? op.scheme->mine_wrongf+offset:
612 cell.m == STD_MINE ? op.scheme->mine_normal:
613 cell.m == DEATH_MINE ? op.scheme->mine_death:
614 cell.o == CLOSED ? op.scheme->field_closed:
615 /*.......................*/ op.scheme->number[f.c[l][c].n]);
616 else return (
617 cell.f == FLAG ? op.scheme->field_flagged+offset:
618 cell.f == QUESM ? op.scheme->field_question+offset:
619 cell.o == CLOSED ? op.scheme->field_closed:
620 cell.m == STD_MINE ? op.scheme->mine_normal:
621 cell.m == DEATH_MINE ? op.scheme->mine_death:
622 /*.......................*/ op.scheme->number[f.c[l][c].n]);
623 }
624
625 void partial_show_minefield (int l, int c, int mode) {
626 move (l+LINE_OFFSET, field2screen_c(c));
627
628 print (cell2schema(l, c, mode));
629 }
630
631 void show_minefield (int mode) {
632 int dtime;
633 static char modechar[] = {'*', '!', '?'};
634
635 move (0,0);
636
637 if (f.t == 0) {
638 dtime = 0;
639 } else {
640 dtime = difftime (time(NULL), f.t);
641 }
642
643 /* first line */
644 print (op.scheme->border_top_l);
645 printm (f.w*op.scheme->cell_width,op.scheme->border_top_m);
646 printf ("%s\r\n", op.scheme->border_top_r);
647 /* second line */
648 print (op.scheme->border_status_l);
649 printf("[%03d]", f.f);
650 printm (f.w*op.scheme->cell_width/2-6, " ");
651 printf ("%s", mode==SHOWMINES?":C":":D");
652 printm (f.w*op.scheme->cell_width/2-6-4, " ");
653 printf ("[%c] [%03d]", modechar[f.s], dtime);
654 print (op.scheme->border_status_r);
655 print ("\r\n");
656 /* third line */
657 print (op.scheme->border_spacer_l);
658 printm (f.w*op.scheme->cell_width,op.scheme->border_spacer_m);
659 print (op.scheme->border_spacer_r);
660 print ("\r\n");
661 /* main body */
662 for (int l = 0; l < f.h; l++) {
663 print (op.scheme->border_field_l);
664 for (int c = 0; c < f.w; c++) {
665 print (cell2schema(l, c, mode));
666 }
667 print (op.scheme->border_field_r); print ("\r\n");
668 }
669 /* last line */
670 print (op.scheme->border_bottom_l);
671 printm (f.w*op.scheme->cell_width,op.scheme->border_bottom_m);
672 print (op.scheme->border_bottom_r);
673 print ("\r\n");
674 }
675
676 int get_neighbours (int line, int col, int reduced_mode) {
677 /* counts mines surrounding a square
678 modes: 0=normal; 1=reduced */
679
680 int count = 0;
681
682 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
683 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
684 if (!l && !c) continue;
685
686 count += !!f.c[l][c].m;
687 count -= reduced_mode * f.c[l][c].f==FLAG;
688 }
689 }
690 return count;
691 }
692
693 struct minecell** alloc_array (int lines, int cols) {
694 struct minecell** a = malloc (lines * sizeof(struct minecell*));
695 if (a == NULL) return NULL;
696 for (int l = 0; l < lines; l++) {
697 a[l] = calloc (cols, sizeof(struct minecell));
698 if (a[l] == NULL) goto unalloc;
699 }
700
701 return a;
702 unalloc:
703 for (int l = 0; l < lines; l++)
704 free (a[l]);
705 return NULL;
706 }
707
708 void free_field () {
709 if (f.c == NULL) return;
710 for (int l = 0; l < f.h; l++) {
711 free (f.c[l]);
712 }
713 free (f.c);
714 }
715
716 int screen2field_l (int l) {
717 return (l-LINE_OFFSET) - 1;
718 }
719 /* some trickery is required to extract the mouse position from the cell width,
720 depending on wheather we are using full width characters or double line width.
721 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
722 int screen2field_c (int c) {
723 return (c-COL_OFFSET+1 - 2*(op.scheme->cell_width%2))/2 - op.scheme->cell_width/2;
724 }
725 int field2screen_l (int l) {
726 return 0; //TODO: is never used, therefore not implemented
727 }
728 int field2screen_c (int c) {
729 return (op.scheme->cell_width*c+COL_OFFSET - (op.scheme->cell_width%2));
730 }
731
732 enum esc_states {
733 START,
734 ESC_SENT,
735 CSI_SENT,
736 MOUSE_EVENT,
737 };
738 int getctrlseq (unsigned char* buf) {
739 int c;
740 int state = START;
741 int offset = 0x20; /* never sends control chars as data */
742 while ((c = getchar()) != EOF) {
743 switch (state) {
744 case START:
745 switch (c) {
746 case '\033': state=ESC_SENT; break;
747 default: return c;
748 }
749 break;
750 case ESC_SENT:
751 switch (c) {
752 case '[': state=CSI_SENT; break;
753 default: return CTRSEQ_INVALID;
754 }
755 break;
756 case CSI_SENT:
757 switch (c) {
758 case 'M': state=MOUSE_EVENT; break;
759 default: return CTRSEQ_INVALID;
760 }
761 break;
762 case MOUSE_EVENT:
763 buf[0] = c - offset;
764 buf[1] = getchar() - offset;
765 buf[2] = getchar() - offset;
766 return CTRSEQ_MOUSE;
767 default:
768 return CTRSEQ_INVALID;
769 }
770 }
771 return 2;
772 }
773
774 int getch(unsigned char* buf) {
775 /* returns a character, EOF, or constant for an escape/control sequence - NOT
776 compatible with the ncurses implementation of same name */
777 int action = getctrlseq(buf);
778 int l, c;
779 switch (action) {
780 case CTRSEQ_MOUSE:
781 l = screen2field_l (buf[2]);
782 c = screen2field_c (buf[1]);
783
784 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
785 int success = wait_mouse_up(l, c);
786
787 /* mouse moved while pressed: */
788 if (!success) return CTRSEQ_INVALID;
789
790 switch (buf[0]) {
791 case 0: return CTRSEQ_MOUSE_LEFT;
792 case 1: return CTRSEQ_MOUSE_MIDDLE;
793 case 2: return CTRSEQ_MOUSE_RIGHT;
794 }
795 }
796
797 return action;
798 }
799
800 void timer_setup (int enable) {
801 static struct itimerval tbuf;
802 tbuf.it_interval.tv_sec = 1;
803 tbuf.it_interval.tv_usec = 0;
804
805 if (enable) {
806 f.t = time(NULL);
807 tbuf.it_value.tv_sec = 1;
808 tbuf.it_value.tv_usec = 0;
809 if (setitimer(ITIMER_REAL, &tbuf, NULL) == -1) {
810 perror("setitimer");
811 exit(1);
812 }
813 } else {
814 tbuf.it_value.tv_sec = 0;
815 tbuf.it_value.tv_usec = 0;
816 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
817 perror("setitimer");
818 exit(1);
819 }
820 }
821
822 }
Imprint / Impressum