hanoi_real.c 13 KB
Newer Older
1 2
/* gcompris - hanoi_real.c
 *
3
 * Copyright (C) 2005, 2008 Bruno Coudoin
4 5 6
 *
 *   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
7
 *   the Free Software Foundation; either version 3 of the License, or
8 9 10 11 12 13 14 15
 *   (at your option) any later version.
 *
 *   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.
 *
 *   You should have received a copy of the GNU General Public License
16
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 */

#include "gcompris/gcompris.h"

#define SOUNDLISTFILE PACKAGE

static GcomprisBoard *gcomprisBoard = NULL;
static gboolean board_paused = TRUE;

static void	 start_board (GcomprisBoard *agcomprisBoard);
static void	 pause_board (gboolean pause);
static void	 end_board (void);
static gboolean	 is_our_board (GcomprisBoard *gcomprisBoard);
static void	 set_level (guint level);
static int	 gamewon;
static void	 game_won(void);

34
static GooCanvasItem *boardRootItem = NULL;
35

36
static GooCanvasItem	*hanoi_create_item(GooCanvasItem *parent);
37 38 39 40 41 42 43
static void		 hanoi_destroy_all_items(void);
static void		 hanoi_next_level(void);

/*
 * Contains the piece information
 */
typedef struct {
44
  GooCanvasItem *item;
45 46 47 48 49 50 51 52
  gint i;
  gint j;
  double x;
  double y;
  gboolean on_top;
  gint width;
} PieceItem;

Bruno Coudoin's avatar
Bruno Coudoin committed
53 54
static gboolean item_event (GooCanvasItem  *item,
			    GooCanvasItem  *target,
55
			    GdkEvent *event,
Bruno Coudoin's avatar
Bruno Coudoin committed
56
			    PieceItem *data);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

/* This contains the layout of the pieces */
#define MAX_NUMBER_X 3
#define MAX_NUMBER_Y 10
static PieceItem *position[MAX_NUMBER_X][MAX_NUMBER_Y];

static int number_of_item = 0;
static int number_of_item_x = MAX_NUMBER_X;
static int number_of_item_y = 0;
static int item_width;
static int item_height;

/* Description of this plugin */
static BoardPlugin menu_bp =
  {
    NULL,
    NULL,
    N_("Tower of Hanoi"),
75
    "",
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    "Bruno Coudoin <bruno.coudoin@free.fr>",
    NULL,
    NULL,
    NULL,
    NULL,
    start_board,
    pause_board,
    end_board,
    is_our_board,
    NULL,
    NULL,
    set_level,
    NULL,
    NULL,
    NULL,
    NULL
  };

/*
 * Main entry point mandatory for each Gcompris's game
 * ---------------------------------------------------
 *
 */

100
GET_BPLUGIN_INFO(hanoi_real)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

/*
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
 *
 */
static void pause_board (gboolean pause)
{
  if(gcomprisBoard==NULL)
    return;

  if(gamewon == TRUE && pause == FALSE) /* the game is won */
    {
      game_won();
    }

  board_paused = pause;
}

/*
 */
static void start_board (GcomprisBoard *agcomprisBoard)
{

  if(agcomprisBoard!=NULL)
    {
      gcomprisBoard=agcomprisBoard;
      gcomprisBoard->level=1;
128
      gcomprisBoard->maxlevel=2;
129 130
      gcomprisBoard->sublevel=1;
      gcomprisBoard->number_of_sublevel=1; /* Go to next level after this number of 'play' */
131
      gc_bar_set(GC_BAR_LEVEL);
132

133
      gc_set_default_background(goo_canvas_get_root_item(gcomprisBoard->canvas));
134 135 136

      boardRootItem = NULL;

137 138 139
      gc_drag_start(goo_canvas_get_root_item(gcomprisBoard->canvas),
		    (GcDragFunc)item_event, GC_DRAG_MODE_DEFAULT);

140 141 142 143 144 145 146 147 148 149 150
      hanoi_next_level();

      gamewon = FALSE;
      pause_board(FALSE);
    }
}
/* ======================================= */
static void end_board ()
{
  if(gcomprisBoard!=NULL)
    {
151
      gc_drag_stop(goo_canvas_get_root_item(gcomprisBoard->canvas));
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
      pause_board(TRUE);
      hanoi_destroy_all_items();
    }
  gcomprisBoard = NULL;
}

