001package com.fs.starfarer.api.impl.campaign.rulecmd; 002 003import java.util.List; 004import java.util.Map; 005import java.util.Random; 006 007import com.fs.starfarer.api.Global; 008import com.fs.starfarer.api.campaign.InteractionDialogAPI; 009import com.fs.starfarer.api.campaign.SectorEntityToken; 010import com.fs.starfarer.api.campaign.rules.MemKeys; 011import com.fs.starfarer.api.campaign.rules.MemoryAPI; 012import com.fs.starfarer.api.characters.PersonAPI; 013import com.fs.starfarer.api.impl.campaign.intel.bar.events.BarEventManager; 014import com.fs.starfarer.api.impl.campaign.missions.hub.HubMission; 015import com.fs.starfarer.api.loading.PersonMissionSpec; 016import com.fs.starfarer.api.util.Misc.Token; 017 018/** 019 * Assumes active person is the mission giver. And rules using this seem likely to 020 * assume that mission creation does not fail. 021 * 022 * BeginMission <string id> <boolean accept> 023 */ 024public class BeginMission extends BaseCommandPlugin { 025 026 public static String TEMP_MISSION_KEY = "$tempMissionKey"; 027 028 public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) { 029 if (dialog == null) return false; 030 031 String missionId = params.get(0).getString(memoryMap); 032 Boolean accept = null; 033 if (params.size() > 1) { 034 accept = params.get(1).getBoolean(memoryMap); 035 } 036 // default behavior is to accept the mission right away w/o an AcceptMission command call 037 if (accept == null) accept = true; 038 039 PersonMissionSpec spec = Global.getSettings().getMissionSpec(missionId); 040 if (spec == null) { 041 throw new RuntimeException("Mission with spec [" + missionId + "] not found"); 042 } 043 044 HubMission mission = spec.createMission(); 045 046 SectorEntityToken entity = dialog.getInteractionTarget(); 047 PersonAPI person = entity.getActivePerson(); 048 049 if (person == null) { 050// throw new RuntimeException("Attempting to BeginMission " + missionId + 051// " in interaction with entity.getActivePerson() == null"); 052// String key = "$beginMission_seedExtra"; 053// String extra = person.getMemoryWithoutUpdate().getString(key); 054 String extra = ""; 055 long seed = BarEventManager.getInstance().getSeed(null, person, extra); 056// person.getMemoryWithoutUpdate().set(key, "" + seed); // so it's not the same seed for multiple missions 057 mission.setGenRandom(new Random(seed)); 058 059 } else { 060 mission.setPersonOverride(person); 061 //mission.setGenRandom(new Random(Misc.getSalvageSeed(entity))); 062 String key = "$beginMission_seedExtra"; 063 String extra = person.getMemoryWithoutUpdate().getString(key); 064 long seed = BarEventManager.getInstance().getSeed(null, person, extra); 065 person.getMemoryWithoutUpdate().set(key, "" + seed); // so it's not the same seed for multiple missions 066 mission.setGenRandom(new Random(seed)); 067 } 068 069 mission.createAndAbortIfFailed(entity.getMarket(), false); 070 071 if (mission.isMissionCreationAborted()) { 072 MemoryAPI memory = memoryMap.get(MemKeys.LOCAL); 073 if (memory == null && dialog.getInteractionTarget() != null) { 074 if (dialog.getInteractionTarget().getActivePerson() != null) { 075 memory = dialog.getInteractionTarget().getActivePerson().getMemoryWithoutUpdate(); 076 } else { 077 memory = dialog.getInteractionTarget().getMemoryWithoutUpdate(); 078 } 079 if (memory != null) { 080 memory.set("$missionCreationFailed", mission.getMissionId()); 081 } 082 } 083 return false; 084 } 085 086 if (accept) { 087 mission.accept(dialog, memoryMap); 088 } else { 089 Global.getSector().getMemoryWithoutUpdate().set(TEMP_MISSION_KEY, mission, 0f); 090 } 091 092 return true; 093 } 094} 095 096