Skip to content

Commit

Permalink
-Set initial capacity for all the lists used for passing arguments to…
Browse files Browse the repository at this point in the history
… custom commands to reduce the rate of garbage generation.

-Inverted the loops in arraygraphics and bytearraygraphics to run row major.
-More fiddling with eco.
-Switch on/off state to use the custom command.
  • Loading branch information
aeonios committed Nov 14, 2019
1 parent 5885b8d commit d5bca36
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 115 deletions.
21 changes: 2 additions & 19 deletions src/zkgbai/ZKGraphBasedAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,7 @@ public int luaMessage(java.lang.String inData){

@Override
public int update(int frame) {
if (slave) {
/*if (frame % 30 == 0) {
// give away income from communism when merged with another AI instance.
Resource metal = callback.getResources().get(0);
Resource energy = callback.getResources().get(1);
callback.getEconomy().sendResource(metal, callback.getEconomy().getIncome(metal), mergeTarget);
callback.getEconomy().sendResource(energy, callback.getEconomy().getIncome(energy), mergeTarget);
}*/
return 0;
}
if (slave) return 0;

if (frame == 0){
say("glhf!");
Expand Down Expand Up @@ -302,15 +293,7 @@ public int update(int frame) {
@Override
public int message(int player, String message) {
if (!slave && message.equals("kgbdebug")){
for (Enemy e: warManager.getTargets()){
if (e.isPorc){
marker(e.position, e.ud.getHumanName());
}
}
/*for (Worker w:ecoManager.workers.values()){
//marker(w.getPos(), w.getTask() == null ? "no task" : w.getTask().toString());
marker(w.getPos(), w.isGreedy ? "greedy" : "non-greedy");
}*/
say("Mobile BP: " + facManager.mobileBP + " Income: " + ecoManager.effectiveIncomeMetal + " Income + Reclaim Bonus: " + (ecoManager.effectiveIncomeMetal + (ecoManager.getReclaimValue() / ((2f + 20f * graphManager.territoryFraction) * 25f))));
}

for (Module module : modules) {
Expand Down
16 changes: 7 additions & 9 deletions src/zkgbai/economy/EconomyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public int update(int frame) {
adjustedIncome = (staticIncome/(1 + (ai.mergedAllies))) + ((1f + ai.mergedAllies) * (frame/1800f));

if (fusions.size() > 3) {
ArrayList<Float> params = new ArrayList<>();
ArrayList<Float> params = new ArrayList<>(1);
if (!fusionTasks.isEmpty()) {
params.add(1f);
for (Unit u : superWeps) {
Expand Down Expand Up @@ -335,7 +335,8 @@ public int update(int frame) {
}
}
}
float eratio = (eFac ? 1.5f : 1f) + (graphManager.territoryFraction + (graphManager.territoryFraction * graphManager.territoryFraction)) / 2f;
float eratio = 2f + fusions.size();
//float eratio = (eFac ? 1.5f : 1f) + (graphManager.territoryFraction + (graphManager.territoryFraction * graphManager.territoryFraction)) / 2f;
hardEstalled = (maxStorage > 1f && energy < Math.min(effectiveIncomeEnergy * (1f + graphManager.territoryFraction), maxStorage/2f)) || staticIncome > effectiveIncomeEnergy;
estalled = hardEstalled || rawMexIncome * eratio > effectiveIncomeEnergy;
if (maxStorage > 1f && hardEstalled && metal/maxStorage > 0.5f) energyReclaim = true;
Expand Down Expand Up @@ -822,7 +823,7 @@ public int unitIdle(Unit u) {
if (newWorkers.contains(u.getUnitId()) && u.getCurrentCommands().isEmpty()){
Worker w = new Worker(u);
workers.put(w.id, w);
if (workers.size() > 1 + ai.mergedAllies && (greed < 2 || greed < ((workers.size() - commanders.size())) * Math.min(1f - graphManager.territoryFraction, 1f))){
if (workers.size() > 1 + ai.mergedAllies && (greed < 2 || greed < (workers.size() - commanders.size()) * Math.min(1f - graphManager.territoryFraction, 1f))){
w.isGreedy = true;
greed++;
}
Expand Down Expand Up @@ -885,7 +886,7 @@ private void checkNanos(){
}

private void setEcoPriorities(){
List<Float> params = new ArrayList<>();
List<Float> params = new ArrayList<>(1);
if (!hardEstalled && !graphManager.eminentTerritory && adjustedIncome < 20f){
params.add(2f);
}else {
Expand Down Expand Up @@ -2429,7 +2430,7 @@ private void morphSparrow(){
if (best != null){
sparrow = best;
radars.remove(sparrow);
List<Float> params = new ArrayList<>();
List<Float> params = new ArrayList<>(1);
params.add((float) callback.getUnitDefByName("planelightscout").getUnitDefId());
sparrow.executeCustomCommand(CMD_MORPH, params, (short) 0, Integer.MAX_VALUE);
params.clear();
Expand All @@ -2447,7 +2448,7 @@ void createEnergyTask(Worker worker){
ConstructionTask ct;

// for solars
if (((solars.size() + solarTasks.size()) < Math.round(rawMexIncome/2f) || Math.random() > 0.35)
if (((solars.size() + solarTasks.size()) < Math.round(rawMexIncome/2f) || Math.random() > 0.25)
&& callback.getMap().getElevationAt(position.x, position.z) > 0){
if (position == null){
return;
Expand Down Expand Up @@ -2831,9 +2832,6 @@ public int compare(Feature o1, Feature o2) {
}

public float getReclaimValue(){
if (effectiveIncome < 20f){
return 0f;
}
if (lastRCValueFrame == 0 || frame - lastRCValueFrame > 300){
reclaimValue = 0;
lastRCValueFrame = frame;
Expand Down
3 changes: 2 additions & 1 deletion src/zkgbai/economy/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public Factory(Unit u, boolean firstFac){
}

if (defName.equals("factorytank")){
scoutAllowance = 2;
scoutAllowance = 1;
raiderSpam = 0;
}

if (defName.equals("factorygunship")){
Expand Down
46 changes: 25 additions & 21 deletions src/zkgbai/economy/FactoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ public int unitFinished(Unit unit) {
newUnits.remove(unit.getUnitId());
String defName = unit.getDef().getName();
if (!unitTypes.AAs.contains(defName)){
fighterValue += Math.min(unit.getDef().getCost(m), unitTypes.heavies.contains(defName) ? 500f: 350f)/(unitTypes.arties.contains(defName) ? 2f : 1f);
//fighterValue += Math.min(unit.getDef().getCost(m), unitTypes.heavies.contains(defName) ? 500f: 350f)/(unitTypes.arties.contains(defName) ? 2f : 1f);
fighterValue += unit.getDef().getCost(m);
}
armyValue += unit.getDef().getCost(m)/2f;
}
Expand Down Expand Up @@ -453,7 +454,8 @@ public int unitDestroyed(Unit unit, Unit attacker) {
newUnits.remove(unit.getUnitId());
armyValue -= unit.getDef().getCost(m)/2f;
}else if (def.getSpeed() > 0 && !def.isAbleToFly() && !unitTypes.AAs.contains(defName)){
fighterValue -= Math.min(unit.getDef().getCost(m), unitTypes.heavies.contains(defName) ? 500f: 350f)/(unitTypes.arties.contains(defName) ? 2f : 1f);
//fighterValue -= Math.min(unit.getDef().getCost(m), unitTypes.heavies.contains(defName) ? 500f: 350f)/(unitTypes.arties.contains(defName) ? 2f : 1f);
fighterValue -= unit.getDef().getCost(m);
armyValue -= unit.getDef().getCost(m);
}else if (unitTypes.AAs.contains(defName)){
numAAs--;
Expand Down Expand Up @@ -731,15 +733,12 @@ private Boolean needWorkers(Factory fac){

float reclaimValue = economyManager.getReclaimValue();

//float territoryMod = Math.min(0.25f, graphManager.territoryFraction/2f);
//float fv = (fighterValue * (facType == "factorytank" ? Math.min(0.6f, 1.25f * graphManager.territoryFraction) : Math.min(0.5f, graphManager.territoryFraction))) + Math.min(fighterValue * territoryMod, reclaimValue/2f);
float income = economyManager.effectiveIncomeMetal /*+ Math.min(20f, reclaimValue/(2f * fac.costPerBP)) + (5f * economyManager.fusions.size())*/;
float income = economyManager.effectiveIncomeMetal + (reclaimValue / ((2f + 20f * graphManager.territoryFraction) * fac.costPerBP));
int workerDeficit = Math.round((income - mobileBP)/fac.workerBP);

if (workerDeficit > 0 &&
(fighterValue > workerValue || numWorkers == 0
|| (economyManager.adjustedIncome > 15f && economyManager.metal/economyManager.maxStorage > 0.8f && economyManager.energy/economyManager.maxStorage > 0.5f && economyManager.maxStorage > 0.5f && workerDeficit > -3))
|| (earlyWorker && numWorkers < 2)
if ((workerDeficit > 0 && fighterValue > workerValue || numWorkers == 0)
|| (economyManager.adjustedIncome > 15f && economyManager.metal/economyManager.maxStorage > 0.8f /*&& economyManager.energy/economyManager.maxStorage > 0.5f */&& economyManager.maxStorage > 0.5f && workerDeficit > -1)
|| (earlyWorker && numWorkers < 2 * (1 + ai.mergedAllies))
) {
return true;
}else if (((fac.raiderSpam >= 0 && scoutBudget == 0 && fac.expensiveRaiderSpam >= 0) && numWorkers < 2 * (1 + ai.mergedAllies))){
Expand Down Expand Up @@ -773,7 +772,7 @@ private String getCloaky(Factory fac) {
return "cloakheavyraid";
}

if (fighterValue > AAvalue && Math.random() < Math.min(1f, graphManager.territoryFraction * 2) * (1f - AAvalue/warManager.maxEnemyAirValue)){
if (fighterValue > AAvalue && Math.random() > (AAvalue/warManager.maxEnemyAirValue) * (AAvalue/warManager.maxEnemyAirValue)){
return "cloakaa";
}

Expand Down Expand Up @@ -860,7 +859,7 @@ private String getShields(Factory fac) {

if (fac.smallRaiderSpam) fac.smallRaiderSpam = false;

if (fighterValue > AAvalue && Math.random() < Math.min(1f, graphManager.territoryFraction * 2f) * (1f - AAvalue/warManager.maxEnemyAirValue)){
if (fighterValue > AAvalue && Math.random() > (AAvalue/warManager.maxEnemyAirValue) * (AAvalue/warManager.maxEnemyAirValue)){
return "shieldaa";
}

Expand Down Expand Up @@ -922,7 +921,7 @@ private String getAmphs(Factory fac) {
return "amphraid";
}

if (fighterValue > AAvalue && Math.random() < Math.min(1f, graphManager.territoryFraction * 2f) * (1f - AAvalue/warManager.maxEnemyAirValue)){
if (fighterValue > AAvalue && Math.random() > (AAvalue/warManager.maxEnemyAirValue) * (AAvalue/warManager.maxEnemyAirValue)){
return "amphaa";
}

Expand Down Expand Up @@ -998,7 +997,7 @@ private String getLV(Factory fac) {
return "vehcapture";
}

if (fighterValue > AAvalue && Math.random() < Math.min(1f, graphManager.territoryFraction * 2f) * (1f - AAvalue/warManager.maxEnemyAirValue)){
if (fighterValue > AAvalue && Math.random() > (AAvalue/warManager.maxEnemyAirValue) * (AAvalue/warManager.maxEnemyAirValue)){
return "vehaa";
}

Expand Down Expand Up @@ -1060,16 +1059,16 @@ private String getHovers(Factory fac) {
return "hoverarty";
}

if (fighterValue > AAvalue && Math.random() < Math.min(1f, graphManager.territoryFraction * 2) * (1f - AAvalue/warManager.maxEnemyAirValue)){
if (fighterValue > AAvalue && Math.random() > (AAvalue/warManager.maxEnemyAirValue) * (AAvalue/warManager.maxEnemyAirValue)){
return "hoveraa";
}

if (!highprio && Math.random() > (warManager.sniperSightings.isEmpty() ? 0.9 + Math.min(0.05f, graphManager.territoryFraction/10f) : 0.85)){
fac.raiderSpam -= Math.min(8, Math.max(4, (int) Math.floor(economyManager.adjustedIncome/5f)));
}
if (Math.random() > 0.85) {
/*if (Math.random() > 0.85) {
fac.expensiveRaiderSpam -= (int) Math.min(6, Math.max(2, Math.floor(economyManager.adjustedIncome / 10f))) + 1;
}
}*/

if (Math.random() < graphManager.territoryFraction){
fac.expensiveRaiderSpam--;
Expand Down Expand Up @@ -1103,15 +1102,20 @@ private String getTanks(Factory fac) {
return "tankraid";
}

if (!fac.smallRaiderSpam){
fac.smallRaiderSpam = true;
fac.expensiveRaiderSpam = -5;
}

if (fac.expensiveRaiderSpam < 0){
fac.expensiveRaiderSpam++;
if (Math.random() > 0.5) fac.scoutAllowance = Math.min(fac.maxScoutAllowance, fac.scoutAllowance + 1);
//if (Math.random() > 0.5) fac.scoutAllowance = Math.min(fac.maxScoutAllowance, fac.scoutAllowance + 1);
return "tankheavyraid";
}

fac.scoutAllowance = Math.min(fac.maxScoutAllowance, fac.scoutAllowance + 1);

if (fighterValue > AAvalue && Math.random() < Math.min(1f, graphManager.territoryFraction * 2) * (1f - AAvalue/warManager.maxEnemyAirValue)){
if (fighterValue > AAvalue && Math.random() > (AAvalue/warManager.maxEnemyAirValue) * (AAvalue/warManager.maxEnemyAirValue)){
return "tankaa";
}

Expand All @@ -1127,7 +1131,7 @@ private String getTanks(Factory fac) {
double rand = Math.random();
if (economyManager.adjustedIncome < 35 && !graphManager.eminentTerritory) {
fac.expensiveRaiderSpam -= 2;
if (numBanishers > numReapers || warManager.enemyHeavyPorcValue > 850 * numReapers){
if (numBanishers > numReapers || warManager.enemyHeavyPorcValue > 850f * numReapers){
fac.expensiveRaiderSpam--;
return "tankassault";
}else {
Expand Down Expand Up @@ -1167,7 +1171,7 @@ private String getSpiders(Factory fac) {
return "spiderscout";
}

if (fighterValue > AAvalue && Math.random() < Math.min(1f, graphManager.territoryFraction * 2) * (1f - AAvalue/warManager.maxEnemyAirValue)){
if (fighterValue > AAvalue && Math.random() > (AAvalue/warManager.maxEnemyAirValue) * (AAvalue/warManager.maxEnemyAirValue)){
return "spideraa";
}

Expand Down Expand Up @@ -1215,7 +1219,7 @@ private String getGunship(Factory fac){
return "gunshipcon";
}

if (graphManager.eminentTerritory && fighterValue > AAvalue && Math.random() < Math.min(1f, graphManager.territoryFraction * 2) * (1f - AAvalue/warManager.maxEnemyAirValue)){
if (graphManager.eminentTerritory && fighterValue > AAvalue && Math.random() > (AAvalue/warManager.maxEnemyAirValue) * (AAvalue/warManager.maxEnemyAirValue)){
return "gunshipaa";
}

Expand Down
34 changes: 18 additions & 16 deletions src/zkgbai/kgbutil/ArrayGraphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ public void paintCircle(int cx, int cy, int radius, int intensity){

int radsq = radius * radius;

for (int x = beginX; x < endX; x++) {
int dX = (cx - x);
int dxSq = dX*dX;

for (int y = beginY; y < endY; y++) {
int dY = (cy - y);
int dySq = dY*dY;

for (int y = beginY; y < endY; y++) {
int dY = (cy - y);
int dySq = dY*dY;
int ypos = (y * width);

for (int x = beginX; x < endX; x++) {
int dX = (cx - x);
int dxSq = dX*dX;

int sum = dxSq + dySq;
int index = (y * width) + x;
int index = ypos + x;

if(sum <= radsq) {
data[index] += intensity;
Expand All @@ -59,16 +60,17 @@ public void unpaintCircle(int cx, int cy, int radius, int intensity){

int radsq = radius * radius;

for (int x = beginX; x < endX; x++) {
int dX = (cx - x);
int dxSq = dX*dX;
for (int y = beginY; y < endY; y++) {
int dY = (cy - y);
int dySq = dY*dY;
int ypos = (y * width);

for (int y = beginY; y < endY; y++) {
int dY = (cy - y);
int dySq = dY*dY;
for (int x = beginX; x < endX; x++) {
int dX = (cx - x);
int dxSq = dX*dX;

int sum = dxSq + dySq;
int index = (y * width) + x;
int index = ypos + x;

if(sum <= radsq) {
data[index] = (short) Math.max(data[index] - intensity, 0);
Expand Down
30 changes: 16 additions & 14 deletions src/zkgbai/kgbutil/ByteArrayGraphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ public void paintCircle(int cx, int cy, int radius, int intensity){

int radsq = radius * radius;

for (int x = beginX; x < endX; x++) {
int dX = (cx - x);
int dxSq = dX*dX;
for (int y = beginY; y < endY; y++) {
int dY = (cy - y);
int dySq = dY*dY;
int ypos = (y * width);

for (int y = beginY; y < endY; y++) {
int dY = (cy - y);
int dySq = dY*dY;
for (int x = beginX; x < endX; x++) {
int dX = (cx - x);
int dxSq = dX*dX;

int sum = dxSq + dySq;
int index = (y * width) + x;
int index = ypos + x;

if(sum <= radsq) {
data[index] += intensity;
Expand All @@ -59,16 +60,17 @@ public void unpaintCircle(int cx, int cy, int radius, int intensity){

int radsq = radius * radius;

for (int x = beginX; x < endX; x++) {
int dX = (cx - x);
int dxSq = dX*dX;
for (int y = beginY; y < endY; y++) {
int dY = (cy - y);
int dySq = dY*dY;
int ypos = (y * width);

for (int y = beginY; y < endY; y++) {
int dY = (cy - y);
int dySq = dY*dY;
for (int x = beginX; x < endX; x++) {
int dX = (cx - x);
int dxSq = dX*dX;

int sum = dxSq + dySq;
int index = (y * width) + x;
int index = ypos + x;

if(sum <= radsq) {
data[index] = (byte) Math.max(data[index] - intensity, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/zkgbai/kgbutil/TerrainAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void populateFacList(){
if (veh.result){
log(taMsg + "Veh path check succeeded, enabling veh!");
initialFacList.add("factoryveh");
//if (veh.avgCostRatio < 1.2f) initialFacList.add("factorytank"); // fuck tanks. Kodachi nerf and dart buff have made them unplayable.
if (veh.avgCostRatio < 1.2f) initialFacList.add("factorytank"); // fuck tanks. Kodachi nerf and dart buff have made them unplayable.
initialFacList.add("factoryhover");
}

Expand Down
Loading

0 comments on commit d5bca36

Please sign in to comment.