diff --git a/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/common/entities.kt b/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/common/entities.kt index 86206aa..e077e4a 100644 --- a/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/common/entities.kt +++ b/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/common/entities.kt @@ -34,6 +34,8 @@ sealed interface FactorioEntityData { val direction: UByte? = null, /** @see FactorioEntityUpdateElement.Standard.recipe */ val recipe: String? = null, + /** @see FactorioEntityUpdateElement.Standard.beltToGroundType */ + val beltToGroundType: String? = null, ) : FactorioEntityData @@ -281,6 +283,8 @@ sealed interface FactorioEntityUpdateElement { val direction: UByte? = null, /** The name of the recipe that a crafting machine is set to (or last used, for furnaces) */ val recipe: String? = null, + /** Whether an underground belt is an entrance (`input`) or an exit (`output`) */ + val beltToGroundType: String? = null, ) : FactorioEntityUpdateElement } diff --git a/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/common/mapBuildings.kt b/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/common/mapBuildings.kt index d67370a..7129c0e 100644 --- a/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/common/mapBuildings.kt +++ b/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/common/mapBuildings.kt @@ -18,4 +18,6 @@ data class MapBuilding( val direction: UByte? = null, /** The name of the recipe of a crafting machine */ val recipe: String? = null, + /** Whether an underground belt is an entrance (`input`) or an exit (`output`) */ + val beltToGroundType: String? = null, ) diff --git a/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/packets/kafkatorioPacketKeyed.kt b/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/packets/kafkatorioPacketKeyed.kt index 7921ba0..d78e8b7 100644 --- a/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/packets/kafkatorioPacketKeyed.kt +++ b/modules/events-library/src/commonMain/kotlin/dev/adamko/kafkatorio/schema/packets/kafkatorioPacketKeyed.kt @@ -177,6 +177,7 @@ data class MapChunkEntityUpdate( lastUser = entity.lastUser, direction = entity.direction, recipe = entity.recipe, + beltToGroundType = entity.beltToGroundType, ) } } diff --git a/modules/events-mod/src/main/typescript/generated/kafkatorio-schema.ts b/modules/events-mod/src/main/typescript/generated/kafkatorio-schema.ts index c504e84..03be14a 100644 --- a/modules/events-mod/src/main/typescript/generated/kafkatorio-schema.ts +++ b/modules/events-mod/src/main/typescript/generated/kafkatorio-schema.ts @@ -308,6 +308,7 @@ export interface FactorioEntityUpdateElementStandard { lastUser?: UInt | null; direction?: UByte | null; recipe?: string | null; + beltToGroundType?: string | null; } export type MinedProduct = diff --git a/modules/events-mod/src/main/typescript/main/events/handlers/EntityUpdateHandler.ts b/modules/events-mod/src/main/typescript/main/events/handlers/EntityUpdateHandler.ts index 91a38b2..eabf9e2 100644 --- a/modules/events-mod/src/main/typescript/main/events/handlers/EntityUpdateHandler.ts +++ b/modules/events-mod/src/main/typescript/main/events/handlers/EntityUpdateHandler.ts @@ -140,6 +140,7 @@ export class EntityUpdatesHandler { status: Converters.entityStatus(entity.status), direction: entity.supports_direction ? entity.direction : undefined, recipe: EntityUpdatesHandler.recipeName(entity), + beltToGroundType: entity.type == "underground-belt" ? entity.belt_to_ground_type : undefined, } EventDataCache.throttle( diff --git a/modules/events-server-web/src/main/kotlin/dev/adamko/kafkatorio/server/web/buildings/BuildingService.kt b/modules/events-server-web/src/main/kotlin/dev/adamko/kafkatorio/server/web/buildings/BuildingService.kt index ff0e584..87c2cde 100644 --- a/modules/events-server-web/src/main/kotlin/dev/adamko/kafkatorio/server/web/buildings/BuildingService.kt +++ b/modules/events-server-web/src/main/kotlin/dev/adamko/kafkatorio/server/web/buildings/BuildingService.kt @@ -71,6 +71,7 @@ class BuildingService( unitNumber = entity.unitNumber, direction = entity.direction, recipe = entity.recipe, + beltToGroundType = entity.beltToGroundType, ) } } diff --git a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/BuildingIconsLayer.kt b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/BuildingIconsLayer.kt index b899a84..b759134 100644 --- a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/BuildingIconsLayer.kt +++ b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/BuildingIconsLayer.kt @@ -21,6 +21,9 @@ import kotlinx.coroutines.Deferred import kotlinx.coroutines.async import kotlinx.coroutines.launch import org.w3c.dom.CanvasRenderingContext2D +import org.w3c.dom.CanvasLineCap +import org.w3c.dom.CanvasLineJoin +import org.w3c.dom.ROUND import org.w3c.dom.HTMLCanvasElement import org.w3c.dom.Image @@ -61,6 +64,9 @@ class BuildingIconsLayer( private val icons = mutableMapOf>() + /** The colour of each belt tier, by transport belt name. See [beltColour]. */ + private val beltColours = mutableMapOf() + /** The surface that's shown. Nauvis is surface 1. */ private var surfaceIndex: Int = 1 @@ -178,7 +184,9 @@ class BuildingIconsLayer( // load the icons before drawing, so the canvas is updated in one go val buildingIcons = buildings.associateWith { building -> - building.protoId.name.toString().takeIf { it in info }?.let { icon("entity", it) } + // belts are coloured like the transport belt of their tier, so use that icon + val iconName = if (building.isBelt()) building.transportBeltName() else building.protoId.name.toString() + iconName.takeIf { it in info }?.let { icon("entity", it) } }.mapValues { (_, icon) -> icon?.await() } // like Factorio's 'alt mode', show the recipe on crafting machines, if it's big enough to see @@ -202,8 +210,16 @@ class BuildingIconsLayer( ctx.fillRect(centreX - w * scale / 2, centreY - h * scale / 2, w * scale, h * scale) } - val size = building.iconSize(info) * scale val icon = buildingIcons[building] + + // belts are drawn like the in-game map, so the direction they're going is visible + if (building.isBelt()) { + val colour = icon?.let { beltColours.getOrPut(building.transportBeltName()) { beltColour(it) } } + ctx.drawBelt(building, info, centreX, centreY, scale, colour ?: DEFAULT_BELT_COLOUR) + return@forEach + } + + val size = building.iconSize(info) * scale if (icon != null) { ctx.drawImage(icon, centreX - size / 2, centreY - size / 2, size, size) } else { @@ -238,6 +254,41 @@ class BuildingIconsLayer( } + /** + * The colour of a belt tier, e.g. yellow, red or blue: the average of the colourful pixels of + * its icon. + */ + private fun beltColour(icon: Image): String { + val size = 16 + val canvas = document.createElement("canvas") as HTMLCanvasElement + canvas.width = size + canvas.height = size + val ctx = canvas.getContext("2d") as CanvasRenderingContext2D + ctx.drawImage(icon, 0.0, 0.0, size.toDouble(), size.toDouble()) + val pixels = runCatching { ctx.getImageData(0.0, 0.0, size.toDouble(), size.toDouble()).data } + .getOrNull() ?: return DEFAULT_BELT_COLOUR + + var r = 0 + var g = 0 + var b = 0 + var count = 0 + for (i in 0 until size * size) { + val pr = pixels.asDynamic()[i * 4] as Int + val pg = pixels.asDynamic()[i * 4 + 1] as Int + val pb = pixels.asDynamic()[i * 4 + 2] as Int + val alpha = pixels.asDynamic()[i * 4 + 3] as Int + val saturation = maxOf(pr, pg, pb) - minOf(pr, pg, pb) + if (alpha > 200 && saturation > 60) { + r += pr + g += pg + b += pb + count++ + } + } + return if (count == 0) DEFAULT_BELT_COLOUR else "rgb(${r / count}, ${g / count}, ${b / count})" + } + + private fun updateHoverIndex( left: Double, top: Double, @@ -274,6 +325,9 @@ class BuildingIconsLayer( /** The size of the [hoverIndex] cells, in Factorio tiles */ private const val CELL_SIZE = 16.0 + /** For belts without an icon */ + private const val DEFAULT_BELT_COLOUR = "rgb(200, 170, 60)" + private fun cellKey(x: Double, y: Double): String = "${floor(x / CELL_SIZE).toInt()},${floor(y / CELL_SIZE).toInt()}" } @@ -299,6 +353,95 @@ private const val DIRECTION_EAST = 4 private const val DIRECTION_WEST = 12 +private const val DIRECTION_COUNT = 16 + +private val BELT_TYPES = setOf("transport-belt", "underground-belt", "splitter") + +private fun MapBuilding.isBelt(): Boolean = protoId.type.toString() in BELT_TYPES + +/** + * The transport belt of the same tier, e.g. `fast-splitter` -> `fast-transport-belt`. + * (This naming holds for the base game, Krastorio 2 and Space Exploration.) + */ +private fun MapBuilding.transportBeltName(): String = + protoId.name.toString() + .replace("underground-belt", "transport-belt") + .replace("splitter", "transport-belt") + + +/** + * Draw a belt, underground belt or splitter: its footprint, with an arrow per lane in the + * direction the items go. + * + * Underground belt entrances are dark on the far side (where the items go underground), and exits + * on the near side. + */ +private fun CanvasRenderingContext2D.drawBelt( + building: MapBuilding, + info: Map, + centreX: Double, + centreY: Double, + scale: Double, + colour: String, +) { + // the size facing north - the canvas is rotated to match the building + val buildingInfo = info[building.protoId.name.toString()] + val w = (buildingInfo?.w ?: 1) * scale + val h = (buildingInfo?.h ?: 1) * scale + + save() + translate(centreX, centreY) + rotate((building.direction?.toInt() ?: 0) * 2 * PI / DIRECTION_COUNT) + + fillStyle = "rgba(30, 30, 30, 0.85)" + fillRect(-w / 2, -h / 2, w, h) + + // at low zoom levels a belt is a few pixels, so just show the colour + if (scale < 8) { + fillStyle = colour + fillRect(-w / 2 + scale * 0.2, -h / 2 + scale * 0.2, w - scale * 0.4, h - scale * 0.4) + restore() + return + } + + when (building.beltToGroundType) { + // items go underground at the far (north) side + "input" -> { + fillStyle = colour + fillRect(-w / 2, -h / 2, w, h * 0.3) + } + // ... and come out at the near (south) side + "output" -> { + fillStyle = colour + fillRect(-w / 2, h / 2 - h * 0.3, w, h * 0.3) + } + } + + // a chevron per lane (a splitter has two) + strokeStyle = colour + lineWidth = scale * 0.15 + lineCap = CanvasLineCap.ROUND + lineJoin = CanvasLineJoin.ROUND + val lanes = max(1, (w / scale).toInt()) + for (lane in 0 until lanes) { + val laneX = -w / 2 + (lane + 0.5) * scale + beginPath() + moveTo(laneX - scale * 0.25, scale * 0.15) + lineTo(laneX, -scale * 0.12) + lineTo(laneX + scale * 0.25, scale * 0.15) + stroke() + } + + // the splitter box, across the lanes + if (lanes > 1) { + fillStyle = colour + fillRect(-w / 2 + scale * 0.1, h / 2 - scale * 0.22, w - scale * 0.2, scale * 0.12) + } + + restore() +} + + private fun MapBuilding.tooltip( info: Map, recipeNames: Map, @@ -306,6 +449,10 @@ private fun MapBuilding.tooltip( val name = protoId.name.toString() append("${info[name]?.name ?: name}") recipe?.let { append("
Recipe: ${recipeNames[it] ?: it}") } + when (beltToGroundType) { + "input" -> append("
Entrance") + "output" -> append("
Exit") + } status?.let { append("
${it.name.lowercase().replace('_', ' ').replaceFirstChar(Char::uppercase)}") } health?.let { append("
Health: ${it.toInt()}") } append("
[${floor(position.x).toInt()}, ${floor(position.y).toInt()}]")