001package com.fs.starfarer.api.impl.campaign.rulecmd.salvage; 002 003import java.util.List; 004import java.util.Map; 005import java.util.Random; 006 007import com.fs.starfarer.api.EveryFrameScript; 008import com.fs.starfarer.api.Global; 009import com.fs.starfarer.api.campaign.CargoAPI; 010import com.fs.starfarer.api.campaign.InteractionDialogAPI; 011import com.fs.starfarer.api.campaign.SectorEntityToken; 012import com.fs.starfarer.api.campaign.TextPanelAPI; 013import com.fs.starfarer.api.campaign.econ.MarketAPI; 014import com.fs.starfarer.api.campaign.listeners.GroundRaidObjectivesListener; 015import com.fs.starfarer.api.campaign.rules.MemoryAPI; 016import com.fs.starfarer.api.impl.campaign.graid.BaseGroundRaidObjectivePluginImpl; 017import com.fs.starfarer.api.impl.campaign.graid.GroundRaidObjectivePlugin; 018import com.fs.starfarer.api.impl.campaign.rulecmd.BaseCommandPlugin; 019import com.fs.starfarer.api.impl.campaign.rulecmd.FireAll; 020import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.MarketCMD.RaidDangerLevel; 021import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.MarketCMD.RaidType; 022import com.fs.starfarer.api.ui.TooltipMakerAPI; 023import com.fs.starfarer.api.util.Misc.Token; 024 025/** 026 * Icon id is under raidObjectives in settings.json. 027 * 028 * Actually it seems to work with both; never mind the below -Alex 029 * 030 * Note: this method ONLY works when option id "mktRaidNonMarket" is used as the option entering the raid menu. 031 * A standard raid will not include the options added with these. In particular, this is because this command 032 * specifies a trigger to call when going back from the raid menu, and that doesn't make sense in the context of 033 * a "normal" raid. 034 * 035 * AddRaidObjective <icon id> <name to show> <danger> <xp gained> <trigger to run when successful> 036 * <optional:show in custom raid menu only> <optional: tooltip> 037 */ 038public class AddRaidObjective extends BaseCommandPlugin { 039 040 public static class CustomRaidObjective extends BaseGroundRaidObjectivePluginImpl { 041 public CustomRaidObjectiveAdder adder; 042 043 public CustomRaidObjective(CustomRaidObjectiveAdder adder) { 044 super(adder.market, null); 045 this.adder = adder; 046 int marines = adder.danger.marineTokens; 047 setMarinesRequired(marines); 048 } 049 050 public boolean withContinueBeforeResult() { 051 return true; 052 } 053 054 @Override 055 public String getQuantityString(int marines) { 056 return ""; 057 } 058 @Override 059 public String getValueString(int marines) { 060 return ""; 061 } 062 public float getValueSortValue() { 063 return super.getValueSortValue(); 064 } 065 public int getCargoSpaceNeeded() { 066 return 0; 067 } 068 public int getFuelSpaceNeeded() { 069 return 0; 070 } 071 public int getProjectedCreditsValue() { 072 return 0; 073 } 074 public RaidDangerLevel getDangerLevel() { 075 return adder.danger; 076 } 077 @Override 078 public int getValue(int marines) { 079 return 0; 080 } 081 public float getQuantitySortValue() { 082 float add = adder.name.hashCode(); 083 return QUANTITY_SORT_TIER_0 + add; 084 } 085 public String getName() { 086 return adder.name; 087 } 088 @Override 089 public String getIconName() { 090 return Global.getSettings().getSpriteName("raidObjectives", adder.icon); 091 } 092 @Override 093 public float getQuantity(int marines) { 094 return 0; 095 } 096 public int performRaid(CargoAPI loot, Random random, float lootMult, TextPanelAPI text) { 097 return adder.xp; 098 } 099 100 @Override 101 public boolean hasTooltip() { 102 return adder.tooltip != null; 103 } 104 105 @Override 106 public void createTooltip(TooltipMakerAPI t, boolean expanded) { 107 t.addPara(adder.tooltip, 0f); 108 } 109 } 110 111 public static class CustomRaidObjectiveAdder implements EveryFrameScript, GroundRaidObjectivesListener { 112 protected boolean done = false; 113 114 public String icon; 115 public String name; 116 public String trigger; 117 public String tooltip; 118 public int xp = 0; 119 public boolean showInCustomOnly; 120 public RaidDangerLevel danger; 121 public MarketAPI market; 122 public SectorEntityToken entity; 123 124 public CustomRaidObjectiveAdder(MarketAPI market, SectorEntityToken entity, String icon, String name, String trigger, int xp, RaidDangerLevel danger, boolean showInCustomOnly, String tooltip) { 125 this.market = market; 126 this.entity = entity; 127 this.icon = icon; 128 this.name = name; 129 this.trigger = trigger; 130 this.tooltip = tooltip; 131 this.xp = xp; 132 this.danger = danger; 133 this.showInCustomOnly = showInCustomOnly; 134 for (CustomRaidObjectiveAdder adder : Global.getSector().getListenerManager().getListeners(CustomRaidObjectiveAdder.class)) { 135 if (adder.name.equals(name) && adder.trigger.equals(trigger)) { 136 return; 137 } 138 } 139 Global.getSector().getListenerManager().addListener(this); 140 Global.getSector().addScript(this); 141 } 142 public void advance(float amount) { 143 if (amount > 0 && !done) { 144 for (CustomRaidObjectiveAdder adder : Global.getSector().getListenerManager().getListeners(CustomRaidObjectiveAdder.class)) { 145 if (adder.name.equals(name) && adder.trigger.equals(trigger)) { 146 Global.getSector().getListenerManager().removeListener(this); 147 done = true; 148 break; 149 } 150 } 151 } 152 } 153 public boolean isDone() { 154 return done; 155 } 156 public boolean runWhilePaused() { 157 return false; 158 } 159 public void modifyRaidObjectives(MarketAPI market, SectorEntityToken entity, List<GroundRaidObjectivePlugin> objectives, RaidType type, int marineTokens, int priority) { 160 if (priority != 0) return; 161 162 if (type == RaidType.DISRUPT) return; 163 if (type != RaidType.CUSTOM_ONLY && showInCustomOnly) return; 164 165 if (type == RaidType.CUSTOM_ONLY && entity != null && 166 entity.getMemoryWithoutUpdate().contains("$raidRestrictToTrigger")) { 167 String restrict = entity.getMemoryWithoutUpdate().getString("$raidRestrictToTrigger"); 168 if (restrict != null && !restrict.isEmpty() && !restrict.equals(trigger)) { 169 return; 170 } 171 } 172 173 if ((this.market != null && market == this.market) || 174 (this.entity != null && entity == this.entity)) { 175 CustomRaidObjective custom = new CustomRaidObjective(this); 176 objectives.add(custom); 177 } 178 } 179 180 public void reportRaidObjectivesAchieved(RaidResultData data, InteractionDialogAPI dialog, Map<String, MemoryAPI> memoryMap) { 181 boolean found = false; 182 for (GroundRaidObjectivePlugin obj : data.objectives) { 183 if (obj instanceof CustomRaidObjective) { 184 CustomRaidObjective custom = (CustomRaidObjective) obj; 185 if (custom.adder == this) { 186 found = true; 187 break; 188 } 189 } 190 } 191 if (found) { 192 advance(0.1f); // triggers removal of objective 193 dialog.getInteractionTarget().getMemoryWithoutUpdate().set("$raidMarinesLost", data.marinesLost, 0); 194 FireAll.fire(null, dialog, memoryMap, trigger); 195 } 196 } 197 } 198 199 200 201 public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) { 202 String icon = params.get(0).getString(memoryMap); 203 String name = params.get(1).getStringWithTokenReplacement(ruleId, dialog, memoryMap); 204 RaidDangerLevel danger = RaidDangerLevel.valueOf(RaidDangerLevel.class, params.get(2).getString(memoryMap)); 205 int xp = (int) params.get(3).getFloat(memoryMap); 206 String trigger = params.get(4).getString(memoryMap); 207 208 boolean showInCustomOnly = false; 209 210 String tooltip = null; 211 if (params.size() > 5) { 212 if (params.size() > 6) { 213 showInCustomOnly = params.get(5).getBoolean(memoryMap); 214 tooltip = params.get(6).getStringWithTokenReplacement(params.get(5).getString(memoryMap), dialog, memoryMap); 215 } else { 216 String str = params.get(5).getString(memoryMap); 217 if (str != null) { 218 if (str.toLowerCase().equals("true") || str.toLowerCase().equals("false")) { 219 showInCustomOnly = params.get(5).getBoolean(memoryMap); 220 } else { 221 tooltip = params.get(5).getStringWithTokenReplacement(params.get(5).getString(memoryMap), dialog, memoryMap); 222 } 223 } 224 } 225 } 226 227 SectorEntityToken entity = dialog.getInteractionTarget(); 228 MarketAPI market = entity.getMarket(); 229 230 new CustomRaidObjectiveAdder(market, entity, icon, name, trigger, xp, danger, showInCustomOnly, tooltip); 231 232 return true; 233 } 234 235} 236 237 238 239