level.c

This include file contains often used, small level related functions, such as terrain tiling, placing entities on terrain, and generating an endless terrain level. The functions are simple enough to understand them at a glance. Examples of using them can be found in the infinite_terrain.c and weather.c samples. If you want to modify them, please edit a level.c copy in your work folder, and not the original file in the include folder.

level_loadendless (STRING* filename)

Loads a level that infinitely repeats in all directions. For this, 8 additional views are created that render the continuation of the level in 8 directions, plus one extra view for the sky. The 9 views are updated from the camera view. The camera clip_far parameter is set to the level size. When the camera crosses a level border, it is automatically placed back to the opposite border of the level, in a way that the user won't notice it.

Parameters:

filename - STRING* or char*; name of the map, model, or terrain file.

Example:

function main()
{
  level_loadendless("terrain.hmp"); 
  terrain_tile(level_ent);
ent_createlayer("skycube+6.dds",SKY|CUBE|SHOW, 0); def_move(); // default movement from default.c }

 

level_loadsplash (STRING* filename,STRING* bmapname,var mode)

Loads a level while displaying a splash screen. The splash screen is displayed for 2 seconds and then fades out.

Parameters:

filename - STRING* or char*; name of the level map, model, or terrain file.
bmapname - STRING* or char*; name of the splash screen bitmap. When its size is different to the screen size, it is stretched.
mode - for loading bars, switching off the fading, or the like. Not used yet.

Remarks:

Example:

function main()
{
  level_loadsplash("racetrack.wmb","racesplash.pcx",0); 
  tcar_race_start();
}

 

terrain_tile ( ENTITY* terrain)

Makes the terrain entity tilable by adjusting all border vertices to the same height. This is required for level_loadendless() when the terrain does not tile by itself.

Parameters:

terrain - terrain entity pointer.
 

terrain_fence ( ENTITY* terrain, var height)

Erects a border of the given height around the terrain entity by bending its edges upwards. Can be used to avoid that people fall over the edge of the world.

Parameters:

terrain - terrain entity pointer.
height - height of the border.
 

terrain_raise ( ENTITY* terrain, var height)

Adds a height to all terrain vertices.

Parameters:

terrain - terrain entity pointer.
height - z distance to be added.

Remarks:

When physics is used, re-register the terrain entity after deformation with PH_POLY | PH_MODIFIED. Otherwise the physics collision detection will still use the original terrain shape.

 

ent_terrain_place ( ENTITY* ent, ENTITY* terrain, var minheight, var maxheight)

Places an entity on a random position on terrain, within a certain height range.

Parameters:

ent - entity to be placed.
terrain - terrain entity pointer, or NULL for a terrain level.
minheight - place entity above this height, in percent of the terrain height (0..100).
maxheight - place entity below this height, in percent of the terrain height (0..100).

Remarks:

Example:

action place_me()
{
  vec_scale(my.scale_x,0.5+random(1.5));
  ent_terrain_place(me,level_ent,50,90);
}
 
function main()
{
  level_load("terrain.hmp"); 
  int i;
  for (i=0; i < 500; i++)
    ent_create("tree.mdl",NULL,place_me);
}	 

ent_seed (char* name, ENTITY* terrain, BMAP* mask, COLOR* color, var dist, var mode, EVENT act)

ent_seed (char* name, ENTITY* terrain, BMAP* mask, COLOR* color, var dist, EVENT act)

Creates a number of model or sprite entities on terrain, using a seed mask.

Parameters:

name - name of the model or sprite, char* or STRING*.
terrain - terrain entity pointer, or NULL for a terrain level.
mask - seed mask pointer, or NULL for the first terrain skin.
color - seed color vector. The entities are created at the positions where the seed color is the same as the mask color.
dist - distance factor, determines the average distance between entities. A smaller distance factor creates a higher number of entities.
mode - 0 = use terrain vertices for placement, 1 = use c_trace (slower but more precise), 2 = let entity face slope direction, 4 = adjust entity to slope angle. A8.11
act - entity action, or NULL for no action.

Returns:

Number of created entities

Modifies:

entity.skill61..skill63 - set to the floor normal when mode > 0.

Remarks:

Example:

#define COLOR_TREE vector(0,255,0)
#define COLOR_GRASS vector(255,255,0)
 
action place_tree()
{
  vec_scale(my.scale_x,1+random(0.5));
  my.eflags |= CLIP1;   // clip away at 50% LOD range
  wait(1);   // wait until it's placed by ent_seed
  my.z -= 5; // place it a little lower
  my.pan = random(360);
  my.tilt = random(20)-10;
  wait(1); // wait 1 frame after the last change, 
  my.emask &= ~DYNAMIC; // then make entity static for better performance
}

action place_grass()
{
  vec_scale(my.scale_x,0.75+random(0.5));
  my.flags |= PASSABLE | TRANSLUCENT;
  my.alpha = 70;
  my.eflags |= CLIP1; 
  wait(1);
  my.emask &= ~DYNAMIC;
}

function main()
{
  max_entities = 10000;
  level_load("terrain.hmp");
  BMAP* bSkin = bmap_create("terrain_vegetation.pcx");
  ent_seed("tree.mdl",level_ent,bSkin,COLOR_TREE,50,0,place_tree);
	wait(1); // let the place_tree actions terminate before creating more entities
  collision_mode = 0; // faster creation for PASSABLE entities
  ent_seed("grass.dds",level_ent,bSkin,COLOR_GRASS,30,2,place_grass);
  collision_mode = 1;
}

 

terrain_height ( ENTITY* terrain, VECTOR* position)

Calculates the height of the next terrain vertex at the given position. This function is less accurate, but faster than a c_trace call.

Parameters:

terrain - terrain entity pointer; must be either chunked terrain, or the level entity of a terrain level.
position - the xy position on the terrain.

Returns:

Z value of the terrain vertex next to the given xy coordinates.
 

terrain_pos( ENTITY* terrain,var x,var y,var z): VECTOR*

Returns a position above terrain ground for a given x y z position. Use this function for easier placing entities on terrain.

Parameters:

terrain - terrain entity pointer, or NULL for the level entity of a terrain level.
x, y - the x y position on the terrain
z - the vertical distance to the terrain surface.

Returns:

VECTOR* - temporary vector to be used as a position above terrain ground.

Example:

function main()
{
  level_load("terrain.hmp"); 
  ent_create("tree.mdl",terrain_pos(NULL,100,100,-10),NULL); // plant the tree 10 units in the ground, at position (100,100). 
}

 

terrain_getpixel(ENTITY* terrain, var x, var y, BMAP* skin): COLOR*

Returns the color of a terrain texture at the given x y world position.

Parameters:

terrain - terrain entity pointer, or NULL for the level entity of a terrain level.
x, y - the x y position on the terrain
skin - the texture pointer, or NULL for the first terrain skin. It must be a 16-, 24-, or 32-bit texture (TGA, PCX, or BMP).

Returns:

COLOR* - temporary BGR color vector containing the texture color at the given position, or NULL when the position is outside the terrain.

Example: See samples\weather.c


 

terrain_setpixel(ENTITY* terrain, var x, var y, BMAP* skin, COLOR* color)

Paints a terrain texture pixel at the given x y world position with a given color.

Parameters:

terrain - terrain entity pointer, or NULL for the level entity of a terrain level.
x, y - the x y position on the terrain.
COLOR* - the BGR pixel color to be painted.
skin - the texture pointer, or NULL for the first terrain skin. It must be a 16-, 24-, or 32-bit texture (TGA, PCX, or BMP).

Remarks:

When mipmaps are used, call bmap_to_mipmap afterwards for updating the higher mipmap levels of the texture.

Example: See samples\weather.c

 

See Also:

Terrain, ent_createterrain, level_load

► latest version online