Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Problems Adding Ships to Custom Faction Station  (Read 7025 times)

billi999

  • Commander
  • ***
  • Posts: 149
    • View Profile
Problems Adding Ships to Custom Faction Station
« on: February 25, 2016, 10:40:20 AM »

Hey all,

I'm glad to be returning to Starsector after some absence and find good progress has been made.
To get to the point, I've been trying to port a mod of mine from an older version.

Problem #1 - Solved, see posts following of Alex and mine if looking for a solution to a problem of runtime null reference exceptions pointing to renderWeapons.

When my mod is enabled, I get a null reference error at a seemingly unpredictable time. As far as I can tell, the stack trace doesn't lead to anything of much use as I've searched for renderWeapons in the API but couldn't find anything.
Spoiler
Code
...
java.lang.NullPointerException
at com.fs.starfarer.campaign.fleet.CampaignFleetMemberView.renderWeapons(Unknown Source)
at com.fs.starfarer.campaign.fleet.CampaignFleetMemberView.renderSingle(Unknown Source)
at com.fs.starfarer.campaign.fleet.CampaignFleetMemberView.render(Unknown Source)
at com.fs.starfarer.campaign.fleet.CampaignFleetView.render(Unknown Source)
at com.fs.starfarer.campaign.fleet.CampaignFleet.render(Unknown Source)
at com.fs.starfarer.campaign.BaseCampaignEntity.render(Unknown Source)
at com.fs.graphics.LayeredRenderer.render(Unknown Source)
at com.fs.starfarer.campaign.BaseLocation.render(Unknown Source)
at com.fs.starfarer.campaign.CampaignEngine.render(Unknown Source)
at com.fs.starfarer.campaign.CampaignState.render(Unknown Source)
at com.fs.starfarer.BaseGameState.traverse(Unknown Source)
at com.fs.state.AppDriver.begin(Unknown Source)
at com.fs.starfarer.combat.CombatMain.main(Unknown Source)
at com.fs.starfarer.StarfarerLauncher$2.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
[close]
I do have weapons in my mod, so I can only guess it's something to do with that causing the problem, but I don't know where to start with finding the culprit due to a lack of information.
Having checked my weapons list in the codex, I can find a couple of possible culprits:
• I have a weapon that is almost a copy of the Dual Flak Cannon, with some stat differences. It has a unique ID but the name is the same. If the game identifies weapons by name alone at any point, this may be the cause of the problem.
• The sprite paths for a built-in weapon which doesn't need a sprite are set as "" (empty double quotes).

Problem #2 - Solved once again with the help of Alex! To add to a stations' cargo, you now have to do, for example: .getMarket().getSubmarket("generic_military").getCargo(), then add the ships as normal with e.g.; addMothballedShips

Originally, ships which are added by my mod were delivered to a custom station which was inserted into Corvus along with a race to fulfil this. At some point, it seems there were big changes to the way delivering ships to stations in the campaign works, which makes sense, considering how complex the markets are and the size of the game world now. However, I cannot get this to work anymore. In fact, I cannot even get my ships to be added to the station inventory at the start of the game. Here's what my code is at the moment:
Spoiler
Code
public class ShipPackSectorGen implements SectorGeneratorPlugin
{
public void generate(SectorAPI sector)
{
StarSystemAPI system = sector.getStarSystem("Corvus");

SectorEntityToken centre = system.createToken(0, 0);
CargoAPI cargo;

SectorEntityToken station = system.addCustomEntity("trade_shipyard",
"Trade Shipyard", "station_jangala_type", "shippackfaction");

station.setCircularOrbitPointingDown(centre, 55, 4750, 100);
station.setCustomDescriptionId("station_jangala");

SectorEntityToken convoyLocation = system.createToken(5000,5000);

MarketAPI market = Global.getFactory().createMarket("corvus_shippack_market", station.getName(), 0);
market.setPrimaryEntity(station);
market.setFactionId(station.getFaction().getId());
market.addSubmarket(Submarkets.SUBMARKET_OPEN);
station.setMarket(market);

cargo = station.getCargo();

int shipQuantity = 48;
SelectShips(cargo,randomDecentShips,shipQuantity);
...
}

private void SelectShips(CargoAPI cargo, String inShipsList[], int inShipQuantity)
{
for (int i=0; i < inShipQuantity; i++)
{
String selectedShipName = inShipsList[(int) (inShipsList.length*Math.random())];

cargo.getMothballedShips().addFleetMember(Global.getFactory().createFleetMember
(FleetMemberType.SHIP, selectedShipName));

//Deprecated?
//cargo.addMothballedShip(FleetMemberType.SHIP,inShipsList[(int)
(inShipsList.length*Math.random())],null);
}
}

private static String [] randomDecentShips =
{
//Contains about 10 ship hull names which all belong to the mod in question
};
}
[close]
What I'd like to know is if I could at least get ships to be in the inventory at the start of the game, fully understanding there's no gaurantee other fleets won't take them all before the player has a chance. What would be even better is if I could get a delivery fleet to work as before, and if that is possible I'll include the code for that as well.
« Last Edit: February 29, 2016, 11:42:49 AM by billi999 »
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24114
    • View Profile
Re: Couple of Problems with my Mod
« Reply #1 on: February 25, 2016, 12:03:53 PM »

Just real quick, regarding the NullPointerException crash: I suspect it would be because of a variant putting a weapon into an invalid weapon slot. Failing that, I'd look at any decorative weapons you might have on your ships.
Logged

billi999

  • Commander
  • ***
  • Posts: 149
    • View Profile
Re: Couple of Problems with my Mod
« Reply #2 on: February 25, 2016, 12:40:56 PM »

Hmm... I think you're onto something with that. Originally, my mod didn't use a prefix for hull names. Some of the ships I added have names which clash with vanilla such as the Heron. My knowledge of how this kind of thing works is very limited, but maybe the game is trying to use a variant for the wrong hull, and this is what causes something like the scenario you mentioned?
Also, thanks for taking the time to reply to my OP.

Edit: Unless someone can confirm this is definitely not going to be the cause of the error, will report back with findings.
« Last Edit: February 25, 2016, 12:44:24 PM by billi999 »
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24114
    • View Profile
Re: Couple of Problems with my Mod
« Reply #3 on: February 25, 2016, 01:22:48 PM »

Ah yeah, that's possible - if your game provides a "heron" hull, then that would override the vanilla "heron" hull, and vanilla variants pointing to it would use your version of it, which would most likely not have the right slots.

You definitely want a mod-specific prefix on all ids.
Logged

billi999

  • Commander
  • ***
  • Posts: 149
    • View Profile
Re: Couple of Problems with my Mod
« Reply #4 on: February 25, 2016, 01:51:33 PM »

That's what I was afraid of, makes sense though. Same for having a prefix to hull names, I was simply clueless at first and now trying to prefix them after the fact seems like it can cause runtime errors if I overlook something. But I'll have to just go through all hulls and give them prefixes before release!
All that said, I don't think that's the cause of this particular problem. I added every non-empty variant to a mission I have been using for testing and there were no problems I'm afraid. I'm not sure where to go from here.
And thanks for the second response :)
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24114
    • View Profile
Re: Couple of Problems with my Mod
« Reply #5 on: February 25, 2016, 02:00:25 PM »

What I'm saying, though, is if one of your hulls replaces a vanilla hull (through accidental hull id collision), then the vanilla variants of that hull will cause errors, because they're now pointing at a different hull than what they're made for.
Logged

billi999

  • Commander
  • ***
  • Posts: 149
    • View Profile
Re: Couple of Problems with my Mod
« Reply #6 on: February 25, 2016, 03:54:44 PM »

My bad, I realise I totally misunderstood now. After comparing both ship_data.csv files I was able to track down the offending hull, add a prefix to it and all references, and the problem is no more. Replacing all references in the mod really wasn't as bad as expected, which I'm thankful for as there's 25 more hulls I've yet to do this for :D

Thanks again for your help and please excuse my slowness in understanding the issue!
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24114
    • View Profile
Re: Couple of Problems with my Mod
« Reply #7 on: February 25, 2016, 04:37:17 PM »

Glad you got that part of it sorted! No worries, it was a pretty convoluted issue :)


For getting your ships/weapons onto a station: if the market belongs to your faction, and you have a "shipRoles" section in your .faction file, then a *military* submarket in your market should have the ships/weapons show up on it. The open market will only stock low-tier weapons and mostly civilian ships.
Logged

billi999

  • Commander
  • ***
  • Posts: 149
    • View Profile
Re: Couple of Problems with my Mod
« Reply #8 on: February 26, 2016, 06:46:25 AM »

Ok, the thing about having a military submarket makes sense. For anyone reading this that might've had the same problem: The submarket name is not Submarkets.SUBMARKET_MILITARY as you might expect based on the open market value, but Submarkets.GENERIC_MILITARY. I was also happy to find that you're not forced to need commission to be able to buy anything from the military market!
Now to work out the spawn point, it's not working quite right at the moment. My messages say the fleet has arrived and that it has delivered ships in immediate succession! I don't know what kind of drive that fleet has but I want it :D
Regardless, no additional ships are actually delivered. For future testing, would I be able to rely on that bit where you start a new game and it runs a couple of months to be able to tell if ships are actually being delivered? And what might make the fleets instantly spawn and despawn? Hmm... Fleet limit?

