User Tools

Site Tools


ai

Monster AI Scripts


The AIscript must be present in the project folder/ServerScripts with the same file name as Hello.lua.

enemy : Represents target monster.
ai : Represents the actions that ai will execute.
event : Check the logic which should be executed.
0 : Keep executed every 2 seconds, 1 : executed once when attacked, 2 : executed once when died
data : can only be used when monster is attacked (event == 1)
Including attacked damage, skill ID, and if attack was critical hit or not (true,false)

Example) Server script - Setting Monster AI

Server.SetMonsterAI(
       22, --the index of moster 
       function(enemy,ai,event,data)
       if (event == 0) then
               --return the unit in the field which fullfills the condition of AI
               --ex) return unit which have hp lower than 100
               --ai.SetTargetUnit(
               --        enemy.field.FindUnit(enemy.x, enemy.y, 200,
               --       function(u)
               --                return u.hp <= 100
               --        end
               --, 0,enemy))

               --return unit in the field with minimum value of type passed
               --ex) return unit which have minimum hp value
               --ai.SetTargetUnit(
               --        enemy.field.FindMinimumUnit(enemy.x, enemy.y, 200,
               --        function(u)
               --                return u.hp
               --        end
               --, 0,enemy))

               --return unit in the field with maximum value of type passed
               --ex) return unit which have maximum hp value
               --ai.SetTargetUnit(
              --        enemy.field.FindMaximumUnit(enemy.x, enemy.y, 200,
               --        function(u)
               --               return u.hp
               --        end
               --, 0,enemy))
               --Decide a target as the closest player when there is no target chosen
               if( ai.GetTargetUnit() == nil) then
                       ai.SetNearTarget(0,200)
               end
               --Set the target as null if there are no players on map
               if( enemy.field.playerCount <=0) then
                       ai.SetTargetUnit(nil)
               --Decide new target when the target player leaves the map
               elseif(enemy.field.GetUnit(ai.GetTargetUnitID()) == nil) then
                       ai.SetNearTarget(0,200)
               end
               --Attack to the direction if there is target
               ai.UseSkill(22);

               --Attack Left side
               ai.UseSkill(22,Point(-1,0))

               --Attack Right side
               ai.UseSkill(22,Point(1,0))

               --Attack upwards
               ai.UseSkill(22,Point(0,1))

               --Attack downwards
               ai.UseSkill(22,Point(0,-1))

               --Attack to the selected point
               ai.UseSkillToPosition(24,Point(150,-150))

               --Handle exception if there is no target
               if(ai.GetTargetUnit() == nil) then
                       return
               end

               --Activate/Deactivate following the target by her HP
               if(ai.GetTargetUnit().hp <=150) then
                       ai.SetFollowTarget(false)
               else
                       ai.SetFollowTarget(true)
              end

       end

      if (event == 1) then
               enemy.Say('Attakced!. \nDamage : ' .. data.damage
               .. '\nSkillID: '..data.skillDataID
               .. '\nIf it was critical shot: '..(data.critical and 'true' or 'false'))

               --Handle exception when there is no attacked unit
               if(ai.GetAttackedUnit() == nil) then
                       return
              end
              --Check the attacker's HP and change the target to him if his HP is lower than 100 
               if(ai.GetAttackedUnit().hp <= 100) then
                       ai.SetTargetUnit(ai.GetAttackedUnit())
               end

       end

       if (event == 2) then
               Server.SendSay('Dead')
               --Respawn at the position after being killed
               enemy.RespawnAt(150,-150)
       end

end)
ai.txt · Last modified: 2019/02/26 11:56 by 리크