Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local portalgun={}
- local colors={ -- portalgun colors
- {0,0,0},
- {204,204,204},
- {244,217,37},
- {239,239,239},
- {170,170,170},
- {48,48,48},
- {32,32,32}
- }
- stkm={}
- stkm.alivetest=true
- stkm.oldalivetest=false
- stkm.a=0
- stkm.b=0
- stkm.x=0
- stkm.y=0
- local img={ -- portalgun image
- {1,2,2,1},
- {2,4,4,5},
- {3,4,4,5},
- {1,5,5,1},
- {1,6,6,1},
- {6,1,1,7},
- {6,1,1,7}
- }
- local point={}
- imgx=7
- imgy=4
- local altimg={}
- portal={}
- portal.radius=10
- portal.altradius=6
- portal.kill=false
- local rottbl={}
- for x=-1,1 do
- rottbl[x]={}
- for y=-1,1 do
- rottbl[x][y]={0,0,0,0}
- end
- end
- rottbl[-1][ 1]={-1,-1}
- rottbl[ 0][ 1]={-1, 0}
- rottbl[ 1][ 1]={-1, 1}
- rottbl[ 1][ 0]={ 0,-1}
- rottbl[ 0][ 0]={ 0, 0}
- rottbl[-1][ 0]={ 0, 1}
- rottbl[ 1][-1]={ 1,-1}
- rottbl[ 0][-1]={ 1, 0}
- rottbl[ 1][-1]={ 1, 1}
- local altrot={}
- for x=-1,1 do
- altrot[x]={}
- for y=-1,1 do
- altrot[x][y]={0,0,0,0}
- end
- end
- altrot[-1][-1]={-1, 1 ,-1 , 0 , 1 , 225}
- altrot[-1][ 0]={ 0, 1 , 0 , 0 , 0 , 180}
- altrot[-1][ 1]={ 1, 1 ,-1 , 0 , 2 , 135}
- altrot[ 0][-1]={ 1, 0 , 0 , 0 , 0 , 270}
- altrot[ 0][ 0]={ 0, 0 , 0 , 0 , 0 , 0 }
- altrot[ 0][ 1]={-1, 0 , 0 , 0 , 0 , 90 }
- altrot[ 1][-1]={ 1, 1 , 1 , 0 , 1 , 315}
- altrot[ 1][ 0]={ 0,-1 , 0 , 0 , 0 , 0 }
- altrot[ 1][ 1]={ 1,-1 , 1 , 0 , 2 , 45 }
- -------------------------------------------------------
- -- +-1 + --
- -- -1 C 1 --
- -- + 1 + --
- -------------------------------------------------------
- -- 3/4*PI PI/2 PI/4 --
- -- PI 2*PI --
- -- 5/4*PI 3/2*PI 7/4*PI --
- -------------------------------------------------------
- -- 2.3561944901923 1.5707963267949 0.7853981633974 --
- -- 3.1415926535898 0 6.2831853071796 --
- -- 3.9269908169872 4.7123889803847 5.4977871437821 --
- -------------------------------------------------------
- -- 225 270 315 --
- -- 180 0 --
- -- 135 90 45 --
- -------------------------------------------------------
- local fix=true
- local function stk2fix()
- if fix then fix=nil
- tpt.element_func(stk2func,tpt.element("stk2"))
- tpt.unregister_step(stk2fix) end
- end
- tpt.register_step(stk2fix)
- local PRTL = elem.allocate("ESMD", "PRTL")
- elem.element(PRTL, elem.element(elem.DEFAULT_PT_DMND))
- elem.property(PRTL, "Name", "PRTL")
- elem.property(PRTL, "Description", "Portal.")
- elem.property(PRTL, "MenuVisible", 0)
- elem.property(PRTL, "Colour", 0xF4D925)
- function math.interval(min,n,max)
- local x=math.max(n,min)
- return math.min(x,max)
- end
- function math.limit(n1,n2)
- local x=math.max(n1,1)
- return math.min(x,n2)
- end
- function math.round(num, idp)
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function rot(x,y,cx,cy,cos,sin)
- local ix,iy
- if sin==nil then
- ix=(x+cx)*math.cos(cos)-(y+cy)*math.sin(cos)
- iy=(x+cx)*math.sin(cos)+(y+cy)*math.cos(cos)
- else
- ix=(x+cx)*cos-(y+cy)*sin
- iy=(x+cx)*sin+(y+cy)*cos
- end
- return ix,iy
- end
- function stk2func(I,x,y)
- local mousex,mousey=sim.adjustCoords(tpt.mousex, tpt.mousey)
- stkm.a,stkm.b=mousex-x,mousey-y
- stkm.x,stkm.y=x,y
- stkm.vx=tpt.get_property("vx",I)
- stkm.vy=tpt.get_property("vy",I)
- portalgun.draw()
- stkm.alivetest= not stkm.alivetest
- local portalid,portalid2
- local c=math.sqrt(stkm.vx^2+stkm.vy^2)
- local cos=stkm.vx/c
- local sin=stkm.vy/c
- local nx,ny
- local deg
- for i=1,12 do
- x=x+cos
- y=y+sin
- if tpt.get_property("type",math.limit(x,611),math.limit(y,383))==tpt.element("PRTL") then
- if tpt.get_property("ctype",x,y)==1 then portalid=1 portalid2=2 else portalid=2 portalid2=1 end
- local angle2=altrot[portal[portalid2][3]][portal[portalid2][4]][6]
- local angle1=altrot[-portal[portalid][3]][-portal[portalid][4]][6]
- local deg=angle2 - angle1
- nx,ny=rot(stkm.vx,stkm.vy,0,0,math.rad(deg))
- stkm.vx=nx
- stkm.vy=ny
- tpt.delete(I)
- local nid=tpt.create(math.limit(portal[portalid2][1]+nx-portal[portalid2][3],611),math.limit(portal[portalid2][2]+ny-portal[portalid2][4],383),tpt.element("stk2"))
- if nid==-1 then
- else
- tpt.set_property("vx",nx,nid)
- tpt.set_property("vy",ny,nid)
- stkm.vx=nx
- stkm.vy=ny
- end
- break
- end
- end
- end
- function portalgun.createimage()
- local c=math.sqrt(stkm.a^2+stkm.b^2)
- local cos=stkm.a/c
- local sin=stkm.b/c
- altimg={}
- for x=1,imgx do
- for y=1,imgy do
- local dx,dy=rot(x,y,4,-3,cos,sin)
- dx,dy=dx+stkm.x,dy+stkm.y
- if x==6 and y==3 then portalgun.shot={dx,dy} end
- altimg[#altimg+1]={dx,dy,img[x][y]}
- end
- end
- end
- function portalgun.draw()
- for i=1,#altimg do
- tpt.drawpixel(
- math.limit(altimg[i][1],611),
- math.limit(altimg[i][2],383),
- colors[altimg[i][3]][1],
- colors[altimg[i][3]][2],
- colors[altimg[i][3]][3])
- end
- pointdraw()
- end
- function portalgun.update()
- if tpt.set_pause()==1 and stkm.alive then
- portalgun.draw()
- else
- portalgun.createimage()
- end
- if tpt.set_pause()==0 then
- if stkm.alivetest==stkm.oldalivetest then
- stkm.alive=false
- else
- stkm.oldalivetest=not stkm.oldalivetest
- stkm.alive=true
- end
- end
- end
- function keytestfunc(key, nkey, modifier, event)
- if key=="e" or key=="s" or key=="q" and tpt.set_pause()==0 then
- if event==1 then
- tpt.set_shortcuts(0)
- else
- tpt.set_shortcuts(1)
- end
- end
- if key=="e" and event==1 and tpt.set_pause()==0 then
- if stkm.alive then
- local c=math.sqrt(stkm.a^2+stkm.b^2)
- local x,y=portalgun.shot[1],portalgun.shot[2]
- for Line=1,300 do
- x=x+stkm.a/c
- y=y+stkm.b/c
- local test=quadtest(x,y)
- if test then break end
- end
- end
- end
- if key=="q" and tpt.set_pause()==0 then
- if event==1 then portal.kill=true else portal.kill=false end
- for i=1,#portal do
- portal[i]=nil
- end
- end
- end
- function quadtest(x,y)
- local nx=0
- local ny=0
- local brk=false
- local J=0
- for wx=-1,1 do
- for wy=-1,1 do
- if tpt.get_property("type",math.limit(x+wx,611),math.limit(y+wy,383))==0 then else J=J+1 brk=true
- nx=nx+wx
- ny=ny+wy
- end
- end
- end
- nx=math.interval(-1,nx,1)
- ny=math.interval(-1,ny,1)
- if J>0 then
- local xfix=altrot[nx][ny][1]
- local yfix=altrot[nx][ny][2]
- local portalradius
- if altrot[nx][ny][5]==0 then portalradius=portal.radius
- else
- portalradius=portal.altradius
- end
- portal.create(x,y,nx,ny,J,portalradius,xfix,yfix)
- else
- point[#point+1]={x,y,colors[3][1],colors[3][2],colors[3][3],100}
- end
- if brk then return true else return false end
- end
- function portal.create(x,y,nx,ny,J,radius,perpx,perpy)
- if #portal<=1 then
- local brk=false
- if J==3 then else x=x+altrot[nx][ny][3]
- end
- local yfix=0
- if altrot[nx][ny][5]==1 then yfix=1 elseif altrot[nx][ny][5]==2 then yfix=-1 end
- for L=-radius,radius do
- if tpt.get_property("type",math.limit(x+perpx*L+nx,611),math.limit(y+perpy*L+ny+yfix,383))==0 then brk=true end
- end
- for L=-radius,radius do
- if tpt.get_property("type",math.limit(x+perpx*L,611),math.limit(y+perpy*L,383))==0 then else brk=true end
- end
- if brk==false then
- portal[#portal+1]={x,y,nx,ny,#portal+1}
- for L=-radius,radius do
- local id=tpt.create(math.limit(x+perpx*L,611),math.limit(y+perpy*L,383),tpt.element("PRTL"))
- tpt.set_property("tmp",nx,id)
- tpt.set_property("tmp2",ny,id)
- tpt.set_property("ctype",#portal,id)
- end
- end
- end
- end
- function pointdraw()
- for i=1,#point do tpt.drawpixel(point[i][1],point[i][2],point[i][3],point[i][4],point[i][5],point[i][6]) end
- point={}
- end
- function PRTLfunc(i,x,y)
- if portal.kill then tpt.delete(i) end
- end
- tpt.register_keypress(keytestfunc)
- tpt.register_step(portalgun.update)
- tpt.element_func(PRTLfunc,tpt.element("PRTL"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement