summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/plugins/SOURCES1
-rw-r--r--apps/plugins/dice.c283
2 files changed, 284 insertions, 0 deletions
diff --git a/apps/plugins/SOURCES b/apps/plugins/SOURCES
index 2043be4696..fe9d6c91f6 100644
--- a/apps/plugins/SOURCES
+++ b/apps/plugins/SOURCES
@@ -20,6 +20,7 @@ vbrfix.c
20viewer.c 20viewer.c
21 21
22#ifndef IRIVER_IFP7XX_SERIES /* Temporarily disable plugins for iFP7xx */ 22#ifndef IRIVER_IFP7XX_SERIES /* Temporarily disable plugins for iFP7xx */
23dice.c
23#ifdef HAVE_LCD_BITMAP /* Not for the Player */ 24#ifdef HAVE_LCD_BITMAP /* Not for the Player */
24 25
25#if CONFIG_LCD != LCD_IPOD2BPP /* Plugins needing the grayscale lib */ 26#if CONFIG_LCD != LCD_IPOD2BPP /* Plugins needing the grayscale lib */
diff --git a/apps/plugins/dice.c b/apps/plugins/dice.c
new file mode 100644
index 0000000000..a343384acb
--- /dev/null
+++ b/apps/plugins/dice.c
@@ -0,0 +1,283 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 by Brandon Low
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20/*****************************************************************************
21Dice by lostlogic
22
23use left and right to pick between sides and number of dice
24use up and down to select number of sides or number of dice
25use select or play to roll the dice
26use stop to exit
27
28*****************************************************************************/
29
30#include "plugin.h"
31#include "button.h"
32#include "lcd.h"
33
34#define SELECTIONS_SIZE 7
35#define SELECTIONS_ROW 4
36#define LINE_LENGTH 26
37#if (CONFIG_KEYPAD == RECORDER_PAD) || (CONFIG_KEYPAD == ONDIO_PAD)
38#define START_DICE_ROW 0
39#define ROWS 3
40#else
41#define START_DICE_ROW 7
42#define ROWS 2
43#endif
44#define MAX_DICE 12
45#define NUM_SIDE_CHOICES 8
46
47/* Values for selected */
48#define CHANGE_DICE 0
49#define CHANGE_SIDES 1
50#define EXIT 2
51
52
53
54#if CONFIG_KEYPAD == RECORDER_PAD
55#define DICE_BUTTON_UP BUTTON_UP
56#define DICE_BUTTON_DOWN BUTTON_DOWN
57#define DICE_BUTTON_LEFT BUTTON_LEFT
58#define DICE_BUTTON_RIGHT BUTTON_RIGHT
59#define DICE_BUTTON_OFF BUTTON_OFF
60#define DICE_BUTTON_ON BUTTON_ON
61#define DICE_BUTTON_SELECT BUTTON_PLAY
62
63#elif CONFIG_KEYPAD == ONDIO_PAD
64#define DICE_BUTTON_UP BUTTON_UP
65#define DICE_BUTTON_DOWN BUTTON_DOWN
66#define DICE_BUTTON_LEFT BUTTON_LEFT
67#define DICE_BUTTON_RIGHT BUTTON_RIGHT
68#define DICE_BUTTON_OFF BUTTON_OFF
69#define DICE_BUTTON_SELECT (BUTTON_MENU|BUTTON_REL)
70#define DICE_BUTTON_ON (BUTTON_MENU|BUTTON_OFF)
71
72#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
73#define DICE_BUTTON_UP BUTTON_UP
74#define DICE_BUTTON_DOWN BUTTON_DOWN
75#define DICE_BUTTON_OFF BUTTON_OFF
76#define DICE_BUTTON_ON BUTTON_ON
77#define DICE_BUTTON_SELECT BUTTON_SELECT
78#define DICE_BUTTON_LEFT BUTTON_LEFT
79#define DICE_BUTTON_RIGHT BUTTON_RIGHT
80
81#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD)
82#define DICE_BUTTON_UP BUTTON_SCROLL_FWD
83#define DICE_BUTTON_DOWN BUTTON_SCROLL_BACK
84#define DICE_BUTTON_OFF (BUTTON_PLAY|BUTTON_REPEAT)
85#define DICE_BUTTON_ON (BUTTON_SELECT|BUTTON_PLAY)
86#define DICE_BUTTON_SELECT BUTTON_SELECT
87#define DICE_BUTTON_LEFT BUTTON_LEFT
88#define DICE_BUTTON_RIGHT BUTTON_RIGHT
89
90#elif (CONFIG_KEYPAD == PLAYER_PAD)
91#define DICE_BUTTON_UP BUTTON_PLAY
92#define DICE_BUTTON_DOWN BUTTON_STOP
93#define DICE_BUTTON_LEFT BUTTON_LEFT
94#define DICE_BUTTON_RIGHT BUTTON_RIGHT
95#define DICE_BUTTON_SELECT BUTTON_ON
96#define DICE_BUTTON_ON (BUTTON_ON|BUTTON_REPEAT)
97#define DICE_BUTTON_OFF BUTTON_MENU
98
99#elif (CONFIG_KEYPAD == IAUDIO_X5_PAD)
100
101#define DICE_BUTTON_UP BUTTON_UP
102#define DICE_BUTTON_DOWN BUTTON_DOWN
103#define DICE_BUTTON_LEFT BUTTON_LEFT
104#define DICE_BUTTON_RIGHT BUTTON_RIGHT
105#define DICE_BUTTON_SELECT BUTTON_SELECT
106#define DICE_BUTTON_ON BUTTON_PLAY
107#define DICE_BUTTON_OFF BUTTON_POWER
108
109#else
110 #error DICE: Unsupported keypad
111#endif
112
113
114
115
116PLUGIN_HEADER
117 /* 0, 1, 2, 3, 4, 5, 6, 7 */
118static const int SIDES[] = { 3, 4, 6, 8, 10, 12, 20, 100 };
119
120static struct plugin_api* rb;
121
122static int roll_dice(int dice[], const int num_dice, const int side_index);
123static void print_dice(const int dice[], const int total);
124static void print_selections(const int selected,
125 const int num_dice,
126 const int side_index);
127static void print_help(void);
128
129/* plugin entry point */
130enum plugin_status plugin_start(struct plugin_api* api, void* parameter) {
131 int side_index = 6;
132 int num_dice = 1;
133 int selected = CHANGE_DICE;
134
135 /* plugin init */
136 (void)parameter;
137 rb = api;
138
139 rb->lcd_clear_display();
140 rb->srand(*rb->current_tick);
141 /* end of plugin init */
142
143 print_selections(selected, num_dice, side_index);
144 print_help();
145 while(selected!=EXIT) {
146 switch(rb->button_get(true)) {
147 case DICE_BUTTON_UP:
148 if (selected==CHANGE_DICE) {
149 num_dice%=MAX_DICE;
150 num_dice++;
151 } else if (selected==CHANGE_SIDES) {
152 side_index++;
153 side_index%=NUM_SIDE_CHOICES;
154 }
155 print_selections(selected, num_dice, side_index);
156 break;
157
158 case DICE_BUTTON_DOWN:
159 if (selected==CHANGE_DICE) {
160 num_dice--;
161 if (num_dice==0) {
162 num_dice=MAX_DICE;
163 }
164 } else if (selected==CHANGE_SIDES) {
165 if (side_index==0) {
166 side_index=NUM_SIDE_CHOICES;
167 }
168 side_index--;
169 }
170 print_selections(selected, num_dice, side_index);
171 break;
172
173 case DICE_BUTTON_LEFT:
174 selected = CHANGE_DICE;
175 print_selections(selected, num_dice, side_index);
176 break;
177
178 case DICE_BUTTON_RIGHT:
179 selected = CHANGE_SIDES;
180 print_selections(selected, num_dice, side_index);
181 break;
182
183 case DICE_BUTTON_ON:
184 case DICE_BUTTON_SELECT:
185 {
186 int dice[MAX_DICE];
187 int total = roll_dice(dice, num_dice, side_index);
188 print_dice(dice, total);
189 }
190 break;
191
192 case DICE_BUTTON_OFF:
193 selected = EXIT;
194 break;
195
196 default:
197 break;
198 } /* switch */
199 } /* while */
200
201 return PLUGIN_OK;
202}
203
204/* Clears the dice array, rolls the dice and returns the sum */
205static int roll_dice(int dice[], const int num_dice, const int side_index) {
206 int i;
207 int total = 0;
208 rb->memset((void*)dice,0,MAX_DICE*sizeof(int));
209 for (i=0; i<num_dice; i++) {
210 dice[i] = rb->rand()%SIDES[side_index] + 1;
211 total+=dice[i];
212 }
213 return total;
214}
215
216#define min(x,y) (x<y?x:y)
217#define max(x,y) (x>y?x:y)
218
219/* Prints the dice, and the sum of the dice values */
220static void print_dice(const int dice[], const int total) {
221 const int dice_per_row = MAX_DICE/ROWS + (MAX_DICE%ROWS?1:0);
222 char showdice[LINE_LENGTH];
223 int row;
224 for (row=0; /*break;*/; row++) {
225 const int start = row*dice_per_row;
226 const int end = min(MAX_DICE,start+dice_per_row);
227 char *pointer = showdice;
228 int space = LINE_LENGTH;
229 int i;
230 rb->memset((void*)showdice,0,LINE_LENGTH*sizeof(char));
231 for (i=start; i<end && dice[i]>0; i++) {
232 int count = rb->snprintf(pointer,space," %3d",dice[i]);
233 pointer = &pointer[count];
234 space -= count;
235 }
236 if (i > start) {
237 rb->lcd_puts(0,START_DICE_ROW+row,showdice);
238 } else {
239 row--;
240 break;
241 }
242 if (i < end || end == MAX_DICE) break;
243 }
244 rb->lcd_puts(0,START_DICE_ROW+(++row)," ");
245 if (total > 0) {
246 rb->snprintf(showdice,LINE_LENGTH,"Total: %4d",total);
247 rb->lcd_puts(1,START_DICE_ROW+(++row),showdice);
248 }
249 while (row < ROWS+2) {
250 rb->lcd_puts(0,START_DICE_ROW+(++row)," ");
251 }
252#ifdef HAVE_LCD_BITMAP
253 rb->lcd_update();
254#endif
255}
256
257/* Print the current user input choices */
258static void print_selections(const int selected,
259 const int num_dice,
260 const int side_index) {
261 char buffer[SELECTIONS_SIZE];
262 rb->snprintf(
263 buffer,SELECTIONS_SIZE,"%2dd%3d",num_dice,SIDES[side_index]);
264 rb->lcd_puts(1,SELECTIONS_ROW,buffer);
265 if (selected==CHANGE_DICE) {
266 rb->lcd_puts(1,SELECTIONS_ROW+1,"--");
267 } else if (selected==CHANGE_SIDES) {
268 rb->lcd_puts(1,SELECTIONS_ROW+1," ---");
269 }
270#ifdef HAVE_LCD_BITMAP
271 rb->lcd_update();
272#endif
273}
274
275static void print_help() {
276 rb->lcd_puts(1,START_DICE_ROW,"</> pick dice/sides");
277 rb->lcd_puts(1,START_DICE_ROW+1,"+/- change");
278 rb->lcd_puts(1,START_DICE_ROW+2,"on/select roll");
279 rb->lcd_puts(1,START_DICE_ROW+3,"off exit");
280#ifdef HAVE_LCD_BITMAP
281 rb->lcd_update();
282#endif
283}