public BuildingPartDestroyedEvent(BuildingBlock buildingBlock, HitInfo info)
 {
     BuildingPart = new BuildingPart(buildingBlock);
     Info = info;
     string bonename = StringPool.Get(info.HitBone);
     HitBone = bonename == "" ? "unknown" : bonename;
 }
Exemple #2
0
        public void On_Placement(BuildingEvent be)
        {
            if (StructureRecorder)
            {
                Player player = be.Builder;
                string name;
                if (DataStore.ContainsKey("StructureRecorder", player.SteamID))
                {
                    name = (string)DataStore.Get("StructureRecorder", player.SteamID);
                }
                else if (DataStore.ContainsKey("StructureRecorderEveryone", "ON"))
                {
                    name = (string)DataStore.Get("StructureRecorderEveryone", "ON");
                }
                else
                {
                    return;
                }
                BuildingPart bp = be.BuildingPart;
                Structure    structure;
                Structures.TryGetValue(name, out structure);
                if (structure == null)
                {
                    return;
                }

                structure.AddComponent(bp);
                player.Message("Added " + bp.Name);
            }
        }
Exemple #3
0
        void AddPart(BuildingPart part)
        {
            Debug.Log("Adding part" + part);
            switch (part)
            {
            case BuildingPart.Main:
                mainBody.SetActive(true);
                break;

            case BuildingPart.Upper:
                upper.SetActive(true);
                holder.SetActive(true);
                break;

            case BuildingPart.LeftEngine:
                leftEngine.SetActive(true);
                break;

            case BuildingPart.RightEngine:
                rightEngine.SetActive(true);
                break;

            case BuildingPart.Holder:
                holder.SetActive(true);
                break;
            }
            CheckActiveParts();
        }
Exemple #4
0
        /// <summary>
        /// Creates the test project.
        /// </summary>
        /// <returns>Project.</returns>
        public Project CreateTestProject()
        {
            var building = new Building
            {
                Name = "Vabi building",
            };

            var project = new Project
            {
                Name        = "Test project",
                Environment = new Environment
                {
                    OutsideTemperature = 7.0,
                    GroundTemperature  = 10.0
                },
                Building = building
            };

            var part = new BuildingPart
            {
                Name = "ground floor - front wall",
                Area = 16.50,
            };

            // TODO: building.BuildingParts.Add(part);

            // TODO

            return(project);
        }
Exemple #5
0
    public bool CanPlace(int _databaseID, Vector3 _position)
    {
        bool canPlace = true;

        WorldChunk insideChunk = IsInsideWorldChunk(_position);

        if (insideChunk != null)
        {
            Vector3 chunkPos = WorldToChunk(insideChunk, _position);
            int     chunkX = (int)chunkPos.x, chunkY = (int)chunkPos.y, chunkZ = (int)chunkPos.z;

            BuildingPart part = FindObjectOfType <BuildingPartDatabaseManager>().GetBuildingPart(_databaseID);

            for (int x = chunkX - (int)part.gridOffset.x; x < chunkX - (int)part.gridOffset.x + (int)part.gridSize.x; x++)
            {
                for (int y = chunkY - (int)part.gridOffset.y; y < chunkY - (int)part.gridOffset.y + (int)part.gridSize.y; y++)
                {
                    for (int z = chunkZ - (int)part.gridOffset.z; z < chunkZ - (int)part.gridOffset.z + (int)part.gridSize.z; z++)
                    {
                        if (insideChunk.gridData[x, y, z] != 0)
                        {
                            canPlace = false;
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            canPlace = false;
        }

        return(canPlace);
    }
        public BuildingPartGradeChangeEvent(BuildingBlock bb, BuildingGrade.Enum bgrade, BasePlayer player)
        {
            BuildingPart = new BuildingPart(bb);
            Builder = Server.GetPlayer(player);
            grade = bgrade;

            HasPrivilege = (bool)bb.CallMethod("CanChangeToGrade", bgrade, player);
        }
Exemple #7
0
 public BuildingEvent(Construction construction, Construction.Target target, BuildingBlock bb, bool bNeedsValidPlacement)
 {
     Builder             = Server.GetPlayer(target.player);
     BuildingPart        = new BuildingPart(bb);
     Construction        = construction;
     Target              = target;
     NeedsValidPlacement = bNeedsValidPlacement;
 }
        public BuildingPartGradeChangeEvent(BuildingBlock bb, BuildingGrade.Enum bgrade, BasePlayer player)
        {
            BuildingPart = new BuildingPart(bb);
            Builder      = Server.GetPlayer(player);
            grade        = bgrade;

            HasPrivilege = (bool)bb.CallMethod("CanChangeToGrade", bgrade, player);
        }
Exemple #9
0
 public BuildingEvent(Construction construction, Construction.Target target, BuildingBlock bb, bool bNeedsValidPlacement)
 {
     Builder = Server.GetPlayer(target.player);
     BuildingPart = new BuildingPart(bb);
     Construction = construction;
     Target = target;
     NeedsValidPlacement = bNeedsValidPlacement;
 }
        public BuildingPartDestroyedEvent(BuildingBlock bb, HitInfo info)
        {
            Info         = info;
            BuildingPart = new BuildingPart(bb);
            string bonename = StringPool.Get(info.HitBone);

            HitBone = bonename == "" ? "unknown" : bonename;
        }
        public void RemoveComponent(BuildingPart bp)
        {
            var v3        = new SerializedVector3(bp.Location - Origo.Position.ToVector3());
            var q         = new SerializedQuaternion(bp.buildingBlock.transform.rotation);
            var component = new StructureComponent(bp, v3, q);

            if (StructureComponents.ContainsKey(component.ToString()))
            {
                StructureComponents.Remove(component.ToString());
            }
        }
Exemple #12
0
        public void On_CombatEntityHurt(CombatEntityHurtEvent cehe)
        {
            if (StructureRecorder)
            {
                if (cehe.Attacker == null || cehe.Victim == null)
                {
                    return;
                }

                Player       player = cehe.Attacker.ToPlayer();
                BuildingPart bp     = cehe.Victim.ToBuildingPart();
                if (player == null || bp == null)
                {
                    return;
                }

                string name;
                if (DataStore.ContainsKey("StructureRecorder", player.SteamID))
                {
                    name = (string)DataStore.Get("StructureRecorder", player.SteamID);
                }
                else if (DataStore.ContainsKey("StructureRecorderEveryone", "ON"))
                {
                    name = (string)DataStore.Get("StructureRecorderEveryone", "ON");
                }
                else
                {
                    return;
                }

                Structure structure;
                Structures.TryGetValue(name, out structure);
                if (structure == null)
                {
                    return;
                }

                foreach (int dmg in Enumerable.Range(0, cehe.DamageAmounts.Length))
                {
                    cehe.DamageAmounts[dmg] = 0f;
                }

                if (cehe.DamageType == Rust.DamageType.Bullet)
                {
                    RecordAllConnected(structure, bp);
                    player.Message("Added everything including connected parts");
                    return;
                }
                structure.AddComponent(bp);
                player.Message("Added " + bp.Name);
            }
        }
        static void RecordAllConnected(Structure structure, BuildingPart bp)
        {
            var partList   = new List <object>();
            var posList    = new List <Vector3>();
            int layerMasks = LayerMask.GetMask("Construction", "Construction Trigger", "Deployed");

            partList.Add(bp.buildingBlock);
            posList.Add(bp.Location);
            structure.AddComponent(bp);
            for (int i = 0; i < posList.Count; i++)
            {
                Collider[] colliders = Physics.OverlapSphere(posList[i], 3f, layerMasks);
                foreach (Collider collider in colliders)
                {
                    if (collider.isTrigger)
                    {
                        continue;
                    }
                    if (collider.GetComponentInParent <BuildingBlock>() != null)
                    {
                        BuildingBlock buildingBlock = collider.GetComponentInParent <BuildingBlock>();
                        if (!partList.Contains(buildingBlock))
                        {
                            partList.Add(buildingBlock);
                            posList.Add(buildingBlock.transform.position);
                            structure.AddComponent(new BuildingPart(buildingBlock));
                        }
                    }
                    else if (collider.GetComponentInParent <Deployable>() != null)
                    {
                        Deployable deployable = collider.GetComponentInParent <Deployable>();
                        if (!partList.Contains(deployable))
                        {
                            partList.Add(deployable);
                            posList.Add(deployable.transform.position);
                            structure.AddComponent(deployable);
                        }
                    }
                    else if (collider.GetComponentInParent <Spawnable>() != null)
                    {
                        Spawnable spawnable = collider.GetComponentInParent <Spawnable>();
                        if (!partList.Contains(spawnable))
                        {
                            partList.Add(spawnable);
                            posList.Add(spawnable.transform.position);
                            structure.AddComponent(spawnable);
                        }
                    }
                }
            }
        }
Exemple #14
0
        public BuildingEvent(BuildingPart bp, Player player, float prof)
        {
            _block       = bp.buildingBlock;
            Proficiency  = prof;
            BuildingPart = bp;
            Builder      = player;

            if (_block.blockDefinition != null)
            {
                BlockName     = _block.blockDefinition.name;
                Sockets       = _block.blockDefinition.sockets;
                BlockFullName = _block.blockDefinition.fullname;
            }
        }
 public StructureComponent(BuildingPart bp, SerializedVector3 v3, SerializedQuaternion q)
 {
     Grade         = bp.buildingBlock.grade;
     Prefab        = bp.buildingBlock.LookupPrefabName();
     LocalPosition = v3;
     LocalRotation = q;
     Health        = (float)((int)Math.Floor((double)(bp.Health / 85)) * 85);
     if (bp.buildingBlock.HasSlot(BaseEntity.Slot.Lock))
     {
         var baseLock = bp.buildingBlock.GetSlot(BaseEntity.Slot.Lock) as BaseLock;
         if (baseLock == null)
         {
             HasCodeLock = false;
             HasKeyLock  = false;
         }
         else if (baseLock.GetComponent <CodeLock>())
         {
             HasCodeLock = true;
             HasKeyLock  = false;
             CodeLock codeLock = baseLock.GetComponent <CodeLock>();
             if (!string.IsNullOrEmpty((string)codeLock.GetFieldValue("code")))
             {
                 LockCode  = (string)codeLock.GetFieldValue("code");
                 LockWList = new List <ulong>();
                 LockWList = (List <ulong>)codeLock.GetFieldValue("whitelistPlayers");
             }
         }
         else if (baseLock.GetComponent <KeyLock>())
         {
             HasCodeLock = false;
             HasKeyLock  = true;
             KeyLock keyLock = baseLock.GetComponent <KeyLock>();
             int     keyCode = (int)keyLock.GetFieldValue("keyCode");
             keyCode  = (bool)keyLock.GetFieldValue("firstKeyCreated") ? keyCode |= 0x80 : (int)keyLock.GetFieldValue("keyCode");
             LockCode = keyCode.ToString();
         }
     }
 }
        public void AddComponent(BuildingPart bp)
        {
            if (Origo == null)
            {
                Origo = new Origo(new SerializedVector3(bp.Location), new SerializedQuaternion(bp.buildingBlock.transform.rotation));
            }
            var v3        = new SerializedVector3(bp.Location - Origo.Position.ToVector3());
            var q         = new SerializedQuaternion(bp.buildingBlock.transform.rotation);
            var component = new StructureComponent(bp, v3, q);

            if (component == null)
            {
                Pluton.Logger.LogDebug("[StructureRecorder] BuildingPart component is null!");
                return;
            }
            if (!StructureComponents.ContainsKey(component.ToString()))
            {
                StructureComponents.Add(component.ToString(), component);
            }
            else
            {
                StructureComponents[component.ToString()] = component;
            }
        }
Exemple #17
0
    public bool CanPlaceAt(int _x, int _y, int _z, int _databaseID, WorldChunk insideChunk)
    {
        BuildingPart part = FindObjectOfType <BuildingPartDatabaseManager>().GetBuildingPart(_databaseID);

        bool canPlace = true;

        for (int x = _x - (int)part.gridOffset.x; x < _x - (int)part.gridOffset.x + (int)part.gridSize.x; x++)
        {
            for (int y = _y - (int)part.gridOffset.y; y < _y - (int)part.gridOffset.y + (int)part.gridSize.y; y++)
            {
                for (int z = _z - (int)part.gridOffset.z; z < _z - (int)part.gridOffset.z + (int)part.gridSize.z; z++)
                {
                    if (x >= insideChunk.gridData.GetLength(0) || y >= insideChunk.gridData.GetLength(1) || z >= insideChunk.gridData.GetLength(2) ||
                        insideChunk.gridData[x, y, z] != 0)
                    {
                        canPlace = false;
                        break;
                    }
                }
            }
        }

        return(canPlace);
    }
Exemple #18
0
    public void DeleteItem(Vector3 _position)
    {
        WorldChunk insideChunk = IsInsideWorldChunk(_position);

        if (insideChunk != null)
        {
            Vector3 chunkPos = WorldToChunk(insideChunk, _position);
            int     xChunk   = (int)chunkPos.x;
            int     yChunk   = (int)chunkPos.y;
            int     zChunk   = (int)chunkPos.z;

            //Find the Centre Chunk
            //-1=Down -2=Up -3=PositiveX -4=NegativeX -5=PositiveY -6=NegativeY
            if (insideChunk.gridData[xChunk, yChunk, zChunk] < 0)
            {
                while (insideChunk.gridData[xChunk, yChunk, zChunk] < 0)
                {
                    if (insideChunk.gridData[xChunk, yChunk, zChunk] == -1)
                    {
                        yChunk--;
                    }
                    if (insideChunk.gridData[xChunk, yChunk, zChunk] == -2)
                    {
                        yChunk++;
                    }
                    if (insideChunk.gridData[xChunk, yChunk, zChunk] == -3)
                    {
                        xChunk++;
                    }
                    if (insideChunk.gridData[xChunk, yChunk, zChunk] == -4)
                    {
                        xChunk--;
                    }
                    if (insideChunk.gridData[xChunk, yChunk, zChunk] == -5)
                    {
                        zChunk++;
                    }
                    if (insideChunk.gridData[xChunk, yChunk, zChunk] == -6)
                    {
                        zChunk--;
                    }
                }
            }

            if (insideChunk.gridData[xChunk, yChunk, zChunk] > 0)
            {
                BuildingPart part = FindObjectOfType <BuildingPartDatabaseManager>().GetBuildingPart(insideChunk.gridData[xChunk, yChunk, zChunk]);

                for (int x = xChunk - (int)part.gridOffset.x; x < xChunk - (int)part.gridOffset.x + (int)part.gridSize.x; x++)
                {
                    for (int y = yChunk - (int)part.gridOffset.y; y < yChunk - (int)part.gridOffset.y + (int)part.gridSize.y; y++)
                    {
                        for (int z = zChunk - (int)part.gridOffset.z; z < zChunk - (int)part.gridOffset.z + (int)part.gridSize.z; z++)
                        {
                            insideChunk.gridData[x, y, z] = 0;
                        }
                    }
                }

                insideChunk.gridRotationAngles[xChunk, yChunk, zChunk] = 0;
                // Debug.Log("Deleted Block at " + xChunk + "," + yChunk + "," + zChunk);

                Destroy(insideChunk.gridObjects[xChunk, yChunk, zChunk]);
            }
        }
    }
Exemple #19
0
    public void CreateItem(int _databaseID, Vector3 _position, float _rotation, bool ignoreExisting = false)
    {
        WorldChunk insideChunk = IsInsideWorldChunk(_position);

        if (insideChunk != null)
        {
            Vector3 chunkPos = WorldToChunk(insideChunk, _position);

            bool isFromMovement = Vector3.Distance(chunkPos, lastBuildPos) >= 0.5f && buildLock;
            if (!buildLock || isFromMovement)
            {
                int  chunkX = (int)chunkPos.x, chunkY = (int)chunkPos.y, chunkZ = (int)chunkPos.z;
                bool canPlace = false;

                if (!ignoreExisting)
                {
                    while (true)
                    {
                        canPlace = CanPlaceAt(chunkX, chunkY, chunkZ, _databaseID, insideChunk);

                        if (canPlace || chunkY >= insideChunk.gridData.GetLength(1) || isFromMovement)
                        {
                            break;
                        }
                        else
                        {
                            chunkY++;
                        }
                    }
                }
                else
                {
                    canPlace = true;
                }

                if (canPlace)
                {
                    BuildingPart part = FindObjectOfType <BuildingPartDatabaseManager>().GetBuildingPart(_databaseID);
                    //Debug.Log("Created Block at " + chunkPos.x + "," + chunkPos.y + "," + chunkPos.z);
                    for (int x = chunkX - (int)part.gridOffset.x; x < chunkX - (int)part.gridOffset.x + (int)part.gridSize.x; x++)
                    {
                        for (int y = chunkY - (int)part.gridOffset.y; y < chunkY - (int)part.gridOffset.y + (int)part.gridSize.y; y++)
                        {
                            for (int z = chunkZ - (int)part.gridOffset.z; z < chunkZ - (int)part.gridOffset.z + (int)part.gridSize.z; z++)
                            {
                                //If in Centre Go up or Down
                                if (x == chunkX && z == chunkZ)
                                {
                                    if (y > chunkY)
                                    {
                                        insideChunk.gridData[x, y, z] = -1;
                                    }
                                    else if (y < chunkY)
                                    {
                                        insideChunk.gridData[x, y, z] = -2;
                                    }
                                    //Debug.Log("Set " + x + "," + y + "," + z + " to " + insideChunk.gridData[x, y, z]);
                                    continue;
                                }

                                //If on X Plane, go in Z
                                if (x == chunkX)
                                {
                                    if (z > chunkZ)
                                    {
                                        insideChunk.gridData[x, y, z] = -6;
                                    }
                                    else if (z < chunkZ)
                                    {
                                        insideChunk.gridData[x, y, z] = -5;
                                    }
                                    //Debug.Log("Set " + x + "," + y + "," + z + " to " + insideChunk.gridData[x, y, z]);
                                    continue;
                                }

                                //If not on X Plane
                                if (x != chunkX)
                                {
                                    if (x > chunkX)
                                    {
                                        insideChunk.gridData[x, y, z] = -4;
                                    }
                                    else if (x < chunkX)
                                    {
                                        insideChunk.gridData[x, y, z] = -3;
                                    }
                                    //Debug.Log("Set " + x + "," + y + "," + z + " to " + insideChunk.gridData[x, y, z]);
                                    continue;
                                }

                                if (x != chunkX || y != chunkY || z != chunkZ)
                                {
                                    //Debug.LogError("AHHHHHH " + x + "," + y + "," + z);
                                }

                                //-1=Down -2=Up -3=PositiveX -4=NegativeX -5=PositiveY -6=NegativeY
                            }
                        }
                    }

                    insideChunk.gridData[chunkX, chunkY, chunkZ] = _databaseID;

                    GameObject prefab   = Instantiate(part.prefab);
                    Vector3    spawnPos = ChunkToWorld(insideChunk, new Vector3(chunkX, chunkY, chunkZ));
                    prefab.transform.position = spawnPos + new Vector3(0f, part.placementYOffset, 0f);
                    prefab.transform.rotation = Quaternion.AngleAxis(_rotation, Vector3.up);

                    insideChunk.gridObjects[chunkX, chunkY, chunkZ]        = prefab;
                    insideChunk.gridRotationAngles[chunkX, chunkY, chunkZ] = _rotation;

                    lastBuildPos = chunkPos;
                    buildLock    = true;
                }
            }
        }
    }
Exemple #20
0
 public BuildingHurtEvent(BuildingPart bp, HitInfo info)
     : base(info)
 {
     Victim = bp;
 }
 public BuildingUpgradeEvent(BuildingBlock buildingBlock, BuildingGrade.Enum buildingGrade, BasePlayer basePlayer)
 {
     BuildingPart = new BuildingPart(buildingBlock);
     Grade = buildingGrade;
     Player = Server.GetPlayer(basePlayer);
 }
 private static IEnumerable<string> IterateBuilding(BuildingPart part)
 {
     if (part == null)
         yield break;
     yield return part.Name;
     yield return part.Description;
     foreach (var item in IterateBuilding(part.Parent))
         yield return item;
 }
    private void SetupCursorObject()
    {
        if (cursorObject != null)
        {
            Destroy(cursorObject);
        }

        BuildingPart buildingPart = FindObjectOfType <BuildingPartDatabaseManager>().GetBuildingPart(currentSelection);

        cursorObject                         = Instantiate(buildingPart.prefab);
        cursorObject.name                    = "lalala";
        cursorObject.transform.parent        = cursor.transform;
        cursorObject.transform.localPosition = new Vector3(0f, buildingPart.placementYOffset, 0f);
        cursorObject.transform.localRotation = Quaternion.identity;

        Joint[]         joints          = cursorObject.GetComponentsInChildren <Joint>();
        WaterBody[]     waterBodys      = cursorObject.GetComponentsInChildren <WaterBody>();
        PhysicsPrefab[] physicsPrefabs  = cursorObject.GetComponentsInChildren <PhysicsPrefab>();
        Rigidbody[]     cursorBodys     = cursorObject.GetComponentsInChildren <Rigidbody>();
        Collider[]      cursorColliders = cursorObject.GetComponentsInChildren <Collider>();
        ItemPlacement[] itemPlacements  = cursorObject.GetComponentsInChildren <ItemPlacement>();

        foreach (Joint joint in joints)
        {
            Destroy(joint);
        }

        foreach (PhysicsPrefab physics in physicsPrefabs)
        {
            Destroy(physics);
        }

        foreach (WaterBody water in waterBodys)
        {
            Destroy(water);
        }

        foreach (Rigidbody rbody in cursorBodys)
        {
            Destroy(rbody);
        }

        foreach (Collider collider in cursorColliders)
        {
            Destroy(collider);
        }

        foreach (ItemPlacement itemPlacement in itemPlacements)
        {
            if (itemPlacement.actualObject != null)
            {
                Destroy(itemPlacement.localObject);
            }
            Destroy(itemPlacement);
        }

        foreach (MeshRenderer meshRenderer in cursorObject.GetComponentsInChildren <MeshRenderer>())
        {
            currentCursorMaterial          = new Material(Resources.Load <Material>("Materials/GhostMaterial"));
            meshRenderer.material          = currentCursorMaterial;
            meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
            meshRenderer.receiveShadows    = false;
        }
    }
Exemple #24
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///Gather GHA inputs
            Curve boundary = null;

            DA.GetData <Curve>(0, ref boundary);

            string osmFilePath = string.Empty;

            DA.GetData <string>("OSM Data Location", ref osmFilePath);

            //string userSRStext = "WGS84";
            //DA.GetData<string>(2, ref userSRStext);

            List <string> filterWords = new List <string>();

            DA.GetDataList <string>(2, filterWords);

            List <string> filterKeyValue = new List <string>();

            DA.GetDataList <string>(3, filterKeyValue);

            Transform xformToMetric   = new Transform(scaleToMetric);
            Transform xformFromMetric = new Transform(scaleFromMetric);

            ///Declare trees
            Rectangle3d recs = new Rectangle3d();
            GH_Structure <GH_String>        fieldNames  = new GH_Structure <GH_String>();
            GH_Structure <GH_String>        fieldValues = new GH_Structure <GH_String>();
            GH_Structure <IGH_GeometricGoo> geometryGoo = new GH_Structure <IGH_GeometricGoo>();
            GH_Structure <IGH_GeometricGoo> buildingGoo = new GH_Structure <IGH_GeometricGoo>();


            Point3d max = new Point3d();
            Point3d min = new Point3d();

            if (boundary != null)
            {
                Point3d maxM = boundary.GetBoundingBox(true).Corner(true, false, true);
                max = Heron.Convert.XYZToWGS(maxM);

                Point3d minM = boundary.GetBoundingBox(true).Corner(false, true, true);
                min = Heron.Convert.XYZToWGS(minM);
            }

            /// get extents (why is this not part of OsmSharp?)
            System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Load(osmFilePath);
            if (xdoc.Root.Element("bounds") != null)
            {
                double  minlat    = System.Convert.ToDouble(xdoc.Root.Element("bounds").Attribute("minlat").Value);
                double  minlon    = System.Convert.ToDouble(xdoc.Root.Element("bounds").Attribute("minlon").Value);
                double  maxlat    = System.Convert.ToDouble(xdoc.Root.Element("bounds").Attribute("maxlat").Value);
                double  maxlon    = System.Convert.ToDouble(xdoc.Root.Element("bounds").Attribute("maxlon").Value);
                Point3d boundsMin = Heron.Convert.WGSToXYZ(new Point3d(minlon, minlat, 0));
                Point3d boundsMax = Heron.Convert.WGSToXYZ(new Point3d(maxlon, maxlat, 0));

                recs = new Rectangle3d(Plane.WorldXY, boundsMin, boundsMax);
            }
            else
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Cannot determine the extents of the OSM file. A 'bounds' element may not be present in the file. " +
                                  "Try turning off clipping in this component's menu.");
            }


            using (var fileStreamSource = File.OpenRead(osmFilePath))
            {
                /// create a source.
                OsmSharp.Streams.XmlOsmStreamSource source = new OsmSharp.Streams.XmlOsmStreamSource(fileStreamSource);

                /// filter by bounding box
                OsmSharp.Streams.OsmStreamSource sourceClipped = source;
                if (clipped)
                {
                    sourceClipped = source.FilterBox((float)max.X, (float)max.Y, (float)min.X, (float)min.Y, true);
                }

                /// create a dictionary of elements
                OsmSharp.Db.Impl.MemorySnapshotDb sourceMem = new OsmSharp.Db.Impl.MemorySnapshotDb(sourceClipped);

                /// filter the source
                var filtered = from osmGeos in sourceClipped
                               where osmGeos.Tags != null
                               select osmGeos;

                if (filterWords.Any())
                {
                    filtered = from osmGeos in filtered
                               where osmGeos.Tags.ContainsAnyKey(filterWords)
                               select osmGeos;
                }

                if (filterKeyValue.Any())
                {
                    List <Tag> tags = new List <Tag>();
                    foreach (string term in filterKeyValue)
                    {
                        string[] kv  = term.Split(',');
                        Tag      tag = new Tag(kv[0], kv[1]);
                        tags.Add(tag);
                    }
                    filtered = from osmGeos in filtered
                               where osmGeos.Tags.Intersect(tags).Any()
                               select osmGeos;
                }

                source.Dispose();

                /// loop over all objects and count them.
                int nodes = 0, ways = 0, relations = 0;
                Dictionary <PolylineCurve, GH_Path> bldgOutlines = new Dictionary <PolylineCurve, GH_Path>();
                List <BuildingPart> buildingParts = new List <BuildingPart>();


                foreach (OsmSharp.OsmGeo osmGeo in filtered)
                {
                    //NODES
                    if (osmGeo.Type == OsmGeoType.Node)
                    {
                        OsmSharp.Node n         = (OsmSharp.Node)osmGeo;
                        GH_Path       nodesPath = new GH_Path(0, nodes);

                        //populate Fields and Values for each node
                        fieldNames.AppendRange(GetKeys(osmGeo), nodesPath);
                        fieldValues.AppendRange(GetValues(osmGeo), nodesPath);

                        //get geometry for node
                        Point3d nPoint = Heron.Convert.WGSToXYZ(new Point3d((double)n.Longitude, (double)n.Latitude, 0));
                        geometryGoo.Append(new GH_Point(nPoint), nodesPath);

                        //increment nodes
                        nodes++;
                    }

                    ////////////////////////////////////////////////////////////
                    //WAYS
                    if (osmGeo.Type == OsmGeoType.Way)
                    {
                        OsmSharp.Way w        = (OsmSharp.Way)osmGeo;
                        GH_Path      waysPath = new GH_Path(1, ways);

                        //populate Fields and Values for each way
                        fieldNames.AppendRange(GetKeys(osmGeo), waysPath);
                        fieldValues.AppendRange(GetValues(osmGeo), waysPath);

                        //get polyline geometry for way
                        List <Point3d> wayNodes = new List <Point3d>();
                        foreach (long j in w.Nodes)
                        {
                            OsmSharp.Node n = (OsmSharp.Node)sourceMem.Get(OsmGeoType.Node, j);
                            wayNodes.Add(Heron.Convert.WGSToXYZ(new Point3d((double)n.Longitude, (double)n.Latitude, 0)));
                        }

                        PolylineCurve pL = new PolylineCurve(wayNodes);
                        if (pL.IsClosed)
                        {
                            //create base surface
                            Brep[] breps = Brep.CreatePlanarBreps(pL, DocumentTolerance());
                            geometryGoo.Append(new GH_Brep(breps[0]), waysPath);
                        }
                        else
                        {
                            geometryGoo.Append(new GH_Curve(pL), waysPath);
                        }

                        //building massing
                        if ((w.Tags.ContainsKey("building") || w.Tags.ContainsKey("building:part")))// && !w.Tags.ContainsKey("construction"))
                        {
                            if (pL.IsClosed)
                            {
                                ///Populate dictionary for sorting building parts later
                                if (w.Tags.ContainsKey("building"))
                                {
                                    bldgOutlines.Add(pL, waysPath);
                                }

                                CurveOrientation orient = pL.ClosedCurveOrientation(Plane.WorldXY);
                                if (orient != CurveOrientation.CounterClockwise)
                                {
                                    pL.Reverse();
                                }

                                ///Move polylines to min height
                                double   minHeightWay = GetMinBldgHeight(osmGeo);
                                Vector3d minVec       = new Vector3d(0, 0, minHeightWay);
                                //minVec.Transform(xformFromMetric);
                                if (minHeightWay > 0.0)
                                {
                                    var minHeightTranslate = Transform.Translation(minVec);
                                    pL.Transform(minHeightTranslate);
                                }

                                Vector3d hVec = new Vector3d(0, 0, GetBldgHeight(osmGeo) - minHeightWay);
                                //hVec.Transform(xformFromMetric);

                                Extrusion        ex      = Extrusion.Create(pL, hVec.Z, true);
                                IGH_GeometricGoo bldgGoo = GH_Convert.ToGeometricGoo(ex);

                                ///Save building parts for sorting later and remove part from geometry goo tree
                                if (w.Tags.ContainsKey("building:part"))
                                {
                                    BuildingPart bldgPart = new BuildingPart(pL, bldgGoo, fieldNames[waysPath], fieldValues[waysPath], osmGeo);
                                    buildingParts.Add(bldgPart);
                                    fieldNames.RemovePath(waysPath);
                                    fieldValues.RemovePath(waysPath);
                                    geometryGoo.RemovePath(waysPath);
                                    ways = ways - 1;
                                }
                                else
                                {
                                    buildingGoo.Append(bldgGoo, waysPath);
                                }
                            }
                        }

                        //increment ways
                        ways++;
                    }
                    ///////////////////////////////////////////////////////////

                    //RELATIONS
                    if (osmGeo.Type == OsmGeoType.Relation)
                    {
                        OsmSharp.Relation r            = (OsmSharp.Relation)osmGeo;
                        GH_Path           relationPath = new GH_Path(2, relations);

                        //populate Fields and Values for each relation
                        fieldNames.AppendRange(GetKeys(osmGeo), relationPath);
                        fieldValues.AppendRange(GetValues(osmGeo), relationPath);

                        List <Curve> pLines = new List <Curve>();

                        // start members loop
                        for (int mem = 0; mem < r.Members.Length; mem++)
                        {
                            GH_Path memberPath = new GH_Path(2, relations, mem);

                            OsmSharp.RelationMember rMem    = r.Members[mem];
                            OsmSharp.OsmGeo         rMemGeo = sourceMem.Get(rMem.Type, rMem.Id);

                            if (rMemGeo != null)
                            {
                                //get geometry for node
                                if (rMemGeo.Type == OsmGeoType.Node)
                                {
                                    long          memNodeId = rMem.Id;
                                    OsmSharp.Node memN      = (OsmSharp.Node)sourceMem.Get(rMem.Type, rMem.Id);
                                    Point3d       memPoint  = Heron.Convert.WGSToXYZ(new Point3d((double)memN.Longitude, (double)memN.Latitude, 0));
                                    geometryGoo.Append(new GH_Point(memPoint), memberPath);
                                }

                                //get geometry for way
                                if (rMem.Type == OsmGeoType.Way)
                                {
                                    long memWayId = rMem.Id;

                                    OsmSharp.Way memWay = (OsmSharp.Way)rMemGeo;

                                    //get polyline geometry for way
                                    List <Point3d> memNodes = new List <Point3d>();
                                    foreach (long memNodeId in memWay.Nodes)
                                    {
                                        OsmSharp.Node memNode = (OsmSharp.Node)sourceMem.Get(OsmGeoType.Node, memNodeId);
                                        memNodes.Add(Heron.Convert.WGSToXYZ(new Point3d((double)memNode.Longitude, (double)memNode.Latitude, 0)));
                                    }

                                    PolylineCurve memPolyline = new PolylineCurve(memNodes);

                                    geometryGoo.Append(new GH_Curve(memPolyline.ToNurbsCurve()), memberPath);

                                    CurveOrientation orient = memPolyline.ClosedCurveOrientation(Plane.WorldXY);
                                    if (orient != CurveOrientation.CounterClockwise)
                                    {
                                        memPolyline.Reverse();
                                    }

                                    pLines.Add(memPolyline.ToNurbsCurve());
                                }

                                //get nested relations
                                if (rMem.Type == OsmGeoType.Relation)
                                {
                                    ///not sure if this is needed
                                }
                            }
                        }
                        //end members loop

                        bool allClosed = true;
                        foreach (Curve pc in pLines)
                        {
                            if (!pc.IsClosed)
                            {
                                allClosed = false;
                            }
                        }

                        if (pLines.Count > 0 && allClosed)
                        {
                            ///Move polylines to min height
                            double minHeight = GetMinBldgHeight(osmGeo);
                            if (minHeight > 0.0)
                            {
                                Vector3d minVec = new Vector3d(0, 0, minHeight);
                                //minVec.Transform(xformFromMetric);
                                var minHeightTranslate = Transform.Translation(minVec);
                                for (int i = 0; i < pLines.Count; i++)
                                {
                                    pLines[i].Transform(minHeightTranslate);
                                }
                            }
                            ///Create base surface
                            Brep[] breps = Brep.CreatePlanarBreps(pLines, DocumentTolerance());
                            geometryGoo.RemovePath(relationPath);

                            foreach (Brep b in breps)
                            {
                                geometryGoo.Append(new GH_Brep(b), relationPath);

                                ///Building massing
                                if (r.Tags.ContainsKey("building") || r.Tags.ContainsKey("building:part"))
                                {
                                    Vector3d hVec = new Vector3d(0, 0, GetBldgHeight(osmGeo) - minHeight);
                                    //hVec.Transform(xformFromMetric);

                                    ///Create extrusion from base surface
                                    buildingGoo.Append(new GH_Brep(Brep.CreateFromOffsetFace(b.Faces[0], hVec.Z, DocumentTolerance(), false, true)), relationPath);
                                }
                            }
                        }

                        ///Increment relations
                        relations++;
                    } ///End relation loop
                }     ///End filtered loop

                ///Add building parts to sub-branches under main building
                for (int partIndex = 0; partIndex < buildingParts.Count; partIndex++)
                {
                    BuildingPart bldgPart  = buildingParts[partIndex];
                    Point3d      partPoint = bldgPart.PartFootprint.PointAtStart;
                    partPoint.Z = 0;
                    bool          replaceBuidingMass   = false;
                    GH_Path       mainBuildingMassPath = new GH_Path();
                    PolylineCurve massOutline          = new PolylineCurve();

                    bool isRoof = bldgPart.PartOsmGeo.Tags.TryGetValue("roof:shape", out string isRoofString);
                    if (isRoof)
                    {
                        bldgPart.PartGoo = BldgPartToRoof(bldgPart);
                    }

                    foreach (KeyValuePair <PolylineCurve, GH_Path> pair in bldgOutlines)
                    {
                        PointContainment pc = pair.Key.Contains(partPoint, Plane.WorldXY, DocumentTolerance());
                        if (pc != PointContainment.Outside)
                        {
                            ///Create new sub-branch
                            int     numSubBranches = 0;
                            GH_Path partPath       = pair.Value.AppendElement(numSubBranches);
                            while (buildingGoo.PathExists(partPath))
                            {
                                numSubBranches++;
                                partPath = pair.Value.AppendElement(numSubBranches);
                            }

                            ///Add data to sub-branch
                            fieldNames.AppendRange(bldgPart.PartFieldNames, partPath);
                            fieldValues.AppendRange(bldgPart.PartFieldValues, partPath);
                            buildingGoo.Append(bldgPart.PartGoo, partPath);

                            ///Remove the main building mass
                            replaceBuidingMass   = true;
                            mainBuildingMassPath = pair.Value;
                            massOutline          = pair.Key;
                        }
                    }
                    ///Remove the main building mass
                    if (replaceBuidingMass)
                    {
                        buildingGoo.RemovePath(mainBuildingMassPath);
                        buildingGoo.Append(new GH_Curve(massOutline), mainBuildingMassPath);
                    }
                    else
                    {
                        GH_Path extrasPath = new GH_Path(3, partIndex);
                        buildingGoo.Append(bldgPart.PartGoo, extrasPath);
                        fieldNames.AppendRange(bldgPart.PartFieldNames, extrasPath);
                        fieldValues.AppendRange(bldgPart.PartFieldValues, extrasPath);
                    }
                }
            } ///end osm source loop

            if (recs.IsValid)
            {
                DA.SetData(0, recs);
            }
            DA.SetDataTree(1, fieldNames);
            DA.SetDataTree(2, fieldValues);
            DA.SetDataTree(3, geometryGoo);
            DA.SetDataTree(4, buildingGoo);
        } ///end SolveInstance
Exemple #25
0
        public static IGH_GeometricGoo BldgPartToRoof(BuildingPart bldgPart)
        {
            IGH_GeometricGoo roof = bldgPart.PartGoo;
            PolylineCurve    pL   = bldgPart.PartFootprint; ///Already at min height

            bldgPart.PartOsmGeo.Tags.TryGetValue("roof:shape", out string roofShape);

            bldgPart.PartOsmGeo.Tags.TryGetValue("height", out string heightString);
            double height = GetHeightDimensioned(heightString);

            bldgPart.PartOsmGeo.Tags.TryGetValue("min_height", out string minHeightString);
            double min_height = GetHeightDimensioned(minHeightString);

            bldgPart.PartOsmGeo.Tags.TryGetValue("roof:height", out string roofHeightString);
            double roofHeight = GetHeightDimensioned(roofHeightString);

            double facadeHeight = height - roofHeight;

            ///Make sure there's a minium facade height for SF Transamerica Pyramid case
            if (facadeHeight <= 0)
            {
                facadeHeight = 2 * DocumentTolerance();
            }

            bldgPart.PartOsmGeo.Tags.TryGetValue("roof:orientation", out string roofOrientationString);

            bldgPart.PartOsmGeo.Tags.TryGetValue("roof:direction", out string roofDirectionString);
            double   roofDirection       = System.Convert.ToDouble(roofDirectionString);
            Vector3d roofDirectionVector = Plane.WorldXY.YAxis;

            roofDirectionVector.Rotate(RhinoMath.ToRadians(-roofDirection), Plane.WorldXY.ZAxis);

            Line[]  edges    = pL.ToPolyline().GetSegments();
            Point3d centroid = AreaMassProperties.Compute(pL).Centroid;

            switch (roofShape)
            {
            case "pyramidal":
                centroid.Z = height;
                pL.TryGetPolyline(out Polyline pLPolyline);
                Line[]      pLLines         = pLPolyline.GetSegments();
                List <Brep> pyramidBrepList = Brep.CreatePlanarBreps(pL, DocumentTolerance()).ToList();

                if (!string.IsNullOrEmpty(roofHeightString))
                {
                    Plane facadeHeightPlane = Plane.WorldXY;
                    facadeHeightPlane.Translate(new Vector3d(0, 0, facadeHeight));
                    pLPolyline.Transform(Transform.PlanarProjection(facadeHeightPlane));

                    ///Creating individual faces seems to work better/cleaner than lofting curves
                    for (int i = 0; i < pLLines.Count(); i++)
                    {
                        Line bottomEdge = pLLines[i];
                        Line topEdge    = bottomEdge;
                        topEdge.Transform(Transform.PlanarProjection(facadeHeightPlane));
                        pyramidBrepList.Add(Brep.CreateFromCornerPoints(bottomEdge.PointAt(0), bottomEdge.PointAt(1), topEdge.PointAt(1), topEdge.PointAt(0), DocumentTolerance()));
                    }
                }

                foreach (Line edge in pLPolyline.GetSegments())
                {
                    pyramidBrepList.Add(Brep.CreateFromCornerPoints(edge.PointAt(0), centroid, edge.PointAt(1), DocumentTolerance()));
                }

                Brep[] pyramidBrep = Brep.CreateSolid(pyramidBrepList, DocumentTolerance());
                if (pyramidBrep[0].IsSolid)
                {
                    roof = GH_Convert.ToGeometricGoo(pyramidBrep[0]);
                }
                break;

            case "skillion":
                Line   frontEdge     = new Line();
                Line   backEdge      = new Line();
                double frontAngleMin = RhinoMath.ToRadians(90);
                double backAngleMin  = RhinoMath.ToRadians(90);

                foreach (Line edge in edges)
                {
                    Point3d  closestPt       = edge.ClosestPoint(centroid, true);
                    Vector3d perpVector      = closestPt - centroid;
                    double   angleDifference = Vector3d.VectorAngle(roofDirectionVector, perpVector);
                    if (angleDifference < frontAngleMin)
                    {
                        frontEdge     = edge;
                        frontAngleMin = angleDifference;
                    }
                    if (angleDifference > backAngleMin)
                    {
                        backEdge     = edge;
                        backAngleMin = angleDifference;
                    }
                }

                Point3d backEdgeFrom = backEdge.From;
                backEdgeFrom.Z = height;
                Point3d backEdgeTo = backEdge.To;
                backEdgeTo.Z = height;
                Point3d frontEdgeFrom = frontEdge.From;
                frontEdgeFrom.Z = facadeHeight;
                Point3d frontEdgeTo = frontEdge.To;
                frontEdgeTo.Z = facadeHeight;

                List <Point3d> basePtList = new List <Point3d> {
                    backEdge.From, backEdge.To, frontEdge.From, frontEdge.To, backEdge.From
                };
                Polyline       basePolyline = new Polyline(basePtList);
                List <Point3d> topPtList    = new List <Point3d> {
                    backEdgeFrom, backEdgeTo, frontEdgeFrom, frontEdgeTo, backEdgeFrom
                };
                Polyline topPolyline = new Polyline(topPtList);

                ///Creating individual faces seems to work better/cleaner than lofting curves
                List <Brep> skillionBreps = new List <Brep>();
                Line[]      baseLines     = basePolyline.GetSegments();
                Line[]      topLines      = topPolyline.GetSegments();
                for (int i = 0; i < baseLines.Count(); i++)
                {
                    Line bottomEdge = baseLines[i];
                    Line topEdge    = topLines[i];
                    skillionBreps.Add(Brep.CreateFromCornerPoints(bottomEdge.PointAt(0), bottomEdge.PointAt(1), topEdge.PointAt(1), topEdge.PointAt(0), DocumentTolerance()));
                }
                Brep baseSkillion = Brep.CreateFromCornerPoints(backEdge.From, backEdge.To, frontEdge.From, frontEdge.To, DocumentTolerance());
                Brep topSkillion  = Brep.CreateFromCornerPoints(backEdgeFrom, backEdgeTo, frontEdgeFrom, frontEdgeTo, DocumentTolerance());

                skillionBreps.Add(baseSkillion);
                skillionBreps.Add(topSkillion);
                Brep[] skillion = Brep.CreateSolid(skillionBreps, DocumentTolerance());
                if (skillion.Count() > 0)
                {
                    roof = GH_Convert.ToGeometricGoo(skillion[0]);
                }

                break;

            case "gabled":
                ///TODO: Look into getting oriented bbox using front edge as orientation plane,
                ///extrude gable roof profile from face of bbox, trim roof geo from footprint,
                ///loft between footprint and trimmed roof edges and join everything.

                ///Need to simply polylines with colinear segments. Angle tolerance based on Notre-Dame de Paris case
                //pL.Simplify(CurveSimplifyOptions.All, 0, RhinoMath.ToRadians(2)).TryGetPolyline(out Polyline pLSimplified);
                Polyline pLSimplified = pL.ToPolyline();
                pLSimplified.MergeColinearSegments(DocumentAngleTolerance() * 5, true);

                Line[] edgesSimplified = pLSimplified.GetSegments();
                if (edgesSimplified.Count() != 4)
                {
                    break;
                }
                Line        ridge            = new Line();
                Line        eaveOne          = new Line();
                Line        eaveTwo          = new Line();
                Polyline    topGablePolyline = new Polyline();
                List <Brep> gableBreps       = new List <Brep>();

                if ((edgesSimplified[0].Length > edgesSimplified[1].Length && roofOrientationString != "across") || ((edgesSimplified[0].Length < edgesSimplified[1].Length && roofOrientationString == "across")))
                {
                    ridge         = new Line(edgesSimplified[3].PointAt(0.5), edgesSimplified[1].PointAt(0.5));
                    ridge.FromZ   = height;
                    ridge.ToZ     = height;
                    eaveOne       = edgesSimplified[0];
                    eaveOne.FromZ = facadeHeight;
                    eaveOne.ToZ   = facadeHeight;
                    eaveTwo       = edgesSimplified[2];
                    eaveTwo.Flip();
                    eaveTwo.FromZ    = facadeHeight;
                    eaveTwo.ToZ      = facadeHeight;
                    topGablePolyline = new Polyline {
                        eaveOne.From, eaveOne.To, ridge.To, eaveTwo.To, eaveTwo.From, ridge.From, eaveOne.From
                    };

                    Brep[] gableRoof = Brep.CreateFromLoft(new List <Curve> {
                        eaveOne.ToNurbsCurve(), ridge.ToNurbsCurve(), eaveTwo.ToNurbsCurve()
                    }, Point3d.Unset, Point3d.Unset, LoftType.Straight, false);
                    gableRoof[0].Faces.SplitKinkyFaces();
                    gableBreps.Add(gableRoof[0]);
                }

                if ((edgesSimplified[0].Length > edgesSimplified[1].Length && roofOrientationString == "across") || (edgesSimplified[0].Length < edgesSimplified[1].Length && roofOrientationString != "across"))
                {
                    ridge         = new Line(edgesSimplified[0].PointAt(0.5), edgesSimplified[2].PointAt(0.5));
                    ridge.FromZ   = height;
                    ridge.ToZ     = height;
                    eaveOne       = edgesSimplified[1];
                    eaveOne.FromZ = facadeHeight;
                    eaveOne.ToZ   = facadeHeight;
                    eaveTwo       = edgesSimplified[3];
                    eaveTwo.Flip();
                    eaveTwo.FromZ    = facadeHeight;
                    eaveTwo.ToZ      = facadeHeight;
                    topGablePolyline = new Polyline {
                        eaveTwo.From, ridge.From, eaveOne.From, eaveOne.To, ridge.To, eaveTwo.To, eaveTwo.From
                    };

                    Brep[] gableRoof = Brep.CreateFromLoft(new List <Curve> {
                        eaveOne.ToNurbsCurve(), ridge.ToNurbsCurve(), eaveTwo.ToNurbsCurve()
                    }, Point3d.Unset, Point3d.Unset, LoftType.Straight, false);
                    gableRoof[0].Faces.SplitKinkyFaces();
                    gableBreps.Add(gableRoof[0]);
                }

                Brep[] gablewalls = Brep.CreateFromLoft(new List <Curve> {
                    pLSimplified.ToPolylineCurve(), topGablePolyline.ToPolylineCurve()
                }, Point3d.Unset, Point3d.Unset, LoftType.Straight, false);
                gablewalls[0].Faces.SplitKinkyFaces();
                gablewalls[0].MergeCoplanarFaces(DocumentTolerance());
                gableBreps.Add(gablewalls[0]);

                Brep baseGable = Brep.CreateFromCornerPoints(edgesSimplified[0].From, edgesSimplified[0].To, edgesSimplified[2].From, edgesSimplified[2].To, DocumentTolerance());
                gableBreps.Add(baseGable);
                Brep[] gable = Brep.JoinBreps(gableBreps, DocumentTolerance());

                if (gable[0].IsValid)
                {
                    roof = GH_Convert.ToGeometricGoo(gable[0]);
                }
                break;

            default:
                break;
            }

            return(roof);
        }
 public BuildingPartDemolishedEvent(BuildingBlock bb, BasePlayer basePlayer)
 {
     BuildingPart = new BuildingPart(bb);
     Player       = Server.GetPlayer(basePlayer);
 }
 public BuildingPartDemolishedEvent(BuildingBlock buildingBlock, BasePlayer basePlayer)
 {
     BuildingPart = new BuildingPart(buildingBlock);
     Player = Server.GetPlayer(basePlayer);
 }
Exemple #28
0
 public void On_BuildingComplete(BuildingPart bp)
 {
     SetHookWorking("On_BuildingComplete");
     Broadcast("Building completed " + bp.Name);
 }
    /// <summary>
    /// Load the Building Part Database from an XML File
    /// </summary>
    public void LoadDatabase(string fileName)
    {
        buildingPartDatabase = new List <BuildingPart>();

        //Get the Text File
        TextAsset textAsset = Resources.Load <TextAsset>(fileName);

        if (textAsset == null)
        {
            throw new Exception("Dialogue file \"" + fileName + "\" dosen't exist.");
        }

        XmlDocument xmldoc = new XmlDocument();

        xmldoc.LoadXml(textAsset.text);

        foreach (XmlNode xmlNode in xmldoc.DocumentElement.ChildNodes)
        {
            Debug.Log("Loading Group: " + xmlNode.Name);

            if (xmlNode.Name == "BuildingPart")
            {
                int id = int.Parse(xmlNode.Attributes["id"].Value);
                if (id != buildingPartDatabase.Count + 1)
                {
                    throw new Exception("Dialogue ID's are an incorrect Order");
                }

                //Make the Class
                BuildingPart buildingPart = new BuildingPart(id);

                buildingPart.partName   = xmlNode.Attributes["partname"].Value;
                buildingPart.gridSize   = new Vector3(int.Parse(xmlNode.Attributes["gridsizeX"].Value), int.Parse(xmlNode.Attributes["gridsizeY"].Value), int.Parse(xmlNode.Attributes["gridsizeZ"].Value));
                buildingPart.gridOffset = new Vector3(int.Parse(xmlNode.Attributes["offsetX"].Value), int.Parse(xmlNode.Attributes["offsetY"].Value), int.Parse(xmlNode.Attributes["offsetZ"].Value));

                buildingPart.prefabResourceName = xmlNode.Attributes["resource"].Value;
                buildingPart.prefab             = Resources.Load <GameObject>(buildingPart.prefabResourceName);

                buildingPart.iconResourceName = xmlNode.Attributes["icon"].Value;
                buildingPart.icon             = Resources.Load <Sprite>(buildingPart.iconResourceName);

                buildingPart.partType = BuildingPart.PartType.Max;

                if (xmlNode.Attributes["placementYOffset"] != null)
                {
                    buildingPart.placementYOffset = float.Parse(xmlNode.Attributes["placementYOffset"].Value);
                }
                else
                {
                    buildingPart.placementYOffset = 0f;
                }

                if (xmlNode.Attributes["type"] != null)
                {
                    switch (xmlNode.Attributes["type"].Value)
                    {
                    case "base": buildingPart.partType = BuildingPart.PartType.Base; break;
                    }
                }

                buildingPartDatabase.Add(buildingPart);

                Debug.Log("Loaded #" + id + ": Name:" + buildingPart.partName + " " + (buildingPart.prefab != null ? buildingPart.prefabResourceName : "{NO PREFAB}"));
            }
        }
    }