Advertisement
Guest User

Untitled

a guest
Mar 12th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fs from 'fs'
  2.  
  3. interface Tile {
  4.     height: number
  5.     value: number
  6.     flags: number
  7.     texture: number  
  8.     details: number
  9.     clifftex: number
  10.     layerHeight: number
  11. }
  12.  
  13.  
  14. export default class W3E {
  15.     data: Buffer
  16.     pointer: number
  17.     size: number
  18.  
  19.     constructor(
  20.         public srcPath: string
  21.         ) {
  22.         this.data = this.getData()
  23.     }
  24.  
  25.     slice(start, end) {
  26.         return this.data.slice(start, end)
  27.     }
  28.    
  29.     getData() {
  30.         return fs.readFileSync(this.srcPath, null)
  31.     }
  32.  
  33.     assureMagic() {
  34.         if(this.magic !== 'W3E!') {
  35.             throw new Error(`INVALID MAGIC NUMBER, "${this.magic}"`)
  36.         }
  37.     }
  38.  
  39.     magic: string
  40.     version: number
  41.     tileset: string
  42.     custom_tilesets: boolean
  43.     tileset_count: number
  44.     tilesets: Array<string>
  45.     cliff_count: number
  46.     cliffsets: Array<string>
  47.     width: number
  48.     height: number
  49.     center: {
  50.         x: number
  51.         y: number
  52.     }
  53.     tileCount: number
  54.     tiles: Array<Tile>
  55.     parse() {
  56.         const { data } = this
  57.         this.magic = data.slice(0, 4).toString()
  58.         this.assureMagic()
  59.         this.version = data.readInt32LE(4)
  60.         this.tileset = data.slice(8, 9).toString()
  61.         this.custom_tilesets = data.readInt32LE(9) === 1
  62.        
  63.         this.tileset_count = data.readInt32LE(13)
  64.         this.tilesets = []
  65.         let offset = 17
  66.         for(let i = 0; i < this.tileset_count; i++) {
  67.             this.tilesets.push(data.slice(offset, offset + 4).toString())
  68.             offset += 4
  69.         }
  70.  
  71.         this.cliff_count = data.readInt32LE(offset)
  72.         offset += 4
  73.         this.cliffsets = []
  74.         for(let i = 0; i < this.cliff_count; i++) {
  75.             this.cliffsets.push(data.slice(offset, offset + 4).toString())
  76.             offset += 4
  77.         }
  78.  
  79.         this.width = data.readInt32LE(offset)
  80.         offset += 4
  81.        
  82.         this.height = data.readInt32LE(offset)
  83.         offset += 4
  84.  
  85.         this.tileCount = this.width * this.height
  86.  
  87.         this.center = {
  88.             x: data.readFloatLE(offset),
  89.             y: data.readFloatLE(offset + 4),
  90.         }
  91.         offset += 8
  92.  
  93.  
  94.         this.tiles = []
  95.         for(let i = 0; i < this.tileCount; i++) {
  96.             let tile: any = {}
  97.  
  98.             tile.height = data.readInt16LE(offset)
  99.             offset += 2
  100.  
  101.             tile.value = data.readInt16LE(offset)
  102.             offset += 2
  103.  
  104.             let tmp = data.readIntLE(offset, 1)
  105.             offset += 1
  106.  
  107.             tile.flags = tmp >> 4
  108.  
  109.             tile.texture = tmp & 0x0F
  110.  
  111.             tile.details = data.readIntLE(offset, 1)
  112.             offset += 1
  113.  
  114.             tmp = data.readIntLE(offset, 1)
  115.             offset += 1
  116.  
  117.             tile.clifftex = tmp >> 4
  118.  
  119.             tile.layerHeight = tmp & 0x0F
  120.  
  121.             this.tiles.push(tile)
  122.         }
  123.        
  124.         this.size = offset
  125.     }
  126.  
  127.     alloc(size, fill = 0, encoding = null): Buffer {
  128.         return Buffer.alloc(size, fill, encoding)
  129.     }
  130.  
  131.     add(data: [Buffer, number], size, cb): [Buffer, number] {
  132.         let buf = this.alloc(size)
  133.         cb(buf)
  134.         return [Buffer.concat([data[0], buf]), data[1] + size]
  135.     }
  136.  
  137.     write(path: string) {
  138.         const stream = fs.createWriteStream(path)
  139.         let data: [Buffer, number] = [this.alloc(0), 0]
  140.  
  141.         data = this.add(data, 17, buf => {
  142.             buf.write(this.magic)
  143.             buf.writeInt32LE(this.version)
  144.             buf.write(this.tileset)
  145.             buf.writeInt32LE(this.custom_tilesets ? 1 : 0)
  146.             buf.writeInt32LE(this.tileset_count)
  147.         })
  148.  
  149.         data = this.tilesets.reduce(
  150.             (data, tile) => this.add(data, 4, buf => buf.write(tile)), data
  151.         )
  152.  
  153.         data = this.add(data, 4, buf => buf.writeInt32LE(this.cliff_count))
  154.  
  155.         data = this.cliffsets.reduce(
  156.             (data, cliff) => this.add(data, 4, buf => buf.write(cliff)), data
  157.         )
  158.  
  159.         data = this.add(data, 4, buf => buf.writeInt32LE(this.width))
  160.         data = this.add(data, 4, buf => buf.writeInt32LE(this.height))
  161.  
  162.         data = this.add(data, 4, buf => buf.writeFloatLE(this.center.x))
  163.         data = this.add(data, 4, buf => buf.writeFloatLE(this.center.y))
  164.  
  165.         data = this.add(data, this.tiles.length * 7, (buf: Buffer) => {
  166.             let offset = 0
  167.             this.tiles.forEach(
  168.                 (tileData) => {
  169.                     buf.writeInt16LE(tileData.height, offset)
  170.                     buf.writeInt16LE(tileData.value, offset + 2)
  171.                     buf.writeIntLE((tileData.flags << 4) | tileData.texture, offset + 4, 1)
  172.                     buf.writeIntLE(tileData.details, offset + 5, 1)
  173.                     buf.writeIntLE((tileData.clifftex << 4) | tileData.layerHeight, offset + 6, 1)
  174.                     offset += 7
  175.                 }
  176.             )
  177.         })
  178.  
  179.         stream.write(data[0])
  180.         stream.end()
  181.     }
  182. }
  183.  
  184. // in another file...
  185.  
  186.  
  187. import path, { resolve } from 'path'
  188. import fs from 'fs'
  189. import MDX from './binary/mdx'
  190. import BLP from './binary/blp'
  191. import W3E from './binary/w3e'
  192.  
  193. const PWD = resolve(__dirname, '../')
  194. const EXAMPLES = resolve(PWD, './examples')
  195.  
  196. const we = new W3E(resolve(EXAMPLES, './war3maporig.w3e'))
  197.  
  198. we.parse()
  199.  
  200. we.tiles = we.tiles.map(
  201.     tile => {
  202.         let height = Math.random() * (16383/12)
  203.         return Object.assign({}, tile, {
  204.             height: height
  205.             // flags: tile.flags | 0x01 // make everything a fuckn ramp
  206.         })
  207.     }
  208. )
  209.  
  210. we.write(resolve(EXAMPLES, './war3map.w3e'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement