forked from gecube/opencaesar3
/
oc3_goodstore_simple.cpp
186 lines (143 loc) · 4.7 KB
/
oc3_goodstore_simple.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// This file is part of openCaesar3.
//
// openCaesar3 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// openCaesar3 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with openCaesar3. If not, see <http://www.gnu.org/licenses/>.
#include "oc3_goodstore_simple.hpp"
#include "oc3_math.hpp"
#include "oc3_variant.hpp"
SimpleGoodStore::SimpleGoodStore()
{
_maxQty = 0;
_goodStockList.resize(G_MAX);
for (int n = 0; n < (int)G_MAX; ++n)
{
_goodStockList[n] = GoodStock((GoodType)n, 0, 0);
}
}
void SimpleGoodStore::setMaxQty(const int maxQty)
{
_maxQty = maxQty;
}
int SimpleGoodStore::getMaxQty() const
{
return _maxQty;
}
int SimpleGoodStore::getCurrentQty() const
{
int qty = 0;
for( std::vector<GoodStock>::const_iterator goodIt = _goodStockList.begin(); goodIt != _goodStockList.end(); ++goodIt)
{
qty += (*goodIt)._currentQty;
}
return qty;
}
GoodStock& SimpleGoodStore::getStock(const GoodType &goodType)
{
return _goodStockList[goodType];
}
int SimpleGoodStore::getCurrentQty(const GoodType &goodType) const
{
return _goodStockList[goodType]._currentQty;
}
int SimpleGoodStore::getMaxQty(const GoodType &goodType) const
{
return _goodStockList[goodType]._maxQty;
}
void SimpleGoodStore::setMaxQty(const GoodType &goodType, const int maxQty)
{
_goodStockList[goodType]._maxQty = maxQty;
}
void SimpleGoodStore::setCurrentQty(const GoodType &goodType, const int currentQty)
{
_goodStockList[goodType]._currentQty = currentQty;
}
int SimpleGoodStore::getMaxStore(const GoodType goodType)
{
int freeRoom = 0;
if( !isDevastation() )
{
int globalFreeRoom = getMaxQty() - getCurrentQty();
// current free capacity
freeRoom = math::clamp( _goodStockList[goodType]._maxQty - _goodStockList[goodType]._currentQty, 0, globalFreeRoom );
// remove all storage reservations
for( _Reservations::iterator reservationIt = _getStoreReservations().begin(); \
reservationIt != _getStoreReservations().end(); ++reservationIt)
{
GoodStock &reservationStock = reservationIt->second;
freeRoom -= reservationStock._currentQty;
}
}
return freeRoom;
}
void SimpleGoodStore::applyStorageReservation(GoodStock &stock, const long reservationID)
{
GoodStock reservedStock = getStorageReservation(reservationID, true);
if (stock._goodType != reservedStock._goodType)
{
_OC3_DEBUG_BREAK_IF( "SimpleGoodStore:GoodType does not match reservation");
return;
}
if (stock._currentQty < reservedStock._currentQty)
{
_OC3_DEBUG_BREAK_IF( "SimpleGoodStore:Quantity does not match reservation");
return;
}
int amount = reservedStock._currentQty;
GoodStock ¤tStock = _goodStockList[reservedStock._goodType];
currentStock._currentQty += amount;
stock._currentQty -= amount;
}
void SimpleGoodStore::applyRetrieveReservation(GoodStock &stock, const long reservationID)
{
GoodStock reservedStock = getRetrieveReservation(reservationID, true);
if (stock._goodType != reservedStock._goodType)
{
_OC3_DEBUG_BREAK_IF("SimpleGoodStore:GoodType does not match reservation");
return;
}
if( stock._maxQty < stock._currentQty + reservedStock._currentQty)
{
_OC3_DEBUG_BREAK_IF("SimpleGoodStore:Quantity does not match reservation");
return;
}
int amount = reservedStock._currentQty;
GoodStock ¤tStock = getStock(reservedStock._goodType);
currentStock._currentQty -= amount;
stock._currentQty += amount;
// std::cout << "SimpleGoodStore, retrieve qty=" << amount << " resID=" << reservationID << std::endl;
}
VariantMap SimpleGoodStore::save() const
{
VariantMap stream = GoodStore::save();
stream[ "max" ] = _maxQty;
VariantList stockSave;
for( std::vector<GoodStock>::const_iterator itStock = _goodStockList.begin(); itStock != _goodStockList.end(); itStock++)
{
stockSave.push_back( (*itStock).save() );
}
stream[ "stock" ] = stockSave;
return stream;
}
void SimpleGoodStore::load( const VariantMap& stream )
{
_goodStockList.clear();
GoodStore::load( stream );
_maxQty = (int)stream.get( "max" );
VariantList stockSave = stream.get( "stock" ).toList();
for( VariantList::iterator it=stockSave.begin(); it!=stockSave.end(); it++ )
{
GoodStock restored;
restored.load( (*it).toList() );
_goodStockList.push_back( restored );
}
}