jacob614

tpt drawing functions lua (from cracker64's script)

Nov 4th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.61 KB | None | 0 0
  1. energytypes = { [18]=true, [31]=true, [136]=true, }
  2. validwalls = { [222]=true, [223]=true, [224]=true, [225]=true, [227]=true, [228]=true, [229]=true, [230]=true, [231]=true, [232]=true, [233]=true, [234]=true, [235]=true, [240]=true, [242]=true, [245]=true, }
  3. tools = { [236]=true, [237]=true, [238]=true, [239]=true, }
  4. setctype = { [9]=true,[74]=true,[83]=true,[85]=true,[93]=true,[153]=true,}
  5.  
  6. function create(x,y,c,small)
  7.     local t = c%256
  8.     local v = math.floor(c/256)
  9.     if t==237 or t==238 then --heat and cool!
  10.         local temp = tpt.get_property("type",x,y)>0 and tpt.get_property("temp",x,y) or nil
  11.         if t==237 and temp~=nil and temp<9999 then
  12.             local heatchange = 4 --implement more temp changes later (ctrl-shift)
  13.             tpt.set_property("temp",math.min(temp+heatchange,9999),x,y)
  14.         elseif t==238 and temp~=nil and temp>0 then
  15.             local heatchange = 4
  16.             tpt.set_property("temp",math.max(temp-heatchange,0),x,y)
  17.         end
  18.         return
  19.     end
  20.     if small then --set ctype for CLNE types if smallish brush
  21.         if not setctype[c] then
  22.             local cm = tpt.get_property("type",x,y)
  23.             if setctype[cm] then
  24.                 if (cm==74 or cm==153) and (c==35 or c==36) then return end --don't give PCLN ctypes of PSCN or NSCN
  25.                 tpt.set_property("ctype",c,x,y)
  26.                 return
  27.             end
  28.         end
  29.     end
  30.     if t==0 then
  31.         tpt.delete(x,y)
  32.     else
  33.         local i = tpt.create(x,y,t)
  34.         --special case for GoL creation
  35.         if t==78 and i>=0 then tpt.set_property("ctype",v,i) tpt.set_property("tmp",GoLrule[(v+2)][10],i) end
  36.     end
  37. end
  38.  
  39. function create_line(x1,y1,x2,y2,rx,ry,c,brush) -- Taken From Powder Toy Source Code
  40.    if c == 87 or c == 158 then return end --never do lines of FIGH and LIGH
  41.    local cp = math.abs(y2-y1)>math.abs(x2-x1)
  42.    local x = 0 local y = 0 local dx = 0 local dy = 0 local sy = 0 local e = 0.0 local de = 0.0 local first = true
  43.    if cp then y = x1 x1 = y1 y1 = y y = x2 x2 = y2 y2 = y end
  44.    if x1 > x2 then y = x1 x1 = x2 x2 = y y = y1 y1 = y2 y2 = y end
  45.    dx = x2 - x1 dy = math.abs(y2 - y1) if dx ~= 0 then de = dy/dx end
  46.    y = y1 if y1 < y2 then sy = 1 else sy = -1 end
  47.    for x = x1, x2 do
  48.       if cp then
  49.          drawstuff(y,x,rx,ry,c,first,brush)
  50.       else
  51.          drawstuff(x,y,rx,ry,c,first,brush)
  52.       end
  53.       first = false
  54.       e = e + de
  55.       if e >= 0.5 then
  56.          y = y + sy
  57.          e = e - 1
  58.          if y1<y2 then
  59.              if y>y2 then return end
  60.          elseif y<y2 then return end
  61.          if (rx+ry)==0 or c>=222 then
  62.             if cp then
  63.                drawstuff(y,x,rx,ry,c,first,brush)
  64.             else
  65.                drawstuff(x,y,rx,ry,c,first,brush)
  66.             end
  67.          end
  68.       end
  69.    end
  70. end
  71.  
  72. function create_box(x1,y1,x2,y2,c)
  73.    local i = 0 local j = 0
  74.    if x1>x2 then i=x2 x2=x1 x1=i end
  75.    if y1>y2 then j=y2 y2=y1 y1=j end
  76.    for j=y1, y2 do
  77.       for i=x1, x2 do
  78.         create(i,j,c)
  79.       end
  80.    end
  81. end
  82.  
  83. function incurrentbrush(i,j,rx,ry,brush)
  84.     if brush==0 then
  85.         return (math.pow(i,2)*math.pow(ry,2)+math.pow(j,2)*math.pow(rx,2)<=math.pow(rx,2)*math.pow(ry,2))
  86.     elseif brush==1 then
  87.         return (math.abs(i)<=rx and math.abs(j)<=ry)
  88.     elseif brush==2 then
  89.         return ((math.abs((rx+2*i)*ry+rx*j) + math.abs(2*rx*(j-ry)) + math.abs((rx-2*i)*ry+rx*j))<=(4*rx*ry))
  90.     else
  91.         return false
  92.     end
  93. end
  94.  
  95. function drawstuff(x,y,rx,ry,c,fill,brush) -- Draws or erases elements
  96.    local energycheck = energytypes[c]
  97.    local small = (rx+ry)<=16
  98.    if c == 87 or c == 158 then create(x,y,c) return end --only draw one pixel of FIGH and LIGH
  99.    --walls!
  100.    local dw = false local b = 0
  101.    if validwalls[c] then
  102.       dw = true
  103.       if c~=230 then b = c-100 end
  104.    end
  105.    if tools[c] then fill = true end
  106.    if dw then
  107.       local r=math.floor(rx/4)
  108.       local wx = math.floor(x/4)- math.floor(r/2)
  109.       local wy = math.floor(y/4)- math.floor(r/2)
  110.       if b==125 then
  111.          wx = wx + math.floor(r/2)
  112.          wy = wy + math.floor(r/2)
  113.          for v=-1,1 do
  114.             for u=-1,1 do
  115.                 if (wx+u>=0 and wx+u<153 and wy+v>=0 and wy+v<96 and tpt.get_wallmap(wx+u,wy+v)==125) then
  116.                     return
  117.                 end end end
  118.          tpt.set_wallmap(wx,wy,b)
  119.          return
  120.       end
  121.       tpt.set_wallmap(wx,wy,r+1,r+1,b)
  122.       return
  123.    end
  124.    if rx<=0 then--0 radius loop prevention
  125.       for j=y-ry,y+ry do
  126.          if valid(x,j,energycheck,c) then
  127.             create(x,j,c,small) end
  128.       end
  129.       return
  130.    end
  131.    local tempy = y local oldy = y
  132.    if brush==2 then tempy=y+ry end
  133.    for i = x - rx, x do
  134.       oldy = tempy
  135.       local check = incurrentbrush(i-x,tempy-y,rx,ry,brush)
  136.       if check then
  137.           while check do
  138.              tempy = tempy - 1
  139.              check = incurrentbrush(i-x,tempy-y,rx,ry,brush)
  140.           end
  141.           tempy = tempy + 1
  142.           if fill then
  143.              local jmax = 2*y - tempy
  144.              if brush == 2 then jmax=y+ry end
  145.              for j = tempy, jmax do
  146.                 if valid(i,j,energycheck,c) then
  147.                    create(i,j,c,small) end
  148.                 if i~=x and valid(x+x-i,j,energycheck,c) then
  149.                    create(x+x-i,j,c,small) end
  150.              end
  151.           else
  152.              if (oldy ~= tempy and brush~=1) or i==x-rx then oldy = oldy - 1 end
  153.              for j = tempy, oldy+1 do
  154.                 local i2=2*x-i local j2 = 2*y-j
  155.                 if brush==2 then j2=y+ry end
  156.                 if valid(i,j,energycheck,c) then
  157.                    create(i,j,c,small) end
  158.                 if i2~=i and valid(i2,j,energycheck,c) then
  159.                    create(i2,j,c,small) end
  160.                 if j2~=j and valid(i,j2,energycheck,c) then
  161.                    create(i,j2,c,small) end
  162.                 if i2~=i and j2~=j and valid(i2,j2,energycheck,c) then
  163.                    create(i2,j2,c,small) end
  164.              end
  165.           end
  166.        end
  167.    end
  168. end
  169. function valid(x,y,energycheck,c)
  170.     if x >= 0 and x < 612 and y >= 0 and y < 384 then
  171.         if energycheck then
  172.             if energytypes[tpt.get_property("type",x,y)] then return false end
  173.         end
  174.     return true end
  175. end
  176. function floodparts(x,y,c,cm,bm)
  177.     local x1=x local x2=x
  178.     if cm==-1 then
  179.         if c==0 then
  180.             cm = tpt.get_property("type",x,y)
  181.             if cm==0 then return false end
  182.         else
  183.             cm = 0
  184.         end
  185.     end
  186.     --wall check here
  187.     while x1>=4 do
  188.         if (tpt.get_property("type",x1-1,y)~=cm) then break end
  189.         x1 = x1-1
  190.     end
  191.     while x2<=608 do
  192.         if (tpt.get_property("type",x2+1,y)~=cm) then break end
  193.         x2 = x2+1
  194.     end
  195.     for x=x1, x2 do
  196.         if c==0 then tpt.delete(x,y) end
  197.         if c>0 and c<222 then create(x,y,c) end
  198.     end
  199.     if y>=5 then
  200.         for x=x1,x2 do
  201.             if tpt.get_property("type",x,y-1)==cm then
  202.                 if not floodparts(x,y-1,c,cm,bm) then
  203.                     return false
  204.                 end end end
  205.     end
  206.     if y<379 then
  207.         for x=x1,x2 do
  208.             if tpt.get_property("type",x,y+1)==cm then
  209.                 if not floodparts(x,y+1,c,cm,bm) then
  210.                     return false
  211.                 end end end
  212.     end
  213.     return true
  214. end
  215.  
  216. function clearsim()
  217.     tpt.start_getPartIndex()
  218.     while tpt.next_getPartIndex() do
  219.        local index = tpt.getPartIndex()
  220.        tpt.set_property("type",0,index)
  221.     end
  222.     tpt.reset_velocity(0,0,153,96)
  223.     tpt.set_pressure(0,0,153,96,0)
  224.     tpt.set_wallmap(0,0,153,96,0)
  225. end
Add Comment
Please, Sign In to add comment