package ru.dbotthepony.kstarbound.defs.player import com.google.common.collect.ImmutableSet import ru.dbotthepony.kommons.collect.immutableSet import ru.dbotthepony.kommons.io.readBinaryString import ru.dbotthepony.kommons.io.readCollection import ru.dbotthepony.kommons.io.writeBinaryString import ru.dbotthepony.kommons.io.writeCollection import ru.dbotthepony.kstarbound.json.builder.JsonFactory import java.io.DataInputStream import java.io.DataOutputStream @JsonFactory data class ShipUpgrades( val shipLevel: Int = 0, val maxFuel: Int = 0, val crewSize: Int = 0, val fuelEfficiency: Double = 1.0, val shipSpeed: Int = 0, val capabilities: ImmutableSet = ImmutableSet.of() ) { constructor(stream: DataInputStream, isLegacy: Boolean) : this( stream.readInt(), stream.readInt(), stream.readInt(), if (isLegacy) stream.readFloat().toDouble() else stream.readDouble(), stream.readInt(), ImmutableSet.copyOf(stream.readCollection { readBinaryString() }) ) fun apply(upgrades: ShipUpgrades): ShipUpgrades { return ShipUpgrades( shipLevel = shipLevel.coerceAtLeast(upgrades.shipLevel), maxFuel = maxFuel.coerceAtLeast(upgrades.maxFuel), crewSize = crewSize.coerceAtLeast(upgrades.crewSize), fuelEfficiency = fuelEfficiency.coerceAtLeast(upgrades.fuelEfficiency), shipSpeed = shipSpeed.coerceAtLeast(upgrades.shipSpeed), capabilities = immutableSet { capabilities.forEach(::accept); upgrades.capabilities.forEach(::accept) } ) } fun write(stream: DataOutputStream, isLegacy: Boolean) { stream.writeInt(shipLevel) stream.writeInt(maxFuel) stream.writeInt(crewSize) if (isLegacy) stream.writeFloat(fuelEfficiency.toFloat()) else stream.writeDouble(fuelEfficiency) stream.writeInt(shipSpeed) stream.writeCollection(capabilities) { writeBinaryString(it) } } }