Route away from the "danger area" !!

Wrote new code? Fixed a bug? Want to discuss technical stuff? Feel free to post it here.

Moderator: Moderators

sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Route away from the "danger area" !!

#1 Post by sofax222 »

I try to add a algorithm into the routing process for avoiding the "danger area".
If I have know what is the "danger area" (the coordinate with a radius), how do I to eliminate these area from the routing process ?

I have traced some program...

src/Task/Route.pm
src/Utils/PathFinding.pm
src/auto/XSTools/PathFinding/algorithm.cpp

Am I right ?

Then, I have two questions:
1. How to pass the parameters (the coordinate with a radius) from Perl to C++ ?
(I guess, from Route::getRoute to PathFinding...)
2. Where do I add the codes to to eliminate the "danger area" from the routing process ?
(I guess, some where in src/auto/XSTools/PathFinding/algorithm.cpp)

Could give me some help !?
EternalHarvest
Developers
Developers
Posts: 1798
Joined: 05 Dec 2008, 05:42
Noob?: Yes

Re: Route away from the "danger area" !!

#2 Post by EternalHarvest »

Maybe distance map can be changed for this. It's used for avoiding walls currently.
sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Re: Route away from the "danger area" !!

#3 Post by sofax222 »

Do you mean to change the .dist file in Fields folder ?
But the "danger area" what I said is dynamics !

Actually, I want to mark the position(coordinate) where the Boss Monster appears,
and to set the (x - r, y - r) to (x + r, y + r) to be the "danger area".
Then, eliminating this "danger area" in routing process.
Changing the "danger area" when next meeting the Boss Monster !
sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Re: Route away from the "danger area" !!

#4 Post by sofax222 »

Another Question:

The CalcPath_pathStep function in src/auto/XSTools/PathFinding/algorithm.cpp, is it only calculating the path steps in the same field ?
EternalHarvest
Developers
Developers
Posts: 1798
Joined: 05 Dec 2008, 05:42
Noob?: Yes

Re: Route away from the "danger area" !!

#5 Post by EternalHarvest »

For basic setup, you can change $field->{dstMap}.
You can do this by changing loading/generation functions in Field module, then use something like

Code: Select all

$field->loadByName($args{field}->name, 1);
to refresh already loaded $field when your data changes, and you'll need to somehow tell existing routing tasks to refresh or clear AI.

However, this way is not very flexible afaik (many things you may want to change are still inside cpp functions) and the whole "temporarily avoid parts of maps in all possible routing" idea has many potential problems which would arise if implemented poorly.

If you just want to avoid danger while you're randomwalking on the map as usual, maybe it's easier to tweak random walking? Like, check whether any of route points are close to danger areas when a new route is generated. If so, generate another route immediately.
sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Re: Route away from the "danger area" !!

#6 Post by sofax222 »

Thank you very much !
Actually, I have completed this job !!

I descript simply as following:

1. I pass a coordnate and a radus (danger area) parametr into $class->_reset(..)
from reset of src/Utils/PathFinding.pm.
Such as:

Code: Select all

	return $class->_reset($args{distance_map}, $args{weights}, $args{width}, $args{height},
		$args{start}{x}, $args{start}{y},
		$args{dest}{x}, $args{dest}{y},
		$args{timeout}, $dgrx, $dgry, $dgrr);
The $dgrx and $dgry are the coordnate and the $dgrr is the radus.

2. In src/auto/XSTools/PathFinding, get the coordnate and the radus with PathFinding__reset(...) of PathFinding.xs.
Then new a pos *dgrpos and unsigned short dgrr, and pass them into CalcPath_init (...)
Such as:

Code: Select all

PathFinding__reset(session, map, weights, width, height, startx, starty, destx, desty, time_max, dgrx, dgry, dgrr)
.....
CalcPath_init (session, real_map, real_weights, width, height, start, dest, time_max, dgrpos, dgrr);
3. In src/auto/XSTools/PathFinding, get the pos and the radus with CalcPath_init (...) of algorithm.cpp
Such as:

Code: Select all

CalcPath_init (CalcPath_session *session, const char* map, const unsigned char* weight,
	unsigned long width, unsigned long height,
	pos * start, pos * dest, unsigned long time_max, pos * dgrpos, unsigned short dgrrds)
And now, the pos * dgrpos unsigned short dgrrds is the danger area !!

4. In src/auto/XSTools/PathFinding/algorithm.cpp, convert the pos and the radus into a rectangle area.
Such as:

Code: Select all

	unsigned short dgrL = (dgrpos->x > dgrrds) ? dgrpos->x - dgrrds : 0 ;
	unsigned short dgrR = dgrpos->x + dgrrds;
	unsigned short dgrT = dgrpos->y + dgrrds;
	unsigned short dgrB = (dgrpos->y > dgrrds) ? dgrpos->y - dgrrds : 0;
5. In src/auto/XSTools/PathFinding/algorithm.cpp, add a new function, IsSafe().
Such as:

Code: Select all

static inline char
IsSafe(pos *p, unsigned short l, unsigned short r, unsigned short t, unsigned short b) {
	if (p->x >= l && p->x <= r && p->y >= b && p->y <= t) {
		return 0;
	} else {
		return 1;
	}
}
6. In src/auto/XSTools/PathFinding/algorithm.cpp, add a condition into the judgement statement.
Such as:

Code: Select all

		if (IsSafe(&mappos, dgrL, dgrR, dgrT, dgrB) != 0
			&& CalcPath_getMap(map, width, height, &mappos) != 0
.....
I am testing this now.
I think I will not commit these codes !
sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Re: Route away from the "danger area" !!

#7 Post by sofax222 »

After some testing...
It seems to works well !
When I set some one coordinate with a radius...
It could route away from this "danger area" !!