pet.c 35 KB

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