Advertisement
Guest User

solar.lua

a guest
Feb 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. sleep(2)
  2.  
  3. os.loadAPI("ocs/apis/sensor")
  4. s = sensor.wrap("top")
  5. m = peripheral.wrap("right")
  6.  
  7. function contains(key)
  8.     return itemTable[key] ~= nil
  9. end
  10.  
  11. function addToTable(key, var)
  12.     itemTable[key] = var
  13. end
  14.  
  15. function addToTable1(key, var)
  16.     solarTable[key] = var
  17. end
  18.  
  19. function getMax(t)
  20.     local maxVar = 0
  21.     for k,v in pairs(t) do
  22.      if v > maxVar then
  23.         maxVar = v
  24.      end
  25.     end
  26.     return maxVar
  27. end
  28.  
  29. function getMin(t)
  30.     local minVar = 0
  31.     for k,v in pairs(t) do
  32.      if v < minVar then
  33.         minVar = v
  34.      end
  35.     end
  36.     return minVar
  37. end
  38.  
  39. function getMin(t)
  40.     local minVar
  41.     for k,v in pairs(t) do
  42.      if minVar == nil then
  43.       minVar = v
  44.      end
  45.      if v < minVar then
  46.         minVar = v
  47.      end
  48.     end
  49.     return minVar
  50. end
  51.  
  52. itemTable = {}
  53. itemTable["Copper"] = 0
  54. itemTable["Iron Ingot"] = 0
  55. itemTable["Refined Iron"] = 0
  56. itemTable["Tin"] = 0
  57. itemTable["Coal Dust"] = 0
  58. itemTable["Redstone"] = 0
  59. itemTable["Cobblestone"] = 0
  60. itemTable["Glass"] = 0
  61. itemTable["Rubber"] = 0
  62. itemTable["Diamond"] = 0
  63. itemTable["Gold Ingot"] = 0
  64. itemTable["Oak Wood Planks"] = 0
  65.  
  66.  
  67. itemTableSolar = {}
  68. itemTableSolar["Copper"] = 3587
  69. itemTableSolar["Iron Ingot"] = 2560
  70. itemTableSolar["Refined Iron"] = 2633.5
  71. itemTableSolar["Tin"] = 2048
  72. itemTableSolar["Coal Dust"] = 1536
  73. itemTableSolar["Redstone"] = 3082
  74. itemTableSolar["Cobblestone"] = 4096
  75. itemTableSolar["Glass"] = 1536
  76. itemTableSolar["Rubber"] = 6832
  77. itemTableSolar["Diamond"] = 1
  78. itemTableSolar["Gold Ingot"] = 4.5
  79. itemTableSolar["Oak Wood Planks"] = 256
  80.  
  81. costper = {}
  82. costper["Copper"] = 0.3
  83. costper["Iron Ingot"] = 1.25
  84. costper["Refined Iron"] = 1.25
  85. costper["Tin"] = 0.3
  86. costper["Coal Dust"] = 0.15
  87. costper["Redstone"] = 4/24
  88. costper["Cobblestone"] = 1
  89. costper["Glass"] = 0.125
  90. costper["Rubber"] = 4/63
  91. costper["Diamond"] = 9
  92. costper["Gold Ingot"] = 1.25
  93. costper["Oak Wood Planks"] = 0.03125
  94.  
  95. details = s.getTargetDetails("-3,-2,2")
  96. types = details.UsedTypes
  97. UU = 0
  98.  
  99.  for i = 1,tonumber(types) do
  100.   local item = details.Items[i].Name
  101.   local amount = details.Items[i].Size
  102.    if contains(item) then
  103.     addToTable(item, amount)
  104.    end
  105.    if item == "UU-Matter" then
  106.     UU = amount
  107.    end
  108.  end
  109.  
  110. solarTable = {}
  111. m.clear()
  112. y = 1
  113. m.setCursorPos(1,y)
  114. for k,v in pairs(itemTable) do
  115.  
  116.   n = v/itemTableSolar[k]
  117.   addToTable1(k, n)
  118.   if z == nil or z > n then
  119.     z = n
  120.   end
  121.  
  122.   m.write(k .. ": " .. v)
  123.   m.setCursorPos(30,y)
  124.   m.write(n)
  125.   y=y+1
  126.   m.setCursorPos(1,y)
  127. end
  128.  
  129. m.setCursorPos(1,25)
  130. m.write(getMax(solarTable))
  131. var = getMax(solarTable)
  132. y = 1
  133. for k,v in pairs(itemTableSolar) do
  134.  dif = var-solarTable[k]
  135.  dif = dif*itemTableSolar[k]
  136.   m.setCursorPos(55,y)
  137.   m.write(dif)
  138.   y=y+1
  139. end
  140.  
  141. function spairs(t, order)
  142.     -- collect the keys
  143.     local keys = {}
  144.     for k in pairs(t) do keys[#keys+1] = k end
  145.  
  146.     -- if order function given, sort by it by passing the table and keys a, b,
  147.     -- otherwise just sort the keys
  148.     if order then
  149.         table.sort(keys, function(a,b) return order(t, a, b) end)
  150.     else
  151.         table.sort(keys)
  152.     end
  153.  
  154.     -- return the iterator function
  155.     local i = 0
  156.     return function()
  157.         i = i + 1
  158.         if keys[i] then
  159.             return keys[i], t[keys[i]]
  160.         end
  161.     end
  162. end
  163.  
  164. -- this uses an custom sorting function ordering by score descending
  165.  
  166. --secmin = getMax(uuTable)
  167. secmin = var
  168. newTable = {}
  169. mini = getMin(solarTable)
  170. function addToTable10(key, var)
  171.     newTable[key] = var
  172. end
  173.  
  174. for k,v in spairs(solarTable, function(t,a,b) return t[b] > t[a] end) do
  175.     --print(k .. " " .. v)
  176.     if v ~= 0 then
  177.      if v < secmin and v > mini then
  178.       secmin = v
  179.      end
  180.     end
  181. end
  182.  
  183.  
  184. print(secmin)
  185. print(UU)
  186. print("")
  187. local uuges = 0
  188.  
  189. function rechner(k,y,v)
  190.       local d = y-v
  191.       local e = (d*itemTableSolar[k])*costper[k]
  192.       --print(k .. " " .. e)
  193.       local uu = UU - e
  194.       if uu > 0 then
  195.        uuges = uuges + e
  196.        UU = UU - e
  197.       else
  198.       return 0
  199.       end
  200. end
  201.  
  202. for k,v in spairs(solarTable, function(t,a,b) return t[b] > t[a] end) do
  203.     --print(k .. " " .. v)
  204.     local y = secmin
  205.     if v < y or v == y then
  206.      if rechner(k,y,v) == 0 then
  207.       break
  208.      end
  209.      addToTable10(k,y)
  210.     elseif v > y then
  211.      y = v
  212.      if rechner(k,y,v) == 0 then
  213.       break
  214.      end
  215.      addToTable10(k,y)
  216.      for k,v in pairs(newTable) do
  217.      if rechner(k,y,v) == 0 then
  218.       break
  219.      end
  220.       addToTable10(k,y)
  221.      end
  222.     end
  223. end
  224.  
  225. for k,v in pairs(newTable) do
  226. print(k .. " " .. v)
  227. end
  228.  
  229. print(uuges)
  230. print(UU)
  231. print(UU/9830.8)
  232. print(getMin(newTable)+(UU/9830.8))
  233.  
  234. --minimum res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement