2022-11-13 00:15:17 +00:00
/// <reference path="../../udbscript.d.ts" />
2022-01-06 17:46:37 +00:00
` #version 4 ` ;
2021-11-28 13:00:24 +00:00
` #name Randomize Texture Offsets ` ;
` #description Randomized texture offsets. Distinct upper, middle, and lower offsets only work if the game configuration supports those local offsets. ` ;
` #scriptoptions
global _x
{
description = "Global X Offset" ;
default = "False" ;
type = 3 ; // Boolean
}
global _y
{
description = "Global Y Offset" ;
default = "False" ;
type = 3 ; // Boolean
}
upper _x
{
description = "Upper X Offset" ;
default = "True" ;
type = 3 ; // Boolean
}
upper _y
{
description = "Upper Y Offset" ;
default = "True" ;
type = 3 ; // Boolean
}
middle _x
{
description = "Middle X Offset" ;
default = "True" ;
type = 3 ; // Boolean
}
middle _y
{
description = "Middle Y Offset" ;
default = "True" ;
type = 3 ; // Boolean
}
lower _x
{
description = "Lower X Offset" ;
default = "True" ;
type = 3 ; // Boolean
}
lower _y
{
description = "Lower Y Offset" ;
default = "True" ;
type = 3 ; // Boolean
}
` ;
// Gets a random number
function getRandomOffset ( max )
{
return Math . floor ( Math . random ( ) * max ) ;
}
// Checks if the given name is a proper texture name and if the texture exists
function isValidTexture ( texture )
{
2022-01-06 17:46:37 +00:00
return texture != '-' && UDB . Data . textureExists ( texture ) ;
2021-11-28 13:00:24 +00:00
}
function randomizeSidedefOffsets ( sd )
{
// Global X texture offset
2022-01-06 17:46:37 +00:00
if ( UDB . ScriptOptions . global _x && ( isValidTexture ( sd . upperTexture ) || isValidTexture ( sd . middleTexture ) || isValidTexture ( sd . lowerTexture ) ) )
2021-11-28 13:00:24 +00:00
{
let widths = [ ] ;
if ( isValidTexture ( sd . upperTexture ) )
2022-01-06 17:46:37 +00:00
widths . push ( UDB . Data . getTextureInfo ( sd . upperTexture ) . width ) ;
2021-11-28 13:00:24 +00:00
if ( isValidTexture ( sd . middleTexture ) )
2022-01-06 17:46:37 +00:00
widths . push ( UDB . Data . getTextureInfo ( sd . middleTexture ) . width ) ;
2021-11-28 13:00:24 +00:00
if ( isValidTexture ( sd . lowerTexture ) )
2022-01-06 17:46:37 +00:00
widths . push ( UDB . Data . getTextureInfo ( sd . lowerTexture ) . width ) ;
2021-11-28 13:00:24 +00:00
if ( widths . length > 0 )
sd . offsetX = getRandomOffset ( Math . max ( widths ) ) ;
}
// Global Y texture offset
2022-01-06 17:46:37 +00:00
if ( UDB . ScriptOptions . global _y && ( isValidTexture ( sd . upperTexture ) || isValidTexture ( sd . middleTexture ) || isValidTexture ( sd . lowerTexture ) ) )
2021-11-28 13:00:24 +00:00
{
let heights = [ ] ;
if ( isValidTexture ( sd . upperTexture ) )
2022-01-06 17:46:37 +00:00
heights . push ( UDB . Data . getTextureInfo ( sd . upperTexture ) . height ) ;
2021-11-28 13:00:24 +00:00
if ( isValidTexture ( sd . middleTexture ) )
2022-01-06 17:46:37 +00:00
heights . push ( UDB . Data . getTextureInfo ( sd . middleTexture ) . height ) ;
2021-11-28 13:00:24 +00:00
if ( isValidTexture ( sd . lowerTexture ) )
2022-01-06 17:46:37 +00:00
heights . push ( UDB . Data . getTextureInfo ( sd . lowerTexture ) . height ) ;
2021-11-28 13:00:24 +00:00
if ( heights . length > 0 )
sd . offsetY = getRandomOffset ( Math . max ( heights ) ) ;
}
// Local X texture offsets
2022-01-06 17:46:37 +00:00
if ( UDB . GameConfiguration . hasLocalSidedefTextureOffsets )
2021-11-28 13:00:24 +00:00
{
2022-01-06 17:46:37 +00:00
if ( UDB . ScriptOptions . upper _x && isValidTexture ( sd . upperTexture ) )
sd . fields . offsetx _top = getRandomOffset ( UDB . Data . getTextureInfo ( sd . upperTexture ) . height ) ;
2021-11-28 13:00:24 +00:00
2022-01-06 17:46:37 +00:00
if ( UDB . ScriptOptions . middle _x && isValidTexture ( sd . middleTexture ) )
sd . fields . offsetx _mid = getRandomOffset ( UDB . Data . getTextureInfo ( sd . middleTexture ) . height ) ;
2021-11-28 13:00:24 +00:00
2022-01-06 17:46:37 +00:00
if ( UDB . ScriptOptions . lower _x && isValidTexture ( sd . lowerTexture ) )
sd . fields . offsetx _bottom = getRandomOffset ( UDB . Data . getTextureInfo ( sd . lowerTexture ) . height ) ;
2021-11-28 13:00:24 +00:00
}
// Local Y texture offsets
2022-01-06 17:46:37 +00:00
if ( UDB . GameConfiguration . hasLocalSidedefTextureOffsets )
2021-11-28 13:00:24 +00:00
{
2022-01-06 17:46:37 +00:00
if ( UDB . ScriptOptions . upper _y && isValidTexture ( sd . upperTexture ) )
sd . fields . offsety _top = getRandomOffset ( UDB . Data . getTextureInfo ( sd . upperTexture ) . height ) ;
2021-11-28 13:00:24 +00:00
2022-01-06 17:46:37 +00:00
if ( UDB . ScriptOptions . middle _y && isValidTexture ( sd . middleTexture ) )
sd . fields . offsety _mid = getRandomOffset ( UDB . Data . getTextureInfo ( sd . middleTexture ) . height ) ;
2021-11-28 13:00:24 +00:00
2022-01-06 17:46:37 +00:00
if ( UDB . ScriptOptions . lower _y && isValidTexture ( sd . lowerTexture ) )
sd . fields . offsety _bottom = getRandomOffset ( UDB . Data . getTextureInfo ( sd . lowerTexture ) . height ) ;
2021-11-28 13:00:24 +00:00
}
}
// Randomize offset of front and back sidedefs of all selected linedefs
2022-01-06 17:46:37 +00:00
UDB . Map . getSelectedLinedefs ( ) . forEach ( ld => {
2021-11-28 13:00:24 +00:00
if ( ld . front != null )
randomizeSidedefOffsets ( ld . front ) ;
if ( ld . back != null )
randomizeSidedefOffsets ( ld . back ) ;
} ) ;