Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Brandon Low |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 22 | #include "plugin.h" |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 23 | #include "pluginlib_actions.h" |
Jonathan Gordon | 36af39b | 2007-07-25 13:08:12 +0000 | [diff] [blame] | 24 | #include "configfile.h" |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 25 | |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 26 | #define MAX_DICES 12 |
| 27 | #define INITIAL_NB_DICES 1 |
Jonathan Gordon | 36af39b | 2007-07-25 13:08:12 +0000 | [diff] [blame] | 28 | #define INITIAL_NB_SIDES 2 /* corresponds to 6 sides in the array */ |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 29 | |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 30 | #define DICE_QUIT PLA_QUIT |
| 31 | #define DICE_ROLL PLA_START |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 32 | |
Jonathan Gordon | 36af39b | 2007-07-25 13:08:12 +0000 | [diff] [blame] | 33 | |
| 34 | #define CFG_FILE "dice.cfg" |
| 35 | |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 36 | const struct button_mapping* plugin_contexts[]={generic_actions}; |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 37 | |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 38 | struct dices |
| 39 | { |
| 40 | int values[MAX_DICES]; |
| 41 | int total; |
| 42 | int nb_dices; |
| 43 | int nb_sides; |
| 44 | }; |
Dave Chapman | d64e626 | 2007-01-14 13:48:09 +0000 | [diff] [blame] | 45 | |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 46 | #define PRINT_BUFFER_LENGTH MAX_DICES*4 |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 47 | PLUGIN_HEADER |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 48 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 49 | static const struct plugin_api* rb; |
Jonathan Gordon | 36af39b | 2007-07-25 13:08:12 +0000 | [diff] [blame] | 50 | static struct dices dice; |
| 51 | static int sides_index; |
| 52 | |
| 53 | static struct opt_items nb_sides_option[8] = { |
| 54 | { "3", -1 }, |
| 55 | { "4", -1 }, |
| 56 | { "6", -1 }, |
| 57 | { "8", -1 }, |
| 58 | { "10", -1 }, |
| 59 | { "12", -1 }, |
| 60 | { "20", -1 }, |
| 61 | { "100", -1 } |
| 62 | }; |
| 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[] = |
| 66 | { |
| 67 | {TYPE_INT, 0, MAX_DICES, &dice.nb_dices, "dice count", NULL, NULL}, |
| 68 | {TYPE_ENUM, 0, 8, &sides_index, "side count", sides_conf, NULL} |
| 69 | }; |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 70 | |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 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); |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 75 | |
| 76 | /* plugin entry point */ |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 77 | enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) { |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 78 | (void)parameter; |
| 79 | rb = api; |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 80 | int i, action; |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 81 | |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 82 | dice_init(&dice); |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 83 | rb->srand(*rb->current_tick); |
Jonathan Gordon | 36af39b | 2007-07-25 13:08:12 +0000 | [diff] [blame] | 84 | |
| 85 | configfile_init(rb); |
| 86 | configfile_load(CFG_FILE, config, 2, 0); |
| 87 | dice.nb_sides = nb_sides_values[sides_index]; |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 88 | if(!dice_menu(&dice)) |
Jonathan Gordon | 36af39b | 2007-07-25 13:08:12 +0000 | [diff] [blame] | 89 | { |
| 90 | configfile_save(CFG_FILE, config, 2, 0); |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 91 | return PLUGIN_OK; |
Jonathan Gordon | 36af39b | 2007-07-25 13:08:12 +0000 | [diff] [blame] | 92 | } |
| 93 | configfile_save(CFG_FILE, config, 2, 0); |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 94 | dice_roll(&dice); |
| 95 | FOR_NB_SCREENS(i) |
| 96 | dice_print( &dice, rb->screens[i] ); |
| 97 | while(true) { |
| 98 | action = pluginlib_getaction(rb, TIMEOUT_BLOCK, |
| 99 | plugin_contexts, 1); |
| 100 | switch(action) { |
| 101 | case DICE_ROLL: |
| 102 | dice_roll(&dice); |
| 103 | FOR_NB_SCREENS(i) |
| 104 | dice_print( &dice, rb->screens[i] ); |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 105 | break; |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 106 | case DICE_QUIT: |
| 107 | return PLUGIN_OK; |
| 108 | } |
| 109 | } |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 112 | void dice_init(struct dices* dice){ |
| 113 | dice->nb_dices=INITIAL_NB_DICES; |
Jonathan Gordon | 36af39b | 2007-07-25 13:08:12 +0000 | [diff] [blame] | 114 | sides_index=INITIAL_NB_SIDES; |
| 115 | |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void dice_roll(struct dices* dice) { |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 119 | int i; |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 120 | dice->total = 0; |
| 121 | for (i=0; i<dice->nb_dices; i++) { |
| 122 | dice->values[i] = rb->rand()%dice->nb_sides + 1; |
| 123 | dice->total+=dice->values[i]; |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 124 | } |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 127 | void dice_print_string_buffer(struct dices* dice, char* buffer, |
| 128 | int start, int end){ |
| 129 | int i, written; |
| 130 | for (i=start; i<end; i++) { |
| 131 | written=rb->snprintf(buffer, PRINT_BUFFER_LENGTH, |
| 132 | " %3d", dice->values[i]); |
| 133 | buffer=&(buffer[written]); |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 134 | } |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 135 | } |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 136 | |
| 137 | void dice_print(struct dices* dice, struct screen* display){ |
| 138 | char buffer[PRINT_BUFFER_LENGTH]; |
| 139 | /* display characteristics */ |
| 140 | int char_height, char_width; |
| 141 | display->getstringsize("M", &char_width, &char_height); |
| 142 | int display_nb_row=display->height/char_height; |
| 143 | int display_nb_col=display->width/char_width; |
| 144 | |
| 145 | int nb_dices_per_line=display_nb_col/4;/* 4 char per dice displayed*/ |
| 146 | int nb_lines_required=dice->nb_dices/nb_dices_per_line; |
| 147 | int current_row=0; |
| 148 | if(dice->nb_dices%nb_dices_per_line!=0) |
| 149 | nb_lines_required++; |
| 150 | display->clear_display(); |
| 151 | if(display_nb_row<nb_lines_required){ |
| 152 | /* Put everything on the same scrolling line */ |
| 153 | dice_print_string_buffer(dice, buffer, 0, dice->nb_dices); |
| 154 | display->puts_scroll(0, current_row, buffer); |
| 155 | current_row++; |
| 156 | }else{ |
| 157 | int start=0; |
| 158 | int end=0; |
| 159 | for(;current_row<nb_lines_required;current_row++){ |
| 160 | end=start+nb_dices_per_line; |
| 161 | if(end>dice->nb_dices) |
| 162 | end=dice->nb_dices; |
| 163 | dice_print_string_buffer(dice, buffer, start, end); |
| 164 | display->puts(0, current_row, buffer); |
| 165 | start=end; |
| 166 | } |
| 167 | } |
Kevin Ferrare | a63181c | 2007-10-26 16:44:52 +0000 | [diff] [blame] | 168 | rb->snprintf(buffer, PRINT_BUFFER_LENGTH, "Total: %d", dice->total); |
| 169 | display->puts_scroll(0, current_row, buffer); |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 170 | display->update(); |
| 171 | } |
| 172 | |
| 173 | bool dice_menu(struct dices * dice) { |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 174 | int selection; |
| 175 | bool menu_quit = false, result = false; |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 176 | |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 177 | MENUITEM_STRINGLIST(menu,"Dice Menu",NULL,"Roll Dice","Number of Dice", |
| 178 | "Number of Sides","Quit"); |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 179 | |
Kevin Ferrare | 40ea9d0 | 2007-07-20 03:03:29 +0000 | [diff] [blame] | 180 | |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 181 | while (!menu_quit) { |
Jonathan Gordon | 5ca1539 | 2008-03-26 03:35:24 +0000 | [diff] [blame] | 182 | switch(rb->do_menu(&menu, &selection, NULL, false)){ |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 183 | case 0: |
| 184 | menu_quit = true; |
| 185 | result = true; |
| 186 | break; |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 187 | |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 188 | case 1: |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 189 | rb->set_int("Number of Dice", "", UNIT_INT, &(dice->nb_dices), |
| 190 | NULL, 1, 1, MAX_DICES, NULL ); |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 191 | break; |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 192 | |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 193 | case 2: |
Jonathan Gordon | 36af39b | 2007-07-25 13:08:12 +0000 | [diff] [blame] | 194 | rb->set_option("Number of Sides", &sides_index, INT, |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 195 | nb_sides_option, |
| 196 | sizeof(nb_sides_values)/sizeof(int), NULL); |
Jonathan Gordon | 36af39b | 2007-07-25 13:08:12 +0000 | [diff] [blame] | 197 | dice->nb_sides=nb_sides_values[sides_index]; |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 198 | break; |
Kevin Ferrare | 21cc9a6 | 2007-07-20 02:47:37 +0000 | [diff] [blame] | 199 | |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 200 | default: |
| 201 | menu_quit = true; |
| 202 | result = false; |
| 203 | break; |
| 204 | } |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 205 | } |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 206 | return result; |
Brandon Low | b626f49 | 2006-03-05 22:42:50 +0000 | [diff] [blame] | 207 | } |