fluss.html: Performance-Fixes (distToRiver Cache, eventLog Cap)

- distToRiver: Distance-Cache pro Tick statt O(ROWS) pro Tile pro Frame
- eventLog: Gekappt auf 50 Eintraege
- Reduziert CPU-Last von ~300k Berechnungen/sec auf ~5k/sec

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-14 08:38:43 +02:00
parent b06aa025c1
commit 00df599197
+16 -3
View File
@@ -164,13 +164,22 @@ function initGame() {
}
// === FLUSS-GEOMETRIE ===
// Abstand eines Tiles (r,c) zum Fluss (kontinuierlich, nicht nur ganzzahlig)
// Distance-Cache: wird einmal pro Tick aktualisiert statt pro Frame pro Tile
let distCache = []
function rebuildDistCache() {
for (let r = 0; r < ROWS; r++) {
if (!distCache[r]) distCache[r] = []
for (let c = 0; c < COLS; c++) distCache[r][c] = _calcDist(r, c)
}
}
function distToRiver(r, c) {
// Naechster Punkt auf der Flusslinie
if (distCache[r] && distCache[r][c] !== undefined) return distCache[r][c]
return _calcDist(r, c)
}
function _calcDist(r, c) {
let minD = 999
for (let i = 0; i < ROWS; i++) {
const rc = riverColSmooth[i]
// Segment von Zeile i zu i+1
if (i < ROWS - 1) {
const rc2 = riverColSmooth[i + 1]
// Punkt-zu-Segment Abstand (vereinfacht: nehme naechsten Endpunkt)
@@ -192,6 +201,10 @@ function isRiverTile(r, c) {
function tick() {
if (gameOver) return
year++
rebuildDistCache() // Cache neu bauen (einmal pro Tick statt pro Frame pro Tile)
// EventLog cappen
if (eventLog.length > 50) eventLog.length = 50
// 1. Einkommen
let income = 0, totalPop = 0, totalFood = 0