Edit: I think it's working now, the above problem only seemed to be occurring due to not starting a new game. Also no ships were being delivered even after but the number of ships to deliver was (int)Random*2.0f so it was probably rounding down to zero.
Edit 2: Still not been able to solve the ship delivery issue. Please see post following.
« Last Edit: February 28, 2016, 03:51:18 AM by billi999 »
Logged

billi999

  • Commander
  • ***
  • Posts: 149
    • View Profile
Re: Couple of Problems with my Mod
« Reply #9 on: February 28, 2016, 03:54:01 AM »

Sorry for the bump, I understand someone might've answered if they knew what the problem was but the situation has changed a bit and I think I can provide more information in a way that might be useful.

I have fleets appearing and carrying out the objectives, but after ships are supposed to be delivered the ship inventory for the military market is always the same as before. It's not to do with a bad calculation on how many ships to deliver because a weapon hardcoded to be added wasn't delivered either. As another way to confirm that I set ship delivery fleets to be created daily. In pseudo-ish code, I've tried adding ships with the following methods:
Spoiler
CargoAPI cargo = fleet.getCargo();
cargo.initMothballedShips("shippackfaction");

for each ship to add between 1-3 quantity:
String targetShip = inShipsList[(int) (inShipsList.length*Math.random())];
cargo.getMothballedShips().addFleetMember(Global.getFactory().createFleetMember(FleetMemberType.SHIP, targetShip));

[close]
And this too:
Spoiler
CargoAPI cargo = fleet.getCargo();
cargo.initMothballedShips("shippackfaction"); //Tried both with and without this line

for each ship to add between 1-3 quantity:
cargo.addMothballedShip(FleetMemberType.SHIP,inShipsList[(int) (inShipsList.length*Math.random())],null);

[close]
Assignments look like this:
Spoiler
fleet.addAssignment(FleetAssignment.DELIVER_RESOURCES, station, 1000, script);
fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, getAnchor(), 1000);
[close]
Logged

billi999

  • Commander
  • ***
  • Posts: 149
    • View Profile
Re: Couple of Problems with my Mod
« Reply #10 on: February 29, 2016, 10:30:57 AM »

I've found out what seems to be the problem but have no idea of how to go about solving it. It seems even the ships added in the SectorGen file are not actually being added, I set it up to add 10 ships but always only 1 or 2 were ever there, so I just assumed in simulating the first couple of months, the economy sucked most of them up.
What actually happened is that the code I've got to add ships seems to be pretty much ignored, and those couple of different ships appear regardless as long as the military market it is present. So at this point I can only determine either of the following:
• I'm doing something wrong with cargo.addMothballedShip() and cargo.getMothballedships().addFleetMember(). Are these methods now deprecated due to the new (relatively speaking) economy system in Starsector? And if so, What would be the "new way" to get this to work?
• My SectorEntityToken with the name "station", which I use CargoAPI cargo = station.getCargo(); on isn't working in some way, or something isn't right with my SectorEntityToken.

I'm assuming the only way I might get any help at this point is if I provide the code to make things clearer. It's messy at the moment, and I'd have really avoided having it see the light of day before I cleaned it up, but I don't see an alternative. Here it is:
https://www.dropbox.com/s/86qgb9bplv561y8/world.rar?dl=0

Please don't take this for me not wanting to do the work I have created myself, I've spent a good amount of time in the last few days trying to find away around these issues, without actually realising the methods I tried were never seeming to work to begin with.
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24114
    • View Profile
Re: Couple of Problems with my Mod
« Reply #11 on: February 29, 2016, 10:45:32 AM »

The way it works now is a market has a bunch of submarkets, which have their own individual cargo.

So you'd want to do something like:

entity.getMarket().getSubmarket("generic_military").getCargo() to get the cargo. Then you can do what you were doing to add the weapons/ships, and they should show up in that submarket.
Logged

billi999

  • Commander
  • ***
  • Posts: 149
    • View Profile
Re: Couple of Problems with my Mod
« Reply #12 on: February 29, 2016, 11:38:11 AM »

Awesome, that worked! Thank you for your reply once again ;D Now that I read that bit of code, the solution sounds so obvious! I think I even had the API page for SectorEntityToken in my browser history so the solution was right there, staring me in the face. I hope anyone having the same issue bumps into this thread, but in retrospect it might've needed a better title for that.
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24114
    • View Profile
Re: Problems Adding Ships to Custom Faction Station
« Reply #13 on: February 29, 2016, 01:24:41 PM »

:)

Glad you got it working; things often seem obvious after the fact, but the API is pretty big so it's not always easy to find things.
Logged