Monster AI Scripting

A Monster AI script can execute as a server script when in the project folder/ServerScripts with the file name Hello.lua.

enemy: Represents the unit (ScriptUnit) of the monster.
ai: Represents the action(ScriptEnemyUnitAI) to be performed.
event: Identifies the logic to be executed.
AI_INIT(-1) : Runs when monster AI submitted for the first time, AI_UPDATE (0): Continues every 2 seconds, AI_ATTACKED (1): Runs once on attack, AI_DEAD (2): Runs once when dead
data: Only available on attack (event == 1)
Information such as damage, skill ID, and critical (true/false) can be can be checked on attack.

Example) Server script - Setting Monster AI

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 Server.SetMonsterAI( 22, --the index of the monster function(enemy,ai,event,data) if (event == 0) then --returns the unit in the field which fullfills the condition of AI --E.g, returns units 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)

Example: Server Script - Monster AI for a first-attack monster

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 function firstAttack (enemy, ai, event, data)     if (event == 0) then - an event that executes every 2 seconds              - null if no player is on the map         if enemy.field.playerCount <= 0 then             ai.SetTargetUnit (nil)                      - If there is no target, or if the existing target unit exits the map, or if the x or y value difference exceeds 300         - initializes target to nil and sets target within range of 200         elseif (ai.GetTargetUnit () == nil)                or (enemy.field.GetUnit (ai.GetTargetUnitID ()) == nil)                or (math.abs (enemy.x-enemy.field.GetUnit (ai.GetTargetUnitID ()). x) & gt; = 300)                or (math.abs (enemy.y-enemy.field.GetUnit (ai.GetTargetUnitID ()). y) & gt; = 300) then                          if ai.GetTargetUnit () ~ = nil then                 enemy.say ('Target has disappeared ..')             end                          ai.SetFollowTarget (false) - disable tracing if target disappears             ai.SetTargetUnit (nil)             ai.SetNearTarget (0,200)             - If you have found the target after looking around (not nil), enables tracing (true), and prints the message             if ai.GetTargetUnit () ~ = nil then                 ai.SetFollowTarget (true)                 enemy.say ('Target found! \ n Tracking started!')             end         end                  - If there is a target, fires 10 skills toward the target direction.         if ai.GetTargetUnit () ~ = nil then             ai.UseSkill (10)         end                  - Handles exception when there is no target         if ai.GetTargetUnit () == nil then             return         end              end          if (event == 1) then - the event that is fired each time the monster is attacked         - Exception handling when there is no attacked unit         if ai.GetAttackedUnit () == nil then             return         else             - When the existing target unit and the attacking unit are not the same, the attacking unit is targeted or changed to and then tracked             if ai.GetTargetUnit () ~ = ai.GetAttackedUnit () then                 ai.SetTargetUnit (ai.GetAttackedUnit ())                 ai.SetFollowTarget (true)                 enemy.say ('Tracking new attackers ..')             end                          - When a monster is attacked, it has 5% chance of attacking with a 10% probability, and sends message to the entire server.             if math.random (0,99) <= 9 then                 ai.UseSkill (5)                 Server.SendCenterLabel ('<color = # FF0000> growl !! </ color> \ nYour stupid boss is crying!')             end         end     end          end Server.SetMonsterAI (1, firstAttack) - apply firstAttack to monster number 1

 

SUPERCAT Inc. All Rights Reserved.