• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Martomate / Hexacraft / 7351367576

28 Dec 2023 06:50PM UTC coverage: 51.185% (-0.1%) from 51.312%
7351367576

push

github

Martomate
Refactor: Made Chunk not know if it has been saved to file

9 of 12 new or added lines in 2 files covered. (75.0%)

110 existing lines in 32 files now uncovered.

2829 of 5527 relevant lines covered (51.19%)

0.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.89
/game/src/main/scala/hexacraft/world/coord/grid.scala
1
package hexacraft.world.coord
2

3
import hexacraft.math.bits.{Int12, Int20}
4
import hexacraft.world.CylinderSize
5

6
import org.joml.Vector2d
7

8
case class Offset(dx: Int, dy: Int, dz: Int) {
1✔
9
  def +(other: Offset): Offset = Offset(dx + other.dx, dy + other.dy, dz + other.dz)
10

1✔
11
  def -(other: Offset): Offset = Offset(dx - other.dx, dy - other.dy, dz - other.dz)
12

1✔
13
  def manhattanDistance: Int =
1✔
14
    math.abs(dy) + math.max(math.max(math.abs(dx), math.abs(dz)), math.abs(dx + dz))
15
}
16

17
object NeighborOffsets {
1✔
18
  val all: Seq[Offset] = IndexedSeq(
19
    Offset(0, 1, 0),
20
    Offset(0, -1, 0),
21
    Offset(1, 0, 0),
22
    Offset(0, 0, 1),
23
    Offset(-1, 0, 1),
24
    Offset(-1, 0, 0),
25
    Offset(0, 0, -1),
26
    Offset(1, 0, -1)
27
  )
28

29
  /** @param side
30
    * the side of the origin block (0: top, 1: bottom, 2-7: sides)
31
    * @return
×
32
    * The offset of the neighboring block on the given side
×
33
    */
1✔
34
  def apply(side: Int): Offset = all(side)
×
35

1✔
36
  val indices: Range = all.indices
1✔
37
}
1✔
38

39
object BlockRelChunk {
1✔
40
  def apply(x: Int, y: Int, z: Int): BlockRelChunk = BlockRelChunk((x & 0xf) << 8 | (y & 0xf) << 4 | (z & 0xf))
41
}
42

43
case class BlockRelChunk(value: Int) extends AnyVal { // xyz
1✔
44
  def cx: Byte = (value >> 8 & 0xf).toByte
1✔
45
  def cy: Byte = (value >> 4 & 0xf).toByte
1✔
46
  def cz: Byte = (value >> 0 & 0xf).toByte
47

1✔
48
  def offset(off: Offset): BlockRelChunk = offset(off.dx, off.dy, off.dz)
1✔
49
  def offset(x: Int, y: Int, z: Int): BlockRelChunk = BlockRelChunk(cx + x, cy + y, cz + z)
50

1✔
51
  def isOnChunkEdge(side: Int): Boolean =
1✔
52
    val off = NeighborOffsets(side)
1✔
53
    val xx = cx + off.dx
1✔
54
    val yy = cy + off.dy
1✔
55
    val zz = cz + off.dz
56
    (xx & ~15 | yy & ~15 | zz & ~15) != 0
57

1✔
58
  def neighbor(side: Int): BlockRelChunk =
1✔
59
    val off = NeighborOffsets(side)
1✔
60
    val xx = cx + off.dx
1✔
61
    val yy = cy + off.dy
1✔
62
    val zz = cz + off.dz
1✔
63
    BlockRelChunk(xx, yy, zz)
64

1✔
65
  def globalNeighbor(side: Int, chunk: ChunkRelWorld)(using CylinderSize): BlockRelWorld =
1✔
66
    val off = NeighborOffsets(side)
1✔
67
    val xx = cx + off.dx
1✔
68
    val yy = cy + off.dy
1✔
69
    val zz = cz + off.dz
1✔
70
    BlockRelWorld(xx, yy, zz, chunk)
71

×
72
  override def toString: String = s"($cx, $cy, $cz)"
73
}
74

75
object BlockRelWorld {
1✔
76
  def apply(X: Long, Y: Int, Z: Long, x: Int, y: Int, z: Int)(using cylSize: CylinderSize): BlockRelWorld =
77
    BlockRelWorld(
1✔
78
      (X & 0xfffff) << 44L | (Z & cylSize.ringSizeMask) << 24 | (Y & 0xfff) << 12 | (x & 0xf) << 8 | (y & 0xf) << 4 | (z & 0xf)
79
    )
80

1✔
81
  def apply(x: Int, y: Int, z: Int)(using CylinderSize): BlockRelWorld =
1✔
82
    BlockRelWorld(x >> 4, y >> 4, z >> 4, x & 15, y & 15, z & 15)
83

1✔
84
  def fromChunk(block: BlockRelChunk, chunk: ChunkRelWorld): BlockRelWorld =
85
    BlockRelWorld(chunk.value << 12 | block.value)
86

1✔
UNCOV
87
  def apply(i: Int, j: Int, k: Int, chunk: ChunkRelWorld)(using CylinderSize): BlockRelWorld =
×
88
    BlockRelWorld(chunk.X.toInt * 16 + i, chunk.Y.toInt * 16 + j, chunk.Z.toInt * 16 + k)
89
}
90

91
case class BlockRelWorld(value: Long) extends AnyVal { // XXXXXZZZZZYYYxyz
1✔
92
  def getBlockRelChunk: BlockRelChunk = BlockRelChunk((value & 0xfff).toInt)
1✔
93
  def getChunkRelWorld: ChunkRelWorld = ChunkRelWorld(value >>> 12)
1✔
94
  def getColumnRelWorld: ColumnRelWorld = ColumnRelWorld(value >>> 24)
95

1✔
96
  def offset(t: Offset)(using CylinderSize): BlockRelWorld = offset(t.dx, t.dy, t.dz)
1✔
97
  def offset(xx: Int, yy: Int, zz: Int)(using CylinderSize): BlockRelWorld = BlockRelWorld(x + xx, y + yy, z + zz)
98

1✔
99
  def X: Int20 = Int20.truncate(value >> 44)
1✔
100
  def Z: Int20 = Int20.truncate(value >> 24)
1✔
101
  def Y: Int12 = Int12.truncate(value >>> 12)
1✔
102
  def cx: Byte = (value >> 8 & 0xf).toByte
1✔
103
  def cy: Byte = (value >> 4 & 0xf).toByte
1✔
104
  def cz: Byte = (value >> 0 & 0xf).toByte
1✔
UNCOV
105
  def x: Int = X.toInt << 4 | cx
×
UNCOV
106
  def y: Int = Y.toInt << 4 | cy
×
107
  def z: Int = Z.toInt << 4 | cz
108

×
109
  override def toString = s"($x, $y, $z)"
110
}
111

112
object ChunkRelWorld {
1✔
113
  def apply(X: Long, Y: Int, Z: Int)(using cylSize: CylinderSize): ChunkRelWorld =
1✔
114
    ChunkRelWorld((X & 0xfffff) << 32 | (Z & cylSize.ringSizeMask) << 12 | (Y & 0xfff))
115

×
116
  def apply(chunk: Int12, column: ColumnRelWorld): ChunkRelWorld =
117
    ChunkRelWorld(column.value << 12L | chunk.repr.toInt)
118

1✔
119
  val neighborOffsets: Seq[Offset] = Seq(
120
    Offset(0, 1, 0),
121
    Offset(1, 0, 0),
122
    Offset(0, 0, 1),
123
    Offset(-1, 0, 1),
124
    Offset(0, -1, 0),
125
    Offset(-1, 0, 0),
126
    Offset(0, 0, -1),
127
    Offset(1, 0, -1)
1✔
128
  )
129
}
×
130

131
case class ChunkRelWorld(value: Long) extends AnyVal { // XXXXXZZZZZYYY
1✔
132
  def getColumnRelWorld: ColumnRelWorld = ColumnRelWorld(value >>> 12)
133

1✔
134
  def X: Int20 = Int20.truncate(value >> 32)
1✔
135
  def Z: Int20 = Int20.truncate(value >> 12)
1✔
136
  def Y: Int12 = Int12.truncate(value)
137

1✔
138
  def neighbors(using CylinderSize): Seq[ChunkRelWorld] =
1✔
139
    ChunkRelWorld.neighborOffsets.map(offset)
140

1✔
141
  def extendedNeighbors(radius: Int)(using CylinderSize): Seq[ChunkRelWorld] =
1✔
142
    for
×
143
      y <- -radius to radius
1✔
UNCOV
144
      z <- -radius to radius
×
145
      x <- -radius to radius
1✔
146
    yield offset(x, y, z)
147

1✔
148
  def offset(t: Offset)(using CylinderSize): ChunkRelWorld = offset(t.dx, t.dy, t.dz)
1✔
149
  def offset(dx: Int, dy: Int, dz: Int)(using CylinderSize): ChunkRelWorld =
1✔
150
    ChunkRelWorld(X.toInt + dx, Y.toInt + dy, Z.toInt + dz)
151

×
152
  override def toString: String = s"(${X.toInt}, ${Y.toInt}, ${Z.toInt})"
153
}
154

155
object ColumnRelWorld {
1✔
156
  def apply(X: Long, Z: Int)(using cylSize: CylinderSize): ColumnRelWorld =
1✔
157
    ColumnRelWorld((X & 0xfffff) << 20 | (Z & cylSize.ringSizeMask))
158
}
159

160
case class ColumnRelWorld(value: Long) extends AnyVal { // XXXXXZZZZZ
1✔
161
  def X: Int20 = Int20.truncate(value >> 20)
1✔
162
  def Z: Int20 = Int20.truncate(value)
163

×
164
  def offset(x: Int, z: Int)(using CylinderSize): ColumnRelWorld = ColumnRelWorld(X.toInt + x, Z.toInt + z)
165

×
166
  def distSq(origin: Vector2d)(using cylSize: CylinderSize): Double =
×
167
    val dx = this.X.toInt - origin.x + 0.5
×
168
    val dz1 = math.abs(this.Z.toInt - origin.y + 0.5 + dx * 0.5f)
×
169
    val dz2 = math.abs(dz1 - cylSize.ringSize)
×
170
    val dz = math.min(dz1, dz2)
171
    dx * dx + dz * dz
172
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc