random wandering function

This commit is contained in:
Andrei Alexeyev 2019-09-02 19:09:09 +03:00
parent 10f0ffe814
commit 1ddea7ab97
No known key found for this signature in database
GPG key ID: 363707CD4C7FE8A4
3 changed files with 60 additions and 1 deletions

View file

@ -9,6 +9,7 @@
#include "taisei.h"
#include "common_tasks.h"
#include "random.h"
DEFINE_EXTERN_TASK(common_drop_items) {
complex p = *ARGS.pos;
@ -44,3 +45,41 @@ DEFINE_EXTERN_TASK(common_move_ext) {
common_move_loop(ARGS.pos, ARGS.move_params);
}
complex common_wander(complex origin, double dist, Rect bounds) {
int attempts = 32;
double angle;
complex dest;
complex dir;
// assert(point_in_rect(origin, bounds));
while(attempts--) {
angle = rand_angle();
dir = cdir(angle);
dest = origin + dist * dir;
if(point_in_rect(dest, bounds)) {
return dest;
}
}
log_warn("Clipping fallback origin = %f%+fi dist = %f bounds.top_left = %f%+fi bounds.bottom_right = %f%+fi",
creal(origin), cimag(origin),
dist,
creal(bounds.top_left), cimag(bounds.top_left),
creal(bounds.bottom_right), cimag(bounds.bottom_right)
);
// TODO: implement proper line-clipping here?
double step = cabs(bounds.bottom_right - bounds.top_left) / 16;
dir *= step;
dest = origin;
while(point_in_rect(dest + dir, bounds)) {
dest += dir;
}
return dest;
}

View file

@ -33,4 +33,13 @@ DECLARE_EXTERN_TASK(
void common_move_loop(complex *restrict pos, MoveParams *restrict mp);
INLINE Rect viewport_bounds(double margin) {
return (Rect) {
.top_left = CMPLX(margin, margin),
.bottom_right = CMPLX(VIEWPORT_W - margin, VIEWPORT_H - margin),
};
}
complex common_wander(complex origin, double dist, Rect bounds);
#endif // IGUARD_common_tasks_h

View file

@ -1720,7 +1720,18 @@ DEFINE_EXTERN_TASK(stage1_spell_perfect_freeze) {
WAIT_EVENT(&a->events.started);
// TODO implement
(void)boss;
boss->move.retention = 0.8;
boss->move.attraction = 0.01;
Rect move_bounds;
move_bounds.top_left = CMPLX(64, 64);
move_bounds.bottom_right = CMPLX(VIEWPORT_W - 64, 200);
for(;;) {
boss->move.attraction_point = common_wander(boss->pos, rand_range(50, 200), move_bounds);
WAIT(60);
}
}
TASK_WITH_INTERFACE(midboss_flee, BossAttack) {