2015年4月—2024年4月,论坛已建立9周年,欢迎加入QQ群讨论:419848937

簡單怪物的 AI 編寫

主要是游戏中一些经典任务、游戏资料的科普.
回复
头像
hagcse
Mr.GL
Mr.GL
帖子: 151
注册时间: 2018-02-25 15:50

簡單怪物的 AI 編寫

帖子 hagcse » 2018-03-10 15:25

其實l2j 已經一早把 怪物的AI 這部分開放給大家去自由 編寫.
不過奇怪的是, 好像沒有什麼人在這方面研究, 其實自己編寫滿有趣的,
以下是本人測試用的AI,

目的是要控制 "巴列斯" 的技能
當巴列斯的 hp 降到一定情度, 會發動不同技能的 ai .

----------------------------------------------------------------------------------------------
import sys
import time
from net.sf.l2j.gameserver.ai import CtrlIntention
from net.sf.l2j.gameserver.model import L2Character
from net.sf.l2j.gameserver.model.quest import State
from net.sf.l2j.gameserver.datatables import SkillTable
from net.sf.l2j.gameserver.model.quest import QuestState
from net.sf.l2j.gameserver.model.actor.instance import L2NpcInstance
from net.sf.l2j.gameserver.model.quest.jython import QuestJython as JQuest
from net.sf.l2j.util import Rnd

class beres(JQuest) :

# init function. Add in here variables that you'd like to be inherited by subclasses (if any)
def __init__(self,id,name,descr):
# finally, don't forget to call the parent constructor to prepare the event triggering
# mechanisms etc.
JQuest.__init__(self,id,name,descr)
beres = 29118
def onAttack(self,npc,player,damage,isPet) :
npcId = npc.getNpcId()
objId = npc.getObjectId()
if npcId == 29118 :
if npc.getCurrentHp() <= npc.getMaxHp() * 0.2 :
npc.doCast(SkillTable.getInstance().getInfo(5531,1))
if npc.getCurrentHp() <= npc.getMaxHp() * 0.4:
npc.doCast(SkillTable.getInstance().getInfo(5532,1))
npc.doCast(SkillTable.getInstance().getInfo(5533,1))
if npc.getCurrentHp() <= npc.getMaxHp() * 0.6 :
npc.doCast(SkillTable.getInstance().getInfo(5495,1))
if npc.getCurrentHp() <= npc.getMaxHp() * 0.8 :
npc.doCast(SkillTable.getInstance().getInfo(5497,1))
npc.doCast(SkillTable.getInstance().getInfo(5498,1))
if npc.getCurrentHp() <= npc.getMaxHp() :
npc.doCast(SkillTable.getInstance().getInfo(5496,1))
npc.doCast(SkillTable.getInstance().getInfo(5499,1))
return

# now call the constructor (starts up the ai)
QUEST = beres(-1,"beres","ai")
QUEST.addKillId(29118)
QUEST.addAttackId(29118)
----------------------------------------------------------------------------------------

當然你可能會說 在 npcskill 加不就簡單得多, 不過 npcskill 沒有發動條件控制.
用 script 寫好玩多了, 大家一起來研究吧
主要只是動用 onAttack 這個條件

def onAttack(self,npc,player,damage,isPet) :
npcId = npc.getNpcId()
objId = npc.getObjectId()
if npcId == 29118 : ( 如果被攻擊的怪物 id = 29118 (巴列斯))
if npc.getCurrentHp() <= npc.getMaxHp() * 0.2 : (如果巴列斯現時的hp少過最大hp的20%)
npc.doCast(SkillTable.getInstance().getInfo(5531,1)) (發動 5531 lv 1 這個技能)
if npc.getCurrentHp() <= npc.getMaxHp() * 0.4:
npc.doCast(SkillTable.getInstance().getInfo(5532,1))
npc.doCast(SkillTable.getInstance().getInfo(5533,1))
if npc.getCurrentHp() <= npc.getMaxHp() * 0.6 :
npc.doCast(SkillTable.getInstance().getInfo(5495,1))
if npc.getCurrentHp() <= npc.getMaxHp() * 0.8 :
npc.doCast(SkillTable.getInstance().getInfo(5497,1))
npc.doCast(SkillTable.getInstance().getInfo(5498,1))
if npc.getCurrentHp() <= npc.getMaxHp() :
npc.doCast(SkillTable.getInstance().getInfo(5496,1))
npc.doCast(SkillTable.getInstance().getInfo(5499,1))
return
图片

回复