001package com.fs.starfarer.api.impl.campaign.intel.events;
002
003import java.util.ArrayList;
004import java.util.Iterator;
005import java.util.List;
006import java.util.Random;
007
008import java.awt.Color;
009
010import com.fs.starfarer.api.Global;
011import com.fs.starfarer.api.campaign.CampaignFleetAPI;
012import com.fs.starfarer.api.campaign.StarSystemAPI;
013import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin.ListInfoMode;
014import com.fs.starfarer.api.campaign.econ.Industry;
015import com.fs.starfarer.api.campaign.econ.MarketAPI;
016import com.fs.starfarer.api.impl.campaign.ids.Industries;
017import com.fs.starfarer.api.impl.campaign.intel.events.BaseEventIntel.EventStageData;
018import com.fs.starfarer.api.impl.campaign.intel.events.HostileActivityEventIntel.HAERandomEventData;
019import com.fs.starfarer.api.ui.Alignment;
020import com.fs.starfarer.api.ui.TooltipMakerAPI;
021import com.fs.starfarer.api.ui.TooltipMakerAPI.TooltipCreator;
022import com.fs.starfarer.api.ui.TooltipMakerAPI.TooltipLocation;
023import com.fs.starfarer.api.util.Misc;
024
025/**
026 * A base event factor with added code for managing multiple hostile activity causes.
027 * 
028 * @author Alex
029 *
030 * Copyright 2022 Fractal Softworks, LLC
031 */
032@SuppressWarnings("unused")
033public class BaseHostileActivityFactor extends BaseEventFactor implements HostileActivityFactor {
034
035        protected HostileActivityEventIntel intel;
036        protected List<HostileActivityCause2> causes = new ArrayList<HostileActivityCause2>();
037        
038        protected long seed;
039        
040
041        public BaseHostileActivityFactor(HostileActivityEventIntel intel) {
042                this.intel = intel;
043        }
044        
045        public int getProgress(BaseEventIntel intel) {
046                int total = 0;
047                for (HostileActivityCause2 cause : getCauses()) {
048                        total += cause.getProgress();
049                }
050                return total;
051        }
052        
053        
054        public void addExtraRows(TooltipMakerAPI info, BaseEventIntel intel) {
055                for (HostileActivityCause2 cause : getCauses()) {
056                        if (!cause.shouldShow()) continue;
057                        
058                        String desc = cause.getDesc();
059                        if (desc != null) {
060                                info.addRowWithGlow(Alignment.LMID, cause.getDescColor(intel), "    " + desc,
061                                                                    Alignment.RMID, cause.getProgressColor(intel), cause.getProgressStr());
062                                TooltipCreator t = cause.getTooltip();
063                                if (t != null) {
064                                        info.addTooltipToAddedRow(t, TooltipLocation.RIGHT, false);
065                                }
066                        }
067                        cause.addExtraRows(info, intel);
068                }
069        }
070        
071        public String getId() {
072                return getClass().getSimpleName();
073        }
074        
075        public float getSpawnFrequency(StarSystemAPI system) {
076//              if (this instanceof PerseanLeagueHostileActivityFactor) {
077//                      return 10000f;
078//              }
079                return getEffectMagnitude(system);
080        }
081
082        public float getSpawnInHyperProbability(StarSystemAPI system) {
083                return 0.25f;
084        }
085
086        
087        public float getStayInHyperProbability(StarSystemAPI system) {
088                return 0.25f;
089        }
090
091        public int getMaxNumFleets(StarSystemAPI system) {
092                return 1000;
093        }
094
095//      public float getEffectMagnitudeAdjustedBySuppression(StarSystemAPI system) {
096//              float mag = getEffectMagnitude(system);
097//              //float s = intel.computeSuppressionAmount();
098//              // currently, keep fleets the same size when suppressed, too
099//              // otherwise, the fights just get less fun as you're trying to suppress more
100//              return mag;
101//      }
102        
103        public float getEffectMagnitude(StarSystemAPI system) {//, boolean adjustByEventProgress) {
104                float mag = 0f;
105                for (HostileActivityCause2 cause : causes) {
106                        mag += cause.getMagnitudeContribution(system);
107                }
108//              if (adjustByEventProgress) {
109//                      float f = intel.getProgressFraction();
110//                      float add = f * 0.5f;
111//                      add = Math.min(add, 1f - mag); wefwefwefewfwe
112//                      if (mag < f) {
113//                              mag = Misc.interpolate(mag, f, 0.5f);
114//                      }
115//              }
116                return mag;
117        }
118
119        public void addCause(HostileActivityCause2 cause) {
120                causes.add(cause);
121        }
122
123        public List<HostileActivityCause2> getCauses() {
124                return causes;
125        }
126        
127        @SuppressWarnings("rawtypes")
128        public void removeCauseOfClass(Class c) {
129                Iterator<HostileActivityCause2> iter = causes.iterator();
130                while (iter.hasNext()) {
131                        HostileActivityCause2 curr = iter.next();
132                        if (curr.getClass() == c) {
133                                iter.remove();
134                        }
135                }
136        }
137        
138        @SuppressWarnings("rawtypes")
139        public HostileActivityCause2 getCauseOfClass(Class c) {
140                Iterator<HostileActivityCause2> iter = causes.iterator();
141                while (iter.hasNext()) {
142                        HostileActivityCause2 curr = iter.next();
143                        if (curr.getClass() == c) {
144                                return curr;
145                        }
146                }
147                return null;
148        }
149
150        public CampaignFleetAPI createFleet(StarSystemAPI system, Random random) {
151                return null;
152        }
153
154        public String getNameForThreatList(boolean first) {
155                return getDesc(intel);
156        }
157
158        public Color getNameColorForThreatList() {
159                return getDescColor(intel);
160        }
161
162        public float getEventFrequency(HostileActivityEventIntel intel, EventStageData stage) {
163                return 0;
164        }
165
166        public void rollEvent(HostileActivityEventIntel intel, EventStageData stage) {
167                
168        }
169
170        public void addBulletPointForEvent(HostileActivityEventIntel intel, EventStageData stage, TooltipMakerAPI info,
171                                                                                 ListInfoMode mode, boolean isUpdate, Color tc, float initPad) {
172                
173        }
174
175        public void addStageDescriptionForEvent(HostileActivityEventIntel intel, EventStageData stage, TooltipMakerAPI info) {
176                
177        }
178
179        public String getEventStageIcon(HostileActivityEventIntel intel, EventStageData stage) {
180                return null;
181        }
182
183        public TooltipCreator getStageTooltipImpl(HostileActivityEventIntel intel, EventStageData stage) {
184                return null;
185        }
186        
187        public TooltipCreator getDefaultEventTooltip(final String title, final HostileActivityEventIntel intel, final EventStageData stage) {
188                return new BaseFactorTooltip() {
189                        @Override
190                        public void createTooltip(TooltipMakerAPI tooltip, boolean expanded, Object tooltipParam) {
191                                tooltip.addTitle(title);
192                                stage.endResetReqList(tooltip, true, "crisis", 
193                                                HostileActivityEventIntel.RESET_MIN, HostileActivityEventIntel.RESET_MAX);
194                        }
195                };
196        }
197
198        public void resetEvent(HostileActivityEventIntel intel, EventStageData stage) {
199                HAERandomEventData data = (HAERandomEventData) stage.rollData;
200                intel.sendUpdateIfPlayerHasIntel(data, false);
201                stage.rollData = null;          
202        }
203
204        public void addBulletPointForEventReset(HostileActivityEventIntel intel, EventStageData stage, TooltipMakerAPI info,
205                        ListInfoMode mode, boolean isUpdate, Color tc, float initPad) {
206                
207        }
208
209        public boolean fireEvent(HostileActivityEventIntel intel, EventStageData stage) {
210                return false;
211        }
212
213        
214        public void setRandomizedStageSeed(long seed) {
215                this.seed = seed;
216        }
217        public long getRandomizedStageSeed() {
218                return seed;
219        }
220        
221        public Random getRandomizedStageRandom(int level) {
222                return Misc.getRandom(seed, level);
223        }
224        public Random getRandomizedStageRandom() {
225                return new Random(seed);
226        }
227
228        public String getEventStageSound(HAERandomEventData data) {
229                return "colony_threat";
230        }
231
232        public static boolean checkFactionExists(String factionId, boolean requireMilitary) {
233                for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) {
234                        if (market.getFactionId().equals(factionId)) {
235                                Industry b = market.getIndustry(Industries.MILITARYBASE);
236                                if (b == null) b = market.getIndustry(Industries.HIGHCOMMAND);
237                                if (b != null || !requireMilitary) {
238                                        return true;
239                                }
240                        }
241                }       
242                return false;
243        }
244
245//      @Override
246//      public boolean canEscalate(HostileActivityEventIntel intel, EventStageData stage) {
247//              return true;
248//      }
249}
250
251
252