runeoptimizer.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 runeoptimizer.c
  19. *
  20. * Implementation of the functions used by the gui command.
  21. *
  22. * This file implements the main function, and the functions declared in
  23. * {@link runeoptimizer.h}.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sqlite3.h>
  29. #include <math.h>
  30. #include "runeoptimizer.h"
  31. #include "error/error.h"
  32. #include "db/db.h"
  33. #include "help/help.h"
  34. #include "optimize/optimize.h"
  35. #include "team/team.h"
  36. #include "unit/unit.h"
  37. #include "update/update.h"
  38. #include "player/player.h"
  39. #include "gui/gui.h"
  40. /**
  41. * DEF stat multiplier.
  42. *
  43. * For effective HP calculation.
  44. */
  45. #define EHP_DEF_MULTIPLIER 3.5f
  46. /**
  47. * DEF base value.
  48. *
  49. * For effective HP calculation.
  50. */
  51. #define EHP_DEF_BASE 1140.0f
  52. /**
  53. * HP stat divider.
  54. *
  55. * For effective HP calculation.
  56. */
  57. #define EHP_HP_DIVIDER 1000.0f
  58. /**
  59. * Just 100.
  60. *
  61. * For perccentage calculations.
  62. */
  63. #define PERCENT 100.0f
  64. /**
  65. * Command version.
  66. *
  67. * Keyword to launch the version command.
  68. */
  69. #define CMD_VERSION "version"
  70. /**
  71. * Command update.
  72. *
  73. * Keyword to launch the update command.
  74. */
  75. #define CMD_UPDATE "update"
  76. /**
  77. * Command optimize.
  78. *
  79. * Keyword to launch the optimize command.
  80. */
  81. #define CMD_OPTIMIZE "optimize"
  82. /**
  83. * Command help.
  84. *
  85. * Keyword to launch the help command.
  86. */
  87. #define CMD_HELP "help"
  88. /**
  89. * Command team.
  90. *
  91. * Keyword to launch the team command.
  92. */
  93. #define CMD_TEAM "team"
  94. /**
  95. * Command unit.
  96. *
  97. * Keyword to launch the unit command.
  98. */
  99. #define CMD_UNIT "unit"
  100. /**
  101. * Command player.
  102. *
  103. * Keyword to launch the player command.
  104. */
  105. #define CMD_PLAYER "player"
  106. /**
  107. * Command gui.
  108. *
  109. * Keyword to launch the gui command.
  110. */
  111. #define CMD_GUI "gui"
  112. #ifdef _WIN32
  113. /**
  114. * Home environment variable for Windows.
  115. *
  116. * Key to get the environment variable to determine the user directory.
  117. */
  118. #define ENV_VAR_HOME "APPDATA"
  119. /**
  120. * The default path to the database for Windows.
  121. *
  122. * Relative (and to be appended to) the user directory path.
  123. * @todo: This won't work on Windows.
  124. */
  125. #define DEFAULT_DB_PATH_FROM_HOME "/RuneOptimizer/data.sqlite"
  126. #else
  127. /**
  128. * Home environment variable for Linux.
  129. *
  130. * Key to get the environment variable to determine the user directory.
  131. */
  132. #define ENV_VAR_HOME "HOME"
  133. /**
  134. * The default path to the database for Linux.
  135. *
  136. * Relative (and to be appended to) the user directory path.
  137. * @todo: This won't work on Windows.
  138. */
  139. #define DEFAULT_DB_PATH_FROM_HOME "/.local/share/RuneOptimizer/data.sqlite"
  140. #endif
  141. /**
  142. * Argument cutter for program arguments.
  143. *
  144. * Determines how many parameters will be removed from the start of the list of
  145. * parameters passed to the program before passing that list to any command that
  146. * needs them.
  147. *
  148. * Basically it to remove the executable name and the command name.
  149. */
  150. #define ARGS_CUT_FOR_COMMANDS 2
  151. sqlite3 *db = NULL;
  152. char db_location[DB_PATH_LEN];
  153. const char STAT_NAMES[DIFFERENT_STATS][9] = {
  154. "NULL", "HP_FLAT", "HP", "ATK_FLAT", "ATK", "DEF_FLAT",
  155. "DEF", "NULL", "SPD", "CRR", "CRD", "RES", "ACC"
  156. };
  157. const char SET_NAMES[DIFFERENT_SETS][9] = {
  158. "NULL", "ENERGY", "GUARD", "SWIFT", "BLADE", "RAGE",
  159. "FOCUS", "ENDURE", "FATAL", "NULL", "DESPAIR", "VAMPIRE",
  160. "NULL", "VIOLENT", "NEMESIS", "WILL", "SHIELD", "REVENGE",
  161. "DESTROY", "FIGHT", "DETERMIN", "ENHANCE", "ACCURACY", "TOLERANCE"
  162. };
  163. const char QUALITY_NAMES[DIFFERENT_QUALITIES][9] = {
  164. "NULL",
  165. "NORMAL", "MAGIC", "RARE", "HERO", "LEGEND", // 1-5
  166. "NULL", "NULL", "NULL", "NULL", "NULL",
  167. "A.NORMAL", "A.MAGIC", "A.RARE", "A.HERO", "A.LEGEND" //11-15
  168. };
  169. const char STAT_NAMES_PRINTABLE[DIFFERENT_STATS][12] = {
  170. "NULL",
  171. "HP %4d ", "HP %4d%% ", "ATK %4d ", "ATK %4d%% ", "DEF %4d ", "DEF %4d%% ", // 1-6
  172. "NULL",
  173. "SPD %4d ", "CRR %4d%% ", "CRD %4d%% ", "RES %4d%% ", "ACC %4d%% " // 8-12
  174. };
  175. const int STAT_ROLL_MAX[DIFFERENT_STATS][RUNE_MAX_STARS + 1] = {
  176. {-1, -1, -1, -1, -1, -1, -1}, // NULL (unused)
  177. {-1, 60, 105, 165, 225, 300, 375}, // HP
  178. {-1, 2, 3, 5, 6, 7, 8}, // HP%
  179. {-1, 4, 5, 8, 10, 15, 20}, // ATK
  180. {-1, 2, 3, 5, 6, 7, 8}, // ATK%
  181. {-1, 4, 5, 8, 10, 15, 20}, // DEF
  182. {-1, 2, 3, 5, 6, 7, 8}, // DEF%
  183. {-1, -1, -1, -1, -1, -1, -1}, // NULL (unused)
  184. {-1, 1, 2, 3, 4, 5, 6}, // SPD
  185. {-1, 1, 2, 3, 4, 5, 6}, // CRR
  186. {-1, 2, 3, 4, 5, 5, 7}, // CRD
  187. {-1, 2, 3, 4, 5, 7, 8}, // RES
  188. {-1, 2, 3, 4, 5, 7, 8} // ACC
  189. };
  190. extern unsigned int calculate_ehp(unsigned int hp, unsigned short def){
  191. // Sorry, but this is the formula, and I don't understand it either.
  192. // Take it or leave it.
  193. unsigned int ehp = ceil(
  194. (((((float) def) * EHP_DEF_MULTIPLIER) + EHP_DEF_BASE) * ((float) hp)) /
  195. EHP_HP_DIVIDER
  196. );
  197. return ehp;
  198. }
  199. extern unsigned short calculate_dmg(
  200. unsigned short atk, unsigned short crr, unsigned short crd
  201. ){
  202. float crr_capped = (float) crr;
  203. if (crr_capped > CRR_CAP){
  204. crr_capped = CRR_CAP;
  205. }
  206. unsigned short dmg = ceil(
  207. ((float)atk * ((PERCENT - crr_capped) / PERCENT)) + // Non-crit
  208. ((float)atk * (crr_capped / PERCENT) * ((float)(crd + 100) / PERCENT)) // Crit
  209. );
  210. return(dmg);
  211. }
  212. int main(int argc, char *argv[]){
  213. // Parse the arguments to find the command (index 1)
  214. // If no arguments, end here
  215. if (argc < 2){
  216. fprintf(stderr, "No command specified\n");
  217. return(ERROR_INPUT_NO_COMMAND);
  218. }
  219. // Set the global database location
  220. strcat(db_location, getenv(ENV_VAR_HOME));
  221. strcat(db_location, DEFAULT_DB_PATH_FROM_HOME);
  222. int result = SUCCESS;
  223. // Version command
  224. if (strcmp(argv[1], CMD_VERSION) == 0){
  225. printf("%s\n", VERSION);
  226. return(SUCCESS);
  227. }
  228. // Update command
  229. else if (strcmp(argv[1], CMD_UPDATE) == 0){
  230. return(
  231. update(argc - ARGS_CUT_FOR_COMMANDS, argv + ARGS_CUT_FOR_COMMANDS)
  232. );
  233. }
  234. // Optimize command.
  235. else if (strcmp(argv[1], CMD_OPTIMIZE) == 0){
  236. // Pass the rest of the arguments to the team command.
  237. return(
  238. optimize(argc - ARGS_CUT_FOR_COMMANDS, argv + ARGS_CUT_FOR_COMMANDS)
  239. );
  240. }
  241. // Help command. Call and return
  242. else if (strcmp(argv[1], CMD_HELP) == 0){
  243. help();
  244. return(SUCCESS);
  245. }
  246. // Team command.
  247. else if (strcmp(argv[1], CMD_TEAM) == 0){
  248. // Pass the rest of the arguments to the team command.
  249. return(
  250. team(argc - ARGS_CUT_FOR_COMMANDS, argv + ARGS_CUT_FOR_COMMANDS)
  251. );
  252. }
  253. // Unit command.
  254. else if (strcmp(argv[1], CMD_UNIT) == 0){
  255. // Pass the rest of the arguments to the team command.
  256. return(
  257. unit(argc - ARGS_CUT_FOR_COMMANDS, argv + ARGS_CUT_FOR_COMMANDS)
  258. );
  259. }
  260. // Player command.
  261. else if (strcmp(argv[1], CMD_PLAYER) == 0){
  262. return(player());
  263. }
  264. // GUI command.
  265. else if (strcmp(argv[1], CMD_GUI) == 0){
  266. return(gui());
  267. }
  268. // Any other command is an error
  269. else{
  270. fprintf(stderr, "Invalid command specified: %s\n", argv[1]);
  271. return(ERROR_INPUT_INVALID_COMMAND);
  272. }
  273. return result;
  274. }