feat: map around 0|0 with negative coordinates and DS continents
Coordinates run from -500 to 499; continents keep their Die Staemme names counted from the top-left, so 0|0 is the corner of K44/K45/K54/K55. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
04f0fe5e15
commit
b072231ba7
|
|
@ -8,7 +8,6 @@ defineProps<{
|
||||||
const emit = defineEmits<{ center: [at: { x: number; y: number }] }>()
|
const emit = defineEmits<{ center: [at: { x: number; y: number }] }>()
|
||||||
const { current } = useCurrentWorld()
|
const { current } = useCurrentWorld()
|
||||||
|
|
||||||
const continentOf = (x: number, y: number) => `K${Math.floor(y / 100)}${Math.floor(x / 100)}`
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,8 @@ const { t } = useI18n()
|
||||||
|
|
||||||
const RULER = 26
|
const RULER = 26
|
||||||
const mapSize = computed(() => props.view?.size ?? 1000)
|
const mapSize = computed(() => props.view?.size ?? 1000)
|
||||||
|
/** Lowest coordinate: the world starts at 0|0, negative included. */
|
||||||
|
const lo = computed(() => props.view?.min ?? mapMin(mapSize.value))
|
||||||
|
|
||||||
const villageAt = computed(() => new Map((props.view?.villages ?? []).map((v) => [`${v.x}|${v.y}`, v])))
|
const villageAt = computed(() => new Map((props.view?.villages ?? []).map((v) => [`${v.x}|${v.y}`, v])))
|
||||||
|
|
||||||
|
|
@ -67,10 +69,10 @@ function groundAt(x: number, y: number): Ground {
|
||||||
|
|
||||||
// Whole fields touching the visible area. These only change when an edge crosses a
|
// Whole fields touching the visible area. These only change when an edge crosses a
|
||||||
// field, so dragging moves one transform instead of re-rendering every field.
|
// field, so dragging moves one transform instead of re-rendering every field.
|
||||||
const x0 = computed(() => Math.max(0, Math.floor(props.left)))
|
const x0 = computed(() => Math.max(lo.value, Math.floor(props.left)))
|
||||||
const y0 = computed(() => Math.max(0, Math.floor(props.top)))
|
const y0 = computed(() => Math.max(lo.value, Math.floor(props.top)))
|
||||||
const x1 = computed(() => Math.min(mapSize.value, Math.ceil(props.left + props.cols)))
|
const x1 = computed(() => Math.min(lo.value + mapSize.value, Math.ceil(props.left + props.cols)))
|
||||||
const y1 = computed(() => Math.min(mapSize.value, Math.ceil(props.top + props.rows)))
|
const y1 = computed(() => Math.min(lo.value + mapSize.value, Math.ceil(props.top + props.rows)))
|
||||||
|
|
||||||
interface Field {
|
interface Field {
|
||||||
x: number
|
x: number
|
||||||
|
|
@ -138,7 +140,7 @@ const continents = computed(() => {
|
||||||
const top = Math.ceil(props.top)
|
const top = Math.ceil(props.top)
|
||||||
for (let cy = Math.floor(y0.value / 100); cy * 100 < y1.value; cy++) {
|
for (let cy = Math.floor(y0.value / 100); cy * 100 < y1.value; cy++) {
|
||||||
for (let cx = Math.floor(x0.value / 100); cx * 100 < x1.value; cx++) {
|
for (let cx = Math.floor(x0.value / 100); cx * 100 < x1.value; cx++) {
|
||||||
out.push({ label: `K${cy}${cx}`, x: Math.max(cx * 100, left), y: Math.max(cy * 100, top) })
|
out.push({ label: continentOf(cx * 100, cy * 100, mapSize.value), x: Math.max(cx * 100, left), y: Math.max(cy * 100, top) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
|
|
@ -188,7 +190,8 @@ function onPointerUp(e: PointerEvent) {
|
||||||
if (px < 0 || py < 0) return
|
if (px < 0 || py < 0) return
|
||||||
const x = Math.floor(props.left + px / props.field)
|
const x = Math.floor(props.left + px / props.field)
|
||||||
const y = Math.floor(props.top + py / props.field)
|
const y = Math.floor(props.top + py / props.field)
|
||||||
if (x >= 0 && y >= 0 && x < mapSize.value && y < mapSize.value) emit('select', { x, y })
|
const hi = lo.value + mapSize.value
|
||||||
|
if (x >= lo.value && y >= lo.value && x < hi && y < hi) emit('select', { x, y })
|
||||||
}
|
}
|
||||||
|
|
||||||
function onKey(e: KeyboardEvent) {
|
function onKey(e: KeyboardEvent) {
|
||||||
|
|
|
||||||
|
|
@ -147,7 +147,7 @@ interface MockState {
|
||||||
reports?: MockReport[]
|
reports?: MockReport[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const STORAGE_KEY = 'vp.mock.v3'
|
const STORAGE_KEY = 'vp.mock.v4'
|
||||||
const LATENCY_MS = 60
|
const LATENCY_MS = 60
|
||||||
const WORLDS = [
|
const WORLDS = [
|
||||||
{ id: 1, name: 'World 1', slug: 'w1', speed: 1 },
|
{ id: 1, name: 'World 1', slug: 'w1', speed: 1 },
|
||||||
|
|
@ -174,6 +174,16 @@ function load(): MockState {
|
||||||
try {
|
try {
|
||||||
const raw = localStorage.getItem(STORAGE_KEY)
|
const raw = localStorage.getItem(STORAGE_KEY)
|
||||||
if (raw) return JSON.parse(raw)
|
if (raw) return JSON.parse(raw)
|
||||||
|
// v3 put the centre at 500|500; v4 puts it at 0|0.
|
||||||
|
const v3 = localStorage.getItem('vp.mock.v3')
|
||||||
|
if (v3) {
|
||||||
|
const state: MockState = JSON.parse(v3)
|
||||||
|
for (const v of state.villages) {
|
||||||
|
v.x -= 500
|
||||||
|
v.y -= 500
|
||||||
|
}
|
||||||
|
return state
|
||||||
|
}
|
||||||
// v2 counted levels from 0: shift buildings and queued orders up by one.
|
// v2 counted levels from 0: shift buildings and queued orders up by one.
|
||||||
const old = localStorage.getItem('vp.mock.v2')
|
const old = localStorage.getItem('vp.mock.v2')
|
||||||
if (old) {
|
if (old) {
|
||||||
|
|
@ -580,31 +590,33 @@ function view(state: MockState, v: MockVillage, now: number): VillageState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const continentOf = (x: number, y: number) => `K${Math.floor(y / 100)}${Math.floor(x / 100)}`
|
const continentOf = (x: number, y: number) => `K${Math.floor((y - MAP_MIN) / 100)}${Math.floor((x - MAP_MIN) / 100)}`
|
||||||
|
|
||||||
/** Placeholder points: 5 per building level. */
|
/** Placeholder points: 5 per building level. */
|
||||||
const points = (v: MockVillage) => v.buildings.reduce((sum, b) => sum + 5 * b.level, 0)
|
const points = (v: MockVillage) => v.buildings.reduce((sum, b) => sum + 5 * b.level, 0)
|
||||||
|
|
||||||
const MAP_SIZE = 1000
|
const MAP_SIZE = 1000
|
||||||
|
/** The world starts at 0|0: coordinates run from -500 to 499. */
|
||||||
|
const MAP_MIN = -MAP_SIZE / 2
|
||||||
const BARBARIAN_NAME = 'Barbarian village'
|
const BARBARIAN_NAME = 'Barbarian village'
|
||||||
|
|
||||||
/** A barbarian made up from a hash of the field, near the map centre only; a player village on the field wins. */
|
/** A barbarian made up from a hash of the field, around 0|0 only; a player village on the field wins. */
|
||||||
function barbarianAt(x: number, y: number) {
|
function barbarianAt(x: number, y: number) {
|
||||||
const r = (Math.imul(x * 73856093 ^ y * 19349663, 1274126177) >>> 0) / 4294967296
|
const r = (Math.imul(x * 73856093 ^ y * 19349663, 1274126177) >>> 0) / 4294967296
|
||||||
const near = Math.abs(x - 500) <= 25 && Math.abs(y - 500) <= 25
|
const near = Math.abs(x) <= 25 && Math.abs(y) <= 25
|
||||||
return near && r < 0.08 ? { id: -(y * MAP_SIZE + x), name: BARBARIAN_NAME, points: 20 + Math.floor(r * 1000) } : null
|
return near && r < 0.08 ? { id: -((y - MAP_MIN) * MAP_SIZE + (x - MAP_MIN) + 1), name: BARBARIAN_NAME, points: 20 + Math.floor(r * 1000) } : null
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Mock map: the stored player villages plus barbarians made up from a hash of the field. */
|
/** Mock map: the stored player villages plus barbarians made up from a hash of the field. */
|
||||||
function mapView(state: MockState, user: AuthUser, data: any): MapView {
|
function mapView(state: MockState, user: AuthUser, data: any): MapView {
|
||||||
const size = MAP_SIZE
|
const size = MAP_SIZE
|
||||||
const own = state.villages.find((v) => v.world_id === Number(data.world_id) && v.user_id === user.id)
|
const own = state.villages.find((v) => v.world_id === Number(data.world_id) && v.user_id === user.id)
|
||||||
const cx = Number.isFinite(Number(data.x)) && data.x !== undefined ? Number(data.x) : (own?.x ?? 500)
|
const cx = Number.isFinite(Number(data.x)) && data.x !== undefined ? Number(data.x) : (own?.x ?? 0)
|
||||||
const cy = Number.isFinite(Number(data.y)) && data.y !== undefined ? Number(data.y) : (own?.y ?? 500)
|
const cy = Number.isFinite(Number(data.y)) && data.y !== undefined ? Number(data.y) : (own?.y ?? 0)
|
||||||
const w = Math.max(1, Math.min(41, Number(data.w) || 15))
|
const w = Math.max(1, Math.min(41, Number(data.w) || 15))
|
||||||
const h = Math.max(1, Math.min(41, Number(data.h) || 15))
|
const h = Math.max(1, Math.min(41, Number(data.h) || 15))
|
||||||
const x0 = Math.max(0, Math.min(size - w, cx - Math.floor(w / 2)))
|
const x0 = Math.max(MAP_MIN, Math.min(MAP_MIN + size - w, cx - Math.floor(w / 2)))
|
||||||
const y0 = Math.max(0, Math.min(size - h, cy - Math.floor(h / 2)))
|
const y0 = Math.max(MAP_MIN, Math.min(MAP_MIN + size - h, cy - Math.floor(h / 2)))
|
||||||
const villages: MapVillage[] = []
|
const villages: MapVillage[] = []
|
||||||
for (let y = y0; y < y0 + h; y++) {
|
for (let y = y0; y < y0 + h; y++) {
|
||||||
for (let x = x0; x < x0 + w; x++) {
|
for (let x = x0; x < x0 + w; x++) {
|
||||||
|
|
@ -629,7 +641,7 @@ function mapView(state: MockState, user: AuthUser, data: any): MapView {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return { world_id: Number(data.world_id), size, center: { x: cx, y: cy }, x: x0, y: y0, w, h, villages }
|
return { world_id: Number(data.world_id), size, min: MAP_MIN, center: { x: cx, y: cy }, x: x0, y: y0, w, h, villages }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rally point: marches, battles and reports, crude stand-ins for app/Engine/Rally.php.
|
// Rally point: marches, battles and reports, crude stand-ins for app/Engine/Rally.php.
|
||||||
|
|
@ -638,7 +650,7 @@ const REPORTS_PER_PAGE = 25
|
||||||
const LOOTED = ['wood', 'clay', 'iron'] as const
|
const LOOTED = ['wood', 'clay', 'iron'] as const
|
||||||
|
|
||||||
function villageRef(state: MockState, id: number): VillageRef {
|
function villageRef(state: MockState, id: number): VillageRef {
|
||||||
if (id < 0) return { id, name: BARBARIAN_NAME, x: -id % MAP_SIZE, y: Math.floor(-id / MAP_SIZE), player: null }
|
if (id < 0) return { id, name: BARBARIAN_NAME, x: ((-id - 1) % MAP_SIZE) + MAP_MIN, y: Math.floor((-id - 1) / MAP_SIZE) + MAP_MIN, player: null }
|
||||||
const v = state.villages.find((v) => v.id === id)
|
const v = state.villages.find((v) => v.id === id)
|
||||||
if (!v) return { id, name: '?', x: 0, y: 0, player: null }
|
if (!v) return { id, name: '?', x: 0, y: 0, player: null }
|
||||||
const owner = state.users.find((u) => u.id === v.user_id)
|
const owner = state.users.find((u) => u.id === v.user_id)
|
||||||
|
|
@ -928,11 +940,11 @@ function reportPage(state: MockState, user: AuthUser, worldId: number, page = 1,
|
||||||
|
|
||||||
function createVillage(state: MockState, user: AuthUser, worldId: number, now: number): MockVillage {
|
function createVillage(state: MockState, user: AuthUser, worldId: number, now: number): MockVillage {
|
||||||
const taken = new Set(state.villages.filter((v) => v.world_id === worldId).map((v) => `${v.x}|${v.y}`))
|
const taken = new Set(state.villages.filter((v) => v.world_id === worldId).map((v) => `${v.x}|${v.y}`))
|
||||||
let x = 500
|
let x = 0
|
||||||
let y = 500
|
let y = 0
|
||||||
while (taken.has(`${x}|${y}`)) {
|
while (taken.has(`${x}|${y}`)) {
|
||||||
x = 475 + Math.floor(Math.random() * 51)
|
x = -25 + Math.floor(Math.random() * 51)
|
||||||
y = 475 + Math.floor(Math.random() * 51)
|
y = -25 + Math.floor(Math.random() * 51)
|
||||||
}
|
}
|
||||||
const v: MockVillage = {
|
const v: MockVillage = {
|
||||||
id: nextId(state),
|
id: nextId(state),
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,9 @@ onBeforeUnmount(() => observer?.disconnect())
|
||||||
*/
|
*/
|
||||||
const center = ref<{ x: number; y: number } | null>(null)
|
const center = ref<{ x: number; y: number } | null>(null)
|
||||||
const mapSize = computed(() => view.value?.size ?? 1000)
|
const mapSize = computed(() => view.value?.size ?? 1000)
|
||||||
|
/** Lowest and highest coordinate: the world starts at 0|0, negative included. */
|
||||||
|
const lo = computed(() => view.value?.min ?? mapMin(mapSize.value))
|
||||||
|
const hi = computed(() => lo.value + mapSize.value - 1)
|
||||||
const rounded = computed(() => (center.value ? { x: Math.round(center.value.x), y: Math.round(center.value.y) } : null))
|
const rounded = computed(() => (center.value ? { x: Math.round(center.value.x), y: Math.round(center.value.y) } : null))
|
||||||
|
|
||||||
/** The visible area in fields. */
|
/** The visible area in fields. */
|
||||||
|
|
@ -92,16 +95,14 @@ watch(view, (v) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
function goTo(at: { x: number; y: number }) {
|
function goTo(at: { x: number; y: number }) {
|
||||||
const max = mapSize.value - 1
|
router.replace({ query: { x: clamp(Math.round(at.x), lo.value, hi.value), y: clamp(Math.round(at.y), lo.value, hi.value) } })
|
||||||
router.replace({ query: { x: clamp(Math.round(at.x), 0, max), y: clamp(Math.round(at.y), 0, max) } })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function onDrag(dx: number, dy: number) {
|
function onDrag(dx: number, dy: number) {
|
||||||
if (!center.value) return
|
if (!center.value) return
|
||||||
const max = mapSize.value - 1
|
|
||||||
center.value = {
|
center.value = {
|
||||||
x: clamp(center.value.x - dx / field.value, 0, max),
|
x: clamp(center.value.x - dx / field.value, lo.value, hi.value),
|
||||||
y: clamp(center.value.y - dy / field.value, 0, max),
|
y: clamp(center.value.y - dy / field.value, lo.value, hi.value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -181,7 +182,7 @@ const counts = computed(() => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const continent = computed(() => (rounded.value ? `K${Math.floor(rounded.value.y / 100)}${Math.floor(rounded.value.x / 100)}` : ''))
|
const continent = computed(() => (rounded.value ? continentOf(rounded.value.x, rounded.value.y, mapSize.value) : ''))
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
@ -272,10 +273,10 @@ const continent = computed(() => (rounded.value ? `K${Math.floor(rounded.value.y
|
||||||
<form class="flex items-center gap-1.5 p-3" @submit.prevent="jump">
|
<form class="flex items-center gap-1.5 p-3" @submit.prevent="jump">
|
||||||
<span class="mr-1 text-sm font-semibold text-ink-700">{{ $t('map.center_map') }}</span>
|
<span class="mr-1 text-sm font-semibold text-ink-700">{{ $t('map.center_map') }}</span>
|
||||||
<label class="sr-only" for="jump-x">x</label>
|
<label class="sr-only" for="jump-x">x</label>
|
||||||
<input id="jump-x" v-model.number="jumpX" class="input w-16 px-1.5 py-1 text-center tabular-nums" type="number" min="0" :max="(view?.size ?? 1000) - 1" />
|
<input id="jump-x" v-model.number="jumpX" class="input w-16 px-1.5 py-1 text-center tabular-nums" type="number" :min="lo" :max="hi" />
|
||||||
<span class="font-semibold text-ink-500">|</span>
|
<span class="font-semibold text-ink-500">|</span>
|
||||||
<label class="sr-only" for="jump-y">y</label>
|
<label class="sr-only" for="jump-y">y</label>
|
||||||
<input id="jump-y" v-model.number="jumpY" class="input w-16 px-1.5 py-1 text-center tabular-nums" type="number" min="0" :max="(view?.size ?? 1000) - 1" />
|
<input id="jump-y" v-model.number="jumpY" class="input w-16 px-1.5 py-1 text-center tabular-nums" type="number" :min="lo" :max="hi" />
|
||||||
<button type="submit" class="btn btn-primary ml-auto py-1">{{ $t('map.jump') }}</button>
|
<button type="submit" class="btn btn-primary ml-auto py-1">{{ $t('map.jump') }}</button>
|
||||||
</form>
|
</form>
|
||||||
</section>
|
</section>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
// `map.view` payloads. Coordinates are map fields; continents are 100 × 100 (K55 holds 500|500).
|
// `map.view` payloads. Coordinates are map fields around 0|0, negative included;
|
||||||
|
// continents are 100 × 100, named from the top-left (0|0 is the corner of K44, K45, K54, K55).
|
||||||
|
|
||||||
export interface MapVillage {
|
export interface MapVillage {
|
||||||
id: number
|
id: number
|
||||||
|
|
@ -17,6 +18,8 @@ export interface MapView {
|
||||||
world_id: number
|
world_id: number
|
||||||
/** The map is size × size fields. */
|
/** The map is size × size fields. */
|
||||||
size: number
|
size: number
|
||||||
|
/** Lowest coordinate on both axes (−size/2); the highest is min + size − 1. */
|
||||||
|
min: number
|
||||||
center: { x: number; y: number }
|
center: { x: number; y: number }
|
||||||
/** Top-left field of the window. */
|
/** Top-left field of the window. */
|
||||||
x: number
|
x: number
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
// The world starts at 0|0: coordinates run from −size/2 to size/2 − 1 on both axes,
|
||||||
|
// negative included, so a 1000-field map spans −500 to 499.
|
||||||
|
|
||||||
|
export const mapMin = (size: number) => -Math.floor(size / 2)
|
||||||
|
export const mapMax = (size: number) => mapMin(size) + size - 1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The continent of a field, named like Die Stämme by its 100 × 100 block's row and column
|
||||||
|
* counted from the top-left: 0|0 is the corner of K44, K45, K54 and K55.
|
||||||
|
*/
|
||||||
|
export const continentOf = (x: number, y: number, size = 1000) =>
|
||||||
|
`K${Math.floor((y - mapMin(size)) / 100)}${Math.floor((x - mapMin(size)) / 100)}`
|
||||||
|
|
@ -13,12 +13,12 @@ export const UNIT_ORDER: UnitType[] = UNIT_GROUPS.flatMap((g) => g.units)
|
||||||
/** Every building type, in building order: what catapults can aim at. */
|
/** Every building type, in building order: what catapults can aim at. */
|
||||||
export const BUILDING_TYPES = Object.keys(BUILDING_COLORS) as BuildingType[]
|
export const BUILDING_TYPES = Object.keys(BUILDING_COLORS) as BuildingType[]
|
||||||
|
|
||||||
/** "Name (500|500)" */
|
/** "Name (12|-34)" */
|
||||||
export const villageLabel = (v: VillageRef) => `${v.name} (${v.x}|${v.y})`
|
export const villageLabel = (v: VillageRef) => `${v.name} (${v.x}|${v.y})`
|
||||||
|
|
||||||
/** "500|500", "500 500" or "500,500" → coordinates. */
|
/** "12|-34", "12 -34" or "12,-34" → coordinates; the world starts at 0|0, so they can be negative. */
|
||||||
export function parseCoords(text: string): { x: number; y: number } | null {
|
export function parseCoords(text: string): { x: number; y: number } | null {
|
||||||
const m = /^\s*(\d{1,4})\s*[|,;\s]\s*(\d{1,4})\s*$/.exec(text)
|
const m = /^\s*(-?\d{1,4})\s*[|,;\s]\s*(-?\d{1,4})\s*$/.exec(text)
|
||||||
return m ? { x: Number(m[1]), y: Number(m[2]) } : null
|
return m ? { x: Number(m[1]), y: Number(m[2]) } : null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ export default {
|
||||||
players: 'Other players',
|
players: 'Other players',
|
||||||
barbarians: 'Barbarian villages',
|
barbarians: 'Barbarian villages',
|
||||||
// {'|'} is a literal pipe: a bare | separates plural forms in vue-i18n.
|
// {'|'} is a literal pipe: a bare | separates plural forms in vue-i18n.
|
||||||
lines: "Thin lines mark sectors of 10 fields, thick lines the continents of 100 (K55 holds 500{'|'}500).",
|
lines: "Thin lines mark sectors of 10 fields, thick lines the continents of 100 (0{'|'}0 is the corner of K44, K45, K54 and K55).",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue