Advertisement
electronic_steve

portalgun script (new portal)

Apr 18th, 2015
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.89 KB | None | 0 0
  1.  
  2.  
  3. local portalgun={}
  4.  
  5. local colors={ -- portalgun colors
  6.     {0,0,0},
  7.     {204,204,204},
  8.     {244,217,37},
  9.     {239,239,239},
  10.     {170,170,170},
  11.     {48,48,48},
  12.     {32,32,32}
  13. }
  14.  
  15. stkm={}
  16. stkm.alivetest=true
  17. stkm.oldalivetest=false
  18. stkm.a=0
  19. stkm.b=0
  20. stkm.x=0
  21. stkm.y=0
  22.  
  23. local img={ -- portalgun image
  24.     {1,2,2,1},
  25.     {2,4,4,5},
  26.     {3,4,4,5},
  27.     {1,5,5,1},
  28.     {1,6,6,1},
  29.     {6,1,1,7},
  30.     {6,1,1,7}
  31. }
  32. local point={}
  33. imgx=7
  34. imgy=4
  35. local altimg={}
  36.  portal={}
  37. portal.radius=10
  38. portal.altradius=6
  39. portal.kill=false
  40. local rottbl={}
  41. for x=-1,1 do
  42.     rottbl[x]={}
  43. for y=-1,1 do
  44.     rottbl[x][y]={0,0,0,0}
  45. end
  46. end
  47. rottbl[-1][ 1]={-1,-1}
  48. rottbl[ 0][ 1]={-1, 0}
  49. rottbl[ 1][ 1]={-1, 1}
  50. rottbl[ 1][ 0]={ 0,-1}
  51. rottbl[ 0][ 0]={ 0, 0}
  52. rottbl[-1][ 0]={ 0, 1}
  53. rottbl[ 1][-1]={ 1,-1}
  54. rottbl[ 0][-1]={ 1, 0}
  55. rottbl[ 1][-1]={ 1, 1}
  56.  
  57. local altrot={}
  58. for x=-1,1 do
  59.     altrot[x]={}
  60. for y=-1,1 do
  61.     altrot[x][y]={0,0,0,0}
  62. end
  63. end
  64. altrot[-1][-1]={-1, 1 ,-1 , 0 , 1 , 225}
  65. altrot[-1][ 0]={ 0, 1 , 0 , 0 , 0 , 180}
  66. altrot[-1][ 1]={ 1, 1 ,-1 , 0 , 2 , 135}
  67. altrot[ 0][-1]={ 1, 0 , 0 , 0 , 0 , 270}
  68. altrot[ 0][ 0]={ 0, 0 , 0 , 0 , 0 , 0  }
  69. altrot[ 0][ 1]={-1, 0 , 0 , 0 , 0 , 90 }
  70. altrot[ 1][-1]={ 1, 1 , 1 , 0 , 1 , 315}
  71. altrot[ 1][ 0]={ 0,-1 , 0 , 0 , 0 , 0  }
  72. altrot[ 1][ 1]={ 1,-1 , 1 , 0 , 2 , 45 }
  73.  
  74.  
  75.  
  76. -------------------------------------------------------
  77. --  +-1 +                                            --
  78. -- -1 C 1                                            --
  79. --  + 1 +                                            --
  80. -------------------------------------------------------
  81. --  3/4*PI PI/2   PI/4                               --
  82. --  PI            2*PI                               --
  83. --  5/4*PI 3/2*PI 7/4*PI                             --
  84. -------------------------------------------------------
  85. -- 2.3561944901923  1.5707963267949  0.7853981633974 --
  86. -- 3.1415926535898  0                6.2831853071796 --
  87. -- 3.9269908169872  4.7123889803847  5.4977871437821 --
  88. -------------------------------------------------------
  89. -- 225 270 315                                       --
  90. -- 180     0                                         --
  91. -- 135 90  45                                        --
  92. -------------------------------------------------------
  93.  
  94. local fix=true
  95. local function stk2fix()
  96.     if fix then fix=nil
  97.         tpt.element_func(stk2func,tpt.element("stk2"))
  98.         tpt.unregister_step(stk2fix) end
  99. end
  100. tpt.register_step(stk2fix)
  101. local PRTL = elem.allocate("ESMD", "PRTL")
  102. elem.element(PRTL, elem.element(elem.DEFAULT_PT_DMND))
  103. elem.property(PRTL, "Name", "PRTL")
  104. elem.property(PRTL, "Description", "Portal.")
  105. elem.property(PRTL, "MenuVisible", 0)
  106. elem.property(PRTL, "Colour", 0xF4D925)
  107.  
  108. function math.interval(min,n,max)
  109.     local x=math.max(n,min)
  110.     return math.min(x,max)
  111. end
  112.  
  113. function math.limit(n1,n2)
  114.     local x=math.max(n1,1)
  115.     return math.min(x,n2)
  116. end
  117.  
  118. function math.round(num, idp)
  119.     local mult = 10^(idp or 0)
  120.     return math.floor(num * mult + 0.5) / mult
  121. end
  122.  
  123. function rot(x,y,cx,cy,cos,sin)
  124.     local ix,iy
  125.     if sin==nil then
  126.          ix=(x+cx)*math.cos(cos)-(y+cy)*math.sin(cos)
  127.          iy=(x+cx)*math.sin(cos)+(y+cy)*math.cos(cos)
  128.     else
  129.          ix=(x+cx)*cos-(y+cy)*sin
  130.          iy=(x+cx)*sin+(y+cy)*cos
  131.     end
  132.     return ix,iy
  133. end
  134.  
  135. function stk2func(I,x,y)
  136.     local mousex,mousey=sim.adjustCoords(tpt.mousex, tpt.mousey)
  137.     stkm.a,stkm.b=mousex-x,mousey-y
  138.     stkm.x,stkm.y=x,y
  139.     stkm.vx=tpt.get_property("vx",I)
  140.     stkm.vy=tpt.get_property("vy",I)
  141.     portalgun.draw()
  142.     stkm.alivetest= not stkm.alivetest
  143.     local portalid,portalid2
  144.     local c=math.sqrt(stkm.vx^2+stkm.vy^2)
  145.     local cos=stkm.vx/c
  146.     local sin=stkm.vy/c
  147.     local nx,ny
  148.     local deg
  149.     for i=1,12 do
  150.         x=x+cos
  151.         y=y+sin
  152.         if tpt.get_property("type",math.limit(x,611),math.limit(y,383))==tpt.element("PRTL") then
  153.             if tpt.get_property("ctype",x,y)==1 then portalid=1 portalid2=2 else portalid=2 portalid2=1 end
  154.             local angle2=altrot[portal[portalid2][3]][portal[portalid2][4]][6]
  155.             local angle1=altrot[-portal[portalid][3]][-portal[portalid][4]][6]
  156.             local deg=angle2 - angle1
  157.             nx,ny=rot(stkm.vx,stkm.vy,0,0,math.rad(deg))
  158.             stkm.vx=nx
  159.             stkm.vy=ny
  160.             tpt.delete(I)
  161.             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"))
  162.             if nid==-1 then
  163.             else
  164.             tpt.set_property("vx",nx,nid)
  165.             tpt.set_property("vy",ny,nid)
  166.             stkm.vx=nx
  167.             stkm.vy=ny
  168.             end
  169.             break
  170.         end
  171.     end
  172. end
  173.  
  174. function portalgun.createimage()
  175.     local c=math.sqrt(stkm.a^2+stkm.b^2)
  176.     local cos=stkm.a/c
  177.     local sin=stkm.b/c
  178.     altimg={}
  179. for x=1,imgx do
  180.     for y=1,imgy do
  181.         local dx,dy=rot(x,y,4,-3,cos,sin)
  182.         dx,dy=dx+stkm.x,dy+stkm.y
  183.         if x==6 and y==3 then portalgun.shot={dx,dy}  end
  184.         altimg[#altimg+1]={dx,dy,img[x][y]}
  185.     end
  186.  
  187. end
  188.  
  189. end
  190. function portalgun.draw()
  191.     for i=1,#altimg do
  192.         tpt.drawpixel(
  193.             math.limit(altimg[i][1],611),
  194.             math.limit(altimg[i][2],383),
  195.             colors[altimg[i][3]][1],
  196.             colors[altimg[i][3]][2],
  197.             colors[altimg[i][3]][3])
  198.     end
  199.     pointdraw()
  200. end
  201.  
  202. function portalgun.update()
  203.     if tpt.set_pause()==1 and stkm.alive then
  204.         portalgun.draw()
  205.         else
  206.         portalgun.createimage()
  207.     end
  208.     if tpt.set_pause()==0 then
  209.         if stkm.alivetest==stkm.oldalivetest then
  210.             stkm.alive=false
  211.         else
  212.             stkm.oldalivetest=not stkm.oldalivetest
  213.             stkm.alive=true
  214.         end
  215.     end
  216. end
  217. function keytestfunc(key, nkey, modifier, event)
  218.     if key=="e" or key=="s" or key=="q" and tpt.set_pause()==0 then
  219.         if event==1 then
  220.             tpt.set_shortcuts(0)
  221.         else
  222.             tpt.set_shortcuts(1)
  223.         end
  224.     end
  225.     if key=="e" and event==1 and tpt.set_pause()==0 then
  226.         if stkm.alive then
  227.             local c=math.sqrt(stkm.a^2+stkm.b^2)
  228.             local x,y=portalgun.shot[1],portalgun.shot[2]
  229.         for Line=1,300 do
  230.             x=x+stkm.a/c
  231.             y=y+stkm.b/c
  232.             local test=quadtest(x,y)
  233.         if test then  break end
  234.         end
  235.     end
  236.     end
  237.     if key=="q"  and tpt.set_pause()==0 then
  238.         if event==1 then portal.kill=true else portal.kill=false end
  239.         for i=1,#portal do
  240.         portal[i]=nil
  241.         end
  242.     end
  243. end
  244.  
  245.  
  246.  
  247.  
  248.  
  249. function quadtest(x,y)
  250.     local nx=0
  251.     local ny=0
  252.     local brk=false
  253.     local J=0
  254.     for wx=-1,1 do
  255.         for wy=-1,1 do
  256.             if tpt.get_property("type",math.limit(x+wx,611),math.limit(y+wy,383))==0 then  else J=J+1 brk=true
  257.                 nx=nx+wx
  258.                 ny=ny+wy
  259.             end
  260.         end
  261.     end
  262.     nx=math.interval(-1,nx,1)
  263.     ny=math.interval(-1,ny,1)
  264.     if J>0 then
  265.         local xfix=altrot[nx][ny][1]
  266.         local yfix=altrot[nx][ny][2]
  267.         local portalradius
  268.         if altrot[nx][ny][5]==0 then portalradius=portal.radius
  269.         else
  270.             portalradius=portal.altradius
  271.         end
  272.         portal.create(x,y,nx,ny,J,portalradius,xfix,yfix)
  273.     else
  274.         point[#point+1]={x,y,colors[3][1],colors[3][2],colors[3][3],100}
  275.     end
  276.     if brk then return true else return false end
  277. end
  278.    
  279.    
  280.  
  281. function portal.create(x,y,nx,ny,J,radius,perpx,perpy)
  282.     if #portal<=1 then
  283.        
  284.         local brk=false
  285.         if  J==3 then else x=x+altrot[nx][ny][3]
  286.         end
  287.         local yfix=0
  288.         if altrot[nx][ny][5]==1 then yfix=1 elseif altrot[nx][ny][5]==2 then   yfix=-1  end
  289.         for L=-radius,radius do
  290.             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
  291.         end
  292.         for L=-radius,radius do
  293.             if tpt.get_property("type",math.limit(x+perpx*L,611),math.limit(y+perpy*L,383))==0 then else brk=true end
  294.         end
  295.         if brk==false then
  296.             portal[#portal+1]={x,y,nx,ny,#portal+1}
  297.             for L=-radius,radius do
  298.                 local id=tpt.create(math.limit(x+perpx*L,611),math.limit(y+perpy*L,383),tpt.element("PRTL"))
  299.                 tpt.set_property("tmp",nx,id)
  300.                 tpt.set_property("tmp2",ny,id)
  301.                 tpt.set_property("ctype",#portal,id)
  302.             end
  303.         end
  304.     end
  305. end
  306.  
  307. function pointdraw()
  308.     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
  309.     point={}
  310. end
  311. function PRTLfunc(i,x,y)
  312.     if portal.kill then tpt.delete(i) end
  313. end
  314.  
  315. tpt.register_keypress(keytestfunc)
  316. tpt.register_step(portalgun.update)
  317. tpt.element_func(PRTLfunc,tpt.element("PRTL"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement