Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- -------------------------------
- -- builder(API)
- -- -------------------------------
- Builder = { }
- Builder.new = function()
- local obj = {}
- obj.event = {
- lackOfBuildingMaterialFunction = nil,
- checkFuelLevelFunction = nil
- }
- return setmetatable(obj, {__index = Builder})
- end
- -- -------------------------------
- -- event
- -- -------------------------------
- Builder.lackOfBuildingMaterial = function(self, myT)
- ut.debug("lackOfBuildingMaterial!!!")
- if nil ~= self.event.lackOfBuildingMaterialFunction then
- return self.event.lackOfBuildingMaterialFunction(myT)
- end
- return true
- end
- Builder.checkFuelLevel = function(self, myT)
- ut.debug("checkFuelLevel!!!")
- if nil ~= self.event.checkFuelLevelFunction then
- return self.event.checkFuelLevelFunction(myT)
- end
- return true
- end
- -- -------------------------------
- -- application
- -- -------------------------------
- Builder.moveToInitialPosition = function(self, myTMesh, meshSize, mod, dir, meshX, meshZ)
- --debug("meshX, meshZ = " .. tostring(meshX) .. ", " .. tostring(meshZ))
- local tgtX, tgtZ
- if "forward" == dir then
- tgtZ = meshZ - mod.size.z + 1
- tgtX = meshX
- myTMesh:moveTo(tgtX, tgtZ)
- myTMesh:turnTo(myturtle.DIR_FORWARD)
- elseif "left" == dir then
- tgtZ = meshZ - mod.size.x + 1
- tgtX = meshX + mod.size.z - 1
- myTMesh:moveTo(tgtX, tgtZ)
- myTMesh:turnTo(myturtle.DIR_LEFT)
- elseif "right" == dir then
- tgtZ = meshZ
- tgtX = meshX
- myTMesh:moveTo(tgtX, tgtZ)
- myTMesh:turnTo(myturtle.DIR_RIGHT)
- elseif "back" == dir then
- tgtZ = meshZ
- tgtX = meshX + mod.size.x - 1
- myTMesh:moveTo(tgtX, tgtZ)
- myTMesh:turnTo(myturtle.DIR_BACK)
- end
- --debug("tgtX, tgtZ = " .. tostring(tgtX) .. ", " .. tostring(tgtZ))
- end
- Builder.clearBuildArea = function(self, mdlDat)
- local function clearLine(mdlDat)
- for k = 1, mdlDat.size.z - 1 do
- myturtle.surelyFwd()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- for k = 1, mdlDat.size.z - 1 do
- myturtle.surelyFwd()
- end
- end
- local function clearPlane(mdlDat)
- for j = 1, mdlDat.size.x do
- Builder.clearLine(mdlDat, isLast2)
- if j < mdlDat.size.x then
- turtle.turnLeft()
- myturtle.surelyFwd()
- turtle.turnLeft()
- end
- end
- turtle.turnRight()
- for j = 1, mdlDat.size.x - 1 do
- myturtle.surelyFwd()
- end
- turtle.turnRight()
- end
- for i = 1, mdlDat.size.y do
- Builder.clearPlane(mdlDat, isLast)
- if i < mdlDat.size.y then
- myturtle.surelyDigUp()
- myturtle.surelyUp()
- end
- end
- for i = 1, mdlDat.size.y - 1 do
- myturtle.surelyDown()
- end
- end
- Builder.copyPlane = function(self, tgtLv, map)
- local plane = { }
- for i = 1, #map do
- plane[i] = map[i][tgtLv]
- end
- return plane
- end
- Builder.addLocal = function(self, myTMeshOrg, myTLocal, dir)
- --debug("[addLocal]!!")
- --debug(" <<before>>")
- --debug(" myTMesh : x, z, y = " .. tostring(myTMesh.turtleX) .. ", "
- -- .. tostring(myTMesh.turtleZ) .. ", " .. tostring(myTMesh.turtleY))
- --debug(" myTMesh.dir = " .. tostring(myTMesh.turtleDir))
- --debug(" myTLocal : x, z, y = " .. tostring(myTLocal.turtleX) .. ", "
- -- .. tostring(myTLocal.turtleZ) .. ", " .. tostring(myTLocal.turtleY))
- --debug(" myTLocal.dir = " .. tostring(myTLocal.turtleDir))
- local myTMesh = myturtle.MyTurtle.copy(myTMeshOrg)
- if "forward" == dir then
- --debug(" <<A>>module dir is [forward]")
- myTMesh.turtleX = myTMesh.turtleX + myTLocal.turtleX
- myTMesh.turtleZ = myTMesh.turtleZ + myTLocal.turtleZ
- myTMesh.turtleDir = myTLocal.turtleDir
- elseif "left" == dir then
- --debug(" <<B>>module dir is [left]")
- myTMesh.turtleX = myTMesh.turtleX - myTLocal.turtleZ
- myTMesh.turtleZ = myTMesh.turtleZ + myTLocal.turtleX
- myTMesh.turtleDir = myturtle.TURN_OPERATION[myTLocal.turtleDir][myturtle.TURN_LEFT]
- elseif "right" == dir then
- --debug(" <<C>>module dir is [right]")
- myTMesh.turtleX = myTMesh.turtleX + myTLocal.turtleZ
- myTMesh.turtleZ = myTMesh.turtleZ - myTLocal.turtleX
- myTMesh.turtleDir = myturtle.TURN_OPERATION[myTLocal.turtleDir][myturtle.TURN_RIGHT]
- elseif "back" == dir then
- --debug(" <<D>>module dir is [back]")
- myTMesh.turtleX = myTMesh.turtleX - myTLocal.turtleX
- myTMesh.turtleZ = myTMesh.turtleZ - myTLocal.turtleZ
- myTMesh.turtleDir = myturtle.TURN_OPERATION[myTLocal.turtleDir][myturtle.TURN_OPPOSIT]
- end
- myTMesh.turtleY = myTMesh.turtleY + myTLocal.turtleY
- --debug(" <<after>>")
- --debug(" myTMesh : x, z, y = " .. tostring(myTMesh.turtleX) .. ", "
- -- .. tostring(myTMesh.turtleZ) .. ", " .. tostring(myTMesh.turtleY))
- --debug(" myTMesh.dir = " .. tostring(myTMesh.turtleDir))
- return myTMesh
- end
- Builder.buildModule = function(self, mdlDat, slotMap, myTMesh, dir)
- local function wallFirst(self, myT, tgtLv, mdlDat, slotMap, map, block, i, j, myTMesh, dir)
- --debug("<<wallFirst>>")
- --debug(" block = " .. block)
- if nil == tcmodules.RES_ATTR[block] then
- --debug(" return at A")
- return
- end
- if nil == tcmodules.RES_ATTR[block].wallFirst then
- --debug(" return at B")
- return
- end
- if false == tcmodules.RES_ATTR[block].wallFirst then
- --debug(" return at C")
- return
- end
- local shiftX = { 0, 1, 0, -1 }
- local shiftZ = { -1, 0, 1, 0 }
- for k = 1, 4 do
- --debug(" k = " .. tostring(k))
- if 1 <= i + shiftZ[k] and i + shiftZ[k] <= #map
- and 1 <= j + shiftX[k] and j + shiftX[k] <= string.len(map[1]) then
- local ii = i + shiftZ[k]
- local jj = j + shiftX[k]
- local block = mdlDat.resDef[string.sub(map[ii], jj, jj)]
- if nil ~= block then
- local mapZ = #map - ii + 1
- myT:moveTo(jj-1, mapZ-1)
- if 1 == turtle.getItemCount(slotMap[block]) then
- local result = myturtle.vacuum(slotMap[block])
- if false == result then
- local tmpMyT = self:addLocal(myTMesh, myT, dir)
- self:lackOfBuildingMaterial(tmpMyT)
- end
- end
- turtle.select(slotMap[block])
- turtle.placeDown()
- --debug(" return at D")
- return
- end
- end
- end
- --debug(" return at E")
- end
- local function buildPlane(self, myT, tgtLv, mdlDat, slotMap, myTMesh, dir)
- local map = self:copyPlane(tgtLv, mdlDat.map)
- --debug("======================")
- --debug("buildPlane")
- --debug(" tgtLv = " .. tostring(tgtLv))
- for i = 1, #map do
- --debug(" " .. map[i])
- end
- if tgtLv < mdlDat.size.y then
- --debug(" <<not>> LASTLEVEL")
- else
- --debug(" <<LASTLEVEL>>")
- end
- --debug(" #map = " .. tostring(#map))
- for i = #map, 1, -1 do
- --debug(" #map[" .. i .. "] = " .. tostring(#map[i]))
- local mapZ = #map - i + 1
- for j = 1, #map[i] do
- local dat = string.sub(map[i], j, j)
- local block = mdlDat.resDef[dat]
- if nil ~= block then
- --debug(" [" .. block .. "] | x,z= " .. tostring(j) .. ", " .. tostring(mapZ))
- --myT:moveTo(j-1, mapZ-1)
- wallFirst(self, myT, tgtLv, mdlDat, slotMap, map, block, i, j, myTMesh, dir)
- myT:moveTo(j-1, mapZ-1)
- if 1 == turtle.getItemCount(slotMap[block]) then
- local result = myturtle.vacuum(slotMap[block])
- if false == result then
- local tmpMyT = self:addLocal(myTMesh, myT, dir)
- self:lackOfBuildingMaterial(tmpMyT)
- end
- end
- turtle.select(slotMap[block])
- turtle.placeDown()
- end
- end
- end
- --debug("buildPlane end")
- end
- local myT = myturtle.MyTurtle.new(0)
- for i = 1, mdlDat.size.y do
- --if i < #mdlDat.map[1] then
- myturtle.surelyDigUp()
- myturtle.surelyUp()
- myT.turtleY = myT.turtleY + 1
- --end
- buildPlane(self, myT, i, mdlDat, slotMap, myTMesh, dir)
- end
- return myT
- end
- Builder.build2D = function(self, bldData, slotMap)
- local meshX, meshZ, meshY = 0, 0, 0
- local mark2Name = { ["^"] = "forward", ["<"] = "left", [">"] = "right", ["v"] = "back" }
- local myTMesh = myturtle.MyTurtle.new(0)
- for i = #bldData.map, 1, -1 do
- --print("bldData.meshSize.z = " .. tostring(bldData.meshSize.z))
- meshZ = (#bldData.map - i) * bldData.meshSize.z + bldData.meshSize.z - 1
- local v = bldData.map[i]
- debug(v)
- for j = 1, string.len(v) - 1, 2 do
- meshX = (j - 1) / 2 * bldData.meshSize.x
- local moduleMnemonic = string.sub(v, j, j)
- local directionMark = string.sub(v, j+1, j+1)
- --debug(moduleMnemonic .. directionMark)
- local mod = bldData.modDef[moduleMnemonic]
- if nil == mod then
- --debug(" mod '" .. moduleMnemonic .. "' is nil !")
- else
- --debug(" mod.id = " .. mod.id)
- local dir = mark2Name[directionMark]
- self:moveToInitialPosition(myTMesh, bldData.meshSize, mod, dir, meshX, meshZ)
- myTMesh:adjustY(0)
- --turtle.select(1)
- --turtle.placeUp()
- self:clearBuildArea(mod) -- clear ha ue-kara shinaito suna-toka ga sitani tamaru-baai ari.
- local myTLocal = self.buildModule(mod, slotMap, myTMesh, dir)
- myTMesh = self.addLocal(myTMesh, myTLocal, dir)
- end
- end
- end
- myTMesh:moveTo(0, -1)
- myTMesh:adjustY(0)
- myTMesh:turnTo(myturtle.DIR_FORWARD)
- end
- Builder.build3D = function(self, bldData, slotMap)
- --debug("==============================")
- --debug("build3D begin")
- --debug("==============================")
- local meshX, meshZ, meshY = 0, 0, 0
- local mark2Name = { ["^"] = "forward", ["<"] = "left", [">"] = "right", ["v"] = "back" }
- local myTMesh = myturtle.MyTurtle.new(0)
- for h = 1, bldData.size.y do
- local map = self:copyPlane(h, bldData.map)
- meshY = (h - 1) * bldData.meshSize.y
- for i = #map, 1, -1 do
- --debug("bldData.meshSize.z = " .. tostring(bldData.meshSize.z))
- meshZ = (#map - i) * bldData.meshSize.z + bldData.meshSize.z - 1
- local v = map[i]
- --debug(v)
- for j = 1, string.len(v) - 1, 2 do
- meshX = (j - 1) / 2 * bldData.meshSize.x
- local moduleMnemonic = string.sub(v, j, j)
- local directionMark = string.sub(v, j+1, j+1)
- --debug(moduleMnemonic .. directionMark)
- local mod = bldData.modDef[moduleMnemonic]
- if nil == mod then
- --debug(" mod '" .. moduleMnemonic .. "' is nil !")
- else
- --debug(" mod.id = " .. mod.id)
- local dir = mark2Name[directionMark]
- self:moveToInitialPosition(myTMesh, bldData.meshSize, mod, dir, meshX, meshZ)
- myTMesh:adjustY(meshY)
- ----turtle.select(1)
- ----turtle.placeUp()
- --clearBuildArea(mod) -- clear ha ue-kara shinaito suna-toka ga sitani tamaru-baai ari.
- local myTLocal = self:buildModule(mod, slotMap, myTMesh, dir)
- myTMesh = self:addLocal(myTMesh, myTLocal, dir)
- end
- end
- end
- end
- myTMesh:moveTo(0, -1)
- myTMesh:adjustY(0)
- myTMesh:turnTo(myturtle.DIR_FORWARD)
- end
- Builder.build = function(self, buildData, slotMap)
- if 2 == buildData.dimension then
- self:build2D(buildData, slotMap)
- elseif 3 == buildData.dimension then
- self:build3D(buildData, slotMap)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement