stoneharry

Untitled

Jan 27th, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.16 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.  
  5. local Race_System = {
  6.     RACE_STATUS = "Looking For Racers",
  7.     RACERS = {},
  8.     WON = {},
  9.     LOST = {},
  10.     PAGE = {},
  11.     SHOW = {},
  12.     CURRENT = {},
  13.     started = false,
  14.     countdown = 10,
  15.     RACING = {}
  16. }
  17.  
  18. local VAR = {}
  19.  
  20. function PageCounter(page)
  21.     if page == 0 then
  22.         return 1
  23.     else
  24.         return (page*10)+1
  25.     end
  26. end
  27.  
  28. function DisplayRacesWon(id, name) -- integrate with DB support
  29.     local query = CharDBQuery("SELECT `won` FROM `racestats` WHERE `name` = '"..name.."';")
  30.     if query ~= nil then
  31.         return query:GetColumn(0):GetString()
  32.     else
  33.         return 0
  34.     end
  35. end
  36.  
  37. function DisplayRacesLost(id, name) -- integrate with DB support
  38.     local query = CharDBQuery("SELECT `lost` FROM `racestats` WHERE `name` = '"..name.."';")
  39.     if query ~= nil then
  40.         return query:GetColumn(0):GetString()
  41.     else
  42.         return 0
  43.     end
  44. end
  45.  
  46. function ReturnPageLength(PAGE_COUNTER, TABLE_LENGTH)
  47.     if (TABLE_LENGTH - PAGE_COUNTER) > 10 then
  48.         return PAGE_COUNTER+10;
  49.     else
  50.         return TABLE_LENGTH;
  51.     end
  52. end
  53.  
  54. function Test_Item_Trigger(item, event, player)
  55.     Test_Item(item, player)
  56. end
  57.  
  58. function Test_Item(item, player, intid, code)
  59.     if Race_System.PAGE[player:GetName()] == nil then
  60.         Race_System.PAGE[player:GetName()] = 0
  61.     end
  62.    
  63.     item:GossipCreateMenu(50, player, 0)
  64.     item:GossipMenuAddItem(4, "|cff3060B5Current status:|r "..Race_System.RACE_STATUS, 1, 0)
  65.     if Race_System.RACE_STATUS == "Looking For Racers" then
  66.         item:GossipMenuAddItem(9, "Sign me up for this race!", 2, 0)
  67.     end
  68.     if #Race_System.RACERS ~= 0 then
  69.         for i = PageCounter(Race_System.PAGE[player:GetName()]), ReturnPageLength(PageCounter(Race_System.PAGE[player:GetName()])-1, #Race_System.RACERS) do
  70.             if Race_System.RACERS[i]:GetName() then
  71.                 item:GossipMenuAddItem(4, "   > Racer "..i..": "..Race_System.RACERS[i]:GetName(), 100+i, 0)
  72.                 if (intid == i+100) and (Race_System.SHOW[intid] == true) then
  73.                     item:GossipMenuAddItem(4, "|cff095F0B     > Races won: |r"..DisplayRacesWon(i, player:GetName()), 100+i, 0)
  74.                     item:GossipMenuAddItem(4, "|cFFFF0000     > Races lost: |r"..DisplayRacesLost(i, player:GetName()), 100+i, 0)
  75.                 end
  76.             end
  77.             if tostring(i/10):find('%.') == nil and #Race_System.RACERS > i then
  78.                 item:GossipMenuAddItem(7, "Next page.", 4, 0)
  79.             end
  80.             if Race_System.PAGE[player:GetName()] ~= 0 and (i == ReturnPageLength(PageCounter(Race_System.PAGE[player:GetName()])-1, #Race_System.RACERS)) then
  81.                 item:GossipMenuAddItem(7, "Previous page.", 5, 0)
  82.             end
  83.         end
  84.     end
  85.     item:GossipMenuAddItem(0, "Nevermind.", 99, 0)
  86.     item:GossipSendMenu(player)
  87. end
  88.  
  89. function Test_Item_Select(item, event, player, id, intid, code)
  90.     if (intid == 1) then
  91.         Test_Item(item, player)
  92.     end
  93.    
  94.     if (intid == 2) then
  95.         local PLAYER_MATCH = 0
  96.         for k,v in pairs(Race_System.RACERS) do
  97.             if v:GetName() == player:GetName() then
  98.                 PLAYER_MATCH = 1
  99.                 break;
  100.             end
  101.         end
  102.         if PLAYER_MATCH == 0 then
  103.                 table.insert(Race_System.RACERS, player)
  104.         else
  105.             player:SendBroadcastMessage("You've already been signed up!")
  106.         end
  107.         Test_Item(item, player)
  108.     end
  109.    
  110.     if (intid == 4) then
  111.         Race_System.PAGE[player:GetName()] = Race_System.PAGE[player:GetName()] + 1
  112.         Test_Item(item, player)
  113.     end
  114.    
  115.     if (intid == 5) then
  116.         Race_System.PAGE[player:GetName()] = Race_System.PAGE[player:GetName()] - 1
  117.         Test_Item(item, player)
  118.     end
  119.    
  120.     if (intid == 99) then
  121.         player:GossipComplete()
  122.     end
  123.    
  124.     for i = 101, (#Race_System.RACERS+100) do
  125.         if (intid == i) then
  126.             if Race_System.SHOW[intid] == true then
  127.                 Race_System.SHOW[intid] = false
  128.             else
  129.                 Race_System.SHOW[intid] = true
  130.             end
  131.             Test_Item(item, player, intid)
  132.         end
  133.     end
  134. end
  135.  
  136. RegisterItemGossipEvent(57060, 1, "Test_Item_Trigger")
  137. RegisterItemGossipEvent(57060, 2, "Test_Item_Select")
  138.  
  139. function CheckIfRaceCanStart()
  140.     if Race_System.RACE_STATUS == "Looking For Racers" then
  141.         if #Race_System.RACERS > 1 then
  142.             local i = 0
  143.             for k,plr in pairs(Race_System.RACERS) do
  144.                 if plr == nil then
  145.                     --table.remove(Race_System.RACERS, plr) -- bugs script
  146.                 elseif plr:GetName() then
  147.                     i = i + 1
  148.                 end
  149.             end
  150.             if i > 1 then
  151.                 local k = 1
  152.                 for _,plr in pairs(Race_System.RACERS) do
  153.                     if plr ~= nil and plr:GetName() then
  154.                         plr:Dismount()
  155.                         plr:SetPlayerLock(true)
  156.                         plr:Root()
  157.                         if k > 5 then
  158.                             plr:SetPlayerLock(false)
  159.                             plr:Unroot()
  160.                             break
  161.                         elseif k == 1 then
  162.                             plr:Teleport(1, -6204, -3911, -60.32)
  163.                         elseif k == 2 then
  164.                             plr:Teleport(1, -6204, -3907, -60.32)
  165.                         elseif k == 3 then
  166.                             plr:Teleport(1, -6204, -3903,  -60.32)
  167.                         elseif k == 4 then
  168.                             plr:Teleport(1, -6202, -3898,  -60.32)
  169.                         elseif k == 5 then
  170.                             plr:Teleport(1, -6204, -3894,  -60.32)
  171.                         end
  172.                         k = k + 1
  173.                         table.insert(Race_System.CURRENT, plr)
  174.                     end
  175.                 end
  176.                 RegisterTimedEvent("SetStatusOncePlayersLoaded", 5000, 1)
  177.                 -- spawning a vehicle in a timed event = crash
  178.             end
  179.         end
  180.     end
  181. end
  182.  
  183. function SetStatusOncePlayersLoaded()
  184.     Race_System.RACE_STATUS = "Race in progress..."
  185. end
  186.  
  187. RegisterTimedEvent("CheckIfRaceCanStart", 10000, 0)
  188.  
  189. function Race_Creature_Handler_Spawn(pUnit, Event)
  190.     pUnit:RegisterEvent("Check_PlayersIfRacing", 1000, 0)
  191. end
  192.  
  193. local countdown = false
  194.  
  195. function Check_PlayersIfRacing(pUnit)
  196.     if countdown then
  197.         Race_System.countdown = Race_System.countdown - 1
  198.         if Race_System.countdown < 1 then
  199.             Race_System.countdown = 10
  200.             Race_System.started = true
  201.             countdown = false
  202.             pUnit:SendChatMessage(42,0,"GO!")
  203.         elseif Race_System.countdown < 6 then
  204.             pUnit:SendChatMessage(42,0,tostring(Race_System.countdown))
  205.         end
  206.     end
  207.     for _,plr in pairs(pUnit:GetInRangePlayers()) do
  208.         if plr ~= nil then
  209.             if Race_System.RACE_STATUS == "Race in progress..." then
  210.                 countdown = true
  211.                 if not plr:IsOnVehicle() then
  212.                     local name = plr:GetName()
  213.                     local istrue = false
  214.                     local position = 0
  215.                     for k,two in pairs(Race_System.CURRENT) do
  216.                         if two:GetName() == name then
  217.                             istrue = true
  218.                             position = k
  219.                             break
  220.                         end
  221.                     end
  222.                     if istrue then
  223.                         local query = CharDBQuery("SELECT `vehicle` FROM `racestats` WHERE `name` = '"..name.."';")
  224.                         if query == nil then
  225.                             CharDBQuery("INSERT INTO `racestats` VALUES ('"..name.."', 0, 0, 33062);")
  226.                             query = "33062"
  227.                         else
  228.                             query = query:GetColumn(0):GetString()
  229.                         end
  230.                         if tonumber(query) == 33062 then
  231.                             plr:SpawnCreature(33775, plr:GetX(), plr:GetY(), plr:GetZ(), plr:GetO(), 35, 0)
  232.                         end
  233.                         plr:SpawnAndEnterVehicle(tonumber(query), 1000)
  234.                         table.remove(Race_System.CURRENT, position)
  235.                         table.insert(Race_System.RACING, plr)
  236.                     end
  237.                 elseif plr:GetVehicleBase():GetEntry() == 33062 and plr:HasEmptyVehicleSeat() then
  238.                     local unit = plr:GetCreatureNearestCoords(plr:GetX(), plr:GetY(), plr:GetZ(), 33775)
  239.                     if unit ~= nil and not unit:IsOnVehicle() then
  240.                         if plr:IsOnVehicle() then
  241.                             unit:SetUInt32Value(UNIT_FIELD_FLAGS, 10) -- look like a player
  242.                             unit:EnterVehicle(plr:GetVehicleBase(), 1000)
  243.                         end
  244.                     end
  245.                 end
  246.                 if not Race_System.started then
  247.                     if plr:IsOnVehicle() then
  248.                         plr:GetVehicleBase():Root()
  249.                     end
  250.                 else
  251.                     if plr:IsOnVehicle() then
  252.                         plr:Unroot()
  253.                         plr:SetPlayerLock(false)
  254.                         plr:GetVehicleBase():Unroot()
  255.                     end
  256.                 end
  257.             end
  258.         end
  259.     end
  260. end
  261.  
  262. RegisterUnitEvent(33340, 18, "Race_Creature_Handler_Spawn")
  263.  
  264. -- 13 checkpoints
  265. -- ID's: 88101 to 88113
  266.  
  267. function CheckPointSpawner_Spawned(pUnit, Event)
  268.    
  269. end
  270.  
  271. RegisterUnitEvent(88101, 18, "CheckPointSpawner_Spawned")
  272.  
  273. function Stop_People_ExploitingDuringRace()
  274.     if Race_System.started then
  275.         for k,plr in pairs(Race_System.CURRENT) do
  276.             if plr == nil then
  277.                 table.remove(Race_System.CURRENT, k)
  278.             elseif not plr:IsOnVehicle() then
  279.                 table.remove(Race_System.CURRENT, k)
  280.                 plr:SendBroadcastMessage("You have been removed from the race for cheating!")
  281.                 plr:Teleport(0, 0, 0, 0) -- teleport out of the race because they are not on a vehicle (cheating)
  282.             end
  283.         end
  284.     end
  285. end
  286.  
  287. RegisterTimedEvent("Stop_People_ExploitingDuringRace", 2500, 0)
Advertisement
Add Comment
Please, Sign In to add comment