Skip to content

Commit

Permalink
feat(sdk): hex angle utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
xeruf committed Jun 5, 2024
1 parent 3caa1b2 commit ff9fc40
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 5 additions & 2 deletions plugin/src/main/kotlin/sc/plugin2024/GameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ data class GameState @JvmOverloads constructor(
): TwoPlayerGameState<Move>(currentTeam) {

val currentShip: Ship
get() = ships[currentTeam.index]
get() = getShip(currentTeam)

val otherShip: Ship
get() = ships[currentTeam.opponent().index]
get() = getShip(currentTeam.opponent())

fun getShip(team: Team) =
ships[team.index]

/**
* Determine the team that should go first at the beginning of the round.
Expand Down
10 changes: 8 additions & 2 deletions sdk/src/main/server-api/sc/api/plugins/CubeCoordinates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ data class CubeCoordinates
override fun equals(other: Any?): Boolean =
other is CubeCoordinates && q == other.q && r == other.r && s == other.s

override fun hashCode(): Int = q * 9999 + r
override fun hashCode(): Int = q * 998 + r

companion object {
/** Der Ursprung des Koordinatensystems (0, 0). */
Expand All @@ -98,6 +98,8 @@ enum class CubeDirection(val vector: CubeCoordinates) {
val angle
get() = ordinal * 60

fun byIndex(index: Int) = values().let { it[index.mod(it.size)] }

/** @return an array of this and the two neighboring directions. */
fun withNeighbors() = arrayOf(rotatedBy(-1), this, rotatedBy(1))

Expand All @@ -110,8 +112,12 @@ enum class CubeDirection(val vector: CubeCoordinates) {
return if(diff > 3) diff - values().size else diff
}

fun angleTo(target: CubeDirection) = turnCountTo(target) * 60

/** @return the direction rotated clockwise [turns] times. */
fun rotatedBy(turns: Int) = values().let { it[(ordinal + turns).mod(it.size)] }
fun rotatedBy(turns: Int) = byIndex(ordinal + turns)

operator fun minus(other: CubeDirection) = byIndex(ordinal - other.ordinal)

operator fun minus(count: Int) = rotatedBy(-count)
operator fun plus(count: Int) = rotatedBy(count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class TwoPlayerGameState<M: IMove>(
/** Calculates the [Team] of the current player from [turn] and [startTeam].
* Based on the assumption that the current player switches every turn. */
protected fun currentTeamFromTurn(): Team =
if(turn.rem(2) == 0) startTeam else startTeam.opponent()
if(turn.mod(2) == 0) startTeam else startTeam.opponent()

override fun toString() =
"GameState(turn=$turn,currentTeam=$currentTeam)"
Expand Down

0 comments on commit ff9fc40

Please sign in to comment.