001package com.fs.starfarer.api.impl.campaign.fleets; 002 003import java.util.Random; 004 005import com.fs.starfarer.api.EveryFrameScript; 006import com.fs.starfarer.api.Global; 007import com.fs.starfarer.api.campaign.BattleAPI; 008import com.fs.starfarer.api.campaign.CampaignEventListener.FleetDespawnReason; 009import com.fs.starfarer.api.campaign.CampaignFleetAPI; 010import com.fs.starfarer.api.campaign.econ.MarketAPI; 011import com.fs.starfarer.api.campaign.listeners.FleetEventListener; 012import com.fs.starfarer.api.characters.PersonAPI; 013import com.fs.starfarer.api.impl.campaign.ids.MemFlags; 014import com.fs.starfarer.api.impl.campaign.ids.People; 015import com.fs.starfarer.api.util.Misc; 016 017public abstract class PersonalFleetScript implements EveryFrameScript, FleetEventListener { 018 019 protected String personId; 020 021 protected float minRespawnDelayDays = 1f; 022 protected float maxRespawnDelayDays = 2f; 023 024 protected float minFailedSpawnRespawnDelayDays = 1f; 025 protected float maxFailedSpawnRespawnDelayDays = 2f; 026 protected float currDelay; 027 protected CampaignFleetAPI fleet; 028 protected Random random = new Random(); 029 protected boolean done = false; 030 protected String defeatTrigger = null; 031 032 protected float origFP = 0f; 033 034 public PersonalFleetScript(String personId) { 035 this.personId = personId; 036 Global.getSector().addScript(this); 037 } 038 039 public PersonAPI getPerson() { 040 return People.getPerson(personId); 041 } 042 043 public boolean isDone() { 044 return done; 045 } 046 047 public boolean runWhilePaused() { 048 return false; 049 } 050 051 public String getDefeatTrigger() { 052 return defeatTrigger; 053 } 054 055 public void setDefeatTrigger(String defeatTrigger) { 056 this.defeatTrigger = defeatTrigger; 057 } 058 059 protected abstract MarketAPI getSourceMarket(); 060 061 public void advance(float amount) { 062 if (amount <= 0 || isDone()) return; 063 064 if (fleet != null && !fleet.isAlive()) { 065 fleet = null; 066 } 067 068 if (fleet == null) { 069 float days = Global.getSector().getClock().convertToDays(amount); 070 currDelay -= days; 071 if (currDelay <= 0f) { 072 currDelay = 0f; 073 074 if (shouldScriptBeRemoved() || getPerson() == null) { 075 done = true; 076 return; 077 } 078 079 if (canSpawnFleetNow()) { 080 fleet = spawnFleet(); 081 if (fleet != null) { 082 origFP = fleet.getFleetPoints(); 083 MarketAPI source = getSourceMarket(); 084 if (source != null) { 085 fleet.getMemoryWithoutUpdate().set(MemFlags.MEMORY_KEY_SOURCE_MARKET, source.getId()); 086 } 087 fleet.addEventListener(this); 088 if (defeatTrigger != null) { 089 Misc.addDefeatTrigger(fleet, defeatTrigger); 090 } 091 } 092 } 093 if (fleet == null) { 094 currDelay = minFailedSpawnRespawnDelayDays + 095 (maxFailedSpawnRespawnDelayDays - minFailedSpawnRespawnDelayDays) * random.nextFloat(); 096 } 097 } 098 } 099 } 100 101 102 public abstract CampaignFleetAPI spawnFleet(); 103 public abstract boolean canSpawnFleetNow(); 104 public abstract boolean shouldScriptBeRemoved(); 105 106 public void reportFleetDespawnedToListener(CampaignFleetAPI fleet, FleetDespawnReason reason, Object param) { 107 if (fleet == this.fleet) { 108 this.fleet = null; 109 currDelay = minRespawnDelayDays + 110 (maxRespawnDelayDays - minRespawnDelayDays) * random.nextFloat(); 111 } 112 } 113 114 public void reportBattleOccurred(CampaignFleetAPI fleet, CampaignFleetAPI primaryWinner, BattleAPI battle) { 115 float currFP = fleet.getFleetPoints(); 116 if (currFP < origFP * 0.33f && !Misc.isFleetReturningToDespawn(fleet)) { 117 Misc.giveStandardReturnToSourceAssignments(fleet); 118 } 119 } 120 121 public String getPersonId() { 122 return personId; 123 } 124 125 public void setPersonId(String personId) { 126 this.personId = personId; 127 } 128 129 public float getMinRespawnDelayDays() { 130 return minRespawnDelayDays; 131 } 132 133 public void setMinRespawnDelayDays(float minRespawnDelayDays) { 134 this.minRespawnDelayDays = minRespawnDelayDays; 135 } 136 137 public float getMaxRespawnDelayDays() { 138 return maxRespawnDelayDays; 139 } 140 141 public void setMaxRespawnDelayDays(float maxRespawnDelayDays) { 142 this.maxRespawnDelayDays = maxRespawnDelayDays; 143 } 144 145 public float getMinFailedSpawnRespawnDelayDays() { 146 return minFailedSpawnRespawnDelayDays; 147 } 148 149 public void setMinFailedSpawnRespawnDelayDays(float minFailedSpawnRespawnDelayDays) { 150 this.minFailedSpawnRespawnDelayDays = minFailedSpawnRespawnDelayDays; 151 } 152 153 public float getMaxFailedSpawnRespawnDelayDays() { 154 return maxFailedSpawnRespawnDelayDays; 155 } 156 157 public void setMaxFailedSpawnRespawnDelayDays(float maxFailedSpawnRespawnDelayDays) { 158 this.maxFailedSpawnRespawnDelayDays = maxFailedSpawnRespawnDelayDays; 159 } 160 161 public float getCurrDelay() { 162 return currDelay; 163 } 164 165 public void setCurrDelay(float currDelay) { 166 this.currDelay = currDelay; 167 } 168 169 public CampaignFleetAPI getFleet() { 170 return fleet; 171 } 172 173 public void setFleet(CampaignFleetAPI fleet) { 174 this.fleet = fleet; 175 } 176 177 public Random getRandom() { 178 return random; 179 } 180 181 public void setRandom(Random random) { 182 this.random = random; 183 } 184 185 186} 187