Sorcerer and I modified the script that we came up with a few days ago to work on a level with any number of terrains stitched together using something like HMPsplit. This way you can drive your car from one terrain to the next and it will read the color info from whatever terrain you're over. This code assumes you're over a terrain at all times. I don't know what it'd do if you drove off the terrains onto blocks or something. Just a side note - I've left some of the variables as global ones just so I could put them on the screen in a panel for debugging purposes. You can throw many of them back into their local functions. I've commented the crap out of the thing so you should be able to make sense of most of it. Enjoy!
Code:
bmap* colorMap_skin;
sound sndOutOfRoad = <HighPitch.wav>;
var store_pixel;
var pixel_colorAl[3];
var WaitForTerrain = 0;
var TerrainWidth;
var TerrainVert1X; //x-coordinate of 1st vertex of terrain
var TerrainVert1Y; //y-coordinate of 1st vertex of terrain
var NumberOfVerts; //total number of vertices in terrain
var RoadMapTexture; //pointer to the terrain's texture
var CurrentTerrain;
var DeltaX; //horizontal distance from vehicle entity to left side of terrain
var DeltaY; //vertical distance from vehicle entity to top of terrain
var c;
var d;
var pixel_Value;
string TerrainName;
string testString;
var format;
entity* plCar01_entity;
entity* myter;
font lcd_20, <ackfont.pcx>, 6,9;
Action Terrain // Attach to each terrain you'll be scanning.
{
TerrainVert1X = 1; //Initial settings to avoid crash
TerrainVert1Y = 1;
TerrainWidth = 100;
myter = my;
RoadMapTexture = bmap_for_entity(my,1); //change the digit to look for your desired skin number
while(1)
{
if(my == CurrentTerrain)
{
myter = my;
RoadMapTexture = bmap_for_entity(my,1); //change the digit to look for your desired skin number
//str_for_entname(teststring,myter); // you can remove the comment marks
//msg_show(teststring,2); //to show on scren what terrain you're over
vec_for_vertex(myter.skill50,myter,1);
vec_for_vertex(myter.skill53,myter,ent_vertices(me));
TerrainVert1X = myter.skill50; // (50,51,52) hold (x,y,z) for vertex 1
TerrainVert1Y = myter.skill51;
TerrainWidth = (myter.skill53 - TerrainVert1X); // (53,54,55) hold (x,y,z) for last vertex
//NumberOfVerts = ent_vertices(me); //just a check for curiosity's sake
WaitForTerrain = 1;
}
else
{
WaitForTerrain = 0;
sleep(2); //if you have a lot of terrains and get a crash on game-start, increase the sleep value (this works for my level with 121 terrains)
my = CurrentTerrain; // by now terrains should have loaded, safe to set my to the terrain under the vehicle
}
wait(1);
}
}
function get_pixel(a)
{
if(WaitForTerrain == 0){return(-1);}
var SkinSize = 256; // Give this var the size of the skin that you are going to use.
var pixel_Value;
DeltaX = plCar01_entity.X - TerrainVert1X;
DeltaY = TerrainVert1Y - plCar01_entity.Y;
c = INT(SkinSize*(DeltaX/TerrainWidth));
d = INT(SkinSize*(DeltaY/TerrainWidth));
var b;
colorMap_skin = bmap_for_entity(myter,a); //gets a bmap pointer to "myter's" texture #a (a is passed from "SoundOfRoad" function)
b = bmap_lock(colorMap_skin,0); // lock the skin so you can do the next command
pixel_Value = pixel_for_bmap(colorMap_skin, c, d); //reads the pixel number from the x(c), y(d) position
bmap_unlock(colorMap_skin); //unlock the skin
return(pixel_Value);
}
}
function SoundOfRoad() //Call this function from your vehicle
{
var store_pixel;
pixel_colorAl[0] = 0; //Blue
pixel_colorAl[1] = 0; //Green
pixel_colorAl[2] = 0; //Red
store_pixel = 0;
while(plCar01_entity == null) { wait(1); }
while(plCar01_entity != null)
{
vec_set (temp, plCar01_entity.x);
temp.z -= 5000;
trace_mode = ignore_me + IGNORE_PASSABLE + IGNORE_MODELS + IGNORE_SPRITES;
trace (my.x,temp);
CurrentTerrain = you;
if(CurrentTerrain == NULL){goto(JumpLabel);}
store_pixel = get_pixel(1);
if(store_pixel == -1){goto(JumpLabel);}
format = bmap_lock(RoadMapTexture,0);
pixel_to_vec(pixel_colorAl,NULL,format,store_pixel); //gets the color from the pixel located at (c,d)
bmap_unlock(RoadMapTexture);
//Centerline Sound (check for red) - (I have a red stripe down the center of my road to tell when the user crossed into oncoming traffic)
if(pixel_colorAl[0] == 0 && pixel_colorAl[1] < 100 && pixel_colorAl[2] > 200)
{
snd_play(sndOutOfRoad,100,0);
}
//Off-Road Sound (check for blue)
if(pixel_colorAl[0] > 200 && pixel_colorAl[1] == 0 && pixel_colorAl[2] == 0)
{
snd_play(sndOutOfRoad,100,0);
}
JumpLabel:
wait(1);
}
}