Skip to content

Commit

Permalink
calc area using points for squaremap
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Jul 8, 2024
1 parent d7dd8e6 commit aac47fa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/api/squaremap/parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-unused-vars */
/* eslint-disable @typescript-eslint/no-unused-vars */
import striptags from 'striptags'
import { asBool, calcArea, fastMergeUnique, formatString, range, roundToNearest16, safeParseInt } from '../../utils/functions.js'
import { asBool, calcAreaPoints, fastMergeUnique, formatString, range, roundToNearest16, safeParseInt } from '../../utils/functions.js'

import type {
Resident,
Expand Down Expand Up @@ -141,18 +141,17 @@ export const parseTowns = async(res: SquaremapMarkerset, removeAccents = false)
name: formatString(townName, removeAccents),
nation: nationName,
foundedTimestamp: Math.floor(new Date(parsedPopup.founded).getTime() / 1000),
wealth: safeParseInt(parsedPopup.wealth.slice(0, -1)),
mayor: parsedPopup.mayor,
councillors: parsedPopup.councillors,
residents: parsedPopup.residents,
area: calcArea(townX, townZ, townX.length),
x: range(townX),
z: range(townZ),
area: calcAreaPoints(points),
points,
bounds: {
x: townX,
z: townZ
},
points,
x: range(townX),
z: range(townZ),
flags: {
// Flags no longer shown
pvp: asBool(parsedPopup.flags.pvp),
Expand Down Expand Up @@ -188,6 +187,10 @@ export const parseTowns = async(res: SquaremapMarkerset, removeAccents = false)
}
//#endregion

if (parsedPopup.wealth) {
town.wealth = safeParseInt(parsedPopup.wealth.slice(0, -1))
}

towns.push(town)
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/town.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface BaseTown {
export type SquaremapTown = Prettify<BaseTown & {
board?: string
foundedTimestamp: number,
wealth: number
wealth?: number
councillors: string[]
wikis?: {
town?: string
Expand Down
17 changes: 16 additions & 1 deletion src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { removeDiacritics } from "modern-diacritics"
import type {
Point2D,
RawPlayer, Player, Town,
BaseTown, BaseNation
BaseTown, BaseNation,
StrictPoint2D
} from '../types/index.js'

import { NotFound } from './errors.js'
Expand Down Expand Up @@ -60,6 +61,20 @@ export function calcArea(X: number[], Z: number[], numPoints: number, divisor =
return Math.abs(area / 2) / divisor
}

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

for (let i = 0, NUM_POINTS = points.length; 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
}

export function averageNationPos(name: string, towns: Town[]) {
const nationTowns = towns.filter(t => t.nation?.toLowerCase() == name.toLowerCase())
return getAveragePos(nationTowns)
Expand Down

0 comments on commit aac47fa

Please sign in to comment.