mirror of
https://github.com/tobspr/shapez.io.git
synced 2024-10-27 20:34:29 +00:00
Vastly improve underground belt performance
This commit is contained in:
parent
9075841768
commit
afa08b06dc
@ -48,7 +48,7 @@ export class UndergroundBeltComponent extends Component {
|
|||||||
* Used on both receiver and sender.
|
* Used on both receiver and sender.
|
||||||
* Reciever: Used to store the next item to transfer, and to block input while doing this
|
* Reciever: Used to store the next item to transfer, and to block input while doing this
|
||||||
* Sender: Used to store which items are currently "travelling"
|
* Sender: Used to store which items are currently "travelling"
|
||||||
* @type {Array<[BaseItem, number]>} Format is [Item, remaining seconds until transfer/ejection]
|
* @type {Array<[BaseItem, number]>} Format is [Item, ingame time to eject the item]
|
||||||
*/
|
*/
|
||||||
this.pendingItems = [];
|
this.pendingItems = [];
|
||||||
|
|
||||||
@ -85,8 +85,9 @@ export class UndergroundBeltComponent extends Component {
|
|||||||
* @param {BaseItem} item
|
* @param {BaseItem} item
|
||||||
* @param {number} travelDistance How many tiles this item has to travel
|
* @param {number} travelDistance How many tiles this item has to travel
|
||||||
* @param {number} beltSpeed How fast this item travels
|
* @param {number} beltSpeed How fast this item travels
|
||||||
|
* @param {number} now Current ingame time
|
||||||
*/
|
*/
|
||||||
tryAcceptTunneledItem(item, travelDistance, beltSpeed) {
|
tryAcceptTunneledItem(item, travelDistance, beltSpeed, now) {
|
||||||
if (this.mode !== enumUndergroundBeltMode.receiver) {
|
if (this.mode !== enumUndergroundBeltMode.receiver) {
|
||||||
// Only receivers can accept tunneled items
|
// Only receivers can accept tunneled items
|
||||||
return false;
|
return false;
|
||||||
@ -105,11 +106,7 @@ export class UndergroundBeltComponent extends Component {
|
|||||||
// Additionally it takes 1 tile for the acceptor which we just add on top.
|
// Additionally it takes 1 tile for the acceptor which we just add on top.
|
||||||
const travelDuration = (travelDistance + 1.5) / beltSpeed / globalConfig.itemSpacingOnBelts;
|
const travelDuration = (travelDistance + 1.5) / beltSpeed / globalConfig.itemSpacingOnBelts;
|
||||||
|
|
||||||
this.pendingItems.push([item, travelDuration]);
|
this.pendingItems.push([item, now + travelDuration]);
|
||||||
|
|
||||||
// Sort so we can only look at the first ones
|
|
||||||
this.pendingItems.sort((a, b) => a[1] - b[1]);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -224,21 +224,9 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
|
|||||||
update() {
|
update() {
|
||||||
this.staleAreaWatcher.update();
|
this.staleAreaWatcher.update();
|
||||||
|
|
||||||
const delta = this.root.dynamicTickrate.deltaSeconds;
|
|
||||||
|
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = 0; i < this.allEntities.length; ++i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntities[i];
|
||||||
const undergroundComp = entity.components.UndergroundBelt;
|
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) {
|
if (undergroundComp.mode === enumUndergroundBeltMode.sender) {
|
||||||
this.handleSender(entity);
|
this.handleSender(entity);
|
||||||
} else {
|
} else {
|
||||||
@ -316,26 +304,22 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we have any item
|
// Check if we have any items to eject
|
||||||
if (undergroundComp.pendingItems.length > 0) {
|
const nextItemAndDuration = undergroundComp.pendingItems[0];
|
||||||
|
if (nextItemAndDuration) {
|
||||||
assert(undergroundComp.pendingItems.length === 1, "more than 1 pending");
|
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
|
// Check if the receiver can accept it
|
||||||
if (remainingTime === 0) {
|
if (
|
||||||
// Check if the receiver can accept it
|
cacheEntry.entity.components.UndergroundBelt.tryAcceptTunneledItem(
|
||||||
if (
|
nextItemAndDuration[0],
|
||||||
cacheEntry.entity.components.UndergroundBelt.tryAcceptTunneledItem(
|
cacheEntry.distance,
|
||||||
nextItem,
|
this.root.hubGoals.getUndergroundBeltBaseSpeed(),
|
||||||
cacheEntry.distance,
|
this.root.time.now()
|
||||||
this.root.hubGoals.getUndergroundBeltBaseSpeed()
|
)
|
||||||
)
|
) {
|
||||||
) {
|
// Drop this item
|
||||||
// Drop this item
|
fastArrayDelete(undergroundComp.pendingItems, 0);
|
||||||
fastArrayDelete(undergroundComp.pendingItems, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -348,19 +332,15 @@ export class UndergroundBeltSystem extends GameSystemWithFilter {
|
|||||||
const undergroundComp = entity.components.UndergroundBelt;
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
|
|
||||||
// Try to eject items, we only check the first one because it is 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 nextItemAndDuration = undergroundComp.pendingItems[0];
|
||||||
if (items.length > 0) {
|
if (nextItemAndDuration) {
|
||||||
const nextItemAndDuration = undergroundComp.pendingItems[0];
|
if (this.root.time.now() > nextItemAndDuration[1]) {
|
||||||
const remainingTime = nextItemAndDuration[1];
|
|
||||||
const nextItem = nextItemAndDuration[0];
|
|
||||||
|
|
||||||
if (remainingTime <= 0) {
|
|
||||||
const ejectorComp = entity.components.ItemEjector;
|
const ejectorComp = entity.components.ItemEjector;
|
||||||
|
|
||||||
const nextSlotIndex = ejectorComp.getFirstFreeSlot();
|
const nextSlotIndex = ejectorComp.getFirstFreeSlot();
|
||||||
if (nextSlotIndex !== null) {
|
if (nextSlotIndex !== null) {
|
||||||
if (ejectorComp.tryEject(nextSlotIndex, nextItem)) {
|
if (ejectorComp.tryEject(nextSlotIndex, nextItemAndDuration[0])) {
|
||||||
items.shift();
|
undergroundComp.pendingItems.shift();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user