~kroq-gar78/ubuntu/precise/ace-of-penguins/fix-978446

« back to all changes in this revision

Viewing changes to .pc/11-include-imagelib.patch/games/spider.c

  • Committer: Bazaar Package Importer
  • Author(s): Artur Rona, Artur Rona, Manfred Hampl
  • Date: 2010-10-31 12:40:19 UTC
  • mfrom: (5.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20101031124019-igmqv4os4wlkxs1r
Tags: 1.3-1ubuntu1
[ Artur Rona ]
* Merge from debian unstable.  Remaining changes:
  - debian/patches/11-include-imagelib.patch:
    + Fix implicit pointer conversion.

[ Manfred Hampl ]
* Add missing information for spider game (available since 1.3):
  - debian/*.man, debian/*.pod, debian/menu:
    + Information about spider added. (LP: #643336)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The Ace of Penguins - freecell.c
 
2
   Copyright (C) 1998, 2001 DJ Delorie
 
3
 
 
4
   This program is free software; you can redistribute it and/or modify
 
5
   it under the terms of the GNU General Public License as published by
 
6
   the Free Software Foundation; either version 2, or (at your option)
 
7
   any later version.
 
8
 
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with this program; if not, write to the Free Software
 
16
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
17
 
 
18
#include <stdlib.h>
 
19
#include <stdio.h>
 
20
#include <string.h>
 
21
#include <time.h>
 
22
 
 
23
#include "cards.h"
 
24
//#include "imagelib.h"
 
25
 
 
26
#define W CARD_WIDTH
 
27
#define H CARD_HEIGHT
 
28
#define M CARD_MARGIN
 
29
#define F CARD_FAN_DOWN
 
30
 
 
31
static Picture *splash, *youwin, *youlose, *arrow, *no_arrow;
 
32
 
 
33
static Stack *deck;
 
34
static Stack *outcells[8];
 
35
static Stack *maincells[10];
 
36
 
 
37
static int suit_mask = 0; // 0 = one suit, 2 = two suit, 3 = four suit 
 
38
 
 
39
static void auto_move();
 
40
static void check_for_end_of_game();
 
41
static void set_arrows();
 
42
static void clear_arrows();
 
43
static int ax, adx, ay, ady;
 
44
 
 
45
static void
 
46
start_again()
 
47
{
 
48
  int i, d, s, v;
 
49
 
 
50
  clear_arrows();
 
51
 
 
52
  for (i=0; i<8; i++)
 
53
    stack_flip_stack(outcells[i], deck);
 
54
  for (i=0; i<10; i++)
 
55
    stack_flip_stack(maincells[i], deck);
 
56
 
 
57
  while(stack_take_card(deck) != -1);
 
58
  for (d=0; d<2; d++)
 
59
    for (s=0; s<4; s++)
 
60
      for (v=ACE; v<=KING; v++)
 
61
        stack_add_card(deck, MAKE_CARD(s & suit_mask, v, FACEDOWN));
 
62
 
 
63
  stack_shuffle(deck);
 
64
  stack_shuffle(deck);
 
65
  stack_shuffle(deck);
 
66
  for (i=0; i<40; i++)
 
67
    stack_move_cards(deck, stack_count_cards(deck)-1, maincells[i%10]);
 
68
  for (i=0; i<4; i++)
 
69
    stack_move_cards(deck, stack_count_cards(deck)-1, maincells[i*3]);
 
70
  for (i=0; i<10; i++)
 
71
    stack_flip_card(deck, maincells[i]);
 
72
 
 
73
  stack_undo_reset();
 
74
 
 
75
  set_arrows();
 
76
}
 
77
 
 
78
static void
 
79
init()
 
80
{
 
81
  int s;
 
82
  Picture *empty;
 
83
 
 
84
  stack_load_standard_deck();
 
85
  empty = get_picture("empty");
 
86
 
 
87
  set_centered_pic(splash);
 
88
 
 
89
  for (s=0; s<8; s++)  {
 
90
    outcells[s] = stack_create((2+s)*(W+M), M);
 
91
    stack_set_empty_picture(outcells[s], empty);
 
92
  }
 
93
 
 
94
  for (s=0; s<10; s++)  {
 
95
    maincells[s] = stack_create(M+s*(M+W), H+M);
 
96
    stack_set_offset(maincells[s], STACK_OFFSET_DOWN);
 
97
  }
 
98
 
 
99
  deck = stack_create(M, M);
 
100
 
 
101
  start_again();
 
102
}
 
103
 
 
104
static void
 
105
resize(int w, int h)
 
106
{
 
107
  int margin, offset, cw, ch, s, fd, fr, tfd, tfr;
 
108
 
 
109
  Picture *empty;
 
110
 
 
111
  stack_set_card_size (w/11, w/11*4/3);
 
112
  stack_get_card_size (&cw, &ch);
 
113
 
 
114
  empty = (Picture *)get_image("empty", cw, ch, 0);
 
115
 
 
116
  margin = (w - 10*cw) / 11;
 
117
  offset = (w - margin*11 - cw*10) / 2 + margin;
 
118
 
 
119
  for (s=0; s<8; s++) {
 
120
    stack_move(outcells[s], (s+2)*(cw+margin)+offset, margin);
 
121
    stack_set_empty_picture(outcells[s], empty);
 
122
  }
 
123
  for (s=0; s<10; s++)
 
124
    stack_move(maincells[s], offset + s*(cw+margin), ch + (offset<0?0:offset));
 
125
  stack_move(deck, margin+offset, margin);
 
126
 
 
127
  stack_get_fans(&fd, &fr, &tfd, &tfr);
 
128
 
 
129
  ax = offset + cw/2;
 
130
  adx = margin + cw;
 
131
  ay = offset + ch*2 + 2;
 
132
  ady = fd;
 
133
 
 
134
  set_arrows();
 
135
}
 
136
 
 
137
 
 
138
static int no_arrows = 0;
 
139
static int supress_arrows = 0;
 
140
static int arrowsx[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
141
static int arrowsy[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
142
 
 
143
static void
 
144
set_arrow(int column, int on)
 
145
{
 
146
  int x = ax + adx*column;
 
147
  if (supress_arrows || no_arrows) return;
 
148
  if (arrowsy[column])
 
149
    put_picture(no_arrow, arrowsx[column]-no_arrow->w/2, arrowsy[column],
 
150
                0, 0, no_arrow->w, no_arrow->h);
 
151
  
 
152
  if (on)
 
153
  {
 
154
    arrowsx[column] = x;
 
155
    arrowsy[column] = ay+ady*(stack_count_cards(maincells[column])-1);
 
156
    put_picture(arrow, arrowsx[column]-arrow->w/2, arrowsy[column],
 
157
                0, 0, arrow->w, arrow->h);
 
158
  }
 
159
  else
 
160
    arrowsy[column] = 0;
 
161
}
 
162
 
 
163
static void
 
164
redraw_arrows()
 
165
{
 
166
  int i;
 
167
  if(no_arrows) return;
 
168
  for (i=0; i<10; i++)
 
169
  {
 
170
    if (arrowsy[i])
 
171
      put_picture(arrow, arrowsx[i]-arrow->w/2, arrowsy[i],
 
172
                  0, 0, arrow->w, arrow->h);
 
173
  }
 
174
}
 
175
 
 
176
static void
 
177
clear_arrows()
 
178
{
 
179
  int i;
 
180
  for (i=0; i<10; i++)
 
181
    set_arrow(i, 0);
 
182
}
 
183
 
 
184
static void
 
185
set_arrows()
 
186
{
 
187
  int i, j, k, src_next;
 
188
  for (i=0; i<10; i++)
 
189
  {
 
190
    Stack *dest = maincells[i]; 
 
191
    int dest_count = stack_count_cards(dest);
 
192
    int dest_top = stack_get_card(dest, dest_count-1);
 
193
    int n = 0;
 
194
    int add = 0;
 
195
 
 
196
    set_arrow(i, 0);
 
197
    if (dest_count == 0) { set_arrow(i,1); add=1; };
 
198
 
 
199
    for (j=0; j<10 && add==0; j++) {
 
200
      Stack *src = maincells[j];
 
201
      int src_count = stack_count_cards(src);
 
202
      int src_top;
 
203
      int src_next;
 
204
      n = src_count;
 
205
      while (n--) {
 
206
        if ((i == j) || (src_count == 0)) n = 0;
 
207
        else {
 
208
          src_top = stack_get_card(src, n);
 
209
          src_next = stack_get_card(src, n-1);
 
210
          if (VALUE(src_top) == VALUE(dest_top)-1) {
 
211
            int better = 0;
 
212
            //possible let's see if better
 
213
            if(n == 0) better = 1;
 
214
            if (!better) {
 
215
              if (VALUE(src_top) != VALUE(src_next)-1) better = 1;
 
216
            }
 
217
            if (!better) {
 
218
              if (SUIT(src_top) != SUIT(src_next)) better = 1;
 
219
            }
 
220
            if (better) add = 1;
 
221
          }
 
222
          if (n == 0 || VALUE(src_top) != VALUE(src_next)-1 ||
 
223
              SUIT(src_top) != SUIT(src_next) ||
 
224
              FACEDOWNP(src_next) == FACEDOWN ) n = 0;
 
225
        }
 
226
      }
 
227
    }
 
228
    if (add) set_arrow(i,1);
 
229
  }
 
230
}
 
231
 
 
232
 
 
233
static void
 
234
redraw()
 
235
{
 
236
  stack_redraw();
 
237
  redraw_arrows();
 
238
}
 
239
 
 
240
extern char spider_help[];
 
241
 
 
242
static void
 
243
key(int k, int x, int y)
 
244
{
 
245
  Picture *p = get_centered_pic();
 
246
 
 
247
  set_centered_pic(0);
 
248
  if (p == splash) return;
 
249
 
 
250
  switch(k) {
 
251
  case 3:  case 27: case 'q': case 'Q':
 
252
    exit(0);
 
253
    break;
 
254
  case KEY_F(1): case 'h': case 'H':
 
255
    help("bj.html", spider_help);
 
256
    break;
 
257
  case KEY_F(2):  case 'r':
 
258
    start_again();
 
259
    break;
 
260
  case 8:  case 127:  case KEY_DELETE:
 
261
    set_centered_pic(0);
 
262
    stack_undo();
 
263
    set_arrows();
 
264
    break;
 
265
  case '1': 
 
266
    suit_mask = 0;
 
267
    start_again();
 
268
    break;
 
269
  case '2': 
 
270
    suit_mask = 2;
 
271
    start_again();
 
272
    break;
 
273
  case '4':
 
274
    suit_mask = 3;
 
275
    start_again();
 
276
    break;
 
277
  case 'a':
 
278
    if (no_arrows) {
 
279
      no_arrows = 0;
 
280
      set_arrows();
 
281
    } else {
 
282
      clear_arrows();
 
283
      no_arrows = 1;
 
284
    }
 
285
    break;
 
286
  }
 
287
}
 
288
 
 
289
static Stack *src_stack = 0;
 
290
static int src_n = 0;
 
291
 
 
292
static Stack *dest_stack;
 
293
static int last_n=-1;
 
294
 
 
295
static int
 
296
n_droppable_s(Stack *dest_stack)
 
297
{
 
298
  int src_top, dest_top, src_count, dest_count, i, src_next;
 
299
 
 
300
  src_count = stack_count_cards(src_stack);
 
301
  dest_count = stack_count_cards(dest_stack);
 
302
  dest_top = stack_get_card(dest_stack, dest_count-1);
 
303
 
 
304
  for (i=0; i<10; i++) {
 
305
    if (dest_stack == maincells[i]) {
 
306
      int n = src_count;
 
307
 
 
308
      while (n--)
 
309
      {
 
310
        src_top = stack_get_card(src_stack, n);
 
311
        if ((VALUE(src_top) == VALUE(dest_top)-1)
 
312
            || (n == src_n && dest_count == 0)) return n;
 
313
 
 
314
        src_next = stack_get_card(src_stack, n-1);
 
315
        if (n == 0 || VALUE(src_top) != VALUE(src_next)-1 ||
 
316
            SUIT(src_top) != SUIT(src_next) ||
 
317
            FACEDOWNP(src_next) == FACEDOWN ) {
 
318
          if ((dest_count == 0) || (dest_stack == src_stack))return n;
 
319
          else return src_count;
 
320
        }
 
321
      }
 
322
      return src_count;
 
323
    }
 
324
  }
 
325
  return src_count;
 
326
}
 
327
 
 
328
static int
 
329
n_droppable(int x, int y)
 
330
{
 
331
  if (!src_stack)
 
332
    return -1;
 
333
  if (!stack_drag_find(x, y, &dest_stack))
 
334
  {
 
335
    dest_stack = src_stack;
 
336
    return last_n != -1 ? last_n : src_n;
 
337
  }
 
338
  return n_droppable_s(dest_stack);
 
339
}
 
340
 
 
341
 
 
342
static void
 
343
auto_move()
 
344
{
 
345
  int i, c, s, v, f, n;
 
346
 
 
347
  for (i=0; i<10; i++) {
 
348
    n = stack_count_cards(maincells[i]);
 
349
    c = stack_get_card(maincells[i],n-1);
 
350
    if (FACEDOWNP(c)) stack_flip_card(maincells[i], maincells[i]);
 
351
    if ((VALUE(c) == ACE) && n >= 13) {
 
352
      s = SUIT(c);
 
353
      v = 2;
 
354
      f = FACEUP;
 
355
      n -= 2;
 
356
      while((n>=0)&&(stack_get_card(maincells[i],n) == MAKE_CARD(s,v,f))) {
 
357
        n--; v++;
 
358
      }
 
359
      if (v == 14) {
 
360
        //Full suit to move, first find empty outcell
 
361
        c =0;
 
362
        while(stack_count_cards(outcells[c++]) != 0);
 
363
        c--;
 
364
        for(v=ACE; v<=KING; v++) stack_animate(maincells[i],outcells[c]);
 
365
        i--; // to flip any cards in this column
 
366
      }
 
367
    }
 
368
  }
 
369
}
 
370
 
 
371
static void
 
372
check_for_end_of_game()
 
373
{
 
374
  auto_move();
 
375
  if (stack_count_cards(outcells[7]) == 13) set_centered_pic(youwin);
 
376
  if (stack_count_cards(deck) == 0) {
 
377
    int i;
 
378
    int end = 1;
 
379
    set_arrows();
 
380
    for (i=0; i<10; i++) {
 
381
      if (arrowsy[i] != 0) end = 0;
 
382
    }
 
383
    //    if (end) set_centered_pic(youlose);
 
384
  }
 
385
}
 
386
 
 
387
static void
 
388
click(int x, int y, int b)
 
389
{
 
390
  int c, f;
 
391
  Picture *cp = get_centered_pic();
 
392
 
 
393
  if (cp == splash){
 
394
    set_centered_pic(0);
 
395
    return;
 
396
  }
 
397
 
 
398
  if (cp == youwin) {
 
399
    set_centered_pic(0);
 
400
    start_again();
 
401
    return;
 
402
  }
 
403
 
 
404
  if (cp == youlose) {
 
405
    set_centered_pic(0);
 
406
    return;
 
407
  }
 
408
 
 
409
  src_stack = 0;
 
410
  f = stack_find(x, y, &src_stack, &src_n);
 
411
  if (!f) return;
 
412
 
 
413
  if (b > 1)
 
414
  {
 
415
    stack_peek_card(src_stack, src_n, 1);
 
416
    return;
 
417
  }
 
418
  last_n = -1;
 
419
 
 
420
  for (c=0; c<8; c++)
 
421
    if (src_stack == outcells[c])
 
422
      return;
 
423
 
 
424
  if (src_stack == deck) {
 
425
    int empty_stack=0;
 
426
    for (c=0; c<10; c++) {
 
427
      if (stack_count_cards(maincells[c]) == 0) empty_stack = 1;
 
428
    }
 
429
    clear_arrows();
 
430
    if (empty_stack == 0) {
 
431
      for (c=0; c<10; c++) {
 
432
        stack_flip_card(deck, maincells[c]);
 
433
      }
 
434
    }
 
435
  }
 
436
 
 
437
  if (stack_count_cards(src_stack) > 0)
 
438
    stack_begin_drag(src_stack, src_n, x, y);
 
439
  else
 
440
    src_stack = 0;
 
441
}
 
442
 
 
443
static void
 
444
drag(int x, int y, int b)
 
445
{
 
446
  if (b > 1) return;
 
447
  last_n = n_droppable(x, y);
 
448
  stack_continue_drag(last_n, x, y);
 
449
}
 
450
 
 
451
static void
 
452
drop(int x, int y, int b)
 
453
{
 
454
  last_n = n_droppable(x, y); /* also sets dest_stack */
 
455
 
 
456
  if (b > 1)
 
457
  {
 
458
    if (src_stack)
 
459
      stack_peek_card(src_stack, src_n, 0);
 
460
    return;
 
461
  }
 
462
  clear_arrows();
 
463
  stack_drop(dest_stack, last_n);
 
464
  check_for_end_of_game();
 
465
  set_arrows();
 
466
}
 
467
 
 
468
static FunctionMapping fmap[] = {
 
469
  { "click", (void *)click },
 
470
  { "drag", (void *)drag },
 
471
  { "drop", (void *)drop },
 
472
  { "init", (void *)init },
 
473
  { "key", (void *)key },
 
474
  { "redraw", (void *)redraw },
 
475
  { "resize", (void *)resize },
 
476
  { 0, 0 }
 
477
};
 
478
 
 
479
int
 
480
main(int argc, char **argv)
 
481
{
 
482
  register_imagelib(appimglib_imagelib);
 
483
  init_ace(argc, argv, fmap);
 
484
  splash = get_picture("spider");
 
485
  youwin = get_picture("youwin");
 
486
  youlose = get_picture("youlose");
 
487
  arrow = get_picture("golf-arrow");
 
488
  no_arrow = get_picture("golf-noarrow");
 
489
  if (table_width == 0 || table_height == 0)
 
490
    {
 
491
      table_width = 10*(W+M) + M;
 
492
      table_height = 480;
 
493
    }
 
494
  init_table(table_width, table_height);
 
495
  table_loop();
 
496
  return 0;
 
497
}