Advertisement
Alyssa

Tron

Feb 7th, 2015
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.56 KB | None | 0 0
  1. version = 0.43
  2. --Current bugs or errors:
  3. --None
  4. --Changelog
  5. --0.43 Improved rendering.
  6. --0.42 Disabled invincibility.
  7. --0.41 Players should now be in sync.
  8. --0.40 Fixed the bug that made 0.39 unplayable.
  9. --0.39 Added customizable amounts of players, 2-4 players can play.
  10. --0.38 Edited debug menu and info bar. Also edited wall graphics.
  11. --0.37 Fixed how trails work, all the posX data was being overwritten.
  12. --0.36 Graphics are hopefully fixed.
  13. --0.35 Graphic changes/bug fixes.
  14. --0.34 Added graphics? probably will have some bugs.
  15. --0.33 Now playerData should be converted even in non-debug mode.
  16. --0.32 Added background.
  17. --0.31 Added walls.
  18. --0.30 Fixed non-hosts dieing at the start of the game?
  19. --0.26-29 Fixed bugs.
  20. --0.25 Added turning.
  21. --0.10-0.24 No data recorded for these updates.
  22. --Goals
  23. --Find bugs and get rid of them. (Every version.)
  24. --Make player count customizable in-game. (v0.44)
  25. debug = false
  26. local oPE = os.pullEvent
  27. os.pullEvent = os.pullEventRaw
  28. local tronchan = 1504
  29. local fModem = false
  30. numPlayers1 = {...}
  31. if numPlayers1[1] then
  32.     numPlayers = tonumber(numPlayers1[1])
  33. else
  34.     numPlayers = 4
  35. end
  36. for k, v in pairs({"right","left","top","bottom","front","back"}) do
  37.     if peripheral.getType(v) == "modem" then
  38.         modem = peripheral.wrap(v)
  39.         modem.open(tronchan)
  40.         fModem = true
  41.     end
  42. end
  43. bcols = {}
  44. for i = 0, 9 do
  45.     bcols[2^i] = i
  46. end
  47. bcols[2^10] = "a"
  48. bcols[2^11] = "b"
  49. bcols[2^12] = "c"
  50. bcols[2^13] = "d"
  51. bcols[2^14] = "e"
  52. bcols[2^15] = "f"
  53. local scrx, scry = term.getSize()
  54. local cx, cy = math.floor(scrx/2),math.floor(scry/2)
  55.  
  56. function tick()
  57.     modem.transmit(UGID,12345, "TIME")
  58.     TID = os.startTimer(0.1)
  59.     ltime = ltime + 0.1
  60.     if hosting and debug then
  61.         modem.transmit(UGID, 13370, ltime)
  62.         --time = ltime
  63.     end
  64.     if hosting then
  65.         time = ltime
  66.     end
  67.     tC2 = 0
  68.     for k, v in pairs(playerData) do
  69.         tC2 = tC2 + 1
  70.         if playerData[k].alive then
  71.             if oCheck(playerData[k].posX, playerData[k].posY) then
  72.                 if playerData[k].invincible <= 0.9 then
  73.                     playerData[k].alive = false
  74.                 end
  75.             end
  76.             if playerData[k].dir == 1 then
  77.                 if not playerData[k].trail[playerData[k].posX] then
  78.                     playerData[k].trail[playerData[k].posX] = {}
  79.                 end
  80.                 playerData[k].trail[playerData[k].posX][playerData[k].posY] = "+"
  81.                 if not theGrid[playerData[k].posX] then
  82.                     theGrid[playerData[k].posX] = {}
  83.                 end
  84.                 theGrid[playerData[k].posX][playerData[k].posY] = {"+",playerData[k].colorID}
  85.  
  86.                 if playerData[k].invincible >= 0.1 then
  87.                     playerData[k].invincible = playerData[k].invincible -0.1
  88.                 else
  89.                     if oCheck(playerData[k].posX, playerData[k].posY+1) then
  90.                         playerData[k].alive = false
  91.                     else
  92.                         playerData[k].posY = playerData[k].posY +1
  93.                     end
  94.                 end
  95.                 if playerData[k].dir == 1 then
  96.                     if not theGrid[playerData[k].posX] then
  97.                         theGrid[playerData[k].posX] = {}
  98.                     end
  99.                     theGrid[playerData[k].posX][playerData[k].posY] = {"^",playerData[k].colorID}
  100.                 end
  101.                 --To avoid insta-deaths I will give 1 second of invincibility.
  102.             elseif playerData[k].dir == 2 then
  103.                 if not playerData[k].trail[playerData[k].posX] then
  104.                     playerData[k].trail[playerData[k].posX] = {}
  105.                 end
  106.                 playerData[k].trail[playerData[k].posX][playerData[k].posY] = "+"
  107.                 if not theGrid[playerData[k].posX] then
  108.                     theGrid[playerData[k].posX] = {}
  109.                 end
  110.                 theGrid[playerData[k].posX][playerData[k].posY] = {"+",playerData[k].colorID}
  111.                 if playerData[k].invincible >= 0.1 then
  112.                     playerData[k].invincible = playerData[k].invincible -0.1
  113.                 else
  114.                     if oCheck(playerData[k].posX+1, playerData[k].posY) then
  115.                         playerData[k].alive = false
  116.                     else
  117.                         playerData[k].posX = playerData[k].posX +1
  118.                     end
  119.                 end
  120.                 if playerData[k].dir == 2 then
  121.                     if not theGrid[playerData[k].posX] then
  122.                         theGrid[playerData[k].posX] = {}
  123.                     end
  124.                     theGrid[playerData[k].posX][playerData[k].posY] = {">",playerData[k].colorID}
  125.                 end
  126.             elseif playerData[k].dir == 3 then
  127.                 if not playerData[k].trail[playerData[k].posX] then
  128.                     playerData[k].trail[playerData[k].posX] = {}
  129.                 end
  130.                 playerData[k].trail[playerData[k].posX][playerData[k].posY] = "+"
  131.                 if not theGrid[playerData[k].posX] then
  132.                     theGrid[playerData[k].posX] = {}
  133.                 end
  134.                 theGrid[playerData[k].posX][playerData[k].posY] = {"+",playerData[k].colorID}
  135.                 if playerData[k].invincible >= 0.1 then
  136.                     playerData[k].invincible = playerData[k].invincible -0.1
  137.                 else
  138.                     if oCheck(playerData[k].posX, playerData[k].posY-1) then
  139.                         playerData[k].alive = false
  140.                     else
  141.                         playerData[k].posY = playerData[k].posY -1
  142.                     end
  143.                 end
  144.                 if playerData[k].dir == 3 then
  145.                     if not theGrid[playerData[k].posX] then
  146.                         theGrid[playerData[k].posX] = {}
  147.                     end
  148.                     theGrid[playerData[k].posX][playerData[k].posY] = {"v",playerData[k].colorID}
  149.                 end
  150.             elseif playerData[k].dir == 4 then
  151.                 if not playerData[k].trail[playerData[k].posX] then
  152.                     playerData[k].trail[playerData[k].posX] = {}
  153.                 end
  154.                 playerData[k].trail[playerData[k].posX][playerData[k].posY] = "+"
  155.                 if not theGrid[playerData[k].posX] then
  156.                     theGrid[playerData[k].posX] = {}
  157.                 end
  158.                 theGrid[playerData[k].posX][playerData[k].posY] = {"+",playerData[k].colorID}
  159.                 if playerData[k].invincible >= 0.1 then
  160.                     playerData[k].invincible = playerData[k].invincible -0.1
  161.                 else
  162.                     if oCheck(playerData[k].posX-1, playerData[k].posY) then
  163.                         playerData[k].alive = false
  164.                     else
  165.                         playerData[k].posX = playerData[k].posX -1
  166.                     end
  167.                 end
  168.                 if playerData[k].dir == 4 then
  169.                     if not theGrid[playerData[k].posX] then
  170.                         theGrid[playerData[k].posX] = {}
  171.                     end
  172.                     theGrid[playerData[k].posX][playerData[k].posY] = {"<",playerData[k].colorID}
  173.                 end
  174.             end
  175.         else
  176.             playerData[k].deathTime = playerData[k].deathTime + 0.1
  177.             if playerData[k].deathTime >= 3 then
  178.                 toKill[#toKill+1] = k
  179.             end
  180.         end
  181.     end
  182.     if tC2 == 1 then
  183.         if playerData[PID] then
  184.             if playerData[PID].alive then
  185.                 gameResult = "won"
  186.                 contGame = false
  187.             else
  188.                 gameResult = "won"
  189.                 contGame = false
  190.             end
  191.         else
  192.             gameResult = "lost"
  193.             contGame = false
  194.         end
  195.     elseif tC2 == 0 then
  196.         if oTC2 >= 2 then
  197.             gameResult = "tied" --Yes, I know you could die before the last 2.
  198.             contGame = false
  199.         end
  200.     end
  201.     if debug then
  202.         if time == math.floor(time) then
  203.             time = tostring(time)..".0"
  204.         end
  205.         timeLen = tostring(time).."s"
  206.         timeLen = #timeLen
  207.         term.setCursorPos(scrx-timeLen,scry)
  208.         write(string.rep(" ",timeLen))
  209.         term.setCursorPos(scrx-timeLen, scry)
  210.         write(time.."s", scry)
  211.         --if time == 30.0 then
  212.         --  gameResult = "won"
  213.         --  contGame = false
  214.         --end
  215.     end
  216. end
  217.  
  218. function oCheck(positionX, positionY)
  219.     if theGrid[positionX] then
  220.         if theGrid[positionX][positionY] then
  221.             if theGrid[positionX][positionY][1] then
  222.                 if theGrid[positionX][positionY][1] ~= "^" and theGrid[positionX][positionY][1] ~= ">" then
  223.                     if theGrid[positionX][positionY][1] ~= "v" and theGrid[positionX][positionY][1] ~= "<" then
  224.                         return true
  225.                     else return false
  226.                     end
  227.                 else return false
  228.                 end
  229.             else return false
  230.             end
  231.         else return false
  232.         end
  233.     else return false
  234.     end
  235. end
  236.  
  237. function cPrint(text,line)
  238.     if not line then
  239.         ccpx, ccpy = term.getCursorPos()
  240.         line = ccpy
  241.     end
  242.     local nCursorX = #text/2
  243.     local nCursorX = cx-nCursorX
  244.     term.setCursorPos(nCursorX, line)
  245.     print(text)
  246. end
  247.  
  248. function cWrite(text,line)
  249.     if not line then
  250.         ccpx, ccpy = term.getCursorPos()
  251.         line = ccpy
  252.     end
  253.     local nCursorX = #text/2
  254.     local nCursorX = cx-nCursorX
  255.     term.setCursorPos(nCursorX, line)
  256.     write(text)
  257. end
  258.  
  259. function renderScreen() --While lots of this is based off of Pixel toast's tron,
  260.     -- I was even viewing his code while creating this render function,
  261.     -- so yeah, some of this is his code!
  262.     term.setTextColor(colors.gray)
  263.     if converted then
  264.         for c=1,scry do
  265.             term.setCursorPos(1,c)
  266.             term.setTextColor(colors.gray)
  267.             local pre = camX%3
  268.             if (c+camY)%3==0 then
  269.                 if pre==1 then
  270.                     pre="--"
  271.                 elseif pre==2 then
  272.                     pre="-"
  273.                 else
  274.                     pre=""
  275.                 end
  276.                 tblit = pre..string.rep("+--",math.ceil(scrx/2))
  277.                 term.blit(tblit,string.rep("7",#tblit),string.rep("f",#tblit))
  278.             else
  279.                 if pre==1 then
  280.                     pre="  "
  281.                 elseif pre==2 then
  282.                     pre=" "
  283.                 else
  284.                     pre=""
  285.                 end
  286.                 tblit = pre..string.rep("|  ",math.ceil(scrx/2))
  287.                 term.blit(tblit,string.rep("7",#tblit),string.rep("f",#tblit))
  288.             end
  289.             for d = 1, scrx do
  290.                 if theGrid[d + camX - cx] then
  291.                     if theGrid[d + camX - cx][-c + camY + cy +1] then --Super hacky, but it works!
  292.                         term.setCursorPos(d,c)
  293.                         term.setTextColor(theGrid[d + camX - cx][-c + camY + cy +1][2])
  294.                         term.write(theGrid[d + camX - cx][-c + camY + cy +1][1])
  295.                     end
  296.                 end
  297.             end
  298.         end
  299.     end
  300.     if debug then
  301.         tCounter = 1
  302.         for k, v in pairs(playerData) do
  303.             tCounter = tCounter + 1
  304.             if playerData[k].alive then
  305.                 term.setTextColor(colors.white)
  306.                 cPrint(k.." / "..playerData[k].teamColor..": alive x: "..playerData[k].posX.." y: "..playerData[k].posY.." dir: "..playerData[k].dir,tCounter)
  307.                 if tonumber(k) == tonumber(PID) then
  308.                     --playerData[PID] = playerData[k] --No idea if this will work, maybe it will make it worse.
  309.                     if playerData[k].dir then
  310.                         if playerData[k].dir == 1 then
  311.                             cPrint("DEBUG | "..PID.." | "..playerData[PID].teamColor.." | UP".." | CamX "..camX.." CamY "..camY,1)
  312.                         elseif playerData[k].dir == 2 then
  313.                             cPrint("DEBUG | "..PID.." | "..playerData[PID].teamColor.." | RIGHT".." | CamX "..camX.." CamY "..camY,1)
  314.                         elseif playerData[k].dir == 3 then
  315.                             cPrint("DEBUG | "..PID.." | "..playerData[PID].teamColor.." | DOWN".." | CamX "..camX.." CamY "..camY,1)
  316.                         elseif playerData[k].dir == 4 then
  317.                             cPrint("DEBUG | "..PID.." | "..playerData[PID].teamColor.." | LEFT".." | CamX "..camX.." CamY "..camY,1)
  318.                         end
  319.                     end
  320.                 end
  321.             else
  322.                 cPrint(k..": dead for "..playerData[k].deathTime.."s",tCounter)
  323.             end
  324.         end
  325.         if event then
  326.             cPrint("Last event: "..event, 6)
  327.         end
  328.     else
  329.         if playerData[PID] then
  330.             term.setTextColor(playerData[PID].colorID)
  331.             cPrint(playerData[PID].teamColor,1)
  332.         else
  333.             term.setTextColor(colors.white)
  334.             cPrint("DEAD",1)
  335.         end
  336.     end
  337. end
  338.  
  339. function control()
  340.     time = 0
  341.     ltime = 0
  342.     if debug then
  343.         cPrint("DEBUG",1)
  344.     else
  345.         term.setTextColor(teamColorID)
  346.         cPrint(teamColor)
  347.     end
  348.     contGame = true
  349.     sleep(0.05)
  350.     TID = os.startTimer(0.1)
  351.     toKill = {}
  352.     toConvert = {}
  353.     converted = false
  354.     for k, v in pairs(playerData) do
  355.         if type(k) ~= "number" then
  356.             toKill[#toKill+1] = k
  357.             toConvert[#toConvert+1] = tonumber(k)
  358.         end
  359.     end
  360.     while contGame do
  361.         if tC2 then
  362.             oTC2 = tC2
  363.         end
  364.         if #toConvert >= 1 then
  365.             for a = 1, #toConvert do
  366.                 playerData[toConvert[a]] = playerData[tostring(toConvert[a])]
  367.             end
  368.             toConvert = {} --OH I keep trying to convert! Causing it to set the new playerData to nil!
  369.             converted = true
  370.         end
  371.         if #toKill >= 1 then
  372.             for a = 1, #toKill do
  373.                 for k, v in pairs(playerData[toKill[a]].trail) do
  374.                     for k2, v2 in pairs(playerData[toKill[a]].trail[k]) do
  375.                         theGrid[k][k2] = nil
  376.                     end
  377.                 end
  378.                 playerData[toKill[a]] = nil
  379.             end
  380.             toKill = {}
  381.         end
  382.         fCam = false
  383.         if converted == true then
  384.             if playerData[PID] then
  385.                 camX = playerData[PID].posX
  386.                 camY = playerData[PID].posY
  387.                 fCam = true
  388.             end
  389.         else
  390.             if playerData[tostring(PID)] then
  391.                 camX = playerData[tostring(PID)].posX
  392.                 camY = playerData[tostring(PID)].posY
  393.                 fCam = true
  394.             end
  395.         end
  396.         if not fCam then --Bug is before here.
  397.             for k, v in pairs(playerData) do
  398.                 camX = playerData[k].posX
  399.                 camY = playerData[k].posY
  400.                 fCam = true
  401.                 break
  402.             end
  403.         end
  404.         if not fCam then
  405.             camX = 0
  406.             camY = 0
  407.         end
  408.         term.clear()
  409.         renderScreen()
  410.         event, key, p2, p3, p4, p5 = os.pullEvent()
  411.         if event == "key" then
  412.             --Do stuff
  413.             if playerData[PID] then
  414.                 if playerData[PID].alive then
  415.                     if key == keys.right then
  416.                         if playerData[PID].dir >= 4 then
  417.                             playerData[PID].dir = 1
  418.                         else
  419.                             playerData[PID].dir = playerData[PID].dir +1
  420.                         end
  421.                         modem.transmit(UGID,31337, "TURN_RIGHT "..PID)
  422.                     elseif key == keys.left then
  423.                         if playerData[PID].dir <= 1 then
  424.                             playerData[PID].dir = 4
  425.                         else
  426.                             playerData[PID].dir = playerData[PID].dir -1
  427.                         end
  428.                         modem.transmit(UGID,31337, "TURN_LEFT "..PID)
  429.                     end
  430.                 end
  431.             end
  432.         elseif event == "terminate" then
  433.             modem.transmit(UGID, 999, "QUIT_GAME "..PID)
  434.             gameResult = "lost"
  435.             contGame = false
  436.         elseif event == "timer" and key == TID and hosting then
  437.             tick()
  438.         elseif event == "modem_message" and p2 == UGID and p3 == 12345 and p4 == "TIME" and hosting ~= true then
  439.             tick()
  440.         end
  441.     end
  442. end
  443.  
  444. function connection()
  445.     if debug then
  446.         --cPrint("DEBUG",2)
  447.         if true then -- hosting then
  448.             for k, v in pairs(playerIDS) do
  449.                 print(playerIDS[k])
  450.             end
  451.         end
  452.     end
  453.     contGame = true
  454.     playerData = {}
  455.     for d = 1, #playerIDS do
  456.         playerData[playerIDS[d]] = {}
  457.     end
  458.     tCounter = 0
  459.     for k, v in pairs(playerData) do
  460.         tCounter = tCounter +1
  461.         playerData[k].dir = tCounter --1 = up, 2 = right, 3 = down, 4 = left
  462.         playerData[k].trail = {} --So we can remove the trail upon death.
  463.         playerData[k].alive = true
  464.         playerData[k].deathTime = 0
  465.         playerData[k].invincible = 0
  466.         if tCounter == 1  then
  467.             playerData[k].posX = 0
  468.             playerData[k].posY = -124
  469.             playerData[k].teamColor = "Blue"
  470.             playerData[k].colorID = colors.blue
  471.         elseif tCounter == 2 then
  472.             playerData[k].posX = -124
  473.             playerData[k].posY = 0
  474.             playerData[k].teamColor = "Red"
  475.             playerData[k].colorID = colors.red
  476.         elseif tCounter == 3 then
  477.             playerData[k].posX = 0
  478.             playerData[k].posY = 124
  479.             playerData[k].teamColor = "Green"
  480.             playerData[k].colorID = colors.green
  481.         elseif tCounter == 4 then
  482.             playerData[k].posX = 124
  483.             playerData[k].posY = 0
  484.             playerData[k].teamColor = "Purple"
  485.             playerData[k].colorID = colors.purple
  486.         end
  487.     end
  488.     theGrid = {}
  489.     for b=125,-125,-1 do
  490.         if not theGrid[b] then
  491.             theGrid[b] = {}
  492.         end
  493.         theGrid[b][-125]={"-",colors.yellow}
  494.     end
  495.     sleep(0)
  496.     for b=125,-125,-1 do
  497.         if not theGrid[b] then
  498.             theGrid[b] = {}
  499.         end
  500.         theGrid[b][125]={"-",colors.yellow}
  501.     end
  502.     sleep(0)
  503.     for b=125,-125,-1 do
  504.         if not theGrid[-125] then
  505.             theGrid[-125] = {}
  506.         end
  507.         theGrid[-125][b]={"|",colors.yellow}
  508.     end
  509.     sleep(0)
  510.     for b=125,-125,-1 do
  511.         if not theGrid[125] then
  512.             theGrid[125] = {}
  513.         end
  514.         theGrid[125][b]={"|",colors.yellow}
  515.     end
  516.     sleep(0)
  517.     theGrid[125][125] = {"=",colors.yellow}
  518.     theGrid[125][-125] = {"=",colors.yellow}
  519.     theGrid[-125][125] = {"=",colors.yellow}
  520.     theGrid[-125][-125] = {"=",colors.yellow}
  521.     while contGame do
  522.         e, side, chan, rchan, msg, dist = os.pullEvent("modem_message")
  523.         if chan == UGID and msg == "CONFLICT_DETECTION" then
  524.             modem.transmit(UGID, 0, "CONFLICT")
  525.         elseif hosting ~= true and chan == UGID and rchan == 13370 then
  526.             time = msg
  527.         elseif chan == UGID and string.sub(msg,1,9) == "QUIT_GAME" then
  528.             if playerData[tonumber(string.sub(msg,11,#msg))] then
  529.                 playerData[tonumber(string.sub(msg,11,#msg))].alive = false
  530.             end
  531.         elseif chan == UGID and string.sub(msg,1,10) == "TURN_RIGHT" then
  532.             PIDFrom = tonumber(string.sub(msg,12, #msg))
  533.             if playerData[PIDFrom] then
  534.                 if playerData[PIDFrom].dir >= 4 then
  535.                     playerData[PIDFrom].dir = 1
  536.                 else
  537.                     playerData[PIDFrom].dir = playerData[PIDFrom].dir +1
  538.                 end
  539.             end
  540.         elseif chan == UGID and string.sub(msg,1,9) == "TURN_LEFT" then
  541.             PIDFrom = tonumber(string.sub(msg,11, #msg))
  542.             if playerData[PIDFrom] then
  543.                 if playerData[PIDFrom].dir <= 1 then
  544.                     playerData[PIDFrom].dir = 4
  545.                 else
  546.                     playerData[PIDFrom].dir = playerData[PIDFrom].dir -1
  547.                 end
  548.             end
  549.         end
  550.         --gameResult = "won"
  551.         --contGame = false
  552.         --break
  553.     end
  554. end
  555.  
  556. function game()
  557.     hosting = false
  558.     PID = math.random(1,65535) -- Player ID, Just hope there are no conflicts.
  559.     gameResult = nil
  560.     timerID = os.startTimer(1)
  561.     searchConnection = true
  562.     UPID = math.random(1024,4096) -- Unique Ping IDentification
  563.     foundGame = false
  564.     modem.transmit(tronchan, UPID, numPlayers.." FIND_GAME "..PID)
  565.     while searchConnection do
  566.         e, p1, p2, p3, p4, p5 = os.pullEvent()
  567.         if p1 == timerID and e == "timer" then
  568.             break
  569.         elseif e == "modem_message" and p2 == tronchan and p3 == UPID then
  570.             if p4 ~= "PID_CONFLICT" then
  571.                 playerNum = tonumber(string.sub(p4, 6,6)) +1
  572.                 sNumPlayers = tonumber(string.sub(p4,8,8))
  573.                 UGID = tonumber(string.sub(p4,10,14))
  574.                 if sNumPlayers == numPlayers then
  575.                     foundGame = true
  576.                     searchConnection = false
  577.                     modem.open(UGID)
  578.                     cWrite(playerNum.."/"..numPlayers.." players found.",2)
  579.                 end
  580.             else
  581.                 PID = math.random(1,65535)
  582.                 modem.transmit(tronchan, UPID, numPlayers.." FIND_GAME "..PID)
  583.             end
  584.         end
  585.     end
  586.     if not foundGame then
  587.         playersFound = 1
  588.         hosting = true
  589.         playerIDS = {PID}
  590.         UGID = math.random(15000,15999) --Unique Game ID, The channel we will use for this game.
  591.         conflictFound = false
  592.         searchConflict = true
  593.         playerNum = 1
  594.         while searchConflict do
  595.             e = nil
  596.             p1 = nil
  597.             timerID = os.startTimer(0.5)
  598.             modem.transmit(UGID, 0, "CONFLICT_DETECTION")
  599.             while not e == "timer" and p1 == timerID do
  600.                 e, p1, p2, p3, p4, p5 = os.pullEvent()
  601.                 if e == "modem_message" and p2 == UGID and p4 == "CONFLICT" then
  602.                     conflictFound = true
  603.                 end
  604.             end
  605.             if not conflictFound then
  606.                 modem.open(UGID)
  607.                 break
  608.             else
  609.                 UGID = math.random(15000,15999)
  610.                 conflictFound = false
  611.             end
  612.         end
  613.         cWrite(playersFound.."/"..numPlayers.." players found.",2)
  614.         while playersFound ~= numPlayers do
  615.             e, side, chan, rchan, msg, dist = os.pullEvent("modem_message")
  616.             if string.sub(msg,1,11) == numPlayers.." FIND_GAME" and chan == tronchan then
  617.                 PIDC = false
  618.                 for b = 1, #playerIDS  do
  619.                     if string.sub(msg,13,#msg) == playerIDS[b] then
  620.                         modem.transmit(tronchan, rchan, "PID_CONFLICT")
  621.                         PIDC = true
  622.                     end
  623.                 end
  624.                 if not PIDC then
  625.                     playerIDS[#playerIDS+1] = string.sub(msg,13,#msg) --Add the player's PID to our database
  626.                     modem.transmit(tronchan, rchan, "GAME "..playersFound.."/"..numPlayers.." "..UGID)
  627.                     playersFound = playersFound+1
  628.                     cWrite(playersFound.."/"..numPlayers.." players found.",2)
  629.                     modem.transmit(UGID,playersFound,"NEW_PLAYER")
  630.                 end
  631.             end
  632.         end
  633.     end
  634.     if not hosting then
  635.         startGame = false --Sorry for using so many global variables ;(
  636.         while not startGame do
  637.             e,side,chan,rchan,msg,dist = os.pullEvent()
  638.             if chan == UGID and msg == "START_GAME" then
  639.                 startGame = true
  640.             elseif chan == UGID and msg == "NEW_PLAYER" then
  641.                 cWrite(rchan.."/"..numPlayers.." players found.",2)
  642.             end
  643.         end
  644.     else
  645.         sleep(0.1)
  646.         modem.transmit(UGID,0,"START_GAME")
  647.     end
  648.     if not hosting then
  649.         startGame = false --Sorry for using so many global variables ;(
  650.         while not startGame do
  651.             e,side,chan,rchan,msg,dist = os.pullEvent()
  652.             if chan == UGID and string.sub(msg,1,5) == "PIDs " then
  653.                 playerIDS = textutils.unserialize(string.sub(msg, 6, #msg))
  654.                 startGame = true
  655.             end
  656.         end
  657.     else
  658.         sleep(0.1)
  659.         modem.transmit(UGID, 8, "PIDs "..textutils.serialize(playerIDS))
  660.     end
  661.     term.clear()
  662.     term.setCursorPos(1,1)
  663.     if playerNum == 1 then
  664.         teamColor = "Blue"
  665.         teamColorID = colors.blue
  666.     elseif playerNum == 2 then
  667.         teamColor = "Red"
  668.         teamColorID = colors.red
  669.     elseif playerNum == 3 then
  670.         teamColor = "Green"
  671.         teamColorID = colors.green
  672.     elseif playerNum == 4 then
  673.         teamColor = "Purple"
  674.         teamColorID = colors.purple
  675.     end
  676.     parallel.waitForAny(control, connection)
  677.     term.clear()
  678.     term.setTextColor(colors.blue)
  679.     gameMsg = "You "..gameResult.."!"
  680.     term.setCursorPos(cx - #gameMsg,cy)
  681.     write(gameMsg)
  682.     sleep(5)
  683. end
  684.  
  685. function drawMenu()
  686.     term.setBackgroundColor(colors.black)
  687.     term.clear()
  688.     term.setCursorPos(1,1)
  689.     term.setTextColor(colors.blue)
  690.     cPrint("Tron")
  691.     term.setTextColor(colors.white)
  692.     selNum = #selections/2
  693.     term.setCursorPos(1,cy-selNum)
  694.     for a = 1, #selections do
  695.         if curSelection == a then
  696.             cPrint("["..selections[a].."]")
  697.         else
  698.             cPrint(selections[a])
  699.         end
  700.     end
  701.     cWrite("v"..version,scry)
  702. end
  703.  
  704. function menu()
  705.     term.setTextColor(colors.white)
  706.     term.setBackgroundColor(colors.black)
  707.     selections = {"Play","Quit"}
  708.     exitGame = false
  709.     curSelection = 1
  710.     drawMenu()
  711.     if not term.isColor() then
  712.         exitGame = true
  713.     end
  714.     while not exitGame do
  715.         e,p1 = os.pullEvent("key")
  716.         if p1 == keys.up then
  717.             if curSelection >= #selections then
  718.                 curSelection = 1
  719.             else
  720.                 curSelection = curSelection +1
  721.             end
  722.         elseif p1 == keys.down then
  723.             if curSelection <= 1 then
  724.                 curSelection = #selections
  725.             else
  726.                 curSelection = curSelection -1
  727.             end
  728.         elseif p1 == keys.enter then
  729.             if selections[curSelection] == "Quit" then
  730.                 exitGame = true
  731.             elseif selections[curSelection] == "Play" then
  732.                 game()
  733.             end
  734.         end
  735.         drawMenu()
  736.            
  737.     end
  738. end
  739.  
  740. if numPlayers <= 4 and numPlayers >= 2 then
  741.     menu()
  742. else
  743.     cPrint("You can not have less than 2 players or more than 4 players!")
  744. end
  745. term.clear()
  746. term.setCursorPos(1,1)
  747. curProgram = shell.getRunningProgram()
  748. if #curProgram >= 2 then
  749.     curProgram = string.upper(string.sub(curProgram,1,1))..string.lower(string.sub(curProgram,2,#curProgram))
  750. else
  751.     curProgram = string.upper(curProgram)
  752. end
  753. if term.isColor() then
  754.     cPrint("Exited "..curProgram.." v"..version..", Thanks for playing!")
  755.     if numPlayers >= 5 or numPlayers <= 1 then
  756.         cPrint("You can not have less than 2 players")
  757.         cPrint("or more than 4 players!")
  758.     end
  759. else
  760.     cPrint("You need a advanced computer for "..curProgram)
  761. end
  762. os.pullEvent = oPE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement