GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — PathsPlusPlus/PathPlusPlus.cs at main · doombubbles/PathsPlusPlus Expand file tree/ Copy pathPathPlusPlus.csMore file actions 450 lines (384 loc) · 16.3 KB / Copy pathPathPlusPlus.csFile metadata and controls450 lines (384 loc) · 16.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 using System; using System.Collections.Generic; using System.Linq; using BTD_Mod_Helper; using BTD_Mod_Helper.Api; using BTD_Mod_Helper.Api.Testing; using BTD_Mod_Helper.Api.Towers; using BTD_Mod_Helper.Extensions; using HarmonyLib; using Il2CppAssets.Scripts.Models; using Il2CppAssets.Scripts.Models.Towers; using Il2CppAssets.Scripts.Simulation.Objects; using Il2CppAssets.Scripts.Simulation.Towers; using Il2CppAssets.Scripts.Unity; namespace PathsPlusPlus; /// <summary> /// Class to register your path, defining what tower its for and how many upgrades you're adding /// </summary> public abstract class PathPlusPlus : NamedModContent, IHasDefaultTest { /// <inheritdoc /> protected sealed override float RegistrationPriority => 11; internal int StartTier => ExtendVanillaPath >= 0 ? Upgrades.Keys.DefaultIfEmpty(5).Min() : 0; /// <summary> /// The tower id this path is for, use <code>TowerType.XXX</code> /// </summary> public abstract string Tower { get; } /// <summary> /// How many upgrades this path has. /// <br/> /// If you're setting <see cref="ExtendVanillaPath"/>, this will be the new max upgrade count for the path. /// </summary> public virtual int UpgradeCount => StartTier + Upgrades.Count; /// <summary> /// Which internal path id this PathPlusPlus will have. /// </summary> public int Path { get; private set; } /// <summary> /// Set this to one of the <see cref="Top"/>, <see cref="Middle"/>, or <see cref="Bottom"/> constants to cause /// <see cref="UpgradePlusPlus"/>s for this path to append to a vanilla path instead of creating a new path. /// </summary> public virtual int ExtendVanillaPath => -1; /// <summary> /// Path ID for the Top path /// </summary> protected internal const int Top = 0; /// <summary> /// Path ID for the Middle path /// </summary> protected internal const int Middle = 1; /// <summary> /// Path ID for the Bottom path /// </summary> protected internal const int Bottom = 2; /// <summary> /// The UpgradePlusPlus(s) for this path /// </summary> public readonly SortedDictionary<int, UpgradePlusPlus> Upgrades = []; /// <summary> /// Whether this path should appear in the Upgrades Screen for the tower /// </summary> public virtual bool ShowInMenu => true; /// <summary> /// Logic for whether an Upgrade++ should be allowed given the current tiers for a tower. ///<br/> /// By default, it will first check if <see cref="PathsPlusPlusMod.BalancedMode"/> is false and return false. /// <br/> /// Otherwise, will use <see cref="DefaultValidTiers"/> /// </summary> /// <param name="tiers">List of all tiers for this tower, will be length 3+</param> /// <returns>Whether to block the upgrade from happening</returns> public virtual bool ValidTiers(int[] tiers) => DefaultValidTiers(tiers); /// <summary> /// Used for paths utilizing <see cref="ExtendVanillaPath"/> with upgrade tiers below 6 /// Set to true to make the base TowerModel for the upgrades be the corresponding upgraded version on the original path, rather than the last TowerModel before the branching off point /// </summary> public virtual bool UseUpgradedTowerModels => false; /// <inheritdoc /> public virtual bool UseDefaultTest => true; /// <summary> /// Ensure that either Ultimate Crosspathing is being used, or that the tiers have at most 1 total path upgraded /// past tier 2, and at most 2 total paths upgraded past tier 0. /// </summary> /// <param name="tiers"></param> /// <returns></returns> public static bool DefaultValidTiers(int[] tiers) => ModHelper.HasMod("UltimateCrosspathing") || tiers.Count(i => i > 2) <= 1 && tiers.Count(i => i > 0) <= 2; /// <inheritdoc /> public override void Register() { var highest = Upgrades.Values.Select(upgrade => upgrade.Tier).DefaultIfEmpty(0).Max(); if (ExtendVanillaPath >= 0 && highest < 5) { highest = 5; // TODO allow non-contiguous upgrade path alternates? } for (var tier = StartTier; tier < highest; tier++) { if (!Upgrades.ContainsKey(tier)) { throw new Exception($"Path {Id} is missing Upgrade for tier {tier + 1}"); } } // ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract PathsPlusPlusMod.PathsById[Id] = this; if (ExtendVanillaPath is >= Top and <= Bottom) { Path = ExtendVanillaPath; var pathsByPath = PathsPlusPlusMod.ExtendedPathsByTower.GetOrAdd(Tower, []); var paths = pathsByPath.GetOrAdd(Path, []); paths.Add(this); } else { var paths = PathsPlusPlusMod.PathsByTower.GetOrAdd(Tower, []); Path = 3 + paths.Count; paths.Add(this); } foreach (var upgrade in Upgrades.Values) { Game.instance.model.AddUpgrade(upgrade.GetUpgradeModel()); } } /// <summary> /// The PathPlusPlusMutator for this path /// </summary> public PathPlusPlusMutator Mutator => field ??= new PathPlusPlusMutator(this); /// <inheritdoc /> public override IEnumerable<ModContent> Load() { yield return this; yield return Mutator; } /// <summary> /// Runs in game when one of the Upgrades of this path is first applied to a tower /// </summary> /// <param name="tower">The tower receiving the upgrade</param> /// <param name="tier">The tier of the upgrade</param> public virtual void OnUpgraded(Tower tower, int tier) { } /// <summary> /// Runs in game when one of the Upgrades of this path is attached to a Tower, whether by having just purchased the upgrade or the tower being loaded from save, etc /// Note that this may apply to a tower multiple times /// </summary> /// <param name="tower">The tower receiving the upgrade</param> /// <param name="tier">The tier of the upgrade</param> public virtual void OnAttached(Tower tower, int tier) { } /// <summary> /// The Priority given to the PathPlusPlus mutator on the tower /// </summary> public virtual int Priority => 100 - Order; /// <summary> /// Applies all upgrades for this path up through the given tier on a TowerModel. /// </summary> /// <param name="tower">TowerModel to apply to</param> /// <param name="tier">Up to and including this tier number</param> public void Apply(TowerModel tower, int tier) { if (tower.isParagon || tower.baseId != Tower) return; tower.tier = Math.Max(tower.tier, Math.Min(5, tier)); var appliedUpgrades = tower.appliedUpgrades.ToList(); if (ExtendVanillaPath >= 0 && StartTier < 5) { if (UseUpgradedTowerModels) { foreach (var upgradeModel in tower.appliedUpgrades.Select(GameModel.Current.GetUpgrade)) { if (upgradeModel.path == Path && upgradeModel.tier >= StartTier) { appliedUpgrades.Remove(upgradeModel.name); } } } else if (tier > 0) { var tierString = tower.tiers.Join(delimiter: ""); tower.tiers[ExtendVanillaPath] = Math.Clamp(tier, 0, 5); tower.name = tower.name.Replace(tierString, tower.tiers.Join(delimiter: "")); } } var upgrades = new List<UpgradePlusPlus>(); for (var i = 0; i < tier; i++) { if (!Upgrades.TryGetValue(i, out var upgrade) || appliedUpgrades.Contains(upgrade.Id)) continue; upgrades.Add(upgrade); upgrade.ApplyUpgrade(tower); upgrade.ApplyUpgrade(tower, tier); if (upgrade.IsHighestUpgrade(tower, true) && upgrade.PortraitReference is not null) { tower.portrait = upgrade.PortraitReference; } appliedUpgrades.Add(upgrade.Id); } tower.appliedUpgrades = appliedUpgrades.ToArray(); foreach (var upgrade in upgrades) { upgrade.LateApplyUpgrade(tower); upgrade.LateApplyUpgrade(tower, tier); } } /// <summary> /// Gets what tier a tower has for this path++ /// </summary> /// <param name="tower"></param> /// <returns></returns> public virtual int GetTier(Tower tower) { var mutator = GetMutator(tower); return mutator == null ? 0 : Mutator.Tier(mutator); } /// <summary> /// Gets the mutator used to track this paths' state on a tower. /// The multiplier field stores the tier. /// A tier of -1 will sometimes be set on a tower to mark that path as the default to be chosen if there are multiple /// options for the same path index. /// </summary> /// <returns>mutator, or null if not present</returns> public BehaviorMutator? GetMutator(Tower tower) => tower.GetMutatorById(Id)?.mutator; /// <summary> /// Sets the tier for a given PathPlusPlus number to be a particular value. /// Will handle the mutation of the tower but not any upgrade side effects /// </summary> /// <param name="tower">The tower</param> /// <param name="tier">The new desired tier</param> /// <param name="onUpgrade">Whether to perform OnUpgrade effects</param> public void SetTier(Tower tower, int tier, bool onUpgrade = false) { tower.RemoveMutatorsById(Id); tower.AddMutator(Mutator.Create(tier)); for (var i = 0; i < tier; i++) { if (Upgrades.TryGetValue(i, out var u)) { OnAttached(tower, tier); u.OnAttached(tower); u.currentlyAppliedOn.Add(tower); } } if (onUpgrade && Upgrades.TryGetValue(tier - 1, out var upgrade)) { OnUpgraded(tower, tier); upgrade.OnUpgraded(tower); if (mod is BloonsTD6Mod btd6Mod) { btd6Mod.OnTowerUpgraded(tower, upgrade.Name, tower.rootModel.Cast<TowerModel>()); } } } /// <summary> /// Gets the PathPlusPlus for a given tower and path number, or null if not found /// </summary> /// <param name="tower">The tower id</param> /// <param name="path">The path number</param> /// <returns>PathPlusPlus or null</returns> [Obsolete("Should pass in an actual Tower object now")] public static PathPlusPlus? GetPath(string tower, int path) => path < 3 ? PathsPlusPlusMod.ExtendedPathsByTower.TryGetValue(tower, out var pathsByPath) ? pathsByPath.TryGetValue(path, out var paths) ? paths.First() : null : null : PathsPlusPlusMod.PathsByTower.TryGetValue(tower, out var list) ? list.FirstOrDefault(plus => plus.Path == path) : null; /// <summary> /// Gets all possible PathsPlusPlus that are possible for a base tower at the given path index. /// For pathIndex > 2 this will always just be empty or 1 PathPlusPlus, /// but for pathIndex 0-2 this may be multiple paths that all extend the same VanillaPath /// </summary> /// <param name="tower">tower id</param> /// <param name="path">path index</param> /// <returns>all possible paths</returns> public static IList<PathPlusPlus> GetPaths(string tower, int path) { if (path >= 3) return PathsPlusPlusMod.PathsByTower.TryGetValue(tower, out var list) ? list.Where(p => p.Path == path).ToArray() : []; if (!PathsPlusPlusMod.ExtendedPathsByTower.TryGetValue(tower, out var pathsByPath)) return []; if (!pathsByPath.TryGetValue(path, out var paths)) return []; return paths; } /// <summary> /// Gets all possible PathsPlusPlus that are possible for a specific tower at the given path index. /// For pathIndex > 2 this will always just be empty or 1 PathPlusPlus, /// but for pathIndex 0-2 this may be multiple paths that all extend the same VanillaPath /// </summary> /// <param name="tower">tower</param> /// <param name="path">path index</param> /// <returns>all possible paths</returns> public static IList<PathPlusPlus> GetPaths(Tower tower, int path) { var paths = GetPaths(tower.towerModel.baseId, path); if (path >= 3) return paths; foreach (var p in paths) { if (p.GetTier(tower) > 0) return [p]; } return paths.Where(p => tower.towerModel.tiers[path] <= p.StartTier).ToArray(); } /// <summary> /// Gets the specific PathPlusPlus that this tower is using for the given path index, or null if it's not using a specific one /// </summary> /// <param name="tower">tower</param> /// <param name="path">path index</param> /// <returns>PathPlusPlus or null </returns> public static PathPlusPlus? GetPath(Tower tower, int path) { var paths = GetPaths(tower, path); if (path >= 3) return paths.FirstOrDefault(); foreach (var p in paths) { if (p.GetMutator(tower) != null) return p; // If mutator already present, always choose the path } return null; } /// <summary> /// Gets the specific PathPlusPlus that this tower should be showing as what it's about to upgrade to /// </summary> /// <param name="tower">tower</param> /// <param name="path">path index</param> /// <param name="tier">tower tier about to be upgraded to, 1 indexed</param> /// <returns>PathPlusPlus or null </returns> public static PathPlusPlus? GetPath(Tower tower, int path, int tier) { if (path >= 3) return GetPath(tower, path); var paths = GetPaths(tower.towerModel.baseId, path); foreach (var p in paths) { if (p.GetTier(tower) > 0) return p; // If tier already set, always choose the path } paths = paths.Where(p => p.StartTier >= tier - 1).ToArray(); foreach (var p in paths) { if (p.GetMutator(tower) != null) return p; // If mutator already present, always choose the path } paths = paths.Where(p => p.StartTier == tier - 1).ToArray(); if (tier >= 6 && paths.Any()) { return paths.First(); // default to showing the first path for tier 6+ vanilla path extensions } return null; // default to not showing a path for vanilla path extensions that start before tier 6 } /// <summary> /// Tries to get the PathPlusPlus for a given tower and path number, or null if not found /// </summary> /// <param name="tower"></param> /// <param name="path"></param> /// <param name="pathPlusPlus"></param> /// <returns>Whether one was found</returns> [Obsolete("Should pass in an actual Tower object now")] public static bool TryGetPath(string tower, int path, out PathPlusPlus pathPlusPlus) => (pathPlusPlus = GetPath(tower, path)!) != null; /// <summary> /// Tries getting the specific PathPlusPlus that this tower is using for the given path index, or null if it's not using a specific one /// </summary> /// <param name="tower">tower</param> /// <param name="path">path index</param> /// <param name="pathPlusPlus">PathPlusPlus or null</param> public static bool TryGetPath(Tower tower, int path, out PathPlusPlus pathPlusPlus) => (pathPlusPlus = GetPath(tower, path)!) != null; /// <summary> /// Tries getting the specific PathPlusPlus that this tower should be showing as what it's about to upgrade to /// </summary> /// <param name="tower">tower</param> /// <param name="path">path index</param> /// <param name="tier">tower tier about to be upgraded to, 1 indexed</param> /// <param name="pathPlusPlus">PathPlusPlus or null</param> public static bool TryGetPath(Tower tower, int path, int tier, out PathPlusPlus pathPlusPlus) => (pathPlusPlus = GetPath(tower, path, tier)!) != null; } /// <summary> /// PathPlusPlus for a ModTower /// </summary> /// <typeparam name="T">The ModTower</typeparam> public abstract class PathPlusPlus<T> : PathPlusPlus where T : ModTower { /// <inheritdoc /> public sealed override string Tower => GetInstance<T>().Id; } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |