Advertisement
SpyMomiji

[Minecraft] 自訂反應爐控制程式,可用版本 v3

Dec 14th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. Face = "north"
  2.  
  3. Nuclear = "front"
  4. Material = "left"
  5. Void = "bottom"
  6.  
  7. coolingSingel = "right"
  8. PauseSingel = "back"
  9.  
  10. ThickID = 30086
  11. FuelID = 30101
  12. ThickWarn = 9980
  13. FuelWarn = 9980
  14.  
  15. TempThresholds = 50
  16.  
  17. ThickSlot = {2,3,4,10,14,20,21,22}
  18. FuelSlot = {11,12,13}
  19.  
  20. --=====codes=====
  21.  
  22. pos = {"north", "east", "south", "western"}
  23. dict = {"front", "right", "back", "left"}
  24.  
  25. function getPosition(drection,flip)
  26.     if (drection=="top") then
  27.         if flip then
  28.             return "down"
  29.         else
  30.             return "up"
  31.         end
  32.     elseif(drection=="bottom") then
  33.         if flip then
  34.             return "up"
  35.         else
  36.             return "down"
  37.         end
  38.     else
  39.         local ph = 1
  40.         local dh = 1
  41.         for s=1,#pos,1 do
  42.             if pos[s] == Face then
  43.                 ph = s
  44.                 if flip then ph = ph + 2 end
  45.                 break
  46.             end
  47.         end
  48.         for s=1,#dict,1 do
  49.             if dict[s] == drection then
  50.                 dh = s + ph -1
  51.                 break
  52.             end
  53.         end
  54.         while dh > 4 do
  55.             dh = dh - 4
  56.         end
  57.         return pos[dh]
  58.     end
  59. end
  60.  
  61. function getChest(direct,check)
  62.     if (check ~= peripheral.getType(direct)) then
  63.         return nil,false,peripheral.getType(direct)
  64.     end
  65.    
  66.     local gd = getPosition(direct,false)
  67.     local gdf = getPosition(direct,true)
  68.     local ch = peripheral.wrap(direct)
  69.     print ("get chest at "..direct..",with pos "..gd..","..peripheral.getType(direct))
  70.     local ct = {
  71.         size = ch.getInventorySize(),
  72.         info = function(index)
  73.             local ti = ch.getStackInSlot(index)
  74.             if ti == nil then
  75.                 return 0,0
  76.             else
  77.                 return ti.id,ti.dmg
  78.             end
  79.         end,
  80.         put = function(index,count,TurtleIndex)
  81.             local c = 0
  82.             if TurtleIndex ~= nil then
  83.                 c = ch.pullItemIntoSlot(gdf,TurtleIndex,count,index)
  84.             else
  85.                 c = ch.pullItem(gdf,index,count)
  86.             end
  87.             if c == 0 then
  88.                 return false,c
  89.             else
  90.                 return true,c
  91.             end
  92.         end,
  93.         take = function(index,count)
  94.             local ful = true
  95.             for si = 1,16,1 do
  96.                 if turtle.getItemCount(si) == 0 then
  97.                         ful = false
  98.                         break
  99.                 end
  100.             end
  101.             if ful then
  102.                 return false,0
  103.             else
  104.                 return true,ch.pushItem(gdf,index,count)
  105.             end
  106.         end
  107.     }
  108.     return ct,true,peripheral.getType(direct)
  109. end
  110.  
  111.  
  112.  
  113. nuc,suc = getChest(Nuclear,"nuclear_reactor")
  114.  
  115. if not suc then print ("fail") return end
  116.  
  117. mac,suc = getChest(Material,"ender_chest")
  118.  
  119. if not suc then print ("fail") return end
  120.  
  121. voc,suc = getChest(Void,"ender_chest")
  122.  
  123. if not suc then print ("fail") return end
  124. suc = nil
  125.  
  126. nust = peripheral.wrap(Nuclear)
  127.  
  128. function temp()
  129.     if (nust.getHeat()/nust.getMaxHeat()*100) > TempThresholds then
  130.         return true
  131.     else
  132.         return false
  133.     end
  134. end
  135.  
  136. prs = function(pa)
  137.     redstone.setOutput(coolingSingel,pa)
  138. end
  139.  
  140. sts = function(pa)
  141.     redstone.setOutput(PauseSingel,pa)
  142. end
  143.  
  144. prs(false)
  145. sts(false)
  146.  
  147. --item control
  148. speedUp = false
  149. expection = false
  150.  
  151. function checkItem(itemID,maxdmg,slots)
  152.     for i=1,#slots,1 do
  153.         local d,g = nuc.info(slots[i])
  154.         if d ~= itemID then
  155.            
  156.            
  157.             sts(false)
  158.            
  159.             local suc = true
  160.            
  161.             --clear turtle
  162.             for sei=1,16,1 do
  163.             local co = turtle.getItemCount(sei)
  164.                 if co ~= 0 then
  165.                     for ty=1,16,1 do
  166.                         local suc = voc.put(sei,co)
  167.                         if suc then break else os.sleep(0.5) end
  168.                     end
  169.                 end
  170.                 if not suc then break end
  171.             end
  172.            
  173.             if not suc then print("<!>can not clear Turtle") expection = true return end
  174.            
  175.             suc = false
  176.            
  177.             --get item
  178.             for ty=1,6,1 do
  179.                 for sei=1,mac.size,1 do
  180.                     if mac.info(sei) == itemID then
  181.                         suc = mac.take(sei,1)
  182.                         if suc then break end
  183.                     end
  184.                 end
  185.                 if suc then break else os.sleep(1) end
  186.             end
  187.             if not suc then print("<!>no item "..itemID) expection = true return end
  188.            
  189.             prs(true)
  190.             suc = false
  191.            
  192.             --change item
  193.             for ty=1,8,1 do
  194.                 suc = nuc.take(slots[i],1)
  195.                 if not suc then break end
  196.                 suc = nuc.put(slots[i],1,1)
  197.                 if suc then break end
  198.             end
  199.            
  200.             if not suc then print("<!>change item "..itemID.."failed,at slot "..slots[i]) expection = true return end
  201.                        
  202.             voc.put(2,turtle.getItemCount(2))
  203.            
  204.         elseif g >= maxdmg then
  205.             speedUp = true
  206.         end
  207.     end
  208. end
  209.  
  210. --main
  211. function start()
  212.     while true do
  213.        
  214.         speedUp = false
  215.        
  216.         checkItem(ThickID,ThickWarn,ThickSlot)
  217.         checkItem(FuelID,FuelWarn,FuelSlot)
  218.        
  219.        
  220.         if expection then return end
  221.         prs(false)
  222.         sts(not temp())
  223.        
  224.         if speedUp then
  225.             os.sleep(0.2)
  226.         else
  227.             os.sleep(2)
  228.         end
  229.     end
  230. end
  231.  
  232. --go
  233. print ("start")
  234. start()
  235. print ("stop with a expection")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement