Exemplo n.º 1
0
    int CreateRiver(TriCell origin)
    {
        int          length    = 0;
        TriCell      cell      = origin;
        TriDirection direction = TriDirection.VERT;

        while (!cell.IsUnderwater)
        {
            flowDirections.Clear();
            for (TriDirection d = TriDirection.VERT; d <= TriDirection.RIGHT; d++)
            {
                TriCell neighbor = cell.GetNeighbor(d);
                if (!neighbor || neighbor.HasRiver)
                {
                    continue;
                }
                int delta = neighbor.Elevation - cell.Elevation;
                if (delta > 0)
                {
                    continue;
                }
                if (delta < 0)
                {
                    flowDirections.Add(d);
                    flowDirections.Add(d);
                    flowDirections.Add(d);
                }
                if (
                    length == 1 ||
                    (d != direction.Next2() && d != direction.Previous2())
                    )
                {
                    flowDirections.Add(d);
                }
                flowDirections.Add(d);
            }
            if (flowDirections.Count == 0)
            {
                return(length > 1 ? length : 0);
            }
            direction = flowDirections[Random.Range(0, flowDirections.Count)];
            cell.SetRiver(direction);
            cell.GetNeighbor(direction).SetRiver(direction);
            length += 1;
            cell    = cell.GetNeighbor(direction);
        }
        return(length);
    }
Exemplo n.º 2
0
    public override List <BuildState> GetBuildStatus(TriCoordinates coord, TriDirection dir)
    {
        List <BuildState> ret  = new List <BuildState>();
        TriCell           cell = TriGrid.Instance.GetCell(coord);
        int elev = cell.Elevation;

        ret.Add(new BuildState(coord, cell.HasRiver));
        TriCell entrance = cell.GetNeighbor(dir);

        ret.Add(new BuildState(entrance.coordinates, entrance.IsBuildable() && Mathf.Abs(elev - entrance.Elevation) < 2));
        cell = cell.GetNeighbor(dir.Previous());
        ret.Add(new BuildState(cell.coordinates, cell.HasRiver && Mathf.Abs(elev - cell.Elevation) < 2));
        cell = cell.GetNeighbor(dir);
        ret.Add(new BuildState(cell.coordinates, cell.IsBuildable() && Mathf.Abs(elev - cell.Elevation) < 2));
        return(ret);
    }
Exemplo n.º 3
0
    public void StartCalculateTerrain()
    {
        terrainSelectionViewer.Clear();
        switch (terrainType)
        {
        case SizeType.HEX:
            TriCell      k    = selectedCell;
            int          elev = selectedCell.Elevation;
            TriDirection tDir = dir.Previous();
            for (int i = 0; i < 6; i++)
            {
                if (!k)
                {
                    break;
                }
                RecalculateTerrain(k);
                k    = k.GetNeighbor(tDir);
                tDir = tDir.Next();
            }
            break;

        case SizeType.SINGLE:
            RecalculateTerrain(selectedCell);
            break;
        }

        terrainSelectionViewer.Apply();
    }
Exemplo n.º 4
0
    public IEnumerator <Coroutine> LoadBuilding(string path)
    {
        ClearBuildings();
        using (BinaryReader reader = new BinaryReader(File.OpenRead(Path.Combine(path, "building.dat")))) {
            int header = reader.ReadInt32();
            if (header <= 0)
            {
                int counter = reader.ReadInt32();
                for (int i = 0; i < counter; i++)
                {
                    TriCoordinates coord  = TriCoordinates.Load(reader);
                    Building       loaded = Building.Load(reader);
                    if (loaded.type == BuildingType.HALL)
                    {
                        camp = loaded as Hall;
                    }
                    if (loaded)
                    {
                        loaded.ID                = i;
                        loaded.Location          = grid.GetCell(coord);
                        loaded.EntranceDirection = loaded.EntranceDirection;
                        switch (loaded.sizeType)
                        {
                        case SizeType.SINGLE:
                            loaded.Location.Statics = loaded;
                            break;

                        case SizeType.HEX:
                            TriCell      k    = loaded.Location;
                            TriDirection tDir = loaded.EntranceDirection.Previous();
                            for (int j = 0; j < 6; j++)
                            {
                                if (!k)
                                {
                                    break;
                                }
                                k.Statics = loaded;
                                k         = k.GetNeighbor(tDir);
                                tDir      = tDir.Next();
                            }
                            break;
                        }

                        loaded.transform.SetParent(BuildingGroup);
                        AddBuilding(loaded);
                    }
//                    if (i % Strings.refreshLimit == 0) yield return null;
                }
            }
            else
            {
                Debug.LogWarning("Unknown building format " + header);
            }
        }
        yield return(null);
    }
Exemplo n.º 5
0
    public Building CreateBuilding(TriDirection dir, TriCell cell, Building prefab)
    {
        List <BuildState> result = prefab.GetBuildStatus(cell.coordinates, dir);
        bool buildable           = true;

        foreach (BuildState i in result)
        {
            if (!i.value)
            {
                buildable = false;
            }
        }
        if (cell && buildable)
        {
            Building ret = Instantiate(prefab);
            ret.ID       = entities.BuildingCount;
            ret.Location = cell;
            switch (ret.sizeType)
            {
            case SizeType.SINGLE:
                cell.Statics = ret;
                break;

            case SizeType.HEX:
                TriCell      k    = cell;
                TriDirection tDir = dir.Previous();
                for (int i = 0; i < 6; i++)
                {
                    if (!k)
                    {
                        break;
                    }
                    k.Statics = ret;
                    k         = k.GetNeighbor(tDir);
                    tDir      = tDir.Next();
                }
                break;
            }
            ret.EntranceDirection = dir;
            ret.personList        = personList;
            entities.AddBuilding(ret);
            island.ShowSummary = island.ShowSummary;
            Debug.Log(ret + " built");
            return(ret);
        }
        else
        {
            Debug.Log("building failed");
            return(null);
        }
    }
Exemplo n.º 6
0
 void ValidateDrag(TriCell currentCell)
 {
     for (
         dragDirection = TriDirection.VERT;
         dragDirection <= TriDirection.RIGHT;
         dragDirection++
         )
     {
         if (previousCell.GetNeighbor(dragDirection) == currentCell)
         {
             isDrag = true;
             return;
         }
     }
     isDrag = false;
 }
Exemplo n.º 7
0
    public override void BindCells(bool flag)
    {
        TriCell      k    = Location;
        int          elev = k.Elevation;
        TriDirection tDir = EntranceDirection.Previous();

        for (int i = 0; i < 6; i++)
        {
            if (!k)
            {
                break;
            }
            k.Statics = flag?this:null;
            k         = k.GetNeighbor(tDir);
            tDir      = tDir.Next();
        }
    }
Exemplo n.º 8
0
    void TriangulateConnection(TriDirection direction, TriCell cell, EdgeVertices e1, int inverter)
    {
        Vector3 center   = cell.transform.localPosition;
        TriCell neighbor = cell.GetNeighbor(direction);

        if (neighbor == null)
        {
            return;
        }
        Vector3 bridge = TriMetrics.GetBridge(direction);

        bridge.y = neighbor.Position.y - cell.Position.y;
        EdgeVertices e2 = new EdgeVertices(
            e1.v1 + bridge,
            e1.v5 + bridge
            );

        TriangulateEdgeStrip(terrain, e1, cell.Color, e2, neighbor.Color, cell.HasRiverThroughEdge(direction));
    }
Exemplo n.º 9
0
    bool IsBuildable()
    {
        if (nowCell)
        {
            if (!nowCell.GetNeighbor(dir).IsBuildable())
            {
                return(false);
            }
            if (sizeType != null)
            {
                switch (sizeType)
                {
                case SizeType.HEX:
                    TriCell      k    = nowCell;
                    int          elev = nowCell.Elevation;
                    TriDirection tDir = dir.Previous();
                    for (int i = 0; i < 6; i++)
                    {
                        if (!k || !k.IsBuildable())
                        {
                            return(false);
                        }
                        k    = k.GetNeighbor(tDir);
                        tDir = tDir.Next();
                    }
                    return(true);

                case SizeType.SINGLE:
                    return(nowCell.IsBuildable());

                default:
                    return(false);
                }
            }
            return(true);
        }
        else
        {
            return(false);
        }
    }
Exemplo n.º 10
0
    public Building CreateBuilding(TriDirection dir, TriCell cell, Building prefab)
    {
        if (cell && Entity.IsBuildable(dir, cell.coordinates, prefab.sizeType))
        {
            Building ret = Instantiate(prefab);
            ret.ID       = entities.BuildingCount;
            ret.Location = cell;
            switch (ret.sizeType)
            {
            case SizeType.SINGLE:
                cell.Statics = ret;
                break;

            case SizeType.HEX:
                TriCell      k    = cell;
                TriDirection tDir = dir.Previous();
                for (int i = 0; i < 6; i++)
                {
                    if (!k)
                    {
                        break;
                    }
                    k.Statics = ret;
                    k         = k.GetNeighbor(tDir);
                    tDir      = tDir.Next();
                }
                break;
            }
            ret.EntranceDirection = dir;
            ret.personList        = personList;
            entities.AddBuilding(ret);
            Debug.Log("camp built");
            return(ret);
        }
        else
        {
            Debug.Log("building failed");
            return(null);
        }
    }
Exemplo n.º 11
0
    public static bool IsBuildable(TriDirection dir, TriCoordinates coord, SizeType sizeType)
    {
        switch (sizeType)
        {
        case SizeType.SINGLE:
            if (TriGrid.Instance.GetCell(coord).Statics)
            {
                return(false);
            }
            else
            {
                return(true);
            }

        case SizeType.HEX:
            TriCell      cell      = TriGrid.Instance.GetCell(coord);
            int          elevation = cell.Elevation;
            TriCell      k         = cell;
            int          elev      = cell.Elevation;
            TriDirection tDir      = dir.Previous();
            for (int i = 0; i < 6; i++)
            {
                if (!k || !k.IsBuildable())
                {
                    return(false);
                }
                if (elev != k.Elevation)
                {
                    return(false);
                }
                k    = k.GetNeighbor(tDir);
                tDir = tDir.Next();
            }
            return(true);

        default:
            return(false);
        }
    }
Exemplo n.º 12
0
    public override List <BuildState> GetBuildStatus(TriCoordinates coord, TriDirection dir)
    {
        List <BuildState> ret = new List <BuildState>();
        TriCell           k   = TriGrid.Instance.GetCell(coord);
        int          elev     = k.Elevation;
        TriDirection tDir     = dir.Previous();

        for (int i = 0; i < 6; i++)
        {
            if (!k)
            {
                break;
            }
            ret.Add(new BuildState(k.coordinates, k.IsBuildable() && elev == k.Elevation));
            k    = k.GetNeighbor(tDir);
            tDir = tDir.Next();
        }
        TriCell entrance = TriGrid.Instance.GetCell(coord).GetNeighbor(dir);

        ret.Add(new BuildState(entrance.coordinates, entrance.IsBuildable() && Mathf.Abs(entrance.GetNeighbor(dir).Elevation - elev) < 2));
        return(ret);
    }
Exemplo n.º 13
0
 void EditCell(TriCell cell)
 {
     if (activeTerrainTypeIndex >= 0)
     {
         cell.TerrainTypeIndex = activeTerrainTypeIndex;
     }
     if (applyElevation)
     {
         cell.Elevation = activeElevation;
     }
     if (riverMode == OptionalToggle.No)
     {
         cell.RemoveRiver(dragDirection);
     }
     else if (isDrag && riverMode == OptionalToggle.Yes)
     {
         TriCell otherCell = cell.GetNeighbor(dragDirection);
         if (otherCell)
         {
             otherCell.SetRiver(dragDirection);
         }
     }
 }
Exemplo n.º 14
0
    public void CalculateTerrain()
    {
        Clear();

        if (selector.nowCell)
        {
            TriCell neighbor = selector.nowCell.GetNeighbor(selector.dir);
            if (neighbor)
            {
                RecalculateTerrain(neighbor, neighbor.IsBuildable() && neighbor.Elevation <= selector.nowCell.Elevation + 1 && neighbor.Elevation >= selector.nowCell.Elevation - 1);
            }
            switch (selector.sizeType)
            {
            case SizeType.HEX:
                k = selector.nowCell;
                int          elev = selector.nowCell.Elevation;
                TriDirection tDir = selector.dir.Previous();
                for (int i = 0; i < 6; i++)
                {
                    if (!k)
                    {
                        break;
                    }
                    RecalculateTerrain(k, k.IsBuildable() && k.Elevation == elev);
                    k    = k.GetNeighbor(tDir);
                    tDir = tDir.Next();
                }
                break;

            case SizeType.SINGLE:
                RecalculateTerrain(selector.nowCell, selector.nowCell.IsBuildable());
                break;
            }
        }
        Apply();
    }
Exemplo n.º 15
0
    void EditHex(TriCell cell)
    {
        TriCell      k        = cell;
        bool         inverted = cell.inverted;
        TriDirection d        = TriDirection.VERT;

        for (int i = 0; i < 6; i++)
        {
            if (!k)
            {
                break;
            }
            EditCell(k);
            k = k.GetNeighbor(d);
            if (inverted)
            {
                d = d.Next();
            }
            else
            {
                d = d.Previous();
            }
        }
    }
Exemplo n.º 16
0
 public override void OnBuilt()
 {
     base.OnBuilt();
     if (workType == TerrainModdingType.LANDFILL)
     {
         TriCell k = Location;
         for (int j = 0; j < 3; j++)
         {
             TriDirection tDir = (TriDirection)j;
             for (int i = 0; i < 6; i++)
             {
                 k.Elevation = k.Elevation < 1 ? 1 : k.Elevation;
                 k           = k.GetNeighbor(tDir);
                 tDir        = tDir.Next();
             }
         }
     }
     else
     {
         location.Elevation += (int)workType;
     }
     SelfWorking = true;
     DeconstructionStart(null);
 }
Exemplo n.º 17
0
    bool Search(TriCell fromCell, TriCell toCell, bool entityCheck)
    {
        searchFrontierPhase += 2;
        if (searchFrontier == null)
        {
            searchFrontier = new TriCellPriorityQueue();
        }
        else
        {
            searchFrontier.Clear();
        }

        for (int i = 0; i < cells.Length; i++)
        {
            cells[i].Distance = int.MaxValue;
        }
        fromCell.SearchPhase = searchFrontierPhase;
        fromCell.Distance    = 0;
        searchFrontier.Enqueue(fromCell);
        while (searchFrontier.Count > 0)
        {
            TriCell current = searchFrontier.Dequeue();
            current.SearchPhase += 1;
            if (current == toCell)
            {
                return(true);
            }
            int currentTurn = current.Distance;

            for (TriDirection d = TriDirection.VERT; d <= TriDirection.RIGHT; d++)
            {
                TriCell neighbor = current.GetNeighbor(d);
                if (neighbor == null ||
                    neighbor.SearchPhase > searchFrontierPhase ||
                    neighbor.Distance != int.MaxValue ||
                    neighbor.IsUnderwater ||
                    neighbor.HasRiver ||
                    (entityCheck && !neighbor.Stepable) ||
                    Mathf.Abs(neighbor.Elevation - current.Elevation) > 1)
                {
                    continue;
                }
                int distance = current.Distance;
                if (current.IsRoad)
                {
                    distance += 1;
                }
                else
                {
                    distance += 10;
                }
                distance += current.Elevation != neighbor.Elevation ? 1 : 0;
                if (neighbor.SearchPhase < searchFrontierPhase)
                {
                    neighbor.SearchPhase     = searchFrontierPhase;
                    neighbor.Distance        = distance;
                    neighbor.PathFrom        = current;
                    neighbor.SearchHeuristic =
                        neighbor.coordinates.DistanceTo(toCell.coordinates);
                    searchFrontier.Enqueue(neighbor);
                }
                else if (distance < neighbor.Distance)
                {
                    int oldPriority = neighbor.SearchPriority;
                    neighbor.Distance = distance;
                    neighbor.PathFrom = current;
                    searchFrontier.Change(neighbor, oldPriority);
                }
                labels[neighbor.Index].text = "" + neighbor.Distance;
            }
        }
        return(false);
    }
