-
Notifications
You must be signed in to change notification settings - Fork 1
/
person.cpp
114 lines (112 loc) · 3.04 KB
/
person.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "person.hpp"
#include "simulation.h"
#include "popularplace.hpp"
#include <cmath>
#include <cstdlib>
namespace sim{
Person::Person()
: status(VULNERABLE), elapsedTimeSinceInfection(0),
locationStatus(AT_HOME), timeSinceArrival(0),
timeTillDeparture(0), infection_duration(rand()%MAX_INFECTION_DURATION)
{
home = new Place();
location = new Location(home->location->getX(), home->location->getY());
if(fmod(rand(), (100/(SOCIAL_DISTANCING_CHANCE*100)+1)) == 0){
isSocialDistancing = true;
} else{
isSocialDistancing = false;
}
targetLocation = home->location;
}
Person::~Person(){
delete home;
delete location;
delete targetLocation;
}
bool Person::on_contact(){
if(status == VULNERABLE){
if(fmod(rand(), (100/(INFECTION_CHANCE*100))+1) == 0){
status = INFECTED;
test:
if(infection_duration <= 38){
infection_duration = rand()%MAX_INFECTION_DURATION;
goto test;
}
return true;
} else{
return false;
}
} else{
return false;
}
}
void Person::action(){
if(status != DEAD){
if(locationStatus == MOVING){
if(location->move(targetLocation, MAX_SPEED)){ //Randomize max speed later for better simulation
if(location->atLocation(*(home->location))){
locationStatus = AT_HOME;
timeTillDeparture = rand()%(MAX_TIME_AT_HOME+1);
} else{
locationStatus = POPULAR_PLACE;
timeTillDeparture = rand()%(MAX_TIME_AT_POPULAR_PLACE+1);
}
}
} else if(locationStatus == AT_HOME){
if(timeSinceArrival >= timeTillDeparture){
timeSinceArrival = 0;
timeTillDeparture = 0;
locationStatus = MOVING;
targetLocation = (PopularPlace::places[rand()%POPULAR_PLACES]).location;
location->move(targetLocation, MAX_SPEED);
} else{
timeSinceArrival++;
}
} else{
if(timeSinceArrival >= timeTillDeparture){
timeSinceArrival = 0;
timeTillDeparture = 0;
locationStatus = MOVING;
targetLocation = home->location;
location->move(targetLocation, MAX_SPEED);
} else{
timeSinceArrival++;
}
}
}
}
int Person::addHour(){
if(status == INFECTED){
elapsedTimeSinceInfection++;
if(elapsedTimeSinceInfection >= infection_duration){
if(rand()%(((int)(100/(FATALITY_RATE*100)))+1) == 0){
status = DEAD;
action();
return DEAD;
} else{
status = IMMUNE;
action();
return IMMUNE;
}
} else{
action();
return INFECTED;
}
} else if(status == VULNERABLE){
action();
return VULNERABLE;
} else{
return -1;
}
return -1;
}
Location* Person::getLocation() const{
return location;
}
void Person::setStatus(int newstatus){
status = newstatus;
}
int Person::getLocationStatus() const{
return locationStatus;
}
}