optimize.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * This file is part of RuneOptimizer.
  3. *
  4. * RuneOptimizer is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation, either version 3 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * RuneOptimizer is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * RuneOptimizer. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * @file optimize.c
  19. * Implementation of the optimize command and some utilities used by it.
  20. */
  21. #include "optimize.h"
  22. #include "optimize_parse_arguments.c"
  23. #include "optimize_fetch_unit.c"
  24. #include "optimize_count_runes_for_sets.c"
  25. #include "optimize_query.c"
  26. #include "optimize_get_runes.c"
  27. #include "optimize_print.c"
  28. void optimize_sort_results(Result results[5000], int total){
  29. // Bubble sort, by descending rating.
  30. int i, j;
  31. Result temp;
  32. for (i = 0; i < total - 1; i++){
  33. for (j = 0; j < (total - 1-i); j++){
  34. if (results[j].rating < results[j + 1].rating){
  35. temp = results[j];
  36. results[j] = results[j + 1];
  37. results[j + 1] = temp;
  38. }
  39. }
  40. }
  41. }
  42. unsigned int optimize_calculate_ehp(unsigned int hp, unsigned short def){
  43. // Lots of magic numbers here!
  44. // Sorry, but this is the formula, and I don't understand it either.
  45. // Take it or leave it.
  46. unsigned int ehp =
  47. ceil((((((float) def) * 3.5f) + 1140.0f) * ((float) hp)) / 1000.0f);
  48. return ehp;
  49. }
  50. unsigned short optimize_calculate_dmg(
  51. unsigned short atk, unsigned short crr, unsigned short crd
  52. ){
  53. float crr_capped = (float) crr;
  54. if (crr_capped > 100.0f){
  55. crr_capped = 100.0f;
  56. }
  57. unsigned short dmg = ceil(
  58. ((float)atk * ((100 - crr_capped) / 100)) + // Non-crit
  59. ((float)atk * (crr_capped / 100) * ((float)crd / 100)) // Crit
  60. );
  61. return(dmg);
  62. }
  63. unsigned char optimize_contains_broken(Rune_Set_Count *set_count){
  64. if (set_count->energy % 2 != 0) return(TRUE);
  65. if (set_count->guard % 2 != 0) return(TRUE);
  66. if (set_count->swift % 4 != 0) return(TRUE);
  67. if (set_count->blade % 2 != 0) return(TRUE);
  68. if (set_count->rage % 4 != 0) return(TRUE);
  69. if (set_count->focus % 2 != 0) return(TRUE);
  70. if (set_count->endure % 2 != 0) return(TRUE);
  71. if (set_count->fatal % 4 != 0) return(TRUE);
  72. if (set_count->despair % 4 != 0) return(TRUE);
  73. if (set_count->vampire % 4 != 0) return(TRUE);
  74. if (set_count->violent % 4 != 0) return(TRUE);
  75. if (set_count->nemesis % 2 != 0) return(TRUE);
  76. if (set_count->will % 2 != 0) return(TRUE);
  77. if (set_count->shield % 2 != 0) return(TRUE);
  78. if (set_count->revenge % 2 != 0) return(TRUE);
  79. if (set_count->destroy % 2 != 0) return(TRUE);
  80. if (set_count->fight % 2 != 0) return(TRUE);
  81. if (set_count->determination % 2 != 0) return(TRUE);
  82. if (set_count->enhance % 2 != 0) return(TRUE);
  83. if (set_count->accuracy % 2 != 0) return(TRUE);
  84. if (set_count->tolerance % 2 != 0) return(TRUE);
  85. return(FALSE);
  86. }
  87. void optimize_set_default_options(Optimizer_Options *options){
  88. strcpy(options->id, "");
  89. options->level = 0;
  90. options->sets[0] = 0;
  91. options->sets[1] = 0;
  92. options->sets[2] = 0;
  93. options->full_set = FALSE;
  94. for (int i = 0; i < 13; i ++){
  95. options->stats[i] = FALSE;
  96. }
  97. for (int i = 0; i < 25; i ++){
  98. options->optional_sets[i] = FALSE;
  99. }
  100. Stats min = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  101. options->min_stats = &min;
  102. options->gui = FALSE;
  103. options->storage = FALSE;
  104. options->total_excluded_teams = 0,
  105. options->total_excluded_units = 0;
  106. options->broken_sets = FALSE;
  107. }
  108. int cmd_optimize(int argc, char *argv[]){
  109. int status = SUCCESS;
  110. // Create custom argument array
  111. // If I don't do this, in the function optimize_parse_arguments,
  112. // every 7th (index 6, index 15...) are missing. NULL.
  113. // I don't know why. It's driving me crazy.
  114. char args[argc][128];
  115. for (int i = 0; i < argc; i ++){
  116. strcpy(args[i], argv[i]);
  117. }
  118. // Read command line arguments.
  119. Optimizer_Options options;
  120. optimize_set_default_options(&options);
  121. status = optimize_parse_arguments(argc, args, &options);
  122. if (SUCCESS != status) return(status);
  123. // Get unit data from database
  124. Unit unit;
  125. status = optimize_fetch_unit(options.id, &unit);
  126. if (SUCCESS != status) return(status);
  127. // Min stats that have the value 1 get overriden by the current stats.
  128. if (options.min_stats->hp == 1) options.min_stats->hp = unit.current_hp;
  129. if (options.min_stats->atk == 1) options.min_stats->atk = unit.current_atk;
  130. if (options.min_stats->def == 1) options.min_stats->def = unit.current_def;
  131. if (options.min_stats->spd == 1) options.min_stats->spd = unit.current_spd;
  132. if (options.min_stats->crr == 1) options.min_stats->crr = unit.current_crr;
  133. if (options.min_stats->crd == 1) options.min_stats->crd = unit.current_crd;
  134. if (options.min_stats->res == 1) options.min_stats->res = unit.current_res;
  135. if (options.min_stats->acc == 1) options.min_stats->acc = unit.current_acc;
  136. if (options.min_stats->ehp == 1) options.min_stats->ehp = 0;
  137. if (options.min_stats->dmg == 1) options.min_stats->dmg = 0;
  138. // Calculate required rune count.
  139. Rune_Set_Count requested_set_count;
  140. unsigned char total_runes_in_requested_sets =
  141. optimize_count_runes_for_sets(options.sets, &requested_set_count);
  142. if (6 < total_runes_in_requested_sets){
  143. fprintf(stderr, "Invalid rune set combination.\n");
  144. return ERROR_INPUT_OPTIMIZE_INCOMPLETE_SETS;
  145. }
  146. else if (6 == total_runes_in_requested_sets){
  147. options.full_set = TRUE;
  148. }
  149. // Create queries for the database.
  150. char query_even[2500];
  151. char query_odd[2500];
  152. optimize_query_for_even_slots(&options, query_even);
  153. optimize_query_for_odd_slots(&options, query_odd);
  154. //printf("\nQ ODD:\n\n%s\n\n\n\n", query_odd);
  155. //printf("\nQ EVEN:\n\n%s\n\n\n\n", query_even);
  156. // Get runes from database.
  157. struct Rune runes[7][600];
  158. unsigned int rune_count[7] = {0, 0, 0, 0, 0, 0, 0};
  159. optimize_get_runes(query_even, query_odd, runes, rune_count);
  160. // If any slot doesn't have matching runes, we can stop now.
  161. for (int i = 1; i < 7; i ++){
  162. if (rune_count[i] == 0){
  163. if (options.gui == FALSE){
  164. printf("No availbale runes for slot %d\n", i);
  165. }
  166. else{
  167. fprintf(stderr, "No available runes for slot %d\n", i);
  168. }
  169. return SUCCESS;
  170. }
  171. }
  172. // Precalculate how many combinations will have to be tested.
  173. unsigned long long max_combinations =
  174. rune_count[1] *
  175. rune_count[2] *
  176. rune_count[3] *
  177. rune_count[4] *
  178. rune_count[5] *
  179. rune_count[6];
  180. // Print summary with data collected so far. Skip for GUI.
  181. if (options.gui == FALSE){
  182. optimize_print_summary(
  183. &unit, &options, rune_count, max_combinations
  184. );
  185. }
  186. // Now its time to loop all 6 'reels' of runes and try to match combos
  187. if (options.gui == FALSE){
  188. printf("\n\n__Optimization progress___________________________\n");
  189. }
  190. else{
  191. printf("Total combinations: %llu\n", max_combinations);
  192. fflush(stdout);
  193. }
  194. // Initialize arrys and some counters
  195. int index[7] = {0, 0, 0, 0, 0, 0};
  196. unsigned long tested_combinations = 0;
  197. unsigned long valid_sets = 0;
  198. unsigned long result_count = 0;
  199. Rune_Set_Count set_count;
  200. Result results[MAX_RESULTS];
  201. while(
  202. index[1] < rune_count[1] &&
  203. index[2] < rune_count[2] &&
  204. index[3] < rune_count[3] &&
  205. index[4] < rune_count[4] &&
  206. index[5] < rune_count[5] &&
  207. index[6] < rune_count[6] &&
  208. result_count < MAX_RESULTS // Hard limit
  209. ){
  210. // Progress bar, 50 characters to 100%
  211. if (
  212. (tested_combinations + 1) %
  213. (unsigned long)(max_combinations / 50)
  214. == 0
  215. ){
  216. if (options.gui == FALSE){
  217. printf("#");
  218. }
  219. else{
  220. // For GUI flushing, newlines are required
  221. printf(
  222. "%d\n",
  223. (unsigned long)( (tested_combinations + 1) /
  224. (unsigned long)(max_combinations / 20)));
  225. }
  226. // Line buffered! need to flush after every char.
  227. fflush(stdout);
  228. }
  229. // Calculate rune sets at current indexes.
  230. set_count.energy = 0;
  231. set_count.guard = 0;
  232. set_count.swift = 0;
  233. set_count.blade = 0;
  234. set_count.rage = 0;
  235. set_count.focus = 0;
  236. set_count.endure = 0;
  237. set_count.fatal = 0;
  238. set_count.despair = 0;
  239. set_count.vampire = 0;
  240. set_count.violent = 0;
  241. set_count.nemesis = 0;
  242. set_count.will = 0;
  243. set_count.shield = 0;
  244. set_count.revenge = 0;
  245. set_count.destroy = 0;
  246. set_count.fight = 0;
  247. set_count.determination = 0;
  248. set_count.enhance = 0;
  249. set_count.accuracy = 0;
  250. set_count.tolerance = 0;
  251. for (int i = 1; i < 7; i ++){
  252. switch (runes[i][index[i]].set){
  253. case ENERGY: set_count.energy ++; break;
  254. case GUARD: set_count.guard ++; break;
  255. case SWIFT: set_count.swift ++; break;
  256. case BLADE: set_count.blade ++; break;
  257. case RAGE: set_count.rage ++; break;
  258. case FOCUS: set_count.focus ++; break;
  259. case ENDURE: set_count.endure ++; break;
  260. case FATAL: set_count.fatal ++; break;
  261. case DESPAIR: set_count.despair ++; break;
  262. case VAMPIRE: set_count.vampire ++; break;
  263. case VIOLENT: set_count.violent ++; break;
  264. case NEMESIS: set_count.nemesis ++; break;
  265. case WILL: set_count.will ++; break;
  266. case SHIELD: set_count.shield ++; break;
  267. case REVENGE: set_count.revenge ++; break;
  268. case DESTROY: set_count.destroy ++; break;
  269. case FIGHT: set_count.fight ++; break;
  270. case DETERMINATION: set_count.determination ++; break;
  271. case ENHANCE: set_count.enhance ++; break;
  272. case ACCURACY: set_count.accuracy ++; break;
  273. case TOLERANCE: set_count.tolerance ++; break;
  274. }
  275. }
  276. // Compare with requested sets
  277. // TODO: Here, validate broken sets too
  278. if (options.broken_sets == FALSE){
  279. }
  280. if (
  281. (
  282. options.broken_sets == TRUE ||
  283. optimize_contains_broken(&set_count) == FALSE
  284. ) &&
  285. set_count.energy >= requested_set_count.energy &&
  286. set_count.guard >= requested_set_count.guard &&
  287. set_count.swift >= requested_set_count.swift &&
  288. set_count.blade >= requested_set_count.blade &&
  289. set_count.rage >= requested_set_count.rage &&
  290. set_count.focus >= requested_set_count.focus &&
  291. set_count.endure >= requested_set_count.endure &&
  292. set_count.fatal >= requested_set_count.fatal &&
  293. set_count.despair >= requested_set_count.despair &&
  294. set_count.vampire >= requested_set_count.vampire &&
  295. set_count.violent >= requested_set_count.violent &&
  296. set_count.nemesis >= requested_set_count.nemesis &&
  297. set_count.will >= requested_set_count.will &&
  298. set_count.shield >= requested_set_count.shield &&
  299. set_count.revenge >= requested_set_count.revenge &&
  300. set_count.destroy >= requested_set_count.destroy &&
  301. set_count.fight >= requested_set_count.fight &&
  302. set_count.determination >= requested_set_count.determination &&
  303. set_count.enhance >= requested_set_count.enhance &&
  304. set_count.accuracy >= requested_set_count.accuracy &&
  305. set_count.tolerance >= requested_set_count.tolerance
  306. ){
  307. // The current runes form a valid set.
  308. valid_sets ++;
  309. // Calculate new stats
  310. struct Stats stats;
  311. stats.hp = unit.base_hp;
  312. stats.atk = unit.base_atk;
  313. stats.def = unit.base_def;
  314. stats.spd = unit.base_spd;
  315. stats.crr = unit.base_crr;
  316. stats.crd = unit.base_crd;
  317. stats.res = unit.base_res;
  318. stats.acc = unit.base_acc;
  319. for (int i = 1; i < 7; i ++){
  320. stats.hp += runes[i][index[i]].hp_flat;
  321. stats.atk += runes[i][index[i]].atk_flat;
  322. stats.def += runes[i][index[i]].def_flat;
  323. stats.hp += unit.base_hp * runes[i][index[i]].hp_percent / 100;
  324. stats.atk +=
  325. unit.base_atk * runes[i][index[i]].atk_percent / 100;
  326. stats.def +=
  327. unit.base_def * runes[i][index[i]].def_percent / 100;
  328. stats.spd += runes[i][index[i]].spd;
  329. stats.crr += runes[i][index[i]].crr;
  330. stats.crd += runes[i][index[i]].crd;
  331. stats.res += runes[i][index[i]].res;
  332. stats.acc += runes[i][index[i]].acc;
  333. }
  334. // Set stats
  335. if (set_count.energy >= 2) // +15% Base HP per set of 2
  336. stats.hp += unit.base_hp * 0.15 * (set_count.energy % 2);
  337. if (set_count.guard >= 2) // +15% base DEF per set of 2
  338. stats.def += unit.base_def * 0.15 * (set_count.guard % 2);
  339. if (set_count.swift >= 4) // +25% base SPD per set of 4
  340. stats.spd += unit.base_spd * 0.25;
  341. if (set_count.blade >= 2) // +12% CRR per set of 2
  342. stats.crr += 12;
  343. if (set_count.rage >= 4) // +40% CRD per set of 4
  344. stats.crd += 40;
  345. if (set_count.focus >= 2) // +20% ACC per set of 2
  346. stats.acc += 20;
  347. if (set_count.endure >= 2) // +20% RES per set of 2
  348. stats.res += 20;
  349. if (set_count.fatal >= 4) // +35% base ATK per set of 4
  350. stats.atk += unit.base_atk * 0.35;
  351. if (set_count.fight >= 2) // +8% base ATK per set of 2
  352. stats.atk += unit.base_atk * 0.08 * (set_count.fight % 2);
  353. if (set_count.determination >= 2) // +8% base DEF per set of 2
  354. stats.def +=
  355. unit.base_def * 0.08 * (set_count.determination % 2);
  356. if (set_count.enhance >= 2) // +8% base HP per set of 2
  357. stats.hp += unit.base_hp * 0.08 * (set_count.enhance % 2);
  358. if (set_count.accuracy >= 2) // +10% ACC per set of 2
  359. stats.acc += 20;
  360. if (set_count.tolerance >= 2) // +10% RES per set of 2
  361. stats.res += 20;
  362. // Cap cappable stats
  363. if (stats.crr > 100) stats.crr = 100;
  364. if (stats.res > 100) stats.res = 100;
  365. if (stats.acc > 85) stats.acc = 85;
  366. // Calculated stats
  367. stats.ehp = optimize_calculate_ehp(stats.hp, stats.def);
  368. stats.dmg = optimize_calculate_dmg(stats.atk, stats.crr, stats.crd);
  369. // Compare with minimum requeriments
  370. if (
  371. stats.hp >= options.min_stats->hp &&
  372. stats.atk >= options.min_stats->atk &&
  373. stats.def >= options.min_stats->def &&
  374. stats.spd >= options.min_stats->spd &&
  375. stats.crr >= options.min_stats->crr &&
  376. stats.crd >= options.min_stats->crd &&
  377. stats.res >= options.min_stats->res &&
  378. stats.acc >= options.min_stats->acc &&
  379. stats.ehp >= options.min_stats->ehp &&
  380. stats.dmg >= options.min_stats->dmg
  381. ){
  382. // This is a valid sets and all stats are above the minimum.
  383. // Create a result with rune indexes, stats, and rating.
  384. for (int i = 1; i < 7; i ++){
  385. strcpy(
  386. results[result_count].rune_ids[i - 1],
  387. runes[i][index[i]].id
  388. );
  389. }
  390. results[result_count].stats.hp = stats.hp;
  391. results[result_count].stats.atk = stats.atk;
  392. results[result_count].stats.def = stats.def;
  393. results[result_count].stats.spd = stats.spd;
  394. results[result_count].stats.crr = stats.crr;
  395. results[result_count].stats.crd = stats.crd;
  396. results[result_count].stats.res = stats.res;
  397. results[result_count].stats.acc = stats.acc;
  398. results[result_count].stats.ehp = stats.ehp;
  399. results[result_count].stats.dmg = stats.dmg;
  400. // Calculate rating, based on the difference between the
  401. // stats with this set and the current stats.
  402. // This is calculated accounting for the proportions between
  403. // the (flat) stats for each main stat in a 6 star, level 15
  404. // rune:
  405. // HP: 1 rating point per 58.29 points
  406. // ATK: 1 rating point per 1.50 points
  407. // DEF: 1 rating point per 1.50 points
  408. // SPD: 1 rating point per 1.00 points
  409. // CRR: 1 rating point per 1.38 points
  410. // CRD: 1 rating point per 1.90 points
  411. // RES: 1 rating point per 1.52 points
  412. // ACC: 1 rating point per 1.52 points
  413. float rating = 0.0f;
  414. rating +=
  415. (float) ((int) stats.hp - (int) unit.current_hp ) / 58.29f;
  416. rating +=
  417. (float) ((int) stats.atk - (int) unit.current_atk) / 1.50f;
  418. rating +=
  419. (float) ((int) stats.def - (int) unit.current_def) / 1.50f;
  420. rating +=
  421. (float) ((int) stats.spd - (int) unit.current_spd) / 1.00f;
  422. rating +=
  423. (float) ((int) stats.crr - (int) unit.current_crr) / 1.38f;
  424. rating +=
  425. (float) ((int) stats.crd - (int) unit.current_crd) / 1.90f;
  426. rating +=
  427. (float) ((int) stats.res - (int) unit.current_res) / 1.52f;
  428. rating +=
  429. (float) ((int) stats.acc - (int) unit.current_acc) / 1.52f;
  430. results[result_count].rating = (int) rating;
  431. // Account for found result
  432. result_count ++;
  433. }
  434. }
  435. // Loop control. Rotate the reels 'right to left'
  436. tested_combinations ++;
  437. index[6] ++;
  438. if (index[6] == rune_count[6]){
  439. index[6] = 0;
  440. index[5] ++;
  441. }
  442. if (index[5] == rune_count[5]){
  443. index[5] = 0;
  444. index[4] ++;
  445. }
  446. if (index[4] == rune_count[4]){
  447. index[4] = 0;
  448. index[3] ++;
  449. }
  450. if (index[3] == rune_count[3]){
  451. index[3] = 0;
  452. index[2] ++;
  453. }
  454. if (index[2] == rune_count[2]){
  455. index[2] = 0;
  456. index[1] ++;
  457. }
  458. //DEBUG: force exit with some results
  459. //if (result_count > 2){
  460. // break;
  461. //}
  462. }
  463. if (options.gui == FALSE){
  464. printf("\n");
  465. }
  466. if (result_count > 0){
  467. // Yay! Some combinations matched te criteria.
  468. if (options.gui == FALSE){
  469. printf("\n\n%d results found\n", result_count);
  470. }
  471. // Sort results
  472. optimize_sort_results(results, result_count);
  473. if (options.gui == FALSE){
  474. // Preview the best option
  475. optimize_print_result(&unit, &results[0]);
  476. }
  477. else{
  478. // Create the output
  479. optimize_print_gui(results, result_count);
  480. }
  481. }
  482. else{
  483. if (options.gui == FALSE){
  484. printf("No results found\n");
  485. }
  486. else{
  487. printf("{\"result_count\":0,\"results\":[]}\n");
  488. }
  489. }
  490. return SUCCESS;
  491. }