AI和行为函数
AttackStart
让生物开始攻击指定目标
creature:AttackStart(target)
-- 让NPC攻击最近的敌对玩家
local nearestPlayer = creature:GetNearestPlayer(30)
if nearestPlayer and nearestPlayer:IsHostileTo(creature) then
creature:AttackStart(nearestPlayer)
end
MoveTo
让生物移动到指定坐标
creature:MoveTo(id, x, y, z)
-- 让NPC移动到指定位置
creature:MoveTo(1, -8949.95, -132.493, 83.5312)
对话和交互函数
SendUnitSay
让生物说话(所有附近玩家都能看到)
creature:SendUnitSay(text, language)
-- NPC说话
creature:SendUnitSay("欢迎来到我的商店!", 0)
-- 定时说话
CreateLuaEvent(function()
if creature:IsInWorld() then
creature:SendUnitSay("我这里有最好的装备!", 0)
end
end, 30000, 0) -- 每30秒说一次
SendUnitYell
让生物大喊(更大范围的玩家能听到)
creature:SendUnitYell(text, language)
-- NPC大喊
creature:SendUnitYell("有敌人入侵!所有人准备战斗!", 0)
战斗和状态函数
IsInCombat
检查生物是否处于战斗状态
creature:IsInCombat()
-- 检查战斗状态并执行相应逻辑
if creature:IsInCombat() then
creature:SendUnitSay("我正在战斗中!", 0)
else
creature:SendUnitSay("现在很安全。", 0)
end
GetVictim
获取生物当前的攻击目标
creature:GetVictim()
-- 获取攻击目标并发送消息
local target = creature:GetVictim()
if target and target:IsPlayer() then
local player = target:ToPlayer()
creature:SendUnitSay("我要击败你," .. player:GetName() .. "!", 0)
end
CastSpell
让生物释放指定法术
creature:CastSpell(target, spellId)
-- 释放治疗法术
local nearbyPlayer = creature:GetNearestPlayer(10)
if nearbyPlayer then
creature:CastSpell(nearbyPlayer, 2061) -- 快速治疗
creature:SendUnitSay("让我来治疗你!", 0)
end
位置和移动函数
GetX / GetY / GetZ
获取生物的坐标位置
creature:GetX(), creature:GetY(), creature:GetZ()
-- 获取并显示生物位置
local x, y, z = creature:GetX(), creature:GetY(), creature:GetZ()
print(string.format("生物位置: X=%.2f, Y=%.2f, Z=%.2f", x, y, z))
Teleport
传送生物到指定位置
creature:Teleport(mapId, x, y, z, orientation)
-- 传送NPC到指定位置
creature:Teleport(0, -8949.95, -132.493, 83.5312, 0)
creature:SendUnitSay("我被传送了!", 0)
高级AI函数
SetNPCFlag
设置NPC的功能标志
creature:SetNPCFlag(flag)
-- 设置NPC为商人
creature:SetNPCFlag(128) -- UNIT_NPC_FLAG_VENDOR
-- 设置NPC为任务给予者
creature:SetNPCFlag(2) -- UNIT_NPC_FLAG_QUESTGIVER
-- 设置NPC为训练师
creature:SetNPCFlag(16) -- UNIT_NPC_FLAG_TRAINER
SetFaction
设置生物的阵营
creature:SetFaction(factionId)
-- 设置为友善阵营
creature:SetFaction(35) -- 友善
-- 设置为敌对阵营
creature:SetFaction(14) -- 敌对
-- 根据玩家阵营动态设置
local function SetCreatureFactionBasedOnPlayer(creature, player)
if player:GetTeam() == 0 then -- 联盟
creature:SetFaction(11) -- 联盟友善
else -- 部落
creature:SetFaction(85) -- 部落友善
end
end
SetLevel
设置生物的等级
creature:SetLevel(level)
-- 根据附近玩家等级调整NPC等级
local function AdjustCreatureLevel(creature)
local nearestPlayer = creature:GetNearestPlayer(50)
if nearestPlayer then
local playerLevel = nearestPlayer:GetLevel()
local newLevel = math.max(1, playerLevel - 2) -- 比玩家低2级
creature:SetLevel(newLevel)
creature:SendUnitSay("我的实力随着你的成长而调整!", 0)
end
end
生物状态函数
GetHealth / GetMaxHealth
获取生物的当前生命值和最大生命值
creature:GetHealth(), creature:GetMaxHealth()
-- 检查生物血量并执行相应逻辑
local currentHP = creature:GetHealth()
local maxHP = creature:GetMaxHealth()
local hpPercent = (currentHP / maxHP) * 100
if hpPercent <= 20 then
creature:SendUnitYell("我快要死了!救救我!", 0)
-- 召唤援军或使用特殊技能
creature:CastSpell(creature, 12345) -- 治疗法术
elseif hpPercent <= 50 then
creature:SendUnitSay("我需要帮助!", 0)
end
SetMaxHealth / SetHealth
设置生物的最大生命值和当前生命值
creature:SetMaxHealth(health), creature:SetHealth(health)
-- 根据玩家数量调整BOSS血量
local function AdjustBossHealth(creature)
local playersNearby = 0
local nearbyPlayers = creature:GetPlayersInRange(100)
if nearbyPlayers then
playersNearby = #nearbyPlayers
end
-- 基础血量 + 每个玩家增加50%
local baseHealth = 100000
local newMaxHealth = baseHealth * (1 + playersNearby * 0.5)
creature:SetMaxHealth(newMaxHealth)
creature:SetHealth(newMaxHealth)
creature:SendUnitYell("感受我的力量吧!我的实力随着挑战者增加而增强!", 0)
end
IsDead / IsAlive
检查生物是否死亡或存活
creature:IsDead(), creature:IsAlive()
-- 检查生物状态并执行复活逻辑
if creature:IsDead() then
-- 延迟复活
CreateLuaEvent(function()
if creature:IsDead() then
creature:Respawn()
creature:SendUnitSay("我又回来了!", 0)
end
end, 30000, 1) -- 30秒后复活
else
print("生物 " .. creature:GetName() .. " 仍然存活")
end
寻路和移动函数
GetNearestPlayer
获取距离生物最近的玩家
creature:GetNearestPlayer(range)
-- 寻找附近玩家并互动
local nearestPlayer = creature:GetNearestPlayer(30)
if nearestPlayer then
local distance = creature:GetDistance(nearestPlayer)
creature:SendUnitSay("我发现了一个玩家,距离: " .. math.floor(distance) .. " 码", 0)
-- 根据距离执行不同行为
if distance < 5 then
creature:SendUnitSay("你好," .. nearestPlayer:GetName() .. "!", 0)
elseif distance < 15 then
creature:MoveTo(1, nearestPlayer:GetX(), nearestPlayer:GetY(), nearestPlayer:GetZ())
end
end
GetPlayersInRange
获取指定范围内的所有玩家
creature:GetPlayersInRange(range)
-- 获取范围内所有玩家并群体互动
local playersInRange = creature:GetPlayersInRange(20)
if playersInRange and #playersInRange > 0 then
creature:SendUnitYell("我感受到了 " .. #playersInRange .. " 个勇士的存在!", 0)
-- 对每个玩家执行操作
for _, player in pairs(playersInRange) do
if player:GetLevel() < 10 then
creature:SendUnitSay("年轻的冒险者 " .. player:GetName() .. ",小心前行!", 0)
end
end
-- 如果玩家太多,召唤援军
if #playersInRange >= 5 then
creature:SendUnitYell("入侵者太多了!召唤援军!", 0)
-- 在附近生成援军
local x, y, z = creature:GetX(), creature:GetY(), creature:GetZ()
SpawnCreature(1234, x + 5, y, z, 0, creature:GetMapId())
end
end
GetDistance
计算生物与目标之间的距离
creature:GetDistance(target)
-- 根据距离调整AI行为
local target = creature:GetVictim()
if target then
local distance = creature:GetDistance(target)
if distance > 30 then
-- 距离太远,停止追击
creature:SendUnitSay("你跑得太远了,我不追了!", 0)
creature:CombatStop()
elseif distance > 10 and distance <= 30 then
-- 中距离,使用远程攻击
creature:CastSpell(target, 9613) -- 暗影箭
elseif distance <= 5 then
-- 近距离,使用近战技能
creature:CastSpell(target, 11976) -- 重击
end
end
AI行为控制函数
CombatStop
让生物停止战斗
creature:CombatStop()
-- 条件性停止战斗
local target = creature:GetVictim()
if target and target:IsPlayer() then
local player = target:ToPlayer()
-- 如果玩家等级太低,停止攻击
if player:GetLevel() < 10 then
creature:CombatStop()
creature:SendUnitSay("你太弱了,我不欺负新手", 0)
end
-- 如果玩家生命值过低,给予怜悯
if player:GetHealthPct() < 10 then
creature:CombatStop()
creature:SendUnitSay("我饶你一命,快逃吧!", 0)
creature:CastSpell(player, 2061) -- 给玩家治疗
end
end
SetRooted
设置生物是否被定身
creature:SetRooted(rooted)
-- 动态控制生物移动
local healthPct = (creature:GetHealth() / creature:GetMaxHealth()) * 100
if healthPct < 20 then
-- 低血量时定身并召唤援军
creature:SetRooted(true)
creature:SendUnitYell("我不能再移动了!但我的盟友会为我报仇!", 0)
-- 3秒后解除定身
CreateLuaEvent(function()
if creature:IsAlive() then
creature:SetRooted(false)
creature:SendUnitSay("我又可以移动了!", 0)
end
end, 3000, 1)
else
creature:SetRooted(false)
end
SetSpeed
设置生物的移动速度
creature:SetSpeed(speedType, speed)
-- 根据战斗状态调整速度
if creature:IsInCombat() then
-- 战斗中提高移动速度
creature:SetSpeed(1, 1.5) -- MOVE_RUN = 1, 速度1.5倍
creature:SendUnitSay("战斗让我更加敏捷!", 0)
else
-- 非战斗状态恢复正常速度
creature:SetSpeed(1, 1.0)
end
-- 根据生命值调整速度
local healthPct = (creature:GetHealth() / creature:GetMaxHealth()) * 100
if healthPct < 30 then
-- 低血量时减速
creature:SetSpeed(1, 0.5)
creature:SendUnitSay("我受伤了,移动变慢了...", 0)
end
特殊效果函数
PlayEmote
让生物播放表情动作
creature:PlayEmote(emoteId)
-- 根据情况播放不同表情
local nearestPlayer = creature:GetNearestPlayer(10)
if nearestPlayer then
local playerLevel = nearestPlayer:GetLevel()
local creatureLevel = creature:GetLevel()
if playerLevel > creatureLevel + 10 then
creature:PlayEmote(18) -- EMOTE_COWER 畏缩
creature:SendUnitSay("强大的勇士,请饶恕我!", 0)
elseif playerLevel < creatureLevel - 10 then
creature:PlayEmote(5) -- EMOTE_LAUGH 大笑
creature:SendUnitSay("哈哈,又来了一个弱者!", 0)
else
creature:PlayEmote(25) -- EMOTE_POINT 指向
creature:SendUnitSay("你看起来是个值得尊敬的对手", 0)
end
end
SetVisibility
设置生物的可见性
creature:SetVisibility(visibility)
-- 实现隐身和现身效果
local playersNearby = creature:GetPlayersInRange(15)
if playersNearby and #playersNearby > 0 then
-- 有玩家接近时现身
creature:SetVisibility(0) -- VISIBILITY_ON
creature:SendUnitSay("我现身了!", 0)
creature:PlayEmote(15) -- EMOTE_ROAR 咆哮
else
-- 没有玩家时隐身
creature:SetVisibility(1) -- VISIBILITY_OFF
end
-- 定时切换可见性(闪烁效果)
CreateLuaEvent(function()
if creature:IsAlive() then
local currentVisibility = creature:GetVisibility()
creature:SetVisibility(currentVisibility == 0 and 1 or 0)
end
end, 2000, 5) -- 每2秒切换一次,共5次
GetDistance
获取生物与目标之间的距离
creature:GetDistance(target)
-- 检查与玩家的距离并执行相应行为
local nearestPlayer = creature:GetNearestPlayer(50)
if nearestPlayer then
local distance = creature:GetDistance(nearestPlayer)
if distance < 5 then
creature:SendUnitSay("你离我太近了!", 0)
elseif distance < 15 then
creature:SendUnitSay("欢迎来到这里!", 0)
elseif distance < 30 then
creature:SendUnitSay("我看到你了,勇敢的冒险者!", 0)
end
end
Respawn
立即重生生物
creature:Respawn()
-- 特殊事件重生BOSS
local function RespawnEventBoss()
local boss = GetCreatureByEntry(12345) -- 获取BOSS
if boss and boss:IsDead() then
boss:Respawn()
boss:SendUnitYell("我又回来了!准备迎接更强大的挑战!", 0)
SendWorldMessage("事件BOSS已重新出现!")
end
end
装备和掉落函数
SetEquipment
设置生物的装备
creature:SetEquipment(slot, itemId)
-- 动态设置NPC装备
local function SetNPCEquipment(creature, playerLevel)
if playerLevel < 20 then
creature:SetEquipment(0, 2023) -- 低级武器
creature:SetEquipment(1, 143) -- 低级盾牌
elseif playerLevel < 40 then
creature:SetEquipment(0, 6975) -- 中级武器
creature:SetEquipment(1, 2129) -- 中级盾牌
else
creature:SetEquipment(0, 12784) -- 高级武器
creature:SetEquipment(1, 18168) -- 高级盾牌
end
creature:SendUnitSay("我已经准备好了!", 0)
end
AddLoot
为生物添加掉落物品
creature:AddLoot(itemId, count, chance)
-- 动态添加掉落物品
local function AddSpecialLoot(creature, player)
local playerLevel = player:GetLevel()
-- 根据玩家等级添加不同掉落
if playerLevel >= 60 then
creature:AddLoot(12345, 1, 100) -- 高级物品,100%掉落
creature:AddLoot(12346, 1, 50) -- 稀有物品,50%掉落
elseif playerLevel >= 30 then
creature:AddLoot(6789, 1, 100) -- 中级物品
else
creature:AddLoot(1234, 1, 100) -- 低级物品
end
-- 特殊节日掉落
local currentTime = GetGameTime()
if IsHolidayActive(HOLIDAY_CHRISTMAS) then
creature:AddLoot(99999, 1, 25) -- 圣诞特殊物品
end
end
SetLootMode
设置生物的掉落模式
creature:SetLootMode(mode)
-- 设置特殊掉落模式
local function SetSpecialLootMode(creature, eventType)
if eventType == "double_drop" then
creature:SetLootMode(2) -- 双倍掉落模式
creature:SendUnitSay("今天是你的幸运日!", 0)
elseif eventType == "rare_only" then
creature:SetLootMode(3) -- 只掉落稀有物品
creature:SendUnitSay("只有最珍贵的宝物等着你!", 0)
else
creature:SetLootMode(1) -- 普通掉落模式
end
end
AI行为控制函数
SetAI
设置生物的AI类型
creature:SetAI(aiName)
-- 动态切换AI行为
local function SwitchCreatureAI(creature, situation)
if situation == "combat" then
creature:SetAI("AggressiveAI")
creature:SendUnitSay("战斗模式启动!", 0)
elseif situation == "patrol" then
creature:SetAI("PatrolAI")
creature:SendUnitSay("开始巡逻...", 0)
elseif situation == "guard" then
creature:SetAI("GuardAI")
creature:SendUnitSay("我将守护这里!", 0)
else
creature:SetAI("PassiveAI")
creature:SendUnitSay("进入待机状态", 0)
end
end
SetReactState
设置生物的反应状态
creature:SetReactState(state)
-- 控制生物反应状态
local function ControlCreatureReaction(creature, mode)
if mode == "aggressive" then
creature:SetReactState(2) -- 主动攻击
creature:SendUnitSay("我会主动寻找敌人!", 0)
elseif mode == "defensive" then
creature:SetReactState(1) -- 防御状态
creature:SendUnitSay("我只会自卫!", 0)
else
creature:SetReactState(0) -- 被动状态
creature:SendUnitSay("我不会攻击任何人", 0)
end
end
SetRooted
设置生物是否被定身
creature:SetRooted(rooted)
-- 特殊技能:定身效果
local function ApplyRootEffect(creature, duration)
creature:SetRooted(true)
creature:SendUnitSay("我被定住了!", 0)
-- 设置定时器解除定身
CreateLuaEvent(function()
if creature:IsInWorld() then
creature:SetRooted(false)
creature:SendUnitSay("我又可以移动了!", 0)
end
end, duration * 1000, 1)
end
-- 使用示例
ApplyRootEffect(creature, 10) -- 定身10秒
SetConfused
设置生物是否混乱
creature:SetConfused(confused)
-- 混乱效果控制
local function ApplyConfusionEffect(creature, duration)
creature:SetConfused(true)
creature:SendUnitSay("我...我在哪里?", 0)
-- 混乱期间随机移动
local confusionTimer = CreateLuaEvent(function()
if creature:IsInWorld() and creature:IsAlive() then
local x, y, z = creature:GetX(), creature:GetY(), creature:GetZ()
local randomX = x + math.random(-10, 10)
local randomY = y + math.random(-10, 10)
creature:MoveTo(1, randomX, randomY, z)
end
end, 2000, duration / 2) -- 每2秒移动一次
-- 解除混乱
CreateLuaEvent(function()
if creature:IsInWorld() then
creature:SetConfused(false)
creature:SendUnitSay("我清醒了!", 0)
RemoveLuaEvent(confusionTimer)
end
end, duration * 1000, 1)
end