Starsector API
Loading...
Searching...
No Matches
PopulationComposition.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.population;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.LinkedHashMap;
7import java.util.List;
8
9import com.fs.starfarer.api.combat.MutableStat;
10import com.fs.starfarer.api.combat.MutableStat.StatMod;
11import com.fs.starfarer.api.util.Misc;
12
14
15 private LinkedHashMap<String, Float> comp = new LinkedHashMap<String, Float>();
16 //private float weight;
17 private MutableStat weight = new MutableStat(0f);
18// private float leaving;
19
20 Object readResolve() {
21 if (weight == null ){
22 weight = new MutableStat(0f);
23 }
24 return this;
25 }
26
27 public LinkedHashMap<String, Float> getComp() {
28 return comp;
29 }
30
31 public float get(String id) {
32 Float val = comp.get(id);
33 if (val == null) return 0f;
34 return val;
35 }
36
37 public void set(String id, float value) {
38 comp.put(id, value);
39 }
40
41 public void add(String id, float value) {
42 set(id, get(id) + value);
43 }
44
45
46 @Override
47 public String toString() {
48 String str = "";
49 if (!weight.getFlatMods().containsKey(SET_WEIGHT_ID)) {
50 for (StatMod mod : weight.getFlatMods().values()) {
51 if (mod.value > 0) {
52 str += "<b>+" + (int)mod.getValue() + "</b> " + mod.getDesc() + "\n";
53 } else {
54 str += "<b>" + (int) mod.getValue() + "</b> " + mod.getDesc() + "\n";
55 }
56 }
57 str += "\n";
58 }
59 List<String> keys = new ArrayList<String>(comp.keySet());
60 Collections.sort(keys, new Comparator<String>() {
61 public int compare(String o1, String o2) {
62 return (int) Math.signum(get(o2) - get(o1));
63 }
64 });
65
66
67 for (String key : keys) {
68 //float val = Math.round(get(key));
69 float val = Misc.getRoundedValueFloat(get(key));
70 str += key + ": " + val + "\n";
71 }
72
73 return str;
74 }
75
76 public float getWeightValue() {
77 return weight.getModifiedValue();
78 }
79
80 public float getPositiveWeight() {
81 float total = 0;
82 for (StatMod mod : weight.getFlatMods().values()) {
83 if (mod.value > 0) {
84 total += mod.value;
85 }
86 }
87 return total;
88 }
89
90 public float getNegativeWeight() {
91 float total = 0;
92 for (StatMod mod : weight.getFlatMods().values()) {
93 if (mod.value < 0) {
94 total -= mod.value;
95 }
96 }
97 return total;
98 }
99
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();
126 }
127
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