Skip to content

Commit

Permalink
Merge pull request #618 from MokhaLeee/aireview
Browse files Browse the repository at this point in the history
review on AI related code
  • Loading branch information
RevoSucks authored May 31, 2024
2 parents 498f101 + a846d29 commit cffb1e0
Show file tree
Hide file tree
Showing 19 changed files with 409 additions and 327 deletions.
8 changes: 4 additions & 4 deletions include/bmsave.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
27 changes: 20 additions & 7 deletions include/bmunit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
94 changes: 85 additions & 9 deletions include/cp_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(???);
Expand Down
4 changes: 2 additions & 2 deletions include/cp_perform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
5 changes: 3 additions & 2 deletions include/cp_script.h
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions include/cp_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions src/bmsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit cffb1e0

Please sign in to comment.