summaryrefslogtreecommitdiffstats
path: root/apps/plugins/sokoban.c
blob: 7941e9758552679d02286975950bc39302153ff2 (plain)
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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
/***************************************************************************
 *             __________               __   ___.
 *   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

PLUGIN_HEADER

#if LCD_DEPTH >= 2
extern const fb_data sokoban_tiles[];
#endif

#define SOKOBAN_TITLE       "Sokoban"

#define LEVELS_FILE         PLUGIN_DIR "/sokoban.levels"

#define ROWS                16
#define COLS                20
#define SOKOBAN_LEVEL_SIZE  (ROWS*COLS)
#define MAX_BUFFERED_BOARDS 500
/* Use either all but 12k of the plugin buffer for board data
   or just enough for MAX_BUFFERED_BOARDS, which ever is less */
#if (PLUGIN_BUFFER_SIZE - 0x3000)/SOKOBAN_LEVEL_SIZE < MAX_BUFFERED_BOARDS
#define NUM_BUFFERED_BOARDS (PLUGIN_BUFFER_SIZE - 0x3000)/SOKOBAN_LEVEL_SIZE
#else
#define NUM_BUFFERED_BOARDS MAX_BUFFERED_BOARDS
#endif
/* Use 4k plus remaining plugin buffer (-8k for prog) for undo, up to 32k */
#if PLUGIN_BUFFER_SIZE - NUM_BUFFERED_BOARDS*SOKOBAN_LEVEL_SIZE - 0x2000 > \
    0x7FFF
#define MAX_UNDOS           0x7FFF
#else
#define MAX_UNDOS           PLUGIN_BUFFER_SIZE - \
                            NUM_BUFFERED_BOARDS*SOKOBAN_LEVEL_SIZE - 0x2000
#endif

/* Move/push definitions for undo */
enum {
    SOKOBAN_PUSH_LEFT,
    SOKOBAN_PUSH_RIGHT,
    SOKOBAN_PUSH_UP,
    SOKOBAN_PUSH_DOWN,
    SOKOBAN_MOVE_LEFT,
    SOKOBAN_MOVE_RIGHT,
    SOKOBAN_MOVE_UP,
    SOKOBAN_MOVE_DOWN
};
#define SOKOBAN_MOVE_DIFF   (SOKOBAN_MOVE_LEFT-SOKOBAN_PUSH_LEFT)
#define SOKOBAN_MOVE_MIN    SOKOBAN_MOVE_LEFT

/* 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_REDO BUTTON_PLAY
#define SOKOBAN_LEVEL_UP BUTTON_F3
#define SOKOBAN_LEVEL_DOWN BUTTON_F1
#define SOKOBAN_LEVEL_REPEAT BUTTON_F2

#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
#define SOKOBAN_UP BUTTON_UP
#define SOKOBAN_DOWN BUTTON_DOWN
#define SOKOBAN_QUIT BUTTON_OFF
#define SOKOBAN_UNDO BUTTON_ON
#define SOKOBAN_REDO BUTTON_PLAY
#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_REDO (BUTTON_MENU | BUTTON_DOWN)
#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_REC
#define SOKOBAN_REDO BUTTON_MODE
#define SOKOBAN_LEVEL_UP (BUTTON_ON | BUTTON_UP)
#define SOKOBAN_LEVEL_DOWN (BUTTON_ON | BUTTON_DOWN)
#define SOKOBAN_LEVEL_REPEAT BUTTON_ON

#define SOKOBAN_RC_QUIT BUTTON_RC_STOP

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
      (CONFIG_KEYPAD == IPOD_3G_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_REDO (BUTTON_SELECT | BUTTON_PLAY)
#define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_RIGHT)
#define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_LEFT)

/* fixme: if/when simultaneous button presses work for X5,
   add redo & level repeat */
#elif (CONFIG_KEYPAD == IAUDIO_X5_PAD)
#define SOKOBAN_UP BUTTON_UP
#define SOKOBAN_DOWN BUTTON_DOWN
#define SOKOBAN_QUIT BUTTON_POWER
#define SOKOBAN_UNDO_PRE BUTTON_SELECT
#define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
#define SOKOBAN_LEVEL_UP BUTTON_PLAY
#define SOKOBAN_LEVEL_DOWN BUTTON_REC

#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
#define SOKOBAN_UP BUTTON_UP
#define SOKOBAN_DOWN BUTTON_DOWN
#define SOKOBAN_QUIT BUTTON_A
#define SOKOBAN_UNDO BUTTON_SELECT
#define SOKOBAN_REDO BUTTON_POWER
#define SOKOBAN_LEVEL_UP (BUTTON_MENU | BUTTON_UP)
#define SOKOBAN_LEVEL_DOWN (BUTTON_MENU | BUTTON_DOWN)
#define SOKOBAN_LEVEL_REPEAT BUTTON_MENU

#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
#define SOKOBAN_UP BUTTON_UP
#define SOKOBAN_DOWN BUTTON_DOWN
#define SOKOBAN_QUIT BUTTON_POWER
#define SOKOBAN_UNDO_PRE BUTTON_SELECT
#define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
#define SOKOBAN_REDO BUTTON_REC
#define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_UP)
#define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_DOWN)
#define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)

#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
#define SOKOBAN_UP BUTTON_SCROLL_UP
#define SOKOBAN_DOWN BUTTON_SCROLL_DOWN
#define SOKOBAN_QUIT BUTTON_POWER
#define SOKOBAN_UNDO_PRE BUTTON_REW
#define SOKOBAN_UNDO (BUTTON_REW | BUTTON_REL)
#define SOKOBAN_REDO BUTTON_FF
#define SOKOBAN_LEVEL_UP (BUTTON_PLAY | BUTTON_SCROLL_UP)
#define SOKOBAN_LEVEL_DOWN (BUTTON_PLAY | BUTTON_SCROLL_DOWN)
#define SOKOBAN_LEVEL_REPEAT (BUTTON_PLAY | BUTTON_RIGHT)

#endif

#ifdef HAVE_LCD_COLOR
/* Background color. Default Rockbox light blue. */
#define BG_COLOR LCD_RGBPACK(181, 199, 231)

#elif LCD_DEPTH >= 2
#define MEDIUM_GRAY LCD_BRIGHTNESS(127)
#endif

/* 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. */

/* Level data & stats */
struct LevelInfo {
    short level;
    short moves;
    short pushes;
    short boxes_to_go;
};

struct Location {
    short row;
    short col;
};

struct Board {
    char spaces[ROWS][COLS];
};

/* Our full undo history */
static struct UndoInfo {
    short count;    /* How many undos are left */
    short current;  /* Which history is the current undo */
    short max;      /* Which history is the max redoable */
    char 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 loaded_level;            /* Which level is in memory */
} current_info;

static struct BufferedBoards {
    struct Board levels[NUM_BUFFERED_BOARDS];
    int low;
} buffered_boards;

static struct plugin_api* rb;

static void init_undo(void)
{
    undo_info.count = 0;
    undo_info.current = -1;
    undo_info.max = -1;
}

static void get_delta(char direction, short *d_r, short *d_c)
{
    switch (direction) {
        case SOKOBAN_PUSH_LEFT:
        case SOKOBAN_MOVE_LEFT:
            *d_r = 0;
            *d_c = -1;
            break;
        case SOKOBAN_PUSH_RIGHT:
        case SOKOBAN_MOVE_RIGHT:
            *d_r = 0;
            *d_c = 1;
            break;
        case SOKOBAN_PUSH_UP:
        case SOKOBAN_MOVE_UP:
            *d_r = -1;
            *d_c = 0;
            break;
        case SOKOBAN_PUSH_DOWN:
        case SOKOBAN_MOVE_DOWN:
            *d_r = 1;
            *d_c = 0;
    }
}

static void undo(void)
{
    char undo;
    short r, c;
    short d_r = 0, d_c = 0; /* delta row & delta col */
    char *space_cur, *space_next, *space_prev;
    bool undo_push = false;

    /* If no more undos or we've wrapped all the way around, quit */
    if (undo_info.count == 0 || undo_info.current-1 == undo_info.max)
        return;

    undo = undo_info.history[undo_info.current];

    if (undo < SOKOBAN_MOVE_MIN)
        undo_push = true;

    get_delta(undo, &d_r, &d_c);

    r = current_info.player.row;
    c = current_info.player.col;

    /* Give the 3 spaces we're going to use better names */
    space_cur = &current_info.board[r][c];
    space_next = &current_info.board[r + d_r][c + d_c];
    space_prev = &current_info.board[r - d_r][c - d_c];

    /* Update board info */
    if (undo_push) {
        /* Moving box from goal to blank */
        if (*space_next == '%' && *space_cur == '@')
            current_info.level.boxes_to_go++;
        /* Moving box from blank to goal */
        else if (*space_next == '$' && *space_cur == '+')
            current_info.level.boxes_to_go--;

        /* Move box off of next space... */
        *space_next = (*space_next == '%' ? '.' : ' ');
        /* ...and on to current space */
        *space_cur = (*space_cur == '+' ? '%' : '$');

        current_info.level.pushes--;
    } else
        /* Just move player off of current space */
        *space_cur = (*space_cur == '+' ? '.' : ' ');
    /* Move player back to previous space */
    *space_prev = (*space_prev == '.' ? '+' : '@');

    /* Update position */
    current_info.player.row -= d_r;
    current_info.player.col -= d_c;

    current_info.level.moves--;

    /* Move to previous undo in the list */
    if (undo_info.current == 0 && undo_info.count > 1)
        undo_info.current = MAX_UNDOS - 1;
    else
        undo_info.current--;

    undo_info.count--;

    return;
}

static void add_undo(char undo)
{
    /* Wrap around if MAX_UNDOS exceeded */
    if (undo_info.current < (MAX_UNDOS - 1))
        undo_info.current++;
    else
        undo_info.current = 0;

    undo_info.history[undo_info.current] = undo;

    if (undo_info.count < MAX_UNDOS)
        undo_info.count++;
}

static bool move(char direction, bool redo)
{
    short r, c;
    short d_r = 0, d_c = 0; /* delta row & delta col */
    char *space_cur, *space_next, *space_beyond;
    bool push = false;

    get_delta(direction, &d_r, &d_c);

    r = current_info.player.row;
    c = current_info.player.col;

    /* Check for out-of-bounds */
    if (r + 2*d_r < 0 || r + 2*d_r >= ROWS ||
        c + 2*d_c < 0 || c + 2*d_c >= COLS)
        return false;

    /* Give the 3 spaces we're going to use better names */
    space_cur = &current_info.board[r][c];
    space_next = &current_info.board[r + d_r][c + d_c];
    space_beyond = &current_info.board[r + 2*d_r][c + 2*d_c];

    if (*space_next == '$' || *space_next == '%') {
        /* Change direction from move to push for undo */
        if (direction >= SOKOBAN_MOVE_MIN)
            direction -= SOKOBAN_MOVE_DIFF;
        push = true;
    }

    /* Update board info */
    if (push) {
        /* Moving box from goal to blank */
        if (*space_next == '%' && *space_beyond == ' ')
            current_info.level.boxes_to_go++;
        /* Moving box from blank to goal */
        else if (*space_next == '$' && *space_beyond == '.')
            current_info.level.boxes_to_go--;
        /* Check for illegal move */
        else if (*space_beyond != '.' && *space_beyond != ' ')
            return false;

        /* Move player onto next space */
        *space_next = (*space_next == '%' ? '+' : '@');
        /* Move box onto space beyond next */
        *space_beyond = (*space_beyond == '.' ? '%' : '$');

        current_info.level.pushes++;
    } else {
        /* Check for illegal move */
        if (*space_next == '#' || *space_next == 'X')
            return false;

        /* Move player onto next space */
        *space_next = (*space_next == '.' ? '+' : '@');
    }
    /* Move player off of current space */
    *space_cur = (*space_cur == '+' ? '.' : ' ');

    /* Update position */
    current_info.player.row += d_r;
    current_info.player.col += d_c;

    current_info.level.moves++;

    /* Update undo_info.max to current on every normal move,
       except if it's the same as a redo. */
    /* normal move */
    if (!redo &&
          /* moves have been undone */
        ((undo_info.max != undo_info.current &&
          /* and the current move is NOT the same as the one in history */
          undo_info.history[undo_info.current+1] != direction) ||
         /* or moves have not been undone */
         undo_info.max == undo_info.current)) {
        add_undo(direction);
        undo_info.max = undo_info.current;
    } else /* redo move or move was same as redo */
        add_undo(direction); /* (just to update current) */

    return true;
}

