Example script

All about writing scripts for Celestia in Lua and the .cel system
Topic author
Toti
Developer
Posts: 338
Joined: 10.02.2004
With us: 20 years 9 months

Example script

Post #1by Toti » 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


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
Last edited by Toti on 23.05.2004, 06:48, edited 4 times in total.

don
Posts: 1709
Joined: 12.07.2003
With us: 21 years 4 months
Location: Colorado, USA (7000 ft)

Post #2by don » 20.04.2004, 22:57

Ha! You beat me to it! I wanted to do that, but had to create a data entry routine first. :D

Great script Toti!

Just a suggestion for the input prompt line, which provides the user with the allowable values:

Code: Select all

from:

input = getUserInput("Enter spectral class and press ENTER: ", true)

Code: Select all

to:

input = getUserInput("(O B A F G K M)\n"
         .. "Enter spectral class and press ENTER: ", true)


Keep 'em coming Toti!

By the way, would you like me to host this script as a file on my Celestia web page?
-Don G.
My Celestia Scripting Resources page

Avatar: Total Lunar Eclipse from our back yard, Oct 2004. Panasonic FZ1 digital camera (no telescope), 36X digital zoom, 8 second exposure at f6.5.

Topic author
Toti
Developer
Posts: 338
Joined: 10.02.2004
With us: 20 years 9 months

Post #3by Toti » 21.04.2004, 03:15

Don:

Thanks for your comments. I didn't know that you were coding something alike. :(

Although the script rejects any invalid entry, it's always good to give some input hint. So I've modified the code as you'd suggested.

don wrote:By the way, would you like me to host this script as a file on my Celestia web page?

Well, it's a too simple script..., but if you honestly think it's enjoyable enough, or that it has any value for people interested in learning Celx programming, of course that you can host it. (In the latter case, thank you very much :D)

Bye

don
Posts: 1709
Joined: 12.07.2003
With us: 21 years 4 months
Location: Colorado, USA (7000 ft)

Post #4by don » 21.04.2004, 06:33

Hi Toti,

Some folks (who shall remain nameless) on the forums have complained about posting code segments more than a couple of lines long, so I try to post everything to my web page first, and then offer a link to the file.

So, in that light, your script (as listed on April 20 at about 11:30pm MDT) is now available on my Celestia web page (see URL below) as MarkMultiSpectralTypes.celx.txt, which is here (http://www.donandcarla.com/Celestia/celx_scripting/MarkMultiSpectralTypes.celx.txt).

:D
-Don G.

My Celestia Scripting Resources page



Avatar: Total Lunar Eclipse from our back yard, Oct 2004. Panasonic FZ1 digital camera (no telescope), 36X digital zoom, 8 second exposure at f6.5.

Topic author
Toti
Developer
Posts: 338
Joined: 10.02.2004
With us: 20 years 9 months

Post #5by Toti » 22.04.2004, 02:08

Thank you for hosting the script, Don. :)

don
Posts: 1709
Joined: 12.07.2003
With us: 21 years 4 months
Location: Colorado, USA (7000 ft)

Post #6by don » 22.04.2004, 18:35

My pleasure.

I would be glad to host scripts, tips & tricks files, etc. from anyone else as well, providing a "centralized" location for scripts and scripting resources. Just send me a PM or add a message here.

Cheers,
-Don G.

My Celestia Scripting Resources page



Avatar: Total Lunar Eclipse from our back yard, Oct 2004. Panasonic FZ1 digital camera (no telescope), 36X digital zoom, 8 second exposure at f6.5.

Adam Kadmon
Posts: 8
Joined: 22.05.2004
With us: 20 years 6 months
Location: Tidbinbilla

Post #7by Adam Kadmon » 16.08.2004, 15:05

Hi, nice script... its still running while i type this...

I was wondering if there is a way to limit the distance of the stars selected.
ie. i only want to select G-type stars within, say, 100ly of Sol.

I looked through the Lua notes, but couldnt see any command that seemed to fit the bill.
If I cant see so well it is because I stand in the footsteps of giants....

Harry
Posts: 559
Joined: 05.09.2003
With us: 21 years 2 months
Location: Germany

Post #8by Harry » 16.08.2004, 15:58

Adam Kadmon wrote:I was wondering if there is a way to limit the distance of the stars selected.
ie. i only want to select G-type stars within, say, 100ly of Sol.

I looked through the Lua notes, but couldnt see any command that seemed to fit the bill.

The currently only way to do this is to check all stars one after the other - get position of sol, get position of star, compute distance.

There has been a request for a function which would return all stars in within a given distance, but it certainly won't be added in time for 1.3.2

Harald


Return to “Scripting”