This code defines a function convertMap
that modifies Quake 3 map files by replacing entities, classes, and textures, scaling the map, and optionally adding a skybox. The modified map is then saved to a new file.
npm run import -- "convert quake 2 map to quake 3"
var fs = require('fs')
var path = require('path')
var importer = require('../Core')
var replaceClasses = importer.import("replace known classes in map")
var replaceEntities = importer.import("replace entities in map")
var replaceTextures = importer.import("replace common textures in map")
var scaleMap = importer.import("scale quake map")
var {addSkybox} = importer.import("add skybox to map")
var {MAPS_HINTS, addHints} = importer.import("draw hints in map")
function convertMap(fileName) {
var file;
if(typeof fileName === 'string' && fs.existsSync(fileName)) {
file = fs.readFileSync(fileName).toString('utf-8')
}
file = replaceEntities(file)
file = replaceClasses(file)
// replace water with foggy water
// replace message with target print
// replace duplicate textures / don't interfere with basepack
file = replaceTextures(file)
// replace barrels with rocket launchers
// replace animations and generate proper shaders
// scale map 1.2x
file = scaleMap(file, 1.15)
//file = addSkybox(file)
// scale lighting with q3map2 bsp options
var newFile = fileName.substr(0, fileName.length - path.extname(fileName).length) + '.map'
console.log(`writing ${newFile}`)
fs.writeFileSync(newFile, file)
}
module.exports = convertMap;
const fs = require('fs');
const path = require('path');
const importer = require('../Core');
// Import modules lazily to avoid loading them when not used
const modules = {
replaceClasses: importer.import('replace known classes in map'),
replaceEntities: importer.import('replace entities in map'),
replaceTextures: importer.import('replace common textures in map'),
scaleMap: importer.import('scale quake map'),
addSkybox: importer.import('add skybox to map'),
hints: importer.import('draw hints in map'),
};
function convertMap(fileName) {
// Check if file exists
if (typeof fileName!=='string' ||!fs.existsSync(fileName)) {
throw new Error(`File does not exist: ${fileName}`);
}
// Read file content
let file = fs.readFileSync(fileName, 'utf-8');
// Replace entities
file = modules.replaceEntities(file);
// Replace classes
file = modules.replaceClasses(file);
// Replace textures
file = modules.replaceTextures(file);
// Scale map
file = modules.scaleMap(file, 1.15);
// Note: addSkybox and drawHints are not implemented yet
// To-do: implement addSkybox and drawHints
// const newFile = addSkybox(file);
// const hints = modules.hints;
// Write new map file
const newFile = fileName.substr(0, fileName.length - path.extname(fileName).length) + '.map';
console.log(`Writing ${newFile}`);
fs.writeFileSync(newFile, file);
}
module.exports = convertMap;
This code snippet defines a function convertMap
that modifies Quake 3 map files.
Here's a breakdown:
Dependencies:
fs
: Node.js module for file system operations.path
: Node.js module for working with file paths.importer
: Custom module likely containing utility functions.importer
handle tasks like replacing entities, classes, textures, scaling the map, and adding a skybox.convertMap
Function:
replaceEntities
: Replaces entities in the map.replaceClasses
: Replaces classes in the map.replaceTextures
: Replaces textures in the map.scaleMap
: Scales the map by a factor of 1.15.addSkybox
: Adds a skybox to the map (commented out)..map
extension.Exports: The code exports the convertMap
function, making it available for use in other parts of the project.
Functionality Breakdown:
Dependencies:
fs
: Node.js built-in module for file system operations (reading and writing files).path
: Node.js built-in module for working with file and directory paths.importer
: A custom module (likely located in ../Core
) that provides functions for modifying Quake map files.Importing Functions:
replaceClasses
, replaceEntities
, replaceTextures
, scaleMap
, and addSkybox
are imported from the importer
module. These functions are responsible for specific modifications to the map file.convertMap
Function:
fileName
(string) as input, representing the path to the Quake map file.fileName
is a valid string and if the file exists using fs.existsSync
.file
variable using fs.readFileSync
and converts it to a UTF-8 string.replaceEntities(file)
: Replaces entities in the map file.replaceClasses(file)
: Replaces classes in the map file.replaceTextures(file)
: Replaces common textures in the map file.scaleMap(file, 1.15)
: Scales the map by a factor of 1.15..map
.file
content to the new file using fs.writeFileSync
.Purpose:
This code snippet is a script for converting Quake map files. It reads a map file, applies various modifications (entity, class, texture replacements, scaling), and writes the modified map to a new file. The script is designed to be modular, allowing for easy addition or removal of modification functions.