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
- local TURN = false
- local clock = os.clock()
- -- Format entry Id's: Castle, Bishop, Knight, King, Queen, Knight, Bishop, Castle, Pawn
- local alliance_creatures = {68001, 68003, 68002, 68005, 68004, 68002, 68003, 68001, 68000}
- local horde_creatures = {68007, 68009, 68008, 68011, 68010, 68008, 68009, 68007, 68006}
- -- Creature entry ID's and what they are
- local creature_index = {[68001] = "a_castle",
- [68007] = "h_castle",
- [68003] = "a_bishop",
- [68009] = "h_bishop",
- [68002] = "a_knight",
- [68008] = "h_knight",
- [68004] = "a_queen",
- [68010] = "h_queen",
- [68005] = "a_king",
- [68011] = "h_king",
- [68000] = "a_pawn",
- [68006] = "h_pawn"}
- --------------------------------------------------------
- -- 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 == 5 then
- table.insert(a_npcs, pUnit:SpawnCreature(alliance_creatures[9], x+i, y+j, z+0.1, 1.5, 35, 0))
- elseif j == 30 then
- table.insert(h_npcs, pUnit:SpawnCreature(horde_creatures[9], x+i, y+j, z+0.1, 4.7, 35, 0))
- elseif 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
- --------------------------------------------------------
- -- Utility
- --------------------------------------------------------
- function IsHordeCreature(entry)
- for _,v in pairs(horde_creatures) do
- if v == entry then
- return true
- end
- end
- return false
- end
- function IsAllianceCreature(entry)
- for _,v in pairs(alliance_creatures) do
- if v == entry then
- return true
- end
- end
- return false
- 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
- --------------------------------------------------------
- -- Moving a piece
- --------------------------------------------------------
- function Chess_Piece_Move(pUnit)
- if os.clock() - clock < 5 then
- return
- end
- local plr = pUnit:GetClosestPlayer()
- if not plr then
- return
- end
- if plr:GetDistanceYards(pUnit) > 1.5 then
- return
- end
- if IsAllianceCreature(pUnit:GetEntry()) then
- if TURN then
- plr:SendBroadcastMessage("It isn't your turn.")
- return
- end
- end
- if IsHordeCreature(pUnit:GetEntry()) then
- if not TURN then
- plr:SendBroadcastMessage("It isn't your turn.")
- return
- end
- end
- -- Flags for stationary creatures
- --[[local _c = pUnit:GetInRangeUnits()
- for _,v in ipairs(_c) do
- if IsAllianceCreature(v:GetEntry()) or IsHordeCreature(v:GetEntry()) then
- if v:IsCreatureMoving() then
- pUnit:SendChatMessage(12,0,v:GetName().." is moving!")
- return
- end
- 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
- for _,v in pairs(pUnit:GetInRangeUnits()) 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" or piece == "a_king" then
- king_move(pUnit)
- elseif piece == "a_pawn" or piece == "h_pawn" then
- pawn_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
- TURN = not TURN
- end
- end
- RegisterUnitEvent(28816, 18, "GridNPCSpawn")
- --------------------------------------------------------
- -- Pawn movement
- --------------------------------------------------------
- function pawn_move(pUnit)
- local x, y, z = pUnit:GetX(), pUnit:GetY(), pUnit:GetZ()
- local _x, _y, _z = pUnit:GetSpawnLocation()
- -- Alliance pawn movement
- if IsAllianceCreature(pUnit:GetEntry()) then
- if x == _x then
- local n = pUnit:GetCreatureNearestCoords(x, y+10, z, 28816)
- if n then
- checkValidTile(n)
- end
- end
- local n = pUnit:GetCreatureNearestCoords(x, y+5, z, 28816)
- if n then
- checkValidTile(n)
- end
- current_npc = pUnit
- return
- end
- -- Horde pawn movement
- if x == _x then
- local n = pUnit:GetCreatureNearestCoords(x, y-10, z, 28816)
- if n then
- checkValidTile(n)
- end
- end
- local n = pUnit:GetCreatureNearestCoords(x, y-5, z, 28816)
- if n then
- checkValidTile(n)
- end
- current_npc = pUnit
- end
- --------------------------------------------------------
- -- 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
- --------------------------------------------------------
- -- Piece events (spawn)
- --------------------------------------------------------
- function PieceSpawn(pUnit, Event)
- pUnit:RegisterEvent("Chess_Piece_Move", 1000, 0)
- local piece = creature_index[pUnit:GetEntry()]
- if piece == "a_knight" or piece == "h_knight" then
- pUnit:RegisterEvent("MountUpKnight_Piece", 1000, 1)
- end
- end
- function MountUpKnight_Piece(pUnit)
- local piece = creature_index[pUnit:GetEntry()]
- if piece == "a_knight" then
- pUnit:SetMount(14337)
- else
- pUnit:SetMount(14334)
- end
- end
- local m
- for m=68000,68011 do
- RegisterUnitEvent(m, 18, "PieceSpawn")
- end
- --[[
- local m
- for m=1,#alliance_creatures do
- RegisterUnitEvent(alliance_creatures[m], 18, "PieceSpawn")
- end
- for m=1,#horde_creatures do
- RegisterUnitEvent(horde_creatures[m], 18, "PieceSpawn")
- end
- ]]
Advertisement
Add Comment
Please, Sign In to add comment