Skip to content

Commit

Permalink
Load ship stats from files so that system health and node health (issue
Browse files Browse the repository at this point in the history
#12) can be calculated
  • Loading branch information
Andrew Brindamour committed Jan 17, 2016
1 parent 07aef94 commit f6faf68
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package com.brindyblitz.artemis.engconsole;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.brindyblitz.artemis.protocol.NonShittyShipSystemGrid;

import net.dhleong.acl.enums.ShipSystem;
import net.dhleong.acl.util.ShipSystemGrid;
import net.dhleong.acl.vesseldata.Vessel;
import net.dhleong.acl.vesseldata.VesselData;
import net.dhleong.acl.vesseldata.VesselNode;
import net.dhleong.acl.world.Artemis;

public abstract class BaseEngineeringConsoleManager implements EngineeringConsoleManager {

private List<EngineeringConsoleChangeListener> listeners = new ArrayList<>();

protected ShipSystemGrid loadGrid() {
NonShittyShipSystemGrid grid = new NonShittyShipSystemGrid();
Vessel vessel = VesselData.get().getVessel(0);
Iterator<VesselNode> nodeIterator = vessel.getInternals().nodeIterator();
while (nodeIterator.hasNext()) {
VesselNode node = nodeIterator.next();
if (node.getSystem() != null) {
grid.addNode(node.getSystem(), node.getGridCoord());
}
}
return grid;
}

protected void fireChange() {
for (EngineeringConsoleChangeListener listener: listeners) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.brindyblitz.artemis.engconsole;

import java.util.Map;

import net.dhleong.acl.enums.ShipSystem;
import net.dhleong.acl.util.GridCoord;

public interface EngineeringConsoleManager {
int getTotalShipCoolant();
Expand All @@ -14,6 +17,8 @@ public interface EngineeringConsoleManager {
int getSystemHealth(ShipSystem system);

int getTotalCoolantRemaining();

Map<GridCoord, Float> getGridHealth();

void setSystemEnergyAllocated(ShipSystem system, int amount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.brindyblitz.artemis.engconsole.ui.SystemStatusRenderer;

import net.dhleong.acl.enums.ShipSystem;
import net.dhleong.acl.util.GridCoord;

public class FakeEngineeringConsoleManager extends BaseEngineeringConsoleManager {

Expand Down Expand Up @@ -50,6 +51,11 @@ public int getSystemHealth(ShipSystem system) {
public int getTotalCoolantRemaining() {
return MAX_COOLANT - coolantAllocated.values().stream().mapToInt(Integer::intValue).sum();
}

@Override
public Map<GridCoord, Float> getGridHealth() {
return new HashMap<>();
}

@Override
protected void updateSystemEnergyAllocated(ShipSystem system, int amount) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.brindyblitz.artemis.engconsole;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.brindyblitz.artemis.protocol.NotifyingSystemManager.SystemManagerChangeListener;
import com.brindyblitz.artemis.protocol.WorldAwareRobustProxyListener;

import net.dhleong.acl.enums.ShipSystem;
import net.dhleong.acl.protocol.core.eng.EngSetCoolantPacket;
import net.dhleong.acl.protocol.core.eng.EngSetEnergyPacket;
import net.dhleong.acl.util.GridCoord;
import net.dhleong.acl.world.Artemis;

public class RealEngineeringConsoleManager extends BaseEngineeringConsoleManager {

Expand All @@ -22,6 +27,7 @@ public void onChange() {
RealEngineeringConsoleManager.this.fireChange();
}
});
this.worldAwareRobustProxyListener.getSystemManager().setSystemGrid(loadGrid());
}

@Override
Expand Down Expand Up @@ -59,7 +65,7 @@ public int getSystemHealth(ShipSystem system) {
@Override
public int getTotalCoolantRemaining() {
if (this.worldAwareRobustProxyListener.getServer() == null) {
return 0;
return Artemis.DEFAULT_COOLANT;
}
final int totalCoolantUsed = Arrays.stream(ShipSystem.values()).mapToInt(system -> this.getSystemCoolantAllocated(system)).sum();
return getTotalShipCoolant() - totalCoolantUsed;
Expand All @@ -70,10 +76,18 @@ public int getTotalShipCoolant() {
if (this.worldAwareRobustProxyListener.getServer() == null) {
return 0;
}

return this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0).getAvailableCoolant();
}

@Override
public Map<GridCoord, Float> getGridHealth() {
Map<GridCoord, Float> result = new HashMap<>();
for (Entry<GridCoord, Float> entry : this.worldAwareRobustProxyListener.getSystemManager().getGridDamages()) {
result.put(entry.getKey(), 1.0f - entry.getValue());
}
return result;
}

@Override
public void incrementSystemEnergyAllocated(ShipSystem system, int amount) {
if (this.worldAwareRobustProxyListener.getServer() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.brindyblitz.artemis.engconsole.config.InputMapping;
import com.brindyblitz.artemis.engconsole.ui.damcon.Damcon;
import net.dhleong.acl.enums.ShipSystem;
import net.dhleong.acl.util.GridCoord;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import static net.dhleong.acl.enums.ShipSystem.*;

Expand Down Expand Up @@ -106,6 +108,10 @@ public void keyPressed(KeyEvent e) {
System.out.println("Warp: " + this.engineeringConsoleManager.getSystemEnergyAllocated(WARP_JUMP_DRIVE) + "%");
System.out.println("Front Shields: " + this.engineeringConsoleManager.getSystemEnergyAllocated(FORE_SHIELDS) + "%");
System.out.println("Rear Shields: " + this.engineeringConsoleManager.getSystemEnergyAllocated(AFT_SHIELDS) + "%");

for (Entry<GridCoord, Float> entry : this.engineeringConsoleManager.getGridHealth().entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}

System.out.println("\n\n\n");
} else if (kc == KeyEvent.VK_BACK_QUOTE) {
Expand Down
66 changes: 66 additions & 0 deletions src/com/brindyblitz/artemis/protocol/NonShittyShipSystemGrid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.brindyblitz.artemis.protocol;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import net.dhleong.acl.enums.ShipSystem;
import net.dhleong.acl.util.GridCoord;
import net.dhleong.acl.util.ShipSystemGrid;

public class NonShittyShipSystemGrid extends ShipSystemGrid {

public static final int GRID_SIZE_X = 5;
public static final int GRID_SIZE_Y = 5;
public static final int GRID_SIZE_Z = 10;


public void addNode(ShipSystem system, GridCoord coord) {
GridEntry entry = createGridEntry(system, getSystemCount(system));
addEntry(coord, entry);
incrementSystemCount(system);
}



private void incrementSystemCount(ShipSystem system){
try {
Field field = this.getClass().getSuperclass().getDeclaredField("mSystemCounts");
field.setAccessible(true);
int[] mSystemCounts = (int[]) field.get(this);
mSystemCounts[system.ordinal()]++;
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException("Reflection black magic failed", e);
}

}

@SuppressWarnings("unchecked")
private void addEntry(GridCoord coord, GridEntry entry){
try {
Field field = this.getClass().getSuperclass().getDeclaredField("mSystems");
field.setAccessible(true);
Map<GridCoord, GridEntry> systems = (Map<GridCoord, GridEntry>) field.get(this);
systems.put(coord, entry);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException("Reflection black magic failed", e);
}

}

private GridEntry createGridEntry(ShipSystem system, int systemCount) {
try {
Constructor<?> constructor = GridEntry.class.getDeclaredConstructors()[0];
constructor.setAccessible(true);
return (GridEntry) constructor.newInstance(system, systemCount);
} catch (SecurityException | IllegalArgumentException | IllegalAccessException | InstantiationException e) {
throw new RuntimeException("Reflection black magic failed", e);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException) {
throw (RuntimeException) e.getTargetException();
}
throw new RuntimeException("Unexpected non-runtime exception", e);
}
}
}

0 comments on commit f6faf68

Please sign in to comment.