# Pet AI

* The pet AI script must exist in the project folder/ServerScripts with a file name like Hello.lua to be read, just like the server script.

| script            | explanation                                                  |
| ----------------- | ------------------------------------------------------------ |
| **pet**           | Indicates the unit (ScriptUnit) of the corresponding pet.    |
| **ai**            | Indicates the action (ScriptPetUnitAI) that AI will execute. |
| **event**         | Check the logic to be executed.                              |
| **AI\_INIT(-1)**  | Runs when registering a pet AI for the first time            |
| **AI\_UPDATE(0)** | Keep running every 2 seconds                                 |

**예제) 서버 스크립트 - 펫 AI 설정**

{% code overflow="wrap" lineNumbers="true" %}

```lua
Server.SetPetAI(
        21, --Character id to be your pet
        function(pet,ai,event)

                -- Runs when first pet AI is registered
                if(event == AI_INIT) then
                        ai.customData.timer = 1;
                end

                --Run every 2 seconds
                if(event == AI_UPDATE) then
                        --When the distance is 200, it follows the owner.
                        --Teleports to the owner's location when the distance is 400
                        --ai.SetFollowMaster(true,200,400)

                        --If there is no target, it follows the owner.
                        if(ai.GetTargetUnit() == nil) then
                                ai.SetFollowMaster(true,200,400)
                        end

                        --basic 100, 200
                        --ai.SetFollowMaster(true)

                        --Target the nearest enemy unit.
                        ai.SetNearTarget(2,200) 

                        --Use skill when pet target exists
                        --Basically, the skill fires at the target.
                        if(ai.GetTargetUnit() ~=nil) then
                                --Once the target is set, stop following them.
                                ai.SetFollowMaster(false)
                                ai.StopMove()

                                -- Once the target is set, follow the target and attack.
                                ai.MoveToPosition(ai.GetTargetUnit().x,ai.GetTargetUnit().y)
                                -- Add a buff to the owner
                                ai.AddMasterBuff(15)
                                -- Fire at the target
                                ai.UseSkill(23) 
                                -- Fires at the target from the pet's owner's position
                                ai.UseSkill(
                                22
                                ,nil
                                ,Point(ai.GetMasterUnit().x,ai.GetMasterUnit().y))
                        end

                        --Timer using custom data to acquire dropped items around you once every 6 seconds                        
                        ai.customData.timer = ai.customData.timer + 1;
                        if(ai.customData.timer == 3) then
                                -- Obtain drop items within a 100 radius
                                ai.AcquireNearDropItem(100)
                                ai.customData.timer = 0;
                        end
                end

        end
)
```

{% endcode %}
