Skip to content

Commit

Permalink
Optimize Vector for Space (#429)
Browse files Browse the repository at this point in the history
* Use direct fields over array

* Use non-capturing lambdas

* Reduce allocations for applying with symmetry

* Make vectors immutable

* Fix location options
  • Loading branch information
Sheikah45 authored Dec 14, 2024
1 parent 1409d6b commit e04d95b
Show file tree
Hide file tree
Showing 41 changed files with 804 additions and 787 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
import com.faforever.neroxis.map.Unit;
import com.faforever.neroxis.util.vector.Vector2;

import java.util.LinkedHashSet;
import java.util.SequencedMap;
import java.util.SequencedSet;
import java.util.stream.Collectors;

public record BaseTemplate(Vector2 center, SequencedMap<String, SequencedSet<Vector2>> units) {

public void addUnits(Army army, Group group) {
units().forEach((name, positions) -> positions.forEach(position -> group.addUnit(
new Unit(String.format("%s %s Unit %d", army.getId(), group.getId(), group.getUnitCount()), name,
new Vector2(position).add(center), 0))));
position.add(center), 0))));
}

public void flip(Symmetry symmetry) {
units().values().forEach(positions -> positions.forEach(position -> position.flip(new Vector2(0, 0), symmetry)));
units.entrySet()
.forEach(entry -> entry.setValue(entry.getValue()
.stream()
.map(position -> position.flip(new Vector2(0, 0), symmetry))
.collect(Collectors.toCollection(LinkedHashSet::new))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.faforever.neroxis.util.FileUtil;
import com.faforever.neroxis.util.serial.biome.SCUnitSet;
import com.faforever.neroxis.util.vector.Vector2;
import com.faforever.neroxis.util.vector.Vector3;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -18,8 +19,8 @@

public class BaseTemplateLoader {

public static final Comparator<Vector2> VECTOR_COMPARATOR = Comparator.comparing(Vector2::getX)
.thenComparing(Vector2::getY);
public static final Comparator<Vector2> VECTOR_COMPARATOR = Comparator.comparing(Vector2::x)
.thenComparing(Vector2::y);
private static final Comparator<Map.Entry<String, Vector2>> UNIT_ENTRY_COMPARATOR = Map.Entry.<String, Vector2>comparingByKey()
.thenComparing(
Map.Entry::getValue,
Expand Down Expand Up @@ -133,11 +134,10 @@ private static double extractNumber(Lua.Expression expression) {
private static SequencedMap<String, SequencedSet<Vector2>> loadUnitsFromSCUnits(InputStream inputStream) throws
IOException {
SCUnitSet scUnitSet = FileUtil.deserialize(inputStream, SCUnitSet.class);
scUnitSet.units().forEach(unit -> unit.pos().subtract(scUnitSet.center()).multiply(10f).round(2));

Vector3 center = scUnitSet.center();
return scUnitSet.units()
.stream()
.map(unit -> Map.entry(unit.ID(), new Vector2(unit.pos())))
.map(unit -> Map.entry(unit.ID(), new Vector2(unit.pos().subtract(center).multiply(10f).round(2))))
.sorted(UNIT_ENTRY_COMPARATOR)
.collect(Collectors.groupingBy(Map.Entry::getKey, LinkedHashMap::new,
Collectors.mapping(Map.Entry::getValue, Collectors.toCollection(
Expand Down
6 changes: 3 additions & 3 deletions shared/src/main/java/com/faforever/neroxis/biomes/Biomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public class Biomes {

public static Biome loadBiome(BiomeName biomeName) {
String folderPath = biomeName.getFolderName();
if (!folderPath.endsWith("/")) {
folderPath += "/";
}
if (Biomes.class.getResource(CUSTOM_BIOMES_DIR + folderPath) != null) {
folderPath = CUSTOM_BIOMES_DIR + folderPath;
if (!folderPath.endsWith("/")) {
folderPath += "/";
}
}

TerrainMaterials terrainMaterials;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ private static BufferedImage shadeLayer(BufferedImage image, SCMap map, FloatMas

float coefficient = irradiance.getPrimitive(x, y) + ambientCoefficient;

newRGBA[0] = (int) (origRGBA[0] * ((lightingSettings.sunColor().getX() * coefficient)
+ lightingSettings.sunAmbience().getX())
newRGBA[0] = (int) (origRGBA[0] * ((lightingSettings.sunColor().x() * coefficient)
+ lightingSettings.sunAmbience().x())
* lightingSettings.lightingMultiplier());
newRGBA[1] = (int) (origRGBA[1] * ((lightingSettings.sunColor().getY() * coefficient)
+ lightingSettings.sunAmbience().getY())
newRGBA[1] = (int) (origRGBA[1] * ((lightingSettings.sunColor().y() * coefficient)
+ lightingSettings.sunAmbience().y())
* lightingSettings.lightingMultiplier());
newRGBA[2] = (int) (origRGBA[2] * ((lightingSettings.sunColor().getZ() * coefficient)
+ lightingSettings.sunAmbience().getZ())
newRGBA[2] = (int) (origRGBA[2] * ((lightingSettings.sunColor().z() * coefficient)
+ lightingSettings.sunAmbience().z())
* lightingSettings.lightingMultiplier());

newRGBA[0] = StrictMath.max(StrictMath.min(newRGBA[0], 255), 0);
Expand Down Expand Up @@ -171,8 +171,8 @@ public static BufferedImage addMarkers(BufferedImage image, SCMap map) throws IO
private static void addMarkerImages(Collection<? extends Marker> markers, BufferedImage markerImage,
BufferedImage preview, SCMap map) {
markers.forEach(marker -> {
int x = (int) (marker.getPosition().getX() / map.getSize() * PREVIEW_SIZE - markerImage.getWidth(null) / 2);
int y = (int) (marker.getPosition().getZ() / map.getSize() * PREVIEW_SIZE
int x = (int) (marker.getPosition().x() / map.getSize() * PREVIEW_SIZE - markerImage.getWidth(null) / 2);
int y = (int) (marker.getPosition().z() / map.getSize() * PREVIEW_SIZE
- markerImage.getHeight(null) / 2);
if (ImageUtil.inImageBounds(x, y, preview)) {
preview.getGraphics().drawImage(markerImage, x, y, null);
Expand Down Expand Up @@ -201,8 +201,8 @@ public static void addReclaimProps(BufferedImage image, SCMap map) {
// Draw the boulder
for (Prop prop : map.getProps()) {
if (prop.isBoulder()) {
int x = (int) (prop.getPosition().getX() / map.getSize() * PREVIEW_SIZE);
int y = (int) (prop.getPosition().getZ() / map.getSize() * PREVIEW_SIZE);
int x = (int) (prop.getPosition().x() / map.getSize() * PREVIEW_SIZE);
int y = (int) (prop.getPosition().z() / map.getSize() * PREVIEW_SIZE);
if (x >= 1 && y >= 1 && x < image.getRaster().getWidth()-1 && y < image.getRaster().getHeight()-1) {
image.getRaster().setPixel(x, y, boulderRGBA);
image.getRaster().setPixel(x+1, y, addRGB(image.getRaster().getPixel(x+1,y,(int[])null), boulderRGBAOutline,0.3f));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ public static void exportPreview(Path folderPath, SCMap map) throws IOException
}

public static void exportNormals(Path folderPath, SCMap map) throws IOException {
float size = map.getPlayableArea().getW() - map.getPlayableArea().getX();
Vector2 topLeftOffset = new Vector2(map.getPlayableArea().getX(), map.getPlayableArea().getY());
float size = map.getPlayableArea().w() - map.getPlayableArea().x();
Vector2 topLeftOffset = new Vector2(map.getPlayableArea().x(), map.getPlayableArea().y());
byte[] compressedNormal = map.getCompressedNormal();
final String fileFormat = "dds";
Path decalsPath = Paths.get("env", "decals");
Expand All @@ -276,8 +276,8 @@ public static void exportNormals(Path folderPath, SCMap map) throws IOException
}

public static void exportShadows(Path folderPath, SCMap map) throws IOException {
float size = map.getPlayableArea().getW() - map.getPlayableArea().getX();
Vector2 topLeftOffset = new Vector2(map.getPlayableArea().getX(), map.getPlayableArea().getY());
float size = map.getPlayableArea().w() - map.getPlayableArea().x();
Vector2 topLeftOffset = new Vector2(map.getPlayableArea().x(), map.getPlayableArea().y());
byte[] compressedShadows = map.getCompressedShadows();
final String fileFormat = "dds";
Path decalsPath = Paths.get("env", "decals");
Expand Down Expand Up @@ -370,21 +370,21 @@ private static void writeString(String s) throws IOException {
}

private static void writeVector3f(Vector3 v) throws IOException {
writeFloat(v.getX());
writeFloat(v.getY());
writeFloat(v.getZ());
writeFloat(v.x());
writeFloat(v.y());
writeFloat(v.z());
}

private static void writeVector4f(Vector4 v) throws IOException {
writeFloat(v.getX());
writeFloat(v.getY());
writeFloat(v.getZ());
writeFloat(v.getW());
writeFloat(v.x());
writeFloat(v.y());
writeFloat(v.z());
writeFloat(v.w());
}

private static void writeVector2f(Vector2 v) throws IOException {
writeFloat(v.getX());
writeFloat(v.getY());
writeFloat(v.x());
writeFloat(v.y());
}

private static void writeProp(Prop prop) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public static void exportSave(Path folderPath, SCMap map) throws IOException {
out.writeBytes(" Props = {},\n");
out.writeBytes(" Areas = {\n");
out.writeBytes(" ['AREA_1'] = {\n");
out.writeBytes(String.format(" ['rectangle'] = RECTANGLE( %d, %d, %d, %d ),\n", (int) playableArea.getX(),
(int) playableArea.getY(), (int) playableArea.getZ(), (int) playableArea.getW()));
out.writeBytes(String.format(" ['rectangle'] = RECTANGLE( %d, %d, %d, %d ),\n", (int) playableArea.x(),
(int) playableArea.y(), (int) playableArea.z(), (int) playableArea.w()));
out.writeBytes(" },\n");
out.writeBytes(" },\n");
out.writeBytes(" MasterChain = {\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public static void exportScenario(Path folderPath, SCMap map) throws IOException
}
out.writeBytes(" norushradius = " + map.getNoRushRadius() + ",\n");
for (Spawn spawn : map.getSpawns()) {
out.writeBytes(" norushoffsetX_" + spawn.getId() + " = " + spawn.getNoRushOffset().getX() + ",\n");
out.writeBytes(" norushoffsetY_" + spawn.getId() + " = " + spawn.getNoRushOffset().getY() + ",\n");
out.writeBytes(" norushoffsetX_" + spawn.getId() + " = " + spawn.getNoRushOffset().x() + ",\n");
out.writeBytes(" norushoffsetY_" + spawn.getId() + " = " + spawn.getNoRushOffset().y() + ",\n");
}
out.writeBytes(" Configurations = {\n");
out.writeBytes(" ['standard'] = {\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void generateScript(SCMap map) {
}

private static String generateUnexploredScript(SCMap map) {
int mapPlayableSize = (int) (map.getPlayableArea().getW() - map.getPlayableArea().getX());
int mapPlayableSize = (int) (map.getPlayableArea().w() - map.getPlayableArea().x());
int decalSize = mapPlayableSize * mapPlayableSize / 8192;
int checkDecalRange = decalSize / 2 + 24;
int checkResourceRange = decalSize / 2 + 32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ private static Prop readProp() throws IOException {
readVector3f(); // Y rotation
readVector3f(); // Z rotation
readVector3f(); // scale
float rotation = (float) StrictMath.atan2(rotationX.getZ(), rotationX.getX());
float rotation = (float) StrictMath.atan2(rotationX.z(), rotationX.x());
return new Prop(path, position, rotation, false);
}

Expand Down
Loading

0 comments on commit e04d95b

Please sign in to comment.