8#define FORMATION_COUNT 380
12#if (LANGUAGE != LANGUAGE_JAPANESE)
13#define ENEMY_NAME_SUFFIX_COUNT 9
15#define ENEMY_NAME_SUFFIX_COUNT 26
38 for (memberIndex = 0; memberIndex < 5; memberIndex++) {
46 for (memberIndex = 0; memberIndex < 5; memberIndex++) {
58 levelTotal += enemy->
level;
63 return levelTotal / enemyCount;
108 s32 candidateOrUnitIndex;
114 u16 *unitCursor = party;
116 candidateOrUnitIndex = unitCount;
120 candidateOrUnitIndex--;
123 }
while (candidateOrUnitIndex != 0);
127 targetLevel = levelTotal / unitCount;
129 if (targetLevel <= 0)
131 if (targetLevel > 99)
134 for (candidateOrUnitIndex = 0; candidateOrUnitIndex <= 31;
135 candidateOrUnitIndex++)
136 pool[candidateOrUnitIndex].
score = -1;
140 for (candidateOrUnitIndex = 0; (
u32)candidateOrUnitIndex <= 19;
141 candidateOrUnitIndex++) {
151 candidateOrUnitIndex++) {
155 s32 lowestIndex = -1;
158 for (poolIndex = 0, lowestLevel = 999; poolIndex <= 31; poolIndex++) {
159 if (pool[poolIndex].
score < lowestLevel) {
160 lowestLevel = pool[poolIndex].
score;
161 lowestIndex = poolIndex;
165 if (lowestIndex >= 0) {
167 pool[lowestIndex].
recordId = candidateOrUnitIndex;
177 if (matchCount != 0) {
181 *outMargin = targetLevel - chosen->
score;
183 *outMargin = matchCount;
215 value += levels * 97 / 10;
216 minimumStat = base->
baseHp * 7 / 10;
217 CLAMP(value, minimumStat, 9999);
221 value += levels * 15 / 10;
222 minimumStat = base->
basePp * 7 / 10;
223 CLAMP(value, minimumStat, 9999);
226 minimumStat = levels * 123 / 10;
228 value += minimumStat;
230 CLAMP(value, minimumStat, 999);
233 minimumStat = levels * 33 / 10;
235 value += minimumStat;
237 CLAMP(value, minimumStat, 999);
240 minimumStat = levels * 51 / 10;
242 value += minimumStat;
244 CLAMP(value, minimumStat, 999);
250 CLAMP(value, minimumStat, 200);
254 unit->
level += levels;
278 for (classIndex = 0; classIndex < classCount; classIndex++) {
282 if (classIndex != classCount) {
292 for (; attempt <= 31; attempt++) {
296 if ((battle->
summonMasks[classIndex] & (1 << (
s8)suffix)) == 0)
302 if (classCount <= 4) {
335 for (index = 0; index < count; index++) {
339 if (index == count || battle->
summonMasks[index] == 0)
342 for (nameLength = 0; nameLength <= 13; nameLength++) {
343 if (unit->
name[nameLength] == 0)
373 for (; enemyIndex <= 5; enemyIndex++) {
375 nameByte = unit->
side;
383 if (unit->
name[0] == 0) {
384 unit->
name[0] = firstSuffix;
385 unit->
name[nameByte] = nameIndex;
393 nameByte = unit->
name[nameIndex];
396 terminatorIndex = nameIndex + 1;
397 unit->
name[nameIndex] = firstSuffix;
398 unit->
name[terminatorIndex] = nameByte;
415 s32 remainingMembers;
421 choicesBase = choices;
425 choiceCursor = choicesBase;
426 remainingMembers = 4;
428 if (*maximumCount++ != 0) {
434 }
while (remainingMembers >= 0);
436 return choices[(
Random16() * choiceCount) >> 16];
467 s32 memberOrCopyIndex;
470 u32 memberOrEnemyIndex;
476 enemyClasses = listBuffer;
482 for (memberIndex = 0;
486 if (memberIndex == 5)
493 for (memberOrEnemyIndex = 0; memberOrEnemyIndex <= 4; memberOrEnemyIndex++) {
506 for (memberOrEnemyIndex = 0; memberOrEnemyIndex <= 4; memberOrEnemyIndex++) {
510 memberCounts[memberOrEnemyIndex] = minimum =
513 additionalRoom = maximum - minimum;
514 if (additionalRoom > 0) {
519 fittingCount = freeSpriteSlots / memberSlots;
520 if (fittingCount < additionalRoom)
521 additionalRoom = fittingCount;
522 extraCounts[memberOrEnemyIndex] =
523 ((additionalRoom + 1) *
Random16()) >> 16;
525 extraCounts[memberOrEnemyIndex] = 0;
533 for (memberOrEnemyIndex = 0; memberOrEnemyIndex <= 4;
534 memberOrEnemyIndex++) {
536 if (extraCounts[memberOrEnemyIndex] != 0) {
538 if (spriteSlots > freeSpriteSlots) {
539 extraCounts[memberOrEnemyIndex] = 0;
541 memberCounts[memberOrEnemyIndex]++;
542 extraCounts[memberOrEnemyIndex]--;
543 freeSpriteSlots -= spriteSlots;
555 for (memberOrEnemyIndex = 0; memberOrEnemyIndex <= 4; memberOrEnemyIndex++)
556 memberOrder[memberOrEnemyIndex] = memberOrEnemyIndex;
557 for (memberOrEnemyIndex = 0; memberOrEnemyIndex <= 9;
558 memberOrEnemyIndex++) {
561 s32 swap = memberOrder[first];
563 memberOrder[first] = memberOrder[second];
564 memberOrder[second] = swap;
566 for (memberOrEnemyIndex = 0; memberOrEnemyIndex <= 4;
567 memberOrEnemyIndex++) {
569 s32 slot = memberOrder[memberOrEnemyIndex];
571 for (copyIndex = 0; copyIndex < memberCounts[slot]; copyIndex++)
572 enemyClasses[enemyCount++] =
580 for (memberOrEnemyIndex = 0; memberOrEnemyIndex <= 4;
581 memberOrEnemyIndex++) {
582 if (memberCounts[memberOrEnemyIndex] != 0)
583 memberOrder[remainingKinds++] = memberOrEnemyIndex;
585 if (remainingKinds == 0)
587 memberOrCopyIndex = memberOrder[(
Random16() * remainingKinds) >> 16];
588 enemyClasses[enemyCount++] =
590 memberCounts[memberOrCopyIndex]--;
594 for (memberOrEnemyIndex = 0; memberOrEnemyIndex <= 4;
595 memberOrEnemyIndex++) {
596 copies = memberCounts[memberOrEnemyIndex];
597 for (memberOrCopyIndex = 0; memberOrCopyIndex < copies;
599 enemyClasses[enemyCount++] =
605 enemyClasses[enemyCount] = 0;
610 memberOrEnemyIndex++)
613 memberOrEnemyIndex = 0;
614 if (enemyClasses[0] == 0)
623 if (suffixCode & 0x8000)
626 enemyClasses[memberOrEnemyIndex], suffixCode & 0x7fff);
628 if (levelAdjustment != 0)
632 memberOrEnemyIndex++;
633 if (memberOrEnemyIndex > 5)
635 if (enemyClasses[memberOrEnemyIndex] != 0)
638 return memberOrEnemyIndex;
s32 BattleClass_IsSmall(s32 index)
Returns 1 for a small class, which takes one enemy sprite slot instead of two and stands closer to it...
s32 BattleParty_PrepareActiveOwners(u16 *owners)
Lists the party members who fight (the first four of the party, three in a link battle) and marks eac...
u16 gBattleFormation_LevelMatchedEnemies[]
What every part of the battle object shares: its includes, in their original order,...
#define ENEMY_CLASS_FIRST
An enemy's class id is its row of enemies.tsv plus 8 (Enemy_GetDefinition); class ids 0 to 7 are the ...
#define FORMATION_INTERLEAVED
#define FORMATION_SHUFFLED
BattleWork.battleType, from the enemy formation (BattleFormation_BuildEnemyList): the random formatio...
#define FIRST_ENEMY_UNIT
Unit ids from 128 are the enemies'; in a link battle the opponent's party takes the first three.
#define ENEMY_BATTLE_SIZE
#define ENEMY_DEFEATED_FLAG(classId)
The game flag of an enemy class the party has defeated, set by BattleEnemy_RecordDefeat outside the m...
struct BattleWork * gBattleWork
struct BattleUnit * UnitGetFar(u32 owner)
s32 BattleUnitSetFar(s32 unitId, s32 enemyId, s32 suffix)
s32 GameFlag_TestFar(s32 flag)
const u8 * Enemy_GetDefinitionFar(s32 selector)
s32 GameFlag_GetByteFar(s32 flag)
void GameFlag_ClearBitFar(s32 flag)
void UnitCalcFar(s32 owner)
#define FLAG_MONSTER_BATTLE_WINS
Monster battles won in a row, added to the monsters' level; -1 after the series ends.
#define FLAG_MONSTER_BATTLE
The link lobby's battle against a monster when no opponent is linked: formations matched to the party...
void * Runtime_BumpAllocateAlternatePool(s32 size)
As MemAlloc, but tries EWRAM first.
static s32 Iwram_CopyWords(volatile void *destination, const volatile void *source, s32 size)
static s32 Iwram_ClearWords(void *destination, s32 size)
#define CLAMP(v, low, high)
Keeps v within low to high, the low bound tested first.
u32 Random16(void)
Advances the game's random number generator (the C library's classic linear congruential one),...
void MemFree(void *buffer)
A copy of a unit's base stats, from baseHp to its element stats, kept while its level is adjusted: Ba...
u8 baseLuck
copied, not read
s16 ppGauge
copied, not read
s16 hpGauge
copied, not read
struct BattleElementStats element[4]
the base power is read
u8 hpRegen
copied, not read
u8 actionCount
copied, not read
u8 ppRegen
copied, not read
A unit's elemental power and resistance in one element.
s16 power
Elemental power in the element.
A unit in battle: a party member's or an enemy's stats, Psynergy, items, Djinn and ailments.
u16 baseDefense
before equipment, Djinn, class and stages
u16 baseAttack
before equipment, Djinn, class and stages
u16 baseAgility
before equipment, Djinn, class and stages
u8 side
BATTLE_UNIT_ABSENT (0), BATTLE_UNIT_ENEMY (1) or BATTLE_UNIT_PARTY (2).
u8 name[0x0f]
The name, one byte a character, ending in 0.
struct BattleElementStats element[4]
Power and resistance in each element, 0 to 200, after equipment, element levels and Resistance stages...
u8 classId
Who the unit is: a character's id (ISAAC to JENNA, below 8) or an enemy's id (BattleUnitSet).
struct BattleElementStats baseElement[4]
Power and resistance before equipment and stages: an enemy's from its element preset,...
s16 basePp
before equipment, Djinn and class (UnitCalc)
u8 classIndex
The character's class, worked out from its Djinn (Unit_UpdateClass); 0 marks an enemy,...
s16 baseHp
before equipment, Djinn and class (UnitCalc)
u8 level
1 to 99 for a character
The battle's own work: its enemies, units and their objects, the turn's actions, the spoils,...
u16 firstDefeated
The enemyClasses index of the first class to fall; 6 for none.
u8 summonCount
classes listed in enemyClasses, at most 5
u16 enemyClasses[6]
The enemy classes met so far, for their name letters, and the first class to fall (firstDefeated).
s8 summonChannels[6]
The letter each class in enemyClasses gave out last; -1 for none yet.
u16 defeatState
DEFEAT_ONE, DEFEAT_SEVERAL or DEFEAT_BY_PARTY_MEMBER.
u8 battleType
FORMATION_SHUFFLED or FORMATION_INTERLEAVED.
u32 summonMasks[6]
Name letters in use by each class in enemyClasses, a bit per letter.
An enemy's definition: its level and stats, items, element preset, the actions it chooses from and it...