pet.c 36 KB

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