コード例 #1
0
            async Task AsyncAdd(ItemSet set)
            {
                await AsyncAskList("Add an item.", 0, async name =>
                {
                    Item item = Item.Find(name);

                    if (item == null)
                    {
                        await ReplyMentionAsync($"An item of the name '{name}' could not be found.");
                    }
                    else
                    {
                        ItemWeightCount itemWeightCount = new ItemWeightCount()
                        {
                            item    = item,
                            minimum = await AsyncAskTryParse <int>($"Minimum", int.TryParse),
                            maximum = await AsyncAskTryParse <int>($"Maximum", int.TryParse),
                            weight  = await AsyncAskTryParse <float>($"Weight", float.TryParse)
                        };

                        set.itemWeights.Add(itemWeightCount);
                    }

                    return(true);
                });
            }
コード例 #2
0
ファイル: ItemSet.cs プロジェクト: whattheshark/Town-Crier
        public void GetBest(Random random, ref ItemWeightCount best, ref double bestWeight)
        {
            foreach (ItemWeightCount item in itemWeights)
            {
                double value = item.weight * random.NextDouble();

                if (value < bestWeight)
                {
                    bestWeight = value;
                    best       = item;
                }
            }

            foreach (ItemSet set in subSets)
            {
                set.GetBest(random, ref best, ref bestWeight);
            }
        }
コード例 #3
0
        public static string Explore(string name, Location location, float luck, Predicate <Location> isKnown, out Encounter bestEncounter, out Location bestLocation, out ItemCount bestItem)
        {
            Random random = new Random();

            double bestEncounterWeight = 1f;

            bestEncounter = null;

            location.exploreSet?.encounterSet?.GetBest(random, ref bestEncounter, ref bestEncounterWeight);

            double bestLocationWeight = bestEncounterWeight;

            bestLocation = null;

            foreach (Connection connection in location.connections)
            {
                bool isA = connection.locationA == location;

                Location other  = isA ? connection.locationB : connection.locationA;
                float    chance = isA ? connection.chanceToFindB : connection.chanceToFindA;

                if (!isKnown(other))
                {
                    double value = random.NextDouble() / chance;

                    if (value < bestLocationWeight)
                    {
                        bestLocation       = other;
                        bestLocationWeight = value;
                    }
                }
            }

            double          bestItemWeight      = bestEncounterWeight;
            ItemWeightCount bestItemWeightCount = null;

            bestItem = null;

            location.exploreSet?.itemSet?.GetBest(random, ref bestItemWeightCount, ref bestEncounterWeight);

            if (bestItemWeightCount != null)
            {
                bestItem = new ItemCount(bestItemWeightCount.item, random.Next(bestItemWeightCount.minimum, bestItemWeightCount.maximum + 1));
            }

            string message = $"{name} explore {location.name}.\n";

            if (location.descriptions.Count > 0)
            {
                message += location.descriptions[random.Next(location.descriptions.Count)] + "\n";
            }

            message += "\n";

            if (bestItem != null)
            {
                bestEncounter = null;
                bestLocation  = null;

                if (bestItem.count > 1)
                {
                    message += $"You found {bestItem.count} {bestItem.item.name}s.";
                }
                else
                {
                    message += $"You found a {bestItem.item.name}.";
                }
            }
            else if (bestLocation != null)
            {
                bestEncounter = null;

                message += $"{name} discovered {bestLocation.name}.";
            }
            else if (bestEncounter != null)
            {
                message += $"{name} encountered a {bestEncounter.name}.\n";
                message += $"{bestEncounter.possibleDescriptions[random.Next(bestEncounter.possibleDescriptions.Count)]}\n";
                message += $"";
            }
            else
            {
                message += $"{name} didn't find anything!";
            }

            return(message);
        }