shithub: asif

Download patch

ref: 7f82ae85a25733351d0d89f294a23f3afc6593eb
parent: a260fe0b8eb106f6c9cd8dd7c9b1c63d8cef8c1f
author: qwx <qwx@sciops.net>
date: Sat Mar 26 05:38:29 EDT 2022

path/bfs: update for new vec api and same improvements

--- a/path/bfs.c
+++ b/path/bfs.c
@@ -27,9 +27,8 @@
 {
 	static Node *suc[8+1];
 	static dtab[2*(nelem(suc)-1)]={
-		-1,-1, 0,-1, 1,-1,
-		-1,0, 1,0,
-		-1,1, 0,1, 1,1,
+		0,-1, 1,0, 0,1, -1,0,
+		1,-1, 1,1, -1,1, -1,-1,
 	};
 	int i;
 	Node *s, **np;
@@ -83,16 +82,16 @@
 static Node *
 bfs(Node *a, Node *b)
 {
-	Vector v;
+	Vector *v;
 	Node *x, *s, **sl;
 
 	assert(a != nil && b != nil);
 	assert(a != b);
+	v = vec(sizeof a);
 	x = a;
-	newvec(&v, manhdist(a, b), sizeof a);
-	pushvec(&v, a);
-	while(v.n > 0){
-		x = popvec(&v);
+	vecpush(v, &x);
+	while(v->len > 0){
+		x = *((Node **)vechpop(v));
 		if(x == b)
 			break;
 		x->closed = 1;
@@ -99,14 +98,14 @@
 		if((sl = successorfn(x)) == nil)
 			sysfatal("bfs: %r");
 		for(s=*sl++; s!=nil; s=*sl++){
-			if(s->closed || s->open)
+			if(s->open)
 				continue;
 			s->from = x;
-			pushvec(&v, s);
+			vecpush(v, &s);
 			s->open = 1;
 		}
 	}
-	freevec(&v);
+	vecfree(v);
 	return x;
 }
 
@@ -113,11 +112,15 @@
 static int
 findpath(void)
 {
-	if(start == nil || goal == nil)
+	if(start == nil || goal == nil){
+		werrstr("findpath: start and/or goal undefined");
 		return -1;
+	}
 	resetmap();
-	if(bfs(start, goal) != goal)
+	if(bfs(start, goal) != goal){
+		werrstr("findpath: failed");
 		return -1;
+	}
 	backtrack();
 	return 0;
 }
@@ -126,8 +129,8 @@
 mouseinput(Node *n, Mouse m)
 {
 	switch(m.buttons & 7){
-	case 1: if(start != n) goal = n; return findpath();
-	case 2: if(goal != n) start = n; return findpath();
+	case 1: if(goal != n) start = n; return findpath();
+	case 2: if(start != n) goal = n; return findpath();
 	case 4: n->blocked ^= 1; break;
 	}
 	return 0;