# Height Calc The procedural terrain the game generates rather than stores. Most tiles carry their own height, but the ones that do not fall back to layered value noise that depends only on the tile's world position - so it can be reproduced exactly here. See {ref}`THeightCalc.Calculate` for one tile and {ref}`THeightCalc.CalculateArea` for a region. - - - ## HeightCalc.Setup ```pascal procedure THeightCalc.Setup(); ``` Fills the blend weights the noise interpolation reads. Called once at startup by `cache.simba`. A noise layer sampled every `frequency` tiles blends between its samples along a cosine curve, and a tile can only sit at one of `frequency` positions within a sample. `CoarseWeights` holds the four weights of the layer sampled every 4 tiles and `MidWeights` the two of the layer sampled every 2, so {ref}`THeightCalc.CalculateArea` indexes them rather than going back to the cosine table per row and column. - - - ## HeightCalc.Noise ```pascal function THeightCalc.Noise(x, y: Integer): Integer; ``` A repeatable pseudo-random value from `0` to `255` for a point. It is a hash rather than a random number: the same point always gives the same value, which is what lets the client generate terrain on the fly instead of storing it, and lets this reproduce the terrain the client would have generated. - - - ## HeightCalc.Interpolate ```pascal function THeightCalc.Interpolate(a, b, x, frequency: Integer): Integer; ``` Blends from `a` to `b` along a cosine curve, `x` steps of the way through `frequency`. A cosine rather than a straight line, so that the terrain flattens out as it approaches each grid point instead of meeting the next slope at a crease. - - - ## HeightCalc.SmoothedNoiseArea ```pascal function THeightCalc.SmoothedNoiseArea(x, y, width, height: Integer): TIntegerMatrix; ``` {ref}`THeightCalc.Noise` smoothed over the 3x3 block around each point of a `width` by `height` rectangle whose top-left corner is `x, y`, returned indexed `[x, y]`. Neighbouring points overlap by two thirds, so laying the raw noise out once and reading it back nine times costs a ninth of the `Noise` calls that smoothing each point on its own would. - - - ## HeightCalc.InterpolatedNoiseArea ```pascal function THeightCalc.InterpolatedNoiseArea(x, y, size, frequency: Integer; constref weights: array of Integer): TIntegerMatrix; ``` One noise layer for every tile of a `size` by `size` square whose corner is `x, y`: smoothed noise sampled every `frequency` tiles, with the tiles in between blended from the four samples around them. `weights` are that layer's blend weights, from {ref}`THeightCalc.Setup`. The whole square shares a handful of samples, so they are built as a grid first rather than re-derived for every tile. - - - ## HeightCalc.CalculateArea ```pascal function THeightCalc.CalculateArea(x, y, size: Integer): TIntegerMatrix; ``` The procedural height of every tile of a `size` by `size` square whose corner is `x, y`, returned indexed `[x, y]`, from `10` to `60`. `x` and `y` are world tile coordinates already moved by the client's `932731, 556238` offset. The height is three noise layers summed: one sampled every 4 tiles, one every 2 tiles at half the weight, and one every tile at a quarter of the weight.