tPlatform_fixed.lua
256 lines · 7.0 KB
{program="tPlatform",version="1.20",date="2025-08-23"}
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/tPlatform/tPlatform_fixed.lua
| 1 | --{program="tPlatform",version="1.20",date="2025-08-23"} |
| 2 | --------------------------------------- |
| 3 | -- tPlatform by Kaikaku |
| 4 | -- 2016-02-28, v1.10 checks fuel |
| 5 | -- 2015-03-28, v1.03 select slot 1 |
| 6 | -- 2015-03-21, v1.02 code tidied up |
| 7 | -- 2013-11-09, v1.01 more compact |
| 8 | -- 2013-11-02, v1.00 initial |
| 9 | --------------------------------------- |
| 10 | |
| 11 | --------------------------------------- |
| 12 | ---- ASSUMPTIONS/PRECONDITIONS -------- |
| 13 | --------------------------------------- |
| 14 | -- Turtle movement: |
| 15 | -- - building space must be empty |
| 16 | |
| 17 | --------------------------------------- |
| 18 | ---- PARAMETERS ----------------------- |
| 19 | --------------------------------------- |
| 20 | local cSleepTime=10 |
| 21 | |
| 22 | --------------------------------------- |
| 23 | ---- VARIABLES ------------------------ |
| 24 | --------------------------------------- |
| 25 | local userX=3 userY=1 |
| 26 | local blnAskForParameters=true |
| 27 | local blnDirectionX=true |
| 28 | local currentSlot=1 |
| 29 | |
| 30 | --------------------------------------- |
| 31 | ---- tArgs ---------------------------- |
| 32 | --------------------------------------- |
| 33 | local tArgs = {...} |
| 34 | if #tArgs == 2 then -- no error check |
| 35 | blnAskForParameters=false |
| 36 | userX=tonumber(tArgs[1]) |
| 37 | userY=tonumber(tArgs[2]) |
| 38 | end |
| 39 | |
| 40 | --------------------------------------- |
| 41 | -- basic functions for turtle control - |
| 42 | --------------------------------------- |
| 43 | local function mats() |
| 44 | if turtle.getItemCount(currentSlot)==0 then |
| 45 | currentSlot=currentSlot+1 |
| 46 | if currentSlot>16 then |
| 47 | currentSlot=1 |
| 48 | print("Out of materials, please restock!") |
| 49 | print(" Sleeping for "..cSleepTime.." sec ...") |
| 50 | os.sleep(cSleepTime) |
| 51 | end |
| 52 | turtle.select(currentSlot) |
| 53 | mats() |
| 54 | end |
| 55 | end |
| 56 | |
| 57 | local function gf() |
| 58 | while not turtle.forward() do |
| 59 | turtle.attack() |
| 60 | turtle.dig() |
| 61 | os.sleep(0.2) |
| 62 | end |
| 63 | end |
| 64 | local function gb() |
| 65 | while not turtle.back() do |
| 66 | turtle.turnLeft(); turtle.turnLeft() |
| 67 | turtle.attack() |
| 68 | turtle.dig() |
| 69 | turtle.turnLeft(); turtle.turnLeft() |
| 70 | os.sleep(0.2) |
| 71 | end |
| 72 | end |
| 73 | local function gu() |
| 74 | while not turtle.up() do |
| 75 | turtle.attackUp() |
| 76 | turtle.digUp() |
| 77 | os.sleep(0.2) |
| 78 | end |
| 79 | end |
| 80 | local function gd() |
| 81 | while not turtle.down() do |
| 82 | turtle.attackDown() |
| 83 | turtle.digDown() |
| 84 | os.sleep(0.2) |
| 85 | end |
| 86 | end |
| 87 | local function gl() while not turtle.turnLeft() do end end |
| 88 | local function gr() while not turtle.turnRight() do end end |
| 89 | local function df() turtle.dig() end |
| 90 | local function du() turtle.digUp() end |
| 91 | local function dd() turtle.digDown() end |
| 92 | local function pf() mats() while not turtle.place() do end end |
| 93 | local function pu() mats() while not turtle.placeUp() do end end |
| 94 | local function pd() mats() while not turtle.placeDown() do end end |
| 95 | local function sf() turtle.suck() end |
| 96 | local function su() turtle.suckUp() end |
| 97 | local function sd() turtle.suckDown() end |
| 98 | local function Df() turtle.drop() end |
| 99 | local function Du() turtle.dropUp() end |
| 100 | local function Dd() turtle.dropDown() end |
| 101 | local function ss(s) turtle.select(s) end |
| 102 | |
| 103 | local function askForInputText(textt) |
| 104 | local at="" |
| 105 | -- check prompting texts |
| 106 | if textt==nil then textt="Enter text:" end |
| 107 | |
| 108 | -- ask for input |
| 109 | write(textt) |
| 110 | at=read() |
| 111 | return at |
| 112 | end |
| 113 | |
| 114 | |
| 115 | local function checkFuel() |
| 116 | local tmp=turtle.getFuelLevel() |
| 117 | return tmp |
| 118 | end |
| 119 | |
| 120 | -- Attempt to refuel up to a target fuel level using any fuel items in inventory |
| 121 | local function autoRefuel(targetFuel) |
| 122 | local function tryConsumeFuelInSlots() |
| 123 | local consumedAny=false |
| 124 | for s=1,16 do |
| 125 | if turtle.getItemCount(s)>0 then |
| 126 | turtle.select(s) |
| 127 | if turtle.refuel(0) then |
| 128 | turtle.refuel() |
| 129 | consumedAny=true |
| 130 | if turtle.getFuelLevel()>=targetFuel then |
| 131 | return true |
| 132 | end |
| 133 | end |
| 134 | end |
| 135 | end |
| 136 | return consumedAny |
| 137 | end |
| 138 | |
| 139 | if turtle.getFuelLevel()=="unlimited" then return true end |
| 140 | |
| 141 | while turtle.getFuelLevel()<targetFuel do |
| 142 | local madeProgress = tryConsumeFuelInSlots() |
| 143 | if turtle.getFuelLevel()>=targetFuel then return true end |
| 144 | if not madeProgress then |
| 145 | print("Out of fuel. Insert coal/fuel. Waiting "..cSleepTime.."s ...") |
| 146 | os.sleep(cSleepTime) |
| 147 | end |
| 148 | end |
| 149 | return true |
| 150 | end |
| 151 | |
| 152 | -- Place a block below the turtle if air; skip if already solid |
| 153 | local function placeBlockDown() |
| 154 | if turtle.detectDown() then return true end |
| 155 | |
| 156 | for s=1,16 do |
| 157 | if turtle.getItemCount(s)>0 then |
| 158 | turtle.select(s) |
| 159 | if not turtle.refuel(0) then |
| 160 | if turtle.placeDown() then |
| 161 | return true |
| 162 | end |
| 163 | end |
| 164 | end |
| 165 | end |
| 166 | |
| 167 | print("No placeable blocks found. Waiting "..cSleepTime.."s ...") |
| 168 | os.sleep(cSleepTime) |
| 169 | return placeBlockDown() |
| 170 | end |
| 171 | |
| 172 | ------------------------------------------------------------------------------ |
| 173 | -- main ---------------------------------------------------------------------- |
| 174 | ------------------------------------------------------------------------------ |
| 175 | |
| 176 | -- step 0 usage hints |
| 177 | term.clear() term.setCursorPos(1,1) |
| 178 | print("+-------------------------------------+") |
| 179 | print("| tPlatform v1.20, by Kaikaku |") |
| 180 | print("+-------------------------------------+") |
| 181 | print("| Put in building materials in any |") |
| 182 | print("| slot(s) and press enter. |") |
| 183 | print("| Platform size: Enter x and y to |") |
| 184 | print("| determine size. Either when asked |") |
| 185 | print("| by program or with function call, |") |
| 186 | print("| e.g., tPlatform 5 10 |") |
| 187 | print("| If turtle runs out of materials it |") |
| 188 | print("| waits until resupplied. |") |
| 189 | print("| Will auto-refuel from inventory. |") |
| 190 | print("+-------------------------------------+") |
| 191 | |
| 192 | -- step 1 get input |
| 193 | ss(1) |
| 194 | if blnAskForParameters then |
| 195 | askForInputText("Put in materials + press enter!") |
| 196 | -- step 1.1 get x |
| 197 | write("Enter length x (default&min=3):") |
| 198 | userX=read() |
| 199 | if userX==nil or userX=="" then userX=3 end |
| 200 | userX=tonumber(userX) -- no error check yet |
| 201 | if userX<3 then userX=3 end |
| 202 | |
| 203 | -- step 1.2 get y |
| 204 | write("Enter width y (default&min=1):") |
| 205 | userY=read() |
| 206 | if userY==nil or userY=="" then userY=1 end |
| 207 | userY=tonumber(userY) -- no error check yet |
| 208 | --if userY<2 then userY=2 end |
| 209 | end |
| 210 | userX=math.floor(userX) |
| 211 | userY=math.floor(userY) |
| 212 | |
| 213 | -- check fuel level |
| 214 | local cMinFuel=(userX)*(userY+1)+1 |
| 215 | turtleOk, turtleVal = pcall(checkFuel) |
| 216 | autoRefuel(cMinFuel) |
| 217 | if turtle.getFuelLevel()<cMinFuel then |
| 218 | term.clear() term.setCursorPos(1,1) |
| 219 | print("+-------------------------------------+") |
| 220 | print("| tPlatform v1.20, by Kaikaku |") |
| 221 | print("+-------------------------------------+") |
| 222 | print("| Unable to reach required fuel level |") |
| 223 | print("| minimum of about ",cMinFuel," units.") |
| 224 | print("| Insert coal/fuel and restart. |") |
| 225 | print("+-------------------------------------+") |
| 226 | return |
| 227 | end |
| 228 | |
| 229 | -- step 2 build (new) |
| 230 | print("Let's build something nice:") |
| 231 | placeBlockDown() |
| 232 | for row=1,userY do |
| 233 | for col=2,userX do |
| 234 | gf() |
| 235 | placeBlockDown() |
| 236 | end |
| 237 | if row<userY then |
| 238 | if row%2==1 then |
| 239 | gr(); gf(); gr() |
| 240 | else |
| 241 | gl(); gf(); gl() |
| 242 | end |
| 243 | placeBlockDown() |
| 244 | end |
| 245 | end |
| 246 | |
| 247 | print("Done. Looks nice to me ;)") |
| 248 | os.sleep(0.4) |
| 249 | print("***************************************") |
| 250 | print("* Check out YouTube for more videos *") |
| 251 | print("* and turtle programs by Kaikaku :) *") |
| 252 | print("***************************************") |
| 253 | return |
| 254 | |
| 255 | -- step 2 loopy loops ; |
| 256 | -- ... existing code ... |