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
9df4dc5f
Commit
9df4dc5f
authored
6 months ago
by
Lorenz Martin Diel
Browse files
Options
Downloads
Patches
Plain Diff
BOOST-GRAPH start
parent
72825774
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/level.cpp
+112
-4
112 additions, 4 deletions
src/level.cpp
src/level.hpp
+9
-31
9 additions, 31 deletions
src/level.hpp
with
121 additions
and
35 deletions
src/level.cpp
+
112
−
4
View file @
9df4dc5f
...
...
@@ -7,12 +7,114 @@
namespace
advanced_wars
{
const
std
::
map
<
std
::
pair
<
MovementType
,
TerrainType
>
,
int
>
MOVEMENT_COSTS
=
{
{{
MovementType
::
INFANTRY
,
TerrainType
::
PLAIN
},
1
},
{{
MovementType
::
INFANTRY
,
TerrainType
::
FOREST
},
2
},
{{
MovementType
::
INFANTRY
,
TerrainType
::
MOUNTAIN
},
2
},
{{
MovementType
::
INFANTRY
,
TerrainType
::
STREET
},
1
},
{{
MovementType
::
INFANTRY
,
TerrainType
::
WATER
},
999
},
Level
::
Level
(
std
::
string
name
,
int
width
,
int
height
,
std
::
vector
<
Tile
>
tiles
,
std
::
vector
<
Building
>
buildings
,
std
::
vector
<
Unit
>
units
)
:
name
(
name
),
width
(
width
),
height
(
height
),
buildings
(
buildings
),
units
(
units
)
{};
{{
MovementType
::
TRACKED
,
TerrainType
::
PLAIN
},
1
},
{{
MovementType
::
TRACKED
,
TerrainType
::
FOREST
},
2
},
{{
MovementType
::
TRACKED
,
TerrainType
::
MOUNTAIN
},
999
},
{{
MovementType
::
TRACKED
,
TerrainType
::
STREET
},
1
},
{{
MovementType
::
TRACKED
,
TerrainType
::
WATER
},
999
},
void
Level
::
render
(
SDL_Renderer
*
renderer
,
std
::
vector
<
SDL_Event
>
&
events
)
{
{{
MovementType
::
WHEELED
,
TerrainType
::
PLAIN
},
2
},
{{
MovementType
::
WHEELED
,
TerrainType
::
FOREST
},
3
},
{{
MovementType
::
WHEELED
,
TerrainType
::
MOUNTAIN
},
999
},
{{
MovementType
::
WHEELED
,
TerrainType
::
STREET
},
1
},
{{
MovementType
::
WHEELED
,
TerrainType
::
WATER
},
999
},
{{
MovementType
::
AIR
,
TerrainType
::
PLAIN
},
1
},
{{
MovementType
::
AIR
,
TerrainType
::
FOREST
},
1
},
{{
MovementType
::
AIR
,
TerrainType
::
MOUNTAIN
},
1
},
{{
MovementType
::
AIR
,
TerrainType
::
STREET
},
1
},
{{
MovementType
::
AIR
,
TerrainType
::
WATER
},
999
},
{{
MovementType
::
SHIPENGINE
,
TerrainType
::
PLAIN
},
999
},
{{
MovementType
::
SHIPENGINE
,
TerrainType
::
FOREST
},
999
},
{{
MovementType
::
SHIPENGINE
,
TerrainType
::
MOUNTAIN
},
999
},
{{
MovementType
::
SHIPENGINE
,
TerrainType
::
STREET
},
999
},
{{
MovementType
::
SHIPENGINE
,
TerrainType
::
WATER
},
1
}
};
Level
::
Level
(
const
std
::
vector
<
std
::
vector
<
TerrainType
>>&
map
,
string
name
,
int
width
,
int
height
,
std
::
vector
<
Tile
>
tiles
,
std
::
vector
<
Building
>
buildings
,
std
::
vector
<
Unit
>
units
)
:
name
(
name
),
width
(
width
),
height
(
height
),
buildings
(
buildings
),
units
(
units
),
vertex_map
(
map
.
size
(),
std
::
vector
<
Vertex
>
(
map
[
0
].
size
())
{
initializeGraph
(
map
);
};
Level
::
initializeGraph
(
const
std
::
vector
<
std
::
vector
<
TerrainType
>>&
map
)
{
// Füge alle Vertices hinzu
for
(
int
y
=
0
;
y
<
map
.
size
();
++
y
)
{
for
(
int
x
=
0
;
x
<
map
[
0
].
size
();
++
x
)
{
Vertex
v
=
add_vertex
({
x
,
y
,
map
[
y
][
x
]},
g
);
vertex_map
[
y
][
x
]
=
v
;
}
}
// Füge Kanten hinzu
for
(
int
y
=
0
;
y
<
map
.
size
();
++
y
)
{
for
(
int
x
=
0
;
x
<
map
[
0
].
size
();
++
x
)
{
// Horizontale und vertikale Kanten
if
(
x
<
map
[
0
].
size
()
-
1
)
{
// Rechts
addEdge
(
vertex_map
[
y
][
x
],
vertex_map
[
y
][
x
+
1
]);
}
if
(
y
<
map
.
size
()
-
1
)
{
// Unten
addEdge
(
vertex_map
[
y
][
x
],
vertex_map
[
y
+
1
][
x
]);
}
}
}
}
Level
::
addEdge
(
Vertex
v1
,
Vertex
v2
)
{
boost
::
add_edge
(
v1
,
v2
,
EdgeProperties
{},
g
);
}
/*Bin dafür den Graph aus der Level Klasse zu nehmen*/
std
::
vector
<
Position
>
Level
::
getMovementRange
(
Position
start
,
MovementType
type
,
int
movementPoints
)
{
EdgeWeightCalculator
weight_calc
(
type
,
g
);
std
::
vector
<
int
>
distances
(
num_vertices
(
g
));
std
::
vector
<
Vertex
>
predecessors
(
num_vertices
(
g
));
Vertex
start_vertex
=
vertex_map
[
start
.
y
][
start
.
x
];
boost
::
dijkstra_shortest_paths
(
g
,
start_vertex
,
boost
::
weight_map
(
boost
::
make_function_property_map
<
Edge
>
(
weight_calc
))
.
distance_map
(
boost
::
make_iterator_property_map
(
distances
.
begin
(),
boost
::
get
(
boost
::
vertex_index
,
g
)))
.
predecessor_map
(
boost
::
make_iterator_property_map
(
predecessors
.
begin
(),
boost
::
get
(
boost
::
vertex_index
,
g
))));
// Sammle erreichbare Positionen
std
::
vector
<
Position
>
reachable
;
for
(
int
y
=
0
;
y
<
vertex_map
.
size
();
++
y
)
{
for
(
int
x
=
0
;
x
<
vertex_map
[
0
].
size
();
++
x
)
{
if
(
distances
[
boost
::
get
(
boost
::
vertex_index
,
g
,
vertex_map
[
y
][
x
])]
<=
movementPoints
)
{
reachable
.
push_back
({
x
,
y
});
}
}
}
return
reachable
;
}
Level
::
render
(
SDL_Renderer
*
renderer
,
std
::
vector
<
SDL_Event
>
&
events
)
{
// Iterate over all events
while
(
!
events
.
empty
())
{
while
(
!
events
.
empty
())
{
events
.
erase
(
events
.
begin
());
}
...
...
@@ -23,4 +125,10 @@ namespace advanced_wars
}
}
int
EdgeWeightCalculator
::
operator
()(
const
Edge
&
e
)
const
{
Vertex
target_vertex
=
boost
::
target
(
e
,
graph
);
TerrainType
destTerrain
=
graph
[
target_vertex
].
terrain
;
return
MovementGraph
::
MOVEMENT_COSTS
.
at
({
currentType
,
destTerrain
});
}
}
This diff is collapsed.
Click to expand it.
src/level.hpp
+
9
−
31
View file @
9df4dc5f
...
...
@@ -31,6 +31,15 @@ namespace advanced_wars
int
weight
;
}
struct
Position
{
int
x
,
y
;
bool
operator
==
(
const
Position
&
other
)
const
{
return
x
==
other
.
x
&&
y
==
other
.
y
;
}
}
using
Graph
=
boost
::
adjacency_list
<
boost
::
vecS
,
// OutEdgeList
boost
::
vecS
,
// VertexList
...
...
@@ -41,37 +50,6 @@ namespace advanced_wars
using
Vertex
=
boost
::
graph_traits
<
Graph
>::
vertex_descriptor
;
const
std
::
map
<
std
::
pair
<
MovementType
,
TerrainType
>
,
int
>
MOVEMENT_COSTS
=
{
{{
MovementType
::
INFANTRY
,
TerrainType
::
PLAIN
},
1
},
{{
MovementType
::
INFANTRY
,
TerrainType
::
FOREST
},
2
},
{{
MovementType
::
INFANTRY
,
TerrainType
::
MOUNTAIN
},
2
},
{{
MovementType
::
INFANTRY
,
TerrainType
::
STREET
},
1
},
{{
MovementType
::
INFANTRY
,
TerrainType
::
WATER
},
999
},
{{
MovementType
::
TRACKED
,
TerrainType
::
PLAIN
},
1
},
{{
MovementType
::
TRACKED
,
TerrainType
::
FOREST
},
2
},
{{
MovementType
::
TRACKED
,
TerrainType
::
MOUNTAIN
},
999
},
{{
MovementType
::
TRACKED
,
TerrainType
::
STREET
},
1
},
{{
MovementType
::
TRACKED
,
TerrainType
::
WATER
},
999
},
{{
MovementType
::
WHEELED
,
TerrainType
::
PLAIN
},
2
},
{{
MovementType
::
WHEELED
,
TerrainType
::
FOREST
},
3
},
{{
MovementType
::
WHEELED
,
TerrainType
::
MOUNTAIN
},
999
},
{{
MovementType
::
WHEELED
,
TerrainType
::
STREET
},
1
},
{{
MovementType
::
WHEELED
,
TerrainType
::
WATER
},
999
},
{{
MovementType
::
AIR
,
TerrainType
::
PLAIN
},
1
},
{{
MovementType
::
AIR
,
TerrainType
::
FOREST
},
1
},
{{
MovementType
::
AIR
,
TerrainType
::
MOUNTAIN
},
1
},
{{
MovementType
::
AIR
,
TerrainType
::
STREET
},
1
},
{{
MovementType
::
AIR
,
TerrainType
::
WATER
},
999
},
{{
MovementType
::
SHIPENGINE
,
TerrainType
::
PLAIN
},
999
},
{{
MovementType
::
SHIPENGINE
,
TerrainType
::
FOREST
},
999
},
{{
MovementType
::
SHIPENGINE
,
TerrainType
::
MOUNTAIN
},
999
},
{{
MovementType
::
SHIPENGINE
,
TerrainType
::
STREET
},
999
},
{{
MovementType
::
SHIPENGINE
,
TerrainType
::
WATER
},
1
}};
class
Level
:
public
Scene
{
public:
...
...
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