From a846d29644e2d2270617147088e942e1eb40e98e Mon Sep 17 00:00:00 2001 From: MokhaLeee Date: Fri, 31 May 2024 16:01:47 +0800 Subject: [PATCH] review on AI related code --- include/bmsave.h | 8 +- include/bmunit.h | 27 +++- include/cp_common.h | 94 ++++++++++++-- include/cp_perform.h | 4 +- include/cp_script.h | 5 +- include/cp_utility.h | 4 +- src/bmsave.c | 16 +-- src/cp_0803E2F4.c | 253 +++++++++++++++++------------------- src/cp_battle.c | 4 +- src/cp_data.c | 126 +++++++++--------- src/cp_decide.c | 2 +- src/cp_perform.c | 29 +++-- src/cp_phase.c | 4 +- src/cp_script.c | 48 +++---- src/cp_utility.c | 42 +++--- src/cpextra_80407F0.c | 48 +++---- src/eventscr.c | 8 +- src/eventscr3.c | 10 +- src/mapanim_specialeffect.c | 4 +- 19 files changed, 409 insertions(+), 327 deletions(-) diff --git a/include/bmsave.h b/include/bmsave.h index ad858b504..820507044 100755 --- a/include/bmsave.h +++ b/include/bmsave.h @@ -236,9 +236,9 @@ struct SuspendSavePackedUnit { /* Suspend Data */ /* 11 */ u8 aiFlags; /* 12 */ u8 ranks[8]; /* 1A */ u8 supports[UNIT_SUPPORT_MAX_COUNT]; - /* 21 */ u8 ai1data; + /* 21 */ u8 ai_a_pc; /* 22 */ u8 ai2; - /* 23 */ u8 ai2data; + /* 23 */ u8 ai_b_pc; /* 24 */ u32 level : 5; u32 xPos : 6; @@ -262,8 +262,8 @@ struct SuspendSavePackedUnit { /* Suspend Data */ /* 30 */ u8 ballistaIndex; - /* 31 */ u8 _u46; - /* 32 */ u16 ai3And4; + /* 31 */ u8 ai_counter; + /* 32 */ u16 ai_config; /* 34 */ } BITPACKED; diff --git a/include/bmunit.h b/include/bmunit.h index cbf7f3fb0..0c39c0295 100644 --- a/include/bmunit.h +++ b/include/bmunit.h @@ -159,20 +159,33 @@ struct Unit /* 32 */ u8 supports[UNIT_SUPPORT_MAX_COUNT]; /* 39 */ s8 supportBits; + + /* pad */ /* 3A */ u8 _u3A; /* 3B */ u8 _u3B; /* 3C */ struct SMSHandle* pMapSpriteHandle; - /* 40 */ u16 ai3And4; - /* 42 */ u8 ai1; - /* 43 */ u8 ai1data; - /* 44 */ u8 ai2; - /* 45 */ u8 ai2data; - /* 46 */ u8 _u46; + /* 40 */ u16 ai_config; // a bitmask + /* 42 */ u8 ai1; // enum to gAi1ScriptTable + /* 43 */ u8 ai_a_pc; + /* 44 */ u8 ai2; // enum to gAi2ScriptTable + /* 45 */ u8 ai_b_pc; + /* 46 */ u8 ai_counter; + + /* pad */ /* 47 */ u8 _u47; }; +enum udef_ai_index { + UDEF_AIIDX_AI_A, + UDEF_AIIDX_AI_B, + UDEF_AIIDX_AI_CONF_L, + UDEF_AIIDX_AI_CONF_H, + + UDEF_AIIDX_MAX +}; + struct UnitDefinition { /* 00 */ u8 charIndex; @@ -196,7 +209,7 @@ struct UnitDefinition /* 0C */ u8 items[UNIT_DEFINITION_ITEM_COUNT]; - /* 10 */ u8 ai[4]; + /* 10 */ u8 ai[UDEF_AIIDX_MAX]; } BITPACKED; enum diff --git a/include/cp_common.h b/include/cp_common.h index a97312b4a..706d74f1e 100644 --- a/include/cp_common.h +++ b/include/cp_common.h @@ -18,7 +18,7 @@ struct AiState /* 80 */ u32 specialItemFlags; /* 84 */ u8 unk84; /* 85 */ u8 bestBlueMov; - /* 86 */ u8 unk86[8]; + /* 86 */ u8 cmd_result[8]; }; struct AiDecision @@ -40,16 +40,18 @@ struct AiDecision enum { + // gAiState.flags AI_FLAGS_NONE = 0, AI_FLAG_0 = (1 << 0), - AI_FLAG_1 = (1 << 1), + AI_FLAG_STAY = (1 << 1), AI_FLAG_BERSERKED = (1 << 2), AI_FLAG_3 = (1 << 3), }; enum { + // Unit::aiFlags AI_UNIT_FLAG_0 = (1 << 0), AI_UNIT_FLAG_1 = (1 << 1), AI_UNIT_FLAG_2 = (1 << 2), @@ -59,6 +61,80 @@ enum AI_UNIT_FLAG_6 = (1 << 6), }; +enum +{ + // "ai3" + + // 0~2 healing ai theshold select + AI_UNIT_CONFIG_HEALTHRESHOLD_SHIFT = 0, + AI_UNIT_CONFIG_HEALTHRESHOLD_BITS = 3, + AI_UNIT_CONFIG_HEALTHRESHOLD_MASK = ((1 << AI_UNIT_CONFIG_HEALTHRESHOLD_BITS) - 1) << AI_UNIT_CONFIG_HEALTHRESHOLD_SHIFT, + + // 3~7 combat target decide weight set select + AI_UNIT_CONFIG_COMBATWEIGHT_SHIFT = 3, + AI_UNIT_CONFIG_COMBATWEIGHT_BITS = 5, + AI_UNIT_CONFIG_COMBATWEIGHT_MASK = ((1 << AI_UNIT_CONFIG_COMBATWEIGHT_BITS) - 1) << AI_UNIT_CONFIG_COMBATWEIGHT_SHIFT, + + // "ai4" + + // 8+ + + // 13 + AI_UNIT_CONFIG_FLAG_STAY = 1 << 13, +}; + +enum +{ + AI_A_00 = 0x00, + AI_A_01 = 0x01, + AI_A_02 = 0x02, + AI_A_03 = 0x03, + AI_A_04 = 0x04, + AI_A_05 = 0x05, + AI_A_06 = 0x06, + AI_A_07 = 0x07, + AI_A_08 = 0x08, + AI_A_09 = 0x09, + AI_A_0A = 0x0A, + AI_A_0B = 0x0B, + AI_A_0C = 0x0C, + AI_A_0D = 0x0D, + AI_A_0E = 0x0E, + AI_A_0F = 0x0F, + AI_A_10 = 0x10, + AI_A_11 = 0x11, + AI_A_12 = 0x12, + AI_A_13 = 0x13, + AI_A_14 = 0x14, + + AI_A_INVALID +}; + +enum +{ + AI_B_00 = 0x00, + AI_B_01 = 0x01, + AI_B_02 = 0x02, + AI_B_03 = 0x03, + AI_B_04 = 0x04, + AI_B_05 = 0x05, + AI_B_06 = 0x06, + AI_B_07 = 0x07, + AI_B_08 = 0x08, + AI_B_09 = 0x09, + AI_B_0A = 0x0A, + AI_B_0B = 0x0B, + AI_B_0C = 0x0C, + AI_B_0D = 0x0D, + AI_B_0E = 0x0E, + AI_B_0F = 0x0F, + AI_B_10 = 0x10, + AI_B_11 = 0x11, + AI_B_12 = 0x12, + + AI_B_INVALID +}; + enum { AI_ACTION_NONE = 0, // move only @@ -146,13 +222,13 @@ s8 AiUpdateGetUnitIsHealing(struct Unit* unit); s8 AiTryHealSelf(void); s8 AiTryMoveTowardsEscape(void); // ??? GetEscapePointStructThingMaybe(???); -s8 sub_803E900(void); -s8 sub_803E93C(u16*); -void sub_803EA58(int, int, u16*, u16*, u16*); -// ??? sub_803EBA4(???); -void sub_803EBF0(u16, u16, u16, u16*); +s8 AiCanEquip(void); +s8 AiEquipGetFlags(u16*); +void AiEquipGetDanger(int, int, u16*, u16*, u16*); +// ??? AiEquipBestMatch(???); +void AiEquipBestConsideringDanger(u16, u16, u16, u16*); // ??? sub_803EC18(???); -// ??? sub_803EC54(???); +// ??? AiIsWithinFlyingDistance(???); // ??? StoreItemAndGetUnitAttack(???); void AiTryDanceOrStealAfterMove(void); void AiTryActionAfterMove(void); @@ -211,7 +287,7 @@ void InitAiMoveMapForUnit(struct Unit*); // ??? sub_8040E98(???); // ??? sub_8040EC8(???); // ??? sub_8040EF8(???); -void sub_8040F28(int x, int y, const s8* cost); // AiMapRangeFillMovementWithPassableWalls +void GenerateExtendedMovementMapOnRangeNeglectWall(int x, int y, const s8* cost); // AiMapRangeFillMovementWithPassableWalls void sub_8040F54(int, int, struct Unit*); // ??? sub_8040F88(???); // ??? sub_8040FBC(???); diff --git a/include/cp_perform.h b/include/cp_perform.h index 06e8696dc..c0f1b759c 100644 --- a/include/cp_perform.h +++ b/include/cp_perform.h @@ -23,11 +23,11 @@ void AiRefreshMap(void); // ??? CpPerform_MoveCameraOntoTarget(???); // ??? CpPerform_PerformAction(???); // ??? CpPerform_WaitAction(???); -// ??? CpPerform_803A63C(???); +// ??? CpPerform_Cleanup(???); // ??? AiDummyAction(???); // ??? AiEscapeAction(???); // ??? AiWaitAndClearScreenAction(???); -// ??? CpPerform_803A6D0(???); +// ??? CpPerform_EquipBest(???); extern struct ProcCmd CONST_DATA gProcScr_AiTargetCursor[]; extern struct ProcCmd CONST_DATA gProcScr_CpPerform[]; diff --git a/include/cp_script.h b/include/cp_script.h index 10a9d91de..edba6aa28 100644 --- a/include/cp_script.h +++ b/include/cp_script.h @@ -1,8 +1,9 @@ #ifndef GUARD_CP_SCRIPT_H #define GUARD_CP_SCRIPT_H -enum -{ +enum unit_ai1_data_enum { + // Unit::ai_a + AI_CMD_CONDITIONAL = 0x00, AI_CMD_CALL_FUNC = 0x01, AI_CMD_SET_AI = 0x02, diff --git a/include/cp_utility.h b/include/cp_utility.h index d31407e19..7ee59304a 100644 --- a/include/cp_utility.h +++ b/include/cp_utility.h @@ -5,7 +5,7 @@ s8 AiCompare(const u8* left, u8 op, u32 right); s8 AiFindTargetInReachByCharId(int uid, struct Vec2* out); s8 AiFindTargetInReachByClassId(int classId, struct Vec2* out); s8 AiFindTargetInReachByFunc(s8(*func)(struct Unit* unit), struct Vec2* out); -s8 sub_803AA40(s8(*func)(struct Unit* unit), struct Vec2* out); +s8 AiFindTargetInReachNeglectWallByFunc(s8(*func)(struct Unit* unit), struct Vec2* out); void AiRandomMove(void); s8 AiReachesByBirdsEyeDistance(struct Unit* unit, struct Unit* other, u16 item); s8 AiCouldReachByBirdsEyeDistance(struct Unit* unit, struct Unit* other, u16 item); @@ -31,7 +31,7 @@ s8 AiFindSafestReachableLocation(struct Unit*, struct Vec2*); s8 AiFindPillageLocation(struct Vec2* out, u8* outItemSlot); // ??? AiGetChestUnlockItemSlot(???); void AiTryMoveTowards(s16, s16, u8, u8, u8); -void sub_803BBF4(s16 x, s16 y, u8 action, u8 maxDanger, u8 unk); +void AiTryMoveTowardsNeglectWall(s16 x, s16 y, u8 action, u8 maxDanger, u8 unk); s8 AiGetUnitClosestValidPosition(struct Unit* unit, s16 x, s16 y, struct Vec2* out); u8 AiGetClassRank(u8 classId); s8 AiUnitWithCharIdExists(u16 uid); diff --git a/src/bmsave.c b/src/bmsave.c index 55115b336..a2317c7d5 100755 --- a/src/bmsave.c +++ b/src/bmsave.c @@ -675,11 +675,11 @@ void EncodeSuspendSavePackedUnit(struct Unit *unit, void *buf) ai1_byte |= 0x80; unit_su->ai1 = ai1_byte; - unit_su->ai1data = unit->ai1data; + unit_su->ai_a_pc = unit->ai_a_pc; unit_su->ai2 = unit->ai2; - unit_su->ai2data = unit->ai2data; - unit_su->ai3And4 = unit->ai3And4; - unit_su->_u46 = unit->_u46; + unit_su->ai_b_pc = unit->ai_b_pc; + unit_su->ai_config = unit->ai_config; + unit_su->ai_counter = unit->ai_counter; unit_su->aiFlags = unit->aiFlags; } @@ -737,11 +737,11 @@ void ReadSuspendSavePackedUnit(const void *sram_src, struct Unit *unit) unit->supports[i] = unit_su.supports[i]; unit->ai1 = unit_su.ai1 & 0x7F; - unit->ai1data = unit_su.ai1data; + unit->ai_a_pc = unit_su.ai_a_pc; unit->ai2 = unit_su.ai2; - unit->ai2data = unit_su.ai2data; - unit->ai3And4 = unit_su.ai3And4; - unit->_u46 = unit_su._u46; + unit->ai_b_pc = unit_su.ai_b_pc; + unit->ai_config = unit_su.ai_config; + unit->ai_counter = unit_su.ai_counter; unit->aiFlags = unit_su.aiFlags; if (0x7F == unit->exp) diff --git a/src/cp_0803E2F4.c b/src/cp_0803E2F4.c index ebb562fee..433a02d1f 100644 --- a/src/cp_0803E2F4.c +++ b/src/cp_0803E2F4.c @@ -23,7 +23,7 @@ void AiFillDangerMap(void); s8 AiUpdateGetUnitIsHealing(struct Unit*); const struct AiEscapePt* GetEscapePointStructThingMaybe(void); void sub_803EC18(u16); -s8 sub_803EC54(struct Unit*, int, int); +s8 AiIsWithinFlyingDistance(struct Unit*, int, int); int StoreItemAndGetUnitAttack(struct Unit*, u16*); s8 AiTryDoDanceAdjacent(int, int); s8 AiTryDoStealAdjacent(int, int); @@ -127,7 +127,7 @@ s8 AiTryGetNearestHealPoint(struct Vec2* out) { int currentCount = 10000; int currentMove = 0xff; - if (gActiveUnit->ai3And4 & 0x2000) { + if (gActiveUnit->ai_config & AI_UNIT_CONFIG_FLAG_STAY) { return 0; } @@ -163,7 +163,7 @@ s8 AiTryGetNearestHealPoint(struct Vec2* out) { unit = GetUnit(gBmMapUnit[iy][ix]); - if (unit->ai3And4 & 0x2000) { + if (unit->ai_config & AI_UNIT_CONFIG_FLAG_STAY) { if (!(unit->aiFlags & AI_UNIT_FLAG_2)) { continue; } @@ -225,14 +225,14 @@ s8 AiUpdateGetUnitIsHealing(struct Unit* unit) { u16 hpPercentage = Div(GetUnitCurrentHp(unit) * 100, GetUnitMaxHp(unit)); if (unit->aiFlags & AI_UNIT_FLAG_0) { - if (gAI3HealingThresholdTable[unit->ai3And4 & 7].exitThreshold > hpPercentage) { + if (gAI3HealingThresholdTable[unit->ai_config & AI_UNIT_CONFIG_HEALTHRESHOLD_MASK].exitThreshold > hpPercentage) { return 1; } else { unit->aiFlags &= ~AI_UNIT_FLAG_0; return 0; } } else { - if (gAI3HealingThresholdTable[unit->ai3And4 & 7].enterThreshold > hpPercentage) { + if (gAI3HealingThresholdTable[unit->ai_config & AI_UNIT_CONFIG_HEALTHRESHOLD_MASK].enterThreshold > hpPercentage) { unit->aiFlags |= AI_UNIT_FLAG_0; return 1; } else { @@ -242,32 +242,40 @@ s8 AiUpdateGetUnitIsHealing(struct Unit* unit) { } //! FE8U = 0x0803E718 -s8 AiTryHealSelf(void) { +bool AiTryHealSelf(void) +{ int i; - for (i = 0; i < UNIT_ITEM_COUNT; i++) { + for (i = 0; i < UNIT_ITEM_COUNT; i++) + { u16 item = gActiveUnit->items[i]; - if (item == 0) { + if (item == 0) return 0; - } - if (GetItemIndex(item) == ITEM_VULNERARY || GetItemIndex(item) == ITEM_ELIXIR) { - if (!(gAiState.flags & AI_FLAG_1) && !(gActiveUnit->ai3And4 & 0x2000)) { + if (GetItemIndex(item) == ITEM_VULNERARY || GetItemIndex(item) == ITEM_ELIXIR) + { + if (!(gAiState.flags & AI_FLAG_STAY) && !(gActiveUnit->ai_config & AI_UNIT_CONFIG_FLAG_STAY)) + { + /** + * If unit can move around (rather than stick on position) + * he may try escape to a safe place then heal itself. + */ struct Vec2 position; - - if (AiFindSafestReachableLocation(gActiveUnit, &position) == 1) { + if (AiFindSafestReachableLocation(gActiveUnit, &position) == true) + { AiSetDecision(position.x, position.y, AI_ACTION_USEITEM, 0, i, 0, 0); - return 1; + return true; } - } else { + } + else + { AiSetDecision(gActiveUnit->xPos, gActiveUnit->yPos, AI_ACTION_USEITEM, 0, i, 0, 0); - return 1; + return true; } } } - - return 0; + return false; } //! FE8U = 0x0803E7D0 @@ -332,7 +340,7 @@ const struct AiEscapePt* GetEscapePointStructThingMaybe(void) { } //! FE8U = 0x0803E900 -s8 sub_803E900(void) { +s8 AiCanEquip(void) { if (gActiveUnit->state & US_CANTOING) { return 0; @@ -350,52 +358,48 @@ s8 sub_803E900(void) { } //! FE8U = 0x0803E93C -s8 sub_803E93C(u16* out) { +s8 AiEquipGetFlags(u16 * out) +{ int i; u32 perc; - if (GetUnitItemCount(gActiveUnit) == 0) { + if (GetUnitItemCount(gActiveUnit) == 0) return 0; - } - for (i = 0; i < UNIT_ITEM_COUNT; i++) { + for (i = 0; i < UNIT_ITEM_COUNT; i++) + { u16 item; - out[i] = 0; item = gActiveUnit->items[i]; - if (item == 0) { + if (item == 0) break; - } - if (!(GetItemAttributes(item) & (IA_WEAPON | IA_STAFF))) { + if (!(GetItemAttributes(item) & (IA_WEAPON | IA_STAFF))) continue; - } - if (GetItemAttributes(item) & IA_LOCK_3) { + if (GetItemAttributes(item) & IA_LOCK_3) continue; - } - if (!CanUnitUseWeapon(gActiveUnit, item) && !CanUnitUseStaff(gActiveUnit, item)) { + if (!CanUnitUseWeapon(gActiveUnit, item) && !CanUnitUseStaff(gActiveUnit, item)) continue; - } - if (GetItemAttributes(item) & IA_WEAPON) { - if (GetItemMinRange(item) > 1) { + if (GetItemAttributes(item) & IA_WEAPON) + { + if (GetItemMinRange(item) > 1) out[i] |= 2; - } - if (GetItemMaxRange(item) == 1) { + if (GetItemMaxRange(item) == 1) out[i] |= 1; - } perc = Div(perc = GetItemUses(item) * 100, GetItemMaxUses(item)); - if (perc <= 10) { + if (perc <= 10) out[i] |= 4; - } - } else { + } + else + { sub_803EC18(item); out[i] |= 8; } @@ -405,115 +409,102 @@ s8 sub_803E93C(u16* out) { } //! FE8U = 0x0803EA58 -void sub_803EA58(int x, int y, u16* param_3, u16* param_4, u16* param_5) { +void AiEquipGetDanger(int x, int y, u16 * range_danger_out, u16 * melee_danger_out, u16 * combined_danger_out) +{ int i; - int iVar6; - int iVar7; - int iVar8; - int iVar10; - u16 local_38; + int might; + int iy, ix; + u16 item; - *param_5 = 0; - *param_4 = 0; - *param_3 = 0; + *combined_danger_out = 0; + *melee_danger_out = 0; + *range_danger_out = 0; BmMapFill(gBmMapOther, 0); - for (i = 1; i < 0xC0; i++) { - struct Unit* unit = GetUnit(i); + for (i = 1; i < 0xC0; i++) + { + struct Unit * unit = GetUnit(i); - if (!UNIT_IS_VALID(unit)) { + if (!UNIT_IS_VALID(unit)) continue; - } - if (unit->state & 0x21) { + if (unit->state & (US_HIDDEN | US_RESCUED)) continue; - } - if (AreUnitsAllied(gActiveUnitId, unit->index)) { + if (AreUnitsAllied(gActiveUnitId, unit->index)) continue; - } - if (!sub_803EC54(unit, x, y)) { + if (!AiIsWithinFlyingDistance(unit, x, y)) continue; - } GenerateUnitMovementMap(unit); - if (gBmMapMovement[y][x] == 0xFF) { + if (gBmMapMovement[y][x] == 0xFF) continue; - } - iVar6 = StoreItemAndGetUnitAttack(unit, &local_38); + might = StoreItemAndGetUnitAttack(unit, &item); - if (GetItemMinRange(local_38) > 1) { - *param_3 += iVar6; - } + if (GetItemMinRange(item) > 1) + *range_danger_out += might; - if (GetItemMaxRange(local_38) == 1) { - *param_4 += iVar6; - } + if (GetItemMaxRange(item) == 1) + *melee_danger_out += might; - for (iVar7 = gBmMapSize.y - 1; iVar7 >= 0; iVar7--) { - for (iVar10 = gBmMapSize.x - 1; iVar10 >= 0; iVar10--) { - if (gBmMapMovement[iVar7][iVar10] > MAP_MOVEMENT_MAX) + for (iy = gBmMapSize.y - 1; iy >= 0; iy--) + { + for (ix = gBmMapSize.x - 1; ix >= 0; ix--) + { + if (gBmMapMovement[iy][ix] > MAP_MOVEMENT_MAX) continue; - if (gBmMapOther[iVar7][iVar10] + iVar6 <= 0xFF) - gBmMapOther[iVar7][iVar10] = gBmMapOther[iVar7][iVar10] + iVar6; + if (gBmMapOther[iy][ix] + might <= 0xFF) + gBmMapOther[iy][ix] = gBmMapOther[iy][ix] + might; else - gBmMapOther[iVar7][iVar10] = 0xFF; + gBmMapOther[iy][ix] = 0xFF; } } } - - *param_5 = *param_3 + *param_4; - - return; + *combined_danger_out = *range_danger_out + *melee_danger_out; } //! FE8U = 0x0803EBA4 -void sub_803EBA4(int a, u16* b) { - +void AiEquipBestMatch(int equip_flag, u16 * equip_flags) +{ int i; int itemSlot = -1; u16 unk = 0; - for (i = 0; i < UNIT_ITEM_COUNT; b++, i++) { - if (*b == 0) { + for (i = 0; i < UNIT_ITEM_COUNT; equip_flags++, i++) + { + if (*equip_flags == 0) continue; - } - if (!(*b & a)) { + if (!(*equip_flags & equip_flag)) continue; - } - if ((*b & 0xff00) > unk) { - unk = *b & 0xff00; + if ((*equip_flags & 0xff00) > unk) + { + unk = *equip_flags & 0xff00; itemSlot = i; } } - if (itemSlot > 0) { + if (itemSlot > 0) EquipUnitItemSlot(gActiveUnit, itemSlot); - } - - return; } //! FE8U = 0x0803EBF0 -void sub_803EBF0(u16 a, u16 b, u16 c, u16* d) { - - if ((b + a) != 0) { - if (b >= a) { - sub_803EBA4(1, d); - } else { - sub_803EBA4(2, d); - } +void AiEquipBestConsideringDanger(u16 range_danger, u16 melee_danger, u16 combined_danger, u16 * equip_flags) +{ + if ((melee_danger + range_danger) != 0) + { + if (melee_danger >= range_danger) + AiEquipBestMatch(1, equip_flags); + else + AiEquipBestMatch(2, equip_flags); } - - return; } //! FE8U = 0x0803EC18 @@ -536,7 +527,7 @@ void sub_803EC18(u16 item) { } //! FE8U = 0x0803EC54 -s8 sub_803EC54(struct Unit* unit, int x, int y) { +s8 AiIsWithinFlyingDistance(struct Unit* unit, int x, int y) { int mov = UNIT_MOV(unit); int dist = RECT_DISTANCE(x, y, unit->xPos, unit->yPos); @@ -761,7 +752,7 @@ s8 sub_803F018(const void* input) { } } - gAiState.unk86[((u8*)(input))[2]] = count; + gAiState.cmd_result[((u8*)(input))[2]] = count; return 0; } @@ -787,8 +778,8 @@ s8 sub_803F15C(const struct Unknown_Sub80315C* input) { item = GetUnitEquippedWeapon(gActiveUnit); - xUnk = input->unk_00[((gActiveUnit->ai3And4 & 0x1fc0) >> 8)*2+0]; - yUnk = input->unk_00[((gActiveUnit->ai3And4 & 0x1fc0) >> 8)*2+1]; + xUnk = input->unk_00[((gActiveUnit->ai_config & 0x1fc0) >> 8)*2+0]; + yUnk = input->unk_00[((gActiveUnit->ai_config & 0x1fc0) >> 8)*2+1]; xPrev = gActiveUnit->xPos; yPrev = gActiveUnit->yPos; @@ -858,7 +849,7 @@ s8 sub_803F15C(const struct Unknown_Sub80315C* input) { //! FE8U = 0x0803F330 s8 sub_803F330(const void* input) { - gAiState.unk86[0] = gActiveUnit->_u46; + gAiState.cmd_result[0] = gActiveUnit->ai_counter; return 0; } @@ -898,8 +889,8 @@ s8 sub_803F3AC(struct Vec2* out) { const struct Vec2* posA; const struct Vec2* posB; - int idx = (gActiveUnit->ai3And4 & 0x1fc0) >> 8; - int unk46 = gActiveUnit->_u46; + int idx = (gActiveUnit->ai_config & 0x1fc0) >> 8; + int ai_counter = gActiveUnit->ai_counter; if (gUnknown_085A8400 == NULL) { return 0; @@ -911,10 +902,10 @@ s8 sub_803F3AC(struct Vec2* out) { return 0; } - posB = posA + unk46; + posB = posA + ai_counter; if (posB->x == -1) { - unk46 = 0; - gActiveUnit->_u46 = 0; + ai_counter = 0; + gActiveUnit->ai_counter = 0; posB = posA; } @@ -922,8 +913,8 @@ s8 sub_803F3AC(struct Vec2* out) { out->y = posB->y; if (gBmMapMovement[posB->y][posB->x] != 0xFF) { - unk46++; - gActiveUnit->_u46 = unk46; + ai_counter++; + gActiveUnit->ai_counter = ai_counter; } return 1; @@ -940,7 +931,7 @@ s8 sub_803F434(const void* input) { FillMovementAndRangeMapForItem(gActiveUnit, item); enemiesInRange = AiCountEnemyUnitsInRange(); if (enemiesInRange != 0) { - gAiState.unk86[0] = enemiesInRange; + gAiState.cmd_result[0] = enemiesInRange; return 0; } } else { @@ -971,11 +962,11 @@ s8 sub_803F4A4(const void* input) { u8 y = gActiveUnit->yPos; if (castInput->unk_00 <= x && castInput->unk_02 >= x && castInput->unk_01 <= y && castInput->unk_03 >= y) { - gAiState.unk86[0] = 1; + gAiState.cmd_result[0] = 1; return 0; } - gAiState.unk86[0] = 0; + gAiState.cmd_result[0] = 0; return 0; } @@ -997,7 +988,7 @@ s8 sub_803F4EC(const void* input) { //! FE8U = 0x0803F51C s8 sub_803F51C(const void* input) { u16 leaderAi1; - u16 leaderAi1data; + u16 leaderai_a_pc; struct Unit* leader; int i = 0; @@ -1015,14 +1006,14 @@ s8 sub_803F51C(const void* input) { if (leader == 0) { gActiveUnit = prevUnit; - gAiState.unk86[1] = 1; + gAiState.cmd_result[1] = 1; return 0; } gActiveUnitId = leader->index; leaderAi1 = leader->ai1; - leaderAi1data = leader->ai1data; + leaderai_a_pc = leader->ai_a_pc; for (; i < 0x100; i++) { if (AiTryExecScriptA() == 1) { @@ -1034,15 +1025,15 @@ s8 sub_803F51C(const void* input) { _0803F584: if ((gAiDecision.actionPerformed == 1) && (gAiDecision.actionId == AI_ACTION_COMBAT)) { - gAiState.unk86[0] = gAiDecision.targetId; + gAiState.cmd_result[0] = gAiDecision.targetId; } else { - gAiState.unk86[0] = 0; + gAiState.cmd_result[0] = 0; } AiClearDecision(); gActiveUnit->ai1 = leaderAi1; - gActiveUnit->ai1data = leaderAi1data; + gActiveUnit->ai_a_pc = leaderai_a_pc; gActiveUnitId = prevUid; gActiveUnit = prevUnit; @@ -1069,15 +1060,15 @@ s8 sub_803F61C(const void* input) { gUnknown_0203AAA0 = ((u8*)(input))[0]; if (AiUnitWithCharIdExists(((u8*)(input))[0]) != 1) { - gAiState.unk86[1] = 1; + gAiState.cmd_result[1] = 1; return 0; } AiAttemptOffensiveAction(sub_803F5E0); - gAiState.unk86[0] = 0; + gAiState.cmd_result[0] = 0; if ((gAiDecision.actionPerformed == 1) && (gAiDecision.actionId == AI_ACTION_COMBAT)) { - gAiState.unk86[0] = gAiDecision.targetId; + gAiState.cmd_result[0] = gAiDecision.targetId; } AiClearDecision(); @@ -1088,7 +1079,7 @@ s8 sub_803F61C(const void* input) { //! FE8U = 0x0803F680 s8 sub_803F680(struct Unit* unit) { - if (unit->index != gAiState.unk86[0]) { + if (unit->index != gAiState.cmd_result[0]) { return 0; } @@ -1106,7 +1097,7 @@ s8 sub_803F6B8(struct Unit* unit) { int c; int d; - struct Unit* other = GetUnit(gAiState.unk86[0]); + struct Unit* other = GetUnit(gAiState.cmd_result[0]); a = (other->xPos - gActiveUnit->xPos); b = (other->yPos - gActiveUnit->yPos); @@ -1132,11 +1123,11 @@ s8 sub_803F6B8(struct Unit* unit) { s8 sub_803F72C(const void* input) { struct Unit* unit; - if (gAiState.unk86[0] == 0) { + if (gAiState.cmd_result[0] == 0) { return 1; } - unit = GetUnit(gAiState.unk86[0]); + unit = GetUnit(gAiState.cmd_result[0]); AiAttemptOffensiveAction(sub_803F680); @@ -1253,9 +1244,9 @@ s8 AiBallistaRideExit(const void * input) } } else { if (unk != 0) { - gAiState.unk86[0] = 7; + gAiState.cmd_result[0] = 7; } else { - gAiState.unk86[0] = 6; + gAiState.cmd_result[0] = 6; } } diff --git a/src/cp_battle.c b/src/cp_battle.c index 403c22d49..be6cc89ca 100644 --- a/src/cp_battle.c +++ b/src/cp_battle.c @@ -53,7 +53,7 @@ s8 AiAttemptOffensiveAction(s8 (* isEnemy)(struct Unit * unit)) } } - if (gAiState.flags & AI_FLAG_1) + if (gAiState.flags & AI_FLAG_STAY) { BmMapFill(gBmMapMovement, -1); gBmMapMovement[gActiveUnit->yPos][gActiveUnit->xPos] = 0; @@ -433,7 +433,7 @@ s8 AiAttemptStealActionWithinMovement(void) { } if (rank != 0xFF) { - gActiveUnit->_u46++; + gActiveUnit->ai_counter++; AiSetDecision(pos.x, pos.y, AI_ACTION_STEAL, target, itemSlot, 0, 0); return 1; diff --git a/src/cp_data.c b/src/cp_data.c index c52ae91f4..eb073b186 100644 --- a/src/cp_data.c +++ b/src/cp_data.c @@ -1174,7 +1174,7 @@ AI_LABEL(0x1), // 0x08 = ? struct AiScr CONST_DATA gAiScript_085A85D0[] = { AI_CALL_FUNC(sub_803F434, 0x0), - AI_GOTO_IFGT(0x1, gAiState.unk86+0, 0x0), + AI_GOTO_IFGT(0x1, gAiState.cmd_result+0, 0x0), AI_GOTO_START, AI_LABEL(0x1), AI_CALL_FUNC(sub_803F34C, gUnknown_080D8660), @@ -1183,7 +1183,7 @@ AI_LABEL(0x1), // 0x06 = If could reach opponents in two turns, change AI2 to 0x0 struct AiScr CONST_DATA gAiScript_085A8620[] = { AI_CALL_FUNC(sub_803F018, gUnknown_080D8664), - AI_GOTO_IFGT(0x1, gAiState.unk86+0, 0x0), + AI_GOTO_IFGT(0x1, gAiState.cmd_result+0, 0x0), AI_NOP_0E, AI_GOTO_START, AI_LABEL(0x1), @@ -1194,7 +1194,7 @@ AI_LABEL(0x1), // 0x07 = If could reach opponents in two turns, change AI2 to 0x1 struct AiScr CONST_DATA gAiScript_085A8690[] = { AI_CALL_FUNC(sub_803F018, gUnknown_080D8664), - AI_GOTO_IFGT(0x1, gAiState.unk86+0, 0x0), + AI_GOTO_IFGT(0x1, gAiState.cmd_result+0, 0x0), AI_NOP_0E, AI_GOTO_START, AI_LABEL(0x1), @@ -1205,7 +1205,7 @@ AI_LABEL(0x1), // unused? struct AiScr CONST_DATA gAiScript_085A8700[] = { AI_CALL_FUNC(sub_803F018, gUnknown_080D8664), - AI_GOTO_IFGT(0x1, gAiState.unk86+0, 0), + AI_GOTO_IFGT(0x1, gAiState.cmd_result+0, 0), AI_NOP_0E, AI_GOTO_START, AI_LABEL(0x1), @@ -1224,7 +1224,7 @@ struct AiScr CONST_DATA gAiScript_085A8790[] = { // unused? struct AiScr CONST_DATA gAiScript_085A87B0[] = { AI_MOVE_TO_LISTED_TERRAIN(gUnknown_085A8180), - AI_GOTO_IFEQ(0x1, gAiState.unk86+0, 0x4), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+0, 0x4), AI_GOTO_START, AI_BAD_LABEL(0x1), AI_MOVE_TO_ENEMY, @@ -1234,7 +1234,7 @@ AI_BAD_LABEL(0x1), // 0x0D = Move on to nearest terrain 0x1B/0x1F (Throne/Fence) (0x5A8182) struct AiScr CONST_DATA gAiScript_085A8810[] = { AI_MOVE_TO_LISTED_TERRAIN(gUnknown_085A8182), - AI_GOTO_IFEQ(0x1, gAiState.unk86+0, 0x4), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+0, 0x4), AI_GOTO_START, }; @@ -1291,7 +1291,7 @@ struct AiScr CONST_DATA gAiScript_DoNothing[] = { struct AiScr CONST_DATA gAiScript_085A8950[] = { AI_ACTION(100), AI_CALL_FUNC(sub_803F330, NULL), - AI_GOTO_IFGT(0x1, gAiState.unk86+0, 0x0), + AI_GOTO_IFGT(0x1, gAiState.cmd_result+0, 0x0), AI_GOTO_START, AI_LABEL(0x1), AI_ACTION_IN_PLACE(100), @@ -1347,12 +1347,12 @@ struct UnitDefinition* CONST_DATA gUnknown_085A8A78 = gUnitDefSumDK; // 0x14 = Try to use Nightmare (but not on turn one), then try to Summon Units, then act like 0x0 (AttackInRange) struct AiScr CONST_DATA gAiScript_085A8A7C[] = { AI_CALL_FUNC(AiTryUseNightmareStaff, gUnknown_085A8A70), - AI_GOTO_IFEQ(0x1, gAiState.unk86+0, 0x0), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+0, 0x0), AI_CALL_FUNC(AiDecideNightmareStaff, 0x0), AI_GOTO_START, AI_LABEL(0x1), AI_CALL_FUNC(AiTryDKSummon, gUnknown_085A8A74), - AI_GOTO_IFEQ(0x2, gAiState.unk86+0, 0x0), + AI_GOTO_IFEQ(0x2, gAiState.cmd_result+0, 0x0), AI_CALL_FUNC(AiDecideDKSummon, 0x0), AI_GOTO_START, AI_LABEL(0x2), @@ -1383,7 +1383,7 @@ struct AiScr CONST_DATA gAiScript_085A8B80[] = { // 0x0A = Only attack character ??? if deployed. (currently 00 01, set it at 5A8BA4) struct AiScr CONST_DATA gAiScript_085A8BA0[] = { AI_ACTION_ON_CHAR(100, 0x100), - AI_GOTO_IFNE(0x0, gAiState.unk86+0, 0x3), + AI_GOTO_IFNE(0x0, gAiState.cmd_result+0, 0x3), AI_ACTION(100), AI_GOTO_START, }; @@ -1397,7 +1397,7 @@ struct AiScr CONST_DATA gAiScript_085A8BE0[] = { // 0x0C = Attack if within Mov/2+Range(?) struct AiScr CONST_DATA gAiScript_085A8C00[] = { AI_CALL_FUNC(sub_803F018, gUnknown_080D8668), - AI_GOTO_IFGT(0x1, gAiState.unk86+0, 0x0), + AI_GOTO_IFGT(0x1, gAiState.cmd_result+0, 0x0), AI_ACTION_IN_PLACE(100), AI_GOTO_START, AI_LABEL(0x1), @@ -1408,7 +1408,7 @@ AI_LABEL(0x1), // 0x0D = CHAI [0x0, 0x0] if the unit's leader has foe in range.(?) struct AiScr CONST_DATA gAiScript_085A8C70[] = { AI_CALL_FUNC(sub_803F51C, NULL), - AI_GOTO_IFEQ(0x1, gAiState.unk86+1, 0x1), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+1, 0x1), AI_CALL_FUNC(sub_803F72C, NULL), AI_GOTO_START, AI_LABEL(0x1), @@ -1425,7 +1425,7 @@ struct AiScr CONST_DATA gAiScript_085A8CE0[] = { // unused struct AiScr CONST_DATA gAiScript_085A8D00[] = { AI_CALL_FUNC(sub_803F018, gUnknown_080D866C), - AI_GOTO_IFGT(0x1, gAiState.unk86+0, 0x0), + AI_GOTO_IFGT(0x1, gAiState.cmd_result+0, 0x0), AI_MOVE_TOWARDS(5, 7), AI_ACTION_IN_PLACE(100), AI_GOTO_START, @@ -1437,7 +1437,7 @@ AI_LABEL(0x1), // unused struct AiScr CONST_DATA gAiScript_085A8D80[] = { AI_CALL_FUNC(sub_803F018, gUnknown_080D866C), - AI_GOTO_IFGT(0x1, gAiState.unk86+0, 0x0), + AI_GOTO_IFGT(0x1, gAiState.cmd_result+0, 0x0), AI_MOVE_TOWARDS(17, 6), AI_ACTION_IN_PLACE(0x64), AI_GOTO_START, @@ -1449,9 +1449,9 @@ AI_LABEL(0x1), // 0x0A = Move to character 0x1 Eirika if not in range, or move to opponents if so struct AiScr CONST_DATA gAiScript_085A8E00[] = { AI_MOVE_TOWARDS_CHAR(CHARACTER_EIRIKA), - AI_GOTO_IFEQ(0x1, gAiState.unk86+0, 0x4), - AI_GOTO_IFEQ(0x2, gAiState.unk86+0, 0x1), - AI_GOTO_IFEQ(0x1, gAiState.unk86+0, 0x2), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+0, 0x4), + AI_GOTO_IFEQ(0x2, gAiState.cmd_result+0, 0x1), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+0, 0x2), AI_GOTO_START, AI_LABEL(0x1), AI_MOVE_TO_ENEMY, @@ -1464,9 +1464,9 @@ AI_LABEL(0x2), // 0x0B = Move to character 0xF Ephraim if not in range, or move to opponents if so struct AiScr CONST_DATA gAiScript_085A8EB0[] = { AI_MOVE_TOWARDS_CHAR(CHARACTER_EPHRAIM), - AI_GOTO_IFEQ(0x1, gAiState.unk86+0, 0x4), - AI_GOTO_IFEQ(0x2, gAiState.unk86+0, 0x1), - AI_GOTO_IFEQ(0x1, gAiState.unk86+0, 0x2), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+0, 0x4), + AI_GOTO_IFEQ(0x2, gAiState.cmd_result+0, 0x1), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+0, 0x2), AI_GOTO_START, AI_LABEL(0x1), AI_MOVE_TO_ENEMY, @@ -1499,7 +1499,7 @@ struct AiScr CONST_DATA gAiScript_085A8FB0[] = { // 0x10 = Pick Locks/Steal, then CHAI [0x6, 0xC] (Escape) struct AiScr CONST_DATA gAiScript_085A8FE0[] = { AI_CALL_FUNC(sub_803F7DC, gUnknown_080D8674), - AI_GOTO_IFEQ(0x1, gAiState.unk86+0, 0x5), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+0, 0x5), AI_GOTO_START, AI_LABEL(0x1), AI_SET_AI(0x6, 0xC), @@ -1515,7 +1515,7 @@ struct AiScr CONST_DATA gAiScript_085A9040[] = { // 0x0E = Attack walls until no more remain(?), then CHAI [0x0,0x0] struct AiScr CONST_DATA gAiScript_AttackWallsSnags[] = { AI_ATTACK_WALLS, - AI_GOTO_IFEQ(0x1, gAiState.unk86+0, 0x4), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+0, 0x4), AI_GOTO_START, AI_LABEL(0x1), AI_SET_AI(0x0, 0x0), @@ -1533,7 +1533,7 @@ const u8 CONST_DATA gUnknown_085A90C4[] = { // 0x10 = If not in area [13,15]-[18,19], move to [15,17]; if in area, CHAI [0x0,0x0] struct AiScr CONST_DATA gAiScript_085A90C8[] = { AI_CALL_FUNC(sub_803F4A4, gUnknown_085A90C4), - AI_GOTO_IFEQ(0x1, gAiState.unk86+0, 0x1), + AI_GOTO_IFEQ(0x1, gAiState.cmd_result+0, 0x1), AI_CALL_FUNC(sub_803F9A8, gUnknown_085A90C0), AI_GOTO_START, AI_LABEL(0x1), @@ -1542,49 +1542,49 @@ AI_LABEL(0x1), }; struct AiScr* CONST_DATA gAi2ScriptTable[] = { - gAiScript_Ai2x00, - gAiScript_085A84B0, - gAiScript_085A84D0, - gAiScript_085A84F0, - gAiScript_Ai2x04_85A8510, - gAiScript_085A8540, - gAiScript_085A8620, - gAiScript_085A8690, - gAiScript_085A85D0, - gAiScript_085A8790, - gAiScript_085A8E00, - gAiScript_085A8EB0, - gAiScript_Escape, - gAiScript_085A8810, - gAiScript_AttackWallsSnags, - gAiScript_085A8430, - gAiScript_085A90C8, - gAiScript_Ai2x11_85A8480, - gAiScript_Ai2x12_85A8450, + [AI_B_00] = gAiScript_Ai2x00, + [AI_B_01] = gAiScript_085A84B0, + [AI_B_02] = gAiScript_085A84D0, + [AI_B_03] = gAiScript_085A84F0, + [AI_B_04] = gAiScript_Ai2x04_85A8510, + [AI_B_05] = gAiScript_085A8540, + [AI_B_06] = gAiScript_085A8620, + [AI_B_07] = gAiScript_085A8690, + [AI_B_08] = gAiScript_085A85D0, + [AI_B_09] = gAiScript_085A8790, + [AI_B_0A] = gAiScript_085A8E00, + [AI_B_0B] = gAiScript_085A8EB0, + [AI_B_0C] = gAiScript_Escape, + [AI_B_0D] = gAiScript_085A8810, + [AI_B_0E] = gAiScript_AttackWallsSnags, + [AI_B_0F] = gAiScript_085A8430, + [AI_B_10] = gAiScript_085A90C8, + [AI_B_11] = gAiScript_Ai2x11_85A8480, + [AI_B_12] = gAiScript_Ai2x12_85A8450, }; struct AiScr* CONST_DATA gAi1ScriptTable[] = { - gAiScript_ActionInRange, - gAiScript_ActionInRange_80Perc, - gAiScript_ActionInRange_50Perc, - gAiScript_ActionStanding, - gAiScript_ActionStanding_80Perc, - gAiScript_ActionStanding_50Perc, - gAiScript_DoNothing, - gAiScript_ActionInRange_ExceptNatasha, - gAiScript_ActionInRange_ExceptCivilian, - gAiScript_085A8B80, - gAiScript_085A8BA0, - gAiScript_085A8BE0, - gAiScript_085A8C00, - gAiScript_085A8C70, - gAiScript_085A8F90, - gAiScript_085A8FB0, - gAiScript_085A8FE0, - gAiScript_085A9040, - gAiScript_085A8A2C, - gAiScript_085A8A50, - gAiScript_085A8A7C, + [AI_A_00] = gAiScript_ActionInRange, + [AI_A_01] = gAiScript_ActionInRange_80Perc, + [AI_A_02] = gAiScript_ActionInRange_50Perc, + [AI_A_03] = gAiScript_ActionStanding, + [AI_A_04] = gAiScript_ActionStanding_80Perc, + [AI_A_05] = gAiScript_ActionStanding_50Perc, + [AI_A_06] = gAiScript_DoNothing, + [AI_A_07] = gAiScript_ActionInRange_ExceptNatasha, + [AI_A_08] = gAiScript_ActionInRange_ExceptCivilian, + [AI_A_09] = gAiScript_085A8B80, + [AI_A_0A] = gAiScript_085A8BA0, + [AI_A_0B] = gAiScript_085A8BE0, + [AI_A_0C] = gAiScript_085A8C00, + [AI_A_0D] = gAiScript_085A8C70, + [AI_A_0E] = gAiScript_085A8F90, + [AI_A_0F] = gAiScript_085A8FB0, + [AI_A_10] = gAiScript_085A8FE0, + [AI_A_11] = gAiScript_085A9040, + [AI_A_12] = gAiScript_085A8A2C, + [AI_A_13] = gAiScript_085A8A50, + [AI_A_14] = gAiScript_085A8A7C, }; struct AiScr** CONST_DATA gpAi2Table[] = { diff --git a/src/cp_decide.c b/src/cp_decide.c index 343e29342..c7ff94de9 100644 --- a/src/cp_decide.c +++ b/src/cp_decide.c @@ -96,7 +96,7 @@ void CpDecide_Main(ProcPtr proc) AiUpdateNoMoveFlag(gActiveUnit); - gAiState.combatWeightTableId = (gActiveUnit->ai3And4 & 0xF8) >> 3; + gAiState.combatWeightTableId = (gActiveUnit->ai_config & AI_UNIT_CONFIG_COMBATWEIGHT_MASK) >> AI_UNIT_CONFIG_COMBATWEIGHT_SHIFT; gAiState.dangerMapFilled = FALSE; AiInitDangerMap(); diff --git a/src/cp_perform.c b/src/cp_perform.c index 39b230559..09b74fa5f 100644 --- a/src/cp_perform.c +++ b/src/cp_perform.c @@ -62,8 +62,8 @@ void CpPerform_BeginUnitMovement(struct CpPerformProc* proc); void CpPerform_MoveCameraOntoTarget(struct CpPerformProc* proc); void CpPerform_PerformAction(struct CpPerformProc* proc); void CpPerform_WaitAction(struct CpPerformProc* proc); -void CpPerform_803A63C(struct CpPerformProc* proc); -void CpPerform_803A6D0(struct CpPerformProc* proc); +void CpPerform_Cleanup(struct CpPerformProc* proc); +void CpPerform_EquipBest(struct CpPerformProc* proc); struct ProcCmd CONST_DATA gProcScr_CpPerform[] = { PROC_NAME("E_CPPERFORM"), @@ -84,8 +84,8 @@ struct ProcCmd CONST_DATA gProcScr_CpPerform[] = { PROC_CALL_2(HandlePostActionTraps), PROC_CALL_2(RunPotentialWaitEvents), - PROC_CALL(CpPerform_803A63C), - PROC_CALL(CpPerform_803A6D0), + PROC_CALL(CpPerform_Cleanup), + PROC_CALL(CpPerform_EquipBest), PROC_LABEL(1), PROC_END, @@ -586,7 +586,7 @@ void CpPerform_WaitAction(struct CpPerformProc* proc) { return; } -void CpPerform_803A63C(struct CpPerformProc* proc) { +void CpPerform_Cleanup(struct CpPerformProc* proc) { UpdateAllPhaseHealingAIStatus(); AiRefreshMap(); @@ -624,15 +624,16 @@ s8 AiWaitAndClearScreenAction(struct CpPerformProc* proc) { return 0; } -void CpPerform_803A6D0(struct CpPerformProc* proc) { - u16 a[6]; - u16 b; - u16 c; - u16 d; +void CpPerform_EquipBest(struct CpPerformProc* proc) { + u16 equip_flags[UNIT_ITEM_COUNT + 1]; - if ((sub_803E900() != 0) && (sub_803E93C(a) != 0)) { - sub_803EA58(gAiDecision.xMove, gAiDecision.yMove, &b, &c, &d); - sub_803EBF0(b, c, d, a); + if (AiCanEquip() && AiEquipGetFlags(equip_flags)) + { + u16 range_danger; + u16 melee_danger; + u16 combined_danger; + + AiEquipGetDanger(gAiDecision.xMove, gAiDecision.yMove, &range_danger, &melee_danger, &combined_danger); + AiEquipBestConsideringDanger(range_danger, melee_danger, combined_danger, equip_flags); } - return; } diff --git a/src/cp_phase.c b/src/cp_phase.c index e744607b5..531c72a7e 100644 --- a/src/cp_phase.c +++ b/src/cp_phase.c @@ -49,7 +49,7 @@ static void AiPhaseInit(struct Proc* proc) gAiState.orderState = 0; for (i = 0; i < 8; ++i) - gAiState.unk86[i] = 0; + gAiState.cmd_result[i] = 0; gAiState.specialItemFlags = gAiItemConfigTable[gPlaySt.chapterIndex]; gAiState.unk84 = 0; @@ -68,7 +68,7 @@ static void AiPhaseBerserkInit(struct Proc* proc) gAiState.unk7E = -1; for (i = 0; i < 8; ++i) - gAiState.unk86[i] = 0; + gAiState.cmd_result[i] = 0; gAiState.specialItemFlags = gAiItemConfigTable[gPlaySt.chapterIndex]; diff --git a/src/cp_script.c b/src/cp_script.c index 74d5dfff9..47d720545 100644 --- a/src/cp_script.c +++ b/src/cp_script.c @@ -63,12 +63,12 @@ void AiScriptCmd_1B_NoOp(u8* pc); //! FE8U = 0x0803C4BC s8 AiTryExecScriptA(void) { gpAiScriptCurrent = gpAi1Table[0][gActiveUnit->ai1]; - gpAiScriptCurrent = gpAiScriptCurrent + gActiveUnit->ai1data; + gpAiScriptCurrent = gpAiScriptCurrent + gActiveUnit->ai_a_pc; gAiScriptEnded = 1; gAiScriptKind = AI_SCRIPT_AI1; - AiScript_Exec(&gActiveUnit->ai1data); + AiScript_Exec(&gActiveUnit->ai_a_pc); return gAiScriptEnded; } @@ -80,7 +80,7 @@ s8 AiExecFallbackScriptA(void) { gAiScriptEnded = 1; gAiScriptKind = AI_SCRIPT_AI1; - AiScript_Exec(&gActiveUnit->ai1data); + AiScript_Exec(&gActiveUnit->ai_a_pc); return gAiScriptEnded; } @@ -88,12 +88,12 @@ s8 AiExecFallbackScriptA(void) { //! FE8U = 0x0803C54C s8 AiTryExecScriptB(void) { gpAiScriptCurrent = gpAi2Table[0][gActiveUnit->ai2]; - gpAiScriptCurrent = gpAiScriptCurrent + gActiveUnit->ai2data; + gpAiScriptCurrent = gpAiScriptCurrent + gActiveUnit->ai_b_pc; gAiScriptEnded = 1; gAiScriptKind = AI_SCRIPT_AI2; - AiScript_Exec(&gActiveUnit->ai2data); + AiScript_Exec(&gActiveUnit->ai_b_pc); return gAiScriptEnded; } @@ -106,7 +106,7 @@ s8 AiExecFallbackScriptB(void) { gAiScriptEnded = 1; gAiScriptKind = AI_SCRIPT_AI2; - AiScript_Exec(&gActiveUnit->ai2data); + AiScript_Exec(&gActiveUnit->ai_b_pc); return gAiScriptEnded; } @@ -213,12 +213,12 @@ void AiScriptCmd_02_ChangeAi(u8* pc) { if (ai1 != 0xFF) { gActiveUnit->ai1 = ai1; - gActiveUnit->ai1data = 0; + gActiveUnit->ai_a_pc = 0; } if (ai2 != 0xFF) { gActiveUnit->ai2 = ai2; - gActiveUnit->ai2data = 0; + gActiveUnit->ai_b_pc = 0; } if (((gAiScriptKind == 0) && (ai1 == 0xFF)) || ((gAiScriptKind == 1 && (ai2 == 0xFF)))) { @@ -328,13 +328,13 @@ void AiScriptCmd_04_ActionOnSelectedCharacter(u8* pc) { if (AiUnitWithCharIdExists(gpAiScriptCurrent->unk_04) == 1) { if (GetUnitFromCharId(gpAiScriptCurrent->unk_04)->state & US_RESCUED) { - gAiState.unk86[0] = 3; + gAiState.cmd_result[0] = 3; gAiScriptEnded = 0; } else { AiAttemptOffensiveAction(AiIsUnitEnemyAndScrCharId); } } else { - gAiState.unk86[0] = 1; + gAiState.cmd_result[0] = 1; gAiScriptEnded = 0; } } @@ -381,7 +381,7 @@ void AiScriptCmd_07_DoStandardActionNoMove(u8* pc) { u8 rand = NextRN_N(100); if (rand <= gpAiScriptCurrent->unk_01) { - gAiState.flags |= AI_FLAG_1; + gAiState.flags |= AI_FLAG_STAY; if (!AiTryDoStaff(AiIsUnitEnemy)) { AiAttemptOffensiveAction(AiIsUnitEnemy); @@ -465,11 +465,11 @@ void AiScriptCmd_0D_MoveTowardsCharacterUntilInRange(u8* pc) { if (AiIsWithinRectDistance(pos.x, pos.y, gAiDecision.xMove, gAiDecision.yMove, 1) == 1) { struct Unit* unit = GetUnitFromCharId(gpAiScriptCurrent->unk_04); if ((unit->state & US_RESCUED) != 0) { - gAiState.unk86[0] = 3; + gAiState.cmd_result[0] = 3; } else { AiUpdateDecision(0, 0, 0, 0, unit->index); - gAiState.unk86[0] = 2; + gAiState.cmd_result[0] = 2; gAiDecision.actionPerformed = 0; gAiScriptEnded = 0; } @@ -510,9 +510,9 @@ void AiScriptCmd_10_DoLooting(u8* pc) { return; } - gActiveUnit->_u46++; + gActiveUnit->ai_counter++; - if (gActiveUnit->_u46 != gpAiScriptCurrent->unk_03) { + if (gActiveUnit->ai_counter != gpAiScriptCurrent->unk_03) { return; } @@ -535,9 +535,9 @@ void AiScriptCmd_10_DoLooting(u8* pc) { return; } - gActiveUnit->_u46++; + gActiveUnit->ai_counter++; - if (gActiveUnit->_u46 != gpAiScriptCurrent->unk_03) { + if (gActiveUnit->ai_counter != gpAiScriptCurrent->unk_03) { return; } @@ -589,12 +589,12 @@ void AiScriptCmd_13(u8* pc) { struct Vec2 pos; if (gpAiScriptCurrent->unk_08 == 0) { - if (sub_803AA40(AiIsUnitEnemy, &pos) == 1) { - sub_803BBF4(pos.x, pos.y, 0, gpAiScriptCurrent->unk_02, 1); + if (AiFindTargetInReachNeglectWallByFunc(AiIsUnitEnemy, &pos) == 1) { + AiTryMoveTowardsNeglectWall(pos.x, pos.y, 0, gpAiScriptCurrent->unk_02, 1); } } else { - if (sub_803AA40(AiIsUnitEnemyAndNotInScrList, &pos) == 1) { - sub_803BBF4(pos.x, pos.y, 0, gpAiScriptCurrent->unk_02, 1); + if (AiFindTargetInReachNeglectWallByFunc(AiIsUnitEnemyAndNotInScrList, &pos) == 1) { + AiTryMoveTowardsNeglectWall(pos.x, pos.y, 0, gpAiScriptCurrent->unk_02, 1); } } @@ -775,7 +775,7 @@ void AiScriptCmd_18_TryAttackSnagWall(u8* pc) { AiTryMoveTowards(posA.x, posA.y, 0, 0xff, 1); } } else { - gAiState.unk86[0] = 4; + gAiState.cmd_result[0] = 4; gAiScriptEnded = 0; } @@ -793,7 +793,7 @@ void AiScriptCmd_19_MoveTowardsTerrain(u8* pc) { if (AiFindClosestTerrainPosition(&gpAiScriptCurrent->unk_03, 0, &pos) == 1) { AiTryMoveTowards(pos.x, pos.y, 0, gpAiScriptCurrent->unk_02, 1); } else { - gAiState.unk86[0] = 4; + gAiState.cmd_result[0] = 4; gAiScriptEnded = 0; } @@ -811,7 +811,7 @@ void AiScriptCmd_1A_MoveTowardsTerrain(u8* pc) { if (AiFindClosestTerrainPosition(gpAiScriptCurrent->unk_08, 0, &pos) == 1) { AiTryMoveTowards(pos.x, pos.y, 0, gpAiScriptCurrent->unk_02, 1); } else { - gAiState.unk86[0] = 4; + gAiState.cmd_result[0] = 4; gAiScriptEnded = 0; } diff --git a/src/cp_utility.c b/src/cp_utility.c index c9a79cbd0..6c50e22a8 100644 --- a/src/cp_utility.c +++ b/src/cp_utility.c @@ -110,12 +110,12 @@ s8 AiFindTargetInReachByCharId(int uid, struct Vec2* out) { } if (unit->state & US_DEAD) { - gAiState.unk86[0] = 1; + gAiState.cmd_result[0] = 1; return 0; } if (unit->state & US_RESCUED) { - gAiState.unk86[0] = 3; + gAiState.cmd_result[0] = 3; } out->x = unit->xPos; @@ -127,11 +127,11 @@ s8 AiFindTargetInReachByCharId(int uid, struct Vec2* out) { } if (!(GetUnitFromCharId(uid)->state & US_UNAVAILABLE)) { - gAiState.unk86[0] = 4; + gAiState.cmd_result[0] = 4; return 0; } - gAiState.unk86[0] = 1; + gAiState.cmd_result[0] = 1; return 0; } @@ -234,7 +234,7 @@ s8 AiFindTargetInReachByFunc(s8(*func)(struct Unit* unit), struct Vec2* out) { } //! FE8U = 0x0803AA40 -s8 sub_803AA40(s8(*func)(struct Unit* unit), struct Vec2* out) { +s8 AiFindTargetInReachNeglectWallByFunc(s8(*func)(struct Unit* unit), struct Vec2* out) { s16 ix; s16 iy; @@ -243,7 +243,7 @@ s8 sub_803AA40(s8(*func)(struct Unit* unit), struct Vec2* out) { s16 xOut = 0; s16 yOut = 0; - sub_8040F28(gActiveUnit->xPos, gActiveUnit->yPos, GetUnitMovementCost(gActiveUnit)); + GenerateExtendedMovementMapOnRangeNeglectWall(gActiveUnit->xPos, gActiveUnit->yPos, GetUnitMovementCost(gActiveUnit)); xOut = -1; @@ -406,7 +406,7 @@ s8 AiFindClosestTerrainPosition(const u8* terrainList, int flags, struct Vec2* o } } - if (flags & AI_FLAG_1) { + if (flags & AI_FLAG_STAY) { if (AiCountNearbyEnemyUnits(ix, iy) != 0) { continue; } @@ -469,7 +469,7 @@ s8 AiFindClosestTerrainAdjacentPosition(const u8* terrainList, int flags, struct } } - if (flags & AI_FLAG_1) { + if (flags & AI_FLAG_STAY) { if (AiCountNearbyEnemyUnits(ix, iy) != 0) { continue; } @@ -544,7 +544,7 @@ s8 AiFindClosestUnlockPosition(int flags, struct Vec2* outA, struct Vec2* outB) } } - if (flags & AI_FLAG_1) { + if (flags & AI_FLAG_STAY) { if (AiCountNearbyEnemyUnits(tmp.x, tmp.y) != 0) { continue; } @@ -595,10 +595,10 @@ s8 AiFindClosestUnlockPosition(int flags, struct Vec2* outA, struct Vec2* outB) } if (!(zero & 0x10000)) // if (1) - gAiState.unk86[1] = 1; + gAiState.cmd_result[1] = 1; if (count == 0) { - gAiState.unk86[0] = 5; + gAiState.cmd_result[0] = 5; } if (bestDistance != 0xff) { @@ -946,7 +946,7 @@ s8 AiFindSafestReachableLocation(struct Unit* unit, struct Vec2* out) { u8 bestDanger = 0xff; - if (gAiState.flags & AI_FLAG_1) { + if (gAiState.flags & AI_FLAG_STAY) { BmMapFill(gBmMapMovement, -1); gBmMapMovement[unit->yPos][unit->xPos] = 0; } else { @@ -1110,7 +1110,7 @@ void AiTryMoveTowards(s16 x, s16 y, u8 action, u8 maxDanger, u8 unk) { } //! FE8U = 0x0803BBF4 -void sub_803BBF4(s16 x, s16 y, u8 action, u8 maxDanger, u8 unk) { +void AiTryMoveTowardsNeglectWall(s16 x, s16 y, u8 action, u8 maxDanger, u8 unk) { s16 ix; s16 iy; @@ -1125,7 +1125,7 @@ void sub_803BBF4(s16 x, s16 y, u8 action, u8 maxDanger, u8 unk) { } if (unk) { - sub_8040F28(x, y, GetUnitMovementCost(gActiveUnit)); + GenerateExtendedMovementMapOnRangeNeglectWall(x, y, GetUnitMovementCost(gActiveUnit)); } else { sub_8040F54(x, y, gActiveUnit); } @@ -1441,20 +1441,20 @@ void SaveNumberOfAlliedUnitsIn0To8Range(struct Unit* unit) { } } - unit->_u46 = count; + unit->ai_counter = count; return; } //! FE8U = 0x0803C258 void CharStoreAI(struct Unit* unit, const struct UnitDefinition* uDef) { - unit->ai1 = uDef->ai[0]; + unit->ai1 = uDef->ai[UDEF_AIIDX_AI_A]; - unit->ai2 = uDef->ai[1]; + unit->ai2 = uDef->ai[UDEF_AIIDX_AI_B]; - unit->ai3And4 &= 0xFFF8; - unit->ai3And4 |= uDef->ai[2]; - unit->ai3And4 |= (uDef->ai[3] << 8); + unit->ai_config &= ~AI_UNIT_CONFIG_HEALTHRESHOLD_MASK; // clear heal threshold + unit->ai_config |= uDef->ai[UDEF_AIIDX_AI_CONF_L]; + unit->ai_config |= (uDef->ai[UDEF_AIIDX_AI_CONF_H] << 8); return; } @@ -1580,7 +1580,7 @@ s8 sub_803C44C(struct Unit* unit) { //! FE8U = 0x0803C490 void sub_803C490(struct Unit* unit) { - if (gAiState.flags & AI_FLAG_1) { + if (gAiState.flags & AI_FLAG_STAY) { GenerateUnitMovementMapExt(unit, 0); } else { GenerateUnitMovementMap(unit); diff --git a/src/cpextra_80407F0.c b/src/cpextra_80407F0.c index d4ab8efd2..d11cdf8c7 100644 --- a/src/cpextra_80407F0.c +++ b/src/cpextra_80407F0.c @@ -61,7 +61,7 @@ int GetSpecialItemFuncIndex(u16 item) { s8 AiTryDoSpecialItems() { int i; - if (gAiState.flags & AI_FLAG_1) { + if (gAiState.flags & AI_FLAG_STAY) { return 0; } @@ -382,7 +382,7 @@ void sub_8040EF8(struct Unit* unit) { return; } -void sub_8040F28(int x, int y, const s8* cost) { +void GenerateExtendedMovementMapOnRangeNeglectWall(int x, int y, const s8* cost) { AiSetMovCostTableWithPassableWalls(cost); SetWorkingBmMap(gBmMapRange); @@ -447,10 +447,10 @@ void sub_8041054(struct Unit* unit) { void AiUpdateNoMoveFlag(struct Unit* unit) { - if ((unit->ai3And4 & (1 << 13)) != 0) { - gAiState.flags |= (1 << 1); + if ((unit->ai_config & AI_UNIT_CONFIG_FLAG_STAY) != 0) { + gAiState.flags |= AI_FLAG_STAY; } else { - gAiState.flags &= ~(1 << 1); + gAiState.flags &= ~AI_FLAG_STAY; } return; @@ -548,8 +548,8 @@ s8 AiTryUseNightmareStaff(struct UnknownAiInputA* input) { yBest = 0; if (gPlaySt.chapterTurnNumber < 2) { - gAiState.unk86[0] = 0; - gAiState.unk86[1] = 0; + gAiState.cmd_result[0] = 0; + gAiState.cmd_result[1] = 0; return 0; } else { @@ -611,15 +611,15 @@ s8 AiTryUseNightmareStaff(struct UnknownAiInputA* input) { } if (countA >= input->unk_01) { - gAiState.unk86[0] = 1; - gAiState.unk86[1] = xBest; - gAiState.unk86[2] = yBest; - gAiState.unk86[3] = uidBest; - gAiState.unk86[4] = itemIdx; - gAiState.unk86[5] = countB; + gAiState.cmd_result[0] = 1; + gAiState.cmd_result[1] = xBest; + gAiState.cmd_result[2] = yBest; + gAiState.cmd_result[3] = uidBest; + gAiState.cmd_result[4] = itemIdx; + gAiState.cmd_result[5] = countB; } else { - gAiState.unk86[0] = 0; - gAiState.unk86[1] = countB; + gAiState.cmd_result[0] = 0; + gAiState.cmd_result[1] = countB; } return 0; @@ -631,7 +631,7 @@ s8 AiTryUseNightmareStaff(struct UnknownAiInputA* input) { s8 AiDecideNightmareStaff() { - AiSetDecision(gAiState.unk86[1], gAiState.unk86[2], AI_ACTION_DKNIGHTMARE, gAiState.unk86[3], gAiState.unk86[4], 0, 0); + AiSetDecision(gAiState.cmd_result[1], gAiState.cmd_result[2], AI_ACTION_DKNIGHTMARE, gAiState.cmd_result[3], gAiState.cmd_result[4], 0, 0); return 1; } @@ -654,9 +654,9 @@ s8 AiTryDKSummon(struct UnknownAiInputB* input) { int totalSummonedUnits = 0; if (gPlaySt.chapterTurnNumber == 1) { - gAiState.unk86[0] = 1; - gAiState.unk86[1] = gActiveUnit->xPos; - gAiState.unk86[2] = gActiveUnit->yPos; + gAiState.cmd_result[0] = 1; + gAiState.cmd_result[1] = gActiveUnit->xPos; + gAiState.cmd_result[2] = gActiveUnit->yPos; return 0; } @@ -704,19 +704,19 @@ s8 AiTryDKSummon(struct UnknownAiInputB* input) { } if (totalSummonedUnits < input->unk_01) { - gAiState.unk86[0] = 1; - gAiState.unk86[1] = gActiveUnit->xPos; - gAiState.unk86[2] = gActiveUnit->yPos; + gAiState.cmd_result[0] = 1; + gAiState.cmd_result[1] = gActiveUnit->xPos; + gAiState.cmd_result[2] = gActiveUnit->yPos; return 0; } } - gAiState.unk86[0] = 0; + gAiState.cmd_result[0] = 0; return 0; } s8 AiDecideDKSummon() { - AiSetDecision(gAiState.unk86[1], gAiState.unk86[2], AI_ACTION_DKSUMMON, 0, 0, 0, 0); + AiSetDecision(gAiState.cmd_result[1], gAiState.cmd_result[2], AI_ACTION_DKSUMMON, 0, 0, 0, 0); return 1; } diff --git a/src/eventscr.c b/src/eventscr.c index d718bdc26..8139d6a25 100644 --- a/src/eventscr.c +++ b/src/eventscr.c @@ -3173,10 +3173,10 @@ u8 Event32_SpawnSingleUnit(struct EventEngineProc * proc) unitDef.items[2] = 0; unitDef.items[3] = 0; - unitDef.ai[0] = 0; - unitDef.ai[1] = 0; - unitDef.ai[2] = 0; - unitDef.ai[3] = 0; + unitDef.ai[UDEF_AIIDX_AI_A] = 0; + unitDef.ai[UDEF_AIIDX_AI_B] = 0; + unitDef.ai[UDEF_AIIDX_AI_CONF_L] = 0; + unitDef.ai[UDEF_AIIDX_AI_CONF_H] = 0; LoadUnit_800F704(&unitDef, 0, 0, subcmd == 0xf); diff --git a/src/eventscr3.c b/src/eventscr3.c index 327eaea29..8cc1e7999 100644 --- a/src/eventscr3.c +++ b/src/eventscr3.c @@ -37,18 +37,18 @@ void ChangeUnitAi(struct Unit * unit, u8 ai1, u8 ai2, u8 unused) return; } - if (ai1 != 0x15) + if (ai1 != AI_A_INVALID) { unit->ai1 = ai1; - unit->ai1data = 0; + unit->ai_a_pc = 0; } - if (ai2 != 0x13) + if (ai2 != AI_B_INVALID) { unit->ai2 = ai2; - unit->ai2data = 0; + unit->ai_b_pc = 0; - if (ai2 == 0xc) + if (ai2 == AI_B_0C) { unit->aiFlags |= AI_UNIT_FLAG_3; } diff --git a/src/mapanim_specialeffect.c b/src/mapanim_specialeffect.c index 266a5079d..a79bed0a6 100644 --- a/src/mapanim_specialeffect.c +++ b/src/mapanim_specialeffect.c @@ -309,8 +309,8 @@ void LoadGorgonFromEgg(struct MAEffectProc * proc) gUnitDefEggHatching.ai[0] = proc->unit->ai1; gUnitDefEggHatching.ai[1] = proc->unit->ai2; - gUnitDefEggHatching.ai[2] = proc->unit->ai1data; // this looks incorrect - gUnitDefEggHatching.ai[3] = proc->unit->ai1data >> 8; // this is 0 + gUnitDefEggHatching.ai[2] = proc->unit->ai_a_pc; // this looks incorrect + gUnitDefEggHatching.ai[3] = proc->unit->ai_a_pc >> 8; // this is 0 LoadUnits(&gUnitDefEggHatching); GetUnitFromCharId(CHARACTER_MONSTER_BA);