001package com.fs.starfarer.api.combat;
002
003import java.util.Iterator;
004import java.util.LinkedHashMap;
005import java.util.Map;
006
007/**
008 * @author Alex Mosolov
009 *
010 * Copyright 2012 Fractal Softworks, LLC
011 */
012public class MutableStatWithTempMods extends MutableStat {
013
014        public static class TemporaryStatMod {
015                float timeRemaining; // days or seconds, whatever
016                String source;
017                public TemporaryStatMod(float timeRemaining, String source) {
018                        this.timeRemaining = timeRemaining;
019                        this.source = source;
020                }
021        }
022        
023        private LinkedHashMap<String, TemporaryStatMod> tempMods = new LinkedHashMap<String, TemporaryStatMod>();
024        
025        public MutableStatWithTempMods(float base) {
026                super(base);
027        }
028        
029        protected Object readResolve() {
030                super.readResolve();
031//              if (tempMods == null) {
032//                      tempMods = new LinkedHashMap<String, TemporaryStatMod>();
033//              }
034//              if (flatAfterMult == null) {
035//                      flatAfterMult = new HashMap<String, StatMod>();
036//              }
037                return this;
038        }
039        
040        protected Object writeReplace() {
041                if (tempMods != null && getMods().isEmpty()) {
042                        tempMods = null;
043                }
044                return this;
045        }
046        
047        
048        public void removeTemporaryMod(String source) {
049                TemporaryStatMod mod = getMods().remove(source);
050                if (mod == null) return;
051                
052                unmodify(mod.source);
053        }
054        
055        private TemporaryStatMod getMod(String source, float durInDays) {
056                TemporaryStatMod mod = getMods().get(source);
057                if (mod == null) {
058                        mod = new TemporaryStatMod(durInDays, source);
059                        getMods().put(source, mod);
060                }
061                mod.timeRemaining = durInDays;
062                return mod;
063        }
064        
065        public void addTemporaryModFlat(float durInDays, String source, String desc, float value) {
066                getMod(source, durInDays);
067                modifyFlat(source, value, desc);
068        }
069        public void addTemporaryModMult(float durInDays, String source, String desc, float value) {
070                getMod(source, durInDays);
071                modifyMult(source, value, desc);
072        }
073        public void addTemporaryModFlat(float durInDays, String source, float value) {
074                getMod(source, durInDays);
075                modifyFlat(source, value);
076        }
077        public void addTemporaryModPercent(float durInDays, String source, String desc, float value) {
078                getMod(source, durInDays);
079                modifyPercent(source, value, desc);
080        }
081        public void addTemporaryModPercent(float durInDays, String source, float value) {
082                getMod(source, durInDays);
083                modifyPercent(source, value);
084        }
085        
086        public Map<String, TemporaryStatMod> getMods() {
087                if (tempMods == null) {
088                        tempMods = new LinkedHashMap<String, TemporaryStatMod>();
089                }
090                return tempMods;
091        }
092        public boolean hasMod(String source) {
093                return getMods().containsKey(source);
094        }
095        
096        public void advance(float days) {
097                if (tempMods == null || getMods().isEmpty()) return;
098                
099                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