Skip to content

Commit

Permalink
Create command
Browse files Browse the repository at this point in the history
  • Loading branch information
J2M2 committed Feb 10, 2021
1 parent 8747f91 commit adca773
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
22 changes: 14 additions & 8 deletions include/movement_path_planning.h
Expand Up @@ -17,15 +17,19 @@ static const u32 moves_walk[] =
MOVEMENT_ACTION_WALK_NORMAL_DOWN,
MOVEMENT_ACTION_WALK_NORMAL_UP,
MOVEMENT_ACTION_WALK_NORMAL_LEFT,
MOVEMENT_ACTION_WALK_NORMAL_RIGHT,
MOVEMENT_ACTION_WALK_NORMAL_RIGHT,
MOVEMENT_ACTION_FACE_DOWN,
MOVEMENT_ACTION_FACE_UP,
MOVEMENT_ACTION_FACE_LEFT,
MOVEMENT_ACTION_FACE_RIGHT,
};


static EWRAM_DATA u8 *SolutionPath = NULL;


#define MAXPATH 100 // Max final path size
#define MAXNODES 100 //Max size of the queues
#define MAXNODES 500 //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 @@ -44,7 +48,7 @@ typedef struct Set{

typedef struct PriorityQueue{
unsigned int size;
int priority[MAXNODES]; // PODEMOS REEMPLAZAR ESTAS LISTAS USANDO EL HEAP
float priority[MAXNODES]; // PODEMOS REEMPLAZAR ESTAS LISTAS USANDO EL HEAP
struct Node value[MAXNODES]; // PODEMOS REEMPLAZAR ESTAS LISTAS USANDO EL HEAP
} PriorityQueue;

Expand All @@ -54,21 +58,23 @@ void swapNode(struct Node *a, struct Node *b) {
*a = temp;
}

void swap(int *a, int *b) {
int temp = *b;
void swap(float *a, float *b) {
float temp = *b;
*b = *a;
*a = temp;
}

u32 abs(s32 x){
return (u32)((x<0)?-x:x);}

u32 L1Distance(s32 x0, s32 y0, s32 xf, s32 yf){ //MAnhatan distance used as heuristic
u32 L1Distance(s32 x0, s32 y0, s32 xf, s32 yf){ //Manhatan distance used as heuristic
return abs(x0 - xf) + abs(y0 - yf);}

u32 CalcHeuristic(struct Node *node, s32 xgoal,s32 ygoal)
float CalcHeuristic(struct Node *node, s32 xgoal,s32 ygoal)
{
return HEUR_WEIGHT*L1Distance(node->coords.x, node->coords.y, xgoal, ygoal);
float heuristic = HEUR_WEIGHT*L1Distance(node->coords.x, node->coords.y, xgoal, ygoal);
heuristic *= (1.0 + 1/MAXPATH); // For breaking ties
return heuristic;
}

// Function to insert to set
Expand Down
1 change: 1 addition & 0 deletions include/script_movement.h
Expand Up @@ -4,5 +4,6 @@
bool8 ScriptMovement_StartObjectMovementScript(u8 localId, u8 mapNum, u8 mapGroup, const u8 *movementScript);
bool8 ScriptMovement_IsObjectMovementFinished(u8 localId, u8 mapNum, u8 mapGroup);
void ScriptMovement_UnfreezeObjectEvents(void);
void MovementPathPlanning_MoveObjectToPos(u8 localId, u8 mapNum, u8 mapGroup, u8 x, u8 y, u8 facing);

#endif // GUARD_SCRIPT_MOVEMENT_H
26 changes: 12 additions & 14 deletions src/movement_path_planning.c
Expand Up @@ -27,16 +27,9 @@
u16 *InitalizeInd();
u32 abs();
u32 distance();
void MoveObjectToPos();

static const u32 MOVES_COUNT = 4;

// PARAMETROS DE ENTRADA DE LA FUNCION
static const u8 localId = 1;
static const s32 x_goal = 9;
static const s32 y_goal = 9;


// .text


Expand All @@ -46,7 +39,7 @@ int isMoveNotPossible(struct ObjectEvent *objectEvent, s16 x, s16 y, u32 dir)
}

// Driver code
int searchPath(struct ObjectEvent *objectEvent,s32 x, s32 y){
int searchPath(struct ObjectEvent *objectEvent,s32 x, s32 y, s32 facing){

Node startNode;
PriorityQueue frontier;
Expand Down Expand Up @@ -84,13 +77,20 @@ int searchPath(struct ObjectEvent *objectEvent,s32 x, s32 y){
setInsert(&explored,node.state);

if(node.cost<MAXPATH){
for(i=0; i<4;i++){
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))
{
getChild(node,i,&child);
childState = child.coords.x + MapWidth * child.coords.y;
child.state = childState;

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 ++;
}
}

if (!(isInSet(&explored,childState) || isInQueue(&frontier,childState)))
insert(&frontier,child,CalcHeuristic(&child,x,y));
Expand All @@ -106,21 +106,19 @@ int searchPath(struct ObjectEvent *objectEvent,s32 x, s32 y){
}
}

void MoveObjectToPos(void)
void MovementPathPlanning_MoveObjectToPos(u8 localId, u8 mapNum, u8 mapGroup, u8 x_goal, u8 y_goal, u8 facing)
{
u8 mapNum = gSaveBlock1Ptr->location.mapNum;
u8 mapGroup = gSaveBlock1Ptr->location.mapGroup;
// LoadCurrentMapData();
u8 objectEventId;

if (!TryGetObjectEventIdByLocalIdAndMap(localId, mapNum, mapGroup, &objectEventId))
{
if(searchPath(&gObjectEvents[objectEventId],x_goal,y_goal))
if(searchPath(&gObjectEvents[objectEventId],x_goal,y_goal,facing))
{
ScriptMovement_StartObjectMovementScript(localId, mapNum, mapGroup, SolutionPath);
SetMovingNpcId(localId);

FREE_AND_SET_NULL(SolutionPath);

}
}

Expand Down
26 changes: 26 additions & 0 deletions src/scrcmd.c
Expand Up @@ -2304,3 +2304,29 @@ bool8 ScrCmd_warpsootopolislegend(struct ScriptContext *ctx)
ResetInitialPlayerAvatarState();
return TRUE;
}

bool8 ScrCmd_MoveObjectToPos(struct ScriptContext *ctx)
{
u16 localId = VarGet(ScriptReadHalfword(ctx));
u16 posX = VarGet(ScriptReadHalfword(ctx));
u16 posY = VarGet(ScriptReadHalfword(ctx));
u16 facing = VarGet(ScriptReadHalfword(ctx));

MovementPathPlanning_MoveObjectToPos(localId, gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup, posX,posY,facing);
// sMovingNpcId = localId;
return FALSE;
}

bool8 ScrCmd_MoveObjectToPos_at(struct ScriptContext *ctx)
{
u16 localId = VarGet(ScriptReadHalfword(ctx));
u16 posX = VarGet(ScriptReadHalfword(ctx));
u16 posY = VarGet(ScriptReadHalfword(ctx));
u16 facing = VarGet(ScriptReadHalfword(ctx));
u8 mapGroup = ScriptReadByte(ctx);
u8 mapNum = ScriptReadByte(ctx);

MovementPathPlanning_MoveObjectToPos(localId, mapNum, mapGroup, posX,posY,facing);
// sMovingNpcId = localId;
return FALSE;
}

0 comments on commit adca773

Please sign in to comment.