quake 3 | scale quake map | replace common textures in map | Search

The translateMap function scales the geometry of a map represented in a text file by modifying brush and origin coordinates based on a provided scaling factor. This allows for flexible resizing and repositioning of the map data.

Run example

npm run import -- "translate quake map"

translate quake map

var importer = require('../Core')


function translateMap(file, amount) {
    // get all brushes in map, leaf nodes with at least one vertex
    var brushes = importer.regexToArray(/\{[^\{\}]*?\}/ig, file)
    
    // replace all brushes with scaled values
    brushes.forEach(b => {
        var newBrush = b
        newBrush = newBrush.replace(/\(((\s*[0-9\.-]+\s*)*)\)/ig, (str, $1) => {
            return '( ' + $1.trim().split(/\s+/ig)
                .map((n, i) => (n.includes('.')
                        ? parseFloat(n.trim())
                        : parseInt(n.trim())) + amount[i])
                .join(' ') + ' )'
        })
        file = file.replace(b, newBrush)
    })
    
    // replace all origins with scaled
    // TODO: make this a function
    var origins = importer.regexToArray(/"origin"\s+"((\s*[0-9\.-]+\s*)*)"/ig, file, 1)
    origins.forEach($1 => {
        var newOrigin = $1.trim().split(/\s+/ig)
            .map((n, i) => (n.includes('.')
                    ? parseFloat(n.trim())
                    : parseInt(n.trim())) + amount[i])
            .join(' ')
        file = file.replace(new RegExp('"origin"\\s+"' + $1 + '"', 'ig'), '"origin" "' + newOrigin + '"')
    })
    
    return file
}

module.exports = translateMap

What the code could have been:

const importer = require('../Core');

/**
 * Translates a 3D model map by scaling all brushes and origins by a given amount.
 *
 * @param {String} file The 3D model map file contents.
 * @param {Array(Number)} amount The scaling amount for each dimension.
 * @returns {String} The translated 3D model map file contents.
 */
function translateMap(file, amount) {
    // Get all brushes in the map, leaf nodes with at least one vertex
    const brushesRegex = /\{[^\{\}]*?\}/ig;
    const brushes = importer.regexToArray(brushesRegex, file);

    // Replace all brushes with scaled values
    brushes.forEach((brush) => {
        const newBrush = brush.replace(/\(((\s*[0-9\.-]+\s*)*)\)/ig, (str, $1) => {
            return '('+ $1.trim().split(/\s+/ig)
               .map((n, i) => (n.includes('.')
                   ? parseFloat(n.trim())
                    : parseInt(n.trim())) + amount[i])
               .join(' ') +')';
        });
        file = file.replace(brush, newBrush);
    });

    // Replace all origins with scaled values
    const originsRegex = /"origin"\s+"((\s*[0-9\.-]+\s*)*)"/ig;
    const origins = importer.regexToArray(originsRegex, file, 1);

    // Function to scale origin values
    const scaleOrigin = (origin) => {
        return origin.trim().split(/\s+/ig)
           .map((n, i) => (n.includes('.')
               ? parseFloat(n.trim())
                : parseInt(n.trim())) + amount[i])
           .join(' ');
    };

    origins.forEach((origin) => {
        const newOrigin = scaleOrigin(origin);
        file = file.replace(new RegExp('"origin"\\s+"' + origin + '"', 'ig'), `"origin" "${newOrigin}"`);
    });

    return file;
}

module.exports = translateMap;

This code defines a function translateMap that modifies a text file representing a map.

Here's a breakdown:

  1. Input:

  2. Processing:

  3. Output:

Purpose:

This code likely modifies a map file used in a game or 3D environment. The amount array allows for flexible scaling of the map's geometry, potentially for resizing, repositioning, or other transformations.