Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------------
- -- Variable setup
- --------------------------------------------------------
- local gameInProgress = false
- local grid_npc = {}
- local grid_obj = {}
- local a_npcs = {}
- local h_npcs = {}
- local current_npc = nil
- -- Format entry Id's: Castle, Bishop, Knight, Queen, King, Knight, Bishop, Castle
- local alliance_creatures = {32561, 32561, 32561, 32561, 32561, 32561, 32561, 32561}
- local horde_creatures = {32561, 32561, 32561, 32561, 32561, 32561, 32561, 32561}
- -- Creature entry ID's and what they are
- local creature_index = {[32561] = "a_castle",
- [32561] = "h_castle",
- [32561] = "a_bishop",
- [32561] = "h_bishop",
- [32561] = "a_knight",
- [32561] = "h_knight",
- [32561] = "a_queen",
- [32561] = "h_queen",
- [32561] = "a_king",
- [32561] = "h_king"}
- --------------------------------------------------------
- -- Event triggering (toggle)
- --------------------------------------------------------
- function chesschat(event, plr, message, mtype, language)
- if message == "#chess" then
- if gameInProgress then
- plr:SendBroadcastMessage("Ending chess.")
- DespawnChess()
- else
- plr:SendBroadcastMessage("Starting chess.")
- SpawnChess(plr)
- end
- return false
- end
- end
- RegisterServerHook(16, "chesschat")
- --------------------------------------------------------
- -- Spawn/Despawn grid
- --------------------------------------------------------
- function SpawnChess(pUnit)
- if pUnit == nil then
- return
- end
- if gameInProgress then
- pUnit:SendBroadcastMessage("Game is in progress - try again later.")
- return
- end
- gameInProgress = true
- local x = pUnit:GetX()
- local y = pUnit:GetY()
- local z = pUnit:GetZ()
- local i, j
- local a = 0
- local b = 0
- for i=0,35,5 do
- a = a + 1
- table.insert(a_npcs, pUnit:SpawnCreature(alliance_creatures[a], x+i, y, z+0.1, 1.5, 35, 0))
- for j=0,35,5 do
- if j == 35 then
- b = b + 1
- table.insert(h_npcs, pUnit:SpawnCreature(horde_creatures[b], x+i, y+j, z+0.1, 4.7, 35, 0))
- end
- table.insert(grid_npc, pUnit:SpawnCreature(28816, x+i, y+j, z, 0, 35, 0))
- table.insert(grid_obj, pUnit:SpawnGameObject(185301, x+i, y+j, z-0.21, 0, 0, 100))
- end
- end
- end
- function DespawnChess()
- for _,v in ipairs(grid_npc) do
- if v then
- v:Despawn(1,0)
- end
- end
- for _,v in ipairs(grid_obj) do
- if v then
- v:Despawn(1,0)
- end
- end
- for _,v in ipairs(a_npcs) do
- if v then
- v:Despawn(1,0)
- end
- end
- for _,v in ipairs(h_npcs) do
- if v then
- v:Despawn(1,0)
- end
- end
- grid_npc = {}
- grid_obj = {}
- a_npcs = {}
- h_npcs = {}
- gameInProgress = false
- current_npc = nil
- end
- --------------------------------------------------------
- -- Moving a piece
- --------------------------------------------------------
- function Chess_Piece_Move(pUnit)
- local plr = pUnit:GetClosestPlayer()
- if not plr then
- return
- end
- if plr:GetDistanceYards(pUnit) > 1.5 then
- return
- end
- local _c = pUnit:GetInRangeUnits()
- for _,v in ipairs(_c) do
- if v:IsCreatureMoving() then
- return
- end
- end
- if current_npc then
- if current_npc:GetX() ~= pUnit:GetX() then
- current_npc:RemoveAura(50236) -- light beam
- for _,v in pairs(grid_npc) do
- v:RemoveAura(50236)
- end
- end
- end
- current_npc = pUnit
- pUnit:CastSpell(50236) -- light beam visual
- ------------------------------------------------
- -- Handle where this unit can move
- ------------------------------------------------
- local piece = creature_index[pUnit:GetEntry()]
- if not piece then
- pUnit:SendChatMessage(12,0,"ERROR: I am not handled.")
- return
- end
- if piece == "h_king" then
- king_move(pUnit)
- end
- end
- function GridNPCSpawn(pUnit, Event)
- pUnit:SetUInt32Value(0x0006+0x0035, 0x02000000) -- untargetable
- pUnit:RegisterEvent("CheckPlayerComeAtMe", 1000, 0)
- end
- function CheckPlayerComeAtMe(pUnit)
- if not pUnit:HasAura(50236) then
- return
- end
- local plr = pUnit:GetClosestPlayer()
- if not plr then
- return
- end
- if plr:GetDistanceYards(pUnit) > 1.5 then
- return
- end
- if current_npc then
- current_npc:MoveTo(pUnit:GetX(), pUnit:GetY(), pUnit:GetZ(), 0) -- orientation should be handled
- current_npc:RemoveAura(50236) -- light beam
- for _,v in pairs(grid_npc) do
- v:RemoveAura(50236)
- end
- end
- end
- RegisterUnitEvent(28816, 18, "GridNPCSpawn")
- --------------------------------------------------------
- -- King movement
- --------------------------------------------------------
- function king_move(pUnit)
- local x, y, z = pUnit:GetX(), pUnit:GetY(), pUnit:GetZ()
- local n = pUnit:GetCreatureNearestCoords(x+5, y, z, 28816)
- if n then
- checkValidTile(n)
- end
- n = pUnit:GetCreatureNearestCoords(x-5, y, z, 28816)
- if n then
- checkValidTile(n)
- end
- n = pUnit:GetCreatureNearestCoords(x, y+5, z, 28816)
- if n then
- checkValidTile(n)
- end
- n = pUnit:GetCreatureNearestCoords(x, y-5, z, 28816)
- if n then
- checkValidTile(n)
- end
- current_npc = pUnit
- end
- function checkValidTile(pUnit)
- if pUnit:HasAura(50236) then
- return
- end
- local u = pUnit:GetClosestFriend()
- if u then
- if u:GetDistanceYards(pUnit) > 2 then
- pUnit:CastSpell(50236) -- light beam
- end
- end
- end
- --------------------------------------------------------
- -- Piece events (spawn)
- --------------------------------------------------------
- function PieceSpawn(pUnit, Event)
- pUnit:RegisterEvent("Chess_Piece_Move", 1000, 0)
- end
- RegisterUnitEvent(32561, 18, "PieceSpawn") -- For each alliance/horde piece
- --[[
- -- The table is not created in time for this register it seems
- local m
- for m=1,#alliance_creatures do
- RegisterUnitEvent(alliance_creatures[i], 18, "PieceSpawn")
- end
- for m=1,#horde_creatures do
- RegisterUnitEvent(horde_creatures[i], 18, "PieceSpawn")
- end
- ]]
Advertisement
Add Comment
Please, Sign In to add comment