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

Lua math error?

Started by moomoomoo3O9, 22 January 2014 - 02:58 PM
moomoomoo3O9 #1
Posted 22 January 2014 - 03:58 PM
I am using coordinates in a for loop for setting blocks, and it seems that lua does not understand in this program that -227>-251. It should always use the lower number as the first number in the for loop, and then the higher one. (as otherwise it wouldn't work because for loops work in a range!)
(More fun with the worldedit program, I just implemented /pos1 from worldedit instead!)
Relevant code:
Spoiler

if x2<x1 then
   x3=x2
   x4=x1
  else
   x3=x1
   x4=x2
end
if y2<y1 then
  y3=y2
  y4=y1
else
  y3=y1
  y4=y2
end
if z2<z1 then
  z3=z2
  z4=z1
else
  z3=z1
  z4=z2
end

for x=x3,x4 do
  for y=y3,y4 do
   for z=z3,z4 do

Full Program for the curious:
Spoiler

tArgs={...}
username=tostring(tArgs[1])
x1=tArgs[2]
y1=tArgs[3]
z1=tArgs[4]
x2=tArgs[5]
y2=tArgs[6]
z2=tArgs[7]
block=tArgs[8]
meta=tArgs[9]
count=0
iterations=0
p=peripheral.wrap("right")
pl=p.getPlayerByName(username)
w=p.getWorld(p.getPeripheralWorldID())
if x1~=nil and x2~=nil and y1~=nil and y2~=nil and z1~=nil and z2~=nil then
if x2<x1 then
   x3=x2
   x4=x1
  else
   x3=x1
   x4=x2
end
if y2<y1 then
  y3=y2
  y4=y1
else
  y3=y1
  y4=y2
end
if z2<z1 then
  z3=z2
  z4=z1
else
  z3=z1
  z4=z2
end
print("x: "..x3.." - "..x4)
print("y: "..y3.." - "..y4)
print("z: "..z3.." - "..z4)
for x=x3,x4 do
  for y=y3,y4 do
   for z=z3,z4 do
	count=count+1
	iterations=iterations+1
	if count>=10000 then
	 count=0
	 sleep(0.05)
	end
	w.setBlockWithoutNotify(x,y,z,tonumber(block),tonumber(meta))
	if iterations==1 then
	 pl.sendChat("Setting blocks...")
	end
   end
  end
end
end
if iterations==0 then
pl.sendChat("For loop not executed.")
end
pl.sendChat(iterations.." blocks changed in "..math.floor(iterations/10000).." seconds.")
Lyqyd #2
Posted 22 January 2014 - 05:05 PM
What arguments are you using to run this program?
moomoomoo3O9 #3
Posted 22 January 2014 - 05:26 PM
Arguments for this program are:

set [username of the player to send chat message] x, y, z of first corner, x, y, z of second corner, block type, and metadata.

everything but the username is a number.
an example of running it would be:
shell.run("set", "moomoomoo309", -277, 5, 32, -255, 10, 48, 1, 0)
What comes out of this would be (on the lines where it prints x3-4, y3-4, z3-4)
x: -255 - -277
y: 10 - 5
z: 32 - 48
Meaning the entire huge block of "if" statements making sure that x3, y3, and z3 is less than x4, y4, and z4 did nothing.
Lyqyd #4
Posted 22 January 2014 - 05:30 PM
You should probably tonumber() the arguments when you assign the variables from the table. Otherwise, they'll be sorted alphabetically instead of numerically. For example,


local x1 = tonumber(tArgs[2])
moomoomoo3O9 #5
Posted 22 January 2014 - 05:31 PM
That worked. Thanks!