diff --git a/VsProject/PropagationRemasteredBeta/Human.cs b/VsProject/PropagationRemasteredBeta/Human.cs index 026f1ae..940f305 100644 --- a/VsProject/PropagationRemasteredBeta/Human.cs +++ b/VsProject/PropagationRemasteredBeta/Human.cs @@ -10,10 +10,21 @@ namespace PropagationRemasteredBeta public class Human : ICloneable { //State values + /* public Color SAIN_COLOR = Color.Green; public Color INFECTED_COLOR = Color.Red; public Color IMMUNE_COLOR = Color.Blue; public Color DEATH_COLOR = Color.Black; + + 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); + */ + public SolidBrush SAIN_COLOR; + public SolidBrush INFECTED_COLOR; + public SolidBrush IMMUNE_COLOR; + public SolidBrush DEATH_COLOR; public const int SAIN = 0; public const int INFECTED = 1; @@ -27,7 +38,8 @@ namespace PropagationRemasteredBeta private Size _humanSize; private int _lifeTimeCounter; private int _InfectedTimeCounter; - private Color _sprite; + //private Color _sprite; + private SolidBrush _sprite; public Point Position { get => _position; set => _position = value; } public int State @@ -58,15 +70,27 @@ namespace PropagationRemasteredBeta public Size HumanSize { get => _humanSize; set => _humanSize = value; } public int LifeTimeCounter { get => _lifeTimeCounter; set => _lifeTimeCounter = value; } public int InfectedTimeCounter { get => _InfectedTimeCounter; set => _InfectedTimeCounter = value; } - public Color Sprite { get => _sprite; set => _sprite = value; } - - public Human(Point position, int state, Size humanSize) + //public Color Sprite { get => Sprite; set => Sprite = value; } + public SolidBrush Sprite { get => _sprite; set => _sprite = value; } + public Human(Point position, int state, Size humanSize, SolidBrush sainColor, SolidBrush infectedColor, SolidBrush immuneColor, SolidBrush deadColor) { HumanSize = humanSize; Position = position; - State = state; + LifeTimeCounter = 1; InfectedTimeCounter = 0; + SAIN_COLOR = sainColor; + INFECTED_COLOR = infectedColor; + IMMUNE_COLOR = immuneColor; + DEATH_COLOR = deadColor; + + //IMPORTANT only set state AFTER colors + State = state; + } + + public Human(Point position, int state, Size humanSize) : this(position,state,humanSize, new SolidBrush(Color.Green), new SolidBrush(Color.Red), new SolidBrush(Color.Blue), new SolidBrush(Color.Black)) + { + } public Human(Point position, int state) : this(position, state, new Size(DEFAULT_HUMAN_SIZE, DEFAULT_HUMAN_SIZE)) { @@ -74,7 +98,7 @@ namespace PropagationRemasteredBeta } //we dont have any constructor with only default values because the human needs his position in the field to process the infection - public void ChangeBaseColors(Color sainColor, Color infectedColor, Color immuneColor, Color deadColor) + public void ChangeBaseColors(SolidBrush sainColor, SolidBrush infectedColor, SolidBrush immuneColor, SolidBrush deadColor) { SAIN_COLOR = sainColor; IMMUNE_COLOR = immuneColor; @@ -112,7 +136,7 @@ namespace PropagationRemasteredBeta { T.PeopleToCheck.Add(target); } - + //check top diff --git a/VsProject/PropagationRemasteredBeta/Terrain.cs b/VsProject/PropagationRemasteredBeta/Terrain.cs index 8d8c534..61d6e2e 100644 --- a/VsProject/PropagationRemasteredBeta/Terrain.cs +++ b/VsProject/PropagationRemasteredBeta/Terrain.cs @@ -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 _peopleToCheck; + private ConcurrentBag _peopleToCheck; private Size _terrainSize; private int _infectedProportion; @@ -53,8 +59,8 @@ namespace PropagationRemasteredBeta public List ImmuneCount { get => _immuneCount; set => _immuneCount = value; } public List DeadCount { get => _deadCount; set => _deadCount = value; } public int SimulationDuration { get => _simulationDuration; set => _simulationDuration = value; } - public List PeopleToCheck { get => _peopleToCheck; set => _peopleToCheck = value; } public Random Rnd { get => rnd; set => rnd = value; } + public ConcurrentBag 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(); + PeopleToCheck = new ConcurrentBag(); + //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 filtered = PeopleToCheck.Distinct().ToList(); + Parallel.ForEach(filtered, + target => + { + Population[target.Position.X, target.Position.Y] = target.CheckInfection(InfectionRate,Rnd); + } + ); + */ + + //List filtered = PeopleToCheck.Distinct().ToList(); 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++; }