Example #1
0
 void Start()
 {
     TownOptions.Default = new TownOptions {
         Patches = 35
     };
 }
        public void Generate()
        {
            for (int i = TownRoot.childCount - 1; i > -1; i--)
            {
                DestroyImmediate(TownRoot.GetChild(i).gameObject);
            }

            var townOptions = new TownOptions
            {
                Overlay = true,
                Patches = Patches,
                Walls   = Walls,
                Water   = Water,
                Seed    = Seed
            };

            var town = new Town.Town(townOptions);

            var townRenderer = new TownMeshRenderer(town, townOptions);

            townRenderer.root             = TownRoot;
            townRenderer.GateMaterial     = GateMaterial;
            townRenderer.TowerMaterial    = TowerMaterial;
            townRenderer.WallMaterial     = WallMaterial;
            townRenderer.RoadMaterial     = RoadMaterial;
            townRenderer.WaterMaterial    = WaterMaterial;
            townRenderer.BuildingMaterial = BuildingMaterial;
            townRenderer.OverlayMaterial  = OverlayMaterial;
            townRenderer.Generate();

            if (townRenderer.Walls != null)
            {
                foreach (Transform child in townRenderer.Walls.transform)
                {
                    var modifier = child.gameObject.AddComponent <NavMeshModifier> ();
                    modifier.overrideArea = true;
                    modifier.area         = 6;
                }
            }

            if (townRenderer.Waters != null)
            {
                foreach (Transform child in townRenderer.Waters.transform)
                {
                    var modifier = child.gameObject.AddComponent <NavMeshModifier> ();
                    modifier.overrideArea = true;
                    modifier.area         = 5;
                }
            }

            foreach (Transform child in townRenderer.Roads.transform)
            {
                var modifier = child.gameObject.AddComponent <NavMeshModifier> ();
                modifier.overrideArea = true;
                modifier.area         = 3;
            }

            foreach (Transform child in townRenderer.Buildings.transform)
            {
                var modifier = child.gameObject.AddComponent <NavMeshModifier> ();
                modifier.overrideArea = true;
                modifier.area         = 4;
                child.gameObject.AddComponent <Building> ();
            }

            GetComponent <BuildNavMesh> ().Build((AsyncOperation operation) =>
            {
                NavMeshQuerySystem.PurgeCacheStatic();
                Debug.Log("Town built. Cache purged.");
            });
        }
