001package com.fs.starfarer.api.impl.campaign.intel.inspection; 002 003import java.util.Random; 004 005import com.fs.starfarer.api.EveryFrameScript; 006import com.fs.starfarer.api.Global; 007import com.fs.starfarer.api.campaign.econ.Industry; 008import com.fs.starfarer.api.campaign.econ.MarketAPI; 009import com.fs.starfarer.api.impl.campaign.DebugFlags; 010import com.fs.starfarer.api.impl.campaign.ids.Commodities; 011import com.fs.starfarer.api.impl.campaign.ids.Factions; 012import com.fs.starfarer.api.impl.campaign.ids.MemFlags; 013import com.fs.starfarer.api.util.IntervalUtil; 014import com.fs.starfarer.api.util.Misc; 015import com.fs.starfarer.api.util.WeightedRandomPicker; 016 017public class HegemonyInspectionManager implements EveryFrameScript { 018 019 public static final String KEY = "$core_hegemonyInspectionManager"; 020 021 public static final float MAX_THRESHOLD = 1000f; 022 public static final float FREQ_MULT = Global.getSettings().getFloat("aiInspectionFrequencyMult"); 023 024 public static HegemonyInspectionManager getInstance() { 025 Object test = Global.getSector().getMemoryWithoutUpdate().get(KEY); 026 return (HegemonyInspectionManager) test; 027 } 028 029 public HegemonyInspectionManager() { 030 super(); 031 Global.getSector().getMemoryWithoutUpdate().set(KEY, this); 032 } 033 034 protected Object readResolve() { 035 if (inspectionChecker == null) { 036 inspectionChecker = new IntervalUtil(20f, 40f); 037 } 038 return this; 039 } 040 041 protected IntervalUtil inspectionChecker = new IntervalUtil(20f, 40f); 042 043 protected float suspicion = 0f; 044 protected float threshold = 250f; 045 protected float inspectionDelay = 0f;; 046 protected int numAttempts = 0; 047 048 public void advance(float amount) { 049 if (true) return; // replaced with colony crisis sending inspection 050 051 float days = Misc.getDays(amount); 052 if (intel != null) { 053 if (intel.isEnded()) { 054 intel = null; 055 inspectionDelay = 100f + 100f * random.nextFloat(); 056 } 057 } else { 058 inspectionDelay -= days; 059 if (inspectionDelay <= 0) inspectionDelay = 0; 060 } 061 062 if (DebugFlags.HEGEMONY_INSPECTION_DEBUG) { 063 days *= 1000f; 064 inspectionDelay = 0f; 065 suspicion = 1000f; 066 } 067 068 inspectionChecker.advance(days * FREQ_MULT); 069 if (inspectionChecker.intervalElapsed() && intel == null && inspectionDelay <= 0) { 070 checkInspection(); 071 } 072 } 073 074 protected void checkInspection() { 075 float total = 0f; 076 for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) { 077 if (market.isPlayerOwned()) { 078 if (market.isInHyperspace()) continue; 079 total += getAICoreUseValue(market); 080 } 081 } 082 083 //suspicion += total; 084 suspicion += total * (0.25f + random.nextFloat() * 0.75f); 085 086 //suspicion += 100000; 087 088 if (suspicion >= threshold) { 089 createInspection(); 090 } 091 } 092 093 protected Random random = new Random(); 094 protected HegemonyInspectionIntel intel = null; 095 public void createInspection() { 096 createInspection(null); 097 } 098 public void createInspection(Integer fpOverride) { 099 100 MarketAPI target = null; 101 float max = 0f; 102 for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) { 103 if (market.isPlayerOwned()) { 104 if (market.isInHyperspace()) continue; 105 float curr = getAICoreUseValue(market); 106 if (curr > max) { 107 target = market; 108 max = curr; 109 } 110 } 111 } 112 113 if (target != null && max > 0) { 114 WeightedRandomPicker<MarketAPI> picker = new WeightedRandomPicker<MarketAPI>(random); 115 for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) { 116 if (market.getFactionId().equals(Factions.HEGEMONY)) { 117 if (market.getMemoryWithoutUpdate().getBoolean(MemFlags.MARKET_MILITARY)) { 118 picker.add(market, market.getSize()); 119 } 120 } 121 } 122 MarketAPI from = picker.pick(); 123 if (from == null) return; 124 125 float fp = 50 + threshold * 0.5f; 126 //fp = 500; 127 if (fpOverride != null) { 128 fp = fpOverride; 129 } 130 intel = new HegemonyInspectionIntel(from, target, fp); 131 if (intel.isDone()) { 132 intel = null; 133 return; 134 } 135 } else { 136 return; 137 } 138 139 numAttempts++; 140 suspicion = 0f; 141 threshold *= 2f; 142 if (threshold > MAX_THRESHOLD) { 143 threshold = MAX_THRESHOLD; 144 } 145 } 146 147 public int getNumAttempts() { 148 return numAttempts; 149 } 150 151 public static float getAICoreUseValue(MarketAPI market) { 152 float total = 0f; 153 154 String aiCoreId = market.getAdmin().getAICoreId(); 155 if (aiCoreId != null) { 156 total += 10f; 157 } 158 159 for (Industry ind : market.getIndustries()) { 160 String id = ind.getAICoreId(); 161 float w = 0f; 162 if (Commodities.ALPHA_CORE.equals(id)) { 163 w = 4f; 164 } else if (Commodities.BETA_CORE.equals(id)) { 165 w = 2f; 166 } else if (Commodities.GAMMA_CORE.equals(id)) { 167 w = 1f; 168 } 169 total += w; 170 } 171 172 return total; 173 } 174 175 public boolean isDone() { 176 return false; 177 } 178 179 public boolean runWhilePaused() { 180 return false; 181 } 182 183 public float getThreshold() { 184 return threshold; 185 } 186 187 188} 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203