#ifdef SOKOBAN_REDO
static bool redo(void)
{
    /* If no moves have been undone, quit */
    if (undo_info.current == undo_info.max)
        return false;

    return move(undo_info.history[(undo_info.current+1 < MAX_UNDOS ?
                                              undo_info.current+1 : 0)], true);
}
#endif

static void init_boards(void)
{
    current_info.level.level = 0;
    current_info.level.moves = 0;
    current_info.level.pushes = 0;
    current_info.level.boxes_to_go = 0;
    current_info.player.row = 0;
    current_info.player.col = 0;
    current_info.max_level = 0;
    current_info.loaded_level = 0;

    buffered_boards.low = 0;

    init_undo();
}

static int read_levels(int initialize_count)
{
    int fd = 0;
    int len;
    int lastlen = 0;
    int row = 0;
    int level_count = 0;
    char buffer[COLS + 3]; /* COLS plus CR/LF and \0 */
    int endpoint = current_info.level.level-1;

    if (endpoint < buffered_boards.low)
        endpoint = current_info.level.level - NUM_BUFFERED_BOARDS;

    if (endpoint < 0) endpoint = 0;

    buffered_boards.low = endpoint;
    endpoint += NUM_BUFFERED_BOARDS;

    if ((fd = rb->open(LEVELS_FILE, O_RDONLY)) < 0) {
        rb->splash(HZ*2, true, "Unable to open %s", LEVELS_FILE);
        return -1;
    }

    do {
        len = rb->read_line(fd, buffer, sizeof(buffer));
        if (len >= 3) {
            /* This finds lines that are more than 1 or 2 characters
             * shorter than they should be.  Due to the possibility of
             * a mixed unix and dos CR/LF file format, I'm not going to
             * do a precise check */
            if (len < COLS) {
                rb->splash(HZ*2, true, "Error in levels file: short line");
                return -1;
            }
            if (level_count >= buffered_boards.low && level_count < endpoint) {
                int index = level_count - buffered_boards.low;
                rb->memcpy(
                      buffered_boards.levels[index].spaces[row], buffer, COLS);
            }
            row++;
        } else if (len) {
            if (lastlen < 3) {
                /* Two short lines in a row means new level */
                level_count++;
                if (level_count >= endpoint && !initialize_count) break;
                if (level_count && row != ROWS) {
                    rb->splash(HZ*2, true,
                               "Error in levels file: short board");
                    return -1;
                }
                row = 0;
            }
        }
    } while ((lastlen=len));

    rb->close(fd);
    if (initialize_count) {
        /* Plus one because there aren't trailing short lines in the file */
        current_info.max_level = level_count + 1;
    }
    return 0;
}

/* return non-zero on error */
static void load_level(void)
{
    int c = 0;
    int r = 0;
    int index = current_info.level.level - buffered_boards.low - 1;
    struct Board *level;

    if (index < 0 || index >= NUM_BUFFERED_BOARDS) {
        read_levels(false);
        index = index < 0 ? NUM_BUFFERED_BOARDS-1 : 0;
    }
    level = &buffered_boards.levels[index];

    current_info.level.boxes_to_go = 0;
    current_info.level.moves = 0;
    current_info.level.pushes = 0;
    current_info.loaded_level = current_info.level.level;

    for (r = 0; r < ROWS; r++) {
        for (c = 0; c < COLS; c++) {
            current_info.board[r][c] = level->spaces[r][c];

            if (current_info.board[r][c] == '.' ||
                current_info.board[r][c] == '+')
                current_info.level.boxes_to_go++;

            if (current_info.board[r][c] == '@' ||
                current_info.board[r][c] == '+') {
                current_info.player.row = r;
                current_info.player.col = c;
            }
        }
    }
}

static void update_screen(void)
{
    int b = 0, c = 0;
    int rows = 0, cols = 0;
    char s[25];

/* magnify is the number of pixels for each block */
#if (LCD_HEIGHT >= 224) && (LCD_WIDTH >= 312) || \
    (LCD_HEIGHT >= 249) && (LCD_WIDTH >= 280) /* ipod 5g */
#define MAGNIFY 14
#elif (LCD_HEIGHT >= 144) && (LCD_WIDTH >= 212) || \
      (LCD_HEIGHT >= 169) && (LCD_WIDTH >= 180-4) /* h3x0, ipod color/photo */
#define MAGNIFY 9
#elif (LCD_HEIGHT >= 96) && (LCD_WIDTH >= 152) || \
      (LCD_HEIGHT >= 121) && (LCD_WIDTH >= 120) /* h1x0, ipod nano/mini */
#define MAGNIFY 6
#else /* other */
#define MAGNIFY 4
#endif

#if LCD_DEPTH < 2
    int i, j;
    int max = MAGNIFY - 1;
    int middle = max / 2;
    int ldelta = (middle + 1) / 2;
#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 >= 2
                rb->lcd_bitmap_part(sokoban_tiles, 0, 1*MAGNIFY, MAGNIFY,
                                    c, b, MAGNIFY, MAGNIFY);
#else
                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 box */
#if LCD_DEPTH >= 2
                rb->lcd_bitmap_part(sokoban_tiles, 0, 2*MAGNIFY, MAGNIFY,
                                    c, b, MAGNIFY, MAGNIFY);
#else
                /* Free boxes are not filled in */
                rb->lcd_drawrect(c, b, MAGNIFY, MAGNIFY);
#endif
                break;

            case '*':
            case '%': /* this is a box on a goal */

#if LCD_DEPTH >= 2
                rb->lcd_bitmap_part(sokoban_tiles, 0, 3*MAGNIFY, MAGNIFY,
                                    c, b, MAGNIFY, MAGNIFY );
#else
                rb->lcd_drawrect(c, b, MAGNIFY, MAGNIFY);
                rb->lcd_drawrect(c+(MAGNIFY/2)-1, b+(MAGNIFY/2)-1, MAGNIFY/2,
                                 MAGNIFY/2);
#endif
                break;

            case '.': /* this is a goal */
#if LCD_DEPTH >= 2
                rb->lcd_bitmap_part(sokoban_tiles, 0, 4*MAGNIFY, MAGNIFY,
                                    c, b, MAGNIFY, MAGNIFY);
#else
                rb->lcd_drawrect(c+(MAGNIFY/2)-1, b+(MAGNIFY/2)-1, MAGNIFY/2,
                                 MAGNIFY/2);
#endif
                break;

            case '@': /* this is you */
#if LCD_DEPTH >= 2
                rb->lcd_bitmap_part(sokoban_tiles, 0, 5*MAGNIFY, MAGNIFY,
                                    c, b, MAGNIFY, MAGNIFY);
#else
                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);
#endif
                break;

            case '+': /* this is you on drugs, erm, on a goal */
#if LCD_DEPTH >= 2
                rb->lcd_bitmap_part(sokoban_tiles, 0, 6*MAGNIFY, MAGNIFY,
                                    c, b, MAGNIFY, MAGNIFY );
#else
                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);
                rb->lcd_drawline(c+middle-1, b+middle+1, c+max-middle+1,
                                 b+middle+1);
