tPlatform_xyz.lua
228 lines · 6.6 KB
{program="tPlatform",version="1.10",date="2016-02-28"}
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/tPlatform/tPlatform_xyz.lua
| 1 | --{program="tPlatform",version="1.10",date="2016-02-28"} |
| 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 userZ=1 |
| 27 | local blnAskForParameters=true |
| 28 | local blnDirectionX=true |
| 29 | local currentSlot=1 |
| 30 | |
| 31 | --------------------------------------- |
| 32 | ---- tArgs ---------------------------- |
| 33 | --------------------------------------- |
| 34 | local tArgs = {...} |
| 35 | if #tArgs == 3 then -- no error check |
| 36 | blnAskForParameters=false |
| 37 | userX=tonumber(tArgs[1]) |
| 38 | userY=tonumber(tArgs[2]) |
| 39 | userZ=tonumber(tArgs[3]) |
| 40 | elseif #tArgs == 2 then -- no error check |
| 41 | blnAskForParameters=false |
| 42 | userX=tonumber(tArgs[1]) |
| 43 | userY=tonumber(tArgs[2]) |
| 44 | end |
| 45 | |
| 46 | --------------------------------------- |
| 47 | -- basic functions for turtle control - |
| 48 | --------------------------------------- |
| 49 | local function mats() |
| 50 | if turtle.getItemCount(currentSlot)==0 then |
| 51 | currentSlot=currentSlot+1 |
| 52 | if currentSlot>16 then |
| 53 | currentSlot=1 |
| 54 | print("Out of materials, please restock!") |
| 55 | print(" Sleeping for "..cSleepTime.." sec ...") |
| 56 | os.sleep(cSleepTime) |
| 57 | end |
| 58 | turtle.select(currentSlot) |
| 59 | mats() |
| 60 | end |
| 61 | end |
| 62 | |
| 63 | local function gf() while not turtle.forward() do end end |
| 64 | local function gb() while not turtle.back() do end end |
| 65 | local function gu() while not turtle.up() do end end |
| 66 | local function gd() while not turtle.down() do end end |
| 67 | local function gl() while not turtle.turnLeft() do end end |
| 68 | local function gr() while not turtle.turnRight() do end end |
| 69 | local function df() turtle.dig() end |
| 70 | local function du() turtle.digUp() end |
| 71 | local function dd() turtle.digDown() end |
| 72 | local function pf() mats() while not turtle.place() do end end |
| 73 | local function pu() mats() while not turtle.placeUp() do end end |
| 74 | local function pd() mats() while not turtle.placeDown() do end end |
| 75 | local function sf() turtle.suck() end |
| 76 | local function su() turtle.suckUp() end |
| 77 | local function sd() turtle.suckDown() end |
| 78 | local function Df() turtle.drop() end |
| 79 | local function Du() turtle.dropUp() end |
| 80 | local function Dd() turtle.dropDown() end |
| 81 | local function ss(s) turtle.select(s) end |
| 82 | |
| 83 | local function askForInputText(textt) |
| 84 | local at="" |
| 85 | -- check prompting texts |
| 86 | if textt==nil then textt="Enter text:" end |
| 87 | |
| 88 | -- ask for input |
| 89 | write(textt) |
| 90 | at=read() |
| 91 | return at |
| 92 | end |
| 93 | |
| 94 | |
| 95 | local function checkFuel() |
| 96 | local tmp=turtle.getFuelLevel() |
| 97 | return tmp |
| 98 | end |
| 99 | |
| 100 | ------------------------------------------------------------------------------ |
| 101 | -- main ---------------------------------------------------------------------- |
| 102 | ------------------------------------------------------------------------------ |
| 103 | |
| 104 | -- step 0 usage hints |
| 105 | term.clear() term.setCursorPos(1,1) |
| 106 | print("+-------------------------------------+") |
| 107 | print("| tPlatform v1.10, by Kaikaku |") |
| 108 | print("+-------------------------------------+") |
| 109 | print("| Put in building materials in any |") |
| 110 | print("| slot(s) and press enter. |") |
| 111 | print("| Platform size: Enter x and y to |") |
| 112 | print("| determine size, and optional z |") |
| 113 | print("| height. Either when asked |") |
| 114 | print("| by program or with function call, |") |
| 115 | print("| e.g., tPlatform 5 10 3 |") |
| 116 | print("| If turtle runs out of materials it |") |
| 117 | print("| waits until resupplied. |") |
| 118 | print("+-------------------------------------+") |
| 119 | |
| 120 | -- step 1 get input |
| 121 | ss(1) |
| 122 | if blnAskForParameters then |
| 123 | askForInputText("Put in materials + press enter!") |
| 124 | -- step 1.1 get x |
| 125 | write("Enter depth x (default&min=3):") |
| 126 | userX=read() |
| 127 | if userX==nil or userX=="" then userX=3 end |
| 128 | userX=tonumber(userX) -- no error check yet |
| 129 | if userX<3 then userX=3 end |
| 130 | |
| 131 | -- step 1.2 get y |
| 132 | write("Enter width y (default&min=1):") |
| 133 | userY=read() |
| 134 | if userY==nil or userY=="" then userY=1 end |
| 135 | userY=tonumber(userY) -- no error check yet |
| 136 | --if userY<2 then userY=2 end |
| 137 | -- step 1.3 get z |
| 138 | write("Enter height z (default&min=1):") |
| 139 | userZ=read() |
| 140 | if userZ==nil or userZ=="" then userZ=1 end |
| 141 | userZ=tonumber(userZ) |
| 142 | if userZ<1 then userZ=1 end |
| 143 | end |
| 144 | userX=math.floor(userX) |
| 145 | userY=math.floor(userY) |
| 146 | userZ=math.floor(userZ) |
| 147 | |
| 148 | -- check fuel level |
| 149 | local cMinFuel=(userX)*(userY+1)+1 |
| 150 | -- account for z layers and vertical travel (up and down between layers) |
| 151 | cMinFuel = userZ * cMinFuel + 2*(userZ-1) |
| 152 | turtleOk, turtleVal = pcall(checkFuel) |
| 153 | if turtleVal<cMinFuel then |
| 154 | term.clear() term.setCursorPos(1,1) |
| 155 | print("+-------------------------------------+") |
| 156 | print("| tPlatform v1.10, by Kaikaku |") |
| 157 | print("+-------------------------------------+") |
| 158 | print("| Please refuel turtle, it needs a |") |
| 159 | print("| minimum of about ",cMinFuel," fuel units.") |
| 160 | print("| Tip: Put some fuel (e.g. coal) in |") |
| 161 | print("| slot 1 and enter: refuel all. |") |
| 162 | print("| This will consume all(!) fuel |") |
| 163 | print("| items in the turtle's inventory|") |
| 164 | print("+-------------------------------------+") |
| 165 | return |
| 166 | end |
| 167 | |
| 168 | -- step 2 loopy loops ;) |
| 169 | print("Let's build something nice:") |
| 170 | for iZ=1,userZ,1 do |
| 171 | if iZ>1 then gu() end |
| 172 | -- step 2.1 go to start position |
| 173 | -- & if odd number go back |
| 174 | if userY%2==1 then |
| 175 | -- odd number of rows |
| 176 | for i=1,userX,1 do gf() end |
| 177 | blnDirectionX=false |
| 178 | else |
| 179 | -- even number of rows |
| 180 | gf() gf() gl() gl() |
| 181 | blnDirectionX=true |
| 182 | end |
| 183 | |
| 184 | -- step 2.2 build it |
| 185 | for iY=1,userY,1 do |
| 186 | for iX=1,userX-1 do |
| 187 | if iX==1 then |
| 188 | if iY~=1 then |
| 189 | if blnDirectionX then |
| 190 | gl() |
| 191 | else |
| 192 | gr() |
| 193 | end |
| 194 | end |
| 195 | gb() pf() |
| 196 | elseif iX==userX-1 then |
| 197 | if iY~=userY then |
| 198 | if blnDirectionX then |
| 199 | -- right turn |
| 200 | gr() |
| 201 | else |
| 202 | -- left turn |
| 203 | gl() |
| 204 | end |
| 205 | end |
| 206 | gb() pf() |
| 207 | else -- in between start and end |
| 208 | gb() pf() |
| 209 | end |
| 210 | |
| 211 | end |
| 212 | blnDirectionX=not blnDirectionX |
| 213 | end |
| 214 | -- go back within 1st row |
| 215 | gr() |
| 216 | for i=1,userY-1,1 do gb() pf() end |
| 217 | gl() gb() pf() |
| 218 | end |
| 219 | |
| 220 | -- return to original height |
| 221 | for i=1,userZ-1,1 do gd() end |
| 222 | |
| 223 | print("Done. Looks nice to me ;)") |
| 224 | os.sleep(0.4) |
| 225 | print("***************************************") |
| 226 | print("* Check out YouTube for more videos *") |
| 227 | print("* and turtle programs by Kaikaku :) *") |
| 228 | print("***************************************") |