Beispiel #1
0
 public Biome(string name)
 {
     Name             = name;
     TemperatureRange = new HashSet <Hex.TemperatureLevel>();
     HumidityRange    = new HashSet <Hex.HumidityLevel>();
     BiomeColor       = Colors.Beige;
     Affinities       = new AffinityMap(new RandomGen());
 }
Beispiel #2
0
        public void GenerateRace()
        {
            if (_raceGen == null)
            {
                _raceGen = new RaceGen();
            }

            if (_raceGen.IsEmpty())
            {
                return;
            }

            Landmass    mass         = _world.Map.GetRandomLandmass();
            AffinityMap massAffinity = _world.Map.GetAffinitiesForLandmass(mass);
            Race        genRace      = _raceGen.GenerateRace(massAffinity);

            Console.WriteLine("Rolled race: " + genRace);
            Console.WriteLine(genRace.Affinities);

            // arbitrarily low number to start with
            int highest = -50;
            // random coordinates as default
            Coords candidate      = mass.RandomCoords();
            int    tick           = 0;
            int    tenPercent     = mass.Count / 10;
            bool   tenPercentMore = false;

            foreach (Coords coords in mass.CoordsFromRandomPoint())
            {
                if (tick >= tenPercent)
                {
                    break;
                }
                AffinityMap hexAffinities = _world.Map.GetAffinitiesForCoords(coords);
                int         sim           = genRace.Affinities.GetSimilarityTo(hexAffinities);
                if (sim > highest)
                {
                    highest   = sim;
                    candidate = coords;
                }
                if (!tenPercentMore && highest > 0)
                {
                    tenPercentMore = true;
                }
                if (tenPercentMore)
                {
                    tick++;
                }
            }
            Console.WriteLine("Placing at : " + candidate);
            Console.WriteLine(_world.Map.GetAffinitiesForCoords(candidate));
            Console.WriteLine("--> Final Score: " + highest);
            Console.WriteLine("------------------");
            _world.PlaceRaceAt(genRace, candidate);
        }
Beispiel #3
0
        public Race FinalizeAgainstMap(AffinityMap map)
        {
            AffinityMap finalAffinities = new AffinityMap(_affinities);

            foreach (var wildcard in _wildcards)
            {
                finalAffinities.ResolveWildcardAgainstMap(wildcard.Item1, wildcard.Item2, map);
            }

            return(new Race(finalAffinities, _name));
        }
Beispiel #4
0
        public void ResolveWildcardAgainstMap(string pool, int affinity, AffinityMap other)
        {
            AffinityMap filtered = other.FilterByPool(pool);

            if (filtered.Count == 0)
            {
                ResolveWildcard(pool, affinity);
            }
            else
            {
                string aspect = filtered.SelectAspectByAffinity();
                SetAffinity(aspect, affinity);
            }
        }
Beispiel #5
0
        public int GetSimilarityTo(AffinityMap other)
        {
            int total = 0;

            foreach (var aspect in _affinities.Keys)
            {
                int otherAffinity = other.GetAffinity(aspect);
                if (otherAffinity != 0)
                {
                    total += (GetAffinity(aspect) * other.GetAffinity(aspect)) / 2;
                }
            }

            return(total);
        }
Beispiel #6
0
        public AffinityMap GetAffinitiesForLandmass(Landmass landmass)
        {
            if (!Landmasses.Contains(landmass))
            {
                throw new KeyNotFoundException("Tried to get affinities for a landmass that is not in this HexMap.");
            }

            if (landmass.Affinities == null)
            {
                var totalAffinityScore = new Dictionary <string, int>();
                foreach (Coords coords in landmass.Hexes)
                {
                    Hex         hex        = GetHexAt(coords);
                    AffinityMap affinities = hex.Affinities;
                    foreach (var aspect in affinities.AspectList)
                    {
                        if (!totalAffinityScore.ContainsKey(aspect))
                        {
                            totalAffinityScore[aspect] = 0;
                        }
                        totalAffinityScore[aspect] += affinities.GetAffinity(aspect);
                    }
                }

                AffinityMap newAffinityMap = new AffinityMap(_rand);

                foreach (var aspect in totalAffinityScore.Keys)
                {
                    int score    = totalAffinityScore[aspect];
                    int affinity = score / landmass.TotalHexes;
                    // don't allow 0 here; just weak affinities
                    if (affinity == 0)
                    {
                        affinity = (score > 0) ? 1 : -1;
                    }
                    newAffinityMap.SetAffinity(aspect, affinity);
                }

                landmass.Affinities = newAffinityMap;
            }

            return(landmass.Affinities);
        }
Beispiel #7
0
        public AffinityMap CombineWith(AffinityMap other)
        {
            AffinityMap combined = new AffinityMap(_rand);

            foreach (var aspect in _affinities.Keys)
            {
                int otherAffinity = other.GetAffinity(aspect);
                if (otherAffinity != 0)
                {
                    int combinedVal = CombineAffinities(GetAffinity(aspect), other.GetAffinity(aspect));
                    if (combinedVal != 0)
                    {
                        combined.SetAffinity(aspect, combinedVal);
                    }
                }
            }

            return(combined);
        }
Beispiel #8
0
        public AffinityMap FilterByPool(string pool)
        {
            if (!_aspectGlossary.HasPool(pool))
            {
                return(new AffinityMap(this));
            }

            var poolMembers   = _aspectGlossary.GetPool(pool);
            var theseAspects  = _affinities.Keys;
            var commonMembers = theseAspects.Intersect(poolMembers);

            AffinityMap newMap = new AffinityMap(_rand);

            foreach (var member in commonMembers)
            {
                newMap.SetAffinity(member, _affinities[member]);
            }

            return(newMap);
        }
Beispiel #9
0
        public Race GenerateRace(AffinityMap massAffinity)
        {
            if (_prototypes.Count == 0)
            {
                return(null);
            }

            var races     = new List <Race>();
            var raceTable = new RandomTable <Race>(new RandomGen());

            foreach (var prototype in _prototypes.Values)
            {
                Race        race         = prototype.FinalizeAgainstMap(massAffinity);
                AffinityMap raceAffinity = race.Affinities;
                int         score        = race.Affinities.GetSimilarityTo(massAffinity);
                raceTable.Add(race, score);
            }

            Race rolledRace = raceTable.Roll();

            _prototypes.Remove(rolledRace.RaceName.Plural);
            return(rolledRace);
        }
Beispiel #10
0
        /**
         * <summary>
         * Method to setup biomes, as read from the biomes definition stream.
         * </summary>
         */
        private void BiomeSetup()
        {
            XDocument biomeDefs      = XDocument.Load(_defsStream);
            XElement  defsRoot       = biomeDefs.Element("root");
            var       temperatureMax = Enum.GetValues(typeof(Hex.TemperatureLevel)).Cast <int>().Last();
            var       humidityMax    = Enum.GetValues(typeof(Hex.HumidityLevel)).Cast <int>().Last();

            foreach (XElement node in defsRoot.Elements())
            {
                // check for disabling attribute, skip this node if it's set to true
                XAttribute disabledAttr = node.Attribute("disabled");
                if (disabledAttr != null && disabledAttr.Value == "true")
                {
                    continue;
                }

                // validate it has the minimum information
                if (node.Element("name") == null ||
                    node.Element("hex_color") == null ||
                    node.Element("primary_aspect") == null ||
                    node.Element("temperature_lb") == null ||
                    node.Element("temperature_ub") == null ||
                    node.Element("humidity_lb") == null ||
                    node.Element("humidity_ub") == null)
                {
                    continue;
                }

                string biomeName     = node.Element("name").Value;
                string biomeColorStr = node.Element("hex_color").Value;

                // Can't deal with empty name or color, abandon ship
                if (biomeName.Length == 0 || biomeColorStr.Length == 0)
                {
                    continue;
                }

                // Regardless of how long the color string is, we'll only process 3 bytes' worth.
                // Beyond that, the string has no meaning.
                int    numChars   = Math.Min(biomeColorStr.Length, 6);
                byte[] colorBytes = new byte[3];
                try
                {
                    for (int i = 0; i < numChars; i += 2)
                    {
                        colorBytes[i / 2] = Convert.ToByte(biomeColorStr.Substring(i, 2), 16);
                    }
                }
                catch
                {
                    // Bail if the format conversion fails
                    continue;
                }

                Color       biomeColor      = Color.FromArgb(0xff, colorBytes[0], colorBytes[1], colorBytes[2]);
                AffinityMap biomeAffinities = new AffinityMap(_rand);

                try
                {
                    string primaryAspect = node.Element("primary_aspect").Value;
                    biomeAffinities.MaximizeAffinity(primaryAspect);
                }
                catch (InvalidAspectException)
                {
                    // Bail if this is not a valid primary aspect
                    continue;
                }

                Biome newBiome = new Biome(biomeName, biomeColor, biomeAffinities);

                try
                {
                    int temperatureLowerBound = Math.Max(int.Parse(node.Element("temperature_lb").Value), 0);
                    int temperatureUpperBound = Math.Min(int.Parse(node.Element("temperature_ub").Value), temperatureMax);
                    for (int i = temperatureLowerBound; i <= temperatureUpperBound; i++)
                    {
                        newBiome.AddTemperature((Hex.TemperatureLevel)i);
                    }

                    int humidityLowerBound = Math.Max(int.Parse(node.Element("humidity_lb").Value), 0);
                    int humidityUpperBound = Math.Min(int.Parse(node.Element("humidity_ub").Value), humidityMax);
                    for (int i = humidityLowerBound; i <= humidityUpperBound; i++)
                    {
                        newBiome.AddHumidity((Hex.HumidityLevel)i);
                    }
                }
                catch
                {
                    // Bail on any parsing exceptions
                    continue;
                }

                Biomes.Add(newBiome);
            }
        }
Beispiel #11
0
 public Biome(string name, Color color, AffinityMap affinities) : this(name, color)
 {
     _affinities = affinities;
 }
Beispiel #12
0
 public RacePrototype(Name name)
 {
     _affinities = new AffinityMap(new RandomGen());
     _wildcards  = new Stack <Tuple <string, int> >();
     _name       = name;
 }
Beispiel #13
0
 public Race(AffinityMap affinities, Name name)
 {
     _affinities = affinities;
     _name       = name;
 }
Beispiel #14
0
 public AffinityMap(AffinityMap other)
 {
     _rand       = other._rand;
     _affinities = new Dictionary <string, int>(other._affinities);
 }