Skip to content

Commit

Permalink
Merge branch 'rectangular-maps'
Browse files Browse the repository at this point in the history
  • Loading branch information
fidwell committed Jul 20, 2024
2 parents 2fab3e0 + 0cff362 commit 7b688d8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 75 deletions.
26 changes: 3 additions & 23 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,9 @@ import { promisify } from "util";
const options =
{
filename: "OpenRct2-Cartographer.js",

/**
* Determines in what build mode the plugin should be build. The default here takes
* from the environment (ex. CLI arguments) with "development" as fallback.
*/
build: process.env.BUILD || "development"
};

/**
* Tip: if you change the path here to your personal user folder,
* you can ignore this change in git with:
* ```
* > git update-index --skip-worktree rollup.config.js
* ```
* To accept changes on this file again, use:
* ```
* > git update-index --no-skip-worktree rollup.config.js
* ```
*/
async function getOutput() {
if (options.build !== "development") {
return `./dist/${options.filename}`;
Expand All @@ -35,23 +19,19 @@ async function getOutput() {
const platform = process.platform;
const pluginPath = `OpenRCT2/plugin/${options.filename}`;

if (platform === "win32") // Windows
{
if (platform === "win32") { // Windows
const { stdout } = await promisify(exec)("powershell -command \"[Environment]::GetFolderPath('MyDocuments')\"");
return `${stdout.trim()}/${pluginPath}`;
}
else if (platform === "darwin") // MacOS
{
else if (platform === "darwin") { // MacOS
return `${homedir()}/Library/Application Support/${pluginPath}`;
}
else // Linux
{
else { // Linux
const configFolder = process.env.XDG_CONFIG_HOME || `${homedir()}/.config`;
return `${configFolder}/${pluginPath}`;
}
}


/**
* @type {import("rollup").RollupOptions}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const pluginVersion = "2.0.0";
export const pluginVersion = "3.0.0";
export const pluginName = "Cartographer";
export const pluginAuthors = ["fidwell"];
export const isUiAvailable = (typeof ui !== "undefined");
Expand Down
124 changes: 79 additions & 45 deletions src/ui/mapWindow.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ColourDecider from "../utilities/colourdecider";
import * as Environment from "../environment";
import Graphics from "./graphics";
import * as Logger from "../utilities/logger";
import Options from "../models/options";
import ColourDecider from "../utilities/colourdecider";
import * as Logger from "../utilities/logger";
import PeepFinder from "../utilities/peepfinder";
import Graphics from "./graphics";

export default class MapWindow {
onUpdate?: () => void;
Expand All @@ -24,7 +24,9 @@ export default class MapWindow {
// Map information
private mapColours: number[][] = [];

private mapSize: number = 0;
private mapWidth: number = 0;

private mapHeight: number = 0;

private peepFinder: PeepFinder = new PeepFinder();

Expand All @@ -45,7 +47,9 @@ export default class MapWindow {
};

private createWindow(): Window {
this.mapSize = map.size.x - 2; // Size is stored as 2 bigger than it really is for some reason
// Size is stored as 2 bigger than it really is to have a one-tile margin
this.mapWidth = map.size.x - 2;
this.mapHeight = map.size.y - 2;

const btnScaleDown: ButtonDesc = {
type: "button",
Expand Down Expand Up @@ -95,7 +99,7 @@ export default class MapWindow {
image: 5169, // SPR_ROTATE_ARROW
onClick: (): void => {
this.rotation = (this.rotation + 1) % 4;
this.draw();
this.changeSize();
}
};

Expand Down Expand Up @@ -251,22 +255,23 @@ export default class MapWindow {
}
};

const mapWidgetSize = this.tileSize * this.mapSize;
const mapWidgetWidth = this.tileSize * this.mapWidth;
const mapWidgetHeight = this.tileSize * this.mapHeight;

const mapWidget: ButtonDesc = {
x: this.margin,
y: btnScaleDown.y + btnScaleDown.height + this.margin,
type: "button",
width: mapWidgetSize,
height: mapWidgetSize,
width: mapWidgetWidth,
height: mapWidgetHeight,
name: "mapWidget",
image: this.mapImageId
};

const window = ui.openWindow({
classification: Environment.namespace,
title: `${Environment.pluginName} (v${Environment.pluginVersion})`,
width: this.margin * 2 + this.tileSize * this.mapSize,
width: this.margin * 2 + mapWidgetWidth,
height: mapWidget.y + mapWidget.height + this.margin,
maxHeight: 10000,
maxWidth: 10000,
Expand Down Expand Up @@ -322,16 +327,16 @@ export default class MapWindow {
const start = new Date().getTime();

if (this.options.showPeeps) {
this.peepFinder.loadPeepData(this.mapSize);
this.peepFinder.loadPeepData(this.mapWidth, this.mapHeight);
}

this.mapColours = [];
for (let x = 0; x < this.mapSize; x += 1) {
for (let x = 0; x < this.mapWidth; x += 1) {
this.mapColours[x] = [];
}

for (let x = 0; x < this.mapSize; x += 1) {
for (let y = 0; y < this.mapSize; y += 1) {
for (let x = 0; x < this.mapWidth; x += 1) {
for (let y = 0; y < this.mapHeight; y += 1) {
this.mapColours[x][y] = ColourDecider.getColourAtTile(x, y, this.options, this.peepFinder);
}
}
Expand All @@ -347,13 +352,15 @@ export default class MapWindow {
}

const mapWidget = <ButtonWidget> this.window.findWidget("mapWidget");
const mapWidgetSize = this.tileSize * this.mapSize;
mapWidget.width = mapWidgetSize;
mapWidget.height = mapWidgetSize;
const isRotated = this.rotation % 2 !== 0;
const mapWidgetWidth = this.tileSize * (isRotated ? this.mapHeight : this.mapWidth);
const mapWidgetHeight = this.tileSize * (isRotated ? this.mapWidth : this.mapHeight);
mapWidget.width = mapWidgetWidth;
mapWidget.height = mapWidgetHeight;

const btnShowPeeps = (this.window.widgets.filter((w) => w.name === "showPeeps")[0] as ButtonWidget);
this.window.minWidth = Math.max(mapWidget.x + mapWidgetSize + this.margin, btnShowPeeps.x + btnShowPeeps.width + this.margin);
this.window.minHeight = mapWidget.y + mapWidgetSize + this.margin;
this.window.minWidth = Math.max(mapWidget.x + mapWidgetWidth + this.margin, btnShowPeeps.x + btnShowPeeps.width + this.margin);
this.window.minHeight = mapWidget.y + mapWidgetHeight + this.margin;

this.window.width = this.window.minWidth;
this.window.height = this.window.minHeight;
Expand All @@ -365,30 +372,31 @@ export default class MapWindow {
initializeImage() {
this.mapImageId = Graphics.allocateImage(<RawPixelData>{
type: "raw",
height: this.mapSize,
width: this.mapSize,
height: this.mapHeight,
width: this.mapWidth,
data: new Uint8Array(0)
}) ?? 0;

if (this.window !== undefined) {
const mapWidget = <ButtonWidget> this.window.findWidget("mapWidget");
mapWidget.width = this.mapSize * this.tileSize;
mapWidget.height = this.mapSize * this.tileSize;
mapWidget.width = this.mapWidth * this.tileSize;
mapWidget.height = this.mapHeight * this.tileSize;
mapWidget.image = this.mapImageId ?? 0;
}
}

draw() {
const scaledMap = MapWindow.scaleMap(this.mapColours, this.tileSize);
const rotatedMap = MapWindow.rotateMap(scaledMap, this.rotation);
const rotatedMap = MapWindow.rotateMap(this.mapColours, this.rotation);
const scaledMap = MapWindow.scaleMap(rotatedMap, this.tileSize);
const finalMap = scaledMap;

const start = new Date().getTime();
Logger.debug("Reducing map...");

const flattenedColours: number[] = [];
for (let i = 0; i < rotatedMap.length; i += 1) {
for (let j = 0; j < rotatedMap[i].length; j += 1) {
flattenedColours.push(rotatedMap[i][j]);
for (let y = 0; y < finalMap[0].length; y += 1) {
for (let x = 0; x < finalMap.length; x += 1) {
flattenedColours.push(finalMap[x][y]);
}
}

Expand All @@ -398,31 +406,57 @@ export default class MapWindow {

Graphics.setPixelData(this.mapImageId, <RawPixelData>{
type: "raw",
height: this.mapSize * this.tileSize,
width: this.mapSize * this.tileSize,
height: finalMap[0].length,
width: finalMap.length,
data: new Uint8Array(flattenedColours)
});
}

static rotateMap(input: number[][], rotation: number): number[][] {
const start = new Date().getTime();
Logger.debug("Rotating map...");
const returnValue: number[][] = [];
for (let x = 0; x < input.length; x += 1) {
returnValue[x] = [];
}

for (let x = 0; x < input.length; x += 1) {
for (let y = 0; y < input.length; y += 1) {
let colour: number;
switch (rotation) {
case 1: colour = input[-y + input.length - 1][x]; break;
case 2: colour = input[-x + input.length - 1][-y + input.length - 1]; break;
case 3: colour = input[y][-x + input.length - 1]; break;
default: colour = input[x][y]; break;
var numRows = input.length;
var numCols = input[0].length;
var returnValue: number[][];

switch (rotation) {
case 1:
returnValue = [];
for (var x = 0; x < numCols; x++) {
returnValue[x] = [];
for (var y = 0; y < numRows; y++) {
returnValue[x][y] = input[y][numCols - x - 1];
}
}
returnValue[x][y] = colour;
}
break;
case 2:
returnValue = [];
for (var x = 0; x < numRows; x++) {
returnValue[x] = [];
for (var y = 0; y < numCols; y++) {
returnValue[x][y] = input[numRows - x - 1][numCols - y - 1];
}
}
break;
case 3:
returnValue = [];
for (var x = 0; x < numCols; x++) {
returnValue[x] = [];
for (var y = 0; y < numRows; y++) {
returnValue[x][y] = input[numRows - y - 1][x];
}
}
break;
default:
returnValue = [];
for (var x = 0; x < numRows; x++) {
returnValue[x] = [];
for (var y = 0; y < numCols; y++) {
returnValue[x][y] = input[x][y];
}
}
break;
}

const finish = new Date().getTime();
Expand All @@ -443,7 +477,7 @@ export default class MapWindow {
}

for (let x = 0; x < mapData.length; x += 1) {
for (let y = 0; y < mapData.length; y += 1) {
for (let y = 0; y < mapData[0].length; y += 1) {
for (let cx = 0; cx < tileSize; cx += 1) {
for (let cy = 0; cy < tileSize; cy += 1) {
returnValue[x * tileSize + cx][y * tileSize + cy] = mapData[x][y];
Expand Down
12 changes: 6 additions & 6 deletions src/utilities/peepfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ export default class PeepFinder {

public maxPeeps: number = 0;

loadPeepData(mapSize: number): void {
this.initializeArray(mapSize);
loadPeepData(mapWidth: number, mapHeight: number): void {
this.initializeArray(mapWidth, mapHeight);

const allPeeps = map.getAllEntities("peep");
const allPeeps = map.getAllEntities("guest");
for (let p = 0; p < allPeeps.length; p += 1) {
const x = Math.floor(allPeeps[p].x / 32) - 1;
const y = Math.floor(allPeeps[p].y / 32) - 1;
Expand All @@ -19,10 +19,10 @@ export default class PeepFinder {
}
}

private initializeArray(mapSize: number): void {
for (let x = 0; x < mapSize; x += 1) {
private initializeArray(mapWidth: number, mapHeight: number): void {
for (let x = 0; x < mapWidth; x += 1) {
this.peepCount[x] = [];
for (let y = 0; y < mapSize; y += 1) {
for (let y = 0; y < mapHeight; y += 1) {
this.peepCount[x][y] = 0;
}
}
Expand Down

0 comments on commit 7b688d8

Please sign in to comment.