public static void AssignPools() { //Stage lengths against stage number. (i.e. chances of a generated stage being // a certain length for a certain difficulty are specified below.) //Somewhat verbose, but allows for complete control of probabilities here for // more consistent stage generation. WeightedPool <int> poolOne = new WeightedPool <int>(); poolOne.Add(2, 60); poolOne.Add(3, 40); WeightedPool <int> poolTwo = new WeightedPool <int>(); poolTwo.Add(3, 20); poolTwo.Add(4, 60); poolTwo.Add(5, 20); WeightedPool <int> poolThree = new WeightedPool <int>(); poolThree.Add(4, 40); poolThree.Add(5, 40); poolThree.Add(6, 20); WeightedPool <int> poolFour = new WeightedPool <int>(); poolFour.Add(4, 30); poolFour.Add(5, 50); poolFour.Add(6, 20); WeightedPool <int> poolFive = new WeightedPool <int>(); poolFive.Add(4, 10); poolFive.Add(5, 30); poolFive.Add(6, 40); poolFive.Add(7, 20); WeightedPool <int> poolSix = new WeightedPool <int>(); poolSix.Add(5, 30); poolSix.Add(6, 40); poolSix.Add(7, 30); WeightedPool <int> poolSeven = new WeightedPool <int>(); poolSeven.Add(6, 20); poolSeven.Add(7, 30); poolSeven.Add(8, 50); stageLengthPools = new Dictionary <int, WeightedPool <int> >() { { 1, poolOne }, { 2, poolTwo }, { 3, poolThree }, { 4, poolFour }, { 5, poolFive }, { 6, poolSix }, { 7, poolSeven }, }; }
protected virtual void SpawnItems(Map map, Rectangle room) { int numberOfItems = Program.Game.Random.Next(MaximumItemsPerRoom + 1); var itemPool = new WeightedPool <Entity.EntityFactoryDelegate>(); itemPool.Add(ItemFactory.CreatePotion, 70); itemPool.Add(ItemFactory.CreateLightningScroll, GetWeightFromDungeonLevel(new SortedDictionary <int, int> { { 2, 10 }, { 4, 25 } })); itemPool.Add(ItemFactory.CreateConfuseScroll, GetWeightFromDungeonLevel(new SortedDictionary <int, int> { { 2, 25 } })); itemPool.Add(ItemFactory.CreateFireballScroll, GetWeightFromDungeonLevel(new SortedDictionary <int, int> { { 4, 20 } })); itemPool.Add(ItemFactory.CreateSword, 30); for (int i = 0; i < numberOfItems; i++) { int x = Program.Game.Random.Next(room.Left + 1, room.Right); int y = Program.Game.Random.Next(room.Top + 1, room.Bottom); if (map.IsWalkable(x, y)) { var itemFactory = itemPool.Pick(); var item = itemFactory(x, y, Program.Game.DungeonLevel); Program.Game.Entities.Add(item); } } }
// put together a pool of eligible events, checking if they're mandatory or not. public WeightedPool<GameEvent> GetWeightedPool(bool mandatory) { WeightedPool<GameEvent> pool = new WeightedPool<GameEvent> (); GameEvent gameEvent; for (int i = 0; i < events_.Count; ++i) { gameEvent = events_[i]; // make sure the event matches the mandatory criteria, and that this event is eligible if (gameEvent.Mandatory == mandatory) { if (CanSpawn (gameEvent)) { // check if that event is sleeping if (sleepingEventIDs_.ContainsKey (gameEvent.Id)) { // if so, decrement its sleep count --sleepingEventIDs_ [gameEvent.Id]; // and if it's now zero, remove it from sleeping if (sleepingEventIDs_ [gameEvent.Id] == 0) { sleepingEventIDs_.Remove (gameEvent.Id); } } else { int actualWeight = GetEventWeightForSpawn (gameEvent); //Debug.LogWarning (gameEvent.Id + ":" + actualWeight); pool.AddToPool (gameEvent, actualWeight); } } else { //Debug.LogWarning ("Should NOT be able to spawn"); } } } return pool; }
public void Draw_Called7TimesOnPoolOf7Items_WillDrawAllOfThemAndCountWillBe0() { WeightedPool <string> pool = new WeightedPool <string>(Singleton.DefaultRandom); pool.Add("White", 5); pool.Add("Blue", 5); pool.Add("Black", 5); pool.Add("Red", 5); pool.Add("Green", 5); pool.Add("Artifact", 3); pool.Add("DualColor", 1); string[] drawn = new string[7]; drawn[0] = pool.Draw(); drawn[1] = pool.Draw(); drawn[2] = pool.Draw(); drawn[3] = pool.Draw(); drawn[4] = pool.Draw(); drawn[5] = pool.Draw(); drawn[6] = pool.Draw(); Assert.IsTrue(drawn.Contains("White")); Assert.IsTrue(drawn.Contains("Blue")); Assert.IsTrue(drawn.Contains("Black")); Assert.IsTrue(drawn.Contains("Red")); Assert.IsTrue(drawn.Contains("Green")); Assert.IsTrue(drawn.Contains("Artifact")); Assert.IsTrue(drawn.Contains("DualColor")); Assert.AreEqual(0, pool.Count); }
public GameEvent SpawnEvent() { GameEvent gameEvent; if (nextEvent_ == null) { //Debug.LogWarning ("~~~~~ WEIGHTED POOL"); WeightedPool<GameEvent> pool = GetWeightedPool (true); if (pool.Count == 0) { Debug.Log ("Found no mandatory events. Getting non-mandatory"); pool = GetWeightedPool (false); } if (pool.Count == 0) { // TODO, end the game if this happens Debug.LogError ("Tried to spawn an event, but found none were eligible."); } gameEvent = pool.GetRandomItem (); } else { gameEvent = GetEvent(nextEvent_); nextEvent_ = null; } Debug.Log ("Spawning event: " + gameEvent); return gameEvent; }
public void Draw_WhenUsingRandomBiggerThanTotalWeight_WillThrowInvalidOperationException() { WeightedPool <int> pool = new WeightedPool <int>(new BadRandom(13)); pool.Add(1, 12); Assert.ThrowsException <InvalidOperationException>(() => pool.Draw()); }
public void Choose_WhenCloneFuncWasNotDefined_WillThrowInvalidOperationException() { WeightedPool <int> pool = new WeightedPool <int>(Singleton.DefaultRandom); pool.Add(1, 1); Assert.ThrowsException <InvalidOperationException>(() => pool.Choose()); }
public void Count_WhenTwoItemsAddedToEmptyPool_WillBe2() { WeightedPool <string> pool = new WeightedPool <string>(); pool.Add("Thing 1", 1); pool.Add("Thing 2", 1); Assert.AreEqual(2, pool.Count); }
public void Add_WhenTotalWeightGoesOverMaximumIntValue_WillThrowOverflowException() { WeightedPool <int> pool = new WeightedPool <int>(); pool.Add(1, int.MaxValue - 10); pool.Add(2, 10); Assert.ThrowsException <OverflowException>(() => pool.Add(3, 1)); }
public WeightedPool <GameObject> Retrieve() { WeightedPool <GameObject> pool = new WeightedPool <GameObject>(); foreach (WeightEntry entry in entries) { pool.AddEntry(entry.gameObject, entry.weight); } return(pool); }
public void Draw_CalledTwiceWhenPoolHas1Item_WillThrowInvalidOperationException() { WeightedPool <int> pool = new WeightedPool <int>(Singleton.DefaultRandom); pool.Add(1, 12); int drawnItem = pool.Draw(); Assert.AreEqual(1, drawnItem); Assert.ThrowsException <InvalidOperationException>(() => pool.Draw()); }
public void Clear_WhenPoolHas2Items_WillHaveCount0() { WeightedPool <string> pool = new WeightedPool <string>(); pool.Add("Thing 1", 1); pool.Add("Thing 2", 1); pool.Clear(); Assert.AreEqual(0, pool.Count); }
public void ResetInteractables() { ClearInteractables(); spawnLocation = Vector2.zero; lastPlayerSpawnLocation = Vector2.zero; //As the player improves luck, hazard spawn goes down and powerup spawn goes up pool = spawnablesPool.Retrieve(); foreach (LuckWeightFactor effect in luckEffects) { pool[effect.weightToAugment] += stats[StatType.Luck].Value * effect.addedWeightPerLuck; } SpawnInteractables(); }
public void Choose_WhenPoolHas1Item_WillGetCloneOfItemWithDifferentReference() { WeightedPool <PlayingCard> pool = new WeightedPool <PlayingCard>(Singleton.DefaultRandom, PlayingCard.Clone); PlayingCard kingOfHearts = new PlayingCard { DisplayName = "King of Hearts", FaceValue = 13, Suit = PlayingCard.Suits.Hearts }; pool.Add(kingOfHearts, 1); PlayingCard selectedCard = pool.Choose(); Assert.AreNotEqual(kingOfHearts, selectedCard); Assert.AreEqual(kingOfHearts.FaceValue, selectedCard.FaceValue); Assert.AreEqual(kingOfHearts.DisplayName, selectedCard.DisplayName); Assert.AreEqual(kingOfHearts.Suit, selectedCard.Suit); Assert.AreEqual(1, pool.Count); }
public GameObject SpawnWeightedRandomObject(string groupTag) { TryFindGroupTag(groupTag); int randomValue = Random.Range(0, poolGroupsDictionary[groupTag].totalWeight + 1); for (int i = 0; i < poolGroupsDictionary[groupTag].poolsInGroup.Count; i++) { WeightedPool weightedPool = poolGroupsDictionary[groupTag].poolsInGroup[i]; if (randomValue <= weightedPool.weight) { return(SpawnObject(weightedPool.pool.poolTag)); } randomValue -= weightedPool.weight; } Debug.LogError($"Return null with {randomValue} weight. Maybe weight are not assigned."); return(null); }
protected virtual void SpawnMonsters(Rectangle room) { int numberOfMonsters = Program.Game.Random.Next(MaximumMonstersPerRoom + 1); var monsterPool = new WeightedPool <Entity.EntityFactoryDelegate>(); monsterPool.Add(MonsterFactory.CreateRat, 50); monsterPool.Add(MonsterFactory.CreateHound, GetWeightFromDungeonLevel(new SortedDictionary <int, int> { { 1, 10 }, { 3, 30 }, { 5, 50 } })); for (int i = 0; i < numberOfMonsters; i++) { int x = Program.Game.Random.Next(room.Left + 1, room.Right); int y = Program.Game.Random.Next(room.Top + 1, room.Bottom); var monsterFactory = monsterPool.Pick(); var monster = monsterFactory(x, y, Program.Game.DungeonLevel); Program.Game.Entities.Add(monster); } }
public void Add_WhenItemArgumentIsNull_WillThrowArgumentNullException() { WeightedPool <string> pool = new WeightedPool <string>(); Assert.ThrowsException <ArgumentNullException>(() => pool.Add(null, 1)); }
public void Add_WhenWeightIsNegative_WillThrowArgumentException() { WeightedPool <int> pool = new WeightedPool <int>(); Assert.ThrowsException <ArgumentException>(() => pool.Add(12, -5)); }
public TetrisGame(ILog logger, int row = 10, int column = 20, WeightedPool <BlockUnit> objectPool = null, Queue <BlockUnit> initialQueue = null) { this.logger = logger; logger.Debug($"TetrisInstance Creating : row{row},column{column}"); TetrisConfig config = new TetrisConfig(); config.Load(); if (!config.Load()) { config.Save(); } if (objectPool == null && config.UseCustomObjectList) { var serializable = (SerializableObjectPool.Load(typeof(SerializableObjectPool), ConfigBase.Directory, config.ObjectListFile) as SerializableObjectPool); if (serializable == null) { serializable = new SerializableObjectPool() { ObjectPool = DefaultObjectPool }; serializable.Save(ConfigBase.Directory, config.ObjectListFile); } objectPool = serializable.ObjectPool; } timer = new Timer(); timer.Interval = TimerSpan; timer.Elapsed += new ElapsedEventHandler((object sender, ElapsedEventArgs e) => controller?.OnTimerTick()); Setting = new GameSetting() { Row = row, Column = column }; field = new Field(row, column); _state = new GameState() { Round = 0, Score = 0, RemovedLines = new Dictionary <int, int>() }; _gameWatch = new Stopwatch(); _playData = new GamePlayData(); ObjectPool = objectPool ?? TetrisGame.DefaultObjectPool; _objectQueue = initialQueue ?? new Queue <BlockUnit>(); field.OnBlockChanged += (object sender, Point point) => { //logger.Debug($"Block was changed:{point}"); }; field.OnRoundStart += (object sender) => { if (RecordPlayDataEnabled) { _gameWatch.Restart(); } lock (_objectQueue) { field.SetObject(Dequeue()); } }; field.OnBlockPlaced += (object sender, BlockObject obj) => { //logger.Debug("Block was placed"); Draw(); }; field.OnLinesRemoved += (object sender, int[] lines, int eroded) => { if (lines.Length != 0) { if (!_state.RemovedLines.ContainsKey(lines.Length)) { _state.RemovedLines.Add(lines.Length, 0); } _state.RemovedLines[lines.Length]++; } }; field.OnRoundEnd += (object sender, RoundResult result) => { logger.Debug($"Round {_state.Round} End"); _state.Round++; _state.Score += result.Score; if (MaxRound > 0 && _state.Round >= MaxRound) { timer.Stop(); OnGameEnd?.Invoke(this, new GameResult() { Score = State.Score, Round = State.Round }); return; } if (RecordPlayDataEnabled) { _playData.Save(); } field.StartRound(); }; field.OnGameOver += (object sender) => { logger.Debug("Game Over"); timer.Stop(); OnGameEnd?.Invoke(this, new GameResult() { Score = State.Score, Round = State.Round }); }; OnGameEnd += (object sender, GameResult result) => { logger.Debug(State.Score); _gameWatch.Stop(); }; }
static TetrisGame() { DefaultObjectPool = new WeightedPool <BlockUnit>(Enum.GetValues(typeof(Kind)).Cast <Kind>().Select(x => new WeightedPool <BlockUnit> .WeightedItem(1, x.GetObject())).ToList()); }
public void Choose_WhenPoolHas0Items_WillThrowInvalidOperationException() { WeightedPool <int> pool = new WeightedPool <int>(Singleton.DefaultRandom, x => x); Assert.ThrowsException <InvalidOperationException>(() => pool.Choose()); }