View difference between Paste ID: PKgSvsA5 and aDY5Ez8M
SHOW: | | - or go back to the newest paste.
1
local version=1.2
2
-- aDY5Ez8M
3
-- r
4
-- sEi'2014-2022
5
--[[
6
	R macros WIP by sEi'2014-2022 - (C)opyleft (Do what you want with the script)
7
	
8
	Get this script here: http://pastebin.com/aDY5Ez8M
9
	or ingame: pastebin get aDY5Ez8M r
10
11
	Used for simple handling of a (Minecraft) Computercraft turtle from the command-line
12
	
13
	My first script and i ended up using it a lot - Bad coding but it works :)
14
15
	Name the script whatever you want, i name it "r" so its quick and easy to use!
16
	
17
	Place this file in the root of the turtle directory and use like the examples below
18
19
    SYNTAX: <Program name> <Orders> [Run this program after completion]
20
21
	Example 1) (Type text in the turtle console and press enter)
22
	r fffurrfffdll
23
	This orders the turtle to go forward 3, up 1, turning right 2 times, forward 3,
24
    down 1, turning left 2 times. - If anything is blocking the path it will stop
25
	with an error message.
26
27
	Example 2)
28
    r Bffrff3y
29
	NOTE: Since the orders starts with a "B" then it will dig blocks blocking the path.
30
    This orders the turtle to move 2 forward, turn right, move 2 forward and finally place a block
31
    from slot #3 below the turtle.
32
33
	RESERVED COMMANDS:
34
	help = Prints the reserved commands.
35
	b = Set BREAK = false (default) - Turtle is NOT breaking blocks that are blocking the path.
36
	B = Set BREAK = True - Turtle IS breaking (digging) blocks that are blocking the path.
37
	P = Place a block in front of turtle from selected slot (default=1).
38
		If BREAK = False (default) - If the spot is not empty then the TURTLE STOP with an error message.
39
		If BREAK = True - If the spot is not empty then the TURTLE DIG until spot is empty and
40
		then place the block.
41
	l = Turn Left 1
42
	r = Turn Right 1
43
	f = Go forward 1
44
	F = Go Forward UNTIL path is blocked. (BREAK true have ofc no effect!)
45
	u = Go Up 1.
46
	U = Go Up UNTIL path is blocked. Use with CAUTION 8^) (BREAK true have ofc no effect!)
47
	d = Go Down 1.
48
	D = Go Down UNTIL path is blocked. (BREAK true have ofc no effect!)
49
	x = Go Back 1.
50
	X = Go Back UNTIL path is blocked. (BREAK true have ofc no effect!)
51
	y = Place block Down
52
	Y = Place block Up
53
	i = Dig in front of Turtle (BREAK true/false have no effect!)
54
	z = Dig Down 1 time (BREAK true/false have no effect!)
55
	Z = Dig Up 1 time (BREAK true/false have no effect!)
56
	w = Do NOT warn when only 1 in current slot
57
	W = (default) Warn when only 1 in current slot
58
	0 (zero) = Use any slot
59
	1-9 = Slot selection (atm. you cannot select slots with a higher number than 9)
60
	I = Ignore blocked path (NOTE: Will give unpredictable results!)
61
	----
62
    I have made some nifty little batch files where i am using R via the shell command!
63
    None published though, just mentioning as inspiration.
64
	Rudimentary batch demo here building a house: https://pastebin.com/r2eMA7WB
65
	ingame: pastebin get r2eMA7WB house
66
	----
67
]]--
68
-- INIT
69
local sCommands = "bBlrPfFuUdDxXyYizZ0123456789wW"
70
local tArgs={...}
71
local sProg = tArgs[1] or "help"
72
local sArg2 =tArgs[2] or ""
73
local iDefSlot = 0
74
iCurSlot = 1 --iDefSlot
75
local bBreak = false
76
local bError = false
77
local bIgnore = false
78
local oT = turtle
79
local max = #sProg
80
local sBak = ""
81
local sOrder
82
local iWarn = 1
83
local iEmpty1 = 4
84
-- FUNCTIONS
85
local function HelpText()
86
    --local sHT = "SYNTAX: <Program name> <Orders>"
87
    local sHT = "TURN:[l]eft,[r]ight"
88
	sHT = sHT.."\nSTEP:[f]orward,[u]p,[d]own,[x]=reverse"
89
	sHT = sHT.."\nMOVE UNTIL BLOCKED:[F],[U],[D],[X]"
90
	sHT = sHT.."\nBREAK:[B]=on,[b]=off(default)"
91
	sHT = sHT.."\nSLOT SELECT:[1-9]or[0]=use any(default)"
92
	sHT = sHT.."\nPLACE BLOCK:[P]front,[Y]up,[y]down"
93
	sHT = sHT.."\nDIG:[i]front,[z]down,[Z]up"
94
	sHT = sHT.."\nEXAMPLE: r Bffdrrffrru"
95
	sHT = sHT.."\n=Set to break blocks if in our path and"
96
	sHT = sHT.."\nforw2,down1,right2,forw2,right2,up1"
97
	sHT = sHT.."\n(See more HELP in the code)"
98
	--sHT = sHT.."\nALL: "..sCommands
99
    return sHT
100
end
101
local function doThis(sChar,sLastMsg)
102
	--doFuel()
103
	if bError==false then
104
		--crude checks
105
		if tonumber(sChar) ~= nil then --is number then is slotselect
106
			local iChar=tonumber(sChar)
107
			if iChar==0 then
108
				local iTemp=0
109
				while iTemp==0 do
110
					iTemp = findAny()
111
					if iTemp>0 then
112
						oT.select(iTemp)
113
					else
114
						--print("No more materials (ANY)")
115
						print("--ERROR--\nNo more materials (ANY)\nI choose to Pause\n--END--")
116
						print("Fill any slot and press ENTER\nor press and hold CTRL-T to quit")
117
						doWaitForEnter()
118
					end
119
				end
120
			else
121
				iCurSlot=iChar
122
				oT.select(iCurSlot)
123
			end
124
		end
125
		if sChar=="I" then bIgnore=true end
126
		if sChar=="P" then
127
			if oT.detect() then --Something in the front and we wanna put there
128
				if bBreak then
129
					while oT.detect() do
130
						oT.dig()
131
						sleep(1)
132
					end
133
					myPlace("front")
134
				else
135
					if bIgnore==false then
136
						error("--ERROR--\nBlock in front of me\nI choose to Exit\n--END--")
137
					end
138
				end
139
			else
140
				myPlace("front")
141
			end
142
		end
143
		if sChar=="w" then
144
			iWarn=0
145
		elseif sChar=="W" then
146
			iWarn=1
147
		elseif sChar=="i"then
148
			oT.dig()
149
		elseif sChar=="z"then
150
			oT.digDown()
151
		elseif sChar=="Z"then
152
			oT.digUp()
153
		elseif sChar=="Y" then
154
			if oT.detectUp() then
155
				if bBreak then
156
					while oT.detectUp() do
157
						oT.digUp()
158
					end
159
					myPlace("up")
160
				else
161
					if bIgnore==false then
162
						error("--ERROR--\nBlock over me\nI choose to Exit\n--END--")
163
						--bError=true
164
					end
165
				end
166
			else
167
				myPlace("up")
168
			end
169
		elseif sChar=="y" then
170
			if oT.detectDown() then
171
				if bBreak then
172
					while oT.detectDown() do
173
						oT.digDown()
174
					end
175
					myPlace("down")
176
				else
177
					if bIgnore==false then
178
						error("--ERROR--\nBlock under me\nI choose to Exit\n--END--")
179
						--bError=true
180
					end
181
				end
182
			else
183
				myPlace("down")
184
			end
185
		elseif sChar == "U" then
186
			while oT.detectUp() == false do
187
				doFuel()
188
				oT.up()
189
			end
190
		elseif sChar == "D" then
191
			while oT.detectDown() == false do
192
				doFuel()
193
				oT.down()
194
			end
195
		elseif sChar == "F" then
196
			while oT.detect() == false do
197
				doFuel()
198
				oT.forward()
199
			end
200
		elseif sChar=="x"then
201
			oT.turnRight()
202
			oT.turnRight()
203
			walk()
204
			oT.turnRight()
205
			oT.turnRight()
206
		elseif sChar=="X" then
207
			oT.turnRight()
208
			oT.turnRight()
209
			while oT.detect() == false do
210
				doFuel()
211
				oT.forward()
212
			end
213
			oT.turnRight()
214
			oT.turnRight()
215
		elseif sChar=="b" then
216
			bBreak = false
217
		elseif sChar=="B" then
218
			bBreak = true
219
		elseif sChar=="f" then
220
			walk()
221
		elseif sChar=="l" then
222
			oT.turnLeft()
223
		elseif sChar=="r" then
224
			oT.turnRight()
225
		elseif sChar=="u" then
226
			if oT.detectUp() then
227
				if bBreak then
228
					while oT.detectUp() do
229
						oT.digUp()
230
						sleep(1)
231
					end
232
				else
233
					if bIgnore==false then
234
						error("--ERROR--\nBlock over me\nI choose to Exit\n--END--")
235
						--bError=true
236
					end
237
				end
238
			end
239
			doFuel()
240
			oT.up()
241
		elseif sChar=="d" then
242
			if oT.detectDown() then
243
				if bBreak then
244
					while oT.detectDown() do
245
						oT.digDown()
246
						sleep(1)
247
					end
248
				else
249
					if bIgnore==false then
250
						error("--ERROR--\nBlock under me\nI choose to Exit\n--END--")
251
						--bError=true
252
					end
253
				end
254
			end
255
			doFuel()
256
			oT.down()
257
		end
258
	end
