From bcb02eab1f97d2069e637f632d9e1c7475a90eb0 Mon Sep 17 00:00:00 2001 From: Lorenz <lorenz-martin.diel@informatik.hs-fulda.de> Date: Thu, 30 Jan 2025 18:31:10 +0100 Subject: [PATCH] Removed UnitID Unkown --- .clang-format | 187 ++++++++++++++++++++++++++++++++++++++ src/config.cpp | 242 ++++++++++++++++++++++++++----------------------- src/unit.hpp | 2 - 3 files changed, 316 insertions(+), 115 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..2910389 --- /dev/null +++ b/.clang-format @@ -0,0 +1,187 @@ +--- +BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: AlwaysBreak +AlignArrayOfStructures: Right +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: Consecutive +AlignConsecutiveMacros: None +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: Yes +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAfterJavaFieldAnnotations: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Allman +BreakBeforeConceptDeclarations: true +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 100 +CommentPragmas: "^ IWYU pragma:" +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: ^"(llvm|llvm-c|clang|clang-c)/ + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: ^(<|"(gtest|gmock|isl|json)/) + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: .* + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: (Test)?$ +IncludeIsMainSourceRegex: "" +IndentAccessModifiers: true +IndentCaseBlocks: true +IndentCaseLabels: false +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequires: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +Language: Cpp +MacroBlockBegin: "" +MacroBlockEnd: "" +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PPIndentWidth: -1 +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDeclarationName: false + AfterFunctionDefinitionName: false + AfterIfMacros: true + AfterOverloadedOperator: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInCStyleCastParentheses: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseCRLF: false +UseTab: Never +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE + - NS_SWIFT_NAME + - CF_SWIFT_NAME diff --git a/src/config.cpp b/src/config.cpp index 859b0d4..2279184 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -23,56 +23,64 @@ namespace advanced_wars const auto &unitData = unit.second; std::string unit_key = unitData.get<std::string>("<xmlattr>.key"); - UnitId unitId = map_unit_key_to_id(unit_key); - if (unitId == UnitId::UNKNOWN) + try { - continue; - } + UnitId unitId = map_unit_key_to_id(unit_key); - unit_costs[unitId] = unitData.get<int>("Cost"); - unit_movement_points[unitId] = unitData.get<int>("MovementPoints"); - unit_ammo[unitId] = unitData.get<int>("Ammo"); - unit_min_range[unitId] = unitData.get<int>("minRange", 0); - unit_max_range[unitId] = unitData.get<int>("maxRange", 0); - unit_movement_type[unitId] = map_movement_type(unitData.get<std::string>("MovementType")); + unit_costs[unitId] = unitData.get<int>("Cost"); + unit_movement_points[unitId] = unitData.get<int>("MovementPoints"); + unit_ammo[unitId] = unitData.get<int>("Ammo"); + unit_min_range[unitId] = unitData.get<int>("minRange", 0); + unit_max_range[unitId] = unitData.get<int>("maxRange", 0); - for (const auto &weapon : unitData.get_child("Weapons")) - { - if (weapon.first == "PrimaryWeapon") + std::string movement_type_str = unitData.get<std::string>("MovementType"); + try + { + unit_movement_type[unitId] = map_movement_type(movement_type_str); + } + catch (const std::out_of_range &e) { - unit_primary_weapon[unitId] = weapon.second.get<std::string>("<xmlattr>.name"); + std::cerr << "Unknown movement type: " << movement_type_str << " for unit key: " << unit_key << std::endl; + continue; + } - for (const auto &damage : weapon.second.get_child("DamageTable")) + for (const auto &weapon : unitData.get_child("Weapons")) + { + if (weapon.first == "PrimaryWeapon") { - if (damage.first != "Damage") - continue; + unit_primary_weapon[unitId] = weapon.second.get<std::string>("<xmlattr>.name"); - std::string target_key = damage.second.get<std::string>("<xmlattr>.unitId"); - UnitId targetId = map_unit_key_to_id(target_key); - if (targetId == UnitId::UNKNOWN) - continue; + for (const auto &damage : weapon.second.get_child("DamageTable")) + { + if (damage.first != "Damage") + continue; - primary_weapon_damage[unitId][targetId] = damage.second.get<int>("<xmlattr>.value"); + std::string target_key = damage.second.get<std::string>("<xmlattr>.unitId"); + UnitId targetId = map_unit_key_to_id(target_key); + primary_weapon_damage[unitId][targetId] = damage.second.get<int>("<xmlattr>.value"); + } } - } - else if (weapon.first == "SecondaryWeapon") - { - unit_secondary_weapon[unitId] = weapon.second.get<std::string>("<xmlattr>.name"); - - for (const auto &damage : weapon.second.get_child("DamageTable")) + else if (weapon.first == "SecondaryWeapon") { - if (damage.first != "Damage") - continue; + unit_secondary_weapon[unitId] = weapon.second.get<std::string>("<xmlattr>.name"); - std::string target_key = damage.second.get<std::string>("<xmlattr>.unitId"); - UnitId targetId = map_unit_key_to_id(target_key); - if (targetId == UnitId::UNKNOWN) - continue; + for (const auto &damage : weapon.second.get_child("DamageTable")) + { + if (damage.first != "Damage") + continue; - secondary_weapon_damage[unitId][targetId] = damage.second.get<int>("<xmlattr>.value"); + std::string target_key = damage.second.get<std::string>("<xmlattr>.unitId"); + UnitId targetId = map_unit_key_to_id(target_key); + secondary_weapon_damage[unitId][targetId] = damage.second.get<int>("<xmlattr>.value"); + } } } } + catch (const std::out_of_range &e) + { + //std::cerr << "Unknown unit key: " << unit_key << std::endl; + continue; + } } } @@ -100,7 +108,11 @@ namespace advanced_wars {"bomber", UnitId::BOMBER}}; auto it = unit_map.find(unit_key); - return it != unit_map.end() ? it->second : UnitId::UNKNOWN; + if (it != unit_map.end()) + { + return it->second; + } + throw std::out_of_range("Unknown unit key: " + unit_key); } MovementType Config::map_movement_type(const std::string &movementTypeStr) const @@ -111,118 +123,122 @@ namespace advanced_wars {"Tread", MovementType::TREAD}, {"Air", MovementType::AIR}, {"Sea", MovementType::SEA}, - {"Lander", MovementType::LANDER} + {"Lander", MovementType::LANDER}}; - }; auto it = movement_map.find(movementTypeStr); - return it != movement_map.end() ? it->second : MovementType::UNKNOWN; - } - - int Config::get_unit_cost(UnitId id) const - { - auto it = unit_costs.find(id); - if (it != unit_costs.end()) + if (it != movement_map.end()) { return it->second; } - throw std::runtime_error("Cost for unit ID not found"); + throw std::out_of_range("Unknown movement type: " + movementTypeStr); } - int Config::get_unit_movement_points(UnitId id) const + +int Config::get_unit_cost(UnitId id) const +{ + auto it = unit_costs.find(id); + if (it != unit_costs.end()) { - auto it = unit_movement_points.find(id); - if (it != unit_movement_points.end()) - { - return it->second; - } - throw std::runtime_error("Movement points for unit ID not found"); + return it->second; } + throw std::runtime_error("Cost for unit ID not found"); +} - MovementType Config::get_unit_movement_type(UnitId id) const +int Config::get_unit_movement_points(UnitId id) const +{ + auto it = unit_movement_points.find(id); + if (it != unit_movement_points.end()) { - auto it = unit_movement_type.find(id); - if (it != unit_movement_type.end()) - { - return it->second; - } - throw std::runtime_error("Movement type for unit ID not found"); + return it->second; + } + throw std::runtime_error("Movement points for unit ID not found"); +} + +MovementType Config::get_unit_movement_type(UnitId id) const +{ + auto it = unit_movement_type.find(id); + if (it != unit_movement_type.end()) + { + return it->second; } + throw std::runtime_error("Movement type for unit ID not found"); +} - int Config::get_unit_ammo(UnitId id) const +int Config::get_unit_ammo(UnitId id) const +{ + auto it = unit_ammo.find(id); + if (it != unit_ammo.end()) { - auto it = unit_ammo.find(id); - if (it != unit_ammo.end()) - { - return it->second; - } - throw std::runtime_error("Ammo for unit ID not found"); + return it->second; } + throw std::runtime_error("Ammo for unit ID not found"); +} - int Config::get_unit_min_range(UnitId id) const +int Config::get_unit_min_range(UnitId id) const +{ + auto it = unit_min_range.find(id); + if (it != unit_min_range.end()) { - auto it = unit_min_range.find(id); - if (it != unit_min_range.end()) - { - return it->second; - } - throw std::runtime_error("Min range for unit ID not found"); + return it->second; } + throw std::runtime_error("Min range for unit ID not found"); +} - int Config::get_unit_max_range(UnitId id) const +int Config::get_unit_max_range(UnitId id) const +{ + auto it = unit_max_range.find(id); + if (it != unit_max_range.end()) { - auto it = unit_max_range.find(id); - if (it != unit_max_range.end()) - { - return it->second; - } - throw std::runtime_error("Max range for unit ID not found"); + return it->second; } + throw std::runtime_error("Max range for unit ID not found"); +} - std::string Config::get_unit_primary_weapon(UnitId id) const +std::string Config::get_unit_primary_weapon(UnitId id) const +{ + auto it = unit_primary_weapon.find(id); + if (it != unit_primary_weapon.end()) { - auto it = unit_primary_weapon.find(id); - if (it != unit_primary_weapon.end()) - { - return it->second; - } - throw std::runtime_error("Primary weapon for unit ID not found"); + return it->second; } + throw std::runtime_error("Primary weapon for unit ID not found"); +} - std::string Config::get_unit_secondary_weapon(UnitId id) const +std::string Config::get_unit_secondary_weapon(UnitId id) const +{ + auto it = unit_secondary_weapon.find(id); + if (it != unit_secondary_weapon.end()) { - auto it = unit_secondary_weapon.find(id); - if (it != unit_secondary_weapon.end()) - { - return it->second; - } - throw std::runtime_error("Secondary weapon for unit ID not found"); + return it->second; } + throw std::runtime_error("Secondary weapon for unit ID not found"); +} - int Config::get_unit_primary_weapon_damage(UnitId attackerid, UnitId defenderid) const +int Config::get_unit_primary_weapon_damage(UnitId attackerid, UnitId defenderid) const +{ + auto it = primary_weapon_damage.find(attackerid); + if (it != primary_weapon_damage.end()) { - auto it = primary_weapon_damage.find(attackerid); - if (it != primary_weapon_damage.end()) + auto damageIt = it->second.find(defenderid); + if (damageIt != it->second.end()) { - auto damageIt = it->second.find(defenderid); - if (damageIt != it->second.end()) - { - return damageIt->second; - } + return damageIt->second; } - throw std::runtime_error("Primary weapon damage not found for given attacker/defender combination"); } + throw std::runtime_error("Primary weapon damage not found for given attacker/defender combination"); +} - int Config::get_unit_secondary_weapon_damage(UnitId attackerid, UnitId defenderid) const +int Config::get_unit_secondary_weapon_damage(UnitId attackerid, UnitId defenderid) const +{ + auto it = secondary_weapon_damage.find(attackerid); + if (it != secondary_weapon_damage.end()) { - auto it = secondary_weapon_damage.find(attackerid); - if (it != secondary_weapon_damage.end()) + auto damageIt = it->second.find(defenderid); + if (damageIt != it->second.end()) { - auto damageIt = it->second.find(defenderid); - if (damageIt != it->second.end()) - { - return damageIt->second; - } + return damageIt->second; } - throw std::runtime_error("Secondary weapon damage not found for given attacker/defender combination"); } + throw std::runtime_error("Secondary weapon damage not found for given attacker/defender combination"); +} } \ No newline at end of file diff --git a/src/unit.hpp b/src/unit.hpp index 626f67f..be7e1d5 100644 --- a/src/unit.hpp +++ b/src/unit.hpp @@ -40,7 +40,6 @@ enum class UnitId { LANDER = 17, //Diese Einheit kann tauchen, und ist somit für einige Einheiten nicht sichtbar und kann zum Teil auch im Tauchen nicht angegriffen werden SUBMARINE = 18, - UNKNOWN = 19 }; /* @@ -82,7 +81,6 @@ enum class MovementType { AIR = 3, SEA = 4, LANDER = 5, - UNKNOWN = 6 }; //Fill the MatchupTabel -- GitLab