commit 37f3fa77658ba8dd608086bcf1ea889dda053aaa Author: codeboss <2422523675@qq.com> Date: Tue Jul 23 15:41:12 2024 +0800 基础实现初步成型 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84b77cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*/bin/* +*/obj/* + diff --git a/.vs/ProjectEvaluation/sharpworld.metadata.v8.bin b/.vs/ProjectEvaluation/sharpworld.metadata.v8.bin new file mode 100644 index 0000000..bb23da2 Binary files /dev/null and b/.vs/ProjectEvaluation/sharpworld.metadata.v8.bin differ diff --git a/.vs/ProjectEvaluation/sharpworld.projects.v8.bin b/.vs/ProjectEvaluation/sharpworld.projects.v8.bin new file mode 100644 index 0000000..4baf24a Binary files /dev/null and b/.vs/ProjectEvaluation/sharpworld.projects.v8.bin differ diff --git a/.vs/ProjectEvaluation/sharpworld.strings.v8.bin b/.vs/ProjectEvaluation/sharpworld.strings.v8.bin new file mode 100644 index 0000000..606cd68 Binary files /dev/null and b/.vs/ProjectEvaluation/sharpworld.strings.v8.bin differ diff --git a/.vs/SharpWorld/DesignTimeBuild/.dtbcache.v2 b/.vs/SharpWorld/DesignTimeBuild/.dtbcache.v2 new file mode 100644 index 0000000..ec0fb36 Binary files /dev/null and b/.vs/SharpWorld/DesignTimeBuild/.dtbcache.v2 differ diff --git a/.vs/SharpWorld/FileContentIndex/a6ef5db8-f1d3-464d-8e1a-7ef7d7e5e95e.vsidx b/.vs/SharpWorld/FileContentIndex/a6ef5db8-f1d3-464d-8e1a-7ef7d7e5e95e.vsidx new file mode 100644 index 0000000..e87daf0 Binary files /dev/null and b/.vs/SharpWorld/FileContentIndex/a6ef5db8-f1d3-464d-8e1a-7ef7d7e5e95e.vsidx differ diff --git a/.vs/SharpWorld/FileContentIndex/c7c53628-8347-4b57-86e0-bc7766e5842b.vsidx b/.vs/SharpWorld/FileContentIndex/c7c53628-8347-4b57-86e0-bc7766e5842b.vsidx new file mode 100644 index 0000000..778a5d5 Binary files /dev/null and b/.vs/SharpWorld/FileContentIndex/c7c53628-8347-4b57-86e0-bc7766e5842b.vsidx differ diff --git a/.vs/SharpWorld/FileContentIndex/ca68d0be-283a-4633-9151-42e030f8275b.vsidx b/.vs/SharpWorld/FileContentIndex/ca68d0be-283a-4633-9151-42e030f8275b.vsidx new file mode 100644 index 0000000..ccc40b5 Binary files /dev/null and b/.vs/SharpWorld/FileContentIndex/ca68d0be-283a-4633-9151-42e030f8275b.vsidx differ diff --git a/.vs/SharpWorld/FileContentIndex/e5fd841f-228c-4293-b42b-af1ec09c67e0.vsidx b/.vs/SharpWorld/FileContentIndex/e5fd841f-228c-4293-b42b-af1ec09c67e0.vsidx new file mode 100644 index 0000000..36bf86e Binary files /dev/null and b/.vs/SharpWorld/FileContentIndex/e5fd841f-228c-4293-b42b-af1ec09c67e0.vsidx differ diff --git a/.vs/SharpWorld/FileContentIndex/faa1d97a-3642-4d84-bf5f-489d4a20d18f.vsidx b/.vs/SharpWorld/FileContentIndex/faa1d97a-3642-4d84-bf5f-489d4a20d18f.vsidx new file mode 100644 index 0000000..9f3da43 Binary files /dev/null and b/.vs/SharpWorld/FileContentIndex/faa1d97a-3642-4d84-bf5f-489d4a20d18f.vsidx differ diff --git a/.vs/SharpWorld/v17/.futdcache.v2 b/.vs/SharpWorld/v17/.futdcache.v2 new file mode 100644 index 0000000..ae457f5 Binary files /dev/null and b/.vs/SharpWorld/v17/.futdcache.v2 differ diff --git a/.vs/SharpWorld/v17/.suo b/.vs/SharpWorld/v17/.suo new file mode 100644 index 0000000..197523b Binary files /dev/null and b/.vs/SharpWorld/v17/.suo differ diff --git a/.vs/SharpWorld/v17/DocumentLayout.json b/.vs/SharpWorld/v17/DocumentLayout.json new file mode 100644 index 0000000..244cab3 --- /dev/null +++ b/.vs/SharpWorld/v17/DocumentLayout.json @@ -0,0 +1,138 @@ +{ + "Version": 1, + "WorkspaceRootPath": "D:\\Projects\\CSharp\\SharpWorld\\", + "Documents": [ + { + "AbsoluteMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|d:\\projects\\csharp\\sharpworld\\sharpworld\\movement.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", + "RelativeMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|solutionrelative:sharpworld\\movement.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" + }, + { + "AbsoluteMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|d:\\projects\\csharp\\sharpworld\\sharpworld\\shapevolume.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", + "RelativeMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|solutionrelative:sharpworld\\shapevolume.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" + }, + { + "AbsoluteMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|d:\\projects\\csharp\\sharpworld\\sharpworld\\massfoster.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", + "RelativeMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|solutionrelative:sharpworld\\massfoster.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" + }, + { + "AbsoluteMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|d:\\projects\\csharp\\sharpworld\\sharpworld\\entityspawn.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", + "RelativeMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|solutionrelative:sharpworld\\entityspawn.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" + }, + { + "AbsoluteMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|d:\\projects\\csharp\\sharpworld\\sharpworld\\coreimplement.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", + "RelativeMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|solutionrelative:sharpworld\\coreimplement.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" + }, + { + "AbsoluteMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|d:\\projects\\csharp\\sharpworld\\sharpworld\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", + "RelativeMoniker": "D:0:0:{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}|SharpWorld\\SharpWorld.csproj|solutionrelative:sharpworld\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" + } + ], + "DocumentGroupContainers": [ + { + "Orientation": 0, + "VerticalTabListWidth": 256, + "DocumentGroups": [ + { + "DockedWidth": 187, + "SelectedChildIndex": 7, + "Children": [ + { + "$type": "Bookmark", + "Name": "ST:129:0:{1fc202d4-d401-403c-9834-5b218574bb67}" + }, + { + "$type": "Bookmark", + "Name": "ST:130:0:{1fc202d4-d401-403c-9834-5b218574bb67}" + }, + { + "$type": "Bookmark", + "Name": "ST:128:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}" + }, + { + "$type": "Bookmark", + "Name": "ST:0:0:{1c4feeaa-4718-4aa9-859d-94ce25d182ba}" + }, + { + "$type": "Document", + "DocumentIndex": 4, + "Title": "CoreImplement.cs", + "DocumentMoniker": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\CoreImplement.cs", + "RelativeDocumentMoniker": "SharpWorld\\CoreImplement.cs", + "ToolTip": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\CoreImplement.cs", + "RelativeToolTip": "SharpWorld\\CoreImplement.cs", + "ViewState": "AQIAAHcAAAAAAAAAAAAAAIIAAABVAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", + "WhenOpened": "2024-07-23T04:58:50.253Z", + "EditorCaption": "" + }, + { + "$type": "Document", + "DocumentIndex": 3, + "Title": "EntitySpawn.cs", + "DocumentMoniker": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\EntitySpawn.cs", + "RelativeDocumentMoniker": "SharpWorld\\EntitySpawn.cs", + "ToolTip": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\EntitySpawn.cs", + "RelativeToolTip": "SharpWorld\\EntitySpawn.cs", + "ViewState": "AQIAACQAAAAAAAAAAAAAAHUAAAA6AAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", + "WhenOpened": "2024-07-22T08:53:39.007Z", + "EditorCaption": "" + }, + { + "$type": "Document", + "DocumentIndex": 2, + "Title": "MassFoster.cs", + "DocumentMoniker": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\MassFoster.cs", + "RelativeDocumentMoniker": "SharpWorld\\MassFoster.cs", + "ToolTip": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\MassFoster.cs", + "RelativeToolTip": "SharpWorld\\MassFoster.cs", + "ViewState": "AQIAABUAAAAAAAAAAAAAAB0AAAAuAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", + "WhenOpened": "2024-07-23T03:03:10.984Z", + "EditorCaption": "" + }, + { + "$type": "Document", + "DocumentIndex": 0, + "Title": "Movement.cs", + "DocumentMoniker": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\Movement.cs", + "RelativeDocumentMoniker": "SharpWorld\\Movement.cs", + "ToolTip": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\Movement.cs", + "RelativeToolTip": "SharpWorld\\Movement.cs", + "ViewState": "AQIAAB8AAAAAAAAAAAAgwCYAAABRAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", + "WhenOpened": "2024-07-23T02:09:29.365Z", + "EditorCaption": "" + }, + { + "$type": "Document", + "DocumentIndex": 1, + "Title": "ShapeVolume.cs", + "DocumentMoniker": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\ShapeVolume.cs", + "RelativeDocumentMoniker": "SharpWorld\\ShapeVolume.cs", + "ToolTip": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\ShapeVolume.cs", + "RelativeToolTip": "SharpWorld\\ShapeVolume.cs", + "ViewState": "AQIAABgAAAAAAAAAAAAAADoAAAAjAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", + "WhenOpened": "2024-07-22T08:04:52.642Z", + "EditorCaption": "" + }, + { + "$type": "Document", + "DocumentIndex": 5, + "Title": "Program.cs", + "DocumentMoniker": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\Program.cs", + "RelativeDocumentMoniker": "SharpWorld\\Program.cs", + "ToolTip": "D:\\Projects\\CSharp\\SharpWorld\\SharpWorld\\Program.cs", + "RelativeToolTip": "SharpWorld\\Program.cs", + "ViewState": "AQIAAAAAAAAAAAAAAAAAAAsAAAAbAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", + "WhenOpened": "2024-07-22T08:33:04.362Z", + "EditorCaption": "" + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/SharpWorld.sln b/SharpWorld.sln new file mode 100644 index 0000000..6791de4 --- /dev/null +++ b/SharpWorld.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.34928.147 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpWorld", "SharpWorld\SharpWorld.csproj", "{63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {63F41712-9AFE-4DCB-8B35-6782AFAB7BCF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C89043E1-A722-436E-A5C3-23FD223C939D} + EndGlobalSection +EndGlobal diff --git a/SharpWorld/CoreImplement.cs b/SharpWorld/CoreImplement.cs new file mode 100644 index 0000000..1b5de87 --- /dev/null +++ b/SharpWorld/CoreImplement.cs @@ -0,0 +1,195 @@ +using MassFoster; +using ShapeVolume; +using Movement; +using SharpWorld; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; +using Variables; +using System.Data; +using EntitySpawn; + +namespace CoreImplement +{ + public class SystemHost : WsSystemHost + { + private UInt128 current_entity_id = 5; + public UInt128 AllocNewID() + { + lock (this) + { + return current_entity_id++; + } + } + + private Dictionary _system_store = new Dictionary(); + + public WsSystem GetSystem() where Datas : WsItemParams where Sys : WsSystem + { +#pragma warning disable CS8603 // 可能返回 null 引用。 + var type_name = typeof(WsSystem).FullName; + if (type_name == null) + throw new ArgumentException("类型名称为null"); + return _system_store[type_name] as WsSystem; +#pragma warning restore CS8603 // 可能返回 null 引用。 + } + + public void RegisterSystem(WsSystem inst) where Datas : WsItemParams + { +#pragma warning disable CS8604 // 引用类型参数可能为 null。 + _system_store[typeof(WsSystem).FullName] = inst; +#pragma warning restore CS8604 // 引用类型参数可能为 null。 + } + + private List ExecutableSystemList + { + get + { + var items = from n in _system_store.Values + orderby n.Name + where n is WsExecutableSystem + select n as WsExecutableSystem; + return items.ToList(); + } + } + + private List> execute_list = new List>(); + public void LoopRun() + { + var task_list = ExecutableSystemList; + foreach (var t in task_list) + { + execute_list.Add(new Tuple(t, 0)); + } + + for (; ; ) + { + var unit = execute_list.ElementAt(0); + unit.Item1.Run(this, unit.Item2); + execute_list.RemoveAt(0); + + bool find_flags = false; + var next_time = unit.Item2 + unit.Item1.ExecutePeriodMsec; + for (var tidx = 0; tidx < execute_list.Count; ++tidx) + { + var item = execute_list.ElementAt(tidx); + + if (next_time < item.Item2) + { + execute_list.Insert(tidx, new Tuple(unit.Item1, next_time)); + find_flags = true; + break; + } + } + + if (!find_flags) + { + execute_list.Add(new Tuple(unit.Item1, next_time)); + } + } + } + } + + public class ExampleEntity : WsEntity, ShapeVolumeParams, MovementParam, MassFosterParams, LifetimeParams + { + public string TypeName { get; set; } = "示例实体"; + public UInt128 RuntimeID { get; set; } = 1; + public string JsonContent { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + + public bool IsConsumed { get; set; } = true; + public double AbsorbAmount { get; set; } = 0; + public double MassInput { get; set; } = 0; + public VariableDouble MassConvRates { get; } = new VariableDouble("重量转换效率", 0.0001); + + + public List TotalVariables { get; } = new List(); + public WsBaseSystem? BindSystem { get; set; } = null; + public WsEntity BindEntity => this; + + public Vector3 PosXYZ { get; set; } = new Vector3(); + public Vector3 OritXYZ { get; set; } = new Vector3(1, 0, 0); + public float Speed { get; set; } = 0.01f; + public double Length { get; set; } + public double Width { get; set; } + public double Height { get; set; } + private VariableDouble volume_current = new VariableDouble("", 0, 0, double.MaxValue / 2); + public double Volume { get { return volume_current.Value; } set { volume_current.Value = value; } } + + public VariableDouble PrimitiveLength { get; } = new VariableDouble("初生体长", 1); + public VariableDouble PrimitiveWidth { get; } = new VariableDouble("初生体宽", 0.5); + public VariableDouble PrimitiveHeight { get; } = new VariableDouble("初生体高", 0.5); + + public VariableDouble Density { get; set; } = new VariableDouble("生体密度", 1); + public UInt128 SpawnStart { get; set; } + public UInt128 LifetimeAcc { get; set; } + public double MatureVolume { get; set; } = -1; + + public VariableULong GrowthTimeCount { get; } = new VariableULong("成长期长度", 1_000_000); + public VariableULong MatureTimeCount { get; } = new VariableULong("成熟期长度", 100_000_000_000); + public VariableULong DeathTimePoint { get; } = new VariableULong("死亡时间点", 120_000_000_000); + public VariableULong SpawnPeriods { get; } = new VariableULong("繁育周期", 100_000); + + public uint SpawnTimes { get; set; } = 0; + + public VariablePercent ImpressRates { get; } = new VariablePercent("能量吸收转换效率", 0.1); + + public void ActiveConfigBeforeExecute(WsSystemHost host) + { + var mass_sys = host.GetSystem(); + var vol_sys = host.GetSystem(); + var mov_sys = host.GetSystem(); + var spawn_sys = host.GetSystem(); + + mass_sys.RegisterItem(this); + vol_sys.RegisterItem(this); + mov_sys.RegisterItem(this); + spawn_sys.RegisterItem(this); + + Console.WriteLine($"实体诞生:ID={RuntimeID}"); + } + + public void DeathExecute(WsSystemHost host) + { + var mass_sys = host.GetSystem(); + var vol_sys = host.GetSystem(); + var mov_sys = host.GetSystem(); + var spawn_sys = host.GetSystem(); + + mass_sys.RemoveItem(this.RuntimeID); + vol_sys.RemoveItem(this.RuntimeID); + mov_sys.RemoveItem(this.RuntimeID); + spawn_sys.RemoveItem(this.RuntimeID); + + Console.WriteLine($"实体死亡:ID={RuntimeID}"); + } + + public ExampleEntity(UInt128 runtime_id) + { + this.RuntimeID = runtime_id; + TotalVariables.Add(MassConvRates); + TotalVariables.Add(PrimitiveLength); + TotalVariables.Add(PrimitiveWidth); + TotalVariables.Add(PrimitiveHeight); + TotalVariables.Add(Density); + TotalVariables.Add(GrowthTimeCount); + TotalVariables.Add(MatureTimeCount); + TotalVariables.Add(DeathTimePoint); + TotalVariables.Add(SpawnPeriods); + + Length = PrimitiveLength.Value; + Width = PrimitiveWidth.Value; + Height = PrimitiveHeight.Value; + Volume = Length * Width * Height; + } + + public WsEntity NewEmpty(UInt128 id) + { + return new ExampleEntity(id); + } + + } + +} diff --git a/SharpWorld/EntitySpawn.cs b/SharpWorld/EntitySpawn.cs new file mode 100644 index 0000000..690f1ca --- /dev/null +++ b/SharpWorld/EntitySpawn.cs @@ -0,0 +1,169 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShapeVolume; +using SharpWorld; +using Variables; + +namespace EntitySpawn +{ + public interface LifetimeParams : WsItemParams + { + /// + /// 诞生时间点 + /// + UInt128 SpawnStart { get; set; } + /// + /// 寿命长度累计 + /// + UInt128 LifetimeAcc { get; set; } + /// + /// 成熟体最小质量 + /// + double MatureVolume { get; set; } + + /// + /// 成长期长度 + /// + VariableULong GrowthTimeCount { get; } + /// + /// 壮年期长度 + /// + VariableULong MatureTimeCount { get; } + /// + /// 死亡时间点 + /// + VariableULong DeathTimePoint { get; } + + /// + /// 繁育周期 + /// + VariableULong SpawnPeriods { get; } + /// + /// 繁育次数 + /// + UInt32 SpawnTimes { get; set; } + } + + public class LifetimeSystem : WsSystem, WsExecutableSystem + { + private Dictionary _items_store = new Dictionary(); + + public uint ExecutePeriodMsec => 1000; + + public string Name => "生命周期控制系统"; + + public LifetimeParams GetItem(UInt128 RtID) + { + lock (this) + { + return _items_store[RtID]; + } + } + + public void RegisterItem(LifetimeParams entity_params) + { + lock (this) + { + _items_store[entity_params.BindEntity.RuntimeID] = entity_params; + } + } + + private void spawn_update(WsSystemHost host, LifetimeParams it, UInt128 current_timepoint) + { + var timepoint_birth = it.SpawnStart; + var growth_span = current_timepoint - timepoint_birth; + it.LifetimeAcc = growth_span; + + // 成长期不繁育 + if (it.LifetimeAcc <= it.GrowthTimeCount.Value) + return; + + // 死亡移除 + if (it.LifetimeAcc >= it.DeathTimePoint.Value) + { + it.BindEntity.DeathExecute(host); + return; + } + + // 记录成熟期初始体积 + if (it.MatureVolume <= 0) + { + var vol_sys = host.GetSystem(); + var vol_param = vol_sys.GetItem(it.BindEntity.RuntimeID); + var birth_volume = vol_param.PrimitiveHeight.Value * vol_param.PrimitiveWidth.Value * vol_param.PrimitiveLength.Value; + + if (vol_param.Volume > birth_volume * 20000) + { + it.MatureVolume = birth_volume * 20000; + } + else + { + it.MatureVolume = vol_param.Volume; + } + } + + // 成熟期开始生育 + if (it.LifetimeAcc <= it.GrowthTimeCount.Value + it.MatureTimeCount.Value) + { + // 执行生育计算 + var span = it.LifetimeAcc - it.GrowthTimeCount.Value; + if (it.SpawnTimes < (span / it.SpawnPeriods.Value)) + { + var vol_sys = host.GetSystem(); + var vol_param = vol_sys.GetItem(it.BindEntity.RuntimeID); + + var current_volume = vol_param.Volume; + var volume_redundance = current_volume - it.MatureVolume; + volume_redundance -= Math.Pow(volume_redundance, 0.75); + var birth_volume = vol_param.PrimitiveHeight.Value * vol_param.PrimitiveWidth.Value * vol_param.PrimitiveLength.Value; + + // 纠正繁育周期错误 + if (volume_redundance < birth_volume) + { + var xval = it.SpawnPeriods.Value * 2; + it.SpawnPeriods.Value = xval; + return; + } + + // 执行繁育 + it.SpawnTimes++; + var spawn_count = (UInt128)(volume_redundance / birth_volume); + Console.WriteLine($"繁育时间:time={current_timepoint}"); + for (UInt128 idx = 0; idx < spawn_count; ++idx) + { + var new_entity = it.BindEntity.NewEmpty(host.AllocNewID()); + new_entity.ActiveConfigBeforeExecute(host); + current_volume -= birth_volume; + } + + // 校正体积 + var times = Math.Pow(current_volume / birth_volume, 1 / 3.0); + vol_param.Length = times * vol_param.PrimitiveLength.Value; + vol_param.Width = times * vol_param.PrimitiveWidth.Value; + vol_param.Height = times * vol_param.PrimitiveHeight.Value; + } + } + } + public void Run(WsSystemHost host, UInt128 timepoint_msec) + { + var srcl = _items_store.Values.ToList(); + var opts = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; + Parallel.For(0, srcl.Count, opts, index => + { + var it = srcl[index]; + spawn_update(host, it, timepoint_msec); + }); + } + + public void RemoveItem(UInt128 RtID) + { + lock (this) + { + _items_store.Remove(RtID); + } + } + } +} diff --git a/SharpWorld/MassFoster.cs b/SharpWorld/MassFoster.cs new file mode 100644 index 0000000..25966bb --- /dev/null +++ b/SharpWorld/MassFoster.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShapeVolume; +using SharpWorld; +using Variables; + +namespace MassFoster +{ + public interface MassFosterParams : WsItemParams + { + bool IsConsumed { get; set; } + + double AbsorbAmount { get; set; } + double MassInput { get; set; } + /// + /// 光合作用转换效率 + /// + VariableDouble MassConvRates { get; } + } + + public class EnvFosterSystem : WsSystem, WsExecutableSystem + { + private Dictionary _items_store = new Dictionary(); + + public string Name => "环境能量养护系统"; + + public uint ExecutePeriodMsec => 1000; + + public MassFosterParams GetItem(UInt128 RtID) + { + lock (this) + { + return _items_store[RtID]; + } + } + + public void RegisterItem(MassFosterParams entity_params) + { + lock (this) + { + _items_store[entity_params.BindEntity.RuntimeID] = entity_params; + } + } + + private void update_batch(WsSystemHost host, MassFosterParams item) + { + var volsys = host.GetSystem(); + var volparam = volsys.GetItem(item.BindEntity.RuntimeID); + var square = volparam.Length * volparam.Width; + + if (item.IsConsumed) + { + item.IsConsumed = false; + item.AbsorbAmount = 0; + item.MassInput = 0; + } + + item.AbsorbAmount += _energy_density * square; + var new_mass = item.AbsorbAmount * item.MassConvRates.Value; + item.MassInput = new_mass; + } + + public void Run(WsSystemHost host, UInt128 timepoint) + { + var srcl = _items_store.Values.ToList(); + var opts = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; + Parallel.For(0, srcl.Count, opts, index => { + var param = srcl[index]; + update_batch(host, param); + }); + } + + public void RemoveItem(UInt128 RtID) + { + lock (this) + { + _items_store.Remove(RtID); + } + } + + //========================= + /// + /// 表面能量密度 + /// + private double _energy_density = 0; + /// + /// 构建能量消耗计算系统 + /// + /// + public EnvFosterSystem(double plain_energy) + { + _energy_density = plain_energy; + } + + } +} diff --git a/SharpWorld/Movement.cs b/SharpWorld/Movement.cs new file mode 100644 index 0000000..82ba87d --- /dev/null +++ b/SharpWorld/Movement.cs @@ -0,0 +1,68 @@ +using SharpWorld; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace Movement +{ + public interface MovementParam : WsItemParams + { + public Vector3 PosXYZ { get; set; } + public Vector3 OritXYZ { get; set; } + + public float Speed { get; set; } + } + + public class MovementSystem : WsSystem, SharpWorld.WsExecutableSystem + { + private Dictionary _items_store = new Dictionary(); + + public string Name => "机动演算系统"; + + public uint ExecutePeriodMsec => 100; + + public MovementParam GetItem(UInt128 RtID) + { + lock (this) + { + return _items_store[RtID]; + } + } + + public void RegisterItem(MovementParam entity_params) + { + lock (this) + { + _items_store[entity_params.BindEntity.RuntimeID] = entity_params; + } + } + + public void movement_update(WsSystemHost host, MovementParam item_ps) + { + var orit_vec = Vector3.Normalize(item_ps.OritXYZ); + var move_span = Vector3.Multiply(orit_vec, item_ps.Speed); + item_ps.PosXYZ = Vector3.Add(item_ps.PosXYZ, move_span); + } + + public void Run(WsSystemHost host, UInt128 timepoint) + { + var srcl = _items_store.Values.ToList(); + var opts = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; + Parallel.For(0, srcl.Count, opts, index => { + var it = srcl[index]; + movement_update(host, it); + }); + } + + public void RemoveItem(UInt128 RtID) + { + lock (this) + { + _items_store.Remove(RtID); + } + } + } +} diff --git a/SharpWorld/Program.cs b/SharpWorld/Program.cs new file mode 100644 index 0000000..ea50c28 --- /dev/null +++ b/SharpWorld/Program.cs @@ -0,0 +1,24 @@ +using MassFoster; +using ShapeVolume; +using Movement; +using SharpWorld; +using CoreImplement; +using EntitySpawn; + +class Program +{ + public static void Main(string[] args) + { + var host = new SystemHost(); + + host.RegisterSystem(new EnvFosterSystem(2)); + host.RegisterSystem(new ShapeVolumeSystem()); + host.RegisterSystem(new MovementSystem()); + host.RegisterSystem(new LifetimeSystem()); + + var entity_inst = new ExampleEntity(1); + entity_inst.ActiveConfigBeforeExecute(host); + + host.LoopRun(); + } +} \ No newline at end of file diff --git a/SharpWorld/ShapeVolume.cs b/SharpWorld/ShapeVolume.cs new file mode 100644 index 0000000..1b78a43 --- /dev/null +++ b/SharpWorld/ShapeVolume.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MassFoster; +using SharpWorld; +using Variables; + +namespace ShapeVolume +{ + public interface ShapeVolumeParams : WsItemParams + { + public double Length { get; set; } + public double Width { get; set; } + public double Height { get; set; } + public double Volume { get; set; } + + public VariableDouble PrimitiveLength { get; } + public VariableDouble PrimitiveWidth { get; } + public VariableDouble PrimitiveHeight { get; } + public VariableDouble Density { get; } + + public VariablePercent ImpressRates { get; } + } + + public class ShapeVolumeSystem : WsExecutableSystem, WsSystem + { + private Dictionary _items_store = new Dictionary(); + + public uint ExecutePeriodMsec => 10_000; + + public string Name => "体重演算系统"; + + public ShapeVolumeParams GetItem(UInt128 RtID) + { + lock (this) + { + return _items_store[RtID]; + } + } + + public void RegisterItem(ShapeVolumeParams entity_params) + { + lock (this) + { + _items_store[entity_params.BindEntity.RuntimeID] = entity_params; + } + } + + private void update_volumes(WsSystemHost host, ShapeVolumeParams vmit) + { + var mass_sys = host.GetSystem(); + + var mass_param = mass_sys.GetItem(vmit.BindEntity.RuntimeID); + mass_param.IsConsumed = true; + var m_new = mass_param.MassInput; + var vol_new = m_new / vmit.Density.Value * vmit.ImpressRates.Value; + vmit.Volume += vol_new; + + var primitive_vol = vmit.PrimitiveHeight.Value * vmit.PrimitiveLength.Value * vmit.PrimitiveWidth.Value; + var times = Math.Pow(vmit.Volume / primitive_vol, 1 / 3.0); + vmit.Length = times * vmit.PrimitiveLength.Value; + vmit.Width = times * vmit.PrimitiveWidth.Value; + vmit.Height = times * vmit.PrimitiveHeight.Value; + } + + public void Run(WsSystemHost host, UInt128 timepoint_msec) + { + var srcl = _items_store.Values.ToList(); + var opts = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; + Parallel.For(0, srcl.Count, opts, index => { + var it = srcl[index]; + update_volumes(host, it); + }); + } + + public void RemoveItem(UInt128 RtID) + { + lock (this) + { + _items_store.Remove(RtID); + } + } + } +} diff --git a/SharpWorld/SharpWorld.cs b/SharpWorld/SharpWorld.cs new file mode 100644 index 0000000..15200c3 --- /dev/null +++ b/SharpWorld/SharpWorld.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SharpWorld +{ + /// + /// 可变参数类型共同接口 + /// + public interface VariableVal { + /// + /// 参数名称 + /// + string Name { get; } + /// + /// 以指定比率执行变异 + /// + /// 变异比率 + void Variation(double rates); + } + + /// + /// 参与系统运算的个体数据 + /// + public interface WsItemParams { + /// + /// 获取所有可变异参数 + /// + List TotalVariables { get; } + + /// + /// 运行时绑定特性演算系统 + /// + WsBaseSystem? BindSystem { get; set; } + /// + /// 绑定实体 + /// + WsEntity BindEntity { get; } + } + + + /// + /// 增强特性运行系统 + /// + public interface WsBaseSystem { + /// + /// 系统组件名称 + /// + string Name { get; } + } + + /// + /// 可运行系统 + /// + public interface WsExecutableSystem : WsBaseSystem + { + /// + /// 执行周期(以毫秒计) + /// + UInt32 ExecutePeriodMsec { get; } + + /// + /// 执行方法 + /// host系统实例 + /// 当前仿真时间点,毫秒计 + /// + void Run(WsSystemHost host, UInt128 timepoint_msec); + } + + public interface WsSystem : WsBaseSystem + where Datas : WsItemParams + { + /// + /// 在系统内注册实体参数 + /// + /// 参数类型 + /// 参数实例 + void RegisterItem(Datas entity_params); + + /// + /// 移除实体 + /// + /// + void RemoveItem(UInt128 RtID); + + /// + /// 通过实体id获取在此系统中绑定的参数包 + /// + /// + /// + Datas GetItem(UInt128 RtID); + } + + /// + /// 系统访问管理接口 + /// + public interface WsSystemHost + { + /// + /// 下一个实体的id + /// + public UInt128 AllocNewID(); + + /// + /// 注册可以执行的系统 + /// + /// + void RegisterSystem(WsSystem inst) where Datas : WsItemParams; + + /// + /// 通过类型获取系统实例 + /// + /// 系统类型 + /// + WsSystem GetSystem() where Datas : WsItemParams where Sys: WsSystem; + } + + /// + /// 实体 + /// + public interface WsEntity + { + /// + /// 创建一个空实例 + /// + /// 实体id + /// 空实例 + WsEntity NewEmpty(UInt128 new_id); + + /// + /// 实体类型 + /// + string TypeName { get; set; } + /// + /// 运行时id + /// + UInt128 RuntimeID { get; set; } + + /// + /// 在系统运行前执行动态配置 + /// + /// + void ActiveConfigBeforeExecute(WsSystemHost host); + void DeathExecute(WsSystemHost host); + + /// + /// 存取序列化Json内容 + /// + string JsonContent { get; set; } + } +} diff --git a/SharpWorld/SharpWorld.csproj b/SharpWorld/SharpWorld.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/SharpWorld/SharpWorld.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/SharpWorld/VariableType.cs b/SharpWorld/VariableType.cs new file mode 100644 index 0000000..3abaad7 --- /dev/null +++ b/SharpWorld/VariableType.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading.Tasks; + +namespace Variables +{ + public abstract class VariableData : SharpWorld.VariableVal + where D : struct + { + public string Name { get; private set; } + private D value_store; + public virtual D Value + { + get + { + return value_store; + } + set + { + value_store = value; + } + } + + public VariableData(string name) + { + this.Name = name; + } + + public abstract void Variation(double rates); + } + + public class VariableDouble : VariableData + { + public double Min { get; set; } = -Double.MaxValue; + public double Max { get; set; } = Double.MaxValue; + + public VariableDouble(string name) : base(name) { } + public VariableDouble(string name, double val) : base(name) { Value = val; } + public VariableDouble(string name, double val, double min, double max) + : base(name) + { + Value = val; + Min = min; + Max = max; + } + + public override double Value + { + get { return base.Value; } + set + { + base.Value = Math.Max(Min, Math.Min(Max, value)); + } + } + + public override void Variation(double rates) + { + var dtinst = DateTime.Now.Millisecond; + var flag = dtinst % 2; + Value *= (1 + Math.Pow(-1, flag) * rates); + } + } + + public class VariablePercent : VariableDouble + { + public VariablePercent(string name) : base(name) { } + public VariablePercent(string name, double val) : base(name, val) + { + Value = val; + } + + public override double Value + { + get + { + return base.Value; + } + set + { + base.Value = Math.Min(1, Math.Max(0, value)); + } + } + } + + public class VariableInt : VariableData + { + public VariableInt(string name) : base(name) { } + + public override void Variation(double rates) + { + var dtinst = DateTime.Now.Millisecond; + var flag = dtinst % 2; + var temp_val = Value * (1 + Math.Pow(-1, flag) * rates); + Value = (int)temp_val; + } + } + + + public class VariableULong : VariableData + { + public VariableULong(string name, ulong val) : base(name) { Value = val; } + + public override void Variation(double rates) + { + var dtinst = DateTime.Now.Millisecond; + var flag = dtinst % 2; + var temp_val = Value * (1 + Math.Pow(-1, flag) * rates); + Value = (UInt64)temp_val; + } + } +}