Advertisement
killer64

TRON

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