fix trainer approach

This commit is contained in:
cawtds 2025-02-12 00:29:10 +01:00
parent 181c40c6a9
commit 4e6334f76b

View File

@ -18,7 +18,7 @@
typedef u8 (*TrainerApproachFunc)(struct ObjectEvent *, s16, s16, s16);
typedef bool32 (*TrainerSeeFunc)(u8, struct Task *, struct ObjectEvent *);
static u32 CheckTrainer(u8 trainerObjId);
static u8 CheckTrainer(u8 trainerObjId);
static u8 GetTrainerApproachDistance(struct ObjectEvent * trainerObj);
static u8 GetTrainerApproachDistanceSouth(struct ObjectEvent * trainerObj, s16 range, s16 x, s16 y);
static u8 GetTrainerApproachDistanceNorth(struct ObjectEvent * trainerObj, s16 range, s16 x, s16 y);
@ -178,31 +178,60 @@ bool8 CheckForTrainersWantingBattle(void)
}
}
static u32 CheckTrainer(u8 trainerObjId)
static u8 CheckTrainer(u8 objectEventId)
{
const u8 *script = GetObjectEventScriptPointerByObjectEventId(trainerObjId);
u32 numTrainers = 1;
u32 approachDistance;
if (GetTrainerFlagFromScriptPointer(script))
return FALSE;
approachDistance = GetTrainerApproachDistance(&gObjectEvents[trainerObjId]);
if (approachDistance != 0)
const u8 *scriptPtr, *trainerBattlePtr;
u8 numTrainers = 1;
u8 approachDistance = GetTrainerApproachDistance(&gObjectEvents[objectEventId]);
if (approachDistance == 0)
return 0;
trainerBattlePtr = scriptPtr = GetObjectEventScriptPointerByObjectEventId(objectEventId);
struct ScriptContext ctx;
if (RunScriptImmediatelyUntilEffect(SCREFF_V1 | SCREFF_SAVE | SCREFF_HARDWARE | SCREFF_TRAINERBATTLE, scriptPtr, &ctx))
{
if (script[1] == TRAINER_BATTLE_DOUBLE && GetMonsStateToDoubles())
if (*ctx.scriptPtr == 0x5c) // trainerbattle
trainerBattlePtr = ctx.scriptPtr;
else
trainerBattlePtr = NULL;
}
else
{
return 0; // no effect
}
if (trainerBattlePtr)
{
if (GetTrainerFlagFromScriptPointer(trainerBattlePtr))
return 0;
}
else
{
numTrainers = 0xFF;
}
if (trainerBattlePtr)
{
TrainerBattleParameter *temp = (TrainerBattleParameter *)(trainerBattlePtr + 1);
if (temp->params.mode == TRAINER_BATTLE_DOUBLE
|| temp->params.mode == TRAINER_BATTLE_REMATCH_DOUBLE
|| temp->params.mode == TRAINER_BATTLE_CONTINUE_SCRIPT_DOUBLE)
{
if (GetMonsStateToDoubles_2() != PLAYER_HAS_TWO_USABLE_MONS)
return 0;
numTrainers = 2;
}
gApproachingTrainers[gNoOfApproachingTrainers].objectEventId = trainerObjId;
gApproachingTrainers[gNoOfApproachingTrainers].trainerScriptPtr = script;
gApproachingTrainers[gNoOfApproachingTrainers].radius = approachDistance;
TrainerApproachPlayer(&gObjectEvents[trainerObjId], approachDistance - 1);
gNoOfApproachingTrainers++;
return numTrainers;
}
return 0;
gApproachingTrainers[gNoOfApproachingTrainers].objectEventId = objectEventId;
gApproachingTrainers[gNoOfApproachingTrainers].trainerScriptPtr = scriptPtr;
gApproachingTrainers[gNoOfApproachingTrainers].radius = approachDistance;
TrainerApproachPlayer(&gObjectEvents[objectEventId], approachDistance - 1);
gNoOfApproachingTrainers++;
return numTrainers;
}
static u8 GetTrainerApproachDistance(struct ObjectEvent *trainerObj)