pokeemerald/src/rotating_tile_puzzle.c

386 lines
14 KiB
C
Raw Normal View History

2018-11-19 17:16:01 +01:00
#include "global.h"
#include "event_object_movement.h"
#include "fieldmap.h"
#include "malloc.h"
#include "rotating_tile_puzzle.h"
2018-11-19 17:16:01 +01:00
#include "script_movement.h"
2019-11-21 20:03:35 +01:00
#include "constants/event_object_movement.h"
2018-12-28 19:29:21 +01:00
#include "constants/event_objects.h"
#include "constants/metatile_labels.h"
2018-11-19 17:16:01 +01:00
2019-10-16 10:09:30 +02:00
#define ROTATE_COUNTERCLOCKWISE 0
#define ROTATE_CLOCKWISE 1
#define ROTATE_NONE 2
2019-10-16 10:09:30 +02:00
struct RotatingTileObject
2018-11-19 17:16:01 +01:00
{
2019-10-16 10:09:30 +02:00
u8 prevPuzzleTileNum;
2018-11-19 17:16:01 +01:00
u8 eventTemplateId;
};
struct RotatingTilePuzzle
2018-11-19 17:16:01 +01:00
{
struct RotatingTileObject objects[OBJECT_EVENTS_COUNT];
2019-10-16 10:09:30 +02:00
u8 numObjects;
bool8 isTrickHouse;
2018-11-19 17:16:01 +01:00
};
2020-02-08 04:08:59 +01:00
static const u8 sMovement_ShiftRight[] =
{
MOVEMENT_ACTION_STORE_AND_LOCK_ANIM,
MOVEMENT_ACTION_WALK_NORMAL_RIGHT,
MOVEMENT_ACTION_FREE_AND_UNLOCK_ANIM,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_ShiftDown[] =
{
MOVEMENT_ACTION_STORE_AND_LOCK_ANIM,
MOVEMENT_ACTION_WALK_NORMAL_DOWN,
MOVEMENT_ACTION_FREE_AND_UNLOCK_ANIM,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_ShiftLeft[] =
{
MOVEMENT_ACTION_STORE_AND_LOCK_ANIM,
MOVEMENT_ACTION_WALK_NORMAL_LEFT,
MOVEMENT_ACTION_FREE_AND_UNLOCK_ANIM,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_ShiftUp[] =
{
MOVEMENT_ACTION_STORE_AND_LOCK_ANIM,
MOVEMENT_ACTION_WALK_NORMAL_UP,
MOVEMENT_ACTION_FREE_AND_UNLOCK_ANIM,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_FaceRight[] =
{
MOVEMENT_ACTION_FACE_RIGHT,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_FaceDown[] =
{
MOVEMENT_ACTION_FACE_DOWN,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_FaceLeft[] =
{
MOVEMENT_ACTION_FACE_LEFT,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_FaceUp[] =
{
MOVEMENT_ACTION_FACE_UP,
MOVEMENT_ACTION_STEP_END
};
2018-11-19 17:16:01 +01:00
// This file's functions.
2019-10-16 10:09:30 +02:00
static void SaveRotatingTileObject(u8 eventTemplateId, u8 arg1);
static void TurnUnsavedRotatingTileObject(u8 eventTemplateId, u8 arg1);
2018-11-19 17:16:01 +01:00
// EWRAM vars
EWRAM_DATA static struct RotatingTilePuzzle *sRotatingTilePuzzle = NULL;
2018-11-19 17:16:01 +01:00
// code
void InitRotatingTilePuzzle(bool8 isTrickHouse)
2018-11-19 17:16:01 +01:00
{
if (sRotatingTilePuzzle == NULL)
sRotatingTilePuzzle = AllocZeroed(sizeof(*sRotatingTilePuzzle));
2018-11-19 17:16:01 +01:00
sRotatingTilePuzzle->isTrickHouse = isTrickHouse;
2018-11-19 17:16:01 +01:00
}
2019-10-16 10:09:30 +02:00
void FreeRotatingTilePuzzle(void)
2018-11-19 17:16:01 +01:00
{
u8 id;
if (sRotatingTilePuzzle != NULL)
FREE_AND_SET_NULL(sRotatingTilePuzzle);
2018-11-19 17:16:01 +01:00
id = GetObjectEventIdByLocalIdAndMap(OBJ_EVENT_ID_PLAYER, 0, 0);
ObjectEventClearHeldMovementIfFinished(&gObjectEvents[id]);
ScriptMovement_UnfreezeObjectEvents();
2018-11-19 17:16:01 +01:00
}
2019-10-16 10:09:30 +02:00
u16 MoveRotatingTileObjects(u8 puzzleNumber)
2018-11-19 17:16:01 +01:00
{
u8 i;
struct ObjectEventTemplate *objectEvents = gSaveBlock1Ptr->objectEventTemplates;
2018-11-19 17:16:01 +01:00
u16 localId = 0;
for (i = 0; i < OBJECT_EVENT_TEMPLATES_COUNT; i++)
2018-11-19 17:16:01 +01:00
{
s32 puzzleTileStart;
2019-10-16 10:09:30 +02:00
u8 puzzleTileNum;
2021-10-09 18:12:18 +02:00
s16 x = objectEvents[i].x + MAP_OFFSET;
s16 y = objectEvents[i].y + MAP_OFFSET;
2018-11-19 17:16:01 +01:00
u16 metatile = MapGridGetMetatileIdAt(x, y);
if (!sRotatingTilePuzzle->isTrickHouse)
2019-10-16 10:09:30 +02:00
puzzleTileStart = METATILE_MossdeepGym_YellowArrow_Right;
2018-11-19 17:16:01 +01:00
else
puzzleTileStart = METATILE_TrickHousePuzzle_Arrow_YellowOnWhite_Right;
2018-11-19 17:16:01 +01:00
2019-10-16 10:09:30 +02:00
// Object is on a metatile before the puzzle tile section
// UB: Because this is not if (metatile < puzzleTileStart), for the trick house (metatile - puzzleTileStart) below can result in casting a negative value to u8
2019-10-16 10:09:30 +02:00
if (metatile < METATILE_MossdeepGym_YellowArrow_Right)
2018-11-19 17:16:01 +01:00
continue;
2019-10-16 10:09:30 +02:00
// Object is on a metatile after the puzzle tile section (never occurs, in both cases the puzzle tiles are last)
if ((u8)((metatile - puzzleTileStart) / TILESET_WIDTH) >= 5)
2018-11-19 17:16:01 +01:00
continue;
2019-10-16 10:09:30 +02:00
// Object is on a metatile in puzzle tile section, but not one of the currently rotating color
if ((u8)((metatile - puzzleTileStart) / TILESET_WIDTH) != puzzleNumber)
2018-11-19 17:16:01 +01:00
continue;
puzzleTileNum = (u8)((metatile - puzzleTileStart) % TILESET_WIDTH);
2019-10-16 10:09:30 +02:00
// First 4 puzzle tiles are the colored arrows
if (puzzleTileNum < 4)
2018-11-19 17:16:01 +01:00
{
s8 x = 0;
s8 y = 0;
const u8 *movementScript;
2019-10-16 10:09:30 +02:00
switch (puzzleTileNum)
2018-11-19 17:16:01 +01:00
{
2019-10-16 10:09:30 +02:00
case 0: // Right Arrow
2020-02-08 04:08:59 +01:00
movementScript = sMovement_ShiftRight;
2018-11-19 17:16:01 +01:00
x = 1;
break;
2019-10-16 10:09:30 +02:00
case 1: // Down Arrow
2020-02-08 04:08:59 +01:00
movementScript = sMovement_ShiftDown;
2018-11-19 17:16:01 +01:00
y = 1;
break;
2019-10-16 10:09:30 +02:00
case 2: // Left Arrow
2020-02-08 04:08:59 +01:00
movementScript = sMovement_ShiftLeft;
2018-11-19 17:16:01 +01:00
x = -1;
break;
2019-10-16 10:09:30 +02:00
case 3: // Up Arrow
2020-02-08 04:08:59 +01:00
movementScript = sMovement_ShiftUp;
2018-11-19 17:16:01 +01:00
y = -1;
break;
default:
continue;
}
objectEvents[i].x += x;
objectEvents[i].y += y;
if (GetObjectEventIdByLocalIdAndMap(objectEvents[i].localId, gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup) != OBJECT_EVENTS_COUNT)
2018-11-19 17:16:01 +01:00
{
2019-10-16 10:09:30 +02:00
SaveRotatingTileObject(i, puzzleTileNum);
localId = objectEvents[i].localId;
2018-11-19 17:16:01 +01:00
ScriptMovement_StartObjectMovementScript(localId, gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup, movementScript);
}
2019-10-16 10:09:30 +02:00
// Never reached in normal gameplay
2018-11-19 17:16:01 +01:00
else
{
2019-10-16 10:09:30 +02:00
TurnUnsavedRotatingTileObject(i, puzzleTileNum);
2018-11-19 17:16:01 +01:00
}
}
}
return localId;
}
2019-10-16 10:09:30 +02:00
void TurnRotatingTileObjects(void)
2018-11-19 17:16:01 +01:00
{
u8 i;
s32 puzzleTileStart;
struct ObjectEventTemplate *objectEvents;
2018-11-19 17:16:01 +01:00
if (sRotatingTilePuzzle == NULL)
2018-11-19 17:16:01 +01:00
return;
if (!sRotatingTilePuzzle->isTrickHouse)
2019-10-16 10:09:30 +02:00
puzzleTileStart = METATILE_MossdeepGym_YellowArrow_Right;
2018-11-19 17:16:01 +01:00
else
puzzleTileStart = METATILE_TrickHousePuzzle_Arrow_YellowOnWhite_Right;
2018-11-19 17:16:01 +01:00
objectEvents = gSaveBlock1Ptr->objectEventTemplates;
2019-10-16 10:09:30 +02:00
for (i = 0; i < sRotatingTilePuzzle->numObjects; i++)
2018-11-19 17:16:01 +01:00
{
2019-10-16 10:09:30 +02:00
s32 rotation;
s8 tileDifference;
u8 objectEventId;
2021-10-09 18:12:18 +02:00
s16 x = objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].x + MAP_OFFSET;
s16 y = objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].y + MAP_OFFSET;
2018-11-19 17:16:01 +01:00
u16 metatile = MapGridGetMetatileIdAt(x, y);
2019-10-16 10:09:30 +02:00
// NOTE: The following 2 assignments and if else could all be replaced with rotation = ROTATE_COUNTERCLOCKWISE
// For an object to be saved in sRotatingTilePuzzle->objects, it must have been on a colored arrow tile
// After the first assignment, tileDifference will always be a number [0-3] representing which arrow tile the object is on now (0: right, 1: down, 2: left, 3: up)
// prevPuzzleTileNum will similarly be a number [0-3] representing the arrow tile the object just moved from
// All the puzzles are oriented counter-clockwise and can only move 1 step at a time, so the difference between the current tile and the previous tile will always either be -1 or 3 (0-1, 1-2, 2-3, 3-0)
// Which means tileDifference will always either be -1 or 3 after the below subtraction, and rotation will always be ROTATE_COUNTERCLOCKWISE after the following conditionals
tileDifference = (u8)((metatile - puzzleTileStart) % TILESET_WIDTH);
2019-10-16 10:09:30 +02:00
tileDifference -= (sRotatingTilePuzzle->objects[i].prevPuzzleTileNum);
2019-10-16 10:09:30 +02:00
// Always true, see above
if (tileDifference < 0 || tileDifference == 3)
2018-11-19 17:16:01 +01:00
{
2019-10-16 10:09:30 +02:00
// Always false, see above
if (tileDifference == -3)
rotation = ROTATE_CLOCKWISE;
2018-11-19 17:16:01 +01:00
else
2019-10-16 10:09:30 +02:00
rotation = ROTATE_COUNTERCLOCKWISE;
2018-11-19 17:16:01 +01:00
}
else
{
2019-10-16 10:09:30 +02:00
if (tileDifference > 0)
rotation = ROTATE_CLOCKWISE;
2018-11-19 17:16:01 +01:00
else
2019-10-16 10:09:30 +02:00
rotation = ROTATE_NONE;
2018-11-19 17:16:01 +01:00
}
objectEventId = GetObjectEventIdByLocalIdAndMap(objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].localId, gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup);
if (objectEventId != OBJECT_EVENTS_COUNT)
2018-11-19 17:16:01 +01:00
{
const u8 *movementScript;
u8 direction = gObjectEvents[objectEventId].facingDirection;
2019-10-16 10:09:30 +02:00
if (rotation == ROTATE_COUNTERCLOCKWISE)
2018-11-19 17:16:01 +01:00
{
switch (direction)
{
case DIR_EAST:
2020-02-08 04:08:59 +01:00
movementScript = sMovement_FaceUp;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_UP;
2018-11-19 17:16:01 +01:00
break;
case DIR_SOUTH:
2020-02-08 04:08:59 +01:00
movementScript = sMovement_FaceRight;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_RIGHT;
2018-11-19 17:16:01 +01:00
break;
case DIR_WEST:
2020-02-08 04:08:59 +01:00
movementScript = sMovement_FaceDown;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_DOWN;
2018-11-19 17:16:01 +01:00
break;
case DIR_NORTH:
2020-02-08 04:08:59 +01:00
movementScript = sMovement_FaceLeft;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_LEFT;
2018-11-19 17:16:01 +01:00
break;
default:
continue;
}
ScriptMovement_StartObjectMovementScript(objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].localId,
2018-11-19 17:16:01 +01:00
gSaveBlock1Ptr->location.mapNum,
gSaveBlock1Ptr->location.mapGroup,
movementScript);
}
2019-10-16 10:09:30 +02:00
// Never reached
else if (rotation == ROTATE_CLOCKWISE)
2018-11-19 17:16:01 +01:00
{
switch (direction)
{
case DIR_EAST:
2020-02-08 04:08:59 +01:00
movementScript = sMovement_FaceDown;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_DOWN;
2018-11-19 17:16:01 +01:00
break;
case DIR_SOUTH:
2020-02-08 04:08:59 +01:00
movementScript = sMovement_FaceLeft;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_LEFT;
2018-11-19 17:16:01 +01:00
break;
case DIR_WEST:
2020-02-08 04:08:59 +01:00
movementScript = sMovement_FaceUp;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_UP;
2018-11-19 17:16:01 +01:00
break;
case DIR_NORTH:
2020-02-08 04:08:59 +01:00
movementScript = sMovement_FaceRight;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_RIGHT;
2018-11-19 17:16:01 +01:00
break;
default:
continue;
}
ScriptMovement_StartObjectMovementScript(objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].localId,
2018-11-19 17:16:01 +01:00
gSaveBlock1Ptr->location.mapNum,
gSaveBlock1Ptr->location.mapGroup,
movementScript);
}
}
}
}
2019-10-16 10:09:30 +02:00
static void SaveRotatingTileObject(u8 eventTemplateId, u8 puzzleTileNum)
2018-11-19 17:16:01 +01:00
{
2019-10-16 10:09:30 +02:00
sRotatingTilePuzzle->objects[sRotatingTilePuzzle->numObjects].eventTemplateId = eventTemplateId;
sRotatingTilePuzzle->objects[sRotatingTilePuzzle->numObjects].prevPuzzleTileNum = puzzleTileNum;
sRotatingTilePuzzle->numObjects++;
2018-11-19 17:16:01 +01:00
}
2019-10-16 10:09:30 +02:00
// Functionally unused
static void TurnUnsavedRotatingTileObject(u8 eventTemplateId, u8 puzzleTileNum)
2018-11-19 17:16:01 +01:00
{
2019-10-16 10:09:30 +02:00
s8 tileDifference;
s32 rotation;
s32 puzzleTileStart;
2018-11-19 17:16:01 +01:00
u16 movementType;
struct ObjectEventTemplate *objectEvents = gSaveBlock1Ptr->objectEventTemplates;
2021-10-09 18:12:18 +02:00
s16 x = objectEvents[eventTemplateId].x + MAP_OFFSET;
s16 y = objectEvents[eventTemplateId].y + MAP_OFFSET;
2018-11-19 17:16:01 +01:00
u16 metatile = MapGridGetMetatileIdAt(x, y);
if (!sRotatingTilePuzzle->isTrickHouse)
2019-10-16 10:09:30 +02:00
puzzleTileStart = METATILE_MossdeepGym_YellowArrow_Right;
2018-11-19 17:16:01 +01:00
else
puzzleTileStart = METATILE_TrickHousePuzzle_Arrow_YellowOnWhite_Right;
2018-11-19 17:16:01 +01:00
tileDifference = (u8)((metatile - puzzleTileStart) % TILESET_WIDTH);
2019-10-16 10:09:30 +02:00
tileDifference -= puzzleTileNum;
if (tileDifference < 0 || tileDifference == 3)
rotation = ROTATE_COUNTERCLOCKWISE;
else if (tileDifference > 0 || tileDifference == -3)
rotation = ROTATE_CLOCKWISE;
2018-11-19 17:16:01 +01:00
else
2019-10-16 10:09:30 +02:00
rotation = ROTATE_NONE;
2018-11-19 17:16:01 +01:00
movementType = objectEvents[eventTemplateId].movementType;
2019-10-16 10:09:30 +02:00
if (rotation == ROTATE_COUNTERCLOCKWISE)
2018-11-19 17:16:01 +01:00
{
switch (movementType)
{
case MOVEMENT_TYPE_FACE_RIGHT:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_UP;
2018-11-19 17:16:01 +01:00
break;
case MOVEMENT_TYPE_FACE_DOWN:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_RIGHT;
2018-11-19 17:16:01 +01:00
break;
case MOVEMENT_TYPE_FACE_LEFT:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_DOWN;
2018-11-19 17:16:01 +01:00
break;
case MOVEMENT_TYPE_FACE_UP:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_LEFT;
2018-11-19 17:16:01 +01:00
break;
default:
break;
}
}
2019-10-16 10:09:30 +02:00
else if (rotation == ROTATE_CLOCKWISE)
2018-11-19 17:16:01 +01:00
{
switch (movementType)
{
case MOVEMENT_TYPE_FACE_RIGHT:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_DOWN;
2018-11-19 17:16:01 +01:00
break;
case MOVEMENT_TYPE_FACE_DOWN:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_LEFT;
2018-11-19 17:16:01 +01:00
break;
case MOVEMENT_TYPE_FACE_LEFT:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_UP;
2018-11-19 17:16:01 +01:00
break;
case MOVEMENT_TYPE_FACE_UP:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_RIGHT;
2018-11-19 17:16:01 +01:00
break;
default:
break;
}
}
}