~ubuntu-branches/ubuntu/maverick/gcompris/maverick

« back to all changes in this revision

Viewing changes to src/traffic-activity/traffic.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Gariepy, Marc Gariepy, Stephane Graber
  • Date: 2010-01-04 17:42:49 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100104174249-7bupatd9dtxyhvs4
Tags: 9.0-0ubuntu1
[Marc Gariepy]
* New upstream release (9.0).
* Remove cache.c from POTFILES to avoid FTBFS
* Remove unneeded rm in debian/rules (file no longer exists upstream)

[Stephane Graber]
* Bump Debian standards to 3.8.3
* Add patch system (dpatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* gcompris - traffic.c
 
2
 *
 
3
 * Copyright (C) 2002, 2008 Bruno Coudoin
 
4
 *
 
5
 * Based on the original code from Geoff Reedy <vader21@imsa.edu>
 
6
 * Copyright (C) 2000 Geoff Reedy
 
7
 *
 
8
 *   This program is free software; you can redistribute it and/or modify
 
9
 *   it under the terms of the GNU General Public License as published by
 
10
 *   the Free Software Foundation; either version 3 of the License, or
 
11
 *   (at your option) any later version.
 
12
 *
 
13
 *   This program is distributed in the hope that it will be useful,
 
14
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *   GNU General Public License for more details.
 
17
 *
 
18
 *   You should have received a copy of the GNU General Public License
 
19
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
#include <string.h>
 
23
 
 
24
#include "traffic.h"
 
25
 
 
26
#include "gcompris/gcompris.h"
 
27
 
 
28
#define SOUNDLISTFILE PACKAGE
 
29
 
 
30
#define DATAFILE "TrafficData"
 
31
 
 
32
 
 
33
static GcomprisBoard  *gcomprisBoard = NULL;
 
34
static gboolean board_paused  = TRUE;
 
35
 
 
36
static GooCanvasItem *allcars         = NULL;
 
37
static gboolean dragging = FALSE;
 
38
static double start_x, start_y;
 
39
static double hit=0;
 
40
 
 
41
#define OFSET_X 250
 
42
#define OFSET_Y 128
 
43
 
 
44
static void      start_board (GcomprisBoard *agcomprisBoard);
 
45
static void      pause_board (gboolean pause);
 
46
static void      end_board (void);
 
47
static gboolean  is_our_board (GcomprisBoard *gcomprisBoard);
 
48
static void      set_level (guint level);
 
49
static int       gamewon;
 
50
static void      game_won(void);
 
51
static void      repeat(void);
 
52
 
 
53
static GooCanvasItem *boardRootItem = NULL;
 
54
 
 
55
static GooCanvasItem    *traffic_create_item(GooCanvasItem *parent);
 
56
static void              traffic_destroy_all_items(void);
 
57
static void              traffic_next_level(void);
 
58
 
 
59
typedef struct _car car;
 
60
typedef struct _jam jam;
 
61
 
 
62
struct _car {
 
63
  guint x : 3;
 
64
  guint y : 3;
 
65
#define CAR_ORIENT_NS   0
 
66
#define CAR_ORIENT_EW   1
 
67
  guint orient : 1;
 
68
  guint goal : 1;
 
69
  guint size;
 
70
  guint color;
 
71
  gchar color_string[50];
 
72
  GooCanvasItem *canvasgroup;
 
73
};
 
74
 
 
75
struct _jam {
 
76
  guint num_cars;
 
77
  guint card;
 
78
  guint level;
 
79
#define MAX_NUMBER_OF_CARS 20
 
80
  car *cars[MAX_NUMBER_OF_CARS];
 
81
};
 
82
 
 
83
static gboolean on_button_press (GooCanvasItem  *item,
 
84
                                 GooCanvasItem  *target,
 
85
                                 GdkEventButton *event,
 
86
                                 car *thiscar);
 
87
static gboolean on_button_release (GooCanvasItem *item,
 
88
                                   GooCanvasItem *target,
 
89
                                   GdkEventButton *event,
 
90
                                   car *thiscar);
 
91
static gboolean on_motion_notify (GooCanvasItem *item,
 
92
                                  GooCanvasItem *target,
 
93
                                  GdkEventMotion *event,
 
94
                                  car *thiscar);
 
95
 
 
96
static gboolean  load_level(guint level, guint card);
 
97
 
 
98
static jam       current_card  ={0,0,0,{NULL}};
 
99
 
 
100
static void      draw_grid  (GooCanvasItem *rootBorder);
 
101
static gint      cars_from_strv(char *strv);
 
102
 
 
103
/* Description of this plugin */
 
104
static BoardPlugin menu_bp =
 
105
  {
 
106
    NULL,
 
107
    NULL,
 
108
    "A sliding block puzzle game",
 
109
    "",
 
110
    "Bruno Coudoin <bruno.coudoin@free.fr>",
 
111
    NULL,
 
112
    NULL,
 
113
    NULL,
 
114
    NULL,
 
115
    start_board,
 
116
    pause_board,
 
117
    end_board,
 
118
    is_our_board,
 
119
    NULL,
 
120
    NULL,
 
121
    set_level,
 
122
    NULL,
 
123
    repeat,
 
124
    NULL,
 
125
    NULL
 
126
  };
 
127
 
 
128
/*
 
129
 * Main entry point mandatory for each Gcompris's game
 
130
 * ---------------------------------------------------
 
131
 *
 
132
 */
 
133
 
 
134
GET_BPLUGIN_INFO(traffic)
 
135
 
 
136
/*
 
137
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
 
138
 *
 
139
 */
 
140
static void pause_board (gboolean pause)
 
141
{
 
142
  if(gcomprisBoard==NULL)
 
143
    return;
 
144
 
 
145
  if(gamewon == TRUE && pause == FALSE) /* the game is won */
 
146
    {
 
147
      game_won();
 
148
    }
 
149
 
 
150
  board_paused = pause;
 
151
}
 
152
 
 
153
/*
 
154
 */
 
155
static void start_board (GcomprisBoard *agcomprisBoard)
 
156
{
 
157
  if(agcomprisBoard!=NULL)
 
158
    {
 
159
      gcomprisBoard=agcomprisBoard;
 
160
      gcomprisBoard->level=1;
 
161
      gcomprisBoard->maxlevel=8;
 
162
      gcomprisBoard->sublevel=1;
 
163
      gcomprisBoard->number_of_sublevel=5; /* Go to next level after this number of 'play' */
 
164
      gc_score_start(SCORESTYLE_NOTE,
 
165
                           BOARDWIDTH - 220,
 
166
                           BOARDHEIGHT - 70,
 
167
                           gcomprisBoard->number_of_sublevel);
 
168
 
 
169
      gc_bar_set(GC_BAR_LEVEL|GC_BAR_REPEAT);
 
170
 
 
171
      gc_set_background(goo_canvas_get_root_item(gcomprisBoard->canvas),
 
172
                        "traffic/traffic-bg.jpg");
 
173
 
 
174
      traffic_next_level();
 
175
 
 
176
      gamewon = FALSE;
 
177
      pause_board(FALSE);
 
178
    }
 
179
}
 
180
/* ======================================= */
 
181
static void end_board ()
 
182
{
 
183
  if(gcomprisBoard!=NULL)
 
184
    {
 
185
      pause_board(TRUE);
 
186
      gc_score_end();
 
187
      traffic_destroy_all_items();
 
188
    }
 
189
  gcomprisBoard = NULL;
 
190
}
 
191
 
 
192
/* ======================================= */
 
193
static void set_level (guint level)
 
194
{
 
195
 
 
196
  if(gcomprisBoard!=NULL)
 
197
    {
 
198
      gcomprisBoard->level=level;
 
199
      gcomprisBoard->sublevel=1;
 
200
      traffic_next_level();
 
201
    }
 
202
}
 
203
/* ======================================= */
 
204
static gboolean is_our_board (GcomprisBoard *gcomprisBoard)
 
205
{
 
206
  if (gcomprisBoard)
 
207
    {
 
208
      if(g_strcasecmp(gcomprisBoard->type, "traffic")==0)
 
209
        {
 
210
          /* Set the plugin entry */
 
211
          gcomprisBoard->plugin=&menu_bp;
 
212
 
 
213
          return TRUE;
 
214
        }
 
215
    }
 
216
  return FALSE;
 
217
}
 
218
 
 
219
/*
 
220
 * Repeat let the user restart the current level
 
221
 *
 
222
 */
 
223
static void repeat (){
 
224
 
 
225
  traffic_destroy_all_items();
 
226
 
 
227
  /* Try the next level */
 
228
  traffic_create_item(goo_canvas_get_root_item(gcomprisBoard->canvas));
 
229
}
 
230
 
 
231
/*-------------------------------------------------------------------------------*/
 
232
/*-------------------------------------------------------------------------------*/
 
233
/* set initial values for the next level */
 
234
static void
 
235
traffic_next_level()
 
236
{
 
237
 
 
238
  gc_bar_set_level(gcomprisBoard);
 
239
 
 
240
  traffic_destroy_all_items();
 
241
  gamewon = FALSE;
 
242
 
 
243
  gc_score_set(gcomprisBoard->sublevel);
 
244
 
 
245
  /* Try the next level */
 
246
  traffic_create_item(goo_canvas_get_root_item(gcomprisBoard->canvas));
 
247
 
 
248
}
 
249
/* ==================================== */
 
250
/* Destroy all the items */
 
251
static void
 
252
traffic_destroy_all_items()
 
253
{
 
254
  guint i;
 
255
 
 
256
  if(boardRootItem!=NULL)
 
257
    goo_canvas_item_remove(boardRootItem);
 
258
  boardRootItem = NULL;
 
259
 
 
260
  for (i=0; i<current_card.num_cars; i++)
 
261
    {
 
262
      g_free(current_card.cars[i]);
 
263
    }
 
264
  current_card.num_cars = 0;
 
265
}
 
266
 
 
267
/* ==================================== */
 
268
static GooCanvasItem *
 
269
traffic_create_item(GooCanvasItem *parent)
 
270
{
 
271
  GooCanvasItem *borderItem = NULL;
 
272
 
 
273
  boardRootItem = \
 
274
    goo_canvas_group_new (goo_canvas_get_root_item(gcomprisBoard->canvas),
 
275
                          NULL);
 
276
 
 
277
  borderItem = \
 
278
    goo_canvas_group_new (boardRootItem,
 
279
                          NULL);
 
280
  goo_canvas_item_translate(borderItem, OFSET_X, OFSET_Y);
 
281
 
 
282
  draw_grid(borderItem);
 
283
 
 
284
  allcars = goo_canvas_group_new (borderItem, NULL);
 
285
  goo_canvas_item_translate(allcars, 11, 11);
 
286
 
 
287
  g_object_set_data(G_OBJECT(allcars),
 
288
                      "whatami", (gpointer)"allcars");
 
289
 
 
290
  /* Ready now, let's go */
 
291
  load_level(gcomprisBoard->level, gcomprisBoard->sublevel);
 
292
 
 
293
  return NULL;
 
294
}
 
295
/* ==================================== */
 
296
static void
 
297
game_won()
 
298
{
 
299
  gcomprisBoard->sublevel++;
 
300
 
 
301
  if(gcomprisBoard->sublevel > gcomprisBoard->number_of_sublevel)
 
302
    {
 
303
      /* Try the next level */
 
304
      gcomprisBoard->sublevel=1;
 
305
      gcomprisBoard->level++;
 
306
      if(gcomprisBoard->level>gcomprisBoard->maxlevel)
 
307
        gcomprisBoard->level = gcomprisBoard->maxlevel;
 
308
 
 
309
      gc_sound_play_ogg ("sounds/bonus.wav", NULL);
 
310
    }
 
311
  traffic_next_level();
 
312
}
 
313
 
 
314
/* from canvas.c */
 
315
 
 
316
static void
 
317
draw_grid(GooCanvasItem *rootBorder)
 
318
{
 
319
  GooCanvasItem *grid_group;
 
320
  int xlooper, ylooper;
 
321
 
 
322
  grid_group = \
 
323
    goo_canvas_group_new (rootBorder,
 
324
                          NULL);
 
325
 
 
326
  goo_canvas_item_translate(rootBorder, 10, 10);
 
327
 
 
328
  g_object_set_data(G_OBJECT(grid_group),
 
329
                      "whatami", (gpointer)"grid_group");
 
330
 
 
331
  goo_canvas_item_lower(grid_group, NULL);
 
332
 
 
333
  for (xlooper=0; xlooper<6; xlooper++)
 
334
    for (ylooper=0; ylooper<6; ylooper++)
 
335
      g_object_set_data(G_OBJECT(
 
336
                                 goo_canvas_rect_new(grid_group,
 
337
                                                     40.0*xlooper,
 
338
                                                     40.0*ylooper,
 
339
                                                     40.0,
 
340
                                                     40.0,
 
341
                                                     "fill-color-rgba", NULL,
 
342
                                                     "stroke-color", "white",
 
343
                                                     "line-width", 2.0,
 
344
                                                     NULL)),
 
345
                        "whatami",(gpointer)"grid square");;
 
346
}
 
347
 
 
348
 
 
349
static void
 
350
draw_car(car *thiscar)
 
351
{
 
352
  GooCanvasItem *car_group;
 
353
  GooCanvasItem *car_rect;
 
354
 
 
355
  g_object_set_data(G_OBJECT(allcars),"whatami",(gpointer)"allcars");
 
356
 
 
357
  car_group = goo_canvas_group_new(allcars,
 
358
                                   NULL);
 
359
  goo_canvas_item_translate(car_group,
 
360
                            40.0 * thiscar->x - 10,
 
361
                            40.0 * thiscar->y - 10);
 
362
 
 
363
  car_rect = goo_canvas_rect_new(car_group,
 
364
                                 0.0,
 
365
                                 0.0,
 
366
                                 (thiscar->orient?40.0*thiscar->size:40.0)-2,
 
367
                                 (thiscar->orient?40.0:40.0*thiscar->size)-2,
 
368
                                 "fill_color_rgba", thiscar->color,
 
369
                                 "stroke-color", "white",
 
370
                                 "line-width", 1.0,
 
371
                                 NULL);
 
372
 
 
373
  g_signal_connect(car_group,"button_press_event",
 
374
                   GTK_SIGNAL_FUNC(on_button_press), (gpointer)thiscar);
 
375
  g_signal_connect (car_group, "button_release_event",
 
376
                    (GtkSignalFunc) on_button_release, (gpointer)thiscar);
 
377
  g_signal_connect (car_group, "motion_notify_event",
 
378
                    (GtkSignalFunc) on_motion_notify, (gpointer)thiscar);
 
379
 
 
380
  g_object_set_data(G_OBJECT(car_group), "car", (gpointer)thiscar);
 
381
  g_object_set_data(G_OBJECT(car_group), "whatami", (gpointer)"car_group");
 
382
  g_object_set_data(G_OBJECT(car_rect), "whatami", (gpointer)"car_rect");
 
383
}
 
384
 
 
385
static void
 
386
draw_jam(jam *myjam)
 
387
{
 
388
  int whichcar;
 
389
  for (whichcar=0;whichcar<myjam->num_cars;whichcar++)
 
390
    draw_car(myjam->cars[whichcar]);
 
391
}
 
392
 
 
393
static gboolean
 
394
on_motion_notify (GooCanvasItem *item,
 
395
                  GooCanvasItem *target,
 
396
                  GdkEventMotion *event,
 
397
                  car *thiscar)
 
398
{
 
399
  GooCanvas *canvas;
 
400
  double small_x, big_x, small_y, big_y;
 
401
  double dx,dy;
 
402
  double item_x, item_y;
 
403
  GooCanvasItem *atdest = NULL;
 
404
  car *othercar = NULL;
 
405
 
 
406
  canvas = goo_canvas_item_get_canvas (item);
 
407
 
 
408
  item_x = event->x;
 
409
  item_y = event->y;
 
410
  goo_canvas_convert_to_item_space(goo_canvas_item_get_canvas(item),
 
411
                                   goo_canvas_item_get_parent(item),
 
412
                                   &item_x, &item_y);
 
413
 
 
414
  if (dragging && (event->state & GDK_BUTTON1_MASK))
 
415
    {
 
416
      switch (thiscar->orient) {
 
417
      case CAR_ORIENT_EW:
 
418
        small_x=0;
 
419
        small_y=0;
 
420
 
 
421
        big_x=40*thiscar->size-1;
 
422
        big_y=40-1;
 
423
 
 
424
        goo_canvas_convert_from_item_space(canvas,
 
425
                                           item, &small_x, &small_y);
 
426
        goo_canvas_convert_from_item_space(canvas,
 
427
                                           item, &big_x, &big_y);
 
428
 
 
429
        dy = CLAMP(item_y - start_y, -39, 39);
 
430
        dx = CLAMP(item_x - start_x, -39, 39);
 
431
 
 
432
        if (thiscar->goal && big_x==250+OFSET_X)
 
433
          {
 
434
            gc_canvas_item_ungrab(item,event->time);
 
435
            g_object_set (item,
 
436
                          "visibility", GOO_CANVAS_ITEM_INVISIBLE,
 
437
                          NULL);
 
438
            dragging=FALSE;
 
439
 
 
440
            gamewon = TRUE;
 
441
            gc_bonus_display(gamewon, GC_BONUS_SMILEY);
 
442
          }
 
443
 
 
444
        if (small_x+dx < 11+OFSET_X)
 
445
          {
 
446
            dx = 11-small_x+OFSET_X;
 
447
          }
 
448
        else if (big_x+dx > 250+OFSET_X)
 
449
          {
 
450
            dx = 250-big_x+OFSET_X;
 
451
          }
 
452
 
 
453
        if ((hit<0) != (dx<0)) { hit=0;}
 
454
 
 
455
        if (hit==0)
 
456
          {
 
457
            if (dx>0)
 
458
              {
 
459
                do
 
460
                  {
 
461
                    atdest = goo_canvas_get_item_at(canvas,
 
462
                                                    big_x+dx, small_y+20, TRUE);
 
463
                    if (atdest)
 
464
                      othercar = (car*)g_object_get_data(G_OBJECT(goo_canvas_item_get_parent(atdest)),
 
465
                                                         "car");
 
466
                    if (othercar)
 
467
                      {
 
468
                        hit=1;
 
469
                        dx=0;
 
470
                      }
 
471
                  } while (othercar);
 
472
              }
 
473
            else if (dx<0)
 
474
              {
 
475
                do {
 
476
                  atdest=goo_canvas_get_item_at(canvas,
 
477
                                                small_x+dx-1,small_y+20, TRUE);
 
478
                  if (atdest)
 
479
                    othercar=(car*)g_object_get_data(G_OBJECT(goo_canvas_item_get_parent(atdest)),
 
480
                                                     "car");
 
481
                  if (othercar) {
 
482
                    hit=-1;
 
483
                    dx=0;
 
484
                  }
 
485
                } while (othercar);
 
486
              }
 
487
          }
 
488
        else
 
489
          { dx=0; }
 
490
 
 
491
        goo_canvas_item_translate(item, dx, 0);
 
492
        break;
 
493
 
 
494
      case CAR_ORIENT_NS:
 
495
        small_x=0;
 
496
        small_y=0;
 
497
 
 
498
        big_x=40-1;
 
499
        big_y=40*thiscar->size-1;
 
500
 
 
501
        goo_canvas_convert_from_item_space(goo_canvas_item_get_canvas(item),
 
502
                                           item,
 
503
                                           &small_x, &small_y);
 
504
        goo_canvas_convert_from_item_space(goo_canvas_item_get_canvas(item),
 
505
                                           item,
 
506
                                           &big_x, &big_y);
 
507
 
 
508
        dy = CLAMP(item_y - start_y, -39, 39);
 
509
        dx = CLAMP(item_x - start_x, -39, 39);
 
510
 
 
511
        if (small_y+dy<11+OFSET_Y)
 
512
          {
 
513
            dy=11-small_y+OFSET_Y;
 
514
          }
 
515
        else if (big_y+dy>250+OFSET_Y)
 
516
          {
 
517
            dy=250-big_y+OFSET_Y;
 
518
          }
 
519
 
 
520
        if ((hit<0)!=(dy<0)) { hit=0; }
 
521
 
 
522
        if (hit==0) {
 
523
          if (dy>0) {
 
524
            do {
 
525
              atdest = goo_canvas_get_item_at(gcomprisBoard->canvas,
 
526
                                              small_x + 20,
 
527
                                              big_y + dy,
 
528
                                              TRUE);
 
529
              if (atdest)
 
530
                othercar=(car*)g_object_get_data(G_OBJECT(goo_canvas_item_get_parent(atdest)),
 
531
                                                 "car");
 
532
              if (othercar) {
 
533
                hit=1;
 
534
                dy=0;
 
535
              }
 
536
            } while (othercar);
 
537
          } else if (dy<0) {
 
538
            do {
 
539
              atdest=goo_canvas_get_item_at(gcomprisBoard->canvas,
 
540
                                            small_x+20,small_y+dy-1, TRUE);
 
541
              if (atdest)
 
542
                othercar=(car*)g_object_get_data(G_OBJECT(goo_canvas_item_get_parent(atdest)),
 
543
                                                   "car");
 
544
              if (othercar) {
 
545
                hit=-1;
 
546
                dy=0;
 
547
              }
 
548
            } while (othercar);
 
549
          }
 
550
        } else { dy=0; }
 
551
 
 
552
        goo_canvas_item_translate(item, 0, dy);
 
553
      }
 
554
 
 
555
    }
 
556
  return TRUE;
 
557
}
 
558
 
 
559
static gboolean
 
560
on_button_release (GooCanvasItem *item,
 
561
                   GooCanvasItem *target,
 
562
                   GdkEventButton *event,
 
563
                   car *thiscar)
 
564
{
 
565
  GooCanvas *canvas;
 
566
  double dx,dy;
 
567
 
 
568
  canvas = goo_canvas_item_get_canvas (item);
 
569
 
 
570
#if 0
 
571
  g_print ("received 'button-release' signal\n");
 
572
#endif
 
573
 
 
574
  if (dragging)
 
575
    {
 
576
      double even_vals_x[]={11+OFSET_X,51+OFSET_X,91+OFSET_X,131+
 
577
                            OFSET_X,171+OFSET_X,211+OFSET_X,HUGE_VAL};
 
578
      double even_vals_y[]={11+OFSET_Y,51+OFSET_Y,91+OFSET_Y,131+
 
579
                            OFSET_Y,171+OFSET_Y,211+OFSET_Y,HUGE_VAL};
 
580
      double *ptr;
 
581
      double x=0,y=0;
 
582
 
 
583
      goo_canvas_convert_from_item_space(canvas, item,
 
584
                                         &x, &y);
 
585
 
 
586
      for (ptr=even_vals_x;*ptr<x;ptr++);
 
587
      if (*ptr-x>20)
 
588
        dx=*(ptr-1)-x;
 
589
      else
 
590
        dx=*ptr-x;
 
591
 
 
592
      for (ptr=even_vals_y;*ptr<y;ptr++);
 
593
      if (*ptr-y>20)
 
594
        dy=*(ptr-1)-y;
 
595
      else
 
596
        dy=*ptr-y;
 
597
 
 
598
      goo_canvas_item_translate(item, dx, dy);
 
599
      gc_canvas_item_ungrab(item, event->time);
 
600
      hit=0;
 
601
      dragging=FALSE;
 
602
    }
 
603
 
 
604
  return TRUE;
 
605
}
 
606
 
 
607
 static gboolean
 
608
 on_button_press (GooCanvasItem  *item,
 
609
                  GooCanvasItem  *target,
 
610
                  GdkEventButton *event,
 
611
                  car *thiscar)
 
612
{
 
613
  GooCanvas *canvas;
 
614
  double item_x, item_y;
 
615
  GdkCursor *cursor;
 
616
 
 
617
  canvas = goo_canvas_item_get_canvas (item);
 
618
 
 
619
  item_x = event->x;
 
620
  item_y = event->y;
 
621
  goo_canvas_convert_to_item_space(goo_canvas_item_get_canvas(item),
 
622
                                   goo_canvas_item_get_parent(item),
 
623
                                   &item_x, &item_y);
 
624
 
 
625
  start_x = item_x;
 
626
  start_y = item_y;
 
627
 
 
628
  if (thiscar->orient==CAR_ORIENT_NS)
 
629
    cursor = gdk_cursor_new(GDK_SB_V_DOUBLE_ARROW);
 
630
  else
 
631
    cursor = gdk_cursor_new(GDK_SB_H_DOUBLE_ARROW);
 
632
 
 
633
  gc_canvas_item_grab(item,
 
634
                      GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
 
635
                      cursor,
 
636
                      event->time);
 
637
 
 
638
  gdk_cursor_unref(cursor);
 
639
  dragging=TRUE;
 
640
 
 
641
  return TRUE;
 
642
}
 
643
 
 
644
/* From jam.c */
 
645
 
 
646
static gboolean
 
647
load_level(guint level, guint sublevel)
 
648
{
 
649
  char *car_strv=NULL;
 
650
 
 
651
  current_card.level = level;
 
652
  current_card.card  = sublevel;
 
653
 
 
654
  car_strv = DataList[(level-1) * gcomprisBoard->number_of_sublevel + (sublevel-1)];
 
655
 
 
656
  current_card.num_cars = cars_from_strv(car_strv);
 
657
 
 
658
  if(current_card.num_cars == -1)
 
659
    g_error("In loading dataset for traffic activity");
 
660
 
 
661
  draw_jam(&current_card);
 
662
 
 
663
  return TRUE;
 
664
}
 
665
 
 
666
/* Returns the number of cars
 
667
 * I took the formatting from
 
668
 *  http://www.javascript-games.org/puzzle/rushhour/
 
669
 */
 
670
static gint
 
671
cars_from_strv(char *strv)
 
672
{
 
673
  car *ccar;
 
674
  char x,y,id;
 
675
  int number_of_cars = 0;
 
676
  gboolean more_car = TRUE;
 
677
 
 
678
  while (more_car) {
 
679
 
 
680
    current_card.cars[number_of_cars] = (car *)g_new(car, 1);
 
681
    ccar = current_card.cars[number_of_cars];
 
682
 
 
683
    /* By default, not a goal car */
 
684
    ccar->goal   = 0;
 
685
 
 
686
    number_of_cars++;
 
687
 
 
688
    if (sscanf(strv,"%c%c%c",
 
689
               &id,&x,&y)!=3) {
 
690
      return -1;
 
691
    }
 
692
 
 
693
    /* Point to the next car */
 
694
    strv += 3;
 
695
 
 
696
    if(strv[0] != ',')
 
697
      more_car = FALSE;
 
698
 
 
699
    strv += 1;
 
700
 
 
701
    if (id == 'O' || id == 'P' || id == 'Q' || id == 'R') ccar->size = 3;
 
702
    else ccar->size = 2;
 
703
 
 
704
    ccar->orient = 1;
 
705
    ccar->x = 0;
 
706
    ccar->y = y-'1';
 
707
 
 
708
    if (x == 'A') ccar->x = 0;
 
709
    else if (x == 'B') ccar->x = 1;
 
710
    else if (x == 'C') ccar->x = 2;
 
711
    else if (x == 'D') ccar->x = 3;
 
712
    else if (x == 'E') ccar->x = 4;
 
713
    else if (x == 'F') ccar->x = 5;
 
714
    else {
 
715
      ccar->y = x-'1';
 
716
      ccar->orient = 0;
 
717
 
 
718
      if (y == 'A') ccar->x = 0;
 
719
      else if (y == 'B') ccar->x = 1;
 
720
      else if (y == 'C') ccar->x = 2;
 
721
      else if (y == 'D') ccar->x = 3;
 
722
      else if (y == 'E') ccar->x = 4;
 
723
      else if (y == 'F') ccar->x = 5;
 
724
    }
 
725
 
 
726
    if (id == 'X')
 
727
      {
 
728
        ccar->color  = 0xFF0000FF;
 
729
        ccar->goal   = 1;
 
730
      }
 
731
    else if (id == 'A') ccar->color = 0x80FF80FF;
 
732
    else if (id == 'B') ccar->color = 0xC0C000FF;
 
733
    else if (id == 'C') ccar->color = 0x8080FFFF;
 
734
    else if (id == 'D') ccar->color = 0xFF80FFFF;
 
735
    else if (id == 'E') ccar->color = 0xC00000FF;
 
736
    else if (id == 'F') ccar->color = 0x008000FF;
 
737
    else if (id == 'G') ccar->color = 0xC0C0C0FF;
 
738
    else if (id == 'H') ccar->color = 0x6000efFF;
 
739
    else if (id == 'I') ccar->color = 0xFFFF00FF;
 
740
    else if (id == 'J') ccar->color = 0xFFA801FF;
 
741
    else if (id == 'K') ccar->color = 0x00FF00FF;
 
742
    else if (id == 'O') ccar->color = 0xFFFF00FF;
 
743
    else if (id == 'P') ccar->color = 0xFF80FFFF;
 
744
    else if (id == 'Q') ccar->color = 0x0000FFFF;
 
745
    else if (id == 'R') ccar->color = 0x00FFFFFF;
 
746
 
 
747
  }
 
748
  return number_of_cars;
 
749
}
 
750