Skip to content

Commit

Permalink
isBattle refactor (#1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
z64a authored Oct 1, 2024
1 parent ba4499e commit 5929e49
Show file tree
Hide file tree
Showing 84 changed files with 326 additions and 302 deletions.
2 changes: 1 addition & 1 deletion include/common_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ typedef struct GameStatus {
/* 0x06A */ s8 demoStickX;
/* 0x06B */ s8 demoStickY;
/* 0x06C */ s32 mainScriptID;
/* 0x070 */ s8 isBattle;
/* 0x070 */ s8 context;
/* 0x071 */ s8 demoState; // see DemoState enum
/* 0x072 */ s8 nextDemoScene; /* which part of the demo to play next */
/* 0x073 */ u8 contBitPattern;
Expand Down
2 changes: 1 addition & 1 deletion include/dead.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#define cos_deg dead_cos_deg
#define sin_cos_deg dead_sin_cos_deg
#define gPlayerStatus dead_gPlayerStatus
#define is_point_within_region dead_is_point_within_region
#define is_point_outside_territory dead_is_point_outside_territory
#define npc_raycast_down_sides dead_npc_raycast_down_sides
#define basic_ai_check_player_dist dead_basic_ai_check_player_dist
#define fx_emote dead_fx_emote
Expand Down
24 changes: 15 additions & 9 deletions include/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -3526,6 +3526,12 @@ enum EffectGfxDataFlags {

#include "move_enum.h"

enum GameContext {
CONTEXT_WORLD = 0,
CONTEXT_BATTLE = 1,
CONTEXT_PAUSE = 2,
};

enum DemoState {
DEMO_STATE_NONE = 0,
DEMO_STATE_ACTIVE = 1,
Expand Down Expand Up @@ -4556,14 +4562,14 @@ enum EnemyFlags {

// used with enemy->aiFlags
enum EnemyAIFlags {
ENEMY_AI_FLAG_1 = 0x00000001,
ENEMY_AI_FLAG_2 = 0x00000002, // do not move; do not sense player
ENEMY_AI_FLAG_SUSPEND = 0x00000004,
ENEMY_AI_FLAG_8 = 0x00000008,
ENEMY_AI_FLAG_10 = 0x00000010,
ENEMY_AI_FLAG_20 = 0x00000020,
ENEMY_AI_FLAG_40 = 0x00000040,
ENEMY_AI_FLAG_80 = 0x00000080,
AI_FLAG_1 = 0x00000001,
AI_FLAG_CANT_DETECT_PLAYER = 0x00000002,
AI_FLAG_SUSPEND = 0x00000004,
AI_FLAG_SKIP_EMOTE_AFTER_FLEE = 0x00000008,
AI_FLAG_SKIP_IDLE_ANIM_AFTER_FLEE = 0x00000010,
AI_FLAG_OUTSIDE_TERRITORY = 0x00000020,
AI_FLAG_NEEDS_HEADING = 0x00000040,
AI_FLAG_80 = 0x00000080,
};

enum EnemyAIStates {
Expand Down Expand Up @@ -4613,7 +4619,7 @@ enum EnemyDetectFlags {
AI_DETECT_FLAG_8 = 0x08,
};

enum EnemyTerritoryFlags {
enum TerritoryFlags {
AI_TERRITORY_IGNORE_HIDING = 0x01, // bow and sushi dont prevent enemy detection
AI_TERRITORY_IGNORE_ELEVATION = 0x02, // vertical size of detection volume is ignored
};
Expand Down
2 changes: 1 addition & 1 deletion include/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ s32 suspend_all_script(s32 id);
s32 resume_all_script(s32 id);

s32 create_shadow_type(s32 type, f32 x, f32 y, f32 z);
s32 is_point_within_region(s32 shape, f32 pointX, f32 pointY, f32 centerX, f32 centerY, f32 sizeX, f32 sizeZ);
b32 is_point_outside_territory(s32 shape, f32 pointX, f32 pointY, f32 centerX, f32 centerY, f32 sizeX, f32 sizeZ);
PlayerData* get_player_data(void);

b32 npc_raycast_down_around(s32, f32*, f32*, f32*, f32*, f32, f32);
Expand Down
2 changes: 1 addition & 1 deletion include/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ extern EncounterStatus gCurrentEncounter;

#endif

s32 basic_ai_check_player_dist(EnemyDetectVolume* arg0, Enemy* arg1, f32 arg2, f32 arg3, s8 arg4);
b32 basic_ai_check_player_dist(EnemyDetectVolume* arg0, Enemy* arg1, f32 arg2, f32 arg3, b8 arg4);

/// The default Npc::onUpdate and Npc::onRender callback.
void STUB_npc_callback(Npc*);
Expand Down
6 changes: 3 additions & 3 deletions src/111f0_len_860.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void state_step_enter_world(void) {
break;
}

gGameStatusPtr->isBattle = FALSE;
gGameStatusPtr->context = CONTEXT_WORLD;
gGameStatusPtr->debugScripts = DEBUG_SCRIPTS_NONE;

if (!gLoadedFromFileSelect) {
Expand Down Expand Up @@ -155,7 +155,7 @@ void state_step_change_map(void) {
if (gMapTransitionStateTime != 0) {
gMapTransitionStateTime--;
} else {
gGameStatusPtr->isBattle = FALSE;
gGameStatusPtr->context = CONTEXT_WORLD;
gGameStatusPtr->debugScripts = DEBUG_SCRIPTS_NONE;
load_map_by_IDs(gGameStatusPtr->areaID, gGameStatusPtr->mapID, LOAD_FROM_MAP);
set_time_freeze_mode(TIME_FREEZE_NORMAL);
Expand Down Expand Up @@ -230,7 +230,7 @@ void state_step_game_over(void) {
if (gMapTransitionStateTime != 0) {
gMapTransitionStateTime--;
} else {
gGameStatusPtr->isBattle = FALSE;
gGameStatusPtr->context = CONTEXT_WORLD;
gGameStatusPtr->debugScripts = DEBUG_SCRIPTS_NONE;
load_map_by_IDs(gGameStatusPtr->areaID, gGameStatusPtr->mapID, LOAD_FROM_MAP);
nuContRmbForceStopEnd();
Expand Down
65 changes: 34 additions & 31 deletions src/23680.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,16 @@ void spawn_drops(Enemy* enemy) {
s32 get_coin_drop_amount(Enemy* enemy) {
EncounterStatus* currentEncounter = &gCurrentEncounter;
EnemyDrops* enemyDrops = enemy->drops;
s32 maxCoinBonus = enemyDrops->maxCoinBonus;
s32 max = enemyDrops->maxCoinBonus;
s32 amt = enemyDrops->minCoinBonus;
s32 minTemp = enemyDrops->minCoinBonus;

if (maxCoinBonus < amt) {
if (max < amt) {
amt = enemyDrops->maxCoinBonus;
maxCoinBonus = enemyDrops->minCoinBonus;
max = enemyDrops->minCoinBonus;
}

minTemp = maxCoinBonus - amt;
minTemp = max - amt;
if ((amt < 0) || (minTemp != 0)) {
amt = rand_int(minTemp) - -amt;
}
Expand Down Expand Up @@ -392,32 +392,32 @@ s32 func_80048F0C(void) {
return 0;
}

s32 is_point_within_region(s32 shape, f32 pointX, f32 pointY, f32 centerX, f32 centerY, f32 sizeX, f32 sizeZ) {
b32 is_point_outside_territory(s32 shape, f32 centerX, f32 centerZ, f32 pointX, f32 pointZ, f32 sizeX, f32 sizeZ) {
f32 dist1;
f32 dist2;

switch (shape) {
case 0:
dist1 = dist2D(pointX, pointY, centerX, centerY);
case SHAPE_CYLINDER:
dist1 = dist2D(centerX, centerZ, pointX, pointZ);
return (sizeX < dist1);
case 1:
dist1 = dist2D(pointX, 0, centerX, 0);
dist2 = dist2D(0, pointY, 0, centerY);
case SHAPE_RECT:
dist1 = dist2D(centerX, 0, pointX, 0);
dist2 = dist2D(0, centerZ, 0, pointZ);
return ((sizeX < dist1) || (sizeZ < dist2));
default:
return FALSE;
}
}

s32 basic_ai_check_player_dist(EnemyDetectVolume* territory, Enemy* enemy, f32 radius, f32 fwdPosOffset, s8 useWorldYaw) {
b32 basic_ai_check_player_dist(EnemyDetectVolume* territory, Enemy* enemy, f32 radius, f32 fwdPosOffset, b8 useWorldYaw) {
Npc* npc = get_npc_unsafe(enemy->npcID);
PlayerStatus* playerStatus = &gPlayerStatus;
PartnerStatus* partnerStatus;
f32 x, y, z;
f32 dist;
s32 skipCheckForPlayer;

if (enemy->aiFlags & ENEMY_AI_FLAG_2) {
if (enemy->aiFlags & AI_FLAG_CANT_DETECT_PLAYER) {
return FALSE;
}

Expand All @@ -441,7 +441,7 @@ s32 basic_ai_check_player_dist(EnemyDetectVolume* territory, Enemy* enemy, f32 r
return FALSE;
}

if (territory->sizeX | territory->sizeZ && is_point_within_region(territory->shape,
if (territory->sizeX | territory->sizeZ && is_point_outside_territory(territory->shape,
territory->pointX, territory->pointZ,
playerStatus->pos.x, playerStatus->pos.z,
territory->sizeX, territory->sizeZ)) {
Expand Down Expand Up @@ -563,8 +563,8 @@ void basic_ai_wander_init(Evt* script, MobileAISettings* npcAISettings, EnemyDet
npc->moveSpeed = enemy->territory->wander.moveSpeedOverride / 32767.0;
}

enemy->aiFlags &= ~ENEMY_AI_FLAG_40;
enemy->aiFlags &= ~ENEMY_AI_FLAG_20;
enemy->aiFlags &= ~AI_FLAG_NEEDS_HEADING;
enemy->aiFlags &= ~AI_FLAG_OUTSIDE_TERRITORY;
script->AI_TEMP_STATE = AI_STATE_WANDER;
}

Expand All @@ -576,6 +576,7 @@ void basic_ai_wander(Evt* script, MobileAISettings* aiSettings, EnemyDetectVolum
EffectInstance* sp34;
f32 yaw;

// search for the player
if (aiSettings->playerSearchInterval >= 0) {
if (script->functionTemp[1] <= 0) {
script->functionTemp[1] = aiSettings->playerSearchInterval;
Expand All @@ -588,8 +589,8 @@ void basic_ai_wander(Evt* script, MobileAISettings* aiSettings, EnemyDetectVolum
npc->yaw = yaw;
ai_enemy_play_sound(npc, SOUND_AI_ALERT_A, SOUND_PARAM_MORE_QUIET);
fx_emote(EMOTE_EXCLAMATION, npc, 0, npc->collisionHeight, 1.0f, 2.0f, -20.0f, 15, &sp34);
enemy->aiFlags &= ~ENEMY_AI_FLAG_40;
enemy->aiFlags &= ~ENEMY_AI_FLAG_20;
enemy->aiFlags &= ~AI_FLAG_NEEDS_HEADING;
enemy->aiFlags &= ~AI_FLAG_OUTSIDE_TERRITORY;

if (enemy->npcSettings->actionFlags & AI_ACTION_JUMP_WHEN_SEE_PLAYER) {
script->AI_TEMP_STATE = AI_STATE_ALERT_INIT;
Expand All @@ -603,37 +604,39 @@ void basic_ai_wander(Evt* script, MobileAISettings* aiSettings, EnemyDetectVolum
script->functionTemp[1]--;
}

// check if the wander we've reached the boundary of the territory
if (is_point_within_region(enemy->territory->wander.wanderShape,
// check if we've wandered beyond the boundary of the territory
if (is_point_outside_territory(enemy->territory->wander.wanderShape,
enemy->territory->wander.centerPos.x,
enemy->territory->wander.centerPos.z,
npc->pos.x,
npc->pos.z,
enemy->territory->wander.wanderSize.x,
enemy->territory->wander.wanderSize.z)
&& npc->moveSpeed < dist2D(enemy->territory->wander.centerPos.x, enemy->territory->wander.centerPos.z, npc->pos.x, npc->pos.z)) {
if (!(enemy->aiFlags & ENEMY_AI_FLAG_20)) {
enemy->aiFlags |= (ENEMY_AI_FLAG_20 | ENEMY_AI_FLAG_40);
&& npc->moveSpeed < dist2D(enemy->territory->wander.centerPos.x, enemy->territory->wander.centerPos.z, npc->pos.x, npc->pos.z)
) {
if (!(enemy->aiFlags & AI_FLAG_OUTSIDE_TERRITORY)) {
enemy->aiFlags |= (AI_FLAG_OUTSIDE_TERRITORY | AI_FLAG_NEEDS_HEADING);
}

if (enemy->aiFlags & ENEMY_AI_FLAG_40) {
if (enemy->aiFlags & AI_FLAG_NEEDS_HEADING) {
npc->yaw = clamp_angle(atan2(npc->pos.x, npc->pos.z, enemy->territory->wander.centerPos.x, enemy->territory->wander.centerPos.z));
enemy->aiFlags &= ~ENEMY_AI_FLAG_40;
enemy->aiFlags &= ~AI_FLAG_NEEDS_HEADING;
}

// if current heading is deflected by a wall, recalculate yaw to continue pursuing centerPos
x = npc->pos.x;
y = npc->pos.y;
z = npc->pos.z;
if (npc_test_move_simple_with_slipping(npc->collisionChannel, &x, &y, &z, 2.0 * npc->moveSpeed, npc->yaw, npc->collisionHeight, npc->collisionDiameter)) {
yaw = clamp_angle(atan2(npc->pos.x, npc->pos.z, enemy->territory->wander.centerPos.x, enemy->territory->wander.centerPos.z));
enemy->aiFlags &= ~ENEMY_AI_FLAG_40;
enemy->aiFlags &= ~AI_FLAG_NEEDS_HEADING;
ai_check_fwd_collisions(npc, 5.0f, &yaw, NULL, NULL, NULL);
npc->yaw = yaw;
}
stillWithinTerritory = TRUE;
} else if (enemy->aiFlags & ENEMY_AI_FLAG_20) {
enemy->aiFlags &= ~ENEMY_AI_FLAG_20;
enemy->aiFlags &= ~ENEMY_AI_FLAG_40;
} else if (enemy->aiFlags & AI_FLAG_OUTSIDE_TERRITORY) {
enemy->aiFlags &= ~AI_FLAG_OUTSIDE_TERRITORY;
enemy->aiFlags &= ~AI_FLAG_NEEDS_HEADING;
}

// perform the motion
Expand Down Expand Up @@ -853,7 +856,7 @@ API_CALLABLE(BasicAI_Main) {
territory.halfHeight = 65.0f;
territory.detectFlags = 0;

if (isInitialCall || enemy->aiFlags & ENEMY_AI_FLAG_SUSPEND) {
if (isInitialCall || enemy->aiFlags & AI_FLAG_SUSPEND) {
script->AI_TEMP_STATE = AI_STATE_WANDER_INIT;
npc->duration = 0;

Expand All @@ -868,14 +871,14 @@ API_CALLABLE(BasicAI_Main) {
npc->flags |= NPC_FLAG_FLYING;
}

if (enemy->aiFlags & ENEMY_AI_FLAG_SUSPEND) {
if (enemy->aiFlags & AI_FLAG_SUSPEND) {
script->AI_TEMP_STATE = AI_STATE_SUSPEND;
script->functionTemp[1] = AI_STATE_WANDER_INIT;
} else if (enemy->flags & ENEMY_FLAG_BEGIN_WITH_CHASING) {
script->AI_TEMP_STATE = AI_STATE_CHASE_INIT;
}

enemy->aiFlags &= ~ENEMY_AI_FLAG_SUSPEND;
enemy->aiFlags &= ~AI_FLAG_SUSPEND;
enemy->flags &= ~ENEMY_FLAG_BEGIN_WITH_CHASING;
}

Expand Down
2 changes: 1 addition & 1 deletion src/25AF0.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ s32 ai_check_fwd_collisions(Npc* npc, f32 time, f32* outYaw, f32* outDistFwd, f3
f32 cwHitDist = -1.0f;
f32 ccwHitDist = -1.0f;
f32 yaw;
s32 fwdHit;
b32 fwdHit;

x1 = npc->pos.x;
y1 = npc->pos.y;
Expand Down
4 changes: 2 additions & 2 deletions src/77480.c
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ void check_for_pulse_stone(void) {
return;
}

if (gGameStatusPtr->areaID != AREA_SBK || gGameStatusPtr->isBattle) {
if (gGameStatusPtr->areaID != AREA_SBK || gGameStatusPtr->context != CONTEXT_WORLD) {
return;
}

Expand Down Expand Up @@ -1318,7 +1318,7 @@ void clear_interact_prompt(void) {
void update_partner_timers(void) {
PlayerData* playerData = &gPlayerData;

if (!gGameStatusPtr->isBattle) {
if (gGameStatusPtr->context == CONTEXT_WORLD) {
s32 i;

for (i = 1; i < ARRAY_COUNT(playerData->partnerUnlockedTime); i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/8a860_len_3f30.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void destroy_popup_menu(void) {
gPopupMenu->popupType == POPUP_TYPE_TRADE_FOR_BADGE ||
gPopupMenu->popupType == POPUP_TYPE_UPGRADE_PARTNER ||
gPopupMenu->popupType == POPUP_TYPE_USE_KEY
) && !gGameStatusPtr->isBattle) {
) && gGameStatusPtr->context == CONTEXT_WORLD) {
if (!PopupMenu_WasStatusBarIgnoringChanges) {
status_bar_respond_to_changes();
}
Expand Down
Loading

0 comments on commit 5929e49

Please sign in to comment.