From a2f36a447ba5a8bee61ca8b3a7c7d51c54eac2dd Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 1 Jul 2011 22:04:58 -0400 Subject: [PATCH] Created GameVertex class so that we can store game-specific data in the vertices --- gamedata.cpp | 31 +++++++++++++++++++++++++++---- gamedata.h | 10 ++++++++++ graph.cpp | 6 +----- graph.h | 3 +-- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/gamedata.cpp b/gamedata.cpp index a8d7250..718ddc3 100644 --- a/gamedata.cpp +++ b/gamedata.cpp @@ -14,6 +14,15 @@ int GameData::BASE_BUILD_RADIUS = 75; int GameData::NODE_RADIUS = 10; +GameVertex::GameVertex(int x, int y, int z, int r, int colour, int score, + VertexType type, Player* player) + : Vertex(x, y, z, r, colour, score) +{ + this->type = type; + this->player = player; +} + + GameData::GameData() : Graph(true) { @@ -108,12 +117,15 @@ bool GameData::select_vertex(int x, int y, bool only_mine) bool GameData::add_vertex(int x, int y, int z, int r, int colour) { + GameVertex* v = new GameVertex(x, y, z, r, colour, 10, VERTEX_ATTACKER, + turn); + if (current == NULL) { // this is the special case for adding the first vertex for each player if (!turn->has_played()) { - if (Graph::add_vertex(x, y, z, r, colour, 10)) + if (Graph::add_vertex(v)) { #ifdef DEBUG fprintf(stderr, "debug: GameData::add_vertex(): strength=%2.f\n", calculate_strength(*(vertices.rbegin()))); @@ -124,17 +136,26 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour) } // really, we shouldn't be able to get here. return false just in case + delete v; return false; } // same here - just a logic check - if (current->colour != turn->get_colour()) return false; + if (current->colour != turn->get_colour()) + { + delete v; + return false; + } // This is the range check... - if (MathUtils::distance(current->x, current->y, 0, x, y, 0) > get_range()) + if (MathUtils::distance(current->x, current->y, 0, v->x, v->y, 0) > + get_range()) + { + delete v; return false; + } - if (Graph::add_vertex(x, y, z, r, colour, 10, current)) + if (Graph::add_vertex(v, current)) { #ifdef DEBUG fprintf(stderr, "debug: GameData::add_vertex(): strength=%.2f\n", @@ -144,6 +165,8 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour) toggle_turn(); return true; } + + delete v; return false; } diff --git a/gamedata.h b/gamedata.h index 98ab615..132fe9d 100644 --- a/gamedata.h +++ b/gamedata.h @@ -12,6 +12,16 @@ enum Mode {MODE_MOVE, MODE_ATTACK, MODE_BUILD, MODE_SELECT}; enum VertexType {VERTEX_ATTACKER, VERTEX_DEFENDER, VERTEX_PRODUCER}; +class GameVertex : public Vertex +{ + public: + GameVertex(int x, int y, int z, int r, int colour = 0, int score = 0, + VertexType type = VERTEX_ATTACKER, Player* player = NULL); + + VertexType type; + Player* player; +}; + class GameData : public Graph { public: diff --git a/graph.cpp b/graph.cpp index f26d0e4..31f3b43 100644 --- a/graph.cpp +++ b/graph.cpp @@ -87,17 +87,14 @@ bool Graph::crosses_edge(Vertex* a, Vertex* b) } -bool Graph::add_vertex(int x, int y, int z, int r, int colour, int score, Vertex* src) +bool Graph::add_vertex(Vertex* v, Vertex* src) { - Vertex* v = new Vertex(x, y, z, r, colour, score); - // Make sure the nodes won't overlap if (vertex_would_overlap(v->x, v->y, v->z, v->r)) { #ifdef DEBUG fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to vertex collision: x=%d, y=%d, z=%d, r=%d\n", v->x, v->y, v->z, v->r); #endif - delete v; return false; } @@ -108,7 +105,6 @@ bool Graph::add_vertex(int x, int y, int z, int r, int colour, int score, Vertex #ifdef DEBUG fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to edge collision: x1=%d, y1=%d, x2=%d, y2=%d\n", v->x, v->y, src->x, src->y); #endif - delete v; return false; } diff --git a/graph.h b/graph.h index 73207f0..50c93f6 100644 --- a/graph.h +++ b/graph.h @@ -48,8 +48,7 @@ class Graph bool point_in_vertex(int x, int y, int z); Vertex * vertex_at(int x, int y, int z); - virtual bool add_vertex(int x, int y, int z, int r, int colour = 0, - int score = 0, Vertex* src=NULL); + virtual bool add_vertex(Vertex* v, Vertex* src=NULL); void remove_vertex(Vertex* target); bool is_planar() const { return planar; }