Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.80 KB | None | 0 0
  1. local npcList = {3100, 3101, 3102};
  2. local range = 40; -- Range that will trigger / stop the npc from announcing.
  3. local delay = 1*10*1000; -- Delay between the announcements.
  4. local subDelay = 1*2*1000; -- Time between linked announcements.
  5.  
  6. local annTable = { ["Says"] = {
  7.     -- [index] = {msg, msgType, linkId, emoteId, spellId},
  8.     [1] = {"Yo, I heard you liked boars!", 1, 0, 6, 58837}, -- Announce with spell cast: 58837.
  9.     [2] = {"What's up dawg?", 0, 100, 1, 0}, -- Announcement say, linked to 100.
  10.     [100] = {"Yo, you good dawg", 0, 101, 1, 0}, -- Announcement say, linked to 101 and linked from 2.
  11.     [101] = {"Hey, I am talking to you!", 1, 0, 1, 0}, -- Announcement yell, linked from 100.
  12. }};
  13.  
  14. local getCreature = {
  15.     __newindex = function(tbl, key, value) 
  16.         local guid = key:GetGUID();
  17.         if value == false then
  18.             tbl.__cache[guid] = nil; return;
  19.         end
  20.        
  21.         local map = GetMapById(key:GetMapId(), 0);
  22.         -- Set the table, so we know the announcer
  23.         -- started announcing.
  24.         tbl.__cache[guid] = map;
  25.     end,   
  26.     __call = function(tbl, ...)
  27.         local guid = select(1, ...);
  28.         -- Does the table exist already?
  29.         if tbl.__cache[guid] == nil then
  30.             return nil;
  31.         end
  32.         local map = tbl.__cache[guid];
  33.         -- Return the object because that's the value
  34.         -- we want.
  35.         return map:GetWorldObject(guid);
  36.     end,
  37.     __cache = {},
  38. }
  39. setmetatable(getCreature, getCreature);
  40.        
  41. -- Amount values we need in a single table
  42. local reqData = 5; -- {msg, msgType, linkId, emoteId, spellId} = 5.
  43.  
  44. local function announce(id, guid)
  45.     local pUnit = getCreature(guid);
  46.     -- If the unit isn't active, we don't want to run this function.
  47.     if type(pUnit) ~= "userdata" then
  48.         return;
  49.     end
  50.    
  51.     local sayTable = annTable["Says"][id];
  52.     -- Are we missing data? We don't want any nil values ...
  53.     if #sayTable ~= reqData then
  54.         return;
  55.     end
  56.     -- Putting the data from the table into local variables,
  57.     -- to prevent a lot of table indexing.
  58.     local msg, msgType, linkId, emoteId, spellId --[[, spawnType, spawnId]] = table.unpack(sayTable);
  59.     -- Checking the data for events that should occur, if so
  60.     -- let it happen.
  61.     if msgType == 0 then pUnit:SendUnitSay(msg, 0); else pUnit:SendUnitYell(msg, 0); end
  62.     if emoteId ~= 0 then pUnit:Emote(emoteId); end
  63.     if linkId ~= 0 then
  64.         -- Does the table exist?
  65.         if annTable["Says"][linkId] == nil then
  66.             return;
  67.         end
  68.         -- Recursive call to announce, we want the same result right?
  69.         CreateLuaEvent(function() announce(linkId, pUnit:GetGUID()); end, subDelay, 1);
  70.     end
  71.     if spellId ~= 0 then pUnit:CastSpell(pUnit, spellId); end
  72. end
  73.  
  74. -- No need to check for active unit here, because this won't
  75. -- be called if the unit isn't active. 
  76. local function timedSay(_, _, _, pUnit)
  77.     announce(math.random(#annTable["Says"]), pUnit:GetGUID());
  78. end
  79.  
  80. local function onMotion(_, pUnit, plr)
  81.     -- If we stop announcing if there are no players
  82.     -- in a certain range, we shouldn't start either.
  83.     if #pUnit:GetPlayersInRange(range) == 0 then
  84.         return;
  85.     end
  86.     -- Are we announcing?
  87.     if type(getCreature(pUnit:GetGUID())) ~= "userdata" then
  88.         getCreature[pUnit] = true; -- We are now ...
  89.         pUnit:RegisterEvent(timedSay, delay, 0);
  90.     end
  91. end
  92.  
  93. local function cleanTable()
  94.     -- Are there any creatures in the table?
  95.     local cacheTable = getCreature.__cache
  96.     if next(cacheTable) == nil then
  97.         return;
  98.     end
  99.     -- Lets check for creatures that aren't active
  100.     -- and clean the table if that's the case.
  101.     for key, v in pairs(cacheTable) do
  102.         -- Is unit active?
  103.         local pUnit = getCreature(key);
  104.         if type(pUnit) ~= "userdata" then
  105.             getCreature[pUnit] = false;
  106.         elseif #pUnit:GetPlayersInRange(range) == 0 then
  107.             getCreature[pUnit] = false;
  108.         end
  109.     end
  110. end
  111. CreateLuaEvent(cleanTable, 5000, 0);
  112.    
  113.        
  114. for index=1, #npcList do
  115.     RegisterCreatureEvent(npcList[index], 27, onMotion);
  116. end
  117.  
  118. math.randomseed(os.time());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement