]> git.gir.st - solVItaire.git/blob - sol.c
clean up reworked join
[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)
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 r[pile].pos = bottom; /* need for would_empty */
444
445 if (top < 0) continue; /* no cards to move */
446 if (would_empty(pile)) continue; /* doesn't help */
447
448 r[pile].ok++;
449 r[pile].above = 0; /* always take as many as possible */
450 r[pile].below = top - 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 //todo: in spider, prefer longest piles if above==0 (faster completions)
491 int from = -1;
492 for (int pile = 0, above = 99, below = 99; pile < NUM_PILES; pile++) {
493 if (!r[pile].ok) continue;
494 /* don't bother if another pile would be better: prefer ... */
495 /* ... to complete a stack: */
496 if (!would_complete(pile) && complete) continue;
497 /* ... emptying piles: */
498 if (!would_empty(pile) && empty && !complete) continue;
499 /* ... to turn_over: */
500 if (!would_turn(pile) && turn && !complete && !empty) continue;
501 /* ... not to rip apart too many cards: */
502 if (r[pile].above > above) continue;
503 /* if tied, prefer ... */
504 if (r[pile].above == above
505 /* ... larger pile if destination is empty */
506 && (top_to < 0? r[pile].below < below
507 /* ... shorter pile otherwise */
508 : r[pile].below > below))
509 continue;
510
511 from = pile;
512 above = r[pile].above;
513 below = r[pile].below;
514 }
515
516 /* 3. move cards over and return: */
517 #ifdef KLONDIKE
518 /* prefer waste if it wouldn't turn_over: */
519 if (!turn && w2t(WASTE, to, 0) == OK)
520 return OK;
521 if (from < 0) /* nothing found */
522 return ERR;
523 return t2t(from, to, 0);
524 #elif defined SPIDER
525 if (from < 0) /* nothing found */
526 return ERR;
527 int bottom = first_movable(f.t[from]);
528 return t2t(from, to, get_rank(f.t[from][bottom]));
529 #endif
530 }
531 #undef would_empty
532 #undef would_turn
533 #undef would_complete
534 int nop(int from, int to, int opt) { (void)from;(void)to;(void)opt;return ERR; }
535 // }}}
536
537 // keyboard input handling {{{
538 // cursor functions{{{
539 #ifdef KLONDIKE
540 void cursor_left (struct cursor* cursor) {
541 if (is_tableu(cursor->pile)) {
542 if (cursor->pile > 0) cursor->pile--;
543 cursor->opt = 0;
544 } else { /* stock/waste/foundation*/
545 switch (cursor->pile) {
546 case WASTE: cursor->pile = STOCK; cursor->opt = 0; break;
547 case FOUNDATION:
548 if (cursor->opt <= 0)
549 cursor->pile = WASTE;
550 else
551 cursor->opt--;
552 }
553 }
554 }
555 void cursor_down (struct cursor* cursor) {
556 if (!is_tableu(cursor->pile)) {
557 switch (cursor->pile) {
558 case STOCK: cursor->pile = TAB_1; break;
559 case WASTE: cursor->pile = TAB_2; break;
560 case FOUNDATION:
561 cursor->pile = TAB_4 + cursor->opt;
562 }
563 cursor->opt = 0;
564 }
565 }
566 void cursor_up (struct cursor* cursor) {
567 if (is_tableu(cursor->pile)) {
568 switch (cursor->pile) { //ugly :|
569 case TAB_1: cursor->pile = STOCK; break;
570 case TAB_2: cursor->pile = WASTE; break;
571 case TAB_3: cursor->pile = WASTE; break;
572 case TAB_4: case TAB_5: case TAB_6: case TAB_7:
573 cursor->opt=cursor->pile-TAB_4;
574 cursor->pile = FOUNDATION;
575 break;
576 }
577 }
578 }
579 void cursor_right (struct cursor* cursor) {
580 if (is_tableu(cursor->pile)) {
581 if (cursor->pile < TAB_MAX) cursor->pile++;
582 } else {
583 switch (cursor->pile) {
584 case STOCK: cursor->pile = WASTE; break;
585 case WASTE: cursor->pile = FOUNDATION;cursor->opt = 0; break;
586 case FOUNDATION:
587 if (cursor->opt < NUM_SUITS-1)
588 cursor->opt++;
589 }
590 }
591 }
592 #elif defined SPIDER
593 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
594 void cursor_left (struct cursor* cursor) {
595 if (cursor->pile > 0) cursor->pile--;
596 cursor->opt = 0;
597 }
598 void cursor_down (struct cursor* cursor) {
599 int first = first_movable(f.t[cursor->pile]);
600 int top = find_top(f.t[cursor->pile]);
601 if (first + cursor->opt < top)
602 cursor->opt++;
603 }
604 void cursor_up (struct cursor* cursor) {
605 if (cursor->opt > 0) cursor->opt--;
606 }
607 void cursor_right (struct cursor* cursor) {
608 if (cursor->pile < TAB_MAX) cursor->pile++;
609 cursor->opt = 0;
610 }
611 #endif
612 void cursor_to (struct cursor* cursor, int pile) {
613 cursor->pile = pile;
614 cursor->opt = 0;
615 }
616 //}}}
617 int get_cmd (int* from, int* to, int* opt) {
618 /*XXX*/unsigned char mouse[3];
619 //TODO: escape sequences (mouse, cursor keys)
620 int _f, t;
621 struct cursor inactive = {-1,-1};
622 static struct cursor active = {0,0};
623 active.opt = 0; /* always reset offset, but keep pile */
624
625 /***/
626 from_l: print_table(&active, &inactive);
627 _f = getch(mouse);
628
629 switch (_f) {
630 /* direct addressing: */
631 case '1': *from = TAB_1; break;
632 case '2': *from = TAB_2; break;
633 case '3': *from = TAB_3; break;
634 case '4': *from = TAB_4; break;
635 case '5': *from = TAB_5; break;
636 case '6': *from = TAB_6; break;
637 case '7': *from = TAB_7; break;
638 #ifdef SPIDER
639 case '8': *from = TAB_8; break;
640 case '9': *from = TAB_9; break;
641 case '0': *from = TAB_10;break;
642 #elif defined KLONDIKE
643 case '9': *from = WASTE; break;
644 case '0': *from = FOUNDATION; break;
645 case '8': /* fallthrough */
646 #endif
647 case '\n': /* shortcut for dealing from stock */
648 *from = STOCK;
649 *to = WASTE;
650 return CMD_MOVE;
651 /* cursor keys addressing: */
652 case KEY_LEFT:
653 case 'h': cursor_left (&active); goto from_l;
654 case KEY_DOWN:
655 case 'j': cursor_down (&active); goto from_l;
656 case KEY_UP:
657 case 'k': cursor_up (&active); goto from_l;
658 case KEY_RIGHT:
659 case 'l': cursor_right(&active); goto from_l;
660 case KEY_HOME:
661 case 'H': cursor_to(&active,TAB_1); goto from_l; /* leftmost tableu */
662 case KEY_END:
663 case 'L': cursor_to(&active,TAB_MAX);goto from_l; /* rigthmost tableu */
664 case KEY_INS:
665 case 'M': cursor_to(&active,TAB_MAX/2); goto from_l; /* center tableu */
666 //TODO: real cursor keys, home/end
667 case ' ': /* continue with second cursor */
668 *from = active.pile;
669 if (*from == STOCK) {
670 *to = WASTE;
671 return CMD_MOVE;
672 }
673 #ifdef KLONDIKE
674 *opt = active.opt; /* when FOUNDATION */
675 #endif
676 inactive = active;
677 break;
678 /* misc keys: */
679 case ':':
680 {char buf[256];
681 fprintf (stderr, ":");
682 raw_mode(0); /* turn on echo */
683 fgets (buf, 256, stdin);
684 raw_mode(1);
685 switch(buf[0]) {
686 case 'q': return CMD_QUIT;
687 case 'n': return CMD_NEW;
688 case 'r': return CMD_AGAIN;
689 default: return CMD_INVAL;
690 }}
691 case 'J':
692 *to = active.pile;
693 #ifdef KLONDIKE
694 if (*to == FOUNDATION) return CMD_JOIN;
695 #endif
696 if (*to > TAB_MAX) return CMD_INVAL;
697 return CMD_JOIN;
698 case 'K': /* fallthrough */
699 case '?': return CMD_HINT;
700 case 'u': return CMD_UNDO;
701 case EOF: return CMD_NONE; /* sent by SIGCONT */
702 default: return CMD_INVAL;
703 }
704 inactive.pile = *from; /* for direct addressing highlighting */
705 if (is_tableu(*from) && f.t[*from][0] == NO_CARD) return CMD_INVAL;
706
707 /***/
708 to_l: print_table(&active, &inactive);
709 t = getch(mouse);
710
711 switch (t) {
712 case KEY_LEFT:
713 case 'h': cursor_left (&active); goto to_l;
714 case KEY_DOWN:
715 case 'j': cursor_down (&active); goto to_l;
716 case KEY_UP:
717 case 'k': cursor_up (&active); goto to_l;
718 case KEY_RIGHT:
719 case 'l': cursor_right(&active); goto to_l;
720 case KEY_HOME:
721 case 'H': cursor_to(&active,TAB_1); goto to_l;
722 case KEY_END:
723 case 'L': cursor_to(&active,TAB_MAX); goto to_l;
724 case KEY_INS:
725 case 'M': cursor_to(&active,TAB_MAX/2); goto to_l;
726 case 'J': /* fallthrough; just join selected pile */
727 case ' ':
728 *to = active.pile;
729 break; /* continues with the foundation/empty tableu check */
730 case 'K': /* fallthrough */
731 case '?': return CMD_HINT;
732 case 'u': return CMD_NONE; /* cancel selection */
733 case EOF: return CMD_NONE; /* sent by SIGCONT */
734 default:
735 if (t < '0' || t > '9') return CMD_INVAL;
736 if (t == '0')
737 #ifdef KLONDIKE
738 *to = FOUNDATION;
739 #elif defined SPIDER
740 *to = TAB_10;
741 #endif
742 else
743 *to = t-'1';
744 }
745
746 /***/
747 #ifdef KLONDIKE
748 if (*from == FOUNDATION) {
749 int top = find_top(f.t[*to]);
750 if (top < 0) return CMD_INVAL;
751 int color = get_color(f.t[*to][top]);
752 int choice_1 = 1-color; /* selects piles of */
753 int choice_2 = 2+color; /* the opposite color */
754 int top_c1 = find_top(f.f[choice_1]);
755 int top_c2 = find_top(f.f[choice_2]);
756
757 switch ((rank_next(f.f[choice_1][top_c1], f.t[*to][top])
758 && top_c1 >= 0 ) << 0
759 |(rank_next(f.f[choice_2][top_c2], f.t[*to][top])
760 && top_c2 >= 0 ) << 1) {
761 case ( 1<<0): *opt = choice_1; break; /* choice_1 only */
762 case (1<<1 ): *opt = choice_2; break; /* choice_2 only */
763 case (1<<1 | 1<<0): /* both, ask user which to pick from */
764 printf ("take from (1-4): "); fflush (stdout);
765 *opt = getch(NULL) - '1';
766 if (*opt < 0 || *opt > 3) return CMD_INVAL;
767 break;
768 default: return CMD_INVAL; /* none matched */
769 }
770 /* `opt` is the foundation index (0..3) */
771 }
772 #elif defined SPIDER
773 /* moving to empty tableu? */
774 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
775 int bottom = first_movable(f.t[*from]);
776 if (inactive.opt >= 0) { /*if from was cursor addressed: */
777 *opt = get_rank(f.t[*from][bottom + inactive.opt]);
778 return CMD_MOVE;
779 }
780 int top = find_top(f.t[*from]);
781 if (top < 0) return CMD_INVAL;
782 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
783 *opt = get_rank(f.t[*from][top]);
784 } else { /* only ask the user if it's unclear: */
785 printf ("\rup to ([a23456789xjqk] or space/return): ");
786 *opt = getch(NULL);
787 switch (*opt) {
788 case ' ': *opt = get_rank(f.t[*from][top]); break;
789 case'\n': *opt = get_rank(f.t[*from][bottom]); break;
790 case 'a': case 'A': *opt = RANK_A; break;
791 case '0': /* fallthrough */
792 case 'x': case 'X': *opt = RANK_X; break;
793 case 'j': case 'J': *opt = RANK_J; break;
794 case 'q': case 'Q': *opt = RANK_Q; break;
795 case 'k': case 'K': *opt = RANK_K; break;
796 default: *opt -= '1';
797 }
798 if (*opt < RANK_A || *opt > RANK_K) return ERR;
799 }
800 /* `opt` is the rank of the highest card to move */
801 }
802 #endif
803 return CMD_MOVE;
804 }
805
806 int getctrlseq(unsigned char* buf) {
807 int c;
808 enum esc_states {
809 START,
810 ESC_SENT,
811 CSI_SENT,
812 MOUSE_EVENT,
813 } state = START;
814 int offset = 0x20; /* never sends control chars as data */
815 while ((c = getchar()) != EOF) {
816 switch (state) {
817 case START:
818 switch (c) {
819 case '\033': state=ESC_SENT; break;
820 default: return c;
821 }
822 break;
823 case ESC_SENT:
824 switch (c) {
825 case '[': state=CSI_SENT; break;
826 default: return KEY_INVAL;
827 }
828 break;
829 case CSI_SENT:
830 switch (c) {
831 case 'A': return KEY_UP;
832 case 'B': return KEY_DOWN;
833 case 'C': return KEY_RIGHT;
834 case 'D': return KEY_LEFT;
835 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
836 case 'H': return KEY_HOME;
837 case 'F': return KEY_END;
838 case '2': getchar(); return KEY_INS;
839 case '5': getchar(); return KEY_PGUP;
840 case '6': getchar(); return KEY_PGDN;
841 case 'M': state=MOUSE_EVENT; break;
842 default: return KEY_INVAL;
843 }
844 break;
845 case MOUSE_EVENT:
846 if (buf == NULL) return KEY_INVAL;
847 buf[0] = c - offset;
848 buf[1] = getchar() - offset;
849 buf[2] = getchar() - offset;
850 return MOUSE_ANY;
851 default:
852 return KEY_INVAL;
853 }
854 }
855 return 2;
856 }
857 int wait_mouse_up(int l, int c) {
858 unsigned char mouse2[3];
859 int level = 1;
860 int l2, c2;
861
862 /* TODO: show a pushed-in button if cursor is on minefield */
863
864 while (level > 0) {
865 if (getctrlseq (mouse2) == MOUSE_ANY) {
866 /* ignore mouse wheel events: */
867 if (mouse2[0] & 0x40) continue;
868
869 else if((mouse2[0]&3) == 3) level--; /* release event */
870 else level++; /* another button pressed */
871 }
872 }
873
874 /* TODO: show normal button */
875
876 c2 = /*screen2field_c*/(mouse2[1]);
877 l2 = /*screen2field_l*/(mouse2[2]);
878 return ((l2 == l) && (c2 == c));
879 }
880
881 int getch(unsigned char* buf) {
882 /* returns a character, EOF, or constant for an escape/control sequence - NOT
883 compatible with the ncurses implementation of same name */
884 int action = getctrlseq(buf);
885 int l, c;
886 switch (action) {
887 case MOUSE_ANY:
888 l = /*screen2field_l*/ (buf[2]);
889 c = /*screen2field_c*/ (buf[1]);
890
891 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
892 int success = wait_mouse_up(l, c);
893
894 /* mouse moved while pressed: */
895 if (!success) return KEY_INVAL;
896
897 switch (buf[0]) {
898 case 0: return MOUSE_LEFT;
899 case 1: return MOUSE_MIDDLE;
900 case 2: return MOUSE_RIGHT;
901 }
902 }
903
904 return action;
905 }
906 // }}}
907
908 // shuffling and dealing {{{
909 void deal(long seed) {
910 f = (const struct playfield){0}; /* clear playfield */
911 card_t deck[DECK_SIZE*NUM_DECKS];
912 int avail = DECK_SIZE*NUM_DECKS;
913 for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) deck[i] = (i%DECK_SIZE)+1;
914 #ifdef SPIDER
915 if (op.m != NORMAL) for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) {
916 if (op.m == MEDIUM) deck[i] = 1+((deck[i]-1) | 2);
917 if (op.m == EASY) deck[i] = 1+((deck[i]-1) | 2 | 1);
918 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
919 }
920 #endif
921 srand (seed);
922 for (int i = DECK_SIZE*NUM_DECKS-1; i > 0; i--) { /* fisher-yates */
923 int j = rand() % (i+1);
924 if (j-i) deck[i]^=deck[j],deck[j]^=deck[i],deck[i]^=deck[j];
925 }
926
927 /* deal cards: */
928 for (int i = 0; i < NUM_PILES; i++) {
929 #ifdef KLONDIKE
930 int closed = i; /* pile n has n closed cards, then 1 open */
931 #elif defined SPIDER
932 int closed = i<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
933 #endif
934 /* face down cards are negated: */
935 for (int j = 0; j < closed; j++) f.t[i][j] = -deck[--avail];
936 f.t[i][closed] = deck[--avail]; /* the face-up card */
937 }
938 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
939 for (f.z = 0; avail; f.z++) f.s[f.z] = deck[--avail];
940 #ifdef KLONDIKE
941 f.w = -1; /* @start: nothing on waste */
942 #elif defined SPIDER
943 f.w = 0; /* number of used foundations */
944 #endif
945
946 f.u = &undo_sentinel;
947 }
948 //}}}
949
950 // screen drawing routines {{{
951 void print_hi(int invert, int grey_bg, int bold, char* str) {
952 if (bold && op.s == &unicode_large_color){//ARGH! awful hack for bold with faint
953 int offset = str[3]==017?16:str[4]==017?17:0;
954 printf ("%s%s%s""%.*s%s%s""%s%s%s",
955 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
956 offset, str, bold?"\033[1m":"", str+offset,
957 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
958 return;
959 }
960 printf ("%s%s%s%s%s%s%s",
961 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
962 str,
963 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
964 }
965 void print_table(const struct cursor* active, const struct cursor* inactive) {
966 printf("\033[2J\033[H"); /* clear screen, reset cursor */
967 #ifdef KLONDIKE
968 /* print stock, waste and foundation: */
969 for (int line = 0; line < op.s->height; line++) {
970 /* stock: */
971 print_hi (active->pile == STOCK, inactive->pile == STOCK, 1, (
972 (f.w < f.z-1)?op.s->facedown
973 :op.s->placeholder)[line]);
974 /* waste: */
975 print_hi (active->pile == WASTE, inactive->pile == WASTE, 1, (
976 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
977 ((short)f.w >= 0)?op.s->card[f.s[f.w]]
978 :op.s->placeholder)[line]);
979 printf ("%s", op.s->card[NO_CARD][line]); /* spacer */
980 /* foundation: */
981 for (int pile = 0; pile < NUM_SUITS; pile++) {
982 int card = find_top(f.f[pile]);
983 print_hi (active->pile==FOUNDATION && active->opt==pile,
984 inactive->pile==FOUNDATION && (
985 /* cursor addr. || direct addr. */
986 inactive->opt==pile || inactive->opt < 0
987 ), 1,
988 (card < 0)?op.s->placeholder[line]
989 :op.s->card[f.f[pile][card]][line]);
990 }
991 printf("\n");
992 }
993 printf("\n");
994 #elif defined SPIDER
995 int fdone; for (fdone = NUM_DECKS*NUM_SUITS; fdone; fdone--)
996 if (f.f[fdone-1][RANK_K]) break; /*number of completed stacks*/
997 int spacer_from = f.z?(f.z/10-1) * op.s->halfwidth[0] + op.s->width:0;
998 int spacer_to = NUM_PILES*op.s->width -
999 ((fdone?(fdone-1) * op.s->halfwidth[1]:0)+op.s->width);
1000 for (int line = 0; line < op.s->height; line++) {
1001 /* available stock: */
1002 for (int i = f.z/10; i; i--) {
1003 if (i==1) printf ("%s", op.s->facedown[line]);
1004 else printf ("%s", op.s->halfstack[line]);
1005 }
1006 /* spacer: */
1007 for (int i = spacer_from; i < spacer_to; i++) printf (" ");
1008 /* foundation (overlapping): */
1009 for (int i = 0; i < NUM_DECKS*NUM_SUITS; i++) { //TODO: print in revrse order (otherwise new piles get put 'below' older ones)
1010 int overlap = i? op.s->halfcard[line]: 0;
1011 if (f.f[i][RANK_K]) printf ("%.*s", op.s->halfwidth[2],
1012 op.s->card[f.f[i][RANK_K]][line]+overlap);
1013 }
1014 printf("\n");
1015 }
1016 printf("\n");
1017 #endif
1018 #ifdef KLONDIKE
1019 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
1020 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
1021 #define INC_OFFSET
1022 #elif defined SPIDER
1023 int offset[NUM_PILES]={1,1,1,1,1,1,1,1,1,1}; // :|
1024 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
1025 && offset[pile] > cursor->opt)
1026 #define TOP_HI(cursor) (cursor->pile == pile && movable \
1027 && offset[pile]-1 == cursor->opt)
1028 #define INC_OFFSET if (movable) offset[pile]++
1029 #endif
1030 /* print tableu piles: */
1031 int row[NUM_PILES] = {0};
1032 int line[NUM_PILES]= {0};
1033 int label[NUM_PILES]={0};
1034 int line_had_card;
1035 int did_placeholders = 0;
1036 do {
1037 line_had_card = 0;
1038 for (int pile = 0; pile < NUM_PILES; pile++) {
1039 card_t card = f.t[pile][row[pile]];
1040 card_t next = f.t[pile][row[pile]+1];
1041 int movable = is_movable(f.t[pile], row[pile]);
1042 int empty = !card && row[pile] == 0;
1043
1044 print_hi (DO_HI(active), DO_HI(inactive), movable, (
1045 (!card && row[pile] == 0)?op.s->placeholder
1046 :(card<0)?op.s->facedown
1047 :op.s->card[card]
1048 )[line[pile]]);
1049
1050 int extreme_overlap = ( 3 /* spacer, labels, status */
1051 + 2 * op.s->height /* stock, top tableu card */
1052 + find_top(f.t[pile]) * op.s->overlap) >op.w[0];
1053 /* normal overlap: */
1054 if (++line[pile] >= (next?op.s->overlap:op.s->height)
1055 /* extreme overlap on closed cards: */
1056 || (extreme_overlap &&
1057 line[pile] >= 1 &&
1058 f.t[pile][row[pile]] < 0 &&
1059 f.t[pile][row[pile]+1] <0)
1060 /* extreme overlap on sequences: */
1061 || (extreme_overlap &&
1062 !TOP_HI(active) && /*always show top selected card*/
1063 line[pile] >= 1 && row[pile] > 0 &&
1064 f.t[pile][row[pile]-1] > NO_CARD &&
1065 is_consecutive (f.t[pile], row[pile]) &&
1066 is_consecutive (f.t[pile], row[pile]-1) &&
1067 f.t[pile][row[pile]+1] != NO_CARD)
1068 ) {
1069 line[pile]=0;
1070 row[pile]++;
1071 INC_OFFSET;
1072 }
1073 /* tableu labels: */
1074 if(!card && !label[pile] && row[pile]>0&&line[pile]>0) {
1075 label[pile] = 1;
1076 printf ("\b\b%d ", (pile+1) % 10); //XXX: hack
1077 }
1078 line_had_card |= !!card;
1079 did_placeholders |= row[pile] > 0;
1080 }
1081 printf ("\n");
1082 } while (line_had_card || !did_placeholders);
1083 }
1084
1085 void visbell (void) {
1086 printf ("\033[?5h"); fflush (stdout);
1087 usleep (100000);
1088 printf ("\033[?5l"); fflush (stdout);
1089 }
1090 void win_anim(void) {
1091 printf ("\033[?25l"); /* hide cursor */
1092 for (;;) {
1093 /* set cursor to random location */
1094 int row = 1+rand()%(24-op.s->width);
1095 int col = 1+rand()%(80-op.s->height);
1096
1097 /* draw random card */
1098 int face = 1 + rand() % 52;
1099 for (int l = 0; l < op.s->height; l++) {
1100 printf ("\033[%d;%dH", row+l, col);
1101 printf ("%s", op.s->card[face][l]);
1102 }
1103 fflush (stdout);
1104
1105 /* exit on keypress */
1106 struct pollfd p = {STDIN_FILENO, POLLIN, 0};
1107 if (poll (&p, 1, 80)) goto fin;
1108 }
1109 fin:
1110 printf ("\033[?25h"); /* show cursor */
1111 return;
1112 }
1113 //}}}
1114
1115 // undo logic {{{
1116 void undo_push (int _f, int t, int n, int o) {
1117 struct undo* new = malloc(sizeof(struct undo));
1118 new->f = _f;
1119 new->t = t;
1120 new->n = n;
1121 new->o = o;
1122 new->prev = f.u;
1123 new->next = NULL;
1124 f.u->next = new;
1125 f.u = f.u->next;
1126 }
1127 void undo_pop (struct undo* u) {
1128 if (u == &undo_sentinel) return;
1129
1130 #ifdef KLONDIKE
1131 if (u->f == FOUNDATION) {
1132 /* foundation -> tableu */
1133 int top_f = find_top(f.f[u->n]);
1134 int top_t = find_top(f.t[u->t]);
1135 f.f[u->n][top_f+1] = f.t[u->t][top_t];
1136 f.t[u->t][top_t] = NO_CARD;
1137 } else if (u->f == WASTE && u->t == FOUNDATION) {
1138 /* waste -> foundation */
1139 /* split u->n into wst and fnd: */
1140 int wst = u->n & 0xffff;
1141 int fnd = u->n >> 16;
1142 /* move stock cards one position up to make room: */
1143 for (int i = f.z; i >= wst; i--) f.s[i+1] = f.s[i];
1144 /* move one card from foundation to waste: */
1145 int top = find_top(f.f[fnd]);
1146 f.s[wst] = f.f[fnd][top];
1147 f.f[fnd][top] = NO_CARD;
1148 f.z++;
1149 f.w++;
1150 } else if (u->f == WASTE) {
1151 /* waste -> tableu */
1152 /* move stock cards one position up to make room: */
1153 for (int i = f.z; i >= u->n; i--) f.s[i+1] = f.s[i];
1154 /* move one card from tableu to waste: */
1155 int top = find_top(f.t[u->t]);
1156 f.s[u->n] = f.t[u->t][top];
1157 f.t[u->t][top] = NO_CARD;
1158 f.z++;
1159 f.w++;
1160 } else if (u->t == FOUNDATION) {
1161 /* tableu -> foundation */
1162 int top_f = find_top(f.t[u->f]);
1163 int top_t = find_top(f.f[u->n]);
1164 /* close topcard if previous action caused turn_over(): */
1165 if (u->o) f.t[u->f][top_f] *= -1;
1166 /* move one card from foundation to tableu: */
1167 f.t[u->f][top_f+1] = f.f[u->n][top_t];
1168 f.f[u->n][top_t] = NO_CARD;
1169 } else {
1170 /* tableu -> tableu */
1171 int top_f = find_top(f.t[u->f]);
1172 int top_t = find_top(f.t[u->t]);
1173 /* close topcard if previous action caused turn_over(): */
1174 if (u->o) f.t[u->f][top_f] *= -1;
1175 /* move n cards from tableu[f] to tableu[t]: */
1176 for (int i = 0; i < u->n; i++) {
1177 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1178 f.t[u->t][top_t-i] = NO_CARD;
1179 }
1180 }
1181 #elif defined SPIDER
1182 if (u->f == STOCK) {
1183 /* stock -> tableu */
1184 /*remove a card from each pile and put it back onto the stock:*/
1185 for (int pile = NUM_PILES-1; pile >= 0; pile--) {
1186 int top = find_top(f.t[pile]);
1187 f.s[f.z++] = f.t[pile][top];
1188 f.t[pile][top] = NO_CARD;
1189 }
1190 } else if (u->t == FOUNDATION) {
1191 /* tableu -> foundation */
1192 int top = find_top(f.t[u->f]);
1193 /* close topcard if previous action caused turn_over(): */
1194 if (u->o) f.t[u->f][top] *= -1;
1195 /* append cards from foundation to tableu */
1196 for (int i = RANK_K; i >= RANK_A; i--) {
1197 f.t[u->f][++top] = f.f[u->n][i];
1198 f.f[u->n][i] = NO_CARD;
1199 }
1200 f.w--; /* decrement complete-foundation-counter */
1201
1202 } else {
1203 /* tableu -> tableu */
1204 int top_f = find_top(f.t[u->f]);
1205 int top_t = find_top(f.t[u->t]);
1206 /* close topcard if previous action caused turn_over(): */
1207 if (u->o) f.t[u->f][top_f] *= -1;
1208 /* move n cards from tableu[f] to tableu[t]: */
1209 for (int i = 0; i < u->n; i++) {
1210 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1211 f.t[u->t][top_t-i] = NO_CARD;
1212 }
1213 }
1214 #endif
1215
1216 void* old = f.u;
1217 f.u = f.u->prev;
1218 free(old);
1219 }
1220 void free_undo (struct undo* u) {
1221 while (u && u != &undo_sentinel) {
1222 void* old = u;
1223 u = u->prev;
1224 free (old);
1225 }
1226 }
1227 //}}}
1228
1229 // initialization stuff {{{
1230 void screen_setup (int enable) {
1231 if (enable) {
1232 raw_mode(1);
1233 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1234 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1235 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
1236 } else {
1237 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
1238 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1239 raw_mode(0);
1240 }
1241 }
1242
1243 void raw_mode(int enable) {
1244 static struct termios saved_term_mode;
1245 struct termios raw_term_mode;
1246
1247 if (enable) {
1248 if (saved_term_mode.c_lflag == 0)/*don't overwrite stored mode*/
1249 tcgetattr(STDIN_FILENO, &saved_term_mode);
1250 raw_term_mode = saved_term_mode;
1251 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
1252 raw_term_mode.c_cc[VMIN] = 1 ;
1253 raw_term_mode.c_cc[VTIME] = 0;
1254 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
1255 } else {
1256 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
1257 }
1258 }
1259
1260 void signal_handler (int signum) {
1261 struct winsize w;
1262 switch (signum) {
1263 case SIGCONT:
1264 screen_setup(0);
1265 screen_setup(1);
1266 print_table(NO_HI, NO_HI);
1267 break;
1268 case SIGINT: //TODO: don't exit; just warn like vim does
1269 exit(128+SIGINT);
1270 case SIGWINCH:
1271 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
1272 op.w[0] = w.ws_row;
1273 op.w[1] = w.ws_col;
1274 break;
1275 }
1276 }
1277 void signal_setup(void) {
1278 struct sigaction saction;
1279
1280 saction.sa_handler = signal_handler;
1281 sigemptyset(&saction.sa_mask);
1282 saction.sa_flags = 0;
1283 if (sigaction(SIGCONT, &saction, NULL) < 0) {
1284 perror ("SIGCONT");
1285 exit (1);
1286 }
1287 if (sigaction(SIGINT, &saction, NULL) < 0) {
1288 perror ("SIGINT");
1289 exit (1);
1290 }
1291 if (sigaction(SIGWINCH, &saction, NULL) < 0) {
1292 perror ("SIGWINCH");
1293 exit (1);
1294 }
1295 }
1296 //}}}
1297
1298 //vim: foldmethod=marker
Imprint / Impressum