Example #1
0
        public bool Load(string fileLocation)
        {
            try
            {
                string extension = Path.GetExtension(fileLocation).ToLowerInvariant();

                var lines = File.ReadAllLines(fileLocation).ToList();
                Logger.LogDebug("read " + lines.Count + " pieces from " + Path.Combine(BlueprintManager.BlueprintPath, m_name + ".blueprint"));

                List <PieceEntry> pieceEntries = new List <PieceEntry>();
                List <Vector3>    snapPoints   = new List <Vector3>();

                bool parsingSnapPoints = false;
                foreach (var line in lines)
                {
                    if (line == HeaderSnapPoints)
                    {
                        parsingSnapPoints = true;
                        continue;
                    }
                    if (line == HeaderPieces)
                    {
                        parsingSnapPoints = false;
                        continue;
                    }
                    if (parsingSnapPoints)
                    {
                        snapPoints.Add(ParsePosition(line));
                        continue;
                    }
                    PieceEntry pieceEntry;
                    switch (extension)
                    {
                    case ".vbuild":
                        pieceEntry = PieceEntry.FromVBuild(line);
                        break;

                    case ".blueprint":
                        pieceEntry = PieceEntry.FromBlueprint(line);
                        break;

                    default:
                        Logger.LogWarning("Unknown extension " + extension);
                        return(false);
                    }

                    pieceEntries.Add(pieceEntry);
                }

                m_pieceEntries = pieceEntries.ToArray();
                m_snapPoints   = snapPoints.ToArray();

                return(true);
            }
            catch (Exception e)
            {
                Logger.LogError(e);
                return(false);
            }
        }
Example #2
0
        public bool Save()
        {
            if (m_pieceEntries == null)
            {
                Logger.LogWarning("No pieces stored to save");
            }
            else
            {
                using (TextWriter tw = new StreamWriter(m_fileLocation))
                {
                    if (m_snapPoints.Count() > 0)
                    {
                        tw.WriteLine(HeaderSnapPoints);
                        foreach (Vector3 pos in m_snapPoints)
                        {
                            tw.WriteLine(string.Join(";", PieceEntry.InvariantString(pos.x), PieceEntry.InvariantString(pos.y), PieceEntry.InvariantString(pos.z)));
                        }
                    }

                    tw.WriteLine(HeaderPieces);
                    foreach (var piece in m_pieceEntries)
                    {
                        tw.WriteLine(piece.line);
                    }

                    Logger.LogDebug("Wrote " + m_pieceEntries.Length + " pieces to " + Path.Combine(BlueprintManager.BlueprintPath, m_name + ".blueprint"));
                }
            }

            return(true);
        }
Example #3
0
        private static bool PlaceBlueprint(Player player, Piece piece)
        {
            Blueprint bp        = Instance.m_blueprints[piece.m_name];
            var       transform = player.m_placementGhost.transform;
            var       position  = player.m_placementGhost.transform.position;
            var       rotation  = player.m_placementGhost.transform.rotation;

            bool placeDirect = ZInput.GetButton("Crouch");

            if (placeDirect && !allowDirectBuildConfig.Value)
            {
                MessageHud.instance.ShowMessage(MessageHud.MessageType.Center, "$msg_direct_build_disabled");
                return(false);
            }

            if (ZInput.GetButton("AltPlace"))
            {
                Vector2 extent = bp.GetExtent();
                FlattenTerrain.FlattenForBlueprint(transform, extent.x, extent.y, bp.m_pieceEntries);
            }

            uint cntEffects = 0u;
            uint maxEffects = 10u;

            GameObject blueprintPrefab = PrefabManager.Instance.GetPrefab(Blueprint.BlueprintPrefabName);
            GameObject blueprintObject = Object.Instantiate(blueprintPrefab, position, rotation);

            ZDO blueprintZDO = blueprintObject.GetComponent <ZNetView>().GetZDO();

            blueprintZDO.Set(ZDOBlueprintName, bp.m_name);
            ZDOIDSet createdPlans = new ZDOIDSet();

            for (int i = 0; i < bp.m_pieceEntries.Length; i++)
            {
                PieceEntry entry = bp.m_pieceEntries[i];
                // Final position
                Vector3 entryPosition = position + transform.forward * entry.posZ + transform.right * entry.posX + new Vector3(0, entry.posY, 0);

                // Final rotation
                Quaternion entryQuat = new Quaternion(entry.rotX, entry.rotY, entry.rotZ, entry.rotW);
                entryQuat.eulerAngles += rotation.eulerAngles;

                // Get the prefab of the piece or the plan piece
                string prefabName = entry.name;
                if (!placeDirect)
                {
                    prefabName += PlanPiecePrefab.PlannedSuffix;
                }

                GameObject prefab = PrefabManager.Instance.GetPrefab(prefabName);
                if (!prefab)
                {
                    Jotunn.Logger.LogWarning(entry.name + " not found, you are probably missing a dependency for blueprint " + bp.m_name + ", not placing @ " + entryPosition);
                    continue;
                }

                // Instantiate a new object with the new prefab
                GameObject gameObject = Object.Instantiate(prefab, entryPosition, entryQuat);

                ZNetView zNetView = gameObject.GetComponent <ZNetView>();
                if (!zNetView)
                {
                    Jotunn.Logger.LogWarning("No ZNetView for " + gameObject + "!!??");
                }
                else if (gameObject.TryGetComponent(out PlanPiece planPiece))
                {
                    planPiece.PartOfBlueprint(blueprintZDO.m_uid, entry);
                    createdPlans.Add(planPiece.GetPlanPieceID());
                }

                // Register special effects
                CraftingStation craftingStation = gameObject.GetComponentInChildren <CraftingStation>();
                if (craftingStation)
                {
                    player.AddKnownStation(craftingStation);
                }
                Piece newpiece = gameObject.GetComponent <Piece>();
                if (newpiece)
                {
                    newpiece.SetCreator(player.GetPlayerID());
                }
                PrivateArea privateArea = gameObject.GetComponent <PrivateArea>();
                if (privateArea)
                {
                    privateArea.Setup(Game.instance.GetPlayerProfile().GetName());
                }
                WearNTear wearntear = gameObject.GetComponent <WearNTear>();
                if (wearntear)
                {
                    wearntear.OnPlaced();
                }
                TextReceiver textReceiver = gameObject.GetComponent <TextReceiver>();
                if (textReceiver != null)
                {
                    textReceiver.SetText(entry.additionalInfo);
                }

                // Limited build effects
                if (cntEffects < maxEffects)
                {
                    newpiece.m_placeEffect.Create(gameObject.transform.position, rotation, gameObject.transform, 1f);
                    player.AddNoise(50f);
                    cntEffects++;
                }

                // Count up player builds
                Game.instance.GetPlayerProfile().m_playerStats.m_builds++;
            }

            blueprintZDO.Set(PlanPiece.zdoBlueprintPiece, createdPlans.ToZPackage().GetArray());

            // Dont set the blueprint piece and clutter the world with it
            return(false);
        }
