몬스터 AI 스크립팅

  • 몬스터 AI 스크립트는 프로젝트 폴더/ServerScripts에 위치한 Hello.lua와 같은 파일 이름으로 저장되어야 읽힙니다.

스크립트
설명

enemy

몬스터의 ScriptUnit을 나타냅니다.

ai

ScriptEnemyUnitAI에서 실행할 AI 행동을 나타냅니다.

event

실행될 로직을 확인합니다.

AI_INIT(-1)

몬스터 AI 등록 시 최초 실행

AI_UPDATE(0)

2초마다 계속 실행

AI_ATTACKED(1)

공격 시 한 번 실행

AI_DEAD(2)

사망 시 한 번 실행

data

공격 시에만 사용 가능하며 (event == 1)일 때 damage, 스킬 ID, 크리티컬 여부(true, false) 정보를 확인할 수 있습니다.

예제) 서버 스크립트 - 몬스터의 AI 설정

 Server.SetMonsterAI(
        22, --AI를 적용할 몬스터의 인덱스
        function(enemy,ai,event,data)
        -- 최초 몬스터 AI 등록 시 실행
        if(event == AI_INIT) then
                 ai.customData.test = 1;
        end
        if (event == AI_UPDATE) then
                --현재 필드의 유닛들 중에서 조건에 해당하는 유닛 반환
                --예) 유닛 hp가 100보다 적은 적이 있을 시 가져온다
                --ai.SetTargetUnit(
                --        enemy.field.FindUnit(enemy.x, enemy.y, 200,
                --       function(u)
                --                return u.hp <= 100
                --        end
                --, 0,enemy))
                
                --현재 필드의 유닛들 중에서 가장 작은 조건에 해당하는 유닛 반환
                --예) 플레이어 유닛 중에서 hp가 가장 적은 적을 가져온다
                --ai.SetTargetUnit(
                --        enemy.field.FindMinimumUnit(enemy.x, enemy.y, 200,
                --        function(u)
                --                return u.hp
                --        end
                --, 0,enemy))

                --현재 필드의 유닛들 중에서 가장 큰 조건에 해당하는 유닛 반환
                --예) 플레이어 유닛 중에서 hp가 가장 많은 적을 가져온다
                --ai.SetTargetUnit(
               --        enemy.field.FindMaximumUnit(enemy.x, enemy.y, 200,
                --        function(u)
                --               return u.hp
                --        end
                --, 0,enemy))
                
               --타깃이 정해지지 않았을 경우 가장 가까운 플레이어를 타깃으로 지정
                if( ai.GetTargetUnit() == nil) then
                        ai.SetNearTarget(0,200)
                end
                
                --맵에 플레이어가 하나도 없을 경우 null로 세팅
                if( enemy.field.playerCount <=0) then
                        ai.SetTargetUnit(nil)
                        
                --타깃이었던 플레이어가 맵을 나갈 경우 타깃 다시 지정
                elseif(enemy.field.GetUnit(ai.GetTargetUnitID()) == nil) then
                        ai.SetNearTarget(0,200)
                end
                
                --타깃이 있을 경우 타깃 방향으로 공격
                ai.UseSkill(22);

                --왼쪽 공격
                ai.UseSkill(22,Point(-1,0))

                --오른쪽 공격
                ai.UseSkill(22,Point(1,0))

                --위쪽 공격
                ai.UseSkill(22,Point(0,1))

                --아래쪽 공격
                ai.UseSkill(22,Point(0,-1))

                --해당 위치로 공격
                ai.UseSkillToPosition(24,Point(150,-150))
                
                --타겟이 없을 경우 예외 처리
                if(ai.GetTargetUnit() == nil) then
                        return
                end
                
                --타깃의 hp에 따른 추적 활성화/비활성화
                if(ai.GetTargetUnit().hp <=150) then
                        ai.SetFollowTarget(false)
                else
                        ai.SetFollowTarget(true)
               end
                
        end

       if (event == AI_ATTACKED) then
                enemy.Say('공격당했다. \n대미지: ' .. data.damage 
                .. '\n스킬ID: '..data.skillDataID
                .. '\n크리티컬여부: '..(data.critical and 'true' or 'false'))
                
                --공격한 유닛이 없을 경우 예외 처리
                if(ai.GetAttackedUnit() == nil) then
                        return
               end
                 --공격한 적의 hp를 확인하여 100보다 적을 경우 타깃 변경
                if(ai.GetAttackedUnit().hp <= 100) then
                        ai.SetTargetUnit(ai.GetAttackedUnit()) 
                end

        end

        if (event == AI_DEAD) then
                Server.SendSay('사망')
                --사망 후 특정 위치에서 부활
                enemy.RespawnAt(150,-150)
        end

end)

예제) 서버 스크립트 - 몬스터 AI 선공격 몬스터

Last updated