Difference between revisions of "Oolite JavaScript Reference: System"

From Elite Wiki
(systemIDForName)
(Removed Oolite 1.75 change notes.)
Line 155: Line 155:
   
 
=== <code>countEntitiesWithScanClass</code> ===
 
=== <code>countEntitiesWithScanClass</code> ===
{{Oolite-method-added|1.75|beta}}
 
 
function '''countEntitiesWithScanClass'''(scanClass : String [, relativeTo : {{oojsclass|Entity}} [, range : Number]]) : Number (integer)
 
function '''countEntitiesWithScanClass'''(scanClass : String [, relativeTo : {{oojsclass|Entity}} [, range : Number]]) : Number (integer)
 
Returns the number of ships with the specified ScanClass in the system.
 
Returns the number of ships with the specified ScanClass in the system.
Line 202: Line 201:
   
 
=== <code>scrambledPseudoRandomNumber</code>===
 
=== <code>scrambledPseudoRandomNumber</code>===
{{oolite-method-added|1.75|beta}}
 
 
function '''scrambledPseudoRandomNumber'''(salt : Number (integer)) : Number
 
function '''scrambledPseudoRandomNumber'''(salt : Number (integer)) : Number
 
Returns a number that’s greater than or equal to zero and less than one. It will always return the same value for a given <code>salt</code> value and system. Crucially, the relationship between systems and numbers is effectively completely different for each salt value, so scripts making similar decisions based on <code>scrambledPseudoRandomNumber()</code> but using different salts will get uncorrelated results. For example, if two different OXPs add a certain type of station to 25 % of systems using <code>if (system.pseudoRandomNumber < 0.25)</code>, the two station types will always be found in the same system; using <code>scrambledPseudoRandomNumber()</code> with different salts avoids this.
 
Returns a number that’s greater than or equal to zero and less than one. It will always return the same value for a given <code>salt</code> value and system. Crucially, the relationship between systems and numbers is effectively completely different for each salt value, so scripts making similar decisions based on <code>scrambledPseudoRandomNumber()</code> but using different salts will get uncorrelated results. For example, if two different OXPs add a certain type of station to 25 % of systems using <code>if (system.pseudoRandomNumber < 0.25)</code>, the two station types will always be found in the same system; using <code>scrambledPseudoRandomNumber()</code> with different salts avoids this.

Revision as of 22:41, 16 December 2011

Prototype: Object
Subtypes: none

The System class represents the current system. There is always one System object, available through the global property system.

Attempts to change system properties are ignored in interstellar space (i.e., the place you end up in after a witchspace malfunction).

Properties

allShips

allShips : Array (read-only)

A list of the ships in the system.

See Also: entitiesWithScanClass(), filteredEntities(), shipsWithPrimaryRole(), shipsWithRole()

description

description : String (read/write)

The description of the current system, as seen on the planet info screen.

economy

economy : Number (integer, read/write)

The type of economy in the current system, ranging from 0 (Rich Industrial) to 7 (Poor Agricultural).

See Also: economyDescription

economyDescription

economyDescription : String (read-only)

A localized description of the economy type, for example, “Mostly Industrial”. Since this string is localized, it should only be used for display purposes.

See Also: economy

government

government : Number (read/write, integer)

The type of government in the current system, ranging from 0 (Anarchy) to 7 (Corporate State).

See Also: governmentDescription

governmentDescription

governmentDescription : String (read-only)

A localized description of the government type, for example, “Democracy”. Since this string is localized, it should only be used for display purposes.

See Also: government

ID

ID : Number (integer, read-only)

A number identifying the system. 0 to 255, or -1 for interstellar space.

info

info : SystemInfo (read/write)

Allows access to system properties, using the same keys as planetinfo.plist.

Example:

system.info.description = "This is a dull planet.";

inhabitantsDescription

inhabitantsDescription : String (read/write)

The description of the inhabitants of the current system, such as “Slimy Blue Frogs”.

isInterstellarSpace

isInterstellarSpace : Boolean (read-only)

true if the current system is in interstellar space, false otherwise.

mainPlanet

mainPlanet : Planet (read-only)

The system’s main planet, or null if there is none.

mainStation

mainStation : Station (read-only)

The system’s main station, or null if there is none.

name

name : String (read/write)

The name of the system.

planets

planets : Array (read-only)

A list of the planets in the system.

population

population : Number (integer, read/write)

The population of the current system.

productivity

productivity : Number (integer, read/write)

The productivity of the current system.

pseudoRandom100

pseudoRandom100 : Number (integer, read-only)

A random number between 0 and 99 that is always the same for a given system. Important: this method has the undesirable side effect of resetting the system random number generator. It should only be used for backwards-compatibility. For new development, use scrambledPseudoRandomNumber() or pseudoRandomNumber.

pseudoRandom256

pseudoRandom256 : Number (integer, read-only)

A random number between 0 and 255 that is always the same for a given system. Important: this method has the undesirable side effect of resetting the system random number generator. It should only be used for backwards-compatibility. For new development, use scrambledPseudoRandomNumber() or pseudoRandomNumber.

pseudoRandomNumber

pseudoRandomNumber : Number (read-only)

A random number between 0 and 1 that is always the same for a given system. In general, you should avoid using this number directly and call scrambledPseudoRandomNumber() instead.

sun

sun : Sun (read-only)

The system’s sun, or null if there is none.

techLevel

techLevel : Number (integer, read/write)

The technology level of the current system, ranging from 0 to 15.

Methods

addGroup

function addGroup(role : String, count : Number [, position: vectorExpression] [, radius: Number]) : ShipGroup

Like addShips(), but puts the ships in a group and returns the group.

See Also: addGroupToRoute(), addShips()

addGroupToRoute

function addGroupToRoute(role : String, count : Number [, fraction: Number] [, route: String]) : ShipGroup

Like addShipsToRoute(), but puts the ships in a group and returns the group.

See Also: addGroup(), addShipsToRoute()

addMoon

function addMoon(planetInfoKey : String) : Planet

Adds a moon to the system, using the specified entry in planetinfo.plist. A moon is the same as a planet, except that it has no atmosphere.

See Also: addPlanet()

addPlanet

function addPlanet(planetInfoKey : String) : Planet

Adds a planet to the system, using the specified entry in planetinfo.plist.

See Also: addMoon()

addShips

function addShips(role : String, count : Number [, position: vectorExpression] [, radius: Number]) : Array

Adds ships to the system and returns the added ships in an array. Position is in absolute coordinates. When no position is given, the witchpoint is assumed. When no radius is given, the maximum scanner range is used. Ships added in range of the witchpoint automatically create a witchpoint entry cloud.

When you want to use coordinates from the legacy "pwp" system you can use the function fromCoordinateSystem to convert legacy type coordinates to absolute coordinates.

See Also: addGroup(), addShipsToRoute()

addShipsToRoute

function addShipsToRoute(role : String, count : Number [, fraction: Number] [, route: String]) : Array

Adds ships to the system on certain common routes and returns the added ships as an array. The routes are the two character codes wp, pw, ws, sw, sp, and ps, where w stands for the witchpoint, p for the planet and s for the sun.

When no route is defined, the route witchpoint → main station is assumed.

The position is a fraction of the route and must be between 0 and 1. Unlike the legacy commands the fraction takes planetary radii in account, so it does not start counting in the centre of sun/planet but from its surface. When no fraction is defined, a random fraction is chosen. Ships are added within scanner range of this position. Ships added in range of the witchpoint automatically create a witchpoint entry cloud.

Example:

var ships = system.addShipsToRoute("myShips", 2, 0.7, "sw")
var pirates = system.addShipsToRoute("pirate", 3, Math.random(), "sp")

The first line adds 2 ships near each-other on the route from sun to witchpoint and puts the added ship in the array ships, and the second line adds a group of 3 pirates at a random position along the sun to planet route and returns the array pirates.

Note: this method will fail and return null if either of the route end points doesn’t exist. No valid routes exist in interstellar space.

See Also: addGroupToRoute(), addShips()

countEntitiesWithScanClass

function countEntitiesWithScanClass(scanClass : String [, relativeTo : Entity [, range : Number]]) : Number (integer)

Returns the number of ships with the specified ScanClass in the system.

countShipsWithPrimaryRole

function countShipsWithPrimaryRole(role : String [, relativeTo : Entity [, range : Number]]) : Number (integer)

Returns the number of ships with the specified primary role in the system.

countShipsWithRole

function countShipsWithRole(role : String [, relativeTo : Entity [, range : Number]]) : Number (integer)

Returns the number of ships with the specified role in the system. Counts not only the primary role the ship is added with but all roles a ship can have.

entitiesWithScanClass

function entitiesWithScanClass(scanClass : String [, relativeTo : Entity [, range : Number]]) : Array

