package arena;
import java.io.File;
import java.util.List;
import megamek.common.CriticalSlot;
import megamek.common.compute.Compute;
import megamek.common.game.Game;
import megamek.common.game.IGame;
import megamek.common.loaders.MekSummaryCache;
import megamek.common.scenario.Scenario;
import megamek.common.scenario.ScenarioLoader;
import megamek.common.units.Entity;
import megamek.common.units.Mek;
/**
* Damage a scenario's first Mek by hand and print what {@link UnitReport}
* makes of it, as one JSON document.
*
*
* UnitReportCheck data/scenarios/TrainingScenarios/1-FirstRun.mms
*
*
* In package {@code arena} so it can call the report directly. The damage
* below is applied to the loaded game rather than played for: a match that
* actually shoots a Mek's arm off takes minutes and a container, and what is
* being checked here is the shape of the document and the reading of MegaMek's
* state - a blown-off location, a destroyed one, a hit critical slot and a
* wounded pilot all coming out as themselves.
*
*
{@code tests/shell/test-unit-report.sh} is what asserts against it.
*/
public final class UnitReportCheck {
private UnitReportCheck() {
}
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("usage: UnitReportCheck ");
System.exit(2);
}
Compute.d6();
MekSummaryCache cache = MekSummaryCache.getInstance();
while (!cache.isInitialized()) {
Thread.sleep(250L);
}
ScenarioLoader loader = new ScenarioLoader(new File(args[0]));
Scenario scenario = loader.load();
IGame game = scenario.createGame();
Entity mek = null;
for (Entity e : ((Game) game).getEntitiesVector()) {
if (e instanceof Mek) {
mek = e;
break;
}
}
if (mek == null) {
System.err.println("no Mek in " + args[0]);
System.exit(1);
}
// A torso stripped to structure, an arm blown off, a hit critical slot
// and a pilot who took one. Between them these cover every branch the
// report has: an amount, a state MegaMek stores as a negative number,
// a slot flag and the crew.
int torso = Mek.LOC_CENTER_TORSO;
mek.setArmor(0, torso);
mek.setInternal(3, torso);
for (int i = 0; i < mek.getNumberOfCriticalSlots(torso); i++) {
CriticalSlot slot = mek.getCritical(torso, i);
if (slot != null) {
slot.setHit(true);
slot.setDestroyed(true);
break;
}
}
mek.setLocationBlownOff(Mek.LOC_LEFT_ARM, true);
mek.getCrew().setHits(2, 0);
// Every unit in the scenario, not just the damaged one: the sheet a
// reader gets depends on what the machine is, and a scenario with a
// tank or a platoon in it is how the other layouts get looked at.
StringBuilder out = new StringBuilder("{\"units\": [");
List all = ((Game) game).getEntitiesVector();
for (int i = 0; i < all.size(); i++) {
out.append(i > 0 ? ", " : "").append(UnitReport.unit(all.get(i)));
}
System.out.println(out.append("]}"));
}
}