2020-06-27 14:40:51 +00:00
|
|
|
import { gItemRegistry } from "../../core/global_registries";
|
|
|
|
import { types } from "../../savegame/serialization";
|
2020-05-09 14:45:23 +00:00
|
|
|
import { BaseItem } from "../base_item";
|
|
|
|
import { Component } from "../component";
|
|
|
|
|
|
|
|
/** @enum {string} */
|
|
|
|
export const enumItemProcessorTypes = {
|
|
|
|
splitter: "splitter",
|
2020-07-06 19:33:37 +00:00
|
|
|
splitterWires: "splitterWires",
|
2020-05-09 14:45:23 +00:00
|
|
|
cutter: "cutter",
|
2020-05-16 22:21:33 +00:00
|
|
|
cutterQuad: "cutterQuad",
|
2020-05-09 14:45:23 +00:00
|
|
|
rotater: "rotater",
|
2020-05-16 21:13:45 +00:00
|
|
|
rotaterCCW: "rotaterCCW",
|
2020-07-14 00:57:09 +00:00
|
|
|
rotaterFL: "rotaterFL",
|
2020-05-09 14:45:23 +00:00
|
|
|
stacker: "stacker",
|
|
|
|
trash: "trash",
|
|
|
|
mixer: "mixer",
|
|
|
|
painter: "painter",
|
2020-05-16 20:45:40 +00:00
|
|
|
painterDouble: "painterDouble",
|
2020-05-16 22:21:33 +00:00
|
|
|
painterQuad: "painterQuad",
|
2020-05-09 14:45:23 +00:00
|
|
|
hub: "hub",
|
2020-08-13 18:30:43 +00:00
|
|
|
filter: "filter",
|
2020-05-09 14:45:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export class ItemProcessorComponent extends Component {
|
|
|
|
static getId() {
|
|
|
|
return "ItemProcessor";
|
|
|
|
}
|
|
|
|
|
|
|
|
static getSchema() {
|
|
|
|
return {
|
2020-05-14 19:54:11 +00:00
|
|
|
nextOutputSlot: types.uint,
|
|
|
|
inputSlots: types.array(
|
|
|
|
types.structured({
|
|
|
|
item: types.obj(gItemRegistry),
|
|
|
|
sourceSlot: types.uint,
|
|
|
|
})
|
|
|
|
),
|
|
|
|
itemsToEject: types.array(
|
|
|
|
types.structured({
|
|
|
|
item: types.obj(gItemRegistry),
|
|
|
|
requiredSlot: types.nullable(types.uint),
|
|
|
|
preferredSlot: types.nullable(types.uint),
|
|
|
|
})
|
|
|
|
),
|
2020-05-15 17:09:29 +00:00
|
|
|
secondsUntilEject: types.float,
|
2020-05-09 14:45:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-27 12:30:59 +00:00
|
|
|
duplicateWithoutContents() {
|
|
|
|
return new ItemProcessorComponent({
|
|
|
|
processorType: this.type,
|
|
|
|
inputsPerCharge: this.inputsPerCharge,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {object} param0
|
2020-05-16 20:45:40 +00:00
|
|
|
* @param {enumItemProcessorTypes=} param0.processorType Which type of processor this is
|
|
|
|
* @param {number=} param0.inputsPerCharge How many items this machine needs until it can start working
|
2020-05-09 14:45:23 +00:00
|
|
|
*
|
|
|
|
*/
|
2020-05-17 08:07:20 +00:00
|
|
|
constructor({ processorType = enumItemProcessorTypes.splitter, inputsPerCharge = 1 }) {
|
2020-05-09 14:45:23 +00:00
|
|
|
super();
|
|
|
|
|
|
|
|
// Which slot to emit next, this is only a preference and if it can't emit
|
|
|
|
// it will take the other one. Some machines ignore this (e.g. the splitter) to make
|
|
|
|
// sure the outputs always match
|
|
|
|
this.nextOutputSlot = 0;
|
|
|
|
|
|
|
|
// Type of the processor
|
|
|
|
this.type = processorType;
|
|
|
|
|
|
|
|
// How many inputs we need for one charge
|
|
|
|
this.inputsPerCharge = inputsPerCharge;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Our current inputs
|
|
|
|
* @type {Array<{ item: BaseItem, sourceSlot: number }>}
|
|
|
|
*/
|
|
|
|
this.inputSlots = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}>}
|
|
|
|
*/
|
|
|
|
this.itemsToEject = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How long it takes until we are done with the current items
|
|
|
|
*/
|
|
|
|
this.secondsUntilEject = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to take the item
|
|
|
|
* @param {BaseItem} item
|
2020-05-17 08:07:20 +00:00
|
|
|
* @param {number} sourceSlot
|
2020-05-09 14:45:23 +00:00
|
|
|
*/
|
2020-05-17 08:07:20 +00:00
|
|
|
tryTakeItem(item, sourceSlot) {
|
2020-06-27 14:40:51 +00:00
|
|
|
if (this.type === enumItemProcessorTypes.hub || this.type === enumItemProcessorTypes.trash) {
|
|
|
|
// Hub has special logic .. not really nice but efficient.
|
|
|
|
this.inputSlots.push({ item, sourceSlot });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
// Check that we only take one item per slot
|
|
|
|
for (let i = 0; i < this.inputSlots.length; ++i) {
|
|
|
|
const slot = this.inputSlots[i];
|
|
|
|
if (slot.sourceSlot === sourceSlot) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.inputSlots.push({ item, sourceSlot });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|