Skip to content

Commit

Permalink
Check elevation, correct collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
J2M2 committed Feb 10, 2021
1 parent f4d50c3 commit 5bc9045
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/event_object_movement.h
Expand Up @@ -419,5 +419,6 @@ bool32 IsObjectEventSpriteInvisible(u8 var);
void SetObjectEventSpriteGraphics(u8 var1, u8 graphicsId);
void SetObjectEventSpriteAnim(u8 var1, u8 var2);
bool32 IsObjectEventSpriteAnimating(u8 var);
u8 CheckCollisionAtCoords(struct ObjectEvent *, s16, s16, u32, u8);

#endif //GUARD_EVENT_OBJECT_MOVEMENT_H
15 changes: 13 additions & 2 deletions include/movement_path_planning.h
Expand Up @@ -28,8 +28,8 @@ static const u32 moves_walk[] =
static EWRAM_DATA u8 *SolutionPath = NULL;


#define MAXPATH 100 // Max final path size
#define MAXNODES 500 //Max size of the queues
#define MAXPATH 200 // Max final path size
#define MAXNODES 800 //Max size of the queues

#define HEUR_WEIGHT 1
//Weight of the Heuristic, 0 for Dijkstra Algoritmin (Uniform Cost Search), 1 for A*, and >>1 for Greedy Best First Search
Expand All @@ -38,6 +38,7 @@ typedef struct Node{
int state;
struct Coords16 coords;
int cost;
u8 currentElevation;
int path[MAXPATH];
} Node;

Expand Down Expand Up @@ -168,6 +169,15 @@ struct Node pop(struct PriorityQueue *queue, int i) {
return element;
}

u8 getElevation(u8 prevZ, s16 x, s16 y){
u8 mapZ;
mapZ = MapGridGetZCoordAt(x+7, y+7);
if (mapZ == 0xF)
return prevZ;
else
return mapZ;
}

