Merge remote-tracking branch 'origin'
This commit is contained in:
@@ -21,11 +21,10 @@ struct navSearchState{
|
||||
navSearchState initState(U32 start, U32 meshSize) {
|
||||
navSearchState state = {};
|
||||
for(U32 i = 0; i < meshSize; i++) {
|
||||
state.nodeStates[i].visited = false;
|
||||
state.nodeStates[i].visited = false;
|
||||
state.nodeStates[i].addedToUnvisited = false;
|
||||
// underflow to the max :)
|
||||
state.nodeStates[i].distance = U64_MAX;
|
||||
state.nodeStates[i].shortest = 0;
|
||||
state.nodeStates[i].distance = U64_MAX;
|
||||
state.nodeStates[i].shortest = 0;
|
||||
}
|
||||
state.nodeStates[start].distance = 0;
|
||||
return state;
|
||||
@@ -53,8 +52,8 @@ U32 getLowestState(U32 unfinishedIndexes[128], U32 unfinishedCount, navSearchSta
|
||||
}
|
||||
|
||||
// Generate a path to follow between the start and end node.
|
||||
NavPath Nav_Path(NavMesh mesh, U32 start, U32 end) {
|
||||
navSearchState state = initState(start, mesh.nodeCount);
|
||||
NavPath Nav_Path(NavMesh *mesh, U32 start, U32 end) {
|
||||
navSearchState state = initState(start, mesh->nodeCount);
|
||||
U32 unfinishedCount = 1;
|
||||
U32 unfinishedIndexes[NAV_MAX_NODES] = {start};
|
||||
// I don't want to spend time removing items from
|
||||
@@ -65,19 +64,19 @@ NavPath Nav_Path(NavMesh mesh, U32 start, U32 end) {
|
||||
U32 lowestNodeIndex = start;
|
||||
bool found = false;
|
||||
while(!found) {
|
||||
for(int connectionI = 0 ; connectionI < mesh.nodes[lowestNodeIndex].connectionCount; connectionI++) {
|
||||
NavConnection connection = mesh.nodes[lowestNodeIndex].connections[connectionI];
|
||||
navSearchNodeState *testNode = &state.nodeStates[connection.NodeIndex];
|
||||
for(int connectionI = 0 ; connectionI < mesh->nodes[lowestNodeIndex].connectionCount; connectionI++) {
|
||||
NavConnection *connection = &mesh->nodes[lowestNodeIndex].connections[connectionI];
|
||||
navSearchNodeState *testNode = &state.nodeStates[connection->NodeIndex];
|
||||
if(testNode->visited) {continue;}
|
||||
U32 distance = cast(U32) (state.nodeStates[lowestNodeIndex].distance + connection.Cost);
|
||||
distance += cast(U32) (mesh.nodes[end].pos.x - mesh.nodes[connection.NodeIndex].pos.x);
|
||||
distance += cast(U32) (mesh.nodes[end].pos.y - mesh.nodes[connection.NodeIndex].pos.y);
|
||||
U32 distance = cast(U32) (state.nodeStates[lowestNodeIndex].distance + connection->Cost);
|
||||
distance += cast(U32) (mesh->nodes[end].pos.x - mesh->nodes[connection->NodeIndex].pos.x);
|
||||
distance += cast(U32) (mesh->nodes[end].pos.y - mesh->nodes[connection->NodeIndex].pos.y);
|
||||
if(testNode->distance > distance) {
|
||||
testNode->distance = distance;
|
||||
testNode->shortest = lowestNodeIndex;
|
||||
}
|
||||
if(!testNode->addedToUnvisited) {
|
||||
unfinishedIndexes[unfinishedCount] = connection.NodeIndex;
|
||||
unfinishedIndexes[unfinishedCount] = connection->NodeIndex;
|
||||
unfinishedCount++;
|
||||
testNode->addedToUnvisited = true;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "../npc.h"
|
||||
#include "../world.h"
|
||||
#include "../../core/types.h"
|
||||
#include "game/npc.h"
|
||||
#include "game/world.h"
|
||||
#include "core/types.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void updateNPC(F32 delta, NPC *npc, World *world) {
|
||||
@@ -15,7 +16,7 @@ void updateNPC(F32 delta, NPC *npc, World *world) {
|
||||
npc->path = Nav_Path(world->navMesh, npc->currentNavNode, npc->targetNavNode);
|
||||
printf("done\n");
|
||||
npc->walkTimer = 0;
|
||||
printf("%*.s started walking to %d\n", Sv(npc->name), npc->targetNavNode);
|
||||
printf("%.*s started walking to %d\n", Sv(npc->name), npc->targetNavNode);
|
||||
}
|
||||
break;
|
||||
case NPC_ACTION_WALKING:
|
||||
@@ -33,11 +34,10 @@ void updateNPC(F32 delta, NPC *npc, World *world) {
|
||||
npc->pathIndex+=1;
|
||||
npc->currentNavNode = npc->path.indexes[npc->pathIndex];
|
||||
}
|
||||
NavNode cNav = world->navMesh.nodes[npc->currentNavNode];
|
||||
NavNode tNav = world->navMesh.nodes[npc->pathIndex];
|
||||
NavNode cNav = world->navMesh->nodes[npc->currentNavNode];
|
||||
NavNode tNav = world->navMesh->nodes[npc->pathIndex];
|
||||
npc->collision.pos.x = cNav.pos.x * (1 - npc->walkTimer/NPC_SPEED) + tNav.pos.x * npc->walkTimer/NPC_SPEED;
|
||||
npc->collision.pos.y = cNav.pos.y * (1 - npc->walkTimer/NPC_SPEED) + tNav.pos.y * npc->walkTimer/NPC_SPEED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user