1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00

Refactor item processor to use charges and thus be more correct, even at low tick rates

This commit is contained in:
tobspr
2020-08-30 15:31:53 +02:00
parent 091401e52b
commit 9b8745535b
20 changed files with 2105 additions and 1975 deletions

View File

@@ -1,144 +1,150 @@
import { enumDirection, enumInvertedDirections, Vector } from "../../core/vector";
import { types } from "../../savegame/serialization";
import { BaseItem } from "../base_item";
import { Component } from "../component";
/** @typedef {{
* pos: Vector,
* directions: enumDirection[],
* filter?: ItemType
* }} ItemAcceptorSlot */
/**
* Contains information about a slot plus its location
* @typedef {{
* slot: ItemAcceptorSlot,
* index: number,
* acceptedDirection: enumDirection
* }} ItemAcceptorLocatedSlot */
/** @typedef {{
* pos: Vector,
* directions: enumDirection[],
* filter?: ItemType
* }} ItemAcceptorSlotConfig */
export class ItemAcceptorComponent extends Component {
static getId() {
return "ItemAcceptor";
}
duplicateWithoutContents() {
const slotsCopy = [];
for (let i = 0; i < this.slots.length; ++i) {
const slot = this.slots[i];
slotsCopy.push({
pos: slot.pos.copy(),
directions: slot.directions.slice(),
filter: slot.filter,
});
}
return new ItemAcceptorComponent({
slots: slotsCopy,
});
}
/**
*
* @param {object} param0
* @param {Array<ItemAcceptorSlotConfig>} param0.slots The slots from which we accept items
*/
constructor({ slots = [] }) {
super();
/**
* Fixes belt animations
* @type {Array<{ item: BaseItem, slotIndex: number, animProgress: number, direction: enumDirection }>}
*/
this.itemConsumptionAnimations = [];
this.setSlots(slots);
}
/**
*
* @param {Array<ItemAcceptorSlotConfig>} slots
*/
setSlots(slots) {
/** @type {Array<ItemAcceptorSlot>} */
this.slots = [];
for (let i = 0; i < slots.length; ++i) {
const slot = slots[i];
this.slots.push({
pos: slot.pos,
directions: slot.directions,
// Which type of item to accept (shape | color | all) @see ItemType
filter: slot.filter,
});
}
}
/**
* Returns if this acceptor can accept a new item at slot N
* @param {number} slotIndex
* @param {BaseItem=} item
*/
canAcceptItem(slotIndex, item) {
const slot = this.slots[slotIndex];
return !slot.filter || slot.filter === item.getItemType();
}
/**
* Called when an item has been accepted so that
* @param {number} slotIndex
* @param {enumDirection} direction
* @param {BaseItem} item
*/
onItemAccepted(slotIndex, direction, item) {
this.itemConsumptionAnimations.push({
item,
slotIndex,
direction,
animProgress: 0.0,
});
}
/**
* Tries to find a slot which accepts the current item
* @param {Vector} targetLocalTile
* @param {enumDirection} fromLocalDirection
* @returns {ItemAcceptorLocatedSlot|null}
*/
findMatchingSlot(targetLocalTile, fromLocalDirection) {
// We need to invert our direction since the acceptor specifies *from* which direction
// it accepts items, but the ejector specifies *into* which direction it ejects items.
// E.g.: Ejector ejects into "right" direction but acceptor accepts from "left" direction.
const desiredDirection = enumInvertedDirections[fromLocalDirection];
// Go over all slots and try to find a target slot
for (let slotIndex = 0; slotIndex < this.slots.length; ++slotIndex) {
const slot = this.slots[slotIndex];
// Make sure the acceptor slot is on the right position
if (!slot.pos.equals(targetLocalTile)) {
continue;
}
// Check if the acceptor slot accepts items from our direction
for (let i = 0; i < slot.directions.length; ++i) {
// const localDirection = targetStaticComp.localDirectionToWorld(slot.directions[l]);
if (desiredDirection === slot.directions[i]) {
return {
slot,
index: slotIndex,
acceptedDirection: desiredDirection,
};
}
}
}
return null;
}
}
import { enumDirection, enumInvertedDirections, Vector } from "../../core/vector";
import { types } from "../../savegame/serialization";
import { BaseItem } from "../base_item";
import { Component } from "../component";
/** @typedef {{
* pos: Vector,
* directions: enumDirection[],
* filter?: ItemType
* }} ItemAcceptorSlot */
/**
* Contains information about a slot plus its location
* @typedef {{
* slot: ItemAcceptorSlot,
* index: number,
* acceptedDirection: enumDirection
* }} ItemAcceptorLocatedSlot */
/** @typedef {{
* pos: Vector,
* directions: enumDirection[],
* filter?: ItemType
* }} ItemAcceptorSlotConfig */
export class ItemAcceptorComponent extends Component {
static getId() {
return "ItemAcceptor";
}
duplicateWithoutContents() {
const slotsCopy = [];
for (let i = 0; i < this.slots.length; ++i) {
const slot = this.slots[i];
slotsCopy.push({
pos: slot.pos.copy(),
directions: slot.directions.slice(),
filter: slot.filter,
});
}
return new ItemAcceptorComponent({
slots: slotsCopy,
});
}
/**
*
* @param {object} param0
* @param {Array<ItemAcceptorSlotConfig>} param0.slots The slots from which we accept items
*/
constructor({ slots = [] }) {
super();
/**
* Fixes belt animations
* @type {Array<{
* item: BaseItem,
* slotIndex: number,
* animProgress: number,
* direction: enumDirection
* }>}
*/
this.itemConsumptionAnimations = [];
this.setSlots(slots);
}
/**
*
* @param {Array<ItemAcceptorSlotConfig>} slots
*/
setSlots(slots) {
/** @type {Array<ItemAcceptorSlot>} */
this.slots = [];
for (let i = 0; i < slots.length; ++i) {
const slot = slots[i];
this.slots.push({
pos: slot.pos,
directions: slot.directions,
// Which type of item to accept (shape | color | all) @see ItemType
filter: slot.filter,
});
}
}
/**
* Returns if this acceptor can accept a new item at slot N
* @param {number} slotIndex
* @param {BaseItem=} item
*/
canAcceptItem(slotIndex, item) {
const slot = this.slots[slotIndex];
return !slot.filter || slot.filter === item.getItemType();
}
/**
* Called when an item has been accepted so that
* @param {number} slotIndex
* @param {enumDirection} direction
* @param {BaseItem} item
* @param {number} remainingProgress World space remaining progress, can be set to set the start position of the item
*/
onItemAccepted(slotIndex, direction, item, remainingProgress = 0.0) {
this.itemConsumptionAnimations.push({
item,
slotIndex,
direction,
animProgress: Math.min(1, remainingProgress * 2),
});
}
/**
* Tries to find a slot which accepts the current item
* @param {Vector} targetLocalTile
* @param {enumDirection} fromLocalDirection
* @returns {ItemAcceptorLocatedSlot|null}
*/
findMatchingSlot(targetLocalTile, fromLocalDirection) {
// We need to invert our direction since the acceptor specifies *from* which direction
// it accepts items, but the ejector specifies *into* which direction it ejects items.
// E.g.: Ejector ejects into "right" direction but acceptor accepts from "left" direction.
const desiredDirection = enumInvertedDirections[fromLocalDirection];
// Go over all slots and try to find a target slot
for (let slotIndex = 0; slotIndex < this.slots.length; ++slotIndex) {
const slot = this.slots[slotIndex];
// Make sure the acceptor slot is on the right position
if (!slot.pos.equals(targetLocalTile)) {
continue;
}
// Check if the acceptor slot accepts items from our direction
for (let i = 0; i < slot.directions.length; ++i) {
// const localDirection = targetStaticComp.localDirectionToWorld(slot.directions[l]);
if (desiredDirection === slot.directions[i]) {
return {
slot,
index: slotIndex,
acceptedDirection: desiredDirection,
};
}
}
}
return null;
}
}

