Skip to content

Commit

Permalink
rename to midrange
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Jan 15, 2025
1 parent 376d74d commit 6b79ac6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/api/dynmap/Towns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type Dynmap from "./Dynmap.js"

import {
formatString, asBool,
calcArea, range,
calcArea, midrange,
getExisting,
isInvitable
} from '../../utils/functions.js'
Expand Down Expand Up @@ -98,8 +98,8 @@ class Towns implements EntityApi<Town | NotFoundError> {
name: formatString(town.label, removeAccents),
nation: nationName == "" ? "No Nation" : formatString(nationName.trim(), removeAccents),
mayor, area,
x: home?.x ?? range(townX),
z: home?.z ?? range(townZ),
x: home?.x ?? midrange(townX),
z: home?.z ?? midrange(townZ),
bounds: {
x: townX.map(num => Math.round(num)),
z: townZ.map(num => Math.round(num))
Expand Down
8 changes: 4 additions & 4 deletions src/api/squaremap/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import striptags from 'striptags'
import {
asBool, calcAreaPoints, fastMergeUnique,
formatString, range, roundToNearest16
formatString, midrange, roundToNearest16
} from '../../utils/functions.js'

import type {
Expand Down Expand Up @@ -78,7 +78,7 @@ export const parsePopup = (popup: string): ParsedPopup => {

const updatedPopup = popup.replace(/<span[^>]*>.*?<\/span>/, '<b>')
const cleaned = striptags(updatedPopup.replaceAll('\n', ''), ['a']).trim()

// Keeping here in-case we should revert from regex.
//const info = cleaned.split(/\s{2,}/)

Expand Down Expand Up @@ -181,8 +181,8 @@ export const parseTowns = async(res: SquaremapMarkerset, removeAccents = false)
mayor: parsedPopup.mayor,
councillors: parsedPopup.councillors,
residents: parsedPopup.residents,
x: range(townX),
z: range(townZ),
x: midrange(townX),
z: midrange(townZ),
area: calcAreaPoints(points),
points,
bounds: {
Expand Down
9 changes: 5 additions & 4 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,19 @@ export function calcArea(X: number[], Z: number[], numPoints: number, divisor =
return Math.abs(area / 2) / divisor
}

export function calcAreaPoints(points: StrictPoint2D[]) {
export function calcAreaPoints(points: StrictPoint2D[], divisor = 256) {
let area = 0

for (let i = 0, NUM_POINTS = points.length; i < NUM_POINTS; i++) {
const NUM_POINTS = points.length
for (let i = 0; i < NUM_POINTS; i++) {
const cur = points[i]
const next = points[(i + 1) % NUM_POINTS]

area += cur.x * next.z
area -= cur.z * next.x
}

return Math.abs(area / 2) / 256
return Math.abs(area / 2) / divisor
}

export function averageNationPos(name: string, towns: Town[]) {
Expand All @@ -93,7 +94,7 @@ export function getAveragePos(arr: Point2D[]) {

export const safeParseInt = (num: number | string) => typeof num === "number" ? num : parseInt(num)
export const asBool = (str: string) => str == "true"
export const range = (args: number[]) => Math.round((Math.max(...args) + Math.min(...args)) / 2)
export const midrange = (args: number[]) => Math.round((Math.max(...args) + Math.min(...args)) / 2)

export const sqr = (a: Point2D, b: Point2D, range: number) => Math.hypot(
safeParseInt(a.x) - safeParseInt(b.x),
Expand Down

0 comments on commit 6b79ac6

Please sign in to comment.