// Spawn deals for all merchants for current year in turn 0 protected override void OnUpdate() { var calendarQuery = GetEntityQuery(ComponentType.ReadOnly <TimeTable>()); var calendar = GetComponent <TimeTable>(calendarQuery.GetSingletonEntity()); if (calendar.Turn != 0 || calendar.Pause) { return; } var dealManagerQuery = GetEntityQuery(ComponentType.ReadOnly <DealSpawner>()); var dealPrefab = GetComponent <DealSpawner>(dealManagerQuery.GetSingletonEntity()).DealPrefab; var merchantQuery = GetEntityQuery(ComponentType.ReadOnly <Merchant>()); var merchants = merchantQuery.ToComponentDataArray <Merchant>(Allocator.TempJob); Entities .WithStructuralChanges() .ForEach((in Merchant merchant) => { // ReSharper disable once AccessToDisposedClosure for (var i = 0; i < merchants.Length; i++) { // ReSharper disable once AccessToDisposedClosure if (merchants[i].Id <= merchant.Id) { continue; } var numDeals = _random.NextInt(5, 11); for (var j = 1; j <= numDeals; j++) { var newDeal = EntityManager.Instantiate(dealPrefab); EntityManager.SetComponentData(newDeal, new Deal { Turn = j, FirstMerchantId = merchant.Id, FirstMerchantRandom = new Random(_random.NextUInt()), FirstMerchantType = merchant.Type, // ReSharper disable once AccessToDisposedClosure SecondMerchantId = merchants[i].Id, SecondMerchantRandom = new Random(_random.NextUInt()), // ReSharper disable once AccessToDisposedClosure SecondMerchantType = merchants[i].Type }); Mappings[merchant.Type].Invoke(EntityManager, newDeal); // ReSharper disable once AccessToDisposedClosure Mappings[merchants[i].Type].Invoke(EntityManager, newDeal); } } }).Run();
public static void ULong() { Unity.Mathematics.Random rng = new Unity.Mathematics.Random(Helpers.GetRngSeed); Helpers.Test <ulong>( (array) => { array.SIMD_MinMax(out ulong min, out ulong max); Assert.AreEqual(min, array.SIMD_Minimum()); Assert.AreEqual(max, array.SIMD_Maximum()); }, () => ((ulong)rng.NextUInt() << 32) | rng.NextUInt()); }
public static Unity.Mathematics.Random Random(uint seed) { // Auto reseeding if (seed == 0) { seed = (uint)UnityEngine.Random.Range(0, 0x7fffffff); } var random = new Unity.Mathematics.Random(seed); // Abandon a few first numbers to warm up the PRNG. random.NextUInt(); random.NextUInt(); return(random); }
public static Unity.Mathematics.Random RandomFromIndexAndSeed(uint index, uint seed) { var r = new Unity.Mathematics.Random(index); r.NextUInt(); r.state = r.state ^ seed; return(r); }
protected override void OnCreate() { Random seedRandom = Random.CreateFromIndex((uint)DateTime.UtcNow.Ticks & int.MaxValue); Debug.LogAlways($"Random seed: {seedRandom.state}"); RandomArray = new NativeArray <Random>(JobsUtility.MaxJobThreadCount, Allocator.Persistent); for (int i = 0; i < RandomArray.Length; i++) { RandomArray[i] = Random.CreateFromIndex(seedRandom.NextUInt()); } }
public override void UpdateSettings() { var nodes = GetComponentsInChildren <PCGNodeBase>(); switch (randomType) { case UpdateRandomType.Parent: foreach (var i in nodes) { if (i == this) { continue; } if (i.enabled) { i.RandomSeed = RandomSeed; i.UpdateSettings(); } } break; case UpdateRandomType.Unchange: foreach (var i in nodes) { if (i == this) { continue; } if (i.enabled) { i.UpdateSettings(); } } break; default: Random rd = new Random(RandomSeed); foreach (var i in nodes) { if (i == this) { continue; } if (i.enabled) { i.RandomSeed = rd.NextUInt(); i.UpdateSettings(); } } break; } }
public static void Byte() { Unity.Mathematics.Random rng = new Unity.Mathematics.Random(Helpers.GetRngSeed); Helpers.Test <byte>( (array) => { array.SIMD_MinMax(out byte min, out byte max); Assert.AreEqual(min, array.SIMD_Minimum()); Assert.AreEqual(max, array.SIMD_Maximum()); }, () => (byte)rng.NextUInt(byte.MinValue, byte.MaxValue + 1)); }
public static void UShort() { Unity.Mathematics.Random rng = new Unity.Mathematics.Random(Helpers.GetRngSeed); Helpers.Test <ushort>( (array) => { array.SIMD_MinMax(out ushort min, out ushort max); Assert.AreEqual(min, array.SIMD_Minimum()); Assert.AreEqual(max, array.SIMD_Maximum()); }, () => (ushort)rng.NextUInt(ushort.MinValue, ushort.MaxValue + 1)); }
protected override void OnUpdate(EntityCommandBuffer.Concurrent ecb) { var random = new Random(mainRandom.NextUInt(uint.MinValue, uint.MaxValue)); var enemyTypeArchetype = enemyTypePrefab; var enemyEnumCount = EnemyEnumCount; Entities .WithAll <EnemyTag>() .WithNone <EnemyType>() .ForEach((Entity entity, int entityInQueryIndex) => { var enemyType = ecb.CreateEntity(entityInQueryIndex, enemyTypeArchetype); ecb.AddComponent(entityInQueryIndex, enemyType, new Parent { Value = entity }); ecb.AddComponent(entityInQueryIndex, entity, new EnemyType { Value = enemyType }); ecb.AddComponent <EntityAliveTag>(entityInQueryIndex, entity); var index = (EnemyEnum)random.NextInt(0, enemyEnumCount); switch (index) { case EnemyEnum.Snake: { ecb.AddComponent(entityInQueryIndex, enemyType, new Snake { }); } break; default: throw new NotImplementedException(); } }) .ScheduleParallel(); }
public MapResourceObject[] GenerateResources(Random rand, Terrain terrain, int size) { int featureSize = size / 16; int halfSize = size / 2 - 16 / 2; MapResourceObject[] mapResourceObjects = new MapResourceObject[featureSize * featureSize]; for (int z = 0; z < featureSize; z++) { for (int x = 0; x < featureSize; x++) { uint index = rand.NextUInt(0, 5); ItemObj[] resources; switch (index) { default: continue; case 0: resources = ironResources; break; case 1: resources = copperResources; break; case 2: resources = coalResources; break; } if (resources.Length == 0) { continue; } ItemObj obj = resources[rand.NextInt(0, resources.Length)]; switch (obj.Item) { case MapResource resourceObject: { MapResourceObject instance = mapResourceObjects[x + z * featureSize] = Instantiate(resourcePrefab, transform); Vector3 pos = new Vector3(x * 16 - halfSize, 0, z * 16 - halfSize); pos.y = terrain.SampleHeight(pos); Quaternion rotation = Quaternion.Euler(0, rand.NextFloat(-180, 180), 0); instance.Init(pos, rotation, obj.Item as MapResource, rand.NextUInt(3000, 6000)); break; } case Factory factory: break; } } } return(mapResourceObjects); }
/// <summary> /// /// </summary> /// <param name="lastSystemState"></param> /// <param name="lSystemNativeData"></param> /// <param name="globalParameters"></param> /// <param name="maxMemoryRequirementsPerSymbol"></param> /// <param name="branchOpenSymbol"></param> /// <param name="branchCloseSymbol"></param> /// <param name="includedCharactersByRuleIndex"></param> /// <param name="customSymbols"></param> /// <param name="parameterModificationJobDependency">A dependency on a job which only makes changes to the parameters of the source symbol string. /// the symbols themselves must be constant</param> public LSystemRuleMatchCompletable( NativeArray <LSystemSingleSymbolMatchData> matchSingletonData, int parameterTotalSum, SymbolStringBranchingCache branchingCache, LSystemState <float> lastSystemState, DependencyTracker <SystemLevelRuleNativeData> lSystemNativeData, float[] globalParameters, CustomRuleSymbols customSymbols, JobHandle parameterModificationJobDependency) { this.customSymbols = customSymbols; this.lastSystemState = lastSystemState; randResult = lastSystemState.randomProvider; nativeData = lSystemNativeData; this.matchSingletonData = matchSingletonData; this.branchingCache = branchingCache; // 1. UnityEngine.Profiling.Profiler.BeginSample("allocating"); tmpParameterMemory = new NativeArray <float>(parameterTotalSum, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); globalParamNative = new NativeArray <float>(globalParameters, Allocator.Persistent); UnityEngine.Profiling.Profiler.EndSample(); // 2. UnityEngine.Profiling.Profiler.BeginSample("matching"); var prematchJob = new RuleCompleteMatchJob { matchSingletonData = matchSingletonData, sourceData = lastSystemState.currentSymbols.Data, tmpParameterMemory = tmpParameterMemory, globalOperatorData = nativeData.Data.dynamicOperatorMemory, outcomes = nativeData.Data.ruleOutcomeMemorySpace, globalParams = globalParamNative, blittableRulesByTargetSymbol = nativeData.Data.blittableRulesByTargetSymbol, branchingCache = branchingCache, seed = randResult.NextUInt() }; var matchingJobHandle = prematchJob.ScheduleBatch( matchSingletonData.Length, 100, parameterModificationJobDependency); UnityEngine.Profiling.Profiler.EndSample(); // 4. UnityEngine.Profiling.Profiler.BeginSample("replacement counting"); UnityEngine.Profiling.Profiler.BeginSample("allocating"); totalSymbolCount = new NativeArray <int>(1, Allocator.Persistent); totalSymbolParameterCount = new NativeArray <int>(1, Allocator.Persistent); UnityEngine.Profiling.Profiler.EndSample(); var totalSymbolLengthJob = new RuleReplacementSizeJob { matchSingletonData = matchSingletonData, totalResultSymbolCount = totalSymbolCount, totalResultParameterCount = totalSymbolParameterCount, sourceData = lastSystemState.currentSymbols.Data, customSymbols = customSymbols }; currentJobHandle = totalSymbolLengthJob.Schedule(matchingJobHandle); lastSystemState.currentSymbols.RegisterDependencyOnData(currentJobHandle); nativeData.RegisterDependencyOnData(currentJobHandle); UnityEngine.Profiling.Profiler.EndSample(); }
public void Execute(int index) { var dh = SizeH * SizeH; var y = index / dh; var h = index % dh; var x = h % SizeH; var z = h / SizeH; var height = HeightMap[(x) * SizeH + z]; var stoneHeight = (int)(height * 0.8f); var rnd8pct = Random.NextUInt(0, 12) < 1 ? true : false; var rnd3pct = Random.NextUInt(0, 30) < 1 ? true : false; if (y == 0) { Blocks[y * dh + z * SizeH + x] = new BlockData(BlockType.Bedrock, 0); return; } if (y < stoneHeight) { Blocks[y * dh + z * SizeH + x] = new BlockData(BlockType.Stone, 0); } else if (y < height) { Blocks[y * dh + z * SizeH + x] = new BlockData(BlockType.Dirt, 0); return; } else if (y == height) { if (y > SeaLevel) { Blocks[y * dh + z * SizeH + x] = new BlockData(BlockType.Grass, 0); } else { Blocks[y * dh + z * SizeH + x] = new BlockData(BlockType.Sand, 0); } } else if (y > height && y < SeaLevel) { Blocks[y * dh + z * SizeH + x] = new BlockData() { Type = BlockType.WaterStill, SunLevel = (byte)(225 - (SeaLevel - y) * 10), AddColor = 65535, }; } else if (y == height + 1 && rnd8pct && y > SeaLevel) { Blocks[y * dh + z * SizeH + x] = new BlockData() { Type = BlockType.Weed, SunLevel = 255, AddColor = 65535, }; } else if (y == height + 1 && rnd3pct && y > SeaLevel) { Blocks[y * dh + z * SizeH + x] = new BlockData() { Type = BlockType.Shrub, Subtype = (byte)Random.NextInt(0, 3), SunLevel = 255, AddColor = 65535, }; } else if (y > height || (!rnd8pct && y > height)) { Blocks[y * dh + z * SizeH + x] = new BlockData() { SunLevel = 255 }; } }
// returns ushort in [1..ushort.MaxValue - 1] range public static ushort GetRandomUShort() { var rnd = new Unity.Mathematics.Random((uint)Stopwatch.GetTimestamp()); return((ushort)rnd.NextUInt(1, 0xffff)); }