hmm
This commit is contained in:
parent
d5c661bc22
commit
9ef1b8dbb1
23 changed files with 329 additions and 96 deletions
|
|
@ -166,14 +166,14 @@
|
|||
rel.fightsWon = rel.fightsWon || 0;
|
||||
rel.fightsLost = rel.fightsLost || 0;
|
||||
if (event) { rel.lastEvent = event; rel.lastTime = this.world.time; }
|
||||
this.relationCache = null;
|
||||
},
|
||||
|
||||
strongestRelation(kind = "friend", liveOnly = true) {
|
||||
let bestId = null;
|
||||
let bestScore = kind === "fear" ? 5 : FRIEND_AFFINITY_THRESHOLD;
|
||||
const liveIds = liveOnly ? new Set((this.world?.tarinai || []).filter(o => o && !o.dead && o !== this).map(o => o.id).filter(Boolean)) : null;
|
||||
for (const [id, rel] of Object.entries(this.relationships || {})) {
|
||||
if (liveIds && !liveIds.has(id)) continue;
|
||||
if (liveOnly && (!this.world?.liveTarinaiById?.(id) || id === this.id)) continue;
|
||||
const score = kind === "fear" ? (rel.fear || 0) : (rel.affinity || 0);
|
||||
if (score > bestScore) { bestId = id; bestScore = score; }
|
||||
}
|
||||
|
|
@ -187,13 +187,16 @@
|
|||
},
|
||||
|
||||
currentFriendCount(minScore = FRIEND_AFFINITY_THRESHOLD, liveOnly = true) {
|
||||
const now = this.world?.time || 0;
|
||||
const cacheKey = `${minScore}:${liveOnly ? 1 : 0}`;
|
||||
if (this.relationCache?.friendCountKey === cacheKey && now < (this.relationCache.friendCountUntil || 0)) return this.relationCache.friendCount;
|
||||
let count = 0;
|
||||
const liveIds = liveOnly ? new Set((this.world?.tarinai || []).filter(o => o && !o.dead && o !== this).map(o => o.id)) : null;
|
||||
for (const [id, rel] of Object.entries(this.relationships || {})) {
|
||||
if ((rel.affinity || 0) <= minScore) continue;
|
||||
if (liveIds && !liveIds.has(id)) continue;
|
||||
if (liveOnly && (!this.world?.liveTarinaiById?.(id) || id === this.id)) continue;
|
||||
count += 1;
|
||||
}
|
||||
this.relationCache = { ...(this.relationCache || {}), friendCountKey: cacheKey, friendCount: count, friendCountUntil: now + 1.4 };
|
||||
return count;
|
||||
},
|
||||
|
||||
|
|
@ -203,16 +206,23 @@
|
|||
},
|
||||
|
||||
bestFriendLive(minScore = FRIEND_AFFINITY_THRESHOLD, maxDist = Infinity) {
|
||||
const now = this.world?.time || 0;
|
||||
const cacheKey = `${minScore}:${Number.isFinite(maxDist) ? Math.round(maxDist) : "inf"}`;
|
||||
if (this.relationCache?.bestFriendKey === cacheKey && now < (this.relationCache.bestFriendUntil || 0)) {
|
||||
const cached = this.world?.liveTarinaiById?.(this.relationCache.bestFriendId);
|
||||
if (cached && (!Number.isFinite(maxDist) || dist(this, cached) <= maxDist)) return cached;
|
||||
}
|
||||
let best = null, bestScore = minScore;
|
||||
for (const [id, rel] of Object.entries(this.relationships || {})) {
|
||||
const score = rel.affinity || 0;
|
||||
if (score <= bestScore) continue;
|
||||
const t = this.world.tarinai.find(o => o.id === id && !o.dead);
|
||||
const t = this.world.liveTarinaiById?.(id) || null;
|
||||
if (!t) continue;
|
||||
if (Number.isFinite(maxDist) && dist(this, t) > maxDist) continue;
|
||||
best = t;
|
||||
bestScore = score;
|
||||
}
|
||||
this.relationCache = { ...(this.relationCache || {}), bestFriendKey: cacheKey, bestFriendId: best?.id || "", bestFriendUntil: now + 1.0 };
|
||||
return best;
|
||||
},
|
||||
|
||||
|
|
@ -223,7 +233,7 @@
|
|||
const when = rel.lastTime || -Infinity;
|
||||
if (this.world.time - when > maxAge) continue;
|
||||
if (!(event.includes("fight") || event.includes("\u55a7\u5629") || (rel.fightsWon || 0) || (rel.fightsLost || 0))) continue;
|
||||
const t = this.world.tarinai.find(o => o.id === id && !o.dead);
|
||||
const t = this.world.liveTarinaiById?.(id) || null;
|
||||
if (!t) continue;
|
||||
if (Number.isFinite(maxDist) && dist(this, t) > maxDist) continue;
|
||||
if (when > bestT) { best = t; bestT = when; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue