using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Verse; namespace Rimworld_Animations { public class AnimationStage_LoopRandomSelectChance : AnimationStage { public int loops; public List animationOptions; public override List> GetAnimations(int actor, int seed) { int numberOfActors = animationOptions[0].animationDefs.Count; List> animations = new List>(); for (int i = 0; i < loops; i++) { AnimationLoopOption option = getAnimationLoopOptionByWeight(seed + i, out int longestAnimLength); Tuple animation = Tuple.Create(longestAnimLength, option.animationDefs[actor]); animations.Append(animation); } return animations; } public class AnimationLoopOption { public int probability; public List animationDefs; } //select random element from loop options by weight; also calculate the longest anim length public AnimationLoopOption getAnimationLoopOptionByWeight(int seed, out int longestAnimLength) { int totalWeight = animationOptions.Sum(x => x.probability); int randomNumber = (seed * 56) % totalWeight; int cumulativeWeight = 0; foreach(AnimationLoopOption option in animationOptions) { cumulativeWeight += option.probability; if (randomNumber <= cumulativeWeight) { longestAnimLength = option.animationDefs.Max(x => x.durationTicks); return option; } } longestAnimLength = animationOptions[0].animationDefs.Max(x => x.durationTicks); return animationOptions[0]; } } }