stoneharry

Untitled

Jul 20th, 2015
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.74 KB | None | 0 0
  1.  
  2. local AI_TICKER = {}
  3. local STATES = {}
  4. local STORE = {}
  5.  
  6. local F_RANGE = 90036
  7. local F_MELEE = 90037
  8. local H_RANGE = 90039
  9. local H_MELEE = 90040
  10. local ALL_NPC = {F_RANGE, F_MELEE, H_RANGE, H_MELEE}
  11.  
  12. local JUST_SPAWNED = 0
  13. local HEADING_ARM = 1
  14. local ARMING_1 = 2
  15. local ARMING_2 = 3
  16. local ARMING_3 = 4
  17. local READY_FOR_BATTLE = 5
  18.  
  19. local ARMY_COUNT_PER_TEAM = 12
  20.  
  21. local ARMORY_POS = {
  22.     {-9463.6, 87.4},
  23.     {-9455.5, 88},
  24.     {-9465.6, 99.2},
  25.     {-9456.98, 98.8}
  26. }
  27.  
  28. local function SpawnMoreFriendly(e, b, c, pUnit)
  29.     local c = 0
  30.     local m = pUnit:GetCreaturesInRange(50, F_MELEE)
  31.     local r = pUnit:GetCreaturesInRange(50, F_RANGE)
  32.     for _,_ in pairs(m) do
  33.         c = c + 1
  34.     end
  35.     for _,_ in pairs(r) do
  36.         c = c + 1
  37.     end
  38.     if c > ARMY_COUNT_PER_TEAM then
  39.         return
  40.     end
  41.     for i=c,ARMY_COUNT_PER_TEAM do
  42.         if math.random(1,2) == 1 then
  43.             pUnit:SpawnCreature(F_MELEE, -9466, 24, 56.4, 1.48)
  44.         else
  45.             pUnit:SpawnCreature(F_RANGE, -9468, 18, 57, 1.46)
  46.         end
  47.     end
  48. end
  49.  
  50. local function SpawnMoreHostile(e, b, c, pUnit)
  51.     local c = 0
  52.     local m = pUnit:GetCreaturesInRange(50, H_MELEE)
  53.     local r = pUnit:GetCreaturesInRange(50, H_RANGE)
  54.     for _,_ in pairs(m) do
  55.         c = c + 1
  56.     end
  57.     for _,_ in pairs(r) do
  58.         c = c + 1
  59.     end
  60.     if c > ARMY_COUNT_PER_TEAM then
  61.         return
  62.     end
  63.     for i=c,ARMY_COUNT_PER_TEAM do
  64.         local sp = math.random(H_RANGE, H_MELEE)
  65.         if math.random(1,2) == 1 then
  66.             pUnit:SpawnCreature(sp, -9516, 70, 60, 6.165):CastSpell(pUnit, 41232, true)
  67.         else
  68.             pUnit:SpawnCreature(sp, -9428, 62, 57.1, 3.25):CastSpell(pUnit, 41232, true)
  69.         end
  70.     end
  71. end
  72.  
  73. local function handleState(state, pUnit, id)
  74.     if not pUnit:IsAlive() then
  75.         return
  76.     end
  77.     if state == JUST_SPAWNED then
  78.         if id < H_RANGE then
  79.             local p = math.random(1,4)
  80.             pUnit:MoveTo(0, ARMORY_POS[p][1], ARMORY_POS[p][2], 58.4)
  81.             STORE[tostring(pUnit:GetGUID())] = ARMORY_POS[p]
  82.             STATES[tostring(pUnit:GetGUID())] = HEADING_ARM
  83.         else
  84.             STATES[tostring(pUnit:GetGUID())] = READY_FOR_BATTLE
  85.         end
  86.     elseif state == HEADING_ARM then
  87.         local pos = STORE[tostring(pUnit:GetGUID())]
  88.         if pUnit:GetDistance2d(pos[1], pos[2]) < 1 then
  89.             pUnit:SetUInt32Value(UNIT_FIELD_BYTES_1, 9)
  90.             STATES[tostring(pUnit:GetGUID())] = ARMING_1
  91.         end
  92.     elseif state == ARMING_1 then
  93.         if id == F_MELEE then
  94.             pUnit:SetEquipmentSlots(2481, 28153, 0)
  95.             pUnit:SetUInt32Value(UNIT_FIELD_BYTES_2, 1)
  96.         elseif id == F_RANGE then
  97.             pUnit:SetEquipmentSlots(4965, 0, 2778)
  98.             pUnit:SetUInt32Value(UNIT_FIELD_BYTES_2, 2)
  99.         end
  100.         --pUnit:SetUInt32Value(UNIT_FIELD_BYTES_1, 8)
  101.         pUnit:Emote(381)
  102.         STATES[tostring(pUnit:GetGUID())] = ARMING_2
  103.     elseif state == ARMING_2 then
  104.         local offset = -9461 + (math.random(0, 22) - 10)
  105.         local offset2 = 64 + (math.random(0, 22) - 10)
  106.         pUnit:MoveTo(0, offset, offset2, 56)
  107.         STORE[tostring(pUnit:GetGUID())] = {offset, offset2}
  108.         pUnit:SetHomePosition(offset, offset2, 56, 3)
  109.         STATES[tostring(pUnit:GetGUID())] = ARMING_3
  110.     elseif state == ARMING_3 then
  111.         local pos = STORE[tostring(pUnit:GetGUID())]
  112.         if pUnit:GetDistance2d(pos[1], pos[2]) < 1 then
  113.             pUnit:SetFaction(814)
  114.             STATES[tostring(pUnit:GetGUID())] = READY_FOR_BATTLE
  115.         end
  116.     elseif state == READY_FOR_BATTLE then
  117.         if not pUnit:IsInCombat() then
  118.             if id >= H_RANGE then
  119.                 local offset = math.random(0, 22) - 10
  120.                 local offset2 = math.random(0, 22) - 10
  121.                 pUnit:MoveTo(0, -9461 + offset, 64 + offset2, 56)
  122.             end
  123.             --[[if id >= H_RANGE then
  124.                 local choice = math.random(F_RANGE, F_MELEE)
  125.                 local targets = pUnit:GetCreaturesInRange(50, choice)
  126.                 local amntTargets = #targets
  127.                 if amntTargets == 0 then
  128.                     return
  129.                 end
  130.                 local target = targets[math.random(1, amntTargets)]
  131.                 if target and target:IsAlive() then
  132.                     local state = STATES[tostring(target:GetGUID())]
  133.                     if state == READY_FOR_BATTLE then
  134.                         pUnit:AttackStart(target)
  135.                         if not target:IsInCombat() then
  136.                             target:AttackStart(pUnit)
  137.                         end
  138.                     end
  139.                 end
  140.             else
  141.                 local choice = math.random(H_RANGE, H_MELEE)
  142.                 local targets = pUnit:GetCreaturesInRange(50, choice)
  143.                 local amntTargets = #targets
  144.                 if amntTargets == 0 then
  145.                     return
  146.                 end
  147.                 local target = targets[math.random(1, amntTargets)]
  148.                 if target and target:IsAlive() then
  149.                     pUnit:AttackStart(target)
  150.                     if not target:IsInCombat() then
  151.                         target:AttackStart(pUnit)
  152.                     end
  153.                 end
  154.             end]]
  155.         end
  156.     end
  157. end
  158.  
  159. local function handleAI(pUnit)
  160.     for _,id in pairs(ALL_NPC) do
  161.         for _,v in pairs(pUnit:GetCreaturesInRange(50, id)) do
  162.             local state = STATES[tostring(v:GetGUID())]
  163.             if not state then
  164.                 state = JUST_SPAWNED
  165.             end
  166.             handleState(state, v, id)
  167.         end
  168.     end
  169. end
  170.  
  171. local function BattleAIChecker(e, b, c, pUnit)
  172.     local i = AI_TICKER[tostring(pUnit:GetGUID())]
  173.     i = i + 1
  174.     if i % 2 == 0 then
  175.         handleAI(pUnit)
  176.     end
  177.     if i % 8 == 0 then
  178.         SpawnMoreFriendly(nil, nil, nil, pUnit)
  179.         SpawnMoreHostile(nil, nil, nil, pUnit)
  180.     end
  181.     --[[if i % 10 == 0 then
  182.         local state_size = 0
  183.         local store_size = 0
  184.         for _,_ in pairs(STATES) do state_size = state_size + 1 end
  185.         for _,_ in pairs(STORE) do store_size = store_size + 1 end
  186.         pUnit:SendUnitSay(tostring(i) .. " | " .. tostring(state_size) .. " | " .. tostring(store_size), 0)
  187.     end]]
  188.     if i % 60 == 0 then
  189.         -- clean up
  190.         local states_copy = STATES
  191.         local store_copy = STORE
  192.         STATES = {}
  193.         STORE = {}
  194.         for _,id in pairs(ALL_NPC) do
  195.             for _,v in pairs(pUnit:GetCreaturesInRange(50, id)) do
  196.                 if v:IsAlive() then
  197.                     STATES[tostring(v:GetGUID())] = states_copy[tostring(v:GetGUID())]
  198.                     STORE[tostring(v:GetGUID())] = store_copy[tostring(v:GetGUID())]
  199.                 end
  200.             end
  201.         end
  202.     end
  203.     --[[if i % 120 == 0 then
  204.         local m = pUnit:GetCreaturesInRange(50, F_MELEE)
  205.         local r = pUnit:GetCreaturesInRange(50, F_RANGE)
  206.         for _,v in pairs(m) do
  207.             v:Kill(v)
  208.         end
  209.         for _,v in pairs(r) do
  210.             v:Kill(v)
  211.         end
  212.     end]]
  213.     if i > 5000 then
  214.         i = 0
  215.     end
  216.     AI_TICKER[tostring(pUnit:GetGUID())] = i
  217. end
  218.  
  219.  
  220. local function GoldshireHandler(event, pUnit)
  221.     for _,id in pairs(ALL_NPC) do
  222.         for _,v in pairs(pUnit:GetCreaturesInRange(50, id)) do
  223.             v:Kill(v)
  224.             v:DespawnOrUnsummon(1000)
  225.         end
  226.     end
  227.     AI_TICKER[tostring(pUnit:GetGUID())] = 0
  228.     pUnit:RegisterEvent(SpawnMoreFriendly, 1000, 1)
  229.     pUnit:RegisterEvent(BattleAIChecker, 1000, 0)
  230. end
  231.  
  232. RegisterCreatureEvent(90038, 5, GoldshireHandler)
  233.  
  234. local function taunt(e, d, r, pUnit)
  235.     local t = pUnit:GetAITarget(1)
  236.     if t then
  237.         pUnit:CastSpell(t, 355, true)
  238.     end
  239.     pUnit:RegisterEvent(taunt, 8000, 1)
  240. end
  241.  
  242. local function f_friendly_ai(event, pUnit, extra)
  243.     if event == 1 then
  244.         pUnit:RegisterEvent(taunt, 1000, 1)
  245.     else
  246.         pUnit:RemoveEvents()
  247.     end
  248. end
  249.  
  250. RegisterCreatureEvent(F_MELEE, 1, f_friendly_ai)
  251. RegisterCreatureEvent(F_MELEE, 2, f_friendly_ai)
  252. RegisterCreatureEvent(F_MELEE, 4, f_friendly_ai)
Advertisement
Add Comment
Please, Sign In to add comment