From 9583fa4a7904ee48cce66d55de799bdbce59a6d9 Mon Sep 17 00:00:00 2001 From: Andrew <5436387+fidwell@users.noreply.github.com> Date: Fri, 19 Jul 2024 20:29:28 -0400 Subject: [PATCH 1/2] Split up mapsize variable --- rollup.config.js | 26 +++--------------------- src/environment.ts | 2 +- src/ui/mapWindow.ts | 40 ++++++++++++++++++++----------------- src/utilities/peepfinder.ts | 12 +++++------ 4 files changed, 32 insertions(+), 48 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 777afd8..a4e8927 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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}`; @@ -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} */ diff --git a/src/environment.ts b/src/environment.ts index 0baf12d..2b38e18 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -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"); diff --git a/src/ui/mapWindow.ts b/src/ui/mapWindow.ts index 13a1c68..f1961b5 100644 --- a/src/ui/mapWindow.ts +++ b/src/ui/mapWindow.ts @@ -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; @@ -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(); @@ -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", @@ -251,7 +255,7 @@ export default class MapWindow { } }; - const mapWidgetSize = this.tileSize * this.mapSize; + const mapWidgetSize = this.tileSize * Math.max(this.mapWidth, this.mapHeight); const mapWidget: ButtonDesc = { x: this.margin, @@ -266,7 +270,7 @@ export default class MapWindow { 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 + mapWidgetSize, height: mapWidget.y + mapWidget.height + this.margin, maxHeight: 10000, maxWidth: 10000, @@ -322,16 +326,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); } } @@ -347,7 +351,7 @@ export default class MapWindow { } const mapWidget = this.window.findWidget("mapWidget"); - const mapWidgetSize = this.tileSize * this.mapSize; + const mapWidgetSize = this.tileSize * Math.max(this.mapWidth, this.mapHeight); mapWidget.width = mapWidgetSize; mapWidget.height = mapWidgetSize; @@ -365,15 +369,15 @@ export default class MapWindow { initializeImage() { this.mapImageId = Graphics.allocateImage({ 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 = 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; } } @@ -398,8 +402,8 @@ export default class MapWindow { Graphics.setPixelData(this.mapImageId, { type: "raw", - height: this.mapSize * this.tileSize, - width: this.mapSize * this.tileSize, + height: this.mapHeight * this.tileSize, + width: this.mapWidth * this.tileSize, data: new Uint8Array(flattenedColours) }); } diff --git a/src/utilities/peepfinder.ts b/src/utilities/peepfinder.ts index e317165..16b1b36 100644 --- a/src/utilities/peepfinder.ts +++ b/src/utilities/peepfinder.ts @@ -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; @@ -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; } } From 0cff362b9a0985fbc27219f3b0504c5c6a342745 Mon Sep 17 00:00:00 2001 From: Andrew <5436387+fidwell@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:23:00 -0400 Subject: [PATCH 2/2] Finally got rotation working --- src/ui/mapWindow.ts | 94 ++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 32 deletions(-) diff --git a/src/ui/mapWindow.ts b/src/ui/mapWindow.ts index f1961b5..1dcaeee 100644 --- a/src/ui/mapWindow.ts +++ b/src/ui/mapWindow.ts @@ -99,7 +99,7 @@ export default class MapWindow { image: 5169, // SPR_ROTATE_ARROW onClick: (): void => { this.rotation = (this.rotation + 1) % 4; - this.draw(); + this.changeSize(); } }; @@ -255,14 +255,15 @@ export default class MapWindow { } }; - const mapWidgetSize = this.tileSize * Math.max(this.mapWidth, this.mapHeight); + 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 }; @@ -270,7 +271,7 @@ export default class MapWindow { const window = ui.openWindow({ classification: Environment.namespace, title: `${Environment.pluginName} (v${Environment.pluginVersion})`, - width: this.margin * 2 + mapWidgetSize, + width: this.margin * 2 + mapWidgetWidth, height: mapWidget.y + mapWidget.height + this.margin, maxHeight: 10000, maxWidth: 10000, @@ -351,13 +352,15 @@ export default class MapWindow { } const mapWidget = this.window.findWidget("mapWidget"); - const mapWidgetSize = this.tileSize * Math.max(this.mapWidth, this.mapHeight); - 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; @@ -383,16 +386,17 @@ export default class MapWindow { } 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]); } } @@ -402,8 +406,8 @@ export default class MapWindow { Graphics.setPixelData(this.mapImageId, { type: "raw", - height: this.mapHeight * this.tileSize, - width: this.mapWidth * this.tileSize, + height: finalMap[0].length, + width: finalMap.length, data: new Uint8Array(flattenedColours) }); } @@ -411,22 +415,48 @@ export default class MapWindow { 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(); @@ -447,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];