1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-07-08 05:48:07 +02:00

Start using structure bindings

This commit is contained in:
Filip Gawin 2018-10-29 22:57:18 +01:00
parent 45ea1df74c
commit 80666bc38d
11 changed files with 101 additions and 114 deletions

View File

@ -12,8 +12,8 @@ using OGameStringStream = std::basic_ostringstream<GameStringChar>;
FontMap::FontMap(std::initializer_list<std::reference_wrapper<const gschar_unicode_map_t>> maps) {
for (const auto &map : maps) {
m_to_unicode.insert(map.get().cbegin(), map.get().cend());
for (const auto &m : map.get()) {
m_from_unicode[m.second] = m.first;
for (const auto &[gameStringChar, unicode] : map.get()) {
m_from_unicode[unicode] = gameStringChar;
}
}
const auto &q = m_from_unicode.find(UnicodeValue::UNICODE_QUESTION_MARK);

View File

@ -110,10 +110,10 @@ bool SoundManager::loadSound(const std::string& name,
if (sound_iter != sounds.end()) {
sound = &sound_iter->second;
} else {
auto emplaced = sounds.emplace(std::piecewise_construct,
auto [it, emplaced] = sounds.emplace(std::piecewise_construct,
std::forward_as_tuple(name),
std::forward_as_tuple());
sound = &emplaced.first->second;
sound = &it->second;
sound->source = std::make_shared<SoundSource>();
sound->buffer = std::make_unique<SoundBuffer>();
@ -128,10 +128,10 @@ bool SoundManager::loadSound(const std::string& name,
void SoundManager::loadSound(size_t index) {
Sound* sound = nullptr;
auto emplaced =
auto [it, emplaced] =
sfx.emplace(std::piecewise_construct, std::forward_as_tuple(index),
std::forward_as_tuple());
sound = &emplaced.first->second;
sound = &it->second;
sound->source = std::make_shared<SoundSource>();
sound->source->loadSfx(sdt, index);
@ -149,22 +149,22 @@ size_t SoundManager::createSfxInstance(size_t index) {
// Try to reuse first available buffer
// (aka with stopped state)
for (auto& sound : buffers) {
if (sound.second.buffer && sound.second.isStopped()) {
for (auto& [id, sound] : buffers) {
if (sound.buffer && sound.isStopped()) {
// Let's use this buffer
sound.second.buffer = std::make_unique<SoundBuffer>();
sound.second.source = soundRef->second.source;
sound.second.isLoaded =
sound.second.buffer->bufferData(*sound.second.source);
return sound.first;
sound.buffer = std::make_unique<SoundBuffer>();
sound.source = soundRef->second.source;
sound.isLoaded =
sound.buffer->bufferData(*sound.source);
return id;
}
}
// There's no available free buffer, so
// we should create a new one.
auto emplaced = buffers.emplace(std::piecewise_construct,
auto [it, emplaced] = buffers.emplace(std::piecewise_construct,
std::forward_as_tuple(bufferNr),
std::forward_as_tuple());
sound = &emplaced.first->second;
sound = &it->second;
sound->id = bufferNr;
sound->buffer = std::make_unique<SoundBuffer>();

View File

@ -24,21 +24,16 @@ void Animator::tick(float dt) {
glm::quat rotation{1.0f,0.0f,0.0f,0.0f};
};
#if 0
// Blend all active animations together
std::map<ModelFrame*, BoneTransform> blendFrames;
#endif
for (AnimationState& state : animations) {
if (state.animation == nullptr) continue;
if (state.boneInstances.empty()) {
for (const auto& bone : state.animation->bones) {
auto frame = model->findFrame(bone.first);
for (const auto& [name, bonePtr] : state.animation->bones) {
auto frame = model->findFrame(name);
if (!frame) {
continue;
}
state.boneInstances.emplace(bone.second.get(), frame);
state.boneInstances.emplace(bonePtr.get(), frame);
}
}
@ -51,42 +46,20 @@ void Animator::tick(float dt) {
animTime = std::fmod(animTime, state.animation->duration);
}
for (auto& b : state.boneInstances) {
if (b.first->frames.empty()) continue;
auto kf = b.first->getInterpolatedKeyframe(animTime);
for (auto& [bonePtr, frame] : state.boneInstances) {
if (bonePtr->frames.empty()) continue;
auto kf = bonePtr->getInterpolatedKeyframe(animTime);
BoneTransform xform;
xform.rotation = kf.rotation;
if (b.first->type != AnimationBone::R00) {
if (bonePtr->type != AnimationBone::R00) {
xform.translation = kf.position;
}
#if 0
auto prevAnim = blendFrames.find(b.second.frameIndex);
if (prevAnim != blendFrames.end())
{
prevAnim->second.translation += xform.translation;
prevAnim->second.rotation *= xform.rotation;
}
else
{
blendFrames[b.second.frameIndex] = xform;
}
#else
b.second->setTranslation(b.second->getDefaultTranslation() +
frame->setTranslation(frame->getDefaultTranslation() +
xform.translation);
b.second->setRotation(glm::mat3_cast(xform.rotation));
#endif
frame->setRotation(glm::mat3_cast(xform.rotation));
}
}
#if 0
for (auto& p : blendFrames) {
p.first->setTranslation(p.first->getDefaultTranslation() +
p.second.translation);
p.first->setRotation(glm::mat3_cast(p.second.rotation));
}
#endif
}
bool Animator::isCompleted(unsigned int slot) const {

View File

@ -2,9 +2,9 @@
int GameState::addRadarBlip(BlipData& blip) {
int l = 0;
for (const auto& radarBlip : radarBlips) {
if ((radarBlip.first) != l) {
l = radarBlip.first - 1;
for (const auto& [nr, blipData] : radarBlips) {
if ((nr) != l) {
l = nr - 1;
} else {
l++;
}

View File

@ -439,8 +439,8 @@ void GameWorld::ObjectPool::insert(std::unique_ptr<GameObject> object) {
if (object->getGameObjectID() == 0) {
// Find the lowest free GameObjectID.
GameObjectID availID = 1;
for (auto& p : objects) {
if (p.first == availID) availID++;
for (auto& [id, objectPtr] : objects) {
if (id == availID) availID++;
}
object->setGameObjectID(availID);

View File

@ -458,20 +458,20 @@ void VehicleObject::tickPhysics(float dt) {
}
// Update passenger positions
for (auto& seat : seatOccupants) {
auto character = static_cast<CharacterObject*>(seat.second);
for (auto& [seatId, objectPtr] : seatOccupants) {
auto character = static_cast<CharacterObject*>(objectPtr);
glm::vec3 passPosition{};
if (character->isEnteringOrExitingVehicle()) {
passPosition = getSeatEntryPositionWorld(seat.first);
passPosition = getSeatEntryPositionWorld(seatId);
} else {
passPosition = getPosition();
if (seat.first < info->seats.size()) {
if (seatId < info->seats.size()) {
passPosition +=
getRotation() * (info->seats[seat.first].offset);
getRotation() * (info->seats[seatId].offset);
}
}
seat.second->updateTransform(passPosition, getRotation());
objectPtr->updateTransform(passPosition, getRotation());
}
if (getVehicle()->vehicletype_ == VehicleModelInfo::BOAT) {

View File

@ -7719,8 +7719,8 @@ void opcode_02dd(const ScriptArguments& args, const ScriptString areaName, Scrip
if (zone) {
// Create a list of candidate characters by iterating and checking if the char is in this zone
std::vector<std::pair<GameObjectID, GameObject*>> candidates;
for (auto& p : args.getWorld()->pedestrianPool.objects) {
auto character = static_cast<CharacterObject*>(p.second.get());
for (auto& [id, pedestrianPtr] : args.getWorld()->pedestrianPool.objects) {
auto character = static_cast<CharacterObject*>(pedestrianPtr.get());
// We only consider characters walking around normally
// @todo not sure if we are able to grab script objects or players too
@ -7735,7 +7735,7 @@ void opcode_02dd(const ScriptArguments& args, const ScriptString areaName, Scrip
auto& max = zone->max;
if (cp.x > min.x && cp.y > min.y && cp.z > min.z &&
cp.x < max.x && cp.y < max.y && cp.z < max.z) {
candidates.emplace_back(p.first, p.second.get());
candidates.emplace_back(id, pedestrianPtr.get());
}
}
@ -7746,13 +7746,13 @@ void opcode_02dd(const ScriptArguments& args, const ScriptString areaName, Scrip
// @todo verify if the lifetime is actually changed in the original game
// husho: lifetime is changed to mission object lifetime
unsigned int randomIndex = std::rand() % candidateCount;
const auto p = candidates[randomIndex];
auto character = static_cast<CharacterObject*>(p.second);
const auto [candidateId, candidatePtr] = candidates.at(randomIndex);
auto character = static_cast<CharacterObject*>(candidatePtr);
character->setLifetime(GameObject::MissionLifetime);
if (args.getThread()->isMission) {
script::addObjectToMissionCleanup(args, character);
}
*args[1].globalInteger = p.first;
*args[1].globalInteger = candidateId;
return;
}
}

View File

@ -455,8 +455,8 @@ std::string GameConfig::ParseResult::what() const {
}
if (!this->m_unknownData.empty()) {
oss << "\nUnknown configuration keys:";
for (const auto &keyvalue : m_unknownData) {
oss << "\n - " << keyvalue.first;
for (const auto &[key, value] : m_unknownData) {
oss << "\n - " << key;
}
}
return oss.str();

View File

@ -71,8 +71,8 @@ void GameInput::updateGameInputState(GameInputState *state,
auto level = event.type == SDL_KEYDOWN ? 1.f : 0.f;
auto& levels = state->levels;
auto range = kDefaultControls.equal_range(sym);
for (auto it = range.first; it != range.second; ++it) {
auto [rangeBegin, rangeEnd] = kDefaultControls.equal_range(sym);
for (auto it = rangeBegin; it != rangeEnd; ++it) {
levels[it->second] = level;
}
} break;

View File

@ -25,16 +25,13 @@
#include <iomanip>
#include <iostream>
const std::map<GameRenderer::SpecialModel, std::pair<std::string, std::string>>
kSpecialModels = {
{GameRenderer::ZoneCylinderA,
std::pair<std::string, std::string>("zonecyla.dff", "particle")},
{GameRenderer::ZoneCylinderB,
std::pair<std::string, std::string>("zonecylb.dff", "particle")},
{GameRenderer::Arrow,
std::pair<std::string, std::string>("arrow.dff", "")}};
namespace {
static constexpr std::array<
std::tuple<GameRenderer::SpecialModel, char const*, char const*>, 3>
kSpecialModels{{{GameRenderer::ZoneCylinderA, "zonecyla.dff", "particle"},
{GameRenderer::ZoneCylinderB, "zonecylb.dff", "particle"},
{GameRenderer::Arrow, "arrow.dff", ""}}};
constexpr float kMaxPhysicsSubSteps = 2;
} // namespace
@ -64,9 +61,9 @@ RWGame::RWGame(Logger& log, int argc, char* argv[])
data.load();
for (const auto& p : kSpecialModels) {
auto model = data.loadClump(p.second.first, p.second.second);
renderer.setSpecialModel(p.first, model);
for (const auto& [specialModel, fileName, name] : kSpecialModels) {
auto model = data.loadClump(fileName, name);
renderer.setSpecialModel(specialModel, model);
}
// Set up text renderer
@ -213,7 +210,7 @@ void RWGame::handleCheatInput(char symbol) {
});
checkForCheat("GUNSGUNSGUNS", [&] {
constexpr std::array<int, 11> ammo = {{
static constexpr std::array<int, 11> ammo = {{
1, //baseball bat
100,//pistol
100,//uzi
@ -226,7 +223,7 @@ void RWGame::handleCheatInput(char symbol) {
200,//m16
5 //sniper rifle
}};
for (std::array<int, 11>::size_type i = 0; i < ammo.size(); i++)
for (auto i = 0u; i < ammo.size(); i++)
player->addToInventory(static_cast<int>(i+1),ammo[i]);
state.showHelpMessage("CHEAT2"); // III / VC: Inputting weapon cheats.
});

View File

@ -108,7 +108,7 @@ std::shared_ptr<Menu> DebugState::createMapMenu() {
}},
{"Unsolid garage doors",
[=] {
std::vector<std::string> garageDoorModels{
static constexpr std::array<char const*, 33> garageDoorModels{{
"8ballsuburbandoor", "amcogaragedoor",
"bankjobdoor", "bombdoor",
"crushercrush", "crushertop",
@ -125,11 +125,11 @@ std::shared_ptr<Menu> DebugState::createMapMenu() {
"SalvGarage", "shedgaragedoor",
"Sub_sprayshopdoor", "towergaragedoor1",
"towergaragedoor2", "towergaragedoor3",
"vheistlocdoor"};
"vheistlocdoor"}};
auto gw = game->getWorld();
for (auto& i : gw->instancePool.objects) {
auto obj = static_cast<InstanceObject*>(i.second.get());
for (auto& [id, instancePtr] : gw->instancePool.objects) {
auto obj = static_cast<InstanceObject*>(instancePtr.get());
if (std::find(garageDoorModels.begin(),
garageDoorModels.end(),
obj->getModelInfo<BaseModelInfo>()->name) !=
@ -148,18 +148,29 @@ std::shared_ptr<Menu> DebugState::createVehicleMenu() {
auto menu = Menu::create({{"Back", [=] { enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight);
const std::map<std::string, int> kVehicleTypes = {
{"Landstalker", 90}, {"Taxi", 110}, {"Firetruck", 97},
{"Police", 116}, {"Ambulance", 106}, {"Bobcat", 112},
{"Banshee", 119}, {"Rhino", 122}, {"Barracks", 123},
{"Rumpo", 130}, {"Columbian", 138}, {"Dodo", 126},
{"Speeder", 142}, {"Yakuza", 136}, {"Cheetah", 105},
{"Ambulance", 106}, {"FBI", 107}, {"Mafia", 134},
{"Infernus", 101},
};
static constexpr std::array<std::tuple<char const*, unsigned int>, 19>
kVehicleTypes{{{"Landstalker", 90},
{"Taxi", 110},
{"Firetruck", 97},
{"Police", 116},
{"Ambulance", 106},
{"Bobcat", 112},
{"Banshee", 119},
{"Rhino", 122},
{"Barracks", 123},
{"Rumpo", 130},
{"Columbian", 138},
{"Dodo", 126},
{"Speeder", 142},
{"Yakuza", 136},
{"Cheetah", 105},
{"Ambulance", 106},
{"FBI", 107},
{"Mafia", 134},
{"Infernus", 101}}};
for (auto& e : kVehicleTypes) {
menu->lambda(e.first, [=] { spawnVehicle(e.second); });
for (const auto& [name, id] : kVehicleTypes) {
menu->lambda(name, [this, id = id] { spawnVehicle(id); });
}
menu->offset = kDebugMenuOffset;
@ -171,23 +182,29 @@ std::shared_ptr<Menu> DebugState::createAIMenu() {
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight);
const std::map<std::string, int> kPedTypes = {
{"Triad", 12}, {"Cop", 1}, {"SWAT", 2},
{"FBI", 3}, {"Fireman", 6}, {"Construction", 74},
};
static constexpr std::array<std::tuple<char const*, unsigned int>, 6>
kPedTypes{{
{"Triad", 12},
{"Cop", 1},
{"SWAT", 2},
{"FBI", 3},
{"Fireman", 6},
{"Construction", 74},
}};
for (auto& e : kPedTypes) {
menu->lambda(e.first + " Follower", [=] { spawnFollower(e.second); });
for (const auto& [name, id] : kPedTypes) {
menu->lambda(name, [this, id = id] { spawnFollower(id); });
}
menu->lambda("Kill All Peds", [=] {
for (auto& p : game->getWorld()->pedestrianPool.objects) {
if (p.second->getLifetime() == GameObject::PlayerLifetime) {
for (auto& [id, pedestrianPtr] :
game->getWorld()->pedestrianPool.objects) {
if (pedestrianPtr->getLifetime() == GameObject::PlayerLifetime) {
continue;
}
p.second->takeDamage({p.second->getPosition(),
p.second->getPosition(), 100.f,
GameObject::DamageInfo::Explosion, 0.f});
pedestrianPtr->takeDamage({pedestrianPtr->getPosition(),
pedestrianPtr->getPosition(), 100.f,
GameObject::DamageInfo::Explosion, 0.f});
}
});
@ -214,7 +231,7 @@ std::shared_ptr<Menu> DebugState::createWeatherMenu() {
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeight);
const std::array<std::string, 4> w{{"Sunny", "Cloudy", "Rainy", "Foggy"}};
static constexpr std::array<char const*, 4> w{{"Sunny", "Cloudy", "Rainy", "Foggy"}};
for (std::size_t i = 0; i < w.size(); ++i) {
menu->lambda(w[i],
@ -230,7 +247,7 @@ std::shared_ptr<Menu> DebugState::createMissionsMenu() {
Menu::create({{"Back", [=] { this->enterMenu(createDebugMenu()); }}},
kDebugFont, kDebugEntryHeightMissions);
const std::array<std::string, 80> w{{
static constexpr std::array<char const*, 80> w{{
"Intro Movie",
"Hospital Info Scene",
"Police Station Info Scene",