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