mercenary.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <math.h>
  6. #include <limits.h>
  7. #include "../common/socket.h"
  8. #include "../common/timer.h"
  9. #include "../common/nullpo.h"
  10. #include "../common/mmo.h"
  11. #include "../common/showmsg.h"
  12. #include "log.h"
  13. #include "clif.h"
  14. #include "chrif.h"
  15. #include "intif.h"
  16. #include "itemdb.h"
  17. #include "map.h"
  18. #include "pc.h"
  19. #include "status.h"
  20. #include "skill.h"
  21. #include "mob.h"
  22. #include "pet.h"
  23. #include "battle.h"
  24. #include "party.h"
  25. #include "guild.h"
  26. #include "atcommand.h"
  27. #include "script.h"
  28. #include "npc.h"
  29. #include "trade.h"
  30. #include "unit.h"
  31. #include "mercenary.h"
  32. #include "charsave.h"
  33. //Better equiprobability than rand()% [orn]
  34. #define rand(a, b) a+(int) ((float)(b-a+1)*rand()/(RAND_MAX+1.0))
  35. struct homunculus_db homunculus_db[MAX_HOMUNCULUS_CLASS]; //[orn]
  36. struct skill_tree_entry hskill_tree[MAX_HOMUNCULUS_CLASS][MAX_SKILL_TREE];
  37. static int merc_hom_hungry(int tid,unsigned int tick,int id,int data);
  38. static unsigned int hexptbl[MAX_LEVEL];
  39. void merc_damage(struct homun_data *hd,struct block_list *src,int hp,int sp)
  40. {
  41. clif_hominfo(hd->master,hd,0);
  42. }
  43. int merc_hom_dead(struct homun_data *hd, struct block_list *src)
  44. {
  45. //There's no intimacy penalties on death (from Tharis)
  46. struct map_session_data *sd = hd->master;
  47. clif_emotion(&hd->bl, 16) ; //wah
  48. //Delete timers when dead.
  49. merc_hom_hungry_timer_delete(hd);
  50. hd->homunculus.hp = 0;
  51. if (!sd) //unit remove map will invoke unit free
  52. return 3;
  53. clif_hominfo(sd,hd,0); // Send dead flag
  54. clif_emotion(&sd->bl, 28) ; //sob
  55. //Remove from map (if it has no intimacy, it is auto-removed from memory)
  56. return 3;
  57. }
  58. //Vaporize a character's homun. If flag, HP needs to be 80% or above.
  59. int merc_hom_vaporize(struct map_session_data *sd, int flag)
  60. {
  61. struct homun_data *hd;
  62. nullpo_retr(0, sd);
  63. hd = sd->hd;
  64. if (!hd || hd->homunculus.vaporize)
  65. return 0;
  66. if (status_isdead(&hd->bl))
  67. return 0; //Can't vaporize a dead homun.
  68. if (flag && hd->battle_status.hp < (hd->battle_status.max_hp*80/100))
  69. return 0;
  70. hd->regen.state.block = 3; //Block regen while vaporized.
  71. //Delete timers when vaporized.
  72. merc_hom_hungry_timer_delete(hd);
  73. hd->homunculus.vaporize = 1;
  74. clif_hominfo(sd, sd->hd, 0);
  75. merc_save(hd);
  76. return unit_remove_map(&hd->bl, 0);
  77. }
  78. //delete a homunculus, completely "killing it".
  79. //Emote is the emotion the master should use, send negative to disable.
  80. int merc_hom_delete(struct homun_data *hd, int emote)
  81. {
  82. struct map_session_data *sd;
  83. nullpo_retr(0, hd);
  84. sd = hd->master;
  85. if (!sd)
  86. return unit_free(&hd->bl,1);
  87. if (emote >= 0)
  88. clif_emotion(&sd->bl, emote);
  89. //This makes it be deleted right away.
  90. hd->homunculus.intimacy = 0;
  91. // Send homunculus_dead to client
  92. hd->homunculus.hp = 0;
  93. clif_hominfo(sd, hd, 0);
  94. return unit_free(&hd->bl,0);
  95. }
  96. int merc_hom_calc_skilltree(struct homun_data *hd)
  97. {
  98. int i,id=0 ;
  99. int j,f=1;
  100. int c=0;
  101. nullpo_retr(0, hd);
  102. c = hd->homunculus.class_ - HM_CLASS_BASE;
  103. for(i=0;i < MAX_SKILL_TREE && (id = hskill_tree[c][i].id) > 0;i++)
  104. {
  105. if(hd->homunculus.hskill[id-HM_SKILLBASE-1].id)
  106. continue; //Skill already known.
  107. if(!battle_config.skillfree)
  108. {
  109. for(j=0;j<5;j++)
  110. {
  111. if( hskill_tree[c][i].need[j].id &&
  112. merc_hom_checkskill(hd,hskill_tree[c][i].need[j].id) < hskill_tree[c][i].need[j].lv)
  113. {
  114. f=0;
  115. break;
  116. }
  117. }
  118. }
  119. if (f)
  120. hd->homunculus.hskill[id-HM_SKILLBASE-1].id = id ;
  121. }
  122. return 0;
  123. }
  124. int merc_hom_checkskill(struct homun_data *hd,int skill_id)
  125. {
  126. int i = skill_id - HM_SKILLBASE - 1;
  127. if(!hd)
  128. return 0;
  129. if(hd->homunculus.hskill[i].id == skill_id)
  130. return (hd->homunculus.hskill[i].lv);
  131. return 0;
  132. }
  133. int merc_skill_tree_get_max(int id, int b_class){
  134. int i, skillid;
  135. b_class -= HM_CLASS_BASE;
  136. for(i=0;(skillid=hskill_tree[b_class][i].id)>0;i++)
  137. if (id == skillid)
  138. return hskill_tree[b_class][i].max;
  139. return skill_get_max(id);
  140. }
  141. void merc_hom_skillup(struct homun_data *hd,int skillnum)
  142. {
  143. int i = 0 ;
  144. nullpo_retv(hd);
  145. if(!hd->homunculus.vaporize)
  146. {
  147. i = skillnum - HM_SKILLBASE - 1 ;
  148. if( hd->homunculus.skillpts > 0 &&
  149. hd->homunculus.hskill[i].id &&
  150. hd->homunculus.hskill[i].flag == 0 && //Don't allow raising while you have granted skills. [Skotlex]
  151. hd->homunculus.hskill[i].lv < merc_skill_tree_get_max(skillnum, hd->homunculus.class_)
  152. )
  153. {
  154. hd->homunculus.hskill[i].lv++ ;
  155. hd->homunculus.skillpts-- ;
  156. status_calc_homunculus(hd,0) ;
  157. if (hd->master) {
  158. clif_homskillup(hd->master, skillnum);
  159. clif_hominfo(hd->master,hd,0);
  160. clif_homskillinfoblock(hd->master);
  161. }
  162. }
  163. }
  164. }
  165. int merc_hom_levelup(struct homun_data *hd)
  166. {
  167. int growth_str, growth_agi, growth_vit, growth_int, growth_dex, growth_luk ;
  168. int growth_max_hp, growth_max_sp ;
  169. char output[256] ;
  170. if (hd->homunculus.level == MAX_LEVEL || !hd->exp_next || hd->homunculus.exp < hd->exp_next)
  171. return 0 ;
  172. hd->homunculus.level++ ;
  173. if (!(hd->homunculus.level % 3))
  174. hd->homunculus.skillpts++ ; //1 skillpoint each 3 base level
  175. hd->homunculus.exp -= hd->exp_next ;
  176. hd->exp_next = hexptbl[hd->homunculus.level - 1] ;
  177. if ( hd->homunculusDB->gmaxHP <= hd->homunculusDB->gminHP )
  178. growth_max_hp = hd->homunculusDB->gminHP ;
  179. else
  180. growth_max_hp = rand(hd->homunculusDB->gminHP, hd->homunculusDB->gmaxHP) ;
  181. if ( hd->homunculusDB->gmaxSP <= hd->homunculusDB->gminSP )
  182. growth_max_sp = hd->homunculusDB->gminSP ;
  183. else
  184. growth_max_sp = rand(hd->homunculusDB->gminSP, hd->homunculusDB->gmaxSP) ;
  185. if ( hd->homunculusDB->gmaxSTR <= hd->homunculusDB->gminSTR )
  186. growth_str = hd->homunculusDB->gminSTR ;
  187. else
  188. growth_str = rand(hd->homunculusDB->gminSTR, hd->homunculusDB->gmaxSTR) ;
  189. if ( hd->homunculusDB->gmaxAGI <= hd->homunculusDB->gminAGI )
  190. growth_agi = hd->homunculusDB->gminAGI ;
  191. else
  192. growth_agi = rand(hd->homunculusDB->gminAGI, hd->homunculusDB->gmaxAGI) ;
  193. if ( hd->homunculusDB->gmaxVIT <= hd->homunculusDB->gminVIT )
  194. growth_vit = hd->homunculusDB->gminVIT ;
  195. else
  196. growth_vit = rand(hd->homunculusDB->gminVIT, hd->homunculusDB->gmaxVIT) ;
  197. if ( hd->homunculusDB->gmaxDEX <= hd->homunculusDB->gminDEX )
  198. growth_dex = hd->homunculusDB->gminDEX ;
  199. else
  200. growth_dex = rand(hd->homunculusDB->gminDEX, hd->homunculusDB->gmaxDEX) ;
  201. if ( hd->homunculusDB->gmaxINT <= hd->homunculusDB->gminINT )
  202. growth_int = hd->homunculusDB->gminINT ;
  203. else
  204. growth_int = rand(hd->homunculusDB->gminINT, hd->homunculusDB->gmaxINT) ;
  205. if ( hd->homunculusDB->gmaxLUK <= hd->homunculusDB->gminLUK )
  206. growth_luk = hd->homunculusDB->gminLUK ;
  207. else
  208. growth_luk = rand(hd->homunculusDB->gminLUK, hd->homunculusDB->gmaxLUK) ;
  209. hd->homunculus.max_hp += growth_max_hp;
  210. hd->homunculus.max_sp += growth_max_sp;
  211. hd->homunculus.str += growth_str ;
  212. hd->homunculus.agi += growth_agi ;
  213. hd->homunculus.vit += growth_vit ;
  214. hd->homunculus.dex += growth_dex ;
  215. hd->homunculus.int_ += growth_int ;
  216. hd->homunculus.luk += growth_luk ;
  217. if ( battle_config.homunculus_show_growth ) {
  218. sprintf(output,
  219. "Growth : hp:%d sp:%d str(%.2f) agi(%.2f) vit(%.2f) int(%.2f) dex(%.2f) luk(%.2f) ", growth_max_hp, growth_max_sp, growth_str/(float)10, growth_agi/(float)10, growth_vit/(float)10, growth_int/(float)10, growth_dex/(float)10, growth_luk/(float)10 ) ;
  220. clif_disp_onlyself(hd->master,output,strlen(output));
  221. }
  222. return 1 ;
  223. }
  224. int merc_hom_change_class(struct homun_data *hd, short class_)
  225. {
  226. int i;
  227. i = search_homunculusDB_index(class_,HOMUNCULUS_CLASS);
  228. if(i < 0)
  229. return 0;
  230. hd->homunculusDB = &homunculus_db[i];
  231. hd->homunculus.class_ = class_;
  232. status_set_viewdata(&hd->bl, class_);
  233. merc_hom_calc_skilltree(hd);
  234. return 1;
  235. }
  236. int merc_hom_evolution(struct homun_data *hd)
  237. {
  238. struct map_session_data *sd;
  239. nullpo_retr(0, hd);
  240. if(!hd->homunculusDB->evo_class)
  241. {
  242. clif_emotion(&hd->bl, 4) ; //swt
  243. return 0 ;
  244. }
  245. sd = hd->master;
  246. if (!sd)
  247. return 0;
  248. merc_hom_vaporize(sd, 0);
  249. if (!merc_hom_change_class(hd, hd->homunculusDB->evo_class)) {
  250. ShowError("merc_hom_evolution: Can't evoluate homunc from %d to %d", hd->homunculus.class_, hd->homunculusDB->evo_class);
  251. return 0;
  252. }
  253. hd->homunculus.intimacy = 500;
  254. merc_call_homunculus(sd);
  255. clif_emotion(&sd->bl, 21); //no1
  256. clif_misceffect2(&hd->bl,568);
  257. return 1 ;
  258. }
  259. int merc_hom_gainexp(struct homun_data *hd,int exp)
  260. {
  261. if(hd->homunculus.vaporize)
  262. return 1;
  263. if( hd->exp_next == 0 ) {
  264. hd->homunculus.exp = 0 ;
  265. return 0;
  266. }
  267. hd->homunculus.exp += exp;
  268. if(hd->homunculus.exp < hd->exp_next) {
  269. clif_hominfo(hd->master,hd,0);
  270. return 0;
  271. }
  272. //levelup
  273. do
  274. {
  275. merc_hom_levelup(hd) ;
  276. }
  277. while(hd->homunculus.exp > hd->exp_next && hd->exp_next != 0 );
  278. if( hd->exp_next == 0 )
  279. hd->homunculus.exp = 0 ;
  280. clif_misceffect2(&hd->bl,568);
  281. status_calc_homunculus(hd,0);
  282. status_percent_heal(&hd->bl, 100, 100);
  283. return 0;
  284. }
  285. // Return the new value
  286. int merc_hom_increase_intimacy(struct homun_data * hd, unsigned int value)
  287. {
  288. if (battle_config.homunculus_friendly_rate != 100)
  289. value = (value * battle_config.homunculus_friendly_rate) / 100;
  290. if (hd->homunculus.intimacy + value <= 100000)
  291. hd->homunculus.intimacy += value;
  292. else
  293. hd->homunculus.intimacy = 100000;
  294. return hd->homunculus.intimacy;
  295. }
  296. // Return 0 if decrease fails or intimacy became 0 else the new value
  297. int merc_hom_decrease_intimacy(struct homun_data * hd, unsigned int value)
  298. {
  299. if (hd->homunculus.intimacy >= value)
  300. hd->homunculus.intimacy -= value;
  301. else
  302. hd->homunculus.intimacy = 0;
  303. return hd->homunculus.intimacy;
  304. }
  305. void merc_hom_heal(struct homun_data *hd,int hp,int sp)
  306. {
  307. clif_hominfo(hd->master,hd,0);
  308. }
  309. void merc_save(struct homun_data *hd)
  310. {
  311. // copy data that must be saved in homunculus struct ( hp / sp )
  312. TBL_PC * sd = hd->master;
  313. //Do not check for max_hp/max_sp caps as current could be higher to max due
  314. //to status changes/skills (they will be capped as needed upon stat
  315. //calculation on login)
  316. hd->homunculus.hp = hd->battle_status.hp;
  317. hd->homunculus.sp = hd->battle_status.sp;
  318. intif_homunculus_requestsave(sd->status.account_id, &hd->homunculus) ;
  319. }
  320. int merc_menu(struct map_session_data *sd,int menunum)
  321. {
  322. nullpo_retr(0, sd);
  323. if (sd->hd == NULL)
  324. return 1;
  325. switch(menunum) {
  326. case 0:
  327. break;
  328. case 1:
  329. merc_hom_food(sd, sd->hd);
  330. break;
  331. case 2:
  332. merc_hom_delete(sd->hd, -1);
  333. break;
  334. default:
  335. ShowError("merc_menu : unknown menu choice : %d\n", menunum) ;
  336. break;
  337. }
  338. return 0;
  339. }
  340. int merc_hom_food(struct map_session_data *sd, struct homun_data *hd)
  341. {
  342. int i, foodID, emotion;
  343. if(hd->homunculus.vaporize)
  344. return 1 ;
  345. foodID = hd->homunculusDB->foodID;
  346. i = pc_search_inventory(sd,foodID);
  347. if(i < 0) {
  348. clif_hom_food(sd,foodID,0);
  349. return 1;
  350. }
  351. pc_delitem(sd,i,1,0);
  352. if ( hd->homunculus.hunger >= 91 ) {
  353. merc_hom_decrease_intimacy(hd, 50);
  354. emotion = 16;
  355. } else if ( hd->homunculus.hunger >= 76 ) {
  356. merc_hom_decrease_intimacy(hd, 5);
  357. emotion = 19;
  358. } else if ( hd->homunculus.hunger >= 26 ) {
  359. merc_hom_increase_intimacy(hd, 75);
  360. emotion = 2;
  361. } else if ( hd->homunculus.hunger >= 11 ) {
  362. merc_hom_increase_intimacy(hd, 100);
  363. emotion = 2;
  364. } else {
  365. merc_hom_increase_intimacy(hd, 50);
  366. emotion = 2;
  367. }
  368. hd->homunculus.hunger += 10; //dunno increase value for each food
  369. if(hd->homunculus.hunger > 100)
  370. hd->homunculus.hunger = 100;
  371. clif_emotion(&hd->bl,emotion) ;
  372. clif_send_homdata(sd,SP_HUNGRY,hd->homunculus.hunger);
  373. clif_send_homdata(sd,SP_INTIMATE,hd->homunculus.intimacy / 100);
  374. clif_hom_food(sd,foodID,1);
  375. // Too much food :/
  376. if(hd->homunculus.intimacy == 0)
  377. return merc_hom_delete(sd->hd, 23); //omg
  378. return 0;
  379. }
  380. static int merc_hom_hungry(int tid,unsigned int tick,int id,int data)
  381. {
  382. struct map_session_data *sd;
  383. struct homun_data *hd;
  384. sd=map_id2sd(id);
  385. if(!sd)
  386. return 1;
  387. if(!sd->status.hom_id || !(hd=sd->hd))
  388. return 1;
  389. if(hd->hungry_timer != tid){
  390. if(battle_config.error_log)
  391. ShowError("merc_hom_hungry_timer %d != %d\n",hd->hungry_timer,tid);
  392. return 0;
  393. }
  394. hd->hungry_timer = -1;
  395. hd->homunculus.hunger-- ;
  396. if(hd->homunculus.hunger <= 10) {
  397. clif_emotion(&hd->bl, 6) ; //an
  398. } else if(hd->homunculus.hunger == 25) {
  399. clif_emotion(&hd->bl, 20) ; //hmm
  400. } else if(hd->homunculus.hunger == 75) {
  401. clif_emotion(&hd->bl, 33) ; //ok
  402. }
  403. if(hd->homunculus.hunger < 0) {
  404. hd->homunculus.hunger = 0;
  405. // Delete the homunculus if intimacy <= 100
  406. if ( !merc_hom_decrease_intimacy(hd, 100) )
  407. return merc_hom_delete(hd, 23); //omg
  408. clif_send_homdata(sd,SP_INTIMATE,hd->homunculus.intimacy / 100);
  409. }
  410. clif_send_homdata(sd,SP_HUNGRY,hd->homunculus.hunger);
  411. hd->hungry_timer = add_timer(tick+hd->homunculusDB->hungryDelay,merc_hom_hungry,sd->bl.id,0); //simple Fix albator
  412. return 0;
  413. }
  414. int merc_hom_hungry_timer_delete(struct homun_data *hd)
  415. {
  416. nullpo_retr(0, hd);
  417. if(hd->hungry_timer != -1) {
  418. delete_timer(hd->hungry_timer,merc_hom_hungry);
  419. hd->hungry_timer = -1;
  420. }
  421. return 1;
  422. }
  423. int search_homunculusDB_index(int key,int type)
  424. {
  425. int i;
  426. for(i=0;i<MAX_HOMUNCULUS_CLASS;i++) {
  427. if(homunculus_db[i].class_ <= 0)
  428. continue;
  429. switch(type) {
  430. case HOMUNCULUS_CLASS:
  431. if(homunculus_db[i].class_ == key)
  432. return i;
  433. break;
  434. case HOMUNCULUS_FOOD:
  435. if(homunculus_db[i].foodID == key)
  436. return i;
  437. break;
  438. default:
  439. return -1;
  440. }
  441. }
  442. return -1;
  443. }
  444. // Create homunc structure
  445. int merc_hom_alloc(struct map_session_data *sd, struct s_homunculus *hom)
  446. {
  447. struct homun_data *hd;
  448. int i = 0;
  449. short x,y;
  450. nullpo_retr(1, sd);
  451. Assert((sd->status.hom_id == 0 || sd->hd == 0) || sd->hd->master == sd);
  452. i = search_homunculusDB_index(hom->class_,HOMUNCULUS_CLASS);
  453. if(i < 0) {
  454. ShowError("merc_hom_alloc: unknown homunculus class [%d]", hom->class_);
  455. sd->status.hom_id = 0;
  456. intif_homunculus_requestdelete(hom->hom_id);
  457. return 1;
  458. }
  459. sd->hd = hd = aCalloc(1,sizeof(struct homun_data));
  460. hd->bl.subtype = MONS;
  461. hd->bl.type = BL_HOM;
  462. hd->bl.id = npc_get_new_npc_id();
  463. hd->master = sd;
  464. hd->homunculusDB = &homunculus_db[i];
  465. memcpy(&hd->homunculus, hom, sizeof(struct s_homunculus));
  466. hd->exp_next = hexptbl[hd->homunculus.level - 1];
  467. status_set_viewdata(&hd->bl, hd->homunculus.class_);
  468. status_change_init(&hd->bl);
  469. unit_dataset(&hd->bl);
  470. hd->ud.dir = sd->ud.dir;
  471. // Find a random valid pos around the player
  472. hd->bl.m = sd->bl.m;
  473. hd->bl.x = sd->bl.x;
  474. hd->bl.y = sd->bl.y;
  475. x = sd->bl.x + 1;
  476. y = sd->bl.y + 1;
  477. map_random_dir(&hd->bl, &x, &y);
  478. hd->bl.x = x;
  479. hd->bl.y = y;
  480. map_addiddb(&hd->bl);
  481. status_calc_homunculus(hd,1);
  482. hd->hungry_timer = -1;
  483. return 0;
  484. }
  485. void merc_hom_init_timers(struct homun_data * hd)
  486. {
  487. if (hd->hungry_timer == -1)
  488. hd->hungry_timer = add_timer(gettick()+hd->homunculusDB->hungryDelay,merc_hom_hungry,hd->master->bl.id,0);
  489. hd->regen.state.block = 0; //Restore HP/SP block.
  490. }
  491. int merc_call_homunculus(struct map_session_data *sd)
  492. {
  493. struct homun_data *hd;
  494. if (!sd->status.hom_id) //Create a new homun.
  495. return merc_create_homunculus_request(sd, HM_CLASS_BASE + rand(0, 7)) ;
  496. // If homunc not yet loaded, load it
  497. if (!sd->hd)
  498. return intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  499. hd = sd->hd;
  500. if (!hd->homunculus.vaporize)
  501. return 0; //Can't use this if homun wasn't vaporized.
  502. merc_hom_init_timers(hd);
  503. hd->homunculus.vaporize = 0;
  504. if (hd->bl.prev == NULL)
  505. { //Spawn him
  506. hd->bl.x = sd->bl.x;
  507. hd->bl.y = sd->bl.y;
  508. hd->bl.m = sd->bl.m;
  509. map_addblock(&hd->bl);
  510. clif_spawn(&hd->bl);
  511. clif_send_homdata(sd,SP_ACK,0);
  512. clif_hominfo(sd,hd,1);
  513. clif_hominfo(sd,hd,0); // send this x2. dunno why, but kRO does that [blackhole89]
  514. clif_homskillinfoblock(sd);
  515. merc_save(hd);
  516. } else
  517. //Warp him to master.
  518. unit_warp(&hd->bl,sd->bl.m, sd->bl.x, sd->bl.y,0);
  519. return 1;
  520. }
  521. // Recv homunculus data from char server
  522. int merc_hom_recv_data(int account_id, struct s_homunculus *sh, int flag)
  523. {
  524. struct map_session_data *sd;
  525. struct homun_data *hd;
  526. sd = map_id2sd(account_id);
  527. if(!sd)
  528. return 0;
  529. if (sd->char_id != sh->char_id)
  530. {
  531. if (sd->status.hom_id == sh->hom_id)
  532. sh->char_id = sd->char_id; //Correct char id.
  533. else
  534. return 0;
  535. }
  536. if(!flag) { // Failed to load
  537. sd->status.hom_id = 0;
  538. return 0;
  539. }
  540. if (!sd->status.hom_id) //Hom just created.
  541. sd->status.hom_id = sh->hom_id;
  542. if (sd->hd) //uh? Overwrite the data.
  543. memcpy(&sd->hd->homunculus, sh, sizeof(struct s_homunculus));
  544. else
  545. merc_hom_alloc(sd, sh);
  546. hd = sd->hd;
  547. if(hd->homunculus.hp && !hd->homunculus.vaporize &&
  548. hd->bl.prev == NULL && sd->bl.prev != NULL)
  549. {
  550. map_addblock(&hd->bl);
  551. clif_spawn(&hd->bl);
  552. clif_hominfo(sd,hd,1);
  553. clif_hominfo(sd,hd,0); // send this x2. dunno why, but kRO does that [blackhole89]
  554. clif_homskillinfoblock(sd);
  555. clif_hominfo(sd,hd,0);
  556. clif_send_homdata(sd,SP_ACK,0);
  557. merc_hom_init_timers(hd);
  558. }
  559. return 1;
  560. }
  561. // Ask homunculus creation to char server
  562. int merc_create_homunculus_request(struct map_session_data *sd, int class_)
  563. {
  564. struct s_homunculus homun;
  565. int i;
  566. nullpo_retr(1, sd);
  567. i = search_homunculusDB_index(class_,HOMUNCULUS_CLASS);
  568. if(i < 0) return 0;
  569. memset(&homun, 0, sizeof(struct s_homunculus));
  570. //Initial data
  571. strncpy(homun.name, homunculus_db[i].name, NAME_LENGTH-1);
  572. homun.class_ = class_;
  573. homun.level = 1;
  574. // FIXME: Commented value is what the map-server had as initial value,
  575. // Uncommented value is what the char-server was overwriting it with
  576. // So which one is correct?
  577. // homun.hunger = 50;
  578. homun.hunger = 32;
  579. // homun.intimacy = 500;
  580. homun.intimacy = 21;
  581. homun.char_id = sd->status.char_id;
  582. homun.hp = 10 ;
  583. homun.max_hp = homunculus_db[i].basemaxHP;
  584. homun.max_sp = homunculus_db[i].basemaxSP;
  585. homun.str = homunculus_db[i].baseSTR * 10;
  586. homun.agi = homunculus_db[i].baseAGI * 10;
  587. homun.vit = homunculus_db[i].baseVIT * 10;
  588. homun.int_ = homunculus_db[i].baseINT * 10;
  589. homun.dex = homunculus_db[i].baseDEX * 10;
  590. homun.luk = homunculus_db[i].baseLUK * 10;
  591. // Request homunculus creation
  592. intif_homunculus_create(sd->status.account_id, &homun);
  593. return 1;
  594. }
  595. int merc_resurrect_homunculus(struct map_session_data *sd, unsigned char per, short x, short y)
  596. {
  597. struct homun_data *hd;
  598. nullpo_retr(0, sd);
  599. if (!sd->status.hom_id)
  600. return 0;
  601. if (!sd->hd) //Load homun data;
  602. return intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  603. hd = sd->hd;
  604. if (hd->homunculus.vaporize)
  605. return 0;
  606. if (!status_isdead(&hd->bl))
  607. return 0;
  608. merc_hom_init_timers(hd);
  609. if (!hd->bl.prev)
  610. { //Add it back to the map.
  611. hd->bl.m = sd->bl.m;
  612. hd->bl.x = x;
  613. hd->bl.y = y;
  614. map_addblock(&hd->bl);
  615. clif_spawn(&hd->bl);
  616. }
  617. status_revive(&hd->bl, per, 0);
  618. return 1;
  619. }
  620. void merc_hom_revive(struct homun_data *hd, unsigned int hp, unsigned int sp)
  621. {
  622. struct map_session_data *sd = hd->master;
  623. hd->homunculus.hp = hd->battle_status.hp;
  624. if (!sd)
  625. return;
  626. clif_send_homdata(sd,SP_ACK,0);
  627. clif_hominfo(sd,hd,1);
  628. clif_hominfo(sd,hd,0);
  629. clif_homskillinfoblock(sd);
  630. }
  631. int read_homunculusdb(void)
  632. {
  633. FILE *fp;
  634. char line[1024], *p;
  635. int i, k, classid;
  636. int j = 0;
  637. char *filename[]={"homunculus_db.txt","homunculus_db2.txt"};
  638. char *str[36];
  639. malloc_set(homunculus_db,0,sizeof(homunculus_db));
  640. for(i = 0; i<2; i++)
  641. {
  642. sprintf(line, "%s/%s", db_path, filename[i]);
  643. fp = fopen(line,"r");
  644. if(!fp){
  645. if(i != 0)
  646. continue;
  647. ShowError("read_homunculusdb : can't read %s\n", line);
  648. return -1;
  649. }
  650. while(fgets(line,sizeof(line)-1,fp) && j < MAX_HOMUNCULUS_CLASS)
  651. {
  652. if(line[0] == '/' && line[1] == '/')
  653. continue;
  654. k = 0;
  655. p = strtok (line,",");
  656. while (p != NULL && k < 36)
  657. {
  658. str[k++] = p;
  659. p = strtok (NULL, ",");
  660. }
  661. classid = atoi(str[0]);
  662. if (k != 36 || classid < HM_CLASS_BASE || classid > HM_CLASS_MAX)
  663. {
  664. ShowError("read_homunculusdb : Error reading %s", filename[i]);
  665. continue;
  666. }
  667. //Class,Homunculus,HP,SP,ATK,MATK,HIT,CRI,DEF,MDEF,FLEE,ASPD,STR,AGI,VIT,INT,DEX,LUK
  668. homunculus_db[j].class_ = classid;
  669. strncpy(homunculus_db[j].name,str[1],NAME_LENGTH-1);
  670. homunculus_db[j].basemaxHP = atoi(str[2]);
  671. homunculus_db[j].basemaxSP = atoi(str[3]);
  672. homunculus_db[j].baseSTR = atoi(str[4]);
  673. homunculus_db[j].baseAGI = atoi(str[5]);
  674. homunculus_db[j].baseVIT = atoi(str[6]);
  675. homunculus_db[j].baseINT = atoi(str[7]);
  676. homunculus_db[j].baseDEX = atoi(str[8]);
  677. homunculus_db[j].baseLUK = atoi(str[9]);
  678. homunculus_db[j].baseIntimacy = atoi(str[10]);
  679. homunculus_db[j].baseHungry = atoi(str[11]);
  680. homunculus_db[j].hungryDelay = atoi(str[12]);
  681. homunculus_db[j].foodID = atoi(str[13]);
  682. homunculus_db[j].gminHP = atoi(str[14]);
  683. homunculus_db[j].gmaxHP = atoi(str[15]);
  684. homunculus_db[j].gminSP = atoi(str[16]);
  685. homunculus_db[j].gmaxSP = atoi(str[17]);
  686. homunculus_db[j].gminSTR = atoi(str[18]);
  687. homunculus_db[j].gmaxSTR = atoi(str[19]);
  688. homunculus_db[j].gminAGI = atoi(str[20]);
  689. homunculus_db[j].gmaxAGI = atoi(str[21]);
  690. homunculus_db[j].gminVIT = atoi(str[22]);
  691. homunculus_db[j].gmaxVIT = atoi(str[23]);
  692. homunculus_db[j].gminINT = atoi(str[24]);
  693. homunculus_db[j].gmaxINT = atoi(str[25]);
  694. homunculus_db[j].gminDEX = atoi(str[26]);
  695. homunculus_db[j].gmaxDEX = atoi(str[27]);
  696. homunculus_db[j].gminLUK = atoi(str[28]);
  697. homunculus_db[j].gmaxLUK = atoi(str[29]);
  698. homunculus_db[j].evo_class = atoi(str[30]);
  699. homunculus_db[j].baseASPD = atoi(str[31]);
  700. homunculus_db[j].size = atoi(str[32]);
  701. homunculus_db[j].race = atoi(str[33]);
  702. homunculus_db[j].element = atoi(str[34]);
  703. homunculus_db[j].accessID = atoi(str[35]);
  704. j++;
  705. }
  706. if (j > MAX_HOMUNCULUS_CLASS)
  707. ShowWarning("read_homunculusdb: Reached max number of homunculus [%d]. Remaining homunculus were not read.\n ", MAX_HOMUNCULUS_CLASS);
  708. fclose(fp);
  709. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' homunculus in '"CL_WHITE"db/%s"CL_RESET"'.\n",j,filename[i]);
  710. }
  711. return 0;
  712. }
  713. int read_homunculus_skilldb(void)
  714. {
  715. FILE *fp;
  716. char line[1024], *p;
  717. int k, classid;
  718. int j = 0;
  719. char *split[15];
  720. malloc_tsetdword(hskill_tree,0,sizeof(hskill_tree));
  721. sprintf(line, "%s/homun_skill_tree.txt", db_path);
  722. fp=fopen(line,"r");
  723. if(fp==NULL){
  724. ShowError("can't read %s\n", line);
  725. return 1;
  726. }
  727. while(fgets(line, sizeof(line)-1, fp))
  728. {
  729. int minJobLevelPresent = 0;
  730. if(line[0]=='/' && line[1]=='/')
  731. continue;
  732. k = 0;
  733. p = strtok(line,",");
  734. while (p != NULL && k < 15)
  735. {
  736. split[k++] = p;
  737. p = strtok(NULL, ",");
  738. }
  739. if(k < 13)
  740. continue;
  741. if (k == 14)
  742. minJobLevelPresent = 1; // MinJobLvl has been added
  743. // check for bounds [celest]
  744. classid = atoi(split[0]) - HM_CLASS_BASE;
  745. if ( classid >= MAX_HOMUNCULUS_CLASS )
  746. continue;
  747. k = atoi(split[1]); //This is to avoid adding two lines for the same skill. [Skotlex]
  748. // Search an empty line or a line with the same skill_id (stored in j)
  749. for(j = 0; j < MAX_SKILL_TREE && hskill_tree[classid][j].id && hskill_tree[classid][j].id != k; j++);
  750. if (j == MAX_SKILL_TREE)
  751. {
  752. ShowWarning("Unable to load skill %d into homunculus %d's tree. Maximum number of skills per class has been reached.\n", k, classid);
  753. continue;
  754. }
  755. hskill_tree[classid][j].id=k;
  756. hskill_tree[classid][j].max=atoi(split[2]);
  757. if (minJobLevelPresent)
  758. hskill_tree[classid][j].joblv=atoi(split[3]);
  759. for(k=0;k<5;k++){
  760. hskill_tree[classid][j].need[k].id=atoi(split[3+k*2+minJobLevelPresent]);
  761. hskill_tree[classid][j].need[k].lv=atoi(split[3+k*2+minJobLevelPresent+1]);
  762. }
  763. }
  764. fclose(fp);
  765. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","homun_skill_tree.txt");
  766. return 0;
  767. }
  768. void read_homunculus_expdb(void)
  769. {
  770. FILE *fp;
  771. char line[1024];
  772. int i, j=0;
  773. char *filename[]={"exp_homun.txt","exp_homun2.txt"};
  774. malloc_tsetdword(hexptbl,0,sizeof(hexptbl));
  775. for(i=0; i<2; i++){
  776. sprintf(line, "%s/%s", db_path, filename[i]);
  777. fp=fopen(line,"r");
  778. if(fp == NULL){
  779. if(i != 0)
  780. continue;
  781. ShowError("can't read %s\n",line);
  782. return;
  783. }
  784. while(fgets(line,sizeof(line)-1,fp) && j < MAX_LEVEL)
  785. {
  786. if(line[0] == '/' && line[1] == '/')
  787. continue;
  788. hexptbl[j] = strtoul(line, NULL, 10);
  789. if (!hexptbl[j++])
  790. break;
  791. }
  792. if (hexptbl[MAX_LEVEL - 1]) // Last permitted level have to be 0!
  793. {
  794. ShowWarning("read_hexptbl: Reached max level in exp_homun [%d]. Remaining lines were not read.\n ", MAX_LEVEL);
  795. hexptbl[MAX_LEVEL - 1] = 0;
  796. }
  797. fclose(fp);
  798. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' levels in '"CL_WHITE"%s"CL_RESET"'.\n", j, filename[i]);
  799. }
  800. }
  801. void merc_reload(void)
  802. {
  803. read_homunculusdb();
  804. read_homunculus_expdb();
  805. }
  806. void merc_skill_reload(void)
  807. {
  808. read_homunculus_skilldb();
  809. }
  810. int do_init_merc(void)
  811. {
  812. read_homunculusdb();
  813. read_homunculus_expdb();
  814. read_homunculus_skilldb();
  815. // Add homunc timer function to timer func list [Toms]
  816. add_timer_func_list(merc_hom_hungry, "merc_hom_hungry");
  817. return 0;
  818. }
  819. int do_final_merc(void);