A list of the entities in the system whose scan class is scanClass. If relativeTo is specified, the list will be sorted in order of proximity to relativeTo (i.e., the first element of the list is closest). If range is also specified, entities further than range metres from relativeTo will be excluded. If no matching entities are found, an empty array is returned.

See Also: shipsWithPrimaryRole(), shipsWithRole(), filteredEntities().

filteredEntities

function filteredEntities(this : Object, predicate : Function [, relativeTo : Entity [, range : Number]]) : Array

A list of the entities for which predicate returns true. If relativeTo is specified, the list will be sorted in order of proximity to relativeTo (i.e., the first element of the list is closest). If range is also specified, entities further than range metres from relativeTo will be excluded. If no matching entities are found, an empty array is returned.

Example:

this.findIdlePoliceInScannerRange = function()
{
    function isIdlePolice(entity)
    {
        return entity.isShip && entity.isPolice && !entity.target
    }
  
    return system.filteredEntities(this, isIdlePolice, player, 25600);
}

See Also: shipsWithPrimaryRole(), shipsWithRole(), entitiesWithScanClass().

legacy_addShips etc.

function legacy_addShips(role : String, count : Number)
function legacy_addSystemShips(role : String, count : Number, position : Number)
function legacy_addShipsAt(role : String, count : Number, coordscheme : String, where : vectorExpression)
function legacy_addShipsAtPrecisely(role : String, count : Number, coordscheme : String, where : Planet)
function legacy_addShipsWithinRadius(role : String, count : Number, coordScheme : String, where : Planet, radius : Number)
function legacy_spawnShip(shipDataKey : String)

Various ways of causing ships to appear. Each of these corresponds to a legacy scripting method. For more info about the coordScheme, look at Oolite coordinate systems. These methods are kept for backwards-compatibility; for new development, addShips() is preferred.

Important: legacy_spawn, previously listed here, never worked as intended and has been replaced with Ship.spawn().

scrambledPseudoRandomNumber

function scrambledPseudoRandomNumber(salt : Number (integer)) : Number

Returns a number that’s greater than or equal to zero and less than one. It will always return the same value for a given salt value and system. Crucially, the relationship between systems and numbers is effectively completely different for each salt value, so scripts making similar decisions based on scrambledPseudoRandomNumber() but using different salts will get uncorrelated results. For example, if two different OXPs add a certain type of station to 25 % of systems using if (system.pseudoRandomNumber < 0.25), the two station types will always be found in the same system; using scrambledPseudoRandomNumber() with different salts avoids this.

sendAllShipsAway

function sendAllShipsAway()

Makes all ships hyperspace out of the system.

shipsWithPrimaryRole

function shipsWithPrimaryRole(role : String [, relativeTo : Entity [, range : Number]]) : Array

A list of the entities in the system whose primary role is role. If relativeTo is specified, the list will be sorted in order of proximity to relativeTo (i.e., the first element of the list is closest). If range is also specified, entities further than range metres from relativeTo will be excluded. If no matching entities are found, an empty array is returned.

See Also: shipsWithRole(), entitiesWithScanClass(), filteredEntities().

shipsWithRole

function shipsWithRole(role : String [, relativeTo : Entity [, range : Number]]) : Array

A list of the entities in the system whose role set contains role. If relativeTo is specified, the list will be sorted in order of proximity to relativeTo (i.e., the first element of the list is closest). If range is also specified, entities further than range metres from relativeTo will be excluded. If no matching entities are found, an empty array is returned.

See Also: shipsWithRole(), entitiesWithScanClass(), filteredEntities().


Static Methods

infoForSystem

function infoForSystem(galaxyNumber: Integer, systemID : Integer) (write-only)

Overwrites existing info of the given system. As properties it uses the planetinfo.plist keys. e.g.

System.infoForSystem(0, 55).description = "This is a dull planet."

sets the description of planet nr 55 in galaxy 0 to his is a dull planet."

Starting from Oolite 1.73, all system Info for the present galaxy is also readable. Even though any galaxy system info property is fully settable, due to engine limitations other galaxies' data is not accessible. In other words

System.infoForSystem(0, 55).description

would return "This is a dull planet." if called from galaxy 0, null from the other galaxies.

systemIDForName

function systemIDForName(systemName : String) : Integer

Returns the ID number of a system in the current galaxy based on the system name)

Note: in galaxy 8, there are two systems named “Inzaan”. systemIDForName() will return the lower ID, which is 22.

systemNameForID

function systemNameForID(systemID : Integer) : String

Returns the name of a system in the current galaxy based on the given ID number)