homunculus.c 41 KB

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