homunculus.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "homunculus.hpp"
  4. #include <stdlib.h>
  5. #include "../common/cbasetypes.h"
  6. #include "../common/malloc.h"
  7. #include "../common/timer.h"
  8. #include "../common/nullpo.h"
  9. #include "../common/mmo.h"
  10. #include "../common/random.h"
  11. #include "../common/showmsg.h"
  12. #include "../common/strlib.h"
  13. #include "../common/utils.h"
  14. #include "log.hpp"
  15. #include "clif.hpp"
  16. #include "intif.hpp"
  17. #include "itemdb.hpp"
  18. #include "pc.hpp"
  19. #include "party.hpp"
  20. #include "trade.hpp"
  21. #include "npc.hpp"
  22. #include "battle.hpp"
  23. struct s_homunculus_db homunculus_db[MAX_HOMUNCULUS_CLASS]; //[orn]
  24. struct homun_skill_tree_entry hskill_tree[MAX_HOMUNCULUS_CLASS][MAX_HOM_SKILL_TREE];
  25. static int hom_hungry(int tid, unsigned int tick, int id, intptr_t data);
  26. static uint16 homunculus_count;
  27. static unsigned int hexptbl[MAX_LEVEL];
  28. //For holding the view data of npc classes. [Skotlex]
  29. static struct view_data hom_viewdb[MAX_HOMUNCULUS_CLASS];
  30. struct s_homun_intimacy_grade {
  31. //const char *grade;
  32. uint32 min_value;
  33. };
  34. /// Intimacy grade, order based on enum e_homun_grade
  35. static struct s_homun_intimacy_grade intimacy_grades[] = {
  36. { /*"Hate with passion",*/ 100 },
  37. { /*"Hate", */ 400 },
  38. { /*"Awkward", */ 1100 },
  39. { /*"Shy", */ 10100 },
  40. { /*"Neutral", */ 25100 },
  41. { /*"Cordial", */ 75100 },
  42. { /*"Loyal", */ 91100 },
  43. };
  44. /**
  45. * Check if the skill is a valid homunculus skill based skill range or availablity in skill db
  46. * @param skill_id
  47. * @return -1 if invalid skill or skill index for homunculus skill in s_homunculus::hskill
  48. */
  49. short hom_skill_get_index(uint16 skill_id) {
  50. if (!SKILL_CHK_HOMUN(skill_id))
  51. return -1;
  52. skill_id -= HM_SKILLBASE;
  53. if (skill_id >= MAX_HOMUNSKILL)
  54. return -1;
  55. return skill_id;
  56. }
  57. /**
  58. * Check homunculus class for array look up
  59. * @param class_
  60. * @return Class index or -1 if invalid class
  61. */
  62. static short hom_class2index(int class_) {
  63. if (homdb_checkid(class_))
  64. return class_ - HM_CLASS_BASE;
  65. return -1;
  66. }
  67. /**
  68. * Get homunculus view data
  69. * @param class_ Homunculus class
  70. * @return vd
  71. */
  72. struct view_data* hom_get_viewdata(int class_)
  73. { //Returns the viewdata for homunculus
  74. if (homdb_checkid(class_))
  75. return &hom_viewdb[class_-HM_CLASS_BASE];
  76. return NULL;
  77. }
  78. /**
  79. * Get homunculus type of specified class_
  80. * @param class_
  81. * @return enum homun_type
  82. */
  83. enum homun_type hom_class2type(int class_) {
  84. int mid = hom_class2mapid(class_);
  85. if((mid&(HOM_REG|HOM_EVO)) == (HOM_REG|HOM_EVO))
  86. return HT_EVO;
  87. else if(mid&(HOM_REG))
  88. return HT_REG;
  89. else if(mid&(HOM_S))
  90. return HT_S;
  91. else
  92. return HT_INVALID;
  93. }
  94. /**
  95. * Get homunculus MAPID from specified class
  96. * @param hom_class
  97. * @return Homunculus MAPID (see enum hom_mapid)
  98. */
  99. int hom_class2mapid(int hom_class)
  100. {
  101. switch(hom_class)
  102. {
  103. // Normal Homunculus
  104. case 6001: case 6005: return MAPID_LIF;
  105. case 6002: case 6006: return MAPID_AMISTR;
  106. case 6003: case 6007: return MAPID_FILIR;
  107. case 6004: case 6008: return MAPID_VANILMIRTH;
  108. // Evolved Homunculus
  109. case 6009: case 6013: return MAPID_LIF_E;
  110. case 6010: case 6014: return MAPID_AMISTR_E;
  111. case 6011: case 6015: return MAPID_FILIR_E;
  112. case 6012: case 6016: return MAPID_VANILMIRTH_E;
  113. // Homunculus S
  114. case 6048: return MAPID_EIRA;
  115. case 6049: return MAPID_BAYERI;
  116. case 6050: return MAPID_SERA;
  117. case 6051: return MAPID_DIETER;
  118. case 6052: return MAPID_ELANOR;
  119. default: return -1;
  120. }
  121. }
  122. /**
  123. * Add homunculus spirit ball
  124. * @param hd
  125. * @param max Maximum number of spirit ball
  126. */
  127. void hom_addspiritball(TBL_HOM *hd, int max) {
  128. nullpo_retv(hd);
  129. if (max > MAX_SPIRITBALL)
  130. max = MAX_SPIRITBALL;
  131. if (hd->homunculus.spiritball < 0)
  132. hd->homunculus.spiritball = 0;
  133. if (hd->homunculus.spiritball && hd->homunculus.spiritball >= max)
  134. hd->homunculus.spiritball = max;
  135. else
  136. hd->homunculus.spiritball++;
  137. clif_spiritball(&hd->bl);
  138. }
  139. /**
  140. * Delete homunculus spirit ball
  141. * @param hd
  142. * @param count Number spirit ball will be deleted
  143. * @param type 1 - Update client
  144. */
  145. void hom_delspiritball(TBL_HOM *hd, int count, int type) {
  146. nullpo_retv(hd);
  147. if (hd->homunculus.spiritball <= 0) {
  148. hd->homunculus.spiritball = 0;
  149. return;
  150. }
  151. if (count <= 0)
  152. return;
  153. if (count > MAX_SPIRITBALL)
  154. count = MAX_SPIRITBALL;
  155. if (count > hd->homunculus.spiritball)
  156. count = hd->homunculus.spiritball;
  157. hd->homunculus.spiritball -= count;
  158. if (!type)
  159. clif_spiritball(&hd->bl);
  160. }
  161. /**
  162. * Update homunculus info to its master after receiving damage
  163. * @param hd
  164. */
  165. void hom_damage(struct homun_data *hd) {
  166. if (hd->master)
  167. clif_hominfo(hd->master,hd,0);
  168. }
  169. /**
  170. * Set homunculus's dead status
  171. * @param hd
  172. * @return flag &1 - Standard dead, &2 - Remove object from map, &4 - Delete object from memory
  173. */
  174. int hom_dead(struct homun_data *hd)
  175. {
  176. //There's no intimacy penalties on death (from Tharis)
  177. struct map_session_data *sd = hd->master;
  178. clif_emotion(&hd->bl, E_WAH);
  179. //Delete timers when dead.
  180. hom_hungry_timer_delete(hd);
  181. hd->homunculus.hp = 0;
  182. if (!sd) //unit remove map will invoke unit free
  183. return 3;
  184. clif_emotion(&sd->bl, E_SOB);
  185. //Remove from map (if it has no intimacy, it is auto-removed from memory)
  186. return 3;
  187. }
  188. /**
  189. * Vaporize a character's homunculus
  190. * @param sd
  191. * @param flag 1: then HP needs to be 80% or above. 2: then set to morph state.
  192. */
  193. int hom_vaporize(struct map_session_data *sd, int flag)
  194. {
  195. struct homun_data *hd;
  196. nullpo_ret(sd);
  197. hd = sd->hd;
  198. if (!hd || hd->homunculus.vaporize)
  199. return 0;
  200. if (status_isdead(&hd->bl))
  201. return 0; //Can't vaporize a dead homun.
  202. if (flag == HOM_ST_REST && get_percentage(hd->battle_status.hp, hd->battle_status.max_hp) < 80)
  203. return 0;
  204. hd->regen.state.block = 3; //Block regen while vaporized.
  205. //Delete timers when vaporized.
  206. hom_hungry_timer_delete(hd);
  207. hd->homunculus.vaporize = flag ? flag : HOM_ST_REST;
  208. if (battle_config.hom_setting&HOMSET_RESET_REUSESKILL_VAPORIZED)
  209. memset(hd->blockskill, 0, sizeof(hd->blockskill));
  210. clif_hominfo(sd, sd->hd, 0);
  211. hom_save(hd);
  212. return unit_remove_map(&hd->bl, CLR_OUTSIGHT);
  213. }
  214. /**
  215. * Delete a homunculus, completely "killing it".
  216. * Emote is the emotion the master should use, send negative to disable.
  217. * @param hd
  218. * @param emote
  219. */
  220. int hom_delete(struct homun_data *hd, int emote)
  221. {
  222. struct map_session_data *sd;
  223. nullpo_ret(hd);
  224. sd = hd->master;
  225. if (!sd)
  226. return unit_free(&hd->bl,CLR_DEAD);
  227. if (emote >= 0)
  228. clif_emotion(&sd->bl, emote);
  229. //This makes it be deleted right away.
  230. hd->homunculus.intimacy = 0;
  231. // Send homunculus_dead to client
  232. hd->homunculus.hp = 0;
  233. clif_hominfo(sd, hd, 0);
  234. return unit_remove_map(&hd->bl,CLR_OUTSIGHT);
  235. }
  236. /**
  237. * Calculates homunculus skill tree
  238. * @param hd
  239. * @param flag_envolve
  240. */
  241. void hom_calc_skilltree(struct homun_data *hd, bool flag_evolve) {
  242. uint8 i;
  243. short c = 0;
  244. nullpo_retv(hd);
  245. /* load previous homunculus form skills first. */
  246. if (hd->homunculus.prev_class != 0 && (c = hom_class2index(hd->homunculus.prev_class)) >= 0) {
  247. for (i = 0; i < MAX_HOM_SKILL_TREE; i++) {
  248. uint16 skill_id;
  249. short idx = -1;
  250. bool fail = false;
  251. if (!(skill_id = hskill_tree[c][i].id) || (idx = hom_skill_get_index(skill_id)) == -1)
  252. continue;
  253. if (hd->homunculus.hskill[idx].id)
  254. continue; //Skill already known.
  255. if (!battle_config.skillfree) {
  256. uint8 j;
  257. if (hskill_tree[c][i].need_level > hd->homunculus.level)
  258. continue;
  259. for (j = 0; j < MAX_HOM_SKILL_REQUIRE; j++) {
  260. if (hskill_tree[c][i].need[j].id &&
  261. hom_checkskill(hd,hskill_tree[c][i].need[j].id) < hskill_tree[c][i].need[j].lv)
  262. {
  263. fail = true;
  264. break;
  265. }
  266. }
  267. }
  268. if (!fail)
  269. hd->homunculus.hskill[idx].id = skill_id;
  270. }
  271. }
  272. if ((c = hom_class2index(hd->homunculus.class_)) < 0)
  273. return;
  274. for (i = 0; i < MAX_HOM_SKILL_TREE; i++) {
  275. unsigned int intimacy = 0;
  276. uint16 skill_id;
  277. short idx = -1;
  278. bool fail = false;
  279. if (!(skill_id = hskill_tree[c][i].id) || (idx = hom_skill_get_index(skill_id)) == -1)
  280. continue;
  281. if (hd->homunculus.hskill[idx].id)
  282. continue; //Skill already known.
  283. intimacy = (flag_evolve) ? 10 : hd->homunculus.intimacy;
  284. if (intimacy < hskill_tree[c][i].intimacy * 100)
  285. continue;
  286. if (!battle_config.skillfree) {
  287. uint8 j;
  288. if (hskill_tree[c][i].need_level > hd->homunculus.level)
  289. continue;
  290. for (j = 0; j < MAX_HOM_SKILL_REQUIRE; j++) {
  291. if (hskill_tree[c][i].need[j].id &&
  292. hom_checkskill(hd,hskill_tree[c][i].need[j].id) < hskill_tree[c][i].need[j].lv)
  293. {
  294. fail = true;
  295. break;
  296. }
  297. }
  298. }
  299. if (!fail)
  300. hd->homunculus.hskill[idx].id = skill_id;
  301. }
  302. if (hd->master)
  303. clif_homskillinfoblock(hd->master);
  304. }
  305. /**
  306. * Check skill from homunculus
  307. * @param hd
  308. * @param skill_id
  309. * @return Skill Level or 0 if invalid or unlearned skill
  310. */
  311. short hom_checkskill(struct homun_data *hd,uint16 skill_id)
  312. {
  313. short idx = hom_skill_get_index(skill_id);
  314. if (idx < 0) // Invalid skill
  315. return 0;
  316. if (!hd || !&hd->homunculus)
  317. return 0;
  318. if (hd->homunculus.hskill[idx].id == skill_id)
  319. return (hd->homunculus.hskill[idx].lv);
  320. return 0;
  321. }
  322. /**
  323. * Get max level for homunculus skill
  324. * @param id Skill ID
  325. * @param b_class
  326. * @return Skill Level
  327. */
  328. int hom_skill_tree_get_max(int skill_id, int b_class){
  329. uint8 i;
  330. if ((b_class = hom_class2index(b_class)) < 0)
  331. return 0;
  332. ARR_FIND(0, MAX_HOM_SKILL_TREE, i, hskill_tree[b_class][i].id == skill_id);
  333. if (i < MAX_HOM_SKILL_TREE)
  334. return hskill_tree[b_class][i].max;
  335. return skill_get_max(skill_id);
  336. }
  337. /**
  338. * Get required minimum level to learn the skill
  339. * @param class_ Homunculus class
  340. * @param skill_id Homunculus skill ID
  341. * @return Level required or 0 if invalid
  342. **/
  343. uint8 hom_skill_get_min_level(int class_, uint16 skill_id) {
  344. short class_idx = hom_class2index(class_), skill_idx = -1;
  345. uint8 i;
  346. if (class_idx == -1 || (skill_idx = hom_skill_get_index(skill_id)) == -1)
  347. return 0;
  348. ARR_FIND(0, MAX_HOM_SKILL_REQUIRE, i, hskill_tree[class_idx][i].id == skill_id);
  349. if (i == MAX_HOM_SKILL_REQUIRE)
  350. return 0;
  351. return hskill_tree[class_idx][i].need_level;
  352. }
  353. /**
  354. * Level up an homunculus skill
  355. * @param hd
  356. * @param skill_id
  357. */
  358. void hom_skillup(struct homun_data *hd, uint16 skill_id)
  359. {
  360. short idx = 0;
  361. nullpo_retv(hd);
  362. if (hd->homunculus.vaporize)
  363. return;
  364. if ((idx = hom_skill_get_index(skill_id)) < 0)
  365. return;
  366. if (hd->homunculus.skillpts > 0 &&
  367. hd->homunculus.hskill[idx].id &&
  368. hd->homunculus.hskill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  369. hd->homunculus.level >= hom_skill_get_min_level(hd->homunculus.class_, skill_id) &&
  370. hd->homunculus.hskill[idx].lv < hom_skill_tree_get_max(skill_id, hd->homunculus.class_)
  371. )
  372. {
  373. hd->homunculus.hskill[idx].lv++;
  374. hd->homunculus.skillpts-- ;
  375. status_calc_homunculus(hd, SCO_NONE);
  376. if (hd->master) {
  377. clif_homskillup(hd->master, skill_id);
  378. clif_hominfo(hd->master,hd,0);
  379. clif_homskillinfoblock(hd->master);
  380. }
  381. }
  382. }
  383. /**
  384. * Homunculus leveled up
  385. * @param hd
  386. */
  387. int hom_levelup(struct homun_data *hd)
  388. {
  389. struct s_homunculus *hom;
  390. struct h_stats *min = NULL, *max = NULL;
  391. int growth_str, growth_agi, growth_vit, growth_int, growth_dex, growth_luk ;
  392. int growth_max_hp, growth_max_sp ;
  393. int m_class;
  394. if ((m_class = hom_class2mapid(hd->homunculus.class_)) == -1) {
  395. ShowError("hom_levelup: Invalid class %d. \n", hd->homunculus.class_);
  396. return 0;
  397. }
  398. /// When homunculus is homunculus S, we check to see if we need to apply previous class stats
  399. if(m_class&HOM_S && hd->homunculus.level < battle_config.hom_S_growth_level) {
  400. int i;
  401. if (!hd->homunculus.prev_class) {
  402. /// We also need to be sure that the previous class exists, otherwise give it something to work with
  403. hd->homunculus.prev_class = 6001;
  404. }
  405. // Give the homunculus the level up stats database it needs
  406. i = hom_search(hd->homunculus.prev_class,HOMUNCULUS_CLASS);
  407. if (i < 0) // Nothing should go wrong here, but check anyways
  408. return 0;
  409. max = &homunculus_db[i].gmax;
  410. min = &homunculus_db[i].gmin;
  411. }
  412. if (((m_class&HOM_REG) && hd->homunculus.level >= battle_config.hom_max_level)
  413. || ((m_class&HOM_S) && hd->homunculus.level >= battle_config.hom_S_max_level)
  414. || !hd->exp_next || hd->homunculus.exp < hd->exp_next)
  415. return 0;
  416. hom = &hd->homunculus;
  417. hom->level++ ;
  418. if (!(hom->level % 3))
  419. hom->skillpts++ ; //1 skillpoint each 3 base level
  420. hom->exp -= hd->exp_next ;
  421. hd->exp_next = hexptbl[hom->level - 1] ;
  422. if (!max) {
  423. max = &hd->homunculusDB->gmax;
  424. min = &hd->homunculusDB->gmin;
  425. }
  426. growth_max_hp = rnd_value(min->HP, max->HP);
  427. growth_max_sp = rnd_value(min->SP, max->SP);
  428. growth_str = rnd_value(min->str, max->str);
  429. growth_agi = rnd_value(min->agi, max->agi);
  430. growth_vit = rnd_value(min->vit, max->vit);
  431. growth_dex = rnd_value(min->dex, max->dex);
  432. growth_int = rnd_value(min->int_,max->int_);
  433. growth_luk = rnd_value(min->luk, max->luk);
  434. //Aegis discards the decimals in the stat growth values!
  435. growth_str-=growth_str%10;
  436. growth_agi-=growth_agi%10;
  437. growth_vit-=growth_vit%10;
  438. growth_dex-=growth_dex%10;
  439. growth_int-=growth_int%10;
  440. growth_luk-=growth_luk%10;
  441. hom->max_hp += growth_max_hp;
  442. hom->max_sp += growth_max_sp;
  443. hom->str += growth_str;
  444. hom->agi += growth_agi;
  445. hom->vit += growth_vit;
  446. hom->dex += growth_dex;
  447. hom->int_+= growth_int;
  448. hom->luk += growth_luk;
  449. APPLY_HOMUN_LEVEL_STATWEIGHT();
  450. // Needed to update skill list for mutated homunculus so unlocked skills will appear when the needed level is reached.
  451. status_calc_homunculus(hd,SCO_NONE);
  452. clif_hominfo(hd->master,hd,0);
  453. clif_homskillinfoblock(hd->master);
  454. if ( hd->master && battle_config.homunculus_show_growth ) {
  455. char output[256] ;
  456. sprintf(output,
  457. "Growth: hp:%d sp:%d str(%.2f) agi(%.2f) vit(%.2f) int(%.2f) dex(%.2f) luk(%.2f) ",
  458. growth_max_hp, growth_max_sp,
  459. growth_str/10.0, growth_agi/10.0, growth_vit/10.0,
  460. growth_int/10.0, growth_dex/10.0, growth_luk/10.0);
  461. clif_messagecolor(&hd->master->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  462. }
  463. return 1;
  464. }
  465. /**
  466. * Changes homunculus class
  467. * @param hd
  468. * @param class_ old class
  469. * @reutrn Fals if the class cannot be changed, True if otherwise
  470. */
  471. static bool hom_change_class(struct homun_data *hd, short class_) {
  472. int i;
  473. i = hom_search(class_,HOMUNCULUS_CLASS);
  474. if (i < 0)
  475. return false;
  476. hd->homunculusDB = &homunculus_db[i];
  477. hd->homunculus.class_ = class_;
  478. status_set_viewdata(&hd->bl, class_);
  479. hom_calc_skilltree(hd, 1);
  480. return true;
  481. }
  482. /**
  483. * Make an homonculus evolve, (changing in evolution class and apply bonus)
  484. * @param hd : homonculus datas
  485. * @return 0:failure, 1:success
  486. */
  487. int hom_evolution(struct homun_data *hd)
  488. {
  489. struct s_homunculus *hom;
  490. struct h_stats *max, *min;
  491. struct map_session_data *sd;
  492. nullpo_ret(hd);
  493. if(!hd->homunculusDB->evo_class || hd->homunculus.class_ == hd->homunculusDB->evo_class) {
  494. clif_emotion(&hd->bl, E_SWT);
  495. return 0 ;
  496. }
  497. sd = hd->master;
  498. if (!sd)
  499. return 0;
  500. if (!hom_change_class(hd, hd->homunculusDB->evo_class)) {
  501. ShowError("hom_evolution: Can't evolve homunc from %d to %d", hd->homunculus.class_, hd->homunculusDB->evo_class);
  502. return 0;
  503. }
  504. //Apply evolution bonuses
  505. hom = &hd->homunculus;
  506. max = &hd->homunculusDB->emax;
  507. min = &hd->homunculusDB->emin;
  508. hom->max_hp += rnd_value(min->HP, max->HP);
  509. hom->max_sp += rnd_value(min->SP, max->SP);
  510. hom->str += 10*rnd_value(min->str, max->str);
  511. hom->agi += 10*rnd_value(min->agi, max->agi);
  512. hom->vit += 10*rnd_value(min->vit, max->vit);
  513. hom->int_+= 10*rnd_value(min->int_,max->int_);
  514. hom->dex += 10*rnd_value(min->dex, max->dex);
  515. hom->luk += 10*rnd_value(min->luk, max->luk);
  516. hom->intimacy = battle_config.homunculus_evo_intimacy_reset;
  517. unit_remove_map(&hd->bl, CLR_OUTSIGHT);
  518. if (map_addblock(&hd->bl))
  519. return 0;
  520. clif_spawn(&hd->bl);
  521. clif_emotion(&sd->bl, E_NO1);
  522. clif_specialeffect(&hd->bl,EF_HO_UP,AREA);
  523. //status_Calc flag&1 will make current HP/SP be reloaded from hom structure
  524. hom->hp = hd->battle_status.hp;
  525. hom->sp = hd->battle_status.sp;
  526. status_calc_homunculus(hd, SCO_FIRST);
  527. if (!(battle_config.hom_setting&HOMSET_NO_INSTANT_LAND_SKILL))
  528. skill_unit_move(&sd->hd->bl,gettick(),1); // apply land skills immediately
  529. return 1 ;
  530. }
  531. /**
  532. * Make an homonculus mutate in renewal homon
  533. * @param hd : homonculus datas
  534. * @param homun_id : id to make it transform into (must be a valid homon class)
  535. * @return 0:failure, 1:sucess
  536. */
  537. int hom_mutate(struct homun_data *hd, int homun_id)
  538. {
  539. struct s_homunculus *hom;
  540. struct map_session_data *sd;
  541. int m_class, m_id, prev_class = 0;
  542. nullpo_ret(hd);
  543. m_class = hom_class2mapid(hd->homunculus.class_);
  544. m_id = hom_class2mapid(homun_id);
  545. if( m_class == -1 || m_id == -1 || !(m_class&HOM_EVO) || !(m_id&HOM_S) ) {
  546. clif_emotion(&hd->bl, E_SWT);
  547. return 0;
  548. }
  549. sd = hd->master;
  550. if (!sd)
  551. return 0;
  552. prev_class = hd->homunculus.class_;
  553. if (!hom_change_class(hd, homun_id)) {
  554. ShowError("hom_mutate: Can't evolve homunc from %d to %d", hd->homunculus.class_, homun_id);
  555. return 0;
  556. }
  557. unit_remove_map(&hd->bl, CLR_OUTSIGHT);
  558. if(map_addblock(&hd->bl))
  559. return 0;
  560. clif_spawn(&hd->bl);
  561. clif_emotion(&sd->bl, E_NO1);
  562. clif_specialeffect(&hd->bl,EF_HO_UP,AREA);
  563. //status_Calc flag&1 will make current HP/SP be reloaded from hom structure
  564. hom = &hd->homunculus;
  565. hom->hp = hd->battle_status.hp;
  566. hom->sp = hd->battle_status.sp;
  567. hom->prev_class = prev_class;
  568. status_calc_homunculus(hd, SCO_FIRST);
  569. if (!(battle_config.hom_setting&HOMSET_NO_INSTANT_LAND_SKILL))
  570. skill_unit_move(&sd->hd->bl,gettick(),1); // apply land skills immediately
  571. return 1;
  572. }
  573. /**
  574. * Add homunculus exp
  575. * @param hd
  576. * @param exp Added EXP
  577. */
  578. void hom_gainexp(struct homun_data *hd,int exp)
  579. {
  580. int m_class;
  581. nullpo_retv(hd);
  582. if(hd->homunculus.vaporize)
  583. return;
  584. if((m_class = hom_class2mapid(hd->homunculus.class_)) == -1) {
  585. ShowError("hom_gainexp: Invalid class %d. \n", hd->homunculus.class_);
  586. return;
  587. }
  588. if( hd->exp_next == 0 ||
  589. ((m_class&HOM_REG) && hd->homunculus.level >= battle_config.hom_max_level) ||
  590. ((m_class&HOM_S) && hd->homunculus.level >= battle_config.hom_S_max_level) )
  591. {
  592. hd->homunculus.exp = 0;
  593. return;
  594. }
  595. hd->homunculus.exp += exp;
  596. if (hd->master && hd->homunculus.exp < hd->exp_next) {
  597. clif_hominfo(hd->master,hd,0);
  598. return;
  599. }
  600. // Do the levelup(s)
  601. while( hd->homunculus.exp > hd->exp_next ){
  602. // Max level reached or error
  603. if( !hom_levelup(hd) ){
  604. break;
  605. }
  606. }
  607. if( hd->exp_next == 0 )
  608. hd->homunculus.exp = 0 ;
  609. clif_specialeffect(&hd->bl,EF_HO_UP,AREA);
  610. status_calc_homunculus(hd, SCO_NONE);
  611. status_percent_heal(&hd->bl, 100, 100);
  612. }
  613. /**
  614. * Increase homunculu sintimacy
  615. * @param hd
  616. * @param value Added intimacy
  617. * @return New intimacy value
  618. */
  619. int hom_increase_intimacy(struct homun_data * hd, unsigned int value)
  620. {
  621. nullpo_ret(hd);
  622. if (battle_config.homunculus_friendly_rate != 100)
  623. value = (value * battle_config.homunculus_friendly_rate) / 100;
  624. if (hd->homunculus.intimacy + value <= 100000)
  625. hd->homunculus.intimacy += value;
  626. else
  627. hd->homunculus.intimacy = 100000;
  628. return hd->homunculus.intimacy;
  629. }
  630. /**
  631. * Decrease homunculu sintimacy
  632. * @param hd
  633. * @param value Reduced intimacy
  634. * @return New intimacy value
  635. */
  636. int hom_decrease_intimacy(struct homun_data * hd, unsigned int value)
  637. {
  638. nullpo_ret(hd);
  639. if (hd->homunculus.intimacy >= value)
  640. hd->homunculus.intimacy -= value;
  641. else
  642. hd->homunculus.intimacy = 0;
  643. return hd->homunculus.intimacy;
  644. }
  645. /**
  646. * Update homunculus info to master after healing
  647. * @param hd
  648. */
  649. void hom_heal(struct homun_data *hd) {
  650. if (hd->master)
  651. clif_hominfo(hd->master,hd,0);
  652. }
  653. /**
  654. * Save homunculus data
  655. * @param hd
  656. */
  657. void hom_save(struct homun_data *hd)
  658. {
  659. // copy data that must be saved in homunculus struct ( hp / sp )
  660. TBL_PC *sd;
  661. nullpo_retv(hd);
  662. sd = hd->master;
  663. //Do not check for max_hp/max_sp caps as current could be higher to max due
  664. //to status changes/skills (they will be capped as needed upon stat
  665. //calculation on login)
  666. hd->homunculus.hp = hd->battle_status.hp;
  667. hd->homunculus.sp = hd->battle_status.sp;
  668. intif_homunculus_requestsave(sd->status.account_id, &hd->homunculus);
  669. }
  670. /**
  671. * Perform requested action from selected homunculus menu
  672. * @param sd
  673. * @param type
  674. */
  675. void hom_menu(struct map_session_data *sd, int type)
  676. {
  677. nullpo_retv(sd);
  678. if (sd->hd == NULL)
  679. return;
  680. switch(type) {
  681. case 0:
  682. break;
  683. case 1:
  684. hom_food(sd, sd->hd);
  685. break;
  686. case 2:
  687. hom_delete(sd->hd, -1);
  688. break;
  689. default:
  690. ShowError("hom_menu : unknown menu choice : %d\n", type);
  691. break;
  692. }
  693. }
  694. /**
  695. * Feed homunculus
  696. * @param sd
  697. * @param hd
  698. */
  699. int hom_food(struct map_session_data *sd, struct homun_data *hd)
  700. {
  701. int i, foodID, emotion;
  702. nullpo_retr(1,sd);
  703. nullpo_retr(1,hd);
  704. if (hd->homunculus.vaporize)
  705. return 1;
  706. foodID = hd->homunculusDB->foodID;
  707. i = pc_search_inventory(sd,foodID);
  708. if (i < 0) {
  709. clif_hom_food(sd,foodID,0);
  710. return 1;
  711. }
  712. pc_delitem(sd,i,1,0,0,LOG_TYPE_CONSUME);
  713. if ( hd->homunculus.hunger >= 91 ) {
  714. hom_decrease_intimacy(hd, 50);
  715. emotion = E_WAH;
  716. } else if ( hd->homunculus.hunger >= 76 ) {
  717. hom_decrease_intimacy(hd, 5);
  718. emotion = E_SWT2;
  719. } else if ( hd->homunculus.hunger >= 26 ) {
  720. hom_increase_intimacy(hd, 75);
  721. emotion = E_HO;
  722. } else if ( hd->homunculus.hunger >= 11 ) {
  723. hom_increase_intimacy(hd, 100);
  724. emotion = E_HO;
  725. } else {
  726. hom_increase_intimacy(hd, 50);
  727. emotion = E_HO;
  728. }
  729. hd->homunculus.hunger += 10; //dunno increase value for each food
  730. if(hd->homunculus.hunger > 100)
  731. hd->homunculus.hunger = 100;
  732. log_feeding(sd, LOG_FEED_HOMUNCULUS, foodID);
  733. clif_emotion(&hd->bl,emotion);
  734. clif_send_homdata(sd,SP_HUNGRY,hd->homunculus.hunger);
  735. clif_send_homdata(sd,SP_INTIMATE,hd->homunculus.intimacy / 100);
  736. clif_hom_food(sd,foodID,1);
  737. // Too much food :/
  738. if(hd->homunculus.intimacy == 0)
  739. return hom_delete(sd->hd, E_OMG);
  740. return 0;
  741. }
  742. /**
  743. * Timer to reduce hunger level
  744. */
  745. static int hom_hungry(int tid, unsigned int tick, int id, intptr_t data)
  746. {
  747. struct map_session_data *sd;
  748. struct homun_data *hd;
  749. sd = map_id2sd(id);
  750. if (!sd)
  751. return 1;
  752. if (!sd->status.hom_id || !(hd=sd->hd))
  753. return 1;
  754. if (hd->hungry_timer != tid) {
  755. ShowError("hom_hungry_timer %d != %d\n",hd->hungry_timer,tid);
  756. return 0;
  757. }
  758. hd->hungry_timer = INVALID_TIMER;
  759. hd->homunculus.hunger--;
  760. if(hd->homunculus.hunger <= 10) {
  761. clif_emotion(&hd->bl, E_AN);
  762. } else if(hd->homunculus.hunger == 25) {
  763. clif_emotion(&hd->bl, E_HMM);
  764. } else if(hd->homunculus.hunger == 75) {
  765. clif_emotion(&hd->bl, E_OK);
  766. }
  767. if (hd->homunculus.hunger < 0) {
  768. hd->homunculus.hunger = 0;
  769. // Delete the homunculus if intimacy <= 100
  770. if (!hom_decrease_intimacy(hd, 100))
  771. return hom_delete(hd, E_OMG);
  772. clif_send_homdata(sd,SP_INTIMATE,hd->homunculus.intimacy / 100);
  773. }
  774. clif_send_homdata(sd,SP_HUNGRY,hd->homunculus.hunger);
  775. hd->hungry_timer = add_timer(tick+hd->homunculusDB->hungryDelay,hom_hungry,sd->bl.id,0); //simple Fix albator
  776. return 0;
  777. }
  778. /**
  779. * Remove hungry timer from homunculus
  780. * @param hd
  781. */
  782. int hom_hungry_timer_delete(struct homun_data *hd)
  783. {
  784. nullpo_ret(hd);
  785. if (hd->hungry_timer != INVALID_TIMER) {
  786. delete_timer(hd->hungry_timer,hom_hungry);
  787. hd->hungry_timer = INVALID_TIMER;
  788. }
  789. return 1;
  790. }
  791. /**
  792. * Change homunculus name
  793. */
  794. int hom_change_name(struct map_session_data *sd,char *name)
  795. {
  796. int i;
  797. struct homun_data *hd;
  798. nullpo_retr(1, sd);
  799. hd = sd->hd;
  800. if (!hom_is_active(hd))
  801. return 1;
  802. if (hd->homunculus.rename_flag && !battle_config.hom_rename)
  803. return 1;
  804. for (i = 0; i < NAME_LENGTH && name[i];i++) {
  805. if (!(name[i]&0xe0) || name[i] == 0x7f)
  806. return 1;
  807. }
  808. return intif_rename_hom(sd, name);
  809. }
  810. /**
  811. * Acknowledge change name request from inter-server
  812. * @param sd
  813. * @param name
  814. * @param flag
  815. */
  816. void hom_change_name_ack(struct map_session_data *sd, char* name, int flag)
  817. {
  818. struct homun_data *hd = sd->hd;
  819. if (!hom_is_active(hd))
  820. return;
  821. normalize_name(name," ");//bugreport:3032
  822. if (!flag || name[0] == '\0') {
  823. clif_displaymessage(sd->fd, msg_txt(sd,280)); // You cannot use this name
  824. return;
  825. }
  826. safestrncpy(hd->homunculus.name,name,NAME_LENGTH);
  827. clif_name_area(&hd->bl);
  828. hd->homunculus.rename_flag = 1;
  829. clif_hominfo(sd,hd,0);
  830. }
  831. /**
  832. * Search homunculus info (food or next class)
  833. * @param key
  834. * @param type see enum e_hom_search_type
  835. * @return info found
  836. */
  837. int hom_search(int key, int type)
  838. {
  839. int i;
  840. for (i = 0; i < homunculus_count; i++) {
  841. if (homunculus_db[i].base_class <= 0)
  842. continue;
  843. switch (type) {
  844. case HOMUNCULUS_CLASS:
  845. if (homunculus_db[i].base_class == key ||
  846. homunculus_db[i].evo_class == key)
  847. return i;
  848. break;
  849. case HOMUNCULUS_FOOD:
  850. if (homunculus_db[i].foodID == key)
  851. return i;
  852. break;
  853. default:
  854. return -1;
  855. }
  856. }
  857. return -1;
  858. }
  859. /**
  860. * Create homunc structure
  861. * @param sd
  862. * @param hom
  863. */
  864. void hom_alloc(struct map_session_data *sd, struct s_homunculus *hom)
  865. {
  866. struct homun_data *hd;
  867. int i = 0;
  868. nullpo_retv(sd);
  869. Assert((sd->status.hom_id == 0 || sd->hd == 0) || sd->hd->master == sd);
  870. i = hom_search(hom->class_,HOMUNCULUS_CLASS);
  871. if(i < 0) {
  872. ShowError("hom_alloc: unknown class [%d] for homunculus '%s', requesting deletion.\n", hom->class_, hom->name);
  873. sd->status.hom_id = 0;
  874. intif_homunculus_requestdelete(hom->hom_id);
  875. return;
  876. }
  877. sd->hd = hd = (struct homun_data*)aCalloc(1,sizeof(struct homun_data));
  878. hd->bl.type = BL_HOM;
  879. hd->bl.id = npc_get_new_npc_id();
  880. hd->master = sd;
  881. hd->homunculusDB = &homunculus_db[i];
  882. memcpy(&hd->homunculus, hom, sizeof(struct s_homunculus));
  883. hd->exp_next = hexptbl[hd->homunculus.level - 1];
  884. status_set_viewdata(&hd->bl, hd->homunculus.class_);
  885. status_change_init(&hd->bl);
  886. unit_dataset(&hd->bl);
  887. hd->ud.dir = sd->ud.dir;
  888. // Find a random valid pos around the player
  889. hd->bl.m = sd->bl.m;
  890. hd->bl.x = sd->bl.x;
  891. hd->bl.y = sd->bl.y;
  892. unit_calc_pos(&hd->bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  893. hd->bl.x = hd->ud.to_x;
  894. hd->bl.y = hd->ud.to_y;
  895. map_addiddb(&hd->bl);
  896. status_calc_homunculus(hd, SCO_FIRST);
  897. status_percent_heal(&hd->bl, 100, 100);
  898. hd->hungry_timer = INVALID_TIMER;
  899. hd->masterteleport_timer = INVALID_TIMER;
  900. }
  901. /**
  902. * Init homunculus timers
  903. * @param hd
  904. */
  905. void hom_init_timers(struct homun_data * hd)
  906. {
  907. if (hd->hungry_timer == INVALID_TIMER)
  908. hd->hungry_timer = add_timer(gettick()+hd->homunculusDB->hungryDelay,hom_hungry,hd->master->bl.id,0);
  909. hd->regen.state.block = 0; //Restore HP/SP block.
  910. hd->masterteleport_timer = INVALID_TIMER;
  911. }
  912. /**
  913. * Make a player spawn a homonculus (call)
  914. * @param sd
  915. * @return False:failure, True:sucess
  916. */
  917. bool hom_call(struct map_session_data *sd)
  918. {
  919. struct homun_data *hd;
  920. if (!sd->status.hom_id) //Create a new homun.
  921. return hom_create_request(sd, HM_CLASS_BASE + rnd_value(0, 7)) ;
  922. // If homunc not yet loaded, load it
  923. if (!sd->hd)
  924. return intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id) > 0;
  925. hd = sd->hd;
  926. if (!hd->homunculus.vaporize)
  927. return false; //Can't use this if homun wasn't vaporized.
  928. if (hd->homunculus.vaporize == HOM_ST_MORPH)
  929. return false; // Can't call homunculus (morph state).
  930. hom_init_timers(hd);
  931. hd->homunculus.vaporize = HOM_ST_ACTIVE;
  932. if (hd->bl.prev == NULL)
  933. { //Spawn him
  934. hd->bl.x = sd->bl.x;
  935. hd->bl.y = sd->bl.y;
  936. hd->bl.m = sd->bl.m;
  937. if(map_addblock(&hd->bl))
  938. return false;
  939. clif_spawn(&hd->bl);
  940. clif_send_homdata(sd,SP_ACK,0);
  941. clif_hominfo(sd,hd,1);
  942. clif_hominfo(sd,hd,0); // send this x2. dunno why, but kRO does that [blackhole89]
  943. clif_homskillinfoblock(sd);
  944. if (battle_config.slaves_inherit_speed&1)
  945. status_calc_bl(&hd->bl, SCB_SPEED);
  946. hom_save(hd);
  947. } else
  948. //Warp him to master.
  949. unit_warp(&hd->bl,sd->bl.m, sd->bl.x, sd->bl.y,CLR_OUTSIGHT);
  950. return true;
  951. }
  952. /**
  953. * Receive homunculus data from char server
  954. * @param account_id : owner account_id of the homon
  955. * @param sh : homonculus data from char-serv
  956. * @param flag : does the creation in inter-serv was a success (0:no,1:yes)
  957. * @return 0:failure, 1:sucess
  958. */
  959. int hom_recv_data(uint32 account_id, struct s_homunculus *sh, int flag)
  960. {
  961. struct map_session_data *sd;
  962. struct homun_data *hd;
  963. sd = map_id2sd(account_id);
  964. if(!sd)
  965. return 0;
  966. if (sd->status.char_id != sh->char_id)
  967. {
  968. if (sd->status.hom_id == sh->hom_id)
  969. sh->char_id = sd->status.char_id; //Correct char id.
  970. else
  971. return 0;
  972. }
  973. if(!flag) { // Failed to load
  974. sd->status.hom_id = 0;
  975. return 0;
  976. }
  977. if (!sd->status.hom_id) //Hom just created.
  978. sd->status.hom_id = sh->hom_id;
  979. if (sd->hd) //uh? Overwrite the data.
  980. memcpy(&sd->hd->homunculus, sh, sizeof(struct s_homunculus));
  981. else
  982. hom_alloc(sd, sh);
  983. hd = sd->hd;
  984. if(hd && hd->homunculus.hp && !hd->homunculus.vaporize && hd->bl.prev == NULL && sd->bl.prev != NULL)
  985. {
  986. if(map_addblock(&hd->bl))
  987. return 0;
  988. clif_spawn(&hd->bl);
  989. clif_send_homdata(sd,SP_ACK,0);
  990. clif_hominfo(sd,hd,1);
  991. clif_hominfo(sd,hd,0); // send this x2. dunno why, but kRO does that [blackhole89]
  992. clif_homskillinfoblock(sd);
  993. hom_init_timers(hd);
  994. }
  995. return 1;
  996. }
  997. /**
  998. * Ask homunculus creation to char-server
  999. * @param sd
  1000. * @param class_
  1001. * @return True:Success; False:Failed
  1002. */
  1003. bool hom_create_request(struct map_session_data *sd, int class_)
  1004. {
  1005. struct s_homunculus homun;
  1006. struct h_stats *base;
  1007. int i;
  1008. nullpo_ret(sd);
  1009. i = hom_search(class_,HOMUNCULUS_CLASS);
  1010. if(i < 0)
  1011. return false;
  1012. memset(&homun, 0, sizeof(struct s_homunculus));
  1013. //Initial data
  1014. safestrncpy(homun.name, homunculus_db[i].name, NAME_LENGTH-1);
  1015. homun.class_ = class_;
  1016. homun.level = 1;
  1017. homun.hunger = 32; //32%
  1018. homun.intimacy = 2100; //21/1000
  1019. homun.char_id = sd->status.char_id;
  1020. homun.hp = 10 ;
  1021. base = &homunculus_db[i].base;
  1022. homun.max_hp = base->HP;
  1023. homun.max_sp = base->SP;
  1024. homun.str = base->str *10;
  1025. homun.agi = base->agi *10;
  1026. homun.vit = base->vit *10;
  1027. homun.int_= base->int_*10;
  1028. homun.dex = base->dex *10;
  1029. homun.luk = base->luk *10;
  1030. // Request homunculus creation
  1031. intif_homunculus_create(sd->status.account_id, &homun);
  1032. return true;
  1033. }
  1034. /**
  1035. * Make a player resurect an homon (player must have one)
  1036. * @param sd : player pointer
  1037. * @param per : hp percentage to revive homon
  1038. * @param x : x map coordinate
  1039. * @param y : Y map coordinate
  1040. * @return 0:failure, 1:success
  1041. */
  1042. int hom_ressurect(struct map_session_data* sd, unsigned char per, short x, short y)
  1043. {
  1044. struct homun_data* hd;
  1045. nullpo_ret(sd);
  1046. if (!sd->status.hom_id)
  1047. return 0; // no homunculus
  1048. if (!sd->hd) //Load homun data;
  1049. return intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1050. hd = sd->hd;
  1051. if (hd->homunculus.vaporize == HOM_ST_REST)
  1052. return 0; // vaporized homunculi need to be 'called'
  1053. if (!status_isdead(&hd->bl))
  1054. return 0; // already alive
  1055. hom_init_timers(hd);
  1056. if (!hd->bl.prev)
  1057. { //Add it back to the map.
  1058. hd->bl.m = sd->bl.m;
  1059. hd->bl.x = x;
  1060. hd->bl.y = y;
  1061. if(map_addblock(&hd->bl))
  1062. return 0;
  1063. clif_spawn(&hd->bl);
  1064. }
  1065. return status_revive(&hd->bl, per, 0);
  1066. }
  1067. /**
  1068. * Revive homunculus
  1069. * @param hd
  1070. * @param hp
  1071. * @param sp
  1072. */
  1073. void hom_revive(struct homun_data *hd, unsigned int hp, unsigned int sp)
  1074. {
  1075. struct map_session_data *sd = hd->master;
  1076. hd->homunculus.hp = hd->battle_status.hp;
  1077. if (!sd)
  1078. return;
  1079. clif_send_homdata(sd,SP_ACK,0);
  1080. clif_hominfo(sd,hd,1);
  1081. clif_hominfo(sd,hd,0);
  1082. clif_homskillinfoblock(sd);
  1083. if (hd->homunculus.class_ == 6052) //eleanor
  1084. sc_start(&hd->bl,&hd->bl, SC_STYLE_CHANGE, 100, MH_MD_FIGHTING, -1);
  1085. }
  1086. /**
  1087. * Reset homunculus status
  1088. * @param hd
  1089. */
  1090. void hom_reset_stats(struct homun_data *hd)
  1091. { //Resets a homunc stats back to zero (but doesn't touches hunger or intimacy)
  1092. struct s_homunculus_db *db;
  1093. struct s_homunculus *hom;
  1094. struct h_stats *base;
  1095. hom = &hd->homunculus;
  1096. db = hd->homunculusDB;
  1097. base = &db->base;
  1098. hom->level = 1;
  1099. hom->hp = 10;
  1100. hom->max_hp = base->HP;
  1101. hom->max_sp = base->SP;
  1102. hom->str = base->str *10;
  1103. hom->agi = base->agi *10;
  1104. hom->vit = base->vit *10;
  1105. hom->int_= base->int_*10;
  1106. hom->dex = base->dex *10;
  1107. hom->luk = base->luk *10;
  1108. hom->exp = 0;
  1109. hd->exp_next = hexptbl[0];
  1110. memset(&hd->homunculus.hskill, 0, sizeof hd->homunculus.hskill);
  1111. hd->homunculus.skillpts = 0;
  1112. }
  1113. /**
  1114. * Shuffle homunculus status
  1115. * @param hd
  1116. */
  1117. int hom_shuffle(struct homun_data *hd)
  1118. {
  1119. struct map_session_data *sd;
  1120. int lv, i, skillpts;
  1121. unsigned int exp;
  1122. struct s_skill b_skill[MAX_HOMUNSKILL];
  1123. if (!hom_is_active(hd))
  1124. return 0;
  1125. sd = hd->master;
  1126. lv = hd->homunculus.level;
  1127. exp = hd->homunculus.exp;
  1128. memcpy(&b_skill, &hd->homunculus.hskill, sizeof(b_skill));
  1129. skillpts = hd->homunculus.skillpts;
  1130. //Reset values to level 1.
  1131. hom_reset_stats(hd);
  1132. //Level it back up
  1133. for (i = 1; i < lv && hd->exp_next; i++){
  1134. hd->homunculus.exp += hd->exp_next;
  1135. // Should never happen, but who knows
  1136. if( !hom_levelup(hd) ) {
  1137. break;
  1138. }
  1139. }
  1140. if(hd->homunculus.class_ == hd->homunculusDB->evo_class) {
  1141. //Evolved bonuses
  1142. struct s_homunculus *hom = &hd->homunculus;
  1143. struct h_stats *max = &hd->homunculusDB->emax, *min = &hd->homunculusDB->emin;
  1144. hom->max_hp += rnd_value(min->HP, max->HP);
  1145. hom->max_sp += rnd_value(min->SP, max->SP);
  1146. hom->str += 10*rnd_value(min->str, max->str);
  1147. hom->agi += 10*rnd_value(min->agi, max->agi);
  1148. hom->vit += 10*rnd_value(min->vit, max->vit);
  1149. hom->int_+= 10*rnd_value(min->int_,max->int_);
  1150. hom->dex += 10*rnd_value(min->dex, max->dex);
  1151. hom->luk += 10*rnd_value(min->luk, max->luk);
  1152. }
  1153. hd->homunculus.exp = exp;
  1154. memcpy(&hd->homunculus.hskill, &b_skill, sizeof(b_skill));
  1155. hd->homunculus.skillpts = skillpts;
  1156. clif_homskillinfoblock(sd);
  1157. status_calc_homunculus(hd, SCO_NONE);
  1158. status_percent_heal(&hd->bl, 100, 100);
  1159. clif_specialeffect(&hd->bl,EF_HO_UP,AREA);
  1160. return 1;
  1161. }
  1162. /**
  1163. * Get minimum intimacy value of specified grade
  1164. * @param grade see enum e_homun_grade
  1165. * @return Intimacy value
  1166. **/
  1167. uint32 hom_intimacy_grade2intimacy(enum e_homun_grade grade) {
  1168. if (grade < HOMGRADE_HATE_WITH_PASSION || grade > HOMGRADE_LOYAL)
  1169. return 0;
  1170. return intimacy_grades[grade].min_value;
  1171. }
  1172. /**
  1173. * Get grade of given intimacy value
  1174. * @param intimacy
  1175. * @return Grade, see enum e_homun_grade
  1176. **/
  1177. enum e_homun_grade hom_intimacy_intimacy2grade(uint32 intimacy) {
  1178. #define CHK_HOMINTIMACY(grade) { if (intimacy >= intimacy_grades[(grade)].min_value) return (grade); }
  1179. CHK_HOMINTIMACY(HOMGRADE_LOYAL)
  1180. CHK_HOMINTIMACY(HOMGRADE_CORDIAL)
  1181. CHK_HOMINTIMACY(HOMGRADE_NEUTRAL)
  1182. CHK_HOMINTIMACY(HOMGRADE_SHY)
  1183. CHK_HOMINTIMACY(HOMGRADE_AWKWARD)
  1184. CHK_HOMINTIMACY(HOMGRADE_HATE)
  1185. #undef CHK_HOMINTIMACY
  1186. return HOMGRADE_HATE_WITH_PASSION;
  1187. }
  1188. /**
  1189. * Get initmacy grade
  1190. * @param hd
  1191. */
  1192. uint8 hom_get_intimacy_grade(struct homun_data *hd) {
  1193. return hom_intimacy_intimacy2grade(hd->homunculus.intimacy);
  1194. }
  1195. /**
  1196. * Read homunculus db
  1197. */
  1198. static bool read_homunculusdb_sub(char* str[], int columns, int current)
  1199. {
  1200. int classid;
  1201. uint16 i;
  1202. struct s_homunculus_db *db;
  1203. //Base Class,Evo Class
  1204. classid = atoi(str[0]);
  1205. if (classid < HM_CLASS_BASE || classid > HM_CLASS_MAX)
  1206. {
  1207. ShowError("read_homunculusdb : Invalid class %d\n", classid);
  1208. return false;
  1209. }
  1210. //Find the ClassID, already exist or not in homunculus_db
  1211. ARR_FIND(0,homunculus_count,i,homunculus_db[i].base_class == classid);
  1212. if (i >= homunculus_count)
  1213. db = &homunculus_db[homunculus_count];
  1214. else
  1215. db = &homunculus_db[i];
  1216. db->base_class = classid;
  1217. classid = atoi(str[1]);
  1218. if (classid < HM_CLASS_BASE || classid > HM_CLASS_MAX)
  1219. {
  1220. db->base_class = 0;
  1221. ShowError("read_homunculusdb : Invalid class %d\n", classid);
  1222. return false;
  1223. }
  1224. db->evo_class = classid;
  1225. //Name, Food, Hungry Delay, Base Size, Evo Size, Race, Element, ASPD
  1226. safestrncpy(db->name,str[2],NAME_LENGTH-1);
  1227. db->foodID = atoi(str[3]);
  1228. db->hungryDelay = atoi(str[4]);
  1229. db->base_size = atoi(str[5]);
  1230. db->evo_size = atoi(str[6]);
  1231. db->race = atoi(str[7]);
  1232. db->element = atoi(str[8]);
  1233. db->baseASPD = atoi(str[9]);
  1234. //base HP, SP, str, agi, vit, int, dex, luk
  1235. db->base.HP = atoi(str[10]);
  1236. db->base.SP = atoi(str[11]);
  1237. db->base.str = atoi(str[12]);
  1238. db->base.agi = atoi(str[13]);
  1239. db->base.vit = atoi(str[14]);
  1240. db->base.int_= atoi(str[15]);
  1241. db->base.dex = atoi(str[16]);
  1242. db->base.luk = atoi(str[17]);
  1243. //Growth Min/Max HP, SP, str, agi, vit, int, dex, luk
  1244. db->gmin.HP = atoi(str[18]);
  1245. db->gmax.HP = atoi(str[19]);
  1246. db->gmin.SP = atoi(str[20]);
  1247. db->gmax.SP = atoi(str[21]);
  1248. db->gmin.str = atoi(str[22]);
  1249. db->gmax.str = atoi(str[23]);
  1250. db->gmin.agi = atoi(str[24]);
  1251. db->gmax.agi = atoi(str[25]);
  1252. db->gmin.vit = atoi(str[26]);
  1253. db->gmax.vit = atoi(str[27]);
  1254. db->gmin.int_= atoi(str[28]);
  1255. db->gmax.int_= atoi(str[29]);
  1256. db->gmin.dex = atoi(str[30]);
  1257. db->gmax.dex = atoi(str[31]);
  1258. db->gmin.luk = atoi(str[32]);
  1259. db->gmax.luk = atoi(str[33]);
  1260. //Evolution Min/Max HP, SP, str, agi, vit, int, dex, luk
  1261. db->emin.HP = atoi(str[34]);
  1262. db->emax.HP = atoi(str[35]);
  1263. db->emin.SP = atoi(str[36]);
  1264. db->emax.SP = atoi(str[37]);
  1265. db->emin.str = atoi(str[38]);
  1266. db->emax.str = atoi(str[39]);
  1267. db->emin.agi = atoi(str[40]);
  1268. db->emax.agi = atoi(str[41]);
  1269. db->emin.vit = atoi(str[42]);
  1270. db->emax.vit = atoi(str[43]);
  1271. db->emin.int_= atoi(str[44]);
  1272. db->emax.int_= atoi(str[45]);
  1273. db->emin.dex = atoi(str[46]);
  1274. db->emax.dex = atoi(str[47]);
  1275. db->emin.luk = atoi(str[48]);
  1276. db->emax.luk = atoi(str[49]);
  1277. //Check that the min/max values really are below the other one.
  1278. if(db->gmin.HP > db->gmax.HP)
  1279. db->gmin.HP = db->gmax.HP;
  1280. if(db->gmin.SP > db->gmax.SP)
  1281. db->gmin.SP = db->gmax.SP;
  1282. if(db->gmin.str > db->gmax.str)
  1283. db->gmin.str = db->gmax.str;
  1284. if(db->gmin.agi > db->gmax.agi)
  1285. db->gmin.agi = db->gmax.agi;
  1286. if(db->gmin.vit > db->gmax.vit)
  1287. db->gmin.vit = db->gmax.vit;
  1288. if(db->gmin.int_> db->gmax.int_)
  1289. db->gmin.int_= db->gmax.int_;
  1290. if(db->gmin.dex > db->gmax.dex)
  1291. db->gmin.dex = db->gmax.dex;
  1292. if(db->gmin.luk > db->gmax.luk)
  1293. db->gmin.luk = db->gmax.luk;
  1294. if(db->emin.HP > db->emax.HP)
  1295. db->emin.HP = db->emax.HP;
  1296. if(db->emin.SP > db->emax.SP)
  1297. db->emin.SP = db->emax.SP;
  1298. if(db->emin.str > db->emax.str)
  1299. db->emin.str = db->emax.str;
  1300. if(db->emin.agi > db->emax.agi)
  1301. db->emin.agi = db->emax.agi;
  1302. if(db->emin.vit > db->emax.vit)
  1303. db->emin.vit = db->emax.vit;
  1304. if(db->emin.int_> db->emax.int_)
  1305. db->emin.int_= db->emax.int_;
  1306. if(db->emin.dex > db->emax.dex)
  1307. db->emin.dex = db->emax.dex;
  1308. if(db->emin.luk > db->emax.luk)
  1309. db->emin.luk = db->emax.luk;
  1310. if (i >= homunculus_count)
  1311. homunculus_count++;
  1312. return true;
  1313. }
  1314. /**
  1315. * Read homunculus db (check the files)
  1316. */
  1317. void read_homunculusdb(void) {
  1318. uint8 i;
  1319. const char *filename[] = {
  1320. DBPATH"homunculus_db.txt",
  1321. DBIMPORT"/homunculus_db.txt",
  1322. };
  1323. homunculus_count = 0;
  1324. memset(homunculus_db,0,sizeof(homunculus_db));
  1325. for(i = 0; i<ARRAYLENGTH(filename); i++){
  1326. sv_readdb(db_path, filename[i], ',', 50, 50, MAX_HOMUNCULUS_CLASS, &read_homunculusdb_sub, i > 0);
  1327. }
  1328. }
  1329. /**
  1330. * Read homunculus skill db
  1331. * <hom class>,<skill id>,<max level>,<need level>,<req id1>,<req lv1>,<req id2>,<req lv2>,<req id3>,<req lv3>,<req id4>,<req lv4>,<req id5>,<req lv5>,<intimacy lv req>
  1332. */
  1333. static bool read_homunculus_skilldb_sub(char* split[], int columns, int current) {
  1334. uint16 skill_id;
  1335. int8 i;
  1336. short class_idx, idx = -1;
  1337. // check for bounds [celest]
  1338. if ((class_idx = hom_class2index(atoi(split[0]))) == -1) {
  1339. ShowWarning("read_homunculus_skilldb: Invalid homunculus class %d.\n", atoi(split[0]));
  1340. return false;
  1341. }
  1342. skill_id = atoi(split[1]);
  1343. if (hom_skill_get_index(skill_id) == -1) {
  1344. ShowError("read_homunculus_skilldb: Invalid Homunculus skill '%s'.\n", split[1]);
  1345. return false;
  1346. }
  1347. // Search an empty line or a line with the same skill_id (stored in idx)
  1348. ARR_FIND(0, MAX_HOM_SKILL_TREE, idx, !hskill_tree[class_idx][idx].id || hskill_tree[class_idx][idx].id == skill_id);
  1349. if (idx == MAX_HOM_SKILL_TREE) {
  1350. ShowWarning("Unable to load skill %d into homunculus %d's tree. Maximum number of skills per class has been reached.\n", skill_id, atoi(split[0]));
  1351. return false;
  1352. }
  1353. hskill_tree[class_idx][idx].id = skill_id;
  1354. hskill_tree[class_idx][idx].max = atoi(split[2]);
  1355. hskill_tree[class_idx][idx].need_level = atoi(split[3]);
  1356. for (i = 0; i < MAX_HOM_SKILL_REQUIRE; i++) {
  1357. hskill_tree[class_idx][idx].need[i].id = atoi(split[4+i*2]);
  1358. hskill_tree[class_idx][idx].need[i].lv = atoi(split[4+i*2+1]);
  1359. }
  1360. hskill_tree[class_idx][idx].intimacy = atoi(split[14]);
  1361. return true;
  1362. }
  1363. /**
  1364. * Read homunculus skill db (check the files)
  1365. */
  1366. static void read_homunculus_skilldb(void) {
  1367. const char *filename[] = { "homun_skill_tree.txt", DBIMPORT"/homun_skill_tree.txt"};
  1368. int i;
  1369. memset(hskill_tree,0,sizeof(hskill_tree));
  1370. for (i = 0; i<ARRAYLENGTH(filename); i++) {
  1371. sv_readdb(db_path, filename[i], ',', 15, 15, -1, &read_homunculus_skilldb_sub, i > 0);
  1372. }
  1373. }
  1374. /**
  1375. * Read homunculus exp db
  1376. */
  1377. void read_homunculus_expdb(void)
  1378. {
  1379. int i;
  1380. const char *filename[]={
  1381. DBPATH"exp_homun.txt",
  1382. DBIMPORT"/exp_homun.txt"
  1383. };
  1384. memset(hexptbl,0,sizeof(hexptbl));
  1385. for (i = 0; i < ARRAYLENGTH(filename); i++) {
  1386. FILE *fp;
  1387. char line[1024];
  1388. int j=0;
  1389. sprintf(line, "%s/%s", db_path, filename[i]);
  1390. fp = fopen(line,"r");
  1391. if (fp == NULL) {
  1392. if (i != 0)
  1393. continue;
  1394. ShowError("Can't read %s\n",line);
  1395. return;
  1396. }
  1397. while (fgets(line, sizeof(line), fp) && j < MAX_LEVEL) {
  1398. if (line[0] == '/' && line[1] == '/')
  1399. continue;
  1400. hexptbl[j] = strtoul(line, NULL, 10);
  1401. if (!hexptbl[j++])
  1402. break;
  1403. }
  1404. if (hexptbl[MAX_LEVEL - 1]) { // Last permitted level have to be 0!
  1405. ShowWarning("read_hexptbl: Reached max level in %s [%d]. Remaining lines were not read.\n ",filename,MAX_LEVEL);
  1406. hexptbl[MAX_LEVEL - 1] = 0;
  1407. }
  1408. fclose(fp);
  1409. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' levels in '" CL_WHITE "%s/%s" CL_RESET "'.\n", j, db_path, filename[i]);
  1410. }
  1411. }
  1412. void hom_reload(void){
  1413. read_homunculusdb();
  1414. read_homunculus_expdb();
  1415. }
  1416. void hom_reload_skill(void){
  1417. read_homunculus_skilldb();
  1418. }
  1419. void do_init_homunculus(void){
  1420. int class_;
  1421. read_homunculusdb();
  1422. read_homunculus_expdb();
  1423. read_homunculus_skilldb();
  1424. // Add homunc timer function to timer func list [Toms]
  1425. add_timer_func_list(hom_hungry, "hom_hungry");
  1426. //Stock view data for homuncs
  1427. memset(&hom_viewdb, 0, sizeof(hom_viewdb));
  1428. for (class_ = 0; class_ < ARRAYLENGTH(hom_viewdb); class_++)
  1429. hom_viewdb[class_].class_ = HM_CLASS_BASE+class_;
  1430. }
  1431. void do_final_homunculus(void) {
  1432. //Nothing todo yet
  1433. }