OpenTTD
cargomonitor_sl.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "../cargomonitor.h"
14 
15 #include "saveload.h"
16 
17 #include "../safeguards.h"
18 
20 struct TempStorage {
21  CargoMonitorID number;
22  uint32 amount;
23 };
24 
27  SLE_VAR(TempStorage, number, SLE_UINT32),
28  SLE_VAR(TempStorage, amount, SLE_UINT32),
29  SLE_END()
30 };
31 
32 static CargoMonitorID FixupCargoMonitor(CargoMonitorID number)
33 {
34  /* Between SLV_EXTEND_CARGOTYPES and SLV_FIX_CARGO_MONITOR, the
35  * CargoMonitorID structure had insufficient packing for more
36  * than 32 cargo types. Here we have to shuffle bits to account
37  * for the change.
38  * Company moved from bits 24-31 to 25-28.
39  * Cargo type increased from bits 19-23 to 19-24.
40  */
41  SB(number, 25, 4, GB(number, 24, 4));
42  SB(number, 29, 3, 0);
43  ClrBit(number, 24);
44  return number;
45 }
46 
48 static void SaveDelivery()
49 {
50  TempStorage storage;
51 
52  int i = 0;
53  CargoMonitorMap::const_iterator iter = _cargo_deliveries.begin();
54  while (iter != _cargo_deliveries.end()) {
55  storage.number = iter->first;
56  storage.amount = iter->second;
57 
58  SlSetArrayIndex(i);
59  SlObject(&storage, _cargomonitor_pair_desc);
60 
61  i++;
62  iter++;
63  }
64 }
65 
67 static void LoadDelivery()
68 {
69  TempStorage storage;
71 
73  for (;;) {
74  if (SlIterateArray() < 0) break;
75  SlObject(&storage, _cargomonitor_pair_desc);
76 
77  if (fix) storage.number = FixupCargoMonitor(storage.number);
78 
79  std::pair<CargoMonitorID, uint32> p(storage.number, storage.amount);
80  _cargo_deliveries.insert(p);
81  }
82 }
83 
84 
86 static void SavePickup()
87 {
88  TempStorage storage;
89 
90  int i = 0;
91  CargoMonitorMap::const_iterator iter = _cargo_pickups.begin();
92  while (iter != _cargo_pickups.end()) {
93  storage.number = iter->first;
94  storage.amount = iter->second;
95 
96  SlSetArrayIndex(i);
97  SlObject(&storage, _cargomonitor_pair_desc);
98 
99  i++;
100  iter++;
101  }
102 }
103 
105 static void LoadPickup()
106 {
107  TempStorage storage;
109 
111  for (;;) {
112  if (SlIterateArray() < 0) break;
113  SlObject(&storage, _cargomonitor_pair_desc);
114 
115  if (fix) storage.number = FixupCargoMonitor(storage.number);
116 
117  std::pair<CargoMonitorID, uint32> p(storage.number, storage.amount);
118  _cargo_pickups.insert(p);
119  }
120 }
121 
124  { 'CMDL', SaveDelivery, LoadDelivery, NULL, NULL, CH_ARRAY},
125  { 'CMPU', SavePickup, LoadPickup, NULL, NULL, CH_ARRAY | CH_LAST},
126 };
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:753
Temporary storage of cargo monitoring data for loading or saving it.
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.
207 PR#7175 Cargo monitor data packing fix to support 64 cargotypes.
Definition: saveload.h:293
Functions/types related to saving and loading games.
static const SaveLoad _cargomonitor_pair_desc[]
Description of the TempStorage structure for the purpose of load and save.
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
static void SavePickup()
Save the _cargo_pickups monitoring map.
void ClearCargoPickupMonitoring(CompanyID company)
Clear all pick-up cargo monitors.
static void SaveDelivery()
Save the _cargo_deliveries monitoring map.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:634
void ClearCargoDeliveryMonitoring(CompanyID company)
Clear all delivery cargo monitors.
Handlers and description of chunk.
Definition: saveload.h:346
uint32 CargoMonitorID
Unique number for a company / cargo type / (town or industry).
Definition: cargomonitor.h:22
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:641
CargoMonitorMap _cargo_pickups
Map of monitored pick-ups to the amount since last query/activation.
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
const ChunkHandler _cargomonitor_chunk_handlers[]
Chunk definition of the cargomonitoring maps.
static void LoadDelivery()
Load the _cargo_deliveries monitoring map.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1543
SaveLoad type struct.
Definition: saveload.h:486
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:584
static void LoadPickup()
Load the _cargo_pickups monitoring map.
Last chunk in this array.
Definition: saveload.h:381