001package com.fs.starfarer.api.impl.campaign.procgen;
002
003import java.awt.Color;
004import java.util.HashMap;
005import java.util.HashSet;
006import java.util.Map;
007import java.util.Set;
008
009import org.json.JSONException;
010import org.json.JSONObject;
011
012public class PlanetGenDataSpec implements EntityGenDataSpec {
013
014        
015        //id    category        frequency       habOffsetMin    habOffsetMax    minRadius       maxRadius       minColor        maxColor
016        private String id, category;
017        private float frequency, habOffsetMin, habOffsetMax, minRadius, maxRadius;
018        private float probOrbits, minOrbits, maxOrbits;
019        private float habOffsetYOUNG, habOffsetAVERAGE, habOffsetOLD;
020        private Color minColor, maxColor;
021        
022        private Map<String, Float> multipliers = new HashMap<String, Float>();
023        private Set<String> tags = new HashSet<String>();
024        
025        public PlanetGenDataSpec(JSONObject row) throws JSONException {
026                id = row.getString("id");
027                category = row.getString("category");
028                frequency = (float) row.getDouble("frequency");
029                habOffsetMin = (float) row.optDouble("habOffsetMin", -1000);
030                habOffsetMax = (float) row.optDouble("habOffsetMax", 1000);
031                minRadius = (float) row.optDouble("minRadius", 0);
032                maxRadius = (float) row.optDouble("maxRadius", 0);
033                
034                habOffsetYOUNG = (float) row.optDouble("habOffsetYOUNG", 0);
035                habOffsetAVERAGE = (float) row.optDouble("habOffsetAVERAGE", 0);
036                habOffsetOLD = (float) row.optDouble("habOffsetOLD", 0);
037                
038                probOrbits = (float) row.optDouble("probOrbits", 0);
039                minOrbits = (float) row.optDouble("minOrbits", 0);
040                maxOrbits = (float) row.optDouble("maxOrbits", 0);
041                
042                for (String key : JSONObject.getNames(row)) {
043                        float frequency = (float) row.optDouble(key, 1f);
044                        if (frequency != 1) {
045                                multipliers.put(key, frequency);
046                        }
047                }
048                
049                String tags = row.optString("tags", null);
050                if (tags != null) {
051                        String [] split = tags.split(",");
052                        for (String tag : split) {
053                                tag = tag.trim();
054                                if (tag.isEmpty()) continue;
055                                addTag(tag);
056                        }
057                }
058                
059                minColor = StarGenDataSpec.parseColor(row.optString("minColor", null) + " 255", " ");
060                maxColor = StarGenDataSpec.parseColor(row.optString("maxColor", null) + " 255", " ");
061        }
062        
063        public String toString() {
064                return getId();
065        }
066
067        public String getCategory() {
068                return category;
069        }
070
071        public float getFrequency() {
072                return frequency;
073        }
074        
075        public String getId() {
076                return id;
077        }
078
079        public float getHabOffsetMin() {
080                return habOffsetMin;
081        }
082
083        public float getHabOffsetMax() {
084                return habOffsetMax;
085        }
086
087        public float getMinRadius() {
088                return minRadius;
089        }
090
091        public float getMaxRadius() {
092                return maxRadius;
093        }
094
095        public Color getMinColor() {
096                return minColor;
097        }
098
099        public Color getMaxColor() {
100                return maxColor;
101        }
102
103        public float getMultiplier(String key) {
104                if (!multipliers.containsKey(key)) return 1f;
105                return multipliers.get(key);
106        }
107        
108        public Set<String> getTags() {
109                return tags;
110        }
111        
112        public void addTag(String tag) {
113                tags.add(tag);
114        }
115
116        public boolean hasTag(String tag) {
117                return tags.contains(tag);
118        }
119
120        public Map<String, Float> getMultipliers() {
121                return multipliers;
122        }
123
124        public float getProbOrbits() {
125                return probOrbits;
126        }
127
128        public float getMinOrbits() {
129                return minOrbits;
130        }
131
132        public float getMaxOrbits() {
133                return maxOrbits;
134        }
135
136        public float getHabOffsetYOUNG() {
137                return habOffsetYOUNG;
138        }
139
140        public float getHabOffsetAVERAGE() {
141                return habOffsetAVERAGE;
142        }
143
144        public float getHabOffsetOLD() {
145                return habOffsetOLD;
146        }
147        
148}