View File

@@ -29,6 +29,17 @@ export const enumItemProcessorRequirements = {
filter: "filter",
};
/** @typedef {{
* item: BaseItem,
* requiredSlot?: number,
* preferredSlot?: number
* }} EjectorItemToEject */
/** @typedef {{
* remainingTime: number,
* items: Array<EjectorItemToEject>,
* }} EjectorCharge */
export class ItemProcessorComponent extends Component {
static getId() {
return "ItemProcessor";
@@ -37,20 +48,6 @@ export class ItemProcessorComponent extends Component {
static getSchema() {
return {
nextOutputSlot: types.uint,
inputSlots: types.array(
types.structured({
item: typeItemSingleton,
sourceSlot: types.uint,
})
),
itemsToEject: types.array(
types.structured({
item: typeItemSingleton,
requiredSlot: types.nullable(types.uint),
preferredSlot: types.nullable(types.uint),
})
),
secondsUntilEject: types.float,
};
}
@@ -101,21 +98,15 @@ export class ItemProcessorComponent extends Component {
* What we are currently processing, empty if we don't produce anything rn
* requiredSlot: Item *must* be ejected on this slot
* preferredSlot: Item *can* be ejected on this slot, but others are fine too if the one is not usable
* @type {Array<{item: BaseItem, requiredSlot?: number, preferredSlot?: number}>}
* @type {Array<EjectorCharge>}
*/
this.itemsToEject = [];
/**
* How long it takes until we are done with the current items
* @type {number}
*/
this.secondsUntilEject = 0;
this.ongoingCharges = [];
/**
* How much processing time we have left from the last tick
* @type {number}
*/
this.bonusFromLastTick = 0;
this.bonusTime = 0;
}
/**