Posted 19 January 2016 - 07:27 AM
Hi there! I'm tinkering about with CC 1.78 and learning a bit more about how GPS systems work - Mostly I'm tinkering about in Creative right now but I made a GPS cluster tower using Ender Modems, and everything's working fine.
I'm trying to write a pair of programs - One records the player's current position and writes those coordinates to a "Home Position" file, the other should take the values from that file, subtract it from the player's current position,run a square root on calculate the sum of the three and return an absolute value as to how far away they are from their designated "Home" point.
Everything worked fine when I had static values for X, Y and Z (i.e. I just had them declared in the file rather than being recorded), but I'm not quite understanding how to work with tables in terms of reading what I've recorded.
Here's the program that sets the player's home:
Sure enough, I get a file in my system called homepos, that contains the GPS coordinates, so that part works.
Here's the program that runs the calculations for the user's current distance from home:
I'm getting an error at line 7: "Attempt to perform arithmetic __sub on number and table", which I can only conclude means I don't know how to convert table values into usable variables. As always, help's appreciated!
EDIT: Man I'm bad at math. I don't need to square-root anything apparently. The sum of the three distances works just fine as far as measuring distance goes. Regardless, I'm still trying to figure out how tables work, haha.
I'm trying to write a pair of programs - One records the player's current position and writes those coordinates to a "Home Position" file, the other should take the values from that file, subtract it from the player's current position,
Everything worked fine when I had static values for X, Y and Z (i.e. I just had them declared in the file rather than being recorded), but I'm not quite understanding how to work with tables in terms of reading what I've recorded.
Here's the program that sets the player's home:
print("Saving current location as home.")
local x, y, z=gps.locate(5)
local coords={
x,
y,
z,
}
local f=fs.open("/data/homepos", "w")
f.write(textutils.serialise(coords))
f.close()
Sure enough, I get a file in my system called homepos, that contains the GPS coordinates, so that part works.
Here's the program that runs the calculations for the user's current distance from home:
local f=fs.open("/data/homepos", "r")
local x, y, z=textutils.unserialise(f.readAll())
f.close()
local curx, cury, curz=gps.locate(5)
local distx=curx-x
local disty=cury-y
local distz=curz-z
local distsum=distx+disty+distz
local absdist=math.abs(distsum)
print("I am "..math.floor(absdist+0.5).."m away from home!")
I'm getting an error at line 7: "Attempt to perform arithmetic __sub on number and table", which I can only conclude means I don't know how to convert table values into usable variables. As always, help's appreciated!
EDIT: Man I'm bad at math. I don't need to square-root anything apparently. The sum of the three distances works just fine as far as measuring distance goes. Regardless, I'm still trying to figure out how tables work, haha.
Edited on 19 January 2016 - 06:34 AM