Files
WT-Offline/native/wto-nav/wto_nav_protocol.c
T

248 lines
8.1 KiB
C

/* The "wtoNav" request protocol on top of the navigation engine (see wto_nav_protocol.h). */
#include "wto_nav_protocol.h"
#include "wto_nav_runtime.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Costs in the order of WtoPolyArea; empty names are areas the mesh never carries. */
static const char *const AREA_KEYS[WTO_NAV_RT_AREA_COUNT] = {
"", "road", "bridge", "ground", "rough", "forest", "crushable", "shallowWater", "", "exposed", "", "",
};
static float get_real(const WtoNavBlock *b, const char *name, float def)
{
const char *text = b->get_str(b->blk, name);
char *end;
float value;
if (text == NULL || text[0] == '\0')
return def;
value = strtof(text, &end);
return (*end == '\0' && value == value) ? value : def;
}
static long get_long(const WtoNavBlock *b, const char *name, long def)
{
const char *text = b->get_str(b->blk, name);
char *end;
long value;
if (text == NULL || text[0] == '\0')
return def;
value = strtol(text, &end, 10);
return *end == '\0' ? value : def;
}
/* <prefix>x, <prefix>y, <prefix>z */
static void get_vec(const WtoNavBlock *b, char prefix, float out[3])
{
char key[3];
key[0] = prefix;
key[1] = 'x';
key[2] = '\0';
out[0] = get_real(b, key, 0.0f);
key[1] = 'y';
out[1] = get_real(b, key, 0.0f);
key[1] = 'z';
out[2] = get_real(b, key, 0.0f);
}
static void set_int(const WtoNavBlock *b, const char *name, int32_t value)
{
b->set_int(b->blk, name, value);
}
static int32_t to_cm(float metres)
{
const float cm = metres * 100.0f;
if (cm > 2.0e9f)
return INT32_MAX;
if (cm < -2.0e9f)
return INT32_MIN;
return (int32_t)(cm >= 0.0f ? cm + 0.5f : cm - 0.5f);
}
static void set_vec_cm(const WtoNavBlock *b, const char *x, const char *y, const char *z, const float v[3])
{
set_int(b, x, to_cm(v[0]));
set_int(b, y, to_cm(v[1]));
set_int(b, z, to_cm(v[2]));
}
static void read_costs(const WtoNavBlock *b, const char *prefix, WtoNavCosts *costs)
{
char key[48];
int area;
for (area = 0; area < WTO_NAV_RT_AREA_COUNT; area++) {
costs->area[area] = 1.0f;
if (AREA_KEYS[area][0] == '\0')
continue;
snprintf(key, sizeof(key), "%s_%s", prefix, AREA_KEYS[area]);
costs->area[area] = get_real(b, key, 1.0f);
if (!(costs->area[area] > 0.0f))
costs->area[area] = 1.0f;
}
snprintf(key, sizeof(key), "%s_chokepoint", prefix);
costs->chokepointMul = get_real(b, key, 1.0f);
if (!(costs->chokepointMul > 0.0f))
costs->chokepointMul = 1.0f;
}
void wto_nav_protocol_setup(const WtoNavBlock *b)
{
WtoNavCosts costs;
WtoNavTuning tuning;
read_costs(b, "t", &costs);
wto_nav_set_costs(0, &costs);
read_costs(b, "w", &costs);
wto_nav_set_costs(1, &costs);
tuning.trafficCostPerPath = get_real(b, "trafficPerPath", 0.0f);
tuning.trafficCostMaxMul = get_real(b, "trafficMaxMul", 1.0f);
tuning.trafficHoldSec = get_real(b, "trafficHoldSec", 0.0f);
tuning.cornerOffsetM = get_real(b, "cornerOffset", 0.0f);
wto_nav_set_tuning(&tuning);
}
static void path_request(const WtoNavBlock *b)
{
WtoNavPathRequest req;
const long slot = get_long(b, "slot", -1), tier = get_long(b, "tier", -1), filter = get_long(b, "filter", 0);
const long group = get_long(b, "group", 0);
uint32_t ticket = 0;
memset(&req, 0, sizeof(req));
req.tier = (uint32_t)tier;
req.filter = (uint32_t)filter;
req.group = group > 0 ? (uint32_t)group : 0u;
req.snapM = get_real(b, "snap", 0.0f);
req.goalSnapM = get_real(b, "goalSnap", 0.0f);
get_vec(b, 's', req.start);
get_vec(b, 'g', req.goal);
if (slot >= 0 && tier >= 0 && filter >= 0)
ticket = wto_nav_path_request((uint32_t)slot, &req);
set_int(b, "ticket", (int32_t)ticket);
}
static void path_result(const WtoNavBlock *b)
{
static const char *const STATUS[] = { "full", "partial", "no_start", "no_goal", "failed", "pending", "stale" };
float points[WTO_NAV_RT_MAX_CORNERS * 3];
const long slot = get_long(b, "slot", -1), ticket = get_long(b, "ticket", 0);
WtoNavPathResult result;
int i;
wto_nav_path_result(slot < 0 ? UINT32_MAX : (uint32_t)slot, ticket <= 0 ? 0u : (uint32_t)ticket,
points, WTO_NAV_RT_MAX_CORNERS, &result);
b->set_str(b->blk, "status", STATUS[result.status]);
set_int(b, "count", result.count);
set_int(b, "lenCm", to_cm(result.lengthM));
set_int(b, "truncated", result.truncated);
set_int(b, "goalMoved", result.goalMoved);
for (i = 0; i < result.count; i++) {
char kx[16], ky[16], kz[16];
snprintf(kx, sizeof(kx), "p%dx", i);
snprintf(ky, sizeof(ky), "p%dy", i);
snprintf(kz, sizeof(kz), "p%dz", i);
set_vec_cm(b, kx, ky, kz, points + i * 3);
}
}
static void probe(const WtoNavBlock *b)
{
WtoNavProbe info;
float pos[3];
get_vec(b, 'p', pos);
if (!wto_nav_probe((uint32_t)get_long(b, "tier", 0), (uint32_t)get_long(b, "filter", 0), pos,
get_real(b, "snap", 0.0f), get_real(b, "wallR", 0.0f), &info))
info.onMesh = -1;
set_int(b, "onMesh", info.onMesh);
if (info.onMesh != 1)
return;
set_int(b, "offCm", to_cm(info.offsetM));
set_int(b, "wallCm", to_cm(info.wallDistM));
set_int(b, "area", (int32_t)info.area);
set_int(b, "flags", (int32_t)info.flags);
set_vec_cm(b, "nx", "ny", "nz", info.nearest);
}
static void raycast(const WtoNavBlock *b)
{
float start[3], goal[3], hit[3], frac = 0.0f;
int clear;
get_vec(b, 's', start);
get_vec(b, 'g', goal);
clear = wto_nav_raycast((uint32_t)get_long(b, "tier", 0), (uint32_t)get_long(b, "filter", 0),
get_real(b, "snap", 0.0f), start, goal, &frac, hit);
set_int(b, "clear", clear);
set_int(b, "hitPm", (int32_t)(frac * 1000.0f + 0.5f));
set_vec_cm(b, "hx", "hy", "hz", hit);
}
static void nearest(const WtoNavBlock *b)
{
float pos[3], out[3] = { 0.0f, 0.0f, 0.0f };
int found;
get_vec(b, 'p', pos);
found = wto_nav_nearest((uint32_t)get_long(b, "tier", 0), (uint32_t)get_long(b, "filter", 0),
get_real(b, "radius", 0.0f), pos, out);
set_int(b, "found", found);
set_vec_cm(b, "x", "y", "z", out);
}
static void add_dynamic(const WtoNavBlock *b)
{
float pos[3];
get_vec(b, 'p', pos);
set_int(b, "count", (int32_t)wto_nav_add_dynamic(pos, get_real(b, "radius", 0.0f), get_real(b, "mul", 1.0f),
get_real(b, "until", 0.0f)));
}
static void get_stats(const WtoNavBlock *b)
{
WtoNavStats stats;
wto_nav_get_stats(&stats);
set_int(b, "loaded", stats.loaded);
set_int(b, "queries", (int32_t)stats.queries);
set_int(b, "failures", (int32_t)stats.failures);
set_int(b, "avgUs", stats.queries ? (int32_t)(stats.totalMicros / stats.queries) : 0);
set_int(b, "maxUs", (int32_t)stats.maxMicros);
set_int(b, "maxFrames", (int32_t)stats.maxFrames);
set_int(b, "pumpMaxUs", (int32_t)stats.pumpMaxMicros);
set_int(b, "pending", (int32_t)stats.pending);
set_int(b, "dynamic", (int32_t)stats.dynamic);
set_int(b, "meshQueries", (int32_t)stats.meshQueries);
set_int(b, "traffic", (int32_t)stats.trafficPaths);
set_int(b, "cornersMoved", (int32_t)stats.cornersMoved);
}
static void pump(const WtoNavBlock *b)
{
WtoNavStats stats;
wto_nav_pump((double)get_real(b, "budgetUs", 500.0f), get_real(b, "now", 0.0f));
wto_nav_get_stats(&stats);
set_int(b, "pending", (int32_t)stats.pending);
}
int wto_nav_protocol_query(const WtoNavBlock *b, const char *op)
{
if (strcmp(op, "probe") == 0)
probe(b);
else if (strcmp(op, "path_result") == 0)
path_result(b);
else if (strcmp(op, "path_request") == 0)
path_request(b);
else if (strcmp(op, "raycast") == 0)
raycast(b);
else if (strcmp(op, "pump") == 0)
pump(b);
else if (strcmp(op, "nearest") == 0)
nearest(b);
else if (strcmp(op, "dynamic") == 0)
add_dynamic(b);
else if (strcmp(op, "get_stats") == 0)
get_stats(b);
else
return 0;
return 1;
}