homunculus.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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/socket.h"
  6. #include "../common/timer.h"
  7. #include "../common/nullpo.h"
  8. #include "../common/mmo.h"
  9. #include "../common/random.h"
  10. #include "../common/showmsg.h"
  11. #include "../common/strlib.h"
  12. #include "../common/utils.h"
  13. #include "log.h"
  14. #include "clif.h"
  15. #include "chrif.h"
  16. #include "intif.h"
  17. #include "itemdb.h"
  18. #include "map.h"
  19. #include "pc.h"
  20. #include "status.h"
  21. #include "skill.h"
  22. #include "mob.h"
  23. #include "pet.h"
  24. #include "battle.h"
  25. #include "party.h"
  26. #include "guild.h"
  27. #include "atcommand.h"
  28. #include "script.h"
  29. #include "npc.h"
  30. #include "trade.h"
  31. #include "unit.h"
  32. #include "homunculus.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <math.h>
  37. struct s_homunculus_db homunculus_db[MAX_HOMUNCULUS_CLASS]; //[orn]
  38. struct homun_skill_tree_entry hskill_tree[MAX_HOMUNCULUS_CLASS][MAX_SKILL_TREE];
  39. static int merc_hom_hungry(int tid, unsigned int tick, int id, intptr_t data);
  40. static unsigned int hexptbl[MAX_LEVEL];
  41. //For holding the view data of npc classes. [Skotlex]
  42. static struct view_data hom_viewdb[MAX_HOMUNCULUS_CLASS];
  43. struct view_data* merc_get_hom_viewdata(int class_)
  44. { //Returns the viewdata for homunculus
  45. if (homdb_checkid(class_))
  46. return &hom_viewdb[class_-HM_CLASS_BASE];
  47. return NULL;
  48. }
  49. enum homun_type hom_class2type(int class_) {
  50. int mid = hom_class2mapid(class_);
  51. if((mid&(HOM_REG|HOM_EVO)) == (HOM_REG|HOM_EVO))
  52. return HT_EVO;
  53. else if(mid&(HOM_REG))
  54. return HT_REG;
  55. else if(mid&(HOM_S))
  56. return HT_S;
  57. else
  58. return HT_INVALID;
  59. }
  60. int hom_class2mapid(int hom_class)
  61. {
  62. switch(hom_class)
  63. {
  64. // Normal Homunculus
  65. case 6001: case 6005: return MAPID_LIF;
  66. case 6002: case 6006: return MAPID_AMISTR;
  67. case 6003: case 6007: return MAPID_FILIR;
  68. case 6004: case 6008: return MAPID_VANILMIRTH;
  69. // Evolved Homunculus
  70. case 6009: case 6013: return MAPID_LIF_E;
  71. case 6010: case 6014: return MAPID_AMISTR_E;
  72. case 6011: case 6015: return MAPID_FILIR_E;
  73. case 6012: case 6016: return MAPID_VANILMIRTH_E;
  74. // Homunculus S
  75. case 6048: return MAPID_EIRA;
  76. case 6049: return MAPID_BAYERI;
  77. case 6050: return MAPID_SERA;
  78. case 6051: return MAPID_DIETER;
  79. case 6052: return MAPID_ELANOR;
  80. default: return -1;
  81. }
  82. }
  83. int hom_addspiritball(TBL_HOM *hd, int max) {
  84. nullpo_ret(hd);
  85. if (max > MAX_SKILL_LEVEL)
  86. max = MAX_SKILL_LEVEL;
  87. if (hd->homunculus.spiritball < 0)
  88. hd->homunculus.spiritball = 0;
  89. if (hd->homunculus.spiritball && hd->homunculus.spiritball >= max) {
  90. hd->homunculus.spiritball = max;
  91. }
  92. else
  93. hd->homunculus.spiritball++;
  94. clif_spiritball(&hd->bl);
  95. return 0;
  96. }
  97. int hom_delspiritball(TBL_HOM *hd, int count, int type) {
  98. nullpo_ret(hd);
  99. if (hd->homunculus.spiritball <= 0) {
  100. hd->homunculus.spiritball = 0;
  101. return 0;
  102. }
  103. if (count <= 0)
  104. return 0;
  105. if (count > MAX_SKILL_LEVEL)
  106. count = MAX_SKILL_LEVEL;
  107. if (count > hd->homunculus.spiritball)
  108. count = hd->homunculus.spiritball;
  109. hd->homunculus.spiritball -= count;
  110. if (!type)
  111. clif_spiritball(&hd->bl);
  112. return 0;
  113. }
  114. void merc_damage(struct homun_data *hd) {
  115. clif_hominfo(hd->master,hd,0);
  116. }
  117. int merc_hom_dead(struct homun_data *hd)
  118. {
  119. //There's no intimacy penalties on death (from Tharis)
  120. struct map_session_data *sd = hd->master;
  121. clif_emotion(&hd->bl, E_WAH);
  122. //Delete timers when dead.
  123. merc_hom_hungry_timer_delete(hd);
  124. hd->homunculus.hp = 0;
  125. if (!sd) //unit remove map will invoke unit free
  126. return 3;
  127. clif_emotion(&sd->bl, E_SOB);
  128. //Remove from map (if it has no intimacy, it is auto-removed from memory)
  129. return 3;
  130. }
  131. //Vaporize a character's homun. If flag == 1 then HP needs to be 80% or above, if flag == 2 then set to morph state.
  132. int merc_hom_vaporize(struct map_session_data *sd, int flag)
  133. {
  134. struct homun_data *hd;
  135. nullpo_ret(sd);
  136. hd = sd->hd;
  137. if (!hd || hd->homunculus.vaporize)
  138. return 0;
  139. if (status_isdead(&hd->bl))
  140. return 0; //Can't vaporize a dead homun.
  141. if (flag == HOM_ST_REST && get_percentage(hd->battle_status.hp, hd->battle_status.max_hp) < 80)
  142. return 0;
  143. hd->regen.state.block = 3; //Block regen while vaporized.
  144. //Delete timers when vaporized.
  145. merc_hom_hungry_timer_delete(hd);
  146. hd->homunculus.vaporize = flag ? flag : HOM_ST_REST;
  147. if(battle_config.hom_setting&0x40)
  148. memset(hd->blockskill, 0, sizeof(hd->blockskill));
  149. clif_hominfo(sd, sd->hd, 0);
  150. merc_save(hd);
  151. return unit_remove_map(&hd->bl, CLR_OUTSIGHT);
  152. }
  153. //delete a homunculus, completely "killing it".
  154. //Emote is the emotion the master should use, send negative to disable.
  155. int merc_hom_delete(struct homun_data *hd, int emote)
  156. {
  157. struct map_session_data *sd;
  158. nullpo_ret(hd);
  159. sd = hd->master;
  160. if (!sd)
  161. return unit_free(&hd->bl,CLR_DEAD);
  162. if (emote >= 0)
  163. clif_emotion(&sd->bl, emote);
  164. //This makes it be deleted right away.
  165. hd->homunculus.intimacy = 0;
  166. // Send homunculus_dead to client
  167. hd->homunculus.hp = 0;
  168. clif_hominfo(sd, hd, 0);
  169. return unit_remove_map(&hd->bl,CLR_OUTSIGHT);
  170. }
  171. int merc_hom_calc_skilltree(struct homun_data *hd, int flag_evolve)
  172. {
  173. int i, id = 0;
  174. int j, f = 1;
  175. int c = 0;
  176. nullpo_ret(hd);
  177. /* load previous homunculus form skills first. */
  178. if( hd->homunculus.prev_class != 0 ) {
  179. c = hd->homunculus.prev_class - HM_CLASS_BASE;
  180. for( i = 0; i < MAX_SKILL_TREE && ( id = hskill_tree[c][i].id ) > 0; i++ ) {
  181. if( hd->homunculus.hskill[ id - HM_SKILLBASE ].id )
  182. continue; //Skill already known.
  183. if(!battle_config.skillfree) {
  184. for( j = 0; j < MAX_PC_SKILL_REQUIRE; j++ ) {
  185. if( hskill_tree[c][i].need[j].id &&
  186. merc_hom_checkskill(hd,hskill_tree[c][i].need[j].id) < hskill_tree[c][i].need[j].lv ) {
  187. f = 0;
  188. break;
  189. }
  190. }
  191. }
  192. if ( f )
  193. hd->homunculus.hskill[id-HM_SKILLBASE].id = id;
  194. }
  195. f = 1;
  196. }
  197. c = hd->homunculus.class_ - HM_CLASS_BASE;
  198. for( i = 0; i < MAX_SKILL_TREE && ( id = hskill_tree[c][i].id ) > 0; i++ ) {
  199. if( hd->homunculus.hskill[ id - HM_SKILLBASE ].id )
  200. continue; //Skill already known.
  201. j = ( flag_evolve ) ? 10 : hd->homunculus.intimacy;
  202. if( j < hskill_tree[c][i].intimacylv )
  203. continue;
  204. if(!battle_config.skillfree) {
  205. for( j = 0; j < MAX_PC_SKILL_REQUIRE; j++ ) {
  206. if( hskill_tree[c][i].need[j].id &&
  207. merc_hom_checkskill(hd,hskill_tree[c][i].need[j].id) < hskill_tree[c][i].need[j].lv ) {
  208. f = 0;
  209. break;
  210. }
  211. }
  212. }
  213. if ( f )
  214. hd->homunculus.hskill[id-HM_SKILLBASE].id = id;
  215. }
  216. if( hd->master )
  217. clif_homskillinfoblock(hd->master);
  218. return 0;
  219. }
  220. int merc_hom_checkskill(struct homun_data *hd,uint16 skill_id)
  221. {
  222. int i = skill_id - HM_SKILLBASE;
  223. if(!hd || !&hd->homunculus)
  224. return 0;
  225. if(hd->homunculus.hskill[i].id == skill_id)
  226. return (hd->homunculus.hskill[i].lv);
  227. return 0;
  228. }
  229. int merc_skill_tree_get_max(int id, int b_class){
  230. int i, skill_id;
  231. b_class -= HM_CLASS_BASE;
  232. for(i=0;(skill_id=hskill_tree[b_class][i].id)>0;i++)
  233. if (id == skill_id)
  234. return hskill_tree[b_class][i].max;
  235. return skill_get_max(id);
  236. }
  237. void merc_hom_skillup(struct homun_data *hd,uint16 skill_id)
  238. {
  239. int i = 0 ;
  240. nullpo_retv(hd);
  241. if(hd->homunculus.vaporize)
  242. return;
  243. i = skill_id - HM_SKILLBASE;
  244. if(hd->homunculus.skillpts > 0 &&
  245. hd->homunculus.hskill[i].id &&
  246. hd->homunculus.hskill[i].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  247. hd->homunculus.hskill[i].lv < merc_skill_tree_get_max(skill_id, hd->homunculus.class_)
  248. )
  249. {
  250. hd->homunculus.hskill[i].lv++;
  251. hd->homunculus.skillpts-- ;
  252. status_calc_homunculus(hd,0);
  253. if (hd->master) {
  254. clif_homskillup(hd->master, skill_id);
  255. clif_hominfo(hd->master,hd,0);
  256. clif_homskillinfoblock(hd->master);
  257. }
  258. }
  259. }
  260. int merc_hom_levelup(struct homun_data *hd)
  261. {
  262. struct s_homunculus *hom;
  263. struct h_stats *min = NULL, *max = NULL;
  264. int growth_str, growth_agi, growth_vit, growth_int, growth_dex, growth_luk ;
  265. int growth_max_hp, growth_max_sp ;
  266. int m_class;
  267. if((m_class = hom_class2mapid(hd->homunculus.class_)) == -1) {
  268. ShowError("merc_hom_levelup: Invalid class %d. \n", hd->homunculus.class_);
  269. return 0;
  270. }
  271. /// When homunculus is homunculus S, we check to see if we need to apply previous class stats
  272. if(m_class&HOM_S && hd->homunculus.level < battle_config.hom_S_growth_level) {
  273. int i;
  274. if(!hd->homunculus.prev_class) {
  275. /// We also need to be sure that the previous class exists, otherwise give it something to work with
  276. hd->homunculus.prev_class = 6001;
  277. }
  278. // Give the homunculus the level up stats database it needs
  279. i = search_homunculusDB_index(hd->homunculus.prev_class,HOMUNCULUS_CLASS);
  280. if(i < 0) // Nothing should go wrong here, but check anyways
  281. return 0;
  282. max = &homunculus_db[i].gmax;
  283. min = &homunculus_db[i].gmin;
  284. }
  285. if(((m_class&HOM_REG) && hd->homunculus.level >= battle_config.hom_max_level)
  286. || ((m_class&HOM_S) && hd->homunculus.level >= battle_config.hom_S_max_level)
  287. || !hd->exp_next || hd->homunculus.exp < hd->exp_next)
  288. return 0;
  289. hom = &hd->homunculus;
  290. hom->level++ ;
  291. if (!(hom->level % 3))
  292. hom->skillpts++ ; //1 skillpoint each 3 base level
  293. hom->exp -= hd->exp_next ;
  294. hd->exp_next = hexptbl[hom->level - 1] ;
  295. if(!max) {
  296. max = &hd->homunculusDB->gmax;
  297. min = &hd->homunculusDB->gmin;
  298. }
  299. growth_max_hp = rnd_value(min->HP, max->HP);
  300. growth_max_sp = rnd_value(min->SP, max->SP);
  301. growth_str = rnd_value(min->str, max->str);
  302. growth_agi = rnd_value(min->agi, max->agi);
  303. growth_vit = rnd_value(min->vit, max->vit);
  304. growth_dex = rnd_value(min->dex, max->dex);
  305. growth_int = rnd_value(min->int_,max->int_);
  306. growth_luk = rnd_value(min->luk, max->luk);
  307. //Aegis discards the decimals in the stat growth values!
  308. growth_str-=growth_str%10;
  309. growth_agi-=growth_agi%10;
  310. growth_vit-=growth_vit%10;
  311. growth_dex-=growth_dex%10;
  312. growth_int-=growth_int%10;
  313. growth_luk-=growth_luk%10;
  314. hom->max_hp += growth_max_hp;
  315. hom->max_sp += growth_max_sp;
  316. hom->str += growth_str;
  317. hom->agi += growth_agi;
  318. hom->vit += growth_vit;
  319. hom->dex += growth_dex;
  320. hom->int_+= growth_int;
  321. hom->luk += growth_luk;
  322. if ( battle_config.homunculus_show_growth ) {
  323. char output[256] ;
  324. sprintf(output,
  325. "Growth: hp:%d sp:%d str(%.2f) agi(%.2f) vit(%.2f) int(%.2f) dex(%.2f) luk(%.2f) ",
  326. growth_max_hp, growth_max_sp,
  327. growth_str/10.0, growth_agi/10.0, growth_vit/10.0,
  328. growth_int/10.0, growth_dex/10.0, growth_luk/10.0);
  329. clif_disp_onlyself(hd->master,output,strlen(output));
  330. }
  331. return 1 ;
  332. }
  333. int merc_hom_change_class(struct homun_data *hd, short class_)
  334. {
  335. int i;
  336. i = search_homunculusDB_index(class_,HOMUNCULUS_CLASS);
  337. if(i < 0)
  338. return 0;
  339. hd->homunculusDB = &homunculus_db[i];
  340. hd->homunculus.class_ = class_;
  341. status_set_viewdata(&hd->bl, class_);
  342. merc_hom_calc_skilltree(hd, 1);
  343. return 1;
  344. }
  345. /**
  346. * Make an homonculus evolve, (changing in evolution class and apply bonus)
  347. * @param hd : homonculus datas
  348. * @return 0:failure, 1:success
  349. */
  350. int merc_hom_evolution(struct homun_data *hd)
  351. {
  352. struct s_homunculus *hom;
  353. struct h_stats *max, *min;
  354. struct map_session_data *sd;
  355. nullpo_ret(hd);
  356. if(!hd->homunculusDB->evo_class || hd->homunculus.class_ == hd->homunculusDB->evo_class)
  357. {
  358. clif_emotion(&hd->bl, E_SWT);
  359. return 0 ;
  360. }
  361. sd = hd->master;
  362. if (!sd)
  363. return 0;
  364. if (!merc_hom_change_class(hd, hd->homunculusDB->evo_class)) {
  365. ShowError("merc_hom_evolution: Can't evolve homunc from %d to %d", hd->homunculus.class_, hd->homunculusDB->evo_class);
  366. return 0;
  367. }
  368. //Apply evolution bonuses
  369. hom = &hd->homunculus;
  370. max = &hd->homunculusDB->emax;
  371. min = &hd->homunculusDB->emin;
  372. hom->max_hp += rnd_value(min->HP, max->HP);
  373. hom->max_sp += rnd_value(min->SP, max->SP);
  374. hom->str += 10*rnd_value(min->str, max->str);
  375. hom->agi += 10*rnd_value(min->agi, max->agi);
  376. hom->vit += 10*rnd_value(min->vit, max->vit);
  377. hom->int_+= 10*rnd_value(min->int_,max->int_);
  378. hom->dex += 10*rnd_value(min->dex, max->dex);
  379. hom->luk += 10*rnd_value(min->luk, max->luk);
  380. hom->intimacy = 500;
  381. unit_remove_map(&hd->bl, CLR_OUTSIGHT);
  382. if(map_addblock(&hd->bl))
  383. return 0;
  384. clif_spawn(&hd->bl);
  385. clif_emotion(&sd->bl, E_NO1);
  386. clif_specialeffect(&hd->bl,568,AREA);
  387. //status_Calc flag&1 will make current HP/SP be reloaded from hom structure
  388. hom->hp = hd->battle_status.hp;
  389. hom->sp = hd->battle_status.sp;
  390. status_calc_homunculus(hd,1);
  391. if (!(battle_config.hom_setting&0x2))
  392. skill_unit_move(&sd->hd->bl,gettick(),1); // apply land skills immediately
  393. return 1 ;
  394. }
  395. /**
  396. * Make an homonculus mutate in renewal homon
  397. * @param hd : homonculus datas
  398. * @param homun_id : id to make it transform into (must be a valid homon class)
  399. * @return 0:failure, 1:sucess
  400. */
  401. int hom_mutate(struct homun_data *hd, int homun_id)
  402. {
  403. struct s_homunculus *hom;
  404. struct map_session_data *sd;
  405. int m_class, m_id, prev_class = 0;
  406. nullpo_ret(hd);
  407. m_class = hom_class2mapid(hd->homunculus.class_);
  408. m_id = hom_class2mapid(homun_id);
  409. if( m_class == -1 || m_id == -1 || !(m_class&HOM_EVO) || !(m_id&HOM_S) ) {
  410. clif_emotion(&hd->bl, E_SWT);
  411. return 0;
  412. }
  413. sd = hd->master;
  414. if (!sd)
  415. return 0;
  416. prev_class = hd->homunculus.class_;
  417. if (!merc_hom_change_class(hd, homun_id)) {
  418. ShowError("hom_mutate: Can't evolve homunc from %d to %d", hd->homunculus.class_, homun_id);
  419. return 0;
  420. }
  421. unit_remove_map(&hd->bl, CLR_OUTSIGHT);
  422. if(map_addblock(&hd->bl))
  423. return 0;
  424. clif_spawn(&hd->bl);
  425. clif_emotion(&sd->bl, E_NO1);
  426. clif_specialeffect(&hd->bl,568,AREA);
  427. //status_Calc flag&1 will make current HP/SP be reloaded from hom structure
  428. hom = &hd->homunculus;
  429. hom->hp = hd->battle_status.hp;
  430. hom->sp = hd->battle_status.sp;
  431. hom->prev_class = prev_class;
  432. status_calc_homunculus(hd,1);
  433. if (!(battle_config.hom_setting&0x2))
  434. skill_unit_move(&sd->hd->bl,gettick(),1); // apply land skills immediately
  435. return 1;
  436. }
  437. int merc_hom_gainexp(struct homun_data *hd,int exp)
  438. {
  439. int m_class;
  440. if(hd->homunculus.vaporize)
  441. return 1;
  442. if((m_class = hom_class2mapid(hd->homunculus.class_)) == -1) {
  443. ShowError("merc_hom_gainexp: Invalid class %d. \n", hd->homunculus.class_);
  444. return 0;
  445. }
  446. if( hd->exp_next == 0 ||
  447. ((m_class&HOM_REG) && hd->homunculus.level >= battle_config.hom_max_level) ||
  448. ((m_class&HOM_S) && hd->homunculus.level >= battle_config.hom_S_max_level) ) {
  449. hd->homunculus.exp = 0;
  450. return 0;
  451. }
  452. hd->homunculus.exp += exp;
  453. if(hd->homunculus.exp < hd->exp_next) {
  454. clif_hominfo(hd->master,hd,0);
  455. return 0;
  456. }
  457. // Do the levelup(s)
  458. while( hd->homunculus.exp > hd->exp_next ){
  459. // Max level reached or error
  460. if( !merc_hom_levelup(hd) ){
  461. break;
  462. }
  463. }
  464. if( hd->exp_next == 0 )
  465. hd->homunculus.exp = 0 ;
  466. clif_specialeffect(&hd->bl,568,AREA);
  467. status_calc_homunculus(hd,0);
  468. status_percent_heal(&hd->bl, 100, 100);
  469. return 0;
  470. }
  471. // Return the new value
  472. int merc_hom_increase_intimacy(struct homun_data * hd, unsigned int value)
  473. {
  474. if (battle_config.homunculus_friendly_rate != 100)
  475. value = (value * battle_config.homunculus_friendly_rate) / 100;
  476. if (hd->homunculus.intimacy + value <= 100000)
  477. hd->homunculus.intimacy += value;
  478. else
  479. hd->homunculus.intimacy = 100000;
  480. return hd->homunculus.intimacy;
  481. }
  482. // Return 0 if decrease fails or intimacy became 0 else the new value
  483. int merc_hom_decrease_intimacy(struct homun_data * hd, unsigned int value)
  484. {
  485. if (hd->homunculus.intimacy >= value)
  486. hd->homunculus.intimacy -= value;
  487. else
  488. hd->homunculus.intimacy = 0;
  489. return hd->homunculus.intimacy;
  490. }
  491. void merc_hom_heal(struct homun_data *hd) {
  492. clif_hominfo(hd->master,hd,0);
  493. }
  494. void merc_save(struct homun_data *hd)
  495. {
  496. // copy data that must be saved in homunculus struct ( hp / sp )
  497. TBL_PC * sd = hd->master;
  498. //Do not check for max_hp/max_sp caps as current could be higher to max due
  499. //to status changes/skills (they will be capped as needed upon stat
  500. //calculation on login)
  501. hd->homunculus.hp = hd->battle_status.hp;
  502. hd->homunculus.sp = hd->battle_status.sp;
  503. intif_homunculus_requestsave(sd->status.account_id, &hd->homunculus);
  504. }
  505. int merc_menu(struct map_session_data *sd,int menunum)
  506. {
  507. nullpo_ret(sd);
  508. if (sd->hd == NULL)
  509. return 1;
  510. switch(menunum) {
  511. case 0:
  512. break;
  513. case 1:
  514. merc_hom_food(sd, sd->hd);
  515. break;
  516. case 2:
  517. merc_hom_delete(sd->hd, -1);
  518. break;
  519. default:
  520. ShowError("merc_menu : unknown menu choice : %d\n", menunum) ;
  521. break;
  522. }
  523. return 0;
  524. }
  525. int merc_hom_food(struct map_session_data *sd, struct homun_data *hd)
  526. {
  527. int i, foodID, emotion;
  528. if(hd->homunculus.vaporize)
  529. return 1 ;
  530. foodID = hd->homunculusDB->foodID;
  531. i = pc_search_inventory(sd,foodID);
  532. if(i < 0) {
  533. clif_hom_food(sd,foodID,0);
  534. return 1;
  535. }
  536. pc_delitem(sd,i,1,0,0,LOG_TYPE_CONSUME);
  537. if ( hd->homunculus.hunger >= 91 ) {
  538. merc_hom_decrease_intimacy(hd, 50);
  539. emotion = E_WAH;
  540. } else if ( hd->homunculus.hunger >= 76 ) {
  541. merc_hom_decrease_intimacy(hd, 5);
  542. emotion = E_SWT2;
  543. } else if ( hd->homunculus.hunger >= 26 ) {
  544. merc_hom_increase_intimacy(hd, 75);
  545. emotion = E_HO;
  546. } else if ( hd->homunculus.hunger >= 11 ) {
  547. merc_hom_increase_intimacy(hd, 100);
  548. emotion = E_HO;
  549. } else {
  550. merc_hom_increase_intimacy(hd, 50);
  551. emotion = E_HO;
  552. }
  553. hd->homunculus.hunger += 10; //dunno increase value for each food
  554. if(hd->homunculus.hunger > 100)
  555. hd->homunculus.hunger = 100;
  556. clif_emotion(&hd->bl,emotion);
  557. clif_send_homdata(sd,SP_HUNGRY,hd->homunculus.hunger);
  558. clif_send_homdata(sd,SP_INTIMATE,hd->homunculus.intimacy / 100);
  559. clif_hom_food(sd,foodID,1);
  560. // Too much food :/
  561. if(hd->homunculus.intimacy == 0)
  562. return merc_hom_delete(sd->hd, E_OMG);
  563. return 0;
  564. }
  565. static int merc_hom_hungry(int tid, unsigned int tick, int id, intptr_t data)
  566. {
  567. struct map_session_data *sd;
  568. struct homun_data *hd;
  569. sd=map_id2sd(id);
  570. if(!sd)
  571. return 1;
  572. if(!sd->status.hom_id || !(hd=sd->hd))
  573. return 1;
  574. if(hd->hungry_timer != tid){
  575. ShowError("merc_hom_hungry_timer %d != %d\n",hd->hungry_timer,tid);
  576. return 0;
  577. }
  578. hd->hungry_timer = INVALID_TIMER;
  579. hd->homunculus.hunger-- ;
  580. if(hd->homunculus.hunger <= 10) {
  581. clif_emotion(&hd->bl, E_AN);
  582. } else if(hd->homunculus.hunger == 25) {
  583. clif_emotion(&hd->bl, E_HMM);
  584. } else if(hd->homunculus.hunger == 75) {
  585. clif_emotion(&hd->bl, E_OK);
  586. }
  587. if(hd->homunculus.hunger < 0) {
  588. hd->homunculus.hunger = 0;
  589. // Delete the homunculus if intimacy <= 100
  590. if ( !merc_hom_decrease_intimacy(hd, 100) )
  591. return merc_hom_delete(hd, E_OMG);
  592. clif_send_homdata(sd,SP_INTIMATE,hd->homunculus.intimacy / 100);
  593. }
  594. clif_send_homdata(sd,SP_HUNGRY,hd->homunculus.hunger);
  595. hd->hungry_timer = add_timer(tick+hd->homunculusDB->hungryDelay,merc_hom_hungry,sd->bl.id,0); //simple Fix albator
  596. return 0;
  597. }
  598. int merc_hom_hungry_timer_delete(struct homun_data *hd)
  599. {
  600. nullpo_ret(hd);
  601. if(hd->hungry_timer != INVALID_TIMER) {
  602. delete_timer(hd->hungry_timer,merc_hom_hungry);
  603. hd->hungry_timer = INVALID_TIMER;
  604. }
  605. return 1;
  606. }
  607. int merc_hom_change_name(struct map_session_data *sd,char *name)
  608. {
  609. int i;
  610. struct homun_data *hd;
  611. nullpo_retr(1, sd);
  612. hd = sd->hd;
  613. if (!merc_is_hom_active(hd))
  614. return 1;
  615. if(hd->homunculus.rename_flag && !battle_config.hom_rename)
  616. return 1;
  617. for(i=0;i<NAME_LENGTH && name[i];i++){
  618. if( !(name[i]&0xe0) || name[i]==0x7f)
  619. return 1;
  620. }
  621. return intif_rename_hom(sd, name);
  622. }
  623. int merc_hom_change_name_ack(struct map_session_data *sd, char* name, int flag)
  624. {
  625. struct homun_data *hd = sd->hd;
  626. if (!merc_is_hom_active(hd)) return 0;
  627. normalize_name(name," ");//bugreport:3032
  628. if ( !flag || name[0] == '\0' ) {
  629. clif_displaymessage(sd->fd, msg_txt(sd,280)); // You cannot use this name
  630. return 0;
  631. }
  632. safestrncpy(hd->homunculus.name,name,NAME_LENGTH);
  633. clif_charnameack (0,&hd->bl);
  634. hd->homunculus.rename_flag = 1;
  635. clif_hominfo(sd,hd,0);
  636. return 1;
  637. }
  638. int search_homunculusDB_index(int key,int type)
  639. {
  640. int i;
  641. for(i=0;i<MAX_HOMUNCULUS_CLASS;i++) {
  642. if(homunculus_db[i].base_class <= 0)
  643. continue;
  644. switch(type) {
  645. case HOMUNCULUS_CLASS:
  646. if(homunculus_db[i].base_class == key ||
  647. homunculus_db[i].evo_class == key)
  648. return i;
  649. break;
  650. case HOMUNCULUS_FOOD:
  651. if(homunculus_db[i].foodID == key)
  652. return i;
  653. break;
  654. default:
  655. return -1;
  656. }
  657. }
  658. return -1;
  659. }
  660. // Create homunc structure
  661. int merc_hom_alloc(struct map_session_data *sd, struct s_homunculus *hom)
  662. {
  663. struct homun_data *hd;
  664. int i = 0;
  665. nullpo_retr(1, sd);
  666. Assert((sd->status.hom_id == 0 || sd->hd == 0) || sd->hd->master == sd);
  667. i = search_homunculusDB_index(hom->class_,HOMUNCULUS_CLASS);
  668. if(i < 0) {
  669. ShowError("merc_hom_alloc: unknown class [%d] for homunculus '%s', requesting deletion.\n", hom->class_, hom->name);
  670. sd->status.hom_id = 0;
  671. intif_homunculus_requestdelete(hom->hom_id);
  672. return 1;
  673. }
  674. sd->hd = hd = (struct homun_data*)aCalloc(1,sizeof(struct homun_data));
  675. hd->bl.type = BL_HOM;
  676. hd->bl.id = npc_get_new_npc_id();
  677. hd->master = sd;
  678. hd->homunculusDB = &homunculus_db[i];
  679. memcpy(&hd->homunculus, hom, sizeof(struct s_homunculus));
  680. hd->exp_next = hexptbl[hd->homunculus.level - 1];
  681. status_set_viewdata(&hd->bl, hd->homunculus.class_);
  682. status_change_init(&hd->bl);
  683. unit_dataset(&hd->bl);
  684. hd->ud.dir = sd->ud.dir;
  685. // Find a random valid pos around the player
  686. hd->bl.m = sd->bl.m;
  687. hd->bl.x = sd->bl.x;
  688. hd->bl.y = sd->bl.y;
  689. unit_calc_pos(&hd->bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  690. hd->bl.x = hd->ud.to_x;
  691. hd->bl.y = hd->ud.to_y;
  692. map_addiddb(&hd->bl);
  693. status_calc_homunculus(hd,1);
  694. hd->hungry_timer = INVALID_TIMER;
  695. hd->masterteleport_timer = INVALID_TIMER;
  696. return 0;
  697. }
  698. void merc_hom_init_timers(struct homun_data * hd)
  699. {
  700. if (hd->hungry_timer == INVALID_TIMER)
  701. hd->hungry_timer = add_timer(gettick()+hd->homunculusDB->hungryDelay,merc_hom_hungry,hd->master->bl.id,0);
  702. hd->regen.state.block = 0; //Restore HP/SP block.
  703. hd->masterteleport_timer = INVALID_TIMER;
  704. }
  705. /**
  706. * Make a player spawn a homonculus (call)
  707. * @param sd
  708. * @return 0:failure, 1:sucess
  709. */
  710. int merc_call_homunculus(struct map_session_data *sd)
  711. {
  712. struct homun_data *hd;
  713. if (!sd->status.hom_id) //Create a new homun.
  714. return merc_create_homunculus_request(sd, HM_CLASS_BASE + rnd_value(0, 7)) ;
  715. // If homunc not yet loaded, load it
  716. if (!sd->hd)
  717. return intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  718. hd = sd->hd;
  719. if (!hd->homunculus.vaporize)
  720. return 0; //Can't use this if homun wasn't vaporized.
  721. if (hd->homunculus.vaporize == HOM_ST_MORPH)
  722. return 0; // Can't call homunculus (morph state).
  723. merc_hom_init_timers(hd);
  724. hd->homunculus.vaporize = HOM_ST_ACTIVE;
  725. if (hd->bl.prev == NULL)
  726. { //Spawn him
  727. hd->bl.x = sd->bl.x;
  728. hd->bl.y = sd->bl.y;
  729. hd->bl.m = sd->bl.m;
  730. if(map_addblock(&hd->bl))
  731. return 0;
  732. clif_spawn(&hd->bl);
  733. clif_send_homdata(sd,SP_ACK,0);
  734. clif_hominfo(sd,hd,1);
  735. clif_hominfo(sd,hd,0); // send this x2. dunno why, but kRO does that [blackhole89]
  736. clif_homskillinfoblock(sd);
  737. if (battle_config.slaves_inherit_speed&1)
  738. status_calc_bl(&hd->bl, SCB_SPEED);
  739. merc_save(hd);
  740. } else
  741. //Warp him to master.
  742. unit_warp(&hd->bl,sd->bl.m, sd->bl.x, sd->bl.y,CLR_OUTSIGHT);
  743. return 1;
  744. }
  745. /**
  746. * Receive homunculus data from char server
  747. * @param account_id : owner account_id of the homon
  748. * @param sh : homonculus data from char-serv
  749. * @param flag : does the creation in inter-serv was a success (0:no,1:yes)
  750. * @return 0:failure, 1:sucess
  751. */
  752. int merc_hom_recv_data(int account_id, struct s_homunculus *sh, int flag)
  753. {
  754. struct map_session_data *sd;
  755. struct homun_data *hd;
  756. sd = map_id2sd(account_id);
  757. if(!sd)
  758. return 0;
  759. if (sd->status.char_id != sh->char_id)
  760. {
  761. if (sd->status.hom_id == sh->hom_id)
  762. sh->char_id = sd->status.char_id; //Correct char id.
  763. else
  764. return 0;
  765. }
  766. if(!flag) { // Failed to load
  767. sd->status.hom_id = 0;
  768. return 0;
  769. }
  770. if (!sd->status.hom_id) //Hom just created.
  771. sd->status.hom_id = sh->hom_id;
  772. if (sd->hd) //uh? Overwrite the data.
  773. memcpy(&sd->hd->homunculus, sh, sizeof(struct s_homunculus));
  774. else
  775. merc_hom_alloc(sd, sh);
  776. hd = sd->hd;
  777. if(hd && hd->homunculus.hp && !hd->homunculus.vaporize && hd->bl.prev == NULL && sd->bl.prev != NULL)
  778. {
  779. if(map_addblock(&hd->bl))
  780. return 0;
  781. clif_spawn(&hd->bl);
  782. clif_send_homdata(sd,SP_ACK,0);
  783. clif_hominfo(sd,hd,1);
  784. clif_hominfo(sd,hd,0); // send this x2. dunno why, but kRO does that [blackhole89]
  785. clif_homskillinfoblock(sd);
  786. merc_hom_init_timers(hd);
  787. }
  788. return 1;
  789. }
  790. // Ask homunculus creation to char server
  791. int merc_create_homunculus_request(struct map_session_data *sd, int class_)
  792. {
  793. struct s_homunculus homun;
  794. struct h_stats *base;
  795. int i;
  796. nullpo_retr(1, sd);
  797. i = search_homunculusDB_index(class_,HOMUNCULUS_CLASS);
  798. if(i < 0) return 0;
  799. memset(&homun, 0, sizeof(struct s_homunculus));
  800. //Initial data
  801. safestrncpy(homun.name, homunculus_db[i].name, NAME_LENGTH-1);
  802. homun.class_ = class_;
  803. homun.level = 1;
  804. homun.hunger = 32; //32%
  805. homun.intimacy = 2100; //21/1000
  806. homun.char_id = sd->status.char_id;
  807. homun.hp = 10 ;
  808. base = &homunculus_db[i].base;
  809. homun.max_hp = base->HP;
  810. homun.max_sp = base->SP;
  811. homun.str = base->str *10;
  812. homun.agi = base->agi *10;
  813. homun.vit = base->vit *10;
  814. homun.int_= base->int_*10;
  815. homun.dex = base->dex *10;
  816. homun.luk = base->luk *10;
  817. // Request homunculus creation
  818. intif_homunculus_create(sd->status.account_id, &homun);
  819. return 1;
  820. }
  821. /**
  822. * Make a player resurect an homon (player must have one)
  823. * @param sd : player pointer
  824. * @param per : hp percentage to revive homon
  825. * @param x : x map coordinate
  826. * @param y : Y map coordinate
  827. * @return 0:failure, 1:success
  828. */
  829. int merc_resurrect_homunculus(struct map_session_data* sd, unsigned char per, short x, short y)
  830. {
  831. struct homun_data* hd;
  832. nullpo_ret(sd);
  833. if (!sd->status.hom_id)
  834. return 0; // no homunculus
  835. if (!sd->hd) //Load homun data;
  836. return intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  837. hd = sd->hd;
  838. if (hd->homunculus.vaporize == HOM_ST_REST)
  839. return 0; // vaporized homunculi need to be 'called'
  840. if (!status_isdead(&hd->bl))
  841. return 0; // already alive
  842. merc_hom_init_timers(hd);
  843. if (!hd->bl.prev)
  844. { //Add it back to the map.
  845. hd->bl.m = sd->bl.m;
  846. hd->bl.x = x;
  847. hd->bl.y = y;
  848. if(map_addblock(&hd->bl))
  849. return 0;
  850. clif_spawn(&hd->bl);
  851. }
  852. return status_revive(&hd->bl, per, 0);
  853. }
  854. void merc_hom_revive(struct homun_data *hd, unsigned int hp, unsigned int sp)
  855. {
  856. struct map_session_data *sd = hd->master;
  857. hd->homunculus.hp = hd->battle_status.hp;
  858. if (!sd)
  859. return;
  860. clif_send_homdata(sd,SP_ACK,0);
  861. clif_hominfo(sd,hd,1);
  862. clif_hominfo(sd,hd,0);
  863. clif_homskillinfoblock(sd);
  864. if(hd->homunculus.class_ == 6052) //eleanor
  865. sc_start(&hd->bl,&hd->bl, SC_STYLE_CHANGE, 100, MH_MD_FIGHTING, -1);
  866. }
  867. void merc_reset_stats(struct homun_data *hd)
  868. { //Resets a homunc stats back to zero (but doesn't touches hunger or intimacy)
  869. struct s_homunculus_db *db;
  870. struct s_homunculus *hom;
  871. struct h_stats *base;
  872. hom = &hd->homunculus;
  873. db = hd->homunculusDB;
  874. base = &db->base;
  875. hom->level = 1;
  876. hom->hp = 10;
  877. hom->max_hp = base->HP;
  878. hom->max_sp = base->SP;
  879. hom->str = base->str *10;
  880. hom->agi = base->agi *10;
  881. hom->vit = base->vit *10;
  882. hom->int_= base->int_*10;
  883. hom->dex = base->dex *10;
  884. hom->luk = base->luk *10;
  885. hom->exp = 0;
  886. hd->exp_next = hexptbl[0];
  887. memset(&hd->homunculus.hskill, 0, sizeof hd->homunculus.hskill);
  888. hd->homunculus.skillpts = 0;
  889. }
  890. int merc_hom_shuffle(struct homun_data *hd)
  891. {
  892. struct map_session_data *sd;
  893. int lv, i, skillpts;
  894. unsigned int exp;
  895. struct s_skill b_skill[MAX_HOMUNSKILL];
  896. if (!merc_is_hom_active(hd))
  897. return 0;
  898. sd = hd->master;
  899. lv = hd->homunculus.level;
  900. exp = hd->homunculus.exp;
  901. memcpy(&b_skill, &hd->homunculus.hskill, sizeof(b_skill));
  902. skillpts = hd->homunculus.skillpts;
  903. //Reset values to level 1.
  904. merc_reset_stats(hd);
  905. //Level it back up
  906. for (i = 1; i < lv && hd->exp_next; i++){
  907. hd->homunculus.exp += hd->exp_next;
  908. // Should never happen, but who knows
  909. if( !merc_hom_levelup(hd) ) {
  910. break;
  911. }
  912. }
  913. if(hd->homunculus.class_ == hd->homunculusDB->evo_class) {
  914. //Evolved bonuses
  915. struct s_homunculus *hom = &hd->homunculus;
  916. struct h_stats *max = &hd->homunculusDB->emax, *min = &hd->homunculusDB->emin;
  917. hom->max_hp += rnd_value(min->HP, max->HP);
  918. hom->max_sp += rnd_value(min->SP, max->SP);
  919. hom->str += 10*rnd_value(min->str, max->str);
  920. hom->agi += 10*rnd_value(min->agi, max->agi);
  921. hom->vit += 10*rnd_value(min->vit, max->vit);
  922. hom->int_+= 10*rnd_value(min->int_,max->int_);
  923. hom->dex += 10*rnd_value(min->dex, max->dex);
  924. hom->luk += 10*rnd_value(min->luk, max->luk);
  925. }
  926. hd->homunculus.exp = exp;
  927. memcpy(&hd->homunculus.hskill, &b_skill, sizeof(b_skill));
  928. hd->homunculus.skillpts = skillpts;
  929. clif_homskillinfoblock(sd);
  930. status_calc_homunculus(hd,0);
  931. status_percent_heal(&hd->bl, 100, 100);
  932. clif_specialeffect(&hd->bl,568,AREA);
  933. return 1;
  934. }
  935. static bool read_homunculusdb_sub(char* str[], int columns, int current)
  936. {
  937. int classid;
  938. struct s_homunculus_db *db;
  939. //Base Class,Evo Class
  940. classid = atoi(str[0]);
  941. if (classid < HM_CLASS_BASE || classid > HM_CLASS_MAX)
  942. {
  943. ShowError("read_homunculusdb : Invalid class %d\n", classid);
  944. return false;
  945. }
  946. db = &homunculus_db[current];
  947. db->base_class = classid;
  948. classid = atoi(str[1]);
  949. if (classid < HM_CLASS_BASE || classid > HM_CLASS_MAX)
  950. {
  951. db->base_class = 0;
  952. ShowError("read_homunculusdb : Invalid class %d\n", classid);
  953. return false;
  954. }
  955. db->evo_class = classid;
  956. //Name, Food, Hungry Delay, Base Size, Evo Size, Race, Element, ASPD
  957. safestrncpy(db->name,str[2],NAME_LENGTH-1);
  958. db->foodID = atoi(str[3]);
  959. db->hungryDelay = atoi(str[4]);
  960. db->base_size = atoi(str[5]);
  961. db->evo_size = atoi(str[6]);
  962. db->race = atoi(str[7]);
  963. db->element = atoi(str[8]);
  964. db->baseASPD = atoi(str[9]);
  965. //base HP, SP, str, agi, vit, int, dex, luk
  966. db->base.HP = atoi(str[10]);
  967. db->base.SP = atoi(str[11]);
  968. db->base.str = atoi(str[12]);
  969. db->base.agi = atoi(str[13]);
  970. db->base.vit = atoi(str[14]);
  971. db->base.int_= atoi(str[15]);
  972. db->base.dex = atoi(str[16]);
  973. db->base.luk = atoi(str[17]);
  974. //Growth Min/Max HP, SP, str, agi, vit, int, dex, luk
  975. db->gmin.HP = atoi(str[18]);
  976. db->gmax.HP = atoi(str[19]);
  977. db->gmin.SP = atoi(str[20]);
  978. db->gmax.SP = atoi(str[21]);
  979. db->gmin.str = atoi(str[22]);
  980. db->gmax.str = atoi(str[23]);
  981. db->gmin.agi = atoi(str[24]);
  982. db->gmax.agi = atoi(str[25]);
  983. db->gmin.vit = atoi(str[26]);
  984. db->gmax.vit = atoi(str[27]);
  985. db->gmin.int_= atoi(str[28]);
  986. db->gmax.int_= atoi(str[29]);
  987. db->gmin.dex = atoi(str[30]);
  988. db->gmax.dex = atoi(str[31]);
  989. db->gmin.luk = atoi(str[32]);
  990. db->gmax.luk = atoi(str[33]);
  991. //Evolution Min/Max HP, SP, str, agi, vit, int, dex, luk
  992. db->emin.HP = atoi(str[34]);
  993. db->emax.HP = atoi(str[35]);
  994. db->emin.SP = atoi(str[36]);
  995. db->emax.SP = atoi(str[37]);
  996. db->emin.str = atoi(str[38]);
  997. db->emax.str = atoi(str[39]);
  998. db->emin.agi = atoi(str[40]);
  999. db->emax.agi = atoi(str[41]);
  1000. db->emin.vit = atoi(str[42]);
  1001. db->emax.vit = atoi(str[43]);
  1002. db->emin.int_= atoi(str[44]);
  1003. db->emax.int_= atoi(str[45]);
  1004. db->emin.dex = atoi(str[46]);
  1005. db->emax.dex = atoi(str[47]);
  1006. db->emin.luk = atoi(str[48]);
  1007. db->emax.luk = atoi(str[49]);
  1008. //Check that the min/max values really are below the other one.
  1009. if(db->gmin.HP > db->gmax.HP)
  1010. db->gmin.HP = db->gmax.HP;
  1011. if(db->gmin.SP > db->gmax.SP)
  1012. db->gmin.SP = db->gmax.SP;
  1013. if(db->gmin.str > db->gmax.str)
  1014. db->gmin.str = db->gmax.str;
  1015. if(db->gmin.agi > db->gmax.agi)
  1016. db->gmin.agi = db->gmax.agi;
  1017. if(db->gmin.vit > db->gmax.vit)
  1018. db->gmin.vit = db->gmax.vit;
  1019. if(db->gmin.int_> db->gmax.int_)
  1020. db->gmin.int_= db->gmax.int_;
  1021. if(db->gmin.dex > db->gmax.dex)
  1022. db->gmin.dex = db->gmax.dex;
  1023. if(db->gmin.luk > db->gmax.luk)
  1024. db->gmin.luk = db->gmax.luk;
  1025. if(db->emin.HP > db->emax.HP)
  1026. db->emin.HP = db->emax.HP;
  1027. if(db->emin.SP > db->emax.SP)
  1028. db->emin.SP = db->emax.SP;
  1029. if(db->emin.str > db->emax.str)
  1030. db->emin.str = db->emax.str;
  1031. if(db->emin.agi > db->emax.agi)
  1032. db->emin.agi = db->emax.agi;
  1033. if(db->emin.vit > db->emax.vit)
  1034. db->emin.vit = db->emax.vit;
  1035. if(db->emin.int_> db->emax.int_)
  1036. db->emin.int_= db->emax.int_;
  1037. if(db->emin.dex > db->emax.dex)
  1038. db->emin.dex = db->emax.dex;
  1039. if(db->emin.luk > db->emax.luk)
  1040. db->emin.luk = db->emax.luk;
  1041. return true;
  1042. }
  1043. int read_homunculusdb(void)
  1044. {
  1045. int i;
  1046. const char *filename[]={DBPATH"homunculus_db.txt","import/homunculus_db.txt"};
  1047. memset(homunculus_db,0,sizeof(homunculus_db));
  1048. for(i = 0; i<ARRAYLENGTH(filename); i++){
  1049. sv_readdb(db_path, filename[i], ',', 50, 50, MAX_HOMUNCULUS_CLASS, &read_homunculusdb_sub, i);
  1050. }
  1051. return 0;
  1052. }
  1053. static bool read_homunculus_skilldb_sub(char* split[], int columns, int current)
  1054. {// <hom class>,<skill id>,<max level>[,<job level>],<req id1>,<req lv1>,<req id2>,<req lv2>,<req id3>,<req lv3>,<req id4>,<req lv4>,<req id5>,<req lv5>,<intimacy lv req>
  1055. int k, classid;
  1056. int j;
  1057. int minJobLevelPresent = 0;
  1058. if( columns == 14 )
  1059. minJobLevelPresent = 1; // MinJobLvl has been added
  1060. // check for bounds [celest]
  1061. classid = atoi(split[0]) - HM_CLASS_BASE;
  1062. if ( classid >= MAX_HOMUNCULUS_CLASS )
  1063. {
  1064. ShowWarning("read_homunculus_skilldb: Invalud homunculus class %d.\n", atoi(split[0]));
  1065. return false;
  1066. }
  1067. k = atoi(split[1]); //This is to avoid adding two lines for the same skill. [Skotlex]
  1068. // Search an empty line or a line with the same skill_id (stored in j)
  1069. ARR_FIND( 0, MAX_SKILL_TREE, j, !hskill_tree[classid][j].id || hskill_tree[classid][j].id == k );
  1070. if (j == MAX_SKILL_TREE)
  1071. {
  1072. ShowWarning("Unable to load skill %d into homunculus %d's tree. Maximum number of skills per class has been reached.\n", k, classid);
  1073. return false;
  1074. }
  1075. hskill_tree[classid][j].id = k;
  1076. hskill_tree[classid][j].max = atoi(split[2]);
  1077. if (minJobLevelPresent)
  1078. hskill_tree[classid][j].joblv = atoi(split[3]);
  1079. for( k = 0; k < MAX_PC_SKILL_REQUIRE; k++ )
  1080. {
  1081. hskill_tree[classid][j].need[k].id = atoi(split[3+k*2+minJobLevelPresent]);
  1082. hskill_tree[classid][j].need[k].lv = atoi(split[3+k*2+minJobLevelPresent+1]);
  1083. }
  1084. hskill_tree[classid][j].intimacylv = atoi(split[13+minJobLevelPresent]);
  1085. return true;
  1086. }
  1087. int read_homunculus_skilldb(void)
  1088. {
  1089. const char *filename[]={ "homun_skill_tree.txt","import/homun_skill_tree.txt"};
  1090. int i;
  1091. memset(hskill_tree,0,sizeof(hskill_tree));
  1092. for(i = 0; i<ARRAYLENGTH(filename); i++){
  1093. sv_readdb(db_path, filename[i], ',', 13, 15, -1, &read_homunculus_skilldb_sub, i);
  1094. }
  1095. return 0;
  1096. }
  1097. void read_homunculus_expdb(void)
  1098. {
  1099. FILE *fp;
  1100. char line[1024];
  1101. int i, j=0;
  1102. char *filename[]={
  1103. DBPATH"exp_homun.txt",
  1104. "import/exp_homun.txt"
  1105. };
  1106. memset(hexptbl,0,sizeof(hexptbl));
  1107. for(i=0; i<ARRAYLENGTH(filename); i++){
  1108. sprintf(line, "%s/%s", db_path, filename[i]);
  1109. fp=fopen(line,"r");
  1110. if(fp == NULL){
  1111. if(i != 0)
  1112. continue;
  1113. if(i==0) ShowError("can't read %s\n",line);
  1114. return;
  1115. }
  1116. while(fgets(line, sizeof(line), fp) && j < MAX_LEVEL)
  1117. {
  1118. if(line[0] == '/' && line[1] == '/')
  1119. continue;
  1120. hexptbl[j] = strtoul(line, NULL, 10);
  1121. if (!hexptbl[j++])
  1122. break;
  1123. }
  1124. if (hexptbl[MAX_LEVEL - 1]) // Last permitted level have to be 0!
  1125. {
  1126. ShowWarning("read_hexptbl: Reached max level in %s [%d]. Remaining lines were not read.\n ",filename,MAX_LEVEL);
  1127. hexptbl[MAX_LEVEL - 1] = 0;
  1128. }
  1129. fclose(fp);
  1130. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' levels in '"CL_WHITE"%s"CL_RESET"'.\n", j, filename[i]);
  1131. }
  1132. }
  1133. void merc_reload(void)
  1134. {
  1135. read_homunculusdb();
  1136. read_homunculus_expdb();
  1137. }
  1138. void merc_skill_reload(void)
  1139. {
  1140. read_homunculus_skilldb();
  1141. }
  1142. int do_init_merc(void)
  1143. {
  1144. int class_;
  1145. read_homunculusdb();
  1146. read_homunculus_expdb();
  1147. read_homunculus_skilldb();
  1148. // Add homunc timer function to timer func list [Toms]
  1149. add_timer_func_list(merc_hom_hungry, "merc_hom_hungry");
  1150. //Stock view data for homuncs
  1151. memset(&hom_viewdb, 0, sizeof(hom_viewdb));
  1152. for (class_ = 0; class_ < ARRAYLENGTH(hom_viewdb); class_++)
  1153. hom_viewdb[class_].class_ = HM_CLASS_BASE+class_;
  1154. return 0;
  1155. }
  1156. int do_final_merc(void);