Advertisement
hbar

oldfresco

Mar 12th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.43 KB | None | 0 0
  1. --[[
  2. Fresco v 1.0.0
  3. Matti Vapa, 2013
  4. https://github.com/mattijv/fresco
  5. http://pastebin.com/42KWALhR
  6.  
  7. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  8. NB: this version is for modpacks with older version of OpenPeripheral
  9.     (generally 1.5 packs) and will not be developed further. For newer
  10.     versions, check the github repository linked above.
  11. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  12.  
  13. This program will take as an argument a URL to an image file and request a copy of
  14. it via a server that converts it to a proper format. It then reproduces the image
  15. using Glowstone Illuminators from Thermal Expansion. Required mods are
  16.  
  17.     * ComputerCraft (duh)
  18.     * Thermal Expansion
  19.     * OpenPeripheral
  20.     (* MiscPeripherals if you want to use Resupply Station)
  21.  
  22. Instead of a direct URL you can also input a Minecraft username with the -u handle.
  23. The program will download the skin corresponding to the username and build the face
  24. part of the skin.
  25.  
  26. Possible future features:
  27.    
  28.     * Ability to specify a part of the image to be build, so multiple turtles can
  29.       be used for the same picture. This will probably be implemented.
  30.     * Add more comments so people can easily modify this code.
  31.  
  32. Changelog:
  33.  
  34.     0.1.0 - 0.8.0:
  35.                 * Added most features and tested until 6 AM.
  36.    
  37.     0.9.0:
  38.                 * Released for public testing.
  39.     1.0.0:
  40.                 * Added option to build the image horizontally
  41.                 * Cleaning up the code.
  42.  
  43.  
  44. ]]--
  45.  
  46. if not http then
  47.     print("HTTP must be enabled.")
  48.     return
  49. end
  50.  
  51.  
  52. VERSION = "1.0.0"
  53.  
  54. print("Fresco v "..VERSION)
  55.  
  56. apiURL = "http://lakka.kapsi.fi:62096"
  57. playerURL = "http://s3.amazonaws.com/MinecraftSkins/"
  58.  
  59. local chest = nil
  60. local supply = nil
  61. local url
  62. local maxsize
  63. local height,width
  64. local row,column
  65. -- for finding the face in the skin file
  66. local offsetY,offsetX = 0,0
  67. local ascend = turtle.up
  68. local descend = turtle.down
  69.  
  70. local usage = function()
  71.     print("Usage:")
  72.     print("fresco [-h] [-u playername] [url] [size]")
  73.     print("Either playername with -u option or url to image must be supplied.")
  74.     print("With -u option the size parameter is the dimensions of the face (default 8 pixels).")
  75.  
  76.     --[[
  77.     print("Options:")
  78.     print("-u    Uses the face from the skin of the player with the name 'playername' as the image.")
  79.     print("      If you use -u you don't need to provide the url as an argument.")
  80.     print("url   The url to the desired picture in the form 'http://www.images.com/coolpic.png'.")
  81.     print("      Not needed if you use -u.")
  82.     print("size  The maximum lenght of the longer side. The image will be (down/up)scaled to fit this size.")
  83.     print("      With -u the size is the size of the players face, default 8 pixels.")
  84.     ]]--
  85. end
  86.  
  87. local move = function(f)
  88.     while turtle.getFuelLevel() < 1 do
  89.         print("Low on fuel. Please add fuel to my inventory and press return.")
  90.         local e,c = os.pullEvent("key")
  91.         while c ~= 28 do
  92.             e,c = os.pullEvent("key")
  93.         end
  94.         print("Checking for fuel...")
  95.         shell.run("refuel all")
  96.     end
  97.     while not f() do
  98.         sleep(0.3)
  99.     end
  100.     sleep(0.1)
  101. end
  102.  
  103. local resupplyFromStation = function()
  104.     while not supply.resupply(1) do
  105.         print("Please restock the Resupply Station.")
  106.         sleep(10)
  107.     end
  108. end
  109.  
  110. local resupplyFromChest = function()
  111.     local r = row
  112.     local c = column
  113.     while r < height do
  114.         move(descend)
  115.         r = r + 1
  116.     end
  117.     turtle.turnLeft()
  118.     while c > 1 do
  119.         move(turtle.forward)
  120.         c = c - 1
  121.     end
  122.     turtle.turnRight()
  123.     move(turtle.back)
  124.     local slot = 0
  125.     chest.condense()
  126.     while not chest.getStackInSlot(0) do
  127.             print("Please add more Illuminators to the chest.")
  128.             sleep(5)
  129.             chest.condense()
  130.     end
  131.     chest.pushIntoSlot("down",0,64,slot)
  132.     slot = 1
  133.     while slot < 15 do
  134.         chest.condense()
  135.         if not chest.getStackInSlot(0) then
  136.             break
  137.         end
  138.         chest.pushIntoSlot("down",0,64,slot)
  139.         slot = slot + 1
  140.     end
  141.     move(turtle.forward)
  142.     turtle.turnRight()
  143.     while c < column do
  144.         move(turtle.forward)
  145.         c = c + 1
  146.     end
  147.     turtle.turnLeft()
  148.     while r > row do
  149.         move(ascend)
  150.         r = r - 1
  151.     end
  152. end
  153.  
  154. local placeFront = function(color)
  155.     if turtle.detect() then return end
  156.     if turtle.getItemCount(1) < 2 then
  157.         if supply then
  158.             resupplyFromStation()
  159.         elseif turtle.getItemCount(1) == 0 then
  160.             local slot = 2
  161.             while slot <= 16 do
  162.                 turtle.select(slot)
  163.                 if turtle.transferTo(1) then
  164.                     break
  165.                 end
  166.                 slot = slot + 1
  167.             end
  168.             if slot > 16 then
  169.                 resupplyFromChest()
  170.             end
  171.         end
  172.     end
  173.     turtle.select(1)
  174.     turtle.place()
  175.     local lamp = peripheral.wrap("front")
  176.     lamp.setColor(tonumber(color,16))
  177.     turtle.attack()
  178. end
  179.  
  180. local placeDown = function(color)
  181.     if turtle.detectDown() then return end
  182.     if turtle.getItemCount(1) < 2 then
  183.         if supply then
  184.             resupplyFromStation()
  185.         elseif turtle.getItemCount(1) == 0 then
  186.             local slot = 2
  187.             while slot <= 16 do
  188.                 turtle.select(slot)
  189.                 if turtle.transferTo(1) then
  190.                     break
  191.                 end
  192.                 slot = slot + 1
  193.             end
  194.             if slot > 16 then
  195.                 resupplyFromChest()
  196.             end
  197.         end
  198.     end
  199.     turtle.select(1)
  200.     turtle.placeDown()
  201.     local lamp = peripheral.wrap("bottom")
  202.     lamp.setColor(tonumber(color,16))
  203.     turtle.attackDown()
  204. end
  205.  
  206. local calcFuelNeed = function (h,w)
  207.     local fuel = (h-1)*w+w+1
  208.     if w%2 == 1 then
  209.         fuel = fuel + (h-1)
  210.     end
  211.     if chest then
  212.         local fuelruns = math.floor((w*h-1)/1024)
  213.         for i = 1,fuelruns do
  214.             local columns, frac = math.modf(i*1024/h)
  215.             -- horizontal movement
  216.             fuel = fuel + 2*(columns+1)+2
  217.             -- vertical movement
  218.             if columns % 2 == 1 then
  219.                 fuel = fuel + 2*(h-math.floor(frac*h+0.5)-1)
  220.             else
  221.                 fuel = fuel + 2*(math.floor(frac*h+0.5)-1)
  222.             end
  223.         end
  224.     end
  225.     return fuel
  226. end
  227.  
  228.  
  229. _args = {...}
  230. args = {}
  231.  
  232.  
  233. local place = placeFront
  234.  
  235. local i = 1
  236. while i <= #_args do
  237.     if _args[i] == "-h" then
  238.         place = placeDown
  239.         ascend = turtle.forward
  240.         descend = turtle.back
  241.     elseif _args[i]:sub(1,1) == "-" then
  242.         args[_args[i]] = _args[i+1]
  243.         i = i + 1
  244.     elseif tonumber(_args[i]) ~= nil then
  245.         args["maxsize"] = _args[i]
  246.     else
  247.         args["url"] = _args[i]
  248.     end
  249.     i = i + 1
  250. end
  251.  
  252.  
  253. if not args["url"] and not args["-u"] then
  254.     usage()
  255.     return
  256. else
  257.     if args["-u"] ~= nil then
  258.         url = playerURL..args["-u"]..".png"
  259.         if args["maxsize"] then
  260.                 maxsize = tostring(math.floor(tonumber(args["maxsize"])*(64/8)+0.5))
  261.         end
  262.     else
  263.         url = args["url"]
  264.         maxsize = args["maxsize"]
  265.     end
  266.    
  267. end
  268.  
  269. if not url then
  270.     usage()
  271.     return
  272. end
  273.  
  274. local vars = "url="..url
  275. if maxsize then
  276.     vars = vars.."&maxsize="..maxsize
  277. end
  278. sleep(0.5)
  279. print("Requesting image from")
  280. print(url)
  281. f = http.post(apiURL,vars)
  282. if not f then
  283.     print("No response from image server.")
  284.     return
  285. elseif f.getResponseCode() ~= 200 then
  286.     print("Error while connecting to server!")
  287.     print("Server response")
  288.     print("Code: "..f.getResponseCode())
  289.     print("Message:")
  290.     print(f.readAll())
  291.     return
  292. end
  293.  
  294. local oldSize = f.readLine()
  295. local newSize = f.readLine()
  296. if oldSize ~= newSize then
  297.     print("Original image size was (w,h): "..oldSize)
  298.     print("New size is (w,h): "..newSize)
  299. else
  300.     print("Image size is (w,h): "..newSize)
  301. end
  302.  
  303. local img = {}
  304.  
  305. line = f.readLine()
  306. while line ~= nil do
  307.     table.insert(img,line)
  308.     line = f.readLine()
  309. end
  310. f.close()
  311.  
  312.  
  313. -- X:8, Y:8 to X: 15, Y:15 (top left, bottom right of face)
  314. maxsize = tonumber(maxsize)
  315. if args["-u"] then
  316.     if maxsize then
  317.         -- multiply by 8/64 to get the face size and hack a rounding function
  318.         local size = math.floor(maxsize*0.125+0.5)
  319.         height,width = size,size
  320.         offsetY,offsetX = size,size
  321.     else
  322.         height,width = 8,8
  323.         offsetY,offsetX = 8,8
  324.     end
  325. else
  326.     height,width = #img, #img[1]/8
  327.     row,column = height,1
  328. end
  329.  
  330. row,column = height,1
  331. sleep(0.5)
  332. print("Press return to continue.")
  333. local e,c = os.pullEvent("key")
  334. while c ~= 28 do
  335.     e,c = os.pullEvent("key")
  336. end
  337.  
  338. if peripheral.getType("right") == "resupply" then
  339.     print("Resupply module located, using that for resupply.")
  340.     supply = peripheral.wrap("right")
  341.     if not supply.link() then
  342.         print("\nCould not locate Resupply Station!\nPlease put a Resupply Station near me.")
  343.         return
  344.     end
  345. else
  346.     print("No resupply module, there should be a chest on top of me.")
  347.     if peripheral.getType("top") == nil then
  348.         print("No valid inventory found on top.")
  349.         return
  350.     end
  351.     chest = peripheral.wrap("top")
  352.     if chest.pushIntoSlot == nil then
  353.         print("Inventory does not support required operations.")
  354.         return
  355.     end
  356. end
  357. print("Resupply inventory located.")
  358. sleep(0.5)
  359. print("Calculating needed fuel...")
  360. local fuelNeed = calcFuelNeed(height,width)
  361. sleep(0.5)
  362. print("Fuel consumption will be (approximate): "..tostring(fuelNeed))
  363. print("Current fuel level is: "..tostring(turtle.getFuelLevel()))
  364. if turtle.getFuelLevel() < fuelNeed then
  365.     while turtle.getFuelLevel() < fuelNeed do
  366.         print(string.format("Need %d fuel units more.",fuelNeed-turtle.getFuelLevel()))
  367.         print("Please put more fuel into my inventory and press return.")
  368.         local e,c = os.pullEvent("key")
  369.         while c ~= 28 do
  370.             e,c = os.pullEvent("key")
  371.         end
  372.         print("Checking for fuel...")
  373.         shell.run("refuel all")
  374.     end
  375. end
  376. print("Fuel requirements met. Looking for building materials...")
  377. sleep(0.3)
  378. while turtle.getItemCount(1) < 1 do
  379.     if chest then
  380.         print("Please put Glowstone Illuminators into my inventory.")
  381.     else
  382.         print("Please put Glowstone Illuminators into the first slot of my inventory.")
  383.     end
  384.     print("Press return to continue.")
  385.     local e,c = os.pullEvent("key")
  386.     while c ~= 28 do
  387.         e,c = os.pullEvent("key")
  388.     end
  389. end
  390.  
  391. print("Good to go!")
  392. print(string.format("The process will take around %f minutes to finish.",(1.2*fuelNeed)/60))
  393. print("Press return to begin.")
  394. e,c = os.pullEvent("key")
  395. while c ~= 28 do
  396.     e,c = os.pullEvent("key")
  397. end
  398.  
  399. print("Starting printing...")
  400.  
  401. move(turtle.forward)
  402. turtle.select(1)
  403. while true do
  404.     if column > width then break end
  405.     place(img[row+offsetY]:sub(1+8*(column-1+offsetX),1+8*(column-1+offsetX)+7))
  406.     while row > 1 do
  407.         move(ascend)
  408.         row = row - 1
  409.         place(img[row+offsetY]:sub(1+8*(column-1+offsetX),1+8*(column-1+offsetX)+7))
  410.     end
  411.     turtle.turnRight()
  412.     move(turtle.forward)
  413.     turtle.turnLeft()
  414.     column = column + 1
  415.     if column > width then break end
  416.     place(img[row+offsetY]:sub(1+8*(column-1+offsetX),1+8*(column-1+offsetX)+7))
  417.     while row < height do
  418.         move(descend)
  419.         row = row + 1
  420.         place(img[row+offsetY]:sub(1+8*(column-1+offsetX),1+8*(column-1+offsetX)+7))
  421.     end
  422.     turtle.turnRight()
  423.     move(turtle.forward)
  424.     turtle.turnLeft()
  425.     column = column + 1
  426. end
  427.  
  428. if width % 2 == 1 then
  429.     for i = 1, height-1 do
  430.         move(descend)
  431.     end
  432. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement