Code cleanup and minor performance increase
This commit is contained in:
@@ -9,36 +9,22 @@ 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;
|
||||
|
||||
//those are constants to use with the state attribute and not get confused
|
||||
public const int SAIN = 0;
|
||||
public const int INFECTED = 1;
|
||||
public const int IMMUNE = 2;
|
||||
public const int DEAD = 3;
|
||||
|
||||
const int DEFAULT_HUMAN_SIZE = 1;
|
||||
|
||||
private Point _position;
|
||||
private int _state;
|
||||
private Size _humanSize;
|
||||
private int _lifeTimeCounter;
|
||||
private int _InfectedTimeCounter;
|
||||
//private Color _sprite;
|
||||
private SolidBrush _sprite;
|
||||
|
||||
public Point Position { get => _position; set => _position = value; }
|
||||
@@ -67,14 +53,12 @@ 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 SolidBrush Sprite { get => _sprite; set => _sprite = value; }
|
||||
public Human(Point position, int state, Size humanSize, SolidBrush sainColor, SolidBrush infectedColor, SolidBrush immuneColor, SolidBrush deadColor)
|
||||
public Human(Point position, int state, SolidBrush sainColor, SolidBrush infectedColor, SolidBrush immuneColor, SolidBrush deadColor)
|
||||
{
|
||||
HumanSize = humanSize;
|
||||
Position = position;
|
||||
|
||||
LifeTimeCounter = 1;
|
||||
@@ -83,20 +67,14 @@ namespace PropagationRemasteredBeta
|
||||
INFECTED_COLOR = infectedColor;
|
||||
IMMUNE_COLOR = immuneColor;
|
||||
DEATH_COLOR = deadColor;
|
||||
|
||||
//IMPORTANT only set state AFTER colors
|
||||
//IMPORTANT only set state AFTER colors or some people will have sprites set to null
|
||||
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 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))
|
||||
{
|
||||
|
||||
}
|
||||
//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(SolidBrush sainColor, SolidBrush infectedColor, SolidBrush immuneColor, SolidBrush deadColor)
|
||||
{
|
||||
@@ -126,80 +104,41 @@ namespace PropagationRemasteredBeta
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Point> peopleToCheckLocations = new List<Point>();
|
||||
//we need to check if we are not in a corner
|
||||
if (Position.X > 0 && Position.Y > 0 && Position.X < (T.TerrainSize.Width - 1) && Position.Y < (T.TerrainSize.Height - 1))
|
||||
{
|
||||
//check top left
|
||||
location = new Point(Position.X - 1, Position.Y + 1);
|
||||
target = T.Population[location.X, location.Y];
|
||||
if (target.State == Human.SAIN)
|
||||
{
|
||||
T.PeopleToCheck.Add(target);
|
||||
}
|
||||
|
||||
|
||||
peopleToCheckLocations.Add(new Point(Position.X - 1, Position.Y + 1));
|
||||
|
||||
//check top
|
||||
location = new Point(Position.X, Position.Y + 1);
|
||||
target = T.Population[location.X, location.Y];
|
||||
|
||||
if (target.State == Human.SAIN)
|
||||
{
|
||||
T.PeopleToCheck.Add(target);
|
||||
}
|
||||
peopleToCheckLocations.Add(new Point(Position.X, Position.Y + 1));
|
||||
|
||||
//check top left
|
||||
location = new Point(Position.X + 1, Position.Y + 1);
|
||||
target = T.Population[location.X, location.Y];
|
||||
|
||||
if (target.State == Human.SAIN)
|
||||
{
|
||||
T.PeopleToCheck.Add(target);
|
||||
}
|
||||
peopleToCheckLocations.Add(new Point(Position.X, Position.Y + 1));
|
||||
|
||||
//check left
|
||||
location = new Point(Position.X - 1, Position.Y);
|
||||
target = T.Population[location.X, location.Y];
|
||||
|
||||
if (target.State == Human.SAIN)
|
||||
{
|
||||
T.PeopleToCheck.Add(target);
|
||||
}
|
||||
peopleToCheckLocations.Add(new Point(Position.X - 1, Position.Y));
|
||||
|
||||
//check right
|
||||
location = new Point(Position.X + 1, Position.Y);
|
||||
target = T.Population[location.X, location.Y];
|
||||
|
||||
if (target.State == Human.SAIN)
|
||||
{
|
||||
T.PeopleToCheck.Add(target);
|
||||
}
|
||||
peopleToCheckLocations.Add(new Point(Position.X + 1, Position.Y));
|
||||
|
||||
//check bottom left
|
||||
location = new Point(Position.X - 1, Position.Y - 1);
|
||||
target = T.Population[location.X, location.Y];
|
||||
|
||||
if (target.State == Human.SAIN)
|
||||
{
|
||||
T.PeopleToCheck.Add(target);
|
||||
}
|
||||
peopleToCheckLocations.Add(new Point(Position.X - 1, Position.Y - 1));
|
||||
|
||||
//check bottom
|
||||
location = new Point(Position.X, Position.Y - 1);
|
||||
target = T.Population[location.X, location.Y];
|
||||
peopleToCheckLocations.Add(new Point(Position.X, Position.Y - 1));
|
||||
|
||||
//check bottom right
|
||||
peopleToCheckLocations.Add(new Point(Position.X + 1, Position.Y - 1));
|
||||
|
||||
foreach (Point p in peopleToCheckLocations)
|
||||
{
|
||||
target = T.Population[p.X, p.Y];
|
||||
if (target.State == Human.SAIN)
|
||||
{
|
||||
T.PeopleToCheck.Add(target);
|
||||
}
|
||||
|
||||
//check bottom right
|
||||
location = new Point(Position.X + 1, Position.Y - 1);
|
||||
target = T.Population[location.X, location.Y];
|
||||
|
||||
if (target.State == Human.SAIN)
|
||||
{
|
||||
T.PeopleToCheck.Add(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -218,7 +157,6 @@ namespace PropagationRemasteredBeta
|
||||
public object Clone()
|
||||
{
|
||||
Human newHuman = new Human(Position, State);
|
||||
newHuman.HumanSize = HumanSize;
|
||||
newHuman.InfectedTimeCounter = InfectedTimeCounter;
|
||||
newHuman.LifeTimeCounter = LifeTimeCounter;
|
||||
return newHuman;
|
||||
|
||||
@@ -18,6 +18,7 @@ namespace PropagationRemasteredBeta
|
||||
const int DEFAULT_INFECTION_RATE = 20;
|
||||
const int DEFAULT_CURE_TIME = 25;
|
||||
|
||||
//Only here for performance purposes
|
||||
public SolidBrush SAIN_COLOR = new SolidBrush(Color.Green);
|
||||
public SolidBrush INFECTED_COLOR = new SolidBrush(Color.Red);
|
||||
public SolidBrush IMMUNE_COLOR = new SolidBrush(Color.Blue);
|
||||
@@ -81,7 +82,6 @@ namespace PropagationRemasteredBeta
|
||||
DeadCount = new List<int>();
|
||||
|
||||
SimulationDuration = -1;
|
||||
|
||||
}
|
||||
public Terrain() : this(new Size(DEFAULT_SIZE, DEFAULT_SIZE), DEFAULT_INFECTED_PROPORTION, DEFAULT_IMMUNE_PROPORTION, DEFAULT_DEATH_RATE, DEFAULT_INFECTION_RATE, DEFAULT_CURE_TIME)
|
||||
{
|
||||
@@ -113,7 +113,7 @@ namespace PropagationRemasteredBeta
|
||||
state = Human.IMMUNE;
|
||||
}
|
||||
}
|
||||
Human tmp = new Human(new Point(width, height), state,new Size(1,1),SAIN_COLOR,INFECTED_COLOR,IMMUNE_COLOR,DEATH_COLOR);
|
||||
Human tmp = new Human(new Point(width, height), state,SAIN_COLOR,INFECTED_COLOR,IMMUNE_COLOR,DEATH_COLOR);
|
||||
Population[width, height] = tmp;
|
||||
}
|
||||
}
|
||||
@@ -138,8 +138,7 @@ namespace PropagationRemasteredBeta
|
||||
state = Human.IMMUNE;
|
||||
}
|
||||
|
||||
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);
|
||||
Human tmp = new Human(new Point(width, height), state, SAIN_COLOR, INFECTED_COLOR, IMMUNE_COLOR, DEATH_COLOR);
|
||||
Population[width, height] = tmp;
|
||||
}
|
||||
}
|
||||
@@ -195,17 +194,6 @@ namespace PropagationRemasteredBeta
|
||||
}//);
|
||||
}//);
|
||||
|
||||
/*
|
||||
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);
|
||||
@@ -224,10 +212,10 @@ namespace PropagationRemasteredBeta
|
||||
//we check that it is not the first frame and by default we expect the first frame to be dominantly sain people
|
||||
if (InfectedCount.Count() > 0)
|
||||
{
|
||||
int infecteds = InfectedCount[InfectedCount.Count() - 1];
|
||||
int sain = SainCount[SainCount.Count() - 1];
|
||||
int immune = ImmuneCount[ImmuneCount.Count() - 1];
|
||||
int deaths = DeadCount[DeadCount.Count() - 1];
|
||||
int infecteds = InfectedCount[InfectedCount.Count - 1];
|
||||
int sain = SainCount[SainCount.Count - 1];
|
||||
int immune = ImmuneCount[ImmuneCount.Count - 1];
|
||||
int deaths = DeadCount[DeadCount.Count - 1];
|
||||
|
||||
if (infecteds > sain && infecteds > immune && infecteds > deaths)
|
||||
{
|
||||
@@ -247,24 +235,26 @@ namespace PropagationRemasteredBeta
|
||||
}
|
||||
//as Color object cannot be a constant we cannot use Human.SAIN_COLOR we need to use a fully constructed human
|
||||
Human sample = new Human(new Point(-1, -1), Human.DEAD);
|
||||
|
||||
switch (dominantState)
|
||||
{
|
||||
case Human.SAIN:
|
||||
gr.FillRectangle(sample.SAIN_COLOR, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
sample.State = Human.SAIN;
|
||||
break;
|
||||
case Human.IMMUNE:
|
||||
gr.FillRectangle(sample.IMMUNE_COLOR, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
sample.State = Human.IMMUNE;
|
||||
break;
|
||||
case Human.INFECTED:
|
||||
gr.FillRectangle(sample.INFECTED_COLOR, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
sample.State = Human.INFECTED;
|
||||
break;
|
||||
case Human.DEAD:
|
||||
gr.FillRectangle(sample.DEATH_COLOR, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
sample.State = Human.DEAD;
|
||||
break;
|
||||
default:
|
||||
gr.FillRectangle(sample.SAIN_COLOR, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
sample.State = Human.SAIN;
|
||||
break;
|
||||
}
|
||||
gr.FillRectangle(sample.Sprite, new Rectangle(new Point(0, 0), new Size(Render.Width, Render.Height)));
|
||||
|
||||
for (int width = 0; width < TerrainSize.Width; width += 1)
|
||||
{
|
||||
@@ -274,7 +264,7 @@ namespace PropagationRemasteredBeta
|
||||
if (individual.State != dominantState)
|
||||
{
|
||||
SolidBrush c = individual.Sprite;
|
||||
gr.FillRectangle(c, new Rectangle(individual.Position, individual.HumanSize));
|
||||
gr.FillRectangle(c, new Rectangle(individual.Position, new Size(1,1)));
|
||||
DRAWED_ELEMENTS++;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user