TerrainRiver
Package: Libraries.Game.Graphics.Worlds · Inherits from: Libraries.Language.Object
A TerrainRiver is one river on a terrain: water that runs downhill along a path. TerrainWater holds a terrain's rivers with its lakes, saves them in water.json, and draws each with a WaterSurface whose ripples flow downstream. A river is a list of points, from where it starts to where it ends, each with the river's width and depth there. Compute makes a smooth line through the points and finds the water's surface along it: the ground's height, smoothed and never rising going downstream, so the water always runs downhill. Where the path climbs, the surface stays level and the river cuts into the ground; GetDeepestCut says how far. A river can start from, or end in, a lake or the sea, at that water's level. Carve shapes the ground to hold the river: a bed, deepest in the middle, under the water, and low banks beside it, so the water never floats. The ground's heights from before are kept, so Uncarve can put them back when the river is moved or removed, unless the ground there was changed since.
Example Code
use Libraries.Game.Graphics.Worlds.TerrainItem
use Libraries.Game.Graphics.Worlds.TerrainWater
use Libraries.Game.Graphics.Worlds.TerrainRiver
TerrainItem ground
ground:Load("Terrains/Island/terrain.json")
TerrainWater water = ground:GetWater()
TerrainRiver river = water:AddRiver()
river:SetName("Mill river")
river:AddPoint(10, 40, 4, 1.5)
river:AddPoint(30, 10, 5, 1.5)
river:AddPoint(60, -20, 6, 2)
water:RiverChanged(river)
ground:WaterChanged()
Variables
number SURFACE_DROP: beside the river is a little higher than the water.number BANK_SHARE: beside the river is a little higher than the water.integer MINIMUM_POINTS: The fewest points a river needs before it has any water.number NO_LEVEL: How a level is given as "none", for Compute's start and end.number BANK_HEIGHT: are, as a share of the river's width.
Actions
AddPoint(number x, number z, number width, number depth)
This action adds a point at the river's end.
Parameters
xnumber: The point along x.znumber: The point along z.widthnumber: How wide the river is there.depthnumber: How deep its water is in the middle there.
Carve(Libraries.Game.Graphics.Worlds.TerrainData data)
This action shapes the ground to hold the river: under the water, a bed, deepest in the middle, as deep as the river; beside it, low banks a little above the water, blending into the ground. The ground under the river from before is kept, for Uncarve. Call Compute first, and Uncarve before carving a river again.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain's data.
Returns
integer: How many of the ground's samples changed.
Compute(Libraries.Game.Graphics.Worlds.TerrainData data, number startLevel, number endLevel)
This action finds the river's line and its water's surface from its points and the ground. The line is a smooth curve through the points (Catmull-Rom), sampled every half a terrain cell. The surface is the ground's height along it, smoothed, a little below the ground, and never rising going downstream. A river starting from a lake or the sea starts at that water's level; one ending in one ends at its level.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain's data.startLevelnumber: The level of the water it starts from, or NO_LEVEL.endLevelnumber: The level of the water it ends in, or NO_LEVEL.
CopyFrom(Libraries.Game.Graphics.Worlds.TerrainRiver other)
This action makes this river the same as another: its name, points, speed and carved ground. Its line is found again by the next Compute.
Parameters
otherLibraries.Game.Graphics.Worlds.TerrainRiver: The river to copy.
Covers(number x, number z)
This action returns true if a point is on the river's water.
Parameters
xnumber: The point along x.znumber: The point along z.
Returns
boolean: True if it is.
GetCarveArea(Libraries.Game.Graphics.Worlds.TerrainData data)
This action returns the box of ground samples the river carved, and would carve, as four numbers: smallest x, smallest z, largest x and largest z. Undo copies this area before the ground changes.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain's data.
Returns
Libraries.Containers.Array<integer>: The box, or an empty list for a river with no water and nothing carved.
GetCarvedCount()
This action returns how many ground samples the river carved.
Returns
integer: The count.
GetCarvedList()
This action returns the carved ground as a list, for saving: four numbers per sample, its x and z, its height before and the height the river gave it.
Returns
Libraries.Containers.Array<number>: The list.
GetDeepestCut()
This action returns the most the river cuts into the ground where its path climbs, after Compute.
Returns
number: How far below the ground's level the water had to stay, or 0.
GetDeepestCutPoint()
This action returns the point the deepest cut comes after, so the cut is between it and the next point.
Returns
integer: The point, from 0, or -1 if the river cuts nowhere.
GetDepth()
This action returns the river's depth at its first point, or 0 for a river with no points.
Returns
number: The depth.
GetEndLevel()
This action returns the water's height at the river's end.
Returns
number: The height, or NO_LEVEL before Compute.
GetFlowX(integer index)
This action returns which way the water flows at a sample, along x, as part of a direction one unit long.
Parameters
indexinteger: The sample, from 0.
Returns
number: The direction's x.
GetFlowZ(integer index)
This action returns which way the water flows at a sample, along z.
Parameters
indexinteger: The sample, from 0.
Returns
number: The direction's z.
GetHighestLevel()
This action returns the river's highest water, its start, for the terrain's render box.
Returns
number: The height, or NO_LEVEL before Compute.
GetInsertIndexAt(number x, number z)
This action returns where a new point on the river's line at a place goes in the path: the point it would come before. It is -1 if the place isn't on the river's water.
Parameters
xnumber: The place along x.znumber: The place along z.
Returns
integer: The place in the path for a new point, or -1.
GetLength()
This action returns how long the river is along its line.
Returns
number: The length, or 0 before Compute.
GetLevelAt(number x, number z)
This action returns the water's height at a point, or NO_LEVEL if the point isn't on the river.
Parameters
xnumber: The point along x.znumber: The point along z.
Returns
number: The height.
GetLowestLevel()
This action returns the river's lowest water, its end.
Returns
number: The height, or NO_LEVEL before Compute.
GetName()
This action returns the river's name, such as "Mill river".
Returns
text: The name, or "" for none.
GetPointCount()
This action returns how many points the river's path has.
Returns
integer: The count.
GetPointDepth(integer index)
This action returns how deep the river is at a point.
Parameters
indexinteger: The point, from 0.
Returns
number: The depth.
GetPointNear(number x, number z, number within)
This action returns the point nearest a place, within a distance, or -1.
Parameters
xnumber: The place along x.znumber: The place along z.withinnumber: The farthest a point may be.
Returns
integer: The point, from 0, or -1.
GetPointWidth(integer index)
This action returns how wide the river is at a point.
Parameters
indexinteger: The point, from 0.
Returns
number: The width.
GetPointX(integer index)
This action returns a point's place along x.
Parameters
indexinteger: The point, from 0.
Returns
number: The place.
GetPointZ(integer index)
This action returns a point's place along z.
Parameters
indexinteger: The point, from 0.
Returns
number: The place.
GetSampleAt(number x, number z)
This action returns the sample nearest a point, or -1 if the point isn't on the river's water.
Parameters
xnumber: The point along x.znumber: The point along z.
Returns
integer: The sample, from 0, or -1.
GetSampleCount()
This action returns how many samples the river's line has, after Compute.
Returns
integer: The count.
GetSampleDepth(integer index)
This action returns the river's depth at a sample.
Parameters
indexinteger: The sample, from 0.
Returns
number: The depth.
GetSampleDistance(integer index)
This action returns how far along the river a sample is.
Parameters
indexinteger: The sample, from 0.
Returns
number: The distance from the river's start.
GetSampleSurface(integer index)
This action returns the water's surface height at a sample.
Parameters
indexinteger: The sample, from 0.
Returns
number: The height.
GetSampleWidth(integer index)
This action returns the river's width at a sample.
Parameters
indexinteger: The sample, from 0.
Returns
number: The width.
GetSampleX(integer index)
This action returns a sample's place along x.
Parameters
indexinteger: The sample, from 0.
Returns
number: The place.
GetSampleZ(integer index)
This action returns a sample's place along z.
Parameters
indexinteger: The sample, from 0.
Returns
number: The place.
GetSpeed()
This action returns how fast the ripples move downstream.
Returns
number: The speed, in units a second.
GetStartLevel()
This action returns the water's height at the river's start.
Returns
number: The height, or NO_LEVEL before Compute.
GetSurface()
This action returns the river's water sheet.
Returns
Libraries.Game.Graphics.Worlds.WaterSurface: The sheet, or undefined until it is drawn.
GetWidth()
This action returns the river's width at its first point, or 0 for a river with no points.
Returns
number: The width.
InsertPoint(integer index, number x, number z, number width, number depth)
This action puts a point in the river's path, before the point now at that place.
Parameters
indexinteger: Its place in the path, from 0.xnumber: The point along x.znumber: The point along z.widthnumber: How wide the river is there.depthnumber: How deep its water is in the middle there.
IsComputed()
This action returns true if Compute has run since the points last changed.
Returns
boolean: True if the line and surface are up to date.
IsSameAs(Libraries.Game.Graphics.Worlds.TerrainRiver other)
This action returns true if this river is the same as another: the same name, points, speed and carved ground.
Parameters
otherLibraries.Game.Graphics.Worlds.TerrainRiver: The other river.
Returns
boolean: True if they are the same.
MovePoint(integer index, number x, number z)
This action moves a point of the river's path.
Parameters
indexinteger: The point, from 0.xnumber: Its new place along x.znumber: Its new place along z.
RemovePoint(integer index)
This action takes a point out of the river's path.
Parameters
indexinteger: The point, from 0.
SetCarvedList(Libraries.Containers.Array<number> list)
This action sets the carved ground from a list made by GetCarvedList, such as one being loaded.
Parameters
listLibraries.Containers.Array<number>: Four numbers per sample.
SetDepth(number value)
This action sets how deep the river is at every point.
Parameters
valuenumber: The depth.
SetName(text value)
This action sets the river's name.
Parameters
valuetext: The name.
SetSpeed(number value)
This action sets how fast the ripples move downstream.
Parameters
valuenumber: The speed, in units a second.
SetSurface(Libraries.Game.Graphics.Worlds.WaterSurface value)
This action sets the river's water sheet. TerrainWater calls it.
Parameters
valueLibraries.Game.Graphics.Worlds.WaterSurface: The sheet.
SetWidth(number value)
This action sets how wide the river is at every point.
Parameters
valuenumber: The width.
Uncarve(Libraries.Game.Graphics.Worlds.TerrainData data)
This action puts back the ground the river carved, where it is still as the river left it. Ground changed since, such as by sculpting, is left as it is.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain's data.
Returns
integer: How many samples were left as they are, since they changed after carving.