Monster AI
Monster AI scripts must be saved in the project folder/ServerScripts with a file name like Hello.lua to be read.
script
explanation
enemy
Represents the ScriptUnit of a monster.
ai
Represents an AI behavior to be executed in ScriptEnemyUnitAI.
event
Check the logic to be executed.
AI_INIT(-1)
First run when registering monster AI
AI_UPDATE(0)
Keep running every 2 seconds
AI_ATTACKED(1)
Runs once per attack
AI_DEAD(2)
Runs once on death
data
It can only be used when attacking, and when (event == 1), you can check damage, skill ID, and whether it is critical (true, false) information.
Example) Server script - Setting monster AI
Server.SetMonsterAI(
22, --Index of monsters to which AI will be applied
function(enemy,ai,event,data)
-- Runs when registering the first monster AI
if(event == AI_INIT) then
ai.customData.test = 1;
end
if (event == AI_UPDATE) then
--Returns the units that meet the condition among the units in the current field.
--Example) Bring when there is an enemy with unit HP less than 100.
--ai.SetTargetUnit(
-- enemy.field.FindUnit(enemy.x, enemy.y, 200,
-- function(u)
-- return u.hp <= 100
-- end
--, 0,enemy))
--Returns the unit that satisfies the smallest condition among the units in the current field.
--Example) Bring the enemy with the lowest HP among the player units.
--ai.SetTargetUnit(
-- enemy.field.FindMinimumUnit(enemy.x, enemy.y, 200,
-- function(u)
-- return u.hp
-- end
--, 0,enemy))
--Returns the unit that meets the largest condition among the units in the current field.
--Example) Bring the enemy with the most HP among the player units.
--ai.SetTargetUnit(
-- enemy.field.FindMaximumUnit(enemy.x, enemy.y, 200,
-- function(u)
-- return u.hp
-- end
--, 0,enemy))
--If no target is set, the closest player is targeted.
if( ai.GetTargetUnit() == nil) then
ai.SetNearTarget(0,200)
end
--Set to null if there are no players on the map.
if( enemy.field.playerCount <=0) then
ai.SetTargetUnit(nil)
--If the targeted player leaves the map, retarget.
elseif(enemy.field.GetUnit(ai.GetTargetUnitID()) == nil) then
ai.SetNearTarget(0,200)
end
--If there is a target, attack in the direction of the target.
ai.UseSkill(22);
--Left attack
ai.UseSkill(22,Point(-1,0))
--Right attack
ai.UseSkill(22,Point(1,0))
--Attack from above
ai.UseSkill(22,Point(0,1))
--Downward attack
ai.UseSkill(22,Point(0,-1))
--Attack the location
ai.UseSkillToPosition(24,Point(150,-150))
--Exception handling when target is not found
if(ai.GetTargetUnit() == nil) then
return
end
--Enable/disable tracking based on target's HP
if(ai.GetTargetUnit().hp <=150) then
ai.SetFollowTarget(false)
else
ai.SetFollowTarget(true)
end
end
if (event == AI_ATTACKED) then
enemy.Say('I was attacked. \nDamage: ' .. data.damage
.. '\nSkillID: '..data.skillDataID
.. '\nWhether it is critical or not: '..(data.critical and 'true' or 'false'))
--Exception handling when there is no unit attacking
if(ai.GetAttackedUnit() == nil) then
return
end
--Check the HP of the attacked enemy and change target if it is less than 100.
if(ai.GetAttackedUnit().hp <= 100) then
ai.SetTargetUnit(ai.GetAttackedUnit())
end
end
if (event == AI_DEAD) then
Server.SendSay('dying')
--Resurrect at a specific location after death
enemy.RespawnAt(150,-150)
end
end)Example) Server script - Monster AI preemptive attack monster
Last updated