Merge pull request #281 from VacuumBreather/train_speed_adaptation
# Conflicts: # src/saveload/extended_ver_sl.cpp # src/saveload/extended_ver_sl.hpull/295/head
commit
158608442c
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* 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.
|
||||
* 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.
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/** @file train_speed_adaptation.cpp Code handling saving and loading of data for train speed adaptation */
|
||||
|
||||
#include "../stdafx.h"
|
||||
#include "../train_speed_adaptation.h"
|
||||
#include "saveload.h"
|
||||
|
||||
using SignalSpeedType = std::pair<const SignalSpeedKey, SignalSpeedValue>;
|
||||
|
||||
static const SaveLoad _train_speed_adaptation_map_desc[] = {
|
||||
SLE_VAR(SignalSpeedType, first.signal_track, SLE_UINT8),
|
||||
SLE_VAR(SignalSpeedType, first.last_passing_train_dir, SLE_UINT8),
|
||||
SLE_VAR(SignalSpeedType, second.train_speed, SLE_UINT16),
|
||||
SLE_VAR(SignalSpeedType, second.time_stamp, SLE_UINT64),
|
||||
SLE_END()
|
||||
};
|
||||
|
||||
static void Load_TSAS()
|
||||
{
|
||||
int index;
|
||||
SignalSpeedType data;
|
||||
while ((index = SlIterateArray()) != -1) {
|
||||
const_cast<SignalSpeedKey &>(data.first).signal_tile = index;
|
||||
SlObject(&data, _train_speed_adaptation_map_desc);
|
||||
_signal_speeds.insert(data);
|
||||
}
|
||||
}
|
||||
|
||||
static void RealSave_TSAS(SignalSpeedType *data)
|
||||
{
|
||||
SlObject(data, _train_speed_adaptation_map_desc);
|
||||
}
|
||||
|
||||
static void Save_TSAS()
|
||||
{
|
||||
for (auto &it : _signal_speeds) {
|
||||
SlSetArrayIndex(it.first.signal_tile);
|
||||
SignalSpeedType *data = ⁢
|
||||
SlAutolength((AutolengthProc*) RealSave_TSAS, data);
|
||||
}
|
||||
}
|
||||
|
||||
extern const ChunkHandler _train_speed_adaptation_chunk_handlers[] = {
|
||||
{ 'TSAS', Save_TSAS, Load_TSAS, nullptr, nullptr, CH_SPARSE_ARRAY | CH_LAST},
|
||||
};
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* 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.
|
||||
* 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.
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/** @file train_speed_adaptation.h Train speed adaptation data structures. */
|
||||
|
||||
#ifndef TRAIN_SPEED_ADAPTATION_H
|
||||
#define TRAIN_SPEED_ADAPTATION_H
|
||||
|
||||
#include "date_type.h"
|
||||
#include "track_type.h"
|
||||
#include "tile_type.h"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
struct SignalSpeedKey
|
||||
{
|
||||
TileIndex signal_tile;
|
||||
Track signal_track;
|
||||
Trackdir last_passing_train_dir;
|
||||
|
||||
bool operator==(const SignalSpeedKey& other) const
|
||||
{
|
||||
return signal_tile == other.signal_tile &&
|
||||
signal_track == other.signal_track &&
|
||||
last_passing_train_dir == other.last_passing_train_dir;
|
||||
}
|
||||
};
|
||||
|
||||
struct SignalSpeedValue
|
||||
{
|
||||
uint16 train_speed;
|
||||
DateTicksScaled time_stamp;
|
||||
};
|
||||
|
||||
struct SignalSpeedKeyHashFunc
|
||||
{
|
||||
std::size_t operator() (const SignalSpeedKey &key) const
|
||||
{
|
||||
const std::size_t h1 = std::hash<TileIndex>()(key.signal_tile);
|
||||
const std::size_t h2 = std::hash<Trackdir>()(key.last_passing_train_dir);
|
||||
const std::size_t h3 = std::hash<Track>()(key.signal_track);
|
||||
|
||||
return (h1 ^ h2) ^ h3;
|
||||
}
|
||||
};
|
||||
|
||||
extern std::unordered_map<SignalSpeedKey, SignalSpeedValue, SignalSpeedKeyHashFunc> _signal_speeds;
|
||||
|
||||
#endif /* TRAIN_SPEED_ADAPTATION_H */
|
Loading…
Reference in New Issue