Skip to content

Commit

Permalink
Fixed #9726
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuken committed Apr 8, 2024
1 parent 870c2aa commit 3b0fac1
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions core/src/mindustry/world/blocks/ConstructBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public class ConstructBuild extends Building{

private float[] accumulator;
private float[] totalAccumulator;
private int[] itemsLeft;

@Override
public String getDisplayName(){
Expand Down Expand Up @@ -270,7 +271,7 @@ public void construct(Unit builder, @Nullable Building core, float amount, Objec

for(int i = 0; i < current.requirements.length; i++){
int reqamount = Math.round(state.rules.buildCostMultiplier * current.requirements[i].amount);
accumulator[i] += Math.min(reqamount * maxProgress, reqamount - totalAccumulator[i] + 0.00001f); //add min amount progressed to the accumulator
accumulator[i] += Math.min(reqamount * maxProgress, reqamount - totalAccumulator[i]); //add min amount progressed to the accumulator
totalAccumulator[i] = Math.min(totalAccumulator[i] + reqamount * maxProgress, reqamount);
}

Expand All @@ -279,9 +280,28 @@ public void construct(Unit builder, @Nullable Building core, float amount, Objec
progress = Mathf.clamp(progress + maxProgress);

if(progress >= 1f || state.rules.infiniteResources){
if(lastBuilder == null) lastBuilder = builder;
if(!net.client()){
constructed(tile, current, lastBuilder, (byte)rotation, builder.team, config);
boolean canFinish = true;

//look at leftover resources to consume, get them from the core if necessary, delay building if not
if(!state.rules.infiniteResources){
for(int i = 0; i < itemsLeft.length; i++){
if(itemsLeft[i] > 0){
if(core != null && core.items.has(current.requirements[i].item, itemsLeft[i])){
core.items.remove(current.requirements[i].item, itemsLeft[i]);
itemsLeft[i] = 0;
}else{
canFinish = false;
break;
}
}
}
}

if(canFinish){
if(lastBuilder == null) lastBuilder = builder;
if(!net.client()){
constructed(tile, current, lastBuilder, (byte)rotation, builder.team, config);
}
}
}
}
Expand Down Expand Up @@ -321,6 +341,7 @@ public void deconstruct(Unit builder, @Nullable CoreBuild core, float amount){
int accepting = Math.min(accumulated, core.storageCapacity - core.items.get(requirements[i].item));
//transfer items directly, as this is not production.
core.items.add(requirements[i].item, accepting);
itemsLeft[i] -= accepting;
accumulator[i] -= accepting;
}else{
accumulator[i] -= accumulated;
Expand All @@ -331,6 +352,14 @@ public void deconstruct(Unit builder, @Nullable CoreBuild core, float amount){
progress = Mathf.clamp(progress - amount);

if(progress <= current.deconstructThreshold || state.rules.infiniteResources){
//add any leftover items that weren't obtained due to rounding errors
if(core != null){
for(int i = 0; i < itemsLeft.length; i++){
core.items.add(current.requirements[i].item, Mathf.clamp(itemsLeft[i], 0, core.storageCapacity - core.items.get(current.requirements[i].item)));
itemsLeft[i] = 0;
}
}

if(lastBuilder == null) lastBuilder = builder;
Call.deconstructFinish(tile, this.current, lastBuilder);
}
Expand All @@ -341,6 +370,11 @@ private float checkRequired(ItemModule inventory, float amount, boolean remove){
boolean infinite = team.rules().infiniteResources || state.rules.infiniteResources;

for(int i = 0; i < current.requirements.length; i++){
//there is no need to remove items that have already been fully taken out
if(itemsLeft[i] == 0){
continue;
}

int sclamount = Math.round(state.rules.buildCostMultiplier * current.requirements[i].amount);
int required = (int)(accumulator[i]); //calculate items that are required now

Expand All @@ -360,6 +394,7 @@ private float checkRequired(ItemModule inventory, float amount, boolean remove){
//remove stuff that is actually used
if(remove && !infinite){
inventory.remove(current.requirements[i].item, maxUse);
itemsLeft[i] -= maxUse;
}
}
//else, no items are required yet, so just keep going
Expand All @@ -368,6 +403,7 @@ private float checkRequired(ItemModule inventory, float amount, boolean remove){
return maxProgress;
}

@Override
public float progress(){
return progress;
}
Expand All @@ -380,8 +416,14 @@ public void setConstruct(Block previous, Block block){
this.current = block;
this.previous = previous;
this.buildCost = block.buildCost * state.rules.buildCostMultiplier;
this.itemsLeft = new int[block.requirements.length];
this.accumulator = new float[block.requirements.length];
this.totalAccumulator = new float[block.requirements.length];

ItemStack[] requirements = current.requirements;
for(int i = 0; i < requirements.length; i++){
this.itemsLeft[i] = Mathf.round(requirements[i].amount * state.rules.buildCostMultiplier);
}
pathfinder.updateTile(tile);
}

Expand All @@ -394,8 +436,14 @@ public void setDeconstruct(Block previous){
this.progress = 1f;
this.current = previous;
this.buildCost = previous.buildCost * state.rules.buildCostMultiplier;
this.itemsLeft = new int[previous.requirements.length];
this.accumulator = new float[previous.requirements.length];
this.totalAccumulator = new float[previous.requirements.length];

ItemStack[] requirements = current.requirements;
for(int i = 0; i < requirements.length; i++){
this.itemsLeft[i] = Mathf.round(requirements[i].amount * state.rules.buildCostMultiplier * state.rules.deconstructRefundMultiplier);
}
pathfinder.updateTile(tile);
}

Expand Down

0 comments on commit 3b0fac1

Please sign in to comment.