Starsector API
Loading...
Searching...
No Matches
BeginMission.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.rulecmd;
2
3import java.util.List;
4import java.util.Map;
5import java.util.Random;
6
7import com.fs.starfarer.api.Global;
8import com.fs.starfarer.api.campaign.InteractionDialogAPI;
9import com.fs.starfarer.api.campaign.SectorEntityToken;
10import com.fs.starfarer.api.campaign.rules.MemKeys;
11import com.fs.starfarer.api.campaign.rules.MemoryAPI;
12import com.fs.starfarer.api.characters.PersonAPI;
13import com.fs.starfarer.api.impl.campaign.intel.bar.events.BarEventManager;
14import com.fs.starfarer.api.impl.campaign.missions.hub.HubMission;
15import com.fs.starfarer.api.loading.PersonMissionSpec;
16import com.fs.starfarer.api.util.Misc.Token;
17
24public class BeginMission extends BaseCommandPlugin {
25
26 public static String TEMP_MISSION_KEY = "$tempMissionKey";
27
28 public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
29 if (dialog == null) return false;
30
31 String missionId = params.get(0).getString(memoryMap);
32 Boolean accept = null;
33 if (params.size() > 1) {
34 accept = params.get(1).getBoolean(memoryMap);
35 }
36 // default behavior is to accept the mission right away w/o an AcceptMission command call
37 if (accept == null) accept = true;
38
39 PersonMissionSpec spec = Global.getSettings().getMissionSpec(missionId);
40 if (spec == null) {
41 throw new RuntimeException("Mission with spec [" + missionId + "] not found");
42 }
43
44 HubMission mission = spec.createMission();
45
46 SectorEntityToken entity = dialog.getInteractionTarget();
47 PersonAPI person = entity.getActivePerson();
48
49 if (person == null) {
50// throw new RuntimeException("Attempting to BeginMission " + missionId +
51// " in interaction with entity.getActivePerson() == null");
52// String key = "$beginMission_seedExtra";
53// String extra = person.getMemoryWithoutUpdate().getString(key);
54 String extra = "";
55 long seed = BarEventManager.getInstance().getSeed(null, person, extra);
56// person.getMemoryWithoutUpdate().set(key, "" + seed); // so it's not the same seed for multiple missions
57 mission.setGenRandom(new Random(seed));
58
59 } else {
60 mission.setPersonOverride(person);
61 //mission.setGenRandom(new Random(Misc.getSalvageSeed(entity)));
62 String key = "$beginMission_seedExtra";
63 String extra = person.getMemoryWithoutUpdate().getString(key);
64 long seed = BarEventManager.getInstance().getSeed(null, person, extra);
65 person.getMemoryWithoutUpdate().set(key, "" + seed); // so it's not the same seed for multiple missions
66 mission.setGenRandom(new Random(seed));
67 }
68
69 mission.createAndAbortIfFailed(entity.getMarket(), false);
70
71 if (mission.isMissionCreationAborted()) {
72 MemoryAPI memory = memoryMap.get(MemKeys.LOCAL);
73 if (memory == null && dialog.getInteractionTarget() != null) {
74 if (dialog.getInteractionTarget().getActivePerson() != null) {
75 memory = dialog.getInteractionTarget().getActivePerson().getMemoryWithoutUpdate();
76 } else {
77 memory = dialog.getInteractionTarget().getMemoryWithoutUpdate();
78 }
79 if (memory != null) {
80 memory.set("$missionCreationFailed", mission.getMissionId());
81 }
82 }
83 return false;
84 }
85
86 if (accept) {
87 mission.accept(dialog, memoryMap);
88 } else {
89 Global.getSector().getMemoryWithoutUpdate().set(TEMP_MISSION_KEY, mission, 0f);
90 }
91
92 return true;
93 }
94}
95
96
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
boolean execute(String ruleId, InteractionDialogAPI dialog, List< Token > params, Map< String, MemoryAPI > memoryMap)
PersonMissionSpec getMissionSpec(String id)