/* ======================================= */
static void set_level (guint level)
{

  if(gcomprisBoard!=NULL)
    {
      gcomprisBoard->level=level;
      gcomprisBoard->sublevel=1;
      hanoi_next_level();
    }
}
/* ======================================= */
static gboolean is_our_board (GcomprisBoard *gcomprisBoard)
{
  if (gcomprisBoard)
    {
      if(g_strcasecmp(gcomprisBoard->type, "hanoi_real")==0)
	{
	  /* Set the plugin entry */
	  gcomprisBoard->plugin=&menu_bp;

	  return TRUE;
	}
    }
  return FALSE;
}

/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/
/* set initial values for the next level */
static void hanoi_next_level()
{

191
  gc_bar_set_level(gcomprisBoard);
192 193 194 195 196 197 198 199

  hanoi_destroy_all_items();
  gamewon = FALSE;

  /* Select level difficulty */
  number_of_item_y = gcomprisBoard->level + 2;

  /* Try the next level */
200
  hanoi_create_item(goo_canvas_get_root_item(gcomprisBoard->canvas));
201 202 203 204 205 206 207 208 209
}
/* ==================================== */
/* Destroy all the items */
static void hanoi_destroy_all_items()
{
  guint i,j;

  if(boardRootItem!=NULL)
    {
Bruno Coudoin's avatar
Bruno Coudoin committed
210
      goo_canvas_item_remove(boardRootItem);
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      /* Cleanup our memory structure */
      for(i=0; i<number_of_item_x; i++)
	{
	  for(j=0; j<number_of_item_y; j++)
	    {
	      g_free(position[i][j]);
	    }
	}
    }
  boardRootItem = NULL;

}

#if 0
static void dump_solution()
{
  guint i, j;

  printf("Dumping solution\n");
  for(i=0; i<number_of_item_x; i++)
    {
      for(j=0; j<number_of_item_y; j++)
	{
235 236
	  printf("(%d,%d= width=%d top=%d) ",  position[i][j]->i, position[i][j]->j,
		    position[i][j]->width,
237 238 239 240 241 242 243 244 245
		    position[i][j]->on_top);
	}
      printf("\n");
    }

}
#endif

