Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed AI planetary defense building #1148

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/hu/openig/mechanics/DefaultAIControls.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package hu.openig.mechanics;

import hu.openig.core.Location;
import hu.openig.model.AIAttackMode;
import hu.openig.model.AIControls;
import hu.openig.model.AIResult;
Expand All @@ -27,6 +28,8 @@
import hu.openig.model.World;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

/**
* The default AI controls.
Expand Down Expand Up @@ -223,9 +226,16 @@ public AIResult actionPlaceBuilding(Planet planet, BuildingType buildingType) {
if (!planet.canBuild(buildingType)) {
log(p, "PlaceBuilding, Planet = %s, Type = %s, FAIL = not supported or no colony hub", planet.id, buildingType.id);
return AIResult.NO_AVAIL;
} else
if (p.money() >= buildingType.cost) {
Point pt = planet.surface.placement.findLocation(planet.getPlacementDimensions(buildingType));
} else if (p.money() >= buildingType.cost) {
Point pt;
if (buildingType.kind.equals("Gun") || buildingType.kind.equals("Shield")) {
Location loc = findLocationForPlanetaryDefenseBuilding(planet);
System.out.printf("PlaceBuilding Defensive Building, Planet = %s, Type = %s Loc: %s \n", planet.id, buildingType.id, loc);
pt = planet.surface.placement.findLocation(planet.getPlacementDimensions(buildingType), loc);
System.out.println(pt);
} else {
pt = planet.surface.placement.findLocation(planet.getPlacementDimensions(buildingType));
}
if (pt != null) {
AutoBuilder.construct(w, planet, buildingType, pt);
// log("PlaceBuilding, Planet = %s, Type = %s", planet.id, buildingType.id);
Expand All @@ -238,6 +248,22 @@ public AIResult actionPlaceBuilding(Planet planet, BuildingType buildingType) {
return AIResult.NO_MONEY;
}
}

Location findLocationForPlanetaryDefenseBuilding(Planet planet) {
for (Building b : planet.surface.buildings.findByKind("Gun")) {
return b.location();
}
for (Building b : planet.surface.buildings.findByKind("Shield")) {
return b.location();
}
ArrayList<Location> corners = new ArrayList<>();
corners.add(Location.of(0,0));
corners.add(Location.of(-planet.surface.height + 1,-planet.surface.height + 1));
corners.add(Location.of(planet.surface.width - planet.surface.height + 1, - planet.surface.height - planet.surface.width + 1));
corners.add(Location.of(planet.surface.width - 1, -planet.surface.width + 1));
return corners.get((new Random()).nextInt(4));
}

@Override
public void actionDemolishBuilding(Planet planet, Building b) {
planet.demolish(b);
Expand Down
40 changes: 33 additions & 7 deletions src/hu/openig/model/PlanetSurface.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,17 @@ public boolean canPlaceBuilding(int x, int y) {
public Point findLocation(Dimension dim) {
return findLocation(dim.width, dim.height);
}

/**
* Find a location for the given dimensions, starting from a specific point on the surface.
* <p>Note: the dimensions should incorporate 1+1 road on both axis.</p>
* @param dim the dimensions
* @param preferredLocation the point from which the location search starts from
* @return the location or null if not found
*/
public Point findLocation(Dimension dim, Location preferredLocation) {
return findLocation(dim.width, dim.height, preferredLocation);
}
/**
* Find a location on the surface which can support a building (and surrounding roads)
* with the given size. The location search starts of from the center of the map
Expand All @@ -656,11 +667,26 @@ public Point findLocation(Dimension dim) {
public Point findLocation(int width, int height) {
int cx = this.width() / 2 - this.height() / 2 - width / 2;
int cy = -this.width() / 2 - this.height() / 2 + height / 2;
return findLocation(width, height, Location.of(cx, cy));
}

int rx1 = Math.abs(this.width() - cx);
int rx2 = Math.abs(-this.height() - cx);
int ry1 = Math.abs(this.width() + this.height() + cy);
int ry2 = Math.abs(cy);
/**
* Find a location on the surface which can support a building (and surrounding roads)
* with the given size. The location search starts from the specified point on the surface.
* @param width should be the building tile width + 2
* @param height should be the building tile height + 2
* @param preferredLocation the point from which the location search starts from
* @return the top-left point where this building could be built, null indicates that
* no suitable location is present
*/
public Point findLocation(int width, int height, Location preferredLocation) {
int startX = preferredLocation.x;
int startY = preferredLocation.y;

int rx1 = Math.abs(this.width() - startX);
int rx2 = Math.abs(-this.height() - startX);
int ry1 = Math.abs(this.width() + this.height() + startY);
int ry2 = Math.abs(startY);
int maxr = Math.max(Math.max(rx1, rx2), Math.max(ry1, ry2));
// the square size
List<PlaceCandidate> candidates = new ArrayList<>();
Expand All @@ -671,10 +697,10 @@ public Point findLocation(int width, int height) {
int[] ys = new int[size];
clockwise(xs, ys, len);
for (int k = 0; k < size; k++) {
int x0 = cx + xs[k] - i;
int y0 = cy - ys[k] + i;
int x0 = startX + xs[k] - i;
int y0 = startY - ys[k] + i;
if (canPlaceBuilding(x0, y0, width, height)) {
int d = (cx - x0) * (cx - x0) + (cy - y0) * (cy - y0);
int d = (startX - x0) * (startX - x0) + (startY - y0) * (startY - y0);
PlaceCandidate pc = createCandidate(x0, y0, width, height, d);
if (pc != null) {
candidates.add(pc);
Expand Down