class_name Upgrades extends RefCounted ## The farm's upgrades and shop items (definitions in GameConfig.upgrades, costs in ## GameConfig.costs), bought by day at one of two shops (shop_of, "workbench" by default: ## player/weapon stat and buildable upgrades, at the work area; "shed": weapons and other ## purchasable items, at the supply shed's counter), each its own UpgradePanel instance. Most are ## bought once; the workbench's stat upgrades have levels ("levels", #26): each buy() takes one to ## the next level, which costs upgrade_cost_growth times the one before (cost()) and opens on a ## later day (upgrade_level_days, level_day()), and each owned level's effect stacks. Shed items ## arrive in stock on set days ("from_day"). Bought ones are kept in FarmState.upgrades ## (id -> level), so they carry over between nights and come back with a retry. Systems ask ## value() for a stat with the owned upgrades applied instead of reading GameConfig directly: the ## corn shooter's fire rate and shot damage, fear's radius and cooldown, fences' and gates' health ## (barrier_hp), the automated defense's range, and the traps' slow time. Straw stuffing raises ## the scarecrow's max health (FarmState) when a level is bought, and the Bigger pouch upgrade ## raises pouch_capacity (ResourceStore, shared: every scarecrow's pouch grows) the same way. The ## buyer pays from their own pouch and the shed (slot). Sprint, the weapons, and radar change no ## stat: owning them (owned(): level 1 or more) unlocks sprinting (PlayerCharacter), the weapons ## (ScarecrowCombat), and the minimap (RadarHud). Rapid fire and heavy seeds (bought at the ## workbench) need the corn shooter (bought at the shed) first ("requires" crosses shops freely). ## Menu order, grouped by track. const ORDER: Array[String] = ["corn_shooter", "rapid_fire", "heavy_seeds", "pumpkin_bomb", "potato_cannon", "shurikens", "propane_torch", "alien_ray", "radar", "wide_fear", "quick_fear", "straw_stuffing", "pouch_upgrade", "sprint", "reinforced_fences", "long_range", "sticky_tar"] const ROMAN: Array[String] = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"] ## The level of an upgrade the farm owns (0: not bought). static func level(state: FarmState, id: String) -> int: return int(state.upgrades.get(id, 0)) if state != null else 0 ## Owned at any level (level 1 or more): what "requires" and the unlocks (sprint, the weapons, ## radar) ask. static func owned(state: FarmState, id: String) -> bool: return level(state, id) > 0 ## How many levels an upgrade has (its "levels", 1 by default). static func max_level(cfg: GameConfig, id: String) -> int: return maxi(1, int(cfg.upgrades.get(id, {}).get("levels", 1))) ## Bought to its last level. static func maxed(cfg: GameConfig, state: FarmState, id: String) -> bool: return level(state, id) >= max_level(cfg, id) ## Levels bought across both shops, and how many there are in all (the "N of M" counts on the ## workbench, the morning screen, and the end screen). static func levels_bought(cfg: GameConfig, state: FarmState) -> int: var n := 0 for id in ORDER: n += mini(level(state, id), max_level(cfg, id)) return n static func levels_total(cfg: GameConfig) -> int: var n := 0 for id in ORDER: n += max_level(cfg, id) return n ## What buying this level costs: costs[id] for level 1, and upgrade_cost_growth times as much ## for each level after it (costs[id] x growth^(level - 1)), each material rounded up. ## next_cost() is the one the farm would buy now. static func cost(cfg: GameConfig, id: String, lvl: int = 1) -> Dictionary: var base: Dictionary = cfg.costs.get(id, {}) if lvl <= 1: return base var k := pow(cfg.upgrade_cost_growth, lvl - 1) var out := {} for m in base: out[m] = ceili(int(base[m]) * k - 0.0001) # (25 x 1.8^2 is 81.00000000000001) return out static func next_cost(cfg: GameConfig, state: FarmState, id: String) -> Dictionary: return cost(cfg, id, level(state, id) + 1) ## One level's entry of a per-level definition key ("mult", "add"): a number is the same every ## level; an Array has one entry per level (its last entry repeats past its end). static func per_level(entry: Variant, lvl: int, fallback: float = 0.0) -> float: if entry is Array: var a: Array = entry return float(a[clampi(lvl - 1, 0, a.size() - 1)]) if not a.is_empty() else fallback return float(entry) if entry != null else fallback ## What a level does: its "texts" entry, else the one "text". static func text_at(cfg: GameConfig, id: String, lvl: int) -> String: var u: Dictionary = cfg.upgrades.get(id, {}) var texts: Array = u.get("texts", []) if not texts.is_empty(): return String(texts[clampi(lvl - 1, 0, texts.size() - 1)]) return String(u.get("text", "")) ## Its name at a level: "Rapid fire II" (no numeral on an upgrade with a single level). static func name_at(cfg: GameConfig, id: String, lvl: int) -> String: var n := String(cfg.upgrades.get(id, {}).get("name", id)) return n if max_level(cfg, id) <= 1 else "%s %s" % [n, roman(lvl)] static func roman(n: int) -> String: return ROMAN[n] if n >= 0 and n < ROMAN.size() else str(n) ## The day a shop item arrives in stock (its "from_day", 1 by default). Day N is the day before ## night N (FarmState.night). static func from_day(cfg: GameConfig, id: String) -> int: return int(cfg.upgrades.get(id, {}).get("from_day", 1)) ## The day a level can first be bought: upgrade_level_days[level - 1] (the last entry for any ## level past its end), or the upgrade's own "level_days". Level 1 also waits for from_day. static func level_day(cfg: GameConfig, id: String, lvl: int) -> int: var days: Array = cfg.upgrades.get(id, {}).get("level_days", cfg.upgrade_level_days) var day := int(days[clampi(lvl - 1, 0, days.size() - 1)]) if not days.is_empty() else 1 return maxi(day, from_day(cfg, id)) if lvl <= 1 else day ## Whether a shop item has arrived by this farm's day (from_day). static func arrived(state: FarmState, cfg: GameConfig, id: String) -> bool: return state != null and state.night >= level_day(cfg, id, 1) ## Which shop sells an upgrade: "workbench" (the default: player/weapon stat and buildable ## upgrades, at the work area) or "shed" (weapons and other purchasable items, at the supply ## shed's counter). UpgradePanel filters ORDER by this to build the two shops' rows; "requires" ## can still cross shops (e.g. rapid_fire at the workbench requiring corn_shooter from the shed). static func shop_of(cfg: GameConfig, id: String) -> String: return String(cfg.upgrades.get(id, {}).get("shop", "workbench")) ## Total Alien Goop collected this vacation so far: every completed night's "gathered_alien_goop" ## (FarmState.totals(), from night_log) plus the current cycle's own count (night_events). This is ## a running total, not a current balance (spending or being robbed of goop doesn't un-reveal ## anything), so it's unaffected by the shed/pouch and persists through save (night_log is saved) ## and retry (retry restores the morning snapshot, dropping the failed night's own goop). static func goop_collected(state: FarmState) -> int: if state == null: return 0 return int(state.totals().get("gathered_alien_goop", 0)) + int(state.night_events.get("gathered_alien_goop", 0)) ## Whether a shop item's reveal condition is met (always true unless its definition sets ## "hidden": true). A hidden item reveals once goop_collected() reaches its "reveal_goop" (falls ## back to GameConfig.hidden_reveal_goop). static func revealed(state: FarmState, cfg: GameConfig, id: String) -> bool: var u: Dictionary = cfg.upgrades.get(id, {}) if not bool(u.get("hidden", false)): return true return goop_collected(state) >= int(u.get("reveal_goop", cfg.hidden_reveal_goop)) ## A stat with every owned upgrade applied, each of its owned levels in turn (barrier_hp is a ## multiplier, 1 without upgrades). static func value(cfg: GameConfig, state: FarmState, stat: String) -> float: var v := 1.0 if stat == "barrier_hp" else float(cfg.get(stat)) if state == null: return v for id: String in cfg.upgrades: var u: Dictionary = cfg.upgrades[id] if String(u.get("stat", "")) != stat: continue for lvl in range(1, mini(level(state, id), max_level(cfg, id)) + 1): if u.has("mult"): v *= per_level(u["mult"], lvl, 1.0) if u.has("add"): v += per_level(u["add"], lvl, 0.0) return v ## Why an upgrade's next level can't be bought right now ("" if it can), by the scarecrow in this ## avatar slot (its pouch and the shed pay). static func blocker(campaign: CampaignController, id: String, slot: int = 0) -> String: var cfg := campaign.config var s := campaign.state if not cfg.upgrades.has(id): return "No such upgrade" var next := level(s, id) + 1 if next > max_level(cfg, id): return "Done" if next == 1 and not arrived(s, cfg, id): return "Arrives on day %d" % level_day(cfg, id, 1) if not revealed(s, cfg, id): return "Not discovered yet" var needs := String(cfg.upgrades[id].get("requires", "")) if needs != "" and not owned(s, needs): return "Needs the %s first" % String(cfg.upgrades.get(needs, {}).get("name", needs)).to_lower() if next > 1 and s.night < level_day(cfg, id, next): return "Level %d opens on day %d" % [next, level_day(cfg, id, next)] if not campaign.is_daytime(): return "The work area is for daytime" if not s.resources.can_afford(cost(cfg, id, next), slot): return "Need %s more" % ResourceStore.describe(s.resources.missing(cost(cfg, id, next), slot)) return "" ## Buys an upgrade's next level if it can be bought, paid by the scarecrow in this avatar slot ## (its pouch, then the shed). Returns the message for the player. static func buy(campaign: CampaignController, id: String, structures: StructureSystem = null, slot: int = 0) -> String: var cfg := campaign.config var s := campaign.state var u: Dictionary = cfg.upgrades.get(id, {}) var why := blocker(campaign, id, slot) if why != "": return "%s: %s" % [u.get("name", id), why.to_lower() if why != "Done" else "already done"] var lvl := level(s, id) + 1 var add := roundi(per_level(u.get("add", 0), lvl)) var barrier_before := value(cfg, s, "barrier_hp") s.pay(cost(cfg, id, lvl), slot) s.upgrades[id] = lvl s.record_event("upgrades_bought") # every level counts match String(u.get("stat", "")): "scarecrow_max_hp": # both scarecrows' s.scarecrow_max_hp += add s.scarecrow_hp += add s.partner_max_hp += add s.partner_hp += add "barrier_hp": # Standing fences and gates keep their share of health. var k := value(cfg, s, "barrier_hp") / barrier_before for e in s.structures: if e["type"] == "fence" or e["type"] == "gate": e["hp"] = mini(roundi(int(e["hp"]) * k), structures.max_hp(e["type"]) if structures != null else 1 << 20) "pouch_capacity": # the live value lives in ResourceStore (one for every pouch), not read from cfg each time s.resources.pouch_capacity += add return "%s done: %s" % [name_at(cfg, id, lvl), text_at(cfg, id, lvl).to_lower()]