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