package data.missions.randombattle1;
import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.fleet.FleetGoal;
import com.fs.starfarer.api.fleet.FleetMemberType;
import com.fs.starfarer.api.mission.FleetSide;
import com.fs.starfarer.api.mission.MissionDefinitionAPI;
import com.fs.starfarer.api.mission.MissionDefinitionPlugin;
import java.io.IOException;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
import org.lazywizard.lazylib.MathUtils;
public class MissionDefinition implements MissionDefinitionPlugin {
//This string is used to follow the steps as we attempt to get data out of JSON
private String nameString = "blah";
private void generateFleet(FleetSide side, MissionDefinitionAPI api) {
//If we only get this far, show this upon crashing
nameString = "foobar";
String[] possibleFactionNames = new String[] {"glaug", "hegemony", "tritachyon", "independent", "punkjunkers", "pirates", "exigency"};
String factionName = possibleFactionNames[(int) MathUtils.getRandomNumberInRange(0, possibleFactionNames.length - 1)];
if(factionName.contains("glaug")){
api.addToFleet(side, "glaug_battleship_Standard", FleetMemberType.SHIP, true);
}else if(factionName.contains("hegemony")){
api.addToFleet(side, "onslaught_Standard", FleetMemberType.SHIP, true);
}else if(factionName.contains("tritachyon")){
api.addToFleet(side, "odyssey_Standard", FleetMemberType.SHIP, true);
}else if(factionName.contains("pirates")){
api.addToFleet(side, "acanthus_Standard", FleetMemberType.SHIP, true);
}else if(factionName.contains("independent")){
api.addToFleet(side, "conquest_Standard", FleetMemberType.SHIP, true);
}else if(factionName.contains("exigency")){
api.addToFleet(side, "exigency_carrier_Standard", FleetMemberType.SHIP, true);
}else if(factionName.contains("punkjunkers")){
api.addToFleet(side, "pj_queen_Standard", FleetMemberType.SHIP, true);
}else{
api.addToFleet(side, "conquest_Standard", FleetMemberType.SHIP, true);
}
try {
JSONObject json = Global.getSettings().loadJSON("data/world/factions/" + factionName + ".faction");
//Success, thus far
nameString = "Parsed JSON";
String test = json.getString("shipNamePrefix");
//We've gotten this far...
nameString = test;
JSONObject jsonTwo = json.getJSONObject("fleetCompositions").getJSONObject("StationAssaultFleet").getJSONObject("ships");
Iterator<String> keysIterator = jsonTwo.keys();
int min = 0;
int max = 0;
while (keysIterator.hasNext())
{
//Are we there yet?
nameString = "" + min + max;
test = (String)keysIterator.next();
min = jsonTwo.getJSONArray(test).getInt(0);
max = jsonTwo.getJSONArray(test).getInt(1);
//We're there now :-)
nameString = "" + min + max;
if(test.contains("wing")){
for(int i = 0; i < MathUtils.getRandomNumberInRange(min,max)+1; i++){
api.addToFleet(side, test, FleetMemberType.FIGHTER_WING, false);
}
}else {
for(int i = 0; i < MathUtils.getRandomNumberInRange(min,max)+1; i++){
api.addToFleet(side, test, FleetMemberType.SHIP, false);
}
}
}
//We've succeeded; remove the nameString
nameString = "";
} catch (IOException ex) {
//Error; couldn't find the file
nameString = "no IO";
} catch (JSONException ex) {
//Error; bad JSON command
nameString = "bad JSON";
}
}
@Override
public void defineMission(MissionDefinitionAPI api) {
// Set up the fleets so we can add ships and fighter wings to them.
// In this scenario, the fleets are attacking each other, but
// in other scenarios, a fleet may be defending or trying to escape
api.initFleet(FleetSide.PLAYER, "ISS", FleetGoal.ATTACK, false, 5);
api.initFleet(FleetSide.ENEMY, "ISS", FleetGoal.ATTACK, true, 5);
// Set a small blurb for each fleet that shows up on the mission detail and
// mission results screens to identify each side.
api.setFleetTagline(FleetSide.PLAYER, "Your forces");
api.setFleetTagline(FleetSide.ENEMY, "Enemy forces");
// Set up the fleets
generateFleet(FleetSide.PLAYER, api);
generateFleet(FleetSide.ENEMY, api);
// These show up as items in the bulleted list under
// "Tactical Objectives" on the mission detail screen
api.addBriefingItem("Defeat all enemy forces " + nameString);
// Set up the map.
float width = 18000f;
float height = 14000f;
api.initMap((float)-width/2f, (float)width/2f, (float)-height/2f, (float)height/2f);
float minX = -width/2;
float minY = -height/2;
for (int i = 0; i < 50; i++) {
float x = (float) Math.random() * width - width/2;
float y = (float) Math.random() * height - height/2;
float radius = 100f + (float) Math.random() * 400f;
api.addNebula(x, y, radius);
}
}
}