Advertisement
BombBloke

3dmChar (ComputerCraft)

Jul 25th, 2018
3,883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.61 KB | None | 0 0
  1. -- +--------------------------------------------------------+
  2. -- |                                                        |
  3. -- |                        3dmChar                         |
  4. -- |                                                        |
  5. -- +--------------------------------------------------------+
  6.  
  7. local version = "Version 1.0.0"
  8.  
  9. -- By Jeffrey Alexander, aka Bomb Bloke.
  10. -- Makes OpenComputers 3D print objects out of character skins.
  11. -- http://www.computercraft.info/forums2/index.php?/topic/29653-opencomputers-print3d/
  12.  
  13. ---------------------------------------------
  14. ------------      Load APIs      ------------
  15. ---------------------------------------------
  16.  
  17. local function loadAPI(API, paste)
  18.     if not _G[API] then
  19.         if not (fs.exists(API) or fs.exists(shell.resolve(API))) then
  20.             shell.run("pastebin get " .. paste .. " " .. API)
  21.             os.loadAPI(shell.resolve(API))
  22.         else os.loadAPI(fs.exists(API) and API or shell.resolve(API)) end
  23.     end
  24. end
  25.  
  26. loadAPI("print3d", "Lr9UyrkR")
  27. loadAPI("bbpack",  "cUYTGbpb")
  28. loadAPI("GIF",     "5uk9uRjC")
  29.  
  30. if not fs.exists(shell.resolve("worldTextures.pal")) then shell.run("pastebin get 5aeuGe6Q worldTextures.pal") end
  31.  
  32. ---------------------------------------------
  33. ------------   Texture Offsets   ------------
  34. ---------------------------------------------
  35.  
  36. local head = {
  37.     -- start,        xDir,        yDir,     offset1,dimensions
  38.     {{ 4,32,12}, { 1,  0,  0}, { 0, -1,  0},  8,  8,  8,  8, 40,  8},  -- Head front
  39.     {{ 4,32, 4}, { 0,  0,  1}, { 0, -1,  0},  0,  8,  8,  8, 32,  8},  -- Head right
  40.     {{12,32,12}, { 0,  0, -1}, { 0, -1,  0}, 16,  8,  8,  8, 48,  8},  -- Head left
  41.     {{12,32, 4}, {-1,  0,  0}, { 0, -1,  0}, 24,  8,  8,  8, 56,  8},  -- Head back
  42.     {{ 4,32, 4}, { 1,  0,  0}, { 0,  0,  1},  8,  0,  8,  8, 40,  0},  -- Head top
  43.     {{12,24, 4}, {-1,  0,  0}, { 0,  0,  1}, 16,  0,  8,  2, 48,  0},  -- Head bottom(1)
  44.     {{12,24,10}, {-1,  0,  0}, { 0,  0,  1}, 16,  6,  8,  2, 48,  6},  -- Head bottom(2)
  45. }
  46.  
  47. local v1 = {
  48.     {{ 4,24,10}, { 1,  0,  0}, { 0, -1,  0}, 20, 20,  8, 12},  -- Body front
  49.     {{ 4,24, 6}, { 1,  0,  0}, { 0, -1,  0}, 32, 20,  8, 12},  -- Body back
  50.     {{ 0,24,10}, { 1,  0,  0}, { 0, -1,  0}, 44, 20,  4, 12},  -- Right arm front
  51.     {{16,24,10}, {-1,  0,  0}, { 0, -1,  0}, 44, 20,  4, 12},  -- Left arm front
  52.     {{ 0,24, 6}, { 0,  0,  1}, { 0, -1,  0}, 40, 20,  4, 12},  -- Right arm side
  53.     {{16,24, 6}, { 0,  0,  1}, { 0, -1,  0}, 40, 20,  4, 12},  -- Left arm side
  54.     {{ 4,24, 6}, {-1,  0,  0}, { 0, -1,  0}, 52, 20,  4, 12},  -- Right arm back
  55.     {{12,24, 6}, { 1,  0,  0}, { 0, -1,  0}, 52, 20,  4, 12},  -- Left arm back
  56.     {{ 0,24, 6}, { 1,  0,  0}, { 0,  0,  1}, 44, 16,  4,  4},  -- Right arm top
  57.     {{16,24, 6}, {-1,  0,  0}, { 0,  0,  1}, 44, 16,  4,  4},  -- Left arm top
  58.     {{ 0,12, 6}, { 1,  0,  0}, { 0,  0,  1}, 48, 16,  4,  4},  -- Right arm bottom
  59.     {{16,12, 6}, {-1,  0,  0}, { 0,  0,  1}, 48, 16,  4,  4},  -- Left arm bottom
  60.     {{ 4,12,10}, { 1,  0,  0}, { 0, -1,  0},  4, 20,  4, 12},  -- Right leg front
  61.     {{12,12,10}, {-1,  0,  0}, { 0, -1,  0},  4, 20,  4, 12},  -- Left leg front
  62.     {{ 4,12, 6}, { 0,  0,  1}, { 0, -1,  0},  0, 20,  4, 12},  -- Right leg side
  63.     {{12,12, 6}, { 0,  0,  1}, { 0, -1,  0},  0, 20,  4, 12},  -- Left leg side
  64.     {{ 8,12, 6}, {-1,  0,  0}, { 0, -1,  0}, 12, 20,  4, 12},  -- Right leg back
  65.     {{ 8,12, 6}, { 1,  0,  0}, { 0, -1,  0}, 12, 20,  4, 12},  -- Left leg back
  66.     {{ 4, 0, 6}, { 1,  0,  0}, { 0,  0,  1},  8, 16,  4,  4},  -- Right leg bottom
  67.     {{12, 0, 6}, {-1,  0,  0}, { 0,  0,  1},  8, 16,  4,  4},  -- Left leg bottom
  68. }
  69.  
  70. local v2 = {
  71.     {{ 4,24,10}, { 1,  0,  0}, { 0, -1,  0}, 20, 20,  8, 12, 20, 36},  -- Body front
  72.     {{ 4,24, 6}, { 1,  0,  0}, { 0, -1,  0}, 32, 20,  8, 12, 32, 36},  -- Body back
  73.     {{ 0,24,10}, { 1,  0,  0}, { 0, -1,  0}, 44, 20,  4, 12, 44, 36},  -- Right arm front
  74.     {{ 0,24, 6}, { 0,  0,  1}, { 0, -1,  0}, 40, 20,  4, 12, 40, 36},  -- Right arm side
  75.     {{ 4,24, 6}, {-1,  0,  0}, { 0, -1,  0}, 52, 20,  4, 12, 52, 36},  -- Right arm back
  76.     {{ 0,24, 6}, { 1,  0,  0}, { 0,  0,  1}, 44, 16,  4,  4, 44, 32},  -- Right arm top
  77.     {{ 0,12, 6}, { 1,  0,  0}, { 0,  0,  1}, 48, 16,  4,  4, 48, 32},  -- Right arm bottom
  78.     {{ 4,12,10}, { 1,  0,  0}, { 0, -1,  0},  4, 20,  4, 12,  4, 36},  -- Right leg front
  79.     {{ 4,12, 6}, { 0,  0,  1}, { 0, -1,  0},  0, 20,  4, 12,  0, 36},  -- Right leg side
  80.     {{ 8,12, 6}, {-1,  0,  0}, { 0, -1,  0}, 12, 20,  4, 12, 12, 36},  -- Right leg back
  81.     {{ 4, 0, 6}, { 1,  0,  0}, { 0,  0,  1},  8, 16,  4,  4,  8, 32},  -- Right leg bottom
  82.     {{12,24,10}, { 1,  0,  0}, { 0, -1,  0}, 36, 52,  4, 12, 52, 52},  -- Left arm front
  83.     {{16,24,10}, { 0,  0, -1}, { 0, -1,  0}, 40, 52,  4, 12, 56, 52},  -- Left arm side
  84.     {{16,24, 6}, {-1,  0,  0}, { 0, -1,  0}, 44, 52,  4, 12, 60, 52},  -- Left arm back
  85.     {{12,24, 6}, { 1,  0,  0}, { 0,  0,  1}, 36, 48,  4,  4, 52, 48},  -- Left arm top
  86.     {{12,12, 6}, { 1,  0,  0}, { 0,  0,  1}, 40, 48,  4,  4, 56, 48},  -- Left arm bottom
  87.     {{ 8,12,10}, { 1,  0,  0}, { 0, -1,  0}, 20, 52,  4, 12,  4, 52},  -- Left leg front
  88.     {{12,12,10}, { 0,  0, -1}, { 0, -1,  0}, 24, 52,  4, 12,  8, 52},  -- Left leg side
  89.     {{12,12, 6}, {-1,  0,  0}, { 0, -1,  0}, 28, 52,  4, 12, 12, 52},  -- Left leg back
  90.     {{ 8, 0, 6}, { 1,  0,  0}, { 0,  0,  1}, 24, 48,  4,  4,  8, 48},  -- Left leg bottom
  91. }
  92.  
  93. ---------------------------------------------
  94. ------------      Load Skin      ------------
  95. ---------------------------------------------
  96.  
  97. local filename = ...
  98.  
  99. if not filename then
  100.     print("Usage: 3dmChar <skinGIFfile>")
  101.     return
  102. end
  103.  
  104. if filename:lower():sub(-4) ~= ".gif" and not fs.exists(filename) then filename = filename .. ".gif" end
  105. if not fs.exists(filename) then error("Failed opening file: file not found", 0) end
  106. local GIFImage = GIF.loadGIF(filename, shell.resolve("worldTextures.pal"))
  107. if filename:lower():sub(-4) == ".gif" then filename = filename:sub(1, -5) end
  108.  
  109. local image = {}
  110. for y = 1, GIFImage.height do
  111.     image[y] = {}
  112.     for x = 1, GIFImage.width do image[y][x] = 0 end
  113. end
  114.  
  115. local pal = {}
  116. for i = 1, #GIFImage.pal do pal[i] = GIFImage.pal[i][4] end
  117. pal[0] = "concrete_purple"
  118.  
  119. GIFImage = GIFImage[1]
  120.  
  121. for y = 1, GIFImage.yend do
  122.     local x, GIFImageY, imageY = GIFImage.xstart + 1, GIFImage[y], image[y + GIFImage.ystart]
  123.     for i = 1, #GIFImageY do
  124.         local GIFImageYI = GIFImageY[i]
  125.         if type(GIFImageYI) == "number" then
  126.             x = x + GIFImageYI
  127.         else
  128.             for pixel = 1, #GIFImageYI do imageY[x + pixel - 1] = GIFImageYI[pixel] end
  129.             x = x + #GIFImageYI
  130.         end
  131.     end
  132. end
  133.  
  134. GIFImage = nil
  135.  
  136. ---------------------------------------------
  137. ------------     Paint Model     ------------
  138. ---------------------------------------------
  139.  
  140. local obj = print3d.initModel(filename, 16, 32, 16)
  141.  
  142. local function plotPlanes(planes)
  143.     for i = 1, #planes do
  144.         local plane = planes[i]
  145.         local draw = obj.get2DPlane(plane[1], plane[2], plane[3])
  146.  
  147.         for y = 1, plane[7] do for x = 1, plane[6] do
  148.             local px = image[y+plane[5]][x+plane[4]]
  149.            
  150.             if #plane > 7 then
  151.                 local px2 = image[y+plane[9]][x+plane[8]]
  152.                 if px2 > 0 then px = px2 end
  153.             end
  154.            
  155.             draw(x, y, pal[px])
  156.         end end
  157.     end
  158. end
  159.  
  160. plotPlanes(head)
  161. plotPlanes(#image > 32 and v2 or v1)
  162.  
  163. -- Divider
  164. local draw = obj.get2DPlane({0, 16, 0}, { 1,  0,  0}, { 0,  0,  1})
  165. for y = 7, 10 do for x = 1, 16 do draw(x, y, "concrete_gray") end end
  166.  
  167. ---------------------------------------------
  168. ------------    Create Output    ------------
  169. ---------------------------------------------
  170.  
  171. local outFile = fs.open(filename .. ".txt", "w")
  172.  
  173. obj = obj.parse()
  174.  
  175. for i = 1, #obj do
  176.     local cmd = "give @p opencomputers:print 1 0 " .. obj[i]
  177.     outFile.writeLine(cmd .. "\n")
  178.     commands.exec(cmd)
  179. end
  180.  
  181. outFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement