Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Code Sample - Random Reduction of Station Inventory  (Read 7155 times)

Psiyon

  • Admiral
  • *****
  • Posts: 772
  • Trippy
    • View Profile
Code Sample - Random Reduction of Station Inventory
« on: May 27, 2012, 12:42:04 PM »

Been fiddling around with this for an hour or so. It's simple, and it seems to work well. The idea is that inventory in an orbital station is randomly eaten up as time progresses, providing the feeling that you're not the only one making purchases from stations. With a little tweaking, this can easily prevent a station from having boatloads of crew, fuel, and supplies in your mod. It can also make resupplying more interesting, as perhaps one station will not have enough inventory to satisfy your demand.

I haven't attempted to try to do this with weapons yet, as I'm short on time. If anyone has any ideas on how to do that, feel free to respond, as I'd love to know.

Also, thanks to vorpal+5 because I used his dynamic reinforcements script as a simple template for this.

Here's the code:

Code

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 stationrapist extends BaseSpawnPoint {

   private final SectorEntityToken station;


   public stationrapist(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() {


CargoAPI cargo = station.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;
     
 
  //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 >= 400) && ((float) Math.random() > 0.84)) {
 
 
  //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("Station rapist-supplies");


      }

     if ((fuel >= 600) && ((float) Math.random() > 0.66)) {
 
f = 160 + (int)(Math.random() * ((350 - 160) + 1));
         cargo.removeFuel(f); 
//Global.getSectorAPI().addMessage("Station rapist-fuel");

      }
 
     if ((crewg >= 700) && ((float) Math.random() > 0.95)) {
 
f = 10 + (int)(Math.random() * ((120 - 10) + 1));
         cargo.removeCrew(CrewXPLevel.GREEN,f);
//Global.getSectorAPI().addMessage("Station rapist-green crew");

         
      }  

     if ((crewr >= 260) && ((float) Math.random() > 0.84)) {
 
f = 20 + (int)(Math.random() * ((190 - 20) + 1));
         cargo.removeCrew(CrewXPLevel.REGULAR,f);
//Global.getSectorAPI().addMessage("Station rapist-regular crew");
         
      }    

     if ((marines >= 60) && ((float) Math.random() > 0.91)) {
 
f = 6 + (int)(Math.random() * ((35 - 6) + 1));
cargo.removeMarines(f);
//Global.getSectorAPI().addMessage("Station rapist-marines");

      }    

//Veteran and elite crew weren't added because they're rare enough as it is, but if you do want them to be removed, it should be easy to copy.


CampaignFleetAPI derp = null;
return derp;

}

}



And you'll need one of these lines in your corvus.java file for the station you want this script to run on:

Code

      stationrapist NAMEHERE = new stationrapist(sector, system, 1, 999, token, STATIONHERE);
      system.addSpawnPoint(NAMEHERE);



Sorry about the... uh... crude name, but that's how I keep myself entertained while programming. Those of you who have looked through Ascendency's betrayal script should know that :P
Logged

CrashToDesktop

  • Admiral
  • *****
  • Posts: 3876
  • Quartermaster
    • View Profile
Re: Code Sample - Random Reduction of Station Inventory
« Reply #1 on: May 27, 2012, 05:49:20 PM »

Ah, this shall come in handy for the Caelus mod!  Thanks! ;D
+1
Logged
Quote from: Trylobot
I am officially an epoch.
Quote from: Thaago
Note: please sacrifice your goats responsibly, look up the proper pronunciation of Alex's name. We wouldn't want some other project receiving mystic power.

mrsnuggles

  • Lieutenant
  • **
  • Posts: 59
    • View Profile
Re: Code Sample - Random Reduction of Station Inventory
« Reply #2 on: May 27, 2012, 06:10:50 PM »

yeah this is definitely a nice idea

one question tho can you make it check the total amount of lets say all the small sized weapons & if its total was over an amount x remove some them

for example let say we have:
5 anti matter blaster
10 burst pd lasers
7 harpoon missles
& 15 tactical lasers

& lets say you want only 30 for the entire total of small weapons it would a random % of each
« Last Edit: May 27, 2012, 06:20:07 PM by mrsnuggles »
Logged

vorpal+5

  • Captain
  • ****
  • Posts: 286
    • View Profile
Re: Code Sample - Random Reduction of Station Inventory
« Reply #3 on: May 28, 2012, 01:43:09 AM »

Nice move Psiyon!  :)

You have struck the same problem I have. We have functions to retrieve particular items, like supplies or fuel. But I don't know if we have something that can first check the number of an item, and then remove some.

Something like


if (getWeapon("super-laser") > 5)
  RemoveWeapon("super-laser", 1);

i.e we first check if a weapon is there (more than 5), if yes, remove 1.

Same thing for ships.

Anybody knows? Warstalker perhaps?  ???  ;)

Logged

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7223
  • Harpoon Affectionado
    • View Profile
Re: Code Sample - Random Reduction of Station Inventory
« Reply #4 on: May 28, 2012, 02:15:29 AM »

Take a look at the cargo API file. These functions in particular:

Code

int getNumWeapons(String id);
void removeWeapons(String id, int count);
void addWeapons(String id, int count);


It should be as simple as loading up the cargo of the station using getEntityByName etc and then checking the specific weapon in an if statement. I haven't tested it so no promises, but it should work if there isn't a bug... I don't see anything in the API for removing ships, only adding them.
Logged

vorpal+5

  • Captain
  • ****
  • Posts: 286
    • View Profile
Re: Code Sample - Random Reduction of Station Inventory
« Reply #5 on: May 28, 2012, 03:32:38 AM »

Nice! Thanks, yes it will work well. I'm also adding resources as in the XPlo mod, to check for win or lose conditions for the player...
Logged

kwekly

  • Lieutenant
  • **
  • Posts: 54
    • View Profile
Re: Code Sample - Random Reduction of Station Inventory
« Reply #6 on: May 28, 2012, 05:41:41 AM »

why random? why not whenever a fleet arrives?
Logged

Psiyon

  • Admiral
  • *****
  • Posts: 772
  • Trippy
    • View Profile
Re: Code Sample - Random Reduction of Station Inventory
« Reply #7 on: May 28, 2012, 08:29:30 AM »

Thanks guys :)

---

why random? why not whenever a fleet arrives?

You could do that (I think), but that would require a bit of an overhaul of fleet movements in the system. As it stands, the only fleets that dock at orbital stations are convoys that deliver goods, or the occasional fleet resupplying. But, the concept would be simple--you'd just take the script I have up there, and put it in as an "arrived script" for a fleet:


Code

private Script createArrivedScript() {
return new Script() {
public void run() {

(INSERT CODE HERE)

}
};
}


I actually considered doing it your way, but I decided against it because I wanted to wait a while before implementing civilians and traders into Ascendency.



@Thaago: Yeah, I was looking at those, but it seems like it would be a bit of a pain to have to do it for every specific weapon. Then again, I'm terrible at coding so there's probably a simple way to do it involving an array or list or something.
Logged

mrsnuggles

  • Lieutenant
  • **
  • Posts: 59
    • View Profile
Re: Code Sample - Random Reduction of Station Inventory
« Reply #8 on: May 28, 2012, 09:52:48 AM »

Thanks guys :)

---

why random? why not whenever a fleet arrives?

You could do that (I think), but that would require a bit of an overhaul of fleet movements in the system. As it stands, the only fleets that dock at orbital stations are convoys that deliver goods, or the occasional fleet resupplying. But, the concept would be simple--you'd just take the script I have up there, and put it in as an "arrived script" for a fleet:


Code

private Script createArrivedScript() {
return new Script() {
public void run() {

(INSERT CODE HERE)

}
};
}


I actually considered doing it your way, but I decided against it because I wanted to wait a while before implementing civilians and traders into Ascendency.



@Thaago: Yeah, I was looking at those, but it seems like it would be a bit of a pain to have to do it for every specific weapon. Then again, I'm terrible at coding so there's probably a simple way to do it involving an array or list or something.



ok so what if you use an array to take the the total of lets say all small mount weapons & if that total was greater that x then it would take a random amount from each stack till the total was <= 75%x

i dont really know java or any programing language for that matter but i think that could work


for an example

Spoiler
first off we would need to create variables one for each item were trying to check with the array in this case small weapons which there are 30 of

so then sw1 (small weapon 1) = int getNumWeapons(tactical laser);

and do this for all 30

& then in your array put the thirty variables although this seems a little redundant you could probably just add up the variables but anyways then you compare then to value x (which is lets say 30) and if the total is greater than value x then you check how many of those are greater than 0 because why take into account any weapons not there then for every one of those variables that are greater than 0 you subtract value z

& the math to figure out how many to take would be z = sw1-y-(random number from a range) where the value y is the max amount you want after the script runs

so lets say the station has

 5 anti matter blaster = sw1
10 burst pd lasers = sw2
7 harpoon missiles = sw3
& 15 tactical lasers = sw4
also x= 30 & y = 6

so ...
sw1 = sw1 - z where z = sw1 - 6 - (a random number chose from an array lets say 3)
so z = 5 -6 -3 which equals -4
now we have sw1 = 5 - (-4)which is 9
obviously we don't want this so some where you would have to put if z <= 0 then z = 0 to avoid negatives[/spoilers]

obviously this method is a long longer & more complex than just doing every thing individually although im sure there an easy way to do it in mass like this but anyways your better off doing it individually unless you want to do it based on a group of items like weapon size or crew or even weapon types

or a really simple way to do it is make lets say an attack fleet come every so often & take a list of things base on what supply fleets bring
[close]
« Last Edit: May 28, 2012, 10:02:45 AM by mrsnuggles »
Logged