Starsector API
Loading...
Searching...
No Matches
MutableStatWithTempMods.java
Go to the documentation of this file.
1package com.fs.starfarer.api.combat;
2
3import java.util.Iterator;
4import java.util.LinkedHashMap;
5import java.util.Map;
6
13
14 public static class TemporaryStatMod {
15 float timeRemaining; // days or seconds, whatever
16 String source;
17 public TemporaryStatMod(float timeRemaining, String source) {
18 this.timeRemaining = timeRemaining;
19 this.source = source;
20 }
21 }
22
23 private LinkedHashMap<String, TemporaryStatMod> tempMods = new LinkedHashMap<String, TemporaryStatMod>();
24
26 super(base);
27 }
28
29 protected Object readResolve() {
30 super.readResolve();
31// if (tempMods == null) {
32// tempMods = new LinkedHashMap<String, TemporaryStatMod>();
33// }
34// if (flatAfterMult == null) {
35// flatAfterMult = new HashMap<String, StatMod>();
36// }
37 return this;
38 }
39
40 protected Object writeReplace() {
41 if (tempMods != null && getMods().isEmpty()) {
42 tempMods = null;
43 }
44 return this;
45 }
46
47
48 public void removeTemporaryMod(String source) {
49 TemporaryStatMod mod = getMods().remove(source);
50 if (mod == null) return;
51
52 unmodify(mod.source);
53 }
54
55 private TemporaryStatMod getMod(String source, float durInDays) {
56 TemporaryStatMod mod = getMods().get(source);
57 if (mod == null) {
58 mod = new TemporaryStatMod(durInDays, source);
59 getMods().put(source, mod);
60 }
61 mod.timeRemaining = durInDays;
62 return mod;
63 }
64
65 public void addTemporaryModFlat(float durInDays, String source, String desc, float value) {
66 getMod(source, durInDays);
67 modifyFlat(source, value, desc);
68 }
69 public void addTemporaryModMult(float durInDays, String source, String desc, float value) {
70 getMod(source, durInDays);
71 modifyMult(source, value, desc);
72 }
73 public void addTemporaryModFlat(float durInDays, String source, float value) {
74 getMod(source, durInDays);
75 modifyFlat(source, value);
76 }
77 public void addTemporaryModPercent(float durInDays, String source, String desc, float value) {
78 getMod(source, durInDays);
79 modifyPercent(source, value, desc);
80 }
81 public void addTemporaryModPercent(float durInDays, String source, float value) {
82 getMod(source, durInDays);
83 modifyPercent(source, value);
84 }
85
86 public Map<String, TemporaryStatMod> getMods() {
87 if (tempMods == null) {
88 tempMods = new LinkedHashMap<String, TemporaryStatMod>();
89 }
90 return tempMods;
91 }
92 public boolean hasMod(String source) {
93 return getMods().containsKey(source);
94 }
95
96 public void advance(float days) {
97 if (tempMods == null || getMods().isEmpty()) return;
98
99 Iterator<TemporaryStatMod> iter = getMods().values().iterator();
100 while (iter.hasNext()) {
101 TemporaryStatMod mod = iter.next();
102 mod.timeRemaining -= days;
103 if (mod.timeRemaining <= 0) {
104 iter.remove();
105 unmodify(mod.source);
106 }
107 }
108 }
109}
110
111
112
113
114
115
116
117
118
119
120
void addTemporaryModPercent(float durInDays, String source, float value)
void addTemporaryModFlat(float durInDays, String source, String desc, float value)
void addTemporaryModMult(float durInDays, String source, String desc, float value)
void addTemporaryModFlat(float durInDays, String source, float value)
void addTemporaryModPercent(float durInDays, String source, String desc, float value)
void modifyFlat(String source, float value)
void modifyPercent(String source, float value)
void modifyMult(String source, float value)