134 lines
3.0 KiB
C++
134 lines
3.0 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Einige Aenderungen beigetragen von Toni Fetzer
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef NAVMESHWALKSIMPLE_H
|
||
#define NAVMESHWALKSIMPLE_H
|
||
|
||
#include "../NavMesh.h"
|
||
#include "../NavMeshLocation.h"
|
||
#include "../../geo/Heading.h"
|
||
|
||
#include "NavMeshSub.h"
|
||
#include "NavMeshWalkParams.h"
|
||
#include "NavMeshWalkEval.h"
|
||
|
||
namespace NM {
|
||
|
||
template <typename Tria> class NavMeshWalkSimple {
|
||
|
||
private:
|
||
|
||
const NavMesh<Tria>& mesh;
|
||
|
||
std::vector<NavMeshWalkEval<Tria>*> evals;
|
||
|
||
int hits = 0;
|
||
int misses = 0;
|
||
|
||
public:
|
||
|
||
|
||
/** single result */
|
||
struct ResultEntry {
|
||
NavMeshLocation<Tria> location;
|
||
Heading heading;
|
||
double probability;
|
||
ResultEntry() : heading(0) {;}
|
||
};
|
||
|
||
/** list of results */
|
||
using ResultList = std::vector<ResultEntry>;
|
||
|
||
public:
|
||
|
||
/** ctor */
|
||
NavMeshWalkSimple(const NavMesh<Tria>& mesh) : mesh(mesh) {
|
||
|
||
}
|
||
|
||
/** add a new evaluator to the walker */
|
||
void addEvaluator(NavMeshWalkEval<Tria>* eval) {
|
||
this->evals.push_back(eval);
|
||
}
|
||
|
||
|
||
|
||
ResultEntry getOne(const NavMeshWalkParams<Tria>& params) {
|
||
|
||
// sanity checks
|
||
params.check();
|
||
|
||
ResultEntry re;
|
||
|
||
// to-be-walked distance;
|
||
const float toBeWalkedDist = params.getToBeWalkedDistance();
|
||
const float toBeWalkedDistSafe = 0.75 + toBeWalkedDist * 1.1;
|
||
|
||
// construct reachable region
|
||
NavMeshSub<Tria> reachable(params.start, toBeWalkedDistSafe);
|
||
|
||
// get the to-be-reached destination's position (using start+distance+heading)
|
||
const Point2 dir = params.heading.asVector();
|
||
const Point2 dst = params.start.pos.xy() + (dir * toBeWalkedDist);
|
||
|
||
const Tria* dstTria = reachable.getContainingTriangle(dst);
|
||
|
||
// is above destination reachable?
|
||
if (dstTria) {
|
||
|
||
re.heading = params.heading; // heading was OK -> keep
|
||
re.location.pos = dstTria->toPoint3(dst); // new destination position
|
||
re.location.tria = dstTria; // new destination triangle
|
||
++hits;
|
||
|
||
} else {
|
||
|
||
NavMeshRandom<Tria> rnd = reachable.getRandom(); // random-helper
|
||
re.location = rnd.draw(); // get a random destianation
|
||
re.heading = Heading(params.start.pos.xy(), re.location.pos.xy()); // update the heading
|
||
++misses;
|
||
|
||
}
|
||
|
||
const int total = (hits + misses);
|
||
if (total % 10000 == 0) {
|
||
//std::cout << "hits: " << (hits*100/total) << "%" << std::endl;
|
||
}
|
||
|
||
// calculate probability
|
||
const NavMeshPotentialWalk<Tria> pwalk(params, re.location);
|
||
re.probability = 1.0;
|
||
for (const NavMeshWalkEval<Tria>* eval : evals) {
|
||
const double p1 = eval->getProbability(pwalk);
|
||
re.probability *= p1;
|
||
}
|
||
|
||
// done
|
||
return re;
|
||
|
||
}
|
||
|
||
ResultList getMany(const NavMeshWalkParams<Tria>& params) {
|
||
|
||
// sanity checks
|
||
params.check();
|
||
|
||
return {getOne(params)};
|
||
|
||
}
|
||
|
||
|
||
};
|
||
|
||
}
|
||
|
||
#endif // NAVMESHWALKSIMPLE_H
|