UNPKG

teeworlds

Version:

Library for (ingame) teeworlds bots.

93 lines 4.06 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TwMap = void 0; const zlib_1 = require("zlib"); const twmap_parser_1 = __importDefault(require("./twmap_parser")); const crypto_1 = require("crypto"); class TwMap { constructor(map_name, crc, size) { this.downloading = true; this.current_downloading_chunk = 0; this.mapBuffer = Buffer.alloc(0); this.map_name = map_name; this.crc = crc; } appendChunk(chunk_index, chunk_data) { // assert(chunk_index==this.current_downloading_chunk, chunk_index, this.current_downloading_chunk, chunk_size); if (chunk_index == this.current_downloading_chunk) { this.mapBuffer = Buffer.concat([this.mapBuffer, chunk_data]); } } calculateCrc() { let Crc = 0; let index = 0; const BUFFER_SIZE = 64 * 1024; while (index * BUFFER_SIZE < this.mapBuffer.byteLength) { Crc = (0, zlib_1.crc32)(this.mapBuffer.subarray(index * BUFFER_SIZE, (index + 1) * BUFFER_SIZE), Crc); // calc crc in 64KiB steps just like tw does it index++; } return Crc & 0xffffffff; } calculateSha256() { const sha256 = (0, crypto_1.createHash)("sha256"); let index = 0; const BUFFER_SIZE = 64 * 1024; while (index * BUFFER_SIZE < this.mapBuffer.byteLength) { sha256.update(this.mapBuffer.subarray(index * BUFFER_SIZE, (index + 1) * BUFFER_SIZE)); // calc sha256 in 64KiB steps just like tw does it index++; } return sha256; } parseMap() { if (this.downloading == true) throw new Error("Need to finish map download before trying to parsing the map"); this.parsed_mapinfo = twmap_parser_1.default.parse(this.mapBuffer); } // wrapper functions for twmapparser isSolid(x, y) { if (this.downloading || this.parsed_mapinfo == undefined) throw new Error("Need to finish map download before using is_solid"); return twmap_parser_1.default.is_solid(this.getTile(x, y)); } isNohook(x, y) { if (this.downloading || this.parsed_mapinfo == undefined) throw new Error("Need to finish map download before using is_nohook"); return twmap_parser_1.default.is_nohook(this.getTile(x, y)); } isFreeze(x, y) { if (this.downloading || this.parsed_mapinfo == undefined) throw new Error("Need to finish map download before using is_freeze"); return twmap_parser_1.default.is_freeze(this.getTile(x, y)); } isDfreeze(x, y) { if (this.downloading || this.parsed_mapinfo == undefined) throw new Error("Need to finish map download before using is_dfreeze"); return twmap_parser_1.default.is_dfreeze(this.getTile(x, y)); } isHookthrough(x, y) { if (this.downloading || this.parsed_mapinfo == undefined) throw new Error("Need to finish map download before using is_hookthrough"); return twmap_parser_1.default.is_hookthrough(this.getTile(x, y)); } isDeath(x, y) { if (this.downloading || this.parsed_mapinfo == undefined) throw new Error("Need to finish map download before using is_death"); return twmap_parser_1.default.is_death(this.getTile(x, y)); } isAir(x, y) { if (this.downloading || this.parsed_mapinfo == undefined) throw new Error("Need to finish map download before using is_air"); return twmap_parser_1.default.is_air(this.getTile(x, y)); } getTile(x, y) { if (this.downloading || this.parsed_mapinfo == undefined) throw new Error("Need to finish map download before using is_air"); return twmap_parser_1.default.get_tile(this.parsed_mapinfo, x, y); } } exports.TwMap = TwMap; //# sourceMappingURL=twmap.js.map