-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhouse.cpp
78 lines (67 loc) · 2.29 KB
/
house.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
#include "house.h"
#include <cstdlib>
#include <iostream>
House::House()
{
for(int i = 0; i < 9; i++)
{
appliances.push_back(new Appliance((TYPE_ENUM)i, std::rand() % (types[i].maxE - types[i].minE) + types[i].minE));
appliances[i]->queueAt = types[i].minUseInterval + std::rand() % (types[i].maxUseInterval - types[i].minUseInterval);
}
}
int House::getPower()
{
int ret = 0;
for(int i = 0; i < 9; i++)
{
if(appliances[i]->powerState == POWER_FORCED_ON || appliances[i]->powerState == POWER_GRANTED_ON)
ret += appliances[i]->W;
}
return ret;
}
int House::getPower(TYPE_ENUM type)
{
int ret = 0;
for(int i = 0; i < 9; i++)
{
if((appliances[i]->powerState == POWER_FORCED_ON || appliances[i]->powerState == POWER_GRANTED_ON) && appliances[i]->getType() == type)
ret += appliances[i]->W;
}
return ret;
}
int House::getForced()
{
int ret = 0;
for(int i = 0; i < 9; i++)
{
if(appliances[i]->powerState == POWER_FORCED_ON)
ret += appliances[i]->W;
}
return ret;
}
void House::tick(Cluster* c)
{
//std::cout << appliances[0]->type << ", " << appliances[0]->powerState << "," << appliances[0]->timer << ", " << appliances[0]->queueAt << std::endl;
for(int i = 0; i < 9; i++)
{
appliances[i]->timer++;
if(appliances[i]->timer > appliances[i]->queueAt && appliances[i]->powerState == POWER_OFF)
{
c->toQueue(appliances[i]);
appliances[i]->powerState = POWER_QUEUED;
}
if(appliances[i]->timer > types[appliances[i]->type].maxUseInterval && (appliances[i]->powerState == POWER_QUEUED || appliances[i]->powerState == POWER_OFF))
{
appliances[i]->turnOn(true);
}
if(appliances[i]->timer > appliances[i]->queueAt && (appliances[i]->powerState == POWER_FORCED_ON || appliances[i]->powerState == POWER_GRANTED_ON))
{
appliances[i]->powerState = POWER_OFF;
appliances[i]->queueAt = types[appliances[i]->type].minUseInterval + std::rand() % (types[appliances[i]->type].maxUseInterval - types[appliances[i]->type].minUseInterval);
appliances[i]->timer = 0;
}
}
}
std::vector<Appliance*> House::getAppliances(){
return appliances;
}