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:
+16
-3
@@ -164,13 +164,22 @@ function initGame() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// === FLUSS-GEOMETRIE ===
|
// === 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) {
|
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
|
let minD = 999
|
||||||
for (let i = 0; i < ROWS; i++) {
|
for (let i = 0; i < ROWS; i++) {
|
||||||
const rc = riverColSmooth[i]
|
const rc = riverColSmooth[i]
|
||||||
// Segment von Zeile i zu i+1
|
|
||||||
if (i < ROWS - 1) {
|
if (i < ROWS - 1) {
|
||||||
const rc2 = riverColSmooth[i + 1]
|
const rc2 = riverColSmooth[i + 1]
|
||||||
// Punkt-zu-Segment Abstand (vereinfacht: nehme naechsten Endpunkt)
|
// Punkt-zu-Segment Abstand (vereinfacht: nehme naechsten Endpunkt)
|
||||||
@@ -192,6 +201,10 @@ function isRiverTile(r, c) {
|
|||||||
function tick() {
|
function tick() {
|
||||||
if (gameOver) return
|
if (gameOver) return
|
||||||
year++
|
year++
|
||||||
|
rebuildDistCache() // Cache neu bauen (einmal pro Tick statt pro Frame pro Tile)
|
||||||
|
|
||||||
|
// EventLog cappen
|
||||||
|
if (eventLog.length > 50) eventLog.length = 50
|
||||||
|
|
||||||
// 1. Einkommen
|
// 1. Einkommen
|
||||||
let income = 0, totalPop = 0, totalFood = 0
|
let income = 0, totalPop = 0, totalFood = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user