org.mars_sim.msp.core.time
Class MasterClock

java.lang.Object
  extended by org.mars_sim.msp.core.time.MasterClock
All Implemented Interfaces:
Serializable, Runnable

public class MasterClock
extends Object
implements Runnable, Serializable

The MasterClock represents the simulated time clock on virtual Mars. Virtual Mars has only one master clock. The master clock delivers a clock pulse the virtual Mars every second or so, which represents a pulse of simulated time. All actions taken with virtual Mars and its units are synchronized with this clock pulse.

Update: The pulse is now tied to the system clock. This means that each time a timePulse is generated, it is the following length:

(realworldseconds since last call ) * timeRatio

update: with regard to pauses..

they work. the sim will completely pause when setPause(true) is called, and will resume with setPause(false); However ! Do not make any calls to System.currenttimemillis(), instead use uptimer.getuptimemillis(), as this is "shielded" from showing any passed time while the game is paused. Thank you.

See Also:
Serialized Form

Field Summary
static int secspday
           
static int secsperyear
           
static int secsphour
           
static int secspmin
           
 
Constructor Summary
MasterClock()
          Constructor
 
Method Summary
 void addClockListener(ClockListener newListener)
          Adds a clock listener
 void destroy()
          Prepare object for garbage collection.
 void exitProgram()
          Sets the exit program flag.
 void fireClockPulse(double time)
          Send a clock pulse to all clock listeners.
 void firePauseChange()
          Send a pulse change event to all clock listeners.
 EarthClock getEarthClock()
          Returns the Earth clock
 MarsClock getInitialMarsTime()
          Gets the initial Mars time at the start of the simulation.
 MarsClock getMarsClock()
          Returns the Martian clock
 double getPulsesPerSecond()
           
 double getTimePulse()
          Gets the time pulse length in other words, the number of realworld seconds that have elapsed since it was last called
 double getTimeRatio()
          Gets the real-time/simulation ratio.
 String getTimeString(double seconds)
          the following is a utility.
 long getTotalPulses()
           
 UpTimer getUpTimer()
          Returns uptime timer
 boolean isLoadingSimulation()
          Checks if in the process of loading a simulation.
 boolean isPaused()
          Checks if the simulation is paused or not.
 boolean isSavingSimulation()
          Checks if in the process of saving a simulation.
 void loadSimulation(File file)
          Sets the load simulation flag and the file to load from.
 void removeClockListener(ClockListener oldListener)
          Removes a clock listener
 void run()
          Run clock
 void saveSimulation(File file)
          Sets the save simulation flag and the file to save to.
 void setPaused(boolean isPaused)
          Set if the simulation is paused or not.
 void setTimeRatio(double ratio)
          setTimeRatio is for setting the Masterclock's time ratio directly.
 void stop()
          Stop the clock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

secspmin

public static final int secspmin
See Also:
Constant Field Values

secsphour

public static final int secsphour
See Also:
Constant Field Values

secspday

public static final int secspday
See Also:
Constant Field Values

secsperyear

public static final int secsperyear
See Also:
Constant Field Values
Constructor Detail

MasterClock

public MasterClock()
Constructor

Throws:
Exception - if clock could not be constructed.
Method Detail

getMarsClock

public MarsClock getMarsClock()
Returns the Martian clock

Returns:
Martian clock instance

getInitialMarsTime

public MarsClock getInitialMarsTime()
Gets the initial Mars time at the start of the simulation.

Returns:
initial Mars time.

getEarthClock

public EarthClock getEarthClock()
Returns the Earth clock

Returns:
Earth clock instance

getUpTimer

public UpTimer getUpTimer()
Returns uptime timer

Returns:
uptimer instance

addClockListener

public final void addClockListener(ClockListener newListener)
Adds a clock listener

Parameters:
newListener - the listener to add.

removeClockListener

public final void removeClockListener(ClockListener oldListener)
Removes a clock listener

Parameters:
oldListener - the listener to remove.

loadSimulation

public void loadSimulation(File file)
Sets the load simulation flag and the file to load from.

Parameters:
file - the file to load from.

isLoadingSimulation

public boolean isLoadingSimulation()
Checks if in the process of loading a simulation.

Returns:
true if loading simulation.

saveSimulation

public void saveSimulation(File file)
Sets the save simulation flag and the file to save to.

Parameters:
file - save to file or null if default file.

isSavingSimulation

public boolean isSavingSimulation()
Checks if in the process of saving a simulation.

Returns:
true if saving simulation.

exitProgram

public void exitProgram()
Sets the exit program flag.


getTimePulse

public double getTimePulse()
Gets the time pulse length in other words, the number of realworld seconds that have elapsed since it was last called

Returns:
time pulse length in millisols
Throws:
Exception - if time pulse length could not be determined.

getTotalPulses

public long getTotalPulses()

setTimeRatio

public void setTimeRatio(double ratio)
setTimeRatio is for setting the Masterclock's time ratio directly. It is a double indicating the simetime:realtime ratio. 1000 means 1000 sim time minutes elapse for each real-world minute.


getTimeRatio

public double getTimeRatio()
Gets the real-time/simulation ratio.

Returns:
ratio

run

public void run()
Run clock

Specified by:
run in interface Runnable

fireClockPulse

public void fireClockPulse(double time)
Send a clock pulse to all clock listeners.

Parameters:
time - the amount of time (millisols) in the pulse.

stop

public void stop()
Stop the clock


setPaused

public void setPaused(boolean isPaused)
Set if the simulation is paused or not.

Parameters:
isPaused - true if simulation is paused.

isPaused

public boolean isPaused()
Checks if the simulation is paused or not.

Returns:
true if paused.

firePauseChange

public void firePauseChange()
Send a pulse change event to all clock listeners.


getPulsesPerSecond

public double getPulsesPerSecond()

getTimeString

public String getTimeString(double seconds)
the following is a utility. It may be slow. It returns a string in YY:DDD:HH:MM:SS.SSS format note: it is set up currently to only return hh:mm:ss.s


destroy

public void destroy()
Prepare object for garbage collection.



Copyright © 2009-2013. All Rights Reserved.