2006-03-05 22:42:50 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Brandon Low
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2006-03-05 22:42:50 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "plugin.h"
|
2008-11-20 11:27:31 +00:00
|
|
|
#include "lib/pluginlib_actions.h"
|
|
|
|
#include "lib/configfile.h"
|
2006-03-05 22:42:50 +00:00
|
|
|
|
2007-07-20 02:47:37 +00:00
|
|
|
#define MAX_DICES 12
|
|
|
|
#define INITIAL_NB_DICES 1
|
2007-07-25 13:08:12 +00:00
|
|
|
#define INITIAL_NB_SIDES 2 /* corresponds to 6 sides in the array */
|
2006-03-05 22:42:50 +00:00
|
|
|
|
2007-07-20 02:47:37 +00:00
|
|
|
#define DICE_QUIT PLA_QUIT
|
|
|
|
#define DICE_ROLL PLA_START
|
2006-03-05 22:42:50 +00:00
|
|
|
|
2007-07-25 13:08:12 +00:00
|
|
|
|
|
|
|
#define CFG_FILE "dice.cfg"
|
|
|
|
|
2007-07-20 02:47:37 +00:00
|
|
|
const struct button_mapping* plugin_contexts[]={generic_actions};
|
2006-03-05 22:42:50 +00:00
|
|
|
|
2007-07-20 02:47:37 +00:00
|
|
|
struct dices
|
|
|
|
{
|
|
|
|
int values[MAX_DICES];
|
|
|
|
int total;
|
|
|
|
int nb_dices;
|
|
|
|
int nb_sides;
|
|
|
|
};
|
2006-03-05 22:42:50 +00:00
|
|
|
|
2007-07-20 02:47:37 +00:00
|
|
|
#define PRINT_BUFFER_LENGTH MAX_DICES*4
|
2006-03-05 22:42:50 +00:00
|
|
|
PLUGIN_HEADER
|
|
|
|
|
2007-07-25 13:08:12 +00:00
|
|
|
static struct dices dice;
|
|
|
|
static int sides_index;
|
|
|
|
|
|
|
|
static struct opt_items nb_sides_option[8] = {
|
|
|
|
{ "3", -1 },
|
|
|
|
{ "4", -1 },
|
|
|
|
{ "6", -1 },
|
|
|
|
{ "8", -1 },
|
|
|
|
{ "10", -1 },
|
|
|
|
{ "12", -1 },
|
|
|
|
{ "20", -1 },
|
|
|
|
{ "100", -1 }
|
|
|
|
};
|
|
|
|
static int nb_sides_values[] = { 3, 4, 6, 8, 10, 12, 20, 100 };
|
|
|
|
static char *sides_conf[] = {"3", "4", "6", "8", "10", "12", "20", "100" };
|
|
|
|
static struct configdata config[] =
|
|
|
|
{
|
2009-01-17 22:53:12 +00:00
|
|
|
{TYPE_INT, 0, MAX_DICES, { .int_p = &dice.nb_dices}, "dice count", NULL},
|
|
|
|
{TYPE_ENUM, 0, 8, { .int_p = &sides_index }, "side count", sides_conf}
|
2007-07-25 13:08:12 +00:00
|
|
|
};
|
2006-03-05 22:42:50 +00:00
|
|
|
|
2007-07-20 02:47:37 +00:00
|
|
|
void dice_init(struct dices* dice);
|
|
|
|
void dice_roll(struct dices* dice);
|
|
|
|
void dice_print(struct dices* dice, struct screen* display);
|
|
|
|
bool dice_menu(struct dices* dice);
|
2006-03-05 22:42:50 +00:00
|
|
|
|
|
|
|
/* plugin entry point */
|
2009-01-16 10:34:40 +00:00
|
|
|
enum plugin_status plugin_start(const void* parameter) {
|
2006-03-05 22:42:50 +00:00
|
|
|
(void)parameter;
|
2007-07-20 02:47:37 +00:00
|
|
|
int i, action;
|
2006-03-05 22:42:50 +00:00
|
|
|
|
2007-07-20 02:47:37 +00:00
|
|
|
dice_init(&dice);
|
2006-03-05 22:42:50 +00:00
|
|
|
rb->srand(*rb->current_tick);
|
2007-07-25 13:08:12 +00:00
|
|
|
|
|
|
|
configfile_load(CFG_FILE, config, 2, 0);
|
|
|
|
dice.nb_sides = nb_sides_values[sides_index];
|
2007-07-20 02:47:37 +00:00
|
|
|
if(!dice_menu(&dice))
|
2007-07-25 13:08:12 +00:00
|
|
|
{
|
|
|
|
configfile_save(CFG_FILE, config, 2, 0);
|
2007-07-20 02:47:37 +00:00
|
|
|
return PLUGIN_OK;
|
2007-07-25 13:08:12 +00:00
|
|
|
}
|
|
|
|
configfile_save(CFG_FILE, config, 2, 0);
|
2007-07-20 02:47:37 +00:00
|
|
|
dice_roll(&dice);
|
|
|
|
FOR_NB_SCREENS(i)
|
|
|
|
dice_print( &dice, rb->screens[i] );
|
|
|
|
while(true) {
|
2009-01-16 10:34:40 +00:00
|
|
|
action = pluginlib_getaction(TIMEOUT_BLOCK,
|
2007-07-20 02:47:37 +00:00
|
|
|
plugin_contexts, 1);
|
|
|
|
switch(action) {
|
|
|
|
case DICE_ROLL:
|
|
|
|
dice_roll(&dice);
|
|
|
|
FOR_NB_SCREENS(i)
|
|
|
|
dice_print( &dice, rb->screens[i] );
|
2006-03-05 22:42:50 +00:00
|
|
|
break;
|
2007-07-20 02:47:37 +00:00
|
|
|
case DICE_QUIT:
|
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-03-05 22:42:50 +00:00
|
|
|
|
2007-07-20 02:47:37 +00:00
|
|
|
void dice_init(struct dices* dice){
|
|
|
|
dice->nb_dices=INITIAL_NB_DICES;
|
2007-07-25 13:08:12 +00:00
|
|
|
sides_index=INITIAL_NB_SIDES;
|
2008-06-28 20:45:21 +00:00
|
|
|
|
2006-03-05 22:42:50 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 02:47:37 +00:00
|
|
|
void dice_roll(struct dices* dice) {
|
2006-03-05 22:42:50 +00:00
|
|
|
int i;
|
2007-07-20 02:47:37 +00:00
|
|
|
dice->total = 0;
|
|
|
|
for (i=0; i<dice->nb_dices; i++) {
|
|
|
|
dice->values[i] = rb->rand()%dice->nb_sides + 1;
|
|
|
|
dice->total+=dice->values[i];
|
2006-03-05 22:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-20 02:47:37 +00:00
|
|
|
void dice_print_string_buffer(struct dices* dice, char* buffer,
|
|
|
|
int start, int end){
|
|
|
|
int i, written;
|
|
|
|
for (i=start; i<end; i++) {
|
|
|
|
written=rb->snprintf(buffer, PRINT_BUFFER_LENGTH,
|
|
|
|
" %3d", dice->values[i]);
|
|
|
|
buffer=&(buffer[written]);
|
2006-03-05 22:42:50 +00:00
|
|
|
}
|
2007-07-20 02:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dice_print(struct dices* dice, struct screen* display){
|
|
|
|
char buffer[PRINT_BUFFER_LENGTH];
|
|
|
|
/* display characteristics */
|
|
|
|
int char_height, char_width;
|
|
|
|
display->getstringsize("M", &char_width, &char_height);
|
2008-06-28 20:45:21 +00:00
|
|
|
int display_nb_row=display->getheight()/char_height;
|
|
|
|
int display_nb_col=display->getwidth()/char_width;
|
2007-07-20 02:47:37 +00:00
|
|
|
|
|
|
|
int nb_dices_per_line=display_nb_col/4;/* 4 char per dice displayed*/
|
|
|
|
int nb_lines_required=dice->nb_dices/nb_dices_per_line;
|
|
|
|
int current_row=0;
|
|
|
|
if(dice->nb_dices%nb_dices_per_line!=0)
|
|
|
|
nb_lines_required++;
|
|
|
|
display->clear_display();
|
|
|
|
if(display_nb_row<nb_lines_required){
|
|
|
|
/* Put everything on the same scrolling line */
|
|
|
|
dice_print_string_buffer(dice, buffer, 0, dice->nb_dices);
|
|
|
|
display->puts_scroll(0, current_row, buffer);
|
|
|
|
current_row++;
|
|
|
|
}else{
|
|
|
|
int start=0;
|
|
|
|
int end=0;
|
|
|
|
for(;current_row<nb_lines_required;current_row++){
|
|
|
|
end=start+nb_dices_per_line;
|
|
|
|
if(end>dice->nb_dices)
|
|
|
|
end=dice->nb_dices;
|
|
|
|
dice_print_string_buffer(dice, buffer, start, end);
|
|
|
|
display->puts(0, current_row, buffer);
|
|
|
|
start=end;
|
|
|
|
}
|
2006-03-05 22:42:50 +00:00
|
|
|
}
|
2007-10-26 16:44:52 +00:00
|
|
|
rb->snprintf(buffer, PRINT_BUFFER_LENGTH, "Total: %d", dice->total);
|
|
|
|
display->puts_scroll(0, current_row, buffer);
|
2007-07-20 02:47:37 +00:00
|
|
|
display->update();
|
2006-03-05 22:42:50 +00:00
|
|
|
}
|
2007-07-20 02:47:37 +00:00
|
|
|
|
|
|
|
bool dice_menu(struct dices * dice) {
|
2007-03-28 07:33:18 +00:00
|
|
|
int selection;
|
|
|
|
bool menu_quit = false, result = false;
|
2007-07-20 02:47:37 +00:00
|
|
|
|
2007-03-28 07:33:18 +00:00
|
|
|
MENUITEM_STRINGLIST(menu,"Dice Menu",NULL,"Roll Dice","Number of Dice",
|
|
|
|
"Number of Sides","Quit");
|
|
|
|
|
2007-07-20 03:03:29 +00:00
|
|
|
|
2007-03-28 07:33:18 +00:00
|
|
|
while (!menu_quit) {
|
2008-03-26 03:35:24 +00:00
|
|
|
switch(rb->do_menu(&menu, &selection, NULL, false)){
|
2007-03-28 07:33:18 +00:00
|
|
|
case 0:
|
|
|
|
menu_quit = true;
|
|
|
|
result = true;
|
|
|
|
break;
|
2007-07-20 02:47:37 +00:00
|
|
|
|
2007-03-28 07:33:18 +00:00
|
|
|
case 1:
|
2007-07-20 02:47:37 +00:00
|
|
|
rb->set_int("Number of Dice", "", UNIT_INT, &(dice->nb_dices),
|
|
|
|
NULL, 1, 1, MAX_DICES, NULL );
|
2007-03-28 07:33:18 +00:00
|
|
|
break;
|
2007-07-20 02:47:37 +00:00
|
|
|
|
2007-03-28 07:33:18 +00:00
|
|
|
case 2:
|
2007-07-25 13:08:12 +00:00
|
|
|
rb->set_option("Number of Sides", &sides_index, INT,
|
2007-07-20 02:47:37 +00:00
|
|
|
nb_sides_option,
|
|
|
|
sizeof(nb_sides_values)/sizeof(int), NULL);
|
2007-07-25 13:08:12 +00:00
|
|
|
dice->nb_sides=nb_sides_values[sides_index];
|
2007-03-28 07:33:18 +00:00
|
|
|
break;
|
2007-07-20 02:47:37 +00:00
|
|
|
|
2007-03-28 07:33:18 +00:00
|
|
|
default:
|
|
|
|
menu_quit = true;
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
2006-03-05 22:42:50 +00:00
|
|
|
}
|
2007-03-28 07:33:18 +00:00
|
|
|
return result;
|
2006-03-05 22:42:50 +00:00
|
|
|
}
|