Example #3
0
    private void MakeCity(ref int XzSpread, string HomeTownName, ref int timesThru, int timeThru)
    {
        // The new Town
        Coord newlocality = new Coord();

        // New random choices for the towns coords
        int newXmaybe = 0;
        int newZmaybe = 0;


        int NextValidRandomPatchAmountFromTGOSRange =
            RandomGen.Next(TownGlobalObjectService.PatchCap, TownGlobalObjectService.PatchFloor);


        // Get A valid amount for the patch choice
        int randomPatches = NextValidRandomPatchAmountFromTGOSRange;

        // For "Distance" comparison
        float closest = 0;

        if (timeThru >= TownGlobalObjectService.TownRequests.Count)
        {
            newXmaybe = (int)(GetRandomXZrangedInt() * 1) + (RandomGen.FlipACoin() ? (int)MinimumDistance : -(int)MinimumDistance);
            newZmaybe = (int)(GetRandomXZrangedInt() * 1) + (RandomGen.FlipACoin() ? (int)MinimumDistance : -(int)MinimumDistance);


            // Test if the hit is closer than we would like, if it is then we need to try again

            timesThru = 0;

            while (closest < MinimumDistance + ((RandomGen.FlipACoin()) ? 0 : 1))
            {
                if (timesThru >= ExpandXZsAfterTries)
                {
                    timesThru = 0;
                    Debug.Log("**********BAILED ON COUNT OUT**************");
                    XzSpread++;
                    // closest = MinimumDistance +1;
                    // continue;
                }



                // ADDED +1!
                newXmaybe += (RandomGen.FlipACoin() ? (int)MinimumDistance : -(int)MinimumDistance);
                newZmaybe += (RandomGen.FlipACoin() ? (int)MinimumDistance : -(int)MinimumDistance);


                // wraps the modulo regardless of sign
                //newXmaybe = (newXmaybe < 0) ? newXmaybe = -(Mathf.Abs(newXmaybe) % XzSpread) : newXmaybe % XzSpread;
                //newZmaybe = (newZmaybe < 0) ? newZmaybe = -(Mathf.Abs(newZmaybe) % XzSpread) : newZmaybe % XzSpread;


                // find a random coord with a magnitude high enough
                newlocality = new Coord(newXmaybe, newZmaybe);

                // convert it to 1000* size and 500 Y
                Vector3 p1 = newlocality.ToTileSizeCeilVector3();
                //   Debug.LogFormat("{0}", p1);

                // get hits
                //   Collider[] hitColliders = Physics.OverlapSphere(p1, (MinimumDistance * ratio) * 1000f, layerMask);//, LayerMask.NameToLayer("MAPGEN"));

                ///  Collider[] hitColliders = Physics.OverlapSphere(p1, (MinimumDistance) * 500f, layerMask);//, LayerMask.NameToLayer("MAPGEN"));


                Coord closetTownFound = TownGlobalObject.GetClosest(newlocality, TownGlobalObject.townsData.Keys.ToList());


                //Scale the result based on city size.  allow for 1.41  within reason

                var tinyCityApproxPatchCount = 100;

                var scalar = (0.002f * CityRatioGrowthApproximationmultiplier * Mathf.Min(TownGlobalObject.townsData[closetTownFound].Patches.Count - tinyCityApproxPatchCount, 10));


                closest = Coord.Distance(
                    newlocality,
                    TownGlobalObject.GetClosest(
                        newlocality,
                        TownGlobalObject.townsData.Keys.ToList()
                        )
                    )
                          - scalar;           //// if we didn't hit at all just place it otherwise check the lengths

                //if (hitColliders.Length == 0)
                //{
                //    // Just skip the next bit and place this directly.
                //    closest = MinimumDistance + 1;
                //    //     Debug.LogFormat("{0} is okay as a placement", newlocality);
                //    break;
                //}

                //foreach (var hitCollider in hitColliders)
                //{

                //    var distance = (p1 - hitCollider.gameObject.transform.position).sqrMagnitude * 0.001f;

                //    if (distance < MinimumDistance)
                //    {
                //        closest = distance;
                //        //   Debug.LogFormat(hitCollider.gameObject, "{0} for {1} from {2} at {3}", closest, hitCollider.gameObject.name, "newlocality", p1);
                //    }
                //    //  closest = (distance < MinimumDistance) ? distance : closest;
                //}
                ////   closest = TownGlobalObject.GetClosestMagnitude (newlocality , placesWePlaced);

                timesThru++;
            }
        }
        else  // i < TownGlobalObjectService.TownRequests.Count
        {
            //home North etc...

            newlocality = new Coord(
                (int)TownGlobalObjectService.TownRequests[timeThru].Coord.x,
                (int)TownGlobalObjectService.TownRequests[timeThru].Coord.y);                      // Hometon
        }


        TownTileRenderer mrnewlocalityMaker = new TownTileRenderer();


        // 0 triggers a random
        int amount = 0;


        if (timeThru < TownGlobalObjectService.TownRequests.Count)
        {
            if (timeThru == 0)
            {
                amount += TownHolder.Instance.MinCitySpreadreq;
            }
            else
            {
                amount += TownGlobalObjectService.TownRequests[timeThru].PatchesInSize;
            }
        }
        // Fill it with Joy
        AOTABundle newlocalityBundle = mrnewlocalityMaker.MakeATileBundle(newlocality, true, false, amount);



        //  Debug.Log(TownGlobalObjectService.WorldMultiplier);


        //Town.Town newlocalitylazyTown;

        ////Handle the homes case
        //if (i ==0 )
        //{
        //   newlocalitylazyTown = TownGlobalObject.towns.GetOrAdd(newlocality, k => TownGlobalObject.MakeTown(k, k.x, k.z, 0, 60));

        //}
        //else
        //{
        //  newlocalitylazyTown = TownGlobalObject.towns.GetOrAdd(newlocality, k => TownGlobalObject.MakeTown(k, k.x, k.z, 0, randomPatches));

        //}

        //    TownGlobalObject.townsData[locality] = TownGlobalObject.MakeTown(locality, locality.x, locality.z);

        // THIS CONTAINS THE TOWN DATA
        TownGlobalObject.townsData[newlocality] = newlocalityBundle.town;



        if (timeThru < TownGlobalObjectService.TownRequests.Count)
        {
            TownGlobalObject.townsData[newlocality].name = TownGlobalObjectService.TownRequests[timeThru].Name;
            newlocalityBundle.town.coord = new Coord(
                (int)TownGlobalObjectService.TownRequests[timeThru].Coord.x,
                (int)TownGlobalObjectService.TownRequests[timeThru].Coord.y);
        }

        //handle the home case.
        if (timeThru == 0 && newlocality == new Coord(0))
        {
            TownGlobalObject.townsData[newlocality].name = HomeTownName;
            TownGlobalObject.homeTown = TownGlobalObject.townsData[newlocality];
        }



        //  Debug.Log(TownGlobalObject.townsData[newlocality].name);

        // Bless it.

        // TODO: ADD  METHOD WERE WE CAN PASS A TOWN



        newlocalityBundle.MarkisBundledTrue();

        newlocalityBundle.MarkIsTileDataInBundleTrue();

        TownGlobalObject.bundles[newlocality] = newlocalityBundle;



        AOTABundle reffedBundle = TownGlobalObject.bundles[newlocality];

        //Debug.LogFormat(
        //            "INIT TEST: bundle found for tile {5} town {0}: {7} : {3} with {1} SplineSysBundles and {2} TransitionsListBundles and {4} and {6}",
        //           string.Format("{0}:{1}", newlocality.x, newlocality.z),
        //            reffedBundle.SplineSysBundle.Count(),
        //            reffedBundle.TransitionsListBundle.Count(),
        //            reffedBundle.name,
        //            (reffedBundle.isBundled) ? " is bundled :)" : "is not bundled :(",
        //           newlocality,
        //             (reffedBundle.isTileDataInBundle) ? " Tile Data IS In Bundle :)" : "TILE DATA MISSING!!!!!!!!!!",
        //             reffedBundle.town.name
        //            );

        //  in here we could then span every X|Z and prerender every likely tile to have it's data


        float size = CityRatioGrowthApproximationmultiplier * reffedBundle.town.Patches.Count;


        // rendering 36 tiles by default at max per city
        int roughTileGuess = (int)Mathf.Max(MinTileRender, Mathf.Min(MaxTileRender, Mathf.Ceil(size * 0.004f)));



        //return the even above, we will split this in half and use that as out "theoretical middle";
        roughTileGuess = (roughTileGuess % 2 == 1) ? roughTileGuess + 1 : roughTileGuess;


        //    Debug.LogFormat("approximate city size is {0} for {2} so roughly a {1} tile square", size , roughTileGuess, reffedBundle.name);


        // back assign the bundle names from the town for consistency in the editor (manually named and generated cities)

        reffedBundle.name = reffedBundle.town.name;

        // assign the non manually assigned cases
        if (timeThru >= TownGlobalObjectService.TownRequests.Count)
        {
            reffedBundle.town.coord = reffedBundle.coord;
        }


        if (TownGlobalObject.townsData[reffedBundle.town.coord].TownGameObject == null)
        {
            TownGlobalObject.townsData[reffedBundle.town.coord].TownGameObject = new GameObject(reffedBundle.town.name);
        }

        //create or use the holder now it has the right name.
        var go = TownGlobalObject.townsData[reffedBundle.town.coord].TownGameObject;

        GameObject CityHolder = new GameObject("MAP GEN COLLION HOLDER");

        CityHolder.transform.parent        = go.transform;
        CityHolder.transform.localPosition = Vector3.zero;

        CityHolder.transform.position = new Vector3(newlocality.x * 1000, 400, newlocality.z * 1000);


        //GameObject CityHolder = Instantiate<GameObject>(Temp, Temp.transform.position, Quaternion.identity);

        //Debug.LogFormat(CityHolder, "newlocalityBundle is {0} with {1} cells at {2} closest is {3}",
        //      TownGlobalObject.bundles[newlocality].name,
        //      TownGlobalObject.bundles[newlocality].town.Patches.Count,
        //      newlocality,
        //      closest);


        // add something for every city.


        float halfsize = size * 0.01f;

        // Add an item for the HUD
        //  var HUD = Instantiate(TownHolder.Instance.HUDPrefab,CityHolder.transform);
        //  HUD.transform.localPosition = Vector3.zero;


        var collisionCube = new Cube(reffedBundle.name,
                                     TownMeshRendererUtils.GetVertices((int)size, (int)size, -halfsize, -halfsize), halfsize,
                                     null,
                                     CityHolder.transform, false, false);



        collisionCube.Transform.localPosition = Vector3.zero;
        //  collisionCube.GameObject.layer = LayerMask.NameToLayer("MAPGEN");

        BoxCollider col = collisionCube.GameObject.AddComponent <BoxCollider>();

        col.size = new Vector3(size, halfsize, size);

        // Register our BoxCollider for Disabling later.

        TownGlobalObject.renderedBoxColliders.Add(col);

        TownMeshRendererOptions rendererOptions = TownGlobalObjectService.rendererOptions;

        TownOptions skeletonOptions = TownGlobalObject.bundles[newlocality].town.Options;

        skeletonOptions.IOC        = false;
        skeletonOptions.Farm       = false;
        skeletonOptions.Roads      = false;
        skeletonOptions.Walls      = false;
        skeletonOptions.CityDetail = false;



        //TownGlobalObject.MeshRenderer = new TownMeshRenderer (
        //        reffedBundle.town,
        //        skeletonOptions,
        //        rendererOptions);

        //// This does the fancy  world map colored sections
        TownGlobalObject.MeshRenderer.GenerateOverlay();


        //  Debug.LogFormat("{0} {1} ", reffedBundle.name, reffedBundle.town.name);



        // This does the fancy city overlay over the world map colored sections
        RenderTownMeshes(ref reffedBundle);



        //   TownGlobalObject.bundles[newlocality].isTileDataInBundle = true;

        //  Destroy(CityHolder);


        // Assign it back?
        TownGlobalObject.SetBundle(newlocality, reffedBundle);

        //   TownGlobalObject.bundles[newlocality] = reffedBundle;


        //   UnityEngine.Assertions.Assert.IsTrue( TownGlobalObject.bundles.ContainsKey(newlocality));



        // COULD HAVE LOOPED HERE
    }