void getChild(struct Node parent, int move, struct Node *child){
int i;
child->coords.x = parent.coords.x + moves[move].x;
Expand All @@ -176,6 +186,7 @@ void getChild(struct Node parent, int move, struct Node *child){
for(i =0;i<parent.cost;i++)
child->path[i] = parent.path[i];
child->path[parent.cost] = move;
child->currentElevation = getElevation(parent.currentElevation,parent.coords.x,parent.coords.y);
}

void getSolution(struct Node *node)
Expand Down
34 changes: 34 additions & 0 deletions src/event_object_movement.c
Expand Up @@ -134,6 +134,7 @@ static void UpdateObjectEventSpriteSubpriorityAndVisibility(struct Sprite *);
static void InitSpriteForFigure8Anim(struct Sprite *sprite);
static bool8 AnimateSpriteInFigure8(struct Sprite *sprite);
static void UpdateObjectEventSprite(struct Sprite *);
static bool8 DoesObjectCollideWithObjectAtZ(struct ObjectEvent *, s16, s16);

const u8 gReflectionEffectPaletteMap[] = {1, 1, 6, 7, 8, 9, 6, 7, 8, 9, 11, 11, 0, 0, 0, 0};

Expand Down Expand Up @@ -9020,3 +9021,36 @@ u8 MovementAction_Fly_Finish(struct ObjectEvent *objectEvent, struct Sprite *spr
{
return TRUE;
}

u8 CheckCollisionAtCoords(struct ObjectEvent *objectEvent, s16 x, s16 y, u32 dir, u8 currentElevation)
{
u8 direction = dir;
if (MapGridIsImpassableAt(x, y) || GetMapBorderIdAt(x, y) == -1 || IsMetatileDirectionallyImpassable(objectEvent, x, y, direction))
return COLLISION_IMPASSABLE;
else if (objectEvent->trackedByCamera && !CanCameraMoveInDirection(direction))
return COLLISION_IMPASSABLE;
else if (IsZCoordMismatchAt(currentElevation, x, y))
return COLLISION_ELEVATION_MISMATCH;
else if (DoesObjectCollideWithObjectAtZ(objectEvent, x, y))
return COLLISION_OBJECT_EVENT;
return COLLISION_NONE;
}

static bool8 DoesObjectCollideWithObjectAtZ(struct ObjectEvent *objectEvent, s16 x, s16 y)
{
u8 i;
struct ObjectEvent *curObject;

for (i = 0; i < OBJECT_EVENTS_COUNT; i++)
{
curObject = &gObjectEvents[i];
if (curObject->active && curObject != objectEvent)
{
if ((curObject->currentCoords.x == x && curObject->currentCoords.y == y) || (curObject->previousCoords.x == x && curObject->previousCoords.y == y))
{
return TRUE;
}
}
}
return FALSE;
}
21 changes: 11 additions & 10 deletions src/movement_path_planning.c
Expand Up @@ -33,9 +33,9 @@ static const u32 MOVES_COUNT = 4;
// .text


int isMoveNotPossible(struct ObjectEvent *objectEvent, s16 x, s16 y, u32 dir)
int isMoveNotPossible(struct ObjectEvent *objectEvent, s16 x, s16 y, u32 dir, u8 currentElevation)
{
return GetCollisionAtCoords(objectEvent, x+7, y+7, dir+1);
return CheckCollisionAtCoords(objectEvent, x+7, y+7, dir+1, currentElevation);
}

// Driver code
Expand All @@ -48,8 +48,8 @@ int searchPath(struct ObjectEvent *objectEvent,s32 x, s32 y, s32 facing){
Node node;
u32 MapWidth = GetMapLayout()->width;

u16 rangeX = objectEvent->rangeX;
u16 rangeY = objectEvent->rangeY;
// u16 rangeX = objectEvent->rangeX;
// u16 rangeY = objectEvent->rangeY;

int failure;
int i;
Expand All @@ -60,13 +60,14 @@ int searchPath(struct ObjectEvent *objectEvent,s32 x, s32 y, s32 facing){

failure = 0;

objectEvent->rangeX = 0;
objectEvent->rangeY = 0;
// objectEvent->rangeX = 0;
// objectEvent->rangeY = 0;

startNode.coords.x = objectEvent->currentCoords.x -7;
startNode.coords.y = objectEvent->currentCoords.y -7;
startNode.state = startNode.coords.x + MapWidth * startNode.coords.y;
startNode.cost = 0;
startNode.currentElevation = getElevation(objectEvent->currentElevation,startNode.coords.x,startNode.coords.y);

insert(&frontier, startNode,CalcHeuristic(&child,x,y));

Expand All @@ -78,16 +79,16 @@ int searchPath(struct ObjectEvent *objectEvent,s32 x, s32 y, s32 facing){

if(node.coords.x == x && node.coords.y == y){ // SUCCESS
getSolution(&node);
objectEvent->rangeX = rangeX;
objectEvent->rangeY = rangeY;
// objectEvent->rangeX = rangeX;
// objectEvent->rangeY = rangeY;
return 1;
}
setInsert(&explored,node.state);

if(node.cost<MAXPATH){
for(i=0; i < MOVES_COUNT;i++){
if(node.cost == 0 || !(i+node.path[node.cost-1] == 1 || i+node.path[node.cost-1] == 5)){
if (!isMoveNotPossible(objectEvent,node.coords.x + moves[i].x, node.coords.y + moves[i].y,i))
if (!isMoveNotPossible(objectEvent,node.coords.x + moves[i].x, node.coords.y + moves[i].y,i,node.currentElevation))
{
getChild(node,i,&child);
childState = child.coords.x + MapWidth * child.coords.y;
Expand All @@ -96,7 +97,7 @@ int searchPath(struct ObjectEvent *objectEvent,s32 x, s32 y, s32 facing){
if(child.coords.x == x && child.coords.y == y){ // CHECK FACING
if(i != facing){ //Add facing move
child.path[child.cost] = facing + MOVES_COUNT;
child.cost ++;
child.cost = child.cost + 1;
}
}

Expand Down

0 comments on commit 5bc9045

Please sign in to comment.