Improve tunnel performance by caching receivers

This commit is contained in:
tobspr 2020-06-27 11:39:32 +02:00
parent a057d68a8e
commit 5dab3508cd
3 changed files with 193 additions and 70 deletions

View File

@ -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;
} }
/** /**

View File

@ -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);

View File

@ -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
for (let i = 0; i < this.allEntities.length; ++i) { */
const entity = this.allEntities[i]; onEntityChanged(entity) {
if (!this.root.gameInitialized) {
return;
}
const undergroundComp = entity.components.UndergroundBelt; const undergroundComp = entity.components.UndergroundBelt;
const pendingItems = undergroundComp.pendingItems; if (!undergroundComp) {
return;
}
// Decrease remaining time of all items in belt const affectedArea = entity.components.StaticMapEntity.getTileSpaceBounds().expandedInAllDirections(
for (let k = 0; k < pendingItems.length; ++k) { globalConfig.undergroundBeltMaxTilesByTier[
const item = pendingItems[k]; globalConfig.undergroundBeltMaxTilesByTier.length - 1
item[1] = Math.max(0, item[1] - delta); ] + 1
if (G_IS_DEV && globalConfig.debug.instantBelts) { );
item[1] = 0;
} if (this.areaToRecompute) {
} this.areaToRecompute = this.areaToRecompute.getUnion(affectedArea);
if (undergroundComp.mode === enumUndergroundBeltMode.sender) {
this.handleSender(entity);
} else { } else {
this.handleReceiver(entity); this.areaToRecompute = affectedArea;
}
} }
} }
@ -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;
@ -208,28 +222,74 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
} }
/** /**
* * Recomputes the cache in the given area, invalidating all entries there
* @param {Entity} entity
*/ */
handleSender(entity) { 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 staticComp = entity.components.StaticMapEntity;
const undergroundComp = entity.components.UndergroundBelt; const undergroundComp = entity.components.UndergroundBelt;
// Check if we have any item
if (undergroundComp.pendingItems.length > 0) {
const nextItemAndDuration = undergroundComp.pendingItems[0];
const remainingTime = nextItemAndDuration[1];
const nextItem = nextItemAndDuration[0];
if (remainingTime === 0) {
// Try to find a receiver
const searchDirection = staticComp.localDirectionToWorld(enumDirection.top); const searchDirection = staticComp.localDirectionToWorld(enumDirection.top);
const searchVector = enumDirectionToVector[searchDirection]; const searchVector = enumDirectionToVector[searchDirection];
const targetRotation = enumDirectionToAngle[searchDirection]; const targetRotation = enumDirectionToAngle[searchDirection];
let currentTile = staticComp.origin; let currentTile = staticComp.origin;
// Search in the direction of the tunnel
for ( for (
let searchOffset = 0; let searchOffset = 0;
searchOffset < globalConfig.undergroundBeltMaxTilesByTier[undergroundComp.tier]; searchOffset < globalConfig.undergroundBeltMaxTilesByTier[undergroundComp.tier];
@ -237,33 +297,81 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
) { ) {
currentTile = currentTile.add(searchVector); currentTile = currentTile.add(searchVector);
const contents = this.root.map.getTileContent(currentTile); const potentialReceiver = this.root.map.getTileContent(currentTile);
if (contents) { if (!potentialReceiver) {
const receiverUndergroundComp = contents.components.UndergroundBelt; // Empty tile
if ( continue;
receiverUndergroundComp && }
receiverUndergroundComp.tier === undergroundComp.tier const receiverUndergroundComp = potentialReceiver.components.UndergroundBelt;
) { if (!receiverUndergroundComp || receiverUndergroundComp.tier !== undergroundComp.tier) {
const receiverStaticComp = contents.components.StaticMapEntity; // Not a tunnel, or not on the same tier
if (receiverStaticComp.rotation === targetRotation) { continue;
if (receiverUndergroundComp.mode === enumUndergroundBeltMode.receiver) { }
// Try to pass over the item to the receiver
if ( if (receiverUndergroundComp.mode !== enumUndergroundBeltMode.receiver) {
receiverUndergroundComp.tryAcceptTunneledItem( // Not a receiver
nextItem, continue;
searchOffset, }
this.root.hubGoals.getUndergroundBeltBaseSpeed()
) const receiverStaticComp = potentialReceiver.components.StaticMapEntity;
) { if (receiverStaticComp.rotation !== targetRotation) {
undergroundComp.pendingItems = []; // Wrong rotation
continue;
}
return { entity: potentialReceiver, distance: searchOffset };
}
// None found
return { entity: null, distance: 0 };
}
/**
*
* @param {Entity} entity
*/
handleSender(entity) {
const undergroundComp = entity.components.UndergroundBelt;
// 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"
);
} }
} }
// When we hit some underground belt, always stop, no matter what if (!receiver.entity) {
break; // If there is no connection to a receiver, ignore this one
} return;
}
} }
// Check if we have any item
if (undergroundComp.pendingItems.length > 0) {
assert(undergroundComp.pendingItems.length === 1, "more than 1 pending");
const nextItemAndDuration = undergroundComp.pendingItems[0];
const remainingTime = nextItemAndDuration[1];
const nextItem = nextItemAndDuration[0];
// Check if the item is ready to be emitted
if (remainingTime === 0) {
// Check if the receiver can accept it
if (
receiver.entity.components.UndergroundBelt.tryAcceptTunneledItem(
nextItem,
receiver.distance,
this.root.hubGoals.getUndergroundBeltBaseSpeed()
)
) {
// Drop this item
fastArrayDelete(undergroundComp.pendingItems, 0);
} }
} }
} }
@ -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];