001package com.fs.starfarer.api.impl.campaign.missions.hub; 002 003import com.fs.starfarer.api.EveryFrameScript; 004import com.fs.starfarer.api.Global; 005import com.fs.starfarer.api.campaign.CampaignEventListener.FleetDespawnReason; 006import com.fs.starfarer.api.campaign.CampaignFleetAPI; 007import com.fs.starfarer.api.campaign.FleetAssignment; 008import com.fs.starfarer.api.campaign.ai.FleetAssignmentDataAPI; 009import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin; 010import com.fs.starfarer.api.impl.campaign.ids.Factions; 011import com.fs.starfarer.api.impl.campaign.ids.MemFlags; 012import com.fs.starfarer.api.impl.campaign.missions.DelayedFleetEncounter; 013import com.fs.starfarer.api.util.Misc; 014 015public class MissionFleetAutoDespawn implements EveryFrameScript { 016 017 protected CampaignFleetAPI fleet; 018 protected float elapsedWaitingForDespawn = 0f; 019 protected float elapsed = 0f; 020 protected HubMission mission; 021 022 public MissionFleetAutoDespawn(HubMission mission, CampaignFleetAPI fleet) { 023 this.mission = mission; 024 this.fleet = fleet; 025 } 026 027 protected int framesWithNoAssignment = 0; 028 public void advance(float amount) { 029 elapsed += amount; 030// System.out.println("MFAD: " + fleet.getName()); 031// if (fleet.getName().equals("Courier")) { 032// System.out.println("fwefwf23f"); 033// } 034// System.out.println(fleet.getName()); 035// if (fleet.getName().equals("Mercenary Bounty Hunter")) { 036// System.out.println("efwefweew"); 037// } 038 boolean missionImportant = fleet.getMemoryWithoutUpdate().getBoolean(MemFlags.ENTITY_MISSION_IMPORTANT); 039 FleetAssignmentDataAPI ad = fleet.getCurrentAssignment(); 040 // make a hopefully reasonable guess that the fleet should stop chasing the player, if that's what it's doing 041 // it it's not "important" (i.e. likely wrong mission stage) and not in the same location and far away 042 if (ad != null && 043 (ad.getAssignment() == FleetAssignment.INTERCEPT || ad.getAssignment() == FleetAssignment.FOLLOW) && 044 ad.getTarget() == Global.getSector().getPlayerFleet() && 045 !missionImportant && elapsed > 10f) { 046 CampaignFleetAPI pf = Global.getSector().getPlayerFleet(); 047 if (!fleet.isInCurrentLocation()) { 048 float dist = Misc.getDistanceLY(fleet, Global.getSector().getPlayerFleet()); 049 if (dist > 4f) { 050 fleet.removeFirstAssignment(); 051 if (fleet.getCurrentAssignment() == null) { 052 Misc.giveStandardReturnToSourceAssignments(fleet); 053 } 054 } 055 } 056 else if (fleet.isHostileTo(pf) && fleet.getFaction() != null && 057 !fleet.getFaction().isHostileTo(Factions.PLAYER) && 058 !(mission instanceof DelayedFleetEncounter)) { 059 fleet.removeFirstAssignment(); 060 if (fleet.getCurrentAssignment() == null) { 061 Misc.giveStandardReturnToSourceAssignments(fleet); 062 } 063 } 064 } 065 066// clean up, figure out details of exactly how to do this 067// - don't want to do it super quick when still during the mission 068// - probably want to give some kind of assignment if there's nothing left after removing the intercept 069// - and also when the mission's ended, give return assignments too? still auto-despawn! just, have it 070// be returning instead of sitting around waiting 071 072 if (isMissionEnded()) { 073 if (fleet.getCurrentAssignment() == null) { 074 Misc.giveStandardReturnToSourceAssignments(fleet); 075 } 076 077 if (!fleet.isInCurrentLocation() && Misc.getDistanceToPlayerLY(fleet) > 3f) { 078 elapsedWaitingForDespawn += Global.getSector().getClock().convertToDays(amount); 079 if (elapsedWaitingForDespawn > 30f && fleet.getBattle() == null) { 080 fleet.despawn(FleetDespawnReason.PLAYER_FAR_AWAY, null); 081 elapsedWaitingForDespawn = 0f; 082 } 083 } else { 084 elapsedWaitingForDespawn = 0f; 085 } 086 087 } else if (fleet.isInCurrentLocation() && fleet.getCurrentAssignment() == null) { 088 framesWithNoAssignment++; 089 if (framesWithNoAssignment >= 10) { 090 Misc.giveStandardReturnToSourceAssignments(fleet); 091 } 092 } else { 093 framesWithNoAssignment = 0; 094 } 095 } 096 097 public boolean isMissionEnded() { 098 return mission instanceof IntelInfoPlugin && (((IntelInfoPlugin)mission).isEnded() || ((IntelInfoPlugin)mission).isEnding()); 099 } 100 101 public boolean isDone() { 102 return false; 103 } 104 105 public boolean runWhilePaused() { 106 return false; 107 } 108 109 110} 111 112 113 114 115 116 117 118 119 120