001package squidpony.store.text;
002
003import com.badlogic.gdx.utils.reflect.ClassReflection;
004import squidpony.*;
005import squidpony.squidgrid.mapping.PoliticalMapper;
006import squidpony.squidgrid.mapping.SpillWorldMap;
007import squidpony.squidmath.*;
008
009import java.util.List;
010
011import static squidpony.Converters.*;
012
013/**
014 * Created by Tommy Ettinger on 4/22/2017.
015 */
016@SuppressWarnings("unchecked")
017public class BonusConverters {
018    public static final StringConvert<OrderedMap<Character, String>> convertMapCharString =
019            Converters.convertOrderedMap(Converters.convertChar, Converters.convertString);
020    public static final StringConvert<List<FakeLanguageGen>> convertListLanguage = Converters.convertList(Converters.convertFakeLanguageGen);
021    public static final StringConvert<OrderedMap<Character, List<FakeLanguageGen>>> convertMapCharListLanguage
022            = Converters.convertOrderedMap(Converters.convertChar, convertListLanguage);
023    public static final StringConvert<StatefulRNG> convertStatefulRNG = new StringConvert<StatefulRNG>("StatefulRNG") {
024        @Override
025        public String stringify(StatefulRNG item) {
026            return StringKit.hex(item.getState()) + ':' + (item.getRandomness().getClass().getSimpleName());
027        }
028
029        @Override
030        public StatefulRNG restore(String text) {
031            long state = StringKit.longFromHex(text);
032            try {
033                StatefulRandomness sr = (StatefulRandomness) ClassReflection.newInstance(ClassReflection.forName(text.substring(text.indexOf(':') + 1)));
034                sr.setState(state);
035                return new StatefulRNG(sr);
036            }catch (Exception re)
037            {
038                return new StatefulRNG(state);
039            }
040        }
041    };
042    public static final StringConvert<StatefulRandomness> convertStatefulRandomness = new StringConvert<StatefulRandomness>("StatefulRandomness") {
043        @Override
044        public String stringify(StatefulRandomness item) {
045            return StringKit.hex(item.getState()) + ':' + (item.getClass().getSimpleName());
046        }
047
048        @Override
049        public StatefulRandomness restore(String text) {
050            long state = StringKit.longFromHex(text);
051            try {
052                StatefulRandomness sr = (StatefulRandomness) ClassReflection.newInstance(ClassReflection.forName(text.substring(text.indexOf(':') + 1)));
053                sr.setState(state);
054                return sr;
055            }catch (Exception re)
056            {
057                return new DiverRNG(state);
058            }
059        }
060    };
061
062    public static final StringConvert<RNG> convertRNG = new StringConvert<RNG>("RNG") {
063        @Override
064        public String stringify(RNG item) {
065            return "RNG:" + (item.getRandomness().getClass().getSimpleName());
066        }
067
068        @Override
069        public RNG restore(String text) {
070            try {
071                RandomnessSource rs = (RandomnessSource) ClassReflection.newInstance(ClassReflection.forName(text.substring(text.indexOf(':') + 1)));
072                return new RNG(rs);
073            }catch (Exception re)
074            {
075                return new RNG();
076            }
077        }
078    };
079    public static <K> StringConvert<ProbabilityTable<K>> convertProbabilityTable(final StringConvert<K> convert) {
080        CharSequence[] types = StringConvert.asArray("ProbabilityTable", convert.name);
081        StringConvert found = StringConvert.lookup(types);
082        if (found != null)
083            return found; // in this case we've already created a StringConvert for this type combination
084        final StringConvert<Arrangement<K>> convertArrange = Converters.convertArrangement(convert);
085        return new StringConvert<ProbabilityTable<K>>(types) {
086            @Override
087            public String stringify(ProbabilityTable<K> item) {
088                StringBuilder sb = new StringBuilder(256);
089                appendQuoted(sb, StringKit.hex(item.getRandom().getState()));
090                sb.append(' ');
091                appendQuoted(sb, convertIntVLA.stringify(item.weights));
092                sb.append(' ');
093                appendQuoted(sb, convertArrange.stringify(item.table));
094                for (int i = 0; i < item.extraTable.size(); i++) {
095                    sb.append(' ');
096                    appendQuoted(sb, stringify(item.extraTable.get(i)));
097                }
098                return sb.toString();
099            }
100
101            @Override
102            public ProbabilityTable<K> restore(String text) {
103                ObText.ContentMatcher m = makeMatcher(text);
104                if(!m.find() || !m.hasMatch())
105                    return null;
106                ProbabilityTable<K> pt = new ProbabilityTable<>(StringKit.longFromHex(m.getMatch()));
107                if(!m.find() || !m.hasMatch())
108                    return pt;
109                pt.weights.addAll(convertIntVLA.restore(m.getMatch()));
110                if(!m.find() || !m.hasMatch())
111                {
112                    pt.weights.clear();
113                    return pt;
114                }
115                pt.table.putAll(convertArrange.restore(m.getMatch()));
116                while (m.find()) {
117                    if (m.hasMatch()) {
118                        pt.extraTable.add(restore(m.getMatch()));
119                    }
120                }
121                return pt;
122            }
123        };
124    }
125
126    public static <K> StringConvert<ProbabilityTable<K>> convertProbabilityTable(final CharSequence type) {
127        return convertProbabilityTable((StringConvert<K>) StringConvert.get(type));
128    }
129
130    public static <K> StringConvert<ProbabilityTable<K>> convertProbabilityTable(final Class<K> type) {
131        return convertProbabilityTable((StringConvert<K>) StringConvert.get(type.getSimpleName()));
132    }
133
134    public static final StringConvert<SpillWorldMap> convertSpillWorldMap = new StringConvert<SpillWorldMap>("SpillWorldMap") {
135        @Override
136        public String stringify(SpillWorldMap item) {
137            return item.width
138                    + "\t" + item.height
139                    + "\t" + item.name
140                    + '\t' + Converters.convertArrayInt2D.stringify(item.heightMap)
141                    + '\t' + Converters.convertArrayCoord.stringify(item.mountains)
142                    + '\t' + Converters.convertArrayChar2D.stringify(item.politicalMap)
143                    + '\t' + convertMapCharString.stringify(item.atlas)
144                    + '\t' + convertStatefulRNG.stringify(item.rng);
145        }
146
147        @Override
148        public SpillWorldMap restore(String text) {
149            int pos;
150            SpillWorldMap swm = new SpillWorldMap(
151                    Integer.decode(text.substring(0, (pos = text.indexOf('\t')))),
152                    Integer.decode(text.substring(pos+1, (pos = text.indexOf('\t', pos+1)))),
153                    text.substring(pos+1, (pos = text.indexOf('\t', pos+1)))
154            );
155            swm.heightMap = Converters.convertArrayInt2D.restore(text.substring(pos+1, (pos = text.indexOf('\t', pos+1))));
156            swm.mountains = Converters.convertArrayCoord.restore(text.substring(pos+1, (pos = text.indexOf('\t', pos+1))));
157            swm.politicalMap = Converters.convertArrayChar2D.restore(text.substring(pos+1, (pos = text.indexOf('\t', pos+1))));
158            swm.atlas.clear();
159            swm.atlas.putAll(convertMapCharString.restore(text.substring(pos+1, (pos = text.indexOf('\t', pos+1)))));
160            swm.rng = convertStatefulRNG.restore(text.substring(pos+1));
161            return swm;
162        }
163    };
164
165    public static final StringConvert<PoliticalMapper> convertPoliticalMapper = new StringConvert<PoliticalMapper>("PoliticalMapper") {
166        @Override
167        public String stringify(PoliticalMapper item) {
168            return item.width
169                    + "\t" + item.height
170                    + "\t" + item.name
171                    + '\t' + Converters.convertArrayChar2D.stringify(item.politicalMap)
172                    + '\t' + convertMapCharString.stringify(item.atlas)
173                    + '\t' + convertMapCharListLanguage.stringify(item.spokenLanguages)
174                    + '\t' + convertStatefulRNG.stringify(item.rng);
175        }
176
177        @Override
178        public PoliticalMapper restore(String text) {
179            int pos;
180            PoliticalMapper pm = new PoliticalMapper();
181            pm.width = Integer.decode(text.substring(0, (pos = text.indexOf('\t'))));
182            pm.height = Integer.decode(text.substring(pos+1, (pos = text.indexOf('\t', pos+1))));
183            pm.name = text.substring(pos+1, (pos = text.indexOf('\t', pos+1)));
184            pm.politicalMap = Converters.convertArrayChar2D.restore(text.substring(pos+1, (pos = text.indexOf('\t', pos+1))));
185            pm.atlas.clear();
186            pm.atlas.putAll(convertMapCharString.restore(text.substring(pos+1, (pos = text.indexOf('\t', pos+1)))));
187            pm.spokenLanguages.clear();
188            pm.spokenLanguages.putAll(convertMapCharListLanguage.restore(text.substring(pos+1, (pos = text.indexOf('\t', pos+1)))));
189            pm.rng = convertStatefulRNG.restore(text.substring(pos+1));
190            return pm;
191        }
192    };
193
194
195}