Code:
-- Constants
local guardA_ID = 120001
local guardB_ID = 120002
local invisDummyID = 120003
-- Instance tables
CM = {}
CM.VAR = {}
-- Gossip start event
function CM.VAR.OnGossipHello(event, player, creature)
player:GossipClearMenu()
player:GossipMenuAddItem(9, "< Placeholder Menu >", 120000, 1)
player:GossipMenuAddItem(0, "Nevermind.", 120000, 2)
player:GossipSendMenu(120000, creature)
end
function CM.VAR.OnGossipSelect(event, player, pUnit, sender, intid, code)
player:GossipComplete()
if intid == 1 then
pUnit:SetNPCFlags(0)
CM.VAR.beginEvent(pUnit)
end
end
RegisterCreatureGossipEvent(120000, 1, CM.VAR.OnGossipHello)
RegisterCreatureGossipEvent(120000, 2, CM.VAR.OnGossipSelect)
-- Utility
function getNearbyCreature(id, pUnit)
if id == pUnit:GetEntry() then
return pUnit
end
local creatures = pUnit:GetCreaturesInRange(20, id)
if #creatures == 0 then
pUnit:SendUnitSay("ERROR: creatures == 0", 0)
return
end
if #creatures == 1 then
return creatures[1]
end
return creatures[math.random(1, #creatures)]
end
-- Start event
function CM.VAR.beginEvent(pUnit)
local id = pUnit:GetInstanceId() or 1
CM[id] = CM[id] or {VAR={}}
local guardA = getNearbyCreature(guardA_ID, pUnit)
local guardB = getNearbyCreature(guardB_ID, pUnit)
pUnit:SendUnitSay("The crypts are filled with the undying, we must proceed carefully.", 0)
guardB:SetWalk(true)
guardB:MoveTo(0, 203, 104, 128.4)
guardA:SetWalk(true)
guardA:MoveTo(0, 197.3, 104.57, 128.42)
pUnit:SetWalk(true)
pUnit:MoveTo(0, 200, 107, 128.42)
CM[id].VAR.i = 0
pUnit:RegisterEvent(CM.VAR.eventTicker, 1000, 0)
end
-- Event ticker
function CM.VAR.eventTicker(eventID, delay, repeats, pUnit)
local id = pUnit:GetInstanceId() or 1
CM[id] = CM[id] or {VAR={}}
CM[id].VAR.i = CM[id].VAR.i + 1
local i = CM[id].VAR.i
if i == 5 then
local guardA = getNearbyCreature(guardA_ID, pUnit)
guardA:SendUnitSay("We've dealt with these kind before. This should be no problem.", 0)
elseif i == 9 then
local guardA = getNearbyCreature(guardA_ID, pUnit)
local guardB = getNearbyCreature(guardB_ID, pUnit)
guardA:CastSpell(guardA, 36858)
guardB:CastSpell(guardB, 36858)
local creature = guardA:SpawnCreature(120003, guardA:GetX(), guardA:GetY(), guardA:GetZ(), 0, 8, 10000)
creature:SetUInt32Value(0x0006 + 0x0035, 33554432)
creature:RegisterEvent(CM.VAR.MoveDummies, 1000, 8)
creature = guardB:SpawnCreature(120003, guardB:GetX(), guardB:GetY(), guardB:GetZ(), 0, 8, 10000)
creature:SetUInt32Value(0x0006 + 0x0035, 33554432)
creature:RegisterEvent(CM.VAR.MoveDummies, 1000, 8)
elseif i == 16 then
for _,v in pairs(pUnit:GetCreaturesInRange(70, 120003)) do
v:RemoveEvents()
v:DespawnOrUnsummon(0)
end
local guardA = getNearbyCreature(guardA_ID, pUnit)
local guardB = getNearbyCreature(guardB_ID, pUnit)
guardB:MoveTo(0, 203, 48, 128)
guardA:MoveTo(0, 198, 47, 128)
pUnit:MoveTo(0, 201, 51, 128)
elseif i == 20 then
local guardB = getNearbyCreature(guardB_ID, pUnit)
guardB:SendUnitSay("How have their come to be so many down here?", 0)
elseif i == 25 then
pUnit:SendUnitSay("There must be a necromancer. It's the only possible scenario I can think of.", 0)
elseif i == 32 then
local guardB = getNearbyCreature(guardB_ID, pUnit)
guardB:SendUnitSay("I don't like the sound of that.", 0)
elseif i == 41 then
local guardA = getNearbyCreature(guardA_ID, pUnit)
local guardB = getNearbyCreature(guardB_ID, pUnit)
pUnit:MoveTo(0, 193, 49, 128)
guardA:MoveTo(0, 193, 53, 128)
guardB:MoveTo(0, 193, 46, 128)
elseif i == 44 then
local guardA = getNearbyCreature(guardA_ID, pUnit)
local guardB = getNearbyCreature(guardB_ID, pUnit)
pUnit:MoveTo(0, 172, 49, 115)
guardB:MoveTo(0, 172, 45, 115)
guardA:MoveTo(0, 172, 52, 115)
elseif i == 56 then
local guardA = getNearbyCreature(guardA_ID, pUnit)
local guardB = getNearbyCreature(guardB_ID, pUnit)
pUnit:MoveTo(0, 166, 42, 115)
guardB:MoveTo(0, 170, 42, 115)
guardA:MoveTo(0, 163, 42, 115)
elseif i == 58 then
local guardA = getNearbyCreature(guardA_ID, pUnit)
local guardB = getNearbyCreature(guardB_ID, pUnit)
pUnit:MoveTo(0, 183, 22, 115)
guardB:MoveTo(0, 186, 25, 115)
guardA:MoveTo(0, 180, 17, 115)
elseif i == 73 then
local guardA = getNearbyCreature(guardA_ID, pUnit)
local guardB = getNearbyCreature(guardB_ID, pUnit)
pUnit:SetFacing(5.518449)
guardA:SetFacing(5.333883)
guardB:SetFacing(5.761922)
elseif i == 74 then
pUnit:SendUnitYell("Necromancer, prepare to face the light for your judgement has come!", 0)
pUnit:Emote(5)
elseif i == 76 then
local necro = pUnit:GetCreaturesInRange(40, 120006)[1]
necro:RemoveEvents()
necro:StopSpellCast(40193)
necro:CastSpell(necro, 67040, true)
for _,v in pairs(pUnit:GetCreaturesInRange(40, 120005)) do
v:CastSpell(v, 40927, true)
v:Emote(431)
end
elseif i == 78 then
local guardA = getNearbyCreature(guardA_ID, pUnit)
local guardB = getNearbyCreature(guardB_ID, pUnit)
pUnit:CastSpell(pUnit, 465, true)
pUnit:Emote(45)
guardA:Emote(45)
guardB:Emote(45)
elseif i == 81 then
local necro = pUnit:GetCreaturesInRange(40, 120006)[1]
necro:StopSpellCast(67040)
for _,v in pairs(pUnit:GetCreaturesInRange(40, 120005)) do
v:CastSpell(v, 47496, false)
end
pUnit:CastSpell(pUnit, 45922, true)
local guardA = getNearbyCreature(guardA_ID, pUnit)
local guardB = getNearbyCreature(guardB_ID, pUnit)
necro:SendUnitYell("You shall all perish!", 0)
necro:SetUInt32Value(0x0006 + 0x0035, 0)
elseif i == 87 then
local guardA = getNearbyCreature(guardA_ID, pUnit)
guardA:CastSpell(guardA, 45922, true)
local guardB = getNearbyCreature(guardB_ID, pUnit)
guardB:CastSpell(guardB, 45922, true)
elseif i == 100 then
pUnit:RemoveEvents()
end
end
-- Dummy projectile killing scourge at start
function CM.VAR.MoveDummies(eventID, delay, repeats, pUnit)
pUnit:CastSpell(pUnit, 44880, true)
pUnit:CastSpell(pUnit, 43241, true)
pUnit:MoveTo(0, pUnit:GetX(), pUnit:GetY() - 10, pUnit:GetZ())
for _,v in pairs(pUnit:GetCreaturesInRange(8, 120004)) do
v:CastSpell(v, 16003, true)
pUnit:Kill(v)
end
end
-- Make trash walk around randomly
function CM.VAR.FlashyTrashMoveAround(event, pUnit, ...)
if event == 5 then -- spawned
pUnit:SetUInt32Value(0x0006 + 0x0035, 2 + 131072)
pUnit:SetFaction(14)
pUnit:RegisterEvent(CM.VAR.MoveRandom_Call, math.random(100, 3000), 1)
else
pUnit:RemoveEvents()
end
end
function CM.VAR.MoveRandom_Call(eventID, delay, repeats, pUnit)
pUnit:MoveRandom(2)
pUnit:RegisterEvent(CM.VAR.MoveRandom_Call, math.random(100, 3000), 1)
end
RegisterCreatureEvent(120004, 5, CM.VAR.FlashyTrashMoveAround)
RegisterCreatureEvent(120004, 4, CM.VAR.FlashyTrashMoveAround)
-- Prisoners
function CM.VAR.PrisonerSpawn(event, pUnit)
pUnit:SetUInt32Value(0x0006 + 0x0035, 2 + 131072)
if pUnit:GetEntry() == 120006 then
pUnit:RegisterEvent(CM.VAR.StranglePeople, 3100, 0)
else
local maxHealth = pUnit:GetMaxHealth() / 2
local amount = math.random(1, 10)
if (math.random(1,2) == 1) then
maxHealth = maxHealth + amount
else
maxHealth = maxHealth - amount
end
pUnit:SetHealth(maxHealth)
end
end
function CM.VAR.StranglePeople(eventID, delay, repeats, pUnit)
pUnit:StopSpellCast(40193)
local creature = getNearbyCreature(120005, pUnit)
if not creature then
pUnit:SendUnitSay("ERROR: No unit found.", 0)
return
end
creature:CastSpell(creature, 28136, true)
pUnit:CastSpell(creature, 40193, true)
creature:Emote(473)
creature:RegisterEvent(CM.VAR.StopEmote, 3000, 1)
end
function CM.VAR.StopEmote(id, delay, repeats, pUnit)
pUnit:Emote(20)
end
RegisterCreatureEvent(120005, 5, CM.VAR.PrisonerSpawn)
RegisterCreatureEvent(120006, 5, CM.VAR.PrisonerSpawn)
RegisterCreatureEvent(120006, 4, CM.VAR.FlashyTrashMoveAround)
It is bad practice in general to store any sort of direct reference to the object in a variable outside of the scope of the function in question. If you must determine if it is the same npc as later and it is not a unique npc ID then store the GUID and find the creature that matches the stored GUID.