From a231754af930a36f9b4452ab52ce0e3d702b40dd Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Tue, 26 Aug 2014 20:11:53 -0400 Subject: Superdom: code cleanup Change-Id: I3285238cfc098546307862f7d2e51e3b8e456a98 Reviewed-on: http://gerrit.rockbox.org/931 Reviewed-by: Michael Giacomelli --- apps/plugins/superdom.c | 1169 ++++++++++++++++++++++++++--------------------- 1 file changed, 644 insertions(+), 525 deletions(-) (limited to 'apps/plugins/superdom.c') diff --git a/apps/plugins/superdom.c b/apps/plugins/superdom.c index b9c18abec4..91747edb80 100644 --- a/apps/plugins/superdom.c +++ b/apps/plugins/superdom.c @@ -18,9 +18,17 @@ * KIND, either express or implied. * ****************************************************************************/ +/* TODO list: + - don't hardcode board size + - improve AI (move, use nukes, etc.) + - allow for configurable AI +*/ + + #include "plugin.h" -#include "lib/playback_control.h" #include "lib/display_text.h" +#include "lib/playback_control.h" +#include "lib/pluginlib_actions.h" #include "pluginbitmaps/superdom_boarditems.h" char buf[255]; @@ -64,195 +72,37 @@ char buf[255]; #define ICON_HEIGHT (BMPHEIGHT_superdom_boarditems/6) #define ICON_WIDTH (BMPWIDTH_superdom_boarditems/2) -#if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \ - (CONFIG_KEYPAD == IPOD_1G2G_PAD) -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_CANCEL BUTTON_MENU -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define IPOD_STYLE - -#elif CONFIG_KEYPAD == IRIVER_H300_PAD || CONFIG_KEYPAD == IRIVER_H100_PAD -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_CANCEL BUTTON_OFF - -#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_CANCEL BUTTON_REC - -#elif CONFIG_KEYPAD == IRIVER_H10_PAD -#define SUPERDOM_OK BUTTON_RIGHT -#define SUPERDOM_UP BUTTON_SCROLL_UP -#define SUPERDOM_DOWN BUTTON_SCROLL_DOWN -#define SUPERDOM_CANCEL BUTTON_LEFT - -#elif CONFIG_KEYPAD == GIGABEAT_PAD -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL BUTTON_POWER - -#elif CONFIG_KEYPAD == SANSA_E200_PAD -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_UP BUTTON_SCROLL_BACK -#define SUPERDOM_DOWN BUTTON_SCROLL_FWD -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL BUTTON_POWER - -#elif CONFIG_KEYPAD == SANSA_FUZE_PAD -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_UP BUTTON_SCROLL_BACK -#define SUPERDOM_DOWN BUTTON_SCROLL_FWD -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL (BUTTON_HOME|BUTTON_REPEAT) - -#elif CONFIG_KEYPAD == GIGABEAT_S_PAD || \ - CONFIG_KEYPAD == SAMSUNG_YPR0_PAD -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL BUTTON_BACK - -#elif CONFIG_KEYPAD == COWON_D2_PAD -#define SUPERDOM_CANCEL BUTTON_POWER - -#elif CONFIG_KEYPAD == CREATIVEZVM_PAD -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL BUTTON_BACK - -#elif CONFIG_KEYPAD == CREATIVE_ZENXFI3_PAD -#define SUPERDOM_OK (BUTTON_PLAY|BUTTON_REL) -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_BACK -#define SUPERDOM_RIGHT BUTTON_MENU -#define SUPERDOM_CANCEL (BUTTON_PLAY|BUTTON_REPEAT) - -#elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD -#define SUPERDOM_OK BUTTON_PLAY -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_PREV -#define SUPERDOM_RIGHT BUTTON_NEXT -#define SUPERDOM_CANCEL BUTTON_LEFT - -#elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD -#define SUPERDOM_OK BUTTON_PLAY -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_PREV -#define SUPERDOM_RIGHT BUTTON_NEXT -#define SUPERDOM_CANCEL BUTTON_LEFT - -#elif (CONFIG_KEYPAD == ONDAVX747_PAD) || (CONFIG_KEYPAD == MROBE500_PAD) -#define SUPERDOM_CANCEL BUTTON_POWER - -#elif (CONFIG_KEYPAD == SAMSUNG_YH820_PAD) || \ - (CONFIG_KEYPAD == SAMSUNG_YH920_PAD) -#define SUPERDOM_OK BUTTON_PLAY -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL BUTTON_REW - -#elif CONFIG_KEYPAD == PBELL_VIBE500_PAD -#define SUPERDOM_OK BUTTON_OK -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_PREV -#define SUPERDOM_RIGHT BUTTON_NEXT -#define SUPERDOM_CANCEL BUTTON_CANCEL - -#elif CONFIG_KEYPAD == MPIO_HD300_PAD -#define SUPERDOM_OK BUTTON_ENTER -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_REW -#define SUPERDOM_RIGHT BUTTON_FF -#define SUPERDOM_CANCEL BUTTON_MENU - -#elif CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL BUTTON_BACK - -#elif CONFIG_KEYPAD == SONY_NWZ_PAD -#define SUPERDOM_OK BUTTON_PLAY -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL BUTTON_BACK - -#elif CONFIG_KEYPAD == CREATIVE_ZEN_PAD -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL BUTTON_BACK - -#elif CONFIG_KEYPAD == SANSA_CONNECT_PAD -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL BUTTON_POWER - -#elif (CONFIG_KEYPAD == HM60X_PAD) || \ - (CONFIG_KEYPAD == HM801_PAD) -#define SUPERDOM_OK BUTTON_SELECT -#define SUPERDOM_UP BUTTON_UP -#define SUPERDOM_DOWN BUTTON_DOWN -#define SUPERDOM_LEFT BUTTON_LEFT -#define SUPERDOM_RIGHT BUTTON_RIGHT -#define SUPERDOM_CANCEL BUTTON_POWER - -#endif - -#ifdef HAVE_TOUCHSCREEN -#ifndef SUPERDOM_OK -#define SUPERDOM_OK BUTTON_CENTER -#endif -#ifndef SUPERDOM_UP -#define SUPERDOM_UP BUTTON_TOPMIDDLE -#endif -#ifndef SUPERDOM_LEFT -#define SUPERDOM_LEFT BUTTON_MIDLEFT -#endif -#ifndef SUPERDOM_RIGHT -#define SUPERDOM_RIGHT BUTTON_MIDRIGHT -#endif -#ifndef SUPERDOM_DOWN -#define SUPERDOM_DOWN BUTTON_BOTTOMMIDDLE -#endif -#ifndef SUPERDOM_CANCEL -#define SUPERDOM_CANCEL BUTTON_TOPLEFT -#endif -#endif +#define SUPERDOM_OK PLA_SELECT +#define SUPERDOM_CANCEL PLA_CANCEL +#define SUPERDOM_RIGHT PLA_RIGHT +#define SUPERDOM_LEFT PLA_LEFT +#define SUPERDOM_UP PLA_UP +#define SUPERDOM_DOWN PLA_DOWN + +#define SUPERDOM_RIGHT_REPEAT PLA_RIGHT_REPEAT +#define SUPERDOM_LEFT_REPEAT PLA_LEFT_REPEAT +#define SUPERDOM_UP_REPEAT PLA_UP_REPEAT +#define SUPERDOM_DOWN_REPEAT PLA_DOWN_REPEAT + +/* Some defines for the prices */ +#define PRICE_MEN 1 +#define PRICE_MOVE 100 +#define PRICE_TANK 300 +#define PRICE_PLANE 600 +#define PRICE_FARM 1150 +#define PRICE_FACTORY 1300 +#define PRICE_NUKE 2000 + +#define STRINGIZE_2(X) #X +#define STRINGIZE(X) STRINGIZE_2(X) + +#define PRICE_MEN_STR STRINGIZE(PRICE_MEN) +#define PRICE_MOVE_STR STRINGIZE(PRICE_MOVE) +#define PRICE_TANK_STR STRINGIZE(PRICE_TANK) +#define PRICE_PLANE_STR STRINGIZE(PRICE_PLANE) +#define PRICE_FARM_STR STRINGIZE(PRICE_FARM) +#define PRICE_FACTORY_STR STRINGIZE(PRICE_FACTORY) +#define PRICE_NUKE_STR STRINGIZE(PRICE_NUKE) enum { RET_VAL_OK, @@ -268,7 +118,7 @@ static void gen_resources(void); static void draw_cursor(void); static void draw_board(void); -struct tile{ +struct tile { signed int colour; /* -1 = Unset */ bool tank; bool plane; @@ -306,18 +156,23 @@ static struct resources compres; enum { GS_PROD, GS_MOVE, GS_WAR }; static int gamestate; -static struct cursor{ +static struct cursor { int x; int y; } cursor; static struct tile board[12][12]; -static void init_board(void) { +static const struct button_mapping *plugin_contexts[] = { pla_main_ctx }; + +static void init_board(void) +{ int i,j; rb->srand(*rb->current_tick); - for(i=0;i<12;i++) { /* Hopefully about 50% each colour */ - for(j=0;j<12;j++) { + for(i=0;i<12;i++) + { /* Hopefully about 50% each colour */ + for(j=0;j<12;j++) + { if((i<1)||(j<1)||(i>10)||(j>10)) board[i][j].colour = -1; /* Unset */ else @@ -331,48 +186,62 @@ static void init_board(void) { } } - while(compres.farms < superdom_settings.compstartfarms) { + while(compres.farms < superdom_settings.compstartfarms) + { i = rb->rand()%10 + 1; j = rb->rand()%10 + 1; - if((board[i][j].colour == COLOUR_DARK) && (board[i][j].farm == false)) { + if((board[i][j].colour == COLOUR_DARK) && (board[i][j].farm == false)) + { board[i][j].farm = true; compres.farms++; } } - while(compres.inds < superdom_settings.compstartinds) { + while(compres.inds < superdom_settings.compstartinds) + { i = rb->rand()%10 + 1; j = rb->rand()%10 + 1; - if((board[i][j].colour == COLOUR_DARK) && (board[i][j].ind == false)) { + if((board[i][j].colour == COLOUR_DARK) && (board[i][j].ind == false)) + { board[i][j].ind = true; compres.inds++; } } - while(humanres.farms < superdom_settings.humanstartfarms) { + while(humanres.farms < superdom_settings.humanstartfarms) + { i = rb->rand()%10 + 1; j = rb->rand()%10 + 1; - if((board[i][j].colour == COLOUR_LIGHT)&&(board[i][j].farm == false)) { + if((board[i][j].colour == COLOUR_LIGHT)&&(board[i][j].farm == false)) + { board[i][j].farm = true; humanres.farms++; } } - while(humanres.inds < superdom_settings.humanstartinds) { + while(humanres.inds < superdom_settings.humanstartinds) + { i = rb->rand()%10 + 1; j = rb->rand()%10 + 1; - if((board[i][j].colour == COLOUR_LIGHT) && (board[i][j].ind == false)) { + if((board[i][j].colour == COLOUR_LIGHT) && (board[i][j].ind == false)) + { board[i][j].ind = true; humanres.inds++; } } } -void draw_board(void) { +void draw_board(void) +{ int i,j; rb->lcd_clear_display(); - for(i=1;i<11;i++) { - for(j=1;j<11;j++) { - if(board[i][j].colour == COLOUR_DARK) { + for(i=1;i<11;i++) + { + for(j=1;j<11;j++) + { + if(board[i][j].colour == COLOUR_DARK) + { rb->lcd_set_foreground(LCD_DARKGRAY); - } else { + } + else + { rb->lcd_set_foreground(LCD_LIGHTGRAY); } rb->lcd_fillrect(MARGIN+(BOX_WIDTH*(i-1)), @@ -381,7 +250,8 @@ void draw_board(void) { #if LCD_DEPTH != 16 rb->lcd_set_drawmode(DRMODE_BG | DRMODE_INVERSEVID); #endif - if(board[i][j].ind) { + if(board[i][j].ind) + { MY_BITMAP_PART(superdom_boarditems, board[i][j].colour?ICON_WIDTH:0, 0, ICON_STRIDE, #if LCD_WIDTH > LCD_HEIGHT @@ -393,21 +263,24 @@ void draw_board(void) { #endif ICON_WIDTH, ICON_HEIGHT); } - if(board[i][j].farm) { + if(board[i][j].farm) + { MY_BITMAP_PART(superdom_boarditems, board[i][j].colour?ICON_WIDTH:0, ICON_HEIGHT, ICON_STRIDE, MARGIN+(BOX_WIDTH*(i-1))+1, MARGIN+(BOX_HEIGHT*(j-1))+1, ICON_WIDTH, ICON_HEIGHT); } - if(board[i][j].tank) { + if(board[i][j].tank) + { MY_BITMAP_PART(superdom_boarditems, board[i][j].colour?ICON_WIDTH:0, ICON_HEIGHT*2, ICON_STRIDE, MARGIN+(BOX_WIDTH*(i-1))+ICON_WIDTH+1, MARGIN+(BOX_HEIGHT*(j-1))+ICON_HEIGHT+1, ICON_WIDTH, ICON_HEIGHT); } - if(board[i][j].men) { + if(board[i][j].men) + { MY_BITMAP_PART(superdom_boarditems, board[i][j].colour?ICON_WIDTH:0, ICON_HEIGHT*3, #if LCD_WIDTH > LCD_HEIGHT @@ -419,7 +292,8 @@ void draw_board(void) { #endif ICON_WIDTH, ICON_HEIGHT); } - if(board[i][j].plane) { + if(board[i][j].plane) + { MY_BITMAP_PART(superdom_boarditems, board[i][j].colour?ICON_WIDTH:0, ICON_HEIGHT*4, #if LCD_WIDTH > LCD_HEIGHT @@ -431,7 +305,8 @@ void draw_board(void) { #endif ICON_WIDTH, ICON_HEIGHT); } - if(board[i][j].nuke) { + if(board[i][j].nuke) + { MY_BITMAP_PART(superdom_boarditems, board[i][j].colour?ICON_WIDTH:0, ICON_HEIGHT*5, #if LCD_WIDTH > LCD_HEIGHT @@ -449,21 +324,28 @@ void draw_board(void) { } } rb->lcd_set_foreground(LCD_BLACK); - for(i=0;i<=10;i++) { /* Draw Horizontal lines */ + /* Draw Horizontal lines */ + for(i=0;i<=10;i++) + { rb->lcd_hline(MARGIN, MARGIN+(BOX_WIDTH*10), MARGIN+(BOX_HEIGHT*i)); } - for(i=0;i<=10;i++) { /* Draw Vertical lines */ + /* Draw Vertical lines */ + for(i=0;i<=10;i++) + { rb->lcd_vline(MARGIN+(BOX_WIDTH*i), MARGIN, MARGIN+(BOX_HEIGHT*10)); } rb->lcd_update(); } -static int calc_strength(int colour, int x, int y) { +static int calc_strength(int colour, int x, int y) +{ int a, b, score=0; - for (a = -1; a < 2; a++) { - for (b = -1; b < 2; b++) { - if ((b == 0 || a == 0) && - (board[x + a][y + b].colour == colour)) { + for (a = -1; a < 2; a++) + { + for (b = -1; b < 2; b++) + { + if(board[x+a][y+b].colour==colour) + { score += 10; if(board[x + a][y + b].tank || board[x + a][y + b].farm) score += 30; @@ -479,15 +361,18 @@ static int calc_strength(int colour, int x, int y) { return score; } -void gen_interest(void) { +void gen_interest(void) +{ /* Interest should be around 10% */ - rb->srand(*rb->current_tick); int interest = 7+rb->rand()%6; humanres.bank = humanres.bank+(interest*humanres.bank/100); + /* Different interest for AI player */ + interest = 7+rb->rand()%6; compres.bank = compres.bank+(interest*compres.bank/100); } -void draw_cursor(void) { +void draw_cursor(void) +{ rb->lcd_set_drawmode(DRMODE_COMPLEMENT); rb->lcd_fillrect(MARGIN+((cursor.x-1)*BOX_WIDTH), MARGIN+((cursor.y-1)*BOX_HEIGHT), BOX_WIDTH+1, BOX_HEIGHT+1); @@ -495,49 +380,69 @@ void draw_cursor(void) { rb->lcd_update(); } -void gen_resources(void) { +void gen_resources(void) +{ int inccash = 0; int incfood = 0; int ratecash = 0; int ratefood = 0; int i; gen_interest(); - rb->srand(*rb->current_tick); /* Generate Human's resources */ - for(i=0;irand()%200); } - for(i=0;irand()%200); } if(humanres.inds) ratecash = inccash/humanres.inds; if(humanres.farms) ratefood = incfood/humanres.farms; - if(ratecash > 450) { - if(ratefood > 350) { + if(ratecash > 450) + { + if(ratefood > 350) + { rb->splash(HZ*2, "Patriotism sweeps the land, all production" " is up this year!"); - } else { + } + else + { rb->splash(HZ*2, "Factories working at maximum efficiency," " cash production up this year!"); } - } else if(ratecash > 350) { - if(ratefood > 350) { + } + else if(ratecash > 350) + { + if(ratefood > 350) + { rb->splash(HZ*2, "Record crop harvest this year!"); - } else if(ratefood > 250) { + } + else if(ratefood > 250) + { rb->splash(HZ*2, "Production continues as normal"); - } else { + } + else + { rb->splash(HZ*2, "Spoilage of crops leads to reduced farm" " output this year"); } - } else { - if(ratefood > 350) { + } + else + { + if(ratefood > 350) + { rb->splash(HZ*2, "Record crop harvest this year!"); - } else if(ratefood > 250) { + } + else if(ratefood > 250) + { rb->splash(HZ*2, "Factory unions introduced. Industrial" " production is down this year."); - } else { + } + else + { rb->splash(HZ*2, "Internet created. All production is down" " due to time wasted."); } @@ -548,17 +453,20 @@ void gen_resources(void) { /* Generate Computer's resources */ inccash = 0; incfood = 0; - for(i=0;irand()%200); } - for(i=0;irand()%200); } compres.cash += inccash; compres.food += incfood; } -static void update_score(void) { +static void update_score(void) +{ int strength; rb->lcd_setfont(FONT_SYSFIXED); rb->lcd_set_drawmode(DRMODE_BG|DRMODE_INVERSEVID); @@ -571,7 +479,8 @@ static void update_score(void) { rb->lcd_setfont(FONT_UI); } -static int settings_menu(void) { +static int settings_menu(void) +{ int selection = 0; MENUITEM_STRINGLIST(menu, "Super Domination Settings", NULL, @@ -579,8 +488,10 @@ static int settings_menu(void) { "Human starting farms", "Human starting factories", "Starting cash", "Starting food", "Moves per turn"); - while(1) { - switch(rb->do_menu(&menu, &selection, NULL, false)) { + while(1) + { + switch(rb->do_menu(&menu, &selection, NULL, false)) + { case 0: rb->set_int("Computer starting farms", "", UNIT_INT, &superdom_settings.compstartfarms, NULL, @@ -627,34 +538,41 @@ static int settings_menu(void) { return RET_VAL_OK; } -static int superdom_help(void) { +static int superdom_help(void) +{ static char* help_text[] = { - "Super", "domination", "is", "a", "turn", "based", "strategy", "game,", + "Super", "Domination", "is", "a", "turn-based", "strategy", "game,", "where", "the", "aim", "is", "to", "overpower", "the", "computer", "player", "by", "taking", "their", "territory.", "", "Each", "year", "you", "are", "allocated", "an", "amount", "of", "cash", "and", "food,", "depending", "on", "how", "many", "farms", "and", "factories", "you", "control.", "", - "Use", "this", "cash", "and", "food", "to", "buy", "and", "feed", "your", + "Use", "this", "cash", "and", "food", "to", "build", "and", "feed", "your", "army.", "Each", "tile", "has", "a", "strength,", "calculated", "by", - "the", "ownership", "of", "adjacent", "tiles,", "and", "the", "type", + "the", "ownership", "of", "surrounding", "tiles,", "and", "the", "type", "and", "number", "of", "troops", "on", "them.", }; - +#ifdef HAVE_LCD_COLOR + rb->lcd_set_foreground(LCD_WHITE); + rb->lcd_set_background(LCD_BLACK); +#endif if (display_text(ARRAYLEN(help_text), help_text, NULL, NULL, true)) return RET_VAL_USB; return RET_VAL_OK; } -static int start_menu(void) { +static int start_menu(void) +{ int selection = 0; MENUITEM_STRINGLIST(menu, "Super Domination Menu", NULL, "Play Super Domination", "Settings", "Help", "Playback Control", "Quit"); - while(1) { - switch(rb->do_menu(&menu, &selection, NULL, false)) { + while(1) + { + switch(rb->do_menu(&menu, &selection, NULL, false)) + { case 0: return RET_VAL_OK; /* start playing */ break; @@ -678,19 +596,22 @@ static int start_menu(void) { return RET_VAL_QUIT_ERR; } -static int save_game(void) { +static int save_game(void) +{ int fd; char savepath[MAX_PATH]; rb->snprintf(savepath, sizeof(savepath), "/Savegame.ssg"); - if(rb->kbd_input(savepath, MAX_PATH)) { + if(rb->kbd_input(savepath, MAX_PATH)) + { DEBUGF("Keyboard input failed\n"); return -1; } fd = rb->open(savepath, O_WRONLY|O_CREAT, 0666); DEBUGF("savepath: %s\n", savepath); - if(fd < 0) { + if(fd < 0) + { DEBUGF("Couldn't create/open file\n"); return -1; } @@ -729,12 +650,14 @@ static int save_game(void) { return 0; } -static int ingame_menu(void) { +static int ingame_menu(void) +{ MENUITEM_STRINGLIST(menu, "Super Domination Menu", NULL, "Return to game", "Save Game", "Playback Control", "Quit"); - switch(rb->do_menu(&menu, NULL, NULL, false)) { + switch(rb->do_menu(&menu, NULL, NULL, false)) + { case 0: return RET_VAL_OK; break; @@ -761,37 +684,42 @@ static int ingame_menu(void) { return RET_VAL_OK; } -static int get_number(char* param, int* value, int max) { +static int get_number(char* param, int* value, int max) +{ static const char *button_labels[4][3] = { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" }, - { "CLR", "0", "OK" } + { "DEL", "0", "OK" } }; int i,j,x=0,y=0; int height, width; int button = 0, ret = RET_VAL_OK; bool done = false; rb->lcd_clear_display(); - rb->lcd_getstringsize("CLR", &width, &height); + rb->lcd_getstringsize("DEL", &width, &height); if(width > NUM_BOX_WIDTH || height > NUM_BOX_HEIGHT) rb->lcd_setfont(FONT_SYSFIXED); /* Draw a 3x4 grid */ - for(i=0;i<=3;i++) { /* Vertical lines */ + for(i=0;i<=3;i++) + { /* Vertical lines */ rb->lcd_vline(NUM_MARGIN_X+(NUM_BOX_WIDTH*i), NUM_MARGIN_Y, NUM_MARGIN_Y+(4*NUM_BOX_HEIGHT)); } - for(i=0;i<=4;i++) { /* Horizontal lines */ + /* Horizontal lines */ + for(i=0;i<=4;i++) + { rb->lcd_hline(NUM_MARGIN_X, NUM_MARGIN_X+(3*NUM_BOX_WIDTH), NUM_MARGIN_Y+(NUM_BOX_HEIGHT*i)); } - for(i=0;i<4;i++) { - for(j=0;j<3;j++) { + for(i=0;i<4;i++) + { + for(j=0;j<3;j++) + { rb->lcd_getstringsize(button_labels[i][j], &width, &height); - rb->lcd_putsxy( - NUM_MARGIN_X+(j*NUM_BOX_WIDTH)+NUM_BOX_WIDTH/2-width/2, - NUM_MARGIN_Y+(i*NUM_BOX_HEIGHT)+NUM_BOX_HEIGHT/2-height/2, - button_labels[i][j]); + rb->lcd_putsxy(NUM_MARGIN_X+(j*NUM_BOX_WIDTH)+NUM_BOX_WIDTH/2-width/2, + NUM_MARGIN_Y+(i*NUM_BOX_HEIGHT)+NUM_BOX_HEIGHT/2-height/2, + button_labels[i][j]); } } rb->lcd_putsxyf(NUM_MARGIN_X+10, NUM_MARGIN_Y+4*NUM_BOX_HEIGHT+10,"%d",*value); @@ -800,29 +728,40 @@ static int get_number(char* param, int* value, int max) { rb->lcd_putsxy((LCD_WIDTH-width)/2, (NUM_MARGIN_Y-height)/2, param); else rb->lcd_puts_scroll(0, (NUM_MARGIN_Y/height-1)/2, param); + rb->lcd_set_drawmode(DRMODE_COMPLEMENT); rb->lcd_fillrect(NUM_MARGIN_X+(NUM_BOX_WIDTH*x), NUM_MARGIN_Y+(NUM_BOX_HEIGHT*y), NUM_BOX_WIDTH+1, NUM_BOX_HEIGHT+1); rb->lcd_set_drawmode(DRMODE_SOLID); rb->lcd_update(); - while(!done) { - button = rb->button_get(true); + + while(!done) + { + button=pluginlib_getaction(-1, plugin_contexts, ARRAYLEN(plugin_contexts)); rb->lcd_set_drawmode(DRMODE_COMPLEMENT); rb->lcd_fillrect(NUM_MARGIN_X+(NUM_BOX_WIDTH*x), NUM_MARGIN_Y+(NUM_BOX_HEIGHT*y), NUM_BOX_WIDTH+1, NUM_BOX_HEIGHT+1); rb->lcd_set_drawmode(DRMODE_SOLID); - switch(button) { + switch(button) + { case SUPERDOM_OK: - if(y!=3) { + if(y!=3) + { *value *= 10; *value += button_labels[y][x][0] - '0'; - } else if(x==0) { + } + else if(x==0) + { *value /= 10; - } else if(x==1) { + } + else if(x==1) + { *value *= 10; - } else if(x==2) { + } + else if(x==2) + { done = true; break; } @@ -842,35 +781,29 @@ static int get_number(char* param, int* value, int max) { break; #if CONFIG_KEYPAD != IRIVER_H10_PAD case SUPERDOM_LEFT: - if(x==0) { -#ifdef IPOD_STYLE - if(y>0) - y--; - else - y=3; -#endif + if(x==0) + { x=2; - } else { + } + else + { x--; } break; case SUPERDOM_RIGHT: - if(x==2) { -#ifdef IPOD_STYLE - if(y==3) - y=0; - else - y++; -#endif + if(x==2) + { x=0; - } else { + } + else + { x++; } break; #endif -#ifndef IPOD_STYLE case SUPERDOM_UP: - if(y==0) { + if(y==0) + { #if CONFIG_KEYPAD == IRIVER_H10_PAD if(x > 0) x--; @@ -878,26 +811,30 @@ static int get_number(char* param, int* value, int max) { x=2; #endif y=3; - } else { + } + else + { y--; } break; case SUPERDOM_DOWN: - if(y==3) { + if(y==3) + { #if CONFIG_KEYPAD == IRIVER_H10_PAD if(x < 2) x++; else x=0; -#endif +#endif /* CONFIG_KEYPAD == IRIVER_H10_PAD */ y=0; - } else { + } + else + { y++; } break; -#endif default: - if (rb->default_event_handler(button) == SYS_USB_CONNECTED) + if(rb->default_event_handler(button) == SYS_USB_CONNECTED) { done = true; ret = RET_VAL_USB; @@ -918,8 +855,10 @@ static int get_number(char* param, int* value, int max) { return ret; } -static bool tile_has_item(int type, int x, int y) { - switch(type) { +static bool tile_has_item(int type, int x, int y) +{ + switch(type) + { case 0: return (board[x][y].men > 0); break; @@ -942,7 +881,8 @@ static bool tile_has_item(int type, int x, int y) { return false; } -static int buy_resources(int colour, int type, int x, int y, int nummen) { +static int buy_resources(int colour, int type, int x, int y, int nummen) +{ const char *itemnames[][6] = { { "them", @@ -951,14 +891,16 @@ static int buy_resources(int colour, int type, int x, int y, int nummen) { "the farm", "the industrial plant", "the nuke", - }, { + }, + { "place men", "place a tank", "place a plane", "build a farm", "build an industrial plant", "place a nuke", - }, { + }, + { NULL, "a tank", "a plane", @@ -973,54 +915,63 @@ static int buy_resources(int colour, int type, int x, int y, int nummen) { int temp; struct resources *res; - if(human) { + if(human) + { res = &humanres; - } else { + } + else + { res = &compres; } - switch(type) { + switch(type) + { case 0: /* men */ - price = 1*nummen; + price = PRICE_MEN*nummen; break; case 1: /* tank */ - price = 300; + price = PRICE_TANK; break; case 2: /* plane */ - price = 600; + price = PRICE_PLANE; break; case 3: /* Farm */ - price = 1150; + price = PRICE_FARM; break; case 4: /* Factory */ - price = 1300; + price = PRICE_FACTORY; break; case 5: /* nuke */ - price = 2000; + price = PRICE_NUKE; break; } - if(res->cash < price) { + if(res->cash < price) + { if(human) rb->splash(HZ, "Not enough money!"); return RET_VAL_QUIT_ERR; } - if(human) { + if(human) + { rb->splashf(HZ, "Where do you want to place %s?", itemnames[0][type]); if((temp = select_square()) != RET_VAL_OK) return temp; x = cursor.x; y = cursor.y; } - if(board[x][y].colour != colour) { + if(board[x][y].colour != colour) + { if(human) rb->splashf(HZ, "Can't %s on enemy territory", itemnames[1][type]); return RET_VAL_QUIT_ERR; } - if(type != 0 && tile_has_item(type, x, y)) { + if(type != 0 && tile_has_item(type, x, y)) + { if(human) rb->splashf(HZ, "There is already %s there", itemnames[2][type]); return RET_VAL_QUIT_ERR; } - switch(type) { + switch(type) + { case 0: board[x][y].men += nummen; res->men += nummen; @@ -1054,13 +1005,14 @@ static int buy_resources(int colour, int type, int x, int y, int nummen) { return RET_VAL_OK; } -static int buy_resources_menu(void) { +static int buy_resources_menu(void) +{ int selection = 0,nummen; MENUITEM_STRINGLIST(menu, "Buy Resources", NULL, - "Buy men ($1)", "Buy tank ($300)", "Buy plane ($600)", - "Buy Farm ($1150)", "Buy Factory ($1300)", - "Buy Nuke ($2000)", + "Buy men ($" PRICE_MEN_STR ")", "Buy tank ($" PRICE_TANK_STR ")", "Buy plane ($" PRICE_PLANE_STR ")", + "Buy Farm ($" PRICE_FARM_STR ")", "Buy Factory ($" PRICE_FACTORY_STR ")", + "Buy Nuke ($" PRICE_NUKE_STR ")", "Finish buying"); while(1) { @@ -1078,8 +1030,7 @@ static int buy_resources_menu(void) { case 3: case 4: case 5: - if(buy_resources(COLOUR_LIGHT, selection, 0, 0, nummen) - == RET_VAL_USB) + if(buy_resources(COLOUR_LIGHT, selection, 0, 0, nummen)== RET_VAL_USB) return RET_VAL_USB; break; case 6: @@ -1097,17 +1048,20 @@ static int buy_resources_menu(void) { } static int move_unit(int colour, int type, int fromx, int fromy, - int tox, int toy, int nummen) { + int tox, int toy, int nummen) +{ const char *itemnames[][3] = { { "troops", "the tank", "the plane", - }, { + }, + { "any troops", "a tank", "a plane", - }, { + }, + { "the troops", "the tank", "the plane", @@ -1116,7 +1070,8 @@ static int move_unit(int colour, int type, int fromx, int fromy, bool human = (colour == COLOUR_LIGHT); int temp; - if(human) { + if(human) + { rb->splashf(HZ, "Select where you want to move %s from", itemnames[0][type]); if((temp = select_square()) != RET_VAL_OK) @@ -1124,30 +1079,36 @@ static int move_unit(int colour, int type, int fromx, int fromy, fromx = cursor.x; fromy = cursor.y; } - if(board[fromx][fromy].colour != colour) { + if(board[fromx][fromy].colour != colour) + { if(human) rb->splash(HZ, "That isn't your territory"); return RET_VAL_QUIT_ERR; } - if(!tile_has_item(type, fromx, fromy)) { + if(!tile_has_item(type, fromx, fromy)) + { if(human) rb->splashf(HZ, "You don't have %s there", itemnames[1][type]); return RET_VAL_QUIT_ERR; } - if(type == 0) { - if(human) { + if(type == 0) + { + if(human) + { nummen = board[fromx][fromy].men; if((temp = get_number("How many men do you want to move?", &nummen, nummen)) != RET_VAL_OK) return temp; } - if(nummen > board[fromx][fromy].men) { + if(nummen > board[fromx][fromy].men) + { if(human) rb->splash(HZ, "You don't have that many troops."); return RET_VAL_QUIT_ERR; } } - if(human) { + if(human) + { rb->splashf(HZ, "Select where you want to move %s to", itemnames[2][type]); if((temp = select_square()) != RET_VAL_OK) @@ -1155,19 +1116,23 @@ static int move_unit(int colour, int type, int fromx, int fromy, tox = cursor.x; toy = cursor.y; } - if((tox == fromx && toy == fromy) || - board[tox][toy].colour != colour || - (type != 2 && (abs(tox - fromx) > 1 || abs(toy - fromy) > 1))) { + if((tox == fromx && toy == fromy) || + board[tox][toy].colour != colour || + (type != 2 && (abs(tox - fromx) > 1 || + abs(toy - fromy) > 1))) + { if(human) rb->splash(HZ, "Invalid move"); return RET_VAL_QUIT_ERR; } - if(type != 0 && tile_has_item(type, tox, toy)) { + if(type != 0 && tile_has_item(type, tox, toy)) + { if(human) rb->splashf(HZ, "There is already %s there", itemnames[1][type]); return RET_VAL_QUIT_ERR; } - switch(type) { + switch(type) + { case 0: board[fromx][fromy].men -= nummen; board[tox][toy].men += nummen; @@ -1184,16 +1149,19 @@ static int move_unit(int colour, int type, int fromx, int fromy, return RET_VAL_OK; } -static int move_unit_menu(void) { +static int move_unit_menu(void) +{ int selection = 0; MENUITEM_STRINGLIST(menu, "Move unit", NULL, "Move men", "Move tank", "Move plane"); - switch(rb->do_menu(&menu, &selection, NULL, false)) { + switch(rb->do_menu(&menu, &selection, NULL, false)) + { case 0: case 1: case 2: - switch(move_unit(COLOUR_LIGHT, selection, 0, 0, 0, 0, 0)) { + switch(move_unit(COLOUR_LIGHT, selection, 0, 0, 0, 0, 0)) + { case RET_VAL_OK: humanres.moves--; break; @@ -1208,38 +1176,48 @@ static int move_unit_menu(void) { return RET_VAL_OK; } -static int launch_nuke(int colour, int nukex, int nukey, int targetx, int targety) { +static int launch_nuke(int colour, int nukex, int nukey, int targetx, int targety) +{ bool human = (colour == COLOUR_LIGHT); int temp; struct resources *res; - if(board[nukex][nukey].colour != colour) { + if(board[nukex][nukey].colour != colour) + { if(human) rb->splash(HZ, "That isn't your territory"); return RET_VAL_QUIT_ERR; } - if(! board[nukex][nukey].nuke) { + if(! board[nukex][nukey].nuke) + { if(human) - rb->splashf(HZ, "You don't have %s there", "a nuke"); + rb->splashf(HZ, "You don't have a nuke there"); return RET_VAL_QUIT_ERR; } - if(human) { + if(human) + { rb->splash(HZ, "Select place to target with nuke"); if((temp = select_square()) != RET_VAL_OK) return temp; targetx = cursor.x; targety = cursor.y; } - if(human) { + if(human) + { humanres.nukes--; - } else { + } + else + { compres.nukes--; } board[nukex][nukey].nuke = false; - if(board[targetx][targety].colour == COLOUR_LIGHT) { + if(board[targetx][targety].colour == COLOUR_LIGHT) + { res = &humanres; - } else { + } + else + { res = &compres; } res->men -= board[targetx][targety].men; @@ -1259,40 +1237,55 @@ static int launch_nuke(int colour, int nukex, int nukey, int targetx, int target return RET_VAL_OK; } -static int movement_menu(void) { +static int movement_menu(void) +{ int selection = 0, temp; MENUITEM_STRINGLIST(menu, "Movement", NULL, - "Move unit", "Buy additional moves ($100)", + "Move unit", "Buy additional moves ($" PRICE_MOVE_STR ")", "Launch nuclear missile", "Check map", "Finish moving", "Game menu"); - while(1) { - switch(rb->do_menu(&menu, &selection, NULL, false)) { + while(1) + { + switch(rb->do_menu(&menu, &selection, NULL, false)) + { case 0: - if(humanres.moves) { + if(humanres.moves) + { if(move_unit_menu()==RET_VAL_USB) return RET_VAL_USB; - } else { + } + else + { rb->splash(HZ, "You have no more moves left." - " You can buy more for $100 each."); + " You can buy more for $" PRICE_MOVE_STR " each."); } break; case 1: - if(humanres.cash > 100) { + if(humanres.cash > PRICE_MOVE) + { humanres.moves++; - humanres.cash -= 100; + humanres.cash -= PRICE_MOVE; rb->snprintf(buf, sizeof(buf), "You now have %d moves", humanres.moves); rb->splash(HZ, buf); } + else + { + rb->splash(HZ, "Not enough money!"); + } break; case 2: - if(humanres.nukes==0) { + if(humanres.nukes==0) + { rb->splash(HZ, "You do not have any nukes to launch"); - } else { + } + else + { rb->splash(HZ, "Select place to launch nuke from"); - switch(select_square()) { + switch(select_square()) + { case RET_VAL_OK: if(launch_nuke(COLOUR_LIGHT, cursor.x, cursor.y, 0, 0) == RET_VAL_USB) @@ -1324,9 +1317,11 @@ static int movement_menu(void) { } static const char* inventory_data(int selected_item, void * data, - char * buffer, size_t buffer_len) { + char * buffer, size_t buffer_len) +{ (void)data; - switch(selected_item) { + switch(selected_item) + { case 0: rb->snprintf(buffer,buffer_len,"Men: %d", humanres.men); break; @@ -1360,19 +1355,24 @@ static const char* inventory_data(int selected_item, void * data, return buffer; } -static int show_inventory(void) { +static int show_inventory(void) +{ struct simplelist_info info; rb->simplelist_info_init(&info, "Inventory", 9, NULL); info.hide_selection = true; info.get_name = inventory_data; - if(rb->simplelist_show_list(&info)) { + if(rb->simplelist_show_list(&info)) + { return RET_VAL_USB; - } else { + } + else + { return RET_VAL_OK; } } -static int production_menu(void) { +static int production_menu(void) +{ int selection = 0, temp; MENUITEM_STRINGLIST(menu, "Production", NULL, @@ -1380,8 +1380,10 @@ static int production_menu(void) { "Invest money", "Withdraw money", "Finish turn", "Game menu"); - while(1) { - switch(rb->do_menu(&menu, &selection, NULL, false)) { + while(1) + { + switch(rb->do_menu(&menu, &selection, NULL, false)) + { case 0: if(buy_resources_menu() == RET_VAL_USB) return RET_VAL_USB; @@ -1399,9 +1401,12 @@ static int production_menu(void) { if(get_number("How much do you want to invest?", &temp, humanres.cash) == RET_VAL_USB) return RET_VAL_USB; - if(temp > humanres.cash) { + if(temp > humanres.cash) + { rb->splash(HZ, "You don't have that much cash to invest"); - } else { + } + else + { humanres.cash -= temp; humanres.bank += temp; } @@ -1411,9 +1416,12 @@ static int production_menu(void) { if(get_number("How much do you want to withdraw?", &temp, humanres.bank) == RET_VAL_USB) return RET_VAL_USB; - if(temp > humanres.bank) { + if(temp > humanres.bank) + { rb->splash(HZ, "You don't have that much cash to withdraw"); - } else { + } + else + { humanres.cash += temp; humanres.bank -= temp; } @@ -1433,7 +1441,8 @@ static int production_menu(void) { return RET_VAL_OK; } -static void init_resources(void) { +static void init_resources(void) +{ humanres.cash = superdom_settings.startcash; humanres.food = superdom_settings.startfood; humanres.tanks = 0; @@ -1456,7 +1465,8 @@ static void init_resources(void) { compres.moves = 0; } -static int select_square(void) { +static int select_square(void) +{ int button = 0; draw_board(); draw_cursor(); @@ -1468,9 +1478,11 @@ static int select_square(void) { rb->lcd_setfont(FONT_UI); #endif rb->lcd_update(); - while(1) { - button = rb->button_get(true); - switch(button) { + while(1) + { + button=pluginlib_getaction(-1, plugin_contexts, ARRAYLEN(plugin_contexts)); + switch(button) + { case SUPERDOM_CANCEL: rb->splash(HZ, "Cancelled"); return RET_VAL_QUIT_ERR; @@ -1478,49 +1490,43 @@ static int select_square(void) { case SUPERDOM_OK: return RET_VAL_OK; break; -#if CONFIG_KEYPAD != IRIVER_H10_PAD case SUPERDOM_LEFT: - case (SUPERDOM_LEFT|BUTTON_REPEAT): + case SUPERDOM_LEFT_REPEAT: draw_cursor(); /* Deselect the current tile */ - if(cursor.x>1) { + if(cursor.x>1) + { cursor.x--; - } else { -#ifdef IPOD_STYLE - if(cursor.y>1) - cursor.y--; - else - cursor.y = 10; -#endif + } + else + { cursor.x = 10; } update_score(); draw_cursor(); break; case SUPERDOM_RIGHT: - case (SUPERDOM_RIGHT|BUTTON_REPEAT): + case SUPERDOM_RIGHT_REPEAT: draw_cursor(); /* Deselect the current tile */ - if(cursor.x<10) { + if(cursor.x<10) + { cursor.x++; - } else { -#ifdef IPOD_STYLE - if(cursor.y<10) - cursor.y++; - else - cursor.y = 1; -#endif + } + else + { cursor.x = 1; } update_score(); draw_cursor(); break; -#endif -#ifndef IPOD_STYLE case SUPERDOM_UP: - case (SUPERDOM_UP|BUTTON_REPEAT): + case SUPERDOM_UP_REPEAT: draw_cursor(); /* Deselect the current tile */ - if(cursor.y>1) { + if(cursor.y>1) + { cursor.y--; - } else { + } + else + { #if CONFIG_KEYPAD == IRIVER_H10_PAD if(cursor.x > 1) cursor.x--; @@ -1533,11 +1539,14 @@ static int select_square(void) { draw_cursor(); break; case SUPERDOM_DOWN: - case (SUPERDOM_DOWN|BUTTON_REPEAT): + case SUPERDOM_DOWN_REPEAT: draw_cursor(); /* Deselect the current tile */ - if(cursor.y<10) { + if(cursor.y<10) + { cursor.y++; - } else { + } + else + { #if CONFIG_KEYPAD == IRIVER_H10_PAD if(cursor.x < 10) cursor.x++; @@ -1549,7 +1558,6 @@ static int select_square(void) { update_score(); draw_cursor(); break; -#endif default: if (rb->default_event_handler(button) == SYS_USB_CONNECTED) { @@ -1559,21 +1567,28 @@ static int select_square(void) { } } -static int killmen(int colour) { +static int killmen(int colour) +{ bool human = (colour == COLOUR_LIGHT); int menkilled,i,j; int percent; - if(human) { + if(human) + { percent = (humanres.food*1000)/humanres.men; humanres.food = 0; - } else { + } + else + { percent = (compres.food*1000)/compres.men; compres.food = 0; } menkilled = 0; - for(i=1;i<11;i++) { - for(j=1;j<11;j++) { - if(board[i][j].colour == colour) { + for(i=1;i<11;i++) + { + for(j=1;j<11;j++) + { + if(board[i][j].colour == colour) + { int nummen = ((board[i][j].men * percent)/1000); menkilled += board[i][j].men - nummen; board[i][j].men = nummen; @@ -1589,27 +1604,33 @@ static int killmen(int colour) { } /* return -1 if error, 1 if attack is succeeded, 0 otherwise */ -static int attack_territory(int colour, int x, int y) { +static int attack_territory(int colour, int x, int y) +{ bool human = (colour == COLOUR_LIGHT); int str_diff; - if(board[x][y].colour == colour) { + if(board[x][y].colour == colour) + { if(human) rb->splash(HZ, "You can't attack your own territory"); return -1; } str_diff = calc_strength(COLOUR_DARK, x, y) - calc_strength(COLOUR_LIGHT, x, y); - if(human) { + if(human) + { str_diff = -str_diff; } - rb->srand(*rb->current_tick); - if(str_diff > 0 || (str_diff == 0 && rb->rand()%2)) { + if(str_diff > 0 || (str_diff == 0 && rb->rand()%2)) + { struct resources *offres, *defres; - if(human) { + if(human) + { offres = &humanres; defres = &compres; - } else { + } + else + { offres = &compres; defres = &humanres; } @@ -1632,7 +1653,9 @@ static int attack_territory(int colour, int x, int y) { else rb->sleep(HZ); return 1; - } else { + } + else + { if(human) rb->splash(HZ, "Your troops were unable to overcome" " the enemy troops"); @@ -1645,20 +1668,23 @@ static int attack_territory(int colour, int x, int y) { return 0; } -static int war_menu(void) { +static int war_menu(void) +{ int selection = 0, temp; MENUITEM_STRINGLIST(menu, "War!", NULL, "Select territory to attack", "Finish turn", "Game menu"); - while(humanres.moves) { - switch(rb->do_menu(&menu, &selection, NULL, false)) { + while(humanres.moves) + { + switch(rb->do_menu(&menu, &selection, NULL, false)) + { case 0: - switch(select_square()) { + switch(select_square()) + { case RET_VAL_OK: - if(attack_territory(COLOUR_LIGHT, cursor.x, cursor.y) - >= 0) + if(attack_territory(COLOUR_LIGHT, cursor.x, cursor.y) >= 0) humanres.moves--; break; case RET_VAL_USB: @@ -1684,63 +1710,76 @@ struct threat { int str_diff; }; -static bool place_adjacent(bool tank, int x, int y) { - int type = (tank? 1: 2); - if(!buy_resources(COLOUR_DARK, type, x, y, 0)) { +static bool place_adjacent(bool tank, int x, int y) +{ + int type = (tank ? 1: 2); + if(!buy_resources(COLOUR_DARK, type, x, y, 0)) + { return true; } - if(!buy_resources(COLOUR_DARK, type, x-1, y, 0)) { + if(!buy_resources(COLOUR_DARK, type, x-1, y, 0)) + { return true; } - if(!buy_resources(COLOUR_DARK, type, x+1, y, 0)) { + if(!buy_resources(COLOUR_DARK, type, x+1, y, 0)) + { return true; } - if(!buy_resources(COLOUR_DARK, type, x, y-1, 0)) { + if(!buy_resources(COLOUR_DARK, type, x, y-1, 0)) + { return true; } - if(!buy_resources(COLOUR_DARK, type, x, y+1, 0)) { + if(!buy_resources(COLOUR_DARK, type, x, y+1, 0)) + { return true; } return false; } -static bool has_adjacent(int x, int y) { - if((board[x][y].colour == COLOUR_LIGHT) && - ((board[x-1][y].colour == COLOUR_DARK) || - (board[x+1][y].colour == COLOUR_DARK) || - (board[x][y+1].colour == COLOUR_DARK) || +static bool has_adjacent(int x, int y) +{ + if ((board[x][y].colour == COLOUR_LIGHT) && + ((board[x-1][y].colour == COLOUR_DARK) || + (board[x+1][y].colour == COLOUR_DARK) || + (board[x][y+1].colour == COLOUR_DARK) || (board[x][y-1].colour == COLOUR_DARK))) return 1; else return 0; } -static void find_adjacent(int x, int y, int* adj_x, int* adj_y) { +static void find_adjacent(int x, int y, int* adj_x, int* adj_y) +{ /* Finds adjacent squares, returning squares without tanks on them * in preference to those with them */ - if(board[x-1][y].colour == COLOUR_DARK) { + if(board[x-1][y].colour == COLOUR_DARK) + { *adj_x = x-1; *adj_y = y; return; } - if(board[x+1][y].colour == COLOUR_DARK) { + if(board[x+1][y].colour == COLOUR_DARK) + { *adj_x = x+1; *adj_y = y; return; } - if(board[x][y-1].colour == COLOUR_DARK) { + if(board[x][y-1].colour == COLOUR_DARK) + { *adj_x = x; *adj_y = y-1; return; } - if(board[x][y+1].colour == COLOUR_DARK) { + if(board[x][y+1].colour == COLOUR_DARK) + { *adj_x = x; *adj_y = y+1; return; } } -static void computer_allocate(void) { +static void computer_allocate(void) +{ /* Firstly, decide whether to go offensive or defensive. * This is primarily decided by the human player posing a threat to either * the computer's farms or factories */ @@ -1758,14 +1797,19 @@ static void computer_allocate(void) { compres.cash += compres.bank; compres.bank = 0; - for(i=1;i<11;i++) { - for(j=1;j<11;j++) { - if(board[i][j].colour == COLOUR_DARK) { + for(i=1;i<11;i++) + { + for(j=1;j<11;j++) + { + if(board[i][j].colour == COLOUR_DARK) + { numterritory++; str_diff = calc_strength(COLOUR_LIGHT,i,j) - calc_strength(COLOUR_DARK,i,j); - if(str_diff > 0 && (board[i][j].ind || board[i][j].farm)) { - if(numthreats < 3) { + if(str_diff > 0 && (board[i][j].ind || board[i][j].farm)) + { + if(numthreats < 3) + { offensive = false; threats[numthreats].x = i; threats[numthreats].y = j; @@ -1777,25 +1821,33 @@ static void computer_allocate(void) { rb->yield(); } } - if(offensive) { + if(offensive) + { /* The AI is going to go straight for the throat here and attack * the player's farms and factories. The amount of cash * the AI has to spend will determine how many targets there are */ - if(compres.cash > 1200) { + if(compres.cash > 1200) + { /* 1200 is a figure I pulled out of nowhere. Adjust as needed */ numtargets = 2; - } else { + } + else + { numtargets = 1; } /* Work out which target(s) to attack. They must have adjacent squares * owned by the computer. If none are found just place troops in * random places around the map until we run out of money */ k = 0; - for(i=1;i<11;i++) { - for(j=1;j<11;j++) { + for(i=1;i<11;i++) + { + for(j=1;j<11;j++) + { if(has_adjacent(i,j) && - (board[i][j].ind || board[i][j].farm)) { - if(kyield(); } } - if(k == 0) { + if(k == 0) + { /* No targets found! Randomly pick squares and if they're owned * by the computer then stick a tank on it. */ - rb->srand(*rb->current_tick); while(compres.cash >= 300 && compres.tanks < numterritory) { i = rb->rand()%10 + 1; j = rb->rand()%10 + 1; @@ -1819,16 +1871,22 @@ static void computer_allocate(void) { } rb->yield(); } - } else { - for(i=0;i 0 && compres.cash > 0) { + while(str_diff + 20 > 0 && compres.cash > 0) + { /* While we still need them keep placing men */ - if(!place_adjacent(true, targets[i].x, targets[i].y)) { + if(!place_adjacent(true, targets[i].x, targets[i].y)) + { find_adjacent(targets[i].x, targets[i].y, &adj.x, &adj.y); men_needed = (str_diff + 20)*1000/133; - if(compres.cash < men_needed) { + if(compres.cash < men_needed) + { men_needed = compres.cash; } buy_resources(COLOUR_DARK, 0, adj.x, adj.y, @@ -1842,7 +1900,9 @@ static void computer_allocate(void) { } } } - } else { + } + else + { /* Work out what to place on each square to defend it. * Tanks are preferential because they do not require food, * but if the budget is tight then we fall back onto troops. @@ -1850,34 +1910,44 @@ static void computer_allocate(void) { * place planes will be deployed. We would like a margin of at least * 20 points to be safe. */ - for(i=0;i compres.cash) { + if((total_str_diff+20)*10 > compres.cash) + { /* Not enough cash to accomodate all threats using tanks alone - * use men as a backup */ - for(i=0;i= compres.cash); - for(i=0;i 0) { - if(!place_adjacent(tank, threats[i].x, threats[i].y)) { + while(str_diff + 20 > 0) + { + if(!place_adjacent(tank, threats[i].x, threats[i].y)) + { /* No room for any more planes or tanks, revert to * men */ find_adjacent(threats[i].x, threats[i].y, &adj.x, &adj.y); men_needed = (str_diff + 20)*1000/133; - if(compres.cash < men_needed) { + if(compres.cash < men_needed) + { men_needed = compres.cash; } buy_resources(COLOUR_DARK, 0, threats[i].x, @@ -1896,29 +1966,34 @@ static void computer_allocate(void) { compres.cash = 0; } -static int find_adj_target(int x, int y, struct cursor* adj) { +static int find_adj_target(int x, int y, struct cursor* adj) +{ /* Find a square next to a computer's farm or factory owned by the player * that is vulnerable. Return 1 on success, 0 otherwise */ if(board[x+1][y].colour == COLOUR_LIGHT && - calc_strength(COLOUR_LIGHT,x+1,y)<=calc_strength(COLOUR_DARK,x+1,y)) { + calc_strength(COLOUR_LIGHT,x+1,y)<=calc_strength(COLOUR_DARK,x+1,y)) + { adj->x = x+1; adj->y = y; return 1; } if(board[x-1][y].colour == COLOUR_LIGHT && - calc_strength(COLOUR_LIGHT,x-1,y)<=calc_strength(COLOUR_DARK,x-1,y)) { + calc_strength(COLOUR_LIGHT,x-1,y)<=calc_strength(COLOUR_DARK,x-1,y)) + { adj->x = x-1; adj->y = y; return 1; } if(board[x][y+1].colour == COLOUR_LIGHT && - calc_strength(COLOUR_LIGHT,x,y+1)<=calc_strength(COLOUR_DARK,x,y+1)) { + calc_strength(COLOUR_LIGHT,x,y+1)<=calc_strength(COLOUR_DARK,x,y+1)) + { adj->x = x; adj->y = y+1; return 1; } if(board[x][y-1].colour == COLOUR_LIGHT && - calc_strength(COLOUR_LIGHT,x,y-1)<=calc_strength(COLOUR_DARK,x,y-1)) { + calc_strength(COLOUR_LIGHT,x,y-1)<=calc_strength(COLOUR_DARK,x,y-1)) + { adj->x = x; adj->y = y-1; return 1; @@ -1926,21 +2001,27 @@ static int find_adj_target(int x, int y, struct cursor* adj) { return 0; } -static void computer_war(void) { +static void computer_war(void) +{ /* Work out where to attack - prioritise the defence of buildings */ int i, j; bool found_target = true; struct cursor adj; - while(found_target) { + while(found_target) + { found_target = false; - for(i=1;i<11;i++) { - for(j=1;j<11;j++) { - if((board[i][j].colour == COLOUR_DARK) && + for(i=1;i<11;i++) + { + for(j=1;j<11;j++) + { + if((board[i][j].colour == COLOUR_DARK) && (board[i][j].farm || board[i][j].ind) && - find_adj_target(i, j, &adj)) { + find_adj_target(i, j, &adj)) + { found_target = true; - if(attack_territory(COLOUR_DARK, adj.x, adj.y) >= 0) { + if(attack_territory(COLOUR_DARK, adj.x, adj.y) >= 0) + { compres.moves--; if(!compres.moves) return; @@ -1952,16 +2033,20 @@ static void computer_war(void) { } /* Defence stage done, move on to OFFENCE */ found_target = true; - while(found_target) { + while(found_target) + { found_target = false; - for(i=1;i<11;i++) { - for(j=1;j<11;j++) { - if(board[i][j].colour == COLOUR_LIGHT && + for(i=1;i<11;i++) + { + for(j=1;j<11;j++) + { + if(board[i][j].colour == COLOUR_LIGHT && (board[i][j].ind || board[i][j].farm) && - (calc_strength(COLOUR_DARK, i, j) >= - calc_strength(COLOUR_LIGHT, i, j))) { + (calc_strength(COLOUR_DARK, i, j) >= calc_strength(COLOUR_LIGHT, i, j))) + { found_target = true; - if(attack_territory(COLOUR_DARK, i, j) >= 0) { + if(attack_territory(COLOUR_DARK, i, j) >= 0) + { compres.moves--; if(!compres.moves) return; @@ -1973,15 +2058,20 @@ static void computer_war(void) { } /* Spend leftover moves wherever attacking randomly */ found_target = true; - while(found_target) { + while(found_target) + { found_target = false; - for(i=1;i<11;i++) { - for(j=1;j<11;j++) { + for(i=1;i<11;i++) + { + for(j=1;j<11;j++) + { if(board[i][j].colour == COLOUR_LIGHT && (calc_strength(COLOUR_DARK, i, j) >= - calc_strength(COLOUR_LIGHT, i, j))) { + calc_strength(COLOUR_LIGHT, i, j))) + { found_target = true; - if(attack_territory(COLOUR_DARK, i, j) >= 0) { + if(attack_territory(COLOUR_DARK, i, j) >= 0) + { compres.moves--; if(!compres.moves) return; @@ -1993,16 +2083,19 @@ static void computer_war(void) { } } -static int load_game(const char* file) { +static int load_game(const char* file) +{ int fd; fd = rb->open(file, O_RDONLY); - if(fd < 0) { + if(fd < 0) + { DEBUGF("Couldn't open savegame\n"); return -1; } rb->read(fd, buf, 5); - if(rb->strcmp(buf, "SSGv3")) { + if(rb->strcmp(buf, "SSGv3")) + { rb->splash(HZ, "Invalid/incompatible savegame"); return -1; } @@ -2039,7 +2132,8 @@ static int load_game(const char* file) { return 0; } -static void default_settings(void) { +static void default_settings(void) +{ superdom_settings.compstartfarms = 1; superdom_settings.compstartinds = 1; superdom_settings.humanstartfarms = 2; @@ -2049,14 +2143,18 @@ static void default_settings(void) { superdom_settings.movesperturn = 2; } -static int average_strength(int colour) { +static int average_strength(int colour) +{ /* This function calculates the average strength of the given player, * used to determine when the computer wins or loses. */ int i,j; int totalpower = 0; - for(i=1;i<11;i++) { - for(j=1;j<11;j++) { - if(board[i][j].colour != -1) { + for(i=1;i<11;i++) + { + for(j=1;j<11;j++) + { + if(board[i][j].colour != -1) + { totalpower += calc_strength(colour, i, j); } } @@ -2066,6 +2164,7 @@ static int average_strength(int colour) { enum plugin_status plugin_start(const void* parameter) { + rb->srand(*rb->current_tick); #if LCD_DEPTH > 1 rb->lcd_set_backdrop(NULL); rb->lcd_set_foreground(LCD_BLACK); @@ -2075,11 +2174,16 @@ enum plugin_status plugin_start(const void* parameter) cursor.x = 1; cursor.y = 1; default_settings(); - if(parameter) { - if(load_game(parameter) != 0) { + if(parameter) + { + if(load_game(parameter) != 0) + { DEBUGF("Loading failed, generating new game\n"); - } else { - switch(gamestate) { + } + else + { + switch(gamestate) + { case GS_PROD: goto startprod; break; @@ -2096,7 +2200,8 @@ enum plugin_status plugin_start(const void* parameter) } } - switch(start_menu()) { + switch(start_menu()) + { case RET_VAL_OK: /* start playing */ break; case RET_VAL_QUIT_ERR: /* quit */ @@ -2111,14 +2216,17 @@ enum plugin_status plugin_start(const void* parameter) init_board(); gen_resources(); startyear: - while(1) { + while(1) + { int avg_str_diff = (average_strength(COLOUR_LIGHT) - average_strength(COLOUR_DARK)); - if(avg_str_diff > 15) { + if(avg_str_diff > 15) + { rb->splash(HZ*4, "The computer has surrendered. You win."); return PLUGIN_OK; } - if(-avg_str_diff > 15) { + if(-avg_str_diff > 15) + { rb->splash(HZ*4, "Your army have suffered terrible morale from" " the bleak prospects of winning. You lose."); return PLUGIN_OK; @@ -2127,7 +2235,8 @@ startyear: /* production */ startprod: gamestate = GS_PROD; - switch(production_menu()) { + switch(production_menu()) + { case RET_VAL_USB: return PLUGIN_USB_CONNECTED; break; @@ -2141,7 +2250,8 @@ startyear: humanres.moves = superdom_settings.movesperturn; startmove: gamestate = GS_MOVE; - switch(movement_menu()) { + switch(movement_menu()) + { case RET_VAL_USB: return PLUGIN_USB_CONNECTED; break; @@ -2150,22 +2260,30 @@ startyear: break; } /* feed men */ - if(humanres.men) { - if(humanres.food > humanres.men) { + if(humanres.men) + { + if(humanres.food > humanres.men) + { rb->snprintf(buf, sizeof(buf), "Your men ate %d units of food", humanres.men); humanres.food -= humanres.men; - } else { + } + else + { rb->snprintf(buf, sizeof(buf), "There was not enough food" " to feed all your men, %d men have died of starvation", killmen(COLOUR_LIGHT)); } rb->splash(HZ*2, buf); } - if(compres.men) { - if(compres.food > compres.men) { + if(compres.men) + { + if(compres.food > compres.men) + { compres.food -= compres.men; - } else { + } + else + { rb->snprintf(buf, sizeof(buf), "The computer does not have" " enough food to feed its men. %d have died of starvation", killmen(COLOUR_DARK)); @@ -2176,7 +2294,8 @@ startyear: humanres.moves = superdom_settings.movesperturn; startwar: gamestate = GS_WAR; - switch(war_menu()) { + switch(war_menu()) + { case RET_VAL_USB: return PLUGIN_USB_CONNECTED; break; -- cgit v1.2.3