////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Special thanks go to Loopix for the grass models and to Nifty for the idea
// 1) Include svegetation.wdl in your project
// 2) Place a terrain entity inside the level and attach it the action named my_terrain. Set its skill1 and skill2 values.
// 3) Export the skin of the terrain and paint the areas where the grass and the trees are supposed to appear in green (R G B = 0 255 0)
// 4) Save the resulting file as colormap.tga and copy it inside your game folder. That's it!
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var skin_size_x; // the size of the skin (and the color map) in pixels
var skin_size_y; // on the x and y axis
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
STRING grass1_mdl = "gramacskins.mdl";
STRING grass2_mdl = "gramacskins.mdl";
STRING tree1_mdl = "t3.mdl";
STRING colormap_tga = "teste.bmp";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BMAP* vegetation_map;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ENTITY* terrain1;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function place_grass();
function place_trees();
function color_map();
function weaving_grass();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define grass_area skill1 // the size of the grassy area that surrounds the player, set it in Wed
#define grass_dist skill2 // the distance between two consecutive grass models, set it in Wed
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// uses grass_area, grass_dist
function generate_grass();
action my_terrain()
{
if (my.grass_area == 0) {my.grass_area = 500;} // default grass_area value
if (my.grass_dist == 0) {my.grass_dist = 50;} // default grass_dist value
terrain1 = my;
my.push = 3; // don't allow the grass and tree models to penetrate the terrain
generate_grass();
}
function generate_grass()
{
max_entities = 10000;
wait (3); // wait until the level is loaded
var grass_pos[3] = {0,0,0};
var grass_id = 1;
var grass_coords[3] = {0,0,0};
ent_create (colormap_tga, nullvector, color_map); // create the color map entity
// wait until the player, the terrain and the color map appear in the level
while ((player == NULL) || (terrain1 == NULL) || (vegetation_map == NULL)) {wait (1);}
grass_pos[1] = player.x - terrain1.grass_area; // set the initial position of the grass models
grass_pos[2] = player.y - terrain1.grass_area; // surround the player with the grass models
grass_pos[3] = terrain1.max_z + 400; // and place them up high in the sky
grass_coords[1] = grass_pos[1];
grass_coords[2] = grass_pos[2];
while (grass_coords[1] < player.x + terrain1.grass_area)
{
grass_pos[2] = grass_coords[2] + (random(0.2 * terrain1.grass_dist) - 0.4 * terrain1.grass_dist);
grass_pos[1] = grass_coords[1] + (random(0.2 * terrain1.grass_dist) - 0.4 * terrain1.grass_dist);
if (random (1) > 0.1)
{
you = ent_create (grass1_mdl, grass_pos[1], place_grass);
}
else
{
you = ent_create (grass2_mdl, grass_pos[1], place_grass);
}
// we can spread the cpu load over 10 consecutive frames by giving every 10th model a different id (skill1 value)
you.skill1 = grass_id; // used for further optimizations (not used in this demo)
grass_id %= 10;
grass_id += 1;
grass_coords[2] += terrain1.grass_dist;
if (grass_coords[2] > (player.y + terrain1.grass_area))
{
grass_coords[1] += terrain1.grass_dist;
grass_coords[2] = player.y - terrain1.grass_area;
}
if (random (1) > 0.99) // play with 0.99 to adjust the number of trees in the level
{
grass_pos[1] += 20 - random(40); // don't plant the tree in the same spot with a grass model
grass_pos[2] += 20 - random(40);
ent_create (tree1_mdl, grass_pos[1], place_trees);
}
}
}
function color_map()
{
set (my,PASSABLE);
reset (my,VISIBLE);
vegetation_map = bmap_for_entity (my, 0);
skin_size_x = bmap_width (vegetation_map); // get the size of the color map (same with the skin of the terrain)
skin_size_y = bmap_height (vegetation_map); // on the x and y axis
}
function place_grass()
{
var pixel_color[3];
var format;
var pixel;
var coords_x;
var coords_y;
var temp[3];
my.scale_x += 0.1 - random(2) / 10;
my.scale_y = my.scale_x;
my.scale_z = my.scale_x;
reset (my,VISIBLE);
set (my,PASSABLE);
set (my,TRANSLUCENT);
my.push = 2;
weaving_grass();
while (player != NULL)
{
temp[1] = random(10);
wait (temp); // spread the cpu load over 10 consecutive frames
my.alpha = vec_dist(player.x, my.x); // play with 10000
my.x = cycle(my.x, camera.x - terrain1.grass_area, camera.x + terrain1.grass_area); // wrap the grass around player's position
my.y = cycle(my.y, camera.y - terrain1.grass_area, camera.y + terrain1.grass_area); // on the x and y axis
my.z = terrain1.max_z + 400; // and place it up high in the sky
vec_set(temp[1], my.x);
temp[3] -= 10000;
my.skill10 = c_trace (my.x, temp[1], IGNORE_SPRITES | IGNORE_PASSABLE | IGNORE_PUSH);
// the tracing ray has hit the terrain? (not the surrounding hollowed cube)
if ((trace_hit != 0) && (you != NULL))
{
coords_x = (my.x - terrain1.min_x) / ((terrain1.max_x - terrain1.min_x) / skin_size_x); // get the coordinates of the current pixel
coords_y = (terrain1.max_y - my.y) / ((terrain1.max_y - terrain1.min_y) / skin_size_y); // on x and y
format = bmap_lock (vegetation_map, 0);
pixel = pixel_for_bmap(vegetation_map, coords_x, coords_y);
pixel_to_vec (pixel_color, NULL, format, pixel); // store the color of the pixel in pixel_color
bmap_unlock (vegetation_map);
// detected a green area on the color map bitmap?
if (pixel_color[2] == 255)
{
my.z -= my.skill10 - 1; // then place a grass model on the ground (skill10 was used to store the result of the c_trace operation)
vec_to_angle (my.pan, normal); // and align it properly
my.tilt -= 90;
reset (my,VISIBLE); // make it visible
}
else // this isn't a green pixel on the color map?
{
set (my,VISIBLE); // then hide the grass model (for now)
}
}
wait (1);
}
}
function place_trees()
{
var pixel_color[3];
var format;
var pixel;
var coords_x;
var coords_y;
var temp[3];
my.pan = random(360);
my.scale_x += 0.2 - random(4) / 10;
my.scale_y = my.scale_x;
my.scale_z = my.scale_x;
reset (my,VISIBLE);
set (my,TRANSLUCENT);
set (my,PASSABLE);
my.push = 1;
var player1_pos[3];
var player2_pos[3];
while (player != NULL)
{
// update the trees only at game start of if the player has moved
if ((total_frames < 20) || (vec_dist (player1_pos[1], player2_pos[1]) != 0))
{
my.alpha = (vec_dist(player.x, my.x)); // play with 20000
my.x = cycle(my.x, camera.x - terrain1.grass_area, camera.x + terrain1.grass_area);
my.y = cycle(my.y, camera.y - terrain1.grass_area, camera.y + terrain1.grass_area);
my.z = terrain1.max_z + 400;
vec_set(temp[1], my.x);
temp[3] -= 10000;
my.skill10 = c_trace (my.x, temp[1], IGNORE_ME | IGNORE_SPRITES | IGNORE_PASSABLE | IGNORE_PUSH);
// the tracing ray has hit the terrain? (not the surrounding hollowed cube)
if ((trace_hit != 0) && (you != NULL))
{
coords_x = (my.x - terrain1.min_x) / ((terrain1.max_x - terrain1.min_x) / skin_size_x);
coords_y = (terrain1.max_y - my.y) / ((terrain1.max_y - terrain1.min_y) / skin_size_y);
format = bmap_lock (vegetation_map, 0);
pixel = pixel_for_bmap(vegetation_map, coords_x, coords_y);
pixel_to_vec (pixel_color, NULL, format, pixel); // store the color of the pixel in pixel_color
bmap_unlock (vegetation_map);
if (pixel_color[2] == 255) // got a green pixel here?
{
my.z -= my.skill10 - 1; // place a grass model on the ground (skill10 was used to store the result of the c_trace operation)
reset (my,VISIBLE);
reset (my,PASSABLE); // comment this line if you want to make the trees passable
}
else
{
set (my,VISIBLE);
}
}
}
vec_set (player1_pos[1], player.x); // use these 2 vars to detect if the player has moved or not
wait (1);
vec_set (player2_pos[1], player.x); // in the last frame
}
}
function weaving_grass()
{
var grass_angles;
var grass_speed;
grass_speed = 2 + random(5);
while (1)
{
grass_angles += grass_speed * time_step; // allow the grass to weave
my.roll += 0.02 * sin(grass_angles);
wait (1);
}
}