#endif
                break;

#if LCD_DEPTH >= 2
            default:
                rb->lcd_bitmap_part(sokoban_tiles, 0, 0*MAGNIFY, MAGNIFY,
                                    c, b, MAGNIFY, MAGNIFY );
#endif
            }
        }
    }

#if LCD_WIDTH-(COLS*MAGNIFY) < 32
#define STAT_SIZE 25
#define STAT_POS LCD_HEIGHT-STAT_SIZE
#define STAT_CENTER (LCD_WIDTH-120)/2

    rb->lcd_putsxy(4+STAT_CENTER, STAT_POS+4, "Level");
    rb->snprintf(s, sizeof(s), "%d", current_info.level.level);
    rb->lcd_putsxy(7+STAT_CENTER, STAT_POS+14, s);
    rb->lcd_putsxy(41+STAT_CENTER, STAT_POS+4, "Moves");
    rb->snprintf(s, sizeof(s), "%d", current_info.level.moves);
    rb->lcd_putsxy(44+STAT_CENTER, STAT_POS+14, s);
    rb->lcd_putsxy(79+STAT_CENTER, STAT_POS+4, "Pushes");
    rb->snprintf(s, sizeof(s), "%d", current_info.level.pushes);
    rb->lcd_putsxy(82+STAT_CENTER, STAT_POS+14, s);

    rb->lcd_drawrect(STAT_CENTER, STAT_POS, 38, STAT_SIZE);
    rb->lcd_drawrect(37+STAT_CENTER, STAT_POS, 39, STAT_SIZE);
    rb->lcd_drawrect(75+STAT_CENTER, STAT_POS, 45, STAT_SIZE);

#else
#define STAT_POS COLS*MAGNIFY
#define STAT_SIZE LCD_WIDTH-STAT_POS

    rb->lcd_putsxy(STAT_POS+1, 3, "Level");
    rb->snprintf(s, sizeof(s), "%d", current_info.level.level);
    rb->lcd_putsxy(STAT_POS+4, 13, s);
    rb->lcd_putsxy(STAT_POS+1, 26, "Moves");
    rb->snprintf(s, sizeof(s), "%d", current_info.level.moves);
    rb->lcd_putsxy(STAT_POS+4, 36, s);

    rb->lcd_drawrect(STAT_POS, 0, STAT_SIZE, 24);
    rb->lcd_drawrect(STAT_POS, 23, STAT_SIZE, 24);

#if LCD_HEIGHT >= 70
    rb->lcd_putsxy(STAT_POS+1, 49, "Pushes");
    rb->snprintf(s, sizeof(s), "%d", current_info.level.pushes);
    rb->lcd_putsxy(STAT_POS+4, 59, s);

    rb->lcd_drawrect(STAT_POS, 46, STAT_SIZE, 24);
#endif
#endif

    /* 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)
{
    bool moved = true;
    int i = 0, button = 0, lastbutton = 0;
    short r = 0, c = 0;
    int w, h;
    char s[25];

    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);

        switch(button)
        {
#ifdef SOKOBAN_RC_QUIT
            case SOKOBAN_RC_QUIT:
#endif
            case SOKOBAN_QUIT:
                /* get out of here */
#ifdef HAVE_LCD_COLOR /* reset background color */
                rb->lcd_set_background(rb->global_settings->bg_color);
#endif
                return PLUGIN_OK;

            case SOKOBAN_UNDO:
#ifdef SOKOBAN_UNDO_PRE
                if (lastbutton != SOKOBAN_UNDO_PRE)
                    break;
#else       /* repeat can't work here for Ondio et al */
            case SOKOBAN_UNDO | BUTTON_REPEAT:
#endif
                undo();
                rb->lcd_clear_display();
                update_screen();
                moved = false;
                break;

#ifdef SOKOBAN_REDO
            case SOKOBAN_REDO:
            case SOKOBAN_REDO | BUTTON_REPEAT:
                moved = redo();
                rb->lcd_clear_display();
                update_screen();
                break;
