<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Infinity Code Forum — Do not generate buildings by conditions]]></title>
	<link rel="self" href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=2118&amp;type=atom" />
	<updated>2023-05-16T10:35:17Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.infinity-code.com/viewtopic.php?id=2118</id>
		<entry>
			<title type="html"><![CDATA[Do not generate buildings by conditions]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=9024#p9024" />
			<content type="html"><![CDATA[<p>If you want to not generate buildings by some condition, here&#039;s an example of how to do it.<br />In this example, it is forbidden to generate buildings with a height less than 30 meters.</p><p>How to use this:<br />Place the script in Editor folder.<br />If you want to change the conditions, you can do this in the OnGenerateBuilding method.<br />Select RWT/Do Not Generate Buildings By Conditions, click Active and generate buildings.</p><div class="codebox"><pre><code>using System.Collections.Generic;
using System.Globalization;
using InfinityCode.RealWorldTerrain;
using InfinityCode.RealWorldTerrain.Generators;
using InfinityCode.RealWorldTerrain.OSM;
using InfinityCode.RealWorldTerrain.Windows;
using UnityEditor;
using UnityEngine;

namespace InfinityCode.RealWorldTerrainExamples
{
    /// &lt;summary&gt;
    /// Example of using the Real World Terrain API to not generate buildings under certain conditions.
    /// &lt;/summary&gt;
    public class DoNotGenerateBuildingsByConditions : EditorWindow
    {
        /// &lt;summary&gt;
        /// Will the conditions from the script be used?
        /// &lt;/summary&gt;
        private static bool active = false;
        
        /// &lt;summary&gt;
        /// Minimum building height.
        /// &lt;/summary&gt;
        private static float minHeight = 30;
        
        /// &lt;summary&gt;
        /// Determines whether to generate building based on provided conditions.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;points&quot;&gt;List of points for the building base.&lt;/param&gt;
        /// &lt;param name=&quot;way&quot;&gt;The OSM Way data for the building.&lt;/param&gt;
        /// &lt;param name=&quot;nodes&quot;&gt;The OSM Node data associated with the building.&lt;/param&gt;
        /// &lt;returns&gt;True - the building should not be generated, false - it should be generated.&lt;/returns&gt;
        private bool OnGenerateBuilding(List&lt;Vector3&gt; points, RealWorldTerrainOSMWay way, Dictionary&lt;string, RealWorldTerrainOSMNode&gt; nodes)
        {
            // Default building height.
            float baseHeight = 15;
            
            // Get the height of the building from the OSM data.
            string heightStr = way.GetTagValue(&quot;height&quot;);
            
            // If the height is not specified, try to get the number of floors.
            string levelsStr = way.GetTagValue(&quot;building:levels&quot;);
            
            // Parse the height from the string.
            RealWorldTerrainBuildingGenerator.GetHeightFromString(heightStr, ref baseHeight);
            
            // If the height is not specified, but the number of floors is specified, calculate the height based on the number of floors.
            if (string.IsNullOrEmpty(heightStr))
            {
                if (!string.IsNullOrEmpty(levelsStr))
                {
                    float h;
                    if (float.TryParse(levelsStr, NumberStyles.AllowDecimalPoint, RealWorldTerrainCultureInfo.cultureInfo, out h)) baseHeight = h * RealWorldTerrainWindow.prefs.buildingFloorHeight;
                }
                else baseHeight = RealWorldTerrainWindow.prefs.buildingFloorLimits.Random() * RealWorldTerrainWindow.prefs.buildingFloorHeight;
            }
            
            // If the height is less than the minimum, do not generate the building.
            if (baseHeight &lt; minHeight) return true;

            // Otherwise, generate the building.
            return false;
        }

        /// &lt;summary&gt;
        /// Displays the GUI controls for the editor window.
        /// &lt;/summary&gt;
        private void OnGUI()
        {
            EditorGUILayout.HelpBox(&quot;If active, buildings will not be generated under the conditions specified in the OnGenerateBuilding method.&quot;, MessageType.Info);
            
            EditorGUI.BeginChangeCheck();
            active = EditorGUILayout.Toggle(&quot;Active&quot;, active);
            if (EditorGUI.EndChangeCheck())
            {
                if (active)
                {
                    // Subscribe to the OnGenerateBuilding event.
                    RealWorldTerrainBuildingGenerator.OnGenerateBuilding -= OnGenerateBuilding;
                    RealWorldTerrainBuildingGenerator.OnGenerateBuilding += OnGenerateBuilding;
                }
                else
                {
                    // Unsubscribe from the OnGenerateBuilding event.
                    RealWorldTerrainBuildingGenerator.OnGenerateBuilding -= OnGenerateBuilding;
                }
            }
            
            minHeight = EditorGUILayout.FloatField(&quot;Min Height&quot;, minHeight);
        
            if (GUILayout.Button(&quot;Open Real World Terrain&quot;))
            {
                RealWorldTerrainWindow.OpenWindow(RealWorldTerrainGenerateType.full);
            }
        }

        /// &lt;summary&gt;
        /// Opens the window.
        /// &lt;/summary&gt;
        [MenuItem(&quot;RWT/Do Not Generate Buildings By Conditions&quot;)]
        private static void OpenWindow()
        {
            GetWindow&lt;DoNotGenerateBuildingsByConditions&gt;(false, &quot;Do Not Generate Buildings By Conditions&quot;, true);
        }
    }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2023-05-16T10:35:17Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=9024#p9024</id>
		</entry>
</feed>
