Index: apps/plugins/aegdice.c =================================================================== --- apps/plugins/aegdice.c (revision 0) +++ apps/plugins/aegdice.c (revision 0) @@ -0,0 +1,232 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id: dice.c 19146 2008-11-20 11:27:31Z zagor $ + * + * Copyright (C) 2008 by Tom Bogue + * modified from dice.c copyright 2005 by Brandon Low + * + * 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. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include "plugin.h" +#include "lib/pluginlib_actions.h" +#include "lib/configfile.h" + +#define MAX_DICES 10 +#define NUM_SIDES 10 +#define INITIAL_NB_DICES 4 +#define INITIAL_NB_KEPT 2 + +#define DICE_QUIT PLA_QUIT +#define DICE_ROLL PLA_START + + +#define CFG_FILE "aegdice.cfg" + +const struct button_mapping* plugin_contexts[]={generic_actions}; + +struct dices +{ + int values[MAX_DICES]; + int max; + int nb_dices; + int nb_kept; + bool nb_explode; +}; + +#define PRINT_BUFFER_LENGTH MAX_DICES*4 +PLUGIN_HEADER + +static const struct plugin_api* rb; +static struct dices dice; +static int explode_int; + +static struct configdata config[] = +{ + {TYPE_INT, 0, MAX_DICES, &dice.nb_dices, "dice count", NULL, NULL}, + {TYPE_INT, 0, MAX_DICES, &dice.nb_kept, "dice kept", NULL, NULL}, + {TYPE_INT, 0, 1, &explode_int, "explode", NULL, NULL} +}; + +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); + +/* plugin entry point */ +enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) { + (void)parameter; + rb = api; + int i, action; + + dice_init(&dice); + rb->srand(*rb->current_tick); + + configfile_init(rb); + configfile_load(CFG_FILE, config, 3, 0); + if (dice.nb_kept > dice.nb_dices) { + dice.nb_kept=dice.nb_dices; + } + dice.nb_explode=(explode_int!=0); + if(!dice_menu(&dice)) + { + configfile_save(CFG_FILE, config, 3, 0); + return PLUGIN_OK; + } + configfile_save(CFG_FILE, config, 3, 0); + dice_roll(&dice); + FOR_NB_SCREENS(i) + dice_print( &dice, rb->screens[i] ); + while(true) { + action = pluginlib_getaction(rb, TIMEOUT_BLOCK, + plugin_contexts, 1); + switch(action) { + case DICE_ROLL: + dice_roll(&dice); + FOR_NB_SCREENS(i) + dice_print( &dice, rb->screens[i] ); + break; + case DICE_QUIT: + return PLUGIN_OK; + } + } +} + +void dice_init(struct dices* dice){ + dice->nb_dices=INITIAL_NB_DICES; + dice->nb_kept=INITIAL_NB_KEPT; + explode_int=1; +} + +void dice_roll(struct dices* dice) { + int i,j; + int roll; + int value; + int tmp; + for (i=0; inb_dices; i++) { + value=0; + do { + roll=rb->rand()%NUM_SIDES + 1; + value+=roll; + } + while (dice->nb_explode && roll==NUM_SIDES); + dice->values[i] = value; + } + /* now to sort the list */ + for (i=0; inb_dices; i++) { + for (j=i+1; jnb_dices; j++) { + if (dice->values[i]values[j]) { + tmp=dice->values[i]; + dice->values[i]=dice->values[j]; + dice->values[j]=tmp; + } + } + } + + dice->max = 0; + for (i=0; inb_kept; i++) { + dice->max+=dice->values[i]; + } +} + +void dice_print_string_buffer(struct dices* dice, char* buffer, + int start, int end){ + int i, written; + for (i=start; isnprintf(buffer, PRINT_BUFFER_LENGTH, + " %3d", dice->values[i]); + buffer=&(buffer[written]); + } +} + +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); + int display_nb_row=display->getheight()/char_height; + int display_nb_col=display->getwidth()/char_width; + + 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_rownb_dices); + display->puts_scroll(0, current_row, buffer); + current_row++; + }else{ + int start=0; + int end=0; + for(;current_rowdice->nb_dices) + end=dice->nb_dices; + dice_print_string_buffer(dice, buffer, start, end); + display->puts(0, current_row, buffer); + start=end; + } + } + rb->snprintf(buffer, PRINT_BUFFER_LENGTH, "Max: %d", dice->max); + display->puts_scroll(0, current_row, buffer); + display->update(); +} + +bool dice_menu(struct dices * dice) { + int selection; + bool menu_quit = false, result = false; + + MENUITEM_STRINGLIST(menu,"Dice Menu",NULL,"Roll Dice","Number of Dice", + "Number of Kept Dice","Explode","Quit"); + + + while (!menu_quit) { + switch(rb->do_menu(&menu, &selection, NULL, false)){ + case 0: + menu_quit = true; + result = true; + break; + + case 1: + rb->set_int("Number of Dice", "", UNIT_INT, &(dice->nb_dices), + NULL, 1, 1, MAX_DICES, NULL ); + if (dice->nb_kept > dice->nb_dices) { + dice->nb_kept=dice->nb_dices; + } + break; + + case 2: + rb->set_int("Number of Kept Dice", "", UNIT_INT, + &(dice->nb_kept), NULL, 1, 1, dice->nb_dices, NULL); + break; + + case 3: + /*rb->set_int("Explode", "", UNIT_INT, &(dice->nb_explode), + NULL, 1, 0, 1, NULL );*/ + rb->set_bool("Explode",&(dice->nb_explode)); + explode_int=dice->nb_explode ? 1 : 0; + break; + + default: + menu_quit = true; + result = false; + break; + } + } + return result; +} Index: apps/plugins/SOURCES =================================================================== --- apps/plugins/SOURCES (revision 19179) +++ apps/plugins/SOURCES (working copy) @@ -40,6 +40,7 @@ #ifndef IRIVER_IFP7XX_SERIES /* Temporarily disable plugins for iFP7xx */ dice.c +aegdice.c disktidy.c flipit.c Index: apps/plugins/CATEGORIES =================================================================== --- apps/plugins/CATEGORIES (revision 19179) +++ apps/plugins/CATEGORIES (working copy) @@ -16,6 +16,7 @@ cube,demos demystify,demos dice,games +aegdice,games dict,apps disktidy,apps doom,games