Converted Brushes to solid brudhes and increased performances
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace PropagationRemasteredBeta
|
||||
{
|
||||
@@ -17,8 +18,13 @@ namespace PropagationRemasteredBeta
|
||||
const int DEFAULT_INFECTION_RATE = 20;
|
||||
const int DEFAULT_CURE_TIME = 25;
|
||||
|
||||
public SolidBrush SAIN_COLOR = new SolidBrush(Color.Green);
|
||||
public SolidBrush INFECTED_COLOR = new SolidBrush(Color.Red);
|
||||
public SolidBrush IMMUNE_COLOR = new SolidBrush(Color.Blue);
|
||||
public SolidBrush DEATH_COLOR = new SolidBrush(Color.Black);
|
||||
|
||||
private Human[,] _population;
|
||||
private List<Human> _peopleToCheck;
|
||||
private ConcurrentBag<Human> _peopleToCheck;
|
||||
private Size _terrainSize;
|
||||
|
||||
private int _infectedProportion;
|
||||
@@ -53,8 +59,8 @@ namespace PropagationRemasteredBeta
|
||||
public List<int> ImmuneCount { get => _immuneCount; set => _immuneCount = value; }
|
||||
public List<int> DeadCount { get => _deadCount; set => _deadCount = value; }
|
||||
public int SimulationDuration { get => _simulationDuration; set => _simulationDuration = value; }
|
||||
public List<Human> PeopleToCheck { get => _peopleToCheck; set => _peopleToCheck = value; }
|
||||
public Random Rnd { get => rnd; set => rnd = value; }
|
||||
public ConcurrentBag<Human> PeopleToCheck { get => _peopleToCheck; set => _peopleToCheck = value; }
|
||||
|
||||
public Terrain(Size terrainSize, int infectedProportion, int resistantProportion, int deathRate, int infectionRate, int cureTime)
|
||||
{
|
||||
@@ -83,6 +89,10 @@ namespace PropagationRemasteredBeta
|
||||
}
|
||||
public void Generate()
|
||||
{
|
||||
if (Population[0,0] != null)
|
||||
{
|
||||
Population = new Human[TerrainSize.Width, TerrainSize.Height];
|
||||
}
|
||||
//That is the basic method to scan trough the entire field
|
||||
for (int width = 0; width < TerrainSize.Width; width += 1)
|
||||
{
|
||||
@@ -103,13 +113,17 @@ namespace PropagationRemasteredBeta
|
||||
state = Human.IMMUNE;
|
||||
}
|
||||
}
|
||||
|
||||
Population[width, height] = new Human(new Point(width, height), state);
|
||||
Human tmp = new Human(new Point(width, height), state,new Size(1,1),SAIN_COLOR,INFECTED_COLOR,IMMUNE_COLOR,DEATH_COLOR);
|
||||
Population[width, height] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void GenerateOneForAll()
|
||||
{
|
||||
if (Population[0, 0] != null)
|
||||
{
|
||||
Population = new Human[TerrainSize.Width, TerrainSize.Height];
|
||||
}
|
||||
for (int width = 0; width < TerrainSize.Width; width += 1)
|
||||
{
|
||||
for (int height = 0; height < TerrainSize.Height; height += 1)
|
||||
@@ -124,8 +138,9 @@ namespace PropagationRemasteredBeta
|
||||
state = Human.IMMUNE;
|
||||
}
|
||||
|
||||
|
||||
Population[width, height] = new Human(new Point(width, height), state);
|
||||
Human tmp = new Human(new Point(width, height), state,new Size(1, 1), SAIN_COLOR, INFECTED_COLOR, IMMUNE_COLOR, DEATH_COLOR);
|
||||
//Population[width, height] = new Human(new Point(width, height), state);
|
||||
Population[width, height] = tmp;
|
||||
}
|
||||
}
|
||||
//we generate a single infected in a random location
|
||||
@@ -142,8 +157,9 @@ namespace PropagationRemasteredBeta
|
||||
InfectedCount.Add(0);
|
||||
DeadCount.Add(0);
|
||||
|
||||
PeopleToCheck = new List<Human>();
|
||||
PeopleToCheck = new ConcurrentBag<Human>();
|
||||
|
||||
//Parallel.For(0, TerrainSize.Width, width =>
|
||||
for (int width = 0; width < TerrainSize.Width; width += 1)
|
||||
{
|
||||
//Parallel.For(0, TerrainSize.Height, height =>
|
||||
@@ -159,7 +175,7 @@ namespace PropagationRemasteredBeta
|
||||
//nothing to do
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
//only for stats purposes
|
||||
switch (Population[width, height].State)
|
||||
{
|
||||
@@ -177,8 +193,19 @@ namespace PropagationRemasteredBeta
|
||||
break;
|
||||
}
|
||||
}//);
|
||||
}
|
||||
}//);
|
||||
|
||||
/*
|
||||
List<Human> filtered = PeopleToCheck.Distinct().ToList<Human>();
|
||||
Parallel.ForEach(filtered,
|
||||
target =>
|
||||
{
|
||||
Population[target.Position.X, target.Position.Y] = target.CheckInfection(InfectionRate,Rnd);
|
||||
}
|
||||
);
|
||||
*/
|
||||
|
||||
//List<Human> filtered = PeopleToCheck.Distinct().ToList<Human>();
|
||||
foreach (Human target in PeopleToCheck)
|
||||
{
|
||||
Population[target.Position.X,target.Position.Y] = target.CheckInfection(InfectionRate,Rnd);
|
||||
@@ -223,19 +250,19 @@ namespace PropagationRemasteredBeta
|
||||
switch (dominantState)
|
||||
{
|
||||
case Human.SAIN:
|
||||
gr.FillRectangle(new SolidBrush(sample.SAIN_COLOR), new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
gr.FillRectangle(sample.SAIN_COLOR, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
break;
|
||||
case Human.IMMUNE:
|
||||
gr.FillRectangle(new SolidBrush(sample.IMMUNE_COLOR), new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
gr.FillRectangle(sample.IMMUNE_COLOR, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
break;
|
||||
case Human.INFECTED:
|
||||
gr.FillRectangle(new SolidBrush(sample.INFECTED_COLOR), new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
gr.FillRectangle(sample.INFECTED_COLOR, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
break;
|
||||
case Human.DEAD:
|
||||
gr.FillRectangle(new SolidBrush(sample.DEATH_COLOR), new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
gr.FillRectangle(sample.DEATH_COLOR, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
break;
|
||||
default:
|
||||
gr.FillRectangle(new SolidBrush(sample.SAIN_COLOR), new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
gr.FillRectangle(sample.SAIN_COLOR, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -246,8 +273,8 @@ namespace PropagationRemasteredBeta
|
||||
Human individual = Population[width, height];
|
||||
if (individual.State != dominantState)
|
||||
{
|
||||
Color c = individual.Sprite;
|
||||
gr.FillRectangle(new SolidBrush(c), new Rectangle(individual.Position, individual.HumanSize));
|
||||
SolidBrush c = individual.Sprite;
|
||||
gr.FillRectangle(c, new Rectangle(individual.Position, individual.HumanSize));
|
||||
DRAWED_ELEMENTS++;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user