Example script
Posted: 20.04.2004, 21:56
Hello to everyone:
This is just a feature demonstration of some interesting functions that have been coded recently. The really simple script below shows just one of many possible uses for Don's getUserInput() function. (I have made a few minor modifications to the included procedures, that are properly documented/acknowledged in the code).
Basically, you can enter some spectral classes (O, B, A, F, G, K, M), and the routine will mark the stars belonging to those classes using small squares. Each spectral class has its own marker color, so finding distribution patterns is possible a priori. To terminate the script, press ESC.
Note: If you have a slow PC, Celestia will likely halt the script and pop up an error message while processing the most crowded classes (this is a normal safety behaviour). I've included a WAIT_ENABLE constant for this case. If you change it to true, such script interruptions won't happen, but as a counterpart, the execution speed will be quite slower.
Copy this and paste it on your favourite text editor. Save it on your current Celestia dir as somename.celx
Bye
This is just a feature demonstration of some interesting functions that have been coded recently. The really simple script below shows just one of many possible uses for Don's getUserInput() function. (I have made a few minor modifications to the included procedures, that are properly documented/acknowledged in the code).
Basically, you can enter some spectral classes (O, B, A, F, G, K, M), and the routine will mark the stars belonging to those classes using small squares. Each spectral class has its own marker color, so finding distribution patterns is possible a priori. To terminate the script, press ESC.
Note: If you have a slow PC, Celestia will likely halt the script and pop up an error message while processing the most crowded classes (this is a normal safety behaviour). I've included a WAIT_ENABLE constant for this case. If you change it to true, such script interruptions won't happen, but as a counterpart, the execution speed will be quite slower.
Copy this and paste it on your favourite text editor. Save it on your current Celestia dir as somename.celx
Code: Select all
--***************************************************************************
--***************************************************************************
-- getUserInput() and markSpectralType() example
-- Authors of the above functions: Don G. and Chris Laurel, respectively
--
-- Shows just one of many uses for Don G.'s getUserInput() function.
-- It asks the user to enter a star spectral class and then calls Chris'
-- markSpectralType() function to mark all matching stars in the current
-- Celestia database.
--
-- Coded by Toti
--***************************************************************************
--***************************************************************************
--***************************************************************************
-- Constants
--***************************************************************************
-- if Celestia shows a timeout error, change this to true
WAIT_ENABLE = false
-- this controls the size of markers:
MARKER_SIZE = 3
-- A dictionary of spectral classes, shape and color of the corresponding marker:
SPECTRALCLASSES = {O={shape="square", color="#00ff00"},
B={shape="square", color="#0000ff"},
A={shape="square", color="#ff0000"},
F={shape="square", color="#ff00ff"},
G={shape="square", color="#00ffff"},
K={shape="square", color="#ffff00"},
M={shape="square", color="#d0d0ff"}}
--***************************************************************************
-- Keyboard Input Callback
--***************************************************************************
function celestia_keyboard_callback (input)
userKeypress = input
-- tell Celestia we will handle this keypress:
return true
end
--***************************************************************************
-- markSpectralType function (Author: Chris Laurel
-- slightly adapted by Toti)
--***************************************************************************
function markSpectralType(spectralType, shape, color)
local numOfStars = celestia:getstarcount()
local counter = 0
local star, first, last
local t0, t = celestia:getscripttime()
celestia:flash ("Searching for " ..spectralType.. "-class stars in a catalog of " ..numOfStars.. " ...", 150)
wait(0.01)
while counter < numOfStars do
star = celestia:getstar (counter)
first, last = string.find(star:spectraltype(), spectralType, 1, true)
if first == 1 then
-- mod: added support for marker color, shape and size parameters:
star:mark (color, shape, MARKER_SIZE)
end
counter = counter + 1
-- mod: slow computers need to control elapsed time between wait() calls, or an error will be returned:
if WAIT_ENABLE then
t1 = celestia:getscripttime()
if t1-t0 > 4.5 then
wait()
t0 = t1
end
end
end
end
--***************************************************************************
-- getUserInput from the keyboard (Author: Don G.
-- slightly adapted by Toti)
--***************************************************************************
function getUserInput(prompt, uppercase)
if prompt == nil then
celestia:print ("getUserInput Error: Please include prompt text.",5, -1, -1, 1, 5)
wait(5)
return ""
end
userKeypress = "" -- Clear the userKeypress var
local inputLine = ""
local origTimeScale = celestia:gettimescale() -- Get the current time-scale
celestia:settimescale (0) -- Pause time
celestia:requestkeyboard (true) -- Enable keyboard input
while true do -- Loop until we get Enter key
-- Display the prompt...
celestia:print(prompt .. inputLine, 100, -1, -1, 1, 5)
wait(0.01)
-- What key did the user press...
if userKeypress == "\013" then -- Enter key, we're done
break
elseif userKeypress == "\008" then -- Backspace key, remove last char
local strlen = string.len(inputLine)
if strlen <= 1 then
inputLine = ""
else
inputLine = string.sub (inputLine, 1, strlen - 1)
end
else -- Add the character to inputLine...
-- mod: support for displaying user input on uppercase (mainly of aesthetical value):
if uppercase == true then
userKeypress = string.upper (userKeypress)
end
inputLine = inputLine .. userKeypress
end
userKeypress = ""
end
celestia:requestkeyboard(false) -- Disable keyboard input
celestia:settimescale(origTimeScale) -- Reset the time scale
return inputLine
end
--***************************************************************************
-- This is the main routine
--***************************************************************************
-- Make sure Marker display is ON...
celestia:show("markers")
-- Clear all existing markers...
celestia:unmarkall()
repeat
input = getUserInput("(O B A F G K M)\n Enter spectral class and press ENTER: ", true)
sclass = SPECTRALCLASSES[input]
-- do not waste CPU time searching for non existing spectral classes:
if sclass ~= nil then
markSpectralType (input, sclass.shape, sclass.color)
end
until 1 == 2 --you must press ESC to stop this... ;)
Bye