Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cpp-project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Maul
cpp-project
Commits
97a2125b
Commit
97a2125b
authored
6 months ago
by
Lorenz Martin Diel
Browse files
Options
Downloads
Patches
Plain Diff
adjusted attack function on DamageTable in weapon
parent
5b0d77b7
Branches
Branches containing commit
No related tags found
1 merge request
!15
Merge units into main
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/unit.cpp
+54
-60
54 additions, 60 deletions
src/unit.cpp
src/unit.hpp
+2
-2
2 additions, 2 deletions
src/unit.hpp
src/weapon.cpp
+25
-1
25 additions, 1 deletion
src/weapon.cpp
src/weapon.hpp
+15
-7
15 additions, 7 deletions
src/weapon.hpp
with
96 additions
and
70 deletions
src/unit.cpp
+
54
−
60
View file @
97a2125b
...
...
@@ -7,6 +7,11 @@ namespace advanced_wars
Unit
::
Unit
(
int
x
,
int
y
,
UnitFaction
faction
,
UnitId
id
,
UnitState
state
)
:
x
(
x
),
y
(
y
),
faction
(
faction
),
id
(
id
),
state
(
state
),
max_health
(
100
)
{
//das ist nur für Testzwecke
if
(
id
==
UnitId
::
INFANTERY
)
{
secondary_weapon
=
Weapon
(
"Machine-Gun"
,
{{
UnitId
::
INFANTERY
,
55
}});
}
health
=
max_health
;
};
...
...
@@ -71,79 +76,68 @@ namespace advanced_wars
void
Unit
::
attack
(
Unit
*
enemy
)
{
secondary_weapon
=
fill_matchupTable
(
0
);
primary_weapon
=
fill_matchupTable
(
1
);
// Zuerst die Tabel für die Waffen der angreifenden Einheit holen
auto
&
attackerSecondaryWeaponTable
=
secondary_weapon
[
this
->
id
];
auto
&
attackerPrimaryWeaponTable
=
primary_weapon
[
this
->
id
];
// Angenommen, primary_weapon und secondary_weapon wurden bereits korrekt initialisiert
auto
primary_weapon_damage_it
=
primary_weapon
.
damage
.
find
(
enemy
->
id
);
auto
secondary_weapon_damage_it
=
secondary_weapon
.
damage
.
find
(
enemy
->
id
);
// Schadenswert für die angreifende Einheit gegen die verteidigende Einheit berechnen
// Es wird die Waffe genommen die mehr Schaden macht
int
attacker_damage_value
=
0
;
int
attackerDamageValue
=
0
;
if
(
attackerSecondaryWeaponTable
.
find
(
enemy
->
id
)
!=
attackerSecondaryWeaponTable
.
end
())
// Die Waffe mit dem höchsten Schaden wählen
if
(
secondary_weapon_damage_it
!=
secondary_weapon
.
damage
.
end
())
{
attacker
D
amage
V
alue
=
attackerS
econdary
W
eapon
Table
[
enemy
->
id
]
;
attacker
_d
amage
_v
alue
=
s
econdary
_w
eapon
_damage_it
->
second
;
}
if
(
attackerP
rimary
W
eapon
Table
.
find
(
enemy
->
id
)
!=
attackerP
rimary
W
eapon
Tabl
e
.
end
())
if
(
p
rimary
_w
eapon
_damage_it
!=
p
rimary
_w
eapon
.
damag
e
.
end
())
{
if
(
attackerDamageValue
<
attackerPrimaryWeaponTable
[
enemy
->
id
]
)
if
(
primary_weapon_damage_it
->
second
>
attacker_damage_value
)
{
//
Here ammo deduction should happen if applicable
attacker
D
amage
V
alue
=
attackerP
rimary
W
eapon
Table
[
enemy
->
id
]
;
//
Munitionsabzug sollte hier erfolgen, falls zutreffend
attacker
_d
amage
_v
alue
=
p
rimary
_w
eapon
_damage_it
->
second
;
}
}
if
(
attackerDamageValue
==
0
)
if
(
attacker_damage_value
==
0
)
{
std
::
cout
<<
"No damage value found for attack from unit "
<<
static_cast
<
int
>
(
id
)
<<
" against unit "
<<
static_cast
<
int
>
(
enemy
->
id
)
<<
std
::
endl
;
}
else
{
int
offDamage
=
attackerDamageValue
*
(
static_cast
<
float
>
(
health
)
/
max_health
);
enemy
->
health
-=
offDamage
;
enemy
->
health
=
std
::
max
(
0
,
enemy
->
health
);
// Ensuring health is not negative
int
off_damage
=
attacker_damage_value
*
(
static_cast
<
float
>
(
health
)
/
max_health
);
enemy
->
health
-=
off_damage
;
enemy
->
health
=
std
::
max
(
0
,
enemy
->
health
);
// Sicherstellen, dass die Gesundheit nicht negativ wird
std
::
cout
<<
"Enemy health after attack: "
<<
enemy
->
health
<<
std
::
endl
;
// Prüfen, ob der Gegner noch am Leben ist um zurückzuschlagen
if
(
enemy
->
health
>
0
)
{
// Weapon tables for the defender
auto
&
defender
Second
ary
W
eapon
Table
=
secondary_weapon
[
enemy
->
id
]
;
auto
&
defender
Prim
ary
W
eapon
Table
=
primary_weapon
[
enemy
->
id
]
;
auto
defender
_prim
ary
_w
eapon
_damage_it
=
enemy
->
primary_weapon
.
damage
.
find
(
id
)
;
auto
defender
_second
ary
_w
eapon
_damage_it
=
enemy
->
secondary_weapon
.
damage
.
find
(
id
)
;
int
defender
D
amage
V
alue
=
0
;
// Declare outside for later use
int
defender
_d
amage
_v
alue
=
0
;
// Declare outside for later use
// Determine the damage value for the defender
if
(
defender
S
econdary
W
eapon
Table
.
find
(
id
)
!=
defenderS
econdary
W
eapon
Tabl
e
.
end
())
if
(
defender
_s
econdary
_w
eapon
_damage_it
!=
enemy
->
s
econdary
_w
eapon
.
damag
e
.
end
())
{
defender
D
amage
V
alue
=
defender
S
econdary
W
eapon
Table
[
id
]
;
defender
_d
amage
_v
alue
=
defender
_s
econdary
_w
eapon
_damage_it
->
second
;
}
if
(
defenderPrimaryWeaponTable
.
find
(
id
)
!=
defenderPrimaryWeaponTable
.
end
())
if
(
defender_primary_weapon_damage_it
!=
enemy
->
primary_weapon
.
damage
.
end
())
{
if
(
defender
DamageValue
<
defenderPrimaryWeaponTable
[
id
]
)
if
(
defender
_primary_weapon_damage_it
->
second
>
defender_damage_value
)
{
//
Deduct ammo
f
o
r prim
ary weapon, if applicable
defender
D
amage
V
alue
=
defender
P
rimary
W
eapon
Table
[
id
]
;
//
Munitionsabzug
f
ü
r prim
äre Waffe, falls zutreffend
defender
_d
amage
_v
alue
=
defender
_p
rimary
_w
eapon
_damage_it
->
second
;
}
}
// If a valid damage value was determined for retaliation
if
(
defender
D
amage
V
alue
>
0
)
if
(
defender
_d
amage
_v
alue
>
0
)
{
int
def
D
amage
=
static_cast
<
int
>
(
defender
D
amage
V
alue
*
static_cast
<
float
>
(
enemy
->
health
)
/
enemy
->
max_health
);
this
->
health
-=
def
D
amage
;
int
def
_d
amage
=
static_cast
<
int
>
(
defender
_d
amage
_v
alue
*
static_cast
<
float
>
(
enemy
->
health
)
/
enemy
->
max_health
);
this
->
health
-=
def
_d
amage
;
this
->
health
=
std
::
max
(
0
,
this
->
health
);
// Safeguard against negative health
std
::
cout
<<
"Ally health after retaliation: "
<<
this
->
health
<<
std
::
endl
;
}
...
...
@@ -151,7 +145,8 @@ namespace advanced_wars
}
}
MatchupTable
Unit
::
fill_matchupTable
(
int
type
)
{
MatchupTable
Unit
::
fill_matchupTable
(
int
type
)
{
switch
(
type
)
{
case
0
:
...
...
@@ -169,7 +164,6 @@ namespace advanced_wars
this
->
y
=
posY
;
}
/*
Features:
//select unit
...
...
This diff is collapsed.
Click to expand it.
src/unit.hpp
+
2
−
2
View file @
97a2125b
...
...
@@ -131,8 +131,8 @@ private:
bool
has_attacked
;
bool
is_selected
;
bool
is_targeted
;
MatchupTable
secondary_weapon
;
MatchupTable
primary_weapon
;
Weapon
secondary_weapon
;
Weapon
primary_weapon
;
int
ammo
;
...
...
This diff is collapsed.
Click to expand it.
src/weapon.cpp
+
25
−
1
View file @
97a2125b
#include
"weapon.hpp"
namespace
advanced_wars
{}
\ No newline at end of file
namespace
advanced_wars
{
Weapon
::
Weapon
()
:
name
(
""
),
damage
()
{}
Weapon
::
Weapon
(
const
std
::
string
&
weaponName
,
const
std
::
unordered_map
<
UnitId
,
int
>
&
damageValues
)
:
name
(
weaponName
),
damage
(
damageValues
)
{}
// Funktion zum Hinzufügen von Schadenswerten
void
Weapon
::
addDamageValue
(
UnitId
unitId
,
int
value
)
{
damage
[
unitId
]
=
value
;
}
// Funktion zum Abrufen eines Schadenswertes
int
Weapon
::
getDamageValue
(
UnitId
unitId
)
const
{
auto
it
=
damage
.
find
(
unitId
);
if
(
it
!=
damage
.
end
())
{
return
it
->
second
;
}
return
0
;
// oder ein Fehlerwert
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/weapon.hpp
+
15
−
7
View file @
97a2125b
#pragma once
#include
"unit.hpp"
#include
<string>
#include
<unordered_map>
namespace
advanced_wars
{
// Forward Declaration
enum
class
UnitId
;
class
Weapon
{
public:
//
Ranges
int
min_range
;
int
max_range
;
//
Konstruktoren
Weapon
()
;
Weapon
(
const
std
::
string
&
weaponName
,
const
std
::
unordered_map
<
UnitId
,
int
>
&
damageValues
)
;
// Damage
// Methode, um einen Schadenswert hinzuzufügen
void
addDamageValue
(
UnitId
unitId
,
int
value
);
// Methode, um einen Schadenswert abzurufen
int
getDamageValue
(
UnitId
unitId
)
const
;
// Name der Waffe
std
::
string
name
;
// Schadenstabelle
std
::
unordered_map
<
UnitId
,
int
>
damage
;
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment