function paintSpriteOnTerrain( ENTITY* terrainEnt , BMAP* sourceSpriteBmap , VECTOR* posVec , spriteAngle , spriteScale ) {
if( !terrainEnt || !sourceSpriteBmap ) { return; }
if( spriteScale == 0 ) { spriteScale = 1; }
var sWidth = bmap_width(sourceSpriteBmap) * spriteScale;
var sHeight = bmap_height(sourceSpriteBmap) * spriteScale;
// Create the spriteBmap pointer global and create a bitmap for it one time only
// Blit each time the sprite is used
// to save some run-time...
BMAP* spriteBmap = bmap_createblack( sWidth, sHeight, bmap_format(sourceSpriteBmap) );
bmap_blit( spriteBmap, sourceSpriteBmap, NULL , vector(sWidth, sHeight, 0) );
spriteAngle *= -1;
if( spriteAngle < -180 ) { spriteAngle += 360; }
if( spriteAngle > 180 ) { spriteAngle -= 360; }
var bmapHandle2 = bmap_lock(spriteBmap , 0);
if( bmapHandle2 == 0 ) { return; }
var rotCosine = cosv( spriteAngle );
var rotSine = sinv( spriteAngle );
VECTOR destPoint[4];
vec_zero( destPoint[0] );
destPoint[1].x = (-sHeight*rotSine);
destPoint[1].y = (sHeight*rotCosine);
destPoint[2].x = (sWidth*rotCosine-sHeight*rotSine);
destPoint[2].y = (sHeight*rotCosine+sWidth*rotSine);
destPoint[3].x = (sWidth*rotCosine);
destPoint[3].y = (sWidth*rotSine);
var minx = minv(destPoint[0].x,minv(destPoint[1].x,minv(destPoint[2].x,destPoint[3].x)));
var miny = minv(destPoint[0].y,minv(destPoint[1].y,minv(destPoint[2].y,destPoint[3].y)));
var maxx = maxv(destPoint[0].x,maxv(destPoint[1].x,maxv(destPoint[2].x,destPoint[3].x)));
var maxy = maxv(destPoint[0].y,maxv(destPoint[1].y,maxv(destPoint[2].y,destPoint[3].y)));
int nWidth=(int)ceil(abs(maxx)-minx);
int nHeight=(int)ceil(abs(maxy)-miny);
BMAP* terrainSkin = bmap_for_entity(terrainEnt,0);
if( terrainSkin == NULL ) { return; }
var dWidth = bmap_width( terrainSkin );
var dHeight = bmap_height( terrainSkin );
var bmapHandle = bmap_lock(terrainSkin , 0);
if( bmapHandle == 0 ) { return; }
posVec.x -= integer(nWidth / 2);
posVec.y -= integer(nHeight / 2);
if( posVec.x < 0 ) { posVec.x = 0; }
if( posVec.y < 0 ) { posVec.y = 0; }
if( posVec.x > dWidth - nWidth ) { posVec.x = dWidth - nWidth; }
if( posVec.y > dHeight - nHeight ) { posVec.y = dHeight - nHeight; }
COLOR terrainColor;
var terrainAlpha;
COLOR finalColor;
VECTOR sourcePixelPos;
COLOR currentPixelColor;
var currentPixelAlpha;
var i , j;
for( j = 0; j < nHeight ; j++ ) {
for( i = 0; i < nWidth; i++ ) {
sourcePixelPos.x = integer( (i + minx) * rotCosine + (j + miny) * rotSine);
sourcePixelPos.y = integer( (j + miny) * rotCosine - (i + minx) * rotSine);
if( sourcePixelPos.x >= 0 && sourcePixelPos.x < sWidth && sourcePixelPos.y >= 0 && sourcePixelPos.y < sHeight ) {
pixel_to_vec( currentPixelColor , currentPixelAlpha , bmapHandle2 , pixel_for_bmap( spriteBmap , sourcePixelPos.x , sourcePixelPos.y ) );
if( currentPixelAlpha > 0 ) {
pixel_to_vec( terrainColor , terrainAlpha , bmapHandle , pixel_for_bmap( terrainSkin , posVec.x + i , posVec.y + j ) );
vec_lerp ( finalColor, terrainColor, currentPixelColor , currentPixelAlpha/100 );
pixel_to_bmap( terrainSkin , posVec.x + i , posVec.y + j , pixel_for_vec( finalColor , 100 , bmapHandle) );
}
}
}
}
bmap_unlock(terrainSkin);
bmap_unlock(spriteBmap);
bmap_remove( spriteBmap );
}