Files
windroost/p2_godot_sample/scripts/outsider_book.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

151 lines
5.7 KiB
GDScript

class_name OutsiderBook
extends RefCounted
## S3 CH-derived Outsider book. Source authority:
## clicker_heroes_rebuild_ancient_limit_build25 @ 46cc8ad
## scripts/outsider_book.gd + data/outsiders.json
##
## Definitions, bonus families, cost/quote/buy/respec, TP formula and strict
## snapshot/restore codec are copied from CH. WR calls these购买项 "风之律令"
## in UI prose only; numeric ids and formulas stay source-bound. The CH hidden
## legacy id4 remains defined but excluded from listed_ids().
##
## WR adaptation: CH BigValue is resolved by preload (WR's big_value.gd has no
## class_name); no numeric family or level cap is changed. Wrapper comments mark
## the only non-arithmetic guard (missing definition -> cap MAX_VALUE).
const MAX_VALUE: int = 9007199254740991
const ASWallet = preload("res://scripts/ancient_soul_wallet.gd")
const BigValue = preload("res://scripts/big_value.gd")
var definitions: Dictionary = {}
var levels: Dictionary = {}
var balance: float = 0.0
var total: float = 0.0
func _init() -> void:
var data: Dictionary = JSON.parse_string(FileAccess.get_file_as_string("res://data/outsiders.json"))
for row: Dictionary in data.outsiders:
definitions[int(row.id)] = row
levels[int(row.id)] = 0
func listed_ids() -> Array[int]:
return [1, 2, 3, 5, 6, 7, 8, 9, 10]
func bonus(id: int) -> BigValue:
var level: int = int(levels.get(id, 0))
match id:
1, 4: return BigValue.power(1.5, level).minus(BigValue.new(1.0)).scale(100.0)
2:
# WR adaptation: `level` is already a bounded int, so CH's
# BigValue.new(level).to_int32() conversion is unnecessary.
var power: BigValue = BigValue.new(0.95).pow_int(level)
return BigValue.new(1.0).minus(power).times(BigValue.new(100.0))
3: return BigValue.new(level).scale(100.0)
5: return BigValue.new(level).scale(level).scale(1000.0)
6: return BigValue.new(level).scale(12.5)
7: return BigValue.new(level).scale(25.0)
8: return BigValue.new(level).scale(50.0)
9: return BigValue.new(level).scale(75.0)
10: return BigValue.new(level).scale(100.0)
return BigValue.new()
func ancient_cost_multiplier() -> BigValue:
# m22972 retains the subtraction after the normalized scalar multiply.
return BigValue.new(1.0).minus(bonus(2).scale(0.01))
func multiplier(id: int) -> BigValue:
return bonus(id).scale(0.01).plus(BigValue.new(1.0))
func transcendent_power() -> float:
# CH outsider_book.gd:44; pinned by prestige.json PR009/PR010/PR011.
return (1.0 - exp(-0.0003 * total)) * 0.23 + 0.02
func level_cap(id: int) -> int:
var def: Dictionary = definitions.get(id, {})
var cap: int = int(def.get("maxLevel", 0))
return cap if cap > 0 else MAX_VALUE
func cost(id: int, count: int) -> int:
if not id in listed_ids() or count < 1 or count > 10000: return 0
var level: int = int(levels[id])
if count > level_cap(id) - level: return 0
if id == 3: return count
var triangle: int = int(count * (count + 1) / 2)
if level > int((MAX_VALUE - triangle) / count): return MAX_VALUE + 1
return level * count + triangle
func quote(id: int, requested: int) -> Dictionary:
if not ASWallet.valid(balance, total) or not id in listed_ids() or requested < 1 or requested > 10000:
return {"count": 0, "cost": 0, "affordable": false}
var limit: int = mini(requested, level_cap(id) - int(levels[id]))
var low: int = 0
var high: int = limit
while low < high:
var mid: int = int((low + high + 1) / 2)
if cost(id, mid) <= balance: low = mid
else: high = mid - 1
var price: int = cost(id, low) if low > 0 else cost(id, 1)
return {"count": low, "requested": limit, "cost": price, "affordable": low > 0 and price > 0 and balance >= price}
func buy(id: int, requested: int) -> bool:
var purchase: Dictionary = quote(id, requested)
if not purchase.affordable: return false
# m21908 Deduct subtracts only the current balance, never the total.
var next_balance: float = balance - float(purchase.cost)
if not ASWallet.valid(next_balance, total): return false
balance = next_balance
levels[id] = int(levels[id]) + int(purchase.count)
return true
func respec() -> float:
var result: Dictionary = ASWallet.refund(balance, total)
if result.is_empty() or not ASWallet.valid(result.balance, result.total): return 0.0
if result.clears_levels:
balance = result.balance
total = result.total
for id: int in levels: levels[id] = 0
return result.refund
func snapshot() -> Dictionary:
var saved: Dictionary = {}
for id: int in levels: saved[str(id)] = levels[id]
# No rounded JSON numeric shadow: these strings are the sole wallet encoding.
return {"wallet_codec": ASWallet.CODEC, "balance": ASWallet.raw(balance), "total": ASWallet.raw(total), "levels": saved}
func restore(data: Dictionary, allow_legacy: bool = true) -> bool:
if data.is_empty(): return allow_legacy
if not data.get("levels") is Dictionary: return false
var legacy: bool = not data.has("wallet_codec")
if legacy and not allow_legacy: return false
if not legacy and (not (data.wallet_codec is int or data.wallet_codec is float) or data.wallet_codec != ASWallet.CODEC): return false
if not legacy and data.size() != 4: return false # No competing numeric/raw shadows.
var b: Variant = ASWallet.read(data.get("balance"), legacy)
var t: Variant = ASWallet.read(data.get("total"), legacy)
if b == null or t == null or not ASWallet.valid(b, t): return false
var loaded: Dictionary = levels.duplicate()
for key: String in data.levels:
var id: int = int(key)
if not definitions.has(id): return false
if not (data.levels[key] is int or data.levels[key] is float): return false
var value: float = float(data.levels[key])
if not is_finite(value) or value < 0.0 or value > level_cap(id) or value != floor(value): return false
loaded[id] = int(value)
levels = loaded
balance = b
total = t
return true