예제 #1
0
        int RemoveResource(string resourceType, CPos cell, int amount = 1)
        {
            if (!Content.Contains(cell))
            {
                return(0);
            }

            var content = Content[cell];

            if (content.Type == null || content.Type != resourceType)
            {
                return(0);
            }

            var oldDensity = content.Density;
            var density    = Math.Max(0, oldDensity - amount);

            if (density == 0)
            {
                Content[cell]           = ResourceLayerContents.Empty;
                Map.CustomTerrain[cell] = byte.MaxValue;
                --resCells;

                CellChanged?.Invoke(cell, null);
            }
            else
            {
                Content[cell] = new ResourceLayerContents(content.Type, density);
                CellChanged?.Invoke(cell, content.Type);
            }

            return(oldDensity - density);
        }
예제 #2
0
        int AddResource(string resourceType, CPos cell, int amount = 1)
        {
            if (!Content.Contains(cell))
            {
                return(0);
            }

            if (resourceType == null || !info.ResourceTypes.TryGetValue(resourceType, out var resourceInfo))
            {
                return(0);
            }

            var content = Content[cell];

            if (content.Type == null)
            {
                content = CreateResourceCell(resourceType, cell, 0);
            }

            if (content.Type != resourceType)
            {
                return(0);
            }

            var oldDensity = content.Density;
            var density    = Math.Min(resourceInfo.MaxDensity, oldDensity + amount);

            Content[cell] = new ResourceLayerContents(content.Type, density);

            CellChanged?.Invoke(cell, content.Type);

            return(density - oldDensity);
        }
예제 #3
0
        public void UpdateCell(CPos cell)
        {
            var uv = cell.ToMPos(Map);

            if (!Map.Resources.Contains(uv))
            {
                return;
            }

            var tile = Map.Resources[uv];
            var t    = Tiles[uv];

            var newTile    = ResourceLayerContents.Empty;
            var newTerrain = byte.MaxValue;

            if (Resources.TryGetValue(tile.Type, out var type))
            {
                newTile = new ResourceLayerContents
                {
                    Type    = type,
                    Density = CalculateCellDensity(type, cell)
                };

                newTerrain = Map.Rules.TerrainInfo.GetTerrainIndex(type.Info.TerrainType);
            }

            // Nothing has changed
            if (newTile.Type == t.Type && newTile.Density == t.Density)
            {
                return;
            }

            UpdateNetWorth(t.Type, t.Density, newTile.Type, newTile.Density);
            Tiles[uv]             = newTile;
            Map.CustomTerrain[uv] = newTerrain;
            CellChanged?.Invoke(cell, type);

            // Neighbouring cell density depends on this cell
            foreach (var d in CVec.Directions)
            {
                var neighbouringCell = cell + d;
                if (!Tiles.Contains(neighbouringCell))
                {
                    continue;
                }

                var neighbouringTile = Tiles[neighbouringCell];
                var density          = CalculateCellDensity(neighbouringTile.Type, neighbouringCell);
                if (neighbouringTile.Density == density)
                {
                    continue;
                }

                UpdateNetWorth(neighbouringTile.Type, neighbouringTile.Density, neighbouringTile.Type, density);
                neighbouringTile.Density = density;
                Tiles[neighbouringCell]  = neighbouringTile;

                CellChanged?.Invoke(neighbouringCell, type);
            }
        }
예제 #4
0
        protected RendererCellContents CreateRenderCellContents(WorldRenderer wr, ResourceLayerContents contents, CPos cell)
        {
            if (contents.Type != null && contents.Density > 0 && Info.ResourceTypes.TryGetValue(contents.Type, out var resourceInfo))
            {
                return(new RendererCellContents(contents.Type, contents.Density, resourceInfo, ChooseVariant(contents.Type, cell), wr.Palette(resourceInfo.Palette)));
            }

            return(RendererCellContents.Empty);
        }
예제 #5
0
        protected virtual void WorldLoaded(World w, WorldRenderer wr)
        {
            foreach (var cell in w.Map.AllCells)
            {
                var resource = world.Map.Resources[cell];
                if (!ResourceTypesByIndex.TryGetValue(resource.Type, out var resourceType))
                {
                    continue;
                }

                if (!AllowResourceAt(resourceType, cell))
                {
                    continue;
                }

                Content[cell] = CreateResourceCell(resourceType, cell, resource.Index);
            }

            if (!info.RecalculateResourceDensity)
            {
                return;
            }

            // Set initial density based on the number of neighboring resources
            foreach (var cell in w.Map.AllCells)
            {
                var resource = Content[cell];
                if (resource.Type == null || !info.ResourceTypes.TryGetValue(resource.Type, out var resourceInfo))
                {
                    continue;
                }

                var adjacent   = 0;
                var directions = CVec.Directions;
                for (var i = 0; i < directions.Length; i++)
                {
                    var c = cell + directions[i];
                    if (Content.Contains(c) && Content[c].Type == resource.Type)
                    {
                        ++adjacent;
                    }
                }

                // Adjacent includes the current cell, so is always >= 1
                var density = Math.Max(int2.Lerp(0, resourceInfo.MaxDensity, adjacent, 9), 1);
                Content[cell] = new ResourceLayerContents(resource.Type, density);
            }
        }