Lua API:Simulation

From The Powder Toy
Revision as of 06:32, 5 June 2021 by LBPHacker (talk | contribs) (sim.getSaveID now returns the history offset)
Jump to: navigation, search

The Simulation API allows for modifying the state and properties of particles, air and gravity

Contents

Methods

simulation.adjustCoords

number, number sim.adjustCoords(number x, number y)

Actually this is more of a UI method than a simulation method. Given a mouse position x, y in the window, this function returns the corresponding coordinates in the simulation (taking into account the visibility and position of the zoom window, if applicable).

simulation.airMode

number sim.airMode()

Returns the current Air Simulation Mode.

nil sim.airMode(number mode)

Sets the Air Simulation Mode to mode.

Mode numbers are as follows (not currently available as named constants):

0 Normal
1 Pressure off
2 Velocity off
3 Velocity and pressure off
4 No update

simulation.ambientAirTemp

number sim.ambientAirTemp()

Returns the current ambient temperature. When ambient heat mode is turned on, the air at the edges of the screen will remain at this temperature.

nil sim.ambientAirTemp(number temp)

Sets the ambient temperature. The temperature is measured in Kelvin.

simulation.ambientHeat

number sim.ambientHeat(number x, number y)

Returns a value on the ambient heat map (the temperature of the air at that point).

nil sim.ambientHeat(number x, number y, number temp, [number width, number height])

Sets values on the ambient heat map.

simulation.clearSim

nil sim.clearSim()

Clears everything in the simulation.

simulation.createBox

nil sim.createBox(number x1, number y1, number x2, number y2, [number type], [number flag])

Creates a filled box of either the user's currently selected type or the type specified at the specified coordinates. flag refers to particle replacement flags.

simulation.createLine

nil sim.createLine(number x1, number y1, number x2, number y2, [number rx], [number ry], [number type], [number brush], [number, flag])

Creates a line of of either the user's currently selected type or the type specified at the specified coordinates. rx and ry describe the radius of the brush used. Default radius is 5, 5. flag refers to particle replacement flags.

simulation.createParts

number sim.createParts(number x, number y, [number rx], [number ry], [number type], [number brush], [number flag])

Does something.

simulation.createWallBox

nil sim.createWallBox(number x1, number y1, number x2, number y2, [number walltype])

Creates a filled box of either the specified walltype or the type of the basic wall at the specified particle coordinates. Note: the coordinates might change from particle coordinates to map coordinates in the future.

simulation.createWallLine

nil sim.createWallLine(number x1, number y1, number x2, number y2, [number rx], [number ry], [number walltype])

Creates a line of either the specified walltype or the type of the basic wall at the specified particle coordinates. Note: the coordinates might change from particle coordinates to map coordinates in the future.

simulation.createWalls

number sim.createWalls(number x, number y, [number rx], [number ry], [number walltype])

Does something

simulation.decoBox

nil sim.decoBox(number x1, number y1, number x2, number y2, [number r, number g, number b, [number a]], [number tool])

Changes the decoration color of all particles in the specified coordinates. tool refers to decoration tools.

simulation.decoBrush

nil sim.decoBrush(number x, number y, [number rx], [number ry], [number r, number g, number b, [number a]], [number tool], [number brush])

Does something tool refers to decoration tools.

simulation.decoColor

number sim.decoColor()

Returns the currently selected decoration color.

nil sim.decoColor(number color)

Sets the selected decoration color to color. color is in the format 0xAARRGGBB

nil sim.decoColor(number r, number g, number b, [number a])

Sets the selected decoration color to r,g,b,a

simulation.decoColour

Same as sim.decoColor()

simulation.decoLine

nil sim.decoLine(number x1, number y1, number x2, number y2, [number rx], [number ry], [number r, number g, number b, [number a]], [number tool], [number brush])

Changes the decoration color of all particles in the line specified. rx and ry describe the radius of the brush used. Default radius is 5, 5. tool refers to decoration tools.

simulation.deleteStamp

type sim.deleteStamp(string name)

Deleting a stamp identified by filename or ID.

simulation.edgeMode

number sim.edgeMode()

Returns the current Edge Mode

nil sim.edgeMode(number mode)

Sets the current Edge Mode to mode. 0 means normal, 1 creates a wall all the way round the edge of the simulation.

simulation.elementCount

number sim.elementCount(number type)

Returns the number of particles of the specified type in the simulation.

simulation.floodParts

number sim.floodParts(number x, number y, [number type], [number cm?], [number flag])

Flood fills either the user's currently selected type or the type specified at the coordinates given. flag refers to particle replacement flags.

simulation.floodWalls

number sim.floodWalls(number x, number y, [number walltype], [number bm?])

Flood fills either the specified walltype or the type of the basic wall at the specified particle coordinates. Note: the coordinates might change from particle coordinates to map coordinates in the future.

simulation.framerender

number sim.framerender()
sim.framerender(number frames)

Advances the simulation the given number of frames, even when paused. If called with no arguments, returns the number of frames currently to be rendered. Usually is 0.

simulation.getSaveID

number, number sim.getSaveID()

Returns the save ID and the history offset of the currently loaded save or nil if the simulation is not a downloaded save. The history offset can be used with loadSave.

simulation.gravMap

number sim.gravMap(number x, number y)
nil sim.gravMap(number x, number y, [number width, number height, [number value]])

Returns the newtonian gravity at the given coordinates in the simulation. If given a value, will set the newtonian gravity at those coordinates. Width and height refer to the rectangle of affected cells, starting with the coords. If not given, they will default to 1,1.

simulation.gravityGrid

number sim.gravityGrid()

Returns the current setting for drawing the gravity grid. More of a renderer setting than a simulation setting.

nil sim.gravityGrid(number mode)

Sets the setting for drawing the gravity grid to mode.

simulation.gravityMode

number sim.gravityMode()

Returns the current gravity simulation mode.

nil sim.gravityMode(number mode)

Sets the gravity simulation mode to mode.

0 Normal, vertical gravity
1 No gravity
2 Radial gravity

simulation.gspeed

number sim.gspeed()

Returns the current GoL speed

nil sim.gspeed(number newSpeed)

Sets the current GoL speed. This is the number of frames between GoL updates. Default is one, larger numbers make it slower.

simulation.loadSave

nil sim.loadSave(number SaveID, [number hideDescription], [number history?])

Loads the save associated with the specified SaveID. If hideDescription is non zero, the information for the save is not shown.

simulation.loadStamp

type sim.loadStamp(string name, number x, number y)
type sim.loadStamp(number id, number x, number y)

Loads a stamp identified by filename or ID, and places it at position x,y.

simulation.neighbors

type sim.neighbors(number x, number y, [number rx], [number ry], [number type])

Used for iterating through particles in an area centred on the given coordinates (x, y). Radius in the x and y directions is set by rx and ry. Default radius is 2, 2. If type is provided, only particles of the corresponding type will be returned.

The size of the rectangular area is one plus twice the radius, so a radius of 2, 2 gives a 5x5 pixel area centred on x, y. Particles in the centre position x, y are excluded. Only one particle in each position is included, and energy particles (such as photons, neutrons, electrons) are ignored.

Example (i is the index of the neighbouring particle and nx, ny are its coordinates):

for i,nx,ny in sim.neighbors(100,200,1,1) do
 sim.partProperty(i, sim.FIELD_TEMP, 9999)
end

Or if coordinates of the neighbouring particles are not required:

for i in sim.neighbors(100,200,1,1) do
 sim.partProperty(i, sim.FIELD_TEMP, 9999)
end

simulation.neighbours

Same as sim.neighbors()

simulation.partChangeType

nil sim.partChangeType(number index, number type)

Reliably change the type of a particle, this method avoids the side effects created by changing the type directly with the "partProperty" method.

simulation.partCreate

number sim.partCreate(number index, number x, number y, number type)

Create a single particle at location x, y. Returns the index of the new particle, or a negative number on failure.

Possible values for index are:

  • -1 : Normal particle creation. This is the most useful value. No particle is created if position x, y is occupied and the requested new particle type cannot pass through the particle that is already there.
  • -2 : Create particle as though it was drawn by the user with the brush. Usually not useful.
  • -3 : Create particle without checking for collisions with existing particles. In most cases, this is a bad idea, since a lot of elements don't work properly when there are multiple particles in the same place. Particles may also turn into BHOL if there are too many in the same place. The exception to this is elements that have been specifically designed to cope with this (such as multiple energy particles like PHOT and NEUT in the same place).
  • particle index, >= 0 : Overwrite an existing particle with a new particle. At the moment no collision checking is performed, so the same considerations apply as for index=-3. It is usually safe if the new particle is in the same location as the old one. This is roughly equivalent to calling sim.partKill then sim.partCreate(-3, ...).

simulation.partID

number sim.partID(number x, number y)

Get the index of a particle at the specified position. As of v89.3, this will return nil if there is no particle there.

Example (though this is probably best done with sim.neighbours):

for fx = -1, 1, 1 do
 for fy = -1, 1, 1 do
  local i = sim.partID(x + fx, y + fy)
  if i~=nil and sim.partProperty(i, 'type') == elements.DEFAULT_PT_DMND then
   sim.partProperty(index, sim.FIELD_TEMP, 9999)
  end
 end
end

simulation.partKill

nil sim.partKill(number index)
nil sim.partKill(number x, number y)

Reliably delete a particle at a specified index or location, this method avoids the side effects created by changing the type to 0/DEFAULT_PT_NONE with the "partProperty" method

simulation.partNeighbors

number ... sim.partNeighbours(number x, number y, number radius, [number type])

Returns a list of particles indexes that neighbour the given coordinates that matches the given type (if it is specified) The resulting list does not contain the "origin particle"

simulation.partNeighbours

Same as sim.partNeighbors()

simulation.partPosition

number x, number y sim.partPosition(number index)

Get the location of the particle at the specified index

simulation.partProperty

nil sim.partProperty(number index, object field, object value)

Set the property value on a particle specified by index

object sim.partProperty(number index, object field)

Get the property value on a particle specified by the index

The "field" may be a field name or field ID, see FIELD constants below for valid fields.

simulation.parts

function sim.parts()

Returns an iterator over particle indexes that can be used in lua for loops

simulation.pmap

number sim.pmap(number x, number y)

Get the index of the particle at the specified position. Returns 0 if there is no particle there. This function is very similar to sim.partID, but excludes energy particles (such as PHOT, NEUT, ELEC).

simulation.photons

number sim.photons(number x, number y)

Get the index of the energy particle at the specified position. Returns 0 if there is no particle there. This function is very similar to sim.pmap

simulation.pressure

number sim.pressure(number x, number y)

Returns a value on the pressure map.

nil sim.pressure(number x, number y, number pressure, [number width, number height])

Sets values on the pressure map.

simulation.prettyPowders

number sim.prettyPowders()
nil sim.prettyPowders(mode)

Sets whether the "pretty powders mode" (powders, such as SAND or BCOL, will be assigned random deco values) is on or off. When called with no arguments, returns a value determining whether it is on or off.

simulation.reloadSave

nil sim.reloadSave()

Reloading save.

simulation.resetPressure

nil sim.resetPressure()

Resets the pressure map to no pressure.

simulation.resetTemp

nil sim.resetTemp()

Resets the temperature of all particles to their spawn temperature.

simulation.saveStamp

string sim.saveStamp([number x, number y, number width, number height])

Creates a stamp of the specified coordinates. Coordinates default to entire simulation. Returns the stamp id created.

simulation.signs

table simulation.signs

A table of signs. simulation.signs[1] through simulation.signs[16] are always defined.

simulation.signs[n]

table simulation.signs[n]

A table which always contains the id property. May also contain text, x, y and justification. These properties can be directly modified to change a sign. A justification of 0 means left, 1 means right, 2 means middle and 3 means none. The text property cannot be larger than 45 in length.

simulation.signs.new

number simulation.signs.new([string text, number x, number y, number justification])

Creates a new sign with the specified properties. Returns the sign ID, or nil if there are too many signs (the limit is 16).

simulation.takeSnapshot

nil sim.takeSnapshot()

Takes a undo snapshot, for use with ctrl + z

simulation.toolBox

type sim.toolBox(number x1, number y1, number x2, number y2, [number tool], [number strength])

Does something

simulation.toolBrush

number sim.toolBrush(number x, number y, [number rx], [number ry], [number tool], [number brush], [number strength])

Performs the given tool (HEAT, COOL, AIR, etc) on the given coordinates with the given brush size. The brush types are 0 (circle), 1 (square) and 2 (triangle).

simulation.toolLine

type sim.toolLine(number x1, number y1, number x2, number y2, [number rx], [number ry], [number tool], [number brush], [number strength])

Does something

simulation.velocityX

number sim.velocityX(number x, number y)

Returns an X value on the velocity map.

nil sim.velocityX(number x, number y, [number value], [number width, number height])

Sets X values on the velocity map.

simulation.velocityY

number sim.velocityY(number x, number y)

Returns an Y value on the velocity map.

nil sim.velocityY(number x, number y, [number value], [number width, number height])

Sets Y values on the velocity map.

simulation.waterEqualisation

number sim.waterEqualisation()

Returns the current Water equalisation setting.

nil sim.waterEqualisation(number setting)

Set the Water equalisation setting to setting.

simulation.waterEqualization

Same as sim.waterEqualisation()

Constants

Any of these constants can be accessed with simulation.<constant name here>

DECO

DECO_DIVIDE
DECO_SMUDGE
DECO_ADD
DECO_SUBTRACT
DECO_CLEAR
DECO_DRAW
DECO_MULTIPLY

FIELD

FIELD_DCOLOUR
FIELD_Y
FIELD_TEMP
FIELD_TYPE
FIELD_VY
FIELD_X
FIELD_TMP2
FIELD_TMP
FIELD_FLAGS
FIELD_VX
FIELD_CTYPE
FIELD_LIFE

MAX

MAX_TEMP

MIN

MIN_TEMP

NUM

NUM_PARTS

PT

PT_NUM

R

R_TEMP

TOOL

TOOL_VAC
TOOL_AIR
TOOL_NGRV
TOOL_PGRV
TOOL_HEAT
TOOL_WIND
TOOL_COOL

Uncategorized

YRES
XRES