Alchemy
Loading...
Searching...
No Matches

terrain.c File Reference

Terrain: ground heights and the collision of cells and the world map. More...

Go to the source code of this file.

Macros

#define CURVE_HEIGHT_FIXED(sample)

Functions

s32 Curve_GetFirstSampleA (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 The terrain height functions (gMap_TerrainHeightFunctions): each takes a cell's height samples after its kind byte and a pixel (x, y) within the 16x16 cell, and returns the ground height there in 16.16 pixels.
s32 Curve_LerpTwoSamples (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 A slope along x: from sample 0 at x = 0 towards sample 1 at x = 16.
s32 Curve_LerpTwoSamplesB (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 A slope along y: from sample 0 at y = 0 towards sample 1 at y = 16.
s32 Curve_StepAtSummedPosition (const u8 *heightBytes, s32 x, s32 y)
 A step across the diagonal x + y = 15: sample 0 before it, sample 1 after it and the higher of the two on it.
s32 Curve_StepAtDifferencePosition (const u8 *heightBytes, s32 x, s32 y)
 A step across the diagonal x = y: sample 0 where y < x, sample 1 where y > x and the higher of the two on it.
s32 Curve_LerpThreeSamplesByRatio (const u8 *heightBytes, s32 x, s32 y)
 A slope that bends on the diagonal x + y = 15: from sample 0 at the top left corner to sample 1 on the diagonal to sample 2 at the bottom right.
s32 Curve_LerpThreeSamplesAtDifference (const u8 *heightBytes, s32 x, s32 y)
 A slope that bends on the diagonal x = y: from sample 0 at the top right corner to sample 1 on the diagonal to sample 2 at the bottom left.
s32 Curve_LerpTwoSamplesByTable (const u8 *heightBytes, s32 x, s32 y)
 A raised or sunken rounded square: sample 1 where gCurve_LerpWeightTable is 1, sample 0 in the corners.
s32 Curve_StepAtHalf (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 A step along x: sample 0 for x < 8, sample 1 from x = 8.
s32 Curve_StepAtHalfB (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 A step along y: sample 0 for y < 8, sample 1 from y = 8.
s32 Curve_LerpThreeSamplesAtHalf (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 A slope along x that bends at the middle: sample 0 at x = 0, sample 1 at x = 8, towards sample 2 at x = 16.
s32 Curve_LerpThreeSamplesAtHalfB (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 A slope along y that bends at the middle, as kind 10 does along x.
s32 Curve_LookupSampleByTable (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 The sample gCurve_SampleIndexTable picks for (x, y).
s32 Curve_LookupSampleByTableReversed (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 The same, with the table mirrored left to right.
s32 Curve_GetFirstSampleB (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 Flat, as Curve_GetFirstSampleA. Kind 14.
s32 Curve_GetFirstSampleC (const u8 *heightBytes, s32 pixelX, s32 pixelY)
 Flat, as Curve_GetFirstSampleA. Kind 15.
s32 Map_GetTerrainHeight (s32 layer, s32 x, s32 y)
 Returns the ground height of a layer at (x, y) in 16.16 pixels: the height function of the cell's terrain kind applied to its samples.
s32 Map_GetCellAttributeLowNibble (s32 layer, s32 x, s32 y)
 Returns the terrain kind (the low four bits of the first collision byte) of a layer's cell at (x, y) in 16.16 pixels.
s32 GetMapCellCollision (s32 layer, s32 x, s32 y)
 Returns the collision code of a layer's cell at (x, y) in 16.16 pixels.
void SetMapCellCollision (u32 layer, s32 x, s32 y, u32 collisionCode)
 Sets the collision code of a layer's cell at (x, y) in 16.16 pixels.
UNUSED s32 Map_GetCellHighFlags (s32 x, s32 y)
 Returns the top two bits of the block word of the cell at pixel (x, y).
s32 Map_CheckObjectStep (struct ObjectRuntime *object, struct FieldPosition *position)
 Returns whether an object can step to position: 2 when the cell there is a wall (collision code 0xff), 1 when the ground is more than 8 pixels higher than the object's, -1 when it is more than 12 pixels lower, else 0.
s32 CheckMapPositionCellOccupied (struct WorldPosition *position)
 Returns 0 when the cell under position (its x, and y less its height) in layer 2 is a wall (collision code 0xff), else -1.
s32 GetWorldMapCollision (struct WorldPosition *position)
 Returns the world map's collision value (1-15) at position: from the upper collision layer where it has one, else the lower, else 7.
s32 CheckWorldMapWalkable (s32 unused, struct WorldPosition *position)
 Returns 0 when the world map at position can be walked on (collision 5 to 12), else -1.
s32 GetWorldMapTerrainBehavior (struct WorldPosition *position, s32 *terrainKind)
 Returns the world map's terrain behaviour at position from gWorldMap_TerrainBehaviorTable, and stores the block's terrain kind in terrainKind.

Detailed Description

Terrain: ground heights and the collision of cells and the world map.

Part of field.c, the field object, compiled only through it.

Definition in file terrain.c.

Macro Definition Documentation

◆ CURVE_HEIGHT_FIXED

Function Documentation

◆ Curve_GetFirstSampleA()

s32 Curve_GetFirstSampleA ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

The terrain height functions (gMap_TerrainHeightFunctions): each takes a cell's height samples after its kind byte and a pixel (x, y) within the 16x16 cell, and returns the ground height there in 16.16 pixels.

This one is flat: the first sample everywhere. Each is reached only through that table, by the low four bits of the cell's kind byte: this one is kind 0, and the rest follow in order.

Definition at line 21 of file terrain.c.

Referenced by Curve_GetFirstSampleA().

◆ Curve_LerpTwoSamples()

s32 Curve_LerpTwoSamples ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

A slope along x: from sample 0 at x = 0 towards sample 1 at x = 16.

Kind 1.

Definition at line 28 of file terrain.c.

Referenced by Curve_LerpTwoSamples().

◆ Curve_LerpTwoSamplesB()

s32 Curve_LerpTwoSamplesB ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

A slope along y: from sample 0 at y = 0 towards sample 1 at y = 16.

Kind 2.

Definition at line 44 of file terrain.c.

Referenced by Curve_LerpTwoSamplesB().

◆ Curve_StepAtSummedPosition()

s32 Curve_StepAtSummedPosition ( const u8 * heightBytes,
s32 x,
s32 y )

A step across the diagonal x + y = 15: sample 0 before it, sample 1 after it and the higher of the two on it.

Kind 3.

Definition at line 61 of file terrain.c.

Referenced by Curve_StepAtSummedPosition().

◆ Curve_StepAtDifferencePosition()

s32 Curve_StepAtDifferencePosition ( const u8 * heightBytes,
s32 x,
s32 y )

A step across the diagonal x = y: sample 0 where y < x, sample 1 where y > x and the higher of the two on it.

Kind 4.

Definition at line 88 of file terrain.c.

Referenced by Curve_StepAtDifferencePosition().

◆ Curve_LerpThreeSamplesByRatio()

s32 Curve_LerpThreeSamplesByRatio ( const u8 * heightBytes,
s32 x,
s32 y )

A slope that bends on the diagonal x + y = 15: from sample 0 at the top left corner to sample 1 on the diagonal to sample 2 at the bottom right.

Kind 5.

Definition at line 118 of file terrain.c.

Referenced by Curve_LerpThreeSamplesByRatio().

◆ Curve_LerpThreeSamplesAtDifference()

s32 Curve_LerpThreeSamplesAtDifference ( const u8 * heightBytes,
s32 x,
s32 y )

A slope that bends on the diagonal x = y: from sample 0 at the top right corner to sample 1 on the diagonal to sample 2 at the bottom left.

Kind 6. It divides by 16 with a shift where kind 5 divides by 15, so its slopes are 15/16 as steep and it never reaches sample 2 exactly.

Definition at line 149 of file terrain.c.

Referenced by Curve_LerpThreeSamplesAtDifference().

◆ Curve_LerpTwoSamplesByTable()

s32 Curve_LerpTwoSamplesByTable ( const u8 * heightBytes,
s32 x,
s32 y )

A raised or sunken rounded square: sample 1 where gCurve_LerpWeightTable is 1, sample 0 in the corners.

Kind 7.

Definition at line 188 of file terrain.c.

Referenced by Curve_LerpTwoSamplesByTable().

◆ Curve_StepAtHalf()

s32 Curve_StepAtHalf ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

A step along x: sample 0 for x < 8, sample 1 from x = 8.

Kind 8.

Definition at line 201 of file terrain.c.

Referenced by Curve_StepAtHalf().

◆ Curve_StepAtHalfB()

s32 Curve_StepAtHalfB ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

A step along y: sample 0 for y < 8, sample 1 from y = 8.

Kind 9.

Definition at line 214 of file terrain.c.

Referenced by Curve_StepAtHalfB().

◆ Curve_LerpThreeSamplesAtHalf()

s32 Curve_LerpThreeSamplesAtHalf ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

A slope along x that bends at the middle: sample 0 at x = 0, sample 1 at x = 8, towards sample 2 at x = 16.

Kind 10.

Definition at line 228 of file terrain.c.

Referenced by Curve_LerpThreeSamplesAtHalf().

◆ Curve_LerpThreeSamplesAtHalfB()

s32 Curve_LerpThreeSamplesAtHalfB ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

A slope along y that bends at the middle, as kind 10 does along x.

Kind 11. Its samples are read unsigned: 128 to 255 are heights, where the other kinds read them as -128 to -1.

Definition at line 266 of file terrain.c.

Referenced by Curve_LerpThreeSamplesAtHalfB().

◆ Curve_LookupSampleByTable()

s32 Curve_LookupSampleByTable ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

The sample gCurve_SampleIndexTable picks for (x, y).

Kind 12.

Definition at line 300 of file terrain.c.

Referenced by Curve_LookupSampleByTable().

◆ Curve_LookupSampleByTableReversed()

s32 Curve_LookupSampleByTableReversed ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

The same, with the table mirrored left to right.

Kind 13.

Definition at line 311 of file terrain.c.

Referenced by Curve_LookupSampleByTableReversed().

◆ Curve_GetFirstSampleB()

s32 Curve_GetFirstSampleB ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

Flat, as Curve_GetFirstSampleA. Kind 14.

Definition at line 323 of file terrain.c.

Referenced by Curve_GetFirstSampleB().

◆ Curve_GetFirstSampleC()

s32 Curve_GetFirstSampleC ( const u8 * heightBytes,
s32 pixelX,
s32 pixelY )

Flat, as Curve_GetFirstSampleA. Kind 15.

Definition at line 329 of file terrain.c.

Referenced by Curve_GetFirstSampleC().

◆ Map_GetTerrainHeight()

s32 Map_GetTerrainHeight ( s32 layer,
s32 x,
s32 y )

Returns the ground height of a layer at (x, y) in 16.16 pixels: the height function of the cell's terrain kind applied to its samples.

y is the map's down axis, an object's z.

Called each frame by Object_UpdateAllThumb for objects that follow the terrain, by the player control, and as Map_GetTerrainHeightFar by the field scene's objects, the Psynergy effects and about ten scene overlays.

Definition at line 343 of file terrain.c.

Referenced by AltinPeak_AdvanceLog(), AltinPeak_RollLog(), BabiLighthouse_FindActorAhead(), ColossoLog_Initialize(), ColossoRiver_Initialize(), CrossboneIslePillarRooms_LowerActorToLedge(), Event_JumpOrPushRoom4Pillar(), Event_SettleRocks(), Event_SettleRocks(), Imil_RunIceSlide(), Imil_SlideActorAcrossIce(), KolimaForestBend_CreateLogs(), Map_GetTerrainHeight(), Object_MoveByKeys(), Object_UpdateAllThumb(), and TretTreeBranch_ShiftLeaderAndCameraByCells().

◆ Map_GetCellAttributeLowNibble()

s32 Map_GetCellAttributeLowNibble ( s32 layer,
s32 x,
s32 y )

Returns the terrain kind (the low four bits of the first collision byte) of a layer's cell at (x, y) in 16.16 pixels.

Called through Map_GetCellAttributeLowNibbleFar by the Psynergy Move effect.

Definition at line 376 of file terrain.c.

Referenced by Map_GetCellAttributeLowNibble().

◆ GetMapCellCollision()

s32 GetMapCellCollision ( s32 layer,
s32 x,
s32 y )

Returns the collision code of a layer's cell at (x, y) in 16.16 pixels.

The shift by 20 takes 16.16 pixels straight to 16-pixel cells. Called through GetMapCellCollisionFar by the Psynergy climbing and by scenes (Altin Peak, Colosso, Imil, Crossbone Isle).

Definition at line 401 of file terrain.c.

Referenced by AltinPeak_RollLog(), ColossoLogRollingStage_CheckObstacleDestination(), Event_MovePillar(), GetMapCellCollision(), Imil_RunIceSlide(), and Imil_SlideActorAcrossIce().

◆ SetMapCellCollision()

void SetMapCellCollision ( u32 layer,
s32 x,
s32 y,
u32 collisionCode )

Sets the collision code of a layer's cell at (x, y) in 16.16 pixels.

Writes the cell in the map's grid; does nothing before the map is set up. Called through SetMapCellCollisionFar by scenes (Colosso, Altin Peak, Crossbone Isle, Venus Lighthouse, Tolbi, Sol Sanctum).

Definition at line 422 of file terrain.c.

Referenced by AltinPeak_MarkActorTerrainCell(), ColossoLog_UpdateEastEndCollision(), Event_GrowPlant11(), Event_GrowPlant12(), Event_GrowPlant13(), Event_GrowPlant14(), Event_LiftCarryBlock(), Event_MoveCarryBlock(), Field_SetMapCellCollision(), and SetMapCellCollision().

◆ Map_GetCellHighFlags()

UNUSED s32 Map_GetCellHighFlags ( s32 x,
s32 y )

Returns the top two bits of the block word of the cell at pixel (x, y).

Definition at line 437 of file terrain.c.

Referenced by Map_GetCellHighFlags().

◆ Map_CheckObjectStep()

s32 Map_CheckObjectStep ( struct ObjectRuntime * object,
struct FieldPosition * position )

Returns whether an object can step to position: 2 when the cell there is a wall (collision code 0xff), 1 when the ground is more than 8 pixels higher than the object's, -1 when it is more than 12 pixels lower, else 0.

Called by the player control and the script commands that pick a free step, and as Map_CheckObjectStepFar by the Psynergy effects (Move, Carry, Force, climbing). A terrainId above 2 reads the grid from its start.

Definition at line 454 of file terrain.c.

Referenced by FieldObject_UpdatePlayerControl(), Map_CheckObjectStep(), Object_Wander(), and ScriptObject_WanderNearHome().

◆ CheckMapPositionCellOccupied()

s32 CheckMapPositionCellOccupied ( struct WorldPosition * position)

Returns 0 when the cell under position (its x, and y less its height) in layer 2 is a wall (collision code 0xff), else -1.

The name says the opposite of what it returns: 0 is the blocked answer. Called by the player control and, as CheckMapPositionCellOccupiedFar, by the Psynergy climbing.

Definition at line 506 of file terrain.c.

Referenced by CheckMapPositionCellOccupied(), and Object_MoveByKeys().

◆ GetWorldMapCollision()

s32 GetWorldMapCollision ( struct WorldPosition * position)

Returns the world map's collision value (1-15) at position: from the upper collision layer where it has one, else the lower, else 7.

Each layer is a tile map of 4-bit collision pixels.

Called by the player control, CheckWorldMapWalkable and GetWorldMapTerrainBehavior.

Definition at line 535 of file terrain.c.

Referenced by CheckWorldMapWalkable(), GetWorldMapCollision(), GetWorldMapTerrainBehavior(), and Object_MoveOnWorldMap().

◆ CheckWorldMapWalkable()

s32 CheckWorldMapWalkable ( s32 unused,
struct WorldPosition * position )

Returns 0 when the world map at position can be walked on (collision 5 to 12), else -1.

The first argument is not read; the player control passes its object. Called by the player control as it probes the steps around the object it moves.

Definition at line 586 of file terrain.c.

Referenced by CheckWorldMapWalkable(), and Object_MoveOnWorldMap().

◆ GetWorldMapTerrainBehavior()

s32 GetWorldMapTerrainBehavior ( struct WorldPosition * position,
s32 * terrainKind )

Returns the world map's terrain behaviour at position from gWorldMap_TerrainBehaviorTable, and stores the block's terrain kind in terrainKind.

Called through GetWorldMapTerrainBehaviorFar by the world map's encounters (Encounter_ResolveWorldMapZoneAndBackdrop).

Definition at line 599 of file terrain.c.

Referenced by GetWorldMapTerrainBehavior().