This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kreezxil's profile picture

Platform II with Torch Placing, Mob Killing and Inventory Requesting

Started by kreezxil, 31 May 2014 - 05:45 PM
kreezxil #1
Posted 31 May 2014 - 07:45 PM
Do you need a turtle that can build platforms? Great!

How about one that can place torches or glowstone? Excellent!

What about one that doesn't get stuck when mobs try to pin it down? No problem!

Want it to stop and request more torches or inventory when it's out? Got it covered!

Also want it to stick to the build parameters and not wander about aimlessly? We got that too!

Need it to handle falling blocks like sand and gravel? Already got your back!

You heard right! I have created for your turtle pleasure the program 'Platform II' which based off of Zach Dyers 'Easy Platform Builder'. I greatly enhanced his version with all of the above features.

Instructions:

Place turtle in lower left of area to be built. The turtle will advance and begin building after you've supplied it with materials and issued the command: platform <length> <width> [torch_interval].
Note: make sure you have fueled the turtle first.

'platform' with no arguments will give you the program help. The torch_interval argument is not required, but if it is present any item in slot 16 will be treated like a torch and placed down when the interval is met.

Fuel is not required but if the turtle runs out it will look in slot 15 for fuel.

Pastebin: M0i8fDU5 -LINK-
TurlteScripts.com: market get gjdi4a platform y -INFO LINK-

Demonstration Video:
[media]http://youtu.be/9tnlm330QwI[/media]

Spoiler

--[[
Credit to Zach Dyer for the original program that I based this on
'Easy Platform Builder'.

Current version: 2.0
Pastebin: M0i8fDU5

Changelog:
5/20/2014 - added torch placing, block detection
5/31/2014 - added mob attacking, better block detection,
inventory requests when empty, resume on keypress.
--]]

local tArgs = {...}

local length = tonumber(tArgs[1])
local width = tonumber(tArgs[2])
local torch_interval = tonumber(tArgs[3])

if torch_interval == nil then
	torch_interval = 0
end

local turnRight = true
local slot = 1
local interval = 0

function pauseAnyKey()
   -- if event == "key" and p1 == keys.q then
   while true do
		local event, p1,p2,p3 = os.pullEvent()
		if event == "key" then
			return p1
		end
		sleep(0.5)
   end
end

-- Kudos to BlockSmith on ComputerCraft.info forums for the suggestion!
function refuel(dim1,dim2)
		dim1 = dim1 or 1
dim2 = dim2 or 1
fuelReq = dim1 * dim2
turtle.select(15)  
while (turtle.getFuelLevel() < fuelReq) do
--inform the user that the turtle is out of fuel
print("Waiting for fuel in slot 15...")
				print("Press any key to continue.")
pauseAnyKey()
-- use all of the fuel in slot 15
turtle.refuel()
-- then go back and check the level again
end

-- select the active slot
turtle.select(slot)
end

function flip()
	turtle.turnRight()
	turtle.turnRight()
end

function digDetect()
	while turtle.detect() do
		turtle.dig()
	end
end

function psycho()
	turtle.attack()
	turtle.attackUp()
	turtle.attackDown()
	turtle.turnLeft()
	turtle.attack()
	turtle.turnRight()
	turtle.turnRight()
	turtle.attack()
	turtle.turnLeft()
end

function digDetectUp()
	while turtle.detectUp() do
		turtle.digUp()
	end
end

function tryForward()
	while not turtle.forward() do
		psycho() --turtle.attack()
		turtle.dig()
	end
end

function tryPlaceDown(mode)
	mode = mode or "ignore me"
	while not turtle.placeDown() do
		if mode == "dig" then
			turtle.digDown()
		end
		--turtle.attackDown()
		psycho()
		turtle.attack()
	end
end

function findInventory()
	for i=slot,14 do
		if turtle.getItemCount(i) > 0 then
			return i
		end
	end
	for i=1,slot-1 do
		if turtle.getItemCount(i) > 0 then
			return i
		end
	end
	return -1
end

function torchPlacer()
	turtle.select(16)
	if turtle.getItemCount(16) == 0 then
		print("I need more torches!")
		print("Put more torches in slot 16 and press any key to make me resume!")
		pauseAnyKey()
	end
	digDetectUp()
	turtle.up()
	flip()
	digDetect()
	flip()
	turtle.back()
	tryPlaceDown()
	--turtle.placeDown()
	tryForward()
	turtle.down()
end

if width == nil then
	print("1. Place turtle facing direction of said platform on left side.")
	print("2. Load turtle with materials for the platform. If placing torches, add light source blocks to slot 16.")
	print("3. Type 'platform <length> <width> [torch_interval]'")
	return
end

turtle.select(slot)


-- set the initial fuel load
refuel(length,width)
tryForward()

for j = 1, width, 1 do

  for i = 1, length, 1 do
	if turtle.getItemCount(slot) == 0 then
		x = findInventory()
	  while x == -1 do	
		  if x == -1 then
			  print("I ran out of materials!")
			  print("Put mats in slots 1 to 14 then")
			  print("press any key to continue.")
			  pauseAnyKey()
			  x = findInventory()
		  end
	 end
	 slot = x
	end

	turtle.select(slot)
	tryPlaceDown("dig")

	if i < length then
	  digDetect()
	  tryForward()
	end

	if torch_interval > 0 then
		interval = interval + 1
		if interval == torch_interval then
		  interval = 0
		  torchPlacer()
		end
	end

  end

  if j < width then

	if turnRight == true then
	  turtle.turnRight()
	  digDetect()
	  tryForward()
	  turtle.turnRight()
	  turnRight = false
	else
	  turtle.turnLeft()
	  digDetect()
	  tryForward()
	  turtle.turnLeft()
	  turnRight = true
	end

  end

  refuel(5) -- cursory check for fuel
end

If you can think of way to optimize my code to make it better while keeping it newbie friendly let me know. I'm open to suggestions and feature requests. Feel free to give credit where credit is due when basing your code off of my code.

Thanks and Happy Turtling!
Edited on 31 May 2014 - 07:38 PM
BlockSmith #2
Posted 31 May 2014 - 08:30 PM
I suggest adding this handy little function of mine in there at the beginning so the turtle will prompt the user to add fuel.


function refuel()
    if (turtle.getFuelLevel() < 5) then   --if the turtle's fuel level is less than 5 then
    turtle.select(16)   --select the gas inventory slot
    while (turtle.getItemCount(16) == 0) do --while the gas slot is empty
	    term.clear()    --clear the screen
	    term.setCursorPos(1, 1) --set the position of the cursor to the top left
	    term.write("Waiting for fuel in slot 16...")   --inform the user that the turtle is out of fuel
	    sleep(2)    --wait 2 seconds
    end
    turtle.refuel(1)    --refuel using 1 item
    turtle.select(1)    --select the first inventory slot
    end
end
kreezxil #3
Posted 31 May 2014 - 08:36 PM
I suggest adding this handy little function of mine in there at the beginning so the turtle will prompt the user to add fuel.


function refuel()
	if (turtle.getFuelLevel() < 5) then   --if the turtle's fuel level is less than 5 then
	turtle.select(16)   --select the gas inventory slot
	while (turtle.getItemCount(16) == 0) do --while the gas slot is empty
		term.clear()	--clear the screen
		term.setCursorPos(1, 1) --set the position of the cursor to the top left
		term.write("Waiting for fuel in slot 16...")   --inform the user that the turtle is out of fuel
		sleep(2)	--wait 2 seconds
	end
	turtle.refuel(1)	--refuel using 1 item
	turtle.select(1)	--select the first inventory slot
	end
end

I'd be more than happy to add it, but is it really your routine? That looks rather common. I'm willing to give credit where credit is due but I wouldn't want to give credit for someone claiming ownership of something common. I will however give credit for the suggestion.
BlockSmith #4
Posted 01 June 2014 - 01:55 AM
It's probably standard among decent coders. However, I did not take any of that code from any source other than the ol' noggin. I don't like taking from others. It's like buying a puzzle already put together in a box. What's the point? lol
kreezxil #5
Posted 01 June 2014 - 05:56 PM
It's probably standard among decent coders. However, I did not take any of that code from any source other than the ol' noggin. I don't like taking from others. It's like buying a puzzle already put together in a box. What's the point? lol

I hear that. I gave you kudos in the code for the suggestion, but not full credit.
BlockSmith #6
Posted 01 June 2014 - 06:45 PM
It's all good buddy. We're a community and the only way to grow is together. Speaking of, would you mind reviewing my auto-smelter/cooker? It's my first complete program in CC and it would be greatly appreciated to get some feedback.

http://www.computercraft.info/forums2/index.php?/topic/18928-my-auto-smelter-auto-cooker/
Edited on 01 June 2014 - 04:52 PM