Example #4
0
        public bool Capture(Vector3 position, float radius)
        {
            var rot = Camera.main.transform.rotation.eulerAngles;

            Logger.LogDebug("Collecting piece information");

            var       numPieces   = 0;
            var       collected   = new List <Piece>();
            var       snapPoints  = new List <Vector3>();
            Transform centerPiece = null;

            foreach (var piece in BlueprintManager.GetPiecesInRadius(position, radius))
            {
                if (piece.name.StartsWith(BlueprintRunePrefab.BlueprintSnapPointName))
                {
                    snapPoints.Add(piece.transform.position);
                    WearNTear wearNTear = piece.GetComponent <WearNTear>();
                    wearNTear.Remove();
                    continue;
                }
                if (piece.name.StartsWith(BlueprintRunePrefab.BlueprintCenterPointName))
                {
                    if (centerPiece == null)
                    {
                        centerPiece = piece.transform;
                    }
                    else
                    {
                        Logger.LogWarning("Multiple center points! Ignoring @ " + piece.transform.position);
                    }
                    WearNTear wearNTear = piece.GetComponent <WearNTear>();
                    wearNTear.Remove();
                    continue;
                }
                piece.GetComponent <WearNTear>()?.Highlight();
                collected.Add(piece);
                numPieces++;
            }

            Logger.LogDebug($"Found {numPieces} in a radius of {radius:F2}");
            Vector3 center;

            if (centerPiece == null)
            {
                // Relocate Z
                var minZ = 9999999.9f;
                var minX = 9999999.9f;
                var minY = 9999999.9f;

                foreach (var piece in collected.Where(x => x.m_category != Piece.PieceCategory.Misc && x.IsPlacedByPlayer()))
                {
                    minX = Math.Min(piece.m_nview.GetZDO().m_position.x, minX);
                    minZ = Math.Min(piece.m_nview.GetZDO().m_position.z, minZ);
                    minY = Math.Min(piece.m_nview.GetZDO().m_position.y, minY);
                }

                Logger.LogDebug($"{minX} - {minY} - {minZ}");

                center = new Vector3(minX, minY, minZ);
            }
            else
            {
                center = centerPiece.position;
            }

            // select and order instance piece entries
            var pieces = collected.Where(x => x.IsPlacedByPlayer() && x.m_category != Piece.PieceCategory.Misc)
                         .OrderBy(x => x.transform.position.y)
                         .ThenBy(x => x.transform.position.x)
                         .ThenBy(x => x.transform.position.z);

            if (m_pieceEntries == null)
            {
                m_pieceEntries = new PieceEntry[pieces.Count()];
            }
            else if (m_pieceEntries.Length > 0)
            {
                Array.Clear(m_pieceEntries, 0, m_pieceEntries.Length - 1);
                Array.Resize(ref m_pieceEntries, pieces.Count());
            }

            uint i = 0;

            foreach (var piece in pieces)
            {
                var pos = piece.m_nview.GetZDO().m_position - center;

                var quat = piece.m_nview.GetZDO().m_rotation;
                quat.eulerAngles = piece.transform.eulerAngles;

                var additionalInfo = piece.GetComponent <TextReceiver>() != null?piece.GetComponent <TextReceiver>().GetText() : "";

                string pieceName = piece.name.Split('(')[0];
                if (pieceName.EndsWith(PlanPiecePrefab.PlannedSuffix))
                {
                    pieceName = pieceName.Replace(PlanPiecePrefab.PlannedSuffix, null);
                }
                m_pieceEntries[i++] = new PieceEntry(pieceName, piece.m_category.ToString(), pos, quat, additionalInfo);
            }

            if (m_snapPoints == null)
            {
                m_snapPoints = new Vector3[snapPoints.Count()];
            }
            for (int j = 0; j < snapPoints.Count(); j++)
            {
                m_snapPoints[j] = snapPoints[j] - center;
            }

            return(true);
        }
Example #5
0
        private bool GhostInstantiate(GameObject baseObject)
        {
            var ret = true;

            ZNetView.m_forceDisableInit = true;

            try
            {
                var pieces = new List <PieceEntry>(m_pieceEntries);
                var maxX   = pieces.Max(x => x.posX);
                var maxZ   = pieces.Max(x => x.posZ);

                foreach (Vector3 snapPoint in m_snapPoints)
                {
                    GameObject snapPointObject = new GameObject
                    {
                        name  = "_snappoint",
                        layer = LayerMask.NameToLayer("piece"),
                        tag   = "snappoint"
                    };
                    snapPointObject.SetActive(false);
                    Object.Instantiate(snapPointObject, snapPoint, Quaternion.identity, baseObject.transform);
                }

                //Tiny collider for accurate placement
                GameObject gameObject = new GameObject(PlaceColliderName);
                gameObject.transform.SetParent(baseObject.transform);
                SphereCollider sphereCollider = gameObject.AddComponent <SphereCollider>();
                sphereCollider.radius = 0.002f;

                var tf = baseObject.transform;
                tf.rotation = Camera.main.transform.rotation;
                var quat = Quaternion.Euler(0, tf.rotation.eulerAngles.y, 0);
                tf.SetPositionAndRotation(tf.position, quat);
                tf.position -= tf.right * (maxX / 2f);
                tf.position += tf.forward * 5f;

                var prefabs = new Dictionary <string, GameObject>();
                foreach (var piece in pieces.GroupBy(x => x.name).Select(x => x.FirstOrDefault()))
                {
                    var go = PrefabManager.Instance.GetPrefab(piece.name);
                    if (!go)
                    {
                        Logger.LogWarning("No prefab found for " + piece.name + "! You are probably missing a dependency for blueprint " + m_name);
                    }
                    else
                    {
                        go.transform.SetPositionAndRotation(go.transform.position, quat);
                        prefabs.Add(piece.name, go);
                    }
                }

                for (int i = 0; i < pieces.Count; i++)
                {
                    PieceEntry piece = pieces[i];
                    var        pos   = tf.position + tf.right * piece.GetPosition().x + tf.forward * piece.GetPosition().z +
                                       new Vector3(0, piece.GetPosition().y, 0);

                    var q = Quaternion.Euler(0, tf.transform.rotation.eulerAngles.y + piece.GetRotation().eulerAngles.y, 0);

                    GameObject pieceObject = new GameObject("piece_entry (" + i + ")");
                    pieceObject.transform.SetParent(tf);
                    pieceObject.transform.rotation = q;
                    pieceObject.transform.position = pos;

                    if (prefabs.TryGetValue(piece.name, out var prefab))
                    {
                        GameObject ghostPrefab;
                        Vector3    ghostPosition;
                        Quaternion ghostRotation;
                        if (prefab.TryGetComponent(out WearNTear wearNTear) && wearNTear.m_new)
                        {
                            //Only instantiate the visual part
                            ghostPrefab   = wearNTear.m_new;
                            ghostRotation = ghostPrefab.transform.localRotation;
                            ghostPosition = ghostPrefab.transform.localPosition;
                        }
                        else
                        {
                            //No WearNTear?? Just use the entire prefab
                            ghostPrefab   = prefab;
                            ghostRotation = Quaternion.identity;
                            ghostPosition = Vector3.zero;
                        }

                        var child = Object.Instantiate(ghostPrefab, pieceObject.transform);
                        child.transform.localRotation = ghostRotation;
                        child.transform.localPosition = ghostPosition;
                        MakeGhost(child);

                        //Doors have a dynamic object that also needs to be added
                        if (prefab.TryGetComponent(out Door door))
                        {
                            GameObject doorPrefab = door.m_doorObject;
                            var        doorChild  = Object.Instantiate(doorPrefab, pieceObject.transform);
                            doorChild.transform.localRotation = doorPrefab.transform.localRotation;
                            doorChild.transform.localPosition = doorPrefab.transform.localPosition;
                            MakeGhost(doorChild);
                        }
                    }
                }
            }
Example #6
0
 private Vector3 ParsePosition(string line)
 {
     string[] parts = line.Split(';');
     return(new Vector3(PieceEntry.InvariantFloat(parts[0]), PieceEntry.InvariantFloat(parts[1]), PieceEntry.InvariantFloat(parts[2])));
 }