pokeemerald/src/union_room_player_avatar.c

607 lines
19 KiB
C
Raw Normal View History

2019-04-01 18:05:58 +02:00
#include "global.h"
#include "event_data.h"
#include "event_object_movement.h"
#include "field_player_avatar.h"
#include "fieldmap.h"
2019-05-25 21:26:29 +02:00
#include "script.h"
2019-04-01 18:05:58 +02:00
#include "task.h"
2019-05-25 21:26:29 +02:00
#include "union_room.h"
#include "constants/event_objects.h"
2019-11-21 20:03:35 +01:00
#include "constants/event_object_movement.h"
2019-04-01 18:05:58 +02:00
#define UR_SPRITE_START_ID (MAX_SPRITES - MAX_UNION_ROOM_LEADERS)
// Each parent player can lead a group of up to MAX_RFU_PLAYERS (including themselves).
// Multiply the leader's id by MAX_RFU_PLAYERS and add the member's id (0 if the leader) to
// get the sprite index of that player.
#define UR_PLAYER_SPRITE_ID(leaderId, memberId)(MAX_RFU_PLAYERS * leaderId + memberId)
2019-04-01 18:05:58 +02:00
2020-06-01 16:23:12 +02:00
static EWRAM_DATA struct UnionRoomObject * sUnionObjWork = NULL;
static EWRAM_DATA u32 sUnionObjRefreshTimer = 0;
2019-04-01 18:05:58 +02:00
2020-06-01 16:23:12 +02:00
static u8 CreateTask_AnimateUnionRoomPlayers(void);
static u32 IsUnionRoomPlayerInvisible(u32, u32);
static void SetUnionRoomObjectFacingDirection(s32, s32, u8);
// + 2 is just to match, those elements are empty and never read
static const u8 sUnionRoomObjGfxIds[GENDER_COUNT][MAX_UNION_ROOM_LEADERS + 2] = {
2020-05-30 10:09:21 +02:00
[MALE] = {
OBJ_EVENT_GFX_MAN_3,
OBJ_EVENT_GFX_BLACK_BELT,
OBJ_EVENT_GFX_CAMPER,
OBJ_EVENT_GFX_YOUNGSTER,
OBJ_EVENT_GFX_PSYCHIC_M,
OBJ_EVENT_GFX_BUG_CATCHER,
OBJ_EVENT_GFX_MAN_4,
2020-05-30 10:09:21 +02:00
OBJ_EVENT_GFX_MAN_5
},
[FEMALE] = {
OBJ_EVENT_GFX_WOMAN_5,
OBJ_EVENT_GFX_HEX_MANIAC,
OBJ_EVENT_GFX_PICNICKER,
OBJ_EVENT_GFX_LASS,
OBJ_EVENT_GFX_LASS,
OBJ_EVENT_GFX_GIRL_3,
OBJ_EVENT_GFX_WOMAN_2,
2020-05-30 10:09:21 +02:00
OBJ_EVENT_GFX_BEAUTY
}
2019-04-01 18:05:58 +02:00
};
static const s16 sUnionRoomPlayerCoords[MAX_UNION_ROOM_LEADERS][2] = {
2020-05-30 10:09:21 +02:00
{ 4, 6},
{13, 8},
{10, 6},
{ 1, 8},
{13, 4},
{ 7, 4},
{ 1, 4},
{ 7, 8}
2019-04-01 18:05:58 +02:00
};
// If there's a group of players interacting in the Union Room, the group
// leader will be at one of the positions above and each member in the group
// will be at one of the offsets from that position below. The leader will
// be at the first offset (0,0), as they're at the center.
static const s8 sUnionRoomGroupOffsets[MAX_RFU_PLAYERS][2] = {
{ 0, 0}, // Center
{ 1, 0}, // Left
{ 0, -1}, // Top
{-1, 0}, // Right
{ 0, 1} // Bottom
2019-04-01 18:05:58 +02:00
};
2020-06-01 16:23:12 +02:00
static const u8 sOppositeFacingDirection[] = {
[DIR_NONE] = DIR_NONE,
[DIR_SOUTH] = DIR_NORTH,
[DIR_NORTH] = DIR_SOUTH,
[DIR_WEST] = DIR_EAST,
[DIR_EAST] = DIR_WEST
2019-04-01 18:05:58 +02:00
};
// Compare to sUnionRoomGroupOffsets, the direction each group member
// needs to be facing in order to face the group leader in the center.
static const u8 sMemberFacingDirections[MAX_RFU_PLAYERS] = {
DIR_SOUTH, // Leader, but never read
DIR_WEST,
DIR_SOUTH,
DIR_EAST,
DIR_NORTH
2019-04-01 18:05:58 +02:00
};
2021-07-05 19:54:43 +02:00
static const u8 sUnionRoomLocalIds[] = {
LOCALID_UNION_ROOM_PLAYER_1,
LOCALID_UNION_ROOM_PLAYER_2,
LOCALID_UNION_ROOM_PLAYER_3,
LOCALID_UNION_ROOM_PLAYER_4,
LOCALID_UNION_ROOM_PLAYER_5,
LOCALID_UNION_ROOM_PLAYER_6,
LOCALID_UNION_ROOM_PLAYER_7,
LOCALID_UNION_ROOM_PLAYER_8
};
2020-06-01 16:23:12 +02:00
2021-07-05 19:54:43 +02:00
// Unused
static const u16 sHidePlayerFlags[] = {
FLAG_HIDE_UNION_ROOM_PLAYER_1,
FLAG_HIDE_UNION_ROOM_PLAYER_2,
FLAG_HIDE_UNION_ROOM_PLAYER_3,
FLAG_HIDE_UNION_ROOM_PLAYER_4,
FLAG_HIDE_UNION_ROOM_PLAYER_5,
FLAG_HIDE_UNION_ROOM_PLAYER_6,
FLAG_HIDE_UNION_ROOM_PLAYER_7,
2021-07-05 19:54:43 +02:00
FLAG_HIDE_UNION_ROOM_PLAYER_8
2019-04-01 18:05:58 +02:00
};
2020-06-01 16:23:12 +02:00
static const u8 sMovement_UnionPlayerExit[2] = {
2019-04-01 18:05:58 +02:00
MOVEMENT_ACTION_FLY_UP,
MOVEMENT_ACTION_STEP_END
};
2020-06-01 16:23:12 +02:00
static const u8 sMovement_UnionPlayerEnter[2] = {
2019-04-01 18:05:58 +02:00
MOVEMENT_ACTION_FLY_DOWN,
MOVEMENT_ACTION_STEP_END
};
2020-06-09 00:16:57 +02:00
static bool32 IsPlayerStandingStill(void)
2019-04-01 18:05:58 +02:00
{
2020-06-09 00:16:57 +02:00
if (gPlayerAvatar.tileTransitionState == T_TILE_CENTER || gPlayerAvatar.tileTransitionState == T_NOT_MOVING)
2019-04-01 18:05:58 +02:00
return TRUE;
else
return FALSE;
}
// Gender and trainer id are used to determine which sprite a player appears as
2020-06-01 16:23:12 +02:00
static u8 GetUnionRoomPlayerGraphicsId(u32 gender, u32 id)
2019-04-01 18:05:58 +02:00
{
return sUnionRoomObjGfxIds[gender][id % MAX_UNION_ROOM_LEADERS];
2019-04-01 18:05:58 +02:00
}
static void GetUnionRoomPlayerCoords(u32 leaderId, u32 memberId, s32 * x, s32 * y)
2019-04-01 18:05:58 +02:00
{
*x = sUnionRoomPlayerCoords[leaderId][0] + sUnionRoomGroupOffsets[memberId][0] + MAP_OFFSET;
*y = sUnionRoomPlayerCoords[leaderId][1] + sUnionRoomGroupOffsets[memberId][1] + MAP_OFFSET;
2019-04-01 18:05:58 +02:00
}
static bool32 IsUnionRoomPlayerAt(u32 leaderId, u32 memberId, s32 x, s32 y)
2019-04-01 18:05:58 +02:00
{
if ((sUnionRoomPlayerCoords[leaderId][0] + sUnionRoomGroupOffsets[memberId][0] + MAP_OFFSET == x)
&& (sUnionRoomPlayerCoords[leaderId][1] + sUnionRoomGroupOffsets[memberId][1] + MAP_OFFSET == y))
2019-04-01 18:05:58 +02:00
return TRUE;
2020-06-01 16:23:12 +02:00
else
return FALSE;
2019-04-01 18:05:58 +02:00
}
2019-04-01 20:20:34 +02:00
static bool32 IsUnionRoomPlayerHidden(u32 player_idx)
2019-04-01 18:05:58 +02:00
{
return FlagGet(FLAG_HIDE_UNION_ROOM_PLAYER_1 + player_idx);
}
2019-04-01 20:20:34 +02:00
static void HideUnionRoomPlayer(u32 player_idx)
2019-04-01 18:05:58 +02:00
{
FlagSet(FLAG_HIDE_UNION_ROOM_PLAYER_1 + player_idx);
}
2019-04-01 20:20:34 +02:00
static void ShowUnionRoomPlayer(u32 player_idx)
2019-04-01 18:05:58 +02:00
{
FlagClear(FLAG_HIDE_UNION_ROOM_PLAYER_1 + player_idx);
}
static void SetUnionRoomPlayerGfx(u32 leaderId, u32 gfxId)
2019-04-01 18:05:58 +02:00
{
VarSet(VAR_OBJ_GFX_ID_0 + leaderId, gfxId);
2019-04-01 18:05:58 +02:00
}
static void CreateUnionRoomPlayerObjectEvent(u32 leaderId)
2019-04-01 18:05:58 +02:00
{
TrySpawnObjectEvent(sUnionRoomLocalIds[leaderId], gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup);
2019-04-01 18:05:58 +02:00
}
static void RemoveUnionRoomPlayerObjectEvent(u32 leaderId)
2019-04-01 18:05:58 +02:00
{
RemoveObjectEventByLocalIdAndMap(sUnionRoomLocalIds[leaderId], gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup);
2019-04-01 18:05:58 +02:00
}
static bool32 SetUnionRoomPlayerEnterExitMovement(u32 leaderId, const u8 * movement)
2019-04-01 18:05:58 +02:00
{
u8 objectId;
struct ObjectEvent * object;
if (TryGetObjectEventIdByLocalIdAndMap(sUnionRoomLocalIds[leaderId], gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup, &objectId))
2019-04-01 18:05:58 +02:00
return FALSE;
object = &gObjectEvents[objectId];
if (ObjectEventIsMovementOverridden(object))
2019-04-01 18:05:58 +02:00
return FALSE;
if (ObjectEventSetHeldMovement(object, *movement))
2019-04-01 18:05:58 +02:00
return FALSE;
return TRUE;
}
static bool32 TryReleaseUnionRoomPlayerObjectEvent(u32 leaderId)
2019-04-01 18:05:58 +02:00
{
u8 objectId;
struct ObjectEvent * object;
if (TryGetObjectEventIdByLocalIdAndMap(sUnionRoomLocalIds[leaderId], gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup, &objectId))
2019-04-01 18:05:58 +02:00
return TRUE;
object = &gObjectEvents[objectId];
if (!ObjectEventClearHeldMovementIfFinished(object))
2019-04-01 18:05:58 +02:00
return FALSE;
2019-04-01 18:05:58 +02:00
if (!ScriptContext2_IsEnabled())
UnfreezeObjectEvent(object);
2019-04-01 18:05:58 +02:00
else
FreezeObjectEvent(object);
2019-04-01 18:05:58 +02:00
return TRUE;
}
2020-06-01 16:23:12 +02:00
u8 InitUnionRoomPlayerObjects(struct UnionRoomObject * players)
2019-04-01 18:05:58 +02:00
{
s32 i;
2020-06-01 16:23:12 +02:00
sUnionObjRefreshTimer = 0;
sUnionObjWork = players;
for (i = 0; i < MAX_UNION_ROOM_LEADERS; i++)
2019-04-01 18:05:58 +02:00
{
2020-06-01 16:23:12 +02:00
players[i].state = 0;
players[i].gfxId = 0;
players[i].animState = 0;
players[i].schedAnim = UNION_ROOM_SPAWN_NONE;
2019-04-01 18:05:58 +02:00
}
2020-06-01 16:23:12 +02:00
return CreateTask_AnimateUnionRoomPlayers();
2019-04-01 18:05:58 +02:00
}
static bool32 AnimateUnionRoomPlayerDespawn(s8 * state, u32 leaderId, struct UnionRoomObject * object)
2019-04-01 18:05:58 +02:00
{
2020-06-01 16:23:12 +02:00
switch (*state)
2019-04-01 18:05:58 +02:00
{
case 0:
if (SetUnionRoomPlayerEnterExitMovement(leaderId, sMovement_UnionPlayerExit) == TRUE)
2019-04-01 18:05:58 +02:00
{
HideUnionRoomPlayer(leaderId);
2020-06-01 16:23:12 +02:00
(*state)++;
2019-04-01 18:05:58 +02:00
}
break;
case 1:
if (TryReleaseUnionRoomPlayerObjectEvent(leaderId))
2019-04-01 18:05:58 +02:00
{
RemoveUnionRoomPlayerObjectEvent(leaderId);
HideUnionRoomPlayer(leaderId);
2020-06-01 16:23:12 +02:00
*state = 0;
2019-04-01 18:05:58 +02:00
return TRUE;
}
break;
}
return FALSE;
}
static bool32 AnimateUnionRoomPlayerSpawn(s8 * state, u32 leaderId, struct UnionRoomObject * object)
2019-04-01 18:05:58 +02:00
{
s16 x, y;
2020-06-01 16:23:12 +02:00
switch (*state)
2019-04-01 18:05:58 +02:00
{
case 0:
2020-06-09 00:16:57 +02:00
if (!IsPlayerStandingStill())
2019-04-01 18:05:58 +02:00
break;
PlayerGetDestCoords(&x, &y);
if (IsUnionRoomPlayerAt(leaderId, 0, x, y) == TRUE)
2019-04-01 18:05:58 +02:00
break;
player_get_pos_including_state_based_drift(&x, &y);
if (IsUnionRoomPlayerAt(leaderId, 0, x, y) == TRUE)
2019-04-01 18:05:58 +02:00
break;
SetUnionRoomPlayerGfx(leaderId, object->gfxId);
CreateUnionRoomPlayerObjectEvent(leaderId);
ShowUnionRoomPlayer(leaderId);
2020-06-01 16:23:12 +02:00
(*state)++;
2019-04-01 18:05:58 +02:00
// fallthrough
case 3: // incorrect?
if (SetUnionRoomPlayerEnterExitMovement(leaderId, sMovement_UnionPlayerEnter) == TRUE)
2020-06-01 16:23:12 +02:00
(*state)++;
2019-04-01 18:05:58 +02:00
break;
case 2:
if (TryReleaseUnionRoomPlayerObjectEvent(leaderId))
2019-04-01 18:05:58 +02:00
{
2020-06-01 16:23:12 +02:00
*state = 0;
2019-04-01 18:05:58 +02:00
return TRUE;
}
break;
}
return FALSE;
}
static bool32 SpawnGroupLeader(u32 leaderId, u32 gender, u32 id)
2019-04-01 18:05:58 +02:00
{
struct UnionRoomObject * object = &sUnionObjWork[leaderId];
object->schedAnim = UNION_ROOM_SPAWN_IN;
object->gfxId = GetUnionRoomPlayerGraphicsId(gender, id);
2020-06-01 16:23:12 +02:00
if (object->state == 0)
2019-04-01 18:05:58 +02:00
return TRUE;
else
return FALSE;
}
static bool32 DespawnGroupLeader(u32 leaderId)
2019-04-01 18:05:58 +02:00
{
struct UnionRoomObject * object = &sUnionObjWork[leaderId];
object->schedAnim = UNION_ROOM_SPAWN_OUT;
2020-06-01 16:23:12 +02:00
if (object->state == 1)
2019-04-01 18:05:58 +02:00
return TRUE;
else
return FALSE;
}
static void AnimateUnionRoomPlayer(u32 leaderId, struct UnionRoomObject * object)
2019-04-01 18:05:58 +02:00
{
switch (object->state)
2019-04-01 18:05:58 +02:00
{
case 0:
if (object->schedAnim == UNION_ROOM_SPAWN_IN)
2019-04-01 18:05:58 +02:00
{
object->state = 2;
object->animState = 0;
2019-04-01 18:05:58 +02:00
}
else
{
break;
}
// fallthrough
case 2:
if (!IsUnionRoomPlayerInvisible(leaderId, 0) && object->schedAnim == UNION_ROOM_SPAWN_OUT)
2019-04-01 18:05:58 +02:00
{
object->state = 0;
object->animState = 0;
RemoveUnionRoomPlayerObjectEvent(leaderId);
HideUnionRoomPlayer(leaderId);
2019-04-01 18:05:58 +02:00
}
else if (AnimateUnionRoomPlayerSpawn(&object->animState, leaderId, object) == TRUE)
2019-04-01 18:05:58 +02:00
{
object->state = 1;
2019-04-01 18:05:58 +02:00
}
break;
case 1:
if (object->schedAnim == UNION_ROOM_SPAWN_OUT)
2019-04-01 18:05:58 +02:00
{
object->state = 3;
object->animState = 0;
2019-04-01 18:05:58 +02:00
}
else
{
break;
}
// fallthrough
case 3:
if (AnimateUnionRoomPlayerDespawn(&object->animState, leaderId, object) == 1)
object->state = 0;
2019-04-01 18:05:58 +02:00
break;
}
object->schedAnim = UNION_ROOM_SPAWN_NONE;
2019-04-01 18:05:58 +02:00
}
2020-06-01 16:23:12 +02:00
static void Task_AnimateUnionRoomPlayers(u8 taskId)
2019-04-01 18:05:58 +02:00
{
s32 i;
for (i = 0; i < MAX_UNION_ROOM_LEADERS; i++)
2020-06-01 16:23:12 +02:00
AnimateUnionRoomPlayer(i, &sUnionObjWork[i]);
2019-04-01 18:05:58 +02:00
}
2020-06-01 16:23:12 +02:00
static u8 CreateTask_AnimateUnionRoomPlayers(void)
2019-04-01 18:05:58 +02:00
{
2020-06-01 16:23:12 +02:00
if (FuncIsActiveTask(Task_AnimateUnionRoomPlayers) == TRUE)
2019-04-01 18:05:58 +02:00
return NUM_TASKS;
else
2020-06-01 16:23:12 +02:00
return CreateTask(Task_AnimateUnionRoomPlayers, 5);
2019-04-01 18:05:58 +02:00
}
2020-06-01 16:23:12 +02:00
static void DestroyTask_AnimateUnionRoomPlayers(void)
2019-04-01 18:05:58 +02:00
{
2020-06-01 16:23:12 +02:00
u8 taskId = FindTaskIdByFunc(Task_AnimateUnionRoomPlayers);
2019-04-01 18:05:58 +02:00
if (taskId < NUM_TASKS)
DestroyTask(taskId);
}
2020-06-01 16:23:12 +02:00
void DestroyUnionRoomPlayerObjects(void)
2019-04-01 18:05:58 +02:00
{
s32 i;
for (i = 0; i < MAX_UNION_ROOM_LEADERS; i++)
2019-04-01 18:05:58 +02:00
{
if (!IsUnionRoomPlayerHidden(i))
{
RemoveUnionRoomPlayerObjectEvent(i);
2019-04-01 18:05:58 +02:00
HideUnionRoomPlayer(i);
}
}
2020-06-01 16:23:12 +02:00
sUnionObjWork = NULL;
DestroyTask_AnimateUnionRoomPlayers();
2019-04-01 18:05:58 +02:00
}
void CreateUnionRoomPlayerSprites(u8 * spriteIds, s32 leaderId)
2019-04-01 18:05:58 +02:00
{
s32 memberId;
for (memberId = 0; memberId < MAX_RFU_PLAYERS; memberId++)
2019-04-01 18:05:58 +02:00
{
s32 id = UR_PLAYER_SPRITE_ID(leaderId, memberId);
spriteIds[id] = CreateVirtualObject(OBJ_EVENT_GFX_MAN_4,
id - UR_SPRITE_START_ID,
2021-10-07 22:10:35 +02:00
sUnionRoomPlayerCoords[leaderId][0] + sUnionRoomGroupOffsets[memberId][0],
sUnionRoomPlayerCoords[leaderId][1] + sUnionRoomGroupOffsets[memberId][1],
2020-06-01 16:23:12 +02:00
3, 1);
SetVirtualObjectInvisibility(id - UR_SPRITE_START_ID, TRUE);
2019-04-01 18:05:58 +02:00
}
}
void DestroyUnionRoomPlayerSprites(u8 * spriteIds)
2019-04-01 18:05:58 +02:00
{
s32 i;
for (i = 0; i < NUM_UNION_ROOM_SPRITES; i++)
2020-06-01 16:23:12 +02:00
DestroySprite(&gSprites[spriteIds[i]]);
2019-04-01 18:05:58 +02:00
}
// Clear the impassable metatiles around the group leaders that get set
// to prevent the player from walking through the group member sprites.
2020-06-01 16:23:12 +02:00
void SetTilesAroundUnionRoomPlayersPassable(void)
2019-04-01 18:05:58 +02:00
{
s32 i, memberId, x, y;
for (i = 0; i < MAX_UNION_ROOM_LEADERS; i++)
2019-04-01 18:05:58 +02:00
{
for (memberId = 0; memberId < MAX_RFU_PLAYERS; memberId++)
2019-04-01 18:05:58 +02:00
{
GetUnionRoomPlayerCoords(i, memberId, &x, &y);
2020-06-01 16:23:12 +02:00
MapGridSetMetatileImpassabilityAt(x, y, FALSE);
2019-04-01 18:05:58 +02:00
}
}
}
static u8 GetNewFacingDirectionForUnionRoomPlayer(u32 memberId, u32 leaderId, struct RfuGameData * gameData)
2019-04-01 18:05:58 +02:00
{
if (memberId) // If not leader
return sMemberFacingDirections[memberId];
else if (gameData->activity == (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-06-01 16:23:12 +02:00
return DIR_SOUTH;
2019-04-01 18:05:58 +02:00
else
2020-06-01 16:23:12 +02:00
return DIR_EAST;
2019-04-01 18:05:58 +02:00
}
static bool32 IsUnionRoomPlayerInvisible(u32 leaderId, u32 memberId)
2019-04-01 18:05:58 +02:00
{
return IsVirtualObjectInvisible(UR_PLAYER_SPRITE_ID(leaderId, memberId) - UR_SPRITE_START_ID);
2019-04-01 18:05:58 +02:00
}
static void SpawnGroupMember(u32 leaderId, u32 memberId, u8 graphicsId, struct RfuGameData * gameData)
2019-04-01 18:05:58 +02:00
{
s32 x, y;
s32 id = UR_PLAYER_SPRITE_ID(leaderId, memberId);
if (IsUnionRoomPlayerInvisible(leaderId, memberId) == TRUE)
2019-04-01 18:05:58 +02:00
{
SetVirtualObjectInvisibility(id - UR_SPRITE_START_ID, FALSE);
SetVirtualObjectSpriteAnim(id - UR_SPRITE_START_ID, UNION_ROOM_SPAWN_IN);
2019-04-01 18:05:58 +02:00
}
SetVirtualObjectGraphics(id - UR_SPRITE_START_ID, graphicsId);
SetUnionRoomObjectFacingDirection(memberId, leaderId, GetNewFacingDirectionForUnionRoomPlayer(memberId, leaderId, gameData));
GetUnionRoomPlayerCoords(leaderId, memberId, &x, &y);
2020-06-01 16:23:12 +02:00
MapGridSetMetatileImpassabilityAt(x, y, TRUE);
2019-04-01 18:05:58 +02:00
}
static void DespawnGroupMember(u32 leaderId, u32 memberId)
2019-04-01 18:05:58 +02:00
{
s32 x, y;
SetVirtualObjectSpriteAnim(UR_PLAYER_SPRITE_ID(leaderId, memberId) - UR_SPRITE_START_ID, UNION_ROOM_SPAWN_OUT);
GetUnionRoomPlayerCoords(leaderId, memberId, &x, &y);
2020-06-01 16:23:12 +02:00
MapGridSetMetatileImpassabilityAt(x, y, FALSE);
2019-04-01 18:05:58 +02:00
}
2019-04-01 20:09:56 +02:00
static void AssembleGroup(u32 leaderId, struct RfuGameData * gameData)
2019-04-01 20:09:56 +02:00
{
s16 x, y, x2, y2;
s32 i;
PlayerGetDestCoords(&x, &y);
player_get_pos_including_state_based_drift(&x2, &y2);
if (IsVirtualObjectInvisible(UR_PLAYER_SPRITE_ID(leaderId, 0) - UR_SPRITE_START_ID) == TRUE)
2019-04-01 20:09:56 +02:00
{
if (IsUnionRoomPlayerAt(leaderId, 0, x, y) == TRUE || IsUnionRoomPlayerAt(leaderId, 0, x2, y2) == TRUE)
2019-04-01 20:09:56 +02:00
return;
SpawnGroupMember(leaderId, 0, GetUnionRoomPlayerGraphicsId(gameData->playerGender, gameData->compatibility.playerTrainerId[0]), gameData);
2019-04-01 20:09:56 +02:00
}
for (i = 1; i < MAX_RFU_PLAYERS; i++)
2019-04-01 20:09:56 +02:00
{
if (gameData->partnerInfo[i - 1] == 0)
2019-04-01 20:09:56 +02:00
{
DespawnGroupMember(leaderId, i);
2019-04-01 20:09:56 +02:00
}
else if (IsUnionRoomPlayerAt(leaderId, i, x, y) == FALSE && IsUnionRoomPlayerAt(leaderId, i, x2, y2) == FALSE)
2019-04-01 20:09:56 +02:00
{
SpawnGroupMember(leaderId, i, GetUnionRoomPlayerGraphicsId((gameData->partnerInfo[i - 1] >> PINFO_GENDER_SHIFT) & 1,
gameData->partnerInfo[i - 1] & PINFO_TID_MASK),
gameData);
2019-04-01 20:09:56 +02:00
}
}
}
static void SpawnGroupLeaderAndMembers(u32 leaderId, struct RfuGameData * gameData)
2019-04-01 20:09:56 +02:00
{
u32 i;
switch (gameData->activity)
2019-04-01 20:09:56 +02:00
{
2020-06-01 16:23:12 +02:00
case ACTIVITY_NONE | IN_UNION_ROOM:
case ACTIVITY_PLYRTALK | IN_UNION_ROOM:
SpawnGroupLeader(leaderId, gameData->playerGender, gameData->compatibility.playerTrainerId[0]);
for (i = 0; i < MAX_RFU_PLAYERS; i++)
DespawnGroupMember(leaderId, i);
2019-04-01 20:09:56 +02:00
break;
2020-06-01 16:23:12 +02:00
case ACTIVITY_BATTLE_SINGLE | IN_UNION_ROOM:
case ACTIVITY_TRADE | IN_UNION_ROOM:
case ACTIVITY_CHAT | IN_UNION_ROOM:
case ACTIVITY_CARD | IN_UNION_ROOM:
case ACTIVITY_ACCEPT | IN_UNION_ROOM:
case ACTIVITY_DECLINE | IN_UNION_ROOM:
case ACTIVITY_NPCTALK | IN_UNION_ROOM:
DespawnGroupLeader(leaderId);
AssembleGroup(leaderId, gameData);
2019-04-01 20:09:56 +02:00
break;
}
}
static void DespawnGroupLeaderAndMembers(u32 leaderId, struct RfuGameData *gameData)
2019-04-01 20:09:56 +02:00
{
s32 i;
DespawnGroupLeader(leaderId);
for (i = 0; i < MAX_RFU_PLAYERS; i++)
DespawnGroupMember(leaderId, i);
2019-04-01 20:09:56 +02:00
}
2020-06-01 16:23:12 +02:00
static void UpdateUnionRoomPlayerSprites(struct WirelessLink_URoom *uroom)
2019-04-01 20:09:56 +02:00
{
s32 i;
struct RfuPlayer * leaders;
2020-06-01 16:23:12 +02:00
sUnionObjRefreshTimer = 0;
for (i = 0, leaders = uroom->playerList->players; i < MAX_UNION_ROOM_LEADERS; i++)
2019-04-01 20:09:56 +02:00
{
if (leaders[i].groupScheduledAnim == UNION_ROOM_SPAWN_IN)
SpawnGroupLeaderAndMembers(i, &leaders[i].rfu.data);
else if (leaders[i].groupScheduledAnim == UNION_ROOM_SPAWN_OUT)
DespawnGroupLeaderAndMembers(i, &leaders[i].rfu.data);
2019-04-01 20:09:56 +02:00
}
}
2020-06-01 16:23:12 +02:00
void ScheduleUnionRoomPlayerRefresh(struct WirelessLink_URoom *uroom)
2019-04-01 20:09:56 +02:00
{
2020-06-01 16:23:12 +02:00
sUnionObjRefreshTimer = 300;
2019-04-01 20:09:56 +02:00
}
2020-06-01 16:23:12 +02:00
void HandleUnionRoomPlayerRefresh(struct WirelessLink_URoom *uroom)
2019-04-01 20:09:56 +02:00
{
2020-06-01 16:23:12 +02:00
if (++sUnionObjRefreshTimer > 300)
UpdateUnionRoomPlayerSprites(uroom);
2019-04-01 20:09:56 +02:00
}
bool32 TryInteractWithUnionRoomMember(struct RfuPlayerList *list, s16 *memberIdPtr, s16 *leaderIdPtr, u8 *spriteIds)
2019-04-01 20:09:56 +02:00
{
s16 x, y;
s32 i, memberId;
struct RfuPlayer * leaders;
2020-06-09 00:16:57 +02:00
if (!IsPlayerStandingStill())
2019-04-01 20:09:56 +02:00
return FALSE;
2019-04-01 20:09:56 +02:00
GetXYCoordsOneStepInFrontOfPlayer(&x, &y);
for (i = 0, leaders = list->players; i < MAX_UNION_ROOM_LEADERS; i++)
2019-04-01 20:09:56 +02:00
{
for (memberId = 0; memberId < MAX_RFU_PLAYERS; memberId++)
2019-04-01 20:09:56 +02:00
{
s32 id = UR_PLAYER_SPRITE_ID(i, memberId);
// Is the player in front of a group member position?
if (x != sUnionRoomPlayerCoords[i][0] + sUnionRoomGroupOffsets[memberId][0] + 7)
2019-04-01 20:09:56 +02:00
continue;
if (y != sUnionRoomPlayerCoords[i][1] + sUnionRoomGroupOffsets[memberId][1] + 7)
2019-04-01 20:09:56 +02:00
continue;
// Has a group member spawned at this position?
if (IsVirtualObjectInvisible(id - UR_SPRITE_START_ID))
2019-04-01 20:09:56 +02:00
continue;
if (IsVirtualObjectAnimating(id - UR_SPRITE_START_ID))
2019-04-01 20:09:56 +02:00
continue;
if (leaders[i].groupScheduledAnim != UNION_ROOM_SPAWN_IN)
2019-04-01 20:09:56 +02:00
continue;
// Interaction attempt successful, face player
SetUnionRoomObjectFacingDirection(memberId, i, sOppositeFacingDirection[GetPlayerFacingDirection()]);
*memberIdPtr = memberId;
*leaderIdPtr = i;
2019-04-01 20:09:56 +02:00
return TRUE;
}
}
return FALSE;
}
static void SetUnionRoomObjectFacingDirection(s32 memberId, s32 leaderId, u8 newDirection)
2019-04-01 20:09:56 +02:00
{
TurnVirtualObject(MAX_RFU_PLAYERS * leaderId - UR_SPRITE_START_ID + memberId, newDirection);
2020-06-01 16:23:12 +02:00
// should be line below, but order is swapped here
// TurnVirtualObject(UR_PLAYER_SPRITE_ID(leaderId, memberId) - UR_SPRITE_START_ID, newDirection);
2019-04-01 20:09:56 +02:00
}
void UpdateUnionRoomMemberFacing(u32 memberId, u32 leaderId, struct RfuPlayerList *list)
2019-04-01 20:09:56 +02:00
{
return SetUnionRoomObjectFacingDirection(memberId, leaderId, GetNewFacingDirectionForUnionRoomPlayer(memberId, leaderId, &list->players[leaderId].rfu.data));
2019-04-01 20:09:56 +02:00
}