Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Virtual room API
- -- Created by Zer0Galaxy
- -- No rights reserved
- local scrf, scrl, scrr, scrb={},{},{},{}
- local monitors={}
- local inf=1/0
- local w, h, ww, hh
- local px,py,pz,ex,ey
- local clrscr
- local clgnd,clsky
- local emptyfunc = function() end
- local nomonitor={setCursorPos=emptyfunc, write=emptyfunc,
- setBackgroundColor=emptyfunc}
- local Objects,Obj,Objclick
- local scrClick, xClick, yClick
- local function clrscr1()
- for i=1,h do
- for j=1,w do
- if i<ey then
- scrf[i][j]={c=clsky,d=inf}
- scrl[i][j]={c=clsky,d=inf}
- scrr[i][j]={c=clsky,d=inf}
- scrb[i][j]={c=clsky,d=inf}
- else
- scrf[i][j]={c=clgnd,d=inf}
- scrl[i][j]={c=clgnd,d=inf}
- scrr[i][j]={c=clgnd,d=inf}
- scrb[i][j]={c=clgnd,d=inf}
- end
- end
- end
- Obj=nil
- end
- local function clrscr2()
- for i=1,h do
- for j=1,w do
- scrf[i][j]={c=clsky,d=inf}
- scrl[i][j]={c=clsky,d=inf}
- scrr[i][j]={c=clsky,d=inf}
- scrb[i][j]={c=clsky,d=inf}
- end
- end
- Obj=nil
- end
- function getPlayerPos()
- return px,py,pz
- end
- function setPlayerPos(x,y,z)
- px,py,pz=x,y,z
- end
- function movePlayer(x,y,z)
- px,py,pz=px+x,py+y,pz+z
- end
- local function getMonitor(monName)
- if not monName then return nomonitor end
- if peripheral.getType(monName)=='monitor' then
- local mon=peripheral.wrap(monName)
- mon.setTextScale(0.5)
- return mon
- else
- error('Invalid monitor '..monName,3)
- end
- end
- function CreateWorld(mfront,mleft,mright,mback,_clgnd,_clsky)
- monitors={}
- front=getMonitor(mfront) if mfront then monitors[mfront]=scrf end
- left =getMonitor(mleft) if mleft then monitors[mleft] =scrl end
- right=getMonitor(mright) if mright then monitors[mright]=scrr end
- back =getMonitor(mback) if mback then monitors[mback] =scrb end
- setPlayerPos(0,1.62,0)
- Objects={}
- w,h=front.getSize()
- ww=w/2 hh=h/3
- ex,ey=ww+0.5, h-hh*py+0.5
- for i=1,h do scrf[i]={} scrl[i]={} scrr[i]={} scrb[i]={} end
- clsky=_clsky or _clgnd or colors.lightBlue
- clgnd=_clgnd or colors.green
- if clgnd==clsky then clrscr=clrscr2 else clrscr=clrscr1 end
- clrscr()
- end
- local function getScr(x,y,z)
- local d
- if Obj then x=x+Obj.x y=y+Obj.y z=z+Obj.z end
- if z-pz>x-px then
- if z-pz>px-x then
- d=z-pz
- return scrf,math.floor(ww*(x-px)/d+ex),math.floor(ey-hh*(2*(y-py)/d)),d
- else
- d=px-x
- return scrl,math.floor(ww*(z-pz)/d+ex),math.floor(ey-hh*(2*(y-py)/d)),d
- end
- else
- if z-pz>px-x then
- d=x-px
- return scrr,math.floor(ww*(pz-z)/d+ex),math.floor(ey-hh*(2*(y-py)/d)),d
- else
- d=pz-z
- return scrb,math.floor(ww*(px-x)/d+ex),math.floor(ey-hh*(2*(y-py)/d)),d
- end
- end
- end
- local function dot1(col,scr,dx,dy,d)
- if scr[dy] and scr[dy][dx] and d<=scr[dy][dx].d then
- scr[dy][dx].c=col
- scr[dy][dx].d=d
- end
- end
- local function dot2(col,scr,dx,dy,d)
- if scr[dy] and scr[dy][dx] and d<=scr[dy][dx].d then
- scr[dy][dx].c=col
- scr[dy][dx].d=d
- if scr==scrClick and dx==xClick and dy==yClick then print(Obj.Name) Objclick=Obj end
- end
- end
- local dot=dot1
- function point(x,y,z,col)
- dot(col,getScr(x,y,z))
- end
- function vline(x,y1,y2,z,col)
- local scr,dx,dy1,d=getScr(x,y1,z)
- local _,_,dy2=getScr(x,y2,z)
- for dy=dy1,dy2 do dot(col,scr,dx,dy,d) end
- end
- local function line1(x1,x2,y1,y2,z1,z2,col)
- local scr1,dx1,dy1=getScr(x1,y1,z1)
- local scr2,dx2,dy2=getScr(x2,y2,z2)
- if scr1==scr2 then
- if math.abs(dx1-dx2)<2 and math.abs(dy1-dy2)<2 then return end
- else
- if math.min(dx1,dx2)<=1 and math.max(dx1,dx2)>=w then return end
- end
- local x=(x1+x2)/2
- local y=(y1+y2)/2
- local z=(z1+z2)/2
- point(x,y,z,col)
- line1(x1,x,y1,y,z1,z,col)
- line1(x,x2,y,y2,z,z2,col)
- end
- function line(x1,x2,y1,y2,z1,z2,col)
- point(x1,y1,z1,col)
- line1(x1,x2,y1,y2,z1,z2,col)
- point(x2,y2,z2,col)
- end
- local function wall1(x1,x2,y1,y2,y0,z1,z2,col)
- local scr1,dx1=getScr(x1,y1,z1)
- local scr2,dx2=getScr(x2,y2,z2)
- if scr1==scr2 then
- if math.abs(dx1-dx2)<2 then return end
- else
- if math.min(dx1,dx2)<=1 and math.max(dx1,dx2)>=w then return end
- end
- local x=(x1+x2)/2
- local y=(y1+y2)/2
- local z=(z1+z2)/2
- vline(x,y,y0,z,col)
- wall1(x1,x,y1,y,y0,z1,z,col)
- wall1(x,x2,y,y2,y0,z,z2,col)
- end
- function wall(x1,x2,y1,y2,y0,z1,z2,col,col2)
- if col2 then
- if (z2-z1)*(px-x1) < (x2-x1)*(pz-z1) then col=col2 end
- end
- if col then
- vline(x1,y1,y0,z1,col)
- wall1(x1,x2,y1,y2,y0,z1,z2,col)
- vline(x2,y2,y0,z2,col)
- end
- end
- function draw()
- for i=1,#Objects do
- Obj=Objects[i]
- Obj:paint()
- end
- for i=1,h do
- front.setCursorPos(1,i)
- left.setCursorPos(1,i)
- right.setCursorPos(1,i)
- back.setCursorPos(1,i)
- for j=1,w do
- front.setBackgroundColor(scrf[i][j].c) front.write(' ')
- left.setBackgroundColor(scrl[i][j].c) left.write(' ')
- right.setBackgroundColor(scrr[i][j].c) right.write(' ')
- back.setBackgroundColor(scrb[i][j].c) back.write(' ')
- end
- end
- clrscr()
- end
- function CreateObj(x,y,z,paint,params)
- local obj={x=x,y=y,z=z,paint=paint}
- if type(params)=='table' then
- for k,v in pairs(params) do obj[k]=v end
- end
- table.insert(Objects,obj)
- return obj
- end
- local nativePull
- if not nativePull then nativePull=os.pullEvent end
- function os.pullEvent(_sFilter)
- while true do
- local eventData = { nativePull() }
- if eventData[1] == "monitor_touch" then
- if monitors[eventData[2]] then
- Objclick=nil
- scrClick=monitors[eventData[2]]
- xClick=tonumber(eventData[3])
- yClick=tonumber(eventData[4])
- print(eventData[2],' ',xClick,' ', yClick)
- dot=dot2
- draw()
- dot=dot1
- if not Objclick then print('No object')
- else
- print('Clicked '..Objclick.Name)
- end
- -- if Objclick and type(Objclick.onClick)=='function' then
- -- Objclick.onClick(Objclick)
- -- end
- end
- end
- if not _sFilter or _sFilter==eventData[1] then return unpack( eventData ) end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement