Skip to content

Commit

Permalink
Fixed RT but consumption bwoke
Browse files Browse the repository at this point in the history
  • Loading branch information
Twcash committed Aug 9, 2024
1 parent ad19990 commit 0b7a466
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 93 deletions.
3 changes: 3 additions & 0 deletions src/aquarion/blocks/AquaCrafters.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import aquarion.AquaAttributes;
import aquarion.AquaItems;
import aquarion.world.blocks.ConsumeRT;
import aquarion.world.blocks.rotPower.RTWallCrafter;
import mindustry.content.Items;
import mindustry.type.Category;
Expand All @@ -25,6 +26,8 @@ public static void loadContent(){
ambientSoundVolume = 0.04F;
envEnabled|= Env.terrestrial | Env.underwater;
envDisabled|= Env.spores | Env.scorching;
squareSprite = false;
consume(new ConsumeRT(10));;
}};
}
}
1 change: 1 addition & 0 deletions src/aquarion/blocks/TorqueBlocks.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package aquarion.blocks;

import aquarion.world.blocks.ConsumeRT;
import aquarion.world.blocks.rotPower.RTGenericCrafter;
import aquarion.world.blocks.rotPower.TorqueShaft;
import mindustry.gen.Sounds;
Expand Down
81 changes: 81 additions & 0 deletions src/aquarion/world/blocks/ConsumeRT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package aquarion.world.blocks;

import aquarion.world.graphs.RTGraph;
import aquarion.world.interfaces.HasRT;
import aquarion.world.meta.AquaStat;
import arc.math.Mathf;
import arc.util.Time;
import mindustry.gen.*;
import mindustry.world.consumers.*;
import mindustry.world.meta.*;

public class ConsumeRT extends Consume {
public float min, max;
public boolean stableRT = false;
public float amount;

public ConsumeRT(float amount) {
this.amount = amount;
}

public HasRT cast(Building build) {
try {
return (HasRT) build;
} catch (ClassCastException e) {
throw new IllegalArgumentException("Building is not a HasRT instance", e);
}
}

@Override
public void display(Stats stats) {
stats.add(AquaStat.requiredRT, "@ to @ @", min, max, AquaStat.rotationUnits.localized());
}

@Override
public float efficiency(Building build) {
if (build instanceof HasRT b) {
float currentPower = b.rTGraph().getTotalRotationPower();
return Mathf.clamp((currentPower - amount) / amount);
}
return 0f;
}

@Override
public void update(Building build) {
if (build instanceof HasRT next) {
float consumption = amount * efficiencyMultiplier(build) * Time.delta;
float consumed = pull(next);
next.rotationPower().rotationPower -= consumed; // Reduce the rotation power
}
}


@Override
public float efficiencyMultiplier(Building build) {
if (build instanceof HasRT b) {
float currentPower = b.rTGraph().getTotalRotationPower();
float ratio = Mathf.map(currentPower, amount, amount * 2, 0, 1);
return 1f - ratio; // Adjusted to reduce efficiency as power is consumed
}
return 0f;
}

public float pull(HasRT build) {
RTGraph graph = build.rTGraph();
float availablePower = graph.getTotalRotationPower();
final float[] consumed = {Math.min(amount, availablePower)};
graph.getBuildings().each(b -> {
if (consumed[0] > 0) {
float toRemove = Math.min(amount, b.rotationPower().rotationPower);
b.rotationPower().rotationPower -= toRemove;
consumed[0] -= toRemove;
}
});
return amount - consumed[0]; // Return the amount of power that was actually consumed
}

public Consume consumeRT() {
stableRT = true;
return this;
}
}
45 changes: 0 additions & 45 deletions src/aquarion/world/blocks/RTConsumer.java

This file was deleted.

32 changes: 21 additions & 11 deletions src/aquarion/world/blocks/rotPower/RTGenericCrafter.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package aquarion.world.blocks.rotPower;

import aquarion.world.blocks.RTConsumer;
import aquarion.world.blocks.ConsumeRT;
import aquarion.world.interfaces.HasRT;
import aquarion.world.meta.RTConfig;
import aquarion.world.meta.RTModule;
import aquarion.world.graphs.RTGraph;
import arc.Core;
import arc.struct.Seq;
import arc.util.io.Reads;
import arc.util.io.Writes;
import mindustry.world.Block;
import mindustry.entities.units.BuildPlan;
import arc.graphics.g2d.*;
import arc.util.*;

import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.ui.*;

public class RTGenericCrafter extends Block {
public RTConfig rtConfig = new RTConfig();
Expand All @@ -30,8 +26,8 @@ public void setBars() {
super.setBars();
rtConfig.addBars(this);
}
public RTConsumer consumeRT(float min, float max) {
return consume(new RTConsumer(min, max));
public ConsumeRT consumeRT(float amount) {
return consume(new ConsumeRT(amount));
}

public class RTProducerBuild extends Building implements HasRT {
Expand All @@ -54,16 +50,30 @@ public void onProximityRemoved() {
@Override
public void updateTile() {
super.updateTile();
noSleep();
}

@Override
public RTConfig rTConfig() {
return new RTConfig(); // Ensure this returns an instance of RTConfig
}
@Override
public void onProximityUpdate() {
super.onProximityAdded();
new RTGraph().addBuild(this);
nextBuilds().each(build -> rTGraph().merge(build.rTGraph(), false));

rTGraph().removeBuild(this, true);
noSleep();
}
@Override
public void read(Reads read) {
super.read(read);
rotationPower.read(read);
}
@Override
public void write(Writes write) {
super.write(write);
rotationPower.write(write);
}
}
}
20 changes: 17 additions & 3 deletions src/aquarion/world/blocks/rotPower/RTWallCrafter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package aquarion.world.blocks.rotPower;

import aquarion.world.blocks.RTConsumer;
import aquarion.world.blocks.ConsumeRT;
import aquarion.world.interfaces.HasRT;
import aquarion.world.meta.RTConfig;
import aquarion.world.meta.RTModule;
import arc.Core;
import arc.math.Mathf;
import arc.math.geom.Geometry;
import arc.util.Strings;
import arc.util.io.Reads;
import arc.util.io.Writes;
import mindustry.graphics.Pal;
import mindustry.ui.Bar;
import mindustry.world.blocks.production.WallCrafter;
Expand All @@ -32,8 +34,8 @@ public void setStats() {
stats.add(Stat.tiles, StatValues.blocks(attribute, floating, 1f, true, false));
stats.add(Stat.drillSpeed, 60f / drillTime * size, StatUnit.itemsSecond);
}
public RTConsumer consumeRT(float min, float max) {
return consume(new RTConsumer(min, max));
public ConsumeRT consumeRT(float amount) {
return consume(new ConsumeRT(amount));
}


Expand All @@ -58,17 +60,29 @@ public RTModule rotationPower() {
@Override
public void updateTile() {
super.updateTile();
noSleep();
}

@Override
public RTConfig rTConfig() {
return new RTConfig(); // Ensure this returns an instance of RTConfig
}
@Override
public void read(Reads read) {
super.read(read);
rotationPower.read(read);
}
@Override
public void write(Writes write) {
super.write(write);
rotationPower.write(write);
}

@Override
public void onProximityUpdate() {
super.onProximityAdded();
rTGraph().removeBuild(this, true);
noSleep();
}

@Override
Expand Down
44 changes: 24 additions & 20 deletions src/aquarion/world/blocks/rotPower/TorqueShaft.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public class TorqueShaftBuild extends Building implements HasRT {

@Override
public void draw() {
float rotationSpeed = 1;
float height = 6;
float rotationSpeed = rotationPower.graph.getTotalRotationPower()/1000; // Use the total rotation power from the graph
float height = 6;
float teeth = 6;
float width = 6;

Expand Down Expand Up @@ -137,30 +137,28 @@ public void draw() {
@Override
public void onProximityUpdate() {
super.onProximityUpdate();

new RTGraph().addBuild(this);
nextBuilds().each(build -> rTGraph().merge(build.rTGraph(), false));
tiling = 0;

tiling = 0;
for (int i = 0; i < 4; i++) {
Building nearbyBuild = nearby(i);
HasRT build = nearbyBuild instanceof HasRT ? (HasRT) nearbyBuild : null;

if (build != null) {
int relativeDir = (i - rotation + 4) % 4;
boolean isFrontOrBack = relativeDir == 0 || relativeDir == 2;
boolean isDirectlyToSide = (relativeDir == 1 || relativeDir == 3) && !(nearbyBuild instanceof TorqueShaftBuild);

if (isFrontOrBack || isDirectlyToSide) {
tiling |= (1 << i);
}

rTGraph().addBuilding((Building) build);
}
HasRT build = nearby(i) instanceof HasRT ? (HasRT) nearby(i) : null;
if (
build != null && connects(build)
) tiling |= (1 << i);
}
}

@Override
public void onProximityRemoved() {
super.onProximityRemoved();
rTGraph().removeBuild(this, false);
public void read(Reads read) {
super.read(read);
rotationPower.read(read);
}
@Override
public void write(Writes write) {
super.write(write);
rotationPower.write(write);
}


Expand All @@ -176,6 +174,12 @@ public RTConfig rTConfig() {
@Override
public void updateTile() {
super.updateTile();
noSleep();
}
@Override
public void onProximityRemoved() {
super.onProximityRemoved();
rTGraph().remove(this, true);
}

@Override
Expand Down
22 changes: 22 additions & 0 deletions src/aquarion/world/graphs/EntityMapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package aquarion.world.graphs;

import arc.func.*;
import arc.struct.*;
import arc.struct.ObjectMap.*;
import mindustry.gen.*;


public class EntityMapping {
public static int customUnits;
public static ObjectIntMap<Class<? extends Entityc>> idMap = new ObjectIntMap<>();
public static Entry<Class<? extends Entityc>, Prov<? extends Entityc>>[] entities = new Entry[]{
entry(GraphUpdater.class, GraphUpdater::new)
};

private static <T extends Entityc> Entry<Class<T>, Prov<T>> entry(Class<T> name, Prov<T> prov) {
Entry<Class<T>, Prov<T>> out = new Entry<>();
out.key = name;
out.value = prov;
return out;
}
}
21 changes: 21 additions & 0 deletions src/aquarion/world/graphs/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package aquarion.world.graphs;

import arc.util.Nullable;

public abstract class Graph {
public final @Nullable GraphUpdater entity;

public Graph() {
entity = new GraphUpdater();
entity.graph = this;
}

public void addGraph() {
entity.add();
}
public void removeGraph() {
entity.remove();
}

public abstract void update();
}
Loading

0 comments on commit 0b7a466

Please sign in to comment.