-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisease.java
99 lines (92 loc) · 3.84 KB
/
Disease.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.HashMap;
import java.util.List;
import java.util.Random;
/**
* The disease class which is responsible for infecting the animals and potentially killing them
*
* @author Syraj Alkhalil and Cosmo Colman
* @version 2022.02.27 (2)
*/
public class Disease {
private final double spreadRate; // the spread rate of the disease
private final double deadliness; // how deadly the disease is
private int infectionPeriod = 2; // how long the disease will last
private final Entity mainHost; // the main host of the disease
private final HashMap<Organism, Integer> numOfInteractions; // the number of infections per animal type this helps in mutations
/**
* make a disease that infects a certain type of animal
*
* @param spreadRate The spread rate (how contagious the disease is).
* @param deadliness How deadly the disease is
* @param mainHost the main type of host that this disease will infect
*/
public Disease(double spreadRate, double deadliness, Organism mainHost) {
this.spreadRate = spreadRate;
this.deadliness = deadliness;
this.mainHost = mainHost;
numOfInteractions = new HashMap<>();
numOfInteractions.put(mainHost, 10);
}
/**
* This method is used to update how many animals the disease comes in contact with
* this will be used to mutate the types of animals the disease can infect, for example
* if the disease comes in contact with
*/
public void updateInteractions() {
Field field = mainHost.getField();
if(field != null){
List<Location> potentialVictim = field.getNotNullAdjacentLocations(mainHost.getLocation());
for(Location victim : potentialVictim){
Organism organism = (Organism) field.getObjectAt(victim);
if(numOfInteractions.get(organism) == null){
numOfInteractions.put(organism, 0);
}else{
numOfInteractions.put(organism, numOfInteractions.get(organism) +1 );
}
}
}
}
/**
* This is essentially the act method for the disease.
* the disease will look for not null locations around it and will try to infect these organisms
* The disease is also affected by the weather.
*
* @param currentWeather the current weather
*/
public void infect(Weather currentWeather) {
updateInteractions();
if((new Random().nextDouble() < (spreadRate - (currentWeather.getActualDownfall()*0.04))) && infectionPeriod > 0){
// infect neighbouring squares with a fixed probability.
Field field = mainHost.getField();
List<Location> adjacent = field.getNotNullAdjacentLocations(mainHost.getLocation());
for(Location where : adjacent){
Organism organism = (Organism) field.getObjectAt(where);
if(canInfect(organism) && !(organism).getIsInfected()){
if(new Random().nextDouble() < spreadRate){
organism.setInfected(true);
organism.addDisease(this);
organism.dieDueToInfection();
}
}
}
}
infectionPeriod--;
}
/**
* A simple getter method that returns the deadliness field
*
* @return how deadly the disease is
*/
public double getDeadliness() {
return deadliness;
}
/**
* decide if we can infect a given organism
*
* @param organism the organism we wish to infect
* @return true if we can infect the organism
*/
public boolean canInfect(Organism organism) {
return numOfInteractions.get(organism) >= 10;
}
}