001package com.fs.starfarer.api.impl.campaign; 002 003import com.fs.starfarer.api.Global; 004import com.fs.starfarer.api.campaign.CampaignEngineLayers; 005import com.fs.starfarer.api.campaign.CustomCampaignEntityAPI; 006import com.fs.starfarer.api.campaign.SectorEntityToken; 007import com.fs.starfarer.api.campaign.SectorEntityToken.VisibilityLevel; 008import com.fs.starfarer.api.combat.ViewportAPI; 009import com.fs.starfarer.api.impl.campaign.ids.Entities; 010import com.fs.starfarer.api.util.Misc; 011 012public class CargoPodsEntityPlugin extends BaseCustomEntityPlugin { 013 014 public static float computeDetectionRange(float radius) { 015 float range = 500f + radius * 20f; 016 if (range > 2000) range = 2000; 017 return range; 018 } 019 020 021 //protected CustomCampaignEntityAPI entity; 022 protected transient GenericFieldItemManager manager; 023 024 protected float elapsed = 0; 025 protected float maxDays = 1f; 026 protected float extraDays = 0f; 027 protected Boolean neverExpire = null; 028 029 public void init(SectorEntityToken entity, Object pluginParams) { 030 super.init(entity, pluginParams); 031 entity.setDetectionRangeDetailsOverrideMult(0.5f); 032 //this.entity = (CustomCampaignEntityAPI) entity; 033 readResolve(); 034 035// entity.getMemoryWithoutUpdate().set("$locked", true); 036// entity.getMemoryWithoutUpdate().set("$canUnlock", true); 037// entity.getMemoryWithoutUpdate().set("$trapped", true); 038 } 039 040 Object readResolve() { 041 manager = new GenericFieldItemManager(entity); 042 manager.category = "misc"; 043 manager.key = "cargoPods"; 044 manager.cellSize = 32; 045 046 manager.minSize = 10; 047 manager.maxSize = 10; 048 //manager.numPieces = 15; 049 050 return this; 051 } 052 053 public void advance(float amount) { 054 float days = Global.getSector().getClock().convertToDays(amount); 055 056 float depth = Misc.getAbyssalDepth(entity); 057 if (depth >= 1f) { 058 days *= 5f; 059 } 060 061 elapsed += days; 062 063 if (!isNeverExpire()) { 064 if (elapsed >= maxDays + extraDays && maxDays >= 0) { 065 VisibilityLevel vis = entity.getVisibilityLevelToPlayerFleet(); 066 boolean playerCanSee = entity.isInCurrentLocation() && 067 (vis == VisibilityLevel.COMPOSITION_AND_FACTION_DETAILS || 068 vis == VisibilityLevel.COMPOSITION_DETAILS); 069 if (!playerCanSee) { 070 maxDays = -1; 071 Misc.fadeAndExpire(entity); 072 neverExpire = true; 073 } 074 } 075 } 076 077 if (entity.isInCurrentLocation()) { 078 updateBaseMaxDays(); 079 float radius = 10f + 10f * (float) Math.sqrt(manager.numPieces); 080 081 float range = computeDetectionRange(radius); 082 entity.getDetectedRangeMod().modifyFlat("gen", range); 083 } 084 085 manager.advance(amount); 086 } 087 088 public void updateBaseMaxDays() { 089 if (entity == null || entity.getCargo() == null) return; 090 091 float totalCapacity = entity.getCargo().getSpaceUsed() + 092 entity.getCargo().getFuel() + 093 entity.getCargo().getTotalPersonnel(); 094 095 int minPieces = 5; 096 int numPieces = (int) (Math.sqrt(totalCapacity) / 1); 097 if (numPieces < minPieces) numPieces = minPieces; 098 if (numPieces > 40) numPieces = 40; 099 100 boolean cryo = entity.getCargo().getTotalPersonnel() > entity.getCargo().getSpaceUsed() + entity.getCargo().getFuel(); 101 if (cryo) { 102 entity.setCustomDescriptionId("cryopods"); 103 entity.setName("Cryo Pods"); 104 } else { 105 entity.setCustomDescriptionId(Entities.CARGO_PODS); 106 entity.setName("Cargo Pods"); 107 } 108 109 manager.numPieces = numPieces; 110 111 float radius = 10f + 10f * (float) Math.sqrt(manager.numPieces - (minPieces - 1)); 112 ((CustomCampaignEntityAPI)entity).setRadius(radius); 113 114 maxDays = 5f + (numPieces - minPieces); 115 } 116 117 public float getRenderRange() { 118 return entity.getRadius() + 100f; 119 } 120 121 public void render(CampaignEngineLayers layer, ViewportAPI viewport) { 122 manager.render(layer, viewport); 123 } 124 125 public void setNeverExpire(Boolean neverExpire) { 126 this.neverExpire = neverExpire; 127 } 128 129 public Boolean isNeverExpire() { 130 return neverExpire != null && neverExpire; 131 } 132 133 public float getDaysLeft() { 134 return maxDays + extraDays - elapsed; 135 } 136 137 138 public float getElapsed() { 139 return elapsed; 140 } 141 142 public void setElapsed(float elapsed) { 143 this.elapsed = elapsed; 144 } 145 146 public float getExtraDays() { 147 return extraDays; 148 } 149 150 public void setExtraDays(float extraDays) { 151 this.extraDays = extraDays; 152 } 153 154} 155 156 157