1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Brandon Low
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include "lib/pluginlib_actions.h"
24 #include "lib/configfile.h"
25 #include "lib/playback_control.h"
28 #define INITIAL_NB_DICES 1
29 #define INITIAL_NB_SIDES 2 /* corresponds to 6 sides in the array */
31 #define DICE_QUIT PLA_QUIT
32 #define DICE_ROLL PLA_START
35 #define CFG_FILE "dice.cfg"
37 const struct button_mapping
* plugin_contexts
[]={generic_actions
};
41 int values
[MAX_DICES
];
47 #define PRINT_BUFFER_LENGTH MAX_DICES*4
50 static struct dices dice
;
51 static int sides_index
;
53 static struct opt_items nb_sides_option
[8] = {
63 static int nb_sides_values
[] = { 3, 4, 6, 8, 10, 12, 20, 100 };
64 static char *sides_conf
[] = {"3", "4", "6", "8", "10", "12", "20", "100" };
65 static struct configdata config
[] =
67 {TYPE_INT
, 0, MAX_DICES
, { .int_p
= &dice
.nb_dices
}, "dice count", NULL
},
68 {TYPE_ENUM
, 0, 8, { .int_p
= &sides_index
}, "side count", sides_conf
}
71 void dice_init(struct dices
* dice
);
72 void dice_roll(struct dices
* dice
);
73 void dice_print(struct dices
* dice
, struct screen
* display
);
74 bool dice_menu(struct dices
* dice
);
76 /* plugin entry point */
77 enum plugin_status
plugin_start(const void* parameter
) {
82 rb
->srand(*rb
->current_tick
);
84 configfile_load(CFG_FILE
, config
, 2, 0);
85 dice
.nb_sides
= nb_sides_values
[sides_index
];
88 configfile_save(CFG_FILE
, config
, 2, 0);
91 configfile_save(CFG_FILE
, config
, 2, 0);
94 dice_print( &dice
, rb
->screens
[i
] );
96 action
= pluginlib_getaction(TIMEOUT_BLOCK
,
102 dice_print( &dice
, rb
->screens
[i
] );
110 void dice_init(struct dices
* dice
){
111 dice
->nb_dices
=INITIAL_NB_DICES
;
112 sides_index
=INITIAL_NB_SIDES
;
116 void dice_roll(struct dices
* dice
) {
119 for (i
=0; i
<dice
->nb_dices
; i
++) {
120 dice
->values
[i
] = rb
->rand()%dice
->nb_sides
+ 1;
121 dice
->total
+=dice
->values
[i
];
125 void dice_print_string_buffer(struct dices
* dice
, char* buffer
,
128 for (i
=start
; i
<end
; i
++) {
129 written
=rb
->snprintf(buffer
, PRINT_BUFFER_LENGTH
,
130 " %3d", dice
->values
[i
]);
131 buffer
=&(buffer
[written
]);
135 void dice_print(struct dices
* dice
, struct screen
* display
){
136 char buffer
[PRINT_BUFFER_LENGTH
];
137 /* display characteristics */
138 int char_height
, char_width
;
139 display
->getstringsize("M", &char_width
, &char_height
);
140 int display_nb_row
=display
->getheight()/char_height
;
141 int display_nb_col
=display
->getwidth()/char_width
;
143 int nb_dices_per_line
=display_nb_col
/4;/* 4 char per dice displayed*/
144 if(!nb_dices_per_line
)
147 int nb_lines_required
=dice
->nb_dices
/nb_dices_per_line
;
149 if(dice
->nb_dices
%nb_dices_per_line
!=0)
151 display
->clear_display();
152 if(display_nb_row
<nb_lines_required
){
153 /* Put everything on the same scrolling line */
154 dice_print_string_buffer(dice
, buffer
, 0, dice
->nb_dices
);
155 display
->puts_scroll(0, current_row
, buffer
);
160 for(;current_row
<nb_lines_required
;current_row
++){
161 end
=start
+nb_dices_per_line
;
162 if(end
>dice
->nb_dices
)
164 dice_print_string_buffer(dice
, buffer
, start
, end
);
165 display
->puts(0, current_row
, buffer
);
169 rb
->snprintf(buffer
, PRINT_BUFFER_LENGTH
, "Total: %d", dice
->total
);
170 display
->puts_scroll(0, current_row
, buffer
);
174 bool dice_menu(struct dices
* dice
) {
176 bool menu_quit
= false, result
= false;
178 MENUITEM_STRINGLIST(menu
, "Dice Menu", NULL
,
180 "Number of Dice", "Number of Sides",
181 "Playback Control", "Quit");
185 switch(rb
->do_menu(&menu
, &selection
, NULL
, false)){
192 rb
->set_int("Number of Dice", "", UNIT_INT
, &(dice
->nb_dices
),
193 NULL
, 1, 1, MAX_DICES
, NULL
);
197 rb
->set_option("Number of Sides", &sides_index
, INT
,
199 sizeof(nb_sides_values
)/sizeof(int), NULL
);
200 dice
->nb_sides
=nb_sides_values
[sides_index
];
204 playback_control(NULL
);