pet.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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/random.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/strlib.h"
  10. #include "../common/utils.h"
  11. #include "../common/ers.h"
  12. #include "pc.h"
  13. #include "intif.h"
  14. #include "chrif.h"
  15. #include "pet.h"
  16. #include <stdlib.h>
  17. #define MIN_PETTHINKTIME 100
  18. struct s_pet_db pet_db[MAX_PET_DB];
  19. static struct eri *item_drop_ers; //For loot drops delay structures.
  20. static struct eri *item_drop_list_ers;
  21. /**
  22. * Get the value of the pet's hunger.
  23. * @param pd : pet requesting
  24. * @return Pet's hunger value
  25. */
  26. int pet_hungry_val(struct pet_data *pd)
  27. {
  28. nullpo_ret(pd);
  29. if(pd->pet.hungry > 90)
  30. return 4;
  31. else if(pd->pet.hungry > 75)
  32. return 3;
  33. else if(pd->pet.hungry > 25)
  34. return 2;
  35. else if(pd->pet.hungry > 10)
  36. return 1;
  37. else
  38. return 0;
  39. }
  40. /**
  41. * Set the value of the pet's intimacy.
  42. * @param pd : pet requesting
  43. * @param value : new intimacy value
  44. */
  45. void pet_set_intimate(struct pet_data *pd, int value)
  46. {
  47. int intimate;
  48. struct map_session_data *sd;
  49. nullpo_retv(pd);
  50. intimate = pd->pet.intimate;
  51. sd = pd->master;
  52. pd->pet.intimate = value;
  53. if( sd && ((intimate >= battle_config.pet_equip_min_friendly && pd->pet.intimate < battle_config.pet_equip_min_friendly) || (intimate < battle_config.pet_equip_min_friendly && pd->pet.intimate >= battle_config.pet_equip_min_friendly)) )
  54. status_calc_pc(sd,SCO_NONE);
  55. }
  56. /**
  57. * Create a pet egg.
  58. * @param sd : player requesting
  59. * @param item_id : item ID of tamer
  60. * @return 1:success, 0:failure
  61. */
  62. int pet_create_egg(struct map_session_data *sd, unsigned short item_id)
  63. {
  64. int pet_id = search_petDB_index(item_id, PET_EGG);
  65. if (pet_id < 0)
  66. return 0; //No pet egg here.
  67. if (!pc_inventoryblank(sd))
  68. return 0; // Inventory full
  69. sd->catch_target_class = pet_db[pet_id].class_;
  70. intif_create_pet(sd->status.account_id, sd->status.char_id,
  71. (short)pet_db[pet_id].class_,
  72. (short)mob_db(pet_db[pet_id].class_)->lv,
  73. (short)pet_db[pet_id].EggID, 0,
  74. (short)pet_db[pet_id].intimate,
  75. 100, 0, 1, pet_db[pet_id].jname);
  76. return 1;
  77. }
  78. /**
  79. * Make pet drop target.
  80. * @param pd : pet requesting
  81. * @return 0
  82. */
  83. int pet_unlocktarget(struct pet_data *pd)
  84. {
  85. nullpo_ret(pd);
  86. pd->target_id = 0;
  87. pet_stop_attack(pd);
  88. pet_stop_walking(pd,1);
  89. return 0;
  90. }
  91. /**
  92. * Make pet use attack skill.
  93. * @param pd : pet requesting
  94. * @param target_id : ID of target
  95. * @author [Skotlex]
  96. */
  97. int pet_attackskill(struct pet_data *pd, int target_id)
  98. {
  99. if (!battle_config.pet_status_support || !pd->a_skill ||
  100. (battle_config.pet_equip_required && !pd->pet.equip))
  101. return 0;
  102. if (DIFF_TICK(pd->ud.canact_tick, gettick()) > 0)
  103. return 0;
  104. if (rnd()%100 < (pd->a_skill->rate +pd->pet.intimate*pd->a_skill->bonusrate/1000)) { // Skotlex: Use pet's skill
  105. int inf;
  106. struct block_list *bl;
  107. bl = map_id2bl(target_id);
  108. if(bl == NULL || pd->bl.m != bl->m || bl->prev == NULL || status_isdead(bl) ||
  109. !check_distance_bl(&pd->bl, bl, pd->db->range3))
  110. return 0;
  111. inf = skill_get_inf(pd->a_skill->id);
  112. if (inf & INF_GROUND_SKILL)
  113. unit_skilluse_pos(&pd->bl, bl->x, bl->y, pd->a_skill->id, pd->a_skill->lv);
  114. else //Offensive self skill? Could be stuff like GX.
  115. unit_skilluse_id(&pd->bl,(inf&INF_SELF_SKILL?pd->bl.id:bl->id), pd->a_skill->id, pd->a_skill->lv);
  116. return 1; //Skill invoked.
  117. }
  118. return 0;
  119. }
  120. /**
  121. * Make sure pet can attack from given config values.
  122. * @param pd: pet data
  123. * @param bl: target
  124. * @param type: pet's attack rate type
  125. * @return 0
  126. */
  127. int pet_target_check(struct pet_data *pd,struct block_list *bl,int type)
  128. {
  129. int rate;
  130. nullpo_ret(pd);
  131. Assert((pd->master == 0) || (pd->master->pd == pd));
  132. if(bl == NULL || bl->type != BL_MOB || bl->prev == NULL ||
  133. pd->pet.intimate < battle_config.pet_support_min_friendly ||
  134. pd->pet.hungry < 1 ||
  135. pd->pet.class_ == status_get_class(bl))
  136. return 0;
  137. if(pd->bl.m != bl->m ||
  138. !check_distance_bl(&pd->bl, bl, pd->db->range2))
  139. return 0;
  140. if (!battle_config.pet_master_dead && pc_isdead(pd->master)) {
  141. pet_unlocktarget(pd);
  142. return 0;
  143. }
  144. if (!status_check_skilluse(&pd->bl, bl, 0, 0))
  145. return 0;
  146. if(!type) {
  147. rate = pd->petDB->attack_rate;
  148. rate = rate * pd->rate_fix / 1000;
  149. if(pd->petDB->attack_rate > 0 && rate <= 0)
  150. rate = 1;
  151. } else {
  152. rate = pd->petDB->defence_attack_rate;
  153. rate = rate * pd->rate_fix / 1000;
  154. if(pd->petDB->defence_attack_rate > 0 && rate <= 0)
  155. rate = 1;
  156. }
  157. if(rnd()%10000 < rate) {
  158. if(pd->target_id == 0 || rnd()%10000 < pd->petDB->change_target_rate)
  159. pd->target_id = bl->id;
  160. }
  161. return 0;
  162. }
  163. /**
  164. * Status change check for pets.
  165. * @param sd : player requesting
  166. * @param type : recovery type
  167. * @author [Skotlex]
  168. */
  169. int pet_sc_check(struct map_session_data *sd, int type)
  170. {
  171. struct pet_data *pd;
  172. nullpo_ret(sd);
  173. pd = sd->pd;
  174. if( pd == NULL
  175. || (battle_config.pet_equip_required && pd->pet.equip == 0)
  176. || pd->recovery == NULL
  177. || pd->recovery->timer != INVALID_TIMER
  178. || pd->recovery->type != type )
  179. return 1;
  180. pd->recovery->timer = add_timer(gettick()+pd->recovery->delay*1000,pet_recovery_timer,sd->bl.id,0);
  181. return 0;
  182. }
  183. /**
  184. * Update pet's hungry value and timer and adjust intimacy based on hunger.
  185. * @param tid : current timer value
  186. * @param tick : how often to update
  187. * @param id : ID of pet owner
  188. * @return 0
  189. */
  190. static int pet_hungry(int tid, unsigned int tick, int id, intptr_t data)
  191. {
  192. struct map_session_data *sd;
  193. struct pet_data *pd;
  194. int interval;
  195. sd = map_id2sd(id);
  196. if(!sd)
  197. return 1;
  198. if(!sd->status.pet_id || !sd->pd)
  199. return 1;
  200. pd = sd->pd;
  201. if(pd->pet_hungry_timer != tid) {
  202. ShowError("pet_hungry_timer %d != %d\n",pd->pet_hungry_timer,tid);
  203. return 0;
  204. }
  205. pd->pet_hungry_timer = INVALID_TIMER;
  206. if (pd->pet.intimate <= 0)
  207. return 1; //You lost the pet already, the rest is irrelevant.
  208. pd->pet.hungry--;
  209. if( pd->pet.hungry < 0 ) {
  210. pet_stop_attack(pd);
  211. pd->pet.hungry = 0;
  212. pet_set_intimate(pd, pd->pet.intimate - battle_config.pet_hungry_friendly_decrease);
  213. if( pd->pet.intimate <= 0 ) {
  214. pd->pet.intimate = 0;
  215. pd->status.speed = pd->db->status.speed;
  216. }
  217. status_calc_pet(pd,SCO_NONE);
  218. clif_send_petdata(sd,pd,1,pd->pet.intimate);
  219. }
  220. clif_send_petdata(sd,pd,2,pd->pet.hungry);
  221. if(battle_config.pet_hungry_delay_rate != 100)
  222. interval = (pd->petDB->hungry_delay*battle_config.pet_hungry_delay_rate)/100;
  223. else
  224. interval = pd->petDB->hungry_delay;
  225. if(interval <= 0)
  226. interval = 1;
  227. pd->pet_hungry_timer = add_timer(tick+interval,pet_hungry,sd->bl.id,0);
  228. return 0;
  229. }
  230. /**
  231. * Search pet database for given value and type.
  232. * @param key : value to search for
  233. * @param type : pet type to search for (Class, Catch, Egg, Equip, Food)
  234. * @return Pet ID on success, -1 on failure
  235. */
  236. int search_petDB_index(int key,int type)
  237. {
  238. int i;
  239. for( i = 0; i < MAX_PET_DB; i++ ) {
  240. if(pet_db[i].class_ <= 0)
  241. continue;
  242. switch(type) {
  243. case PET_CLASS: if(pet_db[i].class_ == key) return i; break;
  244. case PET_CATCH: if(pet_db[i].itemID == key) return i; break;
  245. case PET_EGG: if(pet_db[i].EggID == key) return i; break;
  246. case PET_EQUIP: if(pet_db[i].AcceID == key) return i; break;
  247. case PET_FOOD: if(pet_db[i].FoodID == key) return i; break;
  248. default:
  249. return -1;
  250. }
  251. }
  252. return -1;
  253. }
  254. /**
  255. * Remove hunger timer from pet.
  256. * @param pd : pet requesting
  257. * @return 1
  258. */
  259. int pet_hungry_timer_delete(struct pet_data *pd)
  260. {
  261. nullpo_ret(pd);
  262. if(pd->pet_hungry_timer != INVALID_TIMER) {
  263. delete_timer(pd->pet_hungry_timer,pet_hungry);
  264. pd->pet_hungry_timer = INVALID_TIMER;
  265. }
  266. return 1;
  267. }
  268. /**
  269. * Make a pet perform/dance.
  270. * @param sd : player requesting
  271. * @param pd : pet requesting
  272. * @return 1
  273. */
  274. static int pet_performance(struct map_session_data *sd, struct pet_data *pd)
  275. {
  276. int val;
  277. if (pd->pet.intimate > 900)
  278. val = (pd->petDB->s_perfor > 0) ? 4 : 3;
  279. else if(pd->pet.intimate > 750) //TODO: this is way too high
  280. val = 2;
  281. else
  282. val = 1;
  283. pet_stop_walking(pd,2000<<8);
  284. clif_pet_performance(pd, rnd()%val + 1);
  285. pet_lootitem_drop(pd,NULL);
  286. return 1;
  287. }
  288. /**
  289. * Return a pet to it's egg.
  290. * @param sd : player requesting
  291. * @param pd : pet requesting
  292. * @return 1
  293. */
  294. static int pet_return_egg(struct map_session_data *sd, struct pet_data *pd)
  295. {
  296. struct item tmp_item;
  297. unsigned char flag = 0;
  298. pet_lootitem_drop(pd,sd);
  299. memset(&tmp_item,0,sizeof(tmp_item));
  300. tmp_item.nameid = pd->petDB->EggID;
  301. tmp_item.identify = 1;
  302. tmp_item.card[0] = CARD0_PET;
  303. tmp_item.card[1] = GetWord(pd->pet.pet_id,0);
  304. tmp_item.card[2] = GetWord(pd->pet.pet_id,1);
  305. tmp_item.card[3] = pd->pet.rename_flag;
  306. if((flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_OTHER))) {
  307. clif_additem(sd,0,0,flag);
  308. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  309. }
  310. pd->pet.incubate = 1;
  311. unit_free(&pd->bl,CLR_OUTSIGHT);
  312. status_calc_pc(sd,SCO_NONE);
  313. sd->status.pet_id = 0;
  314. return 1;
  315. }
  316. /**
  317. * Load initial pet data when hatching/creating.
  318. * @param sd : player requesting
  319. * @param pet : pet requesting
  320. */
  321. int pet_data_init(struct map_session_data *sd, struct s_pet *pet)
  322. {
  323. struct pet_data *pd;
  324. int i = 0, interval = 0;
  325. nullpo_retr(1, sd);
  326. Assert((sd->status.pet_id == 0 || sd->pd == 0) || sd->pd->master == sd);
  327. if(sd->status.account_id != pet->account_id || sd->status.char_id != pet->char_id) {
  328. sd->status.pet_id = 0;
  329. return 1;
  330. }
  331. if (sd->status.pet_id != pet->pet_id) {
  332. if (sd->status.pet_id) {
  333. //Wrong pet?? Set incubate to no and send it back for saving.
  334. pet->incubate = 1;
  335. intif_save_petdata(sd->status.account_id,pet);
  336. sd->status.pet_id = 0;
  337. return 1;
  338. }
  339. //The pet_id value was lost? odd... restore it.
  340. sd->status.pet_id = pet->pet_id;
  341. }
  342. i = search_petDB_index(pet->class_,PET_CLASS);
  343. if(i < 0) {
  344. sd->status.pet_id = 0;
  345. return 1;
  346. }
  347. sd->pd = pd = (struct pet_data *)aCalloc(1,sizeof(struct pet_data));
  348. pd->bl.type = BL_PET;
  349. pd->bl.id = npc_get_new_npc_id();
  350. pd->master = sd;
  351. pd->petDB = &pet_db[i];
  352. pd->db = mob_db(pet->class_);
  353. memcpy(&pd->pet, pet, sizeof(struct s_pet));
  354. status_set_viewdata(&pd->bl, pet->class_);
  355. unit_dataset(&pd->bl);
  356. pd->ud.dir = sd->ud.dir;
  357. pd->bl.m = sd->bl.m;
  358. pd->bl.x = sd->bl.x;
  359. pd->bl.y = sd->bl.y;
  360. unit_calc_pos(&pd->bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  361. pd->bl.x = pd->ud.to_x;
  362. pd->bl.y = pd->ud.to_y;
  363. map_addiddb(&pd->bl);
  364. status_calc_pet(pd,SCO_FIRST);
  365. pd->last_thinktime = gettick();
  366. pd->state.skillbonus = 0;
  367. if( battle_config.pet_status_support )
  368. run_script(pet_db[i].pet_script,0,sd->bl.id,0);
  369. if( pd->petDB ) {
  370. if( pd->petDB->pet_loyal_script )
  371. status_calc_pc(sd,SCO_NONE);
  372. if( battle_config.pet_hungry_delay_rate != 100 )
  373. interval = pd->petDB->hungry_delay * battle_config.pet_hungry_delay_rate / 100;
  374. else
  375. interval = pd->petDB->hungry_delay;
  376. }
  377. if( interval <= 0 )
  378. interval = 1;
  379. pd->pet_hungry_timer = add_timer(gettick() + interval, pet_hungry, sd->bl.id, 0);
  380. pd->masterteleport_timer = INVALID_TIMER;
  381. return 0;
  382. }
  383. /**
  384. * Begin hatching a pet.
  385. * @param sd : player requesting
  386. * @param pet : pet requesting
  387. */
  388. int pet_birth_process(struct map_session_data *sd, struct s_pet *pet)
  389. {
  390. nullpo_retr(1, sd);
  391. Assert((sd->status.pet_id == 0 || sd->pd == 0) || sd->pd->master == sd);
  392. if(sd->status.pet_id && pet->incubate == 1) {
  393. sd->status.pet_id = 0;
  394. return 1;
  395. }
  396. pet->incubate = 0;
  397. pet->account_id = sd->status.account_id;
  398. pet->char_id = sd->status.char_id;
  399. sd->status.pet_id = pet->pet_id;
  400. if(pet_data_init(sd, pet)) {
  401. sd->status.pet_id = 0;
  402. return 1;
  403. }
  404. intif_save_petdata(sd->status.account_id,pet);
  405. if (save_settings&CHARSAVE_PET)
  406. chrif_save(sd,0); //is it REALLY Needed to save the char for hatching a pet? [Skotlex]
  407. if(sd->bl.prev != NULL) {
  408. if(map_addblock(&sd->pd->bl))
  409. return 1;
  410. clif_spawn(&sd->pd->bl);
  411. clif_send_petdata(sd,sd->pd, 0,0);
  412. clif_send_petdata(sd,sd->pd, 5,battle_config.pet_hair_style);
  413. clif_pet_equip_area(sd->pd);
  414. clif_send_petstatus(sd);
  415. }
  416. Assert((sd->status.pet_id == 0 || sd->pd == 0) || sd->pd->master == sd);
  417. return 0;
  418. }
  419. /**
  420. * Finalize hatching process and load pet to client.
  421. * @param account_id : account ID of owner
  422. * @param p : pet requesting
  423. * @param flag : 1:stop loading of pet
  424. * @return 0:success, 1:failure
  425. */
  426. int pet_recv_petdata(uint32 account_id,struct s_pet *p,int flag)
  427. {
  428. struct map_session_data *sd;
  429. sd = map_id2sd(account_id);
  430. if(sd == NULL)
  431. return 1;
  432. if(flag == 1) {
  433. sd->status.pet_id = 0;
  434. return 1;
  435. }
  436. if(p->incubate == 1) {
  437. int i;
  438. //Delete egg from inventory. [Skotlex]
  439. for (i = 0; i < MAX_INVENTORY; i++) {
  440. if(sd->inventory.u.items_inventory[i].card[0] == CARD0_PET &&
  441. p->pet_id == MakeDWord(sd->inventory.u.items_inventory[i].card[1], sd->inventory.u.items_inventory[i].card[2]))
  442. break;
  443. }
  444. if(i >= MAX_INVENTORY) {
  445. ShowError("pet_recv_petdata: Hatching pet (%d:%s) aborted, couldn't find egg in inventory for removal!\n",p->pet_id, p->name);
  446. sd->status.pet_id = 0;
  447. return 1;
  448. }
  449. if (!pet_birth_process(sd,p)) //Pet hatched. Delete egg.
  450. pc_delitem(sd,i,1,0,0,LOG_TYPE_OTHER);
  451. } else {
  452. pet_data_init(sd,p);
  453. if(sd->pd && sd->bl.prev != NULL) {
  454. if(map_addblock(&sd->pd->bl))
  455. return 1;
  456. clif_spawn(&sd->pd->bl);
  457. clif_send_petdata(sd,sd->pd,0,0);
  458. clif_send_petdata(sd,sd->pd,5,battle_config.pet_hair_style);
  459. clif_pet_equip_area(sd->pd);
  460. clif_send_petstatus(sd);
  461. }
  462. }
  463. return 0;
  464. }
  465. /**
  466. * Selected pet egg to hatch.
  467. * @param sd : player requesting
  468. * @param egg_index : egg index value in inventory
  469. * @return 0
  470. */
  471. int pet_select_egg(struct map_session_data *sd,short egg_index)
  472. {
  473. nullpo_ret(sd);
  474. if(egg_index < 0 || egg_index >= MAX_INVENTORY)
  475. return 0; //Forged packet!
  476. if(sd->inventory.u.items_inventory[egg_index].card[0] == CARD0_PET)
  477. intif_request_petdata(sd->status.account_id, sd->status.char_id, MakeDWord(sd->inventory.u.items_inventory[egg_index].card[1], sd->inventory.u.items_inventory[egg_index].card[2]) );
  478. else
  479. ShowError("wrong egg item inventory %d\n",egg_index);
  480. return 0;
  481. }
  482. /**
  483. * Display the success/failure roulette wheel when trying to catch monster.
  484. * @param sd : player requesting
  485. * @param target_class : monster ID of pet to catch
  486. * @return 0
  487. */
  488. int pet_catch_process1(struct map_session_data *sd,int target_class)
  489. {
  490. nullpo_ret(sd);
  491. sd->catch_target_class = target_class;
  492. clif_catch_process(sd);
  493. return 0;
  494. }
  495. /**
  496. * Begin the actual catching process of a monster.
  497. * @param sd : player requesting
  498. * @param target_id : monster ID of pet to catch
  499. * @return 0:success, 1:failure
  500. */
  501. int pet_catch_process2(struct map_session_data* sd, int target_id)
  502. {
  503. struct mob_data* md;
  504. int i = 0, pet_catch_rate = 0;
  505. nullpo_retr(1, sd);
  506. md = (struct mob_data*)map_id2bl(target_id);
  507. if(!md || md->bl.type != BL_MOB || md->bl.prev == NULL) { // Invalid inputs/state, abort capture.
  508. clif_pet_roulette(sd,0);
  509. sd->catch_target_class = -1;
  510. sd->itemid = sd->itemindex = -1;
  511. return 1;
  512. }
  513. //FIXME: delete taming item here, if this was an item-invoked capture and the item was flagged as delay-consume [ultramage]
  514. i = search_petDB_index(md->mob_id,PET_CLASS);
  515. //catch_target_class == 0 is used for universal lures (except bosses for now). [Skotlex]
  516. if (sd->catch_target_class == 0 && !status_has_mode(&md->status,MD_STATUS_IMMUNE))
  517. sd->catch_target_class = md->mob_id;
  518. if(i < 0 || sd->catch_target_class != md->mob_id) {
  519. clif_emotion(&md->bl, E_AG); //mob will do /ag if wrong lure is used on them.
  520. clif_pet_roulette(sd,0);
  521. sd->catch_target_class = -1;
  522. return 1;
  523. }
  524. pet_catch_rate = (pet_db[i].capture + (sd->status.base_level - md->level)*30 + sd->battle_status.luk*20)*(200 - get_percentage(md->status.hp, md->status.max_hp))/100;
  525. if(pet_catch_rate < 1)
  526. pet_catch_rate = 1;
  527. if(battle_config.pet_catch_rate != 100)
  528. pet_catch_rate = (pet_catch_rate*battle_config.pet_catch_rate)/100;
  529. if(rnd()%10000 < pet_catch_rate) {
  530. unit_remove_map(&md->bl,CLR_OUTSIGHT);
  531. status_kill(&md->bl);
  532. clif_pet_roulette(sd,1);
  533. intif_create_pet(sd->status.account_id,sd->status.char_id,pet_db[i].class_,mob_db(pet_db[i].class_)->lv,
  534. pet_db[i].EggID,0,pet_db[i].intimate,100,0,1,pet_db[i].jname);
  535. } else {
  536. clif_pet_roulette(sd,0);
  537. sd->catch_target_class = -1;
  538. }
  539. return 0;
  540. }
  541. /**
  542. * Is invoked _only_ when a new pet has been created is a product of packet 0x3880
  543. * see mapif_pet_created@int_pet.c for more information.
  544. * Handles new pet data from inter-server and prepares item information to add pet egg.
  545. * @param account_id : account ID of owner
  546. * @param pet_class : class of pet
  547. * @param pet_id : pet ID otherwise means failure
  548. * @return true : success, false : failure
  549. **/
  550. bool pet_get_egg(uint32 account_id, short pet_class, int pet_id ) {
  551. struct map_session_data *sd;
  552. struct item tmp_item;
  553. int i = 0, ret = 0;
  554. if( pet_id == 0 || pet_class == 0 )
  555. return false;
  556. sd = map_id2sd(account_id);
  557. if( sd == NULL )
  558. return false;
  559. // i = pet_search_petDB_index(sd->catch_target_class,PET_CLASS);
  560. // issue: 8150
  561. // Before this change in cases where more than one pet egg were requested in a short
  562. // period of time it wasn't possible to know which kind of egg was being requested after
  563. // the first request. [Panikon]
  564. i = search_petDB_index(pet_class,PET_CLASS);
  565. sd->catch_target_class = -1;
  566. if(i < 0) {
  567. intif_delete_petdata(pet_id);
  568. return false;
  569. }
  570. memset(&tmp_item,0,sizeof(tmp_item));
  571. tmp_item.nameid = pet_db[i].EggID;
  572. tmp_item.identify = 1;
  573. tmp_item.card[0] = CARD0_PET;
  574. tmp_item.card[1] = GetWord(pet_id,0);
  575. tmp_item.card[2] = GetWord(pet_id,1);
  576. tmp_item.card[3] = 0; //New pets are not named.
  577. if((ret = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER))) {
  578. clif_additem(sd,0,0,ret);
  579. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  580. }
  581. return true;
  582. }
  583. static int pet_unequipitem(struct map_session_data *sd, struct pet_data *pd);
  584. static int pet_food(struct map_session_data *sd, struct pet_data *pd);
  585. static int pet_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap);
  586. /**
  587. * Pet menu options.
  588. * @param sd : player requesting
  589. * @param menunum : menu option chosen
  590. * @return 0:success, 1:failure
  591. */
  592. int pet_menu(struct map_session_data *sd,int menunum)
  593. {
  594. struct item_data *egg_id;
  595. nullpo_ret(sd);
  596. if (sd->pd == NULL)
  597. return 1;
  598. //You lost the pet already.
  599. if(!sd->status.pet_id || sd->pd->pet.intimate <= 0 || sd->pd->pet.incubate)
  600. return 1;
  601. egg_id = itemdb_exists(sd->pd->petDB->EggID);
  602. if (egg_id) {
  603. if ((egg_id->flag.trade_restriction&0x01) && !pc_inventoryblank(sd)) {
  604. clif_displaymessage(sd->fd, msg_txt(sd, 451)); // You can't return your pet because your inventory is full.
  605. return 1;
  606. }
  607. }
  608. switch(menunum) {
  609. case 0:
  610. clif_send_petstatus(sd);
  611. break;
  612. case 1:
  613. pet_food(sd, sd->pd);
  614. break;
  615. case 2:
  616. pet_performance(sd, sd->pd);
  617. break;
  618. case 3:
  619. pet_return_egg(sd, sd->pd);
  620. break;
  621. case 4:
  622. pet_unequipitem(sd, sd->pd);
  623. break;
  624. }
  625. return 0;
  626. }
  627. /**
  628. * Apply pet's new name.
  629. * @param sd : player requesting
  630. * @param name : new pet name
  631. * @return 0:success, 1:failure
  632. */
  633. int pet_change_name(struct map_session_data *sd,char *name)
  634. {
  635. int i;
  636. struct pet_data *pd;
  637. nullpo_retr(1, sd);
  638. pd = sd->pd;
  639. if((pd == NULL) || (pd->pet.rename_flag == 1 && !battle_config.pet_rename))
  640. return 1;
  641. for(i = 0; i < NAME_LENGTH && name[i]; i++) {
  642. if( !(name[i]&0xe0) || name[i]==0x7f)
  643. return 1;
  644. }
  645. return intif_rename_pet(sd, name);
  646. }
  647. /**
  648. * Request to change pet's name.
  649. * @param sd : player requesting
  650. * @param name : new pet name
  651. * @param flag : 1:cannot use this name
  652. * @return 1:success, 0:failure
  653. */
  654. int pet_change_name_ack(struct map_session_data *sd, char* name, int flag)
  655. {
  656. struct pet_data *pd = sd->pd;
  657. if (!pd)
  658. return 0;
  659. normalize_name(name," ");//bugreport:3032
  660. if ( !flag || !strlen(name) ) {
  661. clif_displaymessage(sd->fd, msg_txt(sd,280)); // You cannot use this name for your pet.
  662. clif_send_petstatus(sd); //Send status so client knows pet name change got rejected.
  663. return 0;
  664. }
  665. memcpy(pd->pet.name, name, NAME_LENGTH);
  666. clif_name_area(&pd->bl);
  667. pd->pet.rename_flag = 1;
  668. clif_pet_equip_area(pd);
  669. clif_send_petstatus(sd);
  670. return 1;
  671. }
  672. /**
  673. * Apply a pet's equipment.
  674. * @param sd : player requesting
  675. * @param index : index value of item
  676. * @return 0:success, 1:failure
  677. */
  678. int pet_equipitem(struct map_session_data *sd,int index)
  679. {
  680. struct pet_data *pd;
  681. unsigned short nameid;
  682. nullpo_retr(1, sd);
  683. pd = sd->pd;
  684. if (!pd)
  685. return 1;
  686. nameid = sd->inventory.u.items_inventory[index].nameid;
  687. if(pd->petDB->AcceID == 0 || nameid != pd->petDB->AcceID || pd->pet.equip != 0) {
  688. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  689. return 1;
  690. }
  691. pc_delitem(sd,index,1,0,0,LOG_TYPE_OTHER);
  692. pd->pet.equip = nameid;
  693. status_set_viewdata(&pd->bl, pd->pet.class_); //Updates view_data.
  694. clif_pet_equip_area(pd);
  695. if (battle_config.pet_equip_required) { // Skotlex: start support timers if need
  696. unsigned int tick = gettick();
  697. if (pd->s_skill && pd->s_skill->timer == INVALID_TIMER) {
  698. if (pd->s_skill->id)
  699. pd->s_skill->timer=add_timer(tick+pd->s_skill->delay*1000, pet_skill_support_timer, sd->bl.id, 0);
  700. else
  701. pd->s_skill->timer=add_timer(tick+pd->s_skill->delay*1000, pet_heal_timer, sd->bl.id, 0);
  702. }
  703. if (pd->bonus && pd->bonus->timer == INVALID_TIMER)
  704. pd->bonus->timer=add_timer(tick+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  705. }
  706. return 0;
  707. }
  708. /**
  709. * Remove a pet's equipment.
  710. * @param sd : player requesting
  711. * @param pd : pet requesting
  712. * @return 0:success, 1:failure
  713. */
  714. static int pet_unequipitem(struct map_session_data *sd, struct pet_data *pd)
  715. {
  716. struct item tmp_item;
  717. unsigned short nameid;
  718. unsigned char flag = 0;
  719. if(pd->pet.equip == 0)
  720. return 1;
  721. nameid = pd->pet.equip;
  722. pd->pet.equip = 0;
  723. status_set_viewdata(&pd->bl, pd->pet.class_);
  724. clif_pet_equip_area(pd);
  725. memset(&tmp_item,0,sizeof(tmp_item));
  726. tmp_item.nameid = nameid;
  727. tmp_item.identify = 1;
  728. if((flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_OTHER))) {
  729. clif_additem(sd,0,0,flag);
  730. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  731. }
  732. if( battle_config.pet_equip_required ) { // Skotlex: halt support timers if needed
  733. if( pd->state.skillbonus ) {
  734. pd->state.skillbonus = 0;
  735. status_calc_pc(sd,SCO_NONE);
  736. }
  737. if( pd->s_skill && pd->s_skill->timer != INVALID_TIMER ) {
  738. if( pd->s_skill->id )
  739. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  740. else
  741. delete_timer(pd->s_skill->timer, pet_heal_timer);
  742. pd->s_skill->timer = INVALID_TIMER;
  743. }
  744. if( pd->bonus && pd->bonus->timer != INVALID_TIMER ) {
  745. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  746. pd->bonus->timer = INVALID_TIMER;
  747. }
  748. }
  749. return 0;
  750. }
  751. /**
  752. * Feed a pet and update intimacy.
  753. * @param sd : playerr requesting
  754. * @param pd : pet requesting
  755. * @return 0:success, 1:failure
  756. */
  757. static int pet_food(struct map_session_data *sd, struct pet_data *pd)
  758. {
  759. int i,k;
  760. k = pd->petDB->FoodID;
  761. i = pc_search_inventory(sd,k);
  762. if( i < 0 ) {
  763. clif_pet_food(sd,k,0);
  764. return 1;
  765. }
  766. pc_delitem(sd,i,1,0,0,LOG_TYPE_CONSUME);
  767. if( pd->pet.hungry > 90 )
  768. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->r_full);
  769. else {
  770. if( battle_config.pet_friendly_rate != 100 )
  771. k = (pd->petDB->r_hungry * battle_config.pet_friendly_rate) / 100;
  772. else
  773. k = pd->petDB->r_hungry;
  774. if( pd->pet.hungry > 75 ) {
  775. k = k >> 1;
  776. if( k <= 0 )
  777. k = 1;
  778. }
  779. pet_set_intimate(pd, pd->pet.intimate + k);
  780. }
  781. if( pd->pet.intimate <= 0 ) {
  782. pd->pet.intimate = 0;
  783. pet_stop_attack(pd);
  784. pd->status.speed = pd->db->status.speed;
  785. } else if( pd->pet.intimate > 1000 )
  786. pd->pet.intimate = 1000;
  787. status_calc_pet(pd,SCO_NONE);
  788. pd->pet.hungry += pd->petDB->fullness;
  789. if( pd->pet.hungry > 100 )
  790. pd->pet.hungry = 100;
  791. log_feeding(sd, LOG_FEED_PET, pd->petDB->FoodID);
  792. clif_send_petdata(sd,pd,2,pd->pet.hungry);
  793. clif_send_petdata(sd,pd,1,pd->pet.intimate);
  794. clif_pet_food(sd,pd->petDB->FoodID,1);
  795. return 0;
  796. }
  797. /**
  798. * Let a pet walk around and update walk timer.
  799. * @param pd : pet requesting
  800. * @param tick : last walk time
  801. * @return 1:success, 0:failure
  802. */
  803. static int pet_randomwalk(struct pet_data *pd,unsigned int tick)
  804. {
  805. nullpo_ret(pd);
  806. Assert((pd->master == 0) || (pd->master->pd == pd));
  807. if(DIFF_TICK(pd->next_walktime,tick) < 0 && unit_can_move(&pd->bl)) {
  808. const int retrycount = 20;
  809. int i, c, d = 12-pd->move_fail_count;
  810. if(d < 5)
  811. d = 5;
  812. for(i = 0; i < retrycount; i++) {
  813. int r = rnd(), x, y;
  814. x = pd->bl.x+r%(d*2+1)-d;
  815. y = pd->bl.y+r/(d*2+1)%(d*2+1)-d;
  816. if(map_getcell(pd->bl.m,x,y,CELL_CHKPASS) && unit_walktoxy(&pd->bl,x,y,0)) {
  817. pd->move_fail_count = 0;
  818. break;
  819. }
  820. if(i + 1 >= retrycount) {
  821. pd->move_fail_count++;
  822. if(pd->move_fail_count > 1000) {
  823. ShowWarning("Pet can't move. Holding position %d, class = %d\n",pd->bl.id,pd->pet.class_);
  824. pd->move_fail_count = 0;
  825. pd->ud.canmove_tick = tick + 60000;
  826. return 0;
  827. }
  828. }
  829. }
  830. for(i = c = 0; i < pd->ud.walkpath.path_len; i++) {
  831. if(pd->ud.walkpath.path[i]&1)
  832. c += pd->status.speed*MOVE_DIAGONAL_COST/MOVE_COST;
  833. else
  834. c += pd->status.speed;
  835. }
  836. pd->next_walktime = tick+rnd()%1000+MIN_RANDOMWALKTIME+c;
  837. return 1;
  838. }
  839. return 0;
  840. }
  841. /**
  842. * Pet AI decision making when supporting master.
  843. * @param pd : pet requesting
  844. * @param sd : player requesting
  845. * @param tick : last support time
  846. * @return 0
  847. */
  848. static int pet_ai_sub_hard(struct pet_data *pd, struct map_session_data *sd, unsigned int tick)
  849. {
  850. struct block_list *target = NULL;
  851. if(pd->bl.prev == NULL || sd == NULL || sd->bl.prev == NULL)
  852. return 0;
  853. if(DIFF_TICK(tick,pd->last_thinktime) < MIN_PETTHINKTIME)
  854. return 0;
  855. pd->last_thinktime = tick;
  856. if(pd->ud.attacktimer != INVALID_TIMER || pd->ud.skilltimer != INVALID_TIMER || pd->bl.m != sd->bl.m)
  857. return 0;
  858. if(pd->ud.walktimer != INVALID_TIMER && pd->ud.walkpath.path_pos <= 2)
  859. return 0; // No thinking when you just started to walk.
  860. if(pd->pet.intimate <= 0) {
  861. // Pet should just... well, random walk.
  862. pet_randomwalk(pd,tick);
  863. return 0;
  864. }
  865. if (!check_distance_bl(&sd->bl, &pd->bl, pd->db->range3)) {
  866. // Master too far, chase.
  867. if(pd->target_id)
  868. pet_unlocktarget(pd);
  869. if(pd->ud.walktimer != INVALID_TIMER && pd->ud.target == sd->bl.id)
  870. return 0; // Already walking to him
  871. if (DIFF_TICK(tick, pd->ud.canmove_tick) < 0)
  872. return 0; // Can't move yet.
  873. pd->status.speed = (sd->battle_status.speed>>1);
  874. if(pd->status.speed == 0)
  875. pd->status.speed = 1;
  876. if (!unit_walktobl(&pd->bl, &sd->bl, 3, 0))
  877. pet_randomwalk(pd,tick);
  878. return 0;
  879. }
  880. // Return speed to normal.
  881. if (pd->status.speed != pd->petDB->speed) {
  882. if (pd->ud.walktimer != INVALID_TIMER)
  883. return 0; // Wait until the pet finishes walking back to master.
  884. pd->status.speed = pd->petDB->speed;
  885. pd->ud.state.change_walk_target = pd->ud.state.speed_changed = 1;
  886. }
  887. if (pd->target_id) {
  888. target = map_id2bl(pd->target_id);
  889. if (!target || pd->bl.m != target->m || status_isdead(target) ||
  890. !check_distance_bl(&pd->bl, target, pd->db->range3)) {
  891. target = NULL;
  892. pet_unlocktarget(pd);
  893. }
  894. }
  895. if(!target && pd->loot && pd->loot->count < pd->loot->max && DIFF_TICK(tick,pd->ud.canact_tick) > 0) {
  896. // Use half the pet's range of sight.
  897. map_foreachinrange(pet_ai_sub_hard_lootsearch, &pd->bl, pd->db->range2 / 2, BL_ITEM, pd, &target);
  898. }
  899. if (!target) { // Just walk around.
  900. if (check_distance_bl(&sd->bl, &pd->bl, 3))
  901. return 0; // Already next to master.
  902. if(pd->ud.walktimer != INVALID_TIMER && check_distance_blxy(&sd->bl, pd->ud.to_x,pd->ud.to_y, 3))
  903. return 0; // Already walking to him
  904. unit_calc_pos(&pd->bl, sd->bl.x, sd->bl.y, sd->ud.dir);
  905. if(!unit_walktoxy(&pd->bl,pd->ud.to_x,pd->ud.to_y,0))
  906. pet_randomwalk(pd,tick);
  907. return 0;
  908. }
  909. if(pd->ud.target == target->id &&
  910. (pd->ud.attacktimer != INVALID_TIMER || pd->ud.walktimer != INVALID_TIMER))
  911. return 0; //Target already locked.
  912. if (target->type != BL_ITEM) { // enemy targeted
  913. if(!battle_check_range(&pd->bl,target,pd->status.rhw.range)) { // Chase
  914. if(!unit_walktobl(&pd->bl, target, pd->status.rhw.range, 2))
  915. pet_unlocktarget(pd); // Unreachable target.
  916. return 0;
  917. }
  918. //Continuous attack.
  919. unit_attack(&pd->bl, pd->target_id, 1);
  920. } else { // Item Targeted, attempt loot
  921. if (!check_distance_bl(&pd->bl, target, 1)) { // Out of range
  922. if(!unit_walktobl(&pd->bl, target, 1, 1)) // Unreachable target.
  923. pet_unlocktarget(pd);
  924. return 0;
  925. } else {
  926. struct flooritem_data *fitem = (struct flooritem_data *)target;
  927. if(pd->loot->count < pd->loot->max) {
  928. memcpy(&pd->loot->item[pd->loot->count++],&fitem->item,sizeof(pd->loot->item[0]));
  929. pd->loot->weight += itemdb_weight(fitem->item.nameid)*fitem->item.amount;
  930. map_clearflooritem(target);
  931. }
  932. //Target is unlocked regardless of whether it was picked or not.
  933. pet_unlocktarget(pd);
  934. }
  935. }
  936. return 0;
  937. }
  938. /**
  939. * Call pet AI based on targets around.
  940. * @param sd : player requesting
  941. * @param ap
  942. * tick : last search time
  943. * @return 0
  944. */
  945. static int pet_ai_sub_foreachclient(struct map_session_data *sd,va_list ap)
  946. {
  947. unsigned int tick = va_arg(ap,unsigned int);
  948. if(sd->status.pet_id && sd->pd)
  949. pet_ai_sub_hard(sd->pd,sd,tick);
  950. return 0;
  951. }
  952. /**
  953. * Begin searching for targets.
  954. * @param tid : (unused)
  955. * @param tick : last search time
  956. * @param id : (unused)
  957. * @param data : data to pass to pet_ai_sub_foreachclient
  958. * @return 0
  959. */
  960. static int pet_ai_hard(int tid, unsigned int tick, int id, intptr_t data)
  961. {
  962. map_foreachpc(pet_ai_sub_foreachclient,tick);
  963. return 0;
  964. }
  965. /**
  966. * Make a pet search and grab loot if it can.
  967. * @param bl : item data
  968. * @param ap
  969. * pd : pet requesting
  970. * target : item
  971. * @return 1:success, 0:failure
  972. */
  973. static int pet_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap)
  974. {
  975. struct pet_data* pd;
  976. struct flooritem_data *fitem = (struct flooritem_data *)bl;
  977. struct block_list **target;
  978. int sd_charid = 0;
  979. pd = va_arg(ap,struct pet_data *);
  980. target = va_arg(ap,struct block_list**);
  981. sd_charid = fitem->first_get_charid;
  982. if(sd_charid && sd_charid != pd->master->status.char_id)
  983. return 0;
  984. if(unit_can_reach_bl(&pd->bl,bl, pd->db->range2, 1, NULL, NULL) &&
  985. ((*target) == NULL || //New target closer than previous one.
  986. !check_distance_bl(&pd->bl, *target, distance_bl(&pd->bl, bl)))) {
  987. (*target) = bl;
  988. pd->target_id = bl->id;
  989. return 1;
  990. }
  991. return 0;
  992. }
  993. /**
  994. * Make a pet drop their looted items.
  995. * @param tid : (unused)
  996. * @param tick : (unused)
  997. * @param data : items that were looted
  998. * @return 0
  999. */
  1000. static int pet_delay_item_drop(int tid, unsigned int tick, int id, intptr_t data)
  1001. {
  1002. struct item_drop_list *list;
  1003. struct item_drop *ditem;
  1004. list = (struct item_drop_list *)data;
  1005. ditem = list->item;
  1006. while (ditem) {
  1007. struct item_drop *ditem_prev;
  1008. map_addflooritem(&ditem->item_data,ditem->item_data.amount,
  1009. list->m,list->x,list->y,
  1010. list->first_charid,list->second_charid,list->third_charid,4,0);
  1011. ditem_prev = ditem;
  1012. ditem = ditem->next;
  1013. ers_free(item_drop_ers, ditem_prev);
  1014. }
  1015. ers_free(item_drop_list_ers, list);
  1016. return 0;
  1017. }
  1018. /**
  1019. * Make a pet drop their looted items.
  1020. * @param pd : pet requesting
  1021. * @param sd : player requesting
  1022. * @return 1:success, 0:failure
  1023. */
  1024. int pet_lootitem_drop(struct pet_data *pd,struct map_session_data *sd)
  1025. {
  1026. int i;
  1027. struct item_drop_list *dlist;
  1028. struct item_drop *ditem;
  1029. if(!pd || !pd->loot || !pd->loot->count)
  1030. return 0;
  1031. dlist = ers_alloc(item_drop_list_ers, struct item_drop_list);
  1032. dlist->m = pd->bl.m;
  1033. dlist->x = pd->bl.x;
  1034. dlist->y = pd->bl.y;
  1035. dlist->first_charid = 0;
  1036. dlist->second_charid = 0;
  1037. dlist->third_charid = 0;
  1038. dlist->item = NULL;
  1039. for(i = 0; i < pd->loot->count; i++) {
  1040. struct item *it;
  1041. it = &pd->loot->item[i];
  1042. if(sd){
  1043. unsigned char flag = 0;
  1044. if((flag = pc_additem(sd,it,it->amount,LOG_TYPE_PICKDROP_PLAYER))){
  1045. clif_additem(sd,0,0,flag);
  1046. ditem = ers_alloc(item_drop_ers, struct item_drop);
  1047. memcpy(&ditem->item_data, it, sizeof(struct item));
  1048. ditem->next = dlist->item;
  1049. dlist->item = ditem;
  1050. }
  1051. } else {
  1052. ditem = ers_alloc(item_drop_ers, struct item_drop);
  1053. memcpy(&ditem->item_data, it, sizeof(struct item));
  1054. ditem->next = dlist->item;
  1055. dlist->item = ditem;
  1056. }
  1057. }
  1058. //The smart thing to do is use pd->loot->max (thanks for pointing it out, Shinomori)
  1059. memset(pd->loot->item,0,pd->loot->max * sizeof(struct item));
  1060. pd->loot->count = 0;
  1061. pd->loot->weight = 0;
  1062. pd->ud.canact_tick = gettick()+10000; //prevent picked up during 10*1000ms
  1063. if (dlist->item)
  1064. add_timer(gettick()+540,pet_delay_item_drop,0,(intptr_t)dlist);
  1065. else
  1066. ers_free(item_drop_list_ers, dlist);
  1067. return 1;
  1068. }
  1069. /**
  1070. * Pet bonus giving skills.
  1071. * @param tid : current timer value
  1072. * @param tick : next time to use skill
  1073. * @param id : ID of pet owner
  1074. * @author [Valaris], rewritten by [Skotlex]
  1075. */
  1076. int pet_skill_bonus_timer(int tid, unsigned int tick, int id, intptr_t data)
  1077. {
  1078. struct map_session_data *sd = map_id2sd(id);
  1079. struct pet_data *pd;
  1080. int bonus;
  1081. int timer = 0;
  1082. if(sd == NULL || sd->pd==NULL || sd->pd->bonus == NULL)
  1083. return 1;
  1084. pd = sd->pd;
  1085. if(pd->bonus->timer != tid) {
  1086. ShowError("pet_skill_bonus_timer %d != %d\n",pd->bonus->timer,tid);
  1087. pd->bonus->timer = INVALID_TIMER;
  1088. return 0;
  1089. }
  1090. // determine the time for the next timer
  1091. if (pd->state.skillbonus && pd->bonus->delay > 0) {
  1092. bonus = 0;
  1093. timer = pd->bonus->delay*1000; // the duration until pet bonuses will be reactivated again
  1094. } else if (pd->pet.intimate) {
  1095. bonus = 1;
  1096. timer = pd->bonus->duration*1000; // the duration for pet bonuses to be in effect
  1097. } else { //Lost pet...
  1098. pd->bonus->timer = INVALID_TIMER;
  1099. return 0;
  1100. }
  1101. if (pd->state.skillbonus != bonus) {
  1102. pd->state.skillbonus = bonus;
  1103. status_calc_pc(sd,SCO_NONE);
  1104. }
  1105. // wait for the next timer
  1106. pd->bonus->timer=add_timer(tick+timer,pet_skill_bonus_timer,sd->bl.id,0);
  1107. return 0;
  1108. }
  1109. /**
  1110. * Pet recovery skills.
  1111. * @param tid : current timer value
  1112. * @param tick : next time to use skill
  1113. * @param id : ID of pet owner
  1114. * @param data : (unused)
  1115. * @return 0
  1116. * @author [Valaris], rewritten by [Skotlex]
  1117. */
  1118. int pet_recovery_timer(int tid, unsigned int tick, int id, intptr_t data)
  1119. {
  1120. struct map_session_data *sd = map_id2sd(id);
  1121. struct pet_data *pd;
  1122. if(sd == NULL || sd->pd == NULL || sd->pd->recovery == NULL)
  1123. return 1;
  1124. pd = sd->pd;
  1125. if(pd->recovery->timer != tid) {
  1126. ShowError("pet_recovery_timer %d != %d\n",pd->recovery->timer,tid);
  1127. return 0;
  1128. }
  1129. if(sd->sc.data[pd->recovery->type]) {
  1130. //Display a heal animation?
  1131. //Detoxify is chosen for now.
  1132. clif_skill_nodamage(&pd->bl,&sd->bl,TF_DETOXIFY,1,1);
  1133. status_change_end(&sd->bl, pd->recovery->type, INVALID_TIMER);
  1134. clif_emotion(&pd->bl, E_OK);
  1135. }
  1136. pd->recovery->timer = INVALID_TIMER;
  1137. return 0;
  1138. }
  1139. /**
  1140. * Pet natural regeneration timer.
  1141. * @param tid : current timer value
  1142. * @param tick : next time to regenerate
  1143. * @param id : ID of pet owner
  1144. */
  1145. int pet_heal_timer(int tid, unsigned int tick, int id, intptr_t data)
  1146. {
  1147. struct map_session_data *sd = map_id2sd(id);
  1148. struct status_data *status;
  1149. struct pet_data *pd;
  1150. unsigned int rate = 100;
  1151. if(sd == NULL || sd->pd == NULL || sd->pd->s_skill == NULL)
  1152. return 1;
  1153. pd = sd->pd;
  1154. if(pd->s_skill->timer != tid) {
  1155. ShowError("pet_heal_timer %d != %d\n",pd->s_skill->timer,tid);
  1156. return 0;
  1157. }
  1158. status = status_get_status_data(&sd->bl);
  1159. if(pc_isdead(sd) ||
  1160. (rate = get_percentage(status->sp, status->max_sp)) > pd->s_skill->sp ||
  1161. (rate = get_percentage(status->hp, status->max_hp)) > pd->s_skill->hp ||
  1162. (rate = (pd->ud.skilltimer != INVALID_TIMER)) //Another skill is in effect
  1163. ) { //Wait (how long? 1 sec for every 10% of remaining)
  1164. pd->s_skill->timer=add_timer(gettick()+(rate>10?rate:10)*100,pet_heal_timer,sd->bl.id,0);
  1165. return 0;
  1166. }
  1167. pet_stop_attack(pd);
  1168. pet_stop_walking(pd,1);
  1169. clif_skill_nodamage(&pd->bl,&sd->bl,AL_HEAL,pd->s_skill->lv,1);
  1170. status_heal(&sd->bl, pd->s_skill->lv,0, 0);
  1171. pd->s_skill->timer = add_timer(tick+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  1172. return 0;
  1173. }
  1174. /**
  1175. * Pet support skills.
  1176. * @param tid : current timer value
  1177. * @param tick : next time to use skill
  1178. * @param id : ID of pet owner
  1179. * @param data : (unused)
  1180. * @author [Skotlex]
  1181. */
  1182. int pet_skill_support_timer(int tid, unsigned int tick, int id, intptr_t data)
  1183. {
  1184. struct map_session_data *sd = map_id2sd(id);
  1185. struct pet_data *pd;
  1186. struct status_data *status;
  1187. short rate = 100;
  1188. if(sd == NULL || sd->pd == NULL || sd->pd->s_skill == NULL)
  1189. return 1;
  1190. pd = sd->pd;
  1191. if(pd->s_skill->timer != tid) {
  1192. ShowError("pet_skill_support_timer %d != %d\n",pd->s_skill->timer,tid);
  1193. return 0;
  1194. }
  1195. status = status_get_status_data(&sd->bl);
  1196. if (DIFF_TICK(pd->ud.canact_tick, tick) > 0) {
  1197. //Wait until the pet can act again.
  1198. pd->s_skill->timer=add_timer(pd->ud.canact_tick,pet_skill_support_timer,sd->bl.id,0);
  1199. return 0;
  1200. }
  1201. if(pc_isdead(sd) ||
  1202. (rate = get_percentage(status->sp, status->max_sp)) > pd->s_skill->sp ||
  1203. (rate = get_percentage(status->hp, status->max_hp)) > pd->s_skill->hp ||
  1204. (rate = (pd->ud.skilltimer != INVALID_TIMER)) //Another skill is in effect
  1205. ) { //Wait (how long? 1 sec for every 10% of remaining)
  1206. pd->s_skill->timer=add_timer(tick+(rate>10?rate:10)*100,pet_skill_support_timer,sd->bl.id,0);
  1207. return 0;
  1208. }
  1209. pet_stop_attack(pd);
  1210. pet_stop_walking(pd,1);
  1211. pd->s_skill->timer=add_timer(tick+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  1212. if (skill_get_inf(pd->s_skill->id) & INF_GROUND_SKILL)
  1213. unit_skilluse_pos(&pd->bl, sd->bl.x, sd->bl.y, pd->s_skill->id, pd->s_skill->lv);
  1214. else
  1215. unit_skilluse_id(&pd->bl, sd->bl.id, pd->s_skill->id, pd->s_skill->lv);
  1216. return 0;
  1217. }
  1218. /**
  1219. * Read pet databases. (pet_db.txt and pet_db2.txt)
  1220. */
  1221. void read_petdb()
  1222. {
  1223. char* filename[] = {
  1224. DBPATH"pet_db.txt",
  1225. DBIMPORT"/pet_db.txt"
  1226. };
  1227. unsigned short nameid;
  1228. int i,j;
  1229. // Remove any previous scripts in case reloaddb was invoked.
  1230. for( j = 0; j < MAX_PET_DB; j++ ) {
  1231. if( pet_db[j].pet_script ) {
  1232. script_free_code(pet_db[j].pet_script);
  1233. pet_db[j].pet_script = NULL;
  1234. }
  1235. if( pet_db[j].pet_loyal_script ) {
  1236. script_free_code(pet_db[j].pet_loyal_script);
  1237. pet_db[j].pet_loyal_script = NULL;
  1238. }
  1239. }
  1240. // clear database
  1241. memset(pet_db,0,sizeof(pet_db));
  1242. j = 0; // entry counter
  1243. for( i = 0; i < ARRAYLENGTH(filename); i++ ) {
  1244. char line[1024];
  1245. int lines, entries;
  1246. FILE *fp;
  1247. sprintf(line, "%s/%s", db_path, filename[i]);
  1248. fp = fopen(line,"r");
  1249. if( fp == NULL ) {
  1250. if( i == 0 )
  1251. ShowError("can't read %s\n",line);
  1252. continue;
  1253. }
  1254. lines = entries = 0;
  1255. while( fgets(line, sizeof(line), fp) && j < MAX_PET_DB ) {
  1256. char *str[22], *p;
  1257. unsigned k;
  1258. lines++;
  1259. if(line[0] == '/' && line[1] == '/')
  1260. continue;
  1261. memset(str, 0, sizeof(str));
  1262. p = line;
  1263. while( ISSPACE(*p) )
  1264. ++p;
  1265. if( *p == '\0' )
  1266. continue; // empty line
  1267. for( k = 0; k < 20; ++k ) {
  1268. str[k] = p;
  1269. p = strchr(p,',');
  1270. if( p == NULL )
  1271. break; // comma not found
  1272. *p = '\0';
  1273. ++p;
  1274. }
  1275. if( p == NULL ) {
  1276. ShowError("read_petdb: Insufficient columns in line %d, skipping.\n", lines);
  1277. continue;
  1278. }
  1279. // Pet Script
  1280. if( *p != '{' ) {
  1281. ShowError("read_petdb: Invalid format (Pet Script column) in line %d, skipping.\n", lines);
  1282. continue;
  1283. }
  1284. str[20] = p;
  1285. p = strstr(p+1,"},");
  1286. if( p == NULL ) {
  1287. ShowError("read_petdb: Invalid format (Pet Script column) in line %d, skipping.\n", lines);
  1288. continue;
  1289. }
  1290. p[1] = '\0';
  1291. p += 2;
  1292. // Equip Script
  1293. if( *p != '{' ) {
  1294. ShowError("read_petdb: Invalid format (Equip Script column) in line %d, skipping.\n", lines);
  1295. continue;
  1296. }
  1297. str[21] = p;
  1298. if( (nameid = atoi(str[0])) <= 0 )
  1299. continue;
  1300. if( !mobdb_checkid(nameid) ) {
  1301. ShowWarning("pet_db reading: Invalid mob-class %hu, pet not read.\n", nameid);
  1302. continue;
  1303. }
  1304. pet_db[j].class_ = nameid;
  1305. safestrncpy(pet_db[j].name,str[1],NAME_LENGTH);
  1306. safestrncpy(pet_db[j].jname,str[2],NAME_LENGTH);
  1307. pet_db[j].itemID=atoi(str[3]);
  1308. pet_db[j].EggID=atoi(str[4]);
  1309. pet_db[j].AcceID=atoi(str[5]);
  1310. pet_db[j].FoodID=atoi(str[6]);
  1311. pet_db[j].fullness=atoi(str[7]);
  1312. pet_db[j].hungry_delay=atoi(str[8])*1000;
  1313. pet_db[j].r_hungry=atoi(str[9]);
  1314. if( pet_db[j].r_hungry <= 0 )
  1315. pet_db[j].r_hungry=1;
  1316. pet_db[j].r_full=atoi(str[10]);
  1317. pet_db[j].intimate=atoi(str[11]);
  1318. pet_db[j].die=atoi(str[12]);
  1319. pet_db[j].capture=atoi(str[13]);
  1320. pet_db[j].speed=atoi(str[14]);
  1321. pet_db[j].s_perfor=(char)atoi(str[15]);
  1322. pet_db[j].talk_convert_class=atoi(str[16]);
  1323. pet_db[j].attack_rate=atoi(str[17]);
  1324. pet_db[j].defence_attack_rate=atoi(str[18]);
  1325. pet_db[j].change_target_rate=atoi(str[19]);
  1326. pet_db[j].pet_script = NULL;
  1327. pet_db[j].pet_loyal_script = NULL;
  1328. if( *str[20] )
  1329. pet_db[j].pet_script = parse_script(str[20], filename[i], lines, 0);
  1330. if( *str[21] )
  1331. pet_db[j].pet_loyal_script = parse_script(str[21], filename[i], lines, 0);
  1332. j++;
  1333. entries++;
  1334. }
  1335. if( j >= MAX_PET_DB )
  1336. ShowWarning("read_petdb: Reached max number of pets [%d]. Remaining pets were not read.\n ", MAX_PET_DB);
  1337. fclose(fp);
  1338. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' pets in '"CL_WHITE"%s/%s"CL_RESET"'.\n", entries, db_path, filename[i]);
  1339. }
  1340. }
  1341. /**
  1342. * Initialization process of skill relationship.
  1343. */
  1344. void do_init_pet(void)
  1345. {
  1346. read_petdb();
  1347. item_drop_ers = ers_new(sizeof(struct item_drop),"pet.c::item_drop_ers",ERS_OPT_NONE);
  1348. item_drop_list_ers = ers_new(sizeof(struct item_drop_list),"pet.c::item_drop_list_ers",ERS_OPT_NONE);
  1349. add_timer_func_list(pet_hungry,"pet_hungry");
  1350. add_timer_func_list(pet_ai_hard,"pet_ai_hard");
  1351. add_timer_func_list(pet_skill_bonus_timer,"pet_skill_bonus_timer"); // [Valaris]
  1352. add_timer_func_list(pet_delay_item_drop,"pet_delay_item_drop");
  1353. add_timer_func_list(pet_skill_support_timer, "pet_skill_support_timer"); // [Skotlex]
  1354. add_timer_func_list(pet_recovery_timer,"pet_recovery_timer"); // [Valaris]
  1355. add_timer_func_list(pet_heal_timer,"pet_heal_timer"); // [Valaris]
  1356. add_timer_interval(gettick()+MIN_PETTHINKTIME,pet_ai_hard,0,0,MIN_PETTHINKTIME);
  1357. }
  1358. /**
  1359. * Pet destructor.
  1360. */
  1361. void do_final_pet(void)
  1362. {
  1363. int i;
  1364. for( i = 0; i < MAX_PET_DB; i++ ) {
  1365. if( pet_db[i].pet_script ) {
  1366. script_free_code(pet_db[i].pet_script);
  1367. pet_db[i].pet_script = NULL;
  1368. }
  1369. if( pet_db[i].pet_loyal_script ) {
  1370. script_free_code(pet_db[i].pet_loyal_script);
  1371. pet_db[i].pet_loyal_script = NULL;
  1372. }
  1373. }
  1374. ers_destroy(item_drop_ers);
  1375. ers_destroy(item_drop_list_ers);
  1376. }