Index: apps/plugins/CATEGORIES =================================================================== --- apps/plugins/CATEGORIES (Revision 23242) +++ apps/plugins/CATEGORIES (Arbeitskopie) @@ -14,6 +14,7 @@ chopper,games clix,games clock,apps +codebuster,games credits,viewers cube,demos demystify,demos Index: apps/plugins/SOURCES =================================================================== --- apps/plugins/SOURCES (Revision 23242) +++ apps/plugins/SOURCES (Arbeitskopie) @@ -67,6 +67,7 @@ #ifdef HAVE_LCD_COLOR clix.c ppmviewer.c +codebuster.c #endif /* Plugins needing the grayscale lib on low-depth LCDs */ Index: apps/plugins/codebuster.c =================================================================== --- apps/plugins/codebuster.c (Revision 0) +++ apps/plugins/codebuster.c (Revision 0) @@ -0,0 +1,468 @@ +/*************************************************************************** +* __________ __ ___. +* Open \______ \ ____ ____ | | _\_ |__ _______ ___ +* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +* \/ \/ \/ \/ \/ +* $Id: +* +* Copyright (C) 2009 Clément Pit--Claudel +* +* 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/playback_control.h" +#include "lib/pluginlib_actions.h" + +PLUGIN_HEADER + +/* Limits */ +#define MAX_PIECES_COUNT 5 +#define MAX_COLORS_COUNT 8 +#define MAX_GUESSES_COUNT 10 + +const struct button_mapping *plugin_contexts[] = + {generic_directions, generic_actions}; + +/* Mapping */ +#define EXIT PLA_QUIT +#define VALIDATE PLA_FIRE +#define PREV_PIECE PLA_LEFT +#define PREV_PIECE_REPEAT PLA_LEFT_REPEAT +#define NEXT_PIECE PLA_RIGHT +#define NEXT_PIECE_REPEAT PLA_RIGHT_REPEAT +#define PREV_COLOR PLA_UP +#define PREV_COLOR_REPEAT PLA_UP_REPEAT +#define NEXT_COLOR PLA_DOWN +#define NEXT_COLOR_REPEAT PLA_DOWN_REPEAT + +/* + * Screen structure: + * * (guesses_count) lines of guesses, + * * 1 center line of solution (hidden), + * * 1 line showing available colors. + * + * Status vars: + * * quit: exit the plugin + * * leave: restart the plugin (leave the current game) + * * game_ended: the game has ended + * * found: the combination has been found + * + * Colors used are taken from the Tango project. + * + * Due to integer truncations, 2 vars are used for some objects' dimensions + * (eg. true_guess_w, true_score_w). The actual dimension of these objects is + * stored in the corresponding var. without the "true" prefix. + */ + +struct mm_score { + int correct; + int misplaced; +}; + +struct mm_line { + struct mm_score score; + int pieces[MAX_PIECES_COUNT]; +}; + +const int colors[MAX_COLORS_COUNT] = { + LCD_RGBPACK(252, 233, 79), + LCD_RGBPACK(206, 92, 0), + LCD_RGBPACK(143, 89, 2), + LCD_RGBPACK( 78, 154, 6), + /* LCD_RGBPACK( 32, 74, 135), */ + LCD_RGBPACK( 52, 101, 164), + /* LCD_RGBPACK(114, 159, 207), */ + LCD_RGBPACK(117, 80, 123), + /* LCD_RGBPACK(173, 127, 168), */ + LCD_RGBPACK(164, 0, 0), + LCD_RGBPACK(238, 238, 236), + }; + +/* Flags */ +static bool quit, leave, usb; +static bool found, game_ended; + +/* Settings */ +static int pieces_count = 5; +static int colors_count = 7; +static int guesses_count = 10; +static bool colorblind = false, framing = false; + +/* Display */ +static int fg_color; +static int aluminium = LCD_RGBPACK(136, 138, 133), black = LCD_RGBPACK(0, 0, 0); + +static int game_h; +const int inner_margin = 5; +const int margin = LCD_WIDTH / 20; + +static int line_w, line_h; +static int piece_w, tick_w; +static int true_guess_w, true_score_w, guess_w, score_w; +const float score_guess_w_ratio = 0.25; + +/* Guesses and solution */ +struct mm_line solution, hidden; +struct mm_line guesses[MAX_GUESSES_COUNT]; + +/* Alias for pluginlib_getaction */ +static inline int get_button(void) { + return pluginlib_getaction(TIMEOUT_BLOCK, plugin_contexts, 2); +} + +/* Computes the margin to center an element */ +static inline int get_margin(int width, int full_w) { + return ((full_w - width) / 2); +} + +static inline bool stop_game(void) { + return (quit || leave || found); +} + +static void fill_color_rect(int x, int y, int w, int h, int color) { + int fg = rb->lcd_get_foreground(); + rb->lcd_set_foreground(color); + rb->lcd_fillrect(x, y, w, h); + rb->lcd_set_foreground(fg); +} + +static void overfill_rect(int x, int y, int w, int h) { + rb->lcd_fillrect(x - 2, y - 2, w + 4, h + 4); +} + +static void draw_piece(int x, int y, int w, int h, int color_id, bool emph) { + int color; + + if (color_id >= 0) + color = colors[color_id]; + else if (color_id == -2) /* Hidden piece */ + color = aluminium; + else + color = black; + + if (emph) + overfill_rect(x, y, w, h); + + if (color_id == -1) /* Uninitialised color */ + rb->lcd_drawrect(x, y, w, h); + else + fill_color_rect(x, y, w, h, color); + + if (!emph && framing) + rb->lcd_drawrect(x, y, w, h); + + if (colorblind && color_id >= 0) { + char text[2]; + rb->snprintf(text, 2, "%d", color_id); + + int fw, fh; rb->font_getstringsize(text, &fw, &fh, FONT_UI); + rb->lcd_putsxy(x + get_margin(fw, w), y + get_margin(fh, h), text); + } +} + +/* Compute the score for a given guess (expressed in ticks) */ +static void validate_guess(struct mm_line* guess) { + bool solution_match[pieces_count]; + bool guess_match[pieces_count]; + + guess->score.misplaced = 0; + guess->score.correct = 0; + + int guess_pos; + + /* Initialisation with 0s */ + for (guess_pos = 0; guess_pos < pieces_count; guess_pos++) + solution_match[guess_pos] = guess_match[guess_pos] = false; + + /* 1st step : detect correctly positioned pieces */ + for (guess_pos = 0; guess_pos < pieces_count; guess_pos++) { + if (solution.pieces[guess_pos] == guess->pieces[guess_pos]) { + guess->score.correct += 1; + + guess_match[guess_pos] = solution_match[guess_pos] + = true; + } + } + + /* Second step : detect mispositioned pieces */ + for (guess_pos = 0; guess_pos < pieces_count; guess_pos++) { + if (guess_match[guess_pos]) continue; + + int sol_pos; + for (sol_pos = 0; sol_pos < pieces_count; sol_pos++) { + if (guess_match[guess_pos]) break; + if (solution_match[sol_pos]) continue; + + if (guess->pieces[guess_pos] == solution.pieces[sol_pos]) { + guess->score.misplaced += 1; + + solution_match[sol_pos] = true; + break; + } + } + } +} + +static void draw_guess(int line, struct mm_line* guess, int cur_guess, int cur_piece, bool show_score) { + int cur_y = margin + 2 * line_h * line; + int l_margin = margin + (show_score ? 0 : get_margin(guess_w, line_w)); + + int piece; + for (piece = 0; piece < pieces_count; piece++) { + int cur_x = l_margin + 2 * piece_w * piece; + draw_piece(cur_x, cur_y, piece_w, line_h, guess->pieces[piece], + line == cur_guess && piece == cur_piece); + } +} + +static void draw_score(int line, struct mm_line* guess) { + int cur_y = margin + 2 * line_h * line; + int l_margin = margin + true_guess_w + inner_margin; + + int tick = 0; + for (; tick < guess->score.correct; tick++) { + int cur_x = l_margin + 2 * tick_w * tick; + + fill_color_rect(cur_x, cur_y, tick_w, line_h, LCD_RGBPACK(239, 41, 41)); + } + + for (; tick < guess->score.correct + guess->score.misplaced; tick++) { + int cur_x = l_margin + 2 * tick_w * tick; + + fill_color_rect(cur_x, cur_y, tick_w, line_h, LCD_RGBPACK(211, 215, 207)); + } +} + +static void draw_board(int cur_guess, int cur_piece) { + rb->lcd_clear_display(); + + int line = 0; + for (; line < guesses_count; line++) { + draw_guess(line, &guesses[line], cur_guess, cur_piece, true); + if (line < cur_guess) draw_score(line, &guesses[line]); + } + + int color; + int colors_margin = 2; + int cur_y = margin + 2 * line_h * line; + int color_w = (line_w - colors_margin * (colors_count - 1)) / colors_count; + + for (color = 0; color < colors_count; color++) { + int cur_x = margin + color * (color_w + colors_margin); + draw_piece(cur_x, cur_y, color_w, line_h, color, + color == guesses[cur_guess].pieces[cur_piece]); + } + + line++; + + if(game_ended) + draw_guess(line, &solution, cur_guess, cur_piece, false); + else + draw_guess(line, &hidden, cur_guess, cur_piece, false); + + rb->lcd_update(); +} + +static void init_vars(void) { + quit = leave = usb = found = game_ended = false; + + int guess, piece; + for (guess = 0; guess < guesses_count; guess++) + for (piece = 0; piece < pieces_count; piece++) + guesses[guess].pieces[piece] = -1; + + for (piece = 0; piece < pieces_count; piece++) { + guesses[0].pieces[piece] = 0; + hidden.pieces[piece] = -2; + } +} + +static void init_board(void) { + fg_color = rb->lcd_get_foreground(); rb->lcd_set_foreground(LCD_WHITE); + + line_w = LCD_WIDTH - (2 * margin); + game_h = LCD_HEIGHT - (2 * margin); + + line_h = game_h / (2 * (guesses_count + 2) - 1); + + true_score_w = line_w * score_guess_w_ratio; + true_guess_w = line_w - (true_score_w + inner_margin); + + tick_w = true_score_w / (2 * pieces_count - 1); + piece_w = true_guess_w / (2 * pieces_count - 1); + + /* Readjust (due to integer divisions) */ + score_w = tick_w * (2 * pieces_count - 1); + guess_w = piece_w * (2 * pieces_count - 1); +} + +static void randomize_solution(void) { + int piece_id; + for (piece_id = 0; piece_id < pieces_count; piece_id++) + solution.pieces[piece_id] = rb->rand() % colors_count; +} +static bool resume; + +static int menu_cb(int action, const struct menu_item_ex *this_item) +{ + int i = ((intptr_t)this_item); + if ((action == ACTION_REQUEST_MENUITEM) && ((!resume && (i==0)) || + (resume && ((i==2) || (i==3) || (i==4))))) + return ACTION_EXIT_MENUITEM; + return action; +} + +static void main_menu(void) { + MENUITEM_STRINGLIST(main_menu, "Codebuster Menu", menu_cb, + "Resume Game", "Start New Game", "Number of colors", + "Number of pieces", "Number of guesses", + "Display labels", "Playback Control", "Quit"); + + int cur_item =0; + + bool menu_quit = false; + while(!menu_quit) { + + switch(rb->do_menu(&main_menu, &cur_item, NULL, false)) { + case 0: + resume = true; + menu_quit = true; + break; + + case 1: + leave = true; + menu_quit = true; + break; + + case 2: + rb->set_int("Number of colors", "", UNIT_INT, &colors_count, + NULL, -1, MAX_COLORS_COUNT, 1, NULL); + break; + + case 3: + rb->set_int("Number of pieces", "", UNIT_INT, &pieces_count, + NULL, -1, MAX_PIECES_COUNT, 1, NULL); + break; + + case 4: + rb->set_int("Number of guesses", "", UNIT_INT, &guesses_count, + NULL, -1, MAX_GUESSES_COUNT, 1, NULL); + break; + + case 5: + rb->set_bool("Display labels?", &colorblind); + break; + + case 6: + playback_control(NULL); + break; + + case 7: + quit = menu_quit = true; + break; + + case MENU_ATTACHED_USB: + usb = menu_quit = true; + break; + } + } +} + +enum plugin_status plugin_start(const void* parameter) { + (void)parameter; + + rb->srand(*rb->current_tick); + rb->lcd_set_backdrop(NULL); + + main_menu(); + while (!quit) { + init_vars(); + init_board(); + randomize_solution(); + + draw_board(0, 0); + int button = 0, guess = 0, piece = 0; + for (guess = 0; guess < guesses_count && !stop_game(); guess++) { + while(!stop_game()) { + draw_board(guess, piece); + + if ((button = get_button()) == VALIDATE) break; + + switch (button) { + + case EXIT: + resume = true; + main_menu(); + break; + + case NEXT_PIECE: + case NEXT_PIECE_REPEAT: + piece = (piece + 1) % pieces_count; + break; + + case PREV_PIECE: + case PREV_PIECE_REPEAT: + piece = (piece + pieces_count - 1) % pieces_count; + break; + + + case NEXT_COLOR: + case NEXT_COLOR_REPEAT: + guesses[guess].pieces[piece] = + (guesses[guess].pieces[piece] + 1) + % colors_count; + break; + + case PREV_COLOR: + case PREV_COLOR_REPEAT: + guesses[guess].pieces[piece] = + (guesses[guess].pieces[piece] + colors_count - 1) + % colors_count; + break; + + default: + if (rb->default_event_handler(button) == SYS_USB_CONNECTED) + quit = usb = true; + } + + if (guesses[guess].pieces[piece] == -1) + guesses[guess].pieces[piece] = 0; + } + + if (!quit) { + validate_guess(&guesses[guess]); + + if (guesses[guess].score.correct == pieces_count) + found = true; + + if (guess + 1 < guesses_count && !found) + guesses[guess + 1] = guesses[guess]; + } + } + + game_ended = true; + if (!quit && !leave) { + draw_board(guess, piece); + + if (found) + rb->splash(HZ*2, "Well done :)"); + else + rb->splash(HZ*2, "Wooops :("); + resume = false; + main_menu(); + } + } + + rb->lcd_set_foreground(fg_color); + return (usb) ? PLUGIN_USB_CONNECTED : PLUGIN_OK; +}