1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
100
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2002 Eric Linenberg
* February 2003: Robert Hak performs a cleanup/rewrite/feature addition.
* Eric smiles. Bjorn cries. Linus say 'huh?'.
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "plugin.h"
#ifdef HAVE_LCD_BITMAP
#define SOKOBAN_TITLE "Sokoban"
#define SOKOBAN_TITLE_FONT 2
#define LEVELS_FILE PLUGIN_DIR "/sokoban.levels"
#define ROWS 16
#define COLS 20
#define MAX_UNDOS 5
#define SOKOBAN_LEVEL_SIZE (ROWS*COLS)
/* variable button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#define SOKOBAN_UP BUTTON_UP
#define SOKOBAN_DOWN BUTTON_DOWN
#define SOKOBAN_QUIT BUTTON_OFF
#define SOKOBAN_UNDO BUTTON_ON
#define SOKOBAN_LEVEL_UP BUTTON_F3
#define SOKOBAN_LEVEL_DOWN BUTTON_F1
#define SOKOBAN_LEVEL_REPEAT BUTTON_F2
#elif CONFIG_KEYPAD == ONDIO_PAD
#define SOKOBAN_UP BUTTON_UP
#define SOKOBAN_DOWN BUTTON_DOWN
#define SOKOBAN_QUIT BUTTON_OFF
#define SOKOBAN_UNDO_PRE BUTTON_MENU
#define SOKOBAN_UNDO (BUTTON_MENU | BUTTON_REL)
#define SOKOBAN_LEVEL_UP (BUTTON_MENU | BUTTON_RIGHT)
#define SOKOBAN_LEVEL_DOWN (BUTTON_MENU | BUTTON_LEFT)
#define SOKOBAN_LEVEL_REPEAT (BUTTON_MENU | BUTTON_UP)
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
(CONFIG_KEYPAD == IRIVER_H300_PAD)
#define SOKOBAN_UP BUTTON_UP
#define SOKOBAN_DOWN BUTTON_DOWN
#define SOKOBAN_QUIT BUTTON_OFF
#define SOKOBAN_UNDO BUTTON_ON
#define SOKOBAN_LEVEL_UP BUTTON_MODE
#define SOKOBAN_LEVEL_DOWN BUTTON_REC
#define SOKOBAN_LEVEL_REPEAT BUTTON_SELECT
#elif (CONFIG_KEYPAD == IPOD_4G_PAD)
#define SOKOBAN_UP BUTTON_MENU
#define SOKOBAN_DOWN BUTTON_PLAY
#define SOKOBAN_QUIT (BUTTON_SELECT | BUTTON_MENU)
#define SOKOBAN_UNDO_PRE BUTTON_SELECT
#define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
#define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_RIGHT)
#define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_LEFT)
#define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_PLAY)
#endif
#if LCD_DEPTH > 1
#ifdef HAVE_LCD_COLOR
#define MEDIUM_GRAY LCD_RGBPACK(127, 127, 127)
#else
#define MEDIUM_GRAY LCD_BRIGHTNESS(127)
#endif
#endif
static void init_undo(void);
static void undo(void);
static void add_undo(int button);
static int get_level(char *level, int level_size);
static int get_level_count(void);
static int load_level(void);
static void draw_level(void);
static void init_boards(void);
static void update_screen(void);
static bool sokoban_loop(void);
/* The Location, Undo and LevelInfo structs are OO-flavored.
* (oooh!-flavored as Schnueff puts it.) It makes more you have to know,
* but the overall data layout becomes more manageable. */
/* We use the same three values in 2 structs. Makeing them a struct
* hopefully ensures that if you change things in one, the other changes
* as well. */
struct LevelInfo {
short level;
short moves;
short boxes_to_go;
};
/* What a given location on the board looks like at a given time */
struct Location {
char spot;
short row;
short col;
};
/* A single level of undo. Each undo move can affect upto,
* but not more then, 3 spots on the board */
struct Undo {
struct LevelInfo level;
struct Location location[3];
};
/* Our full undo history */
static struct UndoInfo {
short count; /* How many undos are there in history */
short current; /* Which history is the current undo */
struct Undo history[MAX_UNDOS];
} undo_info;
/* Our playing board */
static struct BoardInfo {
char board[ROWS][COLS];
struct LevelInfo level;
struct Location player;
int max_level; /* How many levels do we have? */
int level_offset; /* Where in the level file is this level */
int loaded_level; /* Which level is in memory */
} current_info;
static struct plugin_api* rb;
static void init_undo(void)
{
undo_info.count = 0;
undo_info.current = 0;
}
static void undo(void)
{
struct Undo *undo;
int i = 0;
short row, col;
if (undo_info.count == 0)
return;
/* Update board info */
undo = &undo_info.history[undo_info.current];
rb->memcpy(¤t_info.level, &undo->level, sizeof(undo->level));
rb->memcpy(¤t_info.player, &undo->location[0], sizeof(undo->location[0]));
row = undo->location[0].row;
col = undo->location[0].col;
current_info.board[row][col] = '@';
/* Update the two other possible spots */
for (i = 1; i < 3; i++) {
if (undo->location[i].spot != '\0') {
row = undo->location[i].row;
col = undo->location[i].col;
current_info.board[row][col] = undo->location[i].spot;
undo->location[i].spot = '\0';
}
}
/* Remove this undo from the list */
if (undo_info.current == 0) {
if (undo_info.count > 1)
undo_info.current = MAX_UNDOS - 1;
} else {
undo_info.current--;
}
undo_info.count--;
return;
}
static void add_undo(int button)
{
struct Undo *undo;
int row, col, i;
bool storable;
if ((button != BUTTON_LEFT) && (button != BUTTON_RIGHT) &&
(button != SOKOBAN_UP) && (button != SOKOBAN_DOWN))
return;
if (undo_info.count != 0) {
if (undo_info.current < (MAX_UNDOS - 1))
undo_info.current++;
else
undo_info.current = 0;
}
/* Make what follows more readable */
undo = &undo_info.history[undo_info.current];
/* Store our level info */
rb->memcpy(&undo->level, ¤t_info.level, sizeof(undo->level));
/* Store our player info */
rb->memcpy(&undo->location[0], ¤t_info.player, sizeof(undo->location[0]));
/* Now we need to store upto 2 blocks that may be affected.
* If player.spot is NULL, then there is no info stored
* for that block */
row = current_info.player.row;
col = current_info.player.col;
/* This must stay as _1_ because the first block (0) is the player */
for (i = 1; i < 3; i++) {
storable = true;
switch (button) {
case BUTTON_LEFT:
col--;
if (col < 0)
storable = false;
break;
case BUTTON_RIGHT:
col++;
if (col >= COLS)
storable = false;
break;
case SOKOBAN_UP:
row--;
if (row < 0)
storable = false;
break;
case SOKOBAN_DOWN:
row++;
if (row >= ROWS)
storable = false;
break;
default:
return;
}
if (storable) {
undo->location[i].col = col;
undo->location[i].row = row;
undo->location[i].spot = current_info.board[row][col];
} else {
undo->location[i].spot = '\0';
}
}
if (undo_info.count < MAX_UNDOS)
undo_info.count++;
}
static void init_boards(void)
{
current_info.level.level = 0;
current_info.level.moves = 0;
current_info.level.boxes_to_go = 0;
current_info.player.row = 0;
current_info.player.col = 0;
current_info.player.spot = ' ';
current_info.max_level = 0;
current_info.level_offset = 0;
current_info.loaded_level = 0;
init_undo();
}
static int get_level_count(void)
{
int fd = 0;
int lastlen = 0;
char buffer[COLS + 3]; /* COLS plus CR/LF and \0 */
if ((fd = rb->open(LEVELS_FILE, O_RDONLY)) < 0) {
rb->splash(0, true, "Unable to open %s", LEVELS_FILE);
return -1;
}
while(1) {
int len = rb->read_line(fd, buffer, sizeof(buffer));
if(len <= 0)
break;
/* Two short lines in a row means new level */
if(len < 3 && lastlen < 3)
current_info.max_level++;
lastlen = len;
}
rb->close(fd);
return 0;
}
static int get_level(char *level, int level_size)
{
int fd = 0, i = 0;
int nread = 0;
int count = 0;
int lastlen = 0;
int level_ct = 1;
unsigned char buffer[SOKOBAN_LEVEL_SIZE * 2];
bool level_found = false;
/* open file */
if ((fd = rb->open(LEVELS_FILE, O_RDONLY)) < 0)
return -1;
/* Lets not reparse the full file if we can avoid it */
if (current_info.loaded_level < current_info.level.level) {
rb->lseek(fd, current_info.level_offset, SEEK_SET);
level_ct = current_info.loaded_level;
}
if(current_info.level.level > 1) {
while(!level_found) {
int len = rb->read_line(fd, buffer, SOKOBAN_LEVEL_SIZE);
if(len <= 0) {
rb->close(fd);
return -1;
}
/* Two short lines in a row means new level */
if(len < 3 && lastlen < 3) {
level_ct++;
if(level_ct == current_info.level.level)
level_found = true;
}
lastlen = len;
}
}
/* Remember the current offset */
current_info.level_offset = rb->lseek(fd, 0, SEEK_CUR);
/* read a full buffer chunk from here */
nread = rb->read(fd, buffer, sizeof(buffer)-1);
if (nread < 0)
return -1;
buffer[nread] = 0;
rb->close(fd);
/* If we read less then a level, error */
if (nread < level_size)
return -1;
/* Load our new level */
for(i=0, count=0; (count < nread) && (i<level_size);) {
if (buffer[count] != '\n' && buffer[count] != '\r')
level[i++] = buffer[count];
count++;
}
level[i] = 0;
current_info.loaded_level = current_info.level.level;
return 0;
}
/* return non-zero on error */
static int load_level(void)
{
short c = 0;
short r = 0;
short i = 0;
char level[ROWS*COLS+1];
int x = 0;
current_info.player.spot=' ';
current_info.level.boxes_to_go = 0;
current_info.level.moves = 0;
if (get_level(level, sizeof(level)) != 0)
return -1;
i = 0;
for (r = 0; r < ROWS; r++) {
x++;
for (c = 0; c < COLS; c++, i++) {
current_info.board[r][c] = level[i];
if (current_info.board[r][c] == '.')
current_info.level.boxes_to_go++;
else if (current_info.board[r][c] == '@') {
current_info.player.row = r;
current_info.player.col = c;
}
}
}
return 0;
}
#define STAT_WIDTH (LCD_WIDTH-(COLS * magnify))
static void update_screen(void)
{
int b = 0, c = 0;
int rows = 0, cols = 0;
char s[25];
#if LCD_HEIGHT >= 128
int magnify = 6;
#else
int magnify = 4;
#endif
/* load the board to the screen */
for (rows=0 ; rows < ROWS ; rows++) {
for (cols = 0 ; cols < COLS ; cols++) {
c = cols * magnify;
b = rows * magnify;
switch(current_info.board[rows][cols]) {
case 'X': /* black space */
break;
case '#': /* this is a wall */
#if LCD_DEPTH > 1
rb->lcd_set_foreground(MEDIUM_GRAY);
rb->lcd_fillrect(c, b, magnify, magnify);
rb->lcd_set_foreground(LCD_BLACK);
#else
{
int i, j;
for (i = c; i < c + magnify; i++)
for (j = b; j < b + magnify; j++)
if ((i ^ j) & 1)
rb->lcd_drawpixel(i, j);
}
#endif
break;
case '.': /* this is a home location */
rb->lcd_drawrect(c+(magnify/2)-1, b+(magnify/2)-1, magnify/2,
magnify/2);
break;
case '$': /* this is a box */
rb->lcd_drawrect(c, b, magnify, magnify);
break;
case '@': /* this is you */
{
int max = magnify - 1;
int middle = max / 2;
int ldelta = (middle + 1) / 2;
rb->lcd_drawline(c, b+middle, c+max, b+middle);
rb->lcd_drawline(c+middle, b, c+middle, b+max-ldelta);
rb->lcd_drawline(c+max-middle, b,
c+max-middle, b+max-ldelta);
rb->lcd_drawline(c+middle, b+max-ldelta,
c+middle-ldelta, b+max);
rb->lcd_drawline(c+max-middle, b+max-ldelta,
c+max-middle+ldelta, b+max);
}
break;
case '%': /* this is a box on a home spot */
rb->lcd_drawrect(c, b, magnify, magnify);
rb->lcd_drawrect(c+(magnify/2)-1, b+(magnify/2)-1, magnify/2,
magnify/2);
break;
}
}
}
rb->snprintf(s, sizeof(s), "%d", current_info.level.level);
rb->lcd_putsxy(LCD_WIDTH-STAT_WIDTH+4, 22, s);
rb->snprintf(s, sizeof(s), "%d", current_info.level.moves);
rb->lcd_putsxy(LCD_WIDTH-STAT_WIDTH+4, 54, s);
rb->lcd_drawrect(LCD_WIDTH-STAT_WIDTH,0,STAT_WIDTH,32);
rb->lcd_drawrect(LCD_WIDTH-STAT_WIDTH,32,STAT_WIDTH,LCD_HEIGHT-32);
rb->lcd_putsxy(LCD_WIDTH-STAT_WIDTH+1, 10, "Level");
rb->lcd_putsxy(LCD_WIDTH-STAT_WIDTH+1, 42, "Moves");
/* print out the screen */
rb->lcd_update();
}
static void draw_level(void)
{
load_level();
rb->lcd_clear_display();
update_screen();
}
static bool sokoban_loop(void)
{
char new_spot;
bool moved = true;
int i = 0, button = 0, lastbutton = 0;
short r = 0, c = 0;
current_info.level.level = 1;
load_level();
update_screen();
while (1) {
moved = true;
r = current_info.player.row;
c = current_info.player.col;
button = rb->button_get(true);
add_undo(button);
switch(button)
{
case SOKOBAN_QUIT:
/* get out of here */
return PLUGIN_OK;
case SOKOBAN_UNDO:
#ifdef SOKOBAN_UNDO_PRE
if (lastbutton != SOKOBAN_UNDO_PRE)
break;
#else /* repeat can't work here for Ondio */
case SOKOBAN_UNDO | BUTTON_REPEAT:
#endif
/* this is UNDO */
undo();
rb->lcd_clear_display();
update_screen();
moved = false;
break;
case SOKOBAN_LEVEL_UP:
case SOKOBAN_LEVEL_UP | BUTTON_REPEAT:
/* increase level */
init_undo();
current_info.level.boxes_to_go=0;
moved = true;
break;
case SOKOBAN_LEVEL_DOWN:
case SOKOBAN_LEVEL_DOWN | BUTTON_REPEAT:
/* previous level */
init_undo();
if (current_info.level.level > 1)
current_info.level.level--;
draw_level();
moved = false;
break;
case SOKOBAN_LEVEL_REPEAT:
case SOKOBAN_LEVEL_REPEAT | BUTTON_REPEAT:
/* same level */
init_undo();
draw_level();
moved = false;
break;
case BUTTON_LEFT:
switch(current_info.board[r][c-1])
{
case ' ': /* if it is a blank spot */
case '.': /* if it is a home spot */
new_spot = current_info.board[r][c-1];
current_info.board[r][c-1] = '@';
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = new_spot;
break;
case '$':
switch(current_info.board[r][c-2])
{
case ' ': /* going from blank to blank */
current_info.board[r][c-2] = current_info.board[r][c-1];
current_info.board[r][c-1] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = ' ';
break;
case '.': /* going from a blank to home */
current_info.board[r][c-2] = '%';
current_info.board[r][c-1] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = ' ';
current_info.level.boxes_to_go--;
break;
default:
moved = false;
break;
}
break;
case '%':
switch(current_info.board[r][c-2]) {
case ' ': /* we are going from a home to a blank */
current_info.board[r][c-2] = '$';
current_info.board[r][c-1] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = '.';
current_info.level.boxes_to_go++;
break;
case '.': /* if we are going from a home to home */
current_info.board[r][c-2] = '%';
current_info.board[r][c-1] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = '.';
break;
default:
moved = false;
break;
}
break;
default:
moved = false;
break;
}
if (moved)
current_info.player.col--;
break;
case BUTTON_RIGHT: /* if it is a blank spot */
switch(current_info.board[r][c+1]) {
case ' ':
case '.': /* if it is a home spot */
new_spot = current_info.board[r][c+1];
current_info.board[r][c+1] = '@';
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = new_spot;
break;
case '$':
switch(current_info.board[r][c+2]) {
case ' ': /* going from blank to blank */
current_info.board[r][c+2] = current_info.board[r][c+1];
current_info.board[r][c+1] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = ' ';
break;
case '.': /* going from a blank to home */
current_info.board[r][c+2] = '%';
current_info.board[r][c+1] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = ' ';
current_info.level.boxes_to_go--;
break;
default:
moved = false;
break;
}
break;
case '%':
switch(current_info.board[r][c+2]) {
case ' ': /* going from a home to a blank */
current_info.board[r][c+2] = '$';
current_info.board[r][c+1] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = '.';
current_info.level.boxes_to_go++;
break;
case '.':
current_info.board[r][c+2] = '%';
current_info.board[r][c+1] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = '.';
break;
default:
moved = false;
break;
}
break;
default:
moved = false;
break;
}
if (moved)
current_info.player.col++;
break;
case SOKOBAN_UP:
switch(current_info.board[r-1][c]) {
case ' ': /* if it is a blank spot */
case '.': /* if it is a home spot */
new_spot = current_info.board[r-1][c];
current_info.board[r-1][c] = '@';
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = new_spot;
break;
case '$':
switch(current_info.board[r-2][c]) {
case ' ': /* going from blank to blank */
current_info.board[r-2][c] = current_info.board[r-1][c];
current_info.board[r-1][c] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = ' ';
break;
case '.': /* going from a blank to home */
current_info.board[r-2][c] = '%';
current_info.board[r-1][c] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = ' ';
current_info.level.boxes_to_go--;
break;
default:
moved = false;
break;
}
break;
case '%':
switch(current_info.board[r-2][c]) {
case ' ': /* we are going from a home to a blank */
current_info.board[r-2][c] = '$';
current_info.board[r-1][c] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = '.';
current_info.level.boxes_to_go++;
break;
case '.': /* if we are going from a home to home */
current_info.board[r-2][c] = '%';
current_info.board[r-1][c] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = '.';
break;
default:
moved = false;
break;
}
break;
default:
moved = false;
break;
}
if (moved)
current_info.player.row--;
break;
case SOKOBAN_DOWN:
switch(current_info.board[r+1][c]) {
case ' ': /* if it is a blank spot */
case '.': /* if it is a home spot */
new_spot = current_info.board[r+1][c];
current_info.board[r+1][c] = '@';
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = new_spot;
break;
case '$':
switch(current_info.board[r+2][c]) {
case ' ': /* going from blank to blank */
current_info.board[r+2][c] = current_info.board[r+1][c];
current_info.board[r+1][c] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = ' ';
break;
case '.': /* going from a blank to home */
current_info.board[r+2][c] = '%';
current_info.board[r+1][c] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = ' ';
current_info.level.boxes_to_go--;
break;
default:
moved = false;
break;
}
break;
case '%':
switch(current_info.board[r+2][c]) {
case ' ': /* going from a home to a blank */
current_info.board[r+2][c] = '$';
current_info.board[r+1][c] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = '.';
current_info.level.boxes_to_go++;
break;
case '.': /* going from a home to home */
current_info.board[r+2][c] = '%';
current_info.board[r+1][c] = current_info.board[r][c];
current_info.board[r][c] = current_info.player.spot;
current_info.player.spot = '.';
break;
default:
moved = false;
break;
}
break;
default:
moved = false;
break;
}
if (moved)
current_info.player.row++;
break;
default:
if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
return PLUGIN_USB_CONNECTED;
moved = false;
break;
}
if (button != BUTTON_NONE)
lastbutton = button;
if (moved) {
current_info.level.moves++;
rb->lcd_clear_display();
update_screen();
}
/* We have completed this level */
if (current_info.level.boxes_to_go == 0) {
current_info.level.level++;
/* clear undo stats */
init_undo();
rb->lcd_clear_display();
if (current_info.level.level > current_info.max_level) {
rb->lcd_putsxy(10, 20, "You WIN!!");
rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
for (i = 0; i < 30000 ; i++) {
rb->lcd_fillrect(0, 0, 111, 63);
rb->lcd_update();
button = rb->button_get(false);
if (button && ((button & BUTTON_REL) != BUTTON_REL))
break;
}
rb->lcd_set_drawmode(DRMODE_SOLID);
return PLUGIN_OK;
}
load_level();
update_screen();
}
} /* end while */
return PLUGIN_OK;
}
enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
int w, h;
int len;
TEST_PLUGIN_API(api);
(void)(parameter);
rb = api;
rb->lcd_setfont(FONT_SYSFIXED);
rb->lcd_getstringsize(SOKOBAN_TITLE, &w, &h);
/* Get horizontel centering for text */
len = w;
if (len%2 != 0)
len =((len+1)/2)+(w/2);
else
len /= 2;
if (h%2 != 0)
h = (h/2)+1;
else
h /= 2;
rb->lcd_clear_display();
rb->lcd_putsxy(LCD_WIDTH/2-len,(LCD_HEIGHT/2)-h, SOKOBAN_TITLE);
rb->lcd_update();
rb->sleep(HZ*2);
rb->lcd_clear_display();
#if CONFIG_KEYPAD == RECORDER_PAD
rb->lcd_putsxy(3, 6, "[OFF] To Stop");
rb->lcd_putsxy(3, 16, "[ON] To Undo");
rb->lcd_putsxy(3, 26, "[F1] - Level");
rb->lcd_putsxy(3, 36, "[F2] Same Level");
rb->lcd_putsxy(3, 46, "[F3] + Level");
#elif CONFIG_KEYPAD == ONDIO_PAD
rb->lcd_putsxy(3, 6, "[OFF] To Stop");
rb->lcd_putsxy(3, 16, "[MODE] To Undo");
rb->lcd_putsxy(3, 26, "[M-LEFT] - Level");
rb->lcd_putsxy(3, 36, "[M-UP] Same Level");
rb->lcd_putsxy(3, 46, "[M-RIGHT] + Level");
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
(CONFIG_KEYPAD == IRIVER_H300_PAD)
rb->lcd_putsxy(3, 6, "[STOP] To Stop");
rb->lcd_putsxy(3, 16, "[PLAY] To Undo");
rb->lcd_putsxy(3, 26, "[REC] - Level");
rb->lcd_putsxy(3, 36, "[SELECT] Same Level");
rb->lcd_putsxy(3, 46, "[MODE] + Level");
#endif
rb->lcd_update();
rb->sleep(HZ*2);
rb->lcd_clear_display();
init_boards();
if (get_level_count() != 0) {
rb->splash(HZ*2, true, "Failed loading levels!");
return PLUGIN_OK;
}
return sokoban_loop();
}
#endif
|