it is done
This commit is contained in:
10
core/src/mindustry/world/modules/BlockModule.java
Normal file
10
core/src/mindustry/world/modules/BlockModule.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package mindustry.world.modules;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/** A class that represents compartmentalized tile entity state. */
|
||||
public abstract class BlockModule{
|
||||
public abstract void write(DataOutput stream) throws IOException;
|
||||
|
||||
public abstract void read(DataInput stream) throws IOException;
|
||||
}
|
||||
70
core/src/mindustry/world/modules/ConsumeModule.java
Normal file
70
core/src/mindustry/world/modules/ConsumeModule.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package mindustry.world.modules;
|
||||
|
||||
import mindustry.entities.type.TileEntity;
|
||||
import mindustry.world.consumers.Consume;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class ConsumeModule extends BlockModule{
|
||||
private boolean valid, optionalValid;
|
||||
private final TileEntity entity;
|
||||
|
||||
public ConsumeModule(TileEntity entity){
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
public void update(){
|
||||
//everything is valid here
|
||||
if(entity.tile.isEnemyCheat()){
|
||||
valid = optionalValid = true;
|
||||
return;
|
||||
}
|
||||
|
||||
boolean prevValid = valid();
|
||||
valid = true;
|
||||
optionalValid = true;
|
||||
boolean docons = entity.block.shouldConsume(entity.tile) && entity.block.productionValid(entity.tile);
|
||||
|
||||
for(Consume cons : entity.block.consumes.all()){
|
||||
if(cons.isOptional()) continue;
|
||||
|
||||
if(docons && cons.isUpdate() && prevValid && cons.valid(entity)){
|
||||
cons.update(entity);
|
||||
}
|
||||
|
||||
valid &= cons.valid(entity);
|
||||
}
|
||||
|
||||
for(Consume cons : entity.block.consumes.optionals()){
|
||||
if(docons && cons.isUpdate() && prevValid && cons.valid(entity)){
|
||||
cons.update(entity);
|
||||
}
|
||||
|
||||
optionalValid &= cons.valid(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public void trigger(){
|
||||
for(Consume cons : entity.block.consumes.all()){
|
||||
cons.trigger(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean valid(){
|
||||
return valid && entity.block.shouldConsume(entity.tile);
|
||||
}
|
||||
|
||||
public boolean optionalValid(){
|
||||
return valid() && optionalValid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(DataOutput stream) throws IOException{
|
||||
stream.writeBoolean(valid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(DataInput stream) throws IOException{
|
||||
valid = stream.readBoolean();
|
||||
}
|
||||
}
|
||||
156
core/src/mindustry/world/modules/ItemModule.java
Normal file
156
core/src/mindustry/world/modules/ItemModule.java
Normal file
@@ -0,0 +1,156 @@
|
||||
package mindustry.world.modules;
|
||||
|
||||
import mindustry.type.Item;
|
||||
import mindustry.type.ItemStack;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static mindustry.Vars.content;
|
||||
|
||||
public class ItemModule extends BlockModule{
|
||||
private int[] items = new int[content.items().size];
|
||||
private int total;
|
||||
|
||||
public void forEach(ItemConsumer cons){
|
||||
for(int i = 0; i < items.length; i++){
|
||||
if(items[i] > 0){
|
||||
cons.accept(content.item(i), items[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float sum(ItemCalculator calc){
|
||||
float sum = 0f;
|
||||
for(int i = 0; i < items.length; i++){
|
||||
if(items[i] > 0){
|
||||
sum += calc.get(content.item(i), items[i]);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public boolean has(Item item){
|
||||
return get(item) > 0;
|
||||
}
|
||||
|
||||
public boolean has(Item item, int amount){
|
||||
return get(item) >= amount;
|
||||
}
|
||||
|
||||
public boolean has(ItemStack[] stacks){
|
||||
for(ItemStack stack : stacks){
|
||||
if(!has(stack.item, stack.amount)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean has(ItemStack[] stacks, float multiplier){
|
||||
for(ItemStack stack : stacks){
|
||||
if(!has(stack.item, Math.round(stack.amount * multiplier))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this entity has at least one of each item in each stack.
|
||||
*/
|
||||
public boolean hasOne(ItemStack[] stacks){
|
||||
for(ItemStack stack : stacks){
|
||||
if(!has(stack.item, 1)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int total(){
|
||||
return total;
|
||||
}
|
||||
|
||||
public Item take(){
|
||||
for(int i = 0; i < items.length; i++){
|
||||
if(items[i] > 0){
|
||||
items[i]--;
|
||||
total--;
|
||||
return content.item(i);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int get(Item item){
|
||||
return items[item.id];
|
||||
}
|
||||
|
||||
public void set(Item item, int amount){
|
||||
total += (amount - items[item.id]);
|
||||
items[item.id] = amount;
|
||||
}
|
||||
|
||||
public void add(Item item, int amount){
|
||||
items[item.id] += amount;
|
||||
total += amount;
|
||||
}
|
||||
|
||||
public void addAll(ItemModule items){
|
||||
for(int i = 0; i < items.items.length; i++){
|
||||
this.items[i] += items.items[i];
|
||||
total += items.items[i];
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Item item, int amount){
|
||||
amount = Math.min(amount, items[item.id]);
|
||||
|
||||
items[item.id] -= amount;
|
||||
total -= amount;
|
||||
}
|
||||
|
||||
public void remove(ItemStack stack){
|
||||
remove(stack.item, stack.amount);
|
||||
}
|
||||
|
||||
public void clear(){
|
||||
Arrays.fill(items, 0);
|
||||
total = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(DataOutput stream) throws IOException{
|
||||
byte amount = 0;
|
||||
for(int item : items){
|
||||
if(item > 0) amount++;
|
||||
}
|
||||
|
||||
stream.writeByte(amount); //amount of items
|
||||
|
||||
for(int i = 0; i < items.length; i++){
|
||||
if(items[i] > 0){
|
||||
stream.writeByte(i); //item ID
|
||||
stream.writeInt(items[i]); //item amount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(DataInput stream) throws IOException{
|
||||
//just in case, reset items
|
||||
Arrays.fill(items, 0);
|
||||
byte count = stream.readByte();
|
||||
total = 0;
|
||||
|
||||
for(int j = 0; j < count; j++){
|
||||
int itemid = stream.readByte();
|
||||
int itemamount = stream.readInt();
|
||||
items[content.item(itemid).id] = itemamount;
|
||||
total += itemamount;
|
||||
}
|
||||
}
|
||||
|
||||
public interface ItemConsumer{
|
||||
void accept(Item item, float amount);
|
||||
}
|
||||
|
||||
public interface ItemCalculator{
|
||||
float get(Item item, int amount);
|
||||
}
|
||||
}
|
||||
125
core/src/mindustry/world/modules/LiquidModule.java
Normal file
125
core/src/mindustry/world/modules/LiquidModule.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package mindustry.world.modules;
|
||||
|
||||
import arc.math.*;
|
||||
import mindustry.type.Liquid;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static mindustry.Vars.content;
|
||||
|
||||
public class LiquidModule extends BlockModule{
|
||||
private float[] liquids = new float[content.liquids().size];
|
||||
private float total;
|
||||
private Liquid current = content.liquid(0);
|
||||
private float smoothLiquid;
|
||||
|
||||
public void update(){
|
||||
smoothLiquid = Mathf.lerpDelta(smoothLiquid, currentAmount(), 0.1f);
|
||||
}
|
||||
|
||||
public float smoothAmount(){
|
||||
return smoothLiquid;
|
||||
}
|
||||
|
||||
/** Returns total amount of liquids. */
|
||||
public float total(){
|
||||
return total;
|
||||
}
|
||||
|
||||
/** Last recieved or loaded liquid. Only valid for liquid modules with 1 type of liquid. */
|
||||
public Liquid current(){
|
||||
return current;
|
||||
}
|
||||
|
||||
public void reset(Liquid liquid, float amount){
|
||||
for(int i = 0; i < liquids.length; i++){
|
||||
liquids[i] = 0f;
|
||||
}
|
||||
liquids[liquid.id] = amount;
|
||||
total = amount;
|
||||
current = liquid;
|
||||
}
|
||||
|
||||
public float currentAmount(){
|
||||
return liquids[current.id];
|
||||
}
|
||||
|
||||
public float get(Liquid liquid){
|
||||
return liquids[liquid.id];
|
||||
}
|
||||
|
||||
public void clear(){
|
||||
total = 0;
|
||||
Arrays.fill(liquids, 0);
|
||||
}
|
||||
|
||||
public void add(Liquid liquid, float amount){
|
||||
liquids[liquid.id] += amount;
|
||||
total += amount;
|
||||
current = liquid;
|
||||
}
|
||||
|
||||
public void remove(Liquid liquid, float amount){
|
||||
add(liquid, -amount);
|
||||
}
|
||||
|
||||
public void each(LiquidConsumer cons){
|
||||
for(int i = 0; i < liquids.length; i++){
|
||||
if(liquids[i] > 0){
|
||||
cons.accept(content.liquid(i), liquids[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float sum(LiquidCalculator calc){
|
||||
float sum = 0f;
|
||||
for(int i = 0; i < liquids.length; i++){
|
||||
if(liquids[i] > 0){
|
||||
sum += calc.get(content.liquid(i), liquids[i]);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(DataOutput stream) throws IOException{
|
||||
byte amount = 0;
|
||||
for(float liquid : liquids){
|
||||
if(liquid > 0) amount++;
|
||||
}
|
||||
|
||||
stream.writeByte(amount); //amount of liquids
|
||||
|
||||
for(int i = 0; i < liquids.length; i++){
|
||||
if(liquids[i] > 0){
|
||||
stream.writeByte(i); //liquid ID
|
||||
stream.writeFloat(liquids[i]); //item amount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(DataInput stream) throws IOException{
|
||||
Arrays.fill(liquids, 0);
|
||||
byte count = stream.readByte();
|
||||
|
||||
for(int j = 0; j < count; j++){
|
||||
int liquidid = stream.readByte();
|
||||
float amount = stream.readFloat();
|
||||
liquids[liquidid] = amount;
|
||||
if(amount > 0){
|
||||
current = content.liquid(liquidid);
|
||||
}
|
||||
this.total += amount;
|
||||
}
|
||||
}
|
||||
|
||||
public interface LiquidConsumer{
|
||||
void accept(Liquid liquid, float amount);
|
||||
}
|
||||
|
||||
public interface LiquidCalculator{
|
||||
float get(Liquid liquid, float amount);
|
||||
}
|
||||
}
|
||||
39
core/src/mindustry/world/modules/PowerModule.java
Normal file
39
core/src/mindustry/world/modules/PowerModule.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package mindustry.world.modules;
|
||||
|
||||
import arc.struct.IntArray;
|
||||
import mindustry.world.blocks.power.PowerGraph;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public class PowerModule extends BlockModule{
|
||||
/**
|
||||
* In case of unbuffered consumers, this is the percentage (1.0f = 100%) of the demanded power which can be supplied.
|
||||
* Blocks will work at a reduced efficiency if this is not equal to 1.0f.
|
||||
* In case of buffered consumers, this is the percentage of power stored in relation to the maximum capacity.
|
||||
*/
|
||||
public float status = 0.0f;
|
||||
public PowerGraph graph = new PowerGraph();
|
||||
public IntArray links = new IntArray();
|
||||
|
||||
@Override
|
||||
public void write(DataOutput stream) throws IOException{
|
||||
stream.writeShort(links.size);
|
||||
for(int i = 0; i < links.size; i++){
|
||||
stream.writeInt(links.get(i));
|
||||
}
|
||||
stream.writeFloat(status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(DataInput stream) throws IOException{
|
||||
links.clear();
|
||||
short amount = stream.readShort();
|
||||
for(int i = 0; i < amount; i++){
|
||||
links.add(stream.readInt());
|
||||
}
|
||||
status = stream.readFloat();
|
||||
if(Float.isNaN(status) || Float.isInfinite(status)) status = 0f;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user