try making map tiles better, listen to events for changed tiles
This commit is contained in:
parent
4e8a0c4f69
commit
4d2926ebe2
6 changed files with 97 additions and 22 deletions
|
|
@ -1,6 +1,6 @@
|
|||
// Generated by TypeScriptGenerator - do not edit this file manually
|
||||
|
||||
type ObjectName = "LuaPlayer" | "LuaEntity" | "LuaSurface" | "MapChunk" | "LuaTile" | "ConsoleChatMessage";
|
||||
type ObjectName = "LuaPlayer" | "LuaEntity" | "LuaSurface" | "MapChunk" | "LuaTile" | "LuaTiles" | "ConsoleChatMessage";
|
||||
|
||||
interface FactorioObjectData {
|
||||
objectName: ObjectName;
|
||||
|
|
@ -41,13 +41,18 @@ interface MapTile extends FactorioObjectData {
|
|||
objectName: ObjectName;
|
||||
position: MapTilePosition;
|
||||
prototypeName: string;
|
||||
}
|
||||
|
||||
interface MapTiles extends FactorioObjectData {
|
||||
objectName: ObjectName;
|
||||
surfaceIndex: int;
|
||||
tiles: MapTile[];
|
||||
}
|
||||
|
||||
interface MapChunk extends FactorioObjectData {
|
||||
objectName: ObjectName;
|
||||
position: MapChunkPosition;
|
||||
tiles: MapTile[];
|
||||
tiles: MapTiles;
|
||||
}
|
||||
|
||||
interface ConsoleChatMessage extends FactorioObjectData {
|
||||
|
|
|
|||
|
|
@ -71,7 +71,6 @@ export namespace Converters {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
export function mapChunkPosition(mapPos: ChunkPositionTable): MapChunkPosition {
|
||||
return {
|
||||
x: mapPos.x,
|
||||
|
|
@ -103,8 +102,16 @@ export namespace Converters {
|
|||
|
||||
position: mapTilePosition(tile.position),
|
||||
prototypeName: tile.prototype.name,
|
||||
surfaceIndex: tile.surface.index
|
||||
}
|
||||
}
|
||||
|
||||
// export function convertMapTilePositionToMapChunkPosition(
|
||||
// tilePosition: TilePositionTable
|
||||
// ): ChunkPositionTable {
|
||||
// return {
|
||||
// x: tilePosition.x / 32,
|
||||
// y: tilePosition.y / 32,
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,23 +50,25 @@ export function handleConsoleChat(
|
|||
emitEvent(table, tick, eventType)
|
||||
}
|
||||
|
||||
const CHUNK_SIZE = 32
|
||||
// const CHUNK_SIZE = 32
|
||||
|
||||
export function handleChunkUpdate(
|
||||
tick: uint,
|
||||
eventType: string,
|
||||
surfaceIndex: uint,
|
||||
position: ChunkPositionTable
|
||||
position: ChunkPositionTable,
|
||||
area: BoundingBoxTable,
|
||||
) {
|
||||
|
||||
let surface = game.get_surface(surfaceIndex)
|
||||
|
||||
let x = (position as ChunkPositionTable).x * CHUNK_SIZE
|
||||
let y = (position as ChunkPositionTable).y * CHUNK_SIZE
|
||||
// let x = (position as ChunkPositionTable).x * CHUNK_SIZE
|
||||
// let y = (position as ChunkPositionTable).y * CHUNK_SIZE
|
||||
|
||||
let tiles: LuaTile[] = surface?.find_tiles_filtered({
|
||||
position: {x: x, y: y},
|
||||
radius: CHUNK_SIZE,
|
||||
area: area,
|
||||
// position: {x: x, y: y},
|
||||
// radius: CHUNK_SIZE,
|
||||
collision_mask: ["ground-tile", "water-tile"]
|
||||
}
|
||||
) ?? []
|
||||
|
|
@ -77,9 +79,47 @@ export function handleChunkUpdate(
|
|||
|
||||
let mapChunk: MapChunk = {
|
||||
objectName: "MapChunk",
|
||||
tiles: convertedTiles,
|
||||
tiles: createMapTiles(surfaceIndex, convertedTiles),
|
||||
position: mapChunkPosition(position)
|
||||
}
|
||||
|
||||
emitEvent(mapChunk, tick, eventType)
|
||||
}
|
||||
|
||||
export function handleTilesUpdate(
|
||||
tick: uint,
|
||||
eventType: string,
|
||||
surfaceIndex: uint,
|
||||
newTileType: LuaTilePrototype,
|
||||
updatedTiles: OldTileAndPosition[],
|
||||
) {
|
||||
|
||||
if (!newTileType.collision_mask["ground-tile"] && !newTileType.collision_mask["water-tile"]) {
|
||||
return
|
||||
} else {
|
||||
|
||||
let convertedTiles: MapTile[] = updatedTiles.map((it) => {
|
||||
return {
|
||||
objectName: "LuaTile",
|
||||
position: it.position,
|
||||
prototypeName: newTileType.name,
|
||||
surfaceIndex: surfaceIndex
|
||||
}
|
||||
})
|
||||
|
||||
let mapTiles: MapTiles = createMapTiles(surfaceIndex, convertedTiles)
|
||||
|
||||
emitEvent(mapTiles, tick, eventType)
|
||||
}
|
||||
}
|
||||
|
||||
function createMapTiles(
|
||||
surfaceIndex: uint,
|
||||
tiles: MapTile[],
|
||||
): MapTiles {
|
||||
return {
|
||||
objectName: "LuaTiles",
|
||||
surfaceIndex: surfaceIndex,
|
||||
tiles: tiles,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,11 @@ import {
|
|||
handleConsoleChat,
|
||||
handleEntityUpdate,
|
||||
handlePlayerUpdate,
|
||||
handleSurfaceUpdate
|
||||
handleSurfaceUpdate,
|
||||
handleTilesUpdate
|
||||
} from "./handlers";
|
||||
import on_player_built_tile = defines.events.on_player_built_tile;
|
||||
import on_robot_built_tile = defines.events.on_robot_built_tile;
|
||||
|
||||
const mapEventIdToName = new LuaTable<defines.Events, keyof typeof defines.events>()
|
||||
for (const [k, v] of pairs(defines.events)) {
|
||||
|
|
@ -62,6 +65,19 @@ script.on_event(
|
|||
script.on_event(
|
||||
defines.events.on_chunk_generated,
|
||||
(e: OnChunkGeneratedEvent) => {
|
||||
handleChunkUpdate(e.tick, mapEventIdToName.get(e.name), e.surface.index, e.position)
|
||||
handleChunkUpdate(e.tick, mapEventIdToName.get(e.name), e.surface.index, e.position, e.area)
|
||||
}
|
||||
)
|
||||
|
||||
script.on_event(
|
||||
[on_player_built_tile, on_robot_built_tile],
|
||||
(builtTilesEvent) => {
|
||||
handleTilesUpdate(
|
||||
builtTilesEvent.tick,
|
||||
mapEventIdToName.get(builtTilesEvent.name),
|
||||
builtTilesEvent.surface_index,
|
||||
builtTilesEvent.tile,
|
||||
builtTilesEvent.tiles,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ sealed class FactorioObjectData {
|
|||
LuaSurface,
|
||||
MapChunk,
|
||||
LuaTile,
|
||||
LuaTiles,
|
||||
ConsoleChatMessage,
|
||||
}
|
||||
}
|
||||
|
|
@ -63,6 +64,7 @@ object FactorioObjectDataSerializer : JsonContentPolymorphicSerializer<FactorioO
|
|||
FactorioObjectData.ObjectName.LuaSurface -> SurfaceData.serializer()
|
||||
FactorioObjectData.ObjectName.MapChunk -> MapChunk.serializer()
|
||||
FactorioObjectData.ObjectName.LuaTile -> MapTile.serializer()
|
||||
FactorioObjectData.ObjectName.LuaTiles -> MapTiles.serializer()
|
||||
FactorioObjectData.ObjectName.ConsoleChatMessage -> ConsoleChatMessage.serializer()
|
||||
null ->
|
||||
throw Exception("Unknown FactorioObjectData $key: '$type' ")
|
||||
|
|
@ -112,21 +114,30 @@ data class SurfaceData(
|
|||
override val objectName = ObjectName.LuaSurface
|
||||
}
|
||||
|
||||
/** Quasi-object. This isn't a Factorio Lua type, but a helpful collection of [MapTile]s */
|
||||
/** This isn't a Factorio Lua type, but a helpful collection of Chunk info */
|
||||
@Serializable
|
||||
data class MapChunk(
|
||||
val tiles: List<MapTile>,
|
||||
val tiles: MapTiles,
|
||||
val position: MapChunkPosition,
|
||||
) : FactorioObjectData() {
|
||||
@EncodeDefault
|
||||
override val objectName = ObjectName.MapChunk
|
||||
}
|
||||
|
||||
/** This isn't a Factorio Lua type, but a helpful collection of [MapTile]s */
|
||||
@Serializable
|
||||
data class MapTiles(
|
||||
val surfaceIndex: Int,
|
||||
val tiles: List<MapTile>,
|
||||
) : FactorioObjectData() {
|
||||
@EncodeDefault
|
||||
override val objectName = ObjectName.LuaTiles
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class MapTile(
|
||||
val prototypeName: String,
|
||||
val position: MapTilePosition,
|
||||
val surfaceIndex: Int,
|
||||
) : FactorioObjectData() {
|
||||
@EncodeDefault
|
||||
override val objectName = ObjectName.LuaTile
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ class FactorioEventTest : FunSpec({
|
|||
"characterUnitNumber": 1,
|
||||
"associatedCharactersUnitNumbers": {},
|
||||
"position": {
|
||||
"type": "MAP",
|
||||
"x": 30.6,
|
||||
"y": 84.1
|
||||
},
|
||||
|
|
@ -53,8 +52,7 @@ class FactorioEventTest : FunSpec({
|
|||
name = "fredthedeadhead",
|
||||
characterUnitNumber = 1u,
|
||||
associatedCharactersUnitNumbers = emptyList(),
|
||||
position = PositionData(
|
||||
type = PositionType.MAP,
|
||||
position = MapEntityPosition(
|
||||
x = 30.6,
|
||||
y = 84.1,
|
||||
),
|
||||
|
|
@ -148,7 +146,6 @@ class FactorioEventTest : FunSpec({
|
|||
"surfaceIndex": 1,
|
||||
"unitNumber": 1,
|
||||
"position": {
|
||||
"type": "MAP",
|
||||
"x": 37.5,
|
||||
"y": 81.1
|
||||
}
|
||||
|
|
@ -174,10 +171,9 @@ class FactorioEventTest : FunSpec({
|
|||
surfaceIndex = 1,
|
||||
unitNumber = 1u,
|
||||
playerIndex = null,
|
||||
position = PositionData(
|
||||
position = MapEntityPosition(
|
||||
x = 37.5,
|
||||
y = 81.1,
|
||||
type = PositionType.MAP,
|
||||
)
|
||||
),
|
||||
eventType = "on_player_changed_position",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue