stoneharry

Untitled

Feb 17th, 2013
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.12 KB | None | 0 0
  1.  
  2. local OBJECT_END = 0x0006
  3. local UNIT_FIELD_FLAGS = OBJECT_END + 0x0035 -- Size: 1, Type: INT, Flags: PUBLIC
  4. local GAMEOBJECT_BYTES_1 = OBJECT_END + 0x000B
  5. local UNIT_FLAG_NOT_SELECTABLE = 0x02000000 -- 26    33554432  cannot select the unit
  6. local UNIT_FIELD_BYTES_1 = OBJECT_END + 0x0044
  7.  
  8. local Race_System = {
  9.     RACE_STATUS = "Looking For Racers",
  10.     RACERS = {},
  11.     SHOW = {},
  12.     started = false,
  13.     countdown = 10,
  14.     place = 1,
  15.     RACING = {},
  16.     WINORDER = {},
  17.     print_count = 0,
  18.     counted,
  19.     stime = 0,
  20.     outputwinners = false
  21. }
  22.  
  23. --Checkpoint locations
  24. local Checkpoints = {
  25. [1] = {{-6101.3, -3893.6}, {-6100.1, -3896.8}, {-6098.4, -3901.0}},
  26. [2] = {{-5981.5, -3834.2}, {-5979.1, -3841.7}, {-5976.9, -3848.3}},
  27. [3] = {{-5865.9, -3782.0}, {-5863.8, -3788.6}, {-5862.1, -3794.1}},
  28. [4] = {{-5726.6, -3785.7}, {-5729.8, -3794.4}, {-5733.1, -3802.6}},
  29. [5] = {{-5641.5, -3884.8}, {-5653.0, -3886.0}, {-5661.8, -3887.0}, {-5671.3, -3887.8}},
  30. [6] = {{-5637.5, -4085.0}, {-5649.3, -4081.4}, {-5659.0, -4076.1}},
  31. [7] = {{-5745.7, -4192.3}, {-5748.8, -4183.1}, {-5751.5, -4175.2}},
  32. [8] = {{-5881.5, -4206.2}, {-5882.2, -4196.9}, {-5882.8, -4188.3}},
  33. [9] = {{-5990.3, -4219.0}, {-5990.5, -4207.1}, {-5990.7, -4196.1}, {-5990.9, -4182.3}},
  34. [10] = {{-6105.1, -4207.2}, {-6104.6, -4193.8}, {-6104.0, -4179.6}, {-6103.5, -4166.4}},
  35. [11] = {{-6220.5, -4215.0}, {-6222.3, -4199.3}, {-6224.0, -4185.0}},
  36. [12] = {{-6328.7, -4229.6}, {-6326.4, -4216.8}, {-6323.9, -4202.3}},
  37. [13] = {{-6406.9, -4179.9}, {-6398.5, -4171.7}, {-6391.5, -4164.8}},
  38. [14] = {{-6433.8, -4090.5}, {-6422.5, -4086.0}},
  39. [15] = {{-6432.5, -3987.9}, {-6421.9, -3988.7}},
  40. [16] = {{-6399.7, -3912.8}, {-5395.8, -3923.0}},
  41. [17] = {{-6342.5, -3897.8}, {-6341.3, -3908.8}},
  42. [18] = {{-6200.0, -3894.8}, {-6200.0, -3898.5}, {-6200.1, -3902.4}, {-6200.1, -3906.9}, {-6200.2, -3911.4}}
  43. }
  44.  
  45. function SetupRacePlayer(player)
  46.     if (player) then
  47.         local q = CharDBQuery("SELECT won, lost, vehicle FROM racestats WHERE name = '"..player:GetName().."';")
  48.         if (q) then
  49.             player:SetValue("race_won", q:GetColumn(0):GetLong())
  50.             player:SetValue("race_lost", q:GetColumn(1):GetLong())
  51.             player:SetValue("race_vehicle", q:GetColumn(2):GetLong())
  52.         else
  53.             player:SetValue("race_won", 0)
  54.             player:SetValue("race_lost", 0)
  55.             player:SetValue("race_vehicle", 33062)
  56.             CharDBQuery("INSERT INTO racestats VALUES ('"..player:GetName().."', 0, 0, 33062);")
  57.         end
  58.         if (not player:GetValue("race_status")) then
  59.             player:SetValue("race_place", 0)
  60.             player:SetValue("race_point", 0)
  61.             player:SetValue("race_status", false) --false = idle, true = waiting/racing
  62.             player:SetValue("race_finished", false) --power-up target checks, bitch
  63.             player:SetValue("race_page", 0)
  64.             player:SetValue("race_vehicle_unit", 0)
  65.         end
  66.     end
  67. end
  68.  
  69. local VAR = {}
  70.  
  71. function DisplayRacesWon(player)
  72.     if (player and player:GetValue("race_won")) then
  73.         return player:GetValue("race_won")
  74.     else
  75.         local q = CharDBQuery("SELECT won FROM racestats WHERE name = '"..player:GetName().."';")
  76.         if (q)then
  77.             player:SetValue("race_won", q:GetColumn(0):GetLong())
  78.             return q:GetColumn(0):GetLong()
  79.         else
  80.             player:SetValue("race_won", 0)
  81.             return 0
  82.         end
  83.     end
  84. end
  85.  
  86. function DisplayRacesLost(player)
  87.     if (player and player:GetValue("race_lost")) then
  88.         return player:GetValue("race_lost")
  89.     else
  90.         local q = CharDBQuery("SELECT lost FROM racestats WHERE name = '"..player:GetName().."';")
  91.         if (q)then
  92.             player:SetValue("race_lost", q:GetColumn(0):GetLong())
  93.             return q:GetColumn(0):GetLong()
  94.         else
  95.             player:SetValue("race_lost", 0)
  96.             return 0
  97.         end
  98.     end
  99. end
  100.  
  101. function GetPageLength(player, table_length)
  102.     local page = player:GetValue("race_page")
  103.     local counter = (page * 10) + 1
  104.     local length = table_length
  105.     if (table_length - counter > 10) then
  106.         length = counter + 9
  107.     end
  108.     return counter, length
  109. end
  110.  
  111. function Test_Item_Trigger(item, event, player)
  112.     Test_Item(item, player)
  113. end
  114.  
  115. function Test_Item(item, player, intid, code)
  116.     if (not player:GetValue("race_page")) then
  117.         SetupRacePlayer(player)
  118.     end
  119.     item:GossipCreateMenu(50, player, 0)
  120.     item:GossipMenuAddItem(4, "|cff3060B5Current status:|r "..Race_System.RACE_STATUS, 1, 0)
  121.     if (Race_System.RACE_STATUS == "Looking For Racers") then
  122.         if (player:GetAreaId() == 2240 or player:GetAreaId() == 3038) then
  123.             if (player:GetValue("race_status")) then
  124.                 item:GossipMenuAddItem(9, "Unsign me from this race.", 2, 0)
  125.             else
  126.                 item:GossipMenuAddItem(9, "Sign me up for this race!", 2, 0)
  127.             end
  128.         else
  129.             item:GossipMenuAddItem(9, "Go to the race track to sign up.", 1, 0)
  130.         end
  131.     end
  132.     if (#Race_System.RACERS > 0) then
  133.         local a, b = GetPageLength(player, #Race_System.RACERS)
  134.         for i = a, b do
  135.             if (Race_System.RACERS[i]:GetName()) then
  136.                 item:GossipMenuAddItem(4, "   > Racer "..i..": "..Race_System.RACERS[i]:GetName(), 100+i, 0)
  137.                 if (intid == i+100 and Race_System.SHOW[intid] == true) then
  138.                     item:GossipMenuAddItem(4, "|cff095F0B     > Races won: |r"..DisplayRacesWon(Race_System.RACERS[i]), 100+i, 0)
  139.                     item:GossipMenuAddItem(4, "|cFFFF0000     > Races lost: |r"..DisplayRacesLost(Race_System.RACERS[i]), 100+i, 0)
  140.                 end
  141.             end
  142.         end
  143.         if (b > #Race_System.RACERS) then
  144.             item:GossipMenuAddItem(7, "Next Page.", 4, 0)
  145.         end
  146.         if (player:GetValue("race_page") > 0) then
  147.             item:GossipMenuAddItem(7, "Previous page.", 5, 0)
  148.         end
  149.     end
  150.     item:GossipMenuAddItem(0, "Nevermind.", 99, 0)
  151.     item:GossipSendMenu(player)
  152. end
  153.  
  154. function Test_Item_Select(item, event, player, id, intid, code)
  155.     if (intid == 1) then
  156.         Test_Item(item, player)
  157.     elseif (intid == 2) then
  158.         if (player:GetValue("race_status")) then
  159.             for k, v in pairs (Race_System.RACERS) do
  160.                 if (v:GetName() == player:GetName()) then
  161.                     table.remove(Race_System.RACERS, k)
  162.                     player:SetValue("race_status", false)
  163.                 end
  164.             end
  165.             table.sort(Race_System.RACERS)
  166.         else
  167.             table.insert(Race_System.RACERS, player)
  168.             player:SetValue("race_status", true)
  169.         end
  170.         Test_Item(item, player)
  171.     elseif (intid == 4) then
  172.         player:ModValue("race_page", 1)
  173.         Test_Item(item, player)
  174.     elseif (intid == 5) then
  175.         player:ModValue("race_page", -1)
  176.         Test_Item(item, player)
  177.     elseif (intid == 99) then
  178.         player:GossipComplete()
  179.     end
  180.     for i = 101, (#Race_System.RACERS+100) do
  181.         if (intid == i) then
  182.             if Race_System.SHOW[intid] == true then
  183.                 Race_System.SHOW[intid] = false
  184.             else
  185.                 Race_System.SHOW[intid] = true
  186.             end
  187.             Test_Item(item, player, intid)
  188.         end
  189.     end
  190. end
  191.  
  192. RegisterItemGossipEvent(57060, 1, "Test_Item_Trigger")
  193. RegisterItemGossipEvent(57060, 2, "Test_Item_Select")
  194.  
  195. local RacePoint = {
  196. [1] = {-6204, -3911, -60.32},
  197. [2] = {-6204, -3907, -60.32},
  198. [3] = {-6204, -3903,  -60.32},
  199. [4] = {-6202, -3898,  -60.32},
  200. [5] = {-6204, -3894,  -60.32}
  201. }
  202.  
  203. local DummyRetard
  204.  
  205. function DummyRetardSpawn(pUnit)
  206.     DummyRetard = pUnit
  207.     pUnit:RegisterEvent(DummyRetardLoop, 400, 0)
  208. end
  209. function DummyRetardLoop(pUnit)
  210.     if (Race_System.started) then
  211.         for k, v in pairs (Checkpoints) do
  212.             CheckPoint_CheckPlace(k)
  213.         end
  214.     end
  215. end
  216. function DummyRetardAction(player, q)
  217.     player:SpawnAndEnterVehicle(q, 1000)
  218.     DummyRetard:RegisterEvent(function() DummyRetardAction2(player, q); end, 1001, 1)
  219. end
  220. function DummyRetardAction2(player, q)
  221.     player:GetVehicleBase():Root()
  222.     player:SetValue("race_vehicle_unit", player:GetVehicleBase())
  223.     if (q == 33062) then
  224.         local x, y, z, o = player:GetLocation()
  225.         local c = player:SpawnCreature(33775, x, y, z, o, 35, 0)
  226.         if (c and player:IsOnVehicle()) then
  227.             c:SetUInt32Value(UNIT_FIELD_FLAGS, 10) -- look like a player
  228.             c:EnterVehicle(player:GetVehicleBase(), 1000)
  229.         end
  230.         c:RegisterEvent(RaceCompanionRemove, 1500, 0)
  231.     end
  232. end
  233. function RaceCompanionRemove(pUnit)
  234.     if (pUnit:IsDead() or (not pUnit:IsOnVehicle())) then
  235.         pUnit:RemoveEvents()
  236.         pUnit:RemoveFromWorld()
  237.     end
  238. end
  239.  
  240. RegisterUnitEvent(4507, 18, DummyRetardSpawn)
  241.  
  242. function GetCheckpointDist(player, cp)
  243.     local closest = -1
  244.     if (not Checkpoints[cp]) then
  245.         return -1
  246.     end
  247.     for k, v in pairs (Checkpoints[cp]) do
  248.         local dst = player:CalcToDistance(v[1], v[2], player:GetZ())
  249.         if (closest == -1 or (dst and dst < closest)) then
  250.             closest = dst
  251.         end
  252.     end
  253.     return closest
  254. end
  255.  
  256. local countdownornot = false
  257.  
  258. function CheckIfRaceCanStart()
  259.     if (Race_System.RACE_STATUS == "Looking For Racers") then
  260.         if (#Race_System.RACERS > 1) then
  261.             local i = 0
  262.             for k, v in pairs (Race_System.RACERS) do
  263.                 if (v:GetName()) then
  264.                     i = i + 1
  265.                 else
  266.                     table.remove(Race_System.RACERS, k)
  267.                 end
  268.             end
  269.             if (i > 1) then
  270.                 local k = 1
  271.                 for _, player in pairs(Race_System.RACERS) do
  272.                     if (player and player:GetName()) then
  273.                         player:Dismount()
  274.                         player:SetPlayerLock(true)
  275.                         player:Root()
  276.                         if (k > 5) then
  277.                             player:SetPlayerLock(false)
  278.                             player:Unroot()
  279.                             break
  280.                         else
  281.                             player:Teleport(1, RacePoint[k][1], RacePoint[k][2], RacePoint[k][3])
  282.                         end
  283.                         local q = player:GetValue("race_vehicle")
  284.                         if (not q) then
  285.                             q = 33062
  286.                         end
  287.                         player:SetValue("race_place", k)
  288.                         DummyRetard:RegisterEvent(function() DummyRetardAction(player, q); end, 100, 1)
  289.                         table.insert(Race_System.RACING, player)
  290.                         k = k + 1
  291.                     end
  292.                 end
  293.                 Race_System.RACE_STATUS = "Race in progress..."
  294.                 countdownornot = true
  295.                 DummyRetard:RegisterEvent("Check_PlayersIfRacing", 1000, 11)
  296.             end
  297.         end
  298.     end
  299. end
  300.  
  301. CreateLuaEvent(CheckIfRaceCanStart, 30000, 0)
  302.  
  303. function Check_PlayersIfRacing(pUnit)
  304.     if (countdownornot) then
  305.         Race_System.countdown = Race_System.countdown - 1
  306.         if (Race_System.countdown < 1) then
  307.             Race_System.countdown = 10
  308.             Race_System.started = true
  309.             countdownornot = false
  310.             Race_System.counted = true
  311.             pUnit:SendChatMessage(42, 0, "GO!")
  312.             Race_System.stime = os.clock()
  313.             for _, player in pairs (Race_System.RACING) do
  314.                 if (player:IsOnVehicle()) then
  315.                     player:Unroot()
  316.                     player:SetPlayerLock(false)
  317.                     player:GetVehicleBase():Unroot()
  318.                 end
  319.             end
  320.         elseif (Race_System.countdown < 6) then
  321.             pUnit:SendChatMessage(42, 0, tostring(Race_System.countdown))
  322.             if (Race_System.countdown == 5) then
  323.                 for _, player in pairs (Race_System.RACING) do
  324.                     if (player:IsOnVehicle()) then
  325.                         local vehicle = player:GetVehicleBase()
  326.                         local x, y, z, o = vehicle:GetLocation()
  327.                         vehicle:Teleport(1, x, y, z+0.1, o)
  328.                     end
  329.                 end
  330.             end
  331.         end
  332.     end
  333.     if (Race_System.outputwinners) then
  334.         Race_System.print_count = #Race_System.WINORDER
  335.         pUnit:RegisterEvent(printnamelistorder, 3000, (#Race_System.WINORDER))
  336.         Race_System.outputwinners = false
  337.     end
  338. end
  339.  
  340. function printnamelistorder(pUnit)
  341.     local zzplace = nil
  342.     if (Race_System.print_count == 5) then
  343.         zzplace = "5th"
  344.     elseif (Race_System.print_count == 4) then
  345.         zzplace = "4th"
  346.     elseif (Race_System.print_count == 3) then
  347.         zzplace = "3rd"
  348.     elseif (Race_System.print_count == 2) then
  349.         zzplace = "2nd"
  350.     elseif (Race_System.print_count == 1) then
  351.         zzplace = "1st"
  352.     end
  353.     local player = Race_System.WINORDER[Race_System.print_count]
  354.     pUnit:SendChatMessage(42, 0, player:GetName().." came in "..zzplace.." place!")
  355.     Race_System.print_count = Race_System.print_count - 1
  356.     if (Race_System.print_count == 0) then
  357.         Race_System.print_count = 5
  358.         Race_System.RACE_STATUS = "Looking For Racers"
  359.         Race_System.RACERS = {}
  360.         Race_System.started = false
  361.         Race_System.countdown = 10
  362.         Race_System.place = 1
  363.         Race_System.counted = false
  364.         for k, v in pairs(Race_System.RACING) do
  365.             table.remove(Race_System.RACING, k)
  366.         end
  367.         for k, v in pairs(Race_System.WINORDER) do
  368.             table.remove(Race_System.WINORDER, k)
  369.         end
  370.         for k, v in pairs (GetPlayersInWorld()) do
  371.             if (v:GetValue("race_page")) then
  372.                 v:SetValue("race_page", 0)
  373.             end
  374.         end
  375.         Race_System.SHOW = {}
  376.         player:SetValue("race_place", 0)
  377.         player:SetValue("race_point", 0)
  378.         player:SetValue("race_status", false) --false = idle, true = waiting/racing
  379.         player:SetValue("race_finished", false) --power-up target checks, bitch
  380.     end
  381. end
  382.  
  383. function CheckPoint_CheckPlace(cp)
  384.     if (Race_System.started) then
  385.         for _, player in pairs (Race_System.RACING) do
  386.             local dist = GetCheckpointDist(player, cp)
  387.             if (player:IsOnVehicle() and dist < 20) then
  388.                 local name = player:GetName()
  389.                 local point = player:GetValue("race_point")
  390.                 local nextpoint = point + 1
  391.                 if (cp < #Checkpoints) then
  392.                     if (cp > nextpoint) then
  393.                         DummyRetard:SendChatMessageToPlayer(42, 0, "You missed checkpoint "..(point+1).."!", player)
  394.                     elseif (cp == nextpoint) then
  395.                         DummyRetard:SendChatMessageToPlayer(42, 0, "You have passed checkpoint "..cp.."!", player)
  396.                         player:SetValue("race_point", cp)
  397.                     end
  398.                 elseif (cp == #Checkpoints and point == #Checkpoints - 1) then
  399.                     if (dist < 7) then
  400.                         player:SetValue("race_point", cp)
  401.                         if (Race_System.place == #Race_System.RACING) then
  402.                             DummyRetard:SendChatMessageToPlayer(42, 0, "You have come last!", player)
  403.                             player:ModValue("race_lost", 1)
  404.                             CharDBQuery("UPDATE racestats SET lost = lost + 1 WHERE name = '"..name.."';")
  405.                         elseif (Race_System.place == 1) then
  406.                             DummyRetard:SendChatMessageToPlayer(42, 0, "You have come 1st!", player)
  407.                             player:AddItem(37836,2) -- added reward
  408.                             if (player:HasQuest(6033)) then -- Daily quest
  409.                                 if (player:GetQuestObjectiveCompletion(6033, 0) == 0) then -- Daily quest
  410.                                     player:AdvanceQuestObjective(6033, 0) -- Daily quest
  411.                                 end
  412.                             end
  413.                             player:ModValue("race_won", 1)
  414.                             CharDBQuery("UPDATE racestats SET won = won + 1 WHERE name = '"..name.."';")
  415.                         elseif (Race_System.place == 2) then
  416.                             DummyRetard:SendChatMessageToPlayer(42, 0, "You have come 2nd!", player)
  417.                         elseif (Race_System.place == 3) then
  418.                             DummyRetard:SendChatMessageToPlayer(42, 0, "You have come 3rd!", player)
  419.                         elseif (Race_System.place == 4) then
  420.                             DummyRetard:SendChatMessageToPlayer(42, 0, "You have come 4th!", player)
  421.                         end
  422.                         table.insert(Race_System.WINORDER, player)
  423.                         player:SetValue("race_finished", true)
  424.                         Race_System.place = Race_System.place + 1
  425.                         if (#Race_System.WINORDER == #Race_System.RACING) then
  426.                             DummyRetard:SendChatMessage(42, 0, "All participants have reached the finish line!")
  427.                             Race_System.outputwinners = true
  428.                             Race_System.RACE_STATUS = "Announcing winners..."
  429.                         end
  430.                         player:SetValue("race_point", cp)
  431.                         local vehicle = player:GetVehicleBase()
  432.                         player:ExitVehicle()
  433.                         if (vehicle:GetEntry() == 33062) then
  434.                             local x, y, z = player:GetLocation()
  435.                             local gnome = player:GetCreatureNearestCoords(x, y, z, 33775)
  436.                             if (gnome) then
  437.                                 gnome:Despawn(1, 0)
  438.                             end
  439.                         end
  440.                         vehicle:Despawn(100, 0)
  441.                         player:MoveKnockback(-6176.9, -3886.5, -57, 6, 8)
  442.                     end
  443.                 end
  444.             end
  445.         end
  446.     end
  447. end
  448.  
  449. function renametotest()
  450.     if (Race_System.started) then
  451.         for k, player in pairs (Race_System.RACING) do
  452.             if (player == nil) then
  453.                 table.remove(Race_System.RACING, k)
  454.             elseif (player:IsOnVehicle() == false or (player:GetAreaId() ~= 2240 and player:GetAreaId() ~= 3038)) then
  455.                 local found = false
  456.                 for a, v in pairs(Race_System.WINORDER) do
  457.                     if (v:GetName() == player:GetName()) then
  458.                         found = true
  459.                         break
  460.                     end
  461.                 end
  462.                 if (not found) then
  463.                     player:SetValue("race_place", 0)
  464.                     player:SetValue("race_point", 0)
  465.                     player:SetValue("race_status", false)
  466.                     player:SetValue("race_finished", false)
  467.                     player:SetValue("race_page", 0)
  468.                     player:GetValue("race_vehicle_unit"):DismissVehicle()
  469.                     player:SetValue("race_vehicle_unit", 0)
  470.                     player:SendBroadcastMessage("You have been removed from the race for cheating!")
  471.                     --player:Teleport(0, -7482, -1249, 478) -- teleport out of the race because they are not on a vehicle (cheating)
  472.                     table.remove(Race_System.RACING, k)
  473.                 end
  474.             end
  475.         end
  476.         if (os.clock() - Race_System.stime > (60*5)) then -- race has gone on for more than 5 minutes
  477.             for _, player in pairs(Race_System.RACING) do
  478.                 if (player) then
  479.                     player:SendBroadcastMessage("The race ended since the time limit was exceeded!")
  480.                     player:SendAreaTriggerMessage("The race ended since the time limit was exceeded!")
  481.                     player:SetValue("race_place", 0)
  482.                     player:SetValue("race_point", 0)
  483.                     player:SetValue("race_status", false)
  484.                     player:SetValue("race_finished", false)
  485.                     player:SetValue("race_page", 0)
  486.                     player:SetValue("race_vehicle_unit", 0)
  487.                     if (player:IsOnVehicle()) then
  488.                         local vehicle = player:GetVehicleBase()
  489.                         player:ExitVehicle()
  490.                         if (vehicle:GetEntry() == 33062) then
  491.                             local x, y, z = player:GetLocation()
  492.                             local gnome = player:GetCreatureNearestCoords(x, y, z, 33775)
  493.                             if (gnome) then
  494.                                 gnome:Despawn(1,0)
  495.                             end
  496.                         end
  497.                         vehicle:Despawn(100,0)
  498.                     end
  499.                 end
  500.             end
  501.             Race_System.print_count = 5
  502.             Race_System.RACE_STATUS = "Looking For Racers"
  503.             Race_System.RACERS = {}
  504.             Race_System.started = false
  505.             Race_System.countdown = 10
  506.             Race_System.place = 1
  507.             Race_System.counted = false
  508.             for k, v in pairs(Race_System.RACING) do
  509.                 table.remove(Race_System.RACING, k)
  510.             end
  511.             for k, v in pairs(Race_System.WINORDER) do
  512.                 table.remove(Race_System.WINORDER, k)
  513.             end
  514.             for k, v in pairs (GetPlayersInWorld()) do
  515.                 if (v:GetValue("race_page")) then
  516.                     v:SetValue("race_page", 0)
  517.                 end
  518.             end
  519.             Race_System.SHOW = {}
  520.         end
  521.     end
  522. end
  523.  
  524. CreateLuaEvent(renametotest, 2500, 0)
  525.  
  526. function GetClosestRacer(pUnit)
  527.     local players = pUnit:GetInRangePlayers()
  528.     for k, v in pairs (players) do
  529.         if (not v or not v:IsOnVehicle() or v:GetValue("race_finished") or v:GetValue("race_status") == false) then
  530.             table.remove(players, k)
  531.         end
  532.     end
  533.     if (not players or #players == 0) then return end
  534.     for i = 1, #players do
  535.         for j = 2, #players do
  536.             if (pUnit:GetDistanceYards(players[j]) < player:GetDistanceYards(players[j-1])) then
  537.                 local temp = players[j-1]
  538.                 players[j-1] = players[j]
  539.                 players[j] = temp
  540.             end
  541.         end
  542.     end
  543.     return players[1]
  544. end
  545.  
  546. function Speed_Buff_Spawn(pUnit, Event)
  547.     pUnit:RegisterEvent("CheckForVehicleToSpeedUp", 500, 0)
  548. end
  549.  
  550. function CheckForVehicleToSpeedUp(pUnit)
  551.     local player = GetClosestRacer(pUnit)
  552.     if (player and player:GetDistanceYards(pUnit) <= 5) then
  553.         local vehicle = player:GetVehicleBase()
  554.         if (not vehicle) then return; end
  555.         vehicle:CastSpell(62299) -- speed boost
  556.         vehicle:ModifyRunSpeed(18)
  557.         --the magic of CreateLuaEvent <3 //Alexeng
  558.         CreateLuaEvent(function() if(vehicle and not player:HasAura(62299))then vehicle:ModifyRunSpeed(14);end end, 4100, 1)
  559.         pUnit:RemoveEvents()
  560.         local object = pUnit:GetLocalGameObject(179871)
  561.         if (object) then
  562.             local x, y, z = object:GetLocation()
  563.             if (pUnit:CalcToDistance(x, y, z) <= 3) then
  564.                 object:Despawn(1, 30000)
  565.                 pUnit:RegisterEvent("Speed_Buff_Spawn", 30000, 1)
  566.             end
  567.         end
  568.     end
  569. end
  570.  
  571. RegisterUnitEvent(88115, 18, "Speed_Buff_Spawn")
  572.  
  573. function Rage_Buff_Spawn(pUnit, Event)
  574.     pUnit:RegisterEvent("CheckForVehicleToAttack", 500, 0)
  575. end
  576.  
  577. function CheckForVehicleToAttack(pUnit)
  578.     local player = GetClosestRacer(pUnit)
  579.     if (player and pUnit:GetDistanceYards(player) <= 5) then
  580.         local vehicle = player:GetVehicleBase()
  581.         local name = player:GetName()
  582.         local point = player:GetValue("race_point")
  583.         if (not point) then point = 0; end
  584.         local place = player:GetValue("race_place")
  585.         if (place == Race_System.place) then
  586.             pUnit:SendChatMessageToPlayer(42, 0, "You have been hit by your own bomb!", player)
  587.             vehicle:CastSpell(71599) -- visual
  588.             vehicle:CastSpell(24647) -- stun
  589.         else
  590.             local targets = {}
  591.             for k, v in pairs (Race_System.RACING) do
  592.                 if (v and v:IsOnVehicle() and v:GetValue("race_finished") == false) then
  593.                     table.insert(targets, v)
  594.                 end
  595.             end
  596.             if (not targets or (#targets) < 2) then return; end
  597.             local temp
  598.             for i = 1, (#targets) do
  599.                 for j = 2, (#targets) do
  600.                     if (targets[j][2] < targets[j-1][2]) then
  601.                         temp = targets[j-1]
  602.                         targets[j-1] = targets[j]
  603.                         targets[j] = temp
  604.                     end
  605.                 end
  606.             end
  607.             local n = math.random(1, place) - 1
  608.             if (n == 0) then n = 1; end --First player has a higher chance of being bombed
  609.             local target = targets[n]
  610.             if (not target) then return; end
  611.             target:GetVehicleBase():CastSpell(71599) -- visual
  612.             target:GetVehicleBase():CastSpell(24647) -- stun
  613.             pUnit:SendChatMessageToPlayer(42, 0, "You have been hit by "..player:GetName().."'s bomb!", target)
  614.             pUnit:SendChatMessageToPlayer(42, 0, "You have hit "..target:GetName().." with your bomb!", player)
  615.         end
  616.         pUnit:RemoveEvents()
  617.         local object = pUnit:GetLocalGameObject(180382)
  618.         if (object) then
  619.             local x, y, z = object:GetLocation()
  620.             if (pUnit:CalcToDistance(x, y, z) <= 3) then
  621.                 object:Despawn(1, 30000)
  622.                 pUnit:RegisterEvent("Rage_Buff_Spawn", 30000, 1)
  623.             end
  624.         end
  625.     end
  626. end
  627.  
  628. RegisterUnitEvent(88116, 18, "Rage_Buff_Spawn")
  629.  
  630. function Pull_Buff_Spawn(pUnit, Event)
  631.     pUnit:RegisterEvent("CheckForPlayerToPull", 500, 0)
  632. end
  633.  
  634. function CheckForPlayerToPull(pUnit)
  635.     local player = GetClosestRacer(pUnit)
  636.     if (player and player:GetDistanceYards(pUnit) <= 5) then
  637.         local place = player:GetValue("race_place")
  638.         if (place == Race_System.place) then
  639.             pUnit:SendChatMessageToPlayer(42, 0, "Your grapple failed!", player)
  640.         else
  641.             local targets = {}
  642.             for k, v in pairs (Race_System.RACING) do
  643.                 if (v and v:IsOnVehicle() and player:GetValue("race_finished") == false and v:GetValue("race_finished") == false and player:GetDistanceYards(v) <= 40) then
  644.                     table.insert(targets, v)
  645.                 end
  646.             end
  647.             if (not targets or (#targets) < 2) then
  648.                 pUnit:SendChatMessageToPlayer(42, 0, "No target to grapple!")
  649.                 return
  650.             end
  651.             for i = 1, (#targets) do
  652.                 for j = 2, (#targets) do
  653.                     if (targets[j][2] < targets[j-1][2]) then
  654.                         temp = targets[j-1]
  655.                         targets[j-1] = targets[j]
  656.                         targets[j] = temp
  657.                     end
  658.                 end
  659.             end
  660.             local n = math.random(1, (place - 1))
  661.             local target = targets[n]
  662.             if (not target) then return; end
  663.             local x, y, z = player:GetLocation()
  664.             target:GetVehicleBase():MoveKnockback(x, y, z, 2, 3)
  665.             pUnit:SendChatMessageToPlayer(42, 0, "You have been grappled by "..player:GetName().."!", target)
  666.             pUnit:SendChatMessageToPlayer(42, 0, "You have grappled "..target:GetName().."!", player)
  667.         end
  668.         pUnit:RemoveEvents()
  669.         local object = pUnit:GetLocalGameObject(180383)
  670.         if (object) then
  671.             local x, y, z = object:GetLocation()
  672.             if (pUnit:CalcToDistance(x, y, z) <= 3) then
  673.                 object:Despawn(1, 30000)
  674.                 pUnit:RegisterEvent("Pull_Buff_Spawn", 30000, 1)
  675.             end
  676.         end
  677.     end
  678. end
  679.  
  680. RegisterUnitEvent(88117, 18, "Pull_Buff_Spawn")
  681.  
  682. -- Crowd stuff
  683.  
  684. function CHEERINGCROWD_Spawned(pUnit,Event)
  685.     pUnit:SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_NOT_SELECTABLE)
  686.     pUnit:RegisterEvent("CHEER_CROWD_CHEER", 5000, 0)
  687.     pUnit:RegisterEvent("LOOP_MUSIC_RACE", 180000, 0)
  688.     pUnit:PlaySoundToSet(16337)
  689. end
  690.  
  691. RegisterUnitEvent(477191,18, "CHEERINGCROWD_Spawned")
  692.  
  693. function LOOP_MUSIC_RACE(pUnit,Event)
  694.     pUnit:PlaySoundToSet(16337)
  695. end
  696.  
  697. function CHEER_CROWD_CHEER(pUnit,Event)
  698.     local r = math.random(1, 6)
  699.     if (r == 1) then -- alliance 1
  700.         pUnit:PlaySoundToSet(13838)
  701.     elseif (r == 2) then -- horde 1
  702.         pUnit:PlaySoundToSet(13839)
  703.     elseif (r == 3) then -- alliance 2
  704.         pUnit:PlaySoundToSet(13840)
  705.     elseif (r == 4) then -- horde 2
  706.         pUnit:PlaySoundToSet(13841)
  707.     elseif (r == 5) then -- alliance finale
  708.         pUnit:PlaySoundToSet(13843)
  709.     elseif (r == 6) then -- horde finale
  710.         pUnit:PlaySoundToSet(13844)
  711.     end
  712. end
  713.  
  714. function RIZNEK_VENDORADVERTISE(pUnit,Event)
  715.     local r = math.random(1, 3)
  716.     if (r == 1) then
  717.         pUnit:SendChatMessage(12, 0, "Get your ice cold beverages now, Almost out of stock!")
  718.     elseif (r == 2) then
  719.         pUnit:SendChatMessage(12, 0, "Ice cold beverages!")
  720.     elseif (r == 3) then
  721.         pUnit:SendChatMessage(12, 0, "Enjoy the excitement with a nice cold beverage!")
  722.     end
  723.     pUnit:RegisterEvent("RIZNEK_VENDORADVERTISE", math.random(15000, 50000), 1)
  724. end
  725.  
  726. function REZNIKADV_Spawned(pUnit,Event)
  727.     pUnit:RegisterEvent("RIZNEK_VENDORADVERTISE", math.random(20000, 30000), 1)
  728. end
  729.  
  730. RegisterUnitEvent(6495,18, "REZNIKADV_Spawned")
  731.  
  732. function RACE_BOULDERSPAWN(pUnit,Event)
  733.     pUnit:SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_NOT_SELECTABLE)
  734.     pUnit:CastSpell(55766)
  735.     pUnit:RegisterEvent("RACE_BOULDERROLE", 500, 0)
  736. end
  737.  
  738. RegisterUnitEvent(723190, 18, "RACE_BOULDERSPAWN")
  739.  
  740. function RACE_BOULDERROLE(pUnit,Event)
  741.     pUnit:CastSpell(55766)
  742.     local PlayersAllAround = pUnit:GetInRangePlayers()
  743.     for k, player in pairs (PlayersAllAround) do
  744.         if (pUnit:GetDistanceYards(player) < 4.6) then
  745.             if (player:IsOnVehicle() and player:GetValue("race_status")) then
  746.                 local vehicle = player:GetVehicleBase()
  747.                 --pUnit:CastSpellOnTarget(42299, vehicle) --as if stunning wasn't enought *rolls eyes*
  748.                 vehicle:CastSpell(24647)
  749.             end
  750.         end
  751.     end
  752. end
  753.  
  754. function RACE_UpdatePlaces()
  755.     if (Race_System.started) then
  756.         if (Race_System.RACING and #Race_System.RACING == 0) then
  757.             Race_System.print_count = 5
  758.             Race_System.RACE_STATUS = "Looking For Racers"
  759.             Race_System.RACERS = {}
  760.             Race_System.started = false
  761.             Race_System.countdown = 10
  762.             Race_System.place = 1
  763.             Race_System.counted = false
  764.             for k, v in pairs(Race_System.RACING) do
  765.                 table.remove(Race_System.RACING, k)
  766.             end
  767.             for k, v in pairs(Race_System.WINORDER) do
  768.                 table.remove(Race_System.WINORDER, k)
  769.             end
  770.             for k, v in pairs (GetPlayersInWorld()) do
  771.                 if (v:GetValue("race_page")) then
  772.                     v:SetValue("race_page", 0)
  773.                 end
  774.             end
  775.             Race_System.SHOW = {}
  776.             return
  777.         end
  778.         local racers = {}
  779.         for k, v in pairs (Race_System.RACING) do
  780.             if (v and v:IsOnVehicle()) then
  781.                 local point = v:GetValue("race_point") + 1
  782.                 local dist = -1
  783.                 if (point == #Checkpoints) then
  784.                     dist = GetCheckpointDist(v, point)
  785.                 else
  786.                     dist = GetCheckpointDist(v, point) + GetCheckpointDist(v, point + 1)
  787.                 end
  788.                 if (v:GetAreaId() ~= 2240 and v:GetAreaId() ~= 3038) then --cheaters
  789.                     v:SetValue("race_place", 0)
  790.                     v:SetValue("race_point", 0)
  791.                     v:SetValue("race_status", false)
  792.                     v:SetValue("race_finished", false)
  793.                     v:SetValue("race_page", 0)
  794.                     v:SetValue("race_vehicle_unit", 0)
  795.                     v:GetVehicleBase():DismissVehicle()
  796.                     v:SendBroadcastMessage("You have been removed from the race for cheating!")
  797.                     v:Teleport(0, -7482, -1249, 478) -- teleport out of the race because they are not on a vehicle (cheating)
  798.                     table.remove(Race_System.RACING, k)
  799.                 else
  800.                     racers[k] = {point, dist, v}
  801.                 end
  802.             end
  803.         end
  804.         --Sort by distance to next checkpoint first, then sort by checkpoint.
  805.         for i = 1, #racers do
  806.             for j = 2, #racers do
  807.                 if (racers[j][2] < racers[j-1][2]) then
  808.                     local temp = racers[j-1]
  809.                     racers[j-1] = racers[j]
  810.                     racers[j] = temp
  811.                 end
  812.             end
  813.         end
  814.         for i = 1, #racers do
  815.             for j = 2, #racers do
  816.                 if (racers[j][1] > racers[j-1][1]) then
  817.                     local temp = racers[j-1]
  818.                     racers[j-1] = racers[j]
  819.                     racers[j] = temp
  820.                 end
  821.             end
  822.         end
  823.         local place = 1
  824.         for k, v in pairs (racers) do
  825.             v[3]:SetValue("race_place", place)
  826.             place = place + 1
  827.         end
  828.     end
  829. end
  830.  
  831. CreateLuaEvent(RACE_UpdatePlaces, 1000, 0)
Advertisement
Add Comment
Please, Sign In to add comment