Skip to content

Commit

Permalink
feat: Parse and render segments
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed May 19, 2020
1 parent cd42a7e commit 386c344
Show file tree
Hide file tree
Showing 3 changed files with 11,599 additions and 2 deletions.
20 changes: 20 additions & 0 deletions client/zone/js-modules/map-drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export function MapDrawer() {
function draw(mapData) {
const freeColor = hexToRgb(getComputedStyle(document.documentElement).getPropertyValue("--map-free") || "#0076ff");
const occupiedColor = hexToRgb(getComputedStyle(document.documentElement).getPropertyValue("--map-occupied") || "#52aeff");
const segmentColors = [
"#19A1A1",
"#7AC037",
"#DF5618",
"#F7C841"
].map(function(e) {
return hexToRgb(e);
});

mapCtx.clearRect(0, 0, mapCanvas.width, mapCanvas.height);
const imgData = mapCtx.createImageData(mapCanvas.width, mapCanvas.height);
Expand All @@ -45,6 +53,18 @@ export function MapDrawer() {
break;
}

if (!color) {
if (key.indexOf("segment_") === 0) {
const id = parseInt(key.split("_")[1]);

color = segmentColors[((id-1) % segmentColors.length)];
} else {
console.error("Missing color for " + key);
color = {r: 0, g: 0, b: 0};
}

}

mapData.pixels[key].forEach(function(px){
const imgDataOffset = (px[0] + mapData.position.left + (px[1] + mapData.position.top) * mapCanvas.width) * 4;

Expand Down
17 changes: 15 additions & 2 deletions lib/RRMapParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class RRMapParser {
pixels: {}
};
let view;
let mayContainSegments = false;

switch (block.header_length) {
case 24:
Expand All @@ -181,6 +182,7 @@ class RRMapParser {
//Everything else stays at the same relative offsets so we can just throw those additional bytes away
view = block.view.slice(4);
parsedBlock.segments.count = view.readInt32LE(0x04);
mayContainSegments = true;
break;

default:
Expand Down Expand Up @@ -224,9 +226,20 @@ class RRMapParser {
imageData.obstacle_strong.push(coords);
break;
default: {
// TODO: define map structure with segments
//let segment = (val & 0b11111000) >> 3;
imageData.floor.push(coords);

if (mayContainSegments) {
let segmentId = (val & 0b11111000) >> 3;

if (segmentId !== 0) {
if (!imageData["segment_" + segmentId]) {
imageData["segment_" + segmentId] = [];
}

imageData["segment_" + segmentId].push(coords);
}
}

break;
}
}
Expand Down
Loading

0 comments on commit 386c344

Please sign in to comment.