Exemplo n.º 18
0
 IEnumerator <WaitForEndOfFrame> SetTerrainType()
 {
     for (int i = 0; i < cellCount; i++)
     {
         TriCell cell = grid.GetCell(i), hexCell = grid.GetCell(TriMetrics.TriToHex(cell.coordinates));
         if (cell.coordinates == hexCell.coordinates)
         {
             float        moisture = climate[hexCell.Index].moisture;
             TriDirection d        = TriDirection.VERT;
             TriIsland    isle     = TriIsland.Instance;
             for (int j = 0; j < 6; j++)
             {
                 if (!cell)
                 {
                     break;
                 }
                 if (!cell.IsUnderwater)
                 {
                     if (cell.Elevation > 10)
                     {
                         cell.TerrainTypeIndex = 4;
                     }
                     else if (moisture < 0.02f)
                     {
                         cell.TerrainTypeIndex = 3;
                         if (Random.value < 0.5f && !cell.HasRiver)
                         {
                             Tree t = (Tree)Instantiate(TriIsland.GetNaturalPrefabs((int)NaturalType.TREE, 0), isle.transform);
                             t.Location          = cell;
                             t.EntranceDirection = (TriDirection)((int)(Random.value * 3f));
                             entities.AddNatural(t);
                         }
                     }
                     else if (moisture < 0.12f)
                     {
                         cell.TerrainTypeIndex = 2;
                         if (Random.value < 0.2f && !cell.HasRiver)
                         {
                             Tree t = (Tree)Instantiate(TriIsland.GetNaturalPrefabs((int)NaturalType.TREE, 0), isle.transform);
                             t.Location          = cell;
                             t.EntranceDirection = (TriDirection)((int)(Random.value * 3f));
                             entities.AddNatural(t);
                         }
                     }
                     else if (moisture < 0.20f)
                     {
                         cell.TerrainTypeIndex = 1;
                     }
                     else if (moisture < 0.85f)
                     {
                         cell.TerrainTypeIndex = 0;
                     }
                     else
                     {
                         cell.TerrainTypeIndex = 0;
                     }
                 }
                 else
                 {
                     cell.TerrainTypeIndex = 1;
                 }
                 cell = cell.GetNeighbor(d);
                 if (hexCell.inverted)
                 {
                     d = d.Next();
                 }
                 else
                 {
                     d = d.Previous();
                 }
             }
             if (i % Metrics.refreshLimit == 0)
             {
                 yield return(null);
             }
         }
     }
 }
Exemplo n.º 19
0
    int RaiseTerrain(int chunkSize, int budget, bool initiated)
    {
        searchFrontierPhase += 1;
        TriCoordinates center = grid.GetCell(grid.cellCountX / 2, grid.cellCountZ / 2).coordinates;

        center = TriMetrics.TriToHex(center);
        TriCell firstCell = grid.GetCell(center.X, center.Z);

        if (initiated)
        {
            do
            {
                firstCell = GetRandomCell();
            } while (firstCell.Elevation < 1);
        }
        searchFrontier.Enqueue(firstCell);
        center = firstCell.coordinates;
        checker.Add(center);
        int size = 0;

        while (size < chunkSize && searchFrontier.Count > 0)
        {
            TriCell current = searchFrontier.Dequeue();
            if (current.Elevation < elevationMaximum)
            {
                TriDirection d = TriDirection.VERT;
                TriCell      k = current;
                if (k.Elevation < elevationMaximum)
                {
                    if (k.Elevation == 0)
                    {
                        budget -= 6;
                    }
                    for (int i = 0; i < 6; i++)
                    {
                        if (!k)
                        {
                            break;
                        }
                        k.Elevation += 1;
                        k            = k.GetNeighbor(d);
                        if (current.inverted)
                        {
                            d = d.Next();
                        }
                        else
                        {
                            d = d.Previous();
                        }
                    }
                }
            }

            if (current.Elevation == 1 && budget == 0)
            {
                break;
            }
            size += 6;
            TriCoordinates coord = current.coordinates;
            for (int i = 0; i < 4; i++)
            {
                TriCell neighbor = grid.GetCell(coord.X + TriMetrics.hexDir[i, 0], coord.Z + TriMetrics.hexDir[i, 1]);
                if (neighbor && checkbounds(neighbor.coordinates) && !checker.Contains(neighbor.coordinates) && (i == 3 || Random.value < jitterProbability ? true : false))
                {
                    searchFrontier.Enqueue(neighbor);
                    checker.Add(neighbor.coordinates);
                }
            }
        }
        searchFrontier.Clear();
        checker.Clear();
        return(budget);
    }