Advertisement
Scien_tific

CMND element script for TPT

Apr 15th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.52 KB | None | 0 0
  1. ---------------------------------------
  2. ---Scien_Tific's CMND element script---
  3. --------------ver. 16419---------------
  4.  
  5. --!!!Scroll down at own risk!!!
  6.  
  7.  
  8.  
  9.  
  10. --Variables
  11. local id = elements.allocate("cmndMod","cmnd")
  12.  
  13.  
  14.  
  15.  
  16. --Sparking function
  17. local function spark(i,e)
  18.     if sim.partProperty(i,sim.FIELD_LIFE) == 0 then
  19.         sim.partChangeType(i,elem.DEFAULT_PT_SPRK)
  20.         sim.partProperty(i,sim.FIELD_LIFE,4)
  21.         sim.partProperty(i,sim.FIELD_CTYPE,e)
  22.     end
  23. end
  24.  
  25.  
  26.  
  27.  
  28. --Activating function
  29. local function activate(x,y)
  30.     p = sim.partID(x + 1,y)
  31.    
  32.     if p ~= nil then
  33.         if sim.partProperty(p,sim.FIELD_TYPE) == id and sim.partProperty(p,sim.FIELD_LIFE) == -2 then
  34.             sim.partProperty(p,sim.FIELD_LIFE,-3)
  35.         end
  36.     end
  37. end
  38.  
  39.  
  40.  
  41.  
  42. --Decode function to change raw tmp data to something the CMND particle can use
  43. local function decode(tmp,cmd,tmp2)
  44.     --placeone/ifexists
  45.     if cmd == 0 or cmd == 6 then
  46.         tmp = tostring(tmp)
  47.         tmp = string.sub(tmp,2,string.len(tmp))
  48.         decoded = {"",""}
  49.        
  50.         for i = 1,string.len(tmp),1 do
  51.             if i <= 4 then
  52.                 decoded[1] = decoded[1]..string.sub(tmp,i,i)
  53.             end
  54.                
  55.             if i >= 5 then
  56.                 decoded[2] = decoded[2]..string.sub(tmp,i,i)
  57.             end
  58.         end
  59.        
  60.         for i,s in pairs(decoded) do
  61.             if string.sub(s,1,1) == "1" then
  62.                 decoded[i] = -tonumber(string.sub(s,2,string.len(s)))
  63.             else
  64.                 decoded[i] = tonumber(s)
  65.             end
  66.         end
  67.        
  68.         return decoded
  69.     end
  70.    
  71.     --placebox/placeclone/placecloneexcept
  72.     if cmd >= 1 and cmd <= 3 then
  73.         tmp = tostring(tmp)
  74.         tmp = string.sub(tmp,2,string.len(tmp))
  75.         tmp2 = tostring(tmp2)
  76.         tmp2 = string.sub(tmp2,2,string.len(tmp2))
  77.         decoded = {"","","",""}
  78.        
  79.         for i = 1,string.len(tmp),1 do
  80.             if i <= 4 then
  81.                 decoded[1] = decoded[1]..string.sub(tmp,i,i)
  82.             end
  83.                
  84.             if i >= 5 then
  85.                 decoded[2] = decoded[2]..string.sub(tmp,i,i)
  86.             end
  87.         end
  88.        
  89.         for i = 1,string.len(tmp2),1 do
  90.             if i <= 4 then
  91.                 decoded[3] = decoded[3]..string.sub(tmp2,i,i)
  92.             end
  93.                
  94.             if i >= 5 then
  95.                 decoded[4] = decoded[4]..string.sub(tmp2,i,i)
  96.             end
  97.         end
  98.            
  99.         for i,s in pairs(decoded) do
  100.             if string.sub(s,1,1) == "1" then
  101.                 decoded[i] = -tonumber(string.sub(s,2,string.len(s)))
  102.             else
  103.                 decoded[i] = tonumber(s)
  104.             end
  105.         end
  106.        
  107.         return decoded
  108.     end
  109.        
  110.     --ifinradiusat
  111.     if cmd == 5 then
  112.         tmp = tostring(tmp)
  113.         tmp = string.sub(tmp,2,string.len(tmp))
  114.         decoded = {"","",""}
  115.        
  116.         for i = 1,string.len(tmp),1 do
  117.             if i <= 4 then
  118.                 decoded[1] = decoded[1]..string.sub(tmp,i,i)
  119.             end
  120.                    
  121.             if i >= 5 then
  122.                 decoded[2] = decoded[2]..string.sub(tmp,i,i)
  123.             end
  124.         end
  125.        
  126.         for i,s in pairs(decoded) do
  127.             if string.sub(s,1,1) == "1" then
  128.                 decoded[i] = -tonumber(string.sub(s,2,string.len(s)))
  129.             else
  130.                 decoded[i] = tonumber(s)
  131.             end
  132.         end
  133.        
  134.         return decoded
  135.     end
  136.    
  137.     --datachange
  138.     if cmd == 7 then
  139.         tmp = tostring(tmp)
  140.         tmp = string.sub(tmp,2,string.len(tmp))
  141.         decoded = {"","","",""}
  142.        
  143.         for i = 1,string.len(tmp),1 do
  144.             if i <= 4 then
  145.                 decoded[1] = decoded[1]..string.sub(tmp,i,i)
  146.             end
  147.                
  148.             if i >= 5 and i <= 8 then
  149.                 decoded[2] = decoded[2]..string.sub(tmp,i,i)
  150.             end
  151.            
  152.             if i == 9 then
  153.                 decoded[3] = decoded[3]..string.sub(tmp,i,i)
  154.             end
  155.         end
  156.        
  157.         for i,s in pairs(decoded) do
  158.             if i <= 2 then
  159.                 if string.sub(s,1,1) == "1" then
  160.                     decoded[i] = -tonumber(string.sub(s,2,string.len(s)))
  161.                 else
  162.                     decoded[i] = tonumber(s)
  163.                 end
  164.             else
  165.                 decoded[i] = tonumber(s)
  166.             end
  167.         end
  168.        
  169.         return decoded
  170.     end
  171.    
  172.     return {0,0,0,0}
  173. end
  174.  
  175.  
  176.  
  177.  
  178. --Command function that runs the different commands
  179. local function commands(i,x,y,cmd,data)
  180.     if data ~= nil then
  181.         --placeone
  182.         if cmd == 0 then
  183.             if sim.partProperty(i,sim.FIELD_CTYPE) ~= 0 then
  184.                 sim.partCreate(-1,data[1] + x,data[2] + y,sim.partProperty(i,sim.FIELD_CTYPE))
  185.             else
  186.                 sim.partKill(data[1],data[2])
  187.             end
  188.         end
  189.        
  190.         --placebox
  191.         if cmd == 1 then
  192.             sim.createBox(data[1] + x,data[2] + y,data[3] + data[1] + x,data[4] + data[2] + y,sim.partProperty(i,sim.FIELD_CTYPE))
  193.         end
  194.        
  195.         --placeclone
  196.         if cmd == 2 then
  197.             p = sim.partID(data[1] + x,data[2] + y)
  198.            
  199.             if p ~= nil then
  200.                 if sim.partProperty(p,sim.FIELD_TYPE) == sim.partProperty(i,sim.FIELD_CTYPE) or sim.partProperty(i,sim.FIELD_CTYPE) == 0 then
  201.                     c = sim.partCreate(-1,data[3] + x,data[4] + y,sim.partProperty(p,sim.FIELD_TYPE))
  202.                    
  203.                     if cid ~= -1 then
  204.                         sim.partProperty(c,sim.FIELD_CTYPE,sim.partProperty(p,sim.FIELD_CTYPE))
  205.                         sim.partProperty(c,sim.FIELD_TMP,sim.partProperty(p,sim.FIELD_TMP))
  206.                         sim.partProperty(c,sim.FIELD_TMP2,sim.partProperty(p,sim.FIELD_TMP2))
  207.                         sim.partProperty(c,sim.FIELD_LIFE,sim.partProperty(p,sim.FIELD_LIFE))
  208.                         sim.partProperty(c,sim.FIELD_VX,sim.partProperty(p,sim.FIELD_VX))
  209.                         sim.partProperty(c,sim.FIELD_VY,sim.partProperty(p,sim.FIELD_VY))
  210.                         sim.partProperty(c,sim.FIELD_DCOLOUR,sim.partProperty(p,sim.FIELD_DCOLOUR))
  211.                         sim.partProperty(c,sim.FIELD_TEMP,sim.partProperty(p,sim.FIELD_TEMP))
  212.                     end
  213.                 end
  214.             end
  215.         end
  216.        
  217.         --placecloneexcept
  218.         if cmd == 3 then
  219.             p = sim.partID(data[1] + x,data[2] + y)
  220.            
  221.             if p ~= nil then
  222.                 if sim.partProperty(p,sim.FIELD_TYPE) ~= sim.partProperty(i,sim.FIELD_CTYPE) then
  223.                     c = sim.partCreate(-1,data[3] + x,data[4] + y,sim.partProperty(p,sim.FIELD_TYPE))
  224.                    
  225.                     if cid ~= -1 then
  226.                         sim.partProperty(c,sim.FIELD_CTYPE,sim.partProperty(p,sim.FIELD_CTYPE))
  227.                         sim.partProperty(c,sim.FIELD_TMP,sim.partProperty(p,sim.FIELD_TMP))
  228.                         sim.partProperty(c,sim.FIELD_TMP2,sim.partProperty(p,sim.FIELD_TMP2))
  229.                         sim.partProperty(c,sim.FIELD_LIFE,sim.partProperty(p,sim.FIELD_LIFE))
  230.                         sim.partProperty(c,sim.FIELD_VX,sim.partProperty(p,sim.FIELD_VX))
  231.                         sim.partProperty(c,sim.FIELD_VY,sim.partProperty(p,sim.FIELD_VY))
  232.                         sim.partProperty(c,sim.FIELD_DCOLOUR,sim.partProperty(p,sim.FIELD_DCOLOUR))
  233.                         sim.partProperty(c,sim.FIELD_TEMP,sim.partProperty(p,sim.FIELD_TEMP))
  234.                     end
  235.                 end
  236.             end
  237.         end
  238.        
  239.         --ifinradius
  240.         if cmd == 4 then
  241.             for ni,n in pairs(sim.partNeighbours(x,y,sim.partProperty(i,sim.FIELD_TMP))) do
  242.                 if sim.partProperty(n,sim.FIELD_TYPE) == sim.partProperty(i,sim.FIELD_CTYPE) or sim.partProperty(i,sim.FIELD_CTYPE) == 0 then
  243.                     for ni,n in pairs(sim.partNeighbours(x,y,2,elem.DEFAULT_PT_METL)) do
  244.                         spark(n,elem.DEFAULT_PT_METL)
  245.                     end
  246.                    
  247.                     activate(x,y)
  248.                     break
  249.                 end
  250.             end
  251.         end
  252.        
  253.         --ifinradiusat
  254.         if cmd == 5 then
  255.             for ni,n in pairs(sim.partNeighbours(x + data[1],y + data[2],sim.partProperty(i,sim.FIELD_TMP2))) do
  256.                 if sim.partProperty(n,sim.FIELD_TYPE) == sim.partProperty(i,sim.FIELD_CTYPE) or sim.partProperty(i,sim.FIELD_CTYPE) == 0 then
  257.                     for ni,n in pairs(sim.partNeighbours(x,y,2,elem.DEFAULT_PT_METL)) do
  258.                         spark(n,elem.DEFAULT_PT_METL)
  259.                     end
  260.                    
  261.                     activate(x,y)
  262.                     break
  263.                 end
  264.             end
  265.         end
  266.        
  267.         --ifexists
  268.         if cmd == 6 then
  269.             p = sim.partID(data[1] + x,data[2] + y)
  270.            
  271.             if p ~= nil then
  272.                 if sim.partProperty(p,sim.FIELD_TYPE) == sim.partProperty(i,sim.FIELD_CTYPE) or sim.partProperty(i,sim.FIELD_CTYPE) == 0 then
  273.                     for ni,n in pairs(sim.partNeighbours(x,y,2,elem.DEFAULT_PT_METL)) do
  274.                         spark(n,elem.DEFAULT_PT_METL)
  275.                     end
  276.                    
  277.                     activate(x,y)
  278.                 end
  279.             end
  280.         end
  281.        
  282.         --datachange
  283.         if cmd == 7 then
  284.             p = sim.partID(data[1] + x,data[2] + y)
  285.            
  286.             if p ~= nil then
  287.                 --ctype
  288.                 if data[3] == 0 then
  289.                     sim.partProperty(p,sim.FIELD_CTYPE,sim.partProperty(i,sim.FIELD_CTYPE))
  290.                 end
  291.                
  292.                 --tmp
  293.                 if data[3] == 1 then
  294.                     sim.partProperty(p,sim.FIELD_TMP,sim.partProperty(i,sim.FIELD_TMP2))
  295.                 end
  296.                
  297.                 --life
  298.                 if data[3] == 2 then
  299.                     sim.partProperty(p,sim.FIELD_LIFE,sim.partProperty(i,sim.FIELD_TMP2))
  300.                 end
  301.                
  302.                 --tmp2
  303.                 if data[3] == 2 then
  304.                     sim.partProperty(p,sim.FIELD_TMP2,sim.partProperty(i,sim.FIELD_TMP2))
  305.                 end
  306.                
  307.                 --temp
  308.                 if data[3] == 2 then
  309.                     sim.partProperty(p,sim.FIELD_TEMP,sim.partProperty(i,sim.FIELD_TMP2))
  310.                 end
  311.             end
  312.         end
  313.     end
  314. end
  315.  
  316.  
  317.  
  318.  
  319. --Update function that puts it all together (Defined before properties because Lua)
  320. local function update(i,x,y)
  321.     if sim.partProperty(i,sim.FIELD_LIFE) > 1 then
  322.         sim.partProperty(i,sim.FIELD_LIFE,sim.partProperty(i,sim.FIELD_LIFE) + 1)
  323.        
  324.         if sim.partProperty(i,sim.FIELD_LIFE) == 10 then
  325.             sim.partProperty(i,sim.FIELD_LIFE,1)
  326.         end
  327.     end
  328.  
  329.     --Impulse
  330.     if sim.partProperty(i,sim.FIELD_LIFE) == -1 then
  331.         --Commands here
  332.         commands(i,x,y,sim.partProperty(i,sim.FIELD_TEMP),decode(sim.partProperty(i,sim.FIELD_TMP),sim.partProperty(i,sim.FIELD_TEMP),sim.partProperty(i,sim.FIELD_TMP2)))
  333.         sim.partKill(i)
  334.        
  335.     --Repeat
  336.     elseif sim.partProperty(i,sim.FIELD_LIFE) == 0 then
  337.         --Commands here
  338.         commands(i,x,y,sim.partProperty(i,sim.FIELD_TEMP),decode(sim.partProperty(i,sim.FIELD_TMP),sim.partProperty(i,sim.FIELD_TEMP),sim.partProperty(i,sim.FIELD_TMP2)))
  339.    
  340.     --Sparked
  341.     elseif sim.partProperty(i,sim.FIELD_LIFE) == 1 then
  342.         for ni,n in pairs(sim.partNeighbors(x,y,1,elem.DEFAULT_PT_SPRK)) do
  343.             --Commands here
  344.             commands(i,x,y,sim.partProperty(i,sim.FIELD_TEMP),decode(sim.partProperty(i,sim.FIELD_TMP),sim.partProperty(i,sim.FIELD_TEMP),sim.partProperty(i,sim.FIELD_TMP2)))
  345.             sim.partProperty(i,sim.FIELD_LIFE,2)
  346.            
  347.             break
  348.         end
  349.        
  350.     --Activated
  351.     elseif sim.partProperty(i,sim.FIELD_LIFE) == -3 then
  352.         --Commands here
  353.         commands(i,x,y,sim.partProperty(i,sim.FIELD_TEMP),decode(sim.partProperty(i,sim.FIELD_TMP),sim.partProperty(i,sim.FIELD_TEMP),sim.partProperty(i,sim.FIELD_TMP2)))
  354.         sim.partProperty(i,sim.FIELD_LIFE,-2)
  355.     end
  356. end
  357.  
  358.  
  359.  
  360.  
  361. --Element properties
  362. elem.property(id,"Name","CMND")
  363. elem.property(id,"Enabled",1)
  364. elem.property(id,"Description","Can do lots of stuff. Use carefully in small amounts.")
  365. elem.property(id,"Color",0xFFFF80)
  366. elem.property(id,"MenuVisible",1)
  367. elem.property(id,"MenuSection",elem.SC_SPECIAL)
  368. elem.property(id,"Properties",elem.TYPE_SOLID + elem.PROP_DRAWONCTYPE + elem.PROP_NOAMBHEAT + elem.PROP_NEUTPASS)
  369. elem.property(id,"Falldown",0)
  370. elem.property(id,"Advection",0)
  371. elem.property(id,"AirDrag",0)
  372. elem.property(id,"AirLoss",1)
  373. elem.property(id,"Loss",0)
  374. elem.property(id,"Collision",0)
  375. elem.property(id,"Gravity",0)
  376. elem.property(id,"Diffusion",0)
  377. elem.property(id,"HotAir",0)
  378. elem.property(id,"Flammable",0)
  379. elem.property(id,"Explosive",0)
  380. elem.property(id,"Meltable",0)
  381. elem.property(id,"Hardness",0)
  382. elem.property(id,"Weight",100)
  383. elem.property(id,"Temperature",sim.R_TEMP + 273.15)
  384. elem.property(id,"HeatConduct",0)
  385. elem.property(id,"State",elem.ST_SOLID)
  386. elem.property(id,"Update",update)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement