| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- /*
- * This file is part of RuneOptimizer.
- *
- * RuneOptimizer 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 3 of the License, or (at your option)
- * any later version.
- *
- * RuneOptimizer is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * RuneOptimizer. If not, see <https://www.gnu.org/licenses/>.
- */
- /**
- * @file optimize.c
- * Implementation of the optimize command and some utilities used by it.
- */
- #include "optimize.h"
- #include "optimize_parse_arguments.c"
- #include "optimize_fetch_unit.c"
- #include "optimize_count_runes_for_sets.c"
- #include "optimize_query.c"
- #include "optimize_get_runes.c"
- #include "optimize_print.c"
- void optimize_sort_results(Result results[5000], int total){
- // Bubble sort, by descending rating.
- int i, j;
- Result temp;
- for (i = 0; i < total - 1; i++){
- for (j = 0; j < (total - 1-i); j++){
- if (results[j].rating < results[j + 1].rating){
- temp = results[j];
- results[j] = results[j + 1];
- results[j + 1] = temp;
- }
- }
- }
- }
- unsigned int optimize_calculate_ehp(unsigned int hp, unsigned short def){
- // Lots of magic numbers here!
- // Sorry, but this is the formula, and I don't understand it either.
- // Take it or leave it.
- unsigned int ehp =
- ceil((((((float) def) * 3.5f) + 1140.0f) * ((float) hp)) / 1000.0f);
- return ehp;
- }
- unsigned short optimize_calculate_dmg(
- unsigned short atk, unsigned short crr, unsigned short crd
- ){
- float crr_capped = (float) crr;
- if (crr_capped > 100.0f){
- crr_capped = 100.0f;
- }
- unsigned short dmg = ceil(
- ((float)atk * ((100 - crr_capped) / 100)) + // Non-crit
- ((float)atk * (crr_capped / 100) * ((float)crd / 100)) // Crit
- );
- return(dmg);
- }
- unsigned char optimize_contains_broken(Rune_Set_Count *set_count){
- if (set_count->energy % 2 != 0) return(TRUE);
- if (set_count->guard % 2 != 0) return(TRUE);
- if (set_count->swift % 4 != 0) return(TRUE);
- if (set_count->blade % 2 != 0) return(TRUE);
- if (set_count->rage % 4 != 0) return(TRUE);
- if (set_count->focus % 2 != 0) return(TRUE);
- if (set_count->endure % 2 != 0) return(TRUE);
- if (set_count->fatal % 4 != 0) return(TRUE);
- if (set_count->despair % 4 != 0) return(TRUE);
- if (set_count->vampire % 4 != 0) return(TRUE);
- if (set_count->violent % 4 != 0) return(TRUE);
- if (set_count->nemesis % 2 != 0) return(TRUE);
- if (set_count->will % 2 != 0) return(TRUE);
- if (set_count->shield % 2 != 0) return(TRUE);
- if (set_count->revenge % 2 != 0) return(TRUE);
- if (set_count->destroy % 2 != 0) return(TRUE);
- if (set_count->fight % 2 != 0) return(TRUE);
- if (set_count->determination % 2 != 0) return(TRUE);
- if (set_count->enhance % 2 != 0) return(TRUE);
- if (set_count->accuracy % 2 != 0) return(TRUE);
- if (set_count->tolerance % 2 != 0) return(TRUE);
- return(FALSE);
- }
- void optimize_set_default_options(Optimizer_Options *options){
- strcpy(options->id, "");
- options->level = 0;
- options->sets[0] = 0;
- options->sets[1] = 0;
- options->sets[2] = 0;
- options->full_set = FALSE;
- for (int i = 0; i < 13; i ++){
- options->stats[i] = FALSE;
- }
- for (int i = 0; i < 25; i ++){
- options->optional_sets[i] = FALSE;
- }
- Stats min = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
- options->min_stats = &min;
- options->gui = FALSE;
- options->storage = FALSE;
- options->total_excluded_teams = 0,
- options->total_excluded_units = 0;
- options->broken_sets = FALSE;
- }
- int cmd_optimize(int argc, char *argv[]){
- int status = SUCCESS;
- // Create custom argument array
- // If I don't do this, in the function optimize_parse_arguments,
- // every 7th (index 6, index 15...) are missing. NULL.
- // I don't know why. It's driving me crazy.
- char args[argc][128];
- for (int i = 0; i < argc; i ++){
- strcpy(args[i], argv[i]);
- }
- // Read command line arguments.
- Optimizer_Options options;
- optimize_set_default_options(&options);
- status = optimize_parse_arguments(argc, args, &options);
- if (SUCCESS != status) return(status);
- // Get unit data from database
- Unit unit;
- status = optimize_fetch_unit(options.id, &unit);
- if (SUCCESS != status) return(status);
- // Min stats that have the value 1 get overriden by the current stats.
- if (options.min_stats->hp == 1) options.min_stats->hp = unit.current_hp;
- if (options.min_stats->atk == 1) options.min_stats->atk = unit.current_atk;
- if (options.min_stats->def == 1) options.min_stats->def = unit.current_def;
- if (options.min_stats->spd == 1) options.min_stats->spd = unit.current_spd;
- if (options.min_stats->crr == 1) options.min_stats->crr = unit.current_crr;
- if (options.min_stats->crd == 1) options.min_stats->crd = unit.current_crd;
- if (options.min_stats->res == 1) options.min_stats->res = unit.current_res;
- if (options.min_stats->acc == 1) options.min_stats->acc = unit.current_acc;
- if (options.min_stats->ehp == 1) options.min_stats->ehp = 0;
- if (options.min_stats->dmg == 1) options.min_stats->dmg = 0;
- // Calculate required rune count.
- Rune_Set_Count requested_set_count;
- unsigned char total_runes_in_requested_sets =
- optimize_count_runes_for_sets(options.sets, &requested_set_count);
- if (6 < total_runes_in_requested_sets){
- fprintf(stderr, "Invalid rune set combination.\n");
- return ERROR_INPUT_OPTIMIZE_INCOMPLETE_SETS;
- }
- else if (6 == total_runes_in_requested_sets){
- options.full_set = TRUE;
- }
- // Create queries for the database.
- char query_even[2500];
- char query_odd[2500];
- optimize_query_for_even_slots(&options, query_even);
- optimize_query_for_odd_slots(&options, query_odd);
- //printf("\nQ ODD:\n\n%s\n\n\n\n", query_odd);
- //printf("\nQ EVEN:\n\n%s\n\n\n\n", query_even);
- // Get runes from database.
- struct Rune runes[7][600];
- unsigned int rune_count[7] = {0, 0, 0, 0, 0, 0, 0};
- optimize_get_runes(query_even, query_odd, runes, rune_count);
- // If any slot doesn't have matching runes, we can stop now.
- for (int i = 1; i < 7; i ++){
- if (rune_count[i] == 0){
- if (options.gui == FALSE){
- printf("No availbale runes for slot %d\n", i);
- }
- else{
- fprintf(stderr, "No available runes for slot %d\n", i);
- }
- return SUCCESS;
- }
- }
- // Precalculate how many combinations will have to be tested.
- unsigned long long max_combinations =
- rune_count[1] *
- rune_count[2] *
- rune_count[3] *
- rune_count[4] *
- rune_count[5] *
- rune_count[6];
- // Print summary with data collected so far. Skip for GUI.
- if (options.gui == FALSE){
- optimize_print_summary(
- &unit, &options, rune_count, max_combinations
- );
- }
- // Now its time to loop all 6 'reels' of runes and try to match combos
- if (options.gui == FALSE){
- printf("\n\n__Optimization progress___________________________\n");
- }
- else{
- printf("Total combinations: %llu\n", max_combinations);
- fflush(stdout);
- }
- // Initialize arrys and some counters
- int index[7] = {0, 0, 0, 0, 0, 0};
- unsigned long tested_combinations = 0;
- unsigned long valid_sets = 0;
- unsigned long result_count = 0;
- Rune_Set_Count set_count;
- Result results[MAX_RESULTS];
- while(
- index[1] < rune_count[1] &&
- index[2] < rune_count[2] &&
- index[3] < rune_count[3] &&
- index[4] < rune_count[4] &&
- index[5] < rune_count[5] &&
- index[6] < rune_count[6] &&
- result_count < MAX_RESULTS // Hard limit
- ){
- // Progress bar, 50 characters to 100%
- if (
- (tested_combinations + 1) %
- (unsigned long)(max_combinations / 50)
- == 0
- ){
- if (options.gui == FALSE){
- printf("#");
- }
- else{
- // For GUI flushing, newlines are required
- printf(
- "%d\n",
- (unsigned long)( (tested_combinations + 1) /
- (unsigned long)(max_combinations / 20)));
- }
- // Line buffered! need to flush after every char.
- fflush(stdout);
- }
- // Calculate rune sets at current indexes.
- set_count.energy = 0;
- set_count.guard = 0;
- set_count.swift = 0;
- set_count.blade = 0;
- set_count.rage = 0;
- set_count.focus = 0;
- set_count.endure = 0;
- set_count.fatal = 0;
- set_count.despair = 0;
- set_count.vampire = 0;
- set_count.violent = 0;
- set_count.nemesis = 0;
- set_count.will = 0;
- set_count.shield = 0;
- set_count.revenge = 0;
- set_count.destroy = 0;
- set_count.fight = 0;
- set_count.determination = 0;
- set_count.enhance = 0;
- set_count.accuracy = 0;
- set_count.tolerance = 0;
- for (int i = 1; i < 7; i ++){
- switch (runes[i][index[i]].set){
- case ENERGY: set_count.energy ++; break;
- case GUARD: set_count.guard ++; break;
- case SWIFT: set_count.swift ++; break;
- case BLADE: set_count.blade ++; break;
- case RAGE: set_count.rage ++; break;
- case FOCUS: set_count.focus ++; break;
- case ENDURE: set_count.endure ++; break;
- case FATAL: set_count.fatal ++; break;
- case DESPAIR: set_count.despair ++; break;
- case VAMPIRE: set_count.vampire ++; break;
- case VIOLENT: set_count.violent ++; break;
- case NEMESIS: set_count.nemesis ++; break;
- case WILL: set_count.will ++; break;
- case SHIELD: set_count.shield ++; break;
- case REVENGE: set_count.revenge ++; break;
- case DESTROY: set_count.destroy ++; break;
- case FIGHT: set_count.fight ++; break;
- case DETERMINATION: set_count.determination ++; break;
- case ENHANCE: set_count.enhance ++; break;
- case ACCURACY: set_count.accuracy ++; break;
- case TOLERANCE: set_count.tolerance ++; break;
- }
- }
- // Compare with requested sets
- // TODO: Here, validate broken sets too
- if (options.broken_sets == FALSE){
- }
- if (
- (
- options.broken_sets == TRUE ||
- optimize_contains_broken(&set_count) == FALSE
- ) &&
- set_count.energy >= requested_set_count.energy &&
- set_count.guard >= requested_set_count.guard &&
- set_count.swift >= requested_set_count.swift &&
- set_count.blade >= requested_set_count.blade &&
- set_count.rage >= requested_set_count.rage &&
- set_count.focus >= requested_set_count.focus &&
- set_count.endure >= requested_set_count.endure &&
- set_count.fatal >= requested_set_count.fatal &&
- set_count.despair >= requested_set_count.despair &&
- set_count.vampire >= requested_set_count.vampire &&
- set_count.violent >= requested_set_count.violent &&
- set_count.nemesis >= requested_set_count.nemesis &&
- set_count.will >= requested_set_count.will &&
- set_count.shield >= requested_set_count.shield &&
- set_count.revenge >= requested_set_count.revenge &&
- set_count.destroy >= requested_set_count.destroy &&
- set_count.fight >= requested_set_count.fight &&
- set_count.determination >= requested_set_count.determination &&
- set_count.enhance >= requested_set_count.enhance &&
- set_count.accuracy >= requested_set_count.accuracy &&
- set_count.tolerance >= requested_set_count.tolerance
- ){
- // The current runes form a valid set.
- valid_sets ++;
- // Calculate new stats
- struct Stats stats;
- stats.hp = unit.base_hp;
- stats.atk = unit.base_atk;
- stats.def = unit.base_def;
- stats.spd = unit.base_spd;
- stats.crr = unit.base_crr;
- stats.crd = unit.base_crd;
- stats.res = unit.base_res;
- stats.acc = unit.base_acc;
- for (int i = 1; i < 7; i ++){
- stats.hp += runes[i][index[i]].hp_flat;
- stats.atk += runes[i][index[i]].atk_flat;
- stats.def += runes[i][index[i]].def_flat;
- stats.hp += unit.base_hp * runes[i][index[i]].hp_percent / 100;
- stats.atk +=
- unit.base_atk * runes[i][index[i]].atk_percent / 100;
- stats.def +=
- unit.base_def * runes[i][index[i]].def_percent / 100;
- stats.spd += runes[i][index[i]].spd;
- stats.crr += runes[i][index[i]].crr;
- stats.crd += runes[i][index[i]].crd;
- stats.res += runes[i][index[i]].res;
- stats.acc += runes[i][index[i]].acc;
- }
- // Set stats
- if (set_count.energy >= 2) // +15% Base HP per set of 2
- stats.hp += unit.base_hp * 0.15 * (set_count.energy % 2);
- if (set_count.guard >= 2) // +15% base DEF per set of 2
- stats.def += unit.base_def * 0.15 * (set_count.guard % 2);
- if (set_count.swift >= 4) // +25% base SPD per set of 4
- stats.spd += unit.base_spd * 0.25;
- if (set_count.blade >= 2) // +12% CRR per set of 2
- stats.crr += 12;
- if (set_count.rage >= 4) // +40% CRD per set of 4
- stats.crd += 40;
- if (set_count.focus >= 2) // +20% ACC per set of 2
- stats.acc += 20;
- if (set_count.endure >= 2) // +20% RES per set of 2
- stats.res += 20;
- if (set_count.fatal >= 4) // +35% base ATK per set of 4
- stats.atk += unit.base_atk * 0.35;
- if (set_count.fight >= 2) // +8% base ATK per set of 2
- stats.atk += unit.base_atk * 0.08 * (set_count.fight % 2);
- if (set_count.determination >= 2) // +8% base DEF per set of 2
- stats.def +=
- unit.base_def * 0.08 * (set_count.determination % 2);
- if (set_count.enhance >= 2) // +8% base HP per set of 2
- stats.hp += unit.base_hp * 0.08 * (set_count.enhance % 2);
- if (set_count.accuracy >= 2) // +10% ACC per set of 2
- stats.acc += 20;
- if (set_count.tolerance >= 2) // +10% RES per set of 2
- stats.res += 20;
- // Cap cappable stats
- if (stats.crr > 100) stats.crr = 100;
- if (stats.res > 100) stats.res = 100;
- if (stats.acc > 85) stats.acc = 85;
- // Calculated stats
- stats.ehp = optimize_calculate_ehp(stats.hp, stats.def);
- stats.dmg = optimize_calculate_dmg(stats.atk, stats.crr, stats.crd);
- // Compare with minimum requeriments
- if (
- stats.hp >= options.min_stats->hp &&
- stats.atk >= options.min_stats->atk &&
- stats.def >= options.min_stats->def &&
- stats.spd >= options.min_stats->spd &&
- stats.crr >= options.min_stats->crr &&
- stats.crd >= options.min_stats->crd &&
- stats.res >= options.min_stats->res &&
- stats.acc >= options.min_stats->acc &&
- stats.ehp >= options.min_stats->ehp &&
- stats.dmg >= options.min_stats->dmg
- ){
- // This is a valid sets and all stats are above the minimum.
- // Create a result with rune indexes, stats, and rating.
- for (int i = 1; i < 7; i ++){
- strcpy(
- results[result_count].rune_ids[i - 1],
- runes[i][index[i]].id
- );
- }
- results[result_count].stats.hp = stats.hp;
- results[result_count].stats.atk = stats.atk;
- results[result_count].stats.def = stats.def;
- results[result_count].stats.spd = stats.spd;
- results[result_count].stats.crr = stats.crr;
- results[result_count].stats.crd = stats.crd;
- results[result_count].stats.res = stats.res;
- results[result_count].stats.acc = stats.acc;
- results[result_count].stats.ehp = stats.ehp;
- results[result_count].stats.dmg = stats.dmg;
- // Calculate rating, based on the difference between the
- // stats with this set and the current stats.
- // This is calculated accounting for the proportions between
- // the (flat) stats for each main stat in a 6 star, level 15
- // rune:
- // HP: 1 rating point per 58.29 points
- // ATK: 1 rating point per 1.50 points
- // DEF: 1 rating point per 1.50 points
- // SPD: 1 rating point per 1.00 points
- // CRR: 1 rating point per 1.38 points
- // CRD: 1 rating point per 1.90 points
- // RES: 1 rating point per 1.52 points
- // ACC: 1 rating point per 1.52 points
- float rating = 0.0f;
- rating +=
- (float) ((int) stats.hp - (int) unit.current_hp ) / 58.29f;
- rating +=
- (float) ((int) stats.atk - (int) unit.current_atk) / 1.50f;
- rating +=
- (float) ((int) stats.def - (int) unit.current_def) / 1.50f;
- rating +=
- (float) ((int) stats.spd - (int) unit.current_spd) / 1.00f;
- rating +=
- (float) ((int) stats.crr - (int) unit.current_crr) / 1.38f;
- rating +=
- (float) ((int) stats.crd - (int) unit.current_crd) / 1.90f;
- rating +=
- (float) ((int) stats.res - (int) unit.current_res) / 1.52f;
- rating +=
- (float) ((int) stats.acc - (int) unit.current_acc) / 1.52f;
- results[result_count].rating = (int) rating;
- // Account for found result
- result_count ++;
- }
- }
- // Loop control. Rotate the reels 'right to left'
- tested_combinations ++;
- index[6] ++;
- if (index[6] == rune_count[6]){
- index[6] = 0;
- index[5] ++;
- }
- if (index[5] == rune_count[5]){
- index[5] = 0;
- index[4] ++;
- }
- if (index[4] == rune_count[4]){
- index[4] = 0;
- index[3] ++;
- }
- if (index[3] == rune_count[3]){
- index[3] = 0;
- index[2] ++;
- }
- if (index[2] == rune_count[2]){
- index[2] = 0;
- index[1] ++;
- }
- //DEBUG: force exit with some results
- //if (result_count > 2){
- // break;
- //}
- }
- if (options.gui == FALSE){
- printf("\n");
- }
- if (result_count > 0){
- // Yay! Some combinations matched te criteria.
- if (options.gui == FALSE){
- printf("\n\n%d results found\n", result_count);
- }
- // Sort results
- optimize_sort_results(results, result_count);
- if (options.gui == FALSE){
- // Preview the best option
- optimize_print_result(&unit, &results[0]);
- }
- else{
- // Create the output
- optimize_print_gui(results, result_count);
- }
- }
- else{
- if (options.gui == FALSE){
- printf("No results found\n");
- }
- else{
- printf("{\"result_count\":0,\"results\":[]}\n");
- }
- }
- return SUCCESS;
- }
|