stoneharry

Untitled

Jan 27th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.14 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.     place = 1,
  16.     RACING = {},
  17.     WINORDER = {},
  18.     print_count = 0,
  19.     counted,
  20.     outputwinners = false
  21. }
  22.  
  23. local VAR = {}
  24.  
  25. function PageCounter(page)
  26.     if page == 0 then
  27.         return 1
  28.     else
  29.         return (page*10)+1
  30.     end
  31. end
  32.  
  33. function DisplayRacesWon(name)
  34.     local query = CharDBQuery("SELECT `won` FROM `racestats` WHERE `name` = '"..name.."';")
  35.     if query ~= nil then
  36.         return query:GetColumn(0):GetString()
  37.     else
  38.         return 0
  39.     end
  40. end
  41.  
  42. function DisplayRacesLost(name)
  43.     local query = CharDBQuery("SELECT `lost` FROM `racestats` WHERE `name` = '"..name.."';")
  44.     if query ~= nil then
  45.         return query:GetColumn(0):GetString()
  46.     else
  47.         return 0
  48.     end
  49. end
  50.  
  51. function ReturnPageLength(PAGE_COUNTER, TABLE_LENGTH)
  52.     if (TABLE_LENGTH - PAGE_COUNTER) > 10 then
  53.         return PAGE_COUNTER+10;
  54.     else
  55.         return TABLE_LENGTH;
  56.     end
  57. end
  58.  
  59. function Test_Item_Trigger(item, event, player)
  60.     Test_Item(item, player)
  61. end
  62.  
  63. function Test_Item(item, player, intid, code)
  64.     if Race_System.PAGE[player:GetName()] == nil then
  65.         Race_System.PAGE[player:GetName()] = 0
  66.     end
  67.    
  68.     item:GossipCreateMenu(50, player, 0)
  69.     item:GossipMenuAddItem(4, "|cff3060B5Current status:|r "..Race_System.RACE_STATUS, 1, 0)
  70.     if Race_System.RACE_STATUS == "Looking For Racers" then
  71.         item:GossipMenuAddItem(9, "Sign me up for this race!", 2, 0)
  72.     end
  73.     if #Race_System.RACERS ~= 0 then
  74.         for i = PageCounter(Race_System.PAGE[player:GetName()]), ReturnPageLength(PageCounter(Race_System.PAGE[player:GetName()])-1, #Race_System.RACERS) do
  75.             if Race_System.RACERS[i]:GetName() then
  76.                 item:GossipMenuAddItem(4, "   > Racer "..i..": "..Race_System.RACERS[i]:GetName(), 100+i, 0)
  77.                 if (intid == i+100) and (Race_System.SHOW[intid] == true) then
  78.                     item:GossipMenuAddItem(4, "|cff095F0B     > Races won: |r"..DisplayRacesWon(Race_System.RACERS[i]:GetName()), 100+i, 0)
  79.                     item:GossipMenuAddItem(4, "|cFFFF0000     > Races lost: |r"..DisplayRacesLost(Race_System.RACERS[i]:GetName()), 100+i, 0)
  80.                 end
  81.             end
  82.             if tostring(i/10):find('%.') == nil and #Race_System.RACERS > i then
  83.                 item:GossipMenuAddItem(7, "Next page.", 4, 0)
  84.             end
  85.             if Race_System.PAGE[player:GetName()] ~= 0 and (i == ReturnPageLength(PageCounter(Race_System.PAGE[player:GetName()])-1, #Race_System.RACERS)) then
  86.                 item:GossipMenuAddItem(7, "Previous page.", 5, 0)
  87.             end
  88.         end
  89.     end
  90.     item:GossipMenuAddItem(0, "Nevermind.", 99, 0)
  91.     item:GossipSendMenu(player)
  92. end
  93.  
  94. function Test_Item_Select(item, event, player, id, intid, code)
  95.     if (intid == 1) then
  96.         Test_Item(item, player)
  97.     end
  98.    
  99.     if (intid == 2) then
  100.         local PLAYER_MATCH = 0
  101.         for k,v in pairs(Race_System.RACERS) do
  102.             if v:GetName() == player:GetName() then
  103.                 PLAYER_MATCH = 1
  104.                 break;
  105.             end
  106.         end
  107.         if PLAYER_MATCH == 0 then
  108.                 table.insert(Race_System.RACERS, player)
  109.         else
  110.             player:SendBroadcastMessage("You've already been signed up!")
  111.         end
  112.         Test_Item(item, player)
  113.     end
  114.    
  115.     if (intid == 4) then
  116.         Race_System.PAGE[player:GetName()] = Race_System.PAGE[player:GetName()] + 1
  117.         Test_Item(item, player)
  118.     end
  119.    
  120.     if (intid == 5) then
  121.         Race_System.PAGE[player:GetName()] = Race_System.PAGE[player:GetName()] - 1
  122.         Test_Item(item, player)
  123.     end
  124.    
  125.     if (intid == 99) then
  126.         player:GossipComplete()
  127.     end
  128.    
  129.     for i = 101, (#Race_System.RACERS+100) do
  130.         if (intid == i) then
  131.             if Race_System.SHOW[intid] == true then
  132.                 Race_System.SHOW[intid] = false
  133.             else
  134.                 Race_System.SHOW[intid] = true
  135.             end
  136.             Test_Item(item, player, intid)
  137.         end
  138.     end
  139. end
  140.  
  141. RegisterItemGossipEvent(57060, 1, "Test_Item_Trigger")
  142. RegisterItemGossipEvent(57060, 2, "Test_Item_Select")
  143.  
  144. function CheckIfRaceCanStart()
  145.     if Race_System.RACE_STATUS == "Looking For Racers" then
  146.         if #Race_System.RACERS > 1 then
  147.             local i = 0
  148.             for k,plr in pairs(Race_System.RACERS) do
  149.                 if plr == nil then
  150.                     --table.remove(Race_System.RACERS, plr) -- bugs script
  151.                 elseif plr:GetName() then
  152.                     i = i + 1
  153.                 end
  154.             end
  155.             if i > 1 then
  156.                 local k = 1
  157.                 for _,plr in pairs(Race_System.RACERS) do
  158.                     if plr ~= nil and plr:GetName() then
  159.                         plr:Dismount()
  160.                         plr:SetPlayerLock(true)
  161.                         plr:Root()
  162.                         if k > 5 then
  163.                             plr:SetPlayerLock(false)
  164.                             plr:Unroot()
  165.                             break
  166.                         elseif k == 1 then
  167.                             plr:Teleport(1, -6204, -3911, -60.32)
  168.                         elseif k == 2 then
  169.                             plr:Teleport(1, -6204, -3907, -60.32)
  170.                         elseif k == 3 then
  171.                             plr:Teleport(1, -6204, -3903,  -60.32)
  172.                         elseif k == 4 then
  173.                             plr:Teleport(1, -6202, -3898,  -60.32)
  174.                         elseif k == 5 then
  175.                             plr:Teleport(1, -6204, -3894,  -60.32)
  176.                         end
  177.                         k = k + 1
  178.                         table.insert(Race_System.CURRENT, plr)
  179.                     end
  180.                 end
  181.                 RegisterTimedEvent("SetStatusOncePlayersLoaded", 5000, 1)
  182.                 -- spawning a vehicle in a timed event = crash
  183.             end
  184.         end
  185.     end
  186. end
  187.  
  188. function SetStatusOncePlayersLoaded()
  189.     Race_System.RACE_STATUS = "Race in progress..."
  190. end
  191.  
  192. RegisterTimedEvent("CheckIfRaceCanStart", 10000, 0)
  193.  
  194. function Race_Creature_Handler_Spawn(pUnit, Event)
  195.     pUnit:RegisterEvent("Check_PlayersIfRacing", 1000, 0)
  196. end
  197.  
  198. local countdown = false
  199.  
  200. function Check_PlayersIfRacing(pUnit)
  201.     if countdown then
  202.         Race_System.countdown = Race_System.countdown - 1
  203.         if Race_System.countdown < 1 then
  204.             Race_System.countdown = 10
  205.             Race_System.started = true
  206.             countdown = false
  207.             Race_System.counted = true
  208.             pUnit:SendChatMessage(42,0,"GO!")
  209.             for _,plr in pairs(pUnit:GetInRangePlayers()) do
  210.                 if plr:IsOnVehicle() then
  211.                     plr:Unroot()
  212.                     plr:SetPlayerLock(false)
  213.                     plr:GetVehicleBase():Unroot()
  214.                 end
  215.             end
  216.         elseif Race_System.countdown < 6 then
  217.             pUnit:SendChatMessage(42,0,tostring(Race_System.countdown))
  218.             if Race_System.countdown == 5 then
  219.                 for _,plr in pairs(pUnit:GetInRangePlayers()) do
  220.                     if plr:IsOnVehicle() then
  221.                         local vehicle = plr:GetVehicleBase()
  222.                         vehicle:SetPosition(vehicle:GetX(), vehicle:GetY(), vehicle:GetZ()+0.1, vehicle:GetO())
  223.                     end
  224.                 end
  225.             end
  226.         end
  227.     end
  228.     for _,plr in pairs(pUnit:GetInRangePlayers()) do
  229.         if plr ~= nil then
  230.             if Race_System.RACE_STATUS == "Race in progress..." and not Race_System.counted then
  231.                 countdown = true
  232.                 if not plr:IsOnVehicle() then
  233.                     local name = plr:GetName()
  234.                     local istrue = false
  235.                     local position = 0
  236.                     for k,two in pairs(Race_System.CURRENT) do
  237.                         if two:GetName() == name then
  238.                             istrue = true
  239.                             position = k
  240.                             break
  241.                         end
  242.                     end
  243.                     if istrue then
  244.                         local query = CharDBQuery("SELECT `vehicle` FROM `racestats` WHERE `name` = '"..name.."';")
  245.                         if query == nil then
  246.                             CharDBQuery("INSERT INTO `racestats` VALUES ('"..name.."', 0, 0, 33062);")
  247.                             query = "33062"
  248.                         else
  249.                             query = query:GetColumn(0):GetString()
  250.                         end
  251.                         if tonumber(query) == 33062 then
  252.                             plr:SpawnCreature(33775, plr:GetX(), plr:GetY(), plr:GetZ(), plr:GetO(), 35, 0)
  253.                         end
  254.                         plr:SpawnAndEnterVehicle(tonumber(query), 1000)
  255.                         table.remove(Race_System.CURRENT, position)
  256.                         table.insert(Race_System.RACING, plr)
  257.                     end
  258.                 elseif plr:GetVehicleBase():GetEntry() == 33062 and plr:HasEmptyVehicleSeat() then
  259.                     local unit = plr:GetCreatureNearestCoords(plr:GetX(), plr:GetY(), plr:GetZ(), 33775)
  260.                     if unit ~= nil and not unit:IsOnVehicle() then
  261.                         if plr:IsOnVehicle() then
  262.                             unit:SetUInt32Value(UNIT_FIELD_FLAGS, 10) -- look like a player
  263.                             unit:EnterVehicle(plr:GetVehicleBase(), 1000)
  264.                         end
  265.                     end
  266.                 end
  267.                 if not Race_System.started then
  268.                     if plr:IsOnVehicle() then
  269.                         plr:GetVehicleBase():Root()
  270.                     end
  271.                 --[[else
  272.                     if plr:IsOnVehicle() then
  273.                         plr:Unroot()
  274.                         plr:SetPlayerLock(false)
  275.                         plr:GetVehicleBase():Unroot()
  276.                     end]]
  277.                 end
  278.             end
  279.         end
  280.     end
  281.     if Race_System.outputwinners then
  282.         Race_System.print_count = #Race_System.WINORDER
  283.         for i = 1, #Race_System.WINORDER do
  284.             pUnit:RegisterEvent("printnamelistorder", 3000 * i, 1)
  285.         end
  286.         Race_System.outputwinners = false
  287.     end
  288. end
  289.  
  290. function printnamelistorder(pUnit, plr)
  291.     local zzplace = nil
  292.     if Race_System.print_count == 5 then
  293.         zzplace = "5th"
  294.     elseif Race_System.print_count == 4 then
  295.         zzplace = "4th"
  296.     elseif Race_System.print_count == 3 then
  297.         zzplace = "3rd"
  298.     elseif Race_System.print_count == 2 then
  299.         zzplace = "2nd"
  300.     elseif Race_System.print_count == 1 then
  301.         zzplace = "1st"
  302.     end
  303.     local plr = Race_System.WINORDER[Race_System.print_count]
  304.     pUnit:SendChatMessage(42,0,plr:GetName().." came in "..zzplace.." place!")
  305.     Race_System.print_count = Race_System.print_count - 1
  306.     if Race_System.print_count == 0 then
  307.         Race_System.print_count = 5
  308.         Race_System.RACE_STATUS = "Looking For Racers"
  309.         for k,v in pairs(Race_System.RACERS) do
  310.             table.remove(Race_System.RACERS, k)
  311.         end
  312.         for k,v in pairs(Race_System.CURRENT) do
  313.             table.remove(Race_System.CURRENT, k)
  314.         end
  315.         Race_System.started = false
  316.         Race_System.countdown = 10
  317.         Race_System.place = 1
  318.         for k,v in pairs(Race_System.RACING) do
  319.             table.remove(Race_System.RACING, k)
  320.         end
  321.         for k,v in pairs(Race_System.WINORDER) do
  322.             table.remove(Race_System.WINORDER, k)
  323.         end
  324.         for k,v in pairs(Race_System.PAGE) do
  325.             table.remove(Race_System.PAGE, k)
  326.         end
  327.         for k,v in pairs(Race_System.SHOW) do
  328.             table.remove(Race_System.SHOW)
  329.         end
  330.     end
  331. end
  332.  
  333. RegisterUnitEvent(33340, 18, "Race_Creature_Handler_Spawn")
  334.  
  335. function CheckPointSpawner_Spawned(pUnit, Event)
  336.     pUnit:RegisterEvent("CheckPoint_CheckPlace", 1000, 0)
  337. end
  338.  
  339. function CheckPoint_CheckPlace(pUnit)
  340.     if Race_System.started then
  341.         for _,plr in pairs(pUnit:GetInRangePlayers()) do
  342.             --if plr:IsOnVehicle() then
  343.                 if pUnit:GetDistanceYards(plr) < 18 then
  344.                     local entry = pUnit:GetEntry()
  345.                     local name = plr:GetName()
  346.                     Race_System[name] = Race_System[name] or {VAR={}}
  347.                     if Race_System[name].VAR.point == nil then
  348.                         Race_System[name].VAR.point = 0
  349.                     end
  350.                     if entry == 88101 and Race_System[name].VAR.point == 0 then
  351.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 1!", plr)
  352.                         Race_System[name].VAR.point = 1
  353.                     elseif entry == 88102  and Race_System[name].VAR.point == 1 then
  354.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 2!", plr)
  355.                         Race_System[name].VAR.point = 2
  356.                     elseif entry == 88103  and Race_System[name].VAR.point == 2 then
  357.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 3!", plr)
  358.                         Race_System[name].VAR.point = 3
  359.                     elseif entry == 88104  and Race_System[name].VAR.point == 3 then
  360.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 4!", plr)
  361.                         Race_System[name].VAR.point = 4
  362.                     elseif entry == 88105  and Race_System[name].VAR.point == 4 then
  363.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 5!", plr)
  364.                         Race_System[name].VAR.point = 5
  365.                     elseif entry == 88106  and Race_System[name].VAR.point == 5 then
  366.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 6!", plr)
  367.                         Race_System[name].VAR.point = 6
  368.                     elseif entry == 88107  and Race_System[name].VAR.point == 6 then
  369.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 7!", plr)
  370.                         Race_System[name].VAR.point = 7
  371.                     elseif entry == 88108  and Race_System[name].VAR.point == 7 then
  372.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 8!", plr)
  373.                         Race_System[name].VAR.point = 8
  374.                     elseif entry == 88109  and Race_System[name].VAR.point == 8 then
  375.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 9!", plr)
  376.                         Race_System[name].VAR.point = 9
  377.                     elseif entry == 88110  and Race_System[name].VAR.point == 9 then
  378.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 10!", plr)
  379.                         Race_System[name].VAR.point = 10   
  380.                     elseif entry == 88111  and Race_System[name].VAR.point == 10 then
  381.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 11!", plr)
  382.                         Race_System[name].VAR.point = 11
  383.                     elseif entry == 88112  and Race_System[name].VAR.point == 11 then
  384.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 12!", plr)
  385.                         Race_System[name].VAR.point = 12
  386.                     elseif entry == 88113  and Race_System[name].VAR.point == 12 then
  387.                         pUnit:SendChatMessageToPlayer(42, 0, "You have passed checkpoint 13!", plr)
  388.                         Race_System[name].VAR.point = 13
  389.                     elseif entry == 88114  and Race_System[name].VAR.point == 13 then
  390.                         if pUnit:GetDistanceYards(plr) < 5 then
  391.                             if Race_System.place == 1 then
  392.                                 pUnit:SendChatMessageToPlayer(42, 0, "You have come 1st!", plr)
  393.                                 CharDBQuery("UPDATE `racestats` SET `won` = won + '1' WHERE `name` = '"..plr:GetName().."';")
  394.                             elseif Race_System.place == 2 then
  395.                                 if #Race_System.RACING == 2 then
  396.                                     pUnit:SendChatMessageToPlayer(42, 0, "You have come last!", plr)
  397.                                     CharDBQuery("UPDATE `racestats` SET `lost` = lost + '1' WHERE `name` = '"..plr:GetName().."';")
  398.                                 else
  399.                                     pUnit:SendChatMessageToPlayer(42, 0, "You have come 2nd!", plr)
  400.                                 end
  401.                             elseif Race_System.place == 3 then
  402.                                 if #Race_System.RACING == 3 then
  403.                                     pUnit:SendChatMessageToPlayer(42, 0, "You have come last!", plr)
  404.                                     CharDBQuery("UPDATE `racestats` SET `lost` = lost + '1' WHERE `name` = '"..plr:GetName().."';")
  405.                                 else
  406.                                     pUnit:SendChatMessageToPlayer(42, 0, "You have come 3rd!", plr)
  407.                                 end
  408.                             elseif Race_System.place == 4 then
  409.                                 if #Race_System.RACING == 4 then
  410.                                     pUnit:SendChatMessageToPlayer(42, 0, "You have come last!", plr)
  411.                                     CharDBQuery("UPDATE `racestats` SET `lost` = lost + '1' WHERE `name` = '"..plr:GetName().."';")
  412.                                 else
  413.                                     pUnit:SendChatMessageToPlayer(42, 0, "You have come 4th!", plr)
  414.                                 end
  415.                             elseif Race_System.place == 5 then
  416.                                 pUnit:SendChatMessageToPlayer(42, 0, "You have come last!", plr)
  417.                                 CharDBQuery("UPDATE `racestats` SET `lost` = lost + '1' WHERE `name` = '"..plr:GetName().."';")
  418.                             end
  419.                             table.insert(Race_System.WINORDER, plr)
  420.                             Race_System.place = Race_System.place + 1
  421.                             if #Race_System.WINORDER == #Race_System.RACING then
  422.                                 pUnit:SendChatMessage(42,0,"All participants have reached the finish line!")
  423.                                 Race_System.outputwinners = true
  424.                                 Race_System.RACE_STATUS = "Announcing winners..."
  425.                             end
  426.                             Race_System[name].VAR.point = 14
  427.                             --[[for k,v in pairs(Race_System.RACING) do
  428.                                 if v == nil then
  429.                                     table.remove(Race_System.RACING, k)
  430.                                 elseif v:GetName() == plr:GetName() then
  431.                                     table.remove(Race_System.RACING, k)
  432.                                 end
  433.                             end]]
  434.                             plr:ExitVehicle()
  435.                             local query = CharDBQuery("SELECT `vehicle` FROM `racestats` WHERE `name` = '"..name.."';")
  436.                             if query ~= nil then
  437.                                 local vehicle = tonumber(query:GetColumn(0):GetString())
  438.                                 if vehicle == 33062 then
  439.                                     plr:GetCreatureNearestCoords(plr:GetX(),plr:GetY(),plr:GetZ(),33775):Despawn(1,0)
  440.                                 end
  441.                                 plr:GetCreatureNearestCoords(plr:GetX(),plr:GetY(),plr:GetZ(),vehicle):Despawn(100,0)
  442.                             end
  443.                             plr:MoveKnockback(-6176.9, -3886.5, -57, 6, 8)
  444.                         end
  445.                     end
  446.                 end
  447.             --end
  448.         end
  449.     end
  450. end
  451.  
  452. for i=1, 14 do
  453.     RegisterUnitEvent(88100+i, 18, "CheckPointSpawner_Spawned")
  454. end
  455.  
  456. function exploiters_go_die()
  457.     if Race_System.started then
  458.         for k,plr in pairs(Race_System.RACING) do
  459.             if plr == nil then
  460.                 table.remove(Race_System.RACING, k)
  461.             elseif not plr:IsOnVehicle() then
  462.                 local found = false
  463.                 for k,v in pairs(Race_System.WINORDER) do
  464.                     if v:GetName() == plr:GetName() then
  465.                         found = true
  466.                         break
  467.                     end
  468.                 end
  469.                     if not found then
  470.                     table.remove(Race_System.RACING, k)
  471.                     plr:SendBroadcastMessage("You have been removed from the race for cheating!")
  472.                     plr:Teleport(0, 0, 0, 0) -- teleport out of the race because they are not on a vehicle (cheating)
  473.                 end
  474.             end
  475.         end
  476.     end
  477. end
  478.  
  479. --RegisterTimedEvent("exploiters_go_die", 2500, 0)
Advertisement
Add Comment
Please, Sign In to add comment