You are probably already familiar with the following example or something similar.
However, given the rate that historical data seems to deteriorate in these parts, I'll dump it anyway:

(note: I am not the author.
Modifications might have been made from original.)
Code:

var terrain_chunk = 0; // won't work in 6.40.5 unless you do this

// (Loopix-ShadowmapAmbient0)
// Terrain Brightness
// Credit appreciated...
// src author: Josh Miller (xXxGuitar511)
// tD arc date: 2006-06-09
// tD mod possible
// ver: 6.60, 6.40.5?

define TraceDist, 100; // Distance to scan down...
define FloorOffs, 10; // Height above entities feet to trace from
define IsTerrain, flag5; // Set this flag for all terrain entities
//define SkinNumber, 3; // Skin to scan for brightness (red channel)
define SkinNumber, 2;
define BrightScale,100; // Percentage (0 - 100) of brightness
define BrightOffs, 0; // Offset of brightness (from 0)

var ScanFrom[3];
var ScanTo[3];
var ScanDist;
var TargetX;
var TargetY;
var TargetPixel;
var PixelColor[3];
var BMapFormat;
bmap* TerrainSkin;

var scanFloorEnable = 0;

action terrain0 {
my.skin = SkinNumber;
my.IsTerrain = on;
}

function ScanFloor(); // *** Prototype function...



function ScanFloor()
{
vec_set(ScanFrom, my.x);
ScanFrom[2] += my.min_z + FloorOffs;
vec_set(ScanTo, ScanFrom);
ScanTo[2] -= TraceDist;
ScanDist = c_trace(ScanFrom, ScanTo, ignore_me | ignore_passable | ignore_passents);
//trace_mode = ignore_me + ignore_passable + ignore_passents;
//ScanDist = trace(ScanFrom, ScanTo);
if (ScanDist == 0)
{ScanDist = TraceDist;}
if (you != null) {
if (you.IsTerrain == on) {
// Is a terrain
TerrainSkin = bmap_for_entity(you, SkinNumber);
BMapFormat = bmap_lock(TerrainSkin, 0);
if (TerrainSkin == 0) { goto(NoTerrain); }
vec_for_vertex(ScanFrom, you, 1);
vec_for_vertex(ScanTo, you, ent_vertices(you));
TargetX = (my.x - ScanFrom.x) / (ScanTo.x - ScanFrom.x) * bmap_width(TerrainSkin);
TargetY = (my.y - ScanFrom.y) / (ScanTo.y - ScanFrom.y) * bmap_height(TerrainSkin);
TargetX = clamp(int(TargetX), 0, bmap_width(TerrainSkin));
TargetY = clamp(int(TargetY), 0, bmap_height(TerrainSkin));
TargetPixel = pixel_for_bmap(TerrainSkin, TargetX, TargetY);
if (TargetPixel == -1)
{goto(NoTerrain);}
pixel_to_vec(PixelColor, null, BMapFormat, TargetPixel);
bmap_unlock(TerrainSkin);
my.ambient = clamp((PixelColor.blue / 255 * BrightScale) + BrightOffs, 0, 100);
//my.ambient = (PixelColor.red + PixelColor.green + PixelColor.blue) / 765 * 100;
}
}
//
NoTerrain:
//
return(ScanDist);
}