OpenTTD Source  1.11.0-beta2
cargomonitor_sl.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD 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, version 2.
4  * OpenTTD 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.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../cargomonitor.h"
12 
13 #include "saveload.h"
14 
15 #include "../safeguards.h"
16 
18 struct TempStorage {
19  CargoMonitorID number;
20  uint32 amount;
21 };
22 
25  SLE_VAR(TempStorage, number, SLE_UINT32),
26  SLE_VAR(TempStorage, amount, SLE_UINT32),
27  SLE_END()
28 };
29 
30 static CargoMonitorID FixupCargoMonitor(CargoMonitorID number)
31 {
32  /* Between SLV_EXTEND_CARGOTYPES and SLV_FIX_CARGO_MONITOR, the
33  * CargoMonitorID structure had insufficient packing for more
34  * than 32 cargo types. Here we have to shuffle bits to account
35  * for the change.
36  * Company moved from bits 24-31 to 25-28.
37  * Cargo type increased from bits 19-23 to 19-24.
38  */
39  SB(number, 25, 4, GB(number, 24, 4));
40  SB(number, 29, 3, 0);
41  ClrBit(number, 24);
42  return number;
43 }
44 
46 static void SaveDelivery()
47 {
48  TempStorage storage;
49 
50  int i = 0;
51  CargoMonitorMap::const_iterator iter = _cargo_deliveries.begin();
52  while (iter != _cargo_deliveries.end()) {
53  storage.number = iter->first;
54  storage.amount = iter->second;
55 
56  SlSetArrayIndex(i);
58 
59  i++;
60  iter++;
61  }
62 }
63 
65 static void LoadDelivery()
66 {
67  TempStorage storage;
69 
71  for (;;) {
72  if (SlIterateArray() < 0) break;
74 
75  if (fix) storage.number = FixupCargoMonitor(storage.number);
76 
77  std::pair<CargoMonitorID, uint32> p(storage.number, storage.amount);
78  _cargo_deliveries.insert(p);
79  }
80 }
81 
82 
84 static void SavePickup()
85 {
86  TempStorage storage;
87 
88  int i = 0;
89  CargoMonitorMap::const_iterator iter = _cargo_pickups.begin();
90  while (iter != _cargo_pickups.end()) {
91  storage.number = iter->first;
92  storage.amount = iter->second;
93 
94  SlSetArrayIndex(i);
96 
97  i++;
98  iter++;
99  }
100 }
101 
103 static void LoadPickup()
104 {
105  TempStorage storage;
107 
109  for (;;) {
110  if (SlIterateArray() < 0) break;
112 
113  if (fix) storage.number = FixupCargoMonitor(storage.number);
114 
115  std::pair<CargoMonitorID, uint32> p(storage.number, storage.amount);
116  _cargo_pickups.insert(p);
117  }
118 }
119 
122  { 'CMDL', SaveDelivery, LoadDelivery, nullptr, nullptr, CH_ARRAY},
123  { 'CMPU', SavePickup, LoadPickup, nullptr, nullptr, CH_ARRAY | CH_LAST},
124 };
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
ClearCargoDeliveryMonitoring
void ClearCargoDeliveryMonitoring(CompanyID company)
Clear all delivery cargo monitors.
Definition: cargomonitor.cpp:58
ClrBit
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:151
SLV_FIX_CARGO_MONITOR
@ SLV_FIX_CARGO_MONITOR
207 PR#7175 v1.9 Cargo monitor data packing fix to support 64 cargotypes.
Definition: saveload.h:292
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
saveload.h
_cargomonitor_pair_desc
static const SaveLoad _cargomonitor_pair_desc[]
Description of the TempStorage structure for the purpose of load and save.
Definition: cargomonitor_sl.cpp:24
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
CargoMonitorID
uint32 CargoMonitorID
Unique number for a company / cargo type / (town or industry).
Definition: cargomonitor.h:20
LoadDelivery
static void LoadDelivery()
Load the _cargo_deliveries monitoring map.
Definition: cargomonitor_sl.cpp:65
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
_cargo_pickups
CargoMonitorMap _cargo_pickups
Map of monitored pick-ups to the amount since last query/activation.
Definition: cargomonitor.cpp:16
SaveDelivery
static void SaveDelivery()
Save the _cargo_deliveries monitoring map.
Definition: cargomonitor_sl.cpp:46
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:815
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
LoadPickup
static void LoadPickup()
Load the _cargo_pickups monitoring map.
Definition: cargomonitor_sl.cpp:103
TempStorage
Temporary storage of cargo monitoring data for loading or saving it.
Definition: cargomonitor_sl.cpp:18
_cargo_deliveries
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
Definition: cargomonitor.cpp:17
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
ClearCargoPickupMonitoring
void ClearCargoPickupMonitoring(CompanyID company)
Clear all pick-up cargo monitors.
Definition: cargomonitor.cpp:48
_cargomonitor_chunk_handlers
const ChunkHandler _cargomonitor_chunk_handlers[]
Chunk definition of the cargomonitoring maps.
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631
SavePickup
static void SavePickup()
Save the _cargo_pickups monitoring map.
Definition: cargomonitor_sl.cpp:84