#endif

            case SOKOBAN_LEVEL_UP:
            case SOKOBAN_LEVEL_UP | BUTTON_REPEAT:
                /* next level */
                init_undo();
                if (current_info.level.level < current_info.max_level)
                    current_info.level.level++;

                draw_level();
                moved = false;
                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;

#ifdef SOKOBAN_LEVEL_REPEAT
            case SOKOBAN_LEVEL_REPEAT:
            case SOKOBAN_LEVEL_REPEAT | BUTTON_REPEAT:
                /* same level */
                init_undo();
                draw_level();
                moved = false;
                break;
#endif

            case BUTTON_LEFT:
            case BUTTON_LEFT | BUTTON_REPEAT:
                moved = move(SOKOBAN_MOVE_LEFT, false);
                break;

            case BUTTON_RIGHT:
            case BUTTON_RIGHT | BUTTON_REPEAT:
                moved = move(SOKOBAN_MOVE_RIGHT, false);
                break;

            case SOKOBAN_UP:
            case SOKOBAN_UP | BUTTON_REPEAT:
                moved = move(SOKOBAN_MOVE_UP, false);
                break;

            case SOKOBAN_DOWN:
            case SOKOBAN_DOWN | BUTTON_REPEAT:
                moved = move(SOKOBAN_MOVE_DOWN, false);
                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) {
            rb->lcd_clear_display();
            update_screen();
        }

        /* We have completed this level */
        if (current_info.level.boxes_to_go == 0) {

            if (moved) {
                rb->lcd_clear_display();
                /* Center level completed message */
                rb->snprintf(s, sizeof(s), "Level %d Complete!",
                             current_info.level.level);
                rb->lcd_getstringsize(s, &w, &h);
                rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - 16 , s);
                rb->snprintf(s, sizeof(s), "%4d Moves ",
                             current_info.level.moves);
                rb->lcd_getstringsize(s, &w, &h);
                rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + 0 , s);
                rb->snprintf(s, sizeof(s), "%4d Pushes",
                             current_info.level.pushes);
                rb->lcd_getstringsize(s, &w, &h);
                rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + 8 , s);
                rb->lcd_update();
                rb->button_get(false);

                rb->sleep(HZ/2);
                for (i = 0; i < 30; i++) {
                    rb->sleep(HZ/20);
                    button = rb->button_get(false);
                    if (button && ((button & BUTTON_REL) != BUTTON_REL))
                        break;
                }
            }

            current_info.level.level++;

            /* clear undo stats */
            init_undo();

            rb->lcd_clear_display();

            if (current_info.level.level > current_info.max_level) {
                /* Center "You WIN!!" on all screen sizes */
                rb->snprintf(s, sizeof(s), "You WIN!!");
                rb->lcd_getstringsize(s, &w, &h);
                rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, s);

                rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
                /* Display for 10 seconds or until keypress */
                for (i = 0; i < 200; i++) {
                    rb->lcd_fillrect(0, 0, LCD_WIDTH, LCD_HEIGHT);
                    rb->lcd_update();
                    rb->sleep(HZ/20);

                    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 i;
    int button = 0;

    (void)(parameter);
    rb = api;

    rb->lcd_setfont(FONT_SYSFIXED);

#ifdef HAVE_LCD_COLOR
    rb->lcd_set_background(BG_COLOR);
#endif

    rb->lcd_clear_display();
    rb->lcd_getstringsize(SOKOBAN_TITLE, &w, &h);
    rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, SOKOBAN_TITLE);
    rb->lcd_update();
    rb->sleep(HZ);

    rb->lcd_clear_display();

#if (CONFIG_KEYPAD == RECORDER_PAD) || \
    (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
    rb->lcd_putsxy(3,  6, "[OFF] Quit");
    rb->lcd_putsxy(3, 16, "[ON] Undo");
    rb->lcd_putsxy(3, 26, "[PLAY] Redo");
    rb->lcd_putsxy(3, 36, "[F1] Down a Level");
    rb->lcd_putsxy(3, 46, "[F2] Restart Level");
    rb->lcd_putsxy(3, 56, "[F3] Up a Level");
#elif CONFIG_KEYPAD == ONDIO_PAD
    rb->lcd_putsxy(3,  6, "[OFF] Quit");
    rb->lcd_putsxy(3, 16, "[MODE] Undo");
    rb->lcd_putsxy(3, 26, "[MODE+DOWN] Redo");
    rb->lcd_putsxy(3, 36, "[MODE+LEFT] Down a Level");
    rb->lcd_putsxy(3, 46, "[MODE+UP] Restart Level");
    rb->lcd_putsxy(3, 56, "[MODE+RIGHT] Up Level");
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
      (CONFIG_KEYPAD == IRIVER_H300_PAD)
    rb->lcd_putsxy(3,  6, "[STOP] Quit");
    rb->lcd_putsxy(3, 16, "[REC] Undo");
    rb->lcd_putsxy(3, 26, "[MODE] Redo");
    rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Down a Level");
    rb->lcd_putsxy(3, 46, "[PLAY] Restart Level");
    rb->lcd_putsxy(3, 56, "[PLAY+UP] Up a Level");
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
      (CONFIG_KEYPAD == IPOD_3G_PAD)
    rb->lcd_putsxy(3,  6, "[SELECT+MENU] Quit");
    rb->lcd_putsxy(3, 16, "[SELECT] Undo");
    rb->lcd_putsxy(3, 26, "[SELECT+PLAY] Redo");
    rb->lcd_putsxy(3, 36, "[SELECT+LEFT] Down a Level");
    rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Up a Level");
#elif CONFIG_KEYPAD == IAUDIO_X5_PAD
    rb->lcd_putsxy(3,  6, "[POWER] Quit");
    rb->lcd_putsxy(3, 16, "[SELECT] Undo");
    rb->lcd_putsxy(3, 26, "[REC] Down a Level");
    rb->lcd_putsxy(3, 36, "[PLAY] Up Level");
#elif CONFIG_KEYPAD == GIGABEAT_PAD
    rb->lcd_putsxy(3,  6, "[A] Quit");
    rb->lcd_putsxy(3, 16, "[SELECT] Undo");
    rb->lcd_putsxy(3, 26, "[POWER] Redo");
    rb->lcd_putsxy(3, 36, "[MENU+DOWN] Down a Level");
    rb->lcd_putsxy(3, 46, "[MENU] Restart Level");
    rb->lcd_putsxy(3, 56, "[MENU+UP] Up Level");
#elif CONFIG_KEYPAD == SANSA_E200_PAD
    rb->lcd_putsxy(3,  6, "[POWER] Quit");
    rb->lcd_putsxy(3, 16, "[SELECT] Undo");
    rb->lcd_putsxy(3, 26, "[REC] Redo");
    rb->lcd_putsxy(3, 36, "[SELECT+DOWN] Down a Level");
    rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Restart Level");
    rb->lcd_putsxy(3, 56, "[SELECT+UP] Up Level");
#elif CONFIG_KEYPAD == IRIVER_H10_PAD
    rb->lcd_putsxy(3,  6, "[POWER] Quit");
    rb->lcd_putsxy(3, 16, "[REW] Undo");
    rb->lcd_putsxy(3, 26, "[FF] Redo");
    rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Down a Level");
    rb->lcd_putsxy(3, 46, "[PLAY+RIGHT] Restart Level");
    rb->lcd_putsxy(3, 56, "[PLAY+UP] Up Level");
#endif

    rb->lcd_update();
    rb->button_get(false);
    /* Display for 3 seconds or until keypress */
    for (i = 0; i < 60; i++) {
        rb->sleep(HZ/20);
        button = rb->button_get(false);
        if (button && ((button & BUTTON_REL) != BUTTON_REL))
            break;
    }
    rb->lcd_clear_display();

    init_boards();

    if (read_levels(1) != 0)
        return PLUGIN_OK;

    return sokoban_loop();
}

#endif