using System; using System.Collections.Generic; namespace ET { [FriendClass(typeof(Synthesis))] public static class SynthesisComponentSystem { public static Synthesis StartSynthesis(this SynthesisComponent self, int configId, long maxAmount) { Synthesis synthesis = self.AddChildWithId(configId); synthesis.ConfigId = configId; synthesis.CurrAmount = 0; synthesis.UpdateTime = TimeHelper.ServerNow(); synthesis.Progress = 0; synthesis.State = (int) SynthesisState.Working; synthesis.MaxAmount = maxAmount; return synthesis; } #if SERVER public static void Update(this SynthesisComponent self, Unit unit, long now) { List finishList = new List(); List sectionList = new List(); List stopList = new List(); foreach (var v in self.Children) { Synthesis synthesis = (Synthesis) v.Value; if (synthesis.State == (int) SynthesisState.Stop) { continue; } if (!ConstructOperate.CheckOutBuildingByIds(unit, synthesis.Config.StructureID)) { stopList.Add(synthesis.ConfigId); synthesis.State = (int) SynthesisState.Stop; continue; } var tick = now - synthesis.UpdateTime; synthesis.UpdateTime = now; synthesis.Progress += (int) tick; int timeConsume = synthesis.Config.TimeConsume * 1000; int leftProgress = synthesis.Progress % timeConsume; int count = (int) synthesis.Progress / timeConsume; if (synthesis.Progress < timeConsume) { continue; } if (synthesis.CurrAmount + count > synthesis.MaxAmount) { count = (int)(synthesis.MaxAmount - synthesis.CurrAmount); } synthesis.Progress = leftProgress; synthesis.CurrAmount += count; StoreOperate.AddItem(unit, synthesis.Config.MixtureID, synthesis.Config.Copies * count); if (synthesis.CurrAmount < synthesis.MaxAmount) { sectionList.Add(synthesis.ConfigId); continue; } finishList.Add(synthesis.ConfigId); synthesis.Dispose(); } if (stopList.Count > 0) { SynthesisHelper.NotifySynthesisStop(unit, stopList); } if (sectionList.Count > 0) { SynthesisHelper.NotifySynthesisSection(unit, sectionList); } foreach (var v in finishList) { SynthesisOperate.EndSynthesis(unit, v); } if (finishList.Count > 0) { SynthesisHelper.NotifySynthesisFinish(unit, finishList); } } public static void StopSynthesisForBuildingId(this SynthesisComponent self, Unit unit, int buildingId) { List list = new List(); var kChild = self.Children.Keys; foreach (var v in kChild) { Synthesis synthesis = self.GetChild(v); if (synthesis.State != (int) SynthesisState.Working) { continue; } if (Array.IndexOf(synthesis.Config.StructureID, buildingId) == -1) { continue; } if (ConstructOperate.CheckOutBuildingByIds(unit, synthesis.Config.StructureID)) { continue; } synthesis.State = (int) SynthesisState.Stop; list.Add(synthesis.ConfigId); } if (list.Count > 0) { SynthesisHelper.NotifySynthesisStop(unit, list); } } #endif } }