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