SilentWarrior

essentiaRefiller

Jan 16th, 2018
6,582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --# Program Config
  2. local turtleFromDrawerDirection = "north"
  3. local turtleFromChestLocation = "south"
  4.  
  5. local programRestartInterval = 1.00             -- Program run interval
  6. local maxEssentiaAmount = 2000
  7. local maxEssentiaAmountMessages = false
  8.  
  9. --### ONLY MODFIY BELOW IF YOU KNOW WHAT YOU ARE DOING
  10. ---------------------------------------------------------------------------------------------------------------
  11. --### Static list of items containing specific essentia
  12. local itemHashList = {
  13.   -- Etheal Essence
  14.   ["f36968d453592c101629ff03dbbfee26"] = "machina",
  15.   ["d5a708b5e6cf0f421e7dc9355d304dae"] = "ira",
  16.   ["9124a2acb4161cb0484ccc06924c8e5c"] = "vinculum",
  17.   ["69f0f950fc1af7fe3b5cd76b96fe2a12"] = "tempus",
  18.   ["951cbfb1ee185cc2a0db21a7ee5c4658"] = "herba",
  19.   ["2e0923f828ce335b6356bf1fde489986"] = "instrumentum",
  20.   ["404d0f64c5aeb1fecc174850bf606a0f"] = "arbor",
  21.   ["aa7b17df81fa0be6959f653e259d6a49"] = "meto",
  22.   ["6a7f52f93c5b4558bdbfe12e52ea0209"] = "fabrico",
  23.   ["7168677549d5906cd2fee78349113047"] = "invidia",
  24.   ["74d6ce35611a333020364e921b5864bf"] = "pannus",
  25.   ["2cf039eb58826a770024b859478bdf13"] = "desidia",
  26.   ["50a0d85a9992ecdb9efce7d8a5fc7b15"] = "vitium",
  27.   ["1100f8a3629369e4bd219f2b30d978ac"] = "messis",
  28.   ["4b3878326e97bda1f0c8f02d43e1f724"] = "limus",
  29.   ["3306eb2763dcd8bee9f4e77aa3a916bc"] = "perfodio",
  30.   ["4058af44332d4a3c020ba28595eafe09"] = "tempestas",
  31.   ["33992e4570fb0c5a46061cfe0a0f1550"] = "volatus",
  32.   ["1e995bd21733e006a757af6511adeb14"] = "motus",
  33.   ["c1266ba6956c8507b976ed0af0fe921c"] = "tenebrae",
  34.   ["2d46425f72b47ee82e008df41fe44ff0"] = "fames",
  35.   ["62ab1d0d4ce536081358faa06507f2a8"] = "superbia",
  36.   ["d428fcfe78d4f204a1b7d7c88c597f4d"] = "lucrum",
  37.   ["8e8405341043a4dc957058fea34f7fde"] = "gula",
  38.   ["96e68bba97a9197b08e8b218f89f6cd1"] = "praecantatio",
  39.   ["4b753601ce070e02ae12c0744afa8b01"] = "sensus",
  40.   ["cc1b1319bed11adb3fb1133cfce0a9fc"] = "auram",
  41.   ["c22f882f5d3ad66818112f7e109ed065"] = "humanus",
  42.   ["1490e650a531f04fb231561dfb3cbc1c"] = "vacuos",
  43.   ["b30a393ba36c2914da2124e0c15ba6c6"] = "metallum",
  44.   ["651ed7f8722d8528927ffeb78c3b36af"] = "lux",
  45.   ["503677942dd95011d466c40d7b305e4e"] = "mortuus",
  46.   ["ef0a022b50093731e91c1775e9a6b2c4"] = "telum",
  47.   ["552c659fd588d96f66fd2ad07a4948c6"] = "luxuria",
  48.   ["b39c13da67e0f8fc6a1b415281dc1eb7"] = "tutamen",
  49.   ["76fca4fc64045eb94d1e8eb9ef6d5721"] = "infernus",
  50.   ["f37ca1c21238fb6d1a803ff3697bae37"] = "iter",
  51.   ["6e496450ecf5f1366f504787abc1a7ce"] = "exanimis",
  52.   ["ec96883e36124223662ee4b55e00ac43"] = "alienis",
  53.   ["1af1aa4c0de6f05411c2921f8ea49ada"] = "cognitio",
  54.   ["2e3aaf69f3b0eb9dc91d649ebfd7a6d6"] = "ordo",
  55.   ["dfb1cc975c8f94b2743ac3d5271869a1"] = "potentia",
  56.   ["665401c955afa0b1d77a63a9b8421b37"] = "perditio",
  57.   ["69e96cbd20cf95621d1194fe3b700c5b"] = "permutatio",
  58.   ["8bd1142ca027f3f3f2a7ea86420a4be7"] = "spiritus",
  59.   ["fa69d331cb2d38384e02086c506eaa43"] = "bestia",
  60.   ["dccfa897b53c2b437372cbb24c6e09b3"] = "sano",
  61.   ["fc82a45802f77b91a76fa9f951de83bd"] = "corpus",
  62.   ["c633bfdcd33be5dd1a75f6aff84d2ecf"] = "ignis",
  63.   ["46239a9297df5f8f6b074e3735706db6"] = "victus",
  64.   ["1cb399bc43c8bc3b7a6e53ee7c4b2652"] = "aqua",
  65.   ["2ec178e9e6aa8958d7df1af5d3203999"] = "venenum",
  66.   ["964fdc73a1032004ae92c5faaff017f0"] = "aer",
  67.   ["7e83cd135885aa779957eda2a7f0778c"] = "gelum",
  68.   ["f85a995d3cb9a4f606140f2191a5afc6"] = "terra",
  69.   ["b9f3d17344cf347090104b28d49cfce0"] = "vitreus",
  70. }
  71. ---------------------------------------------------------------------------------------------------------------
  72. --### Helper Functions
  73.  
  74. function firstCharacterUpperCase(str)
  75.   return str:sub(1,1):upper()..str:sub(2)
  76. end
  77.  
  78. function itemInfo(item,slot)
  79.   moveFromDrawerToChest(slot,1)
  80.   for i,v in pairs(item) do
  81.     write(i)
  82.     write(":")
  83.     print(v)
  84.   end
  85. end
  86.  
  87. --### Helper Functions
  88. ---------------------------------------------------------------------------------------------------------------
  89. --### EssentiaProvider Functions
  90. local ep = peripheral.find("EssentiaProvider")
  91. local essentiaAmount = {}
  92.  
  93. if not ep then
  94.   error("\n\nNo EssentiaProvider detected.\nPlace on any side.")
  95. end
  96.  
  97. function checkEssentiaProvider()
  98.   if not ep.isOnline() then
  99.     error("\n\nEssentiaProvider offline.")
  100.   end
  101. end
  102.  
  103. function prepareEssentiaProvider()
  104.   checkEssentiaProvider()
  105.   local success = ep.registerAsWatcher()
  106.   write("registerAsWatcher() ")
  107.   if success then
  108.     print("done")
  109.   else
  110.     print("failed")
  111.   end
  112. end
  113.  
  114. function updateEssentiaAmounts()
  115.   checkEssentiaProvider()
  116.   essentiaAmount = {}
  117.   local tmpLevel = {ep.getAspects()}
  118.  
  119.   for i=1,#tmpLevel,2 do
  120.     e = tmpLevel[i]
  121.     n = tmpLevel[i+1]
  122.     essentiaAmount[e:lower()] = n
  123.   end
  124. end
  125.  
  126. function getEssentiaAmount(essentia)
  127.   updateEssentiaAmounts()
  128.   return essentiaAmount[essentia] or 0
  129. end
  130.  
  131. function testEssentia(essentia)
  132.   if essentia then
  133.     print(firstCharacterUpperCase(essentia)..":"..getEssentiaAmount(essentia))
  134.   end
  135. end
  136. --### EssentiaProvider Functions
  137. ---------------------------------------------------------------------------------------------------------------
  138. --### Drawer Functions
  139. local d  = peripheral.find("storagedrawers_container_controller")
  140. local items = {}  -- knows which slot to use for which essentia type
  141.  
  142. if not d then
  143.   error("\n\nNo drawer controller detected.\nPlace on any  side.")
  144. end
  145.  
  146. function scanDrawerInventory()
  147.   slots = {}
  148.   for slot=1, d.getInventorySize() do                                                -- run through every slot in drawer
  149.     local item = d.getStackInSlot(slot)                                              -- get ItemInformation
  150.     if item then                                                                     -- item in slot ?
  151.       local hash = item["nbt_hash"]                                                  -- get its hash
  152.       local a = itemHashList[hash]                                                   -- get essence for that hash
  153.      
  154.       if a then                                                                      -- hash in itemHashList?
  155.         local tmp = {["slot"] = slot}
  156.         items[a] = tmp
  157.       else
  158.         print(item["id"].." not in itemHashList !!!")                                -- that item is not in itemHashList
  159.         itemInfo(item,slot)
  160.       end
  161.     end
  162.   end
  163. end
  164.  
  165. function getSlotOfAspectItem(aspect)
  166.   return items[aspect]["slot"] or -1
  167. end
  168.  
  169. function getAmountOfAspectItem(aspect)
  170.   return d.getStackInSlot(items[aspect]["slot"])["qty"] or 0
  171. end
  172.  
  173. --### Drawer Functions
  174. ---------------------------------------------------------------------------------------------------------------
  175. --### Chest Functions
  176. local c  = peripheral.find("chest") or peripheral.find("iron") or peripheral.find("crystal")
  177.  
  178. if not c then
  179.   error("\n\nNo output chest detected.\nPlace on any  side.")
  180. end
  181.  
  182. function allFromTurtleToChest()
  183.   for i=1,16 do
  184.     c.pullItem(turtleFromChestLocation,i)
  185.   end
  186. end
  187.  
  188. function fromTurtleToChest(slot)
  189.   c.pullItem(turtleFromChestLocation,slot)
  190. end
  191. --### Chest Functions
  192. ---------------------------------------------------------------------------------------------------------------
  193. --### Program Functions
  194. function moveFromDrawerToChest(slot, nrOfItems)
  195.   d.pushItem(turtleFromDrawerDirection, slot, nrOfItems)
  196.   --allFromTurtleToChest()
  197.   fromTurtleToChest(1)
  198. end
  199.  
  200. function refillEssentia(aspect, amount)
  201.   local essentiaPerItem = 2                                 -- 2 hardcoded for etheral essence  
  202.   local nrOfItems = math.ceil(amount/essentiaPerItem)
  203.   --print("Try refilling "..amount.." "..firstCharacterUpperCase(aspect).." with "..nrOfItems.." items.")
  204.   print("Try refilling "..amount.." "..firstCharacterUpperCase(aspect))
  205.  
  206.   local slot = getSlotOfAspectItem(aspect)
  207.   local stored = getAmountOfAspectItem(aspect)
  208.   if slot ~= nil then
  209.     if stored < nrOfItems then
  210.       print("Too less items to restore all "..amount.." essentia. Only "..stored.." available.")
  211.       nrOfItems = stored
  212.     end
  213.     --print("Refilling "..firstCharacterUpperCase(aspect).." with "..nrOfItems.." items. / "..nrOfItems*essentiaPerItem.." essentia")
  214.     print("Refilling "..firstCharacterUpperCase(aspect).." with "..nrOfItems.." items.")
  215.     moveFromDrawerToChest(slot, nrOfItems)
  216.   else
  217.     print("No Item for "..firstCharacterUpperCase(aspect).." found.")
  218.   end
  219. end
  220.  
  221. function checkForEssentiaRefill(aspect,amount)
  222.   local level = getEssentiaAmount(aspect)
  223.   if amount < 0 then
  224.     if level < maxEssentiaAmount then
  225.       refillEssentia(aspect, -1*amount)
  226.     else
  227.       if printMaxEssentiaAmountMessages then
  228.         print("Reached maxEssentiaAmount for "..firstCharacterUpperCase(aspect).." ("..level.."/"..maxEssentiaAmount..")")
  229.       end
  230.     end
  231.   else
  232.     -- print message if
  233.     if level < maxEssentiaAmount then
  234.       print(amount.." "..firstCharacterUpperCase(aspect).." refilled.")
  235.     end
  236.   end
  237. end
  238.  
  239. --### Program Functions
  240. ---------------------------------------------------------------------------------------------------------------
  241. --### Default Main Program Functions
  242. function preRunProgram()
  243.   checkEssentiaProvider()
  244. end
  245.  
  246. function runProgram()
  247.  
  248. end
  249.  
  250.  
  251. function postRunProgram()
  252.  
  253. end
  254.  
  255. local STOP = false
  256. local PROGRAM_RUN_TIMER = nil
  257. function handleEvent(event)
  258.   if event[1] == "timer" and event[2] == PROGRAM_RUN_TIMER then
  259.     preRunProgram()
  260.     runProgram()
  261.     postRunProgram()
  262.     PROGRAM_RUN_TIMER = os.startTimer(programRestartInterval)
  263.     return
  264.   end
  265.   if event[1] == "char" and event[2] == "t" then
  266.     STOP = true
  267.     print("Termination Key detected.\n!!! Terminating the program !!!")
  268.     return
  269.   end
  270.   if event[1] == "essentia" then
  271.     if event[2] == 1 then
  272.       --essentia event
  273.       checkForEssentiaRefill(event[3]:lower(), event[4]) -- essentia and amount
  274.       return
  275.     end
  276.     if event[2] == 2 then
  277.       --power event
  278.       if event[3] then
  279.         print("EssentiaProvider disconnected from the network")
  280.         STOP = true
  281.       else
  282.         print("EssentiaProvider connected to the network")
  283.       end
  284.       return
  285.     end
  286.     if event[2] == 3 then
  287.       --detach event
  288.       STOP = true
  289.       print("Lost connection with EssentiaProvider")
  290.       return
  291.     end
  292.   end
  293.  
  294.   local ignoreList = {"char", "key", "key_up", "mouse_click", "mouse_up", "mouse_drag", "mouse_scroll", "rednet", "turtle_inventory"}
  295.   for i,v in pairs(ignoreList) do
  296.     if event[1] == v then
  297.       return
  298.     end
  299.   end
  300.  
  301.   print("unhandeled event:")
  302.   for i,v in pairs(event) do
  303.     write(i)
  304.     write(":")
  305.     print(v)
  306.   end
  307. end
  308.  
  309. function preLoop()
  310.   term.clear()
  311.   term.setCursorPos(1,1)
  312.   prepareEssentiaProvider()
  313.   scanDrawerInventory()
  314.   print("EssentiaRefiller started")
  315.   PROGRAM_RUN_TIMER = os.startTimer(0)
  316. end
  317.  
  318. function loop()
  319.   while not STOP do
  320.     handleEvent({os.pullEvent()})
  321.   end
  322. end
  323.  
  324. function postLoop()
  325.  
  326. end
  327. --### Default Main Program Functions
  328. ---------------------------------------------------------------------------------------------------------------
  329. preLoop()
  330. loop()
  331. postLoop()
Add Comment
Please, Sign In to add comment