259
end
260
261
function doFuel()
262
	while turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 do
263
		for i = 16, 1,-1 do -- loop through the slots (backwards to not interfere with important slots 1-9 if possible)
264
		  turtle.select(i) -- change to the slot
265
		  if turtle.refuel(0) then -- it's valid fuel
266
				local halfStack = math.ceil(turtle.getItemCount(i)/2) -- work out half of the amount of fuel in the slot
267
				turtle.refuel(halfStack) -- consume half the stack as fuel
268
				break
269
		  end
270
		end
271
		turtle.select(iCurSlot)
272
		if turtle.getFuelLevel() < 1 then
273
			print("OUT OF FUEL - Put some fuel in a slot and press ENTER")
274
			doWaitForEnter()
275
		end
276
	end
277
end
278
279
function myPlace(sDir)
280
	if iCurSlot>0 then
281
		oT.select(iCurSlot)
282
	else
283
		local iTemp=0
284
		while iTemp==0 do
285
			iTemp = findAny()
286
			if iTemp>0 then
287
				oT.select(iTemp)
288
				iCurSlot=iTemp
289
			else
290
				--print("No more materials (ANY)")
291
				print("--ERROR--\nNo more materials (ANY)\nI choose to Pause\n--END--")
292
				print("Fill any slot and press ENTER\nor press and hold CTRL-T to quit")
293
				doWaitForEnter()
294
			end
295
		end
296
	end
297
	if oT.getItemCount(iCurSlot)==1 then
298
		local iT
299
		--fill current slot up if any similar items around since we only have one item left in current slot.
300
		for iT=1,16 do
301
			if iT~=iCurSlot then
302
				if oT.compareTo(iT) then
303
					local iTemp2=64-oT.getItemCount(iCurSlot)
304
					oT.select(iT)
305
					--local iTemp=oT.getItemCount(iCurSlot)
306
					--if iTemp2<=iTemp then
307
					--	iTemp=iTemp2
308
					--end
309
					oT.transferTo(iCurSlot,iTemp2)
310
					sleep(1)
311
					oT.select(iCurSlot)
312
					break
313
				end
314
			end
315
		end
316
	end
317
	if iEmpty1 and (iCurSlot ~= iEmpty1)then
318
		while oT.getItemCount(iCurSlot)==iWarn do
319
			print("--ERROR--\nNo more materials in slot: "..iCurSlot.."\nI choose to Pause\n--END--")
320
			print("Fill slot: "..iCurSlot.." and press ENTER\nor press and hold CTRL-T to quit")
321
			doWaitForEnter()
322
		end
323
	end
324
	if sDir=="dummy" then
325
		--oT.place()
326
	elseif sDir=="front" then
327
		oT.place()
328
	elseif sDir=="up" then
329
		oT.placeUp()
330
	elseif sDir=="down" then
331
		oT.placeDown()
332
	else --error
333
		error("ERROR: No such direction: "..sDir.."\nI choose to END\n--END--")
334
		--bError=true
335
		return
336
	end
337
	
338
end
339
340
function walk()
341
	if oT.detect() and bBreak then
342
		while oT.detect() do
343
			oT.dig()
344
			sleep(1)
345
		end
346
		doFuel()
347
		oT.forward()
348
	elseif oT.detect() then
349
		if bIgnore==false then
350
			error("--ERROR--\nBlock in front of me\nI choose to Exit\n--END--")
351
			--bError=true
352
		end
353
	else
354
		doFuel()
355
		oT.forward()
356
	end
357
end
358
359
function findAny() --Find a slot containing anything
360
	local iT
361
	for iT=1,15 do
362
		if oT.getItemCount(iT) >0 then return iT end
363
	end
364
	return 0
365
end
366
367
function doWaitForEnter()
368
  while true do
369
    --Pull the event
370
    local _, key = os.pullEvent("key")
371
372
    if key == keys.enter then
373
      break
374
    end
375
  end
376
end
377
--INIT BOT
378
--oT.select(iDefSlot)
379
380
-- MAIN
381
if sProg=="help" then
382
	--print("For help type: r help")
383
	--print("Reserved commands: "..sCommands)
384
    print(HelpText())
385
else
386
	while false do -- ass tests (false = this block is not run)
387
		local sTemp=tArgs[2]
388
		if tArgs[2] then
389
			print("Found Number: "..sProg)
390
		else
391
			print("Found NO 2'nd argument")
392
		end
393
		return
394
	end
395
	--un rem next line to monitor whats going on
396
	--print("Running this: "..sProg)
397
	local iT
398
	for iT=1,max do
399
		sOrder=string.sub(sProg,iT,iT)
400
		doThis(sOrder,sBak)
401
		sBak=sOrder
402
	end
403
404
--	if bError==true then
405
--		print("There was error(s)\nPress any key to exit")
406
--		os.pullEvent("key")
407
--	end
408
	if #sArg2>0 then
409
		shell.run(sArg2)
410
	end
411
end