pet.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. // $Id: pet.c,v 1.4 2004/09/25 05:32:18 MouseJstr Exp $
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "db.h"
  6. #include "timer.h"
  7. #include "socket.h"
  8. #include "nullpo.h"
  9. #include "malloc.h"
  10. #include "pc.h"
  11. #include "map.h"
  12. #include "intif.h"
  13. #include "clif.h"
  14. #include "chrif.h"
  15. #include "pet.h"
  16. #include "itemdb.h"
  17. #include "battle.h"
  18. #include "mob.h"
  19. #include "npc.h"
  20. #include "script.h"
  21. #include "skill.h"
  22. #ifdef MEMWATCH
  23. #include "memwatch.h"
  24. #endif
  25. #define MIN_PETTHINKTIME 100
  26. struct pet_db pet_db[MAX_PET_DB];
  27. static int dirx[8]={0,-1,-1,-1,0,1,1,1};
  28. static int diry[8]={1,1,0,-1,-1,-1,0,1};
  29. static int pet_timer(int tid,unsigned int tick,int id,int data);
  30. static int pet_walktoxy_sub(struct pet_data *pd);
  31. static int distance(int x0,int y0,int x1,int y1)
  32. {
  33. int dx,dy;
  34. dx=abs(x0-x1);
  35. dy=abs(y0-y1);
  36. return dx>dy ? dx : dy;
  37. }
  38. static int calc_next_walk_step(struct pet_data *pd)
  39. {
  40. nullpo_retr(0, pd);
  41. if(pd->walkpath.path_pos>=pd->walkpath.path_len)
  42. return -1;
  43. if(pd->walkpath.path[pd->walkpath.path_pos]&1)
  44. return pd->speed*14/10;
  45. return pd->speed;
  46. }
  47. static int pet_performance_val(struct map_session_data *sd)
  48. {
  49. nullpo_retr(0, sd);
  50. if(sd->pet.intimate > 900)
  51. return (sd->petDB->s_perfor > 0)? 4:3;
  52. else if(sd->pet.intimate > 750)
  53. return 2;
  54. else
  55. return 1;
  56. }
  57. int pet_hungry_val(struct map_session_data *sd)
  58. {
  59. nullpo_retr(0, sd);
  60. if(sd->pet.hungry > 90)
  61. return 4;
  62. else if(sd->pet.hungry > 75)
  63. return 3;
  64. else if(sd->pet.hungry > 25)
  65. return 2;
  66. else if(sd->pet.hungry > 10)
  67. return 1;
  68. else
  69. return 0;
  70. }
  71. static int pet_can_reach(struct pet_data *pd,int x,int y)
  72. {
  73. struct walkpath_data wpd;
  74. nullpo_retr(0, pd);
  75. if( pd->bl.x==x && pd->bl.y==y ) // 同じマス
  76. return 1;
  77. // 障害物判定
  78. wpd.path_len=0;
  79. wpd.path_pos=0;
  80. wpd.path_half=0;
  81. return (path_search(&wpd,pd->bl.m,pd->bl.x,pd->bl.y,x,y,0)!=-1)?1:0;
  82. }
  83. static int pet_calc_pos(struct pet_data *pd,int tx,int ty,int dir)
  84. {
  85. int x,y,dx,dy;
  86. int i,j=0,k;
  87. nullpo_retr(0, pd);
  88. pd->to_x = tx;
  89. pd->to_y = ty;
  90. if(dir >= 0 && dir < 8) {
  91. dx = -dirx[dir]*2;
  92. dy = -diry[dir]*2;
  93. x = tx + dx;
  94. y = ty + dy;
  95. if(!(j=pet_can_reach(pd,x,y))) {
  96. if(dx > 0) x--;
  97. else if(dx < 0) x++;
  98. if(dy > 0) y--;
  99. else if(dy < 0) y++;
  100. if(!(j=pet_can_reach(pd,x,y))) {
  101. for(i=0;i<12;i++) {
  102. k = rand()%8;
  103. dx = -dirx[k]*2;
  104. dy = -diry[k]*2;
  105. x = tx + dx;
  106. y = ty + dy;
  107. if((j=pet_can_reach(pd,x,y)))
  108. break;
  109. else {
  110. if(dx > 0) x--;
  111. else if(dx < 0) x++;
  112. if(dy > 0) y--;
  113. else if(dy < 0) y++;
  114. if((j=pet_can_reach(pd,x,y)))
  115. break;
  116. }
  117. }
  118. if(!j) {
  119. x = tx;
  120. y = ty;
  121. if(!pet_can_reach(pd,x,y))
  122. return 1;
  123. }
  124. }
  125. }
  126. }
  127. else
  128. return 1;
  129. pd->to_x = x;
  130. pd->to_y = y;
  131. return 0;
  132. }
  133. static int pet_attack(struct pet_data *pd,unsigned int tick,int data)
  134. {
  135. struct mob_data *md;
  136. int mode,race,range;
  137. nullpo_retr(0, pd);
  138. pd->state.state=MS_IDLE;
  139. md=(struct mob_data *)map_id2bl(pd->target_id);
  140. if(md == NULL || md->bl.type != BL_MOB || pd->bl.m != md->bl.m || md->bl.prev == NULL ||
  141. distance(pd->bl.x,pd->bl.y,md->bl.x,md->bl.y) > 13) {
  142. pd->target_id=0;
  143. return 0;
  144. }
  145. mode=mob_db[pd->class].mode;
  146. race=mob_db[pd->class].race;
  147. if(mob_db[pd->class].mexp <= 0 && !(mode&0x20) && (md->option & 0x06 && race != 4 && race != 6) ) {
  148. pd->target_id=0;
  149. return 0;
  150. }
  151. range = mob_db[pd->class].range + 1;
  152. if(distance(pd->bl.x,pd->bl.y,md->bl.x,md->bl.y) > range)
  153. return 0;
  154. if(battle_config.monster_attack_direction_change)
  155. pd->dir=map_calc_dir(&pd->bl, md->bl.x,md->bl.y );
  156. clif_fixpetpos(pd);
  157. pd->target_lv = battle_weapon_attack(&pd->bl,&md->bl,tick,0);
  158. pd->attackabletime = tick + battle_get_adelay(&pd->bl);
  159. pd->timer=add_timer(pd->attackabletime,pet_timer,pd->bl.id,0);
  160. pd->state.state=MS_ATTACK;
  161. return 0;
  162. }
  163. /*==========================================
  164. *
  165. *------------------------------------------
  166. */
  167. static int pet_walk(struct pet_data *pd,unsigned int tick,int data)
  168. {
  169. int moveblock;
  170. int i,ctype;
  171. int x,y,dx,dy;
  172. nullpo_retr(0, pd);
  173. pd->state.state=MS_IDLE;
  174. if(pd->walkpath.path_pos >= pd->walkpath.path_len || pd->walkpath.path_pos != data)
  175. return 0;
  176. pd->walkpath.path_half ^= 1;
  177. if(pd->walkpath.path_half==0){
  178. pd->walkpath.path_pos++;
  179. if(pd->state.change_walk_target){
  180. pet_walktoxy_sub(pd);
  181. return 0;
  182. }
  183. }
  184. else {
  185. if(pd->walkpath.path[pd->walkpath.path_pos] >= 8)
  186. return 1;
  187. x = pd->bl.x;
  188. y = pd->bl.y;
  189. /* ctype = map_getcell(pd->bl.m,x,y);
  190. if(ctype == 1 || ctype == 5) {
  191. pet_stop_walking(pd,1);
  192. return 0;
  193. }*/
  194. pd->dir=pd->walkpath.path[pd->walkpath.path_pos];
  195. dx = dirx[pd->dir];
  196. dy = diry[pd->dir];
  197. ctype = map_getcell(pd->bl.m,x+dx,y+dy);
  198. if(ctype == 1 || ctype == 5) {
  199. pet_walktoxy_sub(pd);
  200. return 0;
  201. }
  202. moveblock = ( x/BLOCK_SIZE != (x+dx)/BLOCK_SIZE || y/BLOCK_SIZE != (y+dy)/BLOCK_SIZE);
  203. pd->state.state=MS_WALK;
  204. map_foreachinmovearea(clif_petoutsight,pd->bl.m,x-AREA_SIZE,y-AREA_SIZE,x+AREA_SIZE,y+AREA_SIZE,dx,dy,BL_PC,pd);
  205. x += dx;
  206. y += dy;
  207. if(moveblock) map_delblock(&pd->bl);
  208. pd->bl.x = x;
  209. pd->bl.y = y;
  210. if(moveblock) map_addblock(&pd->bl);
  211. map_foreachinmovearea(clif_petinsight,pd->bl.m,x-AREA_SIZE,y-AREA_SIZE,x+AREA_SIZE,y+AREA_SIZE,-dx,-dy,BL_PC,pd);
  212. pd->state.state=MS_IDLE;
  213. }
  214. if((i=calc_next_walk_step(pd))>0){
  215. i = i>>1;
  216. if(i < 1 && pd->walkpath.path_half == 0)
  217. i = 1;
  218. pd->timer=add_timer(tick+i,pet_timer,pd->bl.id,pd->walkpath.path_pos);
  219. pd->state.state=MS_WALK;
  220. if(pd->walkpath.path_pos >= pd->walkpath.path_len)
  221. clif_fixpetpos(pd);
  222. }
  223. return 0;
  224. }
  225. int pet_stopattack(struct pet_data *pd)
  226. {
  227. nullpo_retr(0, pd);
  228. pd->target_id=0;
  229. if(pd->state.state == MS_ATTACK)
  230. pet_changestate(pd,MS_IDLE,0);
  231. return 0;
  232. }
  233. int pet_target_check(struct map_session_data *sd,struct block_list *bl,int type)
  234. {
  235. struct pet_data *pd;
  236. struct mob_data *md;
  237. int rate,mode,race;
  238. nullpo_retr(0, sd);
  239. pd = sd->pd;
  240. if(bl && pd && bl->type == BL_MOB && sd->pet.intimate > 900 && sd->pet.hungry > 0 && pd->class != battle_get_class(bl)
  241. && pd->state.state != MS_DELAY) {
  242. mode=mob_db[pd->class].mode;
  243. race=mob_db[pd->class].race;
  244. md=(struct mob_data *)bl;
  245. if(md->bl.type != BL_MOB || pd->bl.m != md->bl.m || md->bl.prev == NULL ||
  246. distance(pd->bl.x,pd->bl.y,md->bl.x,md->bl.y) > 13)
  247. return 0;
  248. if(mob_db[pd->class].mexp <= 0 && !(mode&0x20) && (md->option & 0x06 && race!=4 && race!=6) )
  249. return 0;
  250. if(!type) {
  251. rate = sd->petDB->attack_rate;
  252. rate = rate * (150 - (sd->pet.intimate - 1000))/100;
  253. if(battle_config.pet_support_rate != 100)
  254. rate = rate*battle_config.pet_support_rate/100;
  255. if(sd->petDB->attack_rate > 0 && rate <= 0)
  256. rate = 1;
  257. }
  258. else {
  259. rate = sd->petDB->defence_attack_rate;
  260. rate = rate * (150 - (sd->pet.intimate - 1000))/100;
  261. if(battle_config.pet_support_rate != 100)
  262. rate = rate*battle_config.pet_support_rate/100;
  263. if(sd->petDB->defence_attack_rate > 0 && rate <= 0)
  264. rate = 1;
  265. }
  266. if(rand()%10000 < rate) {
  267. if(pd->target_id == 0 || rand()%10000 < sd->petDB->change_target_rate)
  268. pd->target_id = bl->id;
  269. }
  270. }
  271. return 0;
  272. }
  273. int pet_changestate(struct pet_data *pd,int state,int type)
  274. {
  275. unsigned int tick;
  276. int i;
  277. nullpo_retr(0, pd);
  278. if(pd->timer != -1)
  279. delete_timer(pd->timer,pet_timer);
  280. pd->timer=-1;
  281. pd->state.state=state;
  282. switch(state) {
  283. case MS_WALK:
  284. if((i=calc_next_walk_step(pd)) > 0){
  285. i = i>>2;
  286. pd->timer=add_timer(gettick()+i,pet_timer,pd->bl.id,0);
  287. } else
  288. pd->state.state=MS_IDLE;
  289. break;
  290. case MS_ATTACK:
  291. tick = gettick();
  292. i=DIFF_TICK(pd->attackabletime,tick);
  293. if(i>0 && i<2000)
  294. pd->timer=add_timer(pd->attackabletime,pet_timer,pd->bl.id,0);
  295. else
  296. pd->timer=add_timer(tick+1,pet_timer,pd->bl.id,0);
  297. break;
  298. case MS_DELAY:
  299. pd->timer=add_timer(gettick()+type,pet_timer,pd->bl.id,0);
  300. break;
  301. }
  302. return 0;
  303. }
  304. static int pet_timer(int tid,unsigned int tick,int id,int data)
  305. {
  306. struct pet_data *pd;
  307. pd=(struct pet_data*)map_id2bl(id);
  308. if(pd == NULL || pd->bl.type != BL_PET)
  309. return 1;
  310. if(pd->timer != tid){
  311. if(battle_config.error_log)
  312. printf("pet_timer %d != %d\n",pd->timer,tid);
  313. return 0;
  314. }
  315. pd->timer=-1;
  316. if(pd->bl.prev == NULL)
  317. return 1;
  318. switch(pd->state.state){
  319. case MS_WALK:
  320. pet_walk(pd,tick,data);
  321. break;
  322. case MS_ATTACK:
  323. pet_attack(pd,tick,data);
  324. break;
  325. case MS_DELAY:
  326. pet_changestate(pd,MS_IDLE,0);
  327. break;
  328. default:
  329. if(battle_config.error_log)
  330. printf("pet_timer : %d ?\n",pd->state.state);
  331. break;
  332. }
  333. return 0;
  334. }
  335. static int pet_walktoxy_sub(struct pet_data *pd)
  336. {
  337. struct walkpath_data wpd;
  338. nullpo_retr(0, pd);
  339. if(path_search(&wpd,pd->bl.m,pd->bl.x,pd->bl.y,pd->to_x,pd->to_y,0))
  340. return 1;
  341. memcpy(&pd->walkpath,&wpd,sizeof(wpd));
  342. pd->state.change_walk_target=0;
  343. pet_changestate(pd,MS_WALK,0);
  344. clif_movepet(pd);
  345. // if(battle_config.etc_log)
  346. // printf("walkstart\n");
  347. return 0;
  348. }
  349. int pet_walktoxy(struct pet_data *pd,int x,int y)
  350. {
  351. struct walkpath_data wpd;
  352. nullpo_retr(0, pd);
  353. if(pd->state.state == MS_WALK && path_search(&wpd,pd->bl.m,pd->bl.x,pd->bl.y,x,y,0))
  354. return 1;
  355. pd->to_x=x;
  356. pd->to_y=y;
  357. if(pd->state.state == MS_WALK) {
  358. pd->state.change_walk_target=1;
  359. } else {
  360. return pet_walktoxy_sub(pd);
  361. }
  362. return 0;
  363. }
  364. int pet_stop_walking(struct pet_data *pd,int type)
  365. {
  366. nullpo_retr(0, pd);
  367. if(pd->state.state == MS_WALK || pd->state.state == MS_IDLE) {
  368. pd->walkpath.path_len=0;
  369. pd->to_x=pd->bl.x;
  370. pd->to_y=pd->bl.y;
  371. }
  372. if(type&0x01)
  373. clif_fixpetpos(pd);
  374. if(type&~0xff)
  375. pet_changestate(pd,MS_DELAY,type>>8);
  376. else
  377. pet_changestate(pd,MS_IDLE,0);
  378. return 0;
  379. }
  380. static int pet_hungry(int tid,unsigned int tick,int id,int data)
  381. {
  382. struct map_session_data *sd;
  383. int interval,t;
  384. sd=map_id2sd(id);
  385. if(sd==NULL)
  386. return 1;
  387. if(sd->pet_hungry_timer != tid){
  388. if(battle_config.error_log)
  389. printf("pet_hungry_timer %d != %d\n",sd->pet_hungry_timer,tid);
  390. return 0;
  391. }
  392. sd->pet_hungry_timer = -1;
  393. if(!sd->status.pet_id || !sd->pd || !sd->petDB)
  394. return 1;
  395. sd->pet.hungry--;
  396. t = sd->pet.intimate;
  397. if(sd->pet.hungry < 0) {
  398. if(sd->pd->target_id > 0)
  399. pet_stopattack(sd->pd);
  400. sd->pet.hungry = 0;
  401. sd->pet.intimate -= battle_config.pet_hungry_friendly_decrease;
  402. if(sd->pet.intimate <= 0) {
  403. sd->pet.intimate = 0;
  404. if(battle_config.pet_status_support && t > 0) {
  405. if(sd->bl.prev != NULL)
  406. pc_calcstatus(sd,0);
  407. else
  408. pc_calcstatus(sd,2);
  409. }
  410. }
  411. clif_send_petdata(sd,1,sd->pet.intimate);
  412. }
  413. clif_send_petdata(sd,2,sd->pet.hungry);
  414. if(battle_config.pet_hungry_delay_rate != 100)
  415. interval = (sd->petDB->hungry_delay*battle_config.pet_hungry_delay_rate)/100;
  416. else
  417. interval = sd->petDB->hungry_delay;
  418. if(interval <= 0)
  419. interval = 1;
  420. sd->pet_hungry_timer = add_timer(tick+interval,pet_hungry,sd->bl.id,0);
  421. return 0;
  422. }
  423. int search_petDB_index(int key,int type)
  424. {
  425. int i;
  426. for(i=0;i<MAX_PET_DB;i++) {
  427. if(pet_db[i].class <= 0)
  428. continue;
  429. switch(type) {
  430. case PET_CLASS:
  431. if(pet_db[i].class == key)
  432. return i;
  433. break;
  434. case PET_CATCH:
  435. if(pet_db[i].itemID == key)
  436. return i;
  437. break;
  438. case PET_EGG:
  439. if(pet_db[i].EggID == key)
  440. return i;
  441. break;
  442. case PET_EQUIP:
  443. if(pet_db[i].AcceID == key)
  444. return i;
  445. break;
  446. case PET_FOOD:
  447. if(pet_db[i].FoodID == key)
  448. return i;
  449. break;
  450. default:
  451. return -1;
  452. }
  453. }
  454. return -1;
  455. }
  456. int pet_hungry_timer_delete(struct map_session_data *sd)
  457. {
  458. nullpo_retr(0, sd);
  459. if(sd->pet_hungry_timer != -1) {
  460. delete_timer(sd->pet_hungry_timer,pet_hungry);
  461. sd->pet_hungry_timer = -1;
  462. }
  463. return 0;
  464. }
  465. int pet_remove_map(struct map_session_data *sd)
  466. {
  467. nullpo_retr(0, sd);
  468. if(sd->status.pet_id && sd->pd) {
  469. struct pet_data *pd=sd->pd; // [Valaris]
  470. if(pd->skillbonustimer!=-1) pd->skillbonustimer=-1;
  471. if(pd->skillbonusduration!=-1) pd->skillbonusduration=-1;
  472. if(pd->skilltype !=-1) pd->skilltype=-1;
  473. if(pd->skillval !=-1) pd->skillval=-1;
  474. if(pd->skilltimer!=-1) pd->skilltimer=-1;
  475. if(pd->skillduration!=-1) pd->skillduration=-1;
  476. if(pd->skillbonustype!=-1) pd->skillbonustype=-1;
  477. if(pd->skillbonusval!=-1) pd->skillbonusval=-1;
  478. if(sd->perfect_hiding==1) sd->perfect_hiding=0; // end additions
  479. pet_changestate(sd->pd,MS_IDLE,0);
  480. if(sd->pet_hungry_timer != -1)
  481. pet_hungry_timer_delete(sd);
  482. clif_clearchar_area(&sd->pd->bl,0);
  483. map_delblock(&sd->pd->bl);
  484. map_deliddb(&sd->pd->bl);
  485. map_freeblock(sd->pd);
  486. }
  487. return 0;
  488. }
  489. struct delay_item_drop {
  490. int m,x,y;
  491. int nameid,amount;
  492. struct map_session_data *first_sd,*second_sd,*third_sd;
  493. };
  494. struct delay_item_drop2 {
  495. int m,x,y;
  496. struct item item_data;
  497. struct map_session_data *first_sd,*second_sd,*third_sd;
  498. };
  499. int pet_performance(struct map_session_data *sd)
  500. {
  501. struct pet_data *pd;
  502. nullpo_retr(0, sd);
  503. nullpo_retr(0, pd=sd->pd);
  504. pet_stop_walking(pd,2000<<8);
  505. clif_pet_performance(&pd->bl,rand()%pet_performance_val(sd) + 1);
  506. // ルートしたItemを落とさせる
  507. pet_lootitem_drop(pd,NULL);
  508. return 0;
  509. }
  510. int pet_return_egg(struct map_session_data *sd)
  511. {
  512. struct item tmp_item;
  513. int flag;
  514. nullpo_retr(0, sd);
  515. if(sd->status.pet_id && sd->pd) {
  516. struct pet_data *pd=sd->pd;
  517. pet_remove_map(sd);
  518. sd->status.pet_id = 0;
  519. sd->pd = NULL;
  520. if(sd->petDB == NULL)
  521. return 1;
  522. sd->pet.incuvate = 1;
  523. memset(&tmp_item,0,sizeof(tmp_item));
  524. tmp_item.nameid = sd->petDB->EggID;
  525. tmp_item.identify = 1;
  526. tmp_item.card[0] = 0xff00;
  527. *((long *)(&tmp_item.card[1])) = sd->pet.pet_id;
  528. tmp_item.card[3] = sd->pet.rename_flag;
  529. if((flag = pc_additem(sd,&tmp_item,1))) {
  530. clif_additem(sd,0,0,flag);
  531. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  532. }
  533. if(battle_config.pet_status_support && sd->pet.intimate > 0) {
  534. if(sd->bl.prev != NULL)
  535. pc_calcstatus(sd,0);
  536. else
  537. pc_calcstatus(sd,2);
  538. }
  539. // ルートしたItemを落とさせる
  540. pet_lootitem_drop(pd,sd);
  541. intif_save_petdata(sd->status.account_id,&sd->pet);
  542. pc_makesavestatus(sd);
  543. chrif_save(sd);
  544. storage_storage_save(sd);
  545. sd->petDB = NULL;
  546. }
  547. return 0;
  548. }
  549. int pet_data_init(struct map_session_data *sd)
  550. {
  551. struct pet_data *pd;
  552. int i=0,interval=0;
  553. nullpo_retr(1, sd);
  554. if(sd->status.account_id != sd->pet.account_id || sd->status.char_id != sd->pet.char_id ||
  555. sd->status.pet_id != sd->pet.pet_id) {
  556. sd->status.pet_id = 0;
  557. return 1;
  558. }
  559. i = search_petDB_index(sd->pet.class,PET_CLASS);
  560. if(i < 0) {
  561. sd->status.pet_id = 0;
  562. return 1;
  563. }
  564. sd->petDB = &pet_db[i];
  565. sd->pd = pd = (struct pet_data *)aCalloc(1,sizeof(struct pet_data));
  566. pd->bl.m = sd->bl.m;
  567. pd->bl.prev = pd->bl.next = NULL;
  568. pd->bl.x = pd->to_x = sd->bl.x;
  569. pd->bl.y = pd->to_y = sd->bl.y;
  570. pet_calc_pos(pd,sd->bl.x,sd->bl.y,sd->dir);
  571. pd->bl.x = pd->to_x;
  572. pd->bl.y = pd->to_y;
  573. pd->bl.id = npc_get_new_npc_id();
  574. memcpy(pd->name,sd->pet.name,24);
  575. pd->class = sd->pet.class;
  576. pd->equip = sd->pet.equip;
  577. pd->dir = sd->dir;
  578. pd->speed = sd->petDB->speed;
  579. pd->bl.subtype = MONS;
  580. pd->bl.type = BL_PET;
  581. memset(&pd->state,0,sizeof(pd->state));
  582. pd->state.state = MS_IDLE;
  583. pd->state.change_walk_target = 0;
  584. pd->timer = -1;
  585. pd->target_id = 0;
  586. pd->move_fail_count = 0;
  587. pd->next_walktime = pd->attackabletime = pd->last_thinktime = gettick();
  588. pd->msd = sd;
  589. map_addiddb(&pd->bl);
  590. if(sd->pet_hungry_timer != -1)
  591. pet_hungry_timer_delete(sd);
  592. if(battle_config.pet_hungry_delay_rate != 100)
  593. interval = (sd->petDB->hungry_delay*battle_config.pet_hungry_delay_rate)/100;
  594. else
  595. interval = sd->petDB->hungry_delay;
  596. if(interval <= 0)
  597. interval = 1;
  598. sd->pet_hungry_timer = add_timer(gettick()+interval,pet_hungry,sd->bl.id,0);
  599. pd->lootitem=(struct item *)aCalloc(PETLOOT_SIZE,sizeof(struct item));
  600. pd->lootitem_count = 0;
  601. pd->lootitem_weight = 0;
  602. pd->lootitem_timer = gettick();
  603. return 0;
  604. }
  605. int pet_birth_process(struct map_session_data *sd)
  606. {
  607. nullpo_retr(1, sd);
  608. if(sd->status.pet_id && sd->pet.incuvate == 1) {
  609. sd->status.pet_id = 0;
  610. return 1;
  611. }
  612. sd->pet.incuvate = 0;
  613. sd->pet.account_id = sd->status.account_id;
  614. sd->pet.char_id = sd->status.char_id;
  615. sd->status.pet_id = sd->pet.pet_id;
  616. if(pet_data_init(sd)) {
  617. sd->status.pet_id = 0;
  618. sd->pet.incuvate = 1;
  619. sd->pet.account_id = 0;
  620. sd->pet.char_id = 0;
  621. return 1;
  622. }
  623. intif_save_petdata(sd->status.account_id,&sd->pet);
  624. pc_makesavestatus(sd);
  625. chrif_save(sd);
  626. storage_storage_save(sd);
  627. map_addblock(&sd->pd->bl);
  628. clif_spawnpet(sd->pd);
  629. clif_send_petdata(sd,0,0);
  630. clif_send_petdata(sd,5,0x14);
  631. clif_pet_equip(sd->pd,sd->pet.equip);
  632. clif_send_petstatus(sd);
  633. return 0;
  634. }
  635. int pet_recv_petdata(int account_id,struct s_pet *p,int flag)
  636. {
  637. struct map_session_data *sd;
  638. sd = map_id2sd(account_id);
  639. if(sd == NULL)
  640. return 1;
  641. if(flag == 1) {
  642. sd->status.pet_id = 0;
  643. return 1;
  644. }
  645. memcpy(&sd->pet,p,sizeof(struct s_pet));
  646. if(sd->pet.incuvate == 1)
  647. pet_birth_process(sd);
  648. else {
  649. pet_data_init(sd);
  650. if(sd->bl.prev != NULL) {
  651. map_addblock(&sd->pd->bl);
  652. clif_spawnpet(sd->pd);
  653. clif_send_petdata(sd,0,0);
  654. clif_send_petdata(sd,5,0x14);
  655. // clif_pet_equip(sd->pd,sd->pet.equip);
  656. clif_send_petstatus(sd);
  657. }
  658. }
  659. if(battle_config.pet_status_support && sd->pet.intimate > 0) {
  660. if(sd->bl.prev != NULL)
  661. pc_calcstatus(sd,0);
  662. else
  663. pc_calcstatus(sd,2);
  664. }
  665. return 0;
  666. }
  667. int pet_select_egg(struct map_session_data *sd,short egg_index)
  668. {
  669. nullpo_retr(0, sd);
  670. if(sd->status.inventory[egg_index].card[0] == (short)0xff00)
  671. intif_request_petdata(sd->status.account_id,sd->status.char_id,*((long *)&sd->status.inventory[egg_index].card[1]));
  672. else {
  673. if(battle_config.error_log)
  674. printf("wrong egg item inventory %d\n",egg_index);
  675. }
  676. pc_delitem(sd,egg_index,1,0);
  677. return 0;
  678. }
  679. int pet_catch_process1(struct map_session_data *sd,int target_class)
  680. {
  681. nullpo_retr(0, sd);
  682. sd->catch_target_class = target_class;
  683. clif_catch_process(sd);
  684. return 0;
  685. }
  686. int pet_catch_process2(struct map_session_data *sd,int target_id)
  687. {
  688. struct mob_data *md;
  689. int i=0,pet_catch_rate=0;
  690. nullpo_retr(1, sd);
  691. md=(struct mob_data*)map_id2bl(target_id);
  692. if(!md){
  693. clif_pet_rulet(sd,0);
  694. return 1;
  695. }
  696. i = search_petDB_index(md->class,PET_CLASS);
  697. if(md == NULL || md->bl.type != BL_MOB || md->bl.prev == NULL || i < 0 || sd->catch_target_class != md->class) {
  698. clif_pet_rulet(sd,0);
  699. return 1;
  700. }
  701. //target_idによる敵→卵判定
  702. // if(battle_config.etc_log)
  703. // printf("mob_id = %d, mob_class = %d\n",md->bl.id,md->class);
  704. //成功の場合
  705. pet_catch_rate = (pet_db[i].capture + (sd->status.base_level - mob_db[md->class].lv)*30 + sd->paramc[5]*20)*(200 - md->hp*100/mob_db[md->class].max_hp)/100;
  706. if(pet_catch_rate < 1) pet_catch_rate = 1;
  707. if(battle_config.pet_catch_rate != 100)
  708. pet_catch_rate = (pet_catch_rate*battle_config.pet_catch_rate)/100;
  709. if(rand()%10000 < pet_catch_rate) {
  710. mob_catch_delete(md,0);
  711. clif_pet_rulet(sd,1);
  712. // if(battle_config.etc_log)
  713. // printf("rulet success %d\n",target_id);
  714. intif_create_pet(sd->status.account_id,sd->status.char_id,pet_db[i].class,mob_db[pet_db[i].class].lv,
  715. pet_db[i].EggID,0,pet_db[i].intimate,100,0,1,pet_db[i].jname);
  716. }
  717. else
  718. clif_pet_rulet(sd,0);
  719. return 0;
  720. }
  721. int pet_get_egg(int account_id,int pet_id,int flag)
  722. {
  723. struct map_session_data *sd;
  724. struct item tmp_item;
  725. int i=0,ret=0;
  726. if(!flag) {
  727. sd = map_id2sd(account_id);
  728. if(sd == NULL)
  729. return 1;
  730. i = search_petDB_index(sd->catch_target_class,PET_CLASS);
  731. if(i >= 0) {
  732. memset(&tmp_item,0,sizeof(tmp_item));
  733. tmp_item.nameid = pet_db[i].EggID;
  734. tmp_item.identify = 1;
  735. tmp_item.card[0] = 0xff00;
  736. *((long *)(&tmp_item.card[1])) = pet_id;
  737. tmp_item.card[3] = sd->pet.rename_flag;
  738. if((ret = pc_additem(sd,&tmp_item,1))) {
  739. clif_additem(sd,0,0,ret);
  740. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  741. }
  742. }
  743. else
  744. intif_delete_petdata(pet_id);
  745. }
  746. return 0;
  747. }
  748. int pet_menu(struct map_session_data *sd,int menunum)
  749. {
  750. nullpo_retr(0, sd);
  751. switch(menunum) {
  752. case 0:
  753. clif_send_petstatus(sd);
  754. break;
  755. case 1:
  756. pet_food(sd);
  757. break;
  758. case 2:
  759. pet_performance(sd);
  760. break;
  761. case 3:
  762. pet_return_egg(sd);
  763. break;
  764. case 4:
  765. pet_unequipitem(sd);
  766. break;
  767. }
  768. return 0;
  769. }
  770. int pet_change_name(struct map_session_data *sd,char *name)
  771. {
  772. int i;
  773. nullpo_retr(1, sd);
  774. if(sd->pet.rename_flag == 1 && battle_config.pet_rename == 0)
  775. return 1;
  776. for(i=0;i<24 && name[i];i++){
  777. if( !(name[i]&0xe0) || name[i]==0x7f)
  778. return 1;
  779. }
  780. pet_stop_walking(sd->pd,1);
  781. memcpy(sd->pet.name,name,24);
  782. memcpy(sd->pd->name,name,24);
  783. clif_clearchar_area(&sd->pd->bl,0);
  784. clif_spawnpet(sd->pd);
  785. clif_send_petdata(sd,0,0);
  786. clif_send_petdata(sd,5,0x14);
  787. sd->pet.rename_flag = 1;
  788. clif_pet_equip(sd->pd,sd->pet.equip);
  789. clif_send_petstatus(sd);
  790. return 0;
  791. }
  792. int pet_equipitem(struct map_session_data *sd,int index)
  793. {
  794. int nameid;
  795. nullpo_retr(1, sd);
  796. nameid = sd->status.inventory[index].nameid;
  797. if(sd->petDB == NULL)
  798. return 1;
  799. if(sd->petDB->AcceID == 0 || nameid != sd->petDB->AcceID || sd->pet.equip != 0) {
  800. clif_equipitemack(sd,0,0,0);
  801. return 1;
  802. }
  803. else {
  804. pc_delitem(sd,index,1,0);
  805. sd->pet.equip = sd->pd->equip = nameid;
  806. pc_calcstatus(sd,0);
  807. clif_pet_equip(sd->pd,nameid);
  808. }
  809. return 0;
  810. }
  811. int pet_unequipitem(struct map_session_data *sd)
  812. {
  813. struct item tmp_item;
  814. int nameid,flag;
  815. nullpo_retr(1, sd);
  816. if(sd->petDB == NULL)
  817. return 1;
  818. if(sd->pet.equip == 0)
  819. return 1;
  820. nameid = sd->pet.equip;
  821. sd->pet.equip = sd->pd->equip = 0;
  822. pc_calcstatus(sd,0);
  823. clif_pet_equip(sd->pd,0);
  824. memset(&tmp_item,0,sizeof(tmp_item));
  825. tmp_item.nameid = nameid;
  826. tmp_item.identify = 1;
  827. if((flag = pc_additem(sd,&tmp_item,1))) {
  828. clif_additem(sd,0,0,flag);
  829. map_addflooritem(&tmp_item,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  830. }
  831. return 0;
  832. }
  833. int pet_food(struct map_session_data *sd)
  834. {
  835. int i,k,t;
  836. nullpo_retr(1, sd);
  837. if(sd->petDB == NULL)
  838. return 1;
  839. i=pc_search_inventory(sd,sd->petDB->FoodID);
  840. if(i < 0) {
  841. clif_pet_food(sd,sd->petDB->FoodID,0);
  842. return 1;
  843. }
  844. pc_delitem(sd,i,1,0);
  845. t = sd->pet.intimate;
  846. if(sd->pet.hungry > 90)
  847. sd->pet.intimate -= sd->petDB->r_full;
  848. else if(sd->pet.hungry > 75) {
  849. if(battle_config.pet_friendly_rate != 100)
  850. k = (sd->petDB->r_hungry * battle_config.pet_friendly_rate)/100;
  851. else
  852. k = sd->petDB->r_hungry;
  853. k = k >> 1;
  854. if(k <= 0)
  855. k = 1;
  856. sd->pet.intimate += k;
  857. }
  858. else {
  859. if(battle_config.pet_friendly_rate != 100)
  860. k = (sd->petDB->r_hungry * battle_config.pet_friendly_rate)/100;
  861. else
  862. k = sd->petDB->r_hungry;
  863. sd->pet.intimate += k;
  864. }
  865. if(sd->pet.intimate <= 0) {
  866. sd->pet.intimate = 0;
  867. if(battle_config.pet_status_support && t > 0) {
  868. if(sd->bl.prev != NULL)
  869. pc_calcstatus(sd,0);
  870. else
  871. pc_calcstatus(sd,2);
  872. }
  873. }
  874. else if(sd->pet.intimate > 1000)
  875. sd->pet.intimate = 1000;
  876. sd->pet.hungry += sd->petDB->fullness;
  877. if(sd->pet.hungry > 100)
  878. sd->pet.hungry = 100;
  879. clif_send_petdata(sd,2,sd->pet.hungry);
  880. clif_send_petdata(sd,1,sd->pet.intimate);
  881. clif_pet_food(sd,sd->petDB->FoodID,1);
  882. return 0;
  883. }
  884. static int pet_randomwalk(struct pet_data *pd,int tick)
  885. {
  886. const int retrycount=20;
  887. int speed;
  888. nullpo_retr(0, pd);
  889. speed = battle_get_speed(&pd->bl);
  890. if(DIFF_TICK(pd->next_walktime,tick) < 0){
  891. int i,x,y,c,d=12-pd->move_fail_count;
  892. if(d<5) d=5;
  893. for(i=0;i<retrycount;i++){
  894. int r=rand();
  895. x=pd->bl.x+r%(d*2+1)-d;
  896. y=pd->bl.y+r/(d*2+1)%(d*2+1)-d;
  897. if((c=map_getcell(pd->bl.m,x,y))!=1 && c!=5 && pet_walktoxy(pd,x,y)==0){
  898. pd->move_fail_count=0;
  899. break;
  900. }
  901. if(i+1>=retrycount){
  902. pd->move_fail_count++;
  903. if(pd->move_fail_count>1000){
  904. if(battle_config.error_log)
  905. printf("PET cant move. hold position %d, class = %d\n",pd->bl.id,pd->class);
  906. pd->move_fail_count=0;
  907. pet_changestate(pd,MS_DELAY,60000);
  908. return 0;
  909. }
  910. }
  911. }
  912. for(i=c=0;i<pd->walkpath.path_len;i++){
  913. if(pd->walkpath.path[i]&1)
  914. c+=speed*14/10;
  915. else
  916. c+=speed;
  917. }
  918. pd->next_walktime = tick+rand()%3000+3000+c;
  919. return 1;
  920. }
  921. return 0;
  922. }
  923. static int pet_unlocktarget(struct pet_data *pd)
  924. {
  925. nullpo_retr(0, pd);
  926. pd->target_id=0;
  927. return 0;
  928. }
  929. static int pet_ai_sub_hard(struct pet_data *pd,unsigned int tick)
  930. {
  931. struct map_session_data *sd = pd->msd;
  932. struct mob_data *md = NULL;
  933. int dist,i=0,dx,dy,ret;
  934. int mode,race;
  935. nullpo_retr(0, pd);
  936. sd = pd->msd;
  937. if(pd->bl.prev == NULL || sd == NULL || sd->bl.prev == NULL)
  938. return 0;
  939. if(DIFF_TICK(tick,pd->last_thinktime) < MIN_PETTHINKTIME)
  940. return 0;
  941. pd->last_thinktime=tick;
  942. if(pd->state.state == MS_DELAY || pd->bl.m != sd->bl.m)
  943. return 0;
  944. // ペットによるルート
  945. if(!pd->target_id && pd->lootitem_count < PETLOOT_SIZE && pd->lootitem_count < pd->lootmax && pd->loot==1 && DIFF_TICK(gettick(),pd->lootitem_timer)>0)
  946. map_foreachinarea(pet_ai_sub_hard_lootsearch,pd->bl.m,
  947. pd->bl.x-AREA_SIZE*2,pd->bl.y-AREA_SIZE*2,
  948. pd->bl.x+AREA_SIZE*2,pd->bl.y+AREA_SIZE*2,
  949. BL_ITEM,pd,&i);
  950. if(sd->pet.intimate > 0) {
  951. dist = distance(sd->bl.x,sd->bl.y,pd->bl.x,pd->bl.y);
  952. if(dist > 12) {
  953. if(pd->target_id > 0)
  954. pet_unlocktarget(pd);
  955. if(pd->timer != -1 && pd->state.state == MS_WALK && distance(pd->to_x,pd->to_y,sd->bl.x,sd->bl.y) < 3)
  956. return 0;
  957. pd->speed = (sd->speed>>1);
  958. if(pd->speed <= 0)
  959. pd->speed = 1;
  960. pet_calc_pos(pd,sd->bl.x,sd->bl.y,sd->dir);
  961. if(pet_walktoxy(pd,pd->to_x,pd->to_y))
  962. pet_randomwalk(pd,tick);
  963. }
  964. else if(pd->target_id - MAX_FLOORITEM > 0) {
  965. mode=mob_db[pd->class].mode;
  966. race=mob_db[pd->class].race;
  967. md=(struct mob_data *)map_id2bl(pd->target_id);
  968. if(md == NULL || md->bl.type != BL_MOB || pd->bl.m != md->bl.m || md->bl.prev == NULL ||
  969. distance(pd->bl.x,pd->bl.y,md->bl.x,md->bl.y) > 13)
  970. pet_unlocktarget(pd);
  971. else if(mob_db[pd->class].mexp <= 0 && !(mode&0x20) && (md->option & 0x06 && race!=4 && race!=6) )
  972. pet_unlocktarget(pd);
  973. else if(!battle_check_range(&pd->bl,&md->bl,mob_db[pd->class].range)){
  974. if(pd->timer != -1 && pd->state.state == MS_WALK && distance(pd->to_x,pd->to_y,md->bl.x,md->bl.y) < 2)
  975. return 0;
  976. if( !pet_can_reach(pd,md->bl.x,md->bl.y))
  977. pet_unlocktarget(pd);
  978. else {
  979. i=0;
  980. pd->speed = battle_get_speed(&pd->bl);
  981. do {
  982. if(i==0) { // 最初はAEGISと同じ方法で検索
  983. dx=md->bl.x - pd->bl.x;
  984. dy=md->bl.y - pd->bl.y;
  985. if(dx<0) dx++;
  986. else if(dx>0) dx--;
  987. if(dy<0) dy++;
  988. else if(dy>0) dy--;
  989. }
  990. else { // だめならAthena式(ランダム)
  991. dx=md->bl.x - pd->bl.x + rand()%3 - 1;
  992. dy=md->bl.y - pd->bl.y + rand()%3 - 1;
  993. }
  994. ret=pet_walktoxy(pd,pd->bl.x+dx,pd->bl.y+dy);
  995. i++;
  996. } while(ret && i<5);
  997. if(ret) { // 移動不可能な所からの攻撃なら2歩下る
  998. if(dx<0) dx=2;
  999. else if(dx>0) dx=-2;
  1000. if(dy<0) dy=2;
  1001. else if(dy>0) dy=-2;
  1002. pet_walktoxy(pd,pd->bl.x+dx,pd->bl.y+dy);
  1003. }
  1004. }
  1005. }
  1006. else {
  1007. if(pd->state.state==MS_WALK)
  1008. pet_stop_walking(pd,1);
  1009. if(pd->state.state==MS_ATTACK)
  1010. return 0;
  1011. pet_changestate(pd,MS_ATTACK,0);
  1012. }
  1013. }
  1014. else if(pd->target_id > 0){ // ルート処理
  1015. struct block_list *bl_item;
  1016. struct flooritem_data *fitem;
  1017. bl_item = map_id2bl(pd->target_id);
  1018. if(bl_item == NULL || bl_item->type != BL_ITEM ||bl_item->m != pd->bl.m ||
  1019. (dist=distance(pd->bl.x,pd->bl.y,bl_item->x,bl_item->y))>=5){
  1020. // 遠すぎるかアイテムがなくなった
  1021. pet_unlocktarget(pd);
  1022. }
  1023. else if(dist){
  1024. if(pd->timer != -1 && pd->state.state!=MS_ATTACK && (DIFF_TICK(pd->next_walktime,tick)<0 || distance(pd->to_x,pd->to_y,bl_item->x,bl_item->y) <= 0))
  1025. return 0; // 既に移動中
  1026. pd->next_walktime=tick+500;
  1027. dx=bl_item->x - pd->bl.x;
  1028. dy=bl_item->y - pd->bl.y;
  1029. ret=pet_walktoxy(pd,pd->bl.x+dx,pd->bl.y+dy);
  1030. }
  1031. else{ // アイテムまでたどり着いた
  1032. fitem = (struct flooritem_data *)bl_item;
  1033. if(pd->state.state==MS_ATTACK)
  1034. return 0; // 攻撃中
  1035. if(pd->state.state==MS_WALK){ // 歩行中なら停止
  1036. pet_stop_walking(pd,1);
  1037. }
  1038. if(pd->lootitem_count < PETLOOT_SIZE && pd->lootitem_count < pd->lootmax){
  1039. memcpy(&pd->lootitem[pd->lootitem_count++],&fitem->item_data,sizeof(pd->lootitem[0]));
  1040. pd->lootitem_weight += itemdb_search(fitem->item_data.nameid)->weight*fitem->item_data.amount;
  1041. }
  1042. else if(pd->lootitem_count >= PETLOOT_SIZE || pd->lootitem_count >=pd->lootmax) {
  1043. pet_unlocktarget(pd);
  1044. return 0;
  1045. }
  1046. else {
  1047. if(pd->lootitem[0].card[0] == (short)0xff00)
  1048. intif_delete_petdata(*((long *)(&pd->lootitem[0].card[1])));
  1049. for(i=0;i<PETLOOT_SIZE-1;i++)
  1050. memcpy(&pd->lootitem[i],&pd->lootitem[i+1],sizeof(pd->lootitem[0]));
  1051. memcpy(&pd->lootitem[PETLOOT_SIZE-1],&fitem->item_data,sizeof(pd->lootitem[0]));
  1052. }
  1053. map_clearflooritem(bl_item->id);
  1054. pet_unlocktarget(pd);
  1055. }
  1056. }
  1057. else {
  1058. if(dist <= 3 || (pd->timer != -1 && pd->state.state == MS_WALK && distance(pd->to_x,pd->to_y,sd->bl.x,sd->bl.y) < 3) )
  1059. return 0;
  1060. pd->speed = battle_get_speed(&pd->bl);
  1061. pet_calc_pos(pd,sd->bl.x,sd->bl.y,sd->dir);
  1062. if(pet_walktoxy(pd,pd->to_x,pd->to_y))
  1063. pet_randomwalk(pd,tick);
  1064. }
  1065. }
  1066. else {
  1067. pd->speed = battle_get_speed(&pd->bl);
  1068. if(pd->state.state == MS_ATTACK)
  1069. pet_stopattack(pd);
  1070. pet_randomwalk(pd,tick);
  1071. }
  1072. return 0;
  1073. }
  1074. static int pet_ai_sub_foreachclient(struct map_session_data *sd,va_list ap)
  1075. {
  1076. unsigned int tick;
  1077. nullpo_retr(0, sd);
  1078. nullpo_retr(0, ap);
  1079. tick=va_arg(ap,unsigned int);
  1080. if(sd->status.pet_id && sd->pd && sd->petDB)
  1081. pet_ai_sub_hard(sd->pd,tick);
  1082. return 0;
  1083. }
  1084. static int pet_ai_hard(int tid,unsigned int tick,int id,int data)
  1085. {
  1086. clif_foreachclient(pet_ai_sub_foreachclient,tick);
  1087. return 0;
  1088. }
  1089. int pet_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap)
  1090. {
  1091. struct pet_data* pd;
  1092. int dist,*itc;
  1093. nullpo_retr(0, bl);
  1094. nullpo_retr(0, ap);
  1095. nullpo_retr(0, pd=va_arg(ap,struct pet_data *));
  1096. nullpo_retr(0, itc=va_arg(ap,int *));
  1097. if(!pd->target_id){
  1098. struct flooritem_data *fitem = (struct flooritem_data *)bl;
  1099. struct map_session_data *sd = NULL;
  1100. // ルート権無し
  1101. if(fitem && fitem->first_get_id>0)
  1102. sd = map_id2sd(fitem->first_get_id);
  1103. // Removed [Valaris]
  1104. //if((pd->lootitem_weight + (itemdb_search(fitem->item_data.))->weight * fitem->item_data.amount) > battle_config.pet_weight)
  1105. // return 0;
  1106. if(!pd->lootitem || (pd->lootitem_count >= PETLOOT_SIZE) || (pd->lootitem_count >= pd->lootmax) || (sd && sd->pd != pd))
  1107. return 0;
  1108. if(bl->m == pd->bl.m && (dist=distance(pd->bl.x,pd->bl.y,bl->x,bl->y))<5){
  1109. if( pet_can_reach(pd,bl->x,bl->y) // 到達可能性判定
  1110. && rand()%1000<1000/(++(*itc)) ){ // 範囲内PCで等確率にする
  1111. pd->target_id=bl->id;
  1112. }
  1113. }
  1114. }
  1115. return 0;
  1116. }
  1117. int pet_lootitem_drop(struct pet_data *pd,struct map_session_data *sd)
  1118. {
  1119. int i,flag=0;
  1120. if(pd){
  1121. if(pd->lootitem) {
  1122. for(i=0;i<pd->lootitem_count;i++) {
  1123. struct delay_item_drop2 *ditem;
  1124. ditem=(struct delay_item_drop2 *)aCalloc(1,sizeof(struct delay_item_drop2));
  1125. memcpy(&ditem->item_data,&pd->lootitem[i],sizeof(pd->lootitem[0]));
  1126. ditem->m = pd->bl.m;
  1127. ditem->x = pd->bl.x;
  1128. ditem->y = pd->bl.y;
  1129. ditem->first_sd = 0;
  1130. ditem->second_sd = 0;
  1131. ditem->third_sd = 0;
  1132. // 落とさないで直接PCのItem欄へ
  1133. if(sd){
  1134. if((flag = pc_additem(sd,&ditem->item_data,ditem->item_data.amount))){
  1135. clif_additem(sd,0,0,flag);
  1136. map_addflooritem(&ditem->item_data,ditem->item_data.amount,ditem->m,ditem->x,ditem->y,ditem->first_sd,ditem->second_sd,ditem->third_sd,0);
  1137. }
  1138. free(ditem);
  1139. }
  1140. else
  1141. add_timer(gettick()+540+i,pet_delay_item_drop2,(int)ditem,0);
  1142. }
  1143. pd->lootitem=NULL;
  1144. pd->lootitem=(struct item *)aCalloc(PETLOOT_SIZE,sizeof(struct item));
  1145. pd->lootitem_count = 0;
  1146. pd->lootitem_weight = 0;
  1147. pd->lootitem_timer = gettick()+10000; // 10*1000msの間拾わない
  1148. }
  1149. }
  1150. return 1;
  1151. }
  1152. int pet_delay_item_drop2(int tid,unsigned int tick,int id,int data)
  1153. {
  1154. struct delay_item_drop2 *ditem;
  1155. ditem=(struct delay_item_drop2 *)id;
  1156. map_addflooritem(&ditem->item_data,ditem->item_data.amount,ditem->m,ditem->x,ditem->y,ditem->first_sd,ditem->second_sd,ditem->third_sd,0);
  1157. free(ditem);
  1158. return 0;
  1159. }
  1160. /*==========================================
  1161. * pet bonus giving skills [Valaris]
  1162. *------------------------------------------
  1163. */
  1164. int pet_skill_bonus(struct map_session_data *sd,struct pet_data *pd,int type,int val,int duration,int timer,int data)
  1165. {
  1166. if(pd==NULL || sd==NULL)
  1167. return 1;
  1168. pd->skillbonustype=type;
  1169. pd->skillbonusval=val;
  1170. pd->skillduration=duration;
  1171. pd->skilltimer=timer;
  1172. pd->skillbonustimer=add_timer(gettick()+pd->skilltimer*1000,pet_skill_bonus_timer,sd->bl.id,0);
  1173. return 0;
  1174. }
  1175. int pet_skill_bonus_timer(int tid,unsigned int tick,int id,int data)
  1176. {
  1177. struct map_session_data *sd=(struct map_session_data*)map_id2bl(id);
  1178. struct pet_data *pd;
  1179. if(sd==NULL || sd->bl.type!=BL_PC)
  1180. return 1;
  1181. pd=sd->pd;
  1182. if(pd==NULL || pd->bl.type!=BL_PET)
  1183. return 1;
  1184. if(pd->skillbonustimer != tid)
  1185. return 0;
  1186. pd->skillbonustimer=-1;
  1187. pc_bonus(sd,pd->skillbonustype,pd->skillbonusval);
  1188. if(pd->skillbonustype < 56) clif_updatestatus(sd,pd->skillbonustype);
  1189. pd->skillbonusduration=add_timer(gettick()+pd->skillduration*1000,pet_skill_bonus_duration,sd->bl.id,0);
  1190. return 0;
  1191. }
  1192. int pet_skill_bonus_duration(int tid,unsigned int tick,int id,int data)
  1193. {
  1194. struct map_session_data *sd=(struct map_session_data*)map_id2bl(id);
  1195. struct pet_data *pd;
  1196. if(sd==NULL || sd->bl.type!=BL_PC)
  1197. return 1;
  1198. pd=sd->pd;
  1199. if(pd==NULL || pd->bl.type!=BL_PET)
  1200. return 1;
  1201. if(pd->skillbonusduration != tid)
  1202. return 0;
  1203. pd->skillbonusduration=-1;
  1204. pc_bonus(sd,pd->skillbonustype,-pd->skillbonusval);
  1205. if(pd->skillbonustype < 56) clif_updatestatus(sd,pd->skillbonustype);
  1206. pet_skill_bonus(sd,pd,pd->skillbonustype,pd->skillbonusval,pd->skillduration,pd->skilltimer,0);
  1207. return 0;
  1208. }
  1209. int pet_recovery_timer(int tid,unsigned int tick,int id,int data)
  1210. {
  1211. struct map_session_data *sd=(struct map_session_data*)map_id2bl(id);
  1212. struct pet_data *pd;
  1213. if(sd==NULL || sd->bl.type!=BL_PC)
  1214. return 1;
  1215. pd=sd->pd;
  1216. if(pd==NULL || pd->bl.type!=BL_PET)
  1217. return 1;
  1218. if(pd->skillbonustimer != tid)
  1219. return 0;
  1220. if(sd->sc_data[pd->skilltype].timer != -1)
  1221. skill_status_change_end(&sd->bl,pd->skilltype,-1);
  1222. pd->skillbonustimer=add_timer(gettick()+pd->skilltimer*1000,pet_recovery_timer,sd->bl.id,0);
  1223. return 0;
  1224. }
  1225. int pet_heal_timer(int tid,unsigned int tick,int id,int data)
  1226. {
  1227. struct map_session_data *sd=(struct map_session_data*)map_id2bl(id);
  1228. struct pet_data *pd;
  1229. if(sd==NULL || sd->bl.type!=BL_PC)
  1230. return 1;
  1231. pd=sd->pd;
  1232. if(pd==NULL || pd->bl.type!=BL_PET)
  1233. return 1;
  1234. if(pd->skillbonustimer != tid)
  1235. return 0;
  1236. if(sd->status.hp < sd->status.max_hp * pd->skilltype/100) {
  1237. clif_skill_nodamage(&pd->bl,&sd->bl,AL_HEAL,pd->skillval,1);
  1238. pc_heal(sd,pd->skillval,0);
  1239. }
  1240. pd->skillbonustimer=add_timer(gettick()+pd->skilltimer*1000,pet_heal_timer,sd->bl.id,0);
  1241. return 0;
  1242. }
  1243. int pet_mag_timer(int tid,unsigned int tick,int id,int data)
  1244. {
  1245. struct pet_data *pd;
  1246. struct map_session_data *sd=(struct map_session_data*)map_id2bl(id);
  1247. if(sd==NULL || sd->bl.type!=BL_PC)
  1248. return 1;
  1249. pd=sd->pd;
  1250. if(pd==NULL || pd->bl.type!=BL_PET)
  1251. return 1;
  1252. if(pd->skillbonustimer != tid)
  1253. return 0;
  1254. if(sd->status.hp < sd->status.max_hp * pd->skilltype/100 && sd->status.sp < sd->status.max_sp * pd->skillduration/100) {
  1255. clif_skill_nodamage(&pd->bl,&sd->bl,PR_MAGNIFICAT,pd->skillval,1);
  1256. skill_status_change_start(&sd->bl,SkillStatusChangeTable[PR_MAGNIFICAT],pd->skillval,0,0,0,skill_get_time(PR_MAGNIFICAT,pd->skillval),0 );
  1257. }
  1258. pd->skillbonustimer=add_timer(gettick()+pd->skilltimer*1000,pet_mag_timer,sd->bl.id,0);
  1259. return 0;
  1260. }
  1261. int pet_skillattack_timer(int tid,unsigned int tick,int id,int data)
  1262. {
  1263. struct mob_data *md;
  1264. struct map_session_data *sd=(struct map_session_data*)map_id2bl(id);
  1265. struct pet_data *pd;
  1266. if(sd==NULL || sd->bl.type!=BL_PC)
  1267. return 1;
  1268. pd=sd->pd;
  1269. if(pd==NULL || pd->bl.type!=BL_PET)
  1270. return 1;
  1271. if(pd->skillbonustimer != tid)
  1272. return 0;
  1273. md=(struct mob_data *)map_id2bl(sd->attacktarget);
  1274. if(md == NULL || md->bl.type != BL_MOB || pd->bl.m != md->bl.m || md->bl.prev == NULL ||
  1275. distance(pd->bl.x,pd->bl.y,md->bl.x,md->bl.y) > 6) {
  1276. pd->target_id=0;
  1277. pd->skillbonustimer=add_timer(gettick()+100,pet_skillattack_timer,sd->bl.id,pd->skillduration);
  1278. return 0;
  1279. }
  1280. if(md && rand()%100 < sd->pet.intimate*pd->skilltimer/100 ) {
  1281. if(pd->skilltype==6 || pd->skilltype==176) {
  1282. skill_castend_nodamage_id(&pd->bl,&md->bl,pd->skilltype,pd->skillval,tick,0);
  1283. }
  1284. else if(pd->skilltype==110){
  1285. skill_castend_pos2(&pd->bl,md->bl.x,md->bl.y,pd->skilltype,pd->skillval,tick,0);
  1286. }
  1287. else if(pd->skilltype==91) {
  1288. skill_castend_pos2(&pd->bl,md->bl.x,md->bl.y,pd->skilltype,pd->skillval+rand()%100,tick,0);
  1289. }
  1290. else
  1291. skill_castend_damage_id(&pd->bl,&md->bl,pd->skilltype,pd->skillval,tick,0);
  1292. pd->skillbonustimer=add_timer(gettick()+1000,pet_skillattack_timer,sd->bl.id,0);
  1293. return 0;
  1294. }
  1295. pd->skillbonustimer=add_timer(gettick()+100,pet_skillattack_timer,sd->bl.id,0);
  1296. return 0;
  1297. }
  1298. /*==========================================
  1299. *ペットデータ読み込み
  1300. *------------------------------------------
  1301. */
  1302. int read_petdb()
  1303. {
  1304. FILE *fp;
  1305. char line[1024];
  1306. int i;
  1307. int j=0;
  1308. char *filename[]={"db/pet_db.txt","db/pet_db2.txt"};
  1309. memset(pet_db,0,sizeof(pet_db));
  1310. for(i=0;i<2;i++){
  1311. fp=fopen(filename[i],"r");
  1312. if(fp==NULL){
  1313. if(i>0)
  1314. continue;
  1315. printf("can't read %s\n",filename[i]);
  1316. return -1;
  1317. }
  1318. while(fgets(line,1020,fp)){
  1319. int nameid,i;
  1320. char *str[32],*p,*np;
  1321. if(line[0] == '/' && line[1] == '/')
  1322. continue;
  1323. for(i=0,p=line;i<20;i++){
  1324. if((np=strchr(p,','))!=NULL){
  1325. str[i]=p;
  1326. *np=0;
  1327. p=np+1;
  1328. } else {
  1329. str[i]=p;
  1330. p+=strlen(p);
  1331. }
  1332. }
  1333. nameid=atoi(str[0]);
  1334. if(nameid<=0 || nameid>2000)
  1335. continue;
  1336. //MobID,Name,JName,ItemID,EggID,AcceID,FoodID,"Fullness (1回の餌での満腹度増加率%)","HungryDeray (/min)","R_Hungry (空腹時餌やり親密度増加率%)","R_Full (とても満腹時餌やり親密度減少率%)","Intimate (捕獲時親密度%)","Die (死亡時親密度減少率%)","Capture (捕獲率%)",(Name)
  1337. pet_db[j].class = nameid;
  1338. memcpy(pet_db[j].name,str[1],24);
  1339. memcpy(pet_db[j].jname,str[2],24);
  1340. pet_db[j].itemID=atoi(str[3]);
  1341. pet_db[j].EggID=atoi(str[4]);
  1342. pet_db[j].AcceID=atoi(str[5]);
  1343. pet_db[j].FoodID=atoi(str[6]);
  1344. pet_db[j].fullness=atoi(str[7]);
  1345. pet_db[j].hungry_delay=atoi(str[8])*1000;
  1346. pet_db[j].r_hungry=atoi(str[9]);
  1347. if(pet_db[j].r_hungry <= 0)
  1348. pet_db[j].r_hungry=1;
  1349. pet_db[j].r_full=atoi(str[10]);
  1350. pet_db[j].intimate=atoi(str[11]);
  1351. pet_db[j].die=atoi(str[12]);
  1352. pet_db[j].capture=atoi(str[13]);
  1353. pet_db[j].speed=atoi(str[14]);
  1354. pet_db[j].s_perfor=(char)atoi(str[15]);
  1355. pet_db[j].talk_convert_class=atoi(str[16]);
  1356. pet_db[j].attack_rate=atoi(str[17]);
  1357. pet_db[j].defence_attack_rate=atoi(str[18]);
  1358. pet_db[j].change_target_rate=atoi(str[19]);
  1359. pet_db[j].script = NULL;
  1360. if((np=strchr(p,'{'))==NULL)
  1361. continue;
  1362. pet_db[j].script = parse_script(np,0);
  1363. j++;
  1364. }
  1365. fclose(fp);
  1366. printf("read %s done (count=%d)\n",filename[i],j);
  1367. }
  1368. return 0;
  1369. }
  1370. /*==========================================
  1371. * スキル関係初期化処理
  1372. *------------------------------------------
  1373. */
  1374. int do_init_pet(void)
  1375. {
  1376. read_petdb();
  1377. add_timer_func_list(pet_timer,"pet_timer");
  1378. add_timer_func_list(pet_hungry,"pet_hungry");
  1379. add_timer_func_list(pet_ai_hard,"pet_ai_hard");
  1380. add_timer_func_list(pet_skill_bonus_timer,"pet_skill_bonus_timer"); // [Valaris]
  1381. add_timer_func_list(pet_skill_bonus_duration,"pet_skill_bonus_duration"); // [Valaris]
  1382. add_timer_func_list(pet_recovery_timer,"pet_recovery_timer"); // [Valaris]
  1383. add_timer_func_list(pet_mag_timer,"pet_mag_timer"); // [Valaris]
  1384. add_timer_func_list(pet_heal_timer,"pet_heal_timer"); // [Valaris]
  1385. add_timer_func_list(pet_skillattack_timer,"pet_skillattack_timer"); // [Valaris]
  1386. add_timer_interval(gettick()+MIN_PETTHINKTIME,pet_ai_hard,0,0,MIN_PETTHINKTIME);
  1387. return 0;
  1388. }