Index: apps/plugins/superdom.c =================================================================== --- apps/plugins/superdom.c (revision 20190) +++ apps/plugins/superdom.c (working copy) @@ -1582,6 +1580,63 @@ return menkilled; } +/* return -1 if error, 1 if attack is succeeded, 0 otherwise */ +int attack_territory(int colour, int x, int y) { + bool human = (colour == COLOUR_LIGHT); + int str_diff; + + 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) { + str_diff = -str_diff; + } + rb->srand(*rb->current_tick); + if(str_diff > 0 || (str_diff == 0 && rb->rand()%2)) { + struct resources *offres ,*defres; + if(human) { + offres = &humanres; + defres = &compres; + } else { + offres = &compres; + defres = &humanres; + } + defres->men -= board[x][y].men; + defres->tanks -= board[x][y].tank; + defres->planes -= board[x][y].plane; + defres->nukes -= board[x][y].nuke; + defres->farms -= board[x][y].farm; + defres->inds -= board[x][y].ind; + offres->farms += board[x][y].farm; + offres->inds += board[x][y].ind; + board[x][y].colour = colour; + board[x][y].men = 0; + board[x][y].tank = false; + board[x][y].plane = false; + board[x][y].nuke = false; + draw_board(); + if(human) + rb->sleep(HZ*2); + else + rb->sleep(HZ); + return 1; + } else { + if(human) + rb->splash(HZ, "Your troops were unable to overcome" + " the enemy troops"); + else + rb->splash(HZ*2, "The computer attempted to " + "attack, but the invasion was" + " pushed back"); + return 0; + } + return 0; +} + int war_menu(void) { int selection, tempmenu; @@ -1595,41 +1650,8 @@ case 0: if(select_square() == PLUGIN_USB_CONNECTED) return PLUGIN_USB_CONNECTED; - if(board[cursor.x][cursor.y].colour == COLOUR_DARK) { - if(calc_strength(COLOUR_LIGHT, cursor.x, - cursor.y) > calc_strength(COLOUR_DARK, - cursor.x, cursor.y)) { - board[cursor.x][cursor.y].colour = COLOUR_LIGHT; - board[cursor.x][cursor.y].tank = 0; - board[cursor.x][cursor.y].men = 0; - board[cursor.x][cursor.y].plane = 0; - board[cursor.x][cursor.y].nuke = 0; - draw_board(); - rb->sleep(HZ*2); - humanres.moves--; - } else if(calc_strength(COLOUR_LIGHT, cursor.x, cursor.y)== - calc_strength(COLOUR_DARK, cursor.x, cursor.y)) { - if(rb->rand()%2) { - board[cursor.x][cursor.y].colour = COLOUR_LIGHT; - board[cursor.x][cursor.y].tank = 0; - board[cursor.x][cursor.y].men = 0; - board[cursor.x][cursor.y].plane = 0; - board[cursor.x][cursor.y].nuke = 0; - draw_board(); - rb->sleep(HZ*2); - humanres.moves--; - } else { - rb->splash(HZ, "Your troops were unable to" - " overcome the enemy troops"); - humanres.moves--; - } - } else { - rb->splash(HZ, "Your troops were unable to overcome" - " the enemy troops"); - humanres.moves--; - } - } else { - rb->splash(HZ, "You can't attack your own territory"); + if(attack_territory(COLOUR_LIGHT, cursor.x, cursor.y) >= 0) { + humanres.moves--; } break; case 1: @@ -2057,38 +2075,8 @@ if((board[i][j].colour == COLOUR_DARK) && (board[i][j].farm || board[i][j].ind)) { if(find_adj_target(i, j, &adj) && compres.moves) { - if(calc_strength(COLOUR_LIGHT, adj.x, adj.y) == - calc_strength(COLOUR_DARK, adj.x, adj.y)) { - rb->srand(*rb->current_tick); - if(rb->rand()%2) { - board[adj.x][adj.y].colour = COLOUR_DARK; - board[adj.x][adj.y].tank = false; - board[adj.x][adj.y].plane = false; - board[adj.x][adj.y].nuke = false; - humanres.men -= board[adj.x][adj.y].men; - board[adj.x][adj.y].men = 0; - draw_board(); - rb->sleep(HZ); - compres.moves--; - } else { - rb->splash(HZ*2, "The computer attempted" - " to attack, but the" - " invasion was pushed" - " back"); - compres.moves--; - } - } else { - if(compres.moves) { - board[adj.x][adj.y].colour = COLOUR_DARK; - board[adj.x][adj.y].tank = false; - board[adj.x][adj.y].plane = false; - board[adj.x][adj.y].nuke = false; - humanres.men -= board[adj.x][adj.y].men; - board[adj.x][adj.y].men = 0; - draw_board(); - rb->sleep(HZ); - compres.moves--; - } + if(attack_territory(COLOUR_DARK, adj.x, adj.y) >= 0) { + compres.moves--; } } } @@ -2103,31 +2091,7 @@ (board[i][j].ind || board[i][j].farm) && (calc_strength(COLOUR_DARK, i, j) >= calc_strength(COLOUR_LIGHT, i, j))) { - if(calc_strength(COLOUR_DARK, i, j) == - calc_strength(COLOUR_LIGHT, i, j)) { - if(rb->rand()%2) { - board[i][j].colour = COLOUR_DARK; - board[i][j].tank = false; - board[i][j].plane = false; - board[i][j].nuke = false; - board[i][j].men = 0; - draw_board(); - rb->sleep(HZ); - compres.moves--; - } else { - rb->splash(HZ*2, "The computer attempted to " - "attack, but the invasion was" - " pushed back"); - compres.moves--; - } - } else { - board[i][j].colour = COLOUR_DARK; - board[i][j].tank = false; - board[i][j].plane = false; - board[i][j].nuke = false; - board[i][j].men = 0; - draw_board(); - rb->sleep(HZ); + if(attack_territory(COLOUR_DARK, i, j) >= 0) { compres.moves--; } } @@ -2142,34 +2106,11 @@ if(board[i][j].colour == COLOUR_LIGHT && (calc_strength(COLOUR_DARK, i, j) >= calc_strength(COLOUR_LIGHT, i, j))) { - if(calc_strength(COLOUR_DARK, i, j) == - calc_strength(COLOUR_LIGHT, i, j)) { - if(rb->rand()%2) { - board[i][j].colour = COLOUR_DARK; - board[i][j].tank = false; - board[i][j].plane = false; - board[i][j].nuke = false; - board[i][j].men = 0; - draw_board(); - rb->sleep(HZ); - compres.moves--; - } else { - rb->splash(HZ*2, "The computer attempted to" - " attack, but the invasion was" - " pushed back"); - compres.moves--; - } - } else { - board[i][j].colour = COLOUR_DARK; - board[i][j].tank = false; - board[i][j].plane = false; - board[i][j].nuke = false; - board[i][j].men = 0; - draw_board(); - rb->sleep(HZ); + if(attack_territory(COLOUR_DARK, i, j) >= 0) { compres.moves--; } } + rb->yield(); } } }