How drop data works? DropValue and DropRandom? how do they works
If I want a fleet drop 0~4 amount of commodity"XXX", how to add them?
DropValue will always drop some commodities from the specified group with a value equal to the number specified in the drop data with some variance (iirc +-50%).
DropRandom will have X chances to drop something from the group. If the group has a "nothing" entry, each chance may result in nothing being dropped.
For example, if you open up salvage_entity_gen_data.csv, you'll see something like this in the drop_value column:
basic:5000
This means to pick 5000 credits (+-50%) worth of stuff from the "basic" group, which is defined in drop_groups.csv.
Looking at that, we see:
metals basic 20
heavy_machinery basic 1
supplies basic 10
fuel basic 10
The number is the probability of each commodity being picked, so roughly the ratio of the commodities produced will match the ratio of these numbers.
You'll also see something like this in the drop_random column:
ai_cores2:3
This means "roll 3 times from the ai_cores2 group".
Looking at that one, we see:
nothing ai_cores2 100
beta_core ai_cores2 2
gamma_core ai_cores2 5
So each roll will produce nothing with a probability of 100/107, a beta core (2/107), or a gamma core (5/107).
You might also see something like this in the drop_random column:
extended:3x3000
This means "roll 3 times from the extended drop group, and each time produce 3000 credits worth of items". This is useful for example if you want to have it drop some commodities from a list, but instead of a distribution of a little bit of everything, you want to pick a couple to focus on. For example, the group might have 20 commodities, and if rolled twice, it'll pick out two and drop a bunch of those.
DropGroups can also be set up using code - I think TechMining.generateCargoForGatheringPoint() is a decent piece of code to look at for how that works. There, it directly passes the DropData it creates o SalvageEntity.generateSalvage(), but you could also add it to a fleet using fleet.addDropRandom(DropData) (or, indeed, addDropRandom(String id, int chances) and so on.
When it comes to dropping weapons/blueprints/etc, it gets more complicated with special json syntax in place of a commodity id.
So, right, if you want a fleet to drop 0-4 of commodityXXX, you would:
1) add a drop group to drop_data.csv. Let's name it "testDrop".
nothing testDrop 50
commodityXXX testDrop 50
2) Call: fleet.addDropRandom("testDrop", 4)
Note that this would give 4 50/50 chances to drop one commodityXXX, so while you'd get 0 to 4 of them, it would not be an even probability distribution.
Hope this answers your question! Let me know if you'd like me to clarify something.
And how to make fleets defend one place and ignore the hostile fleets which is far from them?(In a starsystem, there are 2 factions which is hostile to each other, the place defended is far from each other too)
You would need to give them a custom assignment AI, for example like this one:
com.fs.starfarer.api.impl.campaign.tutorial.TutorialLeashAssignmentAI
How to do that depends on how the fleets are created.