Improve tunnel performance by caching receivers
This commit is contained in:
parent
a057d68a8e
commit
5dab3508cd
|
@ -3,6 +3,7 @@ import { Component } from "../component";
|
||||||
import { globalConfig } from "../../core/config";
|
import { globalConfig } from "../../core/config";
|
||||||
import { types } from "../../savegame/serialization";
|
import { types } from "../../savegame/serialization";
|
||||||
import { gItemRegistry } from "../../core/global_registries";
|
import { gItemRegistry } from "../../core/global_registries";
|
||||||
|
import { Entity } from "../entity";
|
||||||
|
|
||||||
/** @enum {string} */
|
/** @enum {string} */
|
||||||
export const enumUndergroundBeltMode = {
|
export const enumUndergroundBeltMode = {
|
||||||
|
@ -10,6 +11,13 @@ export const enumUndergroundBeltMode = {
|
||||||
receiver: "receiver",
|
receiver: "receiver",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {{
|
||||||
|
* entity: Entity,
|
||||||
|
* distance: number
|
||||||
|
* }} LinkedUndergroundBelt
|
||||||
|
*/
|
||||||
|
|
||||||
export class UndergroundBeltComponent extends Component {
|
export class UndergroundBeltComponent extends Component {
|
||||||
static getId() {
|
static getId() {
|
||||||
return "UndergroundBelt";
|
return "UndergroundBelt";
|
||||||
|
@ -52,6 +60,13 @@ export class UndergroundBeltComponent extends Component {
|
||||||
* @type {Array<[BaseItem, number]>} Format is [Item, remaining seconds until transfer/ejection]
|
* @type {Array<[BaseItem, number]>} Format is [Item, remaining seconds until transfer/ejection]
|
||||||
*/
|
*/
|
||||||
this.pendingItems = [];
|
this.pendingItems = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The linked entity, used to speed up performance. This contains either
|
||||||
|
* the entrance or exit depending on the tunnel type
|
||||||
|
* @type {LinkedUndergroundBelt}
|
||||||
|
*/
|
||||||
|
this.cachedLinkedEntity = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -62,7 +62,7 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
|
||||||
"#fe50a6"
|
"#fe50a6"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
this.recomputeAreaCache(this.areaToRecompute);
|
this.recomputeAreaCache();
|
||||||
this.areaToRecompute = null;
|
this.areaToRecompute = null;
|
||||||
} else {
|
} else {
|
||||||
logger.log("Full cache recompute");
|
logger.log("Full cache recompute");
|
||||||
|
@ -83,10 +83,10 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Recomputes the cache in the given area
|
||||||
* @param {Rectangle} area
|
|
||||||
*/
|
*/
|
||||||
recomputeAreaCache(area) {
|
recomputeAreaCache() {
|
||||||
|
const area = this.areaToRecompute;
|
||||||
let entryCount = 0;
|
let entryCount = 0;
|
||||||
|
|
||||||
logger.log("Recomputing area:", area.x, area.y, "/", area.w, area.h);
|
logger.log("Recomputing area:", area.x, area.y, "/", area.w, area.h);
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
import { globalConfig } from "../../core/config";
|
import { globalConfig } from "../../core/config";
|
||||||
import { Loader } from "../../core/loader";
|
import { Loader } from "../../core/loader";
|
||||||
|
import { createLogger } from "../../core/logging";
|
||||||
|
import { Rectangle } from "../../core/rectangle";
|
||||||
import {
|
import {
|
||||||
|
enumAngleToDirection,
|
||||||
enumDirection,
|
enumDirection,
|
||||||
enumDirectionToAngle,
|
enumDirectionToAngle,
|
||||||
enumDirectionToVector,
|
enumDirectionToVector,
|
||||||
Vector,
|
|
||||||
enumAngleToDirection,
|
|
||||||
enumInvertedDirections,
|
enumInvertedDirections,
|
||||||
} from "../../core/vector";
|
} from "../../core/vector";
|
||||||
import { enumUndergroundBeltMode, UndergroundBeltComponent } from "../components/underground_belt";
|
import { enumUndergroundBeltMode, UndergroundBeltComponent } from "../components/underground_belt";
|
||||||
import { Entity } from "../entity";
|
import { Entity } from "../entity";
|
||||||
import { GameSystemWithFilter } from "../game_system_with_filter";
|
import { GameSystemWithFilter } from "../game_system_with_filter";
|
||||||
|
import { fastArrayDelete } from "../../core/utils";
|
||||||
|
|
||||||
|
const logger = createLogger("tunnels");
|
||||||
|
|
||||||
export class UndergroundBeltSystem extends GameSystemWithFilter {
|
export class UndergroundBeltSystem extends GameSystemWithFilter {
|
||||||
constructor(root) {
|
constructor(root) {
|
||||||
|
@ -25,30 +29,40 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
this.root.signals.entityManuallyPlaced.add(this.onEntityPlaced, this);
|
this.root.signals.entityManuallyPlaced.add(this.onEntityManuallyPlaced, this);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Rectangle}
|
||||||
|
*/
|
||||||
|
this.areaToRecompute = null;
|
||||||
|
|
||||||
|
this.root.signals.entityAdded.add(this.onEntityChanged, this);
|
||||||
|
this.root.signals.entityDestroyed.add(this.onEntityChanged, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
/**
|
||||||
const delta = this.root.dynamicTickrate.deltaSeconds;
|
* Called when an entity got added or removed
|
||||||
|
* @param {Entity} entity
|
||||||
|
*/
|
||||||
|
onEntityChanged(entity) {
|
||||||
|
if (!this.root.gameInitialized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
|
if (!undergroundComp) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
const affectedArea = entity.components.StaticMapEntity.getTileSpaceBounds().expandedInAllDirections(
|
||||||
const entity = this.allEntities[i];
|
globalConfig.undergroundBeltMaxTilesByTier[
|
||||||
const undergroundComp = entity.components.UndergroundBelt;
|
globalConfig.undergroundBeltMaxTilesByTier.length - 1
|
||||||
const pendingItems = undergroundComp.pendingItems;
|
] + 1
|
||||||
|
);
|
||||||
|
|
||||||
// Decrease remaining time of all items in belt
|
if (this.areaToRecompute) {
|
||||||
for (let k = 0; k < pendingItems.length; ++k) {
|
this.areaToRecompute = this.areaToRecompute.getUnion(affectedArea);
|
||||||
const item = pendingItems[k];
|
} else {
|
||||||
item[1] = Math.max(0, item[1] - delta);
|
this.areaToRecompute = affectedArea;
|
||||||
if (G_IS_DEV && globalConfig.debug.instantBelts) {
|
|
||||||
item[1] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (undergroundComp.mode === enumUndergroundBeltMode.sender) {
|
|
||||||
this.handleSender(entity);
|
|
||||||
} else {
|
|
||||||
this.handleReceiver(entity);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +70,7 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
|
||||||
* Callback when an entity got placed, used to remove belts between underground belts
|
* Callback when an entity got placed, used to remove belts between underground belts
|
||||||
* @param {Entity} entity
|
* @param {Entity} entity
|
||||||
*/
|
*/
|
||||||
onEntityPlaced(entity) {
|
onEntityManuallyPlaced(entity) {
|
||||||
if (!this.root.app.settings.getAllSettings().enableTunnelSmartplace) {
|
if (!this.root.app.settings.getAllSettings().enableTunnelSmartplace) {
|
||||||
// Smart-place disabled
|
// Smart-place disabled
|
||||||
return;
|
return;
|
||||||
|
@ -207,63 +221,157 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recomputes the cache in the given area, invalidating all entries there
|
||||||
|
*/
|
||||||
|
recomputeArea() {
|
||||||
|
const area = this.areaToRecompute;
|
||||||
|
logger.log("Recomputing area:", area.x, area.y, "/", area.w, area.h);
|
||||||
|
if (G_IS_DEV && globalConfig.debug.renderChanges) {
|
||||||
|
this.root.hud.parts.changesDebugger.renderChange("tunnels", this.areaToRecompute, "#fc03be");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let x = area.x; x < area.right(); ++x) {
|
||||||
|
for (let y = area.y; y < area.bottom(); ++y) {
|
||||||
|
const entity = this.root.map.getTileContentXY(x, y);
|
||||||
|
if (!entity) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
|
if (!undergroundComp) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
undergroundComp.cachedLinkedEntity = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
update() {
|
||||||
|
if (this.areaToRecompute) {
|
||||||
|
this.recomputeArea();
|
||||||
|
this.areaToRecompute = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const delta = this.root.dynamicTickrate.deltaSeconds;
|
||||||
|
|
||||||
|
for (let i = 0; i < this.allEntities.length; ++i) {
|
||||||
|
const entity = this.allEntities[i];
|
||||||
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
|
const pendingItems = undergroundComp.pendingItems;
|
||||||
|
|
||||||
|
// Decrease remaining time of all items in belt
|
||||||
|
for (let k = 0; k < pendingItems.length; ++k) {
|
||||||
|
const item = pendingItems[k];
|
||||||
|
item[1] = Math.max(0, item[1] - delta);
|
||||||
|
if (G_IS_DEV && globalConfig.debug.instantBelts) {
|
||||||
|
item[1] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (undergroundComp.mode === enumUndergroundBeltMode.sender) {
|
||||||
|
this.handleSender(entity);
|
||||||
|
} else {
|
||||||
|
this.handleReceiver(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the receiver for a given sender
|
||||||
|
* @param {Entity} entity
|
||||||
|
* @returns {import("../components/underground_belt").LinkedUndergroundBelt}
|
||||||
|
*/
|
||||||
|
findRecieverForSender(entity) {
|
||||||
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
|
const searchDirection = staticComp.localDirectionToWorld(enumDirection.top);
|
||||||
|
const searchVector = enumDirectionToVector[searchDirection];
|
||||||
|
const targetRotation = enumDirectionToAngle[searchDirection];
|
||||||
|
let currentTile = staticComp.origin;
|
||||||
|
|
||||||
|
// Search in the direction of the tunnel
|
||||||
|
for (
|
||||||
|
let searchOffset = 0;
|
||||||
|
searchOffset < globalConfig.undergroundBeltMaxTilesByTier[undergroundComp.tier];
|
||||||
|
++searchOffset
|
||||||
|
) {
|
||||||
|
currentTile = currentTile.add(searchVector);
|
||||||
|
|
||||||
|
const potentialReceiver = this.root.map.getTileContent(currentTile);
|
||||||
|
if (!potentialReceiver) {
|
||||||
|
// Empty tile
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const receiverUndergroundComp = potentialReceiver.components.UndergroundBelt;
|
||||||
|
if (!receiverUndergroundComp || receiverUndergroundComp.tier !== undergroundComp.tier) {
|
||||||
|
// Not a tunnel, or not on the same tier
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (receiverUndergroundComp.mode !== enumUndergroundBeltMode.receiver) {
|
||||||
|
// Not a receiver
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const receiverStaticComp = potentialReceiver.components.StaticMapEntity;
|
||||||
|
if (receiverStaticComp.rotation !== targetRotation) {
|
||||||
|
// Wrong rotation
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { entity: potentialReceiver, distance: searchOffset };
|
||||||
|
}
|
||||||
|
|
||||||
|
// None found
|
||||||
|
return { entity: null, distance: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {Entity} entity
|
* @param {Entity} entity
|
||||||
*/
|
*/
|
||||||
handleSender(entity) {
|
handleSender(entity) {
|
||||||
const staticComp = entity.components.StaticMapEntity;
|
|
||||||
const undergroundComp = entity.components.UndergroundBelt;
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
|
|
||||||
// Check if we have any item
|
// Find the current receiver
|
||||||
|
let receiver = undergroundComp.cachedLinkedEntity;
|
||||||
|
if (!receiver) {
|
||||||
|
// We don't have a receiver, compute it
|
||||||
|
receiver = undergroundComp.cachedLinkedEntity = this.findRecieverForSender(entity);
|
||||||
|
|
||||||
|
if (G_IS_DEV && globalConfig.debug.renderChanges) {
|
||||||
|
this.root.hud.parts.changesDebugger.renderChange(
|
||||||
|
"sender",
|
||||||
|
entity.components.StaticMapEntity.getTileSpaceBounds(),
|
||||||
|
"#fc03be"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!receiver.entity) {
|
||||||
|
// If there is no connection to a receiver, ignore this one
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we have any item
|
||||||
if (undergroundComp.pendingItems.length > 0) {
|
if (undergroundComp.pendingItems.length > 0) {
|
||||||
|
assert(undergroundComp.pendingItems.length === 1, "more than 1 pending");
|
||||||
const nextItemAndDuration = undergroundComp.pendingItems[0];
|
const nextItemAndDuration = undergroundComp.pendingItems[0];
|
||||||
const remainingTime = nextItemAndDuration[1];
|
const remainingTime = nextItemAndDuration[1];
|
||||||
const nextItem = nextItemAndDuration[0];
|
const nextItem = nextItemAndDuration[0];
|
||||||
|
|
||||||
|
// Check if the item is ready to be emitted
|
||||||
if (remainingTime === 0) {
|
if (remainingTime === 0) {
|
||||||
// Try to find a receiver
|
// Check if the receiver can accept it
|
||||||
const searchDirection = staticComp.localDirectionToWorld(enumDirection.top);
|
if (
|
||||||
const searchVector = enumDirectionToVector[searchDirection];
|
receiver.entity.components.UndergroundBelt.tryAcceptTunneledItem(
|
||||||
const targetRotation = enumDirectionToAngle[searchDirection];
|
nextItem,
|
||||||
|
receiver.distance,
|
||||||
let currentTile = staticComp.origin;
|
this.root.hubGoals.getUndergroundBeltBaseSpeed()
|
||||||
|
)
|
||||||
for (
|
|
||||||
let searchOffset = 0;
|
|
||||||
searchOffset < globalConfig.undergroundBeltMaxTilesByTier[undergroundComp.tier];
|
|
||||||
++searchOffset
|
|
||||||
) {
|
) {
|
||||||
currentTile = currentTile.add(searchVector);
|
// Drop this item
|
||||||
|
fastArrayDelete(undergroundComp.pendingItems, 0);
|
||||||
const contents = this.root.map.getTileContent(currentTile);
|
|
||||||
if (contents) {
|
|
||||||
const receiverUndergroundComp = contents.components.UndergroundBelt;
|
|
||||||
if (
|
|
||||||
receiverUndergroundComp &&
|
|
||||||
receiverUndergroundComp.tier === undergroundComp.tier
|
|
||||||
) {
|
|
||||||
const receiverStaticComp = contents.components.StaticMapEntity;
|
|
||||||
if (receiverStaticComp.rotation === targetRotation) {
|
|
||||||
if (receiverUndergroundComp.mode === enumUndergroundBeltMode.receiver) {
|
|
||||||
// Try to pass over the item to the receiver
|
|
||||||
if (
|
|
||||||
receiverUndergroundComp.tryAcceptTunneledItem(
|
|
||||||
nextItem,
|
|
||||||
searchOffset,
|
|
||||||
this.root.hubGoals.getUndergroundBeltBaseSpeed()
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
undergroundComp.pendingItems = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// When we hit some underground belt, always stop, no matter what
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -276,7 +384,7 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
|
||||||
handleReceiver(entity) {
|
handleReceiver(entity) {
|
||||||
const undergroundComp = entity.components.UndergroundBelt;
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
|
|
||||||
// Try to eject items, we only check the first one cuz its sorted by remaining time
|
// Try to eject items, we only check the first one because it is sorted by remaining time
|
||||||
const items = undergroundComp.pendingItems;
|
const items = undergroundComp.pendingItems;
|
||||||
if (items.length > 0) {
|
if (items.length > 0) {
|
||||||
const nextItemAndDuration = undergroundComp.pendingItems[0];
|
const nextItemAndDuration = undergroundComp.pendingItems[0];
|
||||||
|
|
Reference in New Issue