Alyssa

Tron -- Hacked

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