# Class StreamingTerrain

java.lang.Object
com.codename1.gaming.level.StreamingTerrain

All Implemented Interfaces:
  `Terrain`

public class StreamingTerrain extends Object implements Terrain
An unbounded `Terrain` that pages `TerrainChunk`s in and out through a `ChunkProvider`, keeping at most `#getCacheSize()` chunks resident (LRU). World cell coordinates may be negative; each maps to a chunk and a local cell. Call `#streamAround(int,int,int)` as the player moves to pre-load nearby chunks and evict (saving dirty) far ones.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static final class`
  `StreamingTerrain.MemoryChunkProvider`
  A trivial in-memory provider: remembers chunks it has seen, generates empty flat-grass chunks otherwise.

- ## Field Summary

  ### Fields inherited from interface Terrain

  `NO_GROUND`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `StreamingTerrain()`
  `StreamingTerrain(ChunkProvider provider, int cacheSize)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addFeature(TerrainFeature feature)`
  Adds a feature, routing it to the chunk that owns its position.
  `TerrainChunk`
  `chunk(int cx, int cz)`
  Returns the resident chunk for the given chunk coords, loading it on demand.
  `List<TerrainFeature>`
  `features()`
  Features whose anchor cell is currently loaded.
  `void`
  `flush()`
  Persists every dirty resident chunk through the provider.
  `int`
  `getCacheSize()`
  `int`
  `getCols()`
  Column count for a bounded terrain, or -1 if unbounded.
  `float`
  `getHeight(int x, int z)`
  `String`
  `getMaterial(int x, int z)`
  Material id at the cell (resolve via `MaterialRegistry`).
  `ChunkProvider`
  `getProvider()`
  `int`
  `getRows()`
  Row count for a bounded terrain, or -1 if unbounded.
  `boolean`
  `hasGround(int x, int z)`
  `boolean`
  `isBounded()`
  True for a fixed-size terrain (`#getCols()`/`#getRows()` valid), false if unbounded.
  `int`
  `loadedChunkCount()`
  `List<TerrainChunk>`
  `residentChunks()`
  The currently-resident chunks (snapshot) -- used to persist a small region inline.
  `StreamingTerrain`
  `setCacheSize(int cacheSize)`
  `void`
  `setHeight(int x, int z, float height)`
  `void`
  `setMaterial(int x, int z, String materialId)`
  `void`
  `streamAround(int cellX, int cellZ, int radius)`
  Pre-loads the (2*radius+1)^2 chunks around the given cell so movement is seamless; the LRU cache evicts (and saves) chunks that fall out of range.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### StreamingTerrain

    public StreamingTerrain()

  - ### StreamingTerrain

    public StreamingTerrain(ChunkProvider provider, int cacheSize)

- ## Method Details

  - ### getCacheSize

    public int getCacheSize()

  - ### setCacheSize

    public StreamingTerrain setCacheSize(int cacheSize)

  - ### getProvider

    public ChunkProvider getProvider()

  - ### chunk

    public TerrainChunk chunk(int cx, int cz)
    Returns the resident chunk for the given chunk coords, loading it on demand.

  - ### getHeight

    public float getHeight(int x, int z)
    Specified by:
      `getHeight` in interface `Terrain`

  - ### setHeight

    public void setHeight(int x, int z, float height)
    Specified by:
      `setHeight` in interface `Terrain`

  - ### hasGround

    public boolean hasGround(int x, int z)
    Specified by:
      `hasGround` in interface `Terrain`

  - ### getMaterial

    public String getMaterial(int x, int z)
    Description copied from interface: `Terrain`
    Material id at the cell (resolve via `MaterialRegistry`).
    Specified by:
      `getMaterial` in interface `Terrain`

  - ### setMaterial

    public void setMaterial(int x, int z, String materialId)
    Specified by:
      `setMaterial` in interface `Terrain`

  - ### features

    public List<TerrainFeature> features()
    Description copied from interface: `Terrain`
    Features whose anchor cell is currently loaded.
    Specified by:
      `features` in interface `Terrain`

  - ### addFeature

    public void addFeature(TerrainFeature feature)
    Description copied from interface: `Terrain`
    Adds a feature, routing it to the chunk that owns its position.
    Specified by:
      `addFeature` in interface `Terrain`

  - ### isBounded

    public boolean isBounded()
    Description copied from interface: `Terrain`
    True for a fixed-size terrain (`#getCols()`/`#getRows()` valid), false if unbounded.
    Specified by:
      `isBounded` in interface `Terrain`

  - ### getCols

    public int getCols()
    Description copied from interface: `Terrain`
    Column count for a bounded terrain, or -1 if unbounded.
    Specified by:
      `getCols` in interface `Terrain`

  - ### getRows

    public int getRows()
    Description copied from interface: `Terrain`
    Row count for a bounded terrain, or -1 if unbounded.
    Specified by:
      `getRows` in interface `Terrain`

  - ### streamAround

    public void streamAround(int cellX, int cellZ, int radius)
    Pre-loads the (2*radius+1)^2 chunks around the given cell so movement is seamless; the LRU cache evicts (and saves) chunks that fall out of range.

  - ### flush

    public void flush()
    Persists every dirty resident chunk through the provider.

  - ### loadedChunkCount

    public int loadedChunkCount()

  - ### residentChunks

    public List<TerrainChunk> residentChunks()
    The currently-resident chunks (snapshot) -- used to persist a small region inline.