001package com.fs.starfarer.api.impl.campaign.population;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.Comparator;
006import java.util.LinkedHashMap;
007import java.util.List;
008
009import com.fs.starfarer.api.combat.MutableStat;
010import com.fs.starfarer.api.combat.MutableStat.StatMod;
011import com.fs.starfarer.api.util.Misc;
012
013public class PopulationComposition {
014        
015        private LinkedHashMap<String, Float> comp = new LinkedHashMap<String, Float>();
016        //private float weight;
017        private MutableStat weight = new MutableStat(0f);
018//      private float leaving;
019        
020        Object readResolve() {
021                if (weight == null ){
022                        weight = new MutableStat(0f);
023                }
024                return this;
025        }
026        
027        public LinkedHashMap<String, Float> getComp() {
028                return comp;
029        }
030
031        public float get(String id) {
032                Float val = comp.get(id);
033                if (val == null) return 0f;
034                return val;
035        }
036        
037        public void set(String id, float value) {
038                comp.put(id, value);
039        }
040        
041        public void add(String id, float value) {
042                set(id, get(id) + value);
043        }
044
045        
046        @Override
047        public String toString() {
048                String str = "";
049                if (!weight.getFlatMods().containsKey(SET_WEIGHT_ID)) {
050                        for (StatMod mod : weight.getFlatMods().values()) {
051                                if (mod.value > 0) {
052                                        str += "<b>+" + (int)mod.getValue() + "</b> " + mod.getDesc() + "\n";
053                                } else {
054                                        str += "<b>" + (int) mod.getValue() + "</b> " + mod.getDesc() + "\n";
055                                }
056                        }
057                        str += "\n";
058                }
059                List<String> keys = new ArrayList<String>(comp.keySet());
060                Collections.sort(keys, new Comparator<String>() {
061                        public int compare(String o1, String o2) {
062                                return (int) Math.signum(get(o2) - get(o1));
063                        }
064                });
065                
066                
067                for (String key : keys) {
068                        //float val = Math.round(get(key));
069                        float val = Misc.getRoundedValueFloat(get(key));
070                        str += key + ": " + val + "\n";
071                }
072                
073                return str;
074        }
075
076        public float getWeightValue() {
077                return weight.getModifiedValue();
078        }
079        
080        public float getPositiveWeight() {
081                float total = 0;
082                for (StatMod mod : weight.getFlatMods().values()) {
083                        if (mod.value > 0) {
084                                total += mod.value;
085                        }
086                }
087                return total;
088        }
089        
090        public float getNegativeWeight() {
091                float total = 0;
092                for (StatMod mod : weight.getFlatMods().values()) {
093                        if (mod.value < 0) {
094                                total -= mod.value;
095                        }
096                }
097                return total;
098        }
099        
100//      public void addWeight(float weight) {
101//              this.weight += weight;
102//      }
103        
104        public void setWeight(float weight) {
105                this.weight.modifyFlat(SET_WEIGHT_ID, weight);
106        }
107        
108        public static final String SET_WEIGHT_ID = "core_set";
109        public void updateWeight() {
110                //Global.getSettings().profilerBegin("updateWeight()");
111                float total = 0;
112                for (Float f : comp.values()) {
113                        total += f;
114                }
115                weight.modifyFlat(SET_WEIGHT_ID, total);
116                //Global.getSettings().profilerEnd();
117        }
118
119        public MutableStat getWeight() {
120                return weight;
121        }
122
123        public void normalize() {
124                float w = weight.getModifiedValue();
125                normalizeToWeight(w);
126        }
127        
128        public void normalizeToPositive() {
129                normalizeToWeight(getPositiveWeight());
130        }
131        
132        public void normalizeToWeight(float w) {
133                float total = 0f;
134                for (Float f : comp.values()) {
135                        total += f;
136                }
137                
138                if (w <= 0 || total <= 0) {
139                        for (String id : comp.keySet()) {
140                                set(id, 0);
141                        }
142                        return;
143                }
144                
145                for (String id : comp.keySet()) {
146                        float f = get(id);
147                        set(id, f * w / total);
148                }
149        }
150        
151//      public float getLeaving() {
152//              return leaving;
153//      }
154//
155//      public void setLeaving(float negative) {
156//              this.leaving = negative;
157//      }
158        
159        
160}
161
162
163
164
165
166
167
168
169