The code below SHOULD be only going through the indented code if the target is a planet or an asteroid. And it works fine for those bodies. But when I select a star (eg Sol) it prints the type correctly (as expected since that's outside the if statement) and then crashes out because it's trying to get the albedo of a star. What should be happening is that the if statement should spot that it's not a planet or asteroid and skip to the "else" part and not calculate anything, and print that it's not a planet or asteroid.
I can't see any reason why this doesn't work. Why is it still trying to find the albedo of a star here?
And also, can CELX cope with "if... then... else if... then... end" blocks? Or can it only do one if...then...end block at a time?
Code: Select all
KM_PER_LY = 9.46728E12
KM_PER_AU = 149597870.691
camera = celestia:getobserver()
planet = celestia:getselection() -- current selection
camera:center(planet,1)
celestia:select(planet)
type = planet:type()
celestia:flash("target is a "..type)
wait (1)
if type == "planet" or "asteroid" then
cameraPosition = camera:getposition() -- gets position of observer
planetPosition = planet:getposition() -- gets position of target
distance = planetPosition:distanceto(cameraPosition) -- gets distance from position of target to that of observer
distAU = distance/KM_PER_AU
info = planet:getinfo()
name = planet:name()
albedo = info.albedo
radius = planet:radius()
planmag = -26.74 - (5 * (math.log10(((math.sqrt(albedo))*radius)/149597870.691)))
celestia:flash( "Target is a " ..type.. " called "..name.. "\n" ..
"Current distance to target is "..string.format("%.2f",distAU).. " AU\n" ..
"Target albedo is "..string.format("%.2f",albedo).. "\n" ..
"Absolute magnitude of target is "..string.format("%.2f",planmag), 5)
else
celestia:flash( "Target is not a planet or asteroid!", 3)
end