Skip to content
Snippets Groups Projects
Commit a42d1514 authored by Lorenz Martin Diel's avatar Lorenz Martin Diel
Browse files

added damageTable

parent 080165d7
Branches
No related tags found
2 merge requests!9Xml,!8Xml
......@@ -5,35 +5,17 @@
#include <iostream>
#include <stdexcept> // Für die Ausnahmebehandlung
#include "unit.hpp" // Include für UnitId
#include "config.hpp"
namespace pt = boost::property_tree;
namespace advanced_wars
{
class Config
Config::Config(MatchupTabel_secondaryweapon& secWeapon, MatchupTabel_primaryweapon& primWeapon)
: secondary_weapon_damage(secWeapon), primary_weapon_damage(primWeapon)
{
public:
void loadFromXML(const char *filename);
int get_unit_cost(UnitId id) const;
int get_unit_movementPoints(UnitId id) const;
int get_unit_ammo(UnitId id) const;
int get_unit_minRange(UnitId id) const;
int get_unit_maxRange(UnitId id) const;
std::string get_unit_secondaryweapon(UnitId id) const;
std::string get_unit_primaryweapon(UnitId id) const;
private:
std::unordered_map<UnitId, int> unit_costs;
std::unordered_map<UnitId, int> unit_movementPoints;
std::unordered_map<UnitId, int> unit_ammo;
std::unordered_map<UnitId, int> unit_minRange;
std::unordered_map<UnitId, int> unit_maxRange;
std::unordered_map<UnitId, std::string> unit_secondaryweapon;
std::unordered_map<UnitId, std::string> unit_primaryweapon;
};
// Initialisierung hier wenn nötig
}
void Config::loadFromXML(const char *filename)
{
pt::ptree tree;
......@@ -52,7 +34,7 @@ namespace advanced_wars
}
else if (unitKey == "Mech")
{
unitId = UnitId::MECH;
unitId = UnitId::MECHANIZED_INFANTERY;
}
else
{
......@@ -84,6 +66,68 @@ namespace advanced_wars
unit_secondaryweapon[unitId] = weaponName;
}
}
for (const auto &weapon : unit.second.get_child("Weapons"))
{
if (weapon.first == "PrimaryWeapon")
{
std::string weaponName = weapon.second.get<std::string>("<xmlattr>.name");
unit_primaryweapon[unitId] = weaponName;
for (const auto &damage_entry : weapon.second.get_child("DamageTable"))
{
if (damage_entry.first == "Damage")
{
std::string targetUnitId = damage_entry.second.get<std::string>("<xmlattr>.unitId");
int damageValue = damage_entry.second.get<int>("<xmlattr>.value");
UnitId targetId;
if (targetUnitId == "infantry")
{
targetId = UnitId::INFANTERY;
}
else if (targetUnitId == "Mech")
{
targetId = UnitId::MECHANIZED_INFANTERY;
}
else
{
continue; // Überspringt nicht unterstützte Ziele
}
primary_weapon_damage[unitId][targetId] = damageValue;
}
}
}
else if (weapon.first == "SecondaryWeapon")
{
for (const auto &damage_entry : weapon.second.get_child("DamageTable"))
{
std::string weaponName = weapon.second.get<std::string>("<xmlattr>.name");
unit_secondaryweapon[unitId] = weaponName;
if (damage_entry.first == "Damage")
{
std::string targetUnitId = damage_entry.second.get<std::string>("<xmlattr>.unitId");
int damageValue = damage_entry.second.get<int>("<xmlattr>.value");
UnitId targetId;
if (targetUnitId == "infantry")
{
targetId = UnitId::INFANTERY;
}
else if (targetUnitId == "Mech")
{
targetId = UnitId::MECHANIZED_INFANTERY;
}
else
{
continue; // Überspringt nicht unterstützte Ziele
}
secondary_weapon_damage[unitId][targetId] = damageValue;
}
}
}
}
}
}
}
......
#ifndef CONFIG_HPP
#define CONFIG_HPP
#include <unordered_map>
#include <string>
#include <stdexcept>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include "unit.hpp" // Include für UnitId
namespace advanced_wars {
class Config {
public:
Config(MatchupTabel_secondaryweapon& secWeapon, MatchupTabel_primaryweapon& primWeapon);
void loadFromXML(const char* filename);
int get_unit_cost(UnitId id) const;
int get_unit_movementPoints(UnitId id) const;
int get_unit_ammo(UnitId id) const;
int get_unit_minRange(UnitId id) const;
int get_unit_maxRange(UnitId id) const;
std::string get_unit_secondaryweapon(UnitId id) const;
std::string get_unit_primaryweapon(UnitId id) const;
private:
std::unordered_map<UnitId, int> unit_costs;
std::unordered_map<UnitId, int> unit_movementPoints;
std::unordered_map<UnitId, int> unit_ammo;
std::unordered_map<UnitId, int> unit_minRange;
std::unordered_map<UnitId, int> unit_maxRange;
std::unordered_map<UnitId, std::string> unit_secondaryweapon;
std::unordered_map<UnitId, std::string> unit_primaryweapon;
MatchupTabel_secondaryweapon& secondary_weapon_damage;
MatchupTabel_primaryweapon& primary_weapon_damage;
};
} // namespace advanced_wars
#endif // CONFIG_HPP
\ No newline at end of file
......@@ -30,6 +30,8 @@ void Level::render(Engine &engine, std::vector<SDL_Event> &events) {
events.erase(events.begin());
}
// Tiles
for (Tile &tile : tiles) {
tile.render(engine, RENDERING_SCALE);
......
......@@ -8,25 +8,33 @@
#include "window.hpp"
#include <cstddef>
#include <vector>
#include "config.hpp"
#include <iostream>
using namespace advanced_wars;
int main() {
int main()
{
Window window("Advanced Wars", 960, 960);
Engine engine(window);
Unit::readXML();
// Construct a level
std::vector<Tile> tiles;
for (int y = 0; y < 20; y++) {
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < 20; x++)
{
tiles.push_back(Tile(TileId::PLAIN, x, y));
}
}
// Fill the edges with water
for (size_t n = 0; n < 20; n++) {
for (size_t n = 0; n < 20; n++)
{
// Vertical
tiles.at(n * 20) = Tile(TileId::WATER, 0, n);
tiles.at(n * 20 + 19) = Tile(TileId::WATER, 19, n);
......@@ -36,7 +44,8 @@ int main() {
}
// Make the edges cliffs
for (size_t n = 1; n < 19; n++) {
for (size_t n = 1; n < 19; n++)
{
// Vertical
tiles.at(n * 20 + 1) = Tile(TileId::CLIFF_RIGHT, 1, n);
tiles.at(n * 20 + 18) = Tile(TileId::CLIFF_LEFT, 18, n);
......@@ -55,8 +64,10 @@ int main() {
// Buildings
std::vector<Building> buildings;
for (int y = 0; y < 6; y++) {
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 6; y++)
{
for (int x = 0; x < 5; x++)
{
BuildingId id = static_cast<BuildingId>(x);
BuildingFaction faction = static_cast<BuildingFaction>(y);
......@@ -67,8 +78,10 @@ int main() {
// Units
std::vector<Unit> units;
for (int y = 0; y < 19; y++) {
for (int x = 0; x < 6; x++) {
for (int y = 0; y < 19; y++)
{
for (int x = 0; x < 6; x++)
{
units.push_back(Unit(x + 9, y + 2, UnitFaction::URED,
static_cast<UnitId>(y), static_cast<UnitState>(x)));
}
......@@ -82,12 +95,13 @@ int main() {
engine.set_scene(level);
Spritesheet spritesheet("/media/data/rust/sprite-extractor/spritesheet.h5",
Spritesheet spritesheet("/home/lorenz/Hochschule/5.Semester/C++/Projekt2/spritesheet.h5",
engine);
engine.set_spritesheet(spritesheet);
while (!engine.exited()) {
while (!engine.exited())
{
engine.pump();
engine.render();
}
......
#include "unit.hpp"
#include <tinyxml2.h>
#include <iostream>
#include "config.hpp"
namespace advanced_wars
{
......@@ -174,6 +175,87 @@ namespace advanced_wars
}
return false;
}
// Config test start
void Unit::readXML()
{
MatchupTabel_secondaryweapon secWeapon;
MatchupTabel_secondaryweapon primWeapon;
Config config(secWeapon, primWeapon);
// Lade Konfigurationsdaten von XML
try
{
config.loadFromXML("../xml/config.xml");
}
catch (const std::exception &e)
{
std::cerr << "Error loading XML: " << e.what() << std::endl;
}
// Liste der zu testenden UnitIds
std::vector<UnitId> testUnits = {UnitId::INFANTERY, UnitId::MECHANIZED_INFANTERY};
for (auto id : testUnits)
{
try
{
int cost = config.get_unit_cost(id);
int movement = config.get_unit_movementPoints(id);
int ammo = config.get_unit_ammo(id);
int minRange = config.get_unit_minRange(id);
int maxRange = config.get_unit_maxRange(id);
std::string primaryWeapon = "None";
std::string secondaryWeapon = "None";
if (id == UnitId::INFANTERY || id == UnitId::MECHANIZED_INFANTERY)
{ // Bsp.-Fall, hier müssten je nach Wunschbedingungen ID's ergänzt werden
try
{
primaryWeapon = config.get_unit_primaryweapon(id);
}
catch (...)
{
// Es passiert nichts, wenn die Primärwaffe fehlt
}
try
{
secondaryWeapon = config.get_unit_secondaryweapon(id);
}
catch (...)
{
// Es passiert nichts, wenn die Sekundärwaffe fehlt
}
}
std::cout << "Unit ID: " << static_cast<int>(id) << std::endl;
std::cout << " Cost: " << cost << std::endl;
std::cout << " Movement Points: " << movement << std::endl;
std::cout << " Ammo: " << ammo << std::endl;
std::cout << " Primary Weapon: " << primaryWeapon << "; Range: " << minRange << "-" << maxRange << std::endl;
std::cout << " Secondary Weapon: " << secondaryWeapon << std::endl;
}
catch (const std::exception &e)
{
std::cerr << "Error retrieving unit data for ID " << static_cast<int>(id) << ": " << e.what() << std::endl;
}
}
std::cout << "Debug secWeapon Inhalt:" << std::endl;
for (const auto &outer : secWeapon)
{
std::cout << "Einheit ID: " << static_cast<int>(outer.first) << std::endl;
for (const auto &inner : outer.second)
{
std::cout << " Ziel ID: " << static_cast<int>(inner.first)
<< " Schaden: " << inner.second << std::endl;
}
}
}
// Config test end
/*
void Unit::loadXML(const char *filename)
{
......
......@@ -5,6 +5,7 @@
#include "weapon.hpp"
#include <optional>
namespace advanced_wars {
enum class UnitFaction {
......@@ -14,7 +15,7 @@ enum class UnitFaction {
UYELLOW = 3,
UPURPLE = 4,
};
/*
enum class UnitId {
INFANTERY = 0,
MECHANIZED_INFANTERY = 1,
......@@ -36,8 +37,8 @@ enum class UnitId {
LANDER = 17,
SUBMARINE = 18,
};
*/
/*
enum class UnitId {
INFANTERY,
MECH,
......@@ -58,7 +59,7 @@ enum class UnitId {
CRUISER,
LANDER,
SUBMARINE,
};
};*/
enum class UnitState {
IDLE = 0,
......@@ -81,6 +82,9 @@ enum class MovementType {
//Fill the MatchupTabel
using MatchupTabel = std::unordered_map<u_int8_t, std::unordered_map<u_int8_t, int>>;
using MatchupTabel_secondaryweapon = std::unordered_map<UnitId, std::unordered_map<UnitId, int>>;
using MatchupTabel_primaryweapon = std::unordered_map<UnitId, std::unordered_map<UnitId, int>>;
class Unit {
public:
Unit(int x, int y, UnitFaction faction, UnitId id, UnitState state);
......@@ -127,6 +131,8 @@ public:
*/
void loadXML(const char* filename);
static void readXML();
/*
This function will be called by an external event-handler, eventually.
......
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment