stoneharry

Untitled

May 30th, 2013
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.34 KB | None | 0 0
  1.  
  2. --------------------------------------------------------
  3. -- Variable setup
  4. --------------------------------------------------------
  5.  
  6. local gameInProgress = false
  7. local grid_npc = {}
  8. local grid_obj = {}
  9. local a_npcs = {}
  10. local h_npcs = {}
  11. local current_npc = nil
  12. local TURN = false
  13. local clock = os.clock()
  14.  
  15. -- Format entry Id's: Castle, Bishop, Knight, King, Queen, Knight, Bishop, Castle, Pawn
  16. local alliance_creatures = {68001, 68003, 68002, 68005, 68004, 68002, 68003, 68001, 68000}
  17. local horde_creatures = {68007, 68009, 68008, 68011, 68010, 68008, 68009, 68007, 68006}
  18. -- Creature entry ID's and what they are
  19. local creature_index = {[68001] = "a_castle",
  20.                         [68007] = "h_castle",
  21.                         [68003] = "a_bishop",
  22.                         [68009] = "h_bishop",
  23.                         [68002] = "a_knight",
  24.                         [68008] = "h_knight",
  25.                         [68004] = "a_queen",
  26.                         [68010] = "h_queen",
  27.                         [68005] = "a_king",
  28.                         [68011] = "h_king",
  29.                         [68000] = "a_pawn",
  30.                         [68006] = "h_pawn"}
  31.  
  32. --------------------------------------------------------
  33. -- Event triggering (toggle)
  34. --------------------------------------------------------
  35.  
  36. function chesschat(event, plr, message, mtype, language)
  37.     if message == "#chess" then
  38.         if gameInProgress then
  39.             plr:SendBroadcastMessage("Ending chess.")
  40.             DespawnChess()
  41.         else
  42.             plr:SendBroadcastMessage("Starting chess.")
  43.             SpawnChess(plr)
  44.         end
  45.         return false
  46.     end
  47. end
  48.  
  49. RegisterServerHook(16, "chesschat")
  50.  
  51. --------------------------------------------------------
  52. -- Spawn/Despawn grid
  53. --------------------------------------------------------
  54.  
  55. function SpawnChess(pUnit)
  56.     if pUnit == nil then
  57.         return
  58.     end
  59.     if gameInProgress then
  60.         pUnit:SendBroadcastMessage("Game is in progress - try again later.")
  61.         return
  62.     end
  63.     gameInProgress = true
  64.     local x = pUnit:GetX()
  65.     local y = pUnit:GetY()
  66.     local z = pUnit:GetZ()
  67.     local i, j
  68.     local a = 0
  69.     local b = 0
  70.     for i=0,35,5 do
  71.         a = a + 1
  72.         table.insert(a_npcs, pUnit:SpawnCreature(alliance_creatures[a], x+i, y, z+0.1, 1.5, 35, 0))
  73.         for j=0,35,5 do
  74.             if j == 5 then
  75.                 table.insert(a_npcs, pUnit:SpawnCreature(alliance_creatures[9], x+i, y+j, z+0.1, 1.5, 35, 0))
  76.             elseif j == 30 then
  77.                 table.insert(h_npcs, pUnit:SpawnCreature(horde_creatures[9], x+i, y+j, z+0.1, 4.7, 35, 0))
  78.             elseif j == 35 then
  79.                 b = b + 1
  80.                 table.insert(h_npcs, pUnit:SpawnCreature(horde_creatures[b], x+i, y+j, z+0.1, 4.7, 35, 0))
  81.             end
  82.             table.insert(grid_npc, pUnit:SpawnCreature(28816, x+i, y+j, z, 0, 35, 0))
  83.             table.insert(grid_obj, pUnit:SpawnGameObject(185301, x+i, y+j, z-0.21, 0, 0, 100))
  84.         end
  85.     end
  86. end
  87.  
  88. function DespawnChess()
  89.     for _,v in ipairs(grid_npc) do
  90.         if v then
  91.             v:Despawn(1,0)
  92.         end
  93.     end
  94.     for _,v in ipairs(grid_obj) do
  95.         if v then
  96.             v:Despawn(1,0)
  97.         end
  98.     end
  99.     for _,v in ipairs(a_npcs) do
  100.         if v then
  101.             v:Despawn(1,0)
  102.         end
  103.     end
  104.     for _,v in ipairs(h_npcs) do
  105.         if v then
  106.             v:Despawn(1,0)
  107.         end
  108.     end
  109.     grid_npc = {}
  110.     grid_obj = {}
  111.     a_npcs = {}
  112.     h_npcs = {}
  113.     gameInProgress = false
  114.     current_npc = nil
  115. end
  116.  
  117. --------------------------------------------------------
  118. -- Utility
  119. --------------------------------------------------------
  120.  
  121. function IsHordeCreature(entry)
  122.     for _,v in pairs(horde_creatures) do
  123.         if v == entry then
  124.             return true
  125.         end
  126.     end
  127.     return false
  128. end
  129.  
  130. function IsAllianceCreature(entry)
  131.     for _,v in pairs(alliance_creatures) do
  132.         if v == entry then
  133.             return true
  134.         end
  135.     end
  136.     return false
  137. end
  138.  
  139. function checkValidTile(pUnit)
  140.     if pUnit:HasAura(50236) then
  141.         return
  142.     end
  143.     local u = pUnit:GetClosestFriend()
  144.     if u then
  145.         if u:GetDistanceYards(pUnit) > 2 then
  146.             pUnit:CastSpell(50236) -- light beam
  147.         end
  148.     end
  149. end
  150.  
  151. --------------------------------------------------------
  152. -- Moving a piece
  153. --------------------------------------------------------
  154.  
  155. function Chess_Piece_Move(pUnit)
  156.     if os.clock() - clock < 5 then
  157.         return
  158.     end
  159.     local plr = pUnit:GetClosestPlayer()
  160.     if not plr then
  161.         return
  162.     end
  163.     if plr:GetDistanceYards(pUnit) > 1.5 then
  164.         return
  165.     end
  166.     if IsAllianceCreature(pUnit:GetEntry()) then
  167.         if TURN then
  168.             plr:SendBroadcastMessage("It isn't your turn.")
  169.             return
  170.         end
  171.     end
  172.     if IsHordeCreature(pUnit:GetEntry()) then
  173.         if not TURN then
  174.             plr:SendBroadcastMessage("It isn't your turn.")
  175.             return
  176.         end
  177.     end
  178.     -- Flags for stationary creatures
  179.     --[[local _c = pUnit:GetInRangeUnits()
  180.     for _,v in ipairs(_c) do
  181.         if IsAllianceCreature(v:GetEntry()) or IsHordeCreature(v:GetEntry()) then
  182.             if v:IsCreatureMoving() then
  183.                 pUnit:SendChatMessage(12,0,v:GetName().." is moving!")
  184.                 return
  185.             end
  186.         end
  187.     end]]
  188.     if current_npc then
  189.         if current_npc:GetX() ~= pUnit:GetX() then
  190.             current_npc:RemoveAura(50236) -- light beam
  191.             --for _,v in pairs(grid_npc) do
  192.             for _,v in pairs(pUnit:GetInRangeUnits()) do
  193.                 v:RemoveAura(50236)
  194.             end
  195.         end
  196.     end
  197.     current_npc = pUnit
  198.     pUnit:CastSpell(50236) -- light beam visual
  199.     ------------------------------------------------
  200.     -- Handle where this unit can move
  201.     ------------------------------------------------
  202.     local piece = creature_index[pUnit:GetEntry()]
  203.     if not piece then
  204.         pUnit:SendChatMessage(12,0,"ERROR: I am not handled.")
  205.         return
  206.     end
  207.     if piece == "h_king" or piece == "a_king" then
  208.         king_move(pUnit)
  209.     elseif piece == "a_pawn" or piece == "h_pawn" then
  210.         pawn_move(pUnit)
  211.     end
  212. end
  213.  
  214. function GridNPCSpawn(pUnit, Event)
  215.     pUnit:SetUInt32Value(0x0006+0x0035, 0x02000000) -- untargetable
  216.     pUnit:RegisterEvent("CheckPlayerComeAtMe", 1000, 0)
  217. end
  218.  
  219. function CheckPlayerComeAtMe(pUnit)
  220.     if not pUnit:HasAura(50236) then
  221.         return
  222.     end
  223.     local plr = pUnit:GetClosestPlayer()
  224.     if not plr then
  225.         return
  226.     end
  227.     if plr:GetDistanceYards(pUnit) > 1.5 then
  228.         return
  229.     end
  230.     if current_npc then
  231.         current_npc:MoveTo(pUnit:GetX(), pUnit:GetY(), pUnit:GetZ(), 0) -- orientation should be handled
  232.         current_npc:RemoveAura(50236) -- light beam
  233.         for _,v in pairs(grid_npc) do
  234.             v:RemoveAura(50236)
  235.         end
  236.         TURN = not TURN
  237.     end
  238. end
  239.  
  240. RegisterUnitEvent(28816, 18, "GridNPCSpawn")
  241.  
  242. --------------------------------------------------------
  243. -- Pawn movement
  244. --------------------------------------------------------
  245.  
  246. function pawn_move(pUnit)
  247.     local x, y, z = pUnit:GetX(), pUnit:GetY(), pUnit:GetZ()
  248.     local _x, _y, _z = pUnit:GetSpawnLocation()
  249.     -- Alliance pawn movement
  250.     if IsAllianceCreature(pUnit:GetEntry()) then
  251.         if x == _x then
  252.             local n = pUnit:GetCreatureNearestCoords(x, y+10, z, 28816)
  253.             if n then
  254.                 checkValidTile(n)
  255.             end
  256.         end
  257.         local n = pUnit:GetCreatureNearestCoords(x, y+5, z, 28816)
  258.         if n then
  259.             checkValidTile(n)
  260.         end
  261.         current_npc = pUnit
  262.         return
  263.     end
  264.     -- Horde pawn movement
  265.     if x == _x then
  266.         local n = pUnit:GetCreatureNearestCoords(x, y-10, z, 28816)
  267.         if n then
  268.             checkValidTile(n)
  269.         end
  270.     end
  271.     local n = pUnit:GetCreatureNearestCoords(x, y-5, z, 28816)
  272.     if n then
  273.         checkValidTile(n)
  274.     end
  275.     current_npc = pUnit
  276. end
  277.  
  278. --------------------------------------------------------
  279. -- King movement
  280. --------------------------------------------------------
  281.  
  282. function king_move(pUnit)
  283.     local x, y, z = pUnit:GetX(), pUnit:GetY(), pUnit:GetZ()
  284.     local n = pUnit:GetCreatureNearestCoords(x+5, y, z, 28816)
  285.     if n then
  286.         checkValidTile(n)
  287.     end
  288.     n = pUnit:GetCreatureNearestCoords(x-5, y, z, 28816)
  289.     if n then
  290.         checkValidTile(n)
  291.     end
  292.     n = pUnit:GetCreatureNearestCoords(x, y+5, z, 28816)
  293.     if n then
  294.         checkValidTile(n)
  295.     end
  296.     n = pUnit:GetCreatureNearestCoords(x, y-5, z, 28816)
  297.     if n then
  298.         checkValidTile(n)
  299.     end
  300.     current_npc = pUnit
  301. end
  302.  
  303. --------------------------------------------------------
  304. -- Piece events (spawn)
  305. --------------------------------------------------------
  306.  
  307. function PieceSpawn(pUnit, Event)
  308.     pUnit:RegisterEvent("Chess_Piece_Move", 1000, 0)
  309.     local piece = creature_index[pUnit:GetEntry()]
  310.     if piece == "a_knight" or piece == "h_knight" then
  311.         pUnit:RegisterEvent("MountUpKnight_Piece", 1000, 1)
  312.     end
  313. end
  314.  
  315. function MountUpKnight_Piece(pUnit)
  316.     local piece = creature_index[pUnit:GetEntry()]
  317.     if piece == "a_knight" then
  318.         pUnit:SetMount(14337)
  319.     else
  320.         pUnit:SetMount(14334)
  321.     end
  322. end
  323.  
  324. local m
  325. for m=68000,68011 do
  326.     RegisterUnitEvent(m, 18, "PieceSpawn")
  327. end
  328.  
  329. --[[
  330. local m
  331. for m=1,#alliance_creatures do
  332.     RegisterUnitEvent(alliance_creatures[m], 18, "PieceSpawn")
  333. end
  334. for m=1,#horde_creatures do
  335.     RegisterUnitEvent(horde_creatures[m], 18, "PieceSpawn")
  336. end
  337. ]]
Advertisement
Add Comment
Please, Sign In to add comment