/// <summary> /// Initializes a new instance of the <see cref="BuildingProblem" /> struct. /// </summary> /// <param name="problem">The problem.</param> /// <param name="weight">The weight.</param> /// <param name="incrementCount">if set to <c>true</c> increment count.</param> public BuildingProblem(BuildingProblem problem, byte weight = 0, bool incrementCount = false) { this.Count = problem.Count; this.Size = problem.Size + weight; if (incrementCount) { this.Count++; } }
/// <summary> /// Updates this instance. /// </summary> public void Update() { try { uint decrement = (Global.CurrentFrame - this.LastUpdate) / Global.ProblemLingerDelay; if (decrement == 0) { return; } //if (Log.LogALot) //{ // Log.DevDebug(this, "Update", decrement); //} foreach (KeyValuePair <uint, uint> size in this.ProblemSizes.ToArray()) { ushort serviceBuildingId = GetServiceBuildingFromBuildingKey(size.Key); ushort targetBuildingId = GetTargetBuildingFromBuildingKey(size.Key); BuildingProblem oldProblem = this.TargetBuildingProblems[targetBuildingId]; BuildingProblem newProblem; uint newValue; //bool sizeDec; //bool problemDec; if (size.Value <= decrement) { newValue = 0; //sizeDec = false; this.ProblemSizes.Remove(size.Key); if (oldProblem.Count <= 1) { //problemDec = false; newProblem = new BuildingProblem(0, 0); this.TargetBuildingProblems.Remove(targetBuildingId); } else { //problemDec = true; newProblem = new BuildingProblem((ushort)(oldProblem.Count - 1), (oldProblem.Size <= size.Value) ? 1 : oldProblem.Size - size.Value); this.TargetBuildingProblems[targetBuildingId] = newProblem; } } else { //sizeDec = true; newValue = size.Value - decrement; this.ProblemSizes[size.Key] = newValue; //problemDec = true; newProblem = new BuildingProblem(oldProblem.Count, (oldProblem.Size <= decrement) ? size.Value - decrement : oldProblem.Size - decrement); this.TargetBuildingProblems[targetBuildingId] = newProblem; } //if (Log.LogALot) //{ // DevLog("Update", // Log.Data("ServiceBuilding", serviceBuildingId, BuildingHelper.GetBuildingName(serviceBuildingId)), // Log.Data("TargetBuilding", targetBuildingId, BuildingHelper.GetBuildingName(targetBuildingId)), // Log.Data("Actions", (sizeDec ? "Decrement" : "Remove"), (problemDec ? "Decrement" : "Remove"), decrement), // "Key", size.Key, // "OldValue", size.Value, // "OldCount", oldProblem.Count, // "OldSize", oldProblem.Size, // "NewValue", newValue, // "NewCount", newProblem.Count, // "NewSize", newProblem.Size); //} } } catch (Exception ex) { Log.Error(this, "Update", ex); } finally { this.LastUpdate = Global.CurrentFrame; } }
/// <summary> /// Notes a problem. /// </summary> /// <param name="problem">The problem.</param> /// <param name="serviceBuildingId">The service building identifier.</param> /// <param name="targetBuildingId">The target building identifier.</param> public void Add(ServiceProblem problem, ushort serviceBuildingId, ushort targetBuildingId) { if (this.broken) { return; } try { if (Log.LogALot) { Log.Debug( this, "Add", problem, serviceBuildingId, targetBuildingId, BuildingHelper.GetBuildingName(serviceBuildingId), BuildingHelper.GetDistrictName(serviceBuildingId), BuildingHelper.GetBuildingName(targetBuildingId), BuildingHelper.GetDistrictName(targetBuildingId)); } uint key = MakeBuildingKey(serviceBuildingId, targetBuildingId); byte weight = this.GetServiceProblemWeight(problem); uint newValue; uint oldValue; BuildingProblem newProblem; BuildingProblem oldProblem; bool gotSize = this.ProblemSizes.TryGetValue(key, out oldValue); bool gotProblem = this.TargetBuildingProblems.TryGetValue(targetBuildingId, out oldProblem); if (gotSize) { newValue = oldValue + weight; } else { oldValue = 0; newValue = weight; } if (gotProblem) { newProblem = new BuildingProblem(oldProblem, weight, !gotSize); } else { newProblem = new BuildingProblem(weight); } this.ProblemSizes[key] = newValue; this.TargetBuildingProblems[targetBuildingId] = newProblem; //if (Log.LogALot) //{ // DevLog("Add", // Log.Data("ServiceBuilding", serviceBuildingId, BuildingHelper.GetBuildingName(serviceBuildingId)), // Log.Data("TargetBuilding", targetBuildingId, BuildingHelper.GetBuildingName(targetBuildingId)), // Log.Data("Actions", (gotSize ? "Set" : "Add"), (gotProblem ? "Set" : "Add"), weight), // "Problem", problem, // "Key", key, // "OldValue", oldValue, // "OldCount", oldProblem.Count, // "OldSize", oldProblem.Size, // "NewValue", newValue, // "NewCount", newProblem.Count, // "NewSize", newProblem.Size); //} } catch (Exception ex) { this.broken = true; Log.Error(this, "Add", ex); } }