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

Command Computer problems

Started by HPWebcamAble, 21 March 2015 - 09:14 PM
HPWebcamAble #1
Posted 21 March 2015 - 10:14 PM
I've run into a problem while working on my latest program

No spoilers yet, but here are the basics:


Problem 1: SOLVED!

You have the name of a player and a SINGLE command computer
How would you get their position?


Problem 2: Partially solved, but good enough for now

How would you spawn a computer through a command computer, and have it run a file?
I was thinking maybe 'setblock' a computer, then setblock a disk drive and put a disk in it that has a startup file
Edited on 23 March 2015 - 01:44 AM
Wojbie #2
Posted 21 March 2015 - 10:36 PM
For problem 2:

Is computer id stored in NBT tag? If yes you could spawn pre-made computer with same id as existing one so it will have needed program saved as startup. No idea how to start that computer up.
Bomb Bloke #3
Posted 22 March 2015 - 01:08 AM
1) You don't, at least, not exactly.

This is because its functions boil down to being the same thing as the commands available to actual command blocks. Command blocks, if they want to "store" something long-term, use the scoreboard to do it. They can, for example, ask the scoreboard to look out for a certain event, and have it perform an action when that happens - for example, they could set it up so that every time a player is detected killing a zombie, that player gets a cookie. Or when a player hits a block, a firework fires, and so on - you define an objective, then an action to perform when the objective is met. At least, that's to my limited understanding - and of course, they can assign points! The command block(s) used to configure the scoreboard never get notified of these events, though.

Scoreboard aside, you can also target specific players by testing for them within your command. Let's say you were using a regular command block and you wanted to teleport a player from a specific location, say, 246 65 180, to somewhere else, say twenty blocks above where you grabbed them from. You'd do this:

tp @a[x=246,y=65,z=180,r=1] ~ ~20 ~

Let's say the location to search happened to be on top of your command block. Putting a pressure plate directly above the block and stomping on it would run the command, performing a test for all players at that location and 'porting them up into the air.

Now let's say you wanted to do the same thing with a command computer - first off, it's a little simpler in that it can work out its own location, so you don't need to put the numbers in manually. The code would look like this:

local x, y, z = commands.getBlockPosition()

while true do
	os.pullEvent("redstone")
	
	if rs.getInput("top") then
		commands.tp("@a[x="..x..",y="..(y+1)..",z="..z..",r=1] ~ ~20 ~")
	end
end

Only now you should be able to see that it becomes trivial to change the behaviour to wait for things other than redstone signals - for example, you could have the loop sleep for a few seconds instead, removing the need for the pressure plate. With an actual command block, such a "timer" would involve setting up an actual redstone timer (or using the scoreboard).

It's somewhat interesting to note that while you can't directly get a command to tell you where a player is, running a teleport command on a player will give you their name and new location (eg, try storing commands.tp's output into a variable).

2) The only way to turn a system on remotely is via a peripheral call, and command computers unfortunately have no special powers in this regard.

If it helps, remember that command computers can do anything a regular computer can, anything a command block can do, and they also have commands.getBlockPosition() and commands.getBlockInfo(). Those two extra functions are their only "unique" features, though turtles have a less powerful version of the latter (turtle.inspect()). Put all their abilities together, and they make it possible to turn what would otherwise be a huge cluster of command blocks and redstone circuits, into one Lua script.
Edited on 22 March 2015 - 12:14 AM
HPWebcamAble #4
Posted 22 March 2015 - 01:40 AM
For problem 2:

Is computer id stored in NBT tag? If yes you could spawn pre-made computer with same id as existing one so it will have needed program saved as startup. No idea how to start that computer up.

That is a very good point, I'll have to try that

-snip-

Also good points.



I'm going to think about ways to do these two things, I'll post any ideas I have.
HPWebcamAble #5
Posted 22 March 2015 - 02:24 AM
UPDATE!

After some testing and help from MCEdit, I have figured out how to use /setblock to place a computer with some id:

/setblock ~ ~ ~ ComputerCraft:CC-Computer 2 replace {computerID:1}
Just replace the '1' with the ID you want

Now, after looking at the commands that are in 1.7, I've concluded that my program isn't possible in this version
I'll have to wait for 1.8. Mostly because I think the main command I'll use is /execute
Wojbie #6
Posted 22 March 2015 - 05:59 AM
Posting without testing from work again but if TP returns player coords and name what would happen if you did /TP @a ~0 ~0 ~0.
HPWebcamAble #7
Posted 22 March 2015 - 06:47 AM
Posting without testing from work again but if TP returns player coords and name what would happen if you did /TP @a ~0 ~0 ~0.

Unfortunately, it doesn't, but that would make things a lot easier.
I stand corrected. I guess I haven't used /tp in a while

I think the best solution is to use /execute to run commands relative to the player
For my needs, I don't actually need the exact coords, but it would really simplify things.
Edited on 22 March 2015 - 01:54 PM
Bomb Bloke #8
Posted 22 March 2015 - 07:24 AM
As mentioned, yes it does (assuming you've updated past CC 1.7), and although wojbie's command results in a slight camera wobble, it strikes me as a great way to perform player lookups.
HPWebcamAble #9
Posted 22 March 2015 - 03:25 PM
I've written a quick program to find the player location. All you have to do is type in the players name
(It has to be run on a command computer of course)

Here's is a function that gets a players location:

function getPlayerPos(name)
  local state,result = commands.exec("/tp "..name.." ~ ~ ~")
  if not state then return false end
  return result[1]:match( "Teleported %S+ to (.?%d+%.?%d*),(.?%d+%.?%d*),(.?%d+%.?%d*)" )
end


Thanks for the suggestions wojbie and bomb and king :)/>

EDIT: Fixed that it would't work if the coords were negative
Edited on 06 September 2015 - 10:56 PM
KingofGamesYami #10
Posted 22 March 2015 - 06:59 PM
I've got a helpful pattern for you:

result[1]:match( "Teleported %S+ to {(%d+),(%d+),(%d+)}" )
HPWebcamAble #11
Posted 22 March 2015 - 10:46 PM
I've got a helpful pattern for you:

result[1]:match( "Teleported %S+ to {(%d+),(%d+),(%d+)}" )

Doesn't seem to work :(/>


str = "Teleported test to 192.3902,29.3233,392.4020"

str:match( "Teleported %S+ to {(%d+),(%d+),(%d+)}" )  --#> nil
KingofGamesYami #12
Posted 22 March 2015 - 11:25 PM
Ah, sorry. It's formatted a bit differently than I thought.

This'll work


print( str:match( "Teleported %S+ to (%d+%.?%d*),(%d+%.?%d*),(%d+%.?%d*)" ) )
Bomb Bloke #13
Posted 23 March 2015 - 12:06 AM
Now might be a good time to point out that repl.it's there if you actually want to test this stuff.
HPWebcamAble #14
Posted 23 March 2015 - 01:52 AM
Ah, sorry. It's formatted a bit differently than I thought.

This'll work


print( str:match( "Teleported %S+ to (%d+%.?%d*),(%d+%.?%d*),(%d+%.?%d*)" ) )

Very nice, I've added it to my function

Now might be a good time to point out that repl.it's there if you actually want to test this stuff.

I usually use CCEmuRedux, but this is nice to know about
rik_mclightning1 #15
Posted 31 March 2015 - 06:44 AM
For the location you can also use relative coordinates like this

setblock ~ ~1 ~ diamond_block 0 replace

(This is just the vanilla command, but I'm 99% sure it'll work in commands.exec(). I just haven't had much time to play with command computers yet, so I'm not comfortable with the commands api)