ZNZNCOOP

vrAPI

Nov 28th, 2014
249
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Virtual room API
  2. -- Created by Zer0Galaxy
  3. -- No rights reserved
  4. local scrf, scrl, scrr, scrb={},{},{},{}
  5. local monitors={}
  6. local inf=1/0
  7. local w, h, ww, hh
  8. local px,py,pz,ex,ey
  9. local clrscr
  10. local clgnd,clsky
  11. local emptyfunc = function() end
  12. local nomonitor={setCursorPos=emptyfunc, write=emptyfunc,
  13.                  setBackgroundColor=emptyfunc}
  14. local Objects,Obj,Objclick
  15. local scrClick, xClick, yClick
  16.  
  17. local function clrscr1()
  18.   for i=1,h do
  19.     for j=1,w do
  20.       if i<ey then
  21.         scrf[i][j]={c=clsky,d=inf}
  22.         scrl[i][j]={c=clsky,d=inf}
  23.         scrr[i][j]={c=clsky,d=inf}
  24.         scrb[i][j]={c=clsky,d=inf}
  25.       else
  26.         scrf[i][j]={c=clgnd,d=inf}
  27.         scrl[i][j]={c=clgnd,d=inf}
  28.         scrr[i][j]={c=clgnd,d=inf}
  29.         scrb[i][j]={c=clgnd,d=inf}
  30.       end
  31.     end
  32.   end
  33.   Obj=nil
  34. end
  35.  
  36. local function clrscr2()
  37.   for i=1,h do
  38.     for j=1,w do
  39.       scrf[i][j]={c=clsky,d=inf}
  40.       scrl[i][j]={c=clsky,d=inf}
  41.       scrr[i][j]={c=clsky,d=inf}
  42.       scrb[i][j]={c=clsky,d=inf}
  43.     end
  44.   end
  45.   Obj=nil
  46. end
  47.  
  48. function getPlayerPos()
  49.   return px,py,pz
  50. end
  51.  
  52. function setPlayerPos(x,y,z)
  53.   px,py,pz=x,y,z
  54. end
  55.  
  56. function movePlayer(x,y,z)
  57.   px,py,pz=px+x,py+y,pz+z
  58. end
  59.  
  60. local function getMonitor(monName)
  61.   if not monName then return nomonitor end
  62.   if peripheral.getType(monName)=='monitor' then
  63.     local mon=peripheral.wrap(monName)
  64.     mon.setTextScale(0.5)
  65.     return mon
  66.   else
  67.     error('Invalid monitor '..monName,3)
  68.   end
  69. end
  70.  
  71. function CreateWorld(mfront,mleft,mright,mback,_clgnd,_clsky)
  72.   monitors={}
  73.   front=getMonitor(mfront) if mfront then monitors[mfront]=scrf end
  74.   left =getMonitor(mleft)  if mleft  then monitors[mleft] =scrl end
  75.   right=getMonitor(mright) if mright then monitors[mright]=scrr end
  76.   back =getMonitor(mback)  if mback  then monitors[mback] =scrb end
  77.   setPlayerPos(0,1.62,0)
  78.   Objects={}
  79.   w,h=front.getSize()
  80.   ww=w/2 hh=h/3
  81.   ex,ey=ww+0.5, h-hh*py+0.5
  82.   for i=1,h do scrf[i]={} scrl[i]={} scrr[i]={} scrb[i]={} end
  83.   clsky=_clsky or _clgnd or colors.lightBlue
  84.   clgnd=_clgnd or colors.green
  85.   if clgnd==clsky then clrscr=clrscr2 else clrscr=clrscr1 end
  86.   clrscr()
  87. end
  88.  
  89. local function getScr(x,y,z)
  90.   local d
  91.   if Obj then x=x+Obj.x y=y+Obj.y z=z+Obj.z end
  92.   if z-pz>x-px then
  93.     if z-pz>px-x then
  94.       d=z-pz
  95.       return scrf,math.floor(ww*(x-px)/d+ex),math.floor(ey-hh*(2*(y-py)/d)),d
  96.     else
  97.       d=px-x
  98.       return scrl,math.floor(ww*(z-pz)/d+ex),math.floor(ey-hh*(2*(y-py)/d)),d
  99.     end
  100.   else
  101.     if z-pz>px-x then
  102.       d=x-px
  103.       return scrr,math.floor(ww*(pz-z)/d+ex),math.floor(ey-hh*(2*(y-py)/d)),d
  104.     else
  105.       d=pz-z
  106.       return scrb,math.floor(ww*(px-x)/d+ex),math.floor(ey-hh*(2*(y-py)/d)),d
  107.     end
  108.   end
  109. end
  110.  
  111. local function dot1(col,scr,dx,dy,d)
  112.   if scr[dy] and scr[dy][dx] and d<=scr[dy][dx].d then
  113.     scr[dy][dx].c=col
  114.     scr[dy][dx].d=d
  115.   end
  116. end
  117.  
  118. local function dot2(col,scr,dx,dy,d)
  119.   if scr[dy] and scr[dy][dx] and d<=scr[dy][dx].d then
  120.     scr[dy][dx].c=col
  121.     scr[dy][dx].d=d
  122.     if scr==scrClick and dx==xClick and dy==yClick then print(Obj.Name) Objclick=Obj end
  123.   end
  124. end
  125. local dot=dot1
  126.  
  127. function point(x,y,z,col)
  128.   dot(col,getScr(x,y,z))
  129. end
  130.  
  131. function vline(x,y1,y2,z,col)
  132.   local scr,dx,dy1,d=getScr(x,y1,z)
  133.   local _,_,dy2=getScr(x,y2,z)
  134.   for dy=dy1,dy2 do dot(col,scr,dx,dy,d) end
  135. end
  136.  
  137. local function line1(x1,x2,y1,y2,z1,z2,col)
  138.   local scr1,dx1,dy1=getScr(x1,y1,z1)
  139.   local scr2,dx2,dy2=getScr(x2,y2,z2)
  140.   if scr1==scr2 then
  141.     if math.abs(dx1-dx2)<2 and math.abs(dy1-dy2)<2 then return end
  142.   else
  143.     if math.min(dx1,dx2)<=1 and math.max(dx1,dx2)>=w then return end
  144.   end
  145.   local x=(x1+x2)/2
  146.   local y=(y1+y2)/2
  147.   local z=(z1+z2)/2
  148.   point(x,y,z,col)
  149.   line1(x1,x,y1,y,z1,z,col)
  150.   line1(x,x2,y,y2,z,z2,col)
  151. end
  152.  
  153. function line(x1,x2,y1,y2,z1,z2,col)
  154.   point(x1,y1,z1,col)
  155.   line1(x1,x2,y1,y2,z1,z2,col)
  156.   point(x2,y2,z2,col)
  157. end
  158.  
  159. local function wall1(x1,x2,y1,y2,y0,z1,z2,col)
  160.   local scr1,dx1=getScr(x1,y1,z1)
  161.   local scr2,dx2=getScr(x2,y2,z2)
  162.   if scr1==scr2 then
  163.     if math.abs(dx1-dx2)<2 then return end
  164.   else
  165.     if math.min(dx1,dx2)<=1 and math.max(dx1,dx2)>=w then return end
  166.   end
  167.   local x=(x1+x2)/2
  168.   local y=(y1+y2)/2
  169.   local z=(z1+z2)/2
  170.   vline(x,y,y0,z,col)
  171.   wall1(x1,x,y1,y,y0,z1,z,col)
  172.   wall1(x,x2,y,y2,y0,z,z2,col)
  173. end
  174.  
  175. function wall(x1,x2,y1,y2,y0,z1,z2,col,col2)
  176.   if col2 then
  177.     if (z2-z1)*(px-x1) < (x2-x1)*(pz-z1) then col=col2 end
  178.   end
  179.   if col then
  180.   vline(x1,y1,y0,z1,col)
  181.   wall1(x1,x2,y1,y2,y0,z1,z2,col)
  182.   vline(x2,y2,y0,z2,col)
  183.   end
  184. end
  185.  
  186. function draw()
  187.   for i=1,#Objects do
  188.     Obj=Objects[i]
  189.     Obj:paint()
  190.   end
  191.   for i=1,h do
  192.     front.setCursorPos(1,i)
  193.     left.setCursorPos(1,i)
  194.     right.setCursorPos(1,i)
  195.     back.setCursorPos(1,i)
  196.     for j=1,w do
  197.       front.setBackgroundColor(scrf[i][j].c) front.write(' ')
  198.       left.setBackgroundColor(scrl[i][j].c)  left.write(' ')
  199.       right.setBackgroundColor(scrr[i][j].c) right.write(' ')
  200.       back.setBackgroundColor(scrb[i][j].c)  back.write(' ')
  201.     end
  202.   end
  203.   clrscr()
  204. end
  205.  
  206. function CreateObj(x,y,z,paint,params)
  207.   local obj={x=x,y=y,z=z,paint=paint}
  208.   if type(params)=='table' then
  209.     for k,v in pairs(params) do obj[k]=v end
  210.   end
  211.   table.insert(Objects,obj)
  212.   return obj
  213. end
  214.  
  215. local nativePull
  216. if not nativePull then nativePull=os.pullEvent end
  217.  
  218. function os.pullEvent(_sFilter)
  219.   while true do
  220.     local eventData = { nativePull() }
  221.     if eventData[1] == "monitor_touch" then
  222.       if monitors[eventData[2]] then
  223.         Objclick=nil
  224.         scrClick=monitors[eventData[2]]
  225.         xClick=tonumber(eventData[3])
  226.         yClick=tonumber(eventData[4])
  227.         print(eventData[2],' ',xClick,' ', yClick)
  228.         dot=dot2
  229.         draw()
  230.         dot=dot1
  231.         if not Objclick then print('No object')
  232.         else
  233.           print('Clicked '..Objclick.Name)
  234.         end
  235. --        if Objclick and type(Objclick.onClick)=='function' then
  236. --          Objclick.onClick(Objclick)
  237. --        end
  238.       end
  239.     end
  240.     if not _sFilter or _sFilter==eventData[1] then return unpack( eventData ) end
  241.   end
  242. end
RAW Paste Data