]> git.gir.st - solVItaire.git/blob - sol.c
rewrite join() prediction engine
[solVItaire.git] / sol.c
1 #define _DEFAULT_SOURCE /* for getopt, sigaction, usleep */
2 #include <poll.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/ioctl.h>
7 #include <time.h>
8 #include <termios.h>
9 #include <unistd.h>
10
11 #include "sol.h"
12 #include "schemes.h"
13
14 struct playfield f;
15 struct opts op;
16
17 // action table {{{
18 /* stores a function pointer for every takeable action; called by game loop */
19 int (*action[NUM_PLACES][10])(int,int,int) = {
20 #ifdef KLONDIKE
21 /* 1 2 3 4 5 6 7 stk wst fnd*/
22 /* 1 */ { t2f, t2t, t2t, t2t, t2t, t2t, t2t, nop, nop, t2f },
23 /* 2 */ { t2t, t2f, t2t, t2t, t2t, t2t, t2t, nop, nop, t2f },
24 /* 3 */ { t2t, t2t, t2f, t2t, t2t, t2t, t2t, nop, nop, t2f },
25 /* 4 */ { t2t, t2t, t2t, t2f, t2t, t2t, t2t, nop, nop, t2f },
26 /* 5 */ { t2t, t2t, t2t, t2t, t2f, t2t, t2t, nop, nop, t2f },
27 /* 6 */ { t2t, t2t, t2t, t2t, t2t, t2f, t2t, nop, nop, t2f },
28 /* 7 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2f, nop, nop, t2f },
29 /*stk*/ { nop, nop, nop, nop, nop, nop, nop, nop, s2w, nop },
30 /*wst*/ { w2t, w2t, w2t, w2t, w2t, w2t, w2t, w2s, w2f, w2f },
31 /*fnd*/ { f2t, f2t, f2t, f2t, f2t, f2t, f2t, nop, nop, nop },
32 #elif defined SPIDER
33 /* 1 2 3 4 5 6 7 8 9 10*/
34 /* 1 */ { t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
35 /* 2 */ { t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
36 /* 3 */ { t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
37 /* 4 */ { t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t },
38 /* 5 */ { t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t },
39 /* 6 */ { t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t },
40 /* 7 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t },
41 /* 8 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t },
42 /* 9 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t },
43 /*10 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f },
44 /*stk*/ { s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t },
45 #endif
46 };
47 // }}}
48
49 // argv parsing, game loops, cleanup {{{
50 int main(int argc, char** argv) {
51 /* opinionated defaults: */
52 op.s = &unicode_large_color;
53 #ifdef SPIDER
54 op.m = MEDIUM;
55 #endif
56
57 int optget;
58 opterr = 0; /* don't print message on unrecognized option */
59 while ((optget = getopt (argc, argv, "+:hs:vbcm")) != -1) {
60 switch (optget) {
61 #ifdef SPIDER
62 case 's': /* number of suits */
63 switch (optarg[0]) {
64 case '1': op.m = EASY; break;
65 case '2': op.m = MEDIUM; break;
66 case '4': op.m = NORMAL; break;
67 default: goto error;
68 } break;
69 #endif
70 case 'b': op.s = &unicode_large_mono; break;
71 case 'c': op.s = &unicode_large_color; break;
72 case 'm': op.s = &unicode_small_mono; break; /* "mini" */
73 case 'h': default: goto error;
74 error:
75 fprintf (stderr, SHORTHELP LONGHELP KEYHELP, argv[0]);
76 return optget != 'h';
77 }
78 }
79
80 signal_setup();
81 atexit (*quit);
82
83 signal_handler(SIGWINCH); /* initialize window size */
84
85 newgame:
86 screen_setup(1);
87
88 switch(sol()) {
89 case GAME_NEW: goto newgame;
90 case GAME_WON:
91 print_table(NO_HI, NO_HI);
92 win_anim();
93 if (getch(NULL)=='q') return 0;
94 goto newgame;
95 case GAME_QUIT: return 0;
96 }
97 }
98
99 int sol(void) {
100 int ret;
101 long seed = time(NULL);
102 restart:
103 free_undo(f.u);
104 deal(seed);
105
106 int from, to, opt;
107 for(;;) {
108 switch (get_cmd(&from, &to, &opt)) {
109 case CMD_MOVE:
110 ret = action[from][to](from,to,opt);
111 if (ret == ERR) /* try again with from/to swapped: */
112 ret = action[to][from](to,from,opt);
113 switch (ret) {
114 case OK: break;
115 case ERR: visbell(); break;
116 case WON: return GAME_WON;
117 }
118 break;
119 case CMD_JOIN:
120 switch (join(to)) {
121 case OK: break;
122 case ERR: visbell(); break;
123 case WON: return GAME_WON;
124 }
125 break;
126 case CMD_HINT: break;//TODO: show a possible (and sensible) move. if possible, involve active cursor
127 case CMD_UNDO: undo_pop(f.u); break;
128 case CMD_INVAL: visbell(); break;
129 case CMD_NEW: return GAME_NEW;
130 case CMD_AGAIN: goto restart;
131 case CMD_QUIT: return GAME_QUIT;
132 }
133 }
134 }
135
136 void quit(void) {
137 screen_setup(0);
138 free_undo(f.u);
139 }
140 //}}}
141
142 // card games helper functions {{{
143 #define get_suit(card) \
144 ((card-1) % NUM_SUITS)
145 #define get_rank(card) \
146 ((card-1) / NUM_SUITS)
147 #define get_color(card) \
148 ((get_suit(card) ^ get_suit(card)>>1) & 1)
149
150 #define is_tableu(where) (where <= TAB_MAX)
151
152 int find_top(card_t* pile) {
153 int i;
154 for(i=PILE_SIZE-1; i>=0 && !pile[i]; i--);
155 return i;
156 }
157 int first_movable(card_t* pile) {
158 int i = 0;
159 for (;pile[i] && !is_movable(pile, i); i++);
160 return i;
161 }
162 int turn_over(card_t* pile) {
163 int top = find_top(pile);
164 if (pile[top] < 0) {
165 pile[top] *= -1;
166 return 1;
167 } else return 0;
168 }
169 int check_won(void) {
170 for (int pile = 0; pile < NUM_DECKS*NUM_SUITS; pile++)
171 if (f.f[pile][NUM_RANKS-1] == NO_CARD) return 0;
172
173 return 1;
174 }
175 int rank_next (card_t a, card_t b) {
176 return get_rank(a) == get_rank(b)-1;
177 }
178 int is_consecutive (card_t* pile, int pos) {
179 if (pos+1 >= PILE_SIZE) return 1; /* card is last */
180 if (pile[pos+1] == NO_CARD) return 1; /* card is first */
181
182 #ifdef KLONDIKE
183 /* ranks consecutive? */
184 if (!rank_next(pile[pos+1], pile[pos])) return 0;
185 /* color opposite? */
186 if (get_color(pile[pos+1]) == get_color(pile[pos])) return 0;
187 #elif defined SPIDER
188 /* ranks consecutive? */
189 if (!rank_next(pile[pos+1], pile[pos])) return 0;
190 /* same suit? */
191 if (get_suit(pile[pos+1]) != get_suit(pile[pos])) return 0;
192 #endif
193
194 return 1;
195 }
196
197 int is_movable(card_t* pile, int n) {
198 #ifdef KLONDIKE
199 return(pile[n] > NO_CARD); /*non-movable cards don't exist in klondike*/
200 #elif defined SPIDER
201 int top = find_top(pile);
202 for (int i = top; i >= 0; i--) {
203 if (pile[i] <= NO_CARD) return 0; /*no card or card face down?*/
204 if (!is_consecutive(pile, i)) return 0;
205 if (i == n) return 1; /* card reached, must be movable */
206 }
207 return 0;
208 #endif
209 }
210 //}}}
211
212 // takeable actions {{{
213 #ifdef KLONDIKE
214 card_t stack_take(void) { /*NOTE: assert(f.w >= 0) */
215 card_t card = f.s[f.w];
216 /* move stack one over, so there are no gaps in it: */
217 for (int i = f.w; i < f.z-1; i++)
218 f.s[i] = f.s[i+1];
219 f.z--;
220 f.w--; /* make previous card visible again */
221 return card;
222 }
223 int t2f(int from, int to, int opt) { /* tableu to foundation */
224 (void) to; (void) opt; /* don't need */
225 int top_from = find_top(f.t[from]);
226 to = get_suit(f.t[from][top_from]);
227 int top_to = find_top(f.f[to]);
228 if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
229 || (top_to >= 0 && rank_next(f.f[to][top_to],f.t[from][top_from]))) {
230 f.f[to][top_to+1] = f.t[from][top_from];
231 f.t[from][top_from] = NO_CARD;
232 undo_push(from, FOUNDATION, to,
233 turn_over(f.t[from]));
234 if (check_won()) return WON;
235 return OK;
236 } else return ERR;
237 }
238 int w2f(int from, int to, int opt) { /* waste to foundation */
239 (void) from; (void) to; (void) opt; /* don't need */
240 if (f.w < 0) return ERR;
241 to = get_suit(f.s[f.w]);
242 int top_to = find_top(f.f[to]);
243 if ((top_to < 0 && get_rank(f.s[f.w]) == RANK_A)
244 || (top_to >= 0 && rank_next(f.f[to][top_to], f.s[f.w]))) {
245 undo_push(WASTE, FOUNDATION, f.w | to<<16, 0);//ugly encoding :|
246 f.f[to][top_to+1] = stack_take();
247 if (check_won()) return WON;
248 return OK;
249 } else return ERR;
250
251 }
252 int s2w(int from, int to, int opt) { /* stock to waste */
253 (void) from; (void) to; (void) opt; /* don't need */
254 if (f.z == 0) return ERR;
255 f.w++;
256 if (f.w == f.z) f.w = -1;
257 return OK;
258 }
259 int w2s(int from, int to, int opt) { /* waste to stock (undo stock to waste) */
260 (void) from; (void) to; (void) opt; /* don't need */
261 if (f.z == 0) return ERR;
262 f.w--;
263 if (f.w < -1) f.w = f.z-1;
264 return OK;
265 }
266 int f2t(int from, int to, int opt) { /* foundation to tableu */
267 (void) from; /* don't need */
268 int top_to = find_top(f.t[to]);
269 from = opt;
270 int top_from = find_top(f.f[from]);
271
272 if ((get_color(f.t[to][top_to]) != get_color(f.f[from][top_from]))
273 && (rank_next(f.f[from][top_from], f.t[to][top_to]))) {
274 f.t[to][top_to+1] = f.f[from][top_from];
275 f.f[from][top_from] = NO_CARD;
276 undo_push(FOUNDATION, to, from, 0);
277 return OK;
278 } else return ERR;
279 }
280 int w2t(int from, int to, int opt) { /* waste to tableu */
281 (void) from; (void) opt; /* don't need */
282 int top_to = find_top(f.t[to]);
283 if (((get_color(f.t[to][top_to]) != get_color(f.s[f.w]))
284 && (rank_next(f.s[f.w], f.t[to][top_to])))
285 || (top_to < 0 && get_rank(f.s[f.w]) == RANK_K)) {
286 undo_push(WASTE, to, f.w, 0);
287 f.t[to][top_to+1] = stack_take();
288 return OK;
289 } else return ERR;
290 }
291 int t2t(int from, int to, int opt) { /* tableu to tableu */
292 (void) opt; /* don't need */
293 int top_to = find_top(f.t[to]);
294 int top_from = find_top(f.t[from]);
295 int count = 0; //NOTE: could probably be factored out
296 for (int i = top_from; i >=0; i--) {
297 if (((get_color(f.t[to][top_to]) != get_color(f.t[from][i]))
298 && (rank_next(f.t[from][i], f.t[to][top_to]))
299 && f.t[from][i] > NO_CARD) /* card face up? */
300 || (top_to < 0 && get_rank(f.t[from][i]) == RANK_K)) {
301 /* move cards [i..top_from] to their destination */
302 for (;i <= top_from; i++) {
303 top_to++;
304 f.t[to][top_to] = f.t[from][i];
305 f.t[from][i] = NO_CARD;
306 count++;
307 }
308 undo_push(from, to, count,
309 turn_over(f.t[from]));
310 return OK;
311 }
312 }
313 return ERR; /* no such move possible */
314 }
315 #elif defined SPIDER
316 int remove_if_complete (int pileno) { //cleanup!
317 card_t* pile = f.t[pileno];
318 /* test if K...A complete; move to foundation if so */
319 int top_from = find_top(pile);
320 if (get_rank(pile[top_from]) != RANK_A) return 0;
321 for (int i = top_from; i>=0; i--) {
322 if (!is_consecutive (pile, i)) return 0;
323 if (i+RANK_K == top_from /* if ace to king: remove it */
324 && get_rank(pile[top_from-RANK_K]) == RANK_K) {
325 for(int i=top_from, j=0; i>top_from-NUM_RANKS; i--,j++){
326 f.f[f.w][j] = pile[i];
327 pile[i] = NO_CARD;
328 }
329 undo_push(pileno, FOUNDATION, f.w,
330 turn_over(pile));
331 f.w++;
332 return 1;
333 }
334 }
335
336 return 0;
337 }
338 int t2t(int from, int to, int opt) { //in dire need of cleanup
339 int top_from = find_top(f.t[from]);
340 int top_to = find_top(f.t[to]);
341 int empty_to = (top_to < 0)? opt: -1; /* empty pile? */
342 int count = 0; //NOTE: could probably be factored out
343
344 for (int i = top_from; i >= 0; i--) {
345 if (!is_consecutive(f.t[from], i)) break;
346
347 /* is consecutive OR to empty pile and rank ok? */
348 if (rank_next(f.t[from][i], f.t[to][top_to])
349 || (empty_to >= RANK_A && get_rank(f.t[from][i]) == empty_to)) {
350 for (;i <= top_from; i++) {
351 top_to++;
352 f.t[to][top_to] = f.t[from][i];
353 f.t[from][i] = NO_CARD;
354 count++;
355 }
356 undo_push(from, to, count,
357 turn_over(f.t[from]));
358 remove_if_complete(to);
359 if (check_won()) return WON;
360 return OK;
361 }
362 }
363
364 return ERR; /* no such move possible */
365 }
366 int s2t(int from, int to, int opt) {
367 (void) from; (void) to; (void) opt; /* don't need */
368 if (f.z <= 0) return ERR; /* stack out of cards */
369 for (int pile = 0; pile < NUM_PILES; pile++)
370 if (f.t[pile][0]==NO_CARD) return ERR; /*no piles may be empty*/
371 for (int pile = 0; pile < NUM_PILES; pile++) {
372 f.t[pile][find_top(f.t[pile])+1] = f.s[--f.z];
373 remove_if_complete(pile);
374 if (check_won()) return WON;
375 }
376 undo_push(STOCK, TABLEU, 1, 0); /*NOTE: puts 1 card on each tableu pile*/
377 return OK;
378 }
379 int t2f(int from, int to, int opt) {
380 (void) to; (void) opt; /* don't need */
381 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
382 return remove_if_complete(from)?OK:ERR;
383 }
384 #endif
385 //TODO: which pile to take from should form the basis of CMD_HINT
386 #ifdef KLONDIKE
387 #define would_complete(pile) 0
388 #elif defined SPIDER
389 #define would_complete(pile) \
390 (get_rank(f.t[pile][r[pile].top]) == RANK_A \
391 && get_rank(f.t[to][bottom_to]) == RANK_K)
392 #endif
393 #define would_turn(pile) \
394 (f.t[pile][r[pile].pos-1] < 0)
395 #define would_empty(pile) \
396 (r[pile].pos == 0) //TODO: check not currently empty
397
398 int join(int to) {
399 int top_to = find_top(f.t[to]);
400 #ifdef SPIDER
401 int bottom_to = first_movable(f.t[to]);
402 #endif
403
404 #ifdef KLONDIKE
405 if (to == FOUNDATION) {
406 int status = ERR;
407 for (int i = 0; i <= TAB_MAX; i++)
408 switch ((i?t2f:w2f)(i-1, FOUNDATION, 0)) {
409 case WON: return WON;
410 case OK: status = OK;
411 case ERR: /* nop */;
412 }
413 return status;
414 }
415
416 if (top_to < 0) { /* move a king to empty pile: */
417 for (int i = 0; i < TAB_MAX; i++) {
418 if (f.t[i][0] < 0) /* i.e. would turn? */
419 if (t2t(i, to, 0) == OK) return OK;
420 }
421 return w2t(WASTE, to, 0);
422 }
423 #endif
424
425 struct rating {
426 int ok:1; /* card to move in pile? */
427 int above; /* number of movable cards above */
428 int below; /* number of cards below ours */
429 int pos; /* where the card to move is in the pile */
430 int top; /* find_top() */
431 } r[NUM_PILES] = {{0}};
432 int complete = 0;/* SPIDER: true if any pile would complete a stack */
433 int turn = 0; /* SPIDER: true if any pile would turn_over */
434 int empty = 0; /* true if any pile would become empty */
435
436 /* 1. rate each pile: */
437 #ifdef SPIDER
438 if (top_to < 0) {
439 for (int pile = 0; pile < NUM_PILES; pile++) {
440 if (pile == to) continue;
441 int top = find_top(f.t[pile]);
442 int bottom = first_movable(f.t[pile]);
443
444 if (top < 0) continue; /* no cards to move */
445 if (would_empty(pile)) continue; /* doesn't help */ //TODO: should be in step2
446
447 r[pile].ok++;
448 r[pile].above = 0; /* always take as many as possible */
449 r[pile].below = top - bottom;
450 r[pile].pos = bottom;
451 r[pile].top = top;
452 complete |= would_complete(pile); /* never happens */
453 turn |= would_turn(pile);
454 empty |= would_empty(pile);
455 }
456 } else
457 #endif
458 for (int pile = 0; pile < NUM_PILES; pile++) {
459 r[pile].top = r[pile].pos = find_top(f.t[pile]);
460 /* backtrack until we find a compatible-to-'to'-pile card: */
461 while (r[pile].pos >= 0 && is_movable(f.t[pile], r[pile].pos)) {
462 int rankdiff = get_rank(f.t[pile][r[pile].pos])
463 - get_rank(f.t[to][top_to]);
464 if (rankdiff >= 0) break; /* past our card */
465 if (rankdiff == -1 /* rank matches */
466 #ifdef KLONDIKE
467 && get_color(f.t[pile][r[pile].pos]) /* color OK */
468 != get_color(f.t[to][top_to])
469 #elif defined SPIDER
470 && get_suit(f.t[pile][r[pile].pos]) /* color OK */
471 == get_suit(f.t[to][top_to])
472 #endif
473 ) {
474 r[pile].ok++;
475 complete |= would_complete(pile);
476 turn |= would_turn(pile);
477 empty |= would_empty(pile);
478 for (int i = r[pile].pos; i >= 0; i--)
479 if (is_movable(f.t[pile], i-1))
480 r[pile].above++;
481 else break;
482 break;
483 }
484 r[pile].pos--;
485 r[pile].below++;
486 }
487 }
488
489 /* 2. find optimal pile: (optimized for spider) */
490 int from = -1;
491 for (int pile = 0, above = 99, below = 99; pile < NUM_PILES; pile++) {
492 if (!r[pile].ok) continue;
493 /* don't bother if another pile would be better: */
494 if (complete && !would_complete(pile)) continue; //prefer would-complete
495 if (empty && !would_empty(pile) && !complete) continue; //prefer would-become-empty
496 if (turn && !would_turn(pile) && !complete && !empty) continue; //prefer would-turn_over
497 if (r[pile].above > above) continue; //prefer to rip off least amount of cards above
498 if (r[pile].above == above //if tied, prefer ...
499 && (top_to < 0? r[pile].below < below //... larger pile if to==empty
500 : r[pile].below > below)) //... shorter pile otherwise
501 continue;
502
503 from = pile;
504 above = r[pile].above;
505 below = r[pile].below;
506 }
507
508 /* 3. move cards over and return: */
509 #ifdef KLONDIKE
510 /* prefer waste if it wouldn't turn_over: */
511 if (!turn && w2t(WASTE, to, 0) == OK)
512 return OK;
513 if (from < 0) /* nothing found */
514 return ERR;
515 return t2t(from, to, 0);
516 #elif defined SPIDER
517 if (from < 0) /* nothing found */
518 return ERR;
519 int bottom = first_movable(f.t[from]);
520 return t2t(from, to, get_rank(f.t[from][bottom]));
521 #endif
522 }
523 #undef would_empty
524 #undef would_turn
525 #undef would_complete
526 int nop(int from, int to, int opt) { (void)from;(void)to;(void)opt;return ERR; }
527 // }}}
528
529 // keyboard input handling {{{
530 // cursor functions{{{
531 #ifdef KLONDIKE
532 void cursor_left (struct cursor* cursor) {
533 if (is_tableu(cursor->pile)) {
534 if (cursor->pile > 0) cursor->pile--;
535 cursor->opt = 0;
536 } else { /* stock/waste/foundation*/
537 switch (cursor->pile) {
538 case WASTE: cursor->pile = STOCK; cursor->opt = 0; break;
539 case FOUNDATION:
540 if (cursor->opt <= 0)
541 cursor->pile = WASTE;
542 else
543 cursor->opt--;
544 }
545 }
546 }
547 void cursor_down (struct cursor* cursor) {
548 if (!is_tableu(cursor->pile)) {
549 switch (cursor->pile) {
550 case STOCK: cursor->pile = TAB_1; break;
551 case WASTE: cursor->pile = TAB_2; break;
552 case FOUNDATION:
553 cursor->pile = TAB_4 + cursor->opt;
554 }
555 cursor->opt = 0;
556 }
557 }
558 void cursor_up (struct cursor* cursor) {
559 if (is_tableu(cursor->pile)) {
560 switch (cursor->pile) { //ugly :|
561 case TAB_1: cursor->pile = STOCK; break;
562 case TAB_2: cursor->pile = WASTE; break;
563 case TAB_3: cursor->pile = WASTE; break;
564 case TAB_4: case TAB_5: case TAB_6: case TAB_7:
565 cursor->opt=cursor->pile-TAB_4;
566 cursor->pile = FOUNDATION;
567 break;
568 }
569 }
570 }
571 void cursor_right (struct cursor* cursor) {
572 if (is_tableu(cursor->pile)) {
573 if (cursor->pile < TAB_MAX) cursor->pile++;
574 } else {
575 switch (cursor->pile) {
576 case STOCK: cursor->pile = WASTE; break;
577 case WASTE: cursor->pile = FOUNDATION;cursor->opt = 0; break;
578 case FOUNDATION:
579 if (cursor->opt < NUM_SUITS-1)
580 cursor->opt++;
581 }
582 }
583 }
584 #elif defined SPIDER
585 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
586 void cursor_left (struct cursor* cursor) {
587 if (cursor->pile > 0) cursor->pile--;
588 cursor->opt = 0;
589 }
590 void cursor_down (struct cursor* cursor) {
591 int first = first_movable(f.t[cursor->pile]);
592 int top = find_top(f.t[cursor->pile]);
593 if (first + cursor->opt < top)
594 cursor->opt++;
595 }
596 void cursor_up (struct cursor* cursor) {
597 if (cursor->opt > 0) cursor->opt--;
598 }
599 void cursor_right (struct cursor* cursor) {
600 if (cursor->pile < TAB_MAX) cursor->pile++;
601 cursor->opt = 0;
602 }
603 #endif
604 void cursor_to (struct cursor* cursor, int pile) {
605 cursor->pile = pile;
606 cursor->opt = 0;
607 }
608 //}}}
609 int get_cmd (int* from, int* to, int* opt) {
610 /*XXX*/unsigned char mouse[3];
611 //TODO: escape sequences (mouse, cursor keys)
612 int _f, t;
613 struct cursor inactive = {-1,-1};
614 static struct cursor active = {0,0};
615 active.opt = 0; /* always reset offset, but keep pile */
616
617 /***/
618 from_l: print_table(&active, &inactive);
619 _f = getch(mouse);
620
621 switch (_f) {
622 /* direct addressing: */
623 case '1': *from = TAB_1; break;
624 case '2': *from = TAB_2; break;
625 case '3': *from = TAB_3; break;
626 case '4': *from = TAB_4; break;
627 case '5': *from = TAB_5; break;
628 case '6': *from = TAB_6; break;
629 case '7': *from = TAB_7; break;
630 #ifdef SPIDER
631 case '8': *from = TAB_8; break;
632 case '9': *from = TAB_9; break;
633 case '0': *from = TAB_10;break;
634 #elif defined KLONDIKE
635 case '9': *from = WASTE; break;
636 case '0': *from = FOUNDATION; break;
637 case '8': /* fallthrough */
638 #endif
639 case '\n': /* shortcut for dealing from stock */
640 *from = STOCK;
641 *to = WASTE;
642 return CMD_MOVE;
643 /* cursor keys addressing: */
644 case KEY_LEFT:
645 case 'h': cursor_left (&active); goto from_l;
646 case KEY_DOWN:
647 case 'j': cursor_down (&active); goto from_l;
648 case KEY_UP:
649 case 'k': cursor_up (&active); goto from_l;
650 case KEY_RIGHT:
651 case 'l': cursor_right(&active); goto from_l;
652 case KEY_HOME:
653 case 'H': cursor_to(&active,TAB_1); goto from_l; /* leftmost tableu */
654 case KEY_END:
655 case 'L': cursor_to(&active,TAB_MAX);goto from_l; /* rigthmost tableu */
656 case KEY_INS:
657 case 'M': cursor_to(&active,TAB_MAX/2); goto from_l; /* center tableu */
658 //TODO: real cursor keys, home/end
659 case ' ': /* continue with second cursor */
660 *from = active.pile;
661 if (*from == STOCK) {
662 *to = WASTE;
663 return CMD_MOVE;
664 }
665 #ifdef KLONDIKE
666 *opt = active.opt; /* when FOUNDATION */
667 #endif
668 inactive = active;
669 break;
670 /* misc keys: */
671 case ':':
672 {char buf[256];
673 fprintf (stderr, ":");
674 raw_mode(0); /* turn on echo */
675 fgets (buf, 256, stdin);
676 raw_mode(1);
677 switch(buf[0]) {
678 case 'q': return CMD_QUIT;
679 case 'n': return CMD_NEW;
680 case 'r': return CMD_AGAIN;
681 default: return CMD_INVAL;
682 }}
683 case 'J':
684 *to = active.pile;
685 #ifdef KLONDIKE
686 if (*to == FOUNDATION) return CMD_JOIN;
687 #endif
688 if (*to > TAB_MAX) return CMD_INVAL;
689 return CMD_JOIN;
690 case 'K': /* fallthrough */
691 case '?': return CMD_HINT;
692 case 'u': return CMD_UNDO;
693 case EOF: return CMD_NONE; /* sent by SIGCONT */
694 default: return CMD_INVAL;
695 }
696 inactive.pile = *from; /* for direct addressing highlighting */
697 if (is_tableu(*from) && f.t[*from][0] == NO_CARD) return CMD_INVAL;
698
699 /***/
700 to_l: print_table(&active, &inactive);
701 t = getch(mouse);
702
703 switch (t) {
704 case KEY_LEFT:
705 case 'h': cursor_left (&active); goto to_l;
706 case KEY_DOWN:
707 case 'j': cursor_down (&active); goto to_l;
708 case KEY_UP:
709 case 'k': cursor_up (&active); goto to_l;
710 case KEY_RIGHT:
711 case 'l': cursor_right(&active); goto to_l;
712 case KEY_HOME:
713 case 'H': cursor_to(&active,TAB_1); goto to_l;
714 case KEY_END:
715 case 'L': cursor_to(&active,TAB_MAX); goto to_l;
716 case KEY_INS:
717 case 'M': cursor_to(&active,TAB_MAX/2); goto to_l;
718 case 'J': /* fallthrough; just join selected pile */
719 case ' ':
720 *to = active.pile;
721 break; /* continues with the foundation/empty tableu check */
722 case 'K': /* fallthrough */
723 case '?': return CMD_HINT;
724 case 'u': return CMD_NONE; /* cancel selection */
725 case EOF: return CMD_NONE; /* sent by SIGCONT */
726 default:
727 if (t < '0' || t > '9') return CMD_INVAL;
728 if (t == '0')
729 #ifdef KLONDIKE
730 *to = FOUNDATION;
731 #elif defined SPIDER
732 *to = TAB_10;
733 #endif
734 else
735 *to = t-'1';
736 }
737
738 /***/
739 #ifdef KLONDIKE
740 if (*from == FOUNDATION) {
741 int top = find_top(f.t[*to]);
742 if (top < 0) return CMD_INVAL;
743 int color = get_color(f.t[*to][top]);
744 int choice_1 = 1-color; /* selects piles of */
745 int choice_2 = 2+color; /* the opposite color */
746 int top_c1 = find_top(f.f[choice_1]);
747 int top_c2 = find_top(f.f[choice_2]);
748
749 switch ((rank_next(f.f[choice_1][top_c1], f.t[*to][top])
750 && top_c1 >= 0 ) << 0
751 |(rank_next(f.f[choice_2][top_c2], f.t[*to][top])
752 && top_c2 >= 0 ) << 1) {
753 case ( 1<<0): *opt = choice_1; break; /* choice_1 only */
754 case (1<<1 ): *opt = choice_2; break; /* choice_2 only */
755 case (1<<1 | 1<<0): /* both, ask user which to pick from */
756 printf ("take from (1-4): "); fflush (stdout);
757 *opt = getch(NULL) - '1';
758 if (*opt < 0 || *opt > 3) return CMD_INVAL;
759 break;
760 default: return CMD_INVAL; /* none matched */
761 }
762 /* `opt` is the foundation index (0..3) */
763 }
764 #elif defined SPIDER
765 /* moving to empty tableu? */
766 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
767 int bottom = first_movable(f.t[*from]);
768 if (inactive.opt >= 0) { /*if from was cursor addressed: */
769 *opt = get_rank(f.t[*from][bottom + inactive.opt]);
770 return CMD_MOVE;
771 }
772 int top = find_top(f.t[*from]);
773 if (top < 0) return CMD_INVAL;
774 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
775 *opt = get_rank(f.t[*from][top]);
776 } else { /* only ask the user if it's unclear: */
777 printf ("\rup to ([a23456789xjqk] or space/return): ");
778 *opt = getch(NULL);
779 switch (*opt) {
780 case ' ': *opt = get_rank(f.t[*from][top]); break;
781 case'\n': *opt = get_rank(f.t[*from][bottom]); break;
782 case 'a': case 'A': *opt = RANK_A; break;
783 case '0': /* fallthrough */
784 case 'x': case 'X': *opt = RANK_X; break;
785 case 'j': case 'J': *opt = RANK_J; break;
786 case 'q': case 'Q': *opt = RANK_Q; break;
787 case 'k': case 'K': *opt = RANK_K; break;
788 default: *opt -= '1';
789 }
790 if (*opt < RANK_A || *opt > RANK_K) return ERR;
791 }
792 /* `opt` is the rank of the highest card to move */
793 }
794 #endif
795 return CMD_MOVE;
796 }
797
798 int getctrlseq(unsigned char* buf) {
799 int c;
800 enum esc_states {
801 START,
802 ESC_SENT,
803 CSI_SENT,
804 MOUSE_EVENT,
805 } state = START;
806 int offset = 0x20; /* never sends control chars as data */
807 while ((c = getchar()) != EOF) {
808 switch (state) {
809 case START:
810 switch (c) {
811 case '\033': state=ESC_SENT; break;
812 default: return c;
813 }
814 break;
815 case ESC_SENT:
816 switch (c) {
817 case '[': state=CSI_SENT; break;
818 default: return KEY_INVAL;
819 }
820 break;
821 case CSI_SENT:
822 switch (c) {
823 case 'A': return KEY_UP;
824 case 'B': return KEY_DOWN;
825 case 'C': return KEY_RIGHT;
826 case 'D': return KEY_LEFT;
827 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
828 case 'H': return KEY_HOME;
829 case 'F': return KEY_END;
830 case '2': getchar(); return KEY_INS;
831 case '5': getchar(); return KEY_PGUP;
832 case '6': getchar(); return KEY_PGDN;
833 case 'M': state=MOUSE_EVENT; break;
834 default: return KEY_INVAL;
835 }
836 break;
837 case MOUSE_EVENT:
838 if (buf == NULL) return KEY_INVAL;
839 buf[0] = c - offset;
840 buf[1] = getchar() - offset;
841 buf[2] = getchar() - offset;
842 return MOUSE_ANY;
843 default:
844 return KEY_INVAL;
845 }
846 }
847 return 2;
848 }
849 int wait_mouse_up(int l, int c) {
850 unsigned char mouse2[3];
851 int level = 1;
852 int l2, c2;
853
854 /* TODO: show a pushed-in button if cursor is on minefield */
855
856 while (level > 0) {
857 if (getctrlseq (mouse2) == MOUSE_ANY) {
858 /* ignore mouse wheel events: */
859 if (mouse2[0] & 0x40) continue;
860
861 else if((mouse2[0]&3) == 3) level--; /* release event */
862 else level++; /* another button pressed */
863 }
864 }
865
866 /* TODO: show normal button */
867
868 c2 = /*screen2field_c*/(mouse2[1]);
869 l2 = /*screen2field_l*/(mouse2[2]);
870 return ((l2 == l) && (c2 == c));
871 }
872
873 int getch(unsigned char* buf) {
874 /* returns a character, EOF, or constant for an escape/control sequence - NOT
875 compatible with the ncurses implementation of same name */
876 int action = getctrlseq(buf);
877 int l, c;
878 switch (action) {
879 case MOUSE_ANY:
880 l = /*screen2field_l*/ (buf[2]);
881 c = /*screen2field_c*/ (buf[1]);
882
883 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
884 int success = wait_mouse_up(l, c);
885
886 /* mouse moved while pressed: */
887 if (!success) return KEY_INVAL;
888
889 switch (buf[0]) {
890 case 0: return MOUSE_LEFT;
891 case 1: return MOUSE_MIDDLE;
892 case 2: return MOUSE_RIGHT;
893 }
894 }
895
896 return action;
897 }
898 // }}}
899
900 // shuffling and dealing {{{
901 void deal(long seed) {
902 f = (const struct playfield){0}; /* clear playfield */
903 card_t deck[DECK_SIZE*NUM_DECKS];
904 int avail = DECK_SIZE*NUM_DECKS;
905 for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) deck[i] = (i%DECK_SIZE)+1;
906 #ifdef SPIDER
907 if (op.m != NORMAL) for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) {
908 if (op.m == MEDIUM) deck[i] = 1+((deck[i]-1) | 2);
909 if (op.m == EASY) deck[i] = 1+((deck[i]-1) | 2 | 1);
910 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
911 }
912 #endif
913 srand (seed);
914 for (int i = DECK_SIZE*NUM_DECKS-1; i > 0; i--) { /* fisher-yates */
915 int j = rand() % (i+1);
916 if (j-i) deck[i]^=deck[j],deck[j]^=deck[i],deck[i]^=deck[j];
917 }
918
919 /* deal cards: */
920 for (int i = 0; i < NUM_PILES; i++) {
921 #ifdef KLONDIKE
922 int closed = i; /* pile n has n closed cards, then 1 open */
923 #elif defined SPIDER
924 int closed = i<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
925 #endif
926 /* face down cards are negated: */
927 for (int j = 0; j < closed; j++) f.t[i][j] = -deck[--avail];
928 f.t[i][closed] = deck[--avail]; /* the face-up card */
929 }
930 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
931 for (f.z = 0; avail; f.z++) f.s[f.z] = deck[--avail];
932 #ifdef KLONDIKE
933 f.w = -1; /* @start: nothing on waste */
934 #elif defined SPIDER
935 f.w = 0; /* number of used foundations */
936 #endif
937
938 f.u = &undo_sentinel;
939 }
940 //}}}
941
942 // screen drawing routines {{{
943 void print_hi(int invert, int grey_bg, int bold, char* str) {
944 if (bold && op.s == &unicode_large_color){//ARGH! awful hack for bold with faint
945 int offset = str[3]==017?16:str[4]==017?17:0;
946 printf ("%s%s%s""%.*s%s%s""%s%s%s",
947 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
948 offset, str, bold?"\033[1m":"", str+offset,
949 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
950 return;
951 }
952 printf ("%s%s%s%s%s%s%s",
953 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
954 str,
955 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
956 }
957 void print_table(const struct cursor* active, const struct cursor* inactive) {
958 printf("\033[2J\033[H"); /* clear screen, reset cursor */
959 #ifdef KLONDIKE
960 /* print stock, waste and foundation: */
961 for (int line = 0; line < op.s->height; line++) {
962 /* stock: */
963 print_hi (active->pile == STOCK, inactive->pile == STOCK, 1, (
964 (f.w < f.z-1)?op.s->facedown
965 :op.s->placeholder)[line]);
966 /* waste: */
967 print_hi (active->pile == WASTE, inactive->pile == WASTE, 1, (
968 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
969 ((short)f.w >= 0)?op.s->card[f.s[f.w]]
970 :op.s->placeholder)[line]);
971 printf ("%s", op.s->card[NO_CARD][line]); /* spacer */
972 /* foundation: */
973 for (int pile = 0; pile < NUM_SUITS; pile++) {
974 int card = find_top(f.f[pile]);
975 print_hi (active->pile==FOUNDATION && active->opt==pile,
976 inactive->pile==FOUNDATION && (
977 /* cursor addr. || direct addr. */
978 inactive->opt==pile || inactive->opt < 0
979 ), 1,
980 (card < 0)?op.s->placeholder[line]
981 :op.s->card[f.f[pile][card]][line]);
982 }
983 printf("\n");
984 }
985 printf("\n");
986 #elif defined SPIDER
987 int fdone; for (fdone = NUM_DECKS*NUM_SUITS; fdone; fdone--)
988 if (f.f[fdone-1][RANK_K]) break; /*number of completed stacks*/
989 int spacer_from = f.z?(f.z/10-1) * op.s->halfwidth[0] + op.s->width:0;
990 int spacer_to = NUM_PILES*op.s->width -
991 ((fdone?(fdone-1) * op.s->halfwidth[1]:0)+op.s->width);
992 for (int line = 0; line < op.s->height; line++) {
993 /* available stock: */
994 for (int i = f.z/10; i; i--) {
995 if (i==1) printf ("%s", op.s->facedown[line]);
996 else printf ("%s", op.s->halfstack[line]);
997 }
998 /* spacer: */
999 for (int i = spacer_from; i < spacer_to; i++) printf (" ");
1000 /* foundation (overlapping): */
1001 for (int i = 0; i < NUM_DECKS*NUM_SUITS; i++) { //TODO: print in revrse order (otherwise new piles get put 'below' older ones)
1002 int overlap = i? op.s->halfcard[line]: 0;
1003 if (f.f[i][RANK_K]) printf ("%.*s", op.s->halfwidth[2],
1004 op.s->card[f.f[i][RANK_K]][line]+overlap);
1005 }
1006 printf("\n");
1007 }
1008 printf("\n");
1009 #endif
1010 #ifdef KLONDIKE
1011 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
1012 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
1013 #define INC_OFFSET
1014 #elif defined SPIDER
1015 int offset[NUM_PILES]={1,1,1,1,1,1,1,1,1,1}; // :|
1016 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
1017 && offset[pile] > cursor->opt)
1018 #define TOP_HI(cursor) (cursor->pile == pile && movable \
1019 && offset[pile]-1 == cursor->opt)
1020 #define INC_OFFSET if (movable) offset[pile]++
1021 #endif
1022 /* print tableu piles: */
1023 int row[NUM_PILES] = {0};
1024 int line[NUM_PILES]= {0};
1025 int label[NUM_PILES]={0};
1026 int line_had_card;
1027 int did_placeholders = 0;
1028 do {
1029 line_had_card = 0;
1030 for (int pile = 0; pile < NUM_PILES; pile++) {
1031 card_t card = f.t[pile][row[pile]];
1032 card_t next = f.t[pile][row[pile]+1];
1033 int movable = is_movable(f.t[pile], row[pile]);
1034 int empty = !card && row[pile] == 0;
1035
1036 print_hi (DO_HI(active), DO_HI(inactive), movable, (
1037 (!card && row[pile] == 0)?op.s->placeholder
1038 :(card<0)?op.s->facedown
1039 :op.s->card[card]
1040 )[line[pile]]);
1041
1042 int extreme_overlap = ( 3 /* spacer, labels, status */
1043 + 2 * op.s->height /* stock, top tableu card */
1044 + find_top(f.t[pile]) * op.s->overlap) >op.w[0];
1045 /* normal overlap: */
1046 if (++line[pile] >= (next?op.s->overlap:op.s->height)
1047 /* extreme overlap on closed cards: */
1048 || (extreme_overlap &&
1049 line[pile] >= 1 &&
1050 f.t[pile][row[pile]] < 0 &&
1051 f.t[pile][row[pile]+1] <0)
1052 /* extreme overlap on sequences: */
1053 || (extreme_overlap &&
1054 !TOP_HI(active) && /*always show top selected card*/
1055 line[pile] >= 1 && row[pile] > 0 &&
1056 f.t[pile][row[pile]-1] > NO_CARD &&
1057 is_consecutive (f.t[pile], row[pile]) &&
1058 is_consecutive (f.t[pile], row[pile]-1) &&
1059 f.t[pile][row[pile]+1] != NO_CARD)
1060 ) {
1061 line[pile]=0;
1062 row[pile]++;
1063 INC_OFFSET;
1064 }
1065 /* tableu labels: */
1066 if(!card && !label[pile] && row[pile]>0&&line[pile]>0) {
1067 label[pile] = 1;
1068 printf ("\b\b%d ", (pile+1) % 10); //XXX: hack
1069 }
1070 line_had_card |= !!card;
1071 did_placeholders |= row[pile] > 0;
1072 }
1073 printf ("\n");
1074 } while (line_had_card || !did_placeholders);
1075 }
1076
1077 void visbell (void) {
1078 printf ("\033[?5h"); fflush (stdout);
1079 usleep (100000);
1080 printf ("\033[?5l"); fflush (stdout);
1081 }
1082 void win_anim(void) {
1083 printf ("\033[?25l"); /* hide cursor */
1084 for (;;) {
1085 /* set cursor to random location */
1086 int row = 1+rand()%(24-op.s->width);
1087 int col = 1+rand()%(80-op.s->height);
1088
1089 /* draw random card */
1090 int face = 1 + rand() % 52;
1091 for (int l = 0; l < op.s->height; l++) {
1092 printf ("\033[%d;%dH", row+l, col);
1093 printf ("%s", op.s->card[face][l]);
1094 }
1095 fflush (stdout);
1096
1097 /* exit on keypress */
1098 struct pollfd p = {STDIN_FILENO, POLLIN, 0};
1099 if (poll (&p, 1, 80)) goto fin;
1100 }
1101 fin:
1102 printf ("\033[?25h"); /* show cursor */
1103 return;
1104 }
1105 //}}}
1106
1107 // undo logic {{{
1108 void undo_push (int _f, int t, int n, int o) {
1109 struct undo* new = malloc(sizeof(struct undo));
1110 new->f = _f;
1111 new->t = t;
1112 new->n = n;
1113 new->o = o;
1114 new->prev = f.u;
1115 new->next = NULL;
1116 f.u->next = new;
1117 f.u = f.u->next;
1118 }
1119 void undo_pop (struct undo* u) {
1120 if (u == &undo_sentinel) return;
1121
1122 #ifdef KLONDIKE
1123 if (u->f == FOUNDATION) {
1124 /* foundation -> tableu */
1125 int top_f = find_top(f.f[u->n]);
1126 int top_t = find_top(f.t[u->t]);
1127 f.f[u->n][top_f+1] = f.t[u->t][top_t];
1128 f.t[u->t][top_t] = NO_CARD;
1129 } else if (u->f == WASTE && u->t == FOUNDATION) {
1130 /* waste -> foundation */
1131 /* split u->n into wst and fnd: */
1132 int wst = u->n & 0xffff;
1133 int fnd = u->n >> 16;
1134 /* move stock cards one position up to make room: */
1135 for (int i = f.z; i >= wst; i--) f.s[i+1] = f.s[i];
1136 /* move one card from foundation to waste: */
1137 int top = find_top(f.f[fnd]);
1138 f.s[wst] = f.f[fnd][top];
1139 f.f[fnd][top] = NO_CARD;
1140 f.z++;
1141 f.w++;
1142 } else if (u->f == WASTE) {
1143 /* waste -> tableu */
1144 /* move stock cards one position up to make room: */
1145 for (int i = f.z; i >= u->n; i--) f.s[i+1] = f.s[i];
1146 /* move one card from tableu to waste: */
1147 int top = find_top(f.t[u->t]);
1148 f.s[u->n] = f.t[u->t][top];
1149 f.t[u->t][top] = NO_CARD;
1150 f.z++;
1151 f.w++;
1152 } else if (u->t == FOUNDATION) {
1153 /* tableu -> foundation */
1154 int top_f = find_top(f.t[u->f]);
1155 int top_t = find_top(f.f[u->n]);
1156 /* close topcard if previous action caused turn_over(): */
1157 if (u->o) f.t[u->f][top_f] *= -1;
1158 /* move one card from foundation to tableu: */
1159 f.t[u->f][top_f+1] = f.f[u->n][top_t];
1160 f.f[u->n][top_t] = NO_CARD;
1161 } else {
1162 /* tableu -> tableu */
1163 int top_f = find_top(f.t[u->f]);
1164 int top_t = find_top(f.t[u->t]);
1165 /* close topcard if previous action caused turn_over(): */
1166 if (u->o) f.t[u->f][top_f] *= -1;
1167 /* move n cards from tableu[f] to tableu[t]: */
1168 for (int i = 0; i < u->n; i++) {
1169 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1170 f.t[u->t][top_t-i] = NO_CARD;
1171 }
1172 }
1173 #elif defined SPIDER
1174 if (u->f == STOCK) {
1175 /* stock -> tableu */
1176 /*remove a card from each pile and put it back onto the stock:*/
1177 for (int pile = NUM_PILES-1; pile >= 0; pile--) {
1178 int top = find_top(f.t[pile]);
1179 f.s[f.z++] = f.t[pile][top];
1180 f.t[pile][top] = NO_CARD;
1181 }
1182 } else if (u->t == FOUNDATION) {
1183 /* tableu -> foundation */
1184 int top = find_top(f.t[u->f]);
1185 /* close topcard if previous action caused turn_over(): */
1186 if (u->o) f.t[u->f][top] *= -1;
1187 /* append cards from foundation to tableu */
1188 for (int i = RANK_K; i >= RANK_A; i--) {
1189 f.t[u->f][++top] = f.f[u->n][i];
1190 f.f[u->n][i] = NO_CARD;
1191 }
1192 f.w--; /* decrement complete-foundation-counter */
1193
1194 } else {
1195 /* tableu -> tableu */
1196 int top_f = find_top(f.t[u->f]);
1197 int top_t = find_top(f.t[u->t]);
1198 /* close topcard if previous action caused turn_over(): */
1199 if (u->o) f.t[u->f][top_f] *= -1;
1200 /* move n cards from tableu[f] to tableu[t]: */
1201 for (int i = 0; i < u->n; i++) {
1202 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1203 f.t[u->t][top_t-i] = NO_CARD;
1204 }
1205 }
1206 #endif
1207
1208 void* old = f.u;
1209 f.u = f.u->prev;
1210 free(old);
1211 }
1212 void free_undo (struct undo* u) {
1213 while (u && u != &undo_sentinel) {
1214 void* old = u;
1215 u = u->prev;
1216 free (old);
1217 }
1218 }
1219 //}}}
1220
1221 // initialization stuff {{{
1222 void screen_setup (int enable) {
1223 if (enable) {
1224 raw_mode(1);
1225 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1226 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1227 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
1228 } else {
1229 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
1230 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1231 raw_mode(0);
1232 }
1233 }
1234
1235 void raw_mode(int enable) {
1236 static struct termios saved_term_mode;
1237 struct termios raw_term_mode;
1238
1239 if (enable) {
1240 if (saved_term_mode.c_lflag == 0)/*don't overwrite stored mode*/
1241 tcgetattr(STDIN_FILENO, &saved_term_mode);
1242 raw_term_mode = saved_term_mode;
1243 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
1244 raw_term_mode.c_cc[VMIN] = 1 ;
1245 raw_term_mode.c_cc[VTIME] = 0;
1246 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
1247 } else {
1248 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
1249 }
1250 }
1251
1252 void signal_handler (int signum) {
1253 struct winsize w;
1254 switch (signum) {
1255 case SIGCONT:
1256 screen_setup(0);
1257 screen_setup(1);
1258 print_table(NO_HI, NO_HI);
1259 break;
1260 case SIGINT: //TODO: don't exit; just warn like vim does
1261 exit(128+SIGINT);
1262 case SIGWINCH:
1263 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
1264 op.w[0] = w.ws_row;
1265 op.w[1] = w.ws_col;
1266 break;
1267 }
1268 }
1269 void signal_setup(void) {
1270 struct sigaction saction;
1271
1272 saction.sa_handler = signal_handler;
1273 sigemptyset(&saction.sa_mask);
1274 saction.sa_flags = 0;
1275 if (sigaction(SIGCONT, &saction, NULL) < 0) {
1276 perror ("SIGCONT");
1277 exit (1);
1278 }
1279 if (sigaction(SIGINT, &saction, NULL) < 0) {
1280 perror ("SIGINT");
1281 exit (1);
1282 }
1283 if (sigaction(SIGWINCH, &saction, NULL) < 0) {
1284 perror ("SIGWINCH");
1285 exit (1);
1286 }
1287 }
1288 //}}}
1289
1290 //vim: foldmethod=marker
Imprint / Impressum