Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
-- Chopper -- ---------------------------------------------------------- -- Efficiency test; can this turtle provide a semi-decent -- income of wood/charcoal? -- For those reading, plan is to add a companion turtle that -- uses the resulting (minor) power generation to produce -- supplies of various building blocks. Stone bricks, -- glass, planks, etc. -- And then a third that'll build a base from those blocks. -- ---------------------------------------------------------- -- Initialise important values: -- ---------------------------------------------------------- -- Load locations of interest: shell.run("node") local facing = {"north","east","south","west"} local pos, report = {} local modem = peripheral.wrap("right") -- Number of inventory servers (that should be) online: local invServerCount, invServer = 0, {} -- ---------------------------------------------------------- -- Functions and stuff: -- ---------------------------------------------------------- -- Tell the turtle server I'm alive. local function doTurtleServerReport() rednet.send(report,{"Hello TurtleServer",os.getComputerLabel()}) report = nil end -- Poll the inventory servers re a given barrel. local function getAmountOf(barreltype) local myMessage for i=1,3 do for i=1,invServerCount do rednet.send(invServer[i],barreltype) end myMessage = {rednet.receive(3)} if myMessage and tonumber(myMessage[2]) then break end end if not tonumber(myMessage[2]) then print("Warning: Could not get reading for amount of "..barreltype..".") return 0 else return tonumber(myMessage[2]) end end -- Check if the barrels I fill are filled. local function barrelsFull() for i=1,#barrel do if getAmountOf(barrel[i]) < 4096 then return false end end return true end -- Returns true if the turtle is carrying anything. local function carrying() for i=1,16 do if turtle.getItemCount(i) ~= 0 then return true end end return false end -- Accepts strings representing compass-facings to turn the turtle. local function faceDirection(targetdirection) local tardir = 1 for i=1,4 do if targetdirection == facing[i] then tardir = i end end if tardir < pos.direction then if tardir == 1 and pos.direction == 4 then while not turtle.turnRight() do end else for i=1,pos.direction-tardir do while not turtle.turnLeft() do end end end elseif tardir > pos.direction then if tardir == 4 and pos.direction == 1 then while not turtle.turnLeft() do end else for i=1,tardir-pos.direction do while not turtle.turnRight() do end end end end pos.direction = tardir end -- Move ahead a block, keeping track of the turtle's position. local function forward() while not turtle.forward() do turtle.dig() turtle.attack() end if pos.direction == 1 then pos.y = pos.y - 1 elseif pos.direction == 2 then pos.x = pos.x + 1 elseif pos.direction == 3 then pos.y = pos.y + 1 else pos.x = pos.x - 1 end end local function turn(tardir) if tardir == "left" then while not turtle.turnLeft() do end pos.direction = pos.direction - 1 if pos.direction < 1 then pos.direction = 4 end elseif tardir == "right" then while not turtle.turnRight() do end pos.direction = pos.direction + 1 if pos.direction > 4 then pos.direction = 1 end end end -- Travel to a co-ordinate. local function goToPos(target,digger) while (pos.x ~= node[target][1]) or (pos.y ~= node[target][2]) or (pos.z ~= node[target][3]) do if pos.z > node[target][3] then if digger then while not turtle.down() do turtle.digDown() turtle.attackDown() end pos.z = pos.z - 1 elseif turtle.down() then pos.z = pos.z - 1 end elseif pos.z < node[target][3] then if digger then while not turtle.up() do turtle.digUp() turtle.attackUp() end pos.z = pos.z + 1 elseif turtle.up() then pos.z = pos.z + 1 end end if pos.x > node[target][1] then if pos.direction ~= 4 then faceDirection("west") end if digger then forward() elseif turtle.forward() then pos.x = pos.x - 1 end elseif pos.x < node[target][1] then if pos.direction ~= 2 then faceDirection("east") end if digger then forward() elseif turtle.forward() then pos.x = pos.x + 1 end end if pos.y > node[target][2] then if pos.direction ~= 1 then faceDirection("north") end if digger then forward() elseif turtle.forward() then pos.y = pos.y - 1 end elseif pos.y < node[target][2] then if pos.direction ~= 3 then faceDirection("south") end if digger then forward() elseif turtle.forward() then pos.y = pos.y + 1 end end end faceDirection(node[target][4]) end -- Refuel the turtle itself. local function goGetFuel() print("I'm hungry. Off to eat some EU...") goToPos(3) print("OMNOMNOM") turtle.select(10) while turtle.getFuelLevel() < 5000 do turtle.suckUp() turtle.refuel() sleep(1) end print("Ah, that hit the spot!") print("") end -- Dumps the turtle's inventory into the dropoff chest. local function dropOff() print("Dropping off loot...") local safe = false if (node[1][4] == "north" and pos.y < node[8][2]) or (node[1][4] == "east" and pos.x > node[8][1]) or (node[1][4] == "south" and pos.y > node[8][2]) or (node[1][4] == "west" and pos.x < node[8][1]) then safe = true end goToPos(8,safe) while not turtle.down() do turtle.attackDown() end pos.z = pos.z - 1 for i=1,16 do if turtle.getItemCount(i) ~= 0 then turtle.select(i) turtle.dropDown() end end turtle.select(1) print("") end -- Roam the plot collecting and planting. local function harvest() print("I'm a lumberjack and I'm OK!") print("") if node[10][1] then goToPos(10,true) node[10] = {} else forward() pos.lastX, pos.lastY = 1, 1 end for xx = pos.lastX,16 do for yy = pos.lastY,16 do -- Check if a return to base is required. if turtle.getFuelLevel() < 1000 or turtle.getItemCount(15) == 0 or turtle.getItemCount(14) > 0 then node[10][1] = pos.x node[10][2] = pos.y node[10][3] = pos.z node[10][4] = facing[pos.direction] pos.lastX = xx pos.lastY = yy return end -- Combine sapling piles. if turtle.getItemCount(15) < 5 then for i=1,14 do if turtle.getItemCount(i) ~= 0 then turtle.select(i) if turtle.compareTo(15) then turtle.transferTo(15) end if turtle.getItemCount(15) == 64 then break end end end end if turtle.detectDown() then -- Dig out the current column if need be. turtle.select(1) while turtle.detectUp() do while not turtle.up() do turtle.digUp() end pos.z = pos.z + 1 for i=1,4 do while turtle.detect() do turtle.dig() end while not turtle.turnRight() do end end end while pos.z > node[1][3] do while not turtle.down() do turtle.digDown() end pos.z = pos.z - 1 end end -- Re-plant. turtle.select(16) if turtle.compareDown() or not turtle.detectDown() then while turtle.compareDown() do turtle.digDown() end turtle.select(15) turtle.placeDown() end while turtle.suckDown() do end -- Move ahead. if yy < 16 then forward() elseif xx < 16 then if facing[pos.direction] == node[1][4] then turn("right") forward() turn("right") else turn("left") forward() turn("left") end end if report then doTurtleServerReport() end end end end -- ---------------------------------------------------------- -- Primary functions: -- ---------------------------------------------------------- -- Deal with rednet. local function turtleServer() local turtleServerTimeOut, myEvent = os.startTimer(0) while true do myEvent = {os.pullEvent()} if myEvent[1] == "timer" and myEvent[2] == turtleServerTimeOut then report = 65535 turtleServerTimeOut = os.startTimer(600) elseif myEvent[1] == "rednet_message" and myEvent[3] == "Hey, you still there?" then report = myEvent[2] turtleServerTimeOut = os.startTimer(600) end end end -- Primary work loop. local function main() while true do print("Base maintenance...") print("") if carrying() then dropOff() end -- Ensure I'm not carrying spare stuff. if turtle.getFuelLevel() < 1000 then goGetFuel() end goToPos(4) -- Goto log barrel, get logs. turtle.select(1) turtle.suckUp() goToPos(6) -- Goto furnace, drop logs. turtle.drop() goToPos(9) -- Collect charcoal from furnace. turtle.select(2) turtle.suckDown() goToPos(7) -- Goto generator, drop charcoal. turtle.drop() if carrying() then dropOff() end goToPos(2) -- Goto sapling barrel, get saplings. turtle.select(15) turtle.suckUp() while turtle.getItemCount(15) == 0 do print("Oy! No saplings! Waiting a minute...") sleep(60) turtle.suckUp() end if turtle.getItemCount(16) == 0 then goToPos(4) -- Goto log barrel, get log(s) for comparisons. turtle.select(16) turtle.suckUp() end goToPos(8) goToPos(1,true) -- Prepare to harvest. --if barrelsFull() then -- print("I've filled my barrels, guess I'll take a breather...") -- print("") -- while barrelsFull() do -- sleep(60) -- if report then doTurtleServerReport() end -- end --end harvest() end end -- ---------------------------------------------------------- -- I've just booted up. Where am I? Who am I? etc... -- ---------------------------------------------------------- -- Ping the GPS servers until I get a valid reading: do local counter, tempx, tempy, tempz = 0 while tempx == nil or tempy == nil or tempz == nil do tempx, tempz, tempy = gps.locate(5) sleep(5) end while not turtle.forward() do while not turtle.turnLeft() do end counter = counter + 1 if counter == 20 then -- I really don't wanna do this counter = 0 turtle.dig() end end pos.x,pos.z,pos.y = gps.locate(5) if pos.x < tempx then pos.direction = 4 elseif pos.x > tempx then pos.direction = 2 elseif pos.y < tempy then pos.direction = 1 else pos.direction = 3 end end -- Broadcast until all inventory servers are identified. rednet.open("right") while #invServer < invServerCount do rednet.broadcast("Hello InvServer") local myTimer = os.startTimer(5) while true do local myEvent = {os.pullEvent()} if myEvent[1] == "timer" and myEvent[2] == myTimer then break elseif myEvent[1] == "rednet_message" and myEvent[3] == "Hello, InvServerClient!" then invServer[#invServer+1] = myEvent[2] for i=1,#invServer-1 do if invServer[i] == invServer[#invServer] then invServer[#invServer] = nil end end if #invServer == invServerCount then break end end end end print("I'm at "..pos.x..","..pos.y..","..pos.z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facing[pos.direction]..".") print("") parallel.waitForAny(main, turtleServer)
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
Untitled
13 hours ago | 0.16 KB
settings
14 hours ago | 0.10 KB
IT & AI
23 hours ago | 1.62 KB
Stationeers - Sign Tags from Power Distributi...
HTML | 1 day ago | 2.00 KB
PM: Shopify Client Edits
1 day ago | 0.19 KB
PM: Shopify Assigning Design Task 2
1 day ago | 0.14 KB
PM: Shopify Assigning Design Task 1
1 day ago | 0.32 KB
Commodore Callback 8020
1 day ago | 0.18 KB
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
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!