Files
windroost/p2_godot_sample/scripts/transcendence.gd
T
wushenghua 34989d898d S3: port CH transcendence/outsider slice into WR new sky
Source-bound Ancient Soul wallet codec, outsider definitions/levels/bonuses,
TP and sacrifice math from clicker_heroes_rebuild @46cc8ad. Additive schema v7
fields with provenance; WR sky_marks/edicts left untouched. Save-first,
exactly-once claim, strict import, migration and real ScreenTouch probes.
2026-11-29 08:23:12 +08:00

180 lines
8.3 KiB
GDScript

class_name Transcendence
extends RefCounted
## S3 CH Transcension -> WR New Sky slice (source-bound, additive).
##
## Source authority: clicker_heroes_rebuild_ancient_limit_build25 @ 46cc8ad
## scripts/game_state.gd:1405-1472 primal_reward_for_zone / transcendence_preview / transcend
## scripts/ancient_soul_wallet.gd ordered binary64 wallet + receipt codec
## scripts/outsider_book.gd outsider definitions / TP / cost / respec
## scripts/progression_state.gd hero_souls_sacrificed
## scripts/ancient_book.gd total_spent()
## data/ch_numeric_authority/prestige.json (pins PR008..PR011)
##
## Explicit CH -> WR field mapping (no guessed value, nothing silently dropped):
## CH hero_souls_sacrificed -> profile["hero_souls_sacrificed"] (new additive)
## CH ancients.total_spent() -> profile["oath_souls_spent"] (new additive;
## WR oaths are bool flags with no spend ledger)
## CH ascension next_balance -> Prestige.ascension_preview().next_balance (existing)
## CH Ancient Soul wallet balance/total -> profile["ancient_souls"] snapshot (new additive)
## CH outsiders.levels -> profile["ancient_souls"]["levels"] (new additive;
## WR's existing bool `edicts` layer is untouched)
## CH transcend() reset -> Prestige.apply_new_sky() + settled wallet (existing reset)
## CH outsider_page_accessible -> Prestige.new_sky_unlock() (WR gate: 5 regions or rift 50)
## CH save_blocked_reason gate -> not present in WR; deliberately not invented
##
## WR adaptations are labelled at each call site. No native infinite-domain or
## original-save compatibility is claimed; the wallet is a bounded finite ledger.
const Wallet = preload("res://scripts/ancient_soul_wallet.gd")
const Outsiders = preload("res://scripts/outsider_book.gd")
const Prestige = preload("res://scripts/prestige.gd")
const ProfileStore = preload("res://scripts/profile_store.gd")
const BigValue = preload("res://scripts/big_value.gd")
## prestige.json PR008 k_coefficient = 5.0 (game_state.gd:1421).
const K_COEFFICIENT := 5.0
const LOG10 := 2.302585092994045
static func book_from_profile(profile: Dictionary) -> Outsiders:
var book := Outsiders.new()
var data: Variant = profile.get("ancient_souls", {})
if data is Dictionary:
# Strict codec: a tampered wallet is replaced by a fresh one, never paid.
if not (data as Dictionary).is_empty():
book.restore(data as Dictionary, true)
return book
static func ascension_next_balance(profile: Dictionary) -> float:
# CH game_state.gd:1417 uses ascension_preview().next_balance (post-ascension
# reward folded into sacrifice). WR already has that in Prestige (S1).
var asc := Prestige.ascension_preview(profile)
return maxf(0.0, float(asc.get("next_balance", profile.get("hero_souls", 0))))
static func sacrifice_inputs(profile: Dictionary) -> Dictionary:
var carried := maxf(0.0, float(profile.get("hero_souls_sacrificed", 0)))
var oath_spent := maxf(0.0, float(profile.get("oath_souls_spent", 0)))
var post_ascension := ascension_next_balance(profile)
return {
"hero_souls_sacrificed": carried,
"oath_souls_spent": oath_spent,
"hero_souls_after_ascension": post_ascension,
"total": carried + oath_spent + post_ascension,
}
static func earned_total(sacrifice: float) -> int:
# CH game_state.gd:1419-1421:
# earned_total = floor(5 * (exponent + log(mantissa)/log(10)))
# WR bounded domain: convert to BigValue to keep the exact normalization
# order. No unbounded equivalence is claimed.
if not is_finite(sacrifice) or sacrifice <= 0.0: return 0
var value := BigValue.new(float(sacrifice))
if value.mantissa <= 0.0: return 0
var magnitude := value.exponent + log(value.mantissa) / LOG10
return maxi(0, int(floor(K_COEFFICIENT * magnitude)))
static func preview(profile: Dictionary, respec_outsiders: bool = false) -> Dictionary:
var book := book_from_profile(profile)
var sac := sacrifice_inputs(profile)
var earned := earned_total(float(sac["total"]))
var settlement := Wallet.settlement(book.balance, book.total, float(earned), respec_outsiders)
var valid := not settlement.is_empty()
if not valid:
# Same non-paying fallback as CH game_state.gd:1424-1425.
settlement = {"new_total": book.total, "target_total": float(earned), "gain": 0.0,
"balance": book.balance, "refund": 0.0, "entries": [], "clears_levels": false}
var unlock := Prestige.new_sky_unlock(profile)
var can := valid and bool(unlock.get("unlocked"))
settlement.merge({
"sacrifice": sac,
"earned_total": earned,
"transcendent_power": book.transcendent_power(),
"new_sky_unlock": unlock,
"can_transcend": can,
"blocker": "" if can else ("invalid_wallet" if not valid else "new_sky_locked"),
})
return settlement
static func transcendent_primal_reward(level: int, book: Outsiders) -> int:
# CH game_state.gd:1405-1413 transcendent branch. Verified against CH @46cc8ad:
# zones 100=1, 105=21 with a fresh book (tp=0.02, Ponyboy level 0).
if level == 100: return 1
if level <= 100 or level % 5 != 0: return 0
var boss: int = int((level - 100) / 5)
var multiplier = book.multiplier(5)
var normal = BigValue.new(pow(float(boss + 4) / 5.0, 1.3)).times(multiplier).floored()
var power = BigValue.power(1.0 + book.transcendent_power(), float(boss)).scale(20.0).times(multiplier).floored()
return int(normal.plus(power).floored().to_float())
static func apply_transcendence(profile: Dictionary, respec_outsiders: bool = false,
expected_count: int = -1) -> Dictionary:
## Save-first + exactly-once. The claim key is bound to the pre-reset
## new_sky_count (CH binds it to run_id), survives the reset because it is
## appended AFTER Prestige.apply_new_sky (which drops claimed_rewards), and
## reaches disk before the caller's in-memory profile is touched.
var pv := preview(profile, respec_outsiders)
if not bool(pv.get("can_transcend")):
return {"ok": false, "reason": str(pv.get("blocker", "locked")), "reward": 0}
var pre_count := maxi(0, int(profile.get("new_sky_count", 0)))
# CH transcend(expected_run) guard: a stale confirmation cannot settle a
# different sky. WR binds it to new_sky_count instead of CH's run_id.
if expected_count >= 0 and expected_count != pre_count:
return {"ok": false, "reason": "stale_run", "reward": 0}
var claim_key := "transcendence:%d" % pre_count
var prior_claims: Array = profile.get("claimed_rewards", [])
if prior_claims.has(claim_key):
return {"ok": false, "reason": "already_claimed", "reward": 0}
var book := book_from_profile(profile)
if respec_outsiders and bool(pv.get("clears_levels")):
for id: int in book.levels: book.levels[id] = 0
book.balance = float(pv["balance"])
book.total = float(pv["new_total"])
# Reuse the WR third-tier world reset (CH transcend() clears zone/levels/
# first-tier soul wallet and keeps long-term progress through its own retain
# list). WR's apply_new_sky already encodes that retain/reset boundary.
var out := Prestige.apply_new_sky(profile).duplicate(true)
out["ancient_souls"] = book.snapshot()
out["hero_souls_sacrificed"] = float(pv["sacrifice"]["total"])
out["oath_souls_spent"] = 0.0
for key in ["hero_souls", "total_hero_souls", "primal_souls", "souls_from_ascensions", "ascensions"]:
out[key] = 0
out["transcensions"] = maxi(0, int(profile.get("transcensions", 0))) + 1
var receipt := {
"kind": "transcendence",
"from_run": pre_count,
"to_run": pre_count + 1,
"from_node": str(profile.get("current_node", "")),
"sacrifice": float(pv["sacrifice"]["total"]),
"earned_total": int(pv["earned_total"]),
"ancient_souls_gain": float(pv["gain"]),
"ancient_souls_balance": float(pv["balance"]),
"ancient_souls_total": float(pv["new_total"]),
"ancient_soul_entries": (pv["entries"] as Array).duplicate(true),
"outsider_refund": float(pv["refund"]),
}
out["last_transcendence"] = Wallet.encode_receipt(receipt)
var history: Array = (profile.get("transcendence_history", []) as Array).duplicate(true)
history.append(out["last_transcendence"])
out["transcendence_history"] = history.slice(-100)
var claims: Array = out.get("claimed_rewards", [])
claims.append(claim_key)
out["claimed_rewards"] = claims
if not ProfileStore.save_profile(out):
return {"ok": false, "reason": "save_failed", "reward": 0}
for key in out.keys():
profile[key] = out[key]
return {"ok": true, "reward": int(pv["earned_total"]),
"gain": float(pv["gain"]), "preview": pv}