Files
aetherbound-guild/runtime/fg4/market_generator.gd
T

228 lines
7.3 KiB
GDScript

class_name FG4MarketGenerator
extends RefCounted
const Content = preload("res://fg4/content_registry.gd")
const CONTRACT_ID := "CTR-R3-01"
const REGION_ID := "RG-03"
const EQUIPMENT_PRICE := 18
var _catalog: Dictionary
var _validation: Dictionary
func _init(catalog: Dictionary = {}) -> void:
_catalog = Content.snapshot() if catalog.is_empty() else catalog.duplicate(true)
_validation = Content.validate(_catalog)
func validation() -> Dictionary:
return _validation.duplicate(true)
func generate(run_seed: String, market_index: int, refresh_index: int = 0) -> Dictionary:
if not bool(_validation.get("ok", false)):
return {"ok": false, "code": "invalid_catalog", "errors": _validation.errors.duplicate()}
if not _is_seed(run_seed):
return {"ok": false, "code": "invalid_run_seed"}
if market_index < 1 or market_index > 12:
return {"ok": false, "code": "outside_contract1_market"}
if refresh_index < 0:
return {"ok": false, "code": "invalid_refresh_index"}
var market := _market(market_index)
if market.is_empty():
return {"ok": false, "code": "market_authority_missing"}
var valve: Dictionary = Dictionary(market.pressure_valve).duplicate(true)
var board := {
"board_id": _low128(_digest(run_seed, "fg4.contract1.market", [market_index, refresh_index])),
"run_seed": run_seed,
"contract_id": CONTRACT_ID,
"region_id": REGION_ID,
"market_index": market_index,
"round_index": int(market.round_index),
"cleared_rounds_before": 24 + market_index - 1,
"line_capacity": int(market.line_capacity),
"offer_level": int(market.offer_level),
"refresh_index": refresh_index,
"recruit_slots": _recruit_slots(run_seed, market, refresh_index),
"equipment_offers": _equipment_offers(run_seed, market, refresh_index),
"battle_offers": _route_offers(market, valve),
"pressure_valve": valve,
"milestone_disclosure": Dictionary(market.milestone).duplicate(true),
"boss_disclosure": _boss_disclosure(market_index),
}
board["board_hash"] = _hash(board)
return {"ok": true, "code": "ok", "board": board}
func _market(market_index: int) -> Dictionary:
return Dictionary(Content.markets_by_index(_catalog).get(market_index, {})).duplicate(true)
func _recruit_slots(
run_seed: String,
market: Dictionary,
refresh_index: int
) -> Array[Dictionary]:
var slots: Array[Dictionary] = []
for slot in range(3):
var generation_key := _digest(
run_seed,
"fg4.contract1.recruit",
[int(market.market_index), refresh_index, slot]
)
slots.append({
"slot": slot,
"slot_id": _low128(generation_key),
"generation_key": generation_key,
"offer_level": int(market.offer_level),
"binding": "p10.3-recruit-materialization-compatible",
"base_profession_required": slot == 0,
"advanced_profession_allowed": slot > 0,
"list_price": 10 + 4 * int(market.offer_level) + (0 if slot == 0 else 10),
})
return slots
func _equipment_offers(
run_seed: String,
market: Dictionary,
refresh_index: int
) -> Array[Dictionary]:
var equipment := Content.rows_by_id(_catalog, "equipment")
var selected: Array[String] = []
for item_id in market.required_equipment_ids:
selected.append(String(item_id))
var remaining: Array[String] = []
for item_id in equipment:
if _is_rg03_equipment(String(item_id)) and item_id not in selected:
remaining.append(String(item_id))
remaining.sort()
var offset := (
_index(run_seed, "fg4.contract1.equipment.%d" % int(market.market_index), remaining.size())
+ refresh_index
) % remaining.size()
selected.append(remaining[offset])
selected.append(remaining[(offset + 1) % remaining.size()])
var offers: Array[Dictionary] = []
for slot in range(selected.size()):
var catalog_id := selected[slot]
var row: Dictionary = equipment[catalog_id]
var offer := {
"offer_id": _low128(_digest(
run_seed,
"fg4.contract1.equipment",
[int(market.market_index), refresh_index, slot, catalog_id]
)),
"slot": slot,
"catalog_id": catalog_id,
"name": Dictionary(row.name).duplicate(true),
"fit": Array(row.fit).duplicate(),
"effect": Dictionary(row.effect).duplicate(true),
"tradeoff": Dictionary(row.tradeoff).duplicate(true),
"preview": Dictionary(row.preview).duplicate(true),
"response_tags": Array(row.response_tags).duplicate(),
"region_band": 2,
"item_tier": 0,
"list_price": EQUIPMENT_PRICE,
}
offer["offer_hash"] = _hash(offer)
offers.append(offer)
return offers
func _is_rg03_equipment(item_id: String) -> bool:
if not item_id.begins_with("EQ-"):
return false
var number := item_id.trim_prefix("EQ-")
return number.is_valid_int() and int(number) >= 81 and int(number) <= 120
func _route_offers(market: Dictionary, valve: Dictionary) -> Array[Dictionary]:
var routes := Content.rows_by_id(_catalog, "routes")
var offers: Array[Dictionary] = []
for risk in range(3):
var route_id := "RO-R3-C01-M%02d-Q%d" % [int(market.market_index), risk]
var row: Dictionary = routes[route_id]
var offer := {
"offer_id": _low128(_hash({"contract_id": CONTRACT_ID, "route_id": route_id})),
"route_id": route_id,
"market_index": int(row.market_index),
"round_index": int(row.round_index),
"risk": int(row.risk),
"name": Dictionary(row.name).duplicate(true),
"enemy_ids": Array(row.enemy_ids).duplicate(),
"wave_order": Array(row.wave_order).duplicate(true),
"pressure": Dictionary(row.pressure).duplicate(true),
"failure": Dictionary(row.failure).duplicate(true),
"preview": Dictionary(row.preview).duplicate(true),
"response_tags": Array(row.response_tags).duplicate(),
"reward_coin": int(row.reward_coin),
"recovery_coin": int(row.recovery_coin),
"pressure_valve": valve.duplicate(true),
}
offer["offer_hash"] = _hash(offer)
offers.append(offer)
return offers
func _boss_disclosure(market_index: int) -> Dictionary:
if market_index != 12:
return {}
var boss: Dictionary = Content.rows_by_id(_catalog, "bosses")["BO-R3-01"]
return {
"boss_id": String(boss.id),
"name": Dictionary(boss.name).duplicate(true),
"preview": Dictionary(boss.preview).duplicate(true),
"rule": Dictionary(boss.rule).duplicate(true),
"responses": Dictionary(boss.responses).duplicate(true),
"failure": Dictionary(boss.failure).duplicate(true),
"phase_change": Dictionary(boss.phase_change).duplicate(true),
"response_tags": Array(boss.response_tags).duplicate(),
"fixed": true,
"executable": bool(boss.executable),
}
func _index(seed: String, label: String, size: int) -> int:
return int(("0x" + _digest(seed, label).substr(0, 8)).hex_to_int() % size)
func _digest(seed: String, label: String, parts: Array = []) -> String:
return _hash({"seed": seed, "label": label, "parts": parts})
func _is_seed(value: String) -> bool:
if value.length() != 32:
return false
for character in value:
if character not in "0123456789abcdef":
return false
return true
func _low128(value: String) -> String:
return value.substr(0, 32)
func _hash(value) -> String:
return JSON.stringify(_canonical(value)).sha256_text()
func _canonical(value):
if value is float and is_equal_approx(value, round(value)):
return int(round(value))
if value is Dictionary:
var result := {}
var keys: Array = value.keys()
keys.sort_custom(func(left, right): return String(left) < String(right))
for key in keys:
result[key] = _canonical(value[key])
return result
if value is Array:
var result: Array = []
for item in value:
result.append(_canonical(item))
return result
return value