packet size improvements
- change structure resource packets - make more booleans default to false - use encode_string - massive decrease in packet size!!
This commit is contained in:
parent
523f6d5914
commit
085776d42b
14 changed files with 131 additions and 134 deletions
|
|
@ -18,41 +18,23 @@ sealed interface FactorioEntityData {
|
|||
|
||||
/** Non-specific entity data */
|
||||
@Serializable
|
||||
@SerialName("kafkatorio.entity.FactorioEntityData")
|
||||
@SerialName("kafkatorio.entity.FactorioEntityDataStandard")
|
||||
data class Standard(
|
||||
override val protoId: PrototypeId,
|
||||
val status: Status? = null,
|
||||
|
||||
val unitNumber: UnitNumber? = null,
|
||||
|
||||
override val position: MapEntityPosition,
|
||||
|
||||
val graphicsVariation: UByte? = null,
|
||||
val health: Float? = null,
|
||||
val isActive: Boolean? = null,
|
||||
val isRotatable: Boolean? = null,
|
||||
val isActive: Boolean = false,
|
||||
val isRotatable: Boolean = false,
|
||||
val lastUser: UInt? = null,
|
||||
) : FactorioEntityData {
|
||||
constructor(
|
||||
position: MapEntityPosition,
|
||||
protoId: PrototypeId,
|
||||
element: FactorioEntityUpdateElement.Entity
|
||||
) : this(
|
||||
protoId = protoId,
|
||||
position = position,
|
||||
|
||||
status = element.status,
|
||||
unitNumber = element.unitNumber,
|
||||
graphicsVariation = element.graphicsVariation,
|
||||
health = element.health,
|
||||
isActive = element.isActive,
|
||||
isRotatable = element.isRotatable,
|
||||
lastUser = element.lastUser
|
||||
)
|
||||
}
|
||||
val status: Status? = null,
|
||||
val unitNumber: UnitNumber? = null,
|
||||
) : FactorioEntityData
|
||||
|
||||
|
||||
@Serializable
|
||||
@SerialName("kafkatorio.entity.FactorioResourceData")
|
||||
@SerialName("kafkatorio.entity.FactorioEntityDataResource")
|
||||
data class Resource(
|
||||
override val protoId: PrototypeId,
|
||||
|
||||
|
|
@ -60,19 +42,7 @@ sealed interface FactorioEntityData {
|
|||
val amount: UInt,
|
||||
/** Count of initial resource units contained. `null` if resource is not infinite. */
|
||||
val initialAmount: UInt? = null,
|
||||
) : FactorioEntityData {
|
||||
constructor(
|
||||
position: MapEntityPosition,
|
||||
protoId: PrototypeId,
|
||||
element: FactorioEntityUpdateElement.Resource
|
||||
) : this(
|
||||
protoId = protoId,
|
||||
position = position,
|
||||
|
||||
amount = element.amount,
|
||||
initialAmount = element.initialAmount,
|
||||
)
|
||||
}
|
||||
) : FactorioEntityData
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -193,7 +163,7 @@ sealed interface FactorioEntityData {
|
|||
}
|
||||
|
||||
|
||||
sealed interface FactorioEntityUpdateDictionary<T : FactorioEntityUpdateElement>
|
||||
sealed interface FactorioEntityUpdateDictionary<T : Any>
|
||||
: Iterable<Pair<MapEntityPosition, T>> {
|
||||
|
||||
val entitiesXY: Map<String, Map<String, T>>
|
||||
|
|
@ -203,28 +173,37 @@ sealed interface FactorioEntityUpdateDictionary<T : FactorioEntityUpdateElement>
|
|||
|
||||
override fun iterator(): Iterator<Pair<MapEntityPosition, T>> =
|
||||
iterator {
|
||||
entitiesXY.forEach { (x, column) ->
|
||||
column.forEach { (y, entity) ->
|
||||
yield(
|
||||
MapEntityPosition(x.toDouble(), y.toDouble()) to entity
|
||||
)
|
||||
val invalid = mutableListOf<String>()
|
||||
entitiesXY.forEach { (xString, column) ->
|
||||
val x = xString.toDoubleOrNull()
|
||||
column.forEach { (yString, entity) ->
|
||||
val y = yString.toDoubleOrNull()
|
||||
if (x == null || y == null) {
|
||||
invalid += "$xString/$yString"
|
||||
} else {
|
||||
yield(MapEntityPosition(x, y) to entity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (invalid.isNotEmpty()) {
|
||||
println("WARNING FactorioEntityUpdateDictionary contained invalid x/y coords: ${invalid.joinToString()}")
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
@SerialName("kafkatorio.entity.FactorioEntityUpdateResourceDictionary")
|
||||
@SerialName("kafkatorio.entity.FactorioEntityUpdateResourceAmountDictionary")
|
||||
@JvmInline
|
||||
value class Resource(
|
||||
override val entitiesXY: Map<String, Map<String, FactorioEntityUpdateElement.Resource>>
|
||||
) : FactorioEntityUpdateDictionary<FactorioEntityUpdateElement.Resource>
|
||||
value class ResourceAmounts(
|
||||
override val entitiesXY: Map<String, Map<String, UInt>> = emptyMap()
|
||||
) : FactorioEntityUpdateDictionary<UInt>
|
||||
|
||||
@Serializable
|
||||
@SerialName("kafkatorio.entity.FactorioEntityUpdateEntityDictionary")
|
||||
@JvmInline
|
||||
value class Entity(
|
||||
override val entitiesXY: Map<String, Map<String, FactorioEntityUpdateElement.Entity>>
|
||||
) : FactorioEntityUpdateDictionary<FactorioEntityUpdateElement.Entity>
|
||||
override val entitiesXY: Map<String, Map<String, FactorioEntityUpdateElement.Standard>>
|
||||
) : FactorioEntityUpdateDictionary<FactorioEntityUpdateElement.Standard>
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -232,24 +211,16 @@ sealed interface FactorioEntityUpdateDictionary<T : FactorioEntityUpdateElement>
|
|||
sealed interface FactorioEntityUpdateElement {
|
||||
|
||||
@Serializable
|
||||
@SerialName("kafkatorio.entity.FactorioEntityUpdateResource")
|
||||
data class Resource(
|
||||
val amount: UInt,
|
||||
/** Count of initial resource units contained. `null` if resource is not infinite. */
|
||||
val initialAmount: UInt? = null,
|
||||
) : FactorioEntityUpdateElement
|
||||
|
||||
@Serializable
|
||||
@SerialName("kafkatorio.entity.FactorioEntityUpdateEntity")
|
||||
data class Entity(
|
||||
@SerialName("kafkatorio.entity.FactorioEntityUpdateElementStandard")
|
||||
data class Standard(
|
||||
val status: FactorioEntityData.Status? = null,
|
||||
|
||||
val unitNumber: UnitNumber? = null,
|
||||
|
||||
val graphicsVariation: UByte? = null,
|
||||
val health: Float? = null,
|
||||
val isActive: Boolean? = null,
|
||||
val isRotatable: Boolean? = null,
|
||||
val isActive: Boolean = false,
|
||||
val isRotatable: Boolean = false,
|
||||
val lastUser: UInt? = null,
|
||||
) : FactorioEntityUpdateElement
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ sealed interface FactorioPrototype {
|
|||
val collisionMasks: LuaJsonList<String>,
|
||||
val order: String,
|
||||
/** Can the tile be mined for resources? */
|
||||
val canBeMined: Boolean,
|
||||
val canBeMined: Boolean = false,
|
||||
) : FactorioPrototype
|
||||
|
||||
|
||||
|
|
@ -63,7 +63,7 @@ sealed interface FactorioPrototype {
|
|||
@Serializable
|
||||
@SerialName("kafkatorio.prototype.EntityMiningProperties")
|
||||
data class MiningProperties(
|
||||
val canBeMined: Boolean,
|
||||
val canBeMined: Boolean = false,
|
||||
// @Contextual
|
||||
val products: LuaJsonList<MinedProduct>?,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -23,8 +23,8 @@ data class ConfigurationUpdate(
|
|||
val factorioData: ConfigurationUpdateGameData,
|
||||
// @Contextual
|
||||
val allMods: LuaJsonList<ConfigurationUpdateModData>,
|
||||
val modStartupSettingsChange: Boolean,
|
||||
val migrationApplied: Boolean,
|
||||
val modStartupSettingsChange: Boolean = false,
|
||||
val migrationApplied: Boolean = false,
|
||||
) : KafkatorioInstantPacketData() {
|
||||
|
||||
@Serializable
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import dev.adamko.kafkatorio.schema.common.EntityIdentifiersData
|
|||
import dev.adamko.kafkatorio.schema.common.EventName
|
||||
import dev.adamko.kafkatorio.schema.common.FactorioEntityData
|
||||
import dev.adamko.kafkatorio.schema.common.FactorioEntityUpdateDictionary
|
||||
import dev.adamko.kafkatorio.schema.common.FactorioEntityUpdateDictionary.ResourceAmounts
|
||||
import dev.adamko.kafkatorio.schema.common.ForceIndex
|
||||
import dev.adamko.kafkatorio.schema.common.MapChunkPosition
|
||||
import dev.adamko.kafkatorio.schema.common.MapEntityPosition
|
||||
|
|
@ -135,7 +136,7 @@ data class PlayerUpdate(
|
|||
val kickedReason: String? = null,
|
||||
val disconnectReason: String? = null,
|
||||
/** `true` when a player is removed (deleted) from the game */
|
||||
val isRemoved: Boolean? = null,
|
||||
val isRemoved: Boolean = false,
|
||||
) : KafkatorioKeyedPacketData()
|
||||
|
||||
|
||||
|
|
@ -166,7 +167,14 @@ data class MapChunkEntityUpdate(
|
|||
FactorioEntityData.Standard(
|
||||
protoId = key.protoId,
|
||||
position = position,
|
||||
element = entity,
|
||||
|
||||
status = entity.status,
|
||||
unitNumber = entity.unitNumber,
|
||||
graphicsVariation = entity.graphicsVariation,
|
||||
health = entity.health,
|
||||
isActive = entity.isActive,
|
||||
isRotatable = entity.isRotatable,
|
||||
lastUser = entity.lastUser
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -191,15 +199,20 @@ data class MapChunkResourceUpdate(
|
|||
|
||||
override val events: Map<EventName, LuaJsonList<Tick>>? = null,
|
||||
|
||||
val resourcesXY: FactorioEntityUpdateDictionary.Resource,
|
||||
// Optimise JSON size by storing amounts separately.
|
||||
val amounts: ResourceAmounts = ResourceAmounts(),
|
||||
// Initial amount is only available for infinite resources, and infinite resources (oil) are more
|
||||
// uncommon, so there are unlikely to be many per chunk
|
||||
val initialAmounts: ResourceAmounts = ResourceAmounts(),
|
||||
) : KafkatorioKeyedPacketData() {
|
||||
|
||||
fun resources(): List<FactorioEntityData.Resource> =
|
||||
resourcesXY.map { (position, resource) ->
|
||||
amounts.map { (position, amount) ->
|
||||
FactorioEntityData.Resource(
|
||||
protoId = key.protoId,
|
||||
position = position,
|
||||
element = resource,
|
||||
amount = amount,
|
||||
initialAmount = initialAmounts[position],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ class MySourceCodeGenerator(
|
|||
(element.typeRef as? TsTypeRef.Literal)?.element is TsLiteral.Custom
|
||||
|| element.id.name == "Tick"
|
||||
|| element.id.name == "FactorioEntityUpdateEntityDictionary"
|
||||
|| element.id.name == "FactorioEntityUpdateResourceDictionary"
|
||||
|| element.id.name == "FactorioEntityUpdateResourceAmountDictionary"
|
||||
) {
|
||||
val aliasedRef = generateTypeReference(element.typeRef)
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue