001package com.fs.starfarer.api.impl.campaign; 002 003import java.util.HashMap; 004import java.util.Map; 005 006import com.fs.starfarer.api.Global; 007import com.fs.starfarer.api.campaign.CampaignFleetAPI; 008import com.fs.starfarer.api.campaign.CustomEntitySpecAPI; 009import com.fs.starfarer.api.campaign.FactionAPI; 010import com.fs.starfarer.api.campaign.RepLevel; 011import com.fs.starfarer.api.campaign.SectorEntityToken; 012import com.fs.starfarer.api.campaign.StarSystemAPI; 013import com.fs.starfarer.api.campaign.econ.MarketAPI; 014import com.fs.starfarer.api.campaign.rules.MemoryAPI; 015import com.fs.starfarer.api.campaign.rules.RuleTokenReplacementGeneratorPlugin; 016import com.fs.starfarer.api.characters.PersonAPI; 017import com.fs.starfarer.api.impl.campaign.ids.Factions; 018import com.fs.starfarer.api.impl.campaign.ids.MemFlags; 019import com.fs.starfarer.api.util.Misc; 020 021public class CoreRuleTokenReplacementGeneratorImpl implements RuleTokenReplacementGeneratorPlugin { 022 023 public Map<String, String> getTokenReplacements(String ruleId, Object entity, Map<String, MemoryAPI> memoryMap) { 024 025 SectorEntityToken target = null; 026 PersonAPI person = null; 027 MarketAPI market = null; 028 FactionAPI faction = null; 029 FactionAPI personFaction = null; 030 if (entity instanceof SectorEntityToken) { 031 target = (SectorEntityToken) entity; 032 market = target.getMarket(); 033 faction = target.getFaction(); 034 } 035 036 if (entity instanceof CampaignFleetAPI && market == null) { 037 market = Misc.getSourceMarket((CampaignFleetAPI) entity); 038 } 039 040 if (entity instanceof SectorEntityToken) { 041 person = ((SectorEntityToken) entity).getActivePerson(); 042 } 043 044 if (person == null) { 045 if (entity instanceof CampaignFleetAPI) { 046 person = ((CampaignFleetAPI) entity).getCommander(); 047 } else if (entity instanceof PersonAPI ){ 048 person = (PersonAPI) entity; // can't actually happen as entity is always a SectorEntityToken 049 } 050 } 051 052 if (person != null) { 053 personFaction = person.getFaction(); 054 } 055 056 CampaignFleetAPI playerFleet = Global.getSector().getPlayerFleet(); 057 058 Map<String, String> map = new HashMap<String, String>(); 059 060 map.put("$Playername", Global.getSector().getCharacterData().getName()); 061 map.put("$playername", Global.getSector().getCharacterData().getName()); 062 map.put("$playerName", Global.getSector().getCharacterData().getName()); 063 map.put("$PlayerName", Global.getSector().getCharacterData().getName()); 064 065 map.put("$playerFirstName", Global.getSector().getCharacterData().getPerson().getName().getFirst()); 066 map.put("$playerFirstname", Global.getSector().getCharacterData().getPerson().getName().getFirst()); 067 map.put("$playerLastName", Global.getSector().getCharacterData().getPerson().getName().getLast()); 068 map.put("$playerLastname", Global.getSector().getCharacterData().getPerson().getName().getLast()); 069 070 PersonAPI playerPerson = Global.getSector().getPlayerPerson(); 071 String honorific = Global.getSector().getCharacterData().getHonorific(); 072 if (playerPerson != null) { 073 if (playerPerson.isMale()) { 074 map.put("$playerSirOrMadam", "sir"); 075 map.put("$PlayerSirOrMadam", "Sir"); 076 077 map.put("$playerBrotherOrSister", "brother"); 078 map.put("$PlayerBrotherOrSister", "Brother"); 079 080 map.put("$playerHisOrHer", "his"); 081 map.put("$PlayerHisOrHer", "His"); 082 map.put("$playerHimOrHer", "him"); 083 map.put("$PlayerHimOrHer", "Him"); 084 map.put("$playerHeOrShe", "he"); 085 map.put("$PlayerHeOrShe", "He"); 086 } else { 087 map.put("$playerSirOrMadam", "ma'am"); 088 map.put("$PlayerSirOrMadam", "Ma'am"); 089 090 map.put("$playerBrotherOrSister", "sister"); 091 map.put("$PlayerBrotherOrSister", "Sister"); 092 093 map.put("$playerHisOrHer", "her"); 094 map.put("$PlayerHisOrHer", "Her"); 095 map.put("$playerHimOrHer", "her"); 096 map.put("$PlayerHimOrHer", "Her"); 097 map.put("$playerHeOrShe", "she"); 098 map.put("$PlayerHeOrShe", "She"); 099 } 100 101 if (honorific != null && !honorific.isEmpty()) { 102 map.put("$playerSirOrMadam", Misc.lcFirst(honorific)); 103 map.put("$PlayerSirOrMadam", honorific); 104 105 if (!Misc.SIR.toLowerCase().equals(honorific.toLowerCase()) && 106 !Misc.MAAM.toLowerCase().equals(honorific.toLowerCase())) { 107 map.put("$playerBrotherOrSister", "walker"); 108 map.put("$PlayerBrotherOrSister", "Walker"); 109 } 110 } 111 } 112 113 if (market != null) { 114 //map.put("$market", market.getName()); 115 map.put("$marketName", market.getName()); 116 117 if (target.getLocation() instanceof StarSystemAPI) { 118 map.put("$marketSystem", ((StarSystemAPI)target.getLocation()).getBaseName() + " star system"); 119 } else { 120 map.put("$marketSystem", "hyperspace"); 121 } 122 123 MemoryAPI mem = market.getMemoryWithoutUpdate(); 124 if (mem.contains(MemFlags.MEMORY_KEY_PLAYER_HOSTILE_ACTIVITY_NEAR_MARKET)) { 125 float expire = mem.getExpire(MemFlags.MEMORY_KEY_PLAYER_HOSTILE_ACTIVITY_NEAR_MARKET); 126 String days = Misc.getAtLeastStringForDays((int) expire); 127 map.put("$playerHostileTimeoutStr", days.toLowerCase()); 128 mem.set("$playerHostileTimeoutStr", days.toLowerCase(), 0); 129 } 130 } 131 132 if (target != null) { 133 map.put("$entityName", target.getName()); 134 map.put("$fleetName", target.getName().toLowerCase()); 135 map.put("$relayName", target.getName()); 136 137 138 if (target.getCustomEntityType() != null) { 139 if (target.getCustomEntitySpec() != null) { 140 CustomEntitySpecAPI spec = target.getCustomEntitySpec(); 141 map.put("$nameInText", spec.getNameInText()); 142 map.put("$shortName", spec.getShortName()); 143 map.put("$isOrAre", spec.getIsOrAre()); 144 map.put("$aOrAn", spec.getAOrAn()); 145 } 146 } 147 148// map.put("$factionEntityPrefix", target.getFaction().getEntityNamePrefix()); 149// map.put("$FactionEntityPrefix", Misc.ucFirst(target.getFaction().getEntityNamePrefix())); 150 if (target.getFaction() != null) { 151 String factionName = target.getFaction().getEntityNamePrefix(); 152 if (factionName == null || factionName.isEmpty()) { 153 factionName = target.getFaction().getDisplayName(); 154 } 155 156 map.put("$factionAOrAn", target.getFaction().getPersonNamePrefixAOrAn()); 157 158 map.put("$factionIsOrAre", target.getFaction().getDisplayNameIsOrAre()); 159 160 map.put("$faction", factionName); 161 map.put("$ownerFaction", factionName); 162 map.put("$marketFaction", factionName); 163 map.put("$Faction", Misc.ucFirst(factionName)); 164 map.put("$OwnerFaction", Misc.ucFirst(factionName)); 165 map.put("$MarketFaction", Misc.ucFirst(factionName)); 166 map.put("$theFaction", target.getFaction().getDisplayNameWithArticle()); 167 map.put("$theOwnerFaction", target.getFaction().getDisplayNameWithArticle()); 168 map.put("$theMarketFaction", target.getFaction().getDisplayNameWithArticle()); 169 map.put("$TheFaction", Misc.ucFirst(target.getFaction().getDisplayNameWithArticle())); 170 map.put("$TheOwnerFaction", Misc.ucFirst(target.getFaction().getDisplayNameWithArticle())); 171 map.put("$TheMarketFaction", Misc.ucFirst(target.getFaction().getDisplayNameWithArticle())); 172 173 map.put("$factionLong", target.getFaction().getDisplayNameLong()); 174 map.put("$FactionLong", Misc.ucFirst(target.getFaction().getDisplayNameLong())); 175 map.put("$theFactionLong", target.getFaction().getDisplayNameLongWithArticle()); 176 map.put("$TheFactionLong", Misc.ucFirst(target.getFaction().getDisplayNameLongWithArticle())); 177 } 178 } 179 180 if (target != null) { 181 map.put("$entityName", target.getName()); 182 map.put("$fleetName", target.getName().toLowerCase()); 183 map.put("$relayName", target.getName()); 184 185// map.put("$factionEntityPrefix", target.getFaction().getEntityNamePrefix()); 186// map.put("$FactionEntityPrefix", Misc.ucFirst(target.getFaction().getEntityNamePrefix())); 187 if (target.getFaction() != null) { 188 String factionName = target.getFaction().getEntityNamePrefix(); 189 if (factionName == null || factionName.isEmpty()) { 190 factionName = target.getFaction().getDisplayName(); 191 } 192 193 map.put("$factionIsOrAre", target.getFaction().getDisplayNameIsOrAre()); 194 195 map.put("$faction", factionName); 196 map.put("$ownerFaction", factionName); 197 map.put("$marketFaction", factionName); 198 map.put("$Faction", Misc.ucFirst(factionName)); 199 map.put("$OwnerFaction", Misc.ucFirst(factionName)); 200 map.put("$MarketFaction", Misc.ucFirst(factionName)); 201 map.put("$theFaction", target.getFaction().getDisplayNameWithArticle()); 202 map.put("$theOwnerFaction", target.getFaction().getDisplayNameWithArticle()); 203 map.put("$theMarketFaction", target.getFaction().getDisplayNameWithArticle()); 204 map.put("$TheFaction", Misc.ucFirst(target.getFaction().getDisplayNameWithArticle())); 205 map.put("$TheOwnerFaction", Misc.ucFirst(target.getFaction().getDisplayNameWithArticle())); 206 map.put("$TheMarketFaction", Misc.ucFirst(target.getFaction().getDisplayNameWithArticle())); 207 208 map.put("$factionLong", target.getFaction().getDisplayNameLong()); 209 map.put("$FactionLong", Misc.ucFirst(target.getFaction().getDisplayNameLong())); 210 map.put("$theFactionLong", target.getFaction().getDisplayNameLongWithArticle()); 211 map.put("$TheFactionLong", Misc.ucFirst(target.getFaction().getDisplayNameLongWithArticle())); 212 } 213 } 214 215 216 String shipOrFleet = "fleet"; 217 if (playerFleet.getFleetData().getMembersListCopy().size() == 1) { 218 shipOrFleet = "ship"; 219 if (playerFleet.getFleetData().getMembersListCopy().get(0).isFighterWing()) { 220 shipOrFleet = "fighter wing"; 221 } 222 } 223 map.put("$shipOrFleet", shipOrFleet); 224 map.put("$fleetOrShip", shipOrFleet); 225 map.put("$ShipOrFleet", Misc.ucFirst(shipOrFleet)); 226 map.put("$FleetOrShip", Misc.ucFirst(shipOrFleet)); 227 228 if (target instanceof CampaignFleetAPI) { 229 CampaignFleetAPI fleet = (CampaignFleetAPI) target; 230 String otherShipOrFleet = "fleet"; 231 if (fleet.getFleetData().getMembersListCopy().size() == 1) { 232 shipOrFleet = "ship"; 233 if (fleet.getFleetData().getMembersListCopy().get(0).isFighterWing()) { 234 shipOrFleet = "fighter wing"; 235 } 236 } 237 map.put("$otherShipOrFleet", otherShipOrFleet); 238 239 map.put("$otherFleetName", fleet.getName().toLowerCase()); 240 } 241 242 if (person != null) { 243 if (person.isMale()) { 244 map.put("$hisOrHer", "his"); 245 map.put("$HisOrHer", "His"); 246 map.put("$himOrHer", "him"); 247 map.put("$HimOrHer", "Him"); 248 map.put("$heOrShe", "he"); 249 map.put("$HeOrShe", "He"); 250 map.put("$himOrHerself", "himself"); 251 map.put("$HimOrHerself", "Himself"); 252 map.put("$manOrWoman", "man"); 253 map.put("$ManOrWoman", "Man"); 254 map.put("$brotherOrSister", "brother"); 255 map.put("$BrotherOrSister", "Brother"); 256 map.put("$sirOrMadam", "sir"); 257 map.put("$SirOrMadam", "Sir"); 258 } else { 259 map.put("$hisOrHer", "her"); 260 map.put("$HisOrHer", "Her"); 261 map.put("$himOrHer", "her"); 262 map.put("$HimOrHer", "Her"); 263 map.put("$heOrShe", "she"); 264 map.put("$HeOrShe", "She"); 265 map.put("$himOrHerself", "herself"); 266 map.put("$HimOrHerself", "Herself"); 267 map.put("$manOrWoman", "woman"); 268 map.put("$ManOrWoman", "Woman"); 269 map.put("$brotherOrSister", "sister"); 270 map.put("$BrotherOrSister", "Sister"); 271 map.put("$sirOrMadam", "ma'am"); 272 map.put("$SirOrMadam", "Ma'am"); 273 } 274 275 if (person.getRank() != null) { 276 map.put("$personRank", person.getRank().toLowerCase()); 277 map.put("$PersonRank", Misc.ucFirst(person.getRank())); 278 } 279 280 if (person.getPost() != null) { 281 map.put("$personPost", person.getPost().toLowerCase()); 282 map.put("$PersonPost", Misc.ucFirst(person.getPost())); 283 } 284 285 map.put("$PersonName", person.getName().getFullName()); 286 map.put("$personName", person.getName().getFullName()); 287 map.put("$personFirstName", person.getName().getFirst()); 288 map.put("$personLastName", person.getName().getLast()); 289 } 290 291 292 if (faction != null) { 293 float rel = faction.getRelationship(Factions.PLAYER); 294 RepLevel level = RepLevel.getLevelFor(rel); 295 map.put("$relAdjective", level.getDisplayName().toLowerCase()); 296 } 297 298 if (personFaction != null) { 299 String factionName = personFaction.getEntityNamePrefix(); 300 if (factionName == null || factionName.isEmpty()) { 301 factionName = personFaction.getDisplayName(); 302 } 303 304 map.put("$personFactionIsOrAre", personFaction.getDisplayNameIsOrAre()); 305 306 map.put("$personFaction", factionName); 307 map.put("$PersonFaction", Misc.ucFirst(factionName)); 308 map.put("$thePersonFaction", personFaction.getDisplayNameWithArticle()); 309 map.put("$ThePersonFaction", Misc.ucFirst(personFaction.getDisplayNameWithArticle())); 310 311 map.put("$personFactionLong", personFaction.getDisplayNameLong()); 312 map.put("$PersonFactionLong", Misc.ucFirst(personFaction.getDisplayNameLong())); 313 map.put("$thePersonFactionLong", personFaction.getDisplayNameLongWithArticle()); 314 map.put("$ThePersonFactionLong", Misc.ucFirst(personFaction.getDisplayNameLongWithArticle())); 315 } 316 317 return map; 318 } 319 320} 321 322 323 324 325 326 327 328 329