Skip to content

Commit

Permalink
Merge 033c30c into 01035f5
Browse files Browse the repository at this point in the history
  • Loading branch information
ttay24 committed Jun 2, 2019
2 parents 01035f5 + 033c30c commit 21ad38e
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ All output files are placed in the `output` directory.

| Type | Json → War | War → Json | File |
|-------------------------|:-----------:|:-----------:|---------------|
| Terrain | ![check](https://cloud.githubusercontent.com/assets/4079034/25298706/7a881946-26c5-11e7-896b-402f60a0f059.png) | ![times](https://cloud.githubusercontent.com/assets/4079034/25298707/7a883642-26c5-11e7-841c-cd3eb1425461.png) | war3map.w3e |
| Terrain | ![check](https://cloud.githubusercontent.com/assets/4079034/25298706/7a881946-26c5-11e7-896b-402f60a0f059.png) | ![times](https://cloud.githubusercontent.com/assets/4079034/25298706/7a881946-26c5-11e7-896b-402f60a0f059.png) | war3map.w3e |
| Units | ![check](https://cloud.githubusercontent.com/assets/4079034/25298706/7a881946-26c5-11e7-896b-402f60a0f059.png) | ![check](https://cloud.githubusercontent.com/assets/4079034/25298706/7a881946-26c5-11e7-896b-402f60a0f059.png) | war3mapUnits.doo |
| Doodads | ![check](https://cloud.githubusercontent.com/assets/4079034/25298706/7a881946-26c5-11e7-896b-402f60a0f059.png) | ![check](https://cloud.githubusercontent.com/assets/4079034/25298706/7a881946-26c5-11e7-896b-402f60a0f059.png) | war3map.doo |
| Regions | ![check](https://cloud.githubusercontent.com/assets/4079034/25298706/7a881946-26c5-11e7-896b-402f60a0f059.png) | ![check](https://cloud.githubusercontent.com/assets/4079034/25298706/7a881946-26c5-11e7-896b-402f60a0f059.png) | war3map.w3r |
Expand Down
5 changes: 5 additions & 0 deletions lib/W3Buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module.exports = function W3Buffer(buffer) {
offset += 4;
return int;
},
readShort: function() {
let int = buffer.readInt16LE(offset);
offset += 2;
return int;
},
readFloat: function() {
let float = buffer.readFloatLE(offset);
offset += 4;
Expand Down
136 changes: 118 additions & 18 deletions lib/translators/TerrainTranslator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let HexBuffer = require('../HexBuffer'),
outBuffer;
W3Buffer = require('../W3Buffer');

const TerrainTranslator = {
jsonToWar: function(terrainJson) {
outBuffer = new HexBuffer();
let outBuffer = new HexBuffer();

/*
* Header
Expand Down Expand Up @@ -35,37 +35,137 @@ const TerrainTranslator = {
/*
* Map size data
*/
outBuffer.addInt(terrainJson.map.width);
outBuffer.addInt(terrainJson.map.height);
outBuffer.addInt(terrainJson.map.width + 1);
outBuffer.addInt(terrainJson.map.height + 1);

// Unsupported
// map.offset is not implemented yet.. so we hardcode
outBuffer.addByte(0x00);outBuffer.addByte(0x00);outBuffer.addByte(0x80);outBuffer.addByte(0xc5);
outBuffer.addByte(0x00);outBuffer.addByte(0x00);outBuffer.addByte(0x80);outBuffer.addByte(0xc5);
//outBuffer.addFloat(terrainJson.map.offset.x);
//outBuffer.addFloat(terrainJson.map.offset.y);
// map offset
outBuffer.addFloat(terrainJson.map.offset.x);
outBuffer.addFloat(terrainJson.map.offset.y);


/*
* Tile points
*/
for(let i = terrainJson.tiles.length - 1; i >= 0; i--) {
// write each for of tiles
terrainJson.tiles[i].forEach(function(tile) {
outBuffer.addShort(tile[0]); // height
outBuffer.addShort(tile[1]); // water
outBuffer.addByte(tile[2]+''+tile[3]); // flags/tile
outBuffer.addByte(tile[4]); // texture details
outBuffer.addByte(tile[5]+''+tile[6]); // cliff tile/layer
});
for (let width = 0; width < terrainJson.tiles[i].length; width++) {
let currTile = terrainJson.tiles[i][width];

let boundaryFlag = currTile.boundaryFlag ? 0x4000 : 0;

// these bit operations are based off documentation from https://github.com/stijnherfst/HiveWE/wiki/war3map.w3e-Terrain
outBuffer.addShort(currTile.groundHeight); // height
outBuffer.addShort(currTile.waterHeight | boundaryFlag); // water
outBuffer.addByte(currTile.flags | currTile.groundTexture);
outBuffer.addByte(currTile.groundVariation | currTile.cliffVariation);
outBuffer.addByte(currTile.cliffTexture | currTile.layerHeight);
}
}

return {
errors: [],
buffer: outBuffer.getBuffer()
};
},
warToJson: function(buffer) {}
warToJson: function(buffer) {
// create buffer
let result = {};
let b = new W3Buffer(buffer);

/**
* Header
*/
let w3eHeader = b.readChars(4); // W3E!
let version = b.readInt(); // 0B 00 00 00
let tileset = b.readChars(1); // tileset
let customtileset = (b.readInt() === 1);

result.tileset = tileset;
result.customtileset = customtileset;

/**
* Tiles
*/
let numTilePalettes = b.readInt();
let tilePalettes = [];
for (let i = 0; i < numTilePalettes; i++) {
tilePalettes.push(b.readChars(4));
}

result.tilepalette = tilePalettes;

/**
* Cliffs
*/
let numCliffTilePalettes = b.readInt();
let cliffPalettes = [];
for (let i = 0; i < numCliffTilePalettes; i++) {
let cliffPalette = b.readChars(4);
cliffPalettes.push(cliffPalette);
}

result.clifftilepalette = cliffPalettes;

/**
* map dimensions
*/
let width = b.readInt() - 1;
let height = b.readInt() - 1;
result.map = { width: width, height: height };

let offsetX = b.readFloat();
let offsetY = b.readFloat();
result.map.offset = { x: offsetX, y: offsetY };

/**
* map tiles
*/
result.tiles = [];
for (let h = 0; h < height + 1; h++) {
let currRow = [];

for (let w = 0; w < width + 1; w++) {
// get the bytes from the buffer
let groundHeight = b.readShort();
let waterHeightAndBoundary = b.readShort();
let flagsAndGroundTexture = b.readByte();
let groundAndCliffVariation = b.readByte();
let cliffTextureAndLayerHeight = b.readByte();

// parse out different bits (based on documentation from https://github.com/stijnherfst/HiveWE/wiki/war3map.w3e-Terrain)
let waterHeight = waterHeightAndBoundary & 32767;
let boundaryFlag = (waterHeightAndBoundary & 0x4000) === 0x4000;
let flags = flagsAndGroundTexture & 240;
let groundTexture = flagsAndGroundTexture & 15;
let groundVariation = groundAndCliffVariation & 248;
let cliffVariation = groundAndCliffVariation & 7;
let cliffTexture = cliffTextureAndLayerHeight & 240;
let layerHeight = cliffTextureAndLayerHeight & 15;

// add tile
currRow.push({
groundHeight: groundHeight,
waterHeight: waterHeight,
boundaryFlag: boundaryFlag,
flags: flags,
groundTexture: groundTexture,
groundVariation: groundVariation,
cliffVariation: cliffVariation,
cliffTexture: cliffTexture,
layerHeight: layerHeight
});
}
result.tiles.unshift(currRow);
}

// tiles
//[0, 0, 0, 0, 0, 0, 0]

return {
errors: [],
json: result
};
}
};

module.exports = TerrainTranslator;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wc3maptranslator",
"version": "1.1.0",
"version": "2.0.0",
"description": "Module to translate between `war3map` and `json` formats for WarCraft III .w3x maps",
"main": "index.js",
"scripts": {
Expand Down
9 changes: 7 additions & 2 deletions test/TranslatorReversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ describe('Reversion: war -> json -> war', function() {
});

it('Terrain reversion', function() {
// this one is pending the terrain translator support for war3map -> json
assert(false, 'Not implemented');
// take war3map.w3e -> json -> war3map.w3e
let originalBuffer = readWar3MapBuffer('war3map.w3e');
let result = new Translator.Terrain.warToJson(originalBuffer);
let translatedBuffer = new Translator.Terrain.jsonToWar(result.json).buffer;
fs.writeFileSync(Path.join(outputDir, 'war3map.w3e'), translatedBuffer);

assert(buffersAreEqual(originalBuffer, translatedBuffer));
});

it('Units reversion', function() {
Expand Down

0 comments on commit 21ad38e

Please sign in to comment.