Skip to content

Commit

Permalink
Use EnumArray for TransportPriorities
Browse files Browse the repository at this point in the history
  • Loading branch information
Flamefire committed Sep 8, 2020
1 parent 12ef473 commit f5d86c8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions libs/s25main/GamePlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,8 +1195,8 @@ unsigned GamePlayer::GetBuidingSitePriority(const noBuildingSite* building_site)

void GamePlayer::ConvertTransportData(const TransportOrders& transport_data)
{
for(unsigned i = 0; i < NUM_WARE_TYPES; ++i)
transportPrio[i] = GetTransportPrioFromOrdering(transport_data, GoodType(i));
for(const auto ware : helpers::EnumRange<GoodType>{})
transportPrio[ware] = GetTransportPrioFromOrdering(transport_data, ware);
}

bool GamePlayer::IsAlly(const unsigned char playerId) const
Expand Down
9 changes: 5 additions & 4 deletions libs/s25main/gameData/SettingTypeConv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

#include "SettingTypeConv.h"
#include "RTTR_Assert.h"
#include "s25util/warningSuppression.h"
#include "helpers/EnumRange.h"
#include <stdexcept>

/// Max value for each setting.
/// Note: We skip the first 2 steppings for the occupation (last 4 values) as they had no effect in S2
const MilitarySettings SUPPRESS_UNUSED MILITARY_SETTINGS_SCALE = {{10, 5, 5, 5, 8, 8, 8, 8}};
const std::array<unsigned char, NUM_WARE_TYPES> STD_TRANSPORT_PRIO = {
const TransportPriorities STD_TRANSPORT_PRIO = {
{2, 12, 12, 12, 12, 12, 12, 12, 12, 12, 10, 10, 12, 12, 12, 13, 1, 3, 11, 11, 11, 1, 9, 7, 8, 1, 1, 11, 0, 4, 5, 6, 11, 11, 1}};

unsigned GetTransportPrioFromOrdering(const TransportOrders& ordering, GoodType good)
Expand All @@ -36,14 +37,14 @@ unsigned GetTransportPrioFromOrdering(const TransportOrders& ordering, GoodType
return prio;
}
RTTR_Assert(false);
return ordering.size();
throw std::logic_error("Invalid ordering or ware");
}

TransportOrders GetOrderingFromTransportPrio(const TransportPriorities& priorities)
{
TransportOrders result;
// Map prio of each ware to STD prio
for(unsigned ware = 0; ware < NUM_WARE_TYPES; ware++)
for(const auto ware : helpers::EnumRange<GoodType>{})
{
RTTR_Assert(priorities[ware] < result.size());
result[priorities[ware]] = STD_TRANSPORT_PRIO[ware];
Expand Down
3 changes: 2 additions & 1 deletion libs/s25main/gameData/SettingTypeConv.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
#ifndef SettingTypeConv_h__
#define SettingTypeConv_h__

#include "helpers/EnumArray.h"
#include "gameTypes/GoodTypes.h"
#include "gameTypes/SettingsTypes.h"

/// Scaling (max values) of each military setting
extern const MilitarySettings MILITARY_SETTINGS_SCALE;
/// Standard priority of each ware
extern const std::array<unsigned char, NUM_WARE_TYPES> STD_TRANSPORT_PRIO;
extern const TransportPriorities STD_TRANSPORT_PRIO;

/// Get the priority of a given good from the ordering of goods (good categories)
unsigned GetTransportPrioFromOrdering(const TransportOrders& ordering, GoodType good);
Expand Down
6 changes: 3 additions & 3 deletions libs/s25main/gameTypes/SettingsTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#ifndef SettingsTypes_h__
#define SettingsTypes_h__

#include "helpers/EnumArray.h"
#include "gameTypes/BuildingType.h"
#include "gameTypes/GoodTypes.h"
#include "s25util/warningSuppression.h"
#include <array>
#include <tuple>

Expand All @@ -30,7 +30,7 @@
using DistributionMapping = std::tuple<GoodType, BuildingType, uint8_t>;
/// List of all possible distribution mappings ordered by GoodType
using DistributionMap = std::array<DistributionMapping, 23>;
extern const DistributionMap SUPPRESS_UNUSED distributionMap;
extern const DistributionMap distributionMap;
/// List of the percentage a building should get from a specific ware
using Distributions = std::array<uint8_t, std::tuple_size<DistributionMap>::value>;
/// Ordering of building types by priority. All buildings in here except unused and HQ
Expand All @@ -39,7 +39,7 @@ using BuildOrders = std::array<BuildingType, NUM_BUILDING_TYPES - NUM_UNUSED_BLD
/// E.g. std prio of coins = 0 -> TransportOrders[0] = stdPrio[COINS] = 0
/// New prio of coins = 1 -> TransportOrders[1] = stdPrio[COINS] = 0
using TransportOrders = std::array<uint8_t, 14>;
using TransportPriorities = std::array<uint8_t, NUM_WARE_TYPES>;
using TransportPriorities = helpers::EnumArray<uint8_t, GoodType>;
/// Priority of each tool
using ToolSettings = std::array<uint8_t, NUM_TOOLS>;
/// Value of each military slider
Expand Down

0 comments on commit f5d86c8

Please sign in to comment.