예제 #1
0
        public bool TrySpawnUnit(IEncampment encampment)
        {
            if (Randomizer.GetRandom01() <= BarbarianConfig.WaterSpawnChance)
            {
                var navalInfo = SpawningTools.TryGetValidSpawn(encampment, AvailableUnitsLogic.NavalTemplateSelector);

                if (navalInfo.IsValidSpawn)
                {
                    UnitFactory.BuildUnit(navalInfo.LocationOfUnit, navalInfo.TemplateToBuild, CivFactory.BarbarianCiv);

                    return(true);
                }
            }

            var landInfo = SpawningTools.TryGetValidSpawn(encampment, AvailableUnitsLogic.LandTemplateSelector);

            if (landInfo.IsValidSpawn)
            {
                UnitFactory.BuildUnit(landInfo.LocationOfUnit, landInfo.TemplateToBuild, CivFactory.BarbarianCiv);

                return(true);
            }
            else
            {
                return(false);
            }
        }
        private void OnCityGainedBuilding(Tuple <ICity, IBuilding> data)
        {
            var city     = data.Item1;
            var building = data.Item2;

            var cityLocation = CityLocationCanon.GetOwnerOfPossession(city);
            var cityOwner    = CityPossessionCanon.GetOwnerOfPossession(city);

            foreach (var templateToBuild in building.Template.FreeUnits)
            {
                var validCell = GetValidNearbyCell(cityLocation, templateToBuild, cityOwner);

                UnitFactory.BuildUnit(validCell, templateToBuild, cityOwner);
            }
        }
예제 #3
0
        public void DecomposeUnits(SerializableMapData mapData)
        {
            foreach (var unitData in mapData.Units)
            {
                var unitLocation    = Grid.GetCellAtCoordinates(unitData.Location);
                var templateToBuild = AvailableUnitTemplates.Where(template => template.name.Equals(unitData.Template)).First();
                var unitOwner       = CivilizationFactory.AllCivilizations.Where(civ => civ.Template.Name.Equals(unitData.Owner)).First();
                var promotionTree   = PromotionTreeComposer.DecomposePromotionTree(unitData.PromotionTree);

                var newUnit = UnitFactory.BuildUnit(unitLocation, templateToBuild, unitOwner, promotionTree);

                newUnit.CurrentMovement  = unitData.CurrentMovement;
                newUnit.CurrentHitpoints = unitData.Hitpoints;
                newUnit.Experience       = unitData.Experience;
                newUnit.Level            = unitData.Level;

                if (unitData.CurrentPath != null)
                {
                    newUnit.CurrentPath = unitData.CurrentPath.Select(coord => Grid.GetCellAtCoordinates(coord)).ToList();
                }

                if (unitData.IsSetUpToBombard)
                {
                    newUnit.SetUpToBombard();
                }
            }
        }
 private void TryAddUnit(IHexCell location)
 {
     if (ActiveCivilization != null && ActiveTemplate != null &&
         UnitFactory.CanBuildUnit(location, ActiveTemplate, ActiveCivilization)
         )
     {
         UnitFactory.BuildUnit(location, ActiveTemplate, ActiveCivilization);
     }
 }
예제 #5
0
        public void PlaceStartingUnitsInRegion(
            MapRegion region, ICivilization owner, IMapTemplate mapTemplate
            )
        {
            if (owner.Template.IsBarbaric)
            {
                return;
            }

            var centralLocation = GetBestStartingCell(region);

            if (centralLocation == null)
            {
                throw new InvalidOperationException("Failed to find an appropriate central location");
            }

            UnitFactory.BuildUnit(centralLocation, mapTemplate.StartingUnits[0], owner);

            for (int i = 1; i < mapTemplate.StartingUnits.Count; i++)
            {
                var unitTemplate = mapTemplate.StartingUnits[i];

                var location = Grid.GetCellsInRadius(centralLocation, 2).Where(
                    cell => !UnitPositionCanon.GetPossessionsOfOwner(cell).Any() &&
                    UnitFactory.CanBuildUnit(cell, unitTemplate, owner) &&
                    cell != null
                    ).FirstOrDefault();

                if (location == null)
                {
                    throw new InvalidOperationException(
                              string.Format("Failed to place starting unit {0} for civ {1}", unitTemplate, owner)
                              );
                }
                else
                {
                    UnitFactory.BuildUnit(location, unitTemplate, owner);
                }
            }
        }
예제 #6
0
        public void Execute(ICity targetCity)
        {
            if (BuildingToConstruct != null)
            {
                BuildingFactory.BuildBuilding(BuildingToConstruct, targetCity);
            }
            else
            {
                var cityOwner    = CityPossessionCanon.GetOwnerOfPossession(targetCity);
                var cityLocation = CityLocationCanon.GetOwnerOfPossession(targetCity);

                var newUnit = UnitFactory.BuildUnit(cityLocation, UnitToConstruct, cityOwner);

                newUnit.Experience = StartingExperienceLogic.GetStartingExperienceForUnit(newUnit, targetCity);

                foreach (var promotion in LocalPromotionLogic.GetLocalPromotionsForCity(targetCity))
                {
                    newUnit.PromotionTree.AppendPromotion(promotion);
                }
            }
        }
예제 #7
0
        public IUnit BuildGreatPerson(GreatPersonType type, ICivilization owner)
        {
            if (!CanBuildGreatPerson(type, owner))
            {
                throw new InvalidOperationException("CanBuildGreatPerson must return true on the arguments");
            }

            var civCapital      = CapitalCityCanon.GetCapitalOfCiv(owner);
            var capitalLocation = CityLocationCanon.GetOwnerOfPossession(civCapital);

            var personTemplate = UnitConfig.GetTemplateForGreatPersonType(type);

            var location = GetValidNearbyCell(capitalLocation, personTemplate, owner);

            var newPerson = UnitFactory.BuildUnit(location, personTemplate, owner);

            CivSignals.GreatPersonBorn.OnNext(new GreatPersonBirthData()
            {
                Owner = owner, GreatPerson = newPerson, Type = type
            });

            return(newPerson);
        }
예제 #8
0
        public ICity Create(IHexCell location, ICivilization owner, string name)
        {
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }
            else if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            var newCityGameObject = GameObject.Instantiate(CityPrefab);

            Container.InjectGameObject(newCityGameObject);

            newCityGameObject.transform.position = location.AbsolutePosition;
            newCityGameObject.name = name;
            newCityGameObject.transform.SetParent(CityContainer, true);

            var newCity = newCityGameObject.GetComponent <City>();

            CityLocationCanon.ChangeOwnerOfPossession(newCity, location);

            location.SuppressSlot = true;

            CellModificationLogic.ChangeVegetationOfCell(location, CellVegetation.None);

            if (CityPossessionCanon.CanChangeOwnerOfPossession(newCity, owner))
            {
                CityPossessionCanon.ChangeOwnerOfPossession(newCity, owner);
            }
            else
            {
                throw new CityCreationException("Cannot assign the newly created city to its intended civilization");
            }

            var combatantTemplate = Container.Instantiate <CityCombatantTemplate>(new object[] { newCity });

            newCity.CombatFacade = UnitFactory.BuildUnit(location, combatantTemplate, owner);

            if (CellPossessionCanon.CanChangeOwnerOfPossession(location, newCity))
            {
                CellPossessionCanon.ChangeOwnerOfPossession(location, newCity);
            }
            else
            {
                throw new CityCreationException("Cannot assign the given location to the newly created city");
            }

            foreach (var neighbor in Grid.GetNeighbors(location))
            {
                if (CellPossessionCanon.CanChangeOwnerOfPossession(neighbor, newCity))
                {
                    CellPossessionCanon.ChangeOwnerOfPossession(neighbor, newCity);
                }
            }

            newCity.YieldFocus = YieldFocusType.TotalYield;

            newCity.Population = 1;

            allCities.Add(newCity);

            return(newCity);
        }