stoneharry

Untitled

May 30th, 2013
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.74 KB | None | 0 0
  1. --------------------------------------------------------
  2. -- Variable setup
  3. --------------------------------------------------------
  4.  
  5. local gameInProgress = false
  6. local grid_npc = {}
  7. local grid_obj = {}
  8. local a_npcs = {}
  9. local h_npcs = {}
  10. local current_npc = nil
  11.  
  12. -- Format entry Id's: Castle, Bishop, Knight, Queen, King, Knight, Bishop, Castle
  13. local alliance_creatures = {32561, 32561, 32561, 32561, 32561, 32561, 32561, 32561}
  14. local horde_creatures = {32561, 32561, 32561, 32561, 32561, 32561, 32561, 32561}
  15. -- Creature entry ID's and what they are
  16. local creature_index = {[32561] = "a_castle",
  17.                         [32561] = "h_castle",
  18.                         [32561] = "a_bishop",
  19.                         [32561] = "h_bishop",
  20.                         [32561] = "a_knight",
  21.                         [32561] = "h_knight",
  22.                         [32561] = "a_queen",
  23.                         [32561] = "h_queen",
  24.                         [32561] = "a_king",
  25.                         [32561] = "h_king"}
  26.  
  27. --------------------------------------------------------
  28. -- Event triggering (toggle)
  29. --------------------------------------------------------
  30.  
  31. function chesschat(event, plr, message, mtype, language)
  32.     if message == "#chess" then
  33.         if gameInProgress then
  34.             plr:SendBroadcastMessage("Ending chess.")
  35.             DespawnChess()
  36.         else
  37.             plr:SendBroadcastMessage("Starting chess.")
  38.             SpawnChess(plr)
  39.         end
  40.         return false
  41.     end
  42. end
  43.  
  44. RegisterServerHook(16, "chesschat")
  45.  
  46. --------------------------------------------------------
  47. -- Spawn/Despawn grid
  48. --------------------------------------------------------
  49.  
  50. function SpawnChess(pUnit)
  51.     if pUnit == nil then
  52.         return
  53.     end
  54.     if gameInProgress then
  55.         pUnit:SendBroadcastMessage("Game is in progress - try again later.")
  56.         return
  57.     end
  58.     gameInProgress = true
  59.     local x = pUnit:GetX()
  60.     local y = pUnit:GetY()
  61.     local z = pUnit:GetZ()
  62.     local i, j
  63.     local a = 0
  64.     local b = 0
  65.     for i=0,35,5 do
  66.         a = a + 1
  67.         table.insert(a_npcs, pUnit:SpawnCreature(alliance_creatures[a], x+i, y, z+0.1, 1.5, 35, 0))
  68.         for j=0,35,5 do
  69.             if j == 35 then
  70.                 b = b + 1
  71.                 table.insert(h_npcs, pUnit:SpawnCreature(horde_creatures[b], x+i, y+j, z+0.1, 4.7, 35, 0))
  72.             end
  73.             table.insert(grid_npc, pUnit:SpawnCreature(28816, x+i, y+j, z, 0, 35, 0))
  74.             table.insert(grid_obj, pUnit:SpawnGameObject(185301, x+i, y+j, z-0.21, 0, 0, 100))
  75.         end
  76.     end
  77. end
  78.  
  79. function DespawnChess()
  80.     for _,v in ipairs(grid_npc) do
  81.         if v then
  82.             v:Despawn(1,0)
  83.         end
  84.     end
  85.     for _,v in ipairs(grid_obj) do
  86.         if v then
  87.             v:Despawn(1,0)
  88.         end
  89.     end
  90.     for _,v in ipairs(a_npcs) do
  91.         if v then
  92.             v:Despawn(1,0)
  93.         end
  94.     end
  95.     for _,v in ipairs(h_npcs) do
  96.         if v then
  97.             v:Despawn(1,0)
  98.         end
  99.     end
  100.     grid_npc = {}
  101.     grid_obj = {}
  102.     a_npcs = {}
  103.     h_npcs = {}
  104.     gameInProgress = false
  105.     current_npc = nil
  106. end
  107.  
  108. --------------------------------------------------------
  109. -- Moving a piece
  110. --------------------------------------------------------
  111.  
  112. function Chess_Piece_Move(pUnit)
  113.     local plr = pUnit:GetClosestPlayer()
  114.     if not plr then
  115.         return
  116.     end
  117.     if plr:GetDistanceYards(pUnit) > 1.5 then
  118.         return
  119.     end
  120.     local _c = pUnit:GetInRangeUnits()
  121.     for _,v in ipairs(_c) do
  122.         if v:IsCreatureMoving() then
  123.             return
  124.         end
  125.     end
  126.     if current_npc then
  127.         if current_npc:GetX() ~= pUnit:GetX() then
  128.             current_npc:RemoveAura(50236) -- light beam
  129.             for _,v in pairs(grid_npc) do
  130.                 v:RemoveAura(50236)
  131.             end
  132.         end
  133.     end
  134.     current_npc = pUnit
  135.     pUnit:CastSpell(50236) -- light beam visual
  136.     ------------------------------------------------
  137.     -- Handle where this unit can move
  138.     ------------------------------------------------
  139.     local piece = creature_index[pUnit:GetEntry()]
  140.     if not piece then
  141.         pUnit:SendChatMessage(12,0,"ERROR: I am not handled.")
  142.         return
  143.     end
  144.     if piece == "h_king" then
  145.         king_move(pUnit)
  146.     end
  147. end
  148.  
  149. function GridNPCSpawn(pUnit, Event)
  150.     pUnit:SetUInt32Value(0x0006+0x0035, 0x02000000) -- untargetable
  151.     pUnit:RegisterEvent("CheckPlayerComeAtMe", 1000, 0)
  152. end
  153.  
  154. function CheckPlayerComeAtMe(pUnit)
  155.     if not pUnit:HasAura(50236) then
  156.         return
  157.     end
  158.     local plr = pUnit:GetClosestPlayer()
  159.     if not plr then
  160.         return
  161.     end
  162.     if plr:GetDistanceYards(pUnit) > 1.5 then
  163.         return
  164.     end
  165.     if current_npc then
  166.         current_npc:MoveTo(pUnit:GetX(), pUnit:GetY(), pUnit:GetZ(), 0) -- orientation should be handled
  167.         current_npc:RemoveAura(50236) -- light beam
  168.         for _,v in pairs(grid_npc) do
  169.             v:RemoveAura(50236)
  170.         end
  171.     end
  172. end
  173.  
  174. RegisterUnitEvent(28816, 18, "GridNPCSpawn")
  175.  
  176. --------------------------------------------------------
  177. -- King movement
  178. --------------------------------------------------------
  179.  
  180. function king_move(pUnit)
  181.     local x, y, z = pUnit:GetX(), pUnit:GetY(), pUnit:GetZ()
  182.     local n = pUnit:GetCreatureNearestCoords(x+5, y, z, 28816)
  183.     if n then
  184.         checkValidTile(n)
  185.     end
  186.     n = pUnit:GetCreatureNearestCoords(x-5, y, z, 28816)
  187.     if n then
  188.         checkValidTile(n)
  189.     end
  190.     n = pUnit:GetCreatureNearestCoords(x, y+5, z, 28816)
  191.     if n then
  192.         checkValidTile(n)
  193.     end
  194.     n = pUnit:GetCreatureNearestCoords(x, y-5, z, 28816)
  195.     if n then
  196.         checkValidTile(n)
  197.     end
  198.     current_npc = pUnit
  199. end
  200.  
  201. function checkValidTile(pUnit)
  202.     if pUnit:HasAura(50236) then
  203.         return
  204.     end
  205.     local u = pUnit:GetClosestFriend()
  206.     if u then
  207.         if u:GetDistanceYards(pUnit) > 2 then
  208.             pUnit:CastSpell(50236) -- light beam
  209.         end
  210.     end
  211. end
  212.  
  213. --------------------------------------------------------
  214. -- Piece events (spawn)
  215. --------------------------------------------------------
  216.  
  217. function PieceSpawn(pUnit, Event)
  218.     pUnit:RegisterEvent("Chess_Piece_Move", 1000, 0)
  219. end
  220.  
  221. RegisterUnitEvent(32561, 18, "PieceSpawn") -- For each alliance/horde piece
  222.  
  223. --[[
  224. -- The table is not created in time for this register it seems
  225. local m
  226. for m=1,#alliance_creatures do
  227.     RegisterUnitEvent(alliance_creatures[i], 18, "PieceSpawn")
  228. end
  229. for m=1,#horde_creatures do
  230.     RegisterUnitEvent(horde_creatures[i], 18, "PieceSpawn")
  231. end
  232. ]]
Advertisement
Add Comment
Please, Sign In to add comment