-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMS_Storable.cpp
155 lines (130 loc) · 3.49 KB
/
IMS_Storable.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "Common.h"
#include "GUIincludes.h"
#include "Events.h"
#include "Moduletypes.h"
#include "IMS_ModuleFunctionData_Base.h"
#include "IMS_ModuleFunctionData_Pressurised.h"
#include "ModuleFunctionIncludes.h"
#include "IMS_Location.h"
#include "IMS_ModuleDataManager.h"
#include "IMS_Movable.h"
#include "IMS_Storable.h"
#include "Calc.h"
#include <iomanip>
IMS_Storable::IMS_Storable(double volume, CONSUMABLEDATA *contents, IMS_Location *module, double initial_mass)
: IMS_Movable(module)
{
this->volume = volume;
consumable = contents;
capacity = Calc::Round(volume * consumable->density, 1000);
if (initial_mass == -1)
{
initial_mass = capacity;
}
//somebody's trying to initialise with more mass than the storable can carry
assert(initial_mass <= capacity);
mass = initial_mass;
}
IMS_Storable::IMS_Storable(string serialized_storable, IMS_Location *location)
: IMS_Movable(location)
{
vector<string> tokens;
Helpers::Tokenize(serialized_storable, tokens, " :");
if (tokens.size() != 8)
{
Helpers::writeToLog(string("Invalid serialization string for storable in scenario: " + serialized_storable
+ ", aborting simulation!"), L_ERROR);
throw invalid_argument("IMS failed to load from scenario, see log for details");
}
consumable = IMS_ModuleDataManager::GetConsumableData(IMS_ModuleDataManager::GetConsumableId(tokens[1]));
volume = Helpers::stringToDouble(tokens[3]);
capacity = Calc::Round(volume * consumable->density, 1000);
mass = Helpers::stringToDouble(tokens[5]);
available = (bool)Helpers::stringToInt(tokens[7]);
}
IMS_Storable::~IMS_Storable()
{
}
double IMS_Storable::Fill()
{
double previousmass = mass;
mass = capacity;
addEvent(new MassHasChangedEvent);
addEvent(new ConsumableAddedEvent(previousmass));
return capacity - previousmass;
}
double IMS_Storable::AddContent(double amount_kg, int consumable_id)
{
//verify consumable type
if (consumable_id != consumable->id)
{
Helpers::writeToLog(string("Attempt to add incompatible consumable to storable!"), L_WARNING);
return 0.0;
}
if (mass < capacity)
{
double previousmass = mass;
mass += amount_kg;
//verify that the added amount can actually be placed
if (mass > capacity)
{
double addedmass = amount_kg - (mass - capacity);
mass = capacity;
return addedmass;
}
addEvent(new MassHasChangedEvent());
addEvent(new ConsumableAddedEvent(previousmass));
}
else
{
return 0.0;
}
return amount_kg;
}
double IMS_Storable::RemoveContent(double amount_kg, int consumable_id)
{
//verify consumable type
if (consumable_id != consumable->id)
{
Helpers::writeToLog(string("Attempt to remove incompatible consumable from storable!"), L_WARNING);
return 0.0;
}
//if the mass is already zero, nothing will be removed anyways
if (mass > 0.0)
{
mass -= amount_kg;
//verify that the requested ammount can actually be removed
if (mass < 0)
{
double removedmass = amount_kg + mass;
mass = 0.0;
return removedmass;
}
//fire an event to inform the module that the mass has changed
addEvent(new MassHasChangedEvent());
}
else
{
return 0.0;
}
return amount_kg;
}
void IMS_Storable::PreStep(IMS_Location *location)
{
sendEvents();
}
string IMS_Storable::Serialize()
{
stringstream ss;
ss << fixed << setprecision(3);
ss << "T:" << consumable->shorthand << " V:" << volume << " M:" << mass << " A:" << (int)available;
return ss.str();
}
bool IMS_Storable::ProcessEvent(Event_Base *e)
{
return false;
}
string IMS_Storable::GetConsumableName()
{
return consumable->name;
}