Skip to content

Commit

Permalink
Merge pull request #20 from jacobsjo/6-add-marker-for-the-initial-spa…
Browse files Browse the repository at this point in the history
…wn-point

add marker for the initial spawn point
  • Loading branch information
jacobsjo authored Sep 20, 2023
2 parents 1602017 + df848b2 commit 25a3417
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
"map.info.teleport_command_copied": "Teleport Command Copied",
"map.error.structures_unsupported": "Some selected structures are unsupported! Sorry.",

"map.tooltip.spawn": "Spawnpoint",

"map.yslider.aria-label": "Y Level",
"map.yslider.y-label": "Y: {y}",

Expand Down
Binary file added public/images/spawn_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions src/components/MainMap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import { useSettingsStore } from '../stores/useSettingsStore';
import { useLoadedDimensionStore } from '../stores/useLoadedDimensionStore'
import { CachedBiomeSource } from '../util/CachedBiomeSource';
import MapButton from './MapButton.vue';
import { SpawnTarget } from '../util/SpawnTarget';
import { useI18n } from 'vue-i18n';
const searchStore = useSearchStore()
const settingsStore = useSettingsStore()
const loadedDimensionStore = useLoadedDimensionStore()
const i18n = useI18n()
let layer: BiomeLayer
Expand All @@ -34,6 +37,7 @@ const y = ref(320)
var map: L.Map
var markers: L.LayerGroup
var spawnMarker: L.Marker
var marker_map = new Map<string, { marker?: L.Marker, structure?: Identifier }>()
var needs_zoom = ref(false)
Expand Down Expand Up @@ -64,6 +68,16 @@ onMounted(() => {
map.addLayer(layer)
spawnMarker = L.marker({lat: 0, lng: 0}, {
icon: L.icon({
iconUrl: "images/spawn_icon.png",
iconAnchor: [16, 16],
popupAnchor: [0, -10]
}),
}).bindPopup(L.popup())
updateSpawnMarker()
markers = L.layerGroup().addTo(map)
map.addEventListener("mousemove", (evt: L.LeafletMouseEvent) => {
Expand Down Expand Up @@ -237,12 +251,28 @@ function getMarker(structureId: Identifier, chunk: ChunkPos) {
return marker
}
function updateSpawnMarker(){
if (settingsStore.dimension.equals(Identifier.create("overworld"))){
const crs = map.options.crs!
const spawnTarget = SpawnTarget.fromJson(loadedDimensionStore.loaded_dimension.noise_settings_json?.spawn_target)
const spawn = spawnTarget.getSpawnPoint(loadedDimensionStore.sampler)
const pos = new L.Point(spawn[0] + 7, - spawn[1] - 7)
spawnMarker.setLatLng(crs.unproject(pos))
spawnMarker.setPopupContent(`${i18n.t("map.tooltip.spawn")}<br>${spawn[0] + 7}, ${spawn[1] + 7}`)
spawnMarker.addTo(map)
} else {
spawnMarker.removeFrom(map)
}
}
loadedDimensionStore.$subscribe((mutation, state) => {
for (const marker of marker_map.values()){
marker.marker?.remove()
}
marker_map.clear()
updateMarkers()
updateSpawnMarker()
const y_limits = loadedDimensionStore.loaded_dimension.y_limits
if (y_limits){
Expand All @@ -258,6 +288,10 @@ watch(searchStore.structures, () => {
updateMarkers()
})
watch(i18n.locale, () => {
updateSpawnMarker()
})
</script>

<template>
Expand Down
60 changes: 60 additions & 0 deletions src/util/SpawnTarget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Climate, Json } from "deepslate";


export class SpawnTarget{

constructor(
private paramPoints: Climate.ParamPoint[]
){

}

public static fromJson(obj: unknown){
return new SpawnTarget(Json.readArray(obj, Climate.ParamPoint.fromJson) ?? [])
}

public getSpawnPoint(climateSampler: Climate.Sampler): [number, number] {
const self = this
var result: [number, number] = [0, 0]
var result_fitness = getFitness(0, 0)

radialSearch(2048, 512, 0, 0)
radialSearch(512, 32, result[0], result[1])

return result

function getFitness(x: number, z: number){
const distanceFitness = Math.pow((x * x + z * z) / (2500 * 2500),2);

const climate = climateSampler.sample(x >> 2, 0, z >> 2)
const surfaceClimate = Climate.target(climate.temperature, climate.humidity, climate.continentalness, climate.erosion, 0, climate.weirdness)
const climateFitness = Math.min(...self.paramPoints.map(p => p.fittness(surfaceClimate)))

return distanceFitness + climateFitness
}

function radialSearch(maxRadius: number, radiusStep: number, centerX: number, centerZ: number) {
var angle = 0
var radius = radiusStep

while(radius <= maxRadius) {
const x = centerX + Math.floor(Math.sin(angle) * radius)
const z = centerZ + Math.floor(Math.cos(angle) * radius)
const fitness = getFitness(x, z)
if (fitness < result_fitness) {
result = [x, z];
result_fitness = fitness
}

angle += radiusStep / radius
if (angle > Math.PI * 2) {
angle = 0
radius += radiusStep
}
}
}


}

}

0 comments on commit 25a3417

Please sign in to comment.