Advertisement
iamdumb

TPT Logic Gates Mod (Lua)

Aug 21st, 2014
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.28 KB | None | 0 0
  1. --[[
  2.     Logic gates mod for TPT:
  3.  
  4.     AND: SPRKs nearby NSCN when powered by 2 or more PSCN
  5.     OR: Conducts from PSCN and to NSCN
  6.     NOT: SPRKs nearby NSCN unless powered by PSCN
  7.     XOR: SPRKs nearby NSCN if powered by ONE PSCN
  8.     NAND: SPRKs nearby NSCN unless powered by 2 or more PSCN
  9.     XNOR: SPRKs nearby NSCN unless powered by ONE PSCN
  10. --]]
  11.  
  12. local frame_counter = 0     -- Counter to prevent crazy nots.
  13. local frame_max = 8     -- More stuff to prevent crazy nots.
  14.  
  15. local el_and = elements.allocate("logic", "and")
  16. -- (Begin AND properties)
  17.  elements.element(el_and, elements.element(elements.DEFAULT_PT_BRCK))
  18.  elements.property(el_and, "Name", "AND")
  19.  elements.property(el_and, "Colour", 0x800000)
  20.  elements.property(el_and, "Description", "AND logic gate. Input PSCN, output NSCN")
  21.  elements.property(el_and, "MenuSection", 1)
  22. -- (End AND properties)
  23.  
  24. local el_or = elements.allocate("logic", "or")
  25. -- (Begin OR properties)
  26.  elements.element(el_or, elements.element(elements.DEFAULT_PT_BRCK))
  27.  elements.property(el_or, "Name", "OR")
  28.  elements.property(el_or, "Colour", 0x800000)
  29.  elements.property(el_or, "Description", "OR logic gate. Input PSCN, output NSCN")
  30.  elements.property(el_or, "MenuSection", 1)
  31. -- (End OR properties)
  32.  
  33. local el_not= elements.allocate("logic", "not")
  34. -- (Begin NOT properties)
  35.  elements.element(el_not, elements.element(elements.DEFAULT_PT_BRCK))
  36.  elements.property(el_not, "Name", "NOT")
  37.  elements.property(el_not, "Colour", 0x800000)
  38.  elements.property(el_not, "Description", "NOT logic gate. Input PSCN, output NSCN")
  39.  elements.property(el_not, "MenuSection", 1)
  40. -- (End NOT properties)
  41.  
  42. local el_xor = elements.allocate("logic", "xor")
  43. -- (Begin OR properties)
  44.  elements.element(el_xor, elements.element(elements.DEFAULT_PT_BRCK))
  45.  elements.property(el_xor, "Name", "XOR")
  46.  elements.property(el_xor, "Colour", 0x800000)
  47.  elements.property(el_xor, "Description", "XOR logic gate. Input PSCN, output NSCN")
  48.  elements.property(el_xor, "MenuSection", 1)
  49. -- (End OR properties)
  50.  
  51. local el_nand = elements.allocate("logic", "nand")
  52. -- (Begin NAND properties)
  53.  elements.element(el_nand, elements.element(elements.DEFAULT_PT_BRCK))
  54.  elements.property(el_nand, "Name", "NAND")
  55.  elements.property(el_nand, "Colour", 0x800000)
  56.  elements.property(el_nand, "Description", "NAND logic gate. Input PSCN, output NSCN")
  57.  elements.property(el_nand, "MenuSection", 1)
  58. -- (End NAND properties)
  59.  
  60. local el_xnor = elements.allocate("logic", "xnor")
  61. -- (Begin XNOR properties)
  62.  elements.element(el_xnor, elements.element(elements.DEFAULT_PT_BRCK))
  63.  elements.property(el_xnor, "Name", "XNOR")
  64.  elements.property(el_xnor, "Colour", 0x800000)
  65.  elements.property(el_xnor, "Description", "XNOR logic gate. Input PSCN, output NSCN")
  66.  elements.property(el_xnor, "MenuSection", 1)
  67. -- (End XNOR properties)
  68.  
  69. -- Update Functions
  70. -- AND
  71. function and_update(index, partx, party, surround, nt)
  72.     local n_pscn = 0    -- number of SPRK(PSCN found)
  73.  
  74.     for dx = -2, 2, 1 do
  75.         for dy = -2, 2, 1 do
  76.             if tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_SPRK and tpt.get_property("ctype", partx + dx, party + dy) == elements.DEFAULT_PT_PSCN then
  77.                 n_pscn = n_pscn + 1 -- Increment PSCN count
  78.             end
  79.         end
  80.     end
  81.  
  82.     if n_pscn >= 2 then
  83.         for dx = -2, 2, 1 do
  84.             for dy = -2, 2, 1 do
  85.                 if tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_NSCN and tpt.get_property("life", partx + dx, party + dy) == 0 then
  86.                     tpt.set_property("life", 4, partx + dx, party + dy) -- SPRK nscn
  87.                     tpt.set_property("ctype", elements.DEFAULT_PT_NSCN, partx + dx, party + dy)
  88.                     tpt.set_property("type", elements.DEFAULT_PT_SPRK, partx + dx, party + dy)
  89.                 end
  90.             end
  91.         end
  92.     end
  93.     return 0
  94. end
  95.  
  96. -- OR
  97. function or_update(index, partx, party, surround, nt)
  98.     local n_pscn = 0    -- number of SPRK(PSCN found)
  99.  
  100.     for dx = -2, 2, 1 do
  101.         for dy = -2, 2, 1 do
  102.             if tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_SPRK and tpt.get_property("ctype", partx + dx, party + dy) == elements.DEFAULT_PT_PSCN then
  103.                 n_pscn = n_pscn + 1 -- Increment PSCN count
  104.             end
  105.         end
  106.     end
  107.  
  108.     if n_pscn >= 1 then
  109.         for dx = -2, 2, 1 do
  110.             for dy = -2, 2, 1 do
  111.                 if tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_NSCN and tpt.get_property("life", partx + dx, party + dy) == 0 then
  112.                     tpt.set_property("life", 4, partx + dx, party + dy) -- SPRK nscn
  113.                     tpt.set_property("ctype", elements.DEFAULT_PT_NSCN, partx + dx, party + dy)
  114.                     tpt.set_property("type", elements.DEFAULT_PT_SPRK, partx + dx, party + dy)
  115.                 end
  116.             end
  117.         end
  118.     end
  119.     return 0
  120. end
  121.  
  122. -- NOT
  123. function not_update(index, partx, party, surround, nt)
  124.     frame_counter = frame_counter + 1;
  125.     if frame_counter == frame_max then
  126.         frame_counter = 0
  127.     end
  128.  
  129.     local n_pscn = 0    -- number of SPRK(PSCN found)
  130.  
  131.     for dx = -2, 2, 1 do
  132.         for dy = -2, 2, 1 do
  133.             if (tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_SPRK and tpt.get_property("ctype", partx + dx, party + dy) == elements.DEFAULT_PT_PSCN) or (tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_PSCN and tpt.get_property("life", partx + dx, party + dy) > 0) then
  134.                 n_pscn = n_pscn + 1 -- Increment PSCN count
  135.             end
  136.         end
  137.     end
  138.  
  139.     if n_pscn == 0 and frame_counter == 0 then
  140.         for dx = -2, 2, 1 do
  141.             for dy = -2, 2, 1 do
  142.                 if tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_NSCN and tpt.get_property("life", partx + dx, party + dy) == 0 then
  143.                     tpt.set_property("life", 4, partx + dx, party + dy) -- SPRK nscn
  144.                     tpt.set_property("ctype", elements.DEFAULT_PT_NSCN, partx + dx, party + dy)
  145.                     tpt.set_property("type", elements.DEFAULT_PT_SPRK, partx + dx, party + dy)
  146.                 end
  147.             end
  148.         end
  149.     end
  150.     return 0
  151. end
  152.  
  153. -- XOR
  154. function xor_update(index, partx, party, surround, nt)
  155.     local n_pscn = 0    -- number of SPRK(PSCN found)
  156.  
  157.     for dx = -2, 2, 1 do
  158.         for dy = -2, 2, 1 do
  159.             if tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_SPRK and tpt.get_property("ctype", partx + dx, party + dy) == elements.DEFAULT_PT_PSCN then
  160.                 n_pscn = n_pscn + 1 -- Increment PSCN count
  161.             end
  162.         end
  163.     end
  164.  
  165.     if n_pscn == 1 then
  166.         for dx = -2, 2, 1 do
  167.             for dy = -2, 2, 1 do
  168.                 if tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_NSCN and tpt.get_property("life", partx + dx, party + dy) == 0 then
  169.                     tpt.set_property("life", 4, partx + dx, party + dy) -- SPRK nscn
  170.                     tpt.set_property("ctype", elements.DEFAULT_PT_NSCN, partx + dx, party + dy)
  171.                     tpt.set_property("type", elements.DEFAULT_PT_SPRK, partx + dx, party + dy)
  172.                 end
  173.             end
  174.         end
  175.     end
  176.     return 0
  177. end
  178.  
  179. -- NAND
  180. function nand_update(index, partx, party, surround, nt)
  181.     frame_counter = frame_counter + 1;
  182.     if frame_counter == frame_max then
  183.         frame_counter = 0
  184.     end
  185.    
  186.     local n_pscn = 0    -- number of SPRK(PSCN found)
  187.    
  188.  
  189.     for dx = -2, 2, 1 do
  190.         for dy = -2, 2, 1 do
  191.             if (tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_SPRK and tpt.get_property("ctype", partx + dx, party + dy) == elements.DEFAULT_PT_PSCN) or (tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_PSCN and tpt.get_property("life", partx + dx, party + dy) > 0) then
  192.                 n_pscn = n_pscn + 1 -- Increment PSCN count
  193.             end
  194.         end
  195.     end
  196.  
  197.     if n_pscn < 2 and frame_counter == 0 then
  198.         for dx = -2, 2, 1 do
  199.             for dy = -2, 2, 1 do
  200.                 if tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_NSCN and tpt.get_property("life", partx + dx, party + dy) == 0 then
  201.                     tpt.set_property("life", 4, partx + dx, party + dy) -- SPRK nscn
  202.                     tpt.set_property("ctype", elements.DEFAULT_PT_NSCN, partx + dx, party + dy)
  203.                     tpt.set_property("type", elements.DEFAULT_PT_SPRK, partx + dx, party + dy)
  204.                 end
  205.             end
  206.         end
  207.     end
  208.     return 0
  209. end
  210.  
  211. -- XNOR
  212. function xnor_update(index, partx, party, surround, nt)
  213.     frame_counter = frame_counter + 1;
  214.     if frame_counter == frame_max then
  215.         frame_counter = 0
  216.     end
  217.    
  218.     local n_pscn = 0    -- number of SPRK(PSCN found)
  219.  
  220.     for dx = -2, 2, 1 do
  221.         for dy = -2, 2, 1 do
  222.             if (tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_SPRK and tpt.get_property("ctype", partx + dx, party + dy) == elements.DEFAULT_PT_PSCN) or (tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_PSCN and tpt.get_property("life", partx + dx, party + dy) > 0) then
  223.                 n_pscn = n_pscn + 1 -- Increment PSCN count
  224.             end
  225.         end
  226.     end
  227.  
  228.     if (n_pscn ~= 1) and frame_counter == 0 then
  229.         for dx = -2, 2, 1 do
  230.             for dy = -2, 2, 1 do
  231.                 if tpt.get_property("type", partx + dx, party + dy) == elements.DEFAULT_PT_NSCN and tpt.get_property("life", partx + dx, party + dy) == 0 then
  232.                     tpt.set_property("life", 4, partx + dx, party + dy) -- SPRK nscn
  233.                     tpt.set_property("ctype", elements.DEFAULT_PT_NSCN, partx + dx, party + dy)
  234.                     tpt.set_property("type", elements.DEFAULT_PT_SPRK, partx + dx, party + dy)
  235.                 end
  236.             end
  237.         end
  238.     end
  239.     return 0
  240. end
  241.  
  242. -- Update Function Properties
  243. elements.property(el_and, "Update", and_update)
  244. elements.property(el_or, "Update", or_update)
  245. elements.property(el_not, "Update", not_update)
  246. elements.property(el_xor, "Update", xor_update)
  247. elements.property(el_nand, "Update", nand_update)
  248. elements.property(el_xnor, "Update", xnor_update)
  249.  
  250. --[[  ^_^  ]]-- TPT logic gates mod, iamdumb
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement