game.c 63.1 KB
Newer Older
1
/*
2
 * Copyright © 1998, 2003 Jonathan Blandford <jrb@alum.mit.edu>
3
 * Copyright © 2007, 2011 Christian Persch
4
 *
5 6 7 8
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
9
 *
10 11 12 13
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14
 *
15 16
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 18
 */

Christian Persch's avatar
Christian Persch committed
19
#include <config.h>
20 21 22 23 24 25 26 27 28 29

#include <string.h>
#include <unistd.h>
#include <time.h>

#include <libguile.h>

#include <glib.h>
#include <glib/gi18n.h>

30 31 32
#include "ar-debug.h"
#include "ar-runtime.h"
#include "ar-string-utils.h"
33

34 35 36 37 38 39 40
#include "conf.h"
#include "util.h"

#include "game.h"

#define DELAYED_CALLBACK_DELAY (50)

41 42
#define I_(string) g_intern_static_string (string)

43 44 45 46 47
struct _AisleriotGameClass
{
  GObjectClass parent_class;
};

48
enum {
49
  NEW_GAME_LAMBDA,
50 51 52 53 54 55 56 57 58 59 60 61
  BUTTON_PRESSED_LAMBDA,
  BUTTON_RELEASED_LAMBDA,
  BUTTON_CLICKED_LAMBDA,
  BUTTON_DOUBLE_CLICKED_LAMBDA,
  GAME_OVER_LAMBDA,
  WINNING_GAME_LAMBDA,
  HINT_LAMBDA,
  GET_OPTIONS_LAMBDA,
  APPLY_OPTIONS_LAMBDA,
  TIMEOUT_LAMBDA,
  DROPPABLE_LAMBDA,
  DEALABLE_LAMBDA,
62 63
  N_LAMBDAS,
  LAST_MANDATORY_LAMBDA = TIMEOUT_LAMBDA
64 65
};

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
static const char lambda_names[] = {
  "new-game\0"
  "button-pressed\0"
  "button-released\0"
  "button-clicked\0"
  "button-double-clicked\0"
  "game-over\0"
  "winning-game\0"
  "hint\0"
  "get-options\0"
  "apply-options\0"
  "timeout\0"
  "droppable\0"
  "dealable\0"
};

82 83 84 85 86 87
struct _AisleriotGame
{
  GObject parent_instance;

  GPtrArray *slots;

88
  char *game_module;
Christian Persch's avatar
Christian Persch committed
89 90 91 92

  GRand *rand;
  GRand *saved_rand;

93
  guint delayed_call_timeout_id;
94

95
  GTimer *timer;
96

97
  int timeout;
98
  char *score;
99

100 101 102 103
  double width;
  double height;

  /* Game callbacks */
104
  SCM lambdas[N_LAMBDAS];
105 106 107 108

  guint click_to_move : 1;
  guint can_undo : 1;
  guint can_redo : 1;
Christian Persch's avatar
Christian Persch committed
109
  guint can_deal : 1;
110
  guint show_score : 1;
Christian Persch's avatar
Christian Persch committed
111
  guint features : 3; /* enough bits for ALL_FEATURES */
112 113 114 115 116 117 118 119 120 121 122 123 124
  guint state : 3; /* enough bits for LAST_GAME_STATE */
  guint had_exception : 1;
  guint paused : 1;
};

/* The one and only game */
AisleriotGame *app_game;

enum
{
  PROP_0,
  PROP_CAN_UNDO,
  PROP_CAN_REDO,
Christian Persch's avatar
Christian Persch committed
125
  PROP_CAN_DEAL,
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  PROP_GAME_FILE,
  PROP_SCORE,
  PROP_STATE,
};

