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

Okay New Problem xD

Started by IndustrialLemon, 15 March 2014 - 11:37 PM
IndustrialLemon #1
Posted 16 March 2014 - 12:37 AM
So for those who were with me before, I had trouble with the Open CC Sensors peripheral. Now Im taking this script from the CC wiki and working towards something a bit bigger. Only problem is, I don't really know how to go about it.

My goal here is to take the code used for getting the position of the player and making the turtle on a loop constantly run a "goto" program, so that it can chase down the player… or something like that.

Could anyone maybe come up with some code I could use? Keep in mind that I've just started this and I'm basically building right off of the program from the wiki page. So on that note there's most likely a lot of useless code in there. Oh and also I've already tried a bit of this on my own so the additions would be the "~= placement" in the if statement.
My idea was to get the placement of the player in that variable and be able to just tell the turtle "goto placement" but now that I think about more I realize that might have to be a table huh.

Here's the code I've been using.
os.loadAPI("ocs/apis/sensor")

-- the location of the redstone lamp relative to the sensor
local offset = {
  X = 1,
  Y = 1,
  Z = 0
}

-- how close a player has to be to activate the lamp
local radius = 5
-- find the distance from the player position to the offset
function distance(pos)
  local xd = pos.X - offset.X
  local yd = pos.Y - offset.Y
  local zd = pos.Z - offset.Z
return math.sqrt(xd*xd + yd*yd + zd*zd)
end

local proximity = sensor.wrap("left")
while true do
  local signal = false
  local targets = proximity.getTargets()
  for k, v in pairs(targets) do
			    if distance(v.Position) ~= placement then
				  signal = true 
			    end
  end
  rs.setOutput("top", signal)
end
CometWolf #2
Posted 16 March 2014 - 12:53 AM
You won't be able to do this right off the bat, since the turtle has no idea which way he's facing, and thus no idea which way to move to close in on the player. Also in the example code distance(v.Position) is the location of a player, relative to the sensor.

I actually did make this sort of turtle a loooong ass time ago
http://pastebin.com/fNVFZDN1
I eventually abandoned it since path finding around blocks and stuff was proving to be somewhat annoying. It could chase down players most of the time however.
IndustrialLemon #3
Posted 16 March 2014 - 01:09 AM
Okay well I just tried your program and it didn't run so I continued on mine. Now the only problem is that when I run the "goto" program I own it's asking for the coords for where to go. This is what I did to combat that…
if distance(v.Position) ~= offset then
				  shell.run("goto", "v.Position")
				  sleep(1)
Maybe I could modify the "goto" program to check a file on the turtle for the position.
If so, show me how!
Edited on 16 March 2014 - 12:10 AM
CometWolf #4
Posted 16 March 2014 - 01:12 AM
Your passing it the string "v.Position", that will obviously not work.
As for my program, no it will not run unless you have the old modified cTurtle API that it uses installed aswell, reading the error it throws would tell you as much… I didn't mean for you to use it, just examine to understand what you have to do.