public void CreateCells(float x, float xf, float y, float yf) { List <float3> cells = new List <float3>(); for (float i = x; i < xf; i += city.BioParameters.CellWidth) { for (float j = y; j < yf; j += city.BioParameters.CellWidth) { int ID = GridConverter.Position2CellID(new float3(i, j, 0f)); //Debug.Log(ID); if (created_cell_ids.Contains(ID)) { continue; } //Debug.Log(ID + " " + i + " " + j + GridConverter.PositionToGridCell(new float3(i, j, 0f))); Entity newCell = entityManager.CreateEntity(city.CellArchetype); entityManager.SetComponentData <Position>(newCell, new Position { Value = new float3(i, j, 0f) }); entityManager.SetComponentData <Rotation>(newCell, new Rotation { Value = quaternion.identity }); entityManager.SetComponentData <CellData>(newCell, new CellData { ID = ID, Area = city.BioParameters.CellWidth * city.BioParameters.CellWidth }); created_cell_ids.Add(ID); cells.Add(new float3(i, j, 0f)); } } using (System.IO.StreamWriter file = new System.IO.StreamWriter(Parameters.Instance.LogFilePath + "Cells.txt")) { foreach (float3 cellPos in cells) { string line = string.Format("{0:D0};{1:F3};{2:F3};\n", GridConverter.Position2CellID(new float3(cellPos.x, cellPos.y, 0f)), cellPos.x, cellPos.y ); file.Write(line); } } }
public void Execute(int index) { CloudData currentCloudData = CloudData[index]; CloudGoal currentCloudGoal = CloudGoal[index]; Position currentCloudPosition = Position[index]; int[] desiredCells = GridConverter.RadiusInGrid(currentCloudPosition.Value, currentCloudData.Radius); float3 desiredSum = float3.zero; for (int i = 0; i < desiredCells.Length; i++) { float3 partial; if (cellid2pos.TryGetValue(desiredCells[i], out partial)) { var s = math.length(partial - currentCloudPosition.Value); if (s <= currentCloudData.Radius) { desiredSum += (math.normalize(partial - currentCloudPosition.Value)) * (currentCloudData.Radius - s); } } } float3 currentCellPosition; int cellCount = 0; NativeMultiHashMapIterator <int> it; float3 posSum = float3.zero; bool keepgoing = CloudMarkersMap.TryGetFirstValue(currentCloudData.ID, out currentCellPosition, out it); if (!keepgoing) { return; } cellCount++; var t = math.length(currentCellPosition - currentCloudPosition.Value); posSum += (math.normalize(currentCellPosition - currentCloudPosition.Value)) * (currentCloudData.Radius - t); bool right_desempate = false; if (!right_desempate && math.cross(currentCloudPosition.Value, currentCellPosition - currentCloudPosition.Value).z < 0) { extraWeightCellId.TryAdd(currentCloudData.ID, GridConverter.Position2CellID(currentCellPosition)); right_desempate = true; } while (CloudMarkersMap.TryGetNextValue(out currentCellPosition, ref it)) { cellCount++; t = math.length(currentCellPosition - currentCloudPosition.Value); if (t <= currentCloudData.Radius) { posSum += (math.normalize(currentCellPosition - currentCloudPosition.Value)) * (currentCloudData.Radius - t); } if (!right_desempate && math.cross(currentCloudPosition.Value, currentCellPosition - currentCloudPosition.Value).z < 0) { extraWeightCellId.TryAdd(currentCloudData.ID, GridConverter.Position2CellID(currentCellPosition)); right_desempate = true; } } sumVec[index] = posSum; dessumVec[index] = desiredSum; dotVec[index] = math.dot((CloudGoal[index].SubGoal - currentCloudPosition.Value), posSum - desiredSum); }
public void Execute(int index) { //fetch cloud id int currentCloudID = CloudData[index].ID; //fetch cloud agents in window int agentsInWindow; if (!CloudID2AgentInWindow.TryGetValue(currentCloudID, out agentsInWindow)) { return; } // Debug.Log("PASS1"); //fetch desired cloud agents in window int desiredAgentsInWindow; if (!DesiredCloudID2AgentInWindow.TryGetValue(currentCloudID, out desiredAgentsInWindow)) { return; } //Debug.Log("PASS2"); //create um menos o outro int agentsToCreate = (int)math.max(desiredAgentsInWindow - agentsInWindow, 0f); //Debug.Log(agentsToCreate); //if (agentsToCreate < 0) // Debug.Log("DEU MEME GURIZADA"); List <float3> positionList = new List <float3>(); NativeMultiHashMapIterator <int> it; float3 currentCellPosition; bool keepgoing = CloudMarkersMap.TryGetFirstValue(currentCloudID, out currentCellPosition, out it); if (!keepgoing) { return; } //Debug.Log("PASSTOT"); if (CheckCreatePosition(currentCellPosition)) { //Debug.Log(currentCellPosition); positionList.Add(currentCellPosition); } while (CloudMarkersMap.TryGetNextValue(out currentCellPosition, ref it)) { if (CheckCreatePosition(currentCellPosition)) { positionList.Add(currentCellPosition); //Debug.Log(currentCellPosition); } } agentsToCreate = math.min(CloudData[index].AgentQuantity - Counter[index].Quantity, agentsToCreate); int spawnPerCell = (int)math.max(math.ceil((float)agentsToCreate / positionList.Count), 0f); int spawned = 0; //TODO: Fix postionList count == 0 if (positionList.Count <= 0) { return; } //Random Distribution System.Random r = new System.Random(System.DateTime.UtcNow.Millisecond); while (agentsToCreate - spawned > 0) { float3 position = positionList[r.Next(positionList.Count - 1)]; //create agent BioCrowds.AgentSpawner.Parameters par = new BioCrowds.AgentSpawner.Parameters { cloud = currentCloudID, goal = CloudGoal[index].SubGoal, maxSpeed = CloudData[index].MaxSpeed, qtdAgents = math.min(agentsToCreate - spawned, spawnPerCell), spawnDimensions = new float2 { x = 2f, y = 2f }, spawnOrigin = position }; //Debug.Log("CREATE AGENT " + currentCloudID); buffer.TryAdd(GridConverter.Position2CellID(position), par); AddedAgentsPerCloud.Add(currentCloudID, par.qtdAgents); spawned += par.qtdAgents; } //Uniform distribution //foreach (float3 position in positionList) //{ // //create agent // BioCrowds.AgentSpawner.Parameters par = new BioCrowds.AgentSpawner.Parameters // { // cloud = currentCloudID, // goal = CloudGoal[index].SubGoal, // maxSpeed = CloudData[index].MaxSpeed, // qtdAgents = math.min(agentsToCreate-spawned, spawnPerCell), // spawnDimensions = new float2 { x = 2f, y = 2f }, // spawnOrigin = position // }; // //Debug.Log("CREATE AGENT " + currentCloudID); // buffer.TryAdd(GridConverter.Position2CellID(position), par); // AddedAgentsPerCloud.Add(currentCloudID, par.qtdAgents); // spawned += par.qtdAgents; //} //Debug.Log("Agents to create: " + agentsToCreate + " , spawnPerCell: " + spawnPerCell + " , positions: " + positionList.Count + " , spawned: " + spawned); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { if (Settings.experiment.BioCloudsEnabled) { lastAgentId = AgentAtCellQuantity[AgentAtCellQuantity.Length - 1] + lastAgentId; int lastValue = 0; for (int i = 1; i < m_CellGroup.Length; i++) { float3 cellPos = WindowManager.Crowds2Clouds(m_CellGroup.CellName[i].Value); int ind = GridConverter.Position2CellID(cellPos); AgentAtCellQuantity[i] = lastValue + AgentAtCellQuantity[i - 1]; if (clouds2Crowds.parameterBuffer.TryGetValue(ind, out Parameters spawnList)) { lastValue = spawnList.qtdAgents; } } var SpawnGroupJob = new SpawnGroup { parBuffer = clouds2Crowds.parameterBuffer, CommandBuffer = barrier.CreateCommandBuffer().ToConcurrent(), CellName = m_CellGroup.CellName, AgentAtCellQuantity = AgentAtCellQuantity, LastIDUsed = lastAgentId }; var SpawnGroupHandle = SpawnGroupJob.Schedule(m_CellGroup.Length, Settings.BatchSize, inputDeps); SpawnGroupHandle.Complete(); return(SpawnGroupHandle); } else { int lastValue = parBuffer[0].qtdAgents; AgentAtCellQuantity[0] = 0; for (int i = 1; i < parBuffer.Length; i++) { AgentAtCellQuantity[i] = lastValue + AgentAtCellQuantity[i - 1]; Parameters spawnList = parBuffer[i - 1]; lastValue = spawnList.qtdAgents; } var job = new InicialSpawn { AgentAtCellQuantity = AgentAtCellQuantity, CommandBuffer = barrier.CreateCommandBuffer().ToConcurrent(), parBuffer = parBuffer }; var handle = job.Schedule(parBuffer.Length, Settings.BatchSize, inputDeps); handle.Complete(); this.Enabled = false; return(handle); } }
public void Execute(int index) { int doNotFreeze = 0; float3 cellPos = WindowManager.Crowds2Clouds(CellName[index].Value); int ind = GridConverter.Position2CellID(cellPos); Parameters spawnList; bool keepgoing = parBuffer.TryGetValue(ind, out spawnList); if (!keepgoing) { return; } float3 convertedOrigin = WindowManager.Clouds2Crowds(spawnList.spawnOrigin); float2 dim = spawnList.spawnDimensions; int qtdAgtTotal = spawnList.qtdAgents; int maxZ = (int)(convertedOrigin.z + dim.y); int maxX = (int)(convertedOrigin.x + dim.x); int minZ = (int)convertedOrigin.z; int minX = (int)convertedOrigin.x; float maxSpeed = spawnList.maxSpeed; //Debug.Log(" MAX MIN " + new int4(maxZ, minZ, maxX, minX)); int startID = AgentAtCellQuantity[index] + LastIDUsed; System.Random r = new System.Random(DateTime.UtcNow.Millisecond); int CellX = minX + 1; int CellZ = minZ + 1; int CellY = 0; //Debug.Log("ConvertedOrigin:" + convertedOrigin + "CELL: " + CellX + " " + CellZ); //Problema total agents for (int i = startID; i < qtdAgtTotal + startID; i++) { //Debug.Log("Agent id : " + i); if (doNotFreeze > qtdAgtTotal) { doNotFreeze = 0; //maxZ += 2; //maxX += 2; } float x = (float)r.NextDouble() * (maxX - minX) + minX; float z = (float)r.NextDouble() * (maxZ - minZ) + minZ; float y = 0; //Debug.Log("AGENT: " + x + " " + z); float3 g = WindowManager.Clouds2Crowds(spawnList.goal); //x = UnityEngine.Random.Range(x - 0.99f, x + 0.99f); //float y = 0f; //z = UnityEngine.Random.Range(z - 0.99f, z + 0.99f); CommandBuffer.CreateEntity(index, AgentArchetype); CommandBuffer.SetComponent(index, new Position { Value = new float3(x, y, z) }); CommandBuffer.SetComponent(index, new Rotation { Value = Quaternion.identity }); //Debug.Log(maxSpeed / Settings.experiment.FramesPerSecond); CommandBuffer.SetComponent(index, new AgentData { ID = i, MaxSpeed = maxSpeed + (maxSpeed /* (float)(r.NextDouble() */ * 0.2f),// / Settings.experiment.FramesPerSecond, Radius = 1f }); CommandBuffer.SetComponent(index, new AgentStep { delta = float3.zero }); CommandBuffer.SetComponent(index, new Rotation { Value = quaternion.identity }); CommandBuffer.SetComponent(index, new CellName { Value = new int3(CellX, CellY, CellZ) }); CommandBuffer.SetComponent(index, new AgentGoal { SubGoal = g, EndGoal = g }); //entityManager.AddComponent(newAgent, ComponentType.FixedArray(typeof(int), qtdMarkers)); //TODO:Normal Life stuff change CommandBuffer.SetComponent(index, new Counter { Value = 0 }); CommandBuffer.SetComponent(index, new NormalLifeData { confort = 0, stress = 0, agtStrAcumulator = 0f, movStrAcumulator = 0f, incStress = 0f }); CommandBuffer.SetComponent <BioCrowdsAnchor>(index, new BioCrowdsAnchor { Pivot = WindowManager.instance.originBase }); CommandBuffer.AddSharedComponent(index, AgentRenderer); CommandBuffer.AddSharedComponent(index, new AgentCloudID { CloudID = spawnList.cloud }); } }
public void Execute(int index) { float3 currentCellPosition; NativeMultiHashMapIterator <int> it; float3 moveStep = float3.zero; float3 direction = float3.zero; CloudTotalW.TryGetValue(CloudData[index].ID, out float totalW); bool keepgoing = CloudMarkersMap.TryGetFirstValue(CloudData[index].ID, out currentCellPosition, out it); if (!keepgoing) { return; } float F = CloudCalculations.GetF(currentCellPosition, CloudPositions[index].Value, CloudGoals[index].SubGoal - CloudPositions[index].Value); var auxinWeight = CloudCalculations.PartialW(totalW, F) * CloudData[index].MaxSpeed * (currentCellPosition - CloudPositions[index].Value); direction += auxinWeight; if (useSplit) { if (ExtraWeightCell.TryGetValue(CloudData[index].ID, out int extraweightcell)) { if (GridConverter.Position2CellID(currentCellPosition) == extraweightcell) { //TODO dynamic extra weight direction += 5 * auxinWeight; } } } while (CloudMarkersMap.TryGetNextValue(out currentCellPosition, ref it)) { F = CloudCalculations.GetF(currentCellPosition, CloudPositions[index].Value, CloudGoals[index].SubGoal - CloudPositions[index].Value); direction += CloudCalculations.PartialW(totalW, F) * CloudData[index].MaxSpeed * (currentCellPosition - CloudPositions[index].Value); if (useSplit) { if (ExtraWeightCell.TryGetValue(CloudData[index].ID, out int extraweightcell)) { if (GridConverter.Position2CellID(currentCellPosition) == extraweightcell) { direction += auxinWeight; } } } } float moduleM = math.length(direction); float s = (float)(moduleM * math.PI); float3 normalized_direction = math.normalize(direction); float3 normalized_goalvector = math.normalize(CloudGoals[index].SubGoal - CloudPositions[index].Value); if (s > CloudData[index].MaxSpeed) { s = CloudData[index].MaxSpeed; } if (moduleM > 0.00001f) { moveStep = s * (normalized_direction); } else { moveStep = float3.zero; } CloudStep[index] = new CloudMoveStep() { Delta = moveStep }; }