生物函数 (Creature)

基本信息函数

GetEntry
获取生物的模板ID(entry)
creature:GetEntry()
-- 获取生物ID并执行特定逻辑 local entry = creature:GetEntry() if entry == 1234 then -- 特定NPC creature:SendUnitSay("你好,勇敢的冒险者!", 0) end
GetName
获取生物的名称
creature:GetName()
-- 获取并显示生物名称 local name = creature:GetName() print("生物名称: " .. name)

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

生物状态和属性函数

GetLevel
获取生物的等级
creature:GetLevel()

参数

无参数

返回值

number - 生物等级

示例

-- 根据生物等级调整行为 local function AdjustCreatureBehavior(creature, player) local creatureLevel = creature:GetLevel() local playerLevel = player:GetLevel() if creatureLevel > playerLevel + 10 then creature:SendUnitSay("你还太弱了,快逃吧!", 0) creature:SetReactState(0) -- 设为被动 elseif creatureLevel < playerLevel - 10 then creature:SendUnitSay("强者,我向你致敬", 0) creature:SetFaction(35) -- 设为友善 else creature:SendUnitSay("来吧,让我们公平一战!", 0) end end
SetLevel
设置生物的等级
creature:SetLevel(level)

参数

level (number)
要设置的等级

返回值

无返回值

示例

-- 动态等级调整系统 local function DynamicLevelAdjust(creature, player) local playerLevel = player:GetLevel() local baseLevel = creature:GetLevel() -- 根据玩家等级调整生物等级 local newLevel = math.max(1, playerLevel - 2) -- 比玩家低2级 if newLevel ~= baseLevel then creature:SetLevel(newLevel) creature:SendUnitSay("我感受到了你的力量,让我变得更强!", 0) -- 同时调整生命值 local newMaxHealth = newLevel * 100 -- 简单的血量计算 creature:SetMaxHealth(newMaxHealth) creature:SetHealth(newMaxHealth) print("生物等级已调整为: " .. newLevel) end end
GetFaction
获取生物的阵营
creature:GetFaction()

参数

无参数

返回值

number - 阵营ID

示例

-- 阵营关系检查 local function CheckFactionRelation(creature, player) local creatureFaction = creature:GetFaction() local factionNames = { [35] = "友善", [14] = "敌对", [7] = "联盟", [67] = "部落" } local factionName = factionNames[creatureFaction] or "未知阵营" if creatureFaction == 35 then creature:SendUnitSay("我们是朋友", 0) elseif creatureFaction == 14 then creature:SendUnitSay("你是我的敌人!", 0) creature:AttackStart(player) else creature:SendUnitSay("我属于" .. factionName, 0) end end
SetFaction
设置生物的阵营
creature:SetFaction(factionId)

参数

factionId (number)
阵营ID

返回值

无返回值

示例

-- 动态阵营切换系统 local function DynamicFactionSwitch(creature, player) local playerRace = player:GetRace() local currentFaction = creature:GetFaction() -- 根据玩家种族调整阵营 if playerRace <= 3 or playerRace == 4 or playerRace == 7 then -- 联盟种族 if currentFaction ~= 7 then creature:SetFaction(7) -- 联盟友善 creature:SendUnitSay("为了联盟!", 0) end else -- 部落种族 if currentFaction ~= 67 then creature:SetFaction(67) -- 部落友善 creature:SendUnitSay("为了部落!", 0) end end end -- 时间阵营系统 local function TimeFactionSystem(creature) local gameTime = GetGameTime() local hour = math.floor(gameTime / 3600) % 24 if hour >= 6 and hour < 18 then -- 白天 creature:SetFaction(35) -- 友善 creature:SendUnitSay("白天我很友好", 0) else -- 夜晚 creature:SetFaction(14) -- 敌对 creature:SendUnitSay("夜晚我变得危险...", 0) end end

生物召唤和管理函数

