minimite

tronhack

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