1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00
tobspr_shapez.io/src/js/game/systems/item_processor.js

563 lines
22 KiB
JavaScript
Raw Normal View History

2020-08-13 18:30:43 +00:00
import { BaseItem } from "../base_item";
import { enumColorMixingResults, enumColors } from "../colors";
import {
enumItemProcessorRequirements,
enumItemProcessorTypes,
ItemProcessorComponent,
} from "../components/item_processor";
2020-05-09 14:45:23 +00:00
import { Entity } from "../entity";
import { GameSystemWithFilter } from "../game_system_with_filter";
2020-08-29 09:08:30 +00:00
import { BOOL_TRUE_SINGLETON, isTruthyItem } from "../items/boolean_item";
import { ColorItem, COLOR_ITEM_SINGLETONS } from "../items/color_item";
2020-05-17 08:07:20 +00:00
import { ShapeItem } from "../items/shape_item";
2020-05-09 14:45:23 +00:00
/**
* We need to allow queuing charges, otherwise the throughput will stall
*/
const MAX_QUEUED_CHARGES = 2;
2020-05-09 14:45:23 +00:00
export class ItemProcessorSystem extends GameSystemWithFilter {
constructor(root) {
super(root, [ItemProcessorComponent]);
}
update() {
for (let i = 0; i < this.allEntities.length; ++i) {
const entity = this.allEntities[i];
const processorComp = entity.components.ItemProcessor;
const ejectorComp = entity.components.ItemEjector;
const currentCharge = processorComp.ongoingCharges[0];
if (currentCharge) {
// Process next charge
if (currentCharge.remainingTime > 0.0) {
currentCharge.remainingTime -= this.root.dynamicTickrate.deltaSeconds;
if (currentCharge.remainingTime < 0.0) {
// Add bonus time, this is the time we spent too much
processorComp.bonusTime += -currentCharge.remainingTime;
}
}
// Check if it finished
if (currentCharge.remainingTime <= 0.0) {
const itemsToEject = currentCharge.items;
2020-05-09 14:45:23 +00:00
// Go over all items and try to eject them
for (let j = 0; j < itemsToEject.length; ++j) {
const { item, requiredSlot, preferredSlot } = itemsToEject[j];
2020-05-30 15:50:29 +00:00
let slot = null;
if (requiredSlot !== null && requiredSlot !== undefined) {
// We have a slot override, check if that is free
if (ejectorComp.canEjectOnSlot(requiredSlot)) {
slot = requiredSlot;
}
} else if (preferredSlot !== null && preferredSlot !== undefined) {
// We have a slot preference, try using it but otherwise use a free slot
if (ejectorComp.canEjectOnSlot(preferredSlot)) {
slot = preferredSlot;
} else {
slot = ejectorComp.getFirstFreeSlot();
}
2020-05-09 14:45:23 +00:00
} else {
// We can eject on any slot
slot = ejectorComp.getFirstFreeSlot();
2020-05-09 14:45:23 +00:00
}
if (slot !== null) {
// Alright, we can actually eject
if (!ejectorComp.tryEject(slot, item)) {
assert(false, "Failed to eject");
} else {
itemsToEject.splice(j, 1);
j -= 1;
}
2020-05-09 14:45:23 +00:00
}
}
// If the charge was entirely emptied to the outputs, start the next charge
if (itemsToEject.length === 0) {
processorComp.ongoingCharges.shift();
}
2020-05-09 14:45:23 +00:00
}
}
// Check if we have an empty queue and can start a new charge
if (processorComp.ongoingCharges.length < MAX_QUEUED_CHARGES) {
if (this.canProcess(entity)) {
2020-08-11 11:29:47 +00:00
this.startNewCharge(entity);
2020-05-09 14:45:23 +00:00
}
}
}
}
/**
* Returns true if the entity should accept the given item on the given slot.
* This should only be called with matching items! I.e. if a color item is expected
* on the given slot, then only a color item must be passed.
* @param {Entity} entity
* @param {BaseItem} item The item to accept
* @param {number} slotIndex The slot index
* @returns {boolean}
*/
checkRequirements(entity, item, slotIndex) {
const itemProcessorComp = entity.components.ItemProcessor;
const pinsComp = entity.components.WiredPins;
switch (itemProcessorComp.processingRequirement) {
case enumItemProcessorRequirements.painterQuad: {
if (slotIndex === 0) {
// Always accept the shape
return true;
}
// Check the network value at the given slot
const network = pinsComp.slots[slotIndex - 1].linkedNetwork;
2020-08-29 09:08:30 +00:00
const slotIsEnabled = network && isTruthyItem(network.currentValue);
if (!slotIsEnabled) {
return false;
}
return true;
}
case enumItemProcessorRequirements.filter: {
const network = pinsComp.slots[0].linkedNetwork;
if (!network || !network.currentValue) {
// Item filter is not connected
return false;
}
// Otherwise, all good
return true;
}
// By default, everything is accepted
default:
return true;
}
}
/**
* Checks whether it's possible to process something
* @param {Entity} entity
*/
canProcess(entity) {
const processorComp = entity.components.ItemProcessor;
switch (processorComp.processingRequirement) {
// DEFAULT
// By default, we can start processing once all inputs are there
case null: {
return processorComp.inputSlots.length >= processorComp.inputsPerCharge;
}
// QUAD PAINTER
// For the quad painter, it might be possible to start processing earlier
case enumItemProcessorRequirements.painterQuad: {
const pinsComp = entity.components.WiredPins;
/** @type {Object.<number, { item: BaseItem, sourceSlot: number }>} */
const itemsBySlot = {};
for (let i = 0; i < processorComp.inputSlots.length; ++i) {
itemsBySlot[processorComp.inputSlots[i].sourceSlot] = processorComp.inputSlots[i];
}
// First slot is the shape, so if it's not there we can't do anything
if (!itemsBySlot[0]) {
return false;
}
const shapeItem = /** @type {ShapeItem} */ (itemsBySlot[0].item);
const slotStatus = [];
// Check which slots are enabled
for (let i = 0; i < 4; ++i) {
// Extract the network value on the Nth pin
const networkValue = pinsComp.slots[i].linkedNetwork
? pinsComp.slots[i].linkedNetwork.currentValue
: null;
// If there is no "1" on that slot, don't paint there
2020-08-29 09:08:30 +00:00
if (!isTruthyItem(networkValue)) {
slotStatus.push(false);
continue;
}
slotStatus.push(true);
}
// All slots are disabled
if (!slotStatus.includes(true)) {
return false;
}
// Check if all colors of the enabled slots are there
for (let i = 0; i < slotStatus.length; ++i) {
if (slotStatus[i] && !itemsBySlot[1 + i]) {
// A slot which is enabled wasn't enabled. Make sure if there is anything on the quadrant,
// it is not possible to paint, but if there is nothing we can ignore it
for (let j = 0; j < 4; ++j) {
const layer = shapeItem.definition.layers[j];
if (layer && layer[i]) {
return false;
}
}
}
}
return true;
}
// FILTER
// Double check with linked network
case enumItemProcessorRequirements.filter: {
const network = entity.components.WiredPins.slots[0].linkedNetwork;
if (!network || !network.currentValue) {
// Item filter is not connected
return false;
}
return processorComp.inputSlots.length >= processorComp.inputsPerCharge;
}
default:
assertAlways(false, "Unknown requirement for " + processorComp.processingRequirement);
}
}
2020-05-09 14:45:23 +00:00
/**
* Starts a new charge for the entity
* @param {Entity} entity
*/
startNewCharge(entity) {
const processorComp = entity.components.ItemProcessor;
// First, take items
const items = processorComp.inputSlots;
processorComp.inputSlots = [];
/** @type {Object.<string, { item: BaseItem, sourceSlot: number }>} */
const itemsBySlot = {};
for (let i = 0; i < items.length; ++i) {
itemsBySlot[items[i].sourceSlot] = items[i];
}
2020-05-09 14:45:23 +00:00
/** @type {Array<{item: BaseItem, requiredSlot?: number, preferredSlot?: number}>} */
const outItems = [];
2020-05-18 21:58:30 +00:00
// Whether to track the production towards the analytics
let trackProduction = true;
2020-05-09 14:45:23 +00:00
// DO SOME MAGIC
switch (processorComp.type) {
// SPLITTER
case enumItemProcessorTypes.splitterWires:
2020-05-09 14:45:23 +00:00
case enumItemProcessorTypes.splitter: {
2020-05-18 21:58:30 +00:00
trackProduction = false;
2020-05-16 21:01:26 +00:00
const availableSlots = entity.components.ItemEjector.slots.length;
let nextSlot = processorComp.nextOutputSlot++ % availableSlots;
2020-05-09 14:45:23 +00:00
for (let i = 0; i < items.length; ++i) {
outItems.push({
item: items[i].item,
2020-05-16 21:01:26 +00:00
preferredSlot: (nextSlot + i) % availableSlots,
2020-05-09 14:45:23 +00:00
});
}
break;
}
// CUTTER
case enumItemProcessorTypes.cutter: {
const inputItem = /** @type {ShapeItem} */ (items[0].item);
assert(inputItem instanceof ShapeItem, "Input for cut is not a shape");
const inputDefinition = inputItem.definition;
2020-05-16 22:21:33 +00:00
const cutDefinitions = this.root.shapeDefinitionMgr.shapeActionCutHalf(inputDefinition);
for (let i = 0; i < cutDefinitions.length; ++i) {
const definition = cutDefinitions[i];
if (!definition.isEntirelyEmpty()) {
outItems.push({
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(definition),
2020-05-16 22:21:33 +00:00
requiredSlot: i,
});
}
2020-05-09 14:45:23 +00:00
}
2020-05-16 22:21:33 +00:00
break;
}
// CUTTER (Quad)
case enumItemProcessorTypes.cutterQuad: {
const inputItem = /** @type {ShapeItem} */ (items[0].item);
assert(inputItem instanceof ShapeItem, "Input for cut is not a shape");
const inputDefinition = inputItem.definition;
const cutDefinitions = this.root.shapeDefinitionMgr.shapeActionCutQuad(inputDefinition);
for (let i = 0; i < cutDefinitions.length; ++i) {
const definition = cutDefinitions[i];
if (!definition.isEntirelyEmpty()) {
outItems.push({
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(definition),
2020-05-16 22:21:33 +00:00
requiredSlot: i,
});
}
2020-05-09 14:45:23 +00:00
}
break;
}
// ROTATER
case enumItemProcessorTypes.rotater: {
const inputItem = /** @type {ShapeItem} */ (items[0].item);
2020-05-16 21:13:45 +00:00
assert(inputItem instanceof ShapeItem, "Input for rotation is not a shape");
2020-05-09 14:45:23 +00:00
const inputDefinition = inputItem.definition;
const rotatedDefinition = this.root.shapeDefinitionMgr.shapeActionRotateCW(inputDefinition);
outItems.push({
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(rotatedDefinition),
2020-05-09 14:45:23 +00:00
});
2020-05-16 21:13:45 +00:00
break;
}
2020-07-14 00:57:09 +00:00
// ROTATER (CCW)
2020-05-16 21:13:45 +00:00
case enumItemProcessorTypes.rotaterCCW: {
const inputItem = /** @type {ShapeItem} */ (items[0].item);
assert(inputItem instanceof ShapeItem, "Input for rotation is not a shape");
const inputDefinition = inputItem.definition;
const rotatedDefinition = this.root.shapeDefinitionMgr.shapeActionRotateCCW(inputDefinition);
outItems.push({
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(rotatedDefinition),
2020-05-16 21:13:45 +00:00
});
2020-05-09 14:45:23 +00:00
break;
}
2020-07-14 00:57:09 +00:00
// ROTATER (FL)
case enumItemProcessorTypes.rotaterFL: {
const inputItem = /** @type {ShapeItem} */ (items[0].item);
assert(inputItem instanceof ShapeItem, "Input for rotation is not a shape");
const inputDefinition = inputItem.definition;
const rotatedDefinition = this.root.shapeDefinitionMgr.shapeActionRotateFL(inputDefinition);
outItems.push({
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(rotatedDefinition),
2020-07-14 00:57:09 +00:00
});
break;
}
2020-05-09 14:45:23 +00:00
// STACKER
case enumItemProcessorTypes.stacker: {
const lowerItem = /** @type {ShapeItem} */ (itemsBySlot[0].item);
const upperItem = /** @type {ShapeItem} */ (itemsBySlot[1].item);
2020-05-09 14:45:23 +00:00
assert(lowerItem instanceof ShapeItem, "Input for lower stack is not a shape");
assert(upperItem instanceof ShapeItem, "Input for upper stack is not a shape");
const stackedDefinition = this.root.shapeDefinitionMgr.shapeActionStack(
lowerItem.definition,
upperItem.definition
);
outItems.push({
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(stackedDefinition),
2020-05-09 14:45:23 +00:00
});
break;
}
// TRASH
case enumItemProcessorTypes.trash: {
// Well this one is easy .. simply do nothing with the item
break;
}
// MIXER
case enumItemProcessorTypes.mixer: {
// Find both colors and combine them
const item1 = /** @type {ColorItem} */ (items[0].item);
const item2 = /** @type {ColorItem} */ (items[1].item);
assert(item1 instanceof ColorItem, "Input for color mixer is not a color");
assert(item2 instanceof ColorItem, "Input for color mixer is not a color");
const color1 = item1.color;
const color2 = item2.color;
// Try finding mixer color, and if we can't mix it we simply return the same color
const mixedColor = enumColorMixingResults[color1][color2];
let resultColor = color1;
if (mixedColor) {
resultColor = mixedColor;
}
outItems.push({
item: COLOR_ITEM_SINGLETONS[resultColor],
2020-05-09 14:45:23 +00:00
});
break;
}
// PAINTER
case enumItemProcessorTypes.painter: {
const shapeItem = /** @type {ShapeItem} */ (itemsBySlot[0].item);
const colorItem = /** @type {ColorItem} */ (itemsBySlot[1].item);
2020-05-09 14:45:23 +00:00
const colorizedDefinition = this.root.shapeDefinitionMgr.shapeActionPaintWith(
shapeItem.definition,
colorItem.color
);
outItems.push({
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(colorizedDefinition),
2020-05-09 14:45:23 +00:00
});
break;
}
// PAINTER (DOUBLE)
case enumItemProcessorTypes.painterDouble: {
const shapeItem1 = /** @type {ShapeItem} */ (itemsBySlot[0].item);
const shapeItem2 = /** @type {ShapeItem} */ (itemsBySlot[1].item);
const colorItem = /** @type {ColorItem} */ (itemsBySlot[2].item);
assert(shapeItem1 instanceof ShapeItem, "Input for painter is not a shape");
assert(shapeItem2 instanceof ShapeItem, "Input for painter is not a shape");
assert(colorItem instanceof ColorItem, "Input for painter is not a color");
const colorizedDefinition1 = this.root.shapeDefinitionMgr.shapeActionPaintWith(
shapeItem1.definition,
colorItem.color
);
const colorizedDefinition2 = this.root.shapeDefinitionMgr.shapeActionPaintWith(
shapeItem2.definition,
colorItem.color
);
outItems.push({
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(colorizedDefinition1),
});
outItems.push({
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(colorizedDefinition2),
});
break;
}
2020-05-16 22:21:33 +00:00
// PAINTER (QUAD)
case enumItemProcessorTypes.painterQuad: {
const shapeItem = /** @type {ShapeItem} */ (itemsBySlot[0].item);
assert(shapeItem instanceof ShapeItem, "Input for painter is not a shape");
/** @type {Array<enumColors>} */
const colors = [null, null, null, null];
for (let i = 0; i < 4; ++i) {
if (itemsBySlot[i + 1]) {
colors[i] = /** @type {ColorItem} */ (itemsBySlot[i + 1].item).color;
}
}
2020-05-16 22:21:33 +00:00
const colorizedDefinition = this.root.shapeDefinitionMgr.shapeActionPaintWith4Colors(
shapeItem.definition,
/** @type {[string, string, string, string]} */ (colors)
2020-05-16 22:21:33 +00:00
);
outItems.push({
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(colorizedDefinition),
2020-05-16 22:21:33 +00:00
});
break;
}
2020-08-13 18:30:43 +00:00
// FILTER
case enumItemProcessorTypes.filter: {
// TODO
trackProduction = false;
const item = itemsBySlot[0].item;
const network = entity.components.WiredPins.slots[0].linkedNetwork;
if (!network || !network.currentValue) {
outItems.push({
item,
requiredSlot: 1,
});
break;
}
const value = network.currentValue;
if (value.equals(BOOL_TRUE_SINGLETON) || value.equals(item)) {
outItems.push({
item,
requiredSlot: 0,
});
} else {
outItems.push({
item,
requiredSlot: 1,
});
}
break;
}
2020-08-29 08:38:23 +00:00
// READER
case enumItemProcessorTypes.reader: {
// Pass through the item
const item = itemsBySlot[0].item;
outItems.push({ item });
// Track the item
const readerComp = entity.components.BeltReader;
readerComp.lastItemTimes.push(this.root.time.now());
readerComp.lastItem = item;
break;
}
2020-05-09 14:45:23 +00:00
2020-08-29 08:38:23 +00:00
// HUB
2020-05-09 14:45:23 +00:00
case enumItemProcessorTypes.hub: {
2020-05-18 21:58:30 +00:00
trackProduction = false;
2020-05-09 14:45:23 +00:00
const hubComponent = entity.components.Hub;
assert(hubComponent, "Hub item processor has no hub component");
2020-05-18 09:47:17 +00:00
for (let i = 0; i < items.length; ++i) {
2020-08-10 20:04:38 +00:00
const item = /** @type {ShapeItem} */ (items[i].item);
this.root.hubGoals.handleDefinitionDelivered(item.definition);
2020-05-18 09:47:17 +00:00
}
2020-05-09 14:45:23 +00:00
break;
}
default:
assertAlways(false, "Unkown item processor type: " + processorComp.type);
}
2020-05-14 06:44:07 +00:00
// Track produced items
2020-05-18 21:58:30 +00:00
if (trackProduction) {
for (let i = 0; i < outItems.length; ++i) {
this.root.signals.itemProduced.dispatch(outItems[i].item);
}
2020-05-14 06:44:07 +00:00
}
// Queue Charge
const baseSpeed = this.root.hubGoals.getProcessorBaseSpeed(processorComp.type);
const originalTime = 1 / baseSpeed;
const bonusTimeToApply = Math.min(originalTime, processorComp.bonusTime);
const timeToProcess = originalTime - bonusTimeToApply;
processorComp.bonusTime -= bonusTimeToApply;
processorComp.ongoingCharges.push({
items: outItems,
remainingTime: timeToProcess,
});
2020-05-09 14:45:23 +00:00
}
}