示例#1
0
    public void OnBeforeTileGenerateCheck(TerrainTile tile, TileData data)
    {
        if (!TownGlobalObject.InitialTownsGenerated)
        {
            Debug.Log("***** !TownGlobalObject.InitialTownsGenerated THIS IS BAD*****");
            return;
        }

        // This will ALLLLLways return something... it SHOULD be the town.
        Coord locality = TownGlobalObject.GetIndexAtCoord(data.area.Coord);


        if (!TownGlobalObject.bundles.ContainsKey(data.area.Coord))  // By this point  !bundle.isBundled && !bundle.isTileDataInBundle  - basically the bundle is a blank.
        {
            // WE DID NOT HIT THE BUNDLE

            //Debug.LogFormat("bundle not found for tile {1}, town {0} during the {2} phase", locality, data.area.Coord,
            //    (data.isDraft) ? "Draft" : "Main"
            //    );



            TownTileRenderer mrMaker = new TownTileRenderer();

            // But the actual town should be stored at the "Locality"

            bundle = mrMaker.MakeATileBundleWithThisTown(data.area.Coord, false, true, locality);

            // by here all our splineSys and transition lists should be filled.

            // Bless it.

            bundle.MarkisBundledTrue();

            bundle.MarkIsTileDataInBundleTrue();


            TownGlobalObject.bundles[data.area.Coord] = bundle;
        }
        // WE DID HIT THE BUNDLE

        bundle = TownGlobalObject.bundles[data.area.Coord];


        if (locality == data.area.Coord && !bundle.isTileDataInBundle)
        {
            Debug.LogFormat("This is a town tile. Is the locality Town stored? {0}", TownGlobalObject.townsData.ContainsKey(locality));
        }



        // Handle the WE HAVE THE TILE DATA ALREADY case
        if (bundle.isBundled && bundle.isTileDataInBundle)
        {
            return;
        }
        // The Unrendered case. Lets double check.
        else if (bundle.isBundled && !bundle.isTileDataInBundle)
        {
            TownTileRenderer mrMaker = new TownTileRenderer();
            bundle = mrMaker.MakeATileBundle(data.area.Coord, false, false);

            Debug.LogFormat("rewrite for UNRENDERED {0}, {1}, {2} during {3} phase and {4} and {5}",
                            string.Format("{0}:{1}", bundle.town.mapOffset.x, bundle.town.mapOffset.y),
                            bundle.town.name,
                            bundle.name,
                            (data.isDraft) ? "DRAFT" : "MAIN",
                            (bundle.isBundled) ? " is bundled :)" : "is not bundled :(",
                            (bundle.isTileDataInBundle) ? " Tile Data IS In Bundle :)" : "TILE DATA MISSING!!!!!!!!!!"
                            );


            // by here all our splineSys and transition lists should be filled.

            // Bless it.

            bundle.MarkisBundledTrue();



            TownGlobalObject.bundles[data.area.Coord] = bundle;

            //   var bundletest = TownGlobalObject.GetTownBundle(data.area.Coord);


            var bundletest = TownGlobalObject.bundles[data.area.Coord];

            //  if (bundle2test == bundletest)
            //  {
            Debug.LogFormat(
                " FINAL TEST {0} is null = {1}, {2} is {3}, {4} .isBundled is {5}",
                string.Format("{0}:{1}", data.area.Coord.x, data.area.Coord.z),

                bundletest == null,
                nameof(bundletest),
                bundletest,
                nameof(bundletest),
                (bundletest.isBundled) ? " Tile Data IS In Bundle :)" : "TILE DATA MISSING!!!!!!!!!!"
                );


            //   RenderOutlet(data, bundle, OutletFullList);
            bundle.MarkIsTileDataInBundleTrue();
            //   bundle.isTileDataInBundle = true;
            return;
            //  }
        }
    }
        public override void Generate(TileData data, StopToken stop)
        {
            if (stop.stop || !enabled)
            {
                return;
            }

            // the only reason we could get here that I can figure is if you pinned so lets init things.

            if (!TownGlobalObject.InitialTownsGenerated)
            {
                Debug.Log("***** !TownGlobalObject.InitialTownsGenerated THIS IS BAD*****");
                return;
            }



            Dictionary <string, ulong> OutletFullList; // = new Dictionary<string, ulong>();

            InitNamedOutputsDicts(out OutletFullList);

            if (stop.stop)
            {
                return;
            }

            //// Town stuff  /////////// ********

            // This will ALLLLLways return something... it SHOULD be the town.
            Coord locality = TownGlobalObject.GetIndexAtCoord(data.area.Coord);


            if (!TownGlobalObject.bundles.ContainsKey(data.area.Coord))  // By this point  !bundle.isBundled && !bundle.isTileDataInBundle  - basically the bundle is a blank.
            {
                Debug.Log("***** !TownGlobalObject.bundles.ContainsKey(data.area.Coord) THIS IS BAD*****");
                return;
            }
            // WE DID HIT THE BUNDLE



            bundle = TownGlobalObject.bundles[data.area.Coord];


            if (locality == data.area.Coord && !bundle.isTileDataInBundle && !RoadOnly)
            {
                Debug.LogFormat("This is a town tile. Is the locality Town stored? {0}", TownGlobalObject.townsData.ContainsKey(locality));
            }


            // Handle the WE HAVE THE TILE DATA ALREADY case
            if (bundle.isBundled && bundle.isTileDataInBundle)
            {
                //Debug.LogFormat(
                //       "{0} is {1}, {2} is {3}, {4} is {5},  ",
                //       "outlet GUID",
                //       item.splineSys.GetType().GUID,
                //       "name",
                //       item.name,
                //       nameof(item.outlet.Id),
                //       item.outlet.id);

                // Write it to the outputs and exit.

                RenderOutlet(data, bundle, OutletFullList);

                return;
            }
            // The Unrendered case. Lets double check.
            else if (bundle.isBundled && !bundle.isTileDataInBundle)
            {
                RenderOutlet(data, bundle, OutletFullList);
                bundle.MarkIsTileDataInBundleTrue();
                //   bundle.isTileDataInBundle = true;
                return;
            }
        }
示例#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
    }