Skip to content

Commit

Permalink
WIP web bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed May 25, 2024
1 parent 61f3da3 commit 54e4b4f
Show file tree
Hide file tree
Showing 23 changed files with 687 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -992,12 +992,12 @@ public String url(Boolean isBuy, boolean shorten) {

@Command(desc = "If this is a raw resource")
public boolean isRaw() {
return inputs == null || inputs.length == 0 && cap > 0;
return inputs.length == 0 && cap > 0;
}

@Command(desc = "If this is a manufactured resource")
public boolean isManufactured() {
return inputs != null && inputs.length > 0;
return inputs.length > 0;
}

@Command(desc = "The pollution modifier for this resource's production")
Expand All @@ -1021,7 +1021,7 @@ public double getBoostFactor() {
}

public double getInput(Continent continent, double rads, Predicate<Project> hasProject, ICity city, int improvements) {
if (inputs == null) return 0;
if (inputs.length == 0) return 0;

double base = getBaseProduction(continent, rads, hasProject, city.getLand(), -1);
base = (base * baseProductionInverse) * baseInput;
Expand Down
64 changes: 51 additions & 13 deletions src/main/java/link/locutus/discord/apiv1/enums/city/JavaCity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import link.locutus.discord.Locutus;
import link.locutus.discord.apiv1.domains.City;
import link.locutus.discord.apiv1.enums.city.building.*;
import link.locutus.discord.commands.info.optimal.CityBranch;
import link.locutus.discord.config.Settings;
import link.locutus.discord.db.entities.DBCity;
Expand All @@ -16,11 +17,6 @@
import link.locutus.discord.util.search.BFSUtil;
import link.locutus.discord.apiv1.enums.Continent;
import link.locutus.discord.apiv1.enums.ResourceType;
import link.locutus.discord.apiv1.enums.city.building.Building;
import link.locutus.discord.apiv1.enums.city.building.Buildings;
import link.locutus.discord.apiv1.enums.city.building.CommerceBuilding;
import link.locutus.discord.apiv1.enums.city.building.MilitaryBuilding;
import link.locutus.discord.apiv1.enums.city.building.ResourceBuilding;
import link.locutus.discord.apiv1.enums.city.project.Project;
import link.locutus.discord.apiv1.enums.city.project.Projects;
import it.unimi.dsi.fastutil.PriorityQueue;
Expand Down Expand Up @@ -487,7 +483,7 @@ public JavaCity optimalBuild(Continent continent, double rads, int numCities, Pr
};
valueFunction = modifyValueFunc.apply(valueFunction);

return optimalBuild(continent, valueFunction, goal, finalHasProject, timeout);
return optimalBuild(continent, valueFunction, goal, finalHasProject, timeout, rads);
}

public JavaCity roiBuild(Continent continent, double rads, int numCities, Predicate<Project> hasProject, double grossModifier, int days, long timeout) {
Expand All @@ -499,7 +495,7 @@ public JavaCity roiBuild(Continent continent, double rads, int numCities, Predic
origin.setAge(this.getAgeDays() + days / 2);

JavaCity zeroed = new JavaCity(origin);
zeroed.zeroNonMilitary();
zeroed.zeroNonMilitary().setOptimalPower(continent);

int maxImp = (int) (this.getInfra() / 50);
if (maxImp == zeroed.getNumBuildings()) return zeroed;
Expand All @@ -525,19 +521,60 @@ public JavaCity roiBuild(Continent continent, double rads, int numCities, Predic

valueFunction = modifyValueFunc.apply(valueFunction);

JavaCity optimal = zeroed.optimalBuild(continent, valueFunction, goal, finalHasProject, timeout);
JavaCity optimal = zeroed.optimalBuild(continent, valueFunction, goal, finalHasProject, timeout, rads);
return optimal;
}

public JavaCity optimalBuild(Continent continent, Function<JavaCity, Double> valueFunction, Predicate<Project> hasProject, long timeout) {
return optimalBuild(continent, valueFunction, null, hasProject, timeout);
public JavaCity optimalBuild(Continent continent, Function<JavaCity, Double> valueFunction, Predicate<Project> hasProject, long timeout, double rads) {
return optimalBuild(continent, valueFunction, null, hasProject, timeout, rads);
}

public JavaCity optimalBuild(Continent continent, Function<JavaCity, Double> valueFunction, Function<JavaCity, Boolean> goal, Predicate<Project> hasProject, long timeout) {
public JavaCity zeroPower() {
for (int i = 0; i < buildings.length; i++) {
if (!(Buildings.get(i) instanceof PowerBuilding)) continue;
numBuildings -= buildings[i];
buildings[i] = 0;
}
return this;
}

public JavaCity setOptimalPower(Continent continent) {
int nuclear = 0;
int coal = 0;
int oil = 0;
int wind = 0;
double infra = getInfra();
while (infra > 500 || (getInfra() > 2000 && infra > 250)) {
nuclear++;
infra -= Buildings.NUCLEAR_POWER.getInfraMax();
}
while (infra > 250) {
if (continent.canBuild(Buildings.COAL_POWER)) {
coal++;
infra -= Buildings.COAL_POWER.getInfraMax();
} else if (continent.canBuild(Buildings.OIL_POWER)) {
oil++;
infra -= Buildings.OIL_POWER.getInfraMax();
} else {
break;
}
}
while (infra > 0) {
wind++;
infra -= Buildings.WIND_POWER.getInfraMax();
}
set(Buildings.NUCLEAR_POWER, nuclear);
set(Buildings.COAL_POWER, coal);
set(Buildings.OIL_POWER, oil);
set(Buildings.WIND_POWER, wind);
return this;
}

public JavaCity optimalBuild(Continent continent, Function<JavaCity, Double> valueFunction, Function<JavaCity, Boolean> goal, Predicate<Project> hasProject, long timeout, double rads) {
if (goal == null) {
goal = javaCity -> javaCity.getFreeInfra() < 50;
}
CityBranch searchServices = new CityBranch(continent, -1d, -1d, false, hasProject);
CityBranch searchServices = new CityBranch(this, continent, false, rads, hasProject);

Function<Map.Entry<JavaCity, Integer>, Double> valueFunction2 = e -> valueFunction.apply(e.getKey());
Function<JavaCity, Boolean> finalGoal = goal;
Expand Down Expand Up @@ -567,8 +604,9 @@ public JavaCity optimalBuild(Continent continent, Function<JavaCity, Double> val
throw new IllegalArgumentException("The city infrastructure (" + MathMan.format(origin.getInfra()) + ") is too low for the required buildings (required infra: " + MathMan.format(origin.getRequiredInfra()) + ", mmr: " + origin.getMMR() + ")");
}

Map.Entry<JavaCity, Integer> optimized = BFSUtil.search(goal2, valueFunction2, valueCompletionFunction, searchServices, searchServices, new AbstractMap.SimpleEntry<>(origin, 4), queue, timeout);
Map.Entry<JavaCity, Integer> optimized = BFSUtil.search(goal2, valueFunction2, valueCompletionFunction, searchServices, searchServices, new AbstractMap.SimpleEntry<>(origin, 0), queue, timeout);

System.out.println("Buildings " + optimized.getKey().getNumBuildings());
return optimized == null ? null : optimized.getKey();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@
import link.locutus.discord.apiv1.enums.city.JavaCity;
import link.locutus.discord.apiv1.enums.city.building.ResourceBuilding;
import link.locutus.discord.apiv1.enums.city.project.Project;
import link.locutus.discord.util.PW;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class AResourceBuilding extends ABuilding implements ResourceBuilding {
private final int baseInput;
private final double boostFactor;
private final ResourceType output;
private final ResourceType[] inputs;
private boolean[] continents;
private Supplier<Double> outputValue;
private InputValueFunc inputValueFunc;

public AResourceBuilding(BuildingBuilder parent, ResourceType output) {
this(parent, output.getBaseInput(), output.getBoostFactor(), output, output.getInputs());
Expand All @@ -28,6 +33,52 @@ public AResourceBuilding(BuildingBuilder parent, int baseInput, double boostFact
this.boostFactor = boostFactor;
this.output = output;
this.inputs = inputs;
this.outputValue = () -> {
double value = ResourceType.convertedTotalPositive(output, 1);
outputValue = () -> value;
return value;
};
if (inputs.length == 0) {
this.inputValueFunc = (c, r, h, b, a, p) -> 0d;
} else {
this.inputValueFunc = (c0, r0, h0, b0, a0, p0) -> {
double factor = 1d / output.getManufacturingMultiplier();

InputValueFunc newFunc;
if (inputs.length == 1) {
double value = ResourceType.convertedTotalNegative(inputs[0], 1);
newFunc = (c, r, h, b, a, p) -> {
double inputAmt = factor * p;
return -value * inputAmt;
};
} else {
double[] values = new double[inputs.length];
for (int i = 0; i < inputs.length; i++) {
values[i] = ResourceType.convertedTotalNegative(inputs[i], 1);
}
newFunc = (c, r, h, b, a, p) -> {
double inputAmt = factor * p;
double profit = 0;
for (int i = 0; i < values.length; i++) {
profit -= values[i] * inputAmt;
}
return profit;
};
}
inputValueFunc = (c, r, h, b, a, p) -> {
if (p != 0) {
return newFunc.apply(c, r, h, b, a, p);
}
return 0d;
};
return newFunc.apply(c0, r0, h0, b0, a0, p0);
};

}
}

private static interface InputValueFunc {
public double apply(Continent continent, double rads, Predicate<Project> hasProjects, ICity city, int amt, double production);
}

public AResourceBuilding continents(Continent... continents) {
Expand Down Expand Up @@ -58,30 +109,9 @@ public List<ResourceType> getResourceTypesConsumed() {
@Override
public double profitConverted(Continent continent, double rads, Predicate hasProjects, ICity city, int amt) {
double profit = super.profitConverted(continent, rads, hasProjects, city, amt);

int improvements = city.getBuilding(this);


double production = output.getProduction(continent, rads, hasProjects, city.getLand(), improvements, -1);
if (production != 0) {
profit += ResourceType.convertedTotalPositive(output, production);

double inputAmt = output.getInput(continent, rads, hasProjects, city, improvements);

if (inputAmt > 0) {
switch (inputs.length) {
case 0:
break;
case 1:
profit -= ResourceType.convertedTotalNegative(inputs[0], inputAmt);
break;
default:
for (ResourceType input : inputs) {
profit -= ResourceType.convertedTotalNegative(input, inputAmt);
}
}
}
}
double production = output.getProduction(continent, rads, hasProjects, city.getLand(), amt, -1);
profit += production * outputValue.get();
profit += inputValueFunc.apply(continent, rads, hasProjects, city, amt, production);
return profit;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package link.locutus.discord.apiv1.enums.city.building.imp;

import link.locutus.discord.apiv1.domains.Nation;
import link.locutus.discord.apiv1.enums.Continent;
import link.locutus.discord.apiv1.enums.ResourceType;
import link.locutus.discord.apiv1.enums.city.ICity;
import link.locutus.discord.apiv1.enums.city.building.Building;
import link.locutus.discord.apiv1.enums.city.project.Project;
import link.locutus.discord.apiv1.enums.city.project.Projects;
Expand Down
Loading

0 comments on commit 54e4b4f

Please sign in to comment.