Minor performance improvements, improve underground belt performance

This commit is contained in:
tobspr 2020-09-18 19:21:12 +02:00
parent 746f4935ad
commit 0238de1260
4 changed files with 370 additions and 428 deletions

View File

@ -553,25 +553,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
const bonusTimeToApply = Math.min(originalTime, processorComp.bonusTime); const bonusTimeToApply = Math.min(originalTime, processorComp.bonusTime);
const timeToProcess = originalTime - bonusTimeToApply; const timeToProcess = originalTime - bonusTimeToApply;
// Substract one tick because we already process it this frame
// if (processorComp.bonusTime > originalTime) {
// if (processorComp.type === enumItemProcessorTypes.reader) {
// console.log(
// "Bonus time",
// round4Digits(processorComp.bonusTime),
// "Original time",
// round4Digits(originalTime),
// "Overcomit by",
// round4Digits(processorComp.bonusTime - originalTime),
// "->",
// round4Digits(timeToProcess),
// "reduced by",
// round4Digits(bonusTimeToApply)
// );
// }
// }
processorComp.bonusTime -= bonusTimeToApply; processorComp.bonusTime -= bonusTimeToApply;
processorComp.ongoingCharges.push({ processorComp.ongoingCharges.push({
items: outItems, items: outItems,
remainingTime: timeToProcess, remainingTime: timeToProcess,

View File

@ -1,6 +1,6 @@
import { globalConfig } from "../../core/config"; import { globalConfig } from "../../core/config";
import { Loader } from "../../core/loader"; import { Loader } from "../../core/loader";
import { smoothPulse, round4Digits } from "../../core/utils"; import { smoothPulse } from "../../core/utils";
import { enumItemProcessorRequirements, enumItemProcessorTypes } from "../components/item_processor"; import { enumItemProcessorRequirements, enumItemProcessorTypes } from "../components/item_processor";
import { Entity } from "../entity"; import { Entity } from "../entity";
import { GameSystem } from "../game_system"; import { GameSystem } from "../game_system";
@ -17,7 +17,6 @@ export class ItemProcessorOverlaysSystem extends GameSystem {
this.readerOverlaySprite = Loader.getSprite("sprites/misc/reader_overlay.png"); this.readerOverlaySprite = Loader.getSprite("sprites/misc/reader_overlay.png");
this.drawnUids = new Set(); this.drawnUids = new Set();
this.root.signals.gameFrameStarted.add(this.clearDrawnUids, this); this.root.signals.gameFrameStarted.add(this.clearDrawnUids, this);
} }
@ -40,7 +39,6 @@ export class ItemProcessorOverlaysSystem extends GameSystem {
} }
const requirement = processorComp.processingRequirement; const requirement = processorComp.processingRequirement;
if (!requirement && processorComp.type !== enumItemProcessorTypes.reader) { if (!requirement && processorComp.type !== enumItemProcessorTypes.reader) {
continue; continue;
} }

View File

@ -46,7 +46,6 @@ export class MinerSystem extends GameSystemWithFilter {
} }
// Check if miner is above an actual tile // Check if miner is above an actual tile
if (!minerComp.cachedMinedItem) { if (!minerComp.cachedMinedItem) {
const staticComp = entity.components.StaticMapEntity; const staticComp = entity.components.StaticMapEntity;
const tileBelow = this.root.map.getLowerLayerContentXY( const tileBelow = this.root.map.getLowerLayerContentXY(

View File

@ -2,6 +2,8 @@ import { globalConfig } from "../../core/config";
import { Loader } from "../../core/loader"; import { Loader } from "../../core/loader";
import { createLogger } from "../../core/logging"; import { createLogger } from "../../core/logging";
import { Rectangle } from "../../core/rectangle"; import { Rectangle } from "../../core/rectangle";
import { StaleAreaDetector } from "../../core/stale_area_detector";
import { fastArrayDelete } from "../../core/utils";
import { import {
enumAngleToDirection, enumAngleToDirection,
enumDirection, enumDirection,
@ -12,7 +14,6 @@ import {
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"); const logger = createLogger("tunnels");
@ -29,41 +30,20 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
), ),
}; };
this.staleAreaWatcher = new StaleAreaDetector({
root: this.root,
name: "underground-belt",
recomputeMethod: this.recomputeArea.bind(this),
});
this.root.signals.entityManuallyPlaced.add(this.onEntityManuallyPlaced, this); this.root.signals.entityManuallyPlaced.add(this.onEntityManuallyPlaced, this);
/** // NOTICE: Once we remove a tunnel, we need to update the whole area to
* @type {Rectangle} // clear outdated handles
*/ this.staleAreaWatcher.recomputeOnComponentsChanged(
this.areaToRecompute = null; [UndergroundBeltComponent],
globalConfig.undergroundBeltMaxTilesByTier[globalConfig.undergroundBeltMaxTilesByTier.length - 1]
this.root.signals.entityAdded.add(this.onEntityChanged, this);
this.root.signals.entityDestroyed.add(this.onEntityChanged, this);
}
/**
* 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;
}
const affectedArea = entity.components.StaticMapEntity.getTileSpaceBounds().expandedInAllDirections(
globalConfig.undergroundBeltMaxTilesByTier[
globalConfig.undergroundBeltMaxTilesByTier.length - 1
] + 1
); );
if (this.areaToRecompute) {
this.areaToRecompute = this.areaToRecompute.getUnion(affectedArea);
} else {
this.areaToRecompute = affectedArea;
}
} }
/** /**
@ -223,14 +203,9 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
/** /**
* Recomputes the cache in the given area, invalidating all entries there * Recomputes the cache in the given area, invalidating all entries there
* @param {Rectangle} area
*/ */
recomputeArea() { recomputeArea(area) {
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 x = area.x; x < area.right(); ++x) {
for (let y = area.y; y < area.bottom(); ++y) { for (let y = area.y; y < area.bottom(); ++y) {
const entities = this.root.map.getLayersContentsMultipleXY(x, y); const entities = this.root.map.getLayersContentsMultipleXY(x, y);
@ -240,7 +215,6 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
if (!undergroundComp) { if (!undergroundComp) {
continue; continue;
} }
undergroundComp.cachedLinkedEntity = null; undergroundComp.cachedLinkedEntity = null;
} }
} }
@ -248,10 +222,7 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
} }
update() { update() {
if (this.areaToRecompute) { this.staleAreaWatcher.update();
this.recomputeArea();
this.areaToRecompute = null;
}
const delta = this.root.dynamicTickrate.deltaSeconds; const delta = this.root.dynamicTickrate.deltaSeconds;
@ -334,21 +305,13 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
const undergroundComp = entity.components.UndergroundBelt; const undergroundComp = entity.components.UndergroundBelt;
// Find the current receiver // Find the current receiver
let receiver = undergroundComp.cachedLinkedEntity; let cacheEntry = undergroundComp.cachedLinkedEntity;
if (!receiver) { if (!cacheEntry) {
// We don't have a receiver, compute it // Need to recompute cache
receiver = undergroundComp.cachedLinkedEntity = this.findRecieverForSender(entity); cacheEntry = 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 (!cacheEntry.entity) {
// If there is no connection to a receiver, ignore this one // If there is no connection to a receiver, ignore this one
return; return;
} }
@ -364,9 +327,9 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
if (remainingTime === 0) { if (remainingTime === 0) {
// Check if the receiver can accept it // Check if the receiver can accept it
if ( if (
receiver.entity.components.UndergroundBelt.tryAcceptTunneledItem( cacheEntry.entity.components.UndergroundBelt.tryAcceptTunneledItem(
nextItem, nextItem,
receiver.distance, cacheEntry.distance,
this.root.hubGoals.getUndergroundBeltBaseSpeed() this.root.hubGoals.getUndergroundBeltBaseSpeed()
) )
) { ) {