SHOW:
|
|
- or go back to the newest paste.
| 1 | - | --pastebin get nLNQiU0W theinsektAPIs/treefarm1 |
| 1 | + | --This pastebin is to test new stuff while the old code is still working |
| 2 | - | -- see ziBBd33L for startup program and installation instructions |
| 2 | + | --Not working! |
| 3 | - | --not working, experimental, not tested |
| 3 | + | |
| 4 | - | --will plant in middle of room. |
| 4 | + | --pastebin get YVAQ1ZZ9 theinsektAPIs/treefarm2 |
| 5 | - | --the outputChest should be in the floor, |
| 5 | + | |
| 6 | - | --adjacent to a wall, and intersected by a straight line from the middle of the room. |
| 6 | + | |
| 7 | - | --Should be able to handle being restarted at any point in the program |
| 7 | + | --position zero is in one of the top corners, under a crafting table |
| 8 | - | --except maybe the first time if interrupted while writing to file. |
| 8 | + | |
| 9 | ||
| 10 | - | --position zero is above output chest facing inwards |
| 10 | + | |
| 11 | os.loadAPI("theinsektAPIs/poz3")
| |
| 12 | - | --todo: roof, walls and corners at bottom must be walltype |
| 12 | + | |
| 13 | - | -- |
| 13 | + | |
| 14 | function treeFarm(wallBlocks,limit) | |
| 15 | --emptyInventory("d", {["minecraft:sapling"]=2,})--debug
| |
| 16 | --do return end | |
| 17 | ||
| 18 | --if this is the first time, or if there was an error before | |
| 19 | --print usage instructions | |
| 20 | local startStuffPath="configs/startStuff" | |
| 21 | local startString=fileHelp.loadString(startStuffPath) | |
| 22 | if startString~="skip" then | |
| 23 | if startString==nil then startString="first" end | |
| 24 | if startString=="first" then | |
| 25 | print("Place this turtle in an enclosed room, with the width in both directions being odd (5 ,7 etc.).")
| |
| 26 | print("Put one or more saplings in inventory slot 1, in the turtle.")
| |
| 27 | print("Put an output chest in the middle along one of the walls, in the floor.")
| |
| 28 | print("Put a fuel chest 2 steps to the left (if looking towards middle from output chest).")
| |
| 29 | print("Put a dirt block in middle of the floor.")
| |
| 30 | else | |
| 31 | print("Previously quit with error:")
| |
| 32 | print(startString) | |
| 33 | end | |
| 34 | print("Press: c continue, e exit,i instructions, r reset configs, u update.")
| |
| 35 | ||
| 36 | --wait for key press | |
| 37 | local _, key =os.pullEvent("key")
| |
| 38 | --clear terminal | |
| 39 | --term.clear() | |
| 40 | --term.setCursorPos(1,1) | |
| 41 | ||
| 42 | if key==keys.c then | |
| 43 | print("continuing...")
| |
| 44 | fileHelp.saveString(startStuffPath,"skip") | |
| 45 | elseif key==keys.e then | |
| 46 | print("Exiting...")
| |
| 47 | return | |
| 48 | elseif key==keys.i then | |
| 49 | fileHelp.saveString(startStuffPath,"first") | |
| 50 | os.reboot() | |
| 51 | elseif key==keys.r then | |
| 52 | clearConfigs() | |
| 53 | fileHelp.saveString(startStuffPath,"first") | |
| 54 | os.reboot() | |
| 55 | else-- | |
| 56 | print("Choice not yet implemented, terminating")
| |
| 57 | return | |
| 58 | end | |
| 59 | end--if | |
| 60 | treeFarmStates(wallBlocks,limit) | |
| 61 | end--function | |
| 62 | ||
| 63 | --wallblocks is a table like this {"minecraft:cobblestone"=true, "minecraft:glass"=true, "minecraft"}
| |
| 64 | function treeFarmStates(wallBlocks,limit) | |
| 65 | --state: start | |
| 66 | print("debug, state: start")
| |
| 67 | --will save stuff like plantName and dimensions, | |
| 68 | --so that it can be loaded after next restart | |
| 69 | --local tryLoad=true | |
| 70 | --local trySave=true | |
| 71 | ||
| 72 | --state: getting plantName | |
| 73 | print("debug, state: getting plantName")
| |
| 74 | --get type of plant (from file or slot 1) | |
| 75 | local plantName = getItemType(1,"configs/plantName") | |
| 76 | if plantName==nil then | |
| 77 | quitWithError("Error: No plant in slot 1")
| |
| 78 | end | |
| 79 | ||
| 80 | ----(skipped)state: getting fuelName | |
| 81 | --get type of fuel (from file or slot 2) | |
| 82 | --local fuelName = getItemType(2,"configs/fuelName") | |
| 83 | ||
| 84 | --state: getting room dimensions, get dimensions of room (from file or program) | |
| 85 | print("debug, state: getting room dimensions")
| |
| 86 | local dimensions=getDimensions(wallBlocks, limit,"configs/dimensions") | |
| 87 | local halfWidth=math.floor(dimensions["width"]/2) | |
| 88 | ||
| 89 | --state: defining coordinate points, now it's possible to define some coordinates | |
| 90 | print("debug, state: defining coordinate points")
| |
| 91 | --some coords | |
| 92 | --positive x=east | |
| 93 | --positive y=up | |
| 94 | --positive z=south | |
| 95 | --north is if standing on output chest and looking towards plant in the middle | |
| 96 | local outputChestPos={x=0,y=0,z=0,f="n"}--above output chest, looking towards plant in middle of room
| |
| 97 | local southWestCorner={x=-halfWidth,y=0,z=0,f="n"}--used as a starting point when harvesting
| |
| 98 | local waitPos={x=0,y=0,z=-(halfWidth-1),f="n"}--go north from outputChestPos until before plant
| |
| 99 | local fuelPos={x=-2,y=0,z=0,f="n"}--two steps to the left of output chest
| |
| 100 | ||
| 101 | --state: defining keep table | |
| 102 | print("debug, state: defining keep table")
| |
| 103 | --what items to keep when emptying the inventory, and how much | |
| 104 | --local keepTable={[plantName]=32,[fuelName]=32}
| |
| 105 | local keepTable={[plantName]=32,}
| |
| 106 | ||
| 107 | ||
| 108 | ||
| 109 | --state: finding pos | |
| 110 | print("debug, state: finding pos")
| |
| 111 | findAndSetPos(dimensions, plantName, wallBlocks,waitPos,outputChestPos) | |
| 112 | ||
| 113 | ||
| 114 | while(true) do | |
| 115 | --state: refuelling | |
| 116 | print("debug, state: refueling")
| |
| 117 | --refuel if has to little fuel | |
| 118 | gotoAndRefuelFromChestIf(fuelPos, "d",1000,2000) | |
| 119 | --state: waiting | |
| 120 | print("debug, state: waiting")
| |
| 121 | --wait until not plant | |
| 122 | while(true) do | |
| 123 | --get what is infront | |
| 124 | local success, data=poz3.inspect("n")
| |
| 125 | if not success then | |
| 126 | --plant has somehow disappeared, so goto next state | |
| 127 | break | |
| 128 | end | |
| 129 | if success and data.name~=plantName then | |
| 130 | --it's a tree, or the wrong plant, so goto next state | |
| 131 | break | |
| 132 | end | |
| 133 | --the sappling is still there so wait for a while | |
| 134 | os.sleep(10) | |
| 135 | end | |
| 136 | --state: destroying plant position | |
| 137 | print("debug, state: destroying plant position")
| |
| 138 | --dig if it's not a wall | |
| 139 | if not isBlockMatch("n", wallBlocks) then
| |
| 140 | poz3.dig("n")
| |
| 141 | end | |
| 142 | --state: harvesting/clearing (destroy not plant and not wall, then dcube2 inside room) | |
| 143 | print("debug, state: harvesting/clearing")
| |
| 144 | harvest(dimensions, southWestCorner) | |
| 145 | --state: emptying inventory | |
| 146 | print("debug, state: emptying inventory")
| |
| 147 | if not poz3a.digToCoords2(outputChestPos) then | |
| 148 | quitWithError("Error: failed when trying to go to output chest.")
| |
| 149 | end | |
| 150 | emptyInventory("d", keepTable)
| |
| 151 | --state: planting | |
| 152 | print("debug, state: planting")
| |
| 153 | --goto plant/wait position | |
| 154 | if not poz3a.digToCoords2(waitPos) then | |
| 155 | quitWithError("Error: failed when trying to go to wait position (infront of plant).")
| |
| 156 | end | |
| 157 | --place/plant sapling | |
| 158 | if not placeItem("n",plantName) then
| |
| 159 | quitWithError("Error: failed when trying to place sapling.")
| |
| 160 | end | |
| 161 | end | |
| 162 | end--main | |
| 163 | ||
| 164 | function gotoAndRefuelFromChestIf(fuelPos, direction,lower,upper) | |
| 165 | --check if fuel level is below lower limit | |
| 166 | local fuelLevel=turtle.getFuelLevel() | |
| 167 | --quit if doesn’t need to refuel (fuel level isn't below lower) | |
| 168 | local fuelLevel=turtle.getFuelLevel() | |
| 169 | if fuelLevel == "unlimited" or fuelLevel >= lower then | |
| 170 | return | |
| 171 | end | |
| 172 | --goto refuelling chest | |
| 173 | if not poz3a.digToCoords2(fuelPos) then | |
| 174 | quitWithError("Error: failed when trying to go to refuelling position.")
| |
| 175 | end | |
| 176 | --refuel from chest | |
| 177 | if not refuelFromChest(direction, upper) then | |
| 178 | quitWithError("Error: failed when refuelling from chest.")
| |
| 179 | end | |
| 180 | end | |
| 181 | ||
| 182 | --suck fuel from a chest in the given direction | |
| 183 | --and refuel until fuel level reaches upper (upper bound) | |
| 184 | function refuelFromChest(direction,upper) | |
| 185 | --return true if has unlimited fuel | |
| 186 | if turtle.getFuelLevel() == "unlimited" then return true end | |
| 187 | --check upper | |
| 188 | if upper>turtle.getFuelLimit() then upper=turtle.getFuelLimit() end | |
| 189 | --select the slot to place the fuel in | |
| 190 | local slot=findEmptyInventorySlot() | |
| 191 | if slot==-1 then | |
| 192 | return false | |
| 193 | end | |
| 194 | turtle.select(slot) | |
| 195 | --refuel from chest | |
| 196 | while(turtle.getFuelLevel()<upper) do | |
| 197 | if not poz3.suck(direction,1) then | |
| 198 | --could not suck fuel from fuel chest | |
| 199 | return false | |
| 200 | end | |
| 201 | if not turtle.refuel() then | |
| 202 | --could not refuel from the selected slot | |
| 203 | --maybe it's not a fuel | |
| 204 | return false | |
| 205 | end | |
| 206 | end | |
| 207 | --refuelled! | |
| 208 | return true | |
| 209 | end | |
| 210 | ||
| 211 | function findEmptyInventorySlot() | |
| 212 | for slot=1,16 do | |
| 213 | if turtle.getItemCount(slot)==0 then | |
| 214 | return slot | |
| 215 | end | |
| 216 | end | |
| 217 | return -1 | |
| 218 | end | |
| 219 | ||
| 220 | ||
| 221 | function placeItem(direction, itemName) | |
| 222 | local slot=getItemSlot(itemName) | |
| 223 | if slot==-1 then return false end | |
| 224 | turtle.select(slot) | |
| 225 | return poz3.place(direction) | |
| 226 | end | |
| 227 | ||
| 228 | function getItemSlot(itemName) | |
| 229 | for slot=1, 16 do | |
| 230 | local data=turtle.getItemDetail(slot) | |
| 231 | if data and data.name==itemName then | |
| 232 | return slot | |
| 233 | end | |
| 234 | end | |
| 235 | return -1 | |
| 236 | end | |
| 237 | ||
| 238 | ||
| 239 | --not tested | |
| 240 | --example keepTable: {"minecraft:sappling"=32,"minecraft:charcoal"=32}
| |
| 241 | function emptyInventory(direction, keepTable) | |
| 242 | local amounts={}
| |
| 243 | for slot=1, 16 do | |
| 244 | local data=turtle.getItemDetail(slot) | |
| 245 | if data then | |
| 246 | if keepTable[data.name] then | |
| 247 | if not amounts[data.name] then amounts[data.name]=0 end | |
| 248 | local limit=keepTable[data.name] | |
| 249 | amount = turtle.getItemCount(slot) + amounts[data.name] | |
| 250 | if amount>limit then | |
| 251 | local dropAmount=amount-limit | |
| 252 | turtle.select(slot) | |
| 253 | poz3.drop(direction,dropAmount) | |
| 254 | amount=limit | |
| 255 | end --if amount>limit | |
| 256 | amounts[data.name]=amount | |
| 257 | else --if keep | |
| 258 | --drop whole stack | |
| 259 | turtle.select(slot) | |
| 260 | poz3.drop(direction) | |
| 261 | end | |
| 262 | end--if data | |
| 263 | end --for | |
| 264 | return true | |
| 265 | end | |
| 266 | ||
| 267 | function harvest(dimensions, southWestCorner) | |
| 268 | --go to southwest corner | |
| 269 | if not poz3a.digToCoords2(southWestCorner) then | |
| 270 | quitWithError("Error: failed when trying to go to southWestCorner.")
| |
| 271 | end | |
| 272 | --harvest, digcube ,refuel=false, return to origin=true | |
| 273 | if not poz3a.callDigCube("u",dimensions["height"],"e",dimensions["width"],"n",dimensions["width"],false,true) then
| |
| 274 | quitWithError("Error: failed when digging everything inside the room.")
| |
| 275 | end | |
| 276 | --returned to south west corner | |
| 277 | end | |
| 278 | ||
| 279 | ||
| 280 | ||
| 281 | --does not need a known postion | |
| 282 | function getDimensions(wallBlocks,limit, path) | |
| 283 | local dimensions=nil | |
| 284 | ||
| 285 | if path then | |
| 286 | dimensions = fileHelp.loadValue(path) | |
| 287 | if dimensions~=nil then return dimensions end | |
| 288 | end | |
| 289 | ||
| 290 | --measure dimensions | |
| 291 | --go to a top corner | |
| 292 | local len1,data=digGoUntilWall("u",wallBlocks,limit)
| |
| 293 | if turtle.getFuelLevel() == 0 then quitWithError("Turtle error: turtle is out of fuel.") end
| |
| 294 | if not data then quitWithError("Room error: reached limit without finding a wall block.") end
| |
| 295 | ||
| 296 | local len2,data=digGoUntilWall("w",wallBlocks,limit)
| |
| 297 | if turtle.getFuelLevel() == 0 then quitWithError("Turtle error: turtle is out of fuel.") end
| |
| 298 | if not data then quitWithError("Room error: reached limit without finding a wall block.") end
| |
| 299 | ||
| 300 | ||
| 301 | local len3,data=digGoUntilWall("s",wallBlocks,limit)
| |
| 302 | if turtle.getFuelLevel() == 0 then quitWithError("Turtle error: turtle is out of fuel.") end
| |
| 303 | if not data then quitWithError("Room error: reached limit without finding a wall block.") end
| |
| 304 | ||
| 305 | ||
| 306 | --measure height | |
| 307 | local height,data=digGoUntilWall("d",wallBlocks,limit)
| |
| 308 | height=height+1--because needs to count place where turtle stood | |
| 309 | if turtle.getFuelLevel() == 0 then quitWithError("Turtle error: turtle is out of fuel.") end
| |
| 310 | if not data then quitWithError("Room error: reached limit without finding a wall block.") end
| |
| 311 | ||
| 312 | ||
| 313 | if height<2 then error("Room error: the room must have a height that is 2 or larger.") end
| |
| 314 | ||
| 315 | --measure width1 | |
| 316 | local width1,data=digGoUntilWall("e",wallBlocks,limit)
| |
| 317 | width1=width1+1--because needs to count place where turtle stood | |
| 318 | if turtle.getFuelLevel() == 0 then quitWithError("Turtle error: turtle is out of fuel.") end
| |
| 319 | if not data then quitWithError("Room error: reached limit without finding a wall block.") end
| |
| 320 | ||
| 321 | if width1<5 then | |
| 322 | quitWithError("Room error: the width of the room must be 5 or larger.")
| |
| 323 | end | |
| 324 | ||
| 325 | if (width1 % 2) == 0 then | |
| 326 | if not data then | |
| 327 | quitWithError("Room error: the width of the room must be odd number, examples 5 ,7, 9 etc.")
| |
| 328 | end | |
| 329 | end | |
| 330 | ||
| 331 | ||
| 332 | --measure width2 | |
| 333 | local width2,data=digGoUntilWall("n",wallBlocks,limit)
| |
| 334 | width2=width2+1--because needs to count place where turtle stood | |
| 335 | if turtle.getFuelLevel() == 0 then quitWithError("Turtle error: turtle is out of fuel.") end
| |
| 336 | if not data then quitWithError("Room error: reached limit without finding a wall block.") end
| |
| 337 | ||
| 338 | ||
| 339 | if width1 ~= width2 then quitWithError("Room error: width1 and width2 must be the same in the room.") end
| |
| 340 | ||
| 341 | --save and return dimensions | |
| 342 | dimensions={["width"]=width1, ["height"]=height,}
| |
| 343 | if path then | |
| 344 | fileHelp.saveValue(path,dimensions) | |
| 345 | end | |
| 346 | return dimensions | |
| 347 | end | |
| 348 | ||
| 349 | --general code that can be reused in many situations | |
| 350 | function doLoop(limit,doFunction,direction,...) | |
| 351 | for i=1,limit do | |
| 352 | local success, data=doFunction(direction,...) | |
| 353 | if not success then | |
| 354 | return i-1,data | |
| 355 | end | |
| 356 | end | |
| 357 | return limit,nil | |
| 358 | end | |
| 359 | ||
| 360 | --works for all the directions | |
| 361 | --returns steps taken and the data from a wall block if was stopped by it | |
| 362 | function digGoUntilWall(direction, wallBlocks,limit) | |
| 363 | --get absolute direction so that if the direction is right | |
| 364 | --it will continue forward after turning right once | |
| 365 | direction=poz3.getAbsoluteDirection(direction) | |
| 366 | --do the actual loop and then return the result | |
| 367 | return doLoop(limit,digGoIfNotWall,direction,wallBlocks) | |
| 368 | end | |
| 369 | ||
| 370 | --Works in all directions. | |
| 371 | --Handles blocking players or creatures. | |
| 372 | --Returns false if out of fuel (handles unlimited fuel) | |
| 373 | --Kinda handles gravel blocks (will not be blocked, but may leave some) | |
| 374 | --stops if reaches one of the wallBlocks | |
| 375 | function digGoIfNotWall(direction,wallBlocks) | |
| 376 | --check fuel level | |
| 377 | local fuelLevel=turtle.getFuelLevel() | |
| 378 | local noFuel = fuelLevel ~= "unlimited" and fuelLevel < 1 then | |
| 379 | --an infinite loop so that it won't be blocked by creatures | |
| 380 | while(true) do | |
| 381 | local success,data=poz3.inspect(direction) | |
| 382 | if success then | |
| 383 | --detected a block | |
| 384 | if wallBlocks[data.name] then | |
| 385 | --found wall so send data and stop signal | |
| 386 | return false,data | |
| 387 | end--if | |
| 388 | if not poz3.dig(direction) then | |
| 389 | --found non wall block, but could not dig through it | |
| 390 | --so return stop signal and nil | |
| 391 | return false,nil | |
| 392 | end--if | |
| 393 | end--if success | |
| 394 | if poz3.go(direction) then | |
| 395 | --managed to go so return continue signal | |
| 396 | return true,nil | |
| 397 | elseif nofuel then | |
| 398 | --could not go becuase of fuel so return stop signal and nil | |
| 399 | return false,nil | |
| 400 | end--elseif | |
| 401 | --continue trying if could not go becuase of a blocking creature | |
| 402 | end--while | |
| 403 | end | |
| 404 | ||
| 405 | --will add reference block to wallblocks if it's not already added | |
| 406 | function findPos2(limit,path,referencePos,referenceBlock,wallBlocks) | |
| 407 | --add reference block to wallblocks so that it won't be dug | |
| 408 | wallBlocks[referenceBlock]=true | |
| 409 | ||
| 410 | --go to upper north west corner | |
| 411 | local directions={"u","n","w"}
| |
| 412 | for _,dir in ipdairs(directions) do | |
| 413 | local distance, data = digGoUntilWall(direction, wallBlocks,limit) | |
| 414 | if not data then | |
| 415 | error("Room error: Could not find wall when trying to find position.")
| |
| 416 | end | |
| 417 | end | |
| 418 | ||
| 419 | --search for referenceBlock | |
| 420 | local data,matchDirection=nil,nil | |
| 421 | local directions={"s","e","n"}
| |
| 422 | for i,goDirection in ipdairs(directions) do | |
| 423 | data,matchDirection=blockMatching(referenceBlock, "u") | |
| 424 | if data then break end | |
| 425 | digGoUntilWallErrors(goDirection, wallBlocks,limit,noWallErrorString) | |
| 426 | if i==3 then | |
| 427 | data,matchDirection=blockMatching(referenceBlock, "u") | |
| 428 | if data then break end | |
| 429 | end | |
| 430 | end | |
| 431 | if not data then | |
| 432 | error("Room error: there is no reference block in one of the upper corners.")
| |
| 433 | end | |
| 434 | ||
| 435 | --if here, have found referenceBlock, turn to face north | |
| 436 | local turnDirection="l" | |
| 437 | --turn until wall | |
| 438 | while(not poz3.detect("f") and turn(turnDirection))
| |
| 439 | --turn until not wall | |
| 440 | while(poz3.detect("f") and turn(turnDirection))
| |
| 441 | --turn once more | |
| 442 | turn(turnDirection) | |
| 443 | --if was in corner will have wall on right side, if turns right | |
| 444 | --if turns left will have wall on left side | |
| 445 | --if was along wall will be facing out from wall | |
| 446 | ||
| 447 | --if has no refrence position, then set it to amount of steps from previous corner | |
| 448 | if referencePos==nil then referencePos = {x=distance,y=0,z=0,f="n"} end
| |
| 449 | ||
| 450 | --have found referencePos | |
| 451 | poz3.setPos2(referencePos) | |
| 452 | end | |
| 453 | ||
| 454 | --warning: will not automatically turn back | |
| 455 | --directions are relative to the starting direction, so "r","f" is possible | |
| 456 | --example usage1: | |
| 457 | --matchBlockDirections("minecraft:cobblestone","f","u")
| |
| 458 | --example usage2: | |
| 459 | --matchBlockDirections({"minecraft:cobblestone"=true,"minecraft:glass"=true},"f","r","l")
| |
| 460 | function blockMatching(blocksOrBlock,...) | |
| 461 | --check arguments | |
| 462 | checkArg(1, blocksOrBlock, "string","table") | |
| 463 | --arg is the table that ... is automatically stored in | |
| 464 | local directions=arg | |
| 465 | --loops through the directions and checks if it is one of the wanted blocks | |
| 466 | for i, direction in ipairs(directions) do | |
| 467 | checkArg(i+1, direction, "string") | |
| 468 | --neccesary if for example have "r" and "f" | |
| 469 | direction=poz3.getAbsoluteDirection(direction) | |
| 470 | --check if the direction have the block | |
| 471 | local success,data=poz3.inspect(direction) | |
| 472 | --check if detected a block | |
| 473 | if success then | |
| 474 | --check if it's one block or many blocks | |
| 475 | if type(blocksOrBlock)=="string" then | |
| 476 | --check against one block | |
| 477 | local block=blocksOrBlock | |
| 478 | if block==data.name then | |
| 479 | return data,direction | |
| 480 | end--if block | |
| 481 | else | |
| 482 | --check against many blocks | |
| 483 | local blocks=blocksOrBlock | |
| 484 | if blocks[data.name] then | |
| 485 | return data,direction | |
| 486 | end--if blocks | |
| 487 | end--else | |
| 488 | end--if success | |
| 489 | end--for | |
| 490 | --no matching block was found, so retrun nil,nil | |
| 491 | return nil,nil | |
| 492 | end | |
| 493 | ||
| 494 | function findReferenceBlock(wallBlocks,referenceBlock,limit) | |
| 495 | local distance, data, matchDirection=digGoSearchUntilWallOrMatch("s",wallBlocks, limit, referenceBlock, "u")
| |
| 496 | if matchDirection then return true end | |
| 497 | if distance==limit then return false end | |
| 498 | local distance, data, matchDirection=digGoSearchUntilWallOrMatch("e",wallBlocks, limit, referenceBlock, "u")
| |
| 499 | if matchDirection then return true end | |
| 500 | if distance==limit then return false end | |
| 501 | local distance, data, matchDirection=digGoSearchUntilWallOrMatch("n",wallBlocks, limit, referenceBlock, "u")
| |
| 502 | if matchDirection then return true end | |
| 503 | if distance==limit then return false end | |
| 504 | --local distance, data, matchDirection=digGoSearchUntilWallOrMatch("w",wallBlocks, limit, referenceBlock, "u")
| |
| 505 | if matchDirection then return true end | |
| 506 | --if distance==limit then return false end | |
| 507 | --didn't find reference block along any edge | |
| 508 | return false | |
| 509 | end | |
| 510 | ||
| 511 | --returns true if should be waiting | |
| 512 | --returns false if should be harvesting | |
| 513 | function findAndSetPos(dimensions, plantName, wallBlocks,waitPos,outputChestPos) | |
| 514 | local success,data=poz3.inspect("f")
| |
| 515 | if success then | |
| 516 | if data.name==plantName then | |
| 517 | --known pos, at waitpos | |
| 518 | print("debug, pos: waitPos")
| |
| 519 | poz3.setPos2(waitPos) | |
| 520 | return | |
| 521 | end --if | |
| 522 | --if it's not a wall it might be | |
| 523 | --a sapling of wrong type so destroy it | |
| 524 | if not wallBlocks[data.name] then | |
| 525 | print("removing potential bad plant")
| |
| 526 | poz3.dig("f")
| |
| 527 | end | |
| 528 | end --if | |
| 529 | ||
| 530 | --unknown pos, so find it | |
| 531 | local halfWidth=math.floor(dimensions["width"]/2) | |
| 532 | findOutputChest(dimensions,halfWidth,wallBlocks) | |
| 533 | print("debug, pos: outputChestPos")
| |
| 534 | poz3.setPos2(outputChestPos) | |
| 535 | end --function | |
| 536 | ||
| 537 | --Goes to the outputChestPos without knowing the current position | |
| 538 | function findOutputChest(dimensions,halfWidth,wallBlocks) | |
| 539 | --go to a top corner | |
| 540 | local _,data=digGoUntilWall("u",wallBlocks,dimensions["height"])
| |
| 541 | if data==nil then quitWithError("Room error: a wall is missing, or dimensions have changed") end
| |
| 542 | local _,data=digGoUntilWall("w",wallBlocks,dimensions["width"])
| |
| 543 | if data==nil then quitWithError("Room error: a wall is missing, or dimensions have changed") end
| |
| 544 | local _,data=digGoUntilWall("s",wallBlocks,dimensions["width"])
| |
| 545 | if data==nil then quitWithError("Room error: a wall is missing, or dimensions have changed") end
| |
| 546 | ||
| 547 | ||
| 548 | --go to bottom corner | |
| 549 | local len1,data=digGoUntilWall("d",wallBlocks,dimensions["height"]+1)
| |
| 550 | if data==nil then quitWithError("Room error: a wall is missing, or dimensions have changed") end
| |
| 551 | if len1+1 ~= dimensions["height"] then quitWithError("Room error: a wall is missing, or dimensions have changed") end
| |
| 552 | ||
| 553 | ||
| 554 | --go in circle looking for chest, | |
| 555 | local turnDirectionIfFound="l" | |
| 556 | if checkForMiddleChest("e",halfWidth,wallBlocks,turnDirectionIfFound) then return true end
| |
| 557 | if checkForMiddleChest("n",halfWidth,wallBlocks,turnDirectionIfFound) then return true end
| |
| 558 | if checkForMiddleChest("w",halfWidth,wallBlocks,turnDirectionIfFound) then return true end
| |
| 559 | if checkForMiddleChest("s",halfWidth,wallBlocks,turnDirectionIfFound) then return true end
| |
| 560 | ||
| 561 | quitWithError("Room error: there is no output chest at the four possible positions, or room dimensions have changed")
| |
| 562 | end | |
| 563 | ||
| 564 | --must be going along right wall | |
| 565 | function checkForMiddleChest(direction,halfWidth,wallBlocks,directionIfFound) | |
| 566 | local _,data=digGoUntilWall(direction,wallBlocks,halfWidth) | |
| 567 | if data then quitWithError("Room error: a wall block is inside room, or dimensions have changed") end
| |
| 568 | local success, data=poz3.inspect("d")
| |
| 569 | if success and data and data.name=="minecraft:chest" then | |
| 570 | poz3.turn(directionIfFound) | |
| 571 | return true | |
| 572 | end | |
| 573 | local _,data=digGoUntilWall(direction,wallBlocks,halfWidth+1) | |
| 574 | if not data then quitWithError("Room error: a wall block is missing, or dimensions have changed") end
| |
| 575 | return false | |
| 576 | end | |
| 577 | ||
| 578 | ||
| 579 | ||
| 580 | ||
| 581 | ||
| 582 | ||
| 583 | --returns blocksTraveled, blockData if encountered a wall | |
| 584 | --returns limit, nil if travelled limit blocks | |
| 585 | --returns blokcsTraveled, nil if blocked by something undigable (impossible?) | |
| 586 | function digGoUntilWall(direction, wallBlocks,limit) | |
| 587 | for i=1, limit do | |
| 588 | local went, data = digGoIfNotWall(direction, wallBlocks) | |
| 589 | if not went then return i-1, data end | |
| 590 | end | |
| 591 | return limit,nil | |
| 592 | end | |
| 593 | ||
| 594 | --digs or goes one step in the given direction | |
| 595 | --returns true,nil if went | |
| 596 | --returns false, nil if could not dig through non wall block | |
| 597 | --returns false, blockData if reached wall | |
| 598 | --will continue trying if blocked by player | |
| 599 | function digGoIfNotWall(direction,wallBlocks) | |
| 600 | --no fuel | |
| 601 | if turtle.getFuelLevel() == 0 then return false,nil end | |
| 602 | while(true) do | |
| 603 | --check block in direction | |
| 604 | local success, data=poz3.inspect(direction) | |
| 605 | if success then | |
| 606 | if wallBlocks[data.name] then | |
| 607 | return false, data | |
| 608 | end | |
| 609 | if not poz3.dig(direction) then return false, nil end | |
| 610 | end | |
| 611 | --go in direction | |
| 612 | if poz3.go(direction) then return true, nil end | |
| 613 | end | |
| 614 | end | |
| 615 | ||
| 616 | ||
| 617 | --resulting direction could be changed | |
| 618 | --returns the name of the block, if it's one of the blocks | |
| 619 | --blocks could be a string, or a table with string keys | |
| 620 | function isBlockMatch(direction, blocks) | |
| 621 | local success, data=poz3.inspect(direction) | |
| 622 | if success then | |
| 623 | if type(blocks)=="string" then | |
| 624 | if data.name=blocks | |
| 625 | return data.name | |
| 626 | end--if | |
| 627 | return nil | |
| 628 | end--if type | |
| 629 | if blocks[data.name] | |
| 630 | return data.name | |
| 631 | end--if | |
| 632 | end--if success | |
| 633 | return nil | |
| 634 | end--function | |
| 635 | ||
| 636 | ||
| 637 | --first tries to get itemname from the given file path | |
| 638 | --second tries to find name of the item in the given slotNum | |
| 639 | --if got itemName from slot and doSave is true, then saves the itemName in the given path | |
| 640 | function getItemType(slotNum,path) | |
| 641 | local itemName=nil | |
| 642 | if path then | |
| 643 | itemName=fileHelp.loadString(path) | |
| 644 | end | |
| 645 | if itemName==nil then | |
| 646 | local data=turtle.getItemDetail(slotNum) | |
| 647 | if data then | |
| 648 | itemName=data.name | |
| 649 | if path then | |
| 650 | fileHelp.saveString(path,itemName) | |
| 651 | end | |
| 652 | end | |
| 653 | end | |
| 654 | return itemName | |
| 655 | end | |
| 656 | ||
| 657 | function quitWithError(errorMessage) | |
| 658 | fileHelp.saveString("configs/startStuff",errorMessage)
| |
| 659 | error(errorMessage,2) | |
| 660 | end | |
| 661 | ||
| 662 | function clearConfigs() | |
| 663 | --delete the configs folder | |
| 664 | fs.delete("configs")
| |
| 665 | end |