G2A Many GEOs
SHARE
TWEET

builderapi(API)

kssr3951 Apr 18th, 2015 (edited) 239 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- -------------------------------
  2. -- builder(API)
  3. -- -------------------------------
  4. Builder = { }
  5. Builder.new = function()
  6.     local obj = {}
  7.     obj.event = {
  8.       lackOfBuildingMaterialFunction = nil,
  9.       checkFuelLevelFunction = nil
  10.     }
  11.     return setmetatable(obj, {__index = Builder})
  12.   end
  13. -- -------------------------------
  14. -- event
  15. -- -------------------------------
  16. Builder.lackOfBuildingMaterial = function(self, myT)
  17.   ut.debug("lackOfBuildingMaterial!!!")
  18.   if nil ~= self.event.lackOfBuildingMaterialFunction then
  19.     return self.event.lackOfBuildingMaterialFunction(myT)
  20.   end
  21.   return true
  22. end
  23. Builder.checkFuelLevel = function(self, myT)
  24.   ut.debug("checkFuelLevel!!!")
  25.   if nil ~= self.event.checkFuelLevelFunction then
  26.     return self.event.checkFuelLevelFunction(myT)
  27.   end
  28.   return true
  29. end
  30. -- -------------------------------
  31. -- application
  32. -- -------------------------------
  33. Builder.moveToInitialPosition = function(self, myTMesh, meshSize, mod, dir, meshX, meshZ)
  34.   --debug("meshX, meshZ = " .. tostring(meshX) .. ", " .. tostring(meshZ))
  35.   local tgtX, tgtZ
  36.   if "forward" == dir then
  37.     tgtZ = meshZ - mod.size.z + 1
  38.     tgtX = meshX
  39.     myTMesh:moveTo(tgtX, tgtZ)
  40.     myTMesh:turnTo(myturtle.DIR_FORWARD)
  41.   elseif "left" == dir then
  42.     tgtZ = meshZ - mod.size.x + 1
  43.     tgtX = meshX + mod.size.z - 1
  44.     myTMesh:moveTo(tgtX, tgtZ)
  45.     myTMesh:turnTo(myturtle.DIR_LEFT)
  46.   elseif "right" == dir then
  47.     tgtZ = meshZ
  48.     tgtX = meshX
  49.     myTMesh:moveTo(tgtX, tgtZ)
  50.     myTMesh:turnTo(myturtle.DIR_RIGHT)
  51.   elseif "back" == dir then
  52.     tgtZ = meshZ
  53.     tgtX = meshX + mod.size.x - 1
  54.     myTMesh:moveTo(tgtX, tgtZ)
  55.     myTMesh:turnTo(myturtle.DIR_BACK)
  56.   end
  57.   --debug("tgtX, tgtZ = " .. tostring(tgtX) .. ", " .. tostring(tgtZ))
  58. end
  59. Builder.clearBuildArea = function(self, mdlDat)
  60.   local function clearLine(mdlDat)
  61.     for k = 1, mdlDat.size.z - 1 do
  62.       myturtle.surelyFwd()
  63.     end
  64.     turtle.turnLeft()
  65.     turtle.turnLeft()
  66.     for k = 1, mdlDat.size.z - 1 do
  67.       myturtle.surelyFwd()
  68.     end
  69.   end
  70.   local function clearPlane(mdlDat)
  71.     for j = 1, mdlDat.size.x do
  72.       Builder.clearLine(mdlDat, isLast2)
  73.       if j < mdlDat.size.x then
  74.         turtle.turnLeft()
  75.         myturtle.surelyFwd()
  76.         turtle.turnLeft()
  77.       end
  78.     end
  79.     turtle.turnRight()
  80.     for j = 1, mdlDat.size.x - 1 do
  81.       myturtle.surelyFwd()
  82.     end
  83.     turtle.turnRight()
  84.   end
  85.   for i = 1, mdlDat.size.y do
  86.     Builder.clearPlane(mdlDat, isLast)
  87.     if i < mdlDat.size.y then
  88.       myturtle.surelyDigUp()
  89.       myturtle.surelyUp()
  90.     end
  91.   end
  92.   for i = 1, mdlDat.size.y - 1 do
  93.     myturtle.surelyDown()
  94.   end
  95. end
  96. Builder.copyPlane = function(self, tgtLv, map)
  97.   local plane = { }
  98.   for i = 1, #map do
  99.     plane[i] = map[i][tgtLv]
  100.   end
  101.   return plane
  102. end
  103. Builder.addLocal = function(self, myTMeshOrg, myTLocal, dir)
  104.   --debug("[addLocal]!!")
  105.   --debug("  <<before>>")
  106.   --debug("    myTMesh  : x, z, y = " .. tostring(myTMesh.turtleX) .. ", "
  107.   --  .. tostring(myTMesh.turtleZ) .. ", " .. tostring(myTMesh.turtleY))
  108.   --debug("    myTMesh.dir = " .. tostring(myTMesh.turtleDir))
  109.   --debug("    myTLocal : x, z, y = " .. tostring(myTLocal.turtleX) .. ", "
  110.   --  .. tostring(myTLocal.turtleZ) .. ", " .. tostring(myTLocal.turtleY))
  111.   --debug("    myTLocal.dir = " .. tostring(myTLocal.turtleDir))
  112.   local myTMesh = myturtle.MyTurtle.copy(myTMeshOrg)
  113.   if "forward" == dir then
  114.     --debug("    <<A>>module dir is [forward]")
  115.     myTMesh.turtleX = myTMesh.turtleX + myTLocal.turtleX
  116.     myTMesh.turtleZ = myTMesh.turtleZ + myTLocal.turtleZ
  117.     myTMesh.turtleDir = myTLocal.turtleDir
  118.   elseif "left" == dir then
  119.     --debug("    <<B>>module dir is [left]")
  120.     myTMesh.turtleX = myTMesh.turtleX - myTLocal.turtleZ
  121.     myTMesh.turtleZ = myTMesh.turtleZ + myTLocal.turtleX
  122.     myTMesh.turtleDir = myturtle.TURN_OPERATION[myTLocal.turtleDir][myturtle.TURN_LEFT]
  123.   elseif "right" == dir then
  124.     --debug("    <<C>>module dir is [right]")
  125.     myTMesh.turtleX = myTMesh.turtleX + myTLocal.turtleZ
  126.     myTMesh.turtleZ = myTMesh.turtleZ - myTLocal.turtleX
  127.     myTMesh.turtleDir = myturtle.TURN_OPERATION[myTLocal.turtleDir][myturtle.TURN_RIGHT]
  128.   elseif "back" == dir then
  129.     --debug("    <<D>>module dir is [back]")
  130.     myTMesh.turtleX = myTMesh.turtleX - myTLocal.turtleX
  131.     myTMesh.turtleZ = myTMesh.turtleZ - myTLocal.turtleZ
  132.     myTMesh.turtleDir = myturtle.TURN_OPERATION[myTLocal.turtleDir][myturtle.TURN_OPPOSIT]
  133.   end
  134.   myTMesh.turtleY = myTMesh.turtleY + myTLocal.turtleY
  135.   --debug("  <<after>>")
  136.   --debug("    myTMesh  : x, z, y = " .. tostring(myTMesh.turtleX) .. ", "
  137.   --  .. tostring(myTMesh.turtleZ) .. ", " .. tostring(myTMesh.turtleY))
  138.   --debug("    myTMesh.dir = " .. tostring(myTMesh.turtleDir))
  139.   return myTMesh
  140. end
  141. Builder.buildModule = function(self, mdlDat, slotMap, myTMesh, dir)
  142.   local function wallFirst(self, myT, tgtLv, mdlDat, slotMap, map, block, i, j, myTMesh, dir)
  143.     --debug("<<wallFirst>>")
  144.     --debug("  block = " .. block)
  145.     if nil == tcmodules.RES_ATTR[block] then
  146.       --debug("  return at A")
  147.       return
  148.     end
  149.     if nil == tcmodules.RES_ATTR[block].wallFirst then
  150.       --debug("  return at B")
  151.       return
  152.     end
  153.     if false == tcmodules.RES_ATTR[block].wallFirst then
  154.       --debug("  return at C")
  155.       return
  156.     end
  157.     local shiftX = {  0, 1, 0, -1 }
  158.     local shiftZ = { -1, 0, 1,  0 }
  159.     for k = 1, 4 do
  160.       --debug("  k = " .. tostring(k))
  161.       if    1 <= i + shiftZ[k] and i + shiftZ[k] <= #map
  162.         and 1 <= j + shiftX[k] and j + shiftX[k] <= string.len(map[1]) then
  163.         local ii = i + shiftZ[k]
  164.         local jj = j + shiftX[k]
  165.         local block = mdlDat.resDef[string.sub(map[ii], jj, jj)]
  166.         if nil ~= block then
  167.           local mapZ = #map - ii + 1
  168.           myT:moveTo(jj-1, mapZ-1)
  169.           if 1 == turtle.getItemCount(slotMap[block]) then
  170.             local result = myturtle.vacuum(slotMap[block])
  171.             if false == result then
  172.               local tmpMyT = self:addLocal(myTMesh, myT, dir)
  173.               self:lackOfBuildingMaterial(tmpMyT)  
  174.             end
  175.           end
  176.           turtle.select(slotMap[block])
  177.           turtle.placeDown()
  178.           --debug("  return at D")
  179.           return
  180.         end
  181.       end
  182.     end
  183.     --debug("  return at E")
  184.   end
  185.   local function buildPlane(self, myT, tgtLv, mdlDat, slotMap, myTMesh, dir)
  186.     local map = self:copyPlane(tgtLv, mdlDat.map)
  187.     --debug("======================")
  188.     --debug("buildPlane")
  189.     --debug("  tgtLv = " .. tostring(tgtLv))
  190.     for i = 1, #map do
  191.       --debug("  " .. map[i])
  192.     end
  193.     if tgtLv < mdlDat.size.y then
  194.       --debug("  <<not>> LASTLEVEL")
  195.     else
  196.       --debug("  <<LASTLEVEL>>")
  197.     end
  198.     --debug("    #map = " .. tostring(#map))
  199.     for i = #map, 1, -1 do
  200.       --debug("    #map[" .. i .. "] = " .. tostring(#map[i]))
  201.       local mapZ = #map - i + 1
  202.       for j = 1, #map[i] do
  203.         local dat = string.sub(map[i], j, j)
  204.         local block = mdlDat.resDef[dat]
  205.         if nil ~= block then
  206.           --debug("    [" .. block .. "] | x,z= " .. tostring(j) .. ", " .. tostring(mapZ))
  207.           --myT:moveTo(j-1, mapZ-1)
  208.           wallFirst(self, myT, tgtLv, mdlDat, slotMap, map, block, i, j, myTMesh, dir)
  209.           myT:moveTo(j-1, mapZ-1)
  210.           if 1 == turtle.getItemCount(slotMap[block]) then
  211.             local result = myturtle.vacuum(slotMap[block])
  212.             if false == result then
  213.               local tmpMyT = self:addLocal(myTMesh, myT, dir)
  214.               self:lackOfBuildingMaterial(tmpMyT)  
  215.             end
  216.           end
  217.           turtle.select(slotMap[block])
  218.           turtle.placeDown()
  219.         end
  220.       end
  221.     end
  222.     --debug("buildPlane end")
  223.   end
  224.   local myT = myturtle.MyTurtle.new(0)
  225.   for i = 1, mdlDat.size.y do
  226.     --if i < #mdlDat.map[1] then
  227.       myturtle.surelyDigUp()
  228.       myturtle.surelyUp()
  229.       myT.turtleY = myT.turtleY + 1
  230.     --end
  231.     buildPlane(self, myT, i, mdlDat, slotMap, myTMesh, dir)
  232.   end
  233.   return myT
  234. end
  235. Builder.build2D = function(self, bldData, slotMap)
  236.   local meshX, meshZ, meshY = 0, 0, 0
  237.   local mark2Name = { ["^"] = "forward",  ["<"] = "left",  [">"] = "right",  ["v"] = "back" }
  238.   local myTMesh = myturtle.MyTurtle.new(0)
  239.   for i = #bldData.map, 1, -1 do
  240.     --print("bldData.meshSize.z = " .. tostring(bldData.meshSize.z))
  241.     meshZ = (#bldData.map - i) * bldData.meshSize.z + bldData.meshSize.z - 1
  242.  
  243.     local v = bldData.map[i]
  244.     debug(v)
  245.     for j = 1, string.len(v) - 1, 2 do
  246.       meshX = (j - 1) / 2 * bldData.meshSize.x
  247.  
  248.       local moduleMnemonic = string.sub(v, j, j)
  249.       local directionMark  = string.sub(v, j+1, j+1)
  250.       --debug(moduleMnemonic .. directionMark)
  251.       local mod = bldData.modDef[moduleMnemonic]
  252.       if nil == mod then
  253.         --debug(" mod '" .. moduleMnemonic .. "' is nil !")
  254.       else
  255.         --debug(" mod.id = " .. mod.id)
  256.         local dir = mark2Name[directionMark]
  257.         self:moveToInitialPosition(myTMesh, bldData.meshSize, mod, dir, meshX, meshZ)
  258.         myTMesh:adjustY(0)
  259.         --turtle.select(1)
  260.         --turtle.placeUp()
  261.         self:clearBuildArea(mod) -- clear ha ue-kara shinaito suna-toka ga sitani tamaru-baai ari.
  262.         local myTLocal = self.buildModule(mod, slotMap, myTMesh, dir)
  263.         myTMesh = self.addLocal(myTMesh, myTLocal, dir)
  264.       end
  265.     end
  266.   end
  267.   myTMesh:moveTo(0, -1)
  268.   myTMesh:adjustY(0)
  269.   myTMesh:turnTo(myturtle.DIR_FORWARD)
  270. end
  271. Builder.build3D = function(self, bldData, slotMap)
  272.   --debug("==============================")
  273.   --debug("build3D begin")
  274.   --debug("==============================")
  275.   local meshX, meshZ, meshY = 0, 0, 0
  276.   local mark2Name = { ["^"] = "forward",  ["<"] = "left",  [">"] = "right",  ["v"] = "back" }
  277.   local myTMesh = myturtle.MyTurtle.new(0)
  278.  
  279.   for h = 1, bldData.size.y do
  280.     local map = self:copyPlane(h, bldData.map)
  281.     meshY = (h - 1) * bldData.meshSize.y
  282.  
  283.     for i = #map, 1, -1 do
  284.       --debug("bldData.meshSize.z = " .. tostring(bldData.meshSize.z))
  285.       meshZ = (#map - i) * bldData.meshSize.z + bldData.meshSize.z - 1
  286.  
  287.       local v = map[i]
  288.       --debug(v)
  289.       for j = 1, string.len(v) - 1, 2 do
  290.         meshX = (j - 1) / 2 * bldData.meshSize.x
  291.  
  292.         local moduleMnemonic = string.sub(v, j, j)
  293.         local directionMark  = string.sub(v, j+1, j+1)
  294.         --debug(moduleMnemonic .. directionMark)
  295.         local mod = bldData.modDef[moduleMnemonic]
  296.         if nil == mod then
  297.           --debug(" mod '" .. moduleMnemonic .. "' is nil !")
  298.         else
  299.           --debug(" mod.id = " .. mod.id)
  300.           local dir = mark2Name[directionMark]
  301.           self:moveToInitialPosition(myTMesh, bldData.meshSize, mod, dir, meshX, meshZ)
  302.           myTMesh:adjustY(meshY)
  303.           ----turtle.select(1)
  304.           ----turtle.placeUp()
  305.           --clearBuildArea(mod) -- clear ha ue-kara shinaito suna-toka ga sitani tamaru-baai ari.
  306.           local myTLocal = self:buildModule(mod, slotMap, myTMesh, dir)
  307.           myTMesh = self:addLocal(myTMesh, myTLocal, dir)
  308.         end
  309.       end
  310.     end
  311.   end
  312.   myTMesh:moveTo(0, -1)
  313.   myTMesh:adjustY(0)
  314.   myTMesh:turnTo(myturtle.DIR_FORWARD)
  315. end
  316. Builder.build = function(self, buildData, slotMap)
  317.   if 2 == buildData.dimension then
  318.     self:build2D(buildData, slotMap)
  319.   elseif 3 == buildData.dimension then
  320.     self:build3D(buildData, slotMap)
  321.   end
  322. end
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top