SummonPlayer
召唤玩家到生物位置
creature:SummonPlayer(player)

参数

player (Player)
要召唤的玩家

返回值

无返回值

示例

-- BOSS召唤系统 local function BossSummonSystem(creature, targetPlayer) local healthPercent = (creature:GetHealth() / creature:GetMaxHealth()) * 100 if healthPercent < 25 then -- 血量低于25%时召唤玩家 creature:SendUnitYell("你无法逃脱!", 0) creature:SummonPlayer(targetPlayer) -- 召唤后给予debuff targetPlayer:AddAura(12345, 10000) -- 10秒虚弱效果 targetPlayer:SendBroadcastMessage("你被强制召唤了!") end end -- 团队召唤功能 local function GroupSummon(creature, leader) local group = leader:GetGroup() if not group then creature:SendUnitSay("你没有队伍可以召唤", 0) return end creature:SendUnitYell("召唤整个队伍!", 0) local members = group:GetMembers() for _, member in pairs(members) do if member ~= leader then creature:SummonPlayer(member) member:SendBroadcastMessage("你被队长召唤了!") end end end
SummonObject
在生物位置召唤游戏对象
creature:SummonObject(entry, duration)

参数

entry (number)
游戏对象模板ID
duration (number)
持续时间(毫秒)

返回值

GameObject - 召唤的游戏对象

示例

-- 动态环境召唤系统 local function DynamicEnvironmentSummon(creature) local entry = creature:GetEntry() if entry == 12345 then -- 火元素BOSS -- 召唤火焰陷阱 local trap = creature:SummonObject(123456, 30000) -- 30秒 if trap then creature:SendUnitYell("感受火焰的力量!", 0) end elseif entry == 12346 then -- 冰霜BOSS -- 召唤冰墙 local iceWall = creature:SummonObject(123457, 60000) -- 60秒 if iceWall then creature:SendUnitYell("冰霜将阻挡你的去路!", 0) end end end -- 宝藏召唤系统 local function TreasureSummon(creature, player) local playerLevel = player:GetLevel() local treasureEntry = 0 if playerLevel < 20 then treasureEntry = 100001 -- 低级宝箱 elseif playerLevel < 40 then treasureEntry = 100002 -- 中级宝箱 else treasureEntry = 100003 -- 高级宝箱 end local treasure = creature:SummonObject(treasureEntry, 300000) -- 5分钟 if treasure then creature:SendUnitSay("我为你准备了特殊的奖励!", 0) player:SendBroadcastMessage("发现了宝藏!快去开启它!") end end
DespawnOrUnsummon
移除生物或取消召唤
creature:DespawnOrUnsummon(delay)

参数

delay (number)
延迟时间(毫秒),可选

返回值

无返回值

示例

-- 智能消失系统 local function SmartDespawn(creature, reason) if reason == "timeout" then creature:SendUnitSay("我的时间到了...", 0) creature:DespawnOrUnsummon(3000) -- 3秒后消失 elseif reason == "mission_complete" then creature:SendUnitSay("任务完成,我该离开了", 0) creature:DespawnOrUnsummon(5000) -- 5秒后消失 elseif reason == "low_health" then creature:SendUnitSay("我必须撤退!", 0) creature:DespawnOrUnsummon(1000) -- 1秒后消失 else creature:DespawnOrUnsummon(0) -- 立即消失 end end -- 定时清理系统 local function TimedCleanup(creature) local spawnTime = creature:GetData("spawnTime") or GetCurrTime() local currentTime = GetCurrTime() local aliveTime = currentTime - spawnTime -- 如果存在超过1小时且没有玩家在附近 if aliveTime > 3600000 then -- 1小时 local nearbyPlayer = creature:GetNearestPlayer(50) if not nearbyPlayer then creature:SendUnitSay("长时间无人关注,我要离开了", 0) creature:DespawnOrUnsummon(10000) -- 10秒后消失 end end end