Monster.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /**
  3. * Base monster entity file.
  4. *
  5. * Creates the entity and makes it available.
  6. *
  7. * @category Entity
  8. */
  9. /**
  10. * Require dependent entities if not present.
  11. */
  12. require_once(PATH::ENTITY . "Entity.php");
  13. require_once(PATH::ENTITY . "Skill.php");
  14. require_once(PATH::ENTITY . "Leader_Skill.php");
  15. require_once(PATH::ENTITY . "Source.php");
  16. require_once(PATH::ENTITY . "Inventory.php");
  17. require_once(PATH::ENTITY . "Monster_Transformation.php");
  18. /**
  19. * A monster.
  20. *
  21. * Represents an object from the table 'monster'.
  22. *
  23. * @category Entity
  24. */
  25. class Monster extends Entity{
  26. /**
  27. * @var int Monster identifier.
  28. */
  29. public $id;
  30. /**
  31. * @var int Monster family id.
  32. */
  33. public $family;
  34. /**
  35. * @var string Monster name.
  36. */
  37. public $name;
  38. /**
  39. * @var int Monster element id.
  40. */
  41. public $element;
  42. /**
  43. * @var string Element name (title case)
  44. */
  45. public $element_name;
  46. /**
  47. * @var int Monster archetype id.
  48. */
  49. public $archetype;
  50. /**
  51. * @var string Archetype name (title case).
  52. */
  53. public $archetype_name;
  54. /**
  55. * @var int Default star level.
  56. */
  57. public $base_stars;
  58. /**
  59. *@var int Default star level.
  60. */
  61. public $natural_stars;
  62. /**
  63. * @var bool Indicates if the monster is obtainable.
  64. */
  65. public $obtainable;
  66. /**
  67. * @var bool Indicates if the monster can be awakened.
  68. */
  69. public $can_awaken;
  70. /**
  71. * @var string Bonus when awakening.
  72. */
  73. public $awaken_bonus;
  74. /**
  75. * @var int Required skill-ups.
  76. */
  77. public $skill_ups_to_max;
  78. /**
  79. * @var Leader_Skill Monster's leader skill.
  80. */
  81. public $leader_skill;
  82. /**
  83. * @var int Base HP.
  84. */
  85. public $hp;
  86. /**
  87. * @var int Base attack.
  88. */
  89. public $atk;
  90. /**
  91. * @var int Base defense.
  92. */
  93. public $defense;
  94. /**
  95. * @var int Speed.
  96. */
  97. public $speed;
  98. /**
  99. * @var int Critical rate.
  100. */
  101. public $crit_rate;
  102. /**
  103. * @var int Critical damage.
  104. */
  105. public $crit_damage;
  106. /**
  107. * @var int Resistance
  108. */
  109. public $resistance;
  110. /**
  111. * @var int Accuracy.
  112. */
  113. public $accuracy;
  114. /**
  115. * @var int Raw HP.
  116. */
  117. public $raw_hp;
  118. /**
  119. * @var int Raw attack.
  120. */
  121. public $raw_atk;
  122. /**
  123. * @var int Raw Defense.
  124. */
  125. public $raw_defense;
  126. /**
  127. * @var int HP at max_level.
  128. */
  129. public $max_lvl_hp;
  130. /**
  131. * @var int Attack at max_level.
  132. */
  133. public $max_lvl_atk;
  134. /**
  135. * @var int Defense at max_level.
  136. */
  137. public $max_lvl_defense;
  138. /**
  139. * @var Monster Monster it awakens from.
  140. */
  141. public $awakens_from;
  142. /**
  143. * @var Monster Monster it awakens to.
  144. */
  145. public $awakens_to;
  146. /**
  147. * @var bool Indicates if the monster is a fusion ingredient.
  148. */
  149. public $fusion_food;
  150. /**
  151. * @var bool Indicates if the monster is Homunculus.
  152. */
  153. public $homunculus;
  154. /**
  155. * @var int The crafting cost (Homunculus only).
  156. */
  157. public $craft_cost;
  158. /**
  159. * @var \Skill[] Monster skills.
  160. */
  161. public $skill = [];
  162. /**
  163. * @var mixed[] Required essences to awaken the monster.
  164. *
  165. * Formed by arrays of:
  166. * [
  167. * "item" => Inventory,
  168. * "amount" => int
  169. * ]
  170. */
  171. public $essences = [];
  172. /**
  173. * @var int \Source[] Sources the monster can be get from.
  174. */
  175. public $sources = [];
  176. /**
  177. * @var string Monster title.
  178. * If awakened, it will be the awakened from name.
  179. * If unawakened, it will be <element> <name>.
  180. */
  181. public $title;
  182. /**
  183. * @var Monster_Transformation The transformation the monster can undergo.
  184. */
  185. public $transformation;
  186. /**
  187. * Constructor.
  188. *
  189. * Searches the database and retrieves the information about the
  190. * monster, populating it and it's items.
  191. *
  192. * @param int $id Monster identifier.
  193. * @param bool $complete If false, query only monster.
  194. */
  195. public function __construct($id, $complete = true){
  196. $this->id = $id;
  197. $statement = get_context()->get_db()->prepare("
  198. SELECT
  199. id,
  200. family,
  201. name,
  202. element,
  203. archetype,
  204. base_stars,
  205. natural_stars,
  206. obtainable,
  207. can_awaken,
  208. awaken_bonus,
  209. skill_ups_to_max,
  210. leader_skill,
  211. hp,
  212. attack,
  213. defense,
  214. speed,
  215. crit_rate,
  216. crit_damage,
  217. resistance,
  218. accuracy,
  219. raw_hp,
  220. raw_attack,
  221. raw_defense,
  222. max_lvl_hp,
  223. max_lvl_attack,
  224. max_lvl_defense,
  225. awakens_from,
  226. awakens_to,
  227. fusion_food,
  228. homunculus,
  229. craft_cost
  230. FROM monster
  231. WHERE id = :id;
  232. ");
  233. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  234. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  235. if ($r){
  236. $this->family = $r["family"];
  237. $this->name = HTML::e($r["name"]);
  238. $this->element = $r["element"];
  239. switch($this->element){
  240. case ELEMENT_ID::WATER:
  241. $this->element_name = "Water";
  242. break;
  243. case ELEMENT_ID::FIRE:
  244. $this->element_name = "Fire";
  245. break;
  246. case ELEMENT_ID::WIND:
  247. $this->element_name = "Wind";
  248. break;
  249. case ELEMENT_ID::LIGHT:
  250. $this->element_name = "Light";
  251. break;
  252. case ELEMENT_ID::DARK:
  253. $this->element_name = "Dark";
  254. break;
  255. }
  256. $this->archetype = $r["archetype"];
  257. switch($this->archetype){
  258. case ARCHETYPE_ID::ATTACK:
  259. $this->archetype_name = "Attack";
  260. break;
  261. case ARCHETYPE_ID::DEFENSE:
  262. $this->archetype_name = "Defense";
  263. break;
  264. case ARCHETYPE_ID::HP:
  265. $this->archetype_name = "HP";
  266. break;
  267. case ARCHETYPE_ID::SUPPORT:
  268. $this->archetype_name = "Support";
  269. break;
  270. case ARCHETYPE_ID::MATERIAL:
  271. $this->archetype_name = "Material";
  272. break;
  273. }
  274. $this->base_stars = $r["base_stars"];
  275. $this->natural_stars = $r["natural_stars"];
  276. $this->obtainable = $r["obtainable"];
  277. $this->can_awaken = $r["can_awaken"];
  278. $this->awaken_bonus = HTML::e($r["awaken_bonus"]);
  279. $this->skill_ups_to_max = $r["skill_ups_to_max"];
  280. if (strlen($r["leader_skill"]) > 0){
  281. $this->leader_skill = new Leader_Skill($r["leader_skill"]);
  282. }
  283. $this->hp = $r["hp"];
  284. $this->attack = $r["attack"];
  285. $this->defense = $r["defense"];
  286. $this->speed = $r["speed"];
  287. $this->crit_rate = $r["crit_rate"];
  288. $this->crit_damage = $r["crit_damage"];
  289. $this->resistance = $r["resistance"];
  290. $this->accuracy = $r["accuracy"];
  291. $this->raw_hp = $r["raw_hp"];
  292. $this->raw_attack = $r["raw_attack"];
  293. $this->raw_defense = $r["raw_defense"];
  294. $this->max_lvl_hp = $r["max_lvl_hp"];
  295. $this->max_lvl_attack = $r["max_lvl_attack"];
  296. $this->max_lvl_defense = $r["max_lvl_defense"];
  297. $this->awakens_from = $r["awakens_from"];
  298. $this->awakens_to = $r["awakens_to"];
  299. $this->fusion_food = $r["fusion_food"];
  300. $this->homunculus = $r["homunculus"];
  301. $this->craft_cost = $r["craft_cost"];
  302. }
  303. if ($complete){
  304. $this->load_essences();
  305. $this->load_skills();
  306. $this->load_sources();
  307. $this->load_transformation();
  308. }
  309. // Guess monster name
  310. if ($this->awakens_from == "" && $this->awakens_to == ""){
  311. // No to, no from. Silver monster.
  312. if ($this->base_stars >= 4){
  313. // SFV Collab event monsters, always awakened.
  314. $this->title = $this->name;
  315. }
  316. elseif($this->archetype == ARCHETYPE_ID::MATERIAL && $this->element == ELEMENT_ID::LIGHT){
  317. // Rainbowmon, always awakened
  318. $this->title = $this->name;
  319. }
  320. elseif($this->archetype == ARCHETYPE_ID::MATERIAL && $this->element == ELEMENT_ID::DARK){
  321. // Devilmon, always unawakened, but dont include element
  322. $this->title = $this->name;
  323. }
  324. else{
  325. // Unawakeable monster
  326. $this->title = $this->element_name . " " . $this->name;
  327. }
  328. }
  329. // Awakened
  330. elseif ($this->awakens_from != ""){
  331. $this->title = $this->name;
  332. }
  333. // Gold
  334. elseif (strlen($this->awakens_to) > 0){
  335. $this->title = $this->element_name . " " . $this->name;
  336. }
  337. }
  338. /**
  339. * Loads the information about the essences to awake the monster.
  340. *
  341. * It is autoatically called at construction time if the
  342. * $complete parameter is true (by default). No point in calling it
  343. * twice.
  344. */
  345. public function load_essences(){
  346. $this->essences = [];
  347. $statement = get_context()->get_db()->prepare("
  348. SELECT
  349. item,
  350. amount
  351. FROM monster_essence
  352. WHERE
  353. monster = :monster
  354. ORDER BY item;
  355. ");
  356. $statement->bindValue(':monster', $this->id, SQLITE3_TEXT);
  357. $q = $statement->execute();
  358. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  359. $e = [
  360. "item" => new Inventory($r["item"], INVENTORY_TYPE_ID::ESSENCES),
  361. "amount" => $r["amount"]
  362. ];
  363. array_push($this->essences, $e);
  364. }
  365. }
  366. /**
  367. * Loads the monster transformation.
  368. *
  369. * It is automatically called at construction time if the
  370. * $complete parameter is true (by default). No point in calling it
  371. * twice.
  372. */
  373. public function load_transformation(){
  374. $this->transformation = null;
  375. $statement = get_context()->get_db()->prepare("
  376. SELECT id
  377. FROM monster_transformation
  378. WHERE monster = :monster;
  379. ");
  380. $statement->bindValue(':monster', $this->id, SQLITE3_TEXT);
  381. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  382. if ($r){
  383. $this->transformation = new Monster_Transformation($r["id"]);
  384. }
  385. }
  386. /**
  387. * Loads the information about the monster skills.
  388. *
  389. * It is autoatically called at construction time if the
  390. * $complete parameter is true (by default). No point in calling it
  391. * twice.
  392. */
  393. public function load_skills(){
  394. $this->skill = [];
  395. $statement = get_context()->get_db()->prepare("
  396. SELECT skill.id AS skill
  397. FROM
  398. skill,
  399. monster_skill
  400. WHERE
  401. skill.id = monster_skill.skill AND
  402. monster_skill.monster = :monster
  403. ORDER BY slot;
  404. ");
  405. $statement->bindValue(':monster', $this->id, SQLITE3_TEXT);
  406. $q = $statement->execute();
  407. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  408. array_push($this->skill, new Skill($r["skill"]));
  409. }
  410. }
  411. /**
  412. * Loads the information about the monster sources.
  413. *
  414. * It is autoatically called at construction time if the
  415. * $complete parameter is true (by default). No point in calling it
  416. * twice.
  417. */
  418. public function load_sources(){
  419. $this->sources = [];
  420. $statement = get_context()->get_db()->prepare("
  421. SELECT source
  422. FROM monster_source
  423. WHERE monster = :monster;
  424. ");
  425. $statement->bindValue(':monster', $this->id, SQLITE3_TEXT);
  426. $q = $statement->execute();
  427. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  428. array_push($this->sources, new Source($r["source"]));
  429. }
  430. }
  431. /**
  432. * Gets the path to the monster image. The image must be in the
  433. * img/monster/ directory, and it's name must be the monster id,
  434. * padded with '0' to 8 digits, and the extension must be '.png'.
  435. *
  436. * @return string Image URL, or a fixed unknown image.
  437. */
  438. public function get_image(){
  439. return APPLICATION::img("UNIT", $this->id);
  440. }
  441. }
  442. ?>