Author: sveinung
Date: Thu Oct  8 17:42:50 2015
New Revision: 30031

URL: http://svn.gna.org/viewcvs/freeciv?rev=30031&view=rev
Log:
path finding: add backtrack convenience function

The new function pf_path_backtrack() is like pf_path_advance() except that
it removes from the end of the path.

See patch #6396

Modified:
    trunk/common/aicore/path_finding.c
    trunk/common/aicore/path_finding.h

Modified: trunk/common/aicore/path_finding.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/aicore/path_finding.c?rev=30031&r1=30030&r2=30031&view=diff
==============================================================================
--- trunk/common/aicore/path_finding.c  (original)
+++ trunk/common/aicore/path_finding.c  Thu Oct  8 17:42:50 2015
@@ -3441,6 +3441,38 @@
   return TRUE;
 }
 
+/**************************************************************************
+  Remove the part of a path following a given tile.
+  If given tile is on the path more than once then the last occurrence
+  will be the one used.
+  If tile is not on the path at all, returns FALSE and path is not changed
+  at all.
+**************************************************************************/
+bool pf_path_backtrack(struct pf_path *path, struct tile *ptile)
+{
+  int i;
+  struct pf_position *new_positions;
+
+  fc_assert_ret_val(path->length > 0, FALSE);
+
+  for (i = path->length - 1; path->positions[i].tile != ptile; i--) {
+    if (i <= 0) {
+      return FALSE;
+    }
+  }
+
+  fc_assert_ret_val(i >= 0, FALSE);
+
+  path->length = i + 1;
+  new_positions = fc_malloc(sizeof(*path->positions) * path->length);
+  memcpy(new_positions, path->positions,
+         path->length * sizeof(*path->positions));
+  free(path->positions);
+  path->positions = new_positions;
+
+  return TRUE;
+}
+
 /****************************************************************************
   Get the last position of the path.
 ****************************************************************************/

Modified: trunk/common/aicore/path_finding.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/aicore/path_finding.h?rev=30031&r1=30030&r2=30031&view=diff
==============================================================================
--- trunk/common/aicore/path_finding.h  (original)
+++ trunk/common/aicore/path_finding.h  Thu Oct  8 17:42:50 2015
@@ -494,6 +494,7 @@
 struct pf_path *pf_path_concat(struct pf_path *dest_path,
                                const struct pf_path *src_path);
 bool pf_path_advance(struct pf_path *path, struct tile *ptile);
+bool pf_path_backtrack(struct pf_path *path, struct tile *ptile);
 const struct pf_position *pf_path_last_position(const struct pf_path *path);
 void pf_path_print_real(const struct pf_path *path, enum log_level level,
                         const char *file, const char *function, int line);


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to