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
|
// 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 {
|
interface FactorioObjectData {
|
||||||
objectName: ObjectName;
|
objectName: ObjectName;
|
||||||
|
|
@ -41,13 +41,18 @@ interface MapTile extends FactorioObjectData {
|
||||||
objectName: ObjectName;
|
objectName: ObjectName;
|
||||||
position: MapTilePosition;
|
position: MapTilePosition;
|
||||||
prototypeName: string;
|
prototypeName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface MapTiles extends FactorioObjectData {
|
||||||
|
objectName: ObjectName;
|
||||||
surfaceIndex: int;
|
surfaceIndex: int;
|
||||||
|
tiles: MapTile[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MapChunk extends FactorioObjectData {
|
interface MapChunk extends FactorioObjectData {
|
||||||
objectName: ObjectName;
|
objectName: ObjectName;
|
||||||
position: MapChunkPosition;
|
position: MapChunkPosition;
|
||||||
tiles: MapTile[];
|
tiles: MapTiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ConsoleChatMessage extends FactorioObjectData {
|
interface ConsoleChatMessage extends FactorioObjectData {
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,6 @@ export namespace Converters {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export function mapChunkPosition(mapPos: ChunkPositionTable): MapChunkPosition {
|
export function mapChunkPosition(mapPos: ChunkPositionTable): MapChunkPosition {
|
||||||
return {
|
return {
|
||||||
x: mapPos.x,
|
x: mapPos.x,
|
||||||
|
|
@ -103,8 +102,16 @@ export namespace Converters {
|
||||||
|
|
||||||
position: mapTilePosition(tile.position),
|
position: mapTilePosition(tile.position),
|
||||||
prototypeName: tile.prototype.name,
|
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)
|
emitEvent(table, tick, eventType)
|
||||||
}
|
}
|
||||||
|
|
||||||
const CHUNK_SIZE = 32
|
// const CHUNK_SIZE = 32
|
||||||
|
|
||||||
export function handleChunkUpdate(
|
export function handleChunkUpdate(
|
||||||
tick: uint,
|
tick: uint,
|
||||||
eventType: string,
|
eventType: string,
|
||||||
surfaceIndex: uint,
|
surfaceIndex: uint,
|
||||||
position: ChunkPositionTable
|
position: ChunkPositionTable,
|
||||||
|
area: BoundingBoxTable,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
let surface = game.get_surface(surfaceIndex)
|
let surface = game.get_surface(surfaceIndex)
|
||||||
|
|
||||||
let x = (position as ChunkPositionTable).x * CHUNK_SIZE
|
// let x = (position as ChunkPositionTable).x * CHUNK_SIZE
|
||||||
let y = (position as ChunkPositionTable).y * CHUNK_SIZE
|
// let y = (position as ChunkPositionTable).y * CHUNK_SIZE
|
||||||
|
|
||||||
let tiles: LuaTile[] = surface?.find_tiles_filtered({
|
let tiles: LuaTile[] = surface?.find_tiles_filtered({
|
||||||
position: {x: x, y: y},
|
area: area,
|
||||||
radius: CHUNK_SIZE,
|
// position: {x: x, y: y},
|
||||||
|
// radius: CHUNK_SIZE,
|
||||||
collision_mask: ["ground-tile", "water-tile"]
|
collision_mask: ["ground-tile", "water-tile"]
|
||||||
}
|
}
|
||||||
) ?? []
|
) ?? []
|
||||||
|
|
@ -77,9 +79,47 @@ export function handleChunkUpdate(
|
||||||
|
|
||||||
let mapChunk: MapChunk = {
|
let mapChunk: MapChunk = {
|
||||||
objectName: "MapChunk",
|
objectName: "MapChunk",
|
||||||
tiles: convertedTiles,
|
tiles: createMapTiles(surfaceIndex, convertedTiles),
|
||||||
position: mapChunkPosition(position)
|
position: mapChunkPosition(position)
|
||||||
}
|
}
|
||||||
|
|
||||||
emitEvent(mapChunk, tick, eventType)
|
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,
|
handleConsoleChat,
|
||||||
handleEntityUpdate,
|
handleEntityUpdate,
|
||||||
handlePlayerUpdate,
|
handlePlayerUpdate,
|
||||||
handleSurfaceUpdate
|
handleSurfaceUpdate,
|
||||||
|
handleTilesUpdate
|
||||||
} from "./handlers";
|
} 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>()
|
const mapEventIdToName = new LuaTable<defines.Events, keyof typeof defines.events>()
|
||||||
for (const [k, v] of pairs(defines.events)) {
|
for (const [k, v] of pairs(defines.events)) {
|
||||||
|
|
@ -62,6 +65,19 @@ script.on_event(
|
||||||
script.on_event(
|
script.on_event(
|
||||||
defines.events.on_chunk_generated,
|
defines.events.on_chunk_generated,
|
||||||
(e: OnChunkGeneratedEvent) => {
|
(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,
|
LuaSurface,
|
||||||
MapChunk,
|
MapChunk,
|
||||||
LuaTile,
|
LuaTile,
|
||||||
|
LuaTiles,
|
||||||
ConsoleChatMessage,
|
ConsoleChatMessage,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -63,6 +64,7 @@ object FactorioObjectDataSerializer : JsonContentPolymorphicSerializer<FactorioO
|
||||||
FactorioObjectData.ObjectName.LuaSurface -> SurfaceData.serializer()
|
FactorioObjectData.ObjectName.LuaSurface -> SurfaceData.serializer()
|
||||||
FactorioObjectData.ObjectName.MapChunk -> MapChunk.serializer()
|
FactorioObjectData.ObjectName.MapChunk -> MapChunk.serializer()
|
||||||
FactorioObjectData.ObjectName.LuaTile -> MapTile.serializer()
|
FactorioObjectData.ObjectName.LuaTile -> MapTile.serializer()
|
||||||
|
FactorioObjectData.ObjectName.LuaTiles -> MapTiles.serializer()
|
||||||
FactorioObjectData.ObjectName.ConsoleChatMessage -> ConsoleChatMessage.serializer()
|
FactorioObjectData.ObjectName.ConsoleChatMessage -> ConsoleChatMessage.serializer()
|
||||||
null ->
|
null ->
|
||||||
throw Exception("Unknown FactorioObjectData $key: '$type' ")
|
throw Exception("Unknown FactorioObjectData $key: '$type' ")
|
||||||
|
|
@ -112,21 +114,30 @@ data class SurfaceData(
|
||||||
override val objectName = ObjectName.LuaSurface
|
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
|
@Serializable
|
||||||
data class MapChunk(
|
data class MapChunk(
|
||||||
val tiles: List<MapTile>,
|
val tiles: MapTiles,
|
||||||
val position: MapChunkPosition,
|
val position: MapChunkPosition,
|
||||||
) : FactorioObjectData() {
|
) : FactorioObjectData() {
|
||||||
@EncodeDefault
|
@EncodeDefault
|
||||||
override val objectName = ObjectName.MapChunk
|
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
|
@Serializable
|
||||||
data class MapTile(
|
data class MapTile(
|
||||||
val prototypeName: String,
|
val prototypeName: String,
|
||||||
val position: MapTilePosition,
|
val position: MapTilePosition,
|
||||||
val surfaceIndex: Int,
|
|
||||||
) : FactorioObjectData() {
|
) : FactorioObjectData() {
|
||||||
@EncodeDefault
|
@EncodeDefault
|
||||||
override val objectName = ObjectName.LuaTile
|
override val objectName = ObjectName.LuaTile
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ class FactorioEventTest : FunSpec({
|
||||||
"characterUnitNumber": 1,
|
"characterUnitNumber": 1,
|
||||||
"associatedCharactersUnitNumbers": {},
|
"associatedCharactersUnitNumbers": {},
|
||||||
"position": {
|
"position": {
|
||||||
"type": "MAP",
|
|
||||||
"x": 30.6,
|
"x": 30.6,
|
||||||
"y": 84.1
|
"y": 84.1
|
||||||
},
|
},
|
||||||
|
|
@ -53,8 +52,7 @@ class FactorioEventTest : FunSpec({
|
||||||
name = "fredthedeadhead",
|
name = "fredthedeadhead",
|
||||||
characterUnitNumber = 1u,
|
characterUnitNumber = 1u,
|
||||||
associatedCharactersUnitNumbers = emptyList(),
|
associatedCharactersUnitNumbers = emptyList(),
|
||||||
position = PositionData(
|
position = MapEntityPosition(
|
||||||
type = PositionType.MAP,
|
|
||||||
x = 30.6,
|
x = 30.6,
|
||||||
y = 84.1,
|
y = 84.1,
|
||||||
),
|
),
|
||||||
|
|
@ -148,7 +146,6 @@ class FactorioEventTest : FunSpec({
|
||||||
"surfaceIndex": 1,
|
"surfaceIndex": 1,
|
||||||
"unitNumber": 1,
|
"unitNumber": 1,
|
||||||
"position": {
|
"position": {
|
||||||
"type": "MAP",
|
|
||||||
"x": 37.5,
|
"x": 37.5,
|
||||||
"y": 81.1
|
"y": 81.1
|
||||||
}
|
}
|
||||||
|
|
@ -174,10 +171,9 @@ class FactorioEventTest : FunSpec({
|
||||||
surfaceIndex = 1,
|
surfaceIndex = 1,
|
||||||
unitNumber = 1u,
|
unitNumber = 1u,
|
||||||
playerIndex = null,
|
playerIndex = null,
|
||||||
position = PositionData(
|
position = MapEntityPosition(
|
||||||
x = 37.5,
|
x = 37.5,
|
||||||
y = 81.1,
|
y = 81.1,
|
||||||
type = PositionType.MAP,
|
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
eventType = "on_player_changed_position",
|
eventType = "on_player_changed_position",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue