You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

126 lines
4.1 KiB

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<Synthesis>(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<int> finishList = new List<int>();
List<int> sectionList = new List<int>();
List<int> stopList = new List<int>();
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<int> list = new List<int>();
var kChild = self.Children.Keys;
foreach (var v in kChild)
{
Synthesis synthesis = self.GetChild<Synthesis>(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
}
}