// counts/set array can be of length 1 to maximum number of counts // to set a value, set count/set=1 for that entry // to change a value, set count/set = 0 for that entry // to leave a value, set count=0,set=0 for that entry // set means set to value, else add to value public void Change(DateTime utc, MaterialCommodityMicroResourceType.CatType cat, string fdname, int[] counts, bool[] set, long price) { fdname = fdname.ToLowerInvariant(); MaterialCommodityMicroResource mc = items.GetLast(fdname); // find last entry, may return null if none stored if (mc == null) // not stored, make new { MaterialCommodityMicroResourceType mcdb = MaterialCommodityMicroResourceType.EnsurePresent(cat, fdname); // get a MCDB of this mc = new MaterialCommodityMicroResource(mcdb); } else { mc = new MaterialCommodityMicroResource(mc); // copy constructor, new copy of it } double costprev = mc.Counts[0] * mc.Price; double costofnew = counts[0] * price; bool changed = false; for (int i = 0; i < counts.Length; i++) { int newcount = set[i] ? counts[i] : Math.Max(mc.Counts[i] + counts[i], 0); // don't let it go below zero if changing changed |= newcount != mc.Counts[i]; // have we changed? we are trying to minimise deltas to the gen dictionary, so don't add if nothing changed mc.Counts[i] = newcount; } if (changed) // only store back a new entry if material change to counts { if (mc.Counts[0] > 0 && counts[0] > 0) // if bought (defensive with mc.counts) { mc.Price = (costprev + costofnew) / mc.Counts[0]; // price is now a combination of the current cost and the new cost. in case we buy in tranches } items.Add(fdname, mc); // and add a new fresh mc to the dictionary // System.Diagnostics.Debug.WriteLine("{0} Changed {1} {2} {3}", utc, items.Generation, mc.Details.FDName, mc.Count); } else { // System.Diagnostics.Debug.WriteLine("{0} Not changed {1} {2}", utc, mc.Details.FDName, mc.Count); } }
private FactionInfo Clone(string faction, bool incrgen = true) // clone both FactionInformation structure and a faction { if (faction.HasChars() && faction != "$faction_none;") { FactionInfo newfi = history.GetLast(faction); // get the last one, or null newfi = newfi != null ? new FactionInfo(newfi) : new FactionInfo(faction); // make a new copy, or an empty copy if (incrgen) { history.NextGeneration(); } history.Add(faction, newfi); // add this new one to the history list return(newfi); } else { return(null); } }