diff options
author | Max Horn | 2009-02-24 05:30:45 +0000 |
---|---|---|
committer | Max Horn | 2009-02-24 05:30:45 +0000 |
commit | 47bc0d787949faab3705d3e04388d6f0d4892609 (patch) | |
tree | 9dfa77c4944c8e41d26975da96aa066ba6447f13 | |
parent | 325d2ec66b15d63f44e5e11239c18503e7021942 (diff) | |
download | scummvm-rg350-47bc0d787949faab3705d3e04388d6f0d4892609.tar.gz scummvm-rg350-47bc0d787949faab3705d3e04388d6f0d4892609.tar.bz2 scummvm-rg350-47bc0d787949faab3705d3e04388d6f0d4892609.zip |
SCI: Last batch of pathfinding cleanup
svn-id: r38831
-rw-r--r-- | engines/sci/engine/kpathing.cpp | 98 |
1 files changed, 39 insertions, 59 deletions
diff --git a/engines/sci/engine/kpathing.cpp b/engines/sci/engine/kpathing.cpp index 1fc3227eb7..7a42f71ccc 100644 --- a/engines/sci/engine/kpathing.cpp +++ b/engines/sci/engine/kpathing.cpp @@ -116,6 +116,12 @@ struct Vertex { // Previous vertex in shortest path Vertex *path_prev; + +public: + Vertex(const Common::Point &p) : v(p) { + dist = HUGE_DISTANCE; + path_prev = NULL; + } }; typedef Common::List<Vertex *> VertexList; @@ -186,6 +192,18 @@ struct Polygon { // Circular list of vertices CircularVertexList vertices; + +public: + Polygon(int t) : type(t) { + } + + ~Polygon() { + while (!vertices.empty()) { + Vertex *vertex = vertices.first(); + vertices.remove(vertex); + delete vertex; + } + } }; typedef Common::List<Polygon *> PolygonList; @@ -222,8 +240,18 @@ struct PathfindingState { vis_matrix = NULL; vertices = 0; } + + ~PathfindingState() { + free(vertex_index); + free(vis_matrix); + + for (PolygonList::iterator it = polygons.begin(); it != polygons.end(); ++it) { + delete *it; + } + } }; + static Vertex *vertex_cur; // Temporary hack to deal with points in reg_ts @@ -462,29 +490,6 @@ static int intersect(Common::Point a, Common::Point b, Common::Point c, Common:: return between(a, b, c) || between(a, b, d) || between(c, d, a) || between(c, d, b); } -static Vertex *vertex_new(Common::Point p) { - // Allocates and initialises a new vertex - // Parameters: (Common::Point) p: The position of the vertex - // Returns : (Vertex *) A newly allocated vertex - Vertex *vertex = (Vertex*)sci_malloc(sizeof(Vertex)); - - vertex->v = p; - vertex->dist = HUGE_DISTANCE; - vertex->path_prev = NULL; - - return vertex; -} - -static Polygon *polygon_new(int type) { - // Allocates and initialises a new polygon - // Parameters: (int) type: The SCI polygon type - // Returns : (Polygon *) A newly allocated polygon - Polygon *polygon = new Polygon(); - polygon->type = type; - - return polygon; -} - static int contained(Common::Point p, Polygon *polygon) { // Polygon containment test // Parameters: (Common::Point) p: The point @@ -1131,7 +1136,7 @@ static Vertex *merge_point(PathfindingState *s, Common::Point v) { } } - v_new = vertex_new(v); + v_new = new Vertex(v); // Check for point being on an edge for (PolygonList::iterator it = s->polygons.begin(); it != s->polygons.end(); ++it) { @@ -1151,7 +1156,7 @@ static Vertex *merge_point(PathfindingState *s, Common::Point v) { } // Add point as single-vertex polygon - polygon = polygon_new(POLY_BARRED_ACCESS); + polygon = new Polygon(POLY_BARRED_ACCESS); polygon->vertices.insertHead(v_new); s->polygons.push_front(polygon); @@ -1167,11 +1172,11 @@ static Polygon *convert_polygon(EngineState *s, reg_t polygon) { reg_t points = GET_SEL32(polygon, points); int size = KP_UINT(GET_SEL32(polygon, size)); unsigned char *list = kernel_dereference_bulk_pointer(s, points, size * POLY_POINT_SIZE); - Polygon *poly = polygon_new(KP_UINT(GET_SEL32(polygon, type))); + Polygon *poly = new Polygon(KP_UINT(GET_SEL32(polygon, type))); int is_reg_t = polygon_is_reg_t(list, size); for (i = 0; i < size; i++) { - Vertex *vertex = vertex_new(read_point(list, is_reg_t, i)); + Vertex *vertex = new Vertex(read_point(list, is_reg_t, i)); poly->vertices.insertHead(vertex); } @@ -1180,31 +1185,6 @@ static Polygon *convert_polygon(EngineState *s, reg_t polygon) { return poly; } -static void free_polygon(Polygon *polygon) { - // Frees a polygon and its vertices - // Parameters: (Polygon *) polygons: The polygon - while (!polygon->vertices.empty()) { - Vertex *vertex = polygon->vertices.first(); - polygon->vertices.remove(vertex); - free(vertex); - } - - delete polygon; -} - -static void free_pf_state(PathfindingState *p) { - // Frees a pathfinding state - // Parameters: (PathfindingState *) p: The pathfinding state - free(p->vertex_index); - free(p->vis_matrix); - - for (PolygonList::iterator it = p->polygons.begin(); it != p->polygons.end(); ++it) { - free_polygon(*it); - } - - delete p; -} - static void change_polygons_opt_0(PathfindingState *s) { // Changes the polygon list for optimization level 0 (used for keyboard // support). Totally accessible polygons are removed and near-point @@ -1217,7 +1197,7 @@ static void change_polygons_opt_0(PathfindingState *s) { assert(polygon); if (polygon->type == POLY_TOTAL_ACCESS) { - free_polygon(polygon); + delete polygon; it = s->polygons.erase(it); } else { if (polygon->type == POLY_NEAREST_ACCESS) @@ -1263,7 +1243,7 @@ static PathfindingState *convert_polygon_set(EngineState *s, reg_t poly_list, Co if (err == PF_FATAL) { sciprintf("[avoidpath] Error: fatal error finding nearest intersecton\n"); - free_pf_state(pf_s); + delete pf_s; return NULL; } else if (err == PF_OK) // Keep original start position if intersection was found @@ -1271,7 +1251,7 @@ static PathfindingState *convert_polygon_set(EngineState *s, reg_t poly_list, Co } else { if (fix_point(pf_s, start, &start, &polygon) != PF_OK) { sciprintf("[avoidpath] Error: couldn't fix start position for pathfinding\n"); - free_pf_state(pf_s); + delete pf_s; return NULL; } else if (polygon) { // Start position has moved @@ -1283,7 +1263,7 @@ static PathfindingState *convert_polygon_set(EngineState *s, reg_t poly_list, Co if (fix_point(pf_s, end, &end, &polygon) != PF_OK) { sciprintf("[avoidpath] Error: couldn't fix end position for pathfinding\n"); - free_pf_state(pf_s); + delete pf_s; return NULL; } else { // Keep original end position if it is contained in a @@ -1537,7 +1517,7 @@ reg_t kAvoidPath(EngineState *s, int funct_nr, int argc, reg_t *argv) { } retval = make_reg(0, contained(start, polygon) != CONT_OUTSIDE); - free_polygon(polygon); + delete polygon; return retval; } case 6 : @@ -1564,7 +1544,7 @@ reg_t kAvoidPath(EngineState *s, int funct_nr, int argc, reg_t *argv) { if (intersecting_polygons(p)) { sciprintf("[avoidpath] Error: input set contains (self-)intersecting polygons\n"); - free_pf_state(p); + delete p; p = NULL; } @@ -1587,7 +1567,7 @@ reg_t kAvoidPath(EngineState *s, int funct_nr, int argc, reg_t *argv) { dijkstra(p); output = output_path(p, s); - free_pf_state(p); + delete p; // Memory is freed by explicit calls to Memory return output; |