Advertisement
Guest User

tst

a guest
Oct 21st, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.93 KB | None | 0 0
  1. --Usage: open and edit priceSet to set the prices of your items to be listed.
  2. os.loadAPI("buttonAPI")
  3. os.loadAPI("priceSet")
  4.  
  5. local modem = peripheral.wrap("top")
  6. local monitor = peripheral.wrap("right")
  7. local chestStock = peripheral.wrap("container_chest_12")
  8. local chestBank = peripheral.wrap("container_chest_11")
  9. local chestPay = peripheral.wrap("container_chest_13")
  10. local chestOut = peripheral.wrap("container_chest_10")
  11.  
  12. chestStock.condenseItems()
  13. monitor.clear()
  14. modem.open(1)
  15.  
  16. local items = chestStock.getAllStacks()
  17. local itemNames = {}
  18. local itemQtys = {}
  19. local functions = {}
  20. local itemPrices = {}
  21. local itemQtyPerPurchase = {}
  22. local buyQty = {}
  23. local correctNames = {}
  24. local width = nil
  25. local height = nil
  26. width, height = monitor.getSize()
  27. local xDraw = 3
  28. local yDraw = 2
  29.  
  30. function indexChest(chestName)
  31.   items = chestName.getAllStacks()
  32.   itemNames = {}
  33.   itemQtys = {}
  34.   currItemNum = 1
  35.  
  36.   for i = 1, #items do
  37.     itemName = nameFix(items, i)
  38.     dupCheck = checkForDuplicates(itemNames, itemName)
  39.     if dupCheck ~= nil then
  40.       itemQtys[dupCheck] = itemQtys[dupCheck] + items[i].qty
  41.     else
  42.       itemNames[currItemNum] = itemName
  43.       itemQtys[currItemNum] = items[i].qty
  44.       currItemNum = currItemNum + 1
  45.     end
  46.   end
  47. end
  48.  
  49. function nameFix(item, i)
  50.   itemName = item[i].name
  51.   if string.find(itemName, string.char(167)) ~= nil then
  52.     itemName = string.sub(itemName, 4)
  53.   end
  54.  
  55.   if string.find(itemName, "tile.extrautils:") ~= nil then
  56.     itemName = string.sub(itemName, 17)
  57.   end
  58.  
  59.   if string.find(itemName, "Infinity Orb") ~= nil then
  60.     if item[i].dmg == 10 then
  61.       itemName = "B. Infinity Orb"
  62.     elseif item[i].dmg == 11 then
  63.       itemName = "G. Infinity Orb"
  64.     elseif item[i].dmg == 12 then
  65.       itemName = "R. Infinity Orb"
  66.     end
  67.   end
  68.  
  69.   if correctNames[itemName] ~= nil then
  70.     itemName = correctNames[itemName]
  71.   end
  72.  
  73.   return itemName
  74. end
  75.  
  76. function checkForDuplicates(itemNames, newItem)
  77.   for j = 1, #itemNames do
  78.     if newItem == itemNames[j] then
  79.       return j
  80.     end
  81.   end
  82.   return nil
  83. end
  84.  
  85. function findSlotNumber(items, name)
  86.   for j = 1, #items do
  87.     if name == items[j].name then
  88.       return j
  89.     end
  90.   end
  91.   return nil
  92. end
  93.  
  94. function fillTable()
  95.   j = 1
  96.   yDraw = 2
  97.   xDraw = 2
  98.   buttonAPI.setTableNormal("Twilight Forest (1 Iron)", openDoor, xDraw, xDraw + 24, yDraw, yDraw + 2)
  99.   yDraw = 6
  100.   for i = 1, #itemNames do
  101.     buttonAPI.setTable(itemNames[i] .. ", " .. itemQtys[i] .. " ($" .. itemPrices[itemNames[i]] .. ")", printItem, {i}, xDraw, xDraw + 24, yDraw, yDraw + 2)
  102.     yDraw = 2 + (j+1)*4
  103.     j = j + 1
  104.     if yDraw >= height then
  105.       yDraw = 2
  106.       xDraw = xDraw + 28
  107.       j = 1
  108.     end
  109.   end
  110.   buttonAPI.screen()
  111. end
  112.  
  113. function purchaseButtons(id)
  114.   buttonAPI.setTable("Confirm", confirm, id, 2, 10, 20, 22)
  115.   buttonAPI.setTableNormal("Cancel", cancel, 47, 55, 20, 22)
  116.   buttonAPI.screen()
  117. end
  118.  
  119. function quantityButtons(id)
  120.   buttonAPI.setTable("+", addQuantity, id, 2, 6, 20, 22)
  121.   buttonAPI.setTable("Continue", confirmScreen, id, 16, 24, 20, 22)
  122.   buttonAPI.setTableNormal("Cancel", cancel, 29, 37, 20, 22)
  123.   buttonAPI.setTable("-", subQuantity, id, 51, 55, 20, 22)
  124.   buttonAPI.screen()
  125. end
  126.  
  127. function addQuantity(id)
  128.   if buyQty[itemNames[id]] + itemQtyPerPurchase[itemNames[id]] <= itemQtys[id] then
  129.     buyQty[itemNames[id]] = buyQty[itemNames[id]] + itemQtyPerPurchase[itemNames[id]]
  130.   end
  131.   buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[itemNames[id]] .. ".    ")
  132.   buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ".    ")
  133. end
  134.  
  135. function subQuantity(id)
  136.   if buyQty[itemNames[id]] >= itemQtyPerPurchase[itemNames[id]] then
  137.     buyQty[itemNames[id]] = buyQty[itemNames[id]] - itemQtyPerPurchase[itemNames[id]]
  138.   end
  139.   buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[itemNames[id]] .. ".    ")
  140.   buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ".    ")
  141. end
  142.  
  143. function confirmScreen(id)
  144.   buttonAPI.clearTable()
  145.   if (buyQty[itemNames[id]] > 1 and string.sub(itemNames[id], string.len(itemNames[id]) ) ~= "s") then
  146.     buttonAPI.label(3, 2, "You are attemping to purchase " .. buyQty[itemNames[id]] .. " " .. itemNames[id] .. "s.")
  147.   else
  148.     buttonAPI.label(3, 2, "You are attemping to purchase " .. buyQty[itemNames[id]] .. " " .. itemNames[id] .. ".")
  149.   end
  150.   buttonAPI.label(3, 6, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ".    ")
  151.   buttonAPI.label(3, 10, "Please confirm to continue or cancel to leave.")
  152.   purchaseButtons(id)
  153. end
  154.  
  155. function getClick()
  156.   event,side,x,y = os.pullEvent("monitor_touch")
  157.   buttonAPI.checkxy(x, y)
  158. end
  159.  
  160. function confirm(id)
  161.   slot = checkForDuplicates(items, itemNames[id])
  162.   i = 1
  163.   leave = false
  164.   remaining = 0
  165.  
  166.   sendMessage("getPayment", id, slot, remaining)
  167.   evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  168.   print(sendCh .. " <- " .. recCh .. ": " .. message)
  169.   while message == "atPayment" do
  170.     if i ~= 1 then
  171.       evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  172.       print(sendCh .. " <- " .. recCh .. ": " .. message)
  173.     end
  174.    
  175.     evnt, side, sendCh, recCh, remaining, sendDistance = os.pullEvent("modem_message")
  176.     print(sendCh .. " <- " .. recCh .. ": " .. remaining)
  177.    
  178.     if i > 10 then
  179.       leave = true
  180.       remaining = 0
  181.       break
  182.     elseif message == "noChange" then
  183.       leave = true
  184.       buttonAPI.label(15, 6, "Correct change could not be found. Please contact the shop owner or try a smaller currency.")
  185.       sleep(4)
  186.       break
  187.     else
  188.       sendMessage("collectPayment", id, slot, remaining)
  189.       i = i + 1
  190.     end
  191.   end
  192.   if leave ~= true then
  193.     sendMessage("collectItem", id, slot, remaining)
  194.     evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  195.     evnt, side, sendCh, recCh, extra, sendDistance = os.pullEvent("modem_message")
  196.     sendMessage("depositPayment", id, slot, extra)
  197.     evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  198.     sendMessage("backToStart", id, slot, remaining)
  199.   end
  200.   cancel()
  201. end
  202.  
  203. function cancel()
  204.   buttonAPI.clearTable()
  205.   buyQty = priceSet.setQPP()
  206.   chestStock.condenseItems()
  207.   indexChest(chestStock)
  208.   fillTable()
  209. end
  210.  
  211. function printItem(id)
  212.   local i = id[1]
  213.   local name = itemNames[i]
  214.   local qty = itemQtys[i]
  215.   buyQty[name] = itemQtyPerPurchase[name]
  216.  
  217.   buttonAPI.clearTable()
  218.   if itemQtys[i] > 1 then
  219.     if string.sub(name, string.len(name)) ~= "s" then
  220.       buttonAPI.label(3, 2, "There are " .. qty .. " " .. name .."s.")
  221.     else
  222.       buttonAPI.label(3, 2, "There are " .. qty .. " " .. name .. ".")
  223.     end
  224.     buttonAPI.label(3, 6, "To increase the quantity press the \"+\" button,")
  225.     buttonAPI.label(3, 8, "\"-\" to decrease.")
  226.     buttonAPI.label(3, 12, "Once the quantity is correct press \"Continue\"")
  227.     buttonAPI.label(3, 14, "or press \"Cancel\" to leave.")
  228.     buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[name] .. ".    ")
  229.     buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[name] * buyQty[name])/itemQtyPerPurchase[name] .. ".    ")
  230.     quantityButtons(i)
  231.   else
  232.     confirmScreen(i)
  233.   end
  234. end
  235.  
  236. function openDoor()
  237.   redstone.setOutput("back", false)
  238.   sleep(2)
  239.   redstone.setOutput("back", true)
  240. end
  241.  
  242. function findChange(changeAmount)
  243.   chestBank.condenseItems()
  244.   indexChest(chestBank)
  245.  
  246.   totalChestValue = 0
  247.  
  248.   for i = 1, #itemNames do
  249.     totalChestValue = totalChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  250.   end
  251.  
  252.   if totalChestValue >= changeAmount then
  253.     return true
  254.   else
  255.     return false
  256.   end
  257. end
  258.  
  259. function correctAmount(amount, rem, amountType)
  260.   if amountType == "bioPaymentAmount" then
  261.     if amount < 0 then
  262.       amount = rem  
  263.     end
  264.   elseif amountType == "gioPaymentAmount" then
  265.     if amount < 0 then
  266.       amount = 1
  267.     else
  268.       amount = 0
  269.     end
  270.   elseif amountType == "extraAmount" then
  271.     if amount < 0 then
  272.       amount = 0
  273.     end
  274.   return amount
  275. end
  276.  
  277. function sendMessage(type, id, slot, remaining)
  278.   chestStock.condenseItems()
  279.   indexChest(chestStock)
  280.   if #itemNames > 0 then
  281.     name = itemNames[id]
  282.     qty = itemQtys[id]
  283.     price = itemPrices[name]
  284.     qtyBuying = buyQty[name]
  285.     itemQPP = itemQtyPerPurchase[name]
  286.     if remaining == 0 then
  287.       cost = (price * qtyBuying)/itemQPP
  288.     else
  289.       cost = remaining
  290.     end
  291.   end
  292.  
  293.   totalChestValue = 0
  294.   bioChestValue = 0
  295.   bioLocation = 0
  296.   gioChestValue = 0
  297.   gioLocation = 0
  298.   rioChestValue = 0
  299.   rioLocation = 0
  300.  
  301.   if type == "getPayment" then
  302.     modem.transmit(1, 1, "getPayment")
  303.     print("1 -> 1 : getPayment")
  304.   elseif type == "collectPayment" then
  305.     chestPay.condenseItems()
  306.     indexChest(chestPay)
  307.     for i = 1, #itemNames do
  308.       totalChestValue = totalChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  309.       if itemNames[i] == "B. Infinity Orb" then
  310.         bioLocation = i
  311.         bioChestValue = bioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  312.       elseif itemNames[i] == "G. Infinity Orb" then
  313.         gioLocation = i
  314.         gioChestValue = gioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  315.       elseif itemNames[i] == "R. Infinity Orb" then
  316.         rioLocation = i
  317.         rioChestValue = rioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  318.       end
  319.     end
  320.     if totalChestValue < cost then
  321.       if #items == 0 then
  322.         buttonAPI.label(15, 6, "No payment has been found. Try again.")
  323.         modem.transmit(1, 1, "wrongPayment")
  324.         print("1 -> 1 : items = 0, wrongPayment")
  325.       else
  326.         buttonAPI.label(15, 6, "The amount entered is not enough! Missing: $" .. cost)
  327.         modem.transmit(1, 1, "wrongPayment")
  328.         print("1 -> 1 : tCV < " .. cost .. ", wrongPayment")
  329.       end
  330.     elseif totalChestValue == cost then
  331.       for i = 1, #items do
  332.         chestPay.pushItem("down", i, itemQtys[i])
  333.       end
  334.       modem.transmit(1, 1, "collectPayment")
  335.       print("1 -> 1 : rem = 0, collectPayment")
  336.       modem.transmit(1, 1, 0)
  337.     elseif totalChestValue > cost then
  338.       if rioChestValue > 0 then
  339.  
  340.         qtyToGetRed = math.floor(cost/(itemQtyPerPurchase[itemNames[rioLocation]] * itemPrice[itemNames[rioLocation]]))
  341.         priceRed = itemPrices[itemNames[rioLocation]] * qtyToGetRed
  342.  
  343.       else
  344.  
  345.         qtyToGetRed = 0
  346.         priceRed = 0
  347.  
  348.       end
  349.  
  350.  
  351.  
  352.       if gioChestValue > 0 then
  353.  
  354.         qtyToGetGreen = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrice[itemNames[gioLocation]]))
  355.  
  356.         priceGreen = itemPrices[itemNames[gioLocation]] * qtyToGetGreen
  357.         remGreen = cost - priceGreen
  358.  
  359.       else
  360.  
  361.         qtyToGetGreen = 0
  362.  
  363.         priceGreen = 0
  364.         remGreen = 0
  365.  
  366.       end
  367.  
  368.       costDiffBlue = cost - bioChestValue
  369.  
  370.       if costDiffBlue > 0 then
  371.         costDiffGreen = costDiffBlue - gioChestValue
  372.         if costDiffGreen > 0 then --Only red or a combination of the three
  373.           costDiffRed = costDiffGreen - rioChestValue
  374.           --some stuff here
  375.         else --Only green or blue+green
  376.           extra = correctAmount(remGreen * -1, 0, "extraAmount")
  377.           if findChange(extra) then
  378.             chestPay.pushItem("down", bioLocation, correctAmount(itemQtys[bioLocation] - remGreen, remGreen, "bioPaymentAmount") ) --this works
  379.             chestPay.pushItem("down", gioLocation, qtyToGetGreen + correctAmount(itemQtys[bioLocation] - remGreen, 0, "gioPaymentAmount"))
  380.             modem.transmit(1, 1, "collectPayment")
  381.             print("1 -> 1 : costDiffBlue > 0, extra = " .. extra .. ", collectPayment")
  382.             modem.transmit(1, 1, extra)
  383.           else
  384.             modem.transmit(1, 1, "noChange")
  385.             print("1 -> 1 : gio >= cost, noChange")
  386.         end
  387.       else --Only blue
  388.         chestPay.pushItem("down", itemQtys[bioLocation], cost)
  389.         modem.transmit(1, 1, "collectPayment")
  390.         print("1 -> 1 : costDiffBlue <= 0, collectPayment")
  391.         modem.transmit(1, 1, 0)
  392.       end
  393.     end
  394.   elseif type == "collectItem" then
  395.     targetSlot = findSlotNumber(items, name)
  396.     print(targetSlot)
  397.     nextSlot = 0
  398.     while qtyBuying >= 64 do
  399.       chestStock.pushItem("up", targetSlot + nextSlot, 64)
  400.       qtyBuying = qtyBuying - 64
  401.       nextSlot = nextSlot + 1
  402.     end
  403.     chestStock.pushItem("up", targetSlot + nextSlot, qtyBuying)
  404.     modem.transmit(1, 1, "dropOffPayment")
  405.     print("1 -> 1 : qtyBuying = " .. qtyBuying .. ", dropOffPayment")
  406.     modem.transmit(1, 1, qtyBuying)
  407.   elseif type == "depositPayment" then
  408.     extra = remaining
  409.     chestBank.condenseItems()
  410.     indexChest(chestBank)
  411.     for i = 1, #itemNames do
  412.       totalChestValue = totalChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  413.       if itemNames[i] == "B. Infinity Orb" then
  414.         bioLocation = i
  415.         bioChestValue = bioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  416.       elseif itemNames[i] == "G. Infinity Orb" then
  417.         gioLocation = i
  418.         gioChestValue = gioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  419.       elseif itemNames[i] == "R. Infinity Orb" then
  420.         rioLocation = i
  421.         rioChestValue = rioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  422.       end
  423.     end
  424.    
  425.     if extra ~= 0 then
  426.       if bioChestValue >= extra then
  427.         extraDiff = bioChestValue - extra
  428.         chestBank.pullItem("up", bioLocation, extraDiff)
  429.         modem.transmit(1, 1, "dropOffItem")
  430.         print("1 -> 1 : bio >= extra, dropOffItem")
  431.       elseif gioChestValue >= extra then
  432.         qtyToGet = math.floor(extra/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  433.         rem = extra - itemPrices[itemNames[gioLocation]] * qtyToGet
  434.         if rem > 0 then
  435.           if bioChestValue >= rem then
  436.             chestBank.pullItem("up", bioLocation, rem)
  437.             chestBank.pullItem("up", gioLocation, qtyToGet)
  438.             modem.transmit(1, 1, "dropOffItem")
  439.             print("1 -> 1 : gio >= extra, rem > 0, bio >= extra, dropOffItem")
  440.           end
  441.         else
  442.           chestBank.pullItem("up", gioLocation, qtyToGet)
  443.           modem.transmit(1, 1, "dropOffItem")
  444.           print("1 -> 1 : gio >= cost, dropOffItem")
  445.         end
  446.       elseif bioChestValue+gioChestValue >= extra then
  447.         qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  448.         rem = cost - itemPrices[itemNames[gioLocation]] * qtyToGet
  449.         chestPay.pushItem("down", bioLocation, rem)
  450.         chestPay.pushItem("down", gioLocation, qtyToGet)
  451.         modem.transmit(1, 1, "dropOffItem")
  452.         print("1 -> 1 : bio + gio >= extra, dropOffItem")
  453.       elseif rioChestValue >= extra then
  454.         qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[rioLocation]] * itemPrices[itemNames[rioLocation]] ))
  455.         rem = cost - itemPrices[itemNames[rioLocation]] * qtyToGet
  456.         if rem > 0 then
  457.           if bioChestValue >= rem then
  458.             chestBank.pullItem("up", bioLocation, rem)
  459.             chestBank.pullItem("up", rioLocation, qtyToGet)
  460.             modem.transmit(1, 1, "dropOffItem")
  461.             print("1 -> 1 : rio >= extra, rem > 0, bio >= rem, dropOffItem")
  462.           elseif gioChestValue >= rem then
  463.             qtyToGet = math.floor(extra/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  464.             rem = extra - itemPrices[itemNames[gioLocation]] * qtyToGet
  465.             if rem > 0 then
  466.               if bioChestValue >= rem then
  467.                 chestBank.pullItem("up", bioLocation, rem)
  468.                 chestBank.pullItem("up", gioLocation, qtyToGet)
  469.                 modem.transmit(1, 1, "dropOffItem")
  470.                 print("1 -> 1 : rio >= extra, rem > 0, gio >= rem, bio >= rem2, dropOffItem")
  471.               end
  472.             else
  473.               chestBank.pullItem("up", gioLocation, qtyToGet)
  474.               modem.transmit(1, 1, "dropOffItem")
  475.               print("1 -> 1 : rio >= extra, rem > 0, gio >= rem, dropOffItem")
  476.             end
  477.           end
  478.         end
  479.       end
  480.     else
  481.       chestBank.pullItem("up", 1, 64)
  482.       modem.transmit(1, 1, "dropOffItem")
  483.       print("1 -> 1: extra = 0, dropOffItem")
  484.     end
  485.   elseif type == "backToStart" then
  486.     modem.transmit(1, 2, "backToStart")
  487.     print("1 -> 1 : backToStart")
  488.   end  
  489. end
  490.  
  491. correctNames["tile.TFTowerDevice.0.name"] = "Reappearing Block"
  492. correctNames["cursedearthside.name"] = "Cursed Earth"
  493. correctNames["cobblestone_compressed.0.name"] = "Compr. Cobble"
  494. correctNames["Bucket of Liquid Essence"] = "Liquid Essence"
  495.  
  496. itemPrices = priceSet.setPrices()
  497. itemQtyPerPurchase = priceSet.setQPP()
  498. buyQty = priceSet.setQPP()
  499.  
  500. chestStock.condenseItems()
  501. indexChest(chestStock)
  502.  
  503. fillTable()
  504.  
  505. while true do getClick() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement