Example #1
0
        private static int RollToHit(int numAttacks)
        {
            var hits = 0;

            for (var i = 0; i < numAttacks; i++)
            {
                if (DieRoller.Percentage(20))
                {
                    hits++;
                }
            }

            return(hits);
        }
Example #2
0
        public void Act(IReadonlyAdventureGame game, IAdventurePlayer player)
        {
            if (player.CurrentLocation.Level == 1)
            {
                if (!player.EventRecord.ContainsKey(EventIds.Dwarves))
                {
                    if (DieRoller.SixSider(2))
                    {
                        player.EventRecord.Add(EventIds.Dwarves, 1);
                        player.ChatClient.PostDirectMessage(player, "A hunched dwarf just walked " +
                                                            "around a corner, saw you, threw a little axe at you (which missed), cursed, and ran away.");
                        var addItem = new AddToLocation(ItemFactory.GetInstance(game, Item.LittleAxe),
                                                        player.CurrentLocation);
                        addItem.Do(null, null);

                        return;
                    }
                }
            }

            if (player.EventRecord.ContainsKey(EventIds.Dwarves))
            {
                if (Monsters.Count == 0)
                {
                    SpawnDwarfs(player.CurrentLocation);
                    return;
                }

                var dwarfs = Monsters.Where(d => d.CurrentLocation != null).ToArray();

                foreach (var dwarf in dwarfs)
                {
                    if (dwarf.CurrentLocation.LocationId != Location.Nowhere)
                    {
                        // Dwarf is not dead
                        var possibleMoves = new List <Location>();

                        var movesAvailable = (dwarf.CurrentLocation.MonsterValidMoves.Count > 0)
                            ? dwarf.CurrentLocation.MonsterValidMoves
                            : dwarf.CurrentLocation.ValidMoves;

                        foreach (var move in movesAvailable)
                        {
                            var found = game.Dungeon.TryGetLocation(move.Destination, out var potentialMove);

                            if (found && (potentialMove.Level == 1) &&
                                (potentialMove.NumberOfExits > 1) &&
                                (move.Destination != dwarf.CurrentLocation.LocationId))
                            {
                                possibleMoves.Add(move.Destination);
                            }
                        }

                        // Has the dwarf seen the player?
                        if ((dwarf.HasSeenPlayer && (player.CurrentLocation.Level == 1)) ||
                            dwarf.PrevLocation.LocationId.Equals(player.CurrentLocation.LocationId) ||
                            dwarf.CurrentLocation.LocationId.Equals(player.CurrentLocation.LocationId))
                        {
                            // Stick with the player
                            dwarf.HasSeenPlayer   = true;
                            dwarf.PrevLocation    = dwarf.CurrentLocation;
                            dwarf.CurrentLocation = player.CurrentLocation;

                            continue;
                        }

                        dwarf.HasSeenPlayer = false;

                        // Random movement
                        var moveToLocation = 0;

                        if (possibleMoves.Count > 1)
                        {
                            var dieRoll = DieRoller.Percentage();
                            moveToLocation = (dieRoll * possibleMoves.Count) / 100;
                        }

                        game.Dungeon.TryGetLocation(possibleMoves[moveToLocation], out var newLocation);

                        dwarf.PrevLocation    = dwarf.CurrentLocation;
                        dwarf.CurrentLocation = newLocation;
                    }
                }

                var numDwarfs  = 0;
                var numAttacks = 0;

                foreach (var dwarf in Monsters.Where(d => d.CurrentLocation != null))
                {
                    if (player.CurrentLocation.LocationId.Equals(dwarf.CurrentLocation.LocationId))
                    {
                        // A dwarf is in the room with the player
                        numDwarfs++;
                        if (dwarf.CurrentLocation.LocationId.Equals(dwarf.PrevLocation.LocationId))
                        {
                            numAttacks++;
                        }
                    }
                }

                if (numDwarfs > 0)
                {
                    if (numDwarfs == 1)
                    {
                        player.ChatClient.PostDirectMessage(player, "There is a nasty-looking dwarf in the room with you!");

                        if (numAttacks == 1)
                        {
                            player.ChatClient.PostDirectMessage(player, "The dwarf lunges at you with a wickedly sharp knife!");
                        }
                    }
                    else
                    {
                        player.ChatClient.PostDirectMessage(player, $"There are {numDwarfs} nasty-looking dwarfs in the room with you!");

                        if (numAttacks > 0)
                        {
                            player.ChatClient.PostDirectMessage(player, "The dwarfs lunge at you with wickedly sharp knives!");
                        }
                    }

                    if (numAttacks > 0)
                    {
                        var numberOfHits = RollToHit(numAttacks);

                        if (numberOfHits > 0)
                        {
                            // Player is hit by the knife and dies?
                            var woundDescr = (numberOfHits == 1)
                                ? "A knife sinks into your flesh"
                                : ($"{numberOfHits} knives pierce your body");
                            player.ChatClient.PostDirectMessage(player, ($"{woundDescr} and you feel your ") +
                                                                "lifeblood ebbing away...");
                            player.Statuses.Add(PlayerStatus.IsDead);
                            game.EndOfGame(player);
                        }
                        else
                        {
                            player.ChatClient.PostDirectMessage(player, "Phew - missed! You deftly dodge out of the way of danger!");
                        }
                    }
                }
            }
        }
Example #3
0
        public void Act(IReadonlyAdventureGame game, IAdventurePlayer player)
        {
            if (Monsters.Count == 0)
            {
                SpawnPirates();
                return;
            }

            var pirates = Monsters.Where(d => d.CurrentLocation != null).ToArray();

            foreach (var pirate in pirates)
            {
                if (pirate.CurrentLocation.LocationId != Location.Nowhere)
                {
                    // Pirate is still active
                    var possibleMoves = new List <Location>();

                    var movesAvailable = (pirate.CurrentLocation.MonsterValidMoves.Count > 0)
                        ? pirate.CurrentLocation.MonsterValidMoves
                        : pirate.CurrentLocation.ValidMoves;

                    foreach (var move in movesAvailable)
                    {
                        var found = game.Dungeon.TryGetLocation(move.Destination, out var potentialMove);

                        if (found && (potentialMove.Level == 1) &&
                            (potentialMove.NumberOfExits > 1) &&
                            (move.Destination != pirate.CurrentLocation.LocationId))
                        {
                            possibleMoves.Add(move.Destination);
                        }
                    }

                    // Has the pirate seen the player?
                    if ((pirate.HasSeenPlayer && (player.CurrentLocation.Level == 1)) ||
                        pirate.CurrentLocation.LocationId.Equals(player.CurrentLocation.LocationId))
                    {
                        // Stick with the player
                        pirate.HasSeenPlayer   = true;
                        pirate.PrevLocation    = pirate.CurrentLocation;
                        pirate.CurrentLocation = player.CurrentLocation;

                        player.ChatClient.PostDirectMessage(player, "You hear a rustling noise behind you...");
                    }
                    else
                    {
                        pirate.HasSeenPlayer = false;
                    }

                    var playerLocation = player.CurrentLocation.LocationId;

                    if (possibleMoves.Contains(playerLocation) && !pirate.HasSeenPlayer)
                    {
                        // Pirate is 1 move away from the player
                        player.ChatClient.PostDirectMessage(player, "You hear a rustling noise in the darkness...");
                        continue;
                    }

                    if (!pirate.HasSeenPlayer)
                    {
                        // Random movement
                        var moveToLocation = 0;

                        if (possibleMoves.Count > 1)
                        {
                            var dieRoll = DieRoller.Percentage();
                            moveToLocation = (dieRoll * possibleMoves.Count) / 100;
                        }

                        game.Dungeon.TryGetLocation(possibleMoves[moveToLocation], out var newLocation);

                        pirate.PrevLocation    = pirate.CurrentLocation;
                        pirate.CurrentLocation = newLocation;

                        continue;
                    }

                    // Steal treasure?
                    if (player.Inventory.HasTreasure())
                    {
                        if (DieRoller.Percentage(49))
                        {
                            var found = game.Dungeon.TryGetLocation(Location.PirateChestCave, out var pirateChest);

                            if (found)
                            {
                                var treasures = player.Inventory.GetTreasures();

                                foreach (var treasure in treasures)
                                {
                                    player.Inventory.RemoveItem(treasure);
                                    var addItem = new AddToLocation(treasure, pirateChest);
                                    addItem.Do(null, null);
                                }

                                game.Dungeon.TryGetLocation(Location.Nowhere, out var nowhere);
                                pirate.CurrentLocation = nowhere;
                                pirate.PrevLocation    = nowhere;

                                player.ChatClient.PostDirectMessage(player, "...and a pirate jumps out of the darkness!");
                                player.ChatClient.PostDirectMessage(player, "Before you can react, he has swiped all of " +
                                                                    "your treasure and vanished into the gloom! As he leaves, you hear the pirate laughing: " +
                                                                    "'I'll hide this treasure in my maze!'");
                            }
                        }
                    }
                    else
                    {
                        pirate.HasSeenPlayer = false;
                    }
                }
            }
        }