/* ==================================== */
246 247
static GooCanvasItem *
hanoi_create_item(GooCanvasItem *parent)
248 249 250 251
{
  int i,j;
  double gap_x, gap_y;
  double baseline;
252 253
  GooCanvasItem *item;
  GdkPixbuf *pixmap;
254

Bruno Coudoin's avatar
Bruno Coudoin committed
255 256 257
  boardRootItem = goo_canvas_group_new (goo_canvas_get_root_item(gcomprisBoard->canvas),
					NULL);

Bruno Coudoin's avatar
Bruno Coudoin committed
258 259 260 261 262 263 264
  item = goo_canvas_svg_new (boardRootItem,
			     gc_skin_rsvg_get(),
			     "svg-id", "#BAR_BG",
			     NULL);
  SET_ITEM_LOCATION_CENTER(item,
			   BOARDWIDTH/2,
			   BOARDHEIGHT - 100);
265

266 267
  goo_canvas_text_new (boardRootItem,
		       _("Move the entire stack to the right peg, one disc at a time"),
268
		       BOARDWIDTH/2,
269
		       BOARDHEIGHT - 100,
270
		       -1,
Bruno Coudoin's avatar
Bruno Coudoin committed
271
		       GTK_ANCHOR_CENTER,
272
		       "font", gc_skin_font_board_small,
273
		       "fill_color_rgba", gc_skin_color_text_button,
274 275
		       "alignment", PANGO_ALIGN_CENTER,
		       NULL);
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317


  /*----------------------------------------*/
  /* Empty the solution */
  for(i=0; i<number_of_item_x; i++)
    {
      for(j=0; j<number_of_item_y; j++)
	{
	  position[i][j] = g_malloc(sizeof(PieceItem));
	  position[i][j]->width  = -1;
	  position[i][j]->i      = i;
	  position[i][j]->j      = j;
	  position[i][j]->on_top = FALSE;
	}
    }

  /* Initialize the left positions */
  for(j=0; j<number_of_item_y; j++)
    {
      position[0][j]->width = number_of_item_y - j;
    }

  /* Mark the top piece */
  position[0][number_of_item_y-1]->on_top = TRUE;

  /*dump_solution();*/

  /*----------------------------------------*/
  /* Display it now */

  item_width  = BOARDWIDTH / (number_of_item_x);
  item_height = 30;

  gap_x = item_width  * 0.1;
  gap_y = item_height * 0.25;

  baseline = BOARDHEIGHT/2 + item_height * number_of_item_y/2;

  number_of_item = 0;

  for(i=0; i<number_of_item_x; i++)
    {
318
      if(i == number_of_item_x-1)
319 320
	{
	  /* Create the backgound for the target */
Bruno Coudoin's avatar
Bruno Coudoin committed
321 322 323
	  goo_canvas_rect_new (boardRootItem,
			       item_width * i + gap_x/2,
			       baseline - item_height * number_of_item_y - gap_y - 50,
324 325
			       item_width - gap_x,
			       baseline - item_height * number_of_item_y - gap_y,
Bruno Coudoin's avatar
Bruno Coudoin committed
326 327 328 329
			       "fill_color_rgba", 0x036ED8FF,
			       "stroke-color", "black",
			       "line-width", (double)1,
			       NULL);
330
	}
331 332

      /* The disc support */
333
      pixmap = gc_pixmap_load ("hanoi_real/disc_support.png");
334

335 336
      item = goo_canvas_image_new (boardRootItem,
				   pixmap,
337 338 339
				   item_width * i
				   + (item_width - gdk_pixbuf_get_width(pixmap))/2,
				   baseline - gdk_pixbuf_get_height(pixmap) + item_height,
340
				   NULL);
341

342 343 344
      gdk_pixbuf_unref(pixmap);


345 346 347
      for(j=0; j<number_of_item_y; j++)
	{

348
	  position[i][j]->x = item_width * i + item_width/2;
349
	  position[i][j]->y = baseline - item_height * j - item_height;
350 351 352

	  if(position[i][j]->width != -1)
	    {
353
	      pixmap = gc_pixmap_load ("%s%d.png", "hanoi_real/disc", j+1);
354 355 356 357
	      int w = gdk_pixbuf_get_width(pixmap);

	      item = goo_canvas_image_new (boardRootItem,
					   pixmap,
358
					   0, 0,
359
					   NULL);
360
	      gdk_pixbuf_unref(pixmap);
361 362 363 364
	      goo_canvas_item_translate(item,
					position[i][j]->x - w/2,
					position[i][j]->y);

365 366 367

	      position[i][j]->item = item;

368
	      g_signal_connect(item,
Bruno Coudoin's avatar
Bruno Coudoin committed
369
			       "button_press_event",
370 371 372 373
			       (GtkSignalFunc) gc_drag_event,  position[i][j]);
	      g_signal_connect(item,
			       "button_release_event",
			       (GtkSignalFunc) gc_drag_event,  position[i][j]);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
	    }
	}
    }

  return NULL;
}
/* ==================================== */
static void game_won()
{
  gcomprisBoard->sublevel++;

  if(gcomprisBoard->sublevel>gcomprisBoard->number_of_sublevel) {
    /* Try the next level */
    gcomprisBoard->sublevel=1;
    gcomprisBoard->level++;
389 390 391
    if(gcomprisBoard->level>gcomprisBoard->maxlevel)
      gcomprisBoard->level = gcomprisBoard->maxlevel;

392
    gc_sound_play_ogg ("sounds/bonus.wav", NULL);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  }
  hanoi_next_level();
}

/*
 * Returns TRUE is the goal is reached
 */
static gboolean is_completed()
{
  gint j;
  gboolean done = TRUE;

  for(j=0; j<number_of_item_y; j++)
    {
      if(position[number_of_item_x-1][j]->width != number_of_item_y - j)
	done = FALSE;
    }

  return done;
}

/* ==================================== */
415 416 417 418 419
static gboolean
item_event (GooCanvasItem  *item,
	    GooCanvasItem  *target,
	    GdkEvent *event,
	    PieceItem *data)
420 421 422 423 424 425 426 427 428
{
   double item_x, item_y;

   if(!gcomprisBoard)
     return FALSE;

  if(board_paused)
    return FALSE;

429
  if(data && !data->on_top)
430
    return FALSE;
431 432

  switch (event->type)
433 434
    {
    case GDK_BUTTON_PRESS:
435
      switch(event->button.button)
436 437
	{
	case 1:
438

439
	  gc_drag_offset_save(event);
Bruno Coudoin's avatar
Bruno Coudoin committed
440
	  goo_canvas_item_raise(data->item, NULL);
441 442 443
	  break;
	}
      break;
444

445
    case GDK_MOTION_NOTIFY:
446
      gc_drag_item_move(event, NULL);
447
      break;
448

449 450
    case GDK_BUTTON_RELEASE:
	{
451
	  gint i;
452 453 454 455 456
	  gint tmpi, tmpj;
	  double tmpx, tmpy;
	  PieceItem *piece_src;
	  PieceItem *piece_dst;
	  gint line;
457 458
	  gint col=-1;
	  double disc_w, disc_h;
459 460 461 462
	  GooCanvasBounds bounds;

	  item_x = event->button.x;
	  item_y = event->button.y;
463

464 465 466
	  goo_canvas_item_get_bounds(data->item, &bounds);
	  disc_w = (bounds.x2 - bounds.x1)/2;
	  disc_h = (bounds.y2 - bounds.y1)/2;
467

468 469 470 471 472
	  gc_canvas_item_ungrab(data->item, event->button.time);

	  goo_canvas_convert_from_item_space(goo_canvas_item_get_canvas(data->item),
					   data->item,
					   &item_x, &item_y);
473

474
	  /* Search the column (x) where this item is ungrabbed */
475
	  if(item_x > (position[number_of_item_x-1][0]->x
476 477
		       - (position[number_of_item_x-1][0]->x - position[number_of_item_x-2][0]->x) / 2))
	     col = number_of_item_x-1;
478 479
	  else if(item_x < position[0][0]->x)
	    col = 0;
480 481 482 483 484
	  else {
	    for(i=0; i<number_of_item_x-1; i++) {
	      int distance = (position[i+1][0]->x - position[i][0]->x) / 2;
	      if(position[i][0]->x -  distance < item_x &&
		 position[i+1][0]->x - distance > item_x)
485
		col = i;
486 487
	    }
	  }
488 489 490 491 492 493

	  /* Bad drop / Outside of column area */
	  /* Bad drop / On the same column */
	  if(col<0 || col > number_of_item_x || col == data->i)
	    {
	      /* Return to the original position */
494 495 496
	      gc_item_absolute_move (data->item ,
				     data->x - disc_w,
				     data->y);
497 498 499 500 501 502 503 504 505 506

	      return FALSE;
	    }


	  /* Now search the free line (y) */
	  line = number_of_item_y;
	  for(i=number_of_item_y-1; i>=0; i--)
	    if(position[col][i]->width == -1)
	      line = i;
507

508
	  /* Bad drop / Too many pieces here or larger disc is above */
509
	  if(line > number_of_item_y ||
510 511 512
	     (line > 0 && position[col][line-1]->width != -1 && position[col][line-1]->width < data->width))
	    {
	      /* Return to the original position */
513 514 515
	      gc_item_absolute_move (data->item ,
				     data->x - disc_w,
				     data->y - disc_h);
516 517 518 519 520 521
	      return FALSE;
	    }

	  /* Update ontop values for the piece under the grabbed one */
	  if(data->j>0)
	    position[data->i][data->j-1]->on_top = TRUE;
522

523 524 525
	  /* Update ontop values for the piece under the ungrabbed one */
	  if(line>0)
	    position[col][line-1]->on_top = FALSE;
526

527 528 529
	  /* Move the piece */
	  piece_dst = position[col][line];
	  piece_src = data;
530

531
	  gc_item_absolute_move (data->item,
532 533
				 piece_dst->x - disc_w,
				 piece_dst->y);
534

535 536 537 538 539 540 541
	  /* Swap values in the pieces */
	  tmpx    = data->x;
	  tmpy    = data->y;
	  piece_src->x = piece_dst->x;
	  piece_src->y = piece_dst->y;
	  piece_dst->x = tmpx;
	  piece_dst->y = tmpy;
542

543 544 545 546 547 548
	  tmpi    = data->i;
	  tmpj    = data->j;
	  position[tmpi][tmpj]->i = piece_dst->i;
	  position[tmpi][tmpj]->j = piece_dst->j;
	  piece_dst->i  = tmpi;
	  piece_dst->j  = tmpj;
549

550 551 552 553 554 555 556 557
	  position[piece_src->i][piece_src->j] = piece_src;
	  position[piece_dst->i][piece_dst->j] = piece_dst;

	  /*dump_solution();*/
	  if(is_completed())
	    {
	      gamewon = TRUE;
	      hanoi_destroy_all_items();
558
	      gc_bonus_display(gamewon, GC_BONUS_SMILEY);
559 560 561
	    }
	}
      break;
562

563 564 565
    default:
      break;
    }
566 567


568 569 570
  return FALSE;
}