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))); }
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); }
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); }
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)); }
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)); }
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)); }
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(); }
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); }
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); }
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); }
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)); }
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];
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); }
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); }
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)); }
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); } }
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); }
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); }
/// <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); }
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); }
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); }
/// <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); }
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); }
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)); }
public static int Distance(AxialCoord a, AxialCoord b) { return(CubeCoord.Distance((CubeCoord)a, (CubeCoord)b)); }
/// <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); }
/// <summary> /// Copy Constructer /// </summary> /// <param name="c">The CubeCoord to copy</param> public CubeCoord(CubeCoord c) : this(c.q, c.r, c.s) { // Empty }
public static OffsetCoord CubeToOddRow(CubeCoord c) { return(new OffsetCoord((int)(c.X + (c.Z - ((int)c.Z & 1)) / 2), (int)c.Z)); }
public static OffsetCoord CubeToEvenRow(CubeCoord c) { return(new OffsetCoord((int)(c.X + (c.Z + ((int)c.Z & 1))) / 2, (int)c.Z)); }
public static OffsetCoord CubeToOddColumn(CubeCoord c) { return(new OffsetCoord((int)c.X, (int)(c.Z + (c.X - ((int)c.X & 1)) / 2))); }