Advertisement
hbar

Untitled

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