pet.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "../common/db.h"
  7. #include "../common/timer.h"
  8. #include "../common/nullpo.h"
  9. #include "../common/malloc.h"
  10. #include "../common/showmsg.h"
  11. #include "../common/ers.h"
  12. #include "pc.h"
  13. #include "status.h"
  14. #include "map.h"
  15. #include "intif.h"
  16. #include "clif.h"
  17. #include "chrif.h"
  18. #include "pet.h"
  19. #include "itemdb.h"
  20. #include "battle.h"
  21. #include "mob.h"
  22. #include "npc.h"
  23. #include "script.h"
  24. #include "skill.h"
  25. #include "unit.h"
  26. #define MIN_PETTHINKTIME 100
  27. struct pet_db pet_db[MAX_PET_DB];
  28. static struct eri *item_drop_ers; //For loot drops delay structures.
  29. static struct eri *item_drop_list_ers;
  30. static int dirx[8]={0,-1,-1,-1,0,1,1,1};
  31. static int diry[8]={1,1,0,-1,-1,-1,0,1};
  32. int pet_hungry_val(struct map_session_data *sd)
  33. {
  34. nullpo_retr(0, sd);
  35. Assert((sd->status.pet_id == 0 || sd->pd == 0) || sd->pd->msd == sd);
  36. if(sd->pet.hungry > 90)
  37. return 4;
  38. else if(sd->pet.hungry > 75)
  39. return 3;
  40. else if(sd->pet.hungry > 25)
  41. return 2;
  42. else if(sd->pet.hungry > 10)
  43. return 1;
  44. else
  45. return 0;
  46. }
  47. static int pet_calc_pos(struct pet_data *pd,int tx,int ty,int dir)
  48. {
  49. int x,y,dx,dy;
  50. int i,k;
  51. nullpo_retr(0, pd);
  52. pd->ud.to_x = tx;
  53. pd->ud.to_y = ty;
  54. if(dir < 0 || dir >= 8)
  55. return 1;
  56. dx = -dirx[dir]*2;
  57. dy = -diry[dir]*2;
  58. x = tx + dx;
  59. y = ty + dy;
  60. if(!unit_can_reach_pos(&pd->bl,x,y,0)) {
  61. if(dx > 0) x--;
  62. else if(dx < 0) x++;
  63. if(dy > 0) y--;
  64. else if(dy < 0) y++;
  65. if(!unit_can_reach_pos(&pd->bl,x,y,0)) {
  66. for(i=0;i<12;i++) {
  67. k = rand()%8;
  68. dx = -dirx[k]*2;
  69. dy = -diry[k]*2;
  70. x = tx + dx;
  71. y = ty + dy;
  72. if(unit_can_reach_pos(&pd->bl,x,y,0))
  73. break;
  74. else {
  75. if(dx > 0) x--;
  76. else if(dx < 0) x++;
  77. if(dy > 0) y--;
  78. else if(dy < 0) y++;
  79. if(unit_can_reach_pos(&pd->bl,x,y,0))
  80. break;
  81. }
  82. }
  83. if(i>=12) {
  84. x = tx;
  85. y = ty;
  86. if(!unit_can_reach_pos(&pd->bl,x,y,0))
  87. return 1;
  88. }
  89. }
  90. }
  91. pd->ud.to_x = x;
  92. pd->ud.to_y = y;
  93. return 0;
  94. }
  95. int pet_unlocktarget(struct pet_data *pd)
  96. {
  97. nullpo_retr(0, pd);
  98. pd->target_id=0;
  99. pet_stop_attack(pd);
  100. pet_stop_walking(pd,1);
  101. return 0;
  102. }
  103. /*==========================================
  104. * Pet Attack Skill [Skotlex]
  105. *------------------------------------------
  106. */
  107. int pet_attackskill(struct pet_data *pd, int target_id)
  108. {
  109. struct block_list *bl;
  110. if (!battle_config.pet_status_support || !pd->a_skill ||
  111. (battle_config.pet_equip_required && !pd->equip))
  112. return 0;
  113. if (DIFF_TICK(pd->ud.canact_tick, gettick()) > 0)
  114. return 0;
  115. if (rand()%100 < (pd->a_skill->rate +pd->msd->pet.intimate*pd->a_skill->bonusrate/1000))
  116. { //Skotlex: Use pet's skill
  117. bl=map_id2bl(target_id);
  118. if(bl == NULL || pd->bl.m != bl->m || bl->prev == NULL || status_isdead(bl) ||
  119. !check_distance_bl(&pd->bl, bl, pd->db->range3))
  120. return 0;
  121. if (skill_get_inf(pd->a_skill->id) & INF_GROUND_SKILL)
  122. unit_skilluse_pos(&pd->bl, bl->x, bl->y, pd->a_skill->id, pd->a_skill->lv);
  123. else
  124. unit_skilluse_id(&pd->bl, bl->id, pd->a_skill->id, pd->a_skill->lv);
  125. return 1; //Skill invoked.
  126. }
  127. return 0;
  128. }
  129. int pet_target_check(struct map_session_data *sd,struct block_list *bl,int type)
  130. {
  131. struct pet_data *pd;
  132. int rate;
  133. pd = sd->pd;
  134. Assert((pd->msd == 0) || (pd->msd->pd == pd));
  135. if(bl == NULL || bl->type != BL_MOB || bl->prev == NULL ||
  136. sd->pet.intimate < battle_config.pet_support_min_friendly ||
  137. sd->pet.hungry < 1 ||
  138. pd->class_ == status_get_class(bl))
  139. return 0;
  140. if(pd->bl.m != bl->m ||
  141. !check_distance_bl(&pd->bl, bl, pd->db->range2))
  142. return 0;
  143. if (!status_check_skilluse(&pd->bl, bl, 0, 0))
  144. return 0;
  145. if(!type) {
  146. rate = pd->petDB->attack_rate;
  147. rate = rate * pd->rate_fix/1000;
  148. if(pd->petDB->attack_rate > 0 && rate <= 0)
  149. rate = 1;
  150. } else {
  151. rate = pd->petDB->defence_attack_rate;
  152. rate = rate * pd->rate_fix/1000;
  153. if(pd->petDB->defence_attack_rate > 0 && rate <= 0)
  154. rate = 1;
  155. }
  156. if(rand()%10000 < rate)
  157. {
  158. if(pd->target_id == 0 || rand()%10000 < pd->petDB->change_target_rate)
  159. pd->target_id = bl->id;
  160. }
  161. return 0;
  162. }
  163. /*==========================================
  164. * Pet SC Check [Skotlex]
  165. *------------------------------------------
  166. */
  167. int pet_sc_check(struct map_session_data *sd, int type)
  168. {
  169. struct pet_data *pd;
  170. nullpo_retr(0, sd);
  171. pd = sd->pd;
  172. if (pd == NULL ||
  173. (battle_config.pet_equip_required && pd->equip == 0) ||
  174. pd->recovery == NULL ||
  175. pd->recovery->timer != -1 ||
  176. pd->recovery->type != type)
  177. return 1;
  178. pd->recovery->timer = add_timer(gettick()+pd->recovery->delay*1000,pet_recovery_timer,sd->bl.id,0);
  179. return 0;
  180. }
  181. static int pet_hungry(int tid,unsigned int tick,int id,int data)
  182. {
  183. struct map_session_data *sd;
  184. struct pet_data *pd;
  185. int interval,t;
  186. sd=map_id2sd(id);
  187. if(!sd)
  188. return 1;
  189. if(!sd->status.pet_id || !sd->pd)
  190. return 1;
  191. pd = sd->pd;
  192. if(pd->pet_hungry_timer != tid){
  193. if(battle_config.error_log)
  194. ShowError("pet_hungry_timer %d != %d\n",pd->pet_hungry_timer,tid);
  195. return 0;
  196. }
  197. pd->pet_hungry_timer = -1;
  198. if (sd->pet.intimate <= 0)
  199. return 1; //You lost the pet already, the rest is irrelevant.
  200. sd->pet.hungry--;
  201. t = sd->pet.intimate;
  202. if(sd->pet.hungry < 0) {
  203. pet_stop_attack(pd);
  204. sd->pet.hungry = 0;
  205. sd->pet.intimate -= battle_config.pet_hungry_friendly_decrease;
  206. if(sd->pet.intimate <= 0) {
  207. sd->pet.intimate = 0;
  208. pd->status.speed = pd->db->status.speed;
  209. }
  210. status_calc_pet(pd, 0);
  211. clif_send_petdata(sd,1,sd->pet.intimate);
  212. }
  213. clif_send_petdata(sd,2,sd->pet.hungry);
  214. if(battle_config.pet_hungry_delay_rate != 100)
  215. interval = (pd->petDB->hungry_delay*battle_config.pet_hungry_delay_rate)/100;
  216. else
  217. interval = pd->petDB->hungry_delay;
  218. if(interval <= 0)
  219. interval = 1;
  220. pd->pet_hungry_timer = add_timer(tick+interval,pet_hungry,sd->bl.id,0);
  221. return 0;
  222. }
  223. int search_petDB_index(int key,int type)
  224. {
  225. int i;
  226. for(i=0;i<MAX_PET_DB;i++) {
  227. if(pet_db[i].class_ <= 0)
  228. continue;
  229. switch(type) {
  230. case PET_CLASS:
  231. if(pet_db[i].class_ == key)
  232. return i;
  233. break;
  234. case PET_CATCH:
  235. if(pet_db[i].itemID == key)
  236. return i;
  237. break;
  238. case PET_EGG:
  239. if(pet_db[i].EggID == key)
  240. return i;
  241. break;
  242. case PET_EQUIP:
  243. if(pet_db[i].AcceID == key)
  244. return i;
  245. break;
  246. case PET_FOOD:
  247. if(pet_db[i].FoodID == key)
  248. return i;
  249. break;
  250. default:
  251. return -1;
  252. }
  253. }
  254. return -1;
  255. }
  256. int pet_hungry_timer_delete(struct pet_data *pd)
  257. {
  258. nullpo_retr(0, pd);
  259. if(pd->pet_hungry_timer != -1) {
  260. delete_timer(pd->pet_hungry_timer,pet_hungry);
  261. pd->pet_hungry_timer = -1;
  262. }
  263. return 1;
  264. }
  265. static int pet_performance(struct map_session_data *sd, struct pet_data *pd)
  266. {
  267. int val;
  268. if (sd->pet.intimate > 900)
  269. val = (pd->petDB->s_perfor > 0)? 4:3;
  270. else if(sd->pet.intimate > 750)
  271. val = 2;
  272. else
  273. val = 1;
  274. pet_stop_walking(pd,2000<<8);
  275. clif_pet_performance(&pd->bl,rand()%val + 1);
  276. pet_lootitem_drop(pd,NULL);
  277. return 1;
  278. }
  279. static int pet_return_egg(struct map_session_data *sd, struct pet_data *pd)
  280. {
  281. struct item tmp_item;
  282. int flag;
  283. pet_lootitem_drop(pd,sd);
  284. memset(&tmp_item,0,sizeof(tmp_item));
  285. tmp_item.nameid = pd->petDB->EggID;
  286. tmp_item.identify = 1;
  287. tmp_item.card[0] = (short)0xff00;
  288. tmp_item.card[1] = GetWord(sd->pet.pet_id,0);
  289. tmp_item.card[2] = GetWord(sd->pet.pet_id,1);
  290. tmp_item.card[3] = sd->pet.rename_flag;
  291. if((flag = pc_additem(sd,&tmp_item,1))) {
  292. clif_additem(sd,0,0,flag);
  293. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  294. }
  295. sd->pet.incuvate = 1;
  296. intif_save_petdata(sd->status.account_id,&sd->pet);
  297. if(pd->state.skillbonus) {
  298. pd->state.skillbonus = 0;
  299. status_calc_pc(sd,0);
  300. }
  301. unit_free(&pd->bl);
  302. memset(&sd->pet, 0, sizeof(struct s_pet));
  303. sd->status.pet_id = 0;
  304. return 1;
  305. }
  306. int pet_data_init(struct map_session_data *sd)
  307. {
  308. struct pet_data *pd;
  309. int i=0,interval=0;
  310. nullpo_retr(1, sd);
  311. Assert((sd->status.pet_id == 0 || sd->pd == 0) || sd->pd->msd == sd);
  312. if(sd->status.account_id != sd->pet.account_id || sd->status.char_id != sd->pet.char_id) {
  313. sd->status.pet_id = 0;
  314. return 1;
  315. }
  316. if (sd->status.pet_id != sd->pet.pet_id) {
  317. if (sd->status.pet_id) {
  318. //Wrong pet?? Set incuvate to no and send it back for saving.
  319. sd->pet.incuvate = 1;
  320. intif_save_petdata(sd->status.account_id,&sd->pet);
  321. sd->status.pet_id = 0;
  322. return 1;
  323. }
  324. //The pet_id value was lost? odd... restore it.
  325. sd->status.pet_id = sd->pet.pet_id;
  326. }
  327. i = search_petDB_index(sd->pet.class_,PET_CLASS);
  328. if(i < 0) {
  329. sd->status.pet_id = 0;
  330. return 1;
  331. }
  332. sd->pd = pd = (struct pet_data *)aCalloc(1,sizeof(struct pet_data));
  333. pd->petDB = &pet_db[i];
  334. pd->bl.m = sd->bl.m;
  335. pd->bl.x = sd->bl.x;
  336. pd->bl.y = sd->bl.y;
  337. pet_calc_pos(pd,sd->bl.x,sd->bl.y,sd->ud.dir);
  338. pd->bl.x = pd->ud.to_x;
  339. pd->bl.y = pd->ud.to_y;
  340. pd->bl.id = npc_get_new_npc_id();
  341. memcpy(pd->name, sd->pet.name, NAME_LENGTH-1);
  342. pd->class_ = sd->pet.class_;
  343. pd->db = mob_db(pd->class_);
  344. pd->equip = sd->pet.equip;
  345. pd->bl.subtype = MONS;
  346. pd->bl.type = BL_PET;
  347. pd->msd = sd;
  348. status_set_viewdata(&pd->bl,pd->class_);
  349. unit_dataset(&pd->bl);
  350. pd->ud.dir = sd->ud.dir;
  351. pd->last_thinktime = gettick();
  352. map_addiddb(&pd->bl);
  353. // initialise
  354. status_calc_pet(pd,1);
  355. pd->state.skillbonus = 0;
  356. if (battle_config.pet_status_support) //Skotlex
  357. run_script(pet_db[i].script,0,sd->bl.id,0);
  358. if(battle_config.pet_hungry_delay_rate != 100)
  359. interval = (pd->petDB->hungry_delay*battle_config.pet_hungry_delay_rate)/100;
  360. else
  361. interval = pd->petDB->hungry_delay;
  362. if(interval <= 0)
  363. interval = 1;
  364. pd->pet_hungry_timer = add_timer(gettick()+interval,pet_hungry,sd->bl.id,0);
  365. return 0;
  366. }
  367. int pet_birth_process(struct map_session_data *sd)
  368. {
  369. nullpo_retr(1, sd);
  370. Assert((sd->status.pet_id == 0 || sd->pd == 0) || sd->pd->msd == sd);
  371. if(sd->status.pet_id && sd->pet.incuvate == 1) {
  372. sd->status.pet_id = 0;
  373. return 1;
  374. }
  375. sd->pet.incuvate = 0;
  376. sd->pet.account_id = sd->status.account_id;
  377. sd->pet.char_id = sd->status.char_id;
  378. sd->status.pet_id = sd->pet.pet_id;
  379. if(pet_data_init(sd)) {
  380. sd->status.pet_id = 0;
  381. sd->pet.incuvate = 1;
  382. sd->pet.account_id = 0;
  383. sd->pet.char_id = 0;
  384. return 1;
  385. }
  386. intif_save_petdata(sd->status.account_id,&sd->pet);
  387. chrif_save(sd,0); //FIXME: As before, is it REALLY Needed to save the char for hatching a pet? [Skotlex]
  388. map_addblock(&sd->pd->bl);
  389. clif_spawn(&sd->pd->bl);
  390. clif_send_petdata(sd,0,0);
  391. clif_send_petdata(sd,5,battle_config.pet_hair_style);
  392. clif_pet_equip(sd->pd,sd->pet.equip);
  393. clif_send_petstatus(sd);
  394. Assert((sd->status.pet_id == 0 || sd->pd == 0) || sd->pd->msd == sd);
  395. return 0;
  396. }
  397. int pet_recv_petdata(int account_id,struct s_pet *p,int flag)
  398. {
  399. struct map_session_data *sd;
  400. sd = map_id2sd(account_id);
  401. if(sd == NULL)
  402. return 1;
  403. if(flag == 1) {
  404. sd->status.pet_id = 0;
  405. return 1;
  406. }
  407. memcpy(&sd->pet,p,sizeof(struct s_pet));
  408. if(sd->pet.incuvate == 1) {
  409. int i;
  410. //Delete egg from inventory. [Skotlex]
  411. for (i = 0; i < MAX_INVENTORY; i++) {
  412. if(sd->status.inventory[i].card[0] == (short)0xff00 &&
  413. p->pet_id == MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2]))
  414. break;
  415. }
  416. if(i >= MAX_INVENTORY) {
  417. if (battle_config.error_log)
  418. ShowError("pet_recv_petdata: Hatching pet (%d:%s) aborted, couldn't find egg in inventory for removal!\n",p->pet_id, p->name);
  419. sd->status.pet_id = 0;
  420. memset(&sd->pet,0,sizeof(struct s_pet));
  421. sd->pet.incuvate = 1;
  422. return 1;
  423. }
  424. if (!pet_birth_process(sd)) //Pet hatched. Delete egg.
  425. pc_delitem(sd,i,1,0);
  426. } else {
  427. pet_data_init(sd);
  428. if(sd->pd && sd->bl.prev != NULL) {
  429. map_addblock(&sd->pd->bl);
  430. clif_spawn(&sd->pd->bl);
  431. clif_send_petdata(sd,0,0);
  432. clif_send_petdata(sd,5,battle_config.pet_hair_style);
  433. // clif_pet_equip(sd->pd,sd->pet.equip);
  434. clif_send_petstatus(sd);
  435. }
  436. }
  437. return 0;
  438. }
  439. int pet_select_egg(struct map_session_data *sd,short egg_index)
  440. {
  441. nullpo_retr(0, sd);
  442. if(egg_index < 0 || egg_index >= MAX_INVENTORY)
  443. return 0; //Forged packet!
  444. if(sd->status.inventory[egg_index].card[0] == (short)0xff00)
  445. intif_request_petdata(sd->status.account_id, sd->status.char_id, MakeDWord(sd->status.inventory[egg_index].card[1], sd->status.inventory[egg_index].card[2]) );
  446. else {
  447. if(battle_config.error_log)
  448. ShowError("wrong egg item inventory %d\n",egg_index);
  449. }
  450. return 0;
  451. }
  452. int pet_catch_process1(struct map_session_data *sd,int target_class)
  453. {
  454. nullpo_retr(0, sd);
  455. sd->catch_target_class = target_class;
  456. clif_catch_process(sd);
  457. return 0;
  458. }
  459. int pet_catch_process2(struct map_session_data *sd,int target_id)
  460. {
  461. struct mob_data *md;
  462. int i=0,pet_catch_rate=0;
  463. nullpo_retr(1, sd);
  464. md=(struct mob_data*)map_id2bl(target_id);
  465. if(!md || md->bl.type != BL_MOB || md->bl.prev == NULL){
  466. //Abort capture.
  467. sd->catch_target_class = -1;
  468. sd->itemid = sd->itemindex = -1;
  469. return 1;
  470. }
  471. if (sd->menuskill_id != SA_TAMINGMONSTER)
  472. { //Exploit?
  473. clif_pet_rulet(sd,0);
  474. sd->catch_target_class = -1;
  475. return 1;
  476. }
  477. if (sd->menuskill_lv > 0)
  478. { //Consume the pet lure [Skotlex]
  479. i=pc_search_inventory(sd,sd->menuskill_lv);
  480. if (i < 0)
  481. { //they tried an exploit?
  482. clif_pet_rulet(sd,0);
  483. sd->catch_target_class = -1;
  484. return 1;
  485. }
  486. //Delete the item
  487. if (sd->itemid == sd->menuskill_lv)
  488. sd->itemid = sd->itemindex = -1;
  489. sd->menuskill_id = sd->menuskill_lv = 0;
  490. pc_delitem(sd,i,1,0);
  491. }
  492. i = search_petDB_index(md->class_,PET_CLASS);
  493. //catch_target_class == 0 is used for universal lures. [Skotlex]
  494. //for now universal lures do not include bosses.
  495. if (sd->catch_target_class == 0 && !(md->status.mode&MD_BOSS))
  496. sd->catch_target_class = md->class_;
  497. if(i < 0 || sd->catch_target_class != md->class_) {
  498. clif_emotion(&md->bl, 7); //mob will do /ag if wrong lure is used on them.
  499. clif_pet_rulet(sd,0);
  500. sd->catch_target_class = -1;
  501. return 1;
  502. }
  503. //target_idによる敵→卵判定
  504. // if(battle_config.etc_log)
  505. // printf("mob_id = %d, mob_class = %d\n",md->bl.id,md->class_);
  506. //成功の場合
  507. pet_catch_rate = (pet_db[i].capture + (sd->status.base_level - md->db->lv)*30 + sd->battle_status.luk*20)*(200 - md->status.hp*100/md->status.max_hp)/100;
  508. if(pet_catch_rate < 1) pet_catch_rate = 1;
  509. if(battle_config.pet_catch_rate != 100)
  510. pet_catch_rate = (pet_catch_rate*battle_config.pet_catch_rate)/100;
  511. if(rand()%10000 < pet_catch_rate) {
  512. unit_remove_map(&md->bl,0);
  513. clif_pet_rulet(sd,1);
  514. // if(battle_config.etc_log)
  515. // printf("rulet success %d\n",target_id);
  516. intif_create_pet(sd->status.account_id,sd->status.char_id,pet_db[i].class_,mob_db(pet_db[i].class_)->lv,
  517. pet_db[i].EggID,0,pet_db[i].intimate,100,0,1,pet_db[i].jname);
  518. }
  519. else
  520. {
  521. sd->catch_target_class = -1;
  522. clif_pet_rulet(sd,0);
  523. }
  524. return 0;
  525. }
  526. int pet_get_egg(int account_id,int pet_id,int flag)
  527. { //This function is invoked when a new pet has been created, and at no other time!
  528. struct map_session_data *sd;
  529. struct item tmp_item;
  530. int i=0,ret=0;
  531. if(flag)
  532. return 0;
  533. sd = map_id2sd(account_id);
  534. if(sd == NULL)
  535. return 0;
  536. i = search_petDB_index(sd->catch_target_class,PET_CLASS);
  537. sd->catch_target_class = -1;
  538. if(i < 0) {
  539. intif_delete_petdata(pet_id);
  540. return 0;
  541. }
  542. memset(&tmp_item,0,sizeof(tmp_item));
  543. tmp_item.nameid = pet_db[i].EggID;
  544. tmp_item.identify = 1;
  545. tmp_item.card[0] = (short)0xff00;
  546. tmp_item.card[1] = GetWord(pet_id,0);
  547. tmp_item.card[2] = GetWord(pet_id,1);
  548. tmp_item.card[3] = 0; //New pets are not named.
  549. if((ret = pc_additem(sd,&tmp_item,1))) {
  550. clif_additem(sd,0,0,ret);
  551. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  552. }
  553. return 1;
  554. }
  555. static int pet_unequipitem(struct map_session_data *sd, struct pet_data *pd);
  556. static int pet_food(struct map_session_data *sd, struct pet_data *pd);
  557. static int pet_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap);
  558. int pet_menu(struct map_session_data *sd,int menunum)
  559. {
  560. nullpo_retr(0, sd);
  561. if (sd->pd == NULL)
  562. return 1;
  563. //You lost the pet already.
  564. if(sd->pet.intimate <= 0 || !sd->status.pet_id)
  565. return 1;
  566. switch(menunum) {
  567. case 0:
  568. clif_send_petstatus(sd);
  569. break;
  570. case 1:
  571. pet_food(sd, sd->pd);
  572. break;
  573. case 2:
  574. pet_performance(sd, sd->pd);
  575. break;
  576. case 3:
  577. pet_return_egg(sd, sd->pd);
  578. break;
  579. case 4:
  580. pet_unequipitem(sd, sd->pd);
  581. break;
  582. }
  583. return 0;
  584. }
  585. int pet_change_name(struct map_session_data *sd,char *name)
  586. {
  587. int i;
  588. nullpo_retr(1, sd);
  589. if((sd->pd == NULL) || (sd->pet.rename_flag == 1 && battle_config.pet_rename == 0))
  590. return 1;
  591. for(i=0;i<NAME_LENGTH && name[i];i++){
  592. if( !(name[i]&0xe0) || name[i]==0x7f)
  593. return 1;
  594. }
  595. pet_stop_walking(sd->pd,1);
  596. memcpy(sd->pet.name, name, NAME_LENGTH-1);
  597. memcpy(sd->pd->name, name, NAME_LENGTH-1);
  598. clif_charnameack (0,&sd->pd->bl);
  599. sd->pet.rename_flag = 1;
  600. clif_pet_equip(sd->pd,sd->pet.equip);
  601. clif_send_petstatus(sd);
  602. return 0;
  603. }
  604. int pet_equipitem(struct map_session_data *sd,int index)
  605. {
  606. struct pet_data *pd;
  607. int nameid;
  608. nullpo_retr(1, sd);
  609. pd = sd->pd;
  610. nullpo_retr(1, pd);
  611. nameid = sd->status.inventory[index].nameid;
  612. if(pd->petDB->AcceID == 0 || nameid != pd->petDB->AcceID || sd->pet.equip != 0) {
  613. clif_equipitemack(sd,0,0,0);
  614. return 1;
  615. }
  616. pc_delitem(sd,index,1,0);
  617. sd->pet.equip = pd->equip = nameid;
  618. clif_pet_equip(pd,nameid);
  619. if (battle_config.pet_equip_required)
  620. { //Skotlex: start support timers if need
  621. unsigned int tick = gettick();
  622. if (pd->s_skill && pd->s_skill->timer == -1)
  623. {
  624. if (pd->s_skill->id)
  625. pd->s_skill->timer=add_timer(tick+pd->s_skill->delay*1000, pet_skill_support_timer, sd->bl.id, 0);
  626. else
  627. pd->s_skill->timer=add_timer(tick+pd->s_skill->delay*1000, pet_heal_timer, sd->bl.id, 0);
  628. }
  629. if (pd->bonus && pd->bonus->timer == -1)
  630. pd->bonus->timer=add_timer(tick+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  631. }
  632. return 0;
  633. }
  634. static int pet_unequipitem(struct map_session_data *sd, struct pet_data *pd)
  635. {
  636. struct item tmp_item;
  637. int nameid,flag;
  638. if(sd->pet.equip == 0)
  639. return 1;
  640. nameid = sd->pet.equip;
  641. sd->pet.equip = pd->equip = 0;
  642. clif_pet_equip(pd,0);
  643. memset(&tmp_item,0,sizeof(tmp_item));
  644. tmp_item.nameid = nameid;
  645. tmp_item.identify = 1;
  646. if((flag = pc_additem(sd,&tmp_item,1))) {
  647. clif_additem(sd,0,0,flag);
  648. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  649. }
  650. if (battle_config.pet_equip_required)
  651. { //Skotlex: halt support timers if needed
  652. if(pd->state.skillbonus) {
  653. pd->state.skillbonus = 0;
  654. status_calc_pc(sd,0);
  655. }
  656. if (pd->s_skill && pd->s_skill->timer != -1)
  657. {
  658. if (pd->s_skill->id)
  659. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  660. else
  661. delete_timer(pd->s_skill->timer, pet_heal_timer);
  662. pd->s_skill->timer = -1;
  663. }
  664. if (pd->bonus && pd->bonus->timer != -1)
  665. {
  666. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  667. pd->bonus->timer = -1;
  668. }
  669. }
  670. return 0;
  671. }
  672. static int pet_food(struct map_session_data *sd, struct pet_data *pd)
  673. {
  674. int i,k;
  675. k=pd->petDB->FoodID;
  676. i=pc_search_inventory(sd,k);
  677. if(i < 0) {
  678. clif_pet_food(sd,k,0);
  679. return 1;
  680. }
  681. pc_delitem(sd,i,1,0);
  682. if(sd->pet.hungry > 90)
  683. sd->pet.intimate -= pd->petDB->r_full;
  684. else {
  685. if(battle_config.pet_friendly_rate != 100)
  686. k = (pd->petDB->r_hungry * battle_config.pet_friendly_rate)/100;
  687. else
  688. k = pd->petDB->r_hungry;
  689. if(sd->pet.hungry > 75) {
  690. k = k >> 1;
  691. if(k <= 0)
  692. k = 1;
  693. }
  694. sd->pet.intimate += k;
  695. }
  696. if(sd->pet.intimate <= 0) {
  697. sd->pet.intimate = 0;
  698. pet_stop_attack(pd);
  699. pd->status.speed = pd->db->status.speed;
  700. }
  701. else if(sd->pet.intimate > 1000)
  702. sd->pet.intimate = 1000;
  703. status_calc_pet(pd, 0);
  704. sd->pet.hungry += pd->petDB->fullness;
  705. if(sd->pet.hungry > 100)
  706. sd->pet.hungry = 100;
  707. clif_send_petdata(sd,2,sd->pet.hungry);
  708. clif_send_petdata(sd,1,sd->pet.intimate);
  709. clif_pet_food(sd,pd->petDB->FoodID,1);
  710. return 0;
  711. }
  712. static int pet_randomwalk(struct pet_data *pd,unsigned int tick)
  713. {
  714. const int retrycount=20;
  715. nullpo_retr(0, pd);
  716. Assert((pd->msd == 0) || (pd->msd->pd == pd));
  717. if(DIFF_TICK(pd->next_walktime,tick) < 0 && unit_can_move(&pd->bl)) {
  718. int i,x,y,c,d=12-pd->move_fail_count;
  719. if(d<5) d=5;
  720. for(i=0;i<retrycount;i++){
  721. int r=rand();
  722. x=pd->bl.x+r%(d*2+1)-d;
  723. y=pd->bl.y+r/(d*2+1)%(d*2+1)-d;
  724. if(map_getcell(pd->bl.m,x,y,CELL_CHKPASS) && unit_walktoxy(&pd->bl,x,y,0)){
  725. pd->move_fail_count=0;
  726. break;
  727. }
  728. if(i+1>=retrycount){
  729. pd->move_fail_count++;
  730. if(pd->move_fail_count>1000){
  731. if(battle_config.error_log)
  732. ShowWarning("PET cant move. hold position %d, class = %d\n",pd->bl.id,pd->class_);
  733. pd->move_fail_count=0;
  734. pd->ud.canmove_tick = tick + 60000;
  735. return 0;
  736. }
  737. }
  738. }
  739. for(i=c=0;i<pd->ud.walkpath.path_len;i++){
  740. if(pd->ud.walkpath.path[i]&1)
  741. c+=pd->status.speed*14/10;
  742. else
  743. c+=pd->status.speed;
  744. }
  745. pd->next_walktime = tick+rand()%3000+3000+c;
  746. return 1;
  747. }
  748. return 0;
  749. }
  750. static int pet_ai_sub_hard(struct pet_data *pd, struct map_session_data *sd, unsigned int tick)
  751. {
  752. struct block_list *target = NULL;
  753. if(pd->bl.prev == NULL || sd == NULL || sd->bl.prev == NULL)
  754. return 0;
  755. if(DIFF_TICK(tick,pd->last_thinktime) < MIN_PETTHINKTIME)
  756. return 0;
  757. pd->last_thinktime=tick;
  758. if(pd->ud.attacktimer != -1 || pd->ud.skilltimer != -1 || pd->bl.m != sd->bl.m)
  759. return 0;
  760. if(pd->ud.walktimer != -1 && pd->ud.walkpath.path_pos <= 3)
  761. return 0; //No thinking when you just started to walk.
  762. if(sd->pet.intimate <= 0) {
  763. //Pet should just... well, random walk.
  764. pet_randomwalk(pd,tick);
  765. return 0;
  766. }
  767. if (!check_distance_bl(&sd->bl, &pd->bl, pd->db->range2)) {
  768. //Master too far, chase.
  769. if(pd->target_id)
  770. pet_unlocktarget(pd);
  771. if(pd->ud.walktimer != -1 && pd->ud.target == sd->bl.id)
  772. return 0; //Already walking to him
  773. if (DIFF_TICK(tick, pd->ud.canmove_tick) < 0)
  774. return 0; //Can't move yet.
  775. pd->status.speed = (sd->battle_status.speed>>1);
  776. if(pd->status.speed <= 0)
  777. pd->status.speed = 1;
  778. if (!unit_walktobl(&pd->bl, &sd->bl, 3, 0))
  779. pet_randomwalk(pd,tick);
  780. return 0;
  781. }
  782. //Return speed to normal.
  783. if (pd->status.speed != pd->petDB->speed) {
  784. if (pd->ud.walktimer != -1)
  785. return 0; //Wait until the pet finishes walking back to master.
  786. pd->status.speed = pd->petDB->speed;
  787. }
  788. if (pd->target_id) {
  789. target= map_id2bl(pd->target_id);
  790. if (!target || pd->bl.m != target->m || status_isdead(target) ||
  791. !check_distance_bl(&pd->bl, target, pd->db->range3))
  792. {
  793. target = NULL;
  794. pet_unlocktarget(pd);
  795. }
  796. }
  797. if(!target && pd->loot && pd->loot->count < pd->loot->max && DIFF_TICK(tick,pd->ud.canact_tick)>0) {
  798. //Use half the pet's range of sight.
  799. int itc=0;
  800. map_foreachinrange(pet_ai_sub_hard_lootsearch,&pd->bl,
  801. pd->db->range2/2, BL_ITEM,pd,&itc);
  802. }
  803. if (!target) {
  804. //Just walk around.
  805. if (check_distance_bl(&sd->bl, &pd->bl, 3))
  806. return 0; //Already next to master.
  807. if(pd->ud.walktimer != -1 && check_distance_blxy(&sd->bl, pd->ud.to_x,pd->ud.to_y, 3))
  808. return 0; //Already walking to him
  809. pet_calc_pos(pd,sd->bl.x,sd->bl.y,sd->ud.dir);
  810. if(!unit_walktoxy(&pd->bl,pd->ud.to_x,pd->ud.to_y,0))
  811. pet_randomwalk(pd,tick);
  812. return 0;
  813. }
  814. if(pd->ud.target == target->id &&
  815. (pd->ud.attacktimer != -1 || pd->ud.walktimer != -1))
  816. return 0; //Target already locked.
  817. if (target->type != BL_ITEM)
  818. { //enemy targetted
  819. if(!battle_check_range(&pd->bl,target,pd->status.rhw.range))
  820. { //Chase
  821. if(!unit_walktobl(&pd->bl, target, pd->status.rhw.range, 2))
  822. pet_unlocktarget(pd); //Unreachable target.
  823. return 0;
  824. }
  825. //Continuous attack.
  826. unit_attack(&pd->bl, pd->target_id, 1);
  827. } else { //Item Targeted, attempt loot
  828. if (!check_distance_bl(&pd->bl, target, 1))
  829. { //Out of range
  830. if(!unit_walktobl(&pd->bl, target, 0, 1)) //Unreachable target.
  831. pet_unlocktarget(pd);
  832. return 0;
  833. } else{
  834. struct flooritem_data *fitem = (struct flooritem_data *)target;
  835. if(pd->loot->count < pd->loot->max){
  836. memcpy(&pd->loot->item[pd->loot->count++],&fitem->item_data,sizeof(pd->loot->item[0]));
  837. pd->loot->weight += itemdb_search(fitem->item_data.nameid)->weight*fitem->item_data.amount;
  838. map_clearflooritem(target->id);
  839. }
  840. //Target is unlocked regardless of whether it was picked or not.
  841. pet_unlocktarget(pd);
  842. }
  843. }
  844. return 0;
  845. }
  846. static int pet_ai_sub_foreachclient(struct map_session_data *sd,va_list ap)
  847. {
  848. unsigned int tick = va_arg(ap,unsigned int);
  849. if(sd->status.pet_id && sd->pd)
  850. pet_ai_sub_hard(sd->pd,sd,tick);
  851. return 0;
  852. }
  853. static int pet_ai_hard(int tid,unsigned int tick,int id,int data)
  854. {
  855. clif_foreachclient(pet_ai_sub_foreachclient,tick);
  856. return 0;
  857. }
  858. static int pet_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap)
  859. {
  860. struct pet_data* pd;
  861. struct flooritem_data *fitem = (struct flooritem_data *)bl;
  862. int sd_id =0;
  863. int *itc;
  864. pd=va_arg(ap,struct pet_data *);
  865. itc=va_arg(ap,int *);
  866. sd_id = fitem->first_get_id;
  867. if(bl->m == pd->bl.m && unit_can_reach_bl(&pd->bl,bl, pd->db->range2, 1, NULL, NULL)
  868. && rand()%1000<1000/(++(*itc)))
  869. pd->target_id=bl->id;
  870. return 0;
  871. }
  872. static int pet_delay_item_drop(int tid,unsigned int tick,int id,int data)
  873. {
  874. struct item_drop_list *list;
  875. struct item_drop *ditem, *ditem_prev;
  876. list=(struct item_drop_list *)id;
  877. ditem = list->item;
  878. while (ditem) {
  879. map_addflooritem(&ditem->item_data,ditem->item_data.amount,
  880. list->m,list->x,list->y,
  881. list->first_sd,list->second_sd,list->third_sd,0);
  882. ditem_prev = ditem;
  883. ditem = ditem->next;
  884. ers_free(item_drop_ers, ditem_prev);
  885. }
  886. ers_free(item_drop_list_ers, list);
  887. return 0;
  888. }
  889. int pet_lootitem_drop(struct pet_data *pd,struct map_session_data *sd)
  890. {
  891. int i,flag=0;
  892. struct item_drop_list *dlist;
  893. struct item_drop *ditem;
  894. struct item *it;
  895. if(!pd || !pd->loot || !pd->loot->count)
  896. return 0;
  897. dlist = ers_alloc(item_drop_list_ers, struct item_drop_list);
  898. dlist->m = pd->bl.m;
  899. dlist->x = pd->bl.x;
  900. dlist->y = pd->bl.y;
  901. dlist->first_sd = NULL;
  902. dlist->second_sd = NULL;
  903. dlist->third_sd = NULL;
  904. dlist->item = NULL;
  905. for(i=0;i<pd->loot->count;i++) {
  906. it = &pd->loot->item[i];
  907. if(sd){
  908. if((flag = pc_additem(sd,it,it->amount))){
  909. clif_additem(sd,0,0,flag);
  910. ditem = ers_alloc(item_drop_ers, struct item_drop);
  911. memcpy(&ditem->item_data, it, sizeof(struct item));
  912. ditem->next = dlist->item;
  913. dlist->item = ditem;
  914. }
  915. }
  916. else {
  917. ditem = ers_alloc(item_drop_ers, struct item_drop);
  918. memcpy(&ditem->item_data, it, sizeof(struct item));
  919. ditem->next = dlist->item;
  920. dlist->item = ditem;
  921. }
  922. }
  923. //The smart thing to do is use pd->loot->max (thanks for pointing it out, Shinomori)
  924. memset(pd->loot->item,0,pd->loot->max * sizeof(struct item));
  925. pd->loot->count = 0;
  926. pd->loot->weight = 0;
  927. pd->ud.canact_tick = gettick()+10000; // 10*1000msの間拾わない
  928. if (dlist->item)
  929. add_timer(gettick()+540,pet_delay_item_drop,(int)dlist,0);
  930. else
  931. ers_free(item_drop_list_ers, dlist);
  932. return 1;
  933. }
  934. /*==========================================
  935. * pet bonus giving skills [Valaris] / Rewritten by [Skotlex]
  936. *------------------------------------------
  937. */
  938. int pet_skill_bonus_timer(int tid,unsigned int tick,int id,int data)
  939. {
  940. struct map_session_data *sd=map_id2sd(id);
  941. struct pet_data *pd;
  942. int timer = 0;
  943. if(sd == NULL || sd->pd==NULL || sd->pd->bonus == NULL)
  944. return 1;
  945. pd=sd->pd;
  946. if(pd->bonus->timer != tid) {
  947. if(battle_config.error_log)
  948. {
  949. ShowError("pet_skill_bonus_timer %d != %d\n",pd->bonus->timer,tid);
  950. pd->bonus->timer = -1;
  951. }
  952. return 0;
  953. }
  954. // determine the time for the next timer
  955. if (pd->state.skillbonus) {
  956. pd->state.skillbonus = 0;
  957. timer = pd->bonus->delay*1000; // the duration until pet bonuses will be reactivated again
  958. if (timer <= 0) //Always active bonus
  959. timer = MIN_PETTHINKTIME;
  960. } else if (sd->pet.intimate) {
  961. pd->state.skillbonus = 1;
  962. timer = pd->bonus->duration*1000; // the duration for pet bonuses to be in effect
  963. } else { //Lost pet...
  964. pd->bonus->timer = -1;
  965. return 0;
  966. }
  967. status_calc_pc(sd, 0);
  968. // wait for the next timer
  969. pd->bonus->timer=add_timer(tick+timer,pet_skill_bonus_timer,sd->bl.id,0);
  970. return 0;
  971. }
  972. /*==========================================
  973. * pet recovery skills [Valaris] / Rewritten by [Skotlex]
  974. *------------------------------------------
  975. */
  976. int pet_recovery_timer(int tid,unsigned int tick,int id,int data)
  977. {
  978. struct map_session_data *sd=map_id2sd(id);
  979. struct pet_data *pd;
  980. if(sd==NULL || sd->pd == NULL || sd->pd->recovery == NULL)
  981. return 1;
  982. pd=sd->pd;
  983. if(pd->recovery->timer != tid) {
  984. if(battle_config.error_log)
  985. ShowError("pet_recovery_timer %d != %d\n",pd->recovery->timer,tid);
  986. return 0;
  987. }
  988. if(sd->sc.count && sd->sc.data[pd->recovery->type].timer != -1)
  989. { //Display a heal animation?
  990. //Detoxify is chosen for now.
  991. clif_skill_nodamage(&pd->bl,&sd->bl,TF_DETOXIFY,1,1);
  992. status_change_end(&sd->bl,pd->recovery->type,-1);
  993. clif_emotion(&pd->bl, 33);
  994. }
  995. pd->recovery->timer = -1;
  996. return 0;
  997. }
  998. int pet_heal_timer(int tid,unsigned int tick,int id,int data)
  999. {
  1000. struct map_session_data *sd=map_id2sd(id);
  1001. struct status_data *status;
  1002. struct pet_data *pd;
  1003. short rate = 100;
  1004. if(sd==NULL || sd->pd == NULL || sd->pd->s_skill == NULL)
  1005. return 1;
  1006. pd=sd->pd;
  1007. if(pd->s_skill->timer != tid) {
  1008. if(battle_config.error_log)
  1009. ShowError("pet_heal_timer %d != %d\n",pd->s_skill->timer,tid);
  1010. return 0;
  1011. }
  1012. status = status_get_status_data(&sd->bl);
  1013. if(pc_isdead(sd) ||
  1014. (rate = status->sp*100/status->max_sp) > pd->s_skill->sp ||
  1015. (rate = status->hp*100/status->max_hp) > pd->s_skill->hp ||
  1016. (rate = (pd->ud.skilltimer != -1)) //Another skill is in effect
  1017. ) { //Wait (how long? 1 sec for every 10% of remaining)
  1018. pd->s_skill->timer=add_timer(gettick()+(rate>10?rate:10)*100,pet_heal_timer,sd->bl.id,0);
  1019. return 0;
  1020. }
  1021. pet_stop_attack(pd);
  1022. pet_stop_walking(pd,1);
  1023. clif_skill_nodamage(&pd->bl,&sd->bl,AL_HEAL,pd->s_skill->lv,1);
  1024. status_heal(&sd->bl, pd->s_skill->lv,0, 0);
  1025. pd->s_skill->timer=add_timer(tick+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  1026. return 0;
  1027. }
  1028. /*==========================================
  1029. * pet support skills [Skotlex]
  1030. *------------------------------------------
  1031. */
  1032. int pet_skill_support_timer(int tid,unsigned int tick,int id,int data)
  1033. {
  1034. struct map_session_data *sd=map_id2sd(id);
  1035. struct pet_data *pd;
  1036. struct status_data *status;
  1037. short rate = 100;
  1038. if(sd==NULL || sd->pd == NULL || sd->pd->s_skill == NULL)
  1039. return 1;
  1040. pd=sd->pd;
  1041. if(pd->s_skill->timer != tid) {
  1042. if(battle_config.error_log)
  1043. ShowError("pet_skill_support_timer %d != %d\n",pd->s_skill->timer,tid);
  1044. return 0;
  1045. }
  1046. status = status_get_status_data(&sd->bl);
  1047. if (DIFF_TICK(pd->ud.canact_tick, tick) > 0)
  1048. { //Wait until the pet can act again.
  1049. pd->s_skill->timer=add_timer(pd->ud.canact_tick,pet_skill_support_timer,sd->bl.id,0);
  1050. return 0;
  1051. }
  1052. if(pc_isdead(sd) ||
  1053. (rate = status->sp*100/status->max_sp) > pd->s_skill->sp ||
  1054. (rate = status->hp*100/status->max_hp) > pd->s_skill->hp ||
  1055. (rate = (pd->ud.skilltimer != -1)) //Another skill is in effect
  1056. ) { //Wait (how long? 1 sec for every 10% of remaining)
  1057. pd->s_skill->timer=add_timer(tick+(rate>10?rate:10)*100,pet_skill_support_timer,sd->bl.id,0);
  1058. return 0;
  1059. }
  1060. pet_stop_attack(pd);
  1061. pet_stop_walking(pd,1);
  1062. if (skill_get_inf(pd->s_skill->id) & INF_GROUND_SKILL)
  1063. unit_skilluse_pos(&pd->bl, sd->bl.x, sd->bl.y, pd->s_skill->id, pd->s_skill->lv);
  1064. else
  1065. unit_skilluse_id(&pd->bl, sd->bl.id, pd->s_skill->id, pd->s_skill->lv);
  1066. pd->s_skill->timer=add_timer(tick+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  1067. return 0;
  1068. }
  1069. /*==========================================
  1070. *ペットデータ読み込み
  1071. *------------------------------------------
  1072. */
  1073. int read_petdb()
  1074. {
  1075. FILE *fp;
  1076. char line[1024];
  1077. int nameid,i,k;
  1078. int j=0;
  1079. int lines;
  1080. char *filename[]={"pet_db.txt","pet_db2.txt"};
  1081. char *str[32],*p,*np;
  1082. //Remove any previous scripts in case reloaddb was invoked.
  1083. for(j =0; j < MAX_PET_DB; j++)
  1084. if (pet_db[j].script) {
  1085. aFree(pet_db[j].script);
  1086. pet_db[j].script = NULL;
  1087. }
  1088. j = 0;
  1089. memset(pet_db,0,sizeof(pet_db));
  1090. for(i=0;i<2;i++){
  1091. sprintf(line, "%s/%s", db_path, filename[i]);
  1092. fp=fopen(line,"r");
  1093. if(fp==NULL){
  1094. if(i>0)
  1095. continue;
  1096. ShowError("can't read %s\n",line);
  1097. return -1;
  1098. }
  1099. lines = 0;
  1100. while(fgets(line,1020,fp) && j < MAX_PET_DB){
  1101. lines++;
  1102. if(line[0] == '/' && line[1] == '/')
  1103. continue;
  1104. for(k=0,p=line;k<20;k++){
  1105. if((np=strchr(p,','))!=NULL){
  1106. str[k]=p;
  1107. *np=0;
  1108. p=np+1;
  1109. } else {
  1110. str[k]=p;
  1111. p+=strlen(p);
  1112. }
  1113. }
  1114. nameid=atoi(str[0]);
  1115. if(nameid<=0 || nameid>2000)
  1116. continue;
  1117. //MobID,Name,JName,ItemID,EggID,AcceID,FoodID,"Fullness (1回の餌での満腹度増加率%)","HungryDeray (/min)","R_Hungry (空腹時餌やり親密度増加率%)","R_Full (とても満腹時餌やり親密度減少率%)","Intimate (捕獲時親密度%)","Die (死亡時親密度減少率%)","Capture (捕獲率%)",(Name)
  1118. pet_db[j].class_ = nameid;
  1119. memcpy(pet_db[j].name,str[1],NAME_LENGTH-1);
  1120. memcpy(pet_db[j].jname,str[2],NAME_LENGTH-1);
  1121. pet_db[j].itemID=atoi(str[3]);
  1122. pet_db[j].EggID=atoi(str[4]);
  1123. pet_db[j].AcceID=atoi(str[5]);
  1124. pet_db[j].FoodID=atoi(str[6]);
  1125. pet_db[j].fullness=atoi(str[7]);
  1126. pet_db[j].hungry_delay=atoi(str[8])*1000;
  1127. pet_db[j].r_hungry=atoi(str[9]);
  1128. if(pet_db[j].r_hungry <= 0)
  1129. pet_db[j].r_hungry=1;
  1130. pet_db[j].r_full=atoi(str[10]);
  1131. pet_db[j].intimate=atoi(str[11]);
  1132. pet_db[j].die=atoi(str[12]);
  1133. pet_db[j].capture=atoi(str[13]);
  1134. pet_db[j].speed=atoi(str[14]);
  1135. pet_db[j].s_perfor=(char)atoi(str[15]);
  1136. pet_db[j].talk_convert_class=atoi(str[16]);
  1137. pet_db[j].attack_rate=atoi(str[17]);
  1138. pet_db[j].defence_attack_rate=atoi(str[18]);
  1139. pet_db[j].change_target_rate=atoi(str[19]);
  1140. pet_db[j].script = NULL;
  1141. if((np=strchr(p,'{'))==NULL)
  1142. continue;
  1143. pet_db[j].script = parse_script((unsigned char *) np,lines);
  1144. j++;
  1145. }
  1146. if (j >= MAX_PET_DB)
  1147. ShowWarning("read_petdb: Reached max number of pets [%d]. Remaining pets were not read.\n ", MAX_PET_DB);
  1148. fclose(fp);
  1149. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' pets in '"CL_WHITE"%s"CL_RESET"'.\n",j,filename[i]);
  1150. }
  1151. return 0;
  1152. }
  1153. /*==========================================
  1154. * スキル関係初期化処理
  1155. *------------------------------------------
  1156. */
  1157. int do_init_pet(void)
  1158. {
  1159. memset(pet_db,0,sizeof(pet_db));
  1160. read_petdb();
  1161. item_drop_ers = ers_new((uint32)sizeof(struct item_drop));
  1162. item_drop_list_ers = ers_new((uint32)sizeof(struct item_drop_list));
  1163. add_timer_func_list(pet_hungry,"pet_hungry");
  1164. add_timer_func_list(pet_ai_hard,"pet_ai_hard");
  1165. add_timer_func_list(pet_skill_bonus_timer,"pet_skill_bonus_timer"); // [Valaris]
  1166. add_timer_func_list(pet_delay_item_drop,"pet_delay_item_drop");
  1167. add_timer_func_list(pet_skill_support_timer, "pet_skill_support_timer"); // [Skotlex]
  1168. add_timer_func_list(pet_recovery_timer,"pet_recovery_timer"); // [Valaris]
  1169. add_timer_func_list(pet_heal_timer,"pet_heal_timer"); // [Valaris]
  1170. add_timer_interval(gettick()+MIN_PETTHINKTIME,pet_ai_hard,0,0,MIN_PETTHINKTIME);
  1171. return 0;
  1172. }
  1173. int do_final_pet(void) {
  1174. int i;
  1175. for(i = 0;i < MAX_PET_DB; i++) {
  1176. if(pet_db[i].script) {
  1177. script_free_code(pet_db[i].script);
  1178. pet_db[i].script = NULL;
  1179. }
  1180. }
  1181. ers_destroy(item_drop_ers);
  1182. ers_destroy(item_drop_list_ers);
  1183. return 0;
  1184. }