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,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue