axx0/Civ2-clone

Cities don't build anything

Closed this issue · 1 comments

Apologies for noob way of posting the solution:

Game.ActionsCities.cs

`                // Change shield production
                city.ShieldsProgress += city.Production;
                var maxUnitIndex = Game.Rules.UnitTypes.Length;
                int cost;
                // Units
                if (city.ItemInProduction < maxUnitIndex)
                {
                    var unitType = Game.Rules.UnitTypes[city.ItemInProduction];
                    cost = unitType.Cost * 10;
                    if (city.ShieldsProgress >= cost)
                    {
                        city.ShieldsProgress = 0;
                        Game.CreateUnit(unitType.Type, city.X, city.Y, false, true, false, false, city.Owner.Id, 0, 0, city.X, city.Y, CommodityType.Hides, OrderType.NoOrders, city, city.X, city.Y, 0, 0);
                    }

                }
                // Improvements
                else
                {
                    var improvementIndex = city.ItemInProduction - Game.Rules.UnitTypes.Length + 1;
                    var improvement = Game.Rules.Improvements[improvementIndex];
                    cost = improvement.Cost * 10;
                    if (city.ShieldsProgress >= cost)
                    {
                        city.ShieldsProgress = 0;
                        city.AddImprovement(improvement);
                    }
                }

For my convenience I also made additional method of creating units in Game.cs:

        public Unit CreateUnit(UnitType type, int x, int y, bool dead, bool firstMove, bool greyStarShield, bool veteran, int civId,
                                    int movePointsLost, int hitPointsLost, int prevX, int prevY, CommodityType caravanCommodity, OrderType orders,
                                    City homeCity, int goToX, int goToY, int linkOtherUnitsOnTop, int linkOtherUnitsUnder)
        {
            var validTile = CurrentMap.IsValidTileC2(x, y);

            var civilization = AllCivilizations[civId];
            Unit unit = new Unit
            {
                Id = civilization.Units.Count,
                TypeDefinition = Rules.UnitTypes[(int)type],
                Dead = dead || !validTile,
                CurrentLocation = validTile ? CurrentMap.TileC2(x, y) : null,
                Type = type,
                X = x,
                Y = y,
                MovePointsLost = movePointsLost,
                HitPointsLost = hitPointsLost,
                FirstMove = firstMove,
                GreyStarShield = greyStarShield,
                Veteran = veteran,
                Owner = civilization,
                PrevXY = new int[] { prevX, prevY },
                CaravanCommodity = caravanCommodity,
                Order = orders,
                HomeCity = homeCity,
                GoToX = goToX,
                GoToY = goToY,
                LinkOtherUnitsOnTop = linkOtherUnitsOnTop,
                LinkOtherUnitsUnder = linkOtherUnitsUnder
            };

            civilization.Units.Add(unit);
            return unit;
        }
axx0 commented

Well it looks good. Try to commit this into your repo and make a PR. Otherwise I'll check how this works when I have more time.