Advertisement
LDDestroier

PixelToast's Tron - w/o as many bugs

Oct 10th, 2015
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.98 KB | None | 0 0
  1. --[[
  2. pastebin get jgyepx3g tron
  3. std pb jgyepx3g tron
  4. --]]
  5.  
  6. local isOpen=false
  7. for k,v in pairs({"right","left","top","bottom","front","back"}) do
  8.     if peripheral.getType(v)=="modem" then
  9.         rednet.open(v)
  10.         isOpen=true
  11.     end
  12. end
  13. if not isOpen then
  14.     error("no modem attached")
  15. end
  16. if not term.isColor() then
  17.     if _CC_VERSION then
  18.         colors.orange = colors.lightGray
  19.         colors.blue = colors.lightGray
  20.         colors.red = colors.white
  21.         colors.lightBlue = colors.white
  22.     else
  23.         colors.gray = colors.black
  24.         colors.orange = colors.white
  25.         colors.blue = colors.white
  26.         colors.red = colors.white
  27.         colors.lightBlue = colors.white
  28.     end
  29. end
  30.  
  31. local Mx,My=term.getSize()
  32. local Cx,Cy=math.floor(Mx/2),math.floor(My/2)
  33. function maingame()
  34.     local lang={"Waiting for player",{[0]="^",">","v","<"},{{"|","/","|","\\"},{"/","-","\\","-"},{"|","\\","|","/"},{"\\","-","/","-"}},"You died.","You won."}
  35.     local board=setmetatable({},{__index=function(s,n) s[n]={} return s[n] end})
  36.     for l1=99,-99,-1 do
  37.         board[l1][-99]={"-",3}
  38.     end
  39.     for l1=99,-99,-1 do
  40.         board[l1][99]={"|",3}
  41.     end
  42.     for l1=99,-99,-1 do
  43.         board[-99][l1]={"-",3}
  44.     end
  45.     for l1=99,-99,-1 do
  46.         board[99][l1]={"|",3}
  47.     end
  48.     board[100][100]={"/",3}
  49.     board[100][-100]={"\\",3}
  50.     board[-100][100]={"/",3}
  51.     board[-100][-100]={"\\",3}
  52.     local modem
  53.     local initheader="TRON:"
  54.     local pnid
  55.     local function send(...)
  56.         rednet.send(pnid,string.sub(textutils.serialize({...}),2,-2))
  57.     end
  58.     local function decode(dat)
  59.         return textutils.unserialize("{"..dat.."}")
  60.     end
  61.     local col
  62.     term.setCursorPos(math.floor(Cx-(#lang[1])/2),Cy)
  63.     term.setTextColor(colors.orange)
  64.     term.setBackgroundColor(colors.black)
  65.     term.clear()
  66.     term.write(lang[1])
  67.     rednet.broadcast(initheader.."pingcon")
  68.     local p1,p2
  69.     while true do
  70.         local p={os.pullEvent()}
  71.         if p[1]=="rednet_message" and p[2]~=os.getComputerID() then
  72.             if p[3]==initheader.."pingcon" then
  73.                 rednet.send(p[2],initheader.."pongcon")
  74.                 pnid=p[2]
  75.                 col={colors.blue,colors.red,colors.lightBlue}
  76.                 p1={pos={x=2,y=1},dir=0}
  77.                 p2={pos={x=1,y=1},dir=0}
  78.                 break
  79.             elseif p[3]==initheader.."pongcon" then
  80.                 pnid=p[2]
  81.                 col={colors.red,colors.blue,colors.lightBlue}
  82.                 p1={pos={x=1,y=1},dir=0}
  83.                 p2={pos={x=2,y=1},dir=0}
  84.                 break
  85.             end
  86.         end
  87.     end
  88.     term.setBackgroundColor(colors.black)
  89.     term.clear()
  90.     local frs=0
  91.     local fps=0 -- frame counter (debugging)
  92.     local function render()
  93.         local tsv = term.current().setVisible
  94.         if tsv then tsv(false) end
  95.         frs=frs+1
  96.         term.setTextColor(colors.gray)
  97.         for l1=1,My do
  98.             term.setCursorPos(1,l1)
  99.             local pre=p1.pos.x%3
  100.             if (l1+p1.pos.y)%3==0 then
  101.                 if pre==1 then
  102.                     pre="--"
  103.                 elseif pre==2 then
  104.                     pre="-"
  105.                 else
  106.                     pre=""
  107.                 end
  108.                 term.write(pre..("+--"):rep(math.ceil(Mx/2)))
  109.             else
  110.                 if pre==1 then
  111.                     pre="  "
  112.                 elseif pre==2 then
  113.                     pre=" "
  114.                 else
  115.                     pre=""
  116.                 end
  117.                 term.write(pre..("|  "):rep(math.ceil(Mx/2)))
  118.             end
  119.         end
  120.         term.setTextColor(colors.blue)
  121.         local num=0
  122.         for k,v in pairs(board) do
  123.             for l,y in pairs(v) do
  124.                 if (k-p1.pos.x)+Cx<=Mx and (k-p1.pos.x)+Cx>=1 and (l-p1.pos.y)+Cy<=My and (l-p1.pos.y)+Cy>=1 then
  125.                     term.setTextColor(col[y[2]] or y[2])
  126.                     term.setCursorPos((k-p1.pos.x)+Cx,(l-p1.pos.y)+Cy)
  127.                     term.write(y[1])
  128.                     num=num+1
  129.                 end
  130.             end    
  131.         end
  132.         term.setCursorPos(1,1)
  133.         if col[1]==colors.blue then
  134.             term.setTextColor(colors.blue)
  135.             term.write("BLUE")
  136.         else
  137.             term.setTextColor(colors.red)
  138.             term.write("RED")
  139.         end
  140.         if tsv then tsv(true) end
  141.     end
  142.     local odr={[p1]=p1.dir,[p2]=p2.dir}
  143.     local function processmove(u)
  144.         local ccol
  145.         if u==p1 then
  146.             ccol=col[1]
  147.         else
  148.             ccol=col[2]
  149.         end
  150.         term.setTextColor(ccol)
  151.         if u==p1 and board[u.pos.x][u.pos.y] then
  152.             send("DIE")
  153.             term.setCursorPos(Cx,Cy)
  154.             term.write("x")
  155.             sleep(2)
  156.             term.setCursorPos(Cx-math.floor(#lang[4]/2),Cy)
  157.             term.setTextColor(colors.orange)
  158.             term.clear()
  159.             term.write(lang[4])
  160.             sleep(5)
  161.             term.setTextColor(colors.white)
  162.             term.setBackgroundColor(colors.black)
  163.             term.setCursorPos(1,1)
  164.             term.clear()
  165.             error("",0)
  166.         end
  167.         if odr[u]~=u.dir then
  168.             board[u.pos.x][u.pos.y]={lang[3][odr[u]+1][u.dir+1],ccol}
  169.         end
  170.         if not board[u.pos.x][u.pos.y] then
  171.             if u.dir%2==0 then
  172.                 board[u.pos.x][u.pos.y]={"|",ccol}
  173.             else
  174.                 board[u.pos.x][u.pos.y]={"-",ccol}
  175.             end
  176.         end
  177.         local chr=board[u.pos.x][u.pos.y][1]
  178.         local shr={x=u.pos.x,y=u.pos.y}
  179.         if u.dir==0 then
  180.             u.pos.y=u.pos.y-1
  181.         elseif u.dir==1 then
  182.             u.pos.x=u.pos.x+1
  183.         elseif u.dir==2 then
  184.             u.pos.y=u.pos.y+1
  185.         else
  186.             u.pos.x=u.pos.x-1
  187.         end
  188.         odr[u]=u.dir
  189.         return chr,shr
  190.     end
  191.     local function renderchar(u)
  192.         local ccol
  193.         if u==p1 then
  194.             ccol=col[1]
  195.             term.setCursorPos(Cx,Cy)
  196.         else
  197.             ccol=col[2]
  198.             term.setCursorPos((p2.pos.x-p1.pos.x)+Cx,(p2.pos.y-p1.pos.y)+Cy)
  199.         end
  200.         term.setTextColor(ccol)
  201.         term.write(lang[2][u.dir])
  202.     end
  203.     function processturn(p,u)
  204.         local dirs={[keys.up]=0,[keys.right]=1,[keys.down]=2,[keys.left]=3}
  205.         if (odr[u]+2)%4~=dirs[p] then
  206.             u.dir=dirs[p]
  207.             renderchar(u)
  208.             if u==p1 then
  209.                 send("ROT",u.dir)
  210.             end
  211.         end
  212.     end
  213.     render()
  214.     local move=os.startTimer(0.1)
  215.     local fct=os.startTimer(1)
  216.     while true do
  217.         local p={os.pullEvent()}
  218.         if p[1]=="key" then
  219.             if p[2]==keys.up or p[2]==keys.right or p[2]==keys.down or p[2]==keys.left then
  220.                 processturn(p[2],p1)
  221.             end
  222.         elseif p[1]=="timer" then
  223.             if p[2]==move then
  224.                 local ret,ret2=processmove(p1)
  225.                 move=os.startTimer(0.1)
  226.                 send("MOVE",ret2,ret)
  227.             elseif p[2]==fct then
  228.                 fps=frs
  229.                 frs=0
  230.                 fct=os.startTimer(1)
  231.             end
  232.         elseif p[1]=="rednet_message" and p[2]==pnid then
  233.             local dat=decode(p[3])
  234.             if dat[1]=="ROT" then
  235.                 p2.dir=dat[2]
  236.                 renderchar(p2)
  237.             elseif dat[1]=="DIE" then
  238.                 p1.pos=p2.pos
  239.                 render()
  240.                 term.setTextColor(col[2])
  241.                 term.setCursorPos(Cx,Cy)
  242.                 term.write("x")
  243.                 sleep(2)
  244.                 term.setCursorPos(Cx-math.floor(#lang[5]/2),Cy)
  245.                 term.setTextColor(colors.orange)
  246.                 term.clear()
  247.                 term.write(lang[5])
  248.                 sleep(5)
  249.                 term.setTextColor(colors.white)
  250.                 term.setBackgroundColor(colors.black)
  251.                 term.setCursorPos(1,1)
  252.                 term.clear()
  253.                 return
  254.             elseif dat[1]=="MOVE" then
  255.                 p2.pos=dat[2]
  256.                 board[p2.pos.x][p2.pos.y]={dat[3],col[2]}
  257.                 render()
  258.                 renderchar(p1)
  259.                 renderchar(p2)
  260.             end
  261.         end
  262.     end
  263. end
  264. local selected=1
  265. function rmain()
  266.     term.setBackgroundColor(colors.black)
  267.     term.clear()
  268.     term.setCursorPos(1,1)
  269.     term.setTextColor(colors.blue)
  270.     local txt="  _  _______________     ________    __       _\n/ \\/  _____________\\   /  ____  \\  |  \\     / |\n\\_/| /    / \\       | /  /    \\  \\ |   \\ __/  |\n   | |    | |\\  ___/ |  |      |  ||    \\     |\n   | |    | | \\ \\    |  |      |  ||   __\\    |\n   | |    | |  \\ \\    \\  \\____/  / |  /   \\   |\n   \\_/    \\_/   \\_/    \\________/  |_/     \\__|"
  271.     local cnt=1
  272.     local cnt2=Cx-23
  273.     for char in string.gmatch(txt,".") do
  274.         if char~=" " and char~="\n" then
  275.             term.setCursorPos(cnt2,cnt)
  276.             term.write(char)
  277.         elseif char=="\n" then
  278.             cnt=cnt+1
  279.             cnt2=Cx-23
  280.         end
  281.         cnt2=cnt2+1
  282.     end
  283.     local selections={"Multiplayer","Exit"}
  284.     selected=((selected-1)%(#selections))+1
  285.     for k,v in pairs(selections) do
  286.         if k==selected then
  287.             term.setTextColor(colors.blue)
  288.             term.setCursorPos(Cx-(math.floor(#v/2)+1),k+10)
  289.             term.write(">"..v.."<")
  290.             term.setTextColor(colors.lightBlue)
  291.             term.setCursorPos(Cx-math.floor(#v/2),k+10)
  292.             term.write(v)
  293.         else
  294.             term.setTextColor(colors.lightBlue)
  295.             term.setCursorPos(Cx-math.floor(#v/2),k+10)
  296.             term.write(v)
  297.         end
  298.     end
  299. end
  300. rmain()
  301. while true do
  302.     p={os.pullEvent()}
  303.     if p[1]=="key" then
  304.         if p[2]==keys.up then
  305.             selected=selected-1
  306.             rmain()
  307.         elseif p[2]==keys.down then
  308.             selected=selected+1
  309.             rmain()
  310.         elseif p[2]==keys.enter then
  311.             if selected==1 then
  312.                 a,b=pcall(maingame)
  313.                 if not a and b~="" then
  314.                     error(b,0)
  315.                 end
  316.                 rmain()
  317.             else
  318.             break
  319.             end
  320.         end
  321.     end
  322. end
  323. term.setCursorPos(1,1)
  324. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement