001package com.fs.starfarer.api.impl;
002
003import java.util.HashMap;
004import java.util.Map;
005
006import com.fs.starfarer.api.Global;
007import com.fs.starfarer.api.impl.codex.CodexDataV2;
008import com.fs.starfarer.api.impl.codex.CodexEntryPlugin;
009import com.fs.starfarer.api.impl.codex.CodexIntelAdder;
010
011/**
012 * DO NOT store references to the instance anywhere that makes them end up in the savefile,
013 * this does not belong in the campaign savefile.
014 * @author Alex
015 *
016 */
017public class SharedUnlockData extends BaseSharedJSONFile {
018
019        public static String SHARED_UNLOCKS_DATA_FILE = "core_shared_unlocks.json";
020        
021        public static String ITEMS = "items";
022        public static String ILLUSTRATIONS = "illustrations";
023        public static String SHIPS = "ships";
024        public static String FIGHTERS = "fighters";
025        public static String WEAPONS = "weapons";
026        
027        public static String SHIP_SYSTEMS = "ship_systems";
028        public static String HULLMODS = "hullmods";
029        public static String COMMODITIES = "commodities";
030        public static String INDUSTRIES = "industries";
031        public static String PLANETS = "planets";
032        public static String CONDITIONS = "conditions";
033        public static String SKILLS = "skills";
034        public static String ABILITIES = "abilities";
035        
036        protected static SharedUnlockData instance;
037        
038        public static SharedUnlockData get() {
039                if (instance == null) {
040                        instance = new SharedUnlockData();
041                        instance.loadIfNeeded();
042                }
043                return instance;
044        }
045
046        @Override
047        protected String getFilename() {
048                return SHARED_UNLOCKS_DATA_FILE;
049        }
050        
051        public boolean isPlayerAwareOfSpecialItem(String itemId) {
052                return getSet(ITEMS).contains(itemId);
053        }
054        
055        public boolean reportPlayerAwareOfSpecialItem(String itemId, boolean withSave) {
056                return reportPlayerAwareOfThing(itemId, ITEMS, CodexDataV2.getItemEntryId(itemId), withSave);
057        }
058        
059        public boolean isPlayerAwareOfIllustration(String key) {
060                return getSet(ILLUSTRATIONS).contains(key);
061        }
062        
063        public boolean reportPlayerAwareOfIllustration(String key, boolean withSave) {
064                return reportPlayerAwareOfThing(key, ILLUSTRATIONS, CodexDataV2.getGalleryEntryId(key), withSave);
065        }
066        
067        public boolean isPlayerAwareOfShip(String hullId) {
068                return getSet(SHIPS).contains(hullId);
069        }
070        
071        public boolean reportPlayerAwareOfShip(String hullId, boolean withSave) {
072                return reportPlayerAwareOfThing(hullId, SHIPS, CodexDataV2.getShipEntryId(hullId), withSave);
073        }
074        
075        public boolean isPlayerAwareOfFighter(String fighterId) {
076                return getSet(FIGHTERS).contains(fighterId);
077        }
078        
079        public boolean reportPlayerAwareOfFighter(String fighterId, boolean withSave) {
080                return reportPlayerAwareOfThing(fighterId, FIGHTERS, CodexDataV2.getFighterEntryId(fighterId), withSave);
081        }
082        
083        public boolean isPlayerAwareOfWeapon(String weaponId) {
084                return getSet(WEAPONS).contains(weaponId);
085        }
086        
087        public boolean reportPlayerAwareOfWeapon(String weaponId, boolean withSave) {
088                return reportPlayerAwareOfThing(weaponId, WEAPONS, CodexDataV2.getWeaponEntryId(weaponId), withSave);
089        }
090        
091        
092        public boolean isPlayerAwareOfShipSystem(String sysId) {
093                return getSet(SHIP_SYSTEMS).contains(sysId);
094        }
095        
096        public boolean reportPlayerAwareOfShipSystem(String sysId, boolean withSave) {
097                return reportPlayerAwareOfThing(sysId, SHIP_SYSTEMS, CodexDataV2.getShipSystemEntryId(sysId), withSave);
098        }
099        
100        public boolean isPlayerAwareOfHullmod(String hullmodId) {
101                return getSet(HULLMODS).contains(hullmodId);
102        }
103        
104        public boolean reportPlayerAwareOfHullmod(String hullmodId, boolean withSave) {
105                return reportPlayerAwareOfThing(hullmodId, HULLMODS, CodexDataV2.getHullmodEntryId(hullmodId), withSave);
106        }
107        
108        public boolean isPlayerAwareOfCommodity(String commodityId) {
109                return getSet(COMMODITIES).contains(commodityId);
110        }
111        
112        public boolean reportPlayerAwareOfCommodity(String commodityId, boolean withSave) {
113                return reportPlayerAwareOfThing(commodityId, COMMODITIES, CodexDataV2.getCommodityEntryId(commodityId), withSave);
114        }
115        
116        public boolean isPlayerAwareOfIndustry(String industryId) {
117                return getSet(INDUSTRIES).contains(industryId);
118        }
119        
120        public boolean reportPlayerAwareOfIndustry(String industryId, boolean withSave) {
121                return reportPlayerAwareOfThing(industryId, INDUSTRIES, CodexDataV2.getIndustryEntryId(industryId), withSave);
122        }
123        
124        public boolean isPlayerAwareOfPlanet(String planetId) {
125                return getSet(PLANETS).contains(planetId);
126        }
127        
128        public boolean reportPlayerAwareOfPlanet(String planetId, boolean withSave) {
129                return reportPlayerAwareOfThing(planetId, PLANETS, CodexDataV2.getPlanetEntryId(planetId), withSave);
130        }
131        
132        public boolean isPlayerAwareOfCondition(String conditionId) {
133                return getSet(CONDITIONS).contains(conditionId);
134        }
135        
136        public boolean reportPlayerAwareOfCondition(String conditionId, boolean withSave) {
137                return reportPlayerAwareOfThing(conditionId, CONDITIONS, CodexDataV2.getConditionEntryId(conditionId), withSave);
138        }
139        
140        public boolean isPlayerAwareOfSkill(String skillId) {
141                return getSet(SKILLS).contains(skillId);
142        }
143        
144        public boolean reportPlayerAwareOfSkill(String skillId, boolean withSave) {
145                return reportPlayerAwareOfThing(skillId, SKILLS, CodexDataV2.getSkillEntryId(skillId), withSave);
146        }
147        
148        public boolean isPlayerAwareOfAbility(String abilityId) {
149                return getSet(ABILITIES).contains(abilityId);
150        }
151        
152        public boolean reportPlayerAwareOfAbility(String abilityId, boolean withSave) {
153                return reportPlayerAwareOfThing(abilityId, ABILITIES, CodexDataV2.getAbilityEntryId(abilityId), withSave);
154        }
155        
156        public static Map<String, String> ILLUSTRATION_KEY_LOOKUP = null;
157        
158        public void checkIfImageIsIllustrationAndMakeAware(String spriteName) {
159                if (spriteName == null) return;
160                if (ILLUSTRATION_KEY_LOOKUP == null) {
161                        ILLUSTRATION_KEY_LOOKUP = new HashMap<>();
162                        for (String key : Global.getSettings().getSpriteKeys("illustrations")) {
163                                String sprite = Global.getSettings().getSpriteName("illustrations", key);
164                                ILLUSTRATION_KEY_LOOKUP.put(sprite, key);
165                        }
166                }
167                
168                String key = ILLUSTRATION_KEY_LOOKUP.get(spriteName);
169                if (key != null) {
170                        reportPlayerAwareOfIllustration(key, true);
171                }
172        }
173        
174        
175        protected boolean reportPlayerAwareOfThing(String thingId, String setId, String codexEntryId, boolean withSave) {
176                boolean wasLocked = isEntryLocked(codexEntryId);
177                if (addToSet(setId, thingId)) {
178                        if (wasLocked && !isEntryLocked(codexEntryId)) CodexIntelAdder.get().addEntry(codexEntryId);
179                        if (withSave) saveIfNeeded();
180                        return true;
181                }
182                return false;
183        }
184        
185        public boolean isEntryLocked(String entryId) {
186                CodexEntryPlugin entry = CodexDataV2.getEntry(entryId);
187                return entry != null && entry.isLocked();
188        }
189}
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209