fix 'prototype key' in MapTileDictionary

This commit is contained in:
Adam 2022-04-20 23:15:20 +02:00
parent 648cb31de8
commit bb54f6666b
2 changed files with 13 additions and 20 deletions

View file

@ -3,8 +3,7 @@ import {Converters} from "./converters";
import EventUpdatesManager, {EventUpdates} from "../cache/EventDataCache"; import EventUpdatesManager, {EventUpdates} from "../cache/EventDataCache";
import { import {
KafkatorioPacketData, KafkatorioPacketData,
MapChunkPosition, MapChunkPosition
MapTileDictionary
} from "../../generated/kafkatorio-schema/kafkatorio-schema"; } from "../../generated/kafkatorio-schema/kafkatorio-schema";
import CacheKey = EventUpdates.PacketKey; import CacheKey = EventUpdates.PacketKey;
import Type = KafkatorioPacketData.Type; import Type = KafkatorioPacketData.Type;
@ -13,18 +12,6 @@ import Type = KafkatorioPacketData.Type;
type MapChunkUpdater = (data: KafkatorioPacketData.MapChunkUpdate) => void type MapChunkUpdater = (data: KafkatorioPacketData.MapChunkUpdate) => void
const mapProtoNameToKey: { [key: string]: uint } = {}
let protoIndex: uint = 0
function getProtoKey(protoName: string): uint {
if (mapProtoNameToKey[protoName] == undefined) {
mapProtoNameToKey[protoName] = protoIndex++
}
return mapProtoNameToKey[protoName]
}
function mapTilesUpdateDebounce( function mapTilesUpdateDebounce(
surface: LuaSurface | undefined, surface: LuaSurface | undefined,
chunkPosition: MapChunkPosition, chunkPosition: MapChunkPosition,
@ -47,27 +34,31 @@ function mapTilesUpdateDebounce(
key, key,
Type.MapChunkUpdate, Type.MapChunkUpdate,
data => { data => {
data.tileDictionary ??= <MapTileDictionary>{
// tile dictionary update
data.tileDictionary ??= {
tilesXY: {}, tilesXY: {},
protos: {}, protos: {},
} }
let protosCount: uint = 0
for (const tile of tiles) { for (const tile of tiles) {
data.tileDictionary.protos[tile.name] ??= protosCount++
const protoKey = data.tileDictionary.protos[tile.name]
const xString = `${tile.position.x}` const xString = `${tile.position.x}`
const yString = `${tile.position.y}` const yString = `${tile.position.y}`
const protoKey = getProtoKey(tile.name)
data.tileDictionary.protos[tile.name] = protoIndex
data.tileDictionary.tilesXY[xString] ??= {} data.tileDictionary.tilesXY[xString] ??= {}
data.tileDictionary.tilesXY[xString][yString] = protoKey data.tileDictionary.tilesXY[xString][yString] = protoKey
} }
// events count update
data.eventCounts ??= {} data.eventCounts ??= {}
data.eventCounts[eventName] ??= 0 data.eventCounts[eventName] ??= 0
data.eventCounts[eventName]++ data.eventCounts[eventName]++
// apply mutator
if (updater != undefined) { if (updater != undefined) {
updater(data) updater(data)
} }

View file

@ -50,15 +50,17 @@ data class MapTileDictionary(
val protoName = protosIndexToName[protoIndex] val protoName = protosIndexToName[protoIndex]
if (x != null && y != null && protoName != null) { if (x != null && y != null && protoName != null) {
add(MapTile(x, y, protoName)) add(MapTile(x, y, protoName))
} else if (protoName == null) {
println("warning: no prototype name found for index:$protoIndex. $protosIndexToName")
} }
} }
} }
} }
/** The collection-index of a prototype in [MapTileDictionary.protos] */ /** An arbitrary ID for a prototype in [MapTileDictionary.protos]. */
@Serializable @Serializable
@JvmInline @JvmInline
value class PrototypeKey(val index: Int) value class PrototypeKey(private val index: Int)
companion object { companion object {
fun MapTileDictionary?.isNullOrEmpty(): Boolean { fun MapTileDictionary?.isNullOrEmpty(): Boolean {