Example #1
0
        public BuildingVisual(BuildingConfig buildingConfig, int cell)
        {
            Offset = buildingConfig.Offset;
            this.buildingConfig = buildingConfig;
            this.cell           = cell;

            Vector3 positionCBC = Grid.CellToPosCBC(cell, Grid.SceneLayer.Building);

            Visualizer = GameUtil.KInstantiate(buildingConfig.BuildingDef.BuildingPreview, positionCBC, Grid.SceneLayer.Ore, "BlueprintModBuildingVisualizer", LayerMask.NameToLayer("Place"));
            Visualizer.transform.SetPosition(positionCBC);
            Visualizer.SetActive(true);

            if (Visualizer.GetComponent <Rotatable>() != null)
            {
                Visualizer.GetComponent <Rotatable>().SetOrientation(buildingConfig.Orientation);
            }

            KBatchedAnimController batchedAnimController = Visualizer.GetComponent <KBatchedAnimController>();

            if (batchedAnimController != null)
            {
                batchedAnimController.visibilityType = KAnimControllerBase.VisibilityType.OffscreenUpdate;
                batchedAnimController.isMovable      = true;
                batchedAnimController.Offset         = buildingConfig.BuildingDef.GetVisualizerOffset() + buildingConfig.BuildingDef.placementPivot;
                batchedAnimController.TintColour     = GetVisualizerColor(cell);

                batchedAnimController.SetLayer(LayerMask.NameToLayer("Place"));
            }

            else
            {
                Visualizer.SetLayerRecursively(LayerMask.NameToLayer("Place"));
            }
        }
Example #2
0
        public TileVisual(BuildingConfig buildingConfig, int cell)
        {
            Offset = buildingConfig.Offset;
            this.buildingConfig = buildingConfig;
            hasReplacementLayer = buildingConfig.BuildingDef.ReplacementLayer != ObjectLayer.NumLayers;

            Vector3 positionCBC = Grid.CellToPosCBC(cell, Grid.SceneLayer.Building);

            Visualizer = GameUtil.KInstantiate(buildingConfig.BuildingDef.BuildingPreview, positionCBC, Grid.SceneLayer.Ore, "BlueprintModTileVisualizer", LayerMask.NameToLayer("Place"));
            Visualizer.transform.SetPosition(positionCBC);
            Visualizer.SetActive(true);

            if (Visualizer.GetComponent <Rotatable>() != null)
            {
                Visualizer.GetComponent <Rotatable>().SetOrientation(buildingConfig.Orientation);
            }

            KBatchedAnimController batchedAnimController = Visualizer.GetComponent <KBatchedAnimController>();

            if (batchedAnimController != null)
            {
                batchedAnimController.visibilityType = KAnimControllerBase.VisibilityType.OffscreenUpdate;
                batchedAnimController.isMovable      = true;
                batchedAnimController.Offset         = buildingConfig.BuildingDef.GetVisualizerOffset() + buildingConfig.BuildingDef.placementPivot;
            }

            VisualsUtilities.SetVisualizerColor(cell, GetVisualizerColor(cell), Visualizer, buildingConfig);
            DirtyCell = cell;
            UpdateGrid(cell);
        }
Example #3
0
        public UtilityVisual(BuildingConfig buildingConfig, int cell)
        {
            Offset = buildingConfig.Offset;
            this.buildingConfig = buildingConfig;
            this.cell           = cell;

            Vector3 positionCBC = Grid.CellToPosCBC(cell, buildingConfig.BuildingDef.SceneLayer);

            Visualizer = GameUtil.KInstantiate(buildingConfig.BuildingDef.BuildingPreview, positionCBC, Grid.SceneLayer.Ore, "BlueprintModUtilityVisualizer", LayerMask.NameToLayer("Place"));
            Visualizer.transform.SetPosition(positionCBC);
            Visualizer.SetActive(true);

            if (Visualizer.GetComponent <Rotatable>() != null)
            {
                Visualizer.GetComponent <Rotatable>().SetOrientation(buildingConfig.Orientation);
            }

            KBatchedAnimController batchedAnimController = Visualizer.GetComponent <KBatchedAnimController>();

            if (batchedAnimController != null)
            {
                IUtilityNetworkMgr utilityNetworkManager = buildingConfig.BuildingDef.BuildingComplete.GetComponent <IHaveUtilityNetworkMgr>().GetNetworkManager();

                if (utilityNetworkManager != null)
                {
                    string animation = utilityNetworkManager.GetVisualizerString((UtilityConnections)buildingConfig.Flags) + "_place";

                    if (batchedAnimController.HasAnimation(animation))
                    {
                        batchedAnimController.Play(animation);
                    }
                }

                batchedAnimController.visibilityType = KAnimControllerBase.VisibilityType.Always;
                batchedAnimController.isMovable      = true;
                batchedAnimController.Offset         = buildingConfig.BuildingDef.GetVisualizerOffset() + buildingConfig.BuildingDef.placementPivot;
                batchedAnimController.TintColour     = GetVisualizerColor(cell);

                batchedAnimController.SetLayer(LayerMask.NameToLayer("Place"));
            }

            else
            {
                Visualizer.SetLayerRecursively(LayerMask.NameToLayer("Place"));
            }

            VisualsUtilities.SetVisualizerColor(cell, GetVisualizerColor(cell), Visualizer, buildingConfig);
        }
Example #4
0
        public bool ReadBinary()
        {
            if (File.Exists(FilePath))
            {
                BuildingConfiguration.Clear();
                DigLocations.Clear();

                try {
                    using (BinaryReader reader = new BinaryReader(File.Open(FilePath, FileMode.Open))) {
                        FriendlyName = reader.ReadString();

                        int buildingCount = reader.ReadInt32();
                        for (int i = 0; i < buildingCount; ++i)
                        {
                            BuildingConfig buildingConfig = new BuildingConfig();
                            if (!buildingConfig.ReadBinary(reader))
                            {
                                return(false);
                            }

                            BuildingConfiguration.Add(buildingConfig);
                        }

                        int digLocationCount = reader.ReadInt32();
                        for (int i = 0; i < digLocationCount; ++i)
                        {
                            DigLocations.Add(new Vector2I(reader.ReadInt32(), reader.ReadInt32()));
                        }
                    }

                    return(true);
                }

                catch (System.Exception exception) {
                    Debug.LogError("Error when loading blueprint: " + FilePath + ",\n" + nameof(exception) + ":" + exception.Message);
                }
            }

            return(false);
        }
Example #5
0
        /// <summary>
        /// Reads the contents of a JSON-formatted file and adds it to the blueprint.
        /// </summary>
        public void ReadJSON()
        {
            if (File.Exists(FilePath))
            {
                try {
                    using StreamReader reader       = File.OpenText(FilePath);
                    using JsonTextReader jsonReader = new JsonTextReader(reader);

                    JObject rootObject = (JObject)JToken.ReadFrom(jsonReader).Root;

                    JToken friendlyNameToken = rootObject.SelectToken("friendlyname");
                    JToken buildingsToken    = rootObject.SelectToken("buildings");
                    JToken digCommandsToken  = rootObject.SelectToken("digcommands");

                    if (friendlyNameToken != null && friendlyNameToken.Type == JTokenType.String)
                    {
                        FriendlyName = friendlyNameToken.Value <string>();
                    }

                    if (buildingsToken != null)
                    {
                        JArray buildingTokens = buildingsToken.Value <JArray>();

                        if (buildingTokens != null)
                        {
                            foreach (JToken buildingToken in buildingTokens)
                            {
                                BuildingConfig buildingConfig = new BuildingConfig();
                                buildingConfig.ReadJSON((JObject)buildingToken);

                                BuildingConfiguration.Add(buildingConfig);
                            }
                        }
                    }

                    if (digCommandsToken != null)
                    {
                        JArray digCommandTokens = digCommandsToken.Value <JArray>();

                        if (digCommandTokens != null)
                        {
                            foreach (JToken digCommandToken in digCommandTokens)
                            {
                                JToken xToken = digCommandToken.SelectToken("x");
                                JToken yToken = digCommandToken.SelectToken("y");

                                if (xToken != null && xToken.Type == JTokenType.Integer || yToken != null && yToken.Type == JTokenType.Integer)
                                {
                                    DigLocations.Add(new Vector2I(xToken == null ? 0 : xToken.Value <int>(), yToken == null ? 0 : yToken.Value <int>()));
                                }

                                else if (xToken == null && yToken == null)
                                {
                                    DigLocations.Add(new Vector2I(0, 0));
                                }
                            }
                        }
                    }

                    CacheCost();
                }

                catch (System.Exception exception) {
                    Debug.Log("Error when loading blueprint: " + FilePath + ",\n" + nameof(exception) + ": " + exception.Message);
                }
            }
        }
Example #6
0
        public static Blueprint CreateBlueprint(Vector2I topLeft, Vector2I bottomRight, FilteredDragTool originTool = null)
        {
            Blueprint blueprint       = new Blueprint(Utilities.GetNewBlueprintLocation("unnamed"));
            int       blueprintHeight = (topLeft.y - bottomRight.y);

            for (int x = topLeft.x; x <= bottomRight.x; ++x)
            {
                for (int y = bottomRight.y; y <= topLeft.y; ++y)
                {
                    int cell = Grid.XYToCell(x, y);

                    if (Grid.IsVisible(cell))
                    {
                        bool emptyCell = true;

                        for (int layer = 0; layer < Grid.ObjectLayers.Length; ++layer)
                        {
                            if (layer == 7)
                            {
                                continue;
                            }

                            GameObject gameObject = Grid.Objects[cell, layer];
                            Building   building;

                            if (gameObject != null && (building = gameObject.GetComponent <Building>()) != null && building.Def.IsBuildable() && (originTool == null || originTool.IsActiveLayer(originTool.GetFilterLayerFromGameObject(gameObject))))
                            {
                                PrimaryElement primaryElement;

                                if ((primaryElement = building.GetComponent <PrimaryElement>()) != null)
                                {
                                    Vector2I centre = Grid.CellToXY(GameUtil.NaturalBuildingCell(building));

                                    BuildingConfig buildingConfig = new BuildingConfig {
                                        Offset      = new Vector2I(centre.x - topLeft.x, blueprintHeight - (topLeft.y - centre.y)),
                                        BuildingDef = building.Def,
                                        Orientation = building.Orientation
                                    };

                                    buildingConfig.SelectedElements.Add(primaryElement.ElementID.CreateTag());
                                    if (building.Def.BuildingComplete.GetComponent <IHaveUtilityNetworkMgr>() != null)
                                    {
                                        buildingConfig.Flags = (int)building.Def.BuildingComplete.GetComponent <IHaveUtilityNetworkMgr>().GetNetworkManager()?.GetConnections(cell, false);
                                    }

                                    if (!blueprint.BuildingConfiguration.Contains(buildingConfig))
                                    {
                                        blueprint.BuildingConfiguration.Add(buildingConfig);
                                    }
                                }

                                emptyCell = false;
                            }
                        }

                        if (emptyCell && (!Grid.IsSolidCell(cell) || Grid.Objects[cell, 7] != null && Grid.Objects[cell, 7].name == "DigPlacer"))
                        {
                            Vector2I digLocation = new Vector2I(x - topLeft.x, blueprintHeight - (topLeft.y - y));

                            if (!blueprint.DigLocations.Contains(digLocation))
                            {
                                blueprint.DigLocations.Add(digLocation);
                            }
                        }
                    }
                }
            }

            return(blueprint);
        }
Example #7
0
        public static void SetVisualizerColor(int cell, Color color, GameObject visualizer, BuildingConfig buildingConfig)
        {
            if (buildingConfig.BuildingDef.isKAnimTile && buildingConfig.BuildingDef.BlockTileAtlas != null && !BlueprintsState.ColoredCells.ContainsKey(cell))
            {
                BlueprintsState.ColoredCells.Add(cell, new CellColorPayload(color, buildingConfig.BuildingDef.TileLayer, buildingConfig.BuildingDef.ReplacementLayer));
                TileVisualizer.RefreshCell(cell, buildingConfig.BuildingDef.TileLayer, buildingConfig.BuildingDef.ReplacementLayer);
            }

            if (visualizer.GetComponent <KBatchedAnimController>() != null)
            {
                visualizer.GetComponent <KBatchedAnimController>().TintColour = color;
            }
        }
Example #8
0
        public static Blueprint CreateBlueprint(Vector2I topLeft, Vector2I bottomRight, MultiToolParameterMenu filter = null)
        {
            Blueprint blueprint = new Blueprint("unnamed", "");

            int  blueprintHeight    = (topLeft.y - bottomRight.y);
            bool collectingGasTiles = filter.IsActiveLayer(BlueprintsStrings.STRING_BLUEPRINTS_MULTIFILTER_GASTILES);

            for (int x = topLeft.x; x <= bottomRight.x; ++x)
            {
                for (int y = bottomRight.y; y <= topLeft.y; ++y)
                {
                    int cell = Grid.XYToCell(x, y);

                    if (Grid.IsVisible(cell))
                    {
                        bool emptyCell = true;

                        for (int layer = 0; layer < Grid.ObjectLayers.Length; ++layer)
                        {
                            if (layer == 7)
                            {
                                continue;
                            }

                            GameObject gameObject = Grid.Objects[cell, layer];

                            if (gameObject != null && (gameObject.GetComponent <Constructable>() != null || gameObject.GetComponent <Deconstructable>() != null))
                            {
                                Building building;

                                bool validBuilding = (building = gameObject.GetComponent <Building>()) != null;
                                if (!validBuilding && (building = gameObject.GetComponent <BuildingUnderConstruction>()) != null)
                                {
                                    validBuilding = true;
                                }

                                if (gameObject != null && validBuilding && building.Def.IsBuildable() && (filter == null || filter.IsActiveLayer(MultiToolParameterMenu.GetFilterLayerFromGameObject(gameObject))))
                                {
                                    Vector2I centre = Grid.CellToXY(GameUtil.NaturalBuildingCell(building));

                                    BuildingConfig buildingConfig = new BuildingConfig {
                                        Offset      = new Vector2I(centre.x - topLeft.x, blueprintHeight - (topLeft.y - centre.y)),
                                        BuildingDef = building.Def,
                                        Orientation = building.Orientation
                                    };

                                    if (gameObject.GetComponent <Deconstructable>() != null)
                                    {
                                        buildingConfig.SelectedElements.AddRange(gameObject.GetComponent <Deconstructable>().constructionElements);
                                    }

                                    else
                                    {
                                        buildingConfig.SelectedElements.AddRange(Traverse.Create(gameObject.GetComponent <Constructable>()).Field("selectedElementsTags").GetValue <Tag[]>());
                                    }

                                    if (building.Def.BuildingComplete.GetComponent <IHaveUtilityNetworkMgr>() != null)
                                    {
                                        buildingConfig.Flags = (int)building.Def.BuildingComplete.GetComponent <IHaveUtilityNetworkMgr>().GetNetworkManager()?.GetConnections(cell, false);
                                    }

                                    if (!blueprint.BuildingConfiguration.Contains(buildingConfig))
                                    {
                                        blueprint.BuildingConfiguration.Add(buildingConfig);
                                    }

                                    emptyCell = false;
                                }
                            }
                        }

                        if (emptyCell && (collectingGasTiles && !Grid.IsSolidCell(cell) || filter.IsActiveLayer(ToolParameterMenu.FILTERLAYERS.DIGPLACER) && Grid.Objects[cell, 7] != null && Grid.Objects[cell, 7].name == "DigPlacer"))
                        {
                            Vector2I digLocation = new Vector2I(x - topLeft.x, blueprintHeight - (topLeft.y - y));

                            if (!blueprint.DigLocations.Contains(digLocation))
                            {
                                blueprint.DigLocations.Add(digLocation);
                            }
                        }
                    }
                }
            }

            blueprint.CacheCost();
            return(blueprint);
        }