root/src/map/pet.c @ 1

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