enum
{
  GAME_TYPE,
  GAME_CLEARED,
  GAME_NEW,
  GAME_MESSAGE,
  SLOT_CHANGED,
  EXCEPTION,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_TYPE (AisleriotGame, aisleriot_game, G_TYPE_OBJECT);

/* helper functions */

148 149 150 151 152 153 154 155 156
static void
clear_delayed_call (AisleriotGame *game)
{
  if (game->delayed_call_timeout_id != 0) {
    g_source_remove (game->delayed_call_timeout_id);
    game->delayed_call_timeout_id = 0;
  }
}

157 158 159 160 161 162 163 164
static void
set_game_state (AisleriotGame *game,
                guint state)
{
  if (game->state != state) {
    game->state = state;

    if (state == GAME_RUNNING) {
165 166 167
      /* Reset the timer */
      g_timer_start (game->timer);
    } else if (state >= GAME_OVER) {
Christian Persch's avatar
Christian Persch committed
168
      /* Stop the timer now so we will record the right time. See bug #514239. */
169
      g_timer_stop (game->timer);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    }

    g_object_notify (G_OBJECT (game), "state");
  }
}

static void
set_game_undoable (AisleriotGame *game,
                   gboolean enabled)
{
  if (enabled != game->can_undo) {
    game->can_undo = enabled;

    g_object_notify (G_OBJECT (game), "can-undo");
  }
}

static void
set_game_redoable (AisleriotGame *game,
                   gboolean enabled)
{
  if (enabled != game->can_redo) {
    game->can_redo = enabled;

    g_object_notify (G_OBJECT (game), "can-redo");
  }
}

Christian Persch's avatar
Christian Persch committed
198 199 200 201 202
static void
set_game_dealable (AisleriotGame *game,
                   gboolean enabled)
{
  if (enabled != game->can_deal) {
203
    game->can_deal = enabled != FALSE;
Christian Persch's avatar
Christian Persch committed
204 205 206 207 208

    g_object_notify (G_OBJECT (game), "can-deal");
  }
}

Christian Persch's avatar
Christian Persch committed
209
/* Yes, there is a race condition here, but since this is all
210 211 212 213 214 215 216 217 218
   per user they're going to have to be either playing impossibly fast
   or using sharing an account. In the later case they won't be caring
   about the statistics. */
static void
update_statistics (AisleriotGame *game)
{
  AisleriotStatistic current_stats;
  time_t t;

219
  aisleriot_conf_get_statistic (game->game_module, &current_stats);
220 221 222 223 224 225

  current_stats.total++;

  if (game->state == GAME_WON) {
    current_stats.wins++;

226
    t = (time_t) (g_timer_elapsed (game->timer, NULL) + 0.5);
227 228 229 230 231 232 233 234 235 236
    if (t > 0) {
      if ((current_stats.best == 0) || (t < current_stats.best)) {
	current_stats.best = t;
      }
      if (t > current_stats.worst) {
	current_stats.worst = t;
      }
    }
  }

237
  aisleriot_conf_set_statistic (game->game_module, &current_stats);
238 239 240 241 242 243
}

static void
clear_slots (AisleriotGame *game,
             gboolean notify)
{
244
  guint i, n_slots;
245 246 247

  n_slots = game->slots->len;
  for (i = 0; i < n_slots; ++i) {
248
    ArSlot *slot = game->slots->pdata[i];
249

250 251
    g_ptr_array_free (slot->card_images, TRUE);
    g_byte_array_free (slot->cards, TRUE);
252

253
    g_slice_free (ArSlot, slot);
254 255 256 257 258 259 260 261 262
  }

  g_ptr_array_set_size (game->slots, 0);

  if (notify) {
    g_signal_emit (game, signals[GAME_CLEARED], 0);
  }
}

263
static ArSlot *
264 265 266 267 268 269 270
get_slot (AisleriotGame *game,
          gint slotid)
{
  guint i, n_slots;

  n_slots = game->slots->len;
  for (i = 0; i < n_slots; ++i) {
271
    ArSlot *hslot = game->slots->pdata[i];
272 273 274 275 276 277 278 279 280 281 282 283 284

    if (hslot->id == slotid)
      return hslot;
  }

  return NULL;
}


/* Scheme helpers */

typedef struct {
  SCM lambda;
285 286
  SCM *args;
  gsize n_args;
287 288
} CallData;

289 290
static char *
cscmi_exception_get_backtrace (SCM tag, SCM throw_args)
291 292 293 294 295 296
{
  AisleriotGame *game = app_game;
  SCM port;
  SCM stack;
  GPtrArray *slots;
  guint i, n_slots;
297 298
  GString *message;
  char *string;
299

300 301
  scm_dynwind_begin (0);

302
  message = g_string_sized_new (1024);
303

304
  g_string_append_printf (message, "Variation: %s\n", aisleriot_game_get_game_module (game));
Christian Persch's avatar
Christian Persch committed
305
#if 0
306
  g_string_append_printf (message, "Seed: %u\n", game->seed);
Christian Persch's avatar
Christian Persch committed
307
#endif
308

309
  g_string_append (message, "Scheme error:\n\t");
310 311

  port = scm_open_output_string ();
312
  scm_display (throw_args, port);
313
  string = scm_to_utf8_string (scm_get_output_string (port));
314 315
  scm_dynwind_free (string);
  scm_close_output_port (port);
316
  g_string_append (message, string);
317

318
  port = scm_open_output_string ();
319
  g_string_append (message, "\nScheme tag:\n\t");
320
  scm_display (tag, port);
321
  string = scm_to_utf8_string (scm_get_output_string (port));
322 323
  scm_dynwind_free (string);
  scm_close_output_port (port);
324
  g_string_append (message, string);
325

326
  g_string_append (message, "\n\nBacktrace:\n");
Christian Persch's avatar
Christian Persch committed
327 328
  stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
  if (!scm_is_false (stack)) {
329
    port = scm_open_output_string ();
330
    scm_display_backtrace (stack, port, SCM_UNDEFINED, SCM_UNDEFINED);
331
    string = scm_to_utf8_string (scm_get_output_string (port));
332 333
    scm_dynwind_free (string);
    scm_close_output_port (port);
334
    g_string_append (message, string);
335
  } else {
336
    g_string_append (message, "\tNo backtrace available.\n");
337 338
  }

339
  g_string_append (message, "\n\nDeck State:\n");
340 341 342 343 344 345

  slots = aisleriot_game_get_slots (game);

  n_slots = slots->len;
  if (n_slots > 0) {
    for (i = 0; i < n_slots; ++i) {
346
      ArSlot *slot = slots->pdata[i];
347 348 349
      GByteArray *cards = slot->cards;
      guint n_cards;

350
      g_string_append_printf (message, "\tSlot %d\n", slot->id);
351 352 353 354 355 356 357 358 359

      n_cards = cards->len;
      if (n_cards > 0) {
        guint8 *data = cards->data;
        guint count;

        for (count = 0; count < n_cards; ++count) {
          Card card = CARD (data[count]);

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
          if (count == 0) {
            g_string_append_c (message, '\t');
            g_string_append_c (message, '\t');
          } else {
            g_string_append_c (message, ' ');
            g_string_append_c (message, ',');
          }

          g_string_append_printf (message,
                                  "(%d %d %s)",
                                  CARD_GET_SUIT (card),
                                  CARD_GET_RANK (card),
                                  /* See c2scm_card below */
                                  CARD_GET_FACE_DOWN (card) ? "#f" : "#t");

375 376
          count++;
          if (count == 5) {
377
            g_string_append_c (message, '\n');
378 379 380 381
            count = 0;
          }
        }
        if (count != 0) {
382
          g_string_append_c (message, '\n');
383 384
        }
      } else {
385
        g_string_append (message,"\t\t(Empty)\n");
386 387 388
      }
    }
  } else {
389
    g_string_append (message, "\tNo cards in deck\n");
390
  }
391

392 393
  scm_dynwind_end ();

394
  return g_string_free (message, FALSE);
395 396 397 398 399 400
}

/* Called when we get an exception from guile.  We launch bug-buddy with the
 * exception information:
 */
static SCM
401 402 403
game_scm_pre_unwind_handler (void *user_data,
                             SCM tag,
                             SCM throw_args)
404
{
405 406
  GError **error = user_data;
  char *message;
407

408 409 410
  /* Not interested in errors, or already had an exception */
  if (error == NULL || *error != NULL)
    return SCM_UNDEFINED;
411

412
  message = cscmi_exception_get_backtrace (tag, throw_args);
413 414 415 416 417
  g_set_error_literal (error,
                       AISLERIOT_GAME_ERROR,
                       GAME_ERROR_EXCEPTION,
                       message ? message
                               : "A scheme exception occurred, but there was no exception info");
418 419 420 421
  return SCM_UNDEFINED;
}

static SCM
422 423 424
game_scm_catch_handler (void *user_data,
                        SCM tag,
                        SCM throw_args)
425
{
426 427
  return SCM_UNDEFINED;
}
428

429 430 431 432
static SCM
game_scm_call_lambda (void *user_data)
{
  CallData *data = (CallData *) user_data;
433

434 435 436 437
#if SCM_MAJOR_VERSION >= 2
  return scm_call_n (data->lambda, data->args, data->n_args);
#else
  /* Guile 1.8 lacks the scm_call_n function */
438 439
  switch (data->n_args) {
    case 0:
440
      return scm_call_0 (data->lambda);
441
    case 1:
442
      return scm_call_1 (data->lambda, data->args[0]);
443
    case 2:
444
      return scm_call_2 (data->lambda, data->args[0], data->args[1]);
445
    case 3:
446
      return scm_call_3 (data->lambda, data->args[0], data->args[1], data->args[2]);
447 448 449
    default:
      g_assert_not_reached ();
  }
450 451
#endif
}
452

453 454 455 456 457 458
static gboolean
game_scm_call (SCM lambda,
               SCM *args,
               gsize n_args,
               SCM *retval)
{
459 460
  CallData data = { lambda, args, n_args };
  GError *error = NULL;
461 462 463 464
  SCM rv;

  rv = scm_c_catch (SCM_BOOL_T,
                    game_scm_call_lambda, &data,
465 466 467
                    game_scm_catch_handler, NULL,
                    game_scm_pre_unwind_handler, &error);
  if (error) {
468 469
    app_game->had_exception = TRUE;

470 471 472 473 474 475
    g_signal_emit (app_game, signals[EXCEPTION], 0, error);
    g_error_free (error);

    /* This game is over, but don't count it in the statistics */
    set_game_state (app_game, GAME_LOADED);

476
    return FALSE;
477
  }
478 479 480 481 482

  if (retval)
    *retval = rv;

  return TRUE;
483 484
}

485 486 487 488 489
static gboolean
game_scm_call_by_name (const char *name,
                       SCM *args,
                       gsize n_args,
                       SCM *retval)
490
{
491
  SCM lambda;
492

493
  lambda = scm_c_eval_string (name);
494

495 496 497 498 499
  if (!game_scm_call (lambda, args, n_args, retval))
    return FALSE;

  scm_remember_upto_here_1 (lambda);
  return TRUE;
500 501 502 503 504 505 506 507 508 509 510
}

static SCM
c2scm_card (Card card)
{
  return scm_cons (scm_from_uint (CARD_GET_RANK (card)),
                   scm_cons (scm_from_uint (CARD_GET_SUIT (card)),
                             scm_cons (SCM_BOOL (!CARD_GET_FACE_DOWN (card)),
                                       SCM_EOL)));
}

511 512 513
static void
scm2c_card (SCM card_data,
            Card *card)
514 515 516
{
  guint rank, suit, face_down;

517
  card->value = 0;
518 519 520

  rank = scm_to_int (SCM_CAR (card_data));
  suit = scm_to_int (SCM_CADR (card_data));
521
  face_down = !(scm_is_true (SCM_CADDR (card_data)));
522

523 524 525
  card->attr.rank = rank;
  card->attr.suit = suit;
  card->attr.face_down = face_down;
526 527 528 529 530 531
}

static SCM
c2scm_deck (guint8 *cards,
            guint n_cards)
{
532
  SCM scm_cards;
533 534
  guint i;

535
  scm_cards = SCM_EOL;
536 537 538 539 540 541 542 543
  for (i = 0; i < n_cards; ++i) {
    scm_cards = scm_cons (c2scm_card (CARD (cards[i])), scm_cards);
  }

  return scm_cards;
}

static void
544
cscmi_slot_set_cards (ArSlot *slot,
545 546 547 548 549 550 551
                      SCM cards)
{
  AisleriotGame *game = app_game;
  SCM list_el;
  guint8 *data = NULL;
  guint i, n_cards = 0;

Christian Persch's avatar
Christian Persch committed
552
  if (scm_is_true (scm_list_p (cards))) {
553 554 555 556 557 558 559 560
    for (list_el = cards; list_el != SCM_EOL; list_el = SCM_CDR (list_el)) {
      ++n_cards;
    }

    data = g_alloca (n_cards);
    i = n_cards;

    for (list_el = cards; list_el != SCM_EOL; list_el = SCM_CDR (list_el)) {
561 562 563 564
      Card card;

      scm2c_card (SCM_CAR (list_el), &card);
      data[--i] = CARD_UINT (card);
565 566 567 568 569 570 571 572 573 574 575 576 577
    }
  }

  /* Don't set the new cards if the same cards are already there.
   * This saves us lots of updates on undo/redo.
   */
  if (slot->cards->len == n_cards &&
      memcmp (slot->cards->data, data, n_cards) == 0)
    return;

  g_byte_array_set_size (slot->cards, 0);

  aisleriot_game_slot_add_cards (game, slot, data, n_cards);
578
}
579

580
static SCM
581 582 583
cscmi_add_slot (SCM slot_data)
{
  AisleriotGame *game = app_game;
584
  ArSlot *slot;
585 586 587
  gboolean expanded_down = FALSE;
  gboolean expanded_right = FALSE;
  int expansion_depth = 0;
588 589
  ArSlotType type = AR_SLOT_UNKNOWN;
  SCM slot_placement, slot_type;
590 591

  if (game->state > GAME_BEGIN) {
Christian Persch's avatar
Christian Persch committed
592
    return scm_throw (scm_from_locale_symbol ("aisleriot-invalid-call"),
593
                      scm_list_1 (scm_from_utf8_string ("Cannot add a new slot after the game has started.")));
594 595
  }

596
#define EQUALS_SYMBOL(string,object) (scm_is_true (scm_equal_p (scm_from_locale_symbol (string), object)))
597

598 599
  slot_placement = SCM_CADDR (slot_data);
  if (EQUALS_SYMBOL ("expanded", SCM_CAR (slot_placement))) {
600
    expanded_down = TRUE;
601
  } else if (EQUALS_SYMBOL ("expanded-right", SCM_CAR (slot_placement))) {
602
    expanded_right = TRUE;
603
  } else if (EQUALS_SYMBOL ("partially-expanded", SCM_CAR (slot_placement))) {
604
    expanded_down = TRUE;
605 606
    expansion_depth = scm_to_int (SCM_CADDR (slot_placement));
  } else if (EQUALS_SYMBOL ("partially-expanded-right", SCM_CAR (slot_placement))) {
607
    expanded_right = TRUE;
608
    expansion_depth = scm_to_int (SCM_CADDR (slot_placement));
609 610 611 612
  }

#undef CHECK_EXPANSION

613 614 615
  /* 3rd argument is the slot type (optionally) */
  slot_type = SCM_CDDDR (slot_data);
  if (slot_type != SCM_EOL) {
616 617 618
    if (EQUALS_SYMBOL ("chooser", SCM_CAR (slot_type))) {
      type = AR_SLOT_CHOOSER;
    } else if (EQUALS_SYMBOL ("foundation", SCM_CAR (slot_type))) {
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
      type = AR_SLOT_FOUNDATION;
    } else if (EQUALS_SYMBOL ("reserve", SCM_CAR (slot_type))) {
      type = AR_SLOT_RESERVE;
    } else if (EQUALS_SYMBOL ("stock", SCM_CAR (slot_type))) {
      type = AR_SLOT_STOCK;
    } else if (EQUALS_SYMBOL ("tableau", SCM_CAR (slot_type))) {
      type = AR_SLOT_TABLEAU;
    } else if (EQUALS_SYMBOL ("waste", SCM_CAR (slot_type))) {
      type = AR_SLOT_WASTE;
    }
  }

#undef EQUALS_SYMBOL

#ifdef GNOME_ENABLE_DEBUG
634
  _AR_DEBUG_IF (AR_DEBUG_SCHEME) {
635
    static const char *types[] = { "unknown", "chooser", "foundation", "reserve", "stock", "tableau", "waste" };
636

637
    ar_debug_print (AR_DEBUG_SCHEME,
638 639 640 641 642
                        "Adding new slot %d type %s\n",
                        scm_to_int (SCM_CAR (slot_data)), types[type]);
  }
#endif /* GNOME_ENABLE_DEBUG */

643
  /* create and initialize slot */
644
  slot = g_slice_new0 (ArSlot);
645

646 647 648
  g_ptr_array_add (game->slots, slot);

  slot->id = scm_to_int (SCM_CAR (slot_data));
649
  slot->type = type;
650 651 652

  slot->cards = g_byte_array_sized_new (SLOT_CARDS_N_PREALLOC);
  slot->exposed = 0;
653 654
  slot->x = scm_to_double (SCM_CAR (SCM_CADR (SCM_CADDR (slot_data))));
  slot->y = scm_to_double (SCM_CADR (SCM_CADR (SCM_CADDR (slot_data))));
655 656 657 658 659 660 661

  slot->expansion_depth = expansion_depth;

  slot->expansion.dx = 0.0;
  slot->expanded_down = expanded_down != FALSE;
  slot->expanded_right = expanded_right != FALSE;

662 663
  slot->card_images = g_ptr_array_sized_new (SLOT_CARDS_N_PREALLOC);

664 665 666 667
  slot->needs_update = TRUE;

  /* this will update the slot length too */
  cscmi_slot_set_cards (slot, SCM_CADR (slot_data));
668 669

  return SCM_EOL;
670 671 672 673 674 675 676 677 678 679
}

/* Scheme functions */

static SCM
scm_undo_set_sensitive (SCM in_state)
{
  AisleriotGame *game = app_game;
  gboolean state;

680
  state = scm_is_true (in_state) ? TRUE : FALSE;
681 682 683 684 685 686 687 688 689 690 691
  set_game_undoable (game, state);

  return SCM_EOL;
}

static SCM
scm_redo_set_sensitive (SCM in_state)
{
  AisleriotGame *game = app_game;
  gboolean state;

692
  state = scm_is_true (in_state) ? TRUE : FALSE;
693 694 695 696 697
  set_game_redoable (game, state);

  return SCM_EOL;
}

Christian Persch's avatar
Christian Persch committed
698 699 700 701 702 703
static SCM
scm_dealable_set_sensitive (SCM in_state)
{
  AisleriotGame *game = app_game;
  gboolean state;

704
  state = scm_is_true (in_state) ? TRUE : FALSE;
Christian Persch's avatar
Christian Persch committed
705 706 707 708 709
  set_game_dealable (game, state);

  return SCM_EOL;
}

710
static SCM
711
scm_get_feature_word (void)
712 713 714
{
  AisleriotGame *game = app_game;

715 716 717
  g_return_val_if_fail (game != NULL, SCM_EOL);
  
  return scm_from_uint (game->features);
718 719 720 721 722 723 724
}

static SCM
scm_set_feature_word (SCM features)
{
  AisleriotGame *game = app_game;

725
  g_return_val_if_fail (game != NULL, SCM_EOL);
726

727
  game->features = scm_to_uint (features);
728 729 730 731 732 733 734 735

  return SCM_EOL;
}

static SCM
scm_set_statusbar_message (SCM message)
{
  AisleriotGame *game = app_game;
736
  char *str;
737 738 739 740

  if (!scm_is_string (message))
    return SCM_EOL;

741 742
  scm_dynwind_begin (0);

743
  str = scm_to_utf8_string (message);
744
  scm_dynwind_free (str);
745
  if (!str)
746
    goto out;
747

748
  g_signal_emit (game, signals[GAME_MESSAGE], 0, str);
749

750 751 752
out:
  scm_dynwind_end ();

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
  return SCM_EOL;
}

static SCM
scm_reset_surface (void)
{
  AisleriotGame *game = app_game;

  clear_slots (game, TRUE);
  return SCM_EOL;
}

static SCM
scm_set_slot_x_expansion (SCM scm_slot_id,
                          SCM new_exp_val)
{
  AisleriotGame *game = app_game;
770
  ArSlot *slot;
771

772
  slot = get_slot (game, scm_to_int (scm_slot_id));
773 774 775 776 777 778

  /* We should only set the x expansion for right-expanded slots! */
  g_return_val_if_fail (slot->expanded_right, SCM_EOL);
  /* Cannot set x and y expansion at the same time */
  g_return_val_if_fail (!slot->dy_set, SCM_EOL);

779
  slot->expansion.dx = scm_to_double (new_exp_val);
780 781 782 783 784 785 786 787 788 789 790 791 792 793
  slot->dx_set = TRUE;

  /* We don't need to emit the slot-changed signal here,
   * since we should be here only during game initialisation,
   * which means that there will be a slot-changed later anyway.
   */
  return SCM_EOL;
}

static SCM
scm_set_slot_y_expansion (SCM scm_slot_id,
                          SCM new_exp_val)
{
  AisleriotGame *game = app_game;
794
  ArSlot *slot;
795

796
  slot = get_slot (game, scm_to_int (scm_slot_id));
797 798 799 800 801 802

  /* We should only set the y expansion for down-expanded slots! */
  g_return_val_if_fail (slot->expanded_down, SCM_EOL);
  /* Cannot set x and y expansion at the same time */
  g_return_val_if_fail (!slot->dx_set, SCM_EOL);

803
  slot->expansion.dy = scm_to_double (new_exp_val);
804 805 806 807 808 809 810 811 812 813 814 815 816
  slot->dy_set = TRUE;

  /* We don't need to emit the slot-changed signal here,
   * since we should be here only during game initialisation,
   * which means that there will be a slot-changed later anyway.
   */
  return SCM_EOL;
}

static SCM
scm_get_slot (SCM scm_slot_id)
{
  AisleriotGame *game = app_game;
817
  ArSlot *slot;
818

819
  slot = get_slot (game, scm_to_int (scm_slot_id));
820 821 822 823 824 825 826 827 828 829 830 831 832 833

  if (!slot)
    return SCM_EOL;

  return scm_cons (scm_slot_id,
                   scm_cons (c2scm_deck (slot->cards->data, slot->cards->len),
                             SCM_EOL));
}

static SCM
scm_set_cards (SCM scm_slot_id,
               SCM new_cards)
{
  AisleriotGame *game = app_game;
834
  ArSlot *slot;
835

836
  slot = get_slot (game, scm_to_int (scm_slot_id));
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855

  cscmi_slot_set_cards (slot, new_cards);

  return SCM_BOOL_T;
}

static SCM
scm_set_lambda (SCM start_game_lambda,
                SCM pressed_lambda,
                SCM released_lambda,
                SCM clicked_lambda,
                SCM dbl_clicked_lambda,
                SCM game_over_lambda,
                SCM winning_game_lambda,
                SCM hint_lambda,
                SCM rest)
{
  AisleriotGame *game = app_game;

856
  game->lambdas[NEW_GAME_LAMBDA] = start_game_lambda;
857 858 859 860 861 862 863
  game->lambdas[BUTTON_PRESSED_LAMBDA] = pressed_lambda;
  game->lambdas[BUTTON_RELEASED_LAMBDA] = released_lambda;
  game->lambdas[BUTTON_CLICKED_LAMBDA] = clicked_lambda;
  game->lambdas[BUTTON_DOUBLE_CLICKED_LAMBDA] = dbl_clicked_lambda;
  game->lambdas[GAME_OVER_LAMBDA] = game_over_lambda;
  game->lambdas[WINNING_GAME_LAMBDA] = winning_game_lambda;
  game->lambdas[HINT_LAMBDA] = hint_lambda;
864

865
  game->lambdas[GET_OPTIONS_LAMBDA] = SCM_CAR (rest);
866 867
  rest = SCM_CDR (rest);

868
  game->lambdas[APPLY_OPTIONS_LAMBDA] = SCM_CAR (rest);
869 870
  rest = SCM_CDR (rest);

871
  game->lambdas[TIMEOUT_LAMBDA] = SCM_CAR (rest);
872
  rest = SCM_CDR (rest);
873

874
  if (game->features & FEATURE_DROPPABLE) {
875
    game->lambdas[DROPPABLE_LAMBDA] = SCM_CAR (rest);
876 877
    rest = SCM_CDR (rest);
  } else {
878
    game->lambdas[DROPPABLE_LAMBDA] = SCM_UNDEFINED;
879
  }
880

881
  if (game->features & FEATURE_DEALABLE) {
882
    game->lambdas[DEALABLE_LAMBDA] = SCM_CAR (rest);
883 884
    rest = SCM_CDR (rest);
  } else {
885
    game->lambdas[DEALABLE_LAMBDA] = SCM_UNDEFINED;
886 887
  }

888 889 890
  return SCM_EOL;
}

891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
static SCM
scm_set_lambda_x (SCM symbol,
                  SCM lambda)
{
  AisleriotGame *game = app_game;
  const char *lambda_name;
  int i;

  lambda_name = lambda_names;
  for (i = 0; i < N_LAMBDAS; ++i) {
    if (scm_is_true (scm_equal_p (symbol, scm_from_locale_symbol (lambda_name)))) {
      game->lambdas[i] = lambda;
      return SCM_EOL;
    }

    lambda_name += strlen (lambda_name) + 1;
  }

  return scm_throw (scm_from_locale_symbol ("aisleriot-invalid-call"),
910
                    scm_list_1 (scm_from_utf8_string ("Unknown lambda name in set-lambda!")));
911 912
}

913 914 915
static SCM
scm_myrandom (SCM range)
{
Christian Persch's avatar
Christian Persch committed
916 917 918
  AisleriotGame *game = app_game;

  return scm_from_uint32 (g_rand_int_range (game->rand, 0, scm_to_int (range)));
919 920 921 922 923 924 925 926 927 928 929 930 931 932
}

static SCM
scm_click_to_move_p (void)
{
  /* This only affects elevator and escalator games. Their code claims
   * that in click-to-move makes no sense to move the cards away, but that's
   * bogus. Just always return FALSE here instead of
   * game->click_to_move ? SCM_BOOL_T : SCM_BOOL_F
   */
  return SCM_BOOL_F;
}

static SCM
933
scm_update_score (SCM new_score)
934 935
{
  AisleriotGame *game = app_game;
936
  char *score;
937

938
  score = scm_to_utf8_string (new_score);
939 940
  if (g_strcmp0 (score, game->score) != 0) {
    free (game->score);
941
    game->score = score;
942

943
    g_object_notify (G_OBJECT (game), "score");
944 945
  } else {
    free (score);
946
  }
947

948
  return new_score;
949 950 951 952 953 954 955 956 957
}

static SCM
scm_set_timeout (SCM new)
{
  AisleriotGame *game = app_game;

  g_warning ("(set-timeout) unimplemented\n");

958
  game->timeout = scm_to_int (new);
959 960 961 962 963

  return new;
}

static SCM
964
scm_get_timeout (void)
965 966 967 968 969
{
  AisleriotGame *game = app_game;

  g_warning ("(get-timeout) unimplemented\n");

970
  return scm_from_int (game->timeout);
971 972 973 974 975
}

static void
scm_delayed_call_destroy_data (SCM callback)
{
976 977
  AisleriotGame *game = app_game;

978
  scm_gc_unprotect_object (callback);
979 980

  game->delayed_call_timeout_id = 0;
981 982
}

983
/* @callback is GC protected during this call! */
984 985 986 987 988
static gboolean
scm_execute_delayed_function (SCM callback)
{
  AisleriotGame *game = app_game;

989 990 991 992 993
  /* We set game->delayed_call_timeout_id to 0 _before_ calling |callback|,
   * since it might install a new delayed call.
   */
  game->delayed_call_timeout_id = 0;

994
  if (!game_scm_call (callback, NULL, 0, NULL))
995
    return FALSE;
996 997 998 999 1000 1001 1002 1003 1004

  aisleriot_game_test_end_of_game (game);

  return FALSE;
}

static SCM
scm_delayed_call (SCM callback)
{
1005 1006 1007 1008
  AisleriotGame *game = app_game;

  /* We can only have one pending delayed call! */
  if (game->delayed_call_timeout_id != 0) {
Christian Persch's avatar
Christian Persch committed
1009
    return scm_throw (scm_from_locale_symbol ("aisleriot-invalid-call"),
1010
                      scm_list_1 (scm_from_utf8_string ("Already have a delayed callback pending.")));
1011 1012
  }

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
  /* We need to protect the callback data from being GC'd until the
   * timeout has run.
   */
  scm_gc_protect_object (callback);

  g_timeout_add_full (G_PRIORITY_LOW,
                      DELAYED_CALLBACK_DELAY,
                      (GSourceFunc) scm_execute_delayed_function,
                      callback,
                      (GDestroyNotify) scm_delayed_call_destroy_data);

  return SCM_BOOL_T;
}

static void
1028
cscm_init (void *data G_GNUC_UNUSED)
1029 1030 1031 1032
{
  /* Let the scheme side of things know about our C functions. */
  scm_c_define_gsubr ("set-feature-word!", 1, 0, 0, scm_set_feature_word);
  scm_c_define_gsubr ("get-feature-word", 0, 0, 0, scm_get_feature_word);
1033
  scm_c_define_gsubr ("set-statusbar-message-c", 1, 0, 0,
1034 1035
                      scm_set_statusbar_message);
  scm_c_define_gsubr ("reset-surface", 0, 0, 0, scm_reset_surface);
1036
  scm_c_define_gsubr ("add-slot", 1, 0, 0, cscmi_add_slot);
1037 1038 1039 1040 1041 1042 1043
  scm_c_define_gsubr ("get-slot", 1, 0, 0, scm_get_slot);
  scm_c_define_gsubr ("set-cards-c!", 2, 0, 0, scm_set_cards);
  scm_c_define_gsubr ("set-slot-y-expansion!", 2, 0, 0,
                      scm_set_slot_y_expansion);
  scm_c_define_gsubr ("set-slot-x-expansion!", 2, 0, 0,
                      scm_set_slot_x_expansion);
  scm_c_define_gsubr ("set-lambda", 8, 0, 1, scm_set_lambda);
1044
  scm_c_define_gsubr ("set-lambda!", 2, 0, 0, scm_set_lambda_x);
1045
  scm_c_define_gsubr ("aisleriot-random", 1, 0, 0, scm_myrandom);
1046
  scm_c_define_gsubr ("click-to-move?", 0, 0, 0, scm_click_to_move_p);
1047
  scm_c_define_gsubr ("update-score", 1, 0, 0, scm_update_score);
1048 1049 1050 1051 1052
  scm_c_define_gsubr ("get-timeout", 0, 0, 0, scm_get_timeout);
  scm_c_define_gsubr ("set-timeout!", 1, 0, 0, scm_set_timeout);
  scm_c_define_gsubr ("delayed-call", 1, 0, 0, scm_delayed_call);
  scm_c_define_gsubr ("undo-set-sensitive", 1, 0, 0, scm_undo_set_sensitive);
  scm_c_define_gsubr ("redo-set-sensitive", 1, 0, 0, scm_redo_set_sensitive);
Christian Persch's avatar
Christian Persch committed
1053
  scm_c_define_gsubr ("dealable-set-sensitive", 1, 0, 0, scm_dealable_set_sensitive);
1054 1055 1056

  scm_c_export ("set-feature-word!", 
                "get-feature-word", 
1057
                "set-statusbar-message-c",
1058 1059 1060 1061 1062 1063 1064
                "reset-surface",
                "add-slot", 
                "get-slot", 
                "set-cards-c!",
                "set-slot-y-expansion!", 
                "set-slot-x-expansion!",
                "set-lambda", 
1065
                "set-lambda!",
1066 1067
                "aisleriot-random", 
                "click-to-move?", 
1068
                "update-score", 
1069 1070 1071 1072 1073 1074 1075
                "get-timeout", 
                "set-timeout!", 
                "delayed-call", 
                "undo-set-sensitive", 
                "redo-set-sensitive", 
                "dealable-set-sensitive",
                NULL);
1076 1077
}

1078 1079 1080
static void
update_game_dealable (AisleriotGame *game)
{
1081
  SCM retval;
1082 1083 1084 1085

  if ((game->features & FEATURE_DEALABLE) == 0)
    return;

1086
  if (!game_scm_call (game->lambdas[DEALABLE_LAMBDA], NULL, 0, &retval))
1087
    return;
1088

1089
  set_game_dealable (game, scm_is_true (retval));
1090 1091
}

1092 1093 1094 1095
static gboolean
cscmi_game_over_lambda (void)
{
  AisleriotGame *game = app_game;
1096
  SCM retval;
1097

1098
  if (!game_scm_call (game->lambdas[GAME_OVER_LAMBDA], NULL, 0, &retval))
1099
    return TRUE;
1100

1101
  return scm_is_true (retval);
1102 1103 1104 1105 1106 1107
}

static gboolean
cscmi_winning_game_lambda (void)
{
  AisleriotGame *game = app_game;
1108
  SCM retval;
1109

1110
  if (!game_scm_call (game->lambdas[WINNING_GAME_LAMBDA], NULL, 0, &retval))
1111
    return FALSE;
1112

1113
  return scm_is_true (retval);
1114 1115 1116 1117 1118 1119 1120
}

/* Class implementation */

static void
aisleriot_game_init (AisleriotGame *game)
{
Christian Persch's avatar
Christian Persch committed
1121 1122
  game->rand = game->saved_rand = NULL;

1123 1124 1125 1126
  game->state = GAME_UNINITIALISED;

  game->slots = g_ptr_array_sized_new (SLOT_CARDS_N_PREALLOC);

1127 1128
  game->timer = g_timer_new ();

1129
  game->timeout = 60 * 60;
1130
  game->score = NULL;
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
}

static GObject *
aisleriot_game_constructor (GType type,
                            guint n_construct_properties,
                            GObjectConstructParam *construct_params)
{
  GObject *object;

  g_assert (app_game == NULL);

  object = G_OBJECT_CLASS (aisleriot_game_parent_class)->constructor
             (type, n_construct_properties, construct_params);

  app_game = AISLERIOT_GAME (object);

  return object;
}

static void
aisleriot_game_finalize (GObject *object)
{
  AisleriotGame *game = AISLERIOT_GAME (object);

  /* Update the statistics */
  /* FIXMEchpe unless we're saving to session and exiting because of session logout! */
  if (game->state >= GAME_RUNNING) {
    update_statistics (game);
  }

1161
  clear_delayed_call (game);
1162 1163 1164
  clear_slots (game, FALSE);
  g_ptr_array_free (game->slots, TRUE);

1165
  g_free (game->game_module);
1166

1167 1168
  g_timer_destroy (game->timer);

Christian Persch's avatar
Christian Persch committed
1169 1170 1171 1172 1173
  if (game->rand)
    g_rand_free (game->rand);
  if (game->saved_rand)
    g_rand_free (game->saved_rand);

1174 1175
  free (game->score);

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
  app_game = NULL;

  G_OBJECT_CLASS (aisleriot_game_parent_class)->finalize (object);
}

static void
aisleriot_game_get_property (GObject *object,
			     guint prop_id,
			     GValue *value,
			     GParamSpec *pspec)
{
  AisleriotGame *game = AISLERIOT_GAME (object);

  switch (prop_id) {
    case PROP_CAN_UNDO:
      g_value_set_boolean (value, game->can_undo);
      break;
    case PROP_CAN_REDO:
      g_value_set_boolean (value, game->can_redo);
      break;
Christian Persch's avatar
Christian Persch committed
1196 1197 1198
    case PROP_CAN_DEAL:
      g_value_set_boolean (value, game->can_deal);
      break;
1199
    case PROP_GAME_FILE:
1200
      g_value_set_string (value, game->game_module);
1201 1202
      break;
    case PROP_SCORE:
1203
      g_value_set_string (value, game->score);
1204
      break;
1205 1206 1207
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
1208 1209 1210 1211 1212 1213 1214 1215
  }
}

static void
aisleriot_game_class_init (AisleriotGameClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GType param_types[] = { G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE };
1216
  GType error_types[] = { G_TYPE_ERROR | G_SIGNAL_TYPE_STATIC_SCOPE };
1217
  GType ptr_types[] = { G_TYPE_POINTER };
1218
  SCM variable;
1219
  char *path;
1220 1221 1222 1223 1224 1225

  gobject_class->constructor = aisleriot_game_constructor;
  gobject_class->finalize = aisleriot_game_finalize;
  gobject_class->get_property = aisleriot_game_get_property;

  signals[GAME_TYPE] =
1226
    g_signal_newv (I_("game-type"),
1227 1228 1229 1230 1231 1232 1233 1234 1235
                   G_OBJECT_CLASS_TYPE (gobject_class),
                   (GSignalFlags) (G_SIGNAL_RUN_LAST),
                   NULL,
                   NULL, NULL,
                   g_cclosure_marshal_VOID__VOID,
                   G_TYPE_NONE,
                   0, NULL);

  signals[GAME_CLEARED] =
1236
    g_signal_newv (I_("game-cleared"),
1237 1238 1239 1240 1241 1242 1243 1244 1245
                   G_OBJECT_CLASS_TYPE (gobject_class),
                   (GSignalFlags) (G_SIGNAL_RUN_LAST),
                   NULL,
                   NULL, NULL,
                   g_cclosure_marshal_VOID__VOID,
                   G_TYPE_NONE,
                   0, NULL);

  signals[GAME_NEW] =
1246
    g_signal_newv (I_("game-new"),
1247 1248 1249 1250 1251 1252 1253 1254 1255
                   G_OBJECT_CLASS_TYPE (gobject_class),
                   (GSignalFlags) (G_SIGNAL_RUN_LAST),
                   NULL,
                   NULL, NULL,
                   g_cclosure_marshal_VOID__VOID,
                   G_TYPE_NONE,
                   0, NULL);

  signals[SLOT_CHANGED] =
1256
    g_signal_newv (I_("slot-changed"),
1257 1258 1259 1260 1261 1262 1263 1264 1265
                   G_OBJECT_CLASS_TYPE (gobject_class),
                   (GSignalFlags) (G_SIGNAL_RUN_LAST),
                   NULL,
                   NULL, NULL,
                   g_cclosure_marshal_VOID__POINTER,
                   G_TYPE_NONE,
                   1, ptr_types);

 signals[GAME_MESSAGE] =
1266
    g_signal_newv (I_("message"),
1267 1268 1269 1270 1271 1272 1273 1274 1275
                   G_OBJECT_CLASS_TYPE (gobject_class),
                   (GSignalFlags) (G_SIGNAL_RUN_LAST),
                   NULL,
                   NULL, NULL,
                   g_cclosure_marshal_VOID__STRING,
                   G_TYPE_NONE,
                   1, param_types);

 signals[EXCEPTION] =
1276
    g_signal_newv (I_("exception"),
1277 1278 1279 1280
                   G_OBJECT_CLASS_TYPE (gobject_class),
                   (GSignalFlags) (G_SIGNAL_RUN_LAST),
                   NULL,
                   NULL, NULL,
1281
                   g_cclosure_marshal_VOID__BOXED,
1282
                   G_TYPE_NONE,
1283
                   1, error_types);
1284 1285 1286 1287 1288 1289

  g_object_class_install_property
    (gobject_class,
     PROP_CAN_UNDO,
     g_param_spec_boolean ("can-undo", NULL, NULL,
                           FALSE,
1290 1291
                           G_PARAM_READABLE |
                           G_PARAM_STATIC_STRINGS));
1292 1293 1294 1295 1296 1297

  g_object_class_install_property
    (gobject_class,
     PROP_CAN_REDO,
     g_param_spec_boolean ("can-redo", NULL, NULL,
                           FALSE,
1298 1299
                           G_PARAM_READABLE |
                           G_PARAM_STATIC_STRINGS));
1300

Christian Persch's avatar
Christian Persch committed
1301 1302 1303 1304 1305
  g_object_class_install_property
    (gobject_class,
     PROP_CAN_DEAL,
     g_param_spec_boolean ("can-deal", NULL, NULL,
                           FALSE,
1306 1307
                           G_PARAM_READABLE |
                           G_PARAM_STATIC_STRINGS));
Christian Persch's avatar
Christian Persch committed
1308

1309 1310 1311 1312 1313
  g_object_class_install_property
    (gobject_class,
     PROP_GAME_FILE,
     g_param_spec_string ("game-file", NULL, NULL,
                          NULL,
1314 1315
                          G_PARAM_READABLE |
                          G_PARAM_STATIC_STRINGS));
1316 1317 1318 1319

  g_object_class_install_property
    (gobject_class,
     PROP_SCORE,
1320 1321 1322 1323
     g_param_spec_string ("score", NULL, NULL,
                          NULL,
                          G_PARAM_READABLE |
                          G_PARAM_STATIC_STRINGS));
1324 1325 1326 1327

  g_object_class_install_property
    (gobject_class,
     PROP_STATE,
1328
     g_param_spec_uint ("state", NULL, NULL,
1329
                        0, LAST_GAME_STATE, 0, 
1330 1331
                        G_PARAM_READABLE |
                        G_PARAM_STATIC_STRINGS));
1332

1333
  scm_c_define_module ("aisleriot interface", cscm_init, NULL);
1334 1335

  /* Append load-path */
1336 1337 1338 1339
  path = g_build_filename (ar_runtime_get_directory (AR_RUNTIME_PKG_DATA_DIRECTORY),
                           "guile",
                           SCM_EFFECTIVE_VERSION,
                           NULL);
1340 1341
  variable = scm_c_module_lookup (scm_the_root_module (), "%load-path");
  scm_variable_set_x (variable, scm_append_x (scm_list_2 (scm_variable_ref (variable),
1342
                                                          scm_list_1 (scm_from_utf8_string (path)))));
1343
  g_free (path);
1344

1345 1346 1347 1348
  path = g_build_filename (ar_runtime_get_directory (AR_RUNTIME_PKG_LIBRARY_DIRECTORY),
                           "guile",
                           SCM_EFFECTIVE_VERSION,
                           NULL);
1349 1350
  variable = scm_c_module_lookup (scm_the_root_module (), "%load-compiled-path");
  scm_variable_set_x (variable, scm_append_x (scm_list_2 (scm_variable_ref (variable),
1351
                                                          scm_list_1 (scm_from_utf8_string (path)))));
1352
  g_free (path);
1353 1354 1355 1356
}

/* public API */

1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
/**
 * ar_slot_get_slot_type:
 * @slot: a #ArSlot
 *
 * Returns: the slot type of @slot
 */
ArSlotType
ar_slot_get_slot_type (ArSlot *slot)
{
  g_return_val_if_fail (slot != NULL, AR_SLOT_UNKNOWN);

  return slot->type;
}

/**
 * ar_slot_get_type_string:
 * @slot: a #ArSlot
 *
 * Returns: a string describing the slot type
 */
const char *
ar_slot_get_type_string (ArSlot *slot)
{
  const char *text = NULL;

  g_return_val_if_fail (slot != NULL, NULL);

  switch (slot->type) {
    case AR_SLOT_UNKNOWN:
      text = NULL;
      break;
1388 1389 1390 1391
    case AR_SLOT_CHOOSER:
      /* Translators: this is the name of a type of card slot */
      text = C_("slot type", "chooser");
      break;
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    case AR_SLOT_FOUNDATION:
      /* Translators: this is the name of a type of card slot */
      text = C_("slot type", "foundation");
      break;
    case AR_SLOT_RESERVE:
      /* Translators: this is the name of a type of card slot */
      text = C_("slot type", "reserve");
      break;
    case AR_SLOT_STOCK:
      /* Translators: this is the name of a type of card slot */
      text = C_("slot type", "stock");
      break;
    case AR_SLOT_TABLEAU:
      /* Translators: this is the name of a type of card slot */
      text = C_("slot type", "tableau");
      break;
    case AR_SLOT_WASTE:
      /* Translators: this is the name of a type of card slot */
      text = C_("slot type", "waste");
      break;
  }

  return text;
}

/**
 * ar_slot_get_hint_string:
 * @slot: a #ArSlot
 *
 * Returns: a string describing the slot type
 */
char *
ar_slot_get_hint_string (ArSlot *slot,
                         int cardid)
{
  const char *card_name;

  g_return_val_if_fail (slot != NULL, NULL);

1431 1432 1433
  if (slot->type == AR_SLOT_UNKNOWN)
    return NULL;

1434 1435 1436
  if (cardid < 0)
    return g_strdup (ar_slot_get_type_string (slot));

1437
  card_name = ar_card_get_locale_name (CARD (slot->cards->data[cardid]));
1438 1439

  switch (slot->type) {
1440 1441 1442 1443
    case AR_SLOT_CHOOSER:
      /* Translators: %s is the name of the card; "chooser" is the name of a type of card slot */
      return g_strdup_printf (C_("slot hint", "%s on chooser"), card_name);

1444
    case AR_SLOT_FOUNDATION:
1445
      /* Translators: %s is the name of the card; "foundation" is the name of a type of card slot */
1446 1447 1448
      return g_strdup_printf (C_("slot hint", "%s on foundation"), card_name);

    case AR_SLOT_RESERVE:
1449
      /* Translators: %s is the name of the card; "reserve" is the name of a type of card slot */
1450 1451 1452
      return g_strdup_printf (C_("slot hint", "%s on reserve"), card_name);

    case AR_SLOT_STOCK:
1453
      /* Translators: %s is the name of the card; "stock" is the name of a type of card slot */
1454 1455 1456
      return g_strdup_printf (C_("slot hint", "%s on stock"), card_name);

    case AR_SLOT_TABLEAU:
1457
      /* Translators: %s is the name of the card; "tableau" is the name of a type of card slot */
1458 1459 1460
      return g_strdup_printf (C_("slot hint", "%s on tableau"), card_name);

    case AR_SLOT_WASTE:
1461
      /* Translators: %s is the name of the card; "waste" is the name of a type of card slot */
1462 1463 1464 1465 1466 1467 1468 1469 1470
      return g_strdup_printf (C_("slot hint", "%s on waste"), card_name);

    default:
      g_assert_not_reached ();
  }

  return NULL;
}

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
/**
 * aisleriot_game_error_quark:
 *
 * Returns: the #GQuark used for errors
 */
GQuark
aisleriot_game_error_quark (void)
{
  static GQuark quark = 0;

  if (G_UNLIKELY (quark == 0)) {
    quark = g_quark_from_static_string ("AisleRiotGameError");
  }

  return quark;
}

1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
/**
 * aisleriot_game_new:
 *
 * Returns: a new #AisleriotGame
 */
AisleriotGame *
aisleriot_game_new (void)
{
  return g_object_new (AISLERIOT_TYPE_GAME, NULL);
}

/**
 * aisleriot_game_get_slots:
 * @game:
 *
 * Returns an array of #Slot.
 *
 * Returns: a #GPtrArray; it is owned by @game and must not be modified or freed
 */
GPtrArray *
aisleriot_game_get_slots (AisleriotGame *game)
{
  return game->slots;
}

/**
 * aisleriot_game_slot_add_cards:
 * @game:
 * @slot:
 * @cards:
 * @n_cards:
 *
 * Adds @n_cards from @cards to the top of @slot.
 */
void
aisleriot_game_slot_add_cards (AisleriotGame *game,
1524
                               ArSlot *slot,
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
                               guint8 *cards,
                               guint n_cards)
{
  g_byte_array_append (slot->cards, cards, n_cards);

  g_signal_emit (game, signals[SLOT_CHANGED], 0, slot);
}

/**
 * aisleriot_game_get_state:
 * @game:
 *
 * Returns the current game state.
 *
 * Returns: a #AisleriotGameState
 */
guint
aisleriot_game_get_state (AisleriotGame *game)
{
  return game->state;
}

1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
/**
 * aisleriot_game_get_features:
 * @game:
 *
 * Returns the game features.
 *
 * Returns: a #AisleriotGameFeatures
 */
guint
aisleriot_game_get_features (AisleriotGame *game)
{
  return game->features;
}

1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
/**
 * aisleriot_game_start:
 * @game:
 *
 * Starts the game, if it hasn't started already.
 */
void
aisleriot_game_start (AisleriotGame *game)
{
  if (game->state == GAME_BEGIN) {
    set_game_state (game, GAME_RUNNING);
  }
}

/**
 * aisleriot_game_set_paused:
 * @game:
 * @paused: whether to pause the game
 *
 * Stops or resumes the game timer.
 */
void
aisleriot_game_set_paused (AisleriotGame *game,
                           gboolean paused)
{
  g_return_if_fail (game->state == GAME_RUNNING);

  paused = paused != FALSE;
  if (paused == game->paused)
    return;

1592
  game->paused = paused;
1593

1594 1595 1596 1597 1598
  if (paused) {
    g_timer_stop (game->timer);
  } else {
    g_timer_continue (game->timer);
  }
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
}

/**
 * aisleriot_game_undo_move:
 * @game:
 *
 * Undoes the last move.
 */
void
aisleriot_game_undo_move (AisleriotGame *game)
{
  /* If the game had ended, reset to RUNNING */
  if (game->state