summaryrefslogtreecommitdiffstats
path: root/apps/plugins/dice.c
blob: 8638cb8cc3560d68c06e687c24544b1d9a3782b3 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Brandon Low
 *
 * 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.
 *
 ****************************************************************************/

/*****************************************************************************
Dice by lostlogic

use left and right to pick between sides and number of dice
use up and down to select number of sides or number of dice
use select or play to roll the dice
use stop to exit

*****************************************************************************/

#include "plugin.h"
#include "button.h"
#include "lcd.h"

#define MAX_DICE 12
#define NUM_SIDE_CHOICES 8
#if (CONFIG_KEYPAD == PLAYER_PAD)
    #define SELECTIONS_SIZE 9
    #define START_DICE_ROW 1
    #define ROWS 1
    #define LINE_LENGTH 50
    #define SELECTIONS_ROW 0
#else
    #define SELECTIONS_SIZE 7
    #define SELECTIONS_ROW 4
    #if (CONFIG_KEYPAD == RECORDER_PAD) || (CONFIG_KEYPAD == ONDIO_PAD)
        #define START_DICE_ROW 0
        #define ROWS 3
        #define LINE_LENGTH 18
    #else
        #define START_DICE_ROW 7
        #define ROWS 2
        #define LINE_LENGTH 26
    #endif
#endif

/* Values for selected */
#define CHANGE_DICE 0
#define CHANGE_SIDES 1
#define EXIT 2



#if CONFIG_KEYPAD == RECORDER_PAD
#define DICE_BUTTON_UP         BUTTON_UP
#define DICE_BUTTON_DOWN       BUTTON_DOWN
#define DICE_BUTTON_LEFT       BUTTON_LEFT
#define DICE_BUTTON_RIGHT      BUTTON_RIGHT
#define DICE_BUTTON_OFF        BUTTON_OFF
#define DICE_BUTTON_ON         BUTTON_ON
#define DICE_BUTTON_SELECT     BUTTON_PLAY

#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
#define DICE_BUTTON_UP         BUTTON_UP
#define DICE_BUTTON_DOWN       BUTTON_DOWN
#define DICE_BUTTON_LEFT       BUTTON_LEFT
#define DICE_BUTTON_RIGHT      BUTTON_RIGHT
#define DICE_BUTTON_OFF        BUTTON_OFF
#define DICE_BUTTON_ON         BUTTON_ON
#define DICE_BUTTON_SELECT     BUTTON_SELECT

#elif CONFIG_KEYPAD == ONDIO_PAD
#define DICE_BUTTON_UP         BUTTON_UP
#define DICE_BUTTON_DOWN       BUTTON_DOWN
#define DICE_BUTTON_LEFT       BUTTON_LEFT
#define DICE_BUTTON_RIGHT      BUTTON_RIGHT
#define DICE_BUTTON_OFF        BUTTON_OFF
#define DICE_BUTTON_SELECT    (BUTTON_MENU|BUTTON_REL)
#define DICE_BUTTON_ON        (BUTTON_MENU|BUTTON_OFF)

#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define DICE_BUTTON_UP         BUTTON_UP
#define DICE_BUTTON_DOWN       BUTTON_DOWN
#define DICE_BUTTON_OFF        BUTTON_OFF
#define DICE_BUTTON_ON         BUTTON_ON
#define DICE_BUTTON_SELECT     BUTTON_SELECT
#define DICE_BUTTON_LEFT       BUTTON_LEFT
#define DICE_BUTTON_RIGHT      BUTTON_RIGHT

#define DICE_BUTTON_RC_OFF     BUTTON_RC_STOP

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD)
#define DICE_BUTTON_UP         BUTTON_SCROLL_FWD
#define DICE_BUTTON_DOWN       BUTTON_SCROLL_BACK
#define DICE_BUTTON_OFF       (BUTTON_PLAY|BUTTON_REPEAT)
#define DICE_BUTTON_ON        (BUTTON_SELECT|BUTTON_PLAY)
#define DICE_BUTTON_SELECT     BUTTON_SELECT
#define DICE_BUTTON_LEFT       BUTTON_LEFT
#define DICE_BUTTON_RIGHT      BUTTON_RIGHT

#elif (CONFIG_KEYPAD == PLAYER_PAD)
#define DICE_BUTTON_UP         BUTTON_PLAY
#define DICE_BUTTON_DOWN       BUTTON_STOP
#define DICE_BUTTON_LEFT       BUTTON_LEFT
#define DICE_BUTTON_RIGHT      BUTTON_RIGHT
#define DICE_BUTTON_SELECT     BUTTON_ON
#define DICE_BUTTON_ON        (BUTTON_ON|BUTTON_REPEAT)
#define DICE_BUTTON_OFF        BUTTON_MENU

#elif (CONFIG_KEYPAD == IAUDIO_X5_PAD)
#define DICE_BUTTON_UP         BUTTON_UP
#define DICE_BUTTON_DOWN       BUTTON_DOWN
#define DICE_BUTTON_LEFT       BUTTON_LEFT
#define DICE_BUTTON_RIGHT      BUTTON_RIGHT
#define DICE_BUTTON_SELECT     BUTTON_SELECT
#define DICE_BUTTON_ON         BUTTON_PLAY
#define DICE_BUTTON_OFF        BUTTON_POWER
 
#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
#define DICE_BUTTON_UP         BUTTON_UP
#define DICE_BUTTON_DOWN       BUTTON_DOWN
#define DICE_BUTTON_LEFT       BUTTON_LEFT
#define DICE_BUTTON_RIGHT      BUTTON_RIGHT
#define DICE_BUTTON_SELECT     BUTTON_SELECT
#define DICE_BUTTON_ON         BUTTON_POWER
#define DICE_BUTTON_OFF        BUTTON_A

#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
#define DICE_BUTTON_UP         BUTTON_SCROLL_UP
#define DICE_BUTTON_DOWN       BUTTON_SCROLL_DOWN
#define DICE_BUTTON_LEFT       BUTTON_LEFT
#define DICE_BUTTON_RIGHT      BUTTON_RIGHT
#define DICE_BUTTON_SELECT     BUTTON_SELECT
#define DICE_BUTTON_ON         BUTTON_UP
#define DICE_BUTTON_OFF        BUTTON_POWER

#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
#define DICE_BUTTON_UP         BUTTON_SCROLL_UP
#define DICE_BUTTON_DOWN       BUTTON_SCROLL_DOWN
#define DICE_BUTTON_LEFT       BUTTON_LEFT
#define DICE_BUTTON_RIGHT      BUTTON_RIGHT
#define DICE_BUTTON_SELECT     BUTTON_REW
#define DICE_BUTTON_ON         BUTTON_PLAY
#define DICE_BUTTON_OFF        BUTTON_POWER
 
#else
    #error DICE: Unsupported keypad
#endif




PLUGIN_HEADER
                           /* 0, 1, 2, 3,  4,  5,  6,   7 */
static const int SIDES[] = { 3, 4, 6, 8, 10, 12, 20, 100 };

static struct plugin_api* rb;

static int roll_dice(int dice[], const int num_dice, const int side_index);
static void print_dice(const int dice[], const int total);
static void print_selections(const int selected,
        const int num_dice,
        const int side_index);
static void print_help(void);

/* plugin entry point */
enum plugin_status plugin_start(struct plugin_api* api, void* parameter) {
    int side_index = 6;
    int num_dice = 1;
    int selected = CHANGE_DICE;

    /* plugin init */
    (void)parameter;
    rb = api;

    rb->lcd_clear_display();
    rb->srand(*rb->current_tick);
    /* end of plugin init */

    print_selections(selected, num_dice, side_index);
    print_help();
    while(selected!=EXIT) {
        switch(rb->button_get(true)) {
            case DICE_BUTTON_UP:
                if (selected==CHANGE_DICE) {
                    num_dice%=MAX_DICE;
                    num_dice++;
                } else if (selected==CHANGE_SIDES) {
                    side_index++;
                    side_index%=NUM_SIDE_CHOICES;
                }
                print_selections(selected, num_dice, side_index);
                break;

            case DICE_BUTTON_DOWN:
                if (selected==CHANGE_DICE) {
                    num_dice--;
                    if (num_dice==0) {
                        num_dice=MAX_DICE;
                    }
                } else if (selected==CHANGE_SIDES) {
                    if (side_index==0) {
                        side_index=NUM_SIDE_CHOICES;
                    }
                    side_index--;
                }
                print_selections(selected, num_dice, side_index);
                break;

            case DICE_BUTTON_LEFT:
                selected = CHANGE_DICE;
                print_selections(selected, num_dice, side_index);
                break;

            case DICE_BUTTON_RIGHT:
                selected = CHANGE_SIDES;
                print_selections(selected, num_dice, side_index);
                break;

            case DICE_BUTTON_ON:
            case DICE_BUTTON_SELECT:
                {
                    int dice[MAX_DICE];
                    int total = roll_dice(dice, num_dice, side_index);
                    print_dice(dice, total);
                }
                break;
#ifdef DICE_BUTTON_RC_OFF
            case DICE_BUTTON_RC_OFF:
#endif
            case DICE_BUTTON_OFF:
                selected = EXIT;
                break;

            default:
                break;
        } /* switch */
    } /* while */

    return PLUGIN_OK;
}

/* Clears the dice array, rolls the dice and returns the sum */
static int roll_dice(int dice[], const int num_dice, const int side_index) {
    int i;
    int total = 0;
    rb->memset((void*)dice,0,MAX_DICE*sizeof(int));
    for (i=0; i<num_dice; i++) {
        dice[i] = rb->rand()%SIDES[side_index] + 1;
        total+=dice[i];
    }
    return total;
}

#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)

/* Prints the dice, and the sum of the dice values */
static void print_dice(const int dice[], const int total) {
    const int dice_per_row = MAX_DICE/ROWS + (MAX_DICE%ROWS?1:0);
    char showdice[LINE_LENGTH];
    int row;
    for (row=0; /*break;*/; row++) {
        const int start = row*dice_per_row;
        const int end = min(MAX_DICE,start+dice_per_row);
        char *pointer = showdice;
        int space = LINE_LENGTH;
        int i;
        rb->memset((void*)showdice,0,LINE_LENGTH*sizeof(char));
        for (i=start; i<end && dice[i]>0; i++) {
            int count = rb->snprintf(pointer,space," %3d",dice[i]);
            pointer = &pointer[count];
            space -= count;
        }
        if (i > start) {
            rb->lcd_puts_scroll(0,START_DICE_ROW+row,showdice);
        } else {
            row--;
            break;
        }
        if (i < end || end == MAX_DICE) break;
    }
#if (CONFIG_KEYPAD == PLAYER_PAD)
    (void)total;
#else
    rb->lcd_puts(0,START_DICE_ROW+(++row)," ");
    if (total > 0) {
        rb->snprintf(showdice,LINE_LENGTH,"Total: %4d",total);
        rb->lcd_puts(1,START_DICE_ROW+(++row),showdice);
    }
    while (row < ROWS+2) {
        rb->lcd_puts(0,START_DICE_ROW+(++row)," ");
    }
#endif
#ifdef HAVE_LCD_BITMAP
    rb->lcd_update();
#endif
}

/* Print the current user input choices */
static void print_selections(const int selected,
        const int num_dice,
        const int side_index) {
    char buffer[SELECTIONS_SIZE];
#if (CONFIG_KEYPAD == PLAYER_PAD)
    rb->snprintf(buffer, SELECTIONS_SIZE, "%c%2dd%c%3d",
            selected==CHANGE_DICE?'*':' ', num_dice,
            selected==CHANGE_SIDES?'*':' ', SIDES[side_index]);
    rb->lcd_puts(1,SELECTIONS_ROW,buffer);
#else
    rb->snprintf(
            buffer,SELECTIONS_SIZE,"%2dd%3d",num_dice,SIDES[side_index]);
    rb->lcd_puts(1,SELECTIONS_ROW,buffer);
    if (selected==CHANGE_DICE) {
        rb->lcd_puts(1,SELECTIONS_ROW+1,"--");
    } else if (selected==CHANGE_SIDES) {
        rb->lcd_puts(1,SELECTIONS_ROW+1,"   ---");
    }
#endif
#ifdef HAVE_LCD_BITMAP
    rb->lcd_update();
#endif
}

static void print_help() {
#if (CONFIG_KEYPAD == PLAYER_PAD)
    rb->lcd_puts_scroll(1,SELECTIONS_ROW, "</>, +/- setup");
    rb->lcd_puts_scroll(1,SELECTIONS_ROW+1, "on roll, menu exit");
#else
    rb->lcd_puts(1,START_DICE_ROW,"</> pick dice/sides");
    rb->lcd_puts(1,START_DICE_ROW+1,"+/- change");
    rb->lcd_puts(1,START_DICE_ROW+2,"on/select roll");
    rb->lcd_puts(1,START_DICE_ROW+3,"off exit");
#endif
#ifdef HAVE_LCD_BITMAP
    rb->lcd_update();
#endif
}