Skip to content

Commit

Permalink
-Reduced the size of influence maps by 4x in each dimension, reducing…
Browse files Browse the repository at this point in the history
… memory usage and threatmap update times by 16x.

-Replaced cached drawing for enemy porc with real-time updates so that raiders won't run away from 1hp llts.
-Various other minor tweaks
  • Loading branch information
aeonios committed Aug 4, 2016
1 parent d8f2023 commit 4112848
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 180 deletions.
7 changes: 6 additions & 1 deletion src/zkgbai/ZKGraphBasedAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import zkgbai.economy.EconomyManager;
import zkgbai.economy.FactoryManager;
import zkgbai.economy.tasks.ConstructionTask;
import zkgbai.graph.GraphManager;
import zkgbai.graph.MetalSpot;
import zkgbai.gui.DebugView;
Expand Down Expand Up @@ -191,7 +192,11 @@ public int update(int frame) {

@Override
public int message(int player, String message) {
for (Module module : modules) {
/*if (message.equals("kgbdebug")){
//add debugging code here
}*/

for (Module module : modules) {
try {
module.message(player, message);
} catch (Throwable e) {
Expand Down
40 changes: 25 additions & 15 deletions src/zkgbai/economy/EconomyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class EconomyManager extends Module {
List<ConstructionTask> windTasks;
List<ConstructionTask> fusionTasks;
List<ConstructionTask> pylonTasks;
List<ConstructionTask> porcTasks;
public List<ConstructionTask> porcTasks;
List<ConstructionTask> nanoTasks;
List<ConstructionTask> AATasks;
List<ConstructionTask> airpadTasks;
Expand Down Expand Up @@ -225,7 +225,7 @@ public int update(int frame) {
// remove finished or invalidated tasks
cleanOrders();

if (effectiveIncome > 30){
if (defendedFac){
defendLinks();
}
}
Expand All @@ -251,7 +251,7 @@ public int update(int frame) {
}
}

if (frame % 3 == 0) {
if (frame % 6 == 0) {
assignWorkers(); // assign workers to tasks
}

Expand Down Expand Up @@ -339,7 +339,8 @@ public int unitDamaged(Unit h, Unit attacker, float damage, AIFloat3 dir, Weapon
}

// defender push against enemy porc
if (attacker != null && attacker.getMaxSpeed() == 0 && h.getMaxSpeed() > 0 && frame - lastPorcPushFrame > 15) {
if (attacker != null && attacker.getMaxSpeed() == 0 && h.getMaxSpeed() > 0
&& frame - lastPorcPushFrame > 15 && graphManager.isAllyTerritory(h.getPos())) {
lastPorcPushFrame = frame;
porcPush(h, attacker);
}
Expand Down Expand Up @@ -608,7 +609,7 @@ public int enemyEnterLOS(Unit e){
}

// porc over enemy mexes
if (e.getDef().getName().equals("cormex")){
if (e.getDef().getName().equals("cormex") && graphManager.isAllyTerritory(e.getPos())){
fortifyMex(e.getPos());
}

Expand Down Expand Up @@ -976,6 +977,10 @@ void cleanOrders(){
idlers.addAll(idle);
invalidtasks.add(rt);
}
}else{
List<Worker> idle = rt.stopWorkers(frame);
idlers.addAll(idle);
invalidtasks.add(rt);
}
}
reclaimTasks.removeAll(invalidtasks);
Expand Down Expand Up @@ -1097,7 +1102,7 @@ void generateTasks(Worker worker){

float eRatio;
if (effectiveIncomeMetal > 25f && eFac){
eRatio = 1.5f + (((float)fusions.size())/2f);
eRatio = 2.0f + (((float)fusions.size())/2f);
}else if (effectiveIncomeMetal > 20f || eFac) {
eRatio = 1.3f + (((float)fusions.size())/2f);
}else{
Expand Down Expand Up @@ -1245,7 +1250,7 @@ void createFactoryTask(Worker worker){
}

/*if (facManager.factories.size() == 0){
String facName = "factoryshield";
String facName = "factorycloak";
factory = callback.getUnitDefByName(facName);
potentialFacList.remove(facName);
}else*/ if ((facManager.factories.size() < 2 && !hasShields) || hasStriders) {
Expand Down Expand Up @@ -1463,14 +1468,16 @@ void defendLink(AIFloat3 position){
porcTasks.add(ct);
}

for (int i = 0; i < 2; i++) {
position = getRadialPoint(position, 100f);
position = callback.getMap().findClosestBuildSite(defender, position, 600f, 3, 0);
if (effectiveIncome > 20) {
for (int i = 0; i < 2; i++) {
position = getRadialPoint(position, 100f);
position = callback.getMap().findClosestBuildSite(defender, position, 600f, 3, 0);

ct = new ConstructionTask(defender, position, 0);
if (buildCheck(ct) && !porcTasks.contains(ct)) {
constructionTasks.add(ct);
porcTasks.add(ct);
ct = new ConstructionTask(defender, position, 0);
if (buildCheck(ct) && !porcTasks.contains(ct)) {
constructionTasks.add(ct);
porcTasks.add(ct);
}
}
}
}
Expand Down Expand Up @@ -1667,7 +1674,7 @@ void captureMexes(){

void defendLinks(){
for (Link l:graphManager.getLinks()){
if (l.length < 600){
if (l.length < 600 && graphManager.isAllyTerritory(l.getPos())){
defendLink(l.getPos());
}
}
Expand Down Expand Up @@ -1930,6 +1937,9 @@ Worker getCaretakerTarget(){
void collectReclaimables(){
List<Feature> feats = callback.getFeatures();
for (Feature f : feats) {
if (reclaimTasks.size() > facManager.numWorkers){
break;
}
if (f.getDef().getContainedResource(m) > 0){
ReclaimTask rt = new ReclaimTask(f, f.getDef().getContainedResource(m));
if (!reclaimTasks.contains(rt)){
Expand Down
5 changes: 5 additions & 0 deletions src/zkgbai/economy/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public Factory(Unit u, boolean firstFac){
raiderSpam = -4;
}

if (u.getDef().getName().equals("factoryhover")){
raiderSpam = -4;
expensiveRaiderSpam = -2;
}

if (u.getDef().getName().equals("factoryspider")){
raiderSpam = -9;
}
Expand Down
31 changes: 24 additions & 7 deletions src/zkgbai/economy/FactoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class FactoryManager extends Module {
boolean enemyHasAmphs = false;
boolean enemyHasHeavyPorc = false;

boolean glaiveSpam = false;

public int numWorkers = 0;
float workerValue = 0;
float fighterValue = 0;
Expand Down Expand Up @@ -138,7 +140,7 @@ public int unitCreated(Unit unit, Unit builder) {
}else{
fac = new Factory(unit, false);
}
if (defName.equals("factoryspider") || defName.equals("factoryamph") || defName.equals("factoryveh")){
if (defName.equals("factoryspider") || defName.equals("factoryamph") || defName.equals("factoryveh") || defName.equals("factoryhover")){
earlyWorker = true;
}
factories.put(fac.id, fac);
Expand Down Expand Up @@ -481,8 +483,12 @@ private Boolean needWorkers(Factory fac){
float metalratio = 4.0f;
float workerRatio;

if (facType.equals("factorytank") /*|| facType.equals("factoryamph") || facType.equals("factoryspider")*/){
workerRatio = 2f + (economyManager.effectiveIncome/200f);
if (facType.equals("factorytank")){
if (bigMap) {
workerRatio = 1.5f;
}else{
workerRatio = 2f;
}
}else if (bigMap){
workerRatio = 1.5f + (economyManager.effectiveIncome/100f);
}else{
Expand All @@ -498,6 +504,7 @@ private Boolean needWorkers(Factory fac){
if ((float) numWorkers < Math.floor(economyManager.effectiveIncomeMetal/metalratio) + ((warManager.miscHandler.striders.size() + economyManager.fusions.size() + numFunnels + factories.size() - 1) * 3)
&& (fighterValue > workerValue * workerRatio || numWorkers == 0 || (economyManager.metal > 400 && economyManager.energy > 100))
|| (earlyWorker && numWorkers < 2)
|| (bigMap && numWorkers < 3)
|| (fac.getUnit().getDef().getName().equals("factoryshield") && !smallMap && numWorkers < 2)) {
return true;
}else if ((fac.raiderSpam >= 0 && numWorkers < 3 && !facType.equals("factorytank"))
Expand All @@ -522,7 +529,7 @@ private String getCloaky(Factory fac) {
fac.expensiveRaiderSpam++;
return "spherepole";
}
if ((economyManager.effectiveIncome > 15 && numGlaives > 1 && Math.random() > 0.9)) {
if ((!glaiveSpam && economyManager.effectiveIncome > 15 && numGlaives > 1 && Math.random() > 0.9)) {
fac.expensiveRaiderSpam -= (int) Math.min(4, Math.max(2, Math.floor(economyManager.effectiveIncome / 10))) - 1;
return "spherepole";
} else {
Expand All @@ -533,6 +540,10 @@ private String getCloaky(Factory fac) {
}
}

if (glaiveSpam){
glaiveSpam = false;
}

if (((enemyHasAir || enemyHasDrones) && warManager.AAs.size() < 3) ||
(enemyHasAir && Math.random() > 0.70 + (0.05 * ((float) factories.size()-1f)))){
return "armjeth";
Expand All @@ -546,6 +557,11 @@ private String getCloaky(Factory fac) {
return "armtick";
}

if (economyManager.effectiveIncome > 30 && Math.random() > 0.975){
glaiveSpam = true;
fac.raiderSpam -= Math.min(12, Math.max(6, (int) Math.floor(economyManager.effectiveIncome/5f)));
}

double rand = Math.random();
if (economyManager.fusions.isEmpty()){
if (numRockos < 2 * (numWarriors + numZeus)) {
Expand Down Expand Up @@ -691,7 +707,8 @@ private String getAmphs(Factory fac) {
}

private String getLV(Factory fac) {
if (earlyWorker && needWorkers(fac)) {
if (numWorkers < 2
|| (bigMap && numWorkers < 3)) {
return "corned";
}

Expand All @@ -707,7 +724,7 @@ private String getLV(Factory fac) {
return "corgator";
}

if (!earlyWorker && needWorkers(fac)) {
if (needWorkers(fac)) {
return "corned";
}

Expand Down Expand Up @@ -771,7 +788,7 @@ private String getHovers(Factory fac) {
}

if (fac.raiderSpam < 0) {
if (fac.expensiveRaiderSpam < 0){
if (fac.expensiveRaiderSpam < 0 && warManager.raiderHandler.soloRaiders.size() > 1){
fac.expensiveRaiderSpam++;
return "hoverassault";
}
Expand Down
46 changes: 23 additions & 23 deletions src/zkgbai/graph/GraphManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public GraphManager(ZKGraphBasedAI ai){
pylonDefs.put("armfus", 150);
pylonDefs.put("cafus", 150);

final int width = callback.getMap().getWidth();
final int height = callback.getMap().getHeight();
final int width = callback.getMap().getWidth()/4;
final int height = callback.getMap().getHeight()/4;

this.frontLineGraphics = new ByteArrayGraphics(width, height);
this.allyTerritoryGraphics = new ByteArrayGraphics(width, height);
Expand Down Expand Up @@ -302,10 +302,10 @@ public int update(int uframe){
List<MetalSpot> frontLine = getFrontLineSpots();
for (MetalSpot ms: frontLine){
AIFloat3 pos = ms.getPos();
int x = (int) pos.x/8;
int y = (int) pos.z/8;
int x = (int) pos.x/32;
int y = (int) pos.z/32;

frontLineGraphics.paintCircle(x, y, 75, 1);
frontLineGraphics.paintCircle(x, y, 20, 1);
}

calcCenters();
Expand All @@ -320,10 +320,10 @@ private void setHostile(MetalSpot ms){

// paint territory circles
AIFloat3 pos = ms.getPos();
int x = (int) pos.x/8;
int y = (int) pos.z/8;
int x = (int) pos.x/32;
int y = (int) pos.z/32;

enemyTerritoryGraphics.paintCircle(x, y, 100, 1);
enemyTerritoryGraphics.paintCircle(x, y, 25, 1);

// set adjacent spots as enemyShadowed if they aren't already hostile
for (Link l:ms.links){
Expand All @@ -343,10 +343,10 @@ private void setOwned(MetalSpot ms){

// paint territory circles
AIFloat3 pos = ms.getPos();
int x = (int) pos.x/8;
int y = (int) pos.z/8;
int x = (int) pos.x/32;
int y = (int) pos.z/32;

allyTerritoryGraphics.paintCircle(x, y, 200, 1);
allyTerritoryGraphics.paintCircle(x, y, 50, 1);

// set adjacent spots as allyShadowed if they aren't already owned
for (Link l:ms.links){
Expand All @@ -365,20 +365,20 @@ private void setNeutral(MetalSpot ms){

// unpaint territory circles
AIFloat3 pos = ms.getPos();
int x = (int) pos.x/8;
int y = (int) pos.z/8;
int x = (int) pos.x/32;
int y = (int) pos.z/32;

allyTerritoryGraphics.unpaintCircle(x, y, 200, 1);
allyTerritoryGraphics.unpaintCircle(x, y, 50, 1);
}
if (ms.hostile){
ms.enemyShadowed = true;

// unpaint territory circles
AIFloat3 pos = ms.getPos();
int x = (int) pos.x/8;
int y = (int) pos.z/8;
int x = (int) pos.x/32;
int y = (int) pos.z/32;

enemyTerritoryGraphics.unpaintCircle(x, y, 100, 1);
enemyTerritoryGraphics.unpaintCircle(x, y, 25, 1);
}
ms.hostile = false;
ms.owned = false;
Expand Down Expand Up @@ -604,22 +604,22 @@ public List<Link> getLinks(){
}

public boolean isAllyTerritory(AIFloat3 position){
int x = (int) (position.x/8);
int y = (int) (position.z/8);
int x = (int) (position.x/32);
int y = (int) (position.z/32);

return (allyTerritoryGraphics.getValue(x, y) > 0);
}

public boolean isEnemyTerritory(AIFloat3 position){
int x = (int) (position.x/8);
int y = (int) (position.z/8);
int x = (int) (position.x/32);
int y = (int) (position.z/32);

return (enemyTerritoryGraphics.getValue(x, y) > 0);
}

public boolean isFrontLine(AIFloat3 position){
int x = (int) (position.x/8);
int y = (int) (position.z/8);
int x = (int) (position.x/32);
int y = (int) (position.z/32);

return (frontLineGraphics.getValue(x, y) > 0);
}
Expand Down
10 changes: 6 additions & 4 deletions src/zkgbai/military/Enemy.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Enemy {
float threatRadius = 0;
public float value = 0;
float speed = 0;
float lastHealth = 0;
public int lastSeen = 0;
public boolean isPainted = false;
boolean visible = false;
Expand Down Expand Up @@ -151,13 +152,14 @@ public float getDanger(){
return 0;
}

if (!isStatic) {
if (unit.getHealth() > 0) {
health = unit.getHealth();
danger = (ud.getPower() + health)/10;
} else {
}else if (lastHealth > 0){
health = lastHealth;
}else{
health = ud.getHealth();
danger = (ud.getPower() + health)/10;
}
danger = (ud.getPower() + health)/10;

if (isFlamer || ud.getName().equals("arm_venom") || ud.getName().equals("amphraider2")){
danger += 100;
Expand Down
Loading

0 comments on commit 4112848

Please sign in to comment.