public LSL_Float llGround(LSL_Vector offset)
{
if (!ScriptProtection.CheckThreatLevel(ThreatLevel.None, "LSL", m_host, "LSL", m_itemID)) return new LSL_Float();
Vector3 pos = m_host.GetWorldPosition() + new Vector3((float)offset.x,
(float)offset.y,
(float)offset.z);
//Get the slope normal. This gives us the equation of the plane tangent to the slope.
LSL_Vector vsn = llGroundNormal(offset);
ITerrainChannel heightmap = World.RequestModuleInterface<ITerrainChannel>();
// Clamp to valid position
if (pos.X < 0)
pos.X = 0;
else if (pos.X >= heightmap.Width)
pos.X = heightmap.Width - 1;
if (pos.Y < 0)
pos.Y = 0;
else if (pos.Y >= heightmap.Height)
pos.Y = heightmap.Height - 1;
//Get the height for the integer coordinates from the Heightmap
float baseheight = heightmap[(int)pos.X, (int)pos.Y];
//Calculate the difference between the actual coordinates and the integer coordinates
float xdiff = pos.X - (int)pos.X;
float ydiff = pos.Y - (int)pos.Y;
//Use the equation of the tangent plane to adjust the height to account for slope
return (((vsn.x * xdiff) + (vsn.y * ydiff)) / (-1 * vsn.z)) + baseheight;
}