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

This code modifies a map file by replacing specific entity classes with predefined alternatives, allowing for customization and potential behavior changes within the map. It achieves this by using a mapping of old classes to new ones and performing a series of string replacements within the map file content.

Run example

npm run import -- "replace known classes in map"

replace known classes in map

var fs = require('fs')

var classes = {
    weapon_chaingun:                'weapon_lightning',
    misc_teleporter:                'trigger_teleport',
    weapon_hyperblasters:           'weapon_plasmagun',
    weapon_supershotgun:            'weapon_shotgun',
    info_teleport_destination:      'target_position',
    item_armor1:                    'item_armor_combat',
    item_armor2:                    'item_armor_combat',
    item_armorInv:                  'item_armor_body',
    item_artifact_envirosuit:       'item_enviro',
    item_artifact_invisibility:     'item_invis',
    item_artifact_super_damage:     'item_quad',
    item_cells:                     'ammo_lightning',
    item_rockets:                   'ammo_rockets',
    item_shells:                    'ammo_shells',
    item_spikes:                    'ammo_cells',
    weapon_nailgun:                 'weapon_plasmagun',
    weapon_supernailgun:            'weapon_plasmagun',
    weapon_supershotgun:            'weapon_shotgun',
    // replace target_help with target_print
    target_help:                    'target_print',
    func_door_rotating:             'func_rotating'
}
function replaceClasses(file) {
    if(typeof file === 'string' && fs.existsSync(file)) {
        file = fs.readFileSync(file).toString('utf-8')
    }
    
    // replace explosions with misc_model
    file = file.replace(/"classname" "target_explosion"/ig,
                        '"classname" "misc_model"\n "model" "models/objects/r_explode/tris.md2"')
    // TODO: replace explosions with animated delay
    // replace tigger_once with multiple wait -1
    file = file.replace(/"classname" "trigger_once"/ig,
                        '"classname" "trigger_multiple"\n "wait" "-1"')
    // replace classes
    Object.keys(classes).forEach(k => {
        file = file.replace(new RegExp('"classname" "' + k + '"', 'ig'),
                            '"classname" "' + classes[k] + '"')
    })
    return file
}

module.exports = replaceClasses

What the code could have been:

const fs = require('fs');
const path = require('path');

const classMapping = {
  'weapon_chaingun': 'weapon_lightning',
 'misc_teleporter': 'trigger_teleport',
  'weapon_hyperblasters': 'weapon_plasmagun',
  'weapon_supershotgun': 'weapon_shotgun',
  'info_teleport_destination': 'target_position',
  'item_armor1': 'item_armor_combat',
  'item_armor2': 'item_armor_combat', // Potential duplicate, consider removing
  'item_armorInv': 'item_armor_body',
  'item_artifact_envirosuit': 'item_enviro',
  'item_artifact_invisibility': 'item_invis',
  'item_artifact_super_damage': 'item_quad',
  'item_cells': 'ammo_lightning',
  'item_rockets': 'ammo_rockets',
  'item_shells': 'ammo_shells',
  'item_spikes': 'ammo_cells',
  'weapon_nailgun': 'weapon_plasmagun',
  'weapon_supernailgun': 'weapon_plasmagun', // Potential duplicate of weapon_nailgun
  'weapon_supershotgun': 'weapon_shotgun', // Duplicate, consider removing
  'target_help': 'target_print', // Replaced target_help with target_print
  'func_door_rotating': 'func_rotating'
};

function replaceClasses(filePath) {
  if (typeof filePath ==='string' && fs.existsSync(filePath)) {
    return fs.readFileSync(filePath, 'utf-8');
  }

  if (filePath instanceof Buffer) {
    return filePath.toString('utf-8');
  }

  const fileContent = filePath;

  // Replace explosions with misc_model
  fileContent = fileContent.replace(/"classname" "target_explosion"/gi,
    '"classname" "misc_model"\n "model" "models/objects/r_explode/tris.md2"');

  // Replace tigger_once with multiple wait -1
  fileContent = fileContent.replace(/"classname" "trigger_once"/gi,
    '"classname" "trigger_multiple"\n "wait" "-1"');

  // Replace classes
  Object.keys(classMapping).forEach(key => {
    fileContent = fileContent.replace(new RegExp('"classname" "' + key + '"', 'gi'),
      `"classname" "${classMapping[key]}"`);
  });

  return fileContent;
}

module.exports = replaceClasses;

This code snippet defines a function replaceClasses that modifies a map file by replacing specific entity classes with predefined alternatives.

Functionality:

  1. classes Object:

  2. replaceClasses Function:

Purpose:

This function likely serves as a tool for customizing map entities. It allows for: