Example #1
0
    private static int CubeDistance(CubeCoord a, CubeCoord b)
    {
        int dQ = Mathf.Abs(a.q - b.q);
        int dR = Mathf.Abs(a.r - b.r);

        return(Mathf.Max(dQ, dR, Mathf.Abs(a.s - b.s)));
    }
Example #2
0
    private static List <SpaceModel> MapLinedraw(SpaceModel start, SpaceModel end, MapModel map)
    {
        CubeCoord a = CoordinatesToCubeCoords(start);
        CubeCoord b = CoordinatesToCubeCoords(end);


        double            N       = CubeDistance(a, b);
        List <SpaceModel> results = new List <SpaceModel>();

        if (Math.Abs(N) > Double.Epsilon)
        {
            for (int i = 0; i <= N; i++)
            {
                CubeCoord    coord       = RoundCubeCoords(CubeLerp(a, b, 1 / N * i));
                DoubledCoord normalCoord = CubeCoordsToCoordinates(coord);
                SpaceModel   newSpace;
                newSpace = map.GetSpace(normalCoord.Row, normalCoord.Column);
                if (newSpace != null)
                {
                    results.Add(newSpace);
                }
            }
        }
        return(results);
    }
Example #3
0
 private bool CanChoose(int[,] data, AxialCoord start, CubeCoord chooseCoord, int howManyIsChosen, int index)
 {
     if (howManyIsChosen >= 2)
     {
         if (!IsInArea((start + (AxialCoord)chooseCoord).x, (start + (AxialCoord)chooseCoord).z))
         {
             return(false);
         }
         if (data[(start + (AxialCoord)chooseCoord).x, (start + (AxialCoord)chooseCoord).z] != index)
         {
             return(false);
         }
     }
     if (howManyIsChosen == 3)
     {
         if (!IsInArea((start + (AxialCoord)chooseCoord * 2).x, (start + (AxialCoord)chooseCoord * 2).z))
         {
             return(false);
         }
         if (data[(start + (AxialCoord)chooseCoord * 2).x, (start + (AxialCoord)chooseCoord * 2).z] != index)
         {
             return(false);
         }
     }
     return(true);
 }
Example #4
0
    public static CubeCoord CubeRound(CubeCoord cube)
    {
        var rx = Mathf.Round((float)cube.Q);
        var ry = Mathf.Round((float)cube.R);
        var rz = Mathf.Round((float)cube.S);

        var x_diff = Mathf.Abs(rx - (float)cube.Q);
        var y_diff = Mathf.Abs(ry - (float)cube.R);
        var z_diff = Mathf.Abs(rz - (float)cube.S);

        if (x_diff > y_diff && x_diff > z_diff)
        {
            rx = -ry - rz;
        }
        else if (y_diff > z_diff)
        {
            ry = -rx - rz;
        }
        else
        {
            rz = -rx - ry;
        }

        return(new CubeCoord(rx, ry, rz));
    }
Example #5
0
    public static OffsetCoord CubeToOffset(CubeCoord h)
    {
        int col = h.q + (h.r - (h.r & 1)) / 2;
        int row = h.r;

        return(new OffsetCoord(col, row));
    }
Example #6
0
    private static DoubledCoord CubeCoordsToCoordinates(CubeCoord coord)
    {
        int col = (int)Math.Round(2 * coord.X + coord.Z);
        int row = (int)Math.Round(coord.Z);

        return(new DoubledCoord(col, row));
    }
Example #7
0
    public DigArea(CubeCoord origin, int curHeight, int digRadius, int maxHeight, byte flags)
    {
        // Need to know where the block is
        this.Origin = origin;

        // Current dig position (offset from block)
        this.CurHeight = curHeight;

        // Dig settings
        this.DigRadius = digRadius;
        this.MaxHeight = maxHeight;

        Volume = ((digRadius * 2) + 1) * ((digRadius * 2) + 1) * maxHeight;

        // We'll need these to math later
        var rotationQuart = SegmentCustomRenderer.GetRotationQuaternion(flags);

        vectorUp      = rotationQuart * Vector3.up;
        vectorForward = rotationQuart * Vector3.forward;
        vectorRight   = rotationQuart * Vector3.right;

        vectorUp.Normalize();
        vectorForward.Normalize();
        vectorRight.Normalize();
    }
Example #8
0
    public Resources TickTile(TileGridController controller, CubeCoord pos, Upgrades upgrades, TileScript tile)
    {
        var buildingResources = upgrades.Calculate(new Resources());
        var modified          = globalUpgrades.Calculate(buildingResources);

        if (tile.Structure.type == StructureType.LUMBERJACK)
        {
            var neighborTiles = controller.GetNeighborTiles(pos);
            modified.Mul(ItemType.WOOD, neighborTiles.Count(t => t.GetComponent <TileScript>().Structure.type == StructureType.WOODS));
        }

        if (tile.Structure.type == StructureType.CHARCOAL_BURNER)
        {
            var neighborTiles = controller.GetNeighborTiles(pos);
            var cnt           = neighborTiles.Count(t => t.GetComponent <TileScript>().Structure.type == StructureType.CHARCOAL_BURNER) + 1;
            modified.Mul(ItemType.CHARCOAL, cnt);
        }

        if (modified.Items.Count > 0)
        {
            if (Global.Resources.Add(modified))
            {
                var floaty = tile.floaty(controller.worldUi.transform, modified, true, "");
                floaty.transform.position = Camera.main.WorldToScreenPoint(tile.transform.position);
            }
        }

        return(modified);
    }
Example #9
0
        public void CubeToOffsetTest(int x, int y, int z, int expectedX, int expectedY)
        {
            var cube   = new CubeCoord(x, y, z);
            var offset = cube.ToOffset();

            Assert.AreEqual(expectedX, offset.X);
            Assert.AreEqual(expectedY, offset.Y);
        }
Example #10
0
        public void CubeToAxialTest(int x, int y, int z, int expectedQ, int expectedR)
        {
            var cube  = new CubeCoord(x, y, z);
            var axial = cube.ToAxial();

            Assert.AreEqual(expectedQ, axial.Q);
            Assert.AreEqual(expectedR, axial.R);
        }
Example #11
0
    public static int Distance(Hex a, Hex b)
    {
        // convert offset coordinate to cube coordinate
        CubeCoord ac = HexCoordinate.OffsetToCube(a.Coordinates);
        CubeCoord bc = HexCoordinate.OffsetToCube(b.Coordinates);

        return(CubeDistance(ac, bc));
    }
Example #12
0
 private void CacheNeighbourCoords()
 {
     cubeNeighbours  = new CubeCoord[6];
     axialNeighbours = new AxialCoord[6];
     for (int i = 0; i < 6; i++)
     {
         ref CubeCoord reference = ref cubeNeighbours[i];
         reference = CubeCoord + CubeCoord.Directions[i];
         ref AxialCoord reference2 = ref axialNeighbours[i];
Example #13
0
        public void DifferentTypeDifferentValueNotEqualTest(int cubeX, int cubeY, int cubeZ, int axialQ, int axialR, int offsetX, int offsetY)
        {
            var cube   = new CubeCoord(cubeX, cubeY, cubeZ);
            var axial  = new AxialCoord(axialQ, axialR);
            var offset = new OffsetCoord(offsetX, offsetY);

            Assert.AreNotEqual(cube, axial);
            Assert.AreNotEqual(cube, offset);
            Assert.AreNotEqual(axial, offset);
        }
Example #14
0
        private int[,] AfterData(TheCase theCase, int index)
        {
            var theData         = (int[, ])data.Clone();
            var chooseDirection = (CubeDirection)theCase.chooseDirection;
            var moveDirection   = (CubeDirection)theCase.moveDirection;

            for (int j = 0; j < theCase.howManyWillPush; j++)
            {
                var i = theCase.howManyWillPush - j - 1;

                CubeCoord chosenPosition = new CubeCoord(0, 0, 0);
                if (chooseDirection.ToCoord() == moveDirection.ToCoord())
                {
                    chosenPosition = theCase.target + (AxialCoord)chooseDirection.ToCoord() * (theCase.howManyIsChosen + i);
                }
                if (chooseDirection.ToCoord() == moveDirection.ToCoord() * (-1))
                {
                    chosenPosition = theCase.target - (AxialCoord)chooseDirection.ToCoord() + (AxialCoord)moveDirection.ToCoord() * i;
                }

                //var chosenPosition = theCase.target + (AxialCoord)chooseDirection.ToCoord() * (theCase.howManyIsChosen + i);

                var beforePosition = (AxialCoord)chosenPosition;
                var afterPosition  = beforePosition + (AxialCoord)moveDirection.ToCoord();
                theData[beforePosition.x, beforePosition.z] = 0;

                if (!IsInArea(afterPosition.x, afterPosition.z))
                {
                    continue;
                }

                theData[afterPosition.x, afterPosition.z] = 3 - index;
            }
            for (int j = 0; j < theCase.howManyIsChosen; j++)
            {
                int i;
                if (chooseDirection.ToCoord() == moveDirection.ToCoord())
                {
                    i = theCase.howManyIsChosen - j - 1;
                }
                else
                {
                    i = j;
                }

                var beforePosition = theCase.target + (AxialCoord)chooseDirection.ToCoord() * i;
                var afterPosition  = beforePosition + (AxialCoord)moveDirection.ToCoord();
                theData[beforePosition.x, beforePosition.z] = 0;
                theData[afterPosition.x, afterPosition.z]   = index;
            }
            return(theData);
        }
Example #15
0
    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return(false);
        }
        CubeCoord o = (CubeCoord)obj;

        if ((System.Object)o == null)
        {
            return(false);
        }
        return((Q == o.Q) && (R == o.R) && (S == o.S));
    }
Example #16
0
        private bool WasChosen(CubeCoord cubeCoord, CubeDirection cubeDirection, int chosen)
        {
            switch (chosen)
            {
            case 1:
                return(cubeCoord == new CubeCoord(0, 0, 0));

            case 2:
                return(cubeCoord == new CubeCoord(0, 0, 0) || cubeCoord == cubeDirection.ToCoord());

            case 3:
                return(cubeCoord == new CubeCoord(0, 0, 0) || cubeCoord == cubeDirection.ToCoord() || cubeCoord == cubeDirection.ToCoord() * 2);

            default:
                return(false);
            }
        }
Example #17
0
    public bool IsBuildAllowed(TileGridController controller, CubeCoord coord)
    {
        if (!buildable)
        {
            return(false);
        }
        var neighborTiles = controller.GetNeighborTiles(coord);

        if (limit)
        {
            if (limit.BuildLimited(neighborTiles))
            {
                return(false);
            }
        }

        return(true);
    }
Example #18
0
        private List <CubeCoord> ComputeCubeLinedraw(CubeCoord a, CubeCoord b)
        {
            var result = new List <CubeCoord>();

            if (a == b)
            {
                return(result);
            }

            var N = CubeDistance(a, b);

            for (var i = 0; i < N + 1; i++)
            {
                result.Add(CubeLerp(a, b, (float)i / N));
            }

            return(result);
        }
Example #19
0
    /// <summary>
    /// Instantiates a new tile at the coord passed in
    /// </summary>
    /// <param name="coord">The coord to be used</param>
    private void PlaceTile(OffsetCoord coord)
    {
        // Create the object
        var        hexPrefabObject = hexPrefabs[Random.Range(0, hexPrefabs.Count)];
        GameObject hex             = Instantiate(
            hexPrefabObject,
            transform.position + CalculateWorldPosition(new Vector2(coord.Column, coord.Row)),
            Quaternion.Euler(new Vector3(0, Random.Range(0, 5) * 60, 0)),
            this.transform);

        hex.name = string.Format("Hex {0} {1}", coord.Column, coord.Row);

        // Set up the object
        HexObject hexObject = hex.GetComponent <HexObject>();

        hexObject.hex   = new Hex(CubeCoord.OddRowToCube(coord));
        hexObject.Index = Instance.Hexes.Count;
        Instance.Hexes.Add(hexObject);
    }
Example #20
0
        static CubeCoord FollowPath()
        {
            var pos = new CubeCoord();

            foreach (var direction in Input)
            {
                if (direction.Equals("n"))
                {
                    pos.Z--;
                    pos.Y++;
                }
                if (direction.Equals("s"))
                {
                    pos.Z++;
                    pos.Y--;
                }
                if (direction.Contains("ne"))
                {
                    pos.X++;
                    pos.Z--;
                }
                if (direction.Contains("nw"))
                {
                    pos.Y++;
                    pos.X--;
                }
                if (direction.Contains("se"))
                {
                    pos.Y--;
                    pos.X++;
                }
                if (direction.Contains("sw"))
                {
                    pos.X--;
                    pos.Z++;
                }
                var distance = ManhatanCube(pos);
                _maxDistance = distance > _maxDistance ? distance : _maxDistance;
            }

            return(pos);
        }
Example #21
0
    public bool CanBuildDeductCost(TileGridController controller, CubeCoord coord)
    {
        if (!buildable)
        {
            return(false);
        }
        if (!IsBuildAllowed(controller, coord))
        {
            return(false);
        }

        var cost = Cost(controller);

        if (Global.Resources.HasResources(cost))
        {
            Global.Resources.AddNoCheck(cost);
            return(true);
        }
        return(false);
    }
Example #22
0
    /// <summary>
    /// Gets all the Hexes in a range around the center
    /// </summary>
    /// <param name="center">The Hex that will be checked around</param>
    /// <param name="range">The amount of tiles away to be included</param>
    /// <returns>A List of the Hexes</returns>
    public static List <Hex> HexesInRange(this HexGrid Instance, Hex center, int range)
    {
        List <Hex> ret = new List <Hex>();

        CubeCoord c;

        for (int dx = -range; dx <= range; dx++)
        {
            for (int dy = Mathf.Max(-range, -dx - range); dy <= Mathf.Min(range, -dx + range); dy++)
            {
                c = new CubeCoord(dx, dy, -dx - dy) + center.cubeCoords;
                if (Instance.Hexes.Contains(FindHexObject(Instance, c)))
                {
                    ret.Add(new Hex(c));
                }
            }
        }

        return(ret);
    }
Example #23
0
    public Mk2Excavator(Segment segment, long x, long y, long z, ushort cube, byte flags, ushort lValue,
                        bool lbFromDisk, int powerDefault, int powerOre, int digRadius, int digHeight, int maxPower, int opBlocks)
        : base(eSegmentEntity.Mod, SpawnableObjectEnum.AutoExcavator, x, y, z, cube, flags, lValue, Vector3.zero,
               segment)
    {
        this.mrPowerRate               = powerDefault;
        this.mrPowerRateDefault        = this.mrPowerRate;
        this.powerDefaultBackup        = powerDefault;
        this.mrPowerRateOre            = powerOre;
        this.powerOreBackup            = powerOre;
        this.mbNeedsLowFrequencyUpdate = true;
        this.mbNeedsUnityUpdate        = true;
        this.mbWorkComplete            = false;

        this.mnDigSizeX        = digRadius; // 9;
        this.mnDigSizeY        = digHeight; // 128;
        this.mnDigSizeZ        = digRadius; // 9;
        this.mnCurrentDigSizeX = digRadius; // 9;
        this.mnCurrentDigSizeY = 1;
        this.mnCurrentDigSizeZ = digRadius; // 9;
        this.mRand             = new System.Random();
        this.mbLocatedBlock    = false;
        this.mbDoDigOre        = false;
        this.eExcavateState    = ExcavateState.ClearGarbage;
        this.eDropState        = DropState.DropSome;
        this.mfCommandDebounce = 0.02f;
        this.mCubeColor        = Color.blue;
        this.mbDoDropBlocks    = true;
        this.mrMaxPower        = maxPower;
        this.mutePews          = 0;
        // New DigArea object should go in here
        OPBlockCount = opBlocks;
        machineFlags = flags;
        origin       = new CubeCoord(x, y, z);
        digArea      = new DigArea(origin, 0, digRadius, digHeight, machineFlags);
    }
Example #24
0
    private static CubeCoord RoundCubeCoords(CubeCoord cubeCoord)
    {
        double newX = Math.Round(cubeCoord.X);
        double newY = Math.Round(cubeCoord.Y);
        double newZ = Math.Round(cubeCoord.Z);

        double x_diff = Math.Abs(newX - cubeCoord.X);
        double y_diff = Math.Abs(newY - cubeCoord.Y);
        double z_diff = Math.Abs(newZ - cubeCoord.Z);

        if (x_diff > y_diff && x_diff > z_diff)
        {
            newX = -newY - newZ;
        }
        else if (y_diff > z_diff)
        {
            newY = -newX - newZ;
        }
        else
        {
            newZ = -newX - newY;
        }
        return(new CubeCoord(newX, newY, newZ));
    }
Example #25
0
 public static int Distance(AxialCoord a, AxialCoord b)
 {
     return(CubeCoord.Distance((CubeCoord)a, (CubeCoord)b));
 }
Example #26
0
    /// <summary>
    /// Main function for creating the objects in the grid
    /// </summary>
    private void CreateCell(int x, int z, int i)
    {
        /////////////////////////////////
        /// Create Cell and populate data
        /////////////////////////////////

        Vector3   position  = new Vector3(((x + z * 0.5f - z / 2) * HexMetrics.Instance.innerRadius * 2f), 0f, -(z * (HexMetrics.Instance.outerRadius * 1.5f)));
        HexObject hexObject = Hexes[i] = Instantiate(hexPrefab);

        hexObject.Index = i;
        hexObject.Hex   = new Hex(CubeCoord.OddRowToCube(new OffsetCoord(x, z)));
        hexObject.Color = MapGenerator.Instance.colorMap[i];
        hexObject.transform.localPosition = position;
        hexObject.name      += " " + hexObject.Hex.cubeCoords.ToString();
        hexObject.Elevation  = MapGenerator.Instance.heightMap[x, z];
        hexObject.WaterLevel = (hexObject.Elevation <= 0.4f) ? .1f : 0f;

        ///////////////////////////////////////////////////////
        /// Set a starting plant level for hex's that are green
        ///////////////////////////////////////////////////////

        if (hexObject.Color == MapGenerator.Instance.regions.Where(r => r.name == "Grass").FirstOrDefault().color ||
            hexObject.Color == MapGenerator.Instance.regions.Where(r => r.name == "Grass 2").FirstOrDefault().color)
        {
            Random.State currentState = Random.state;
            Random.InitState(MapGenerator.Instance.mapSettings.noiseSetting.seed);

            hexObject.PlantLevel = Random.Range(0, 3);

            Random.state = currentState;
        }

        /////////////////////
        /// Set up Neighbours
        /////////////////////

        if (x > 0)
        {
            hexObject.SetNeighbour(HexDirection.W, Hexes[i - 1]);
        }
        if (z > 0)
        {
            if ((z & 1) == 0)
            {
                hexObject.SetNeighbour(HexDirection.NE, Hexes[i - Width]);
                if (x > 0)
                {
                    hexObject.SetNeighbour(HexDirection.NW, Hexes[i - Width - 1]);
                }
            }
            else
            {
                hexObject.SetNeighbour(HexDirection.NW, Hexes[i - Width]);
                if (x < Width - 1)
                {
                    hexObject.SetNeighbour(HexDirection.NE, Hexes[i - Width + 1]);
                }
            }
        }

        AddHexToChunk(x, z, hexObject);
    }
Example #27
0
 /// <summary>
 /// Copy Constructer
 /// </summary>
 /// <param name="c">The CubeCoord to copy</param>
 public CubeCoord(CubeCoord c) : this(c.q, c.r, c.s)
 {
     // Empty
 }
Example #28
0
 public static OffsetCoord CubeToOddRow(CubeCoord c)
 {
     return(new OffsetCoord((int)(c.X + (c.Z - ((int)c.Z & 1)) / 2), (int)c.Z));
 }
Example #29
0
 public static OffsetCoord CubeToEvenRow(CubeCoord c)
 {
     return(new OffsetCoord((int)(c.X + (c.Z + ((int)c.Z & 1))) / 2, (int)c.Z));
 }
Example #30
0
 public static OffsetCoord CubeToOddColumn(CubeCoord c)
 {
     return(new OffsetCoord((int)c.X, (int)(c.Z + (c.X - ((int)c.X & 1)) / 2)));
 }