OK, here's a SupplyControl script that doesn't need to be tied to any stations or SectorEntityToken:
Spoiler
package data.scripts.world.corvus;
import com.fs.starfarer.api.campaign.CampaignFleetAPI;
import com.fs.starfarer.api.campaign.FleetAssignment;
import com.fs.starfarer.api.campaign.LocationAPI;
import com.fs.starfarer.api.campaign.SectorAPI;
import com.fs.starfarer.api.campaign.SectorEntityToken;
import com.fs.starfarer.api.campaign.CargoAPI;
import com.fs.starfarer.api.campaign.FactionAPI;
import com.fs.starfarer.api.campaign.StarSystemAPI;
import com.fs.starfarer.api.campaign.CargoAPI.CrewXPLevel;
import com.fs.starfarer.api.fleet.FleetMemberType;
import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.Script;
import data.scripts.world.BaseSpawnPoint;
import java.util.List;
@SuppressWarnings("unchecked")
public class SupplyControl extends BaseSpawnPoint {
private final SectorEntityToken station;
public SupplyControl(SectorAPI sector, LocationAPI location, float daysInterval, int maxFleets, SectorEntityToken anchor, SectorEntityToken station)
{
super(sector, location, daysInterval, maxFleets, anchor);
this.station = station;
}
@Override
public CampaignFleetAPI spawnFleet()
{
StarSystemAPI system = getSector().getStarSystem("Corvus");
List theStations = system.getOrbitalStations();
for (int orbital = 0; orbital < theStations.size(); orbital++)
{
String s1 = "" + orbital;
SectorEntityToken myStation = (SectorEntityToken)theStations.get(orbital);
String stationString = (String)myStation.getName();
//Global.getSectorAPI().addMessage(s1 + " = " + stationString);
//Global.getSectorAPI().addMessage("Supply Control is checking " + stationString);
//Future code for evaluating boolean state of named locations via string match for analysis and monitoring
//boolean factionMatch = stationString.matches("(?i).*hegemony*");
CargoAPI cargo = myStation.getCargo();
//sets up all the different cargo items, and gets their amounts
float supplies = cargo.getSupplies();
float fuel = cargo.getFuel();
float crewg = cargo.getCrew(CrewXPLevel.GREEN);
float crewr = cargo.getCrew(CrewXPLevel.REGULAR);
float marines = cargo.getMarines();
int f;
int amt;
int i;
//this randomly removes weapons from a station.
List weaponIds = getSector().getAllWeaponIds();
for (int w = 0; w < weaponIds.size(); ++w)
{
i=cargo.getNumWeapons((String)weaponIds.get(w));
//4% chance each day that a weapon will be removed.
if ((float) Math.random() > 0.96)
{
amt = (int)(Math.random() * ((i - 1)));
if (amt > 0)
{
cargo.removeWeapons((String)weaponIds.get(w),amt);
//Global.getSectorAPI().addMessage("Supply Control -Weapon");
}
}
}
//As long as the supplies at a station are greater than or equal to 400, there's a 16% chance that this code will run
if ((supplies >= 1000) && (supplies <= 2000) && ((float) Math.random() > 0.76))
{
//the 60s here are the minimum value that can be removed. 350 is the maximum value. Make sure the maximum value is below the value in the if statement above, which is 400 here.
//Otherwise, you might get negatives and I don't know how Starfarer will like that.
f = 60 + (int)(Math.random() * ((350 - 60) + 1));
cargo.removeSupplies(f);
//uncomment this to get an ingame notification when this code runs
//Global.getSectorAPI().addMessage("Supply Control is truncating supplies");
}
if (supplies > 2000) {
f = (int)(supplies - 2000);
cargo.removeSupplies(f);
//Global.getSectorAPI().addMessage("Supply Control is truncating lots of supplies");
}
if ((fuel >= 600) && ((float) Math.random() > 0.60))
{
f = 160 + (int)(Math.random() * ((350 - 160) + 1));
cargo.removeFuel(f);
//Global.getSectorAPI().addMessage("Supply Control -fuel");
}
if (fuel > 2000) {
f = (int)(fuel - 2000);
cargo.removeFuel(f);
}
if ((crewg >= 700) && ((float) Math.random() > 0.93))
{
f = 10 + (int)(Math.random() * ((120 - 10) + 1));
cargo.removeCrew(CrewXPLevel.GREEN,f);
//Global.getSectorAPI().addMessage("Supply Control -green crew");
}
if ((crewr >= 500) && ((float) Math.random() > 0.80)) {
f = 20 + (int)(Math.random() * ((190 - 20) + 1));
cargo.removeCrew(CrewXPLevel.REGULAR,f);
//Global.getSectorAPI().addMessage("Supply Control -regular crew");
}
if ((marines >= 60) && ((float) Math.random() > 0.87)) {
f = 6 + (int)(Math.random() * ((35 - 6) + 1));
cargo.removeMarines(f);
//Global.getSectorAPI().addMessage("Supply Control -marines");
}
}
//Veteran and elite crew weren't added because they're rare enough as it is, but if you do want them to be gradually removed, it should be easy to copy.
CampaignFleetAPI derp = null;
return derp;
}
}
It's still ugly in a lot of ways. For one thing, it shouldn't need to be designed as a SpawnPoint in the first place; there should be a cleaner way to run code at X hours / days / weeks.
Hmm. So, I was playing around with some stuff last night before I ran out of steam; basically, I need a generic method to query what Faction an OrbitalStation can be, using SectorEntityToken to start the derivation.
The only method I could think up is pretty hackish; insert a string into each named location giving the Faction ownership, and if the string matches X, then Y.
Is there any more elegant way to approach that problem?