9#define BG_PALETTE_14 ((u16 *)&BG_PLTT_COLOR(14, 0))
34 angle = (frameCount * 3U) << 10;
36 brightness = (0x10000 -
Trig_Cos(angle)) / 0x2AAA;
38 blue = (color >> 10) & channelMask;
39 green = (color >> 5) & channelMask;
40 red = color & channelMask;
45 if (blue < 0 || blue > 31)
47 if (green < 0 || green > 31)
49 if (red < 0 || red > 31)
52 *palette++ = (blue << 10) | (green << 5) | red;
54 }
while (remainingColors >= 0);
83 camera->
pos[1] = 160 << 11;
87 camera->
yaw = 128 << 7;
88 camera->
pitch = 244 << 8;
112 transition->
flag = 1;
125 for (unitIndex = 0; unitIndex < count; unitIndex++) {
135#define BATTLE_START_NO_ESCAPE 2
164 for (unitIndex = escaped; unitIndex < livingCount; unitIndex++) {
167 chance += levelTotal * 500 / livingCount;
170 for (unitIndex = 0; unitIndex < livingCount; unitIndex++) {
173 chance -= levelTotal * 500 / livingCount;
175 if ((chance > 0) && (((10000 *
Random16()) >> 0x10) < chance)) {
178 *failedAttempts += 1;
195 s32 choosingCount = 0;
199 for (unitIndex = 0; unitIndex < unitCount; unitIndex++) {
207 entry->
unitId = unitIds[unitIndex];
214 choosingUnits[choosingCount++] = unitIds[unitIndex];
223 entries += entryCount;
229 unitCount = entryCount + appended;
249 s32 shuffleOrUnitIndex;
256 if (unitCount == 0) {
260 for (shuffleOrUnitIndex = 31; shuffleOrUnitIndex >= 0; shuffleOrUnitIndex--) {
263 s32 swap = unitIds[first];
264 unitIds[first] = unitIds[second];
265 unitIds[second] = swap;
275 if (limit < unitCount) {
280 for (shuffleOrUnitIndex = 0; shuffleOrUnitIndex < unitCount;
281 shuffleOrUnitIndex++) {
282 s32 unitId = unitIds[shuffleOrUnitIndex];
293 if (copyIndex != 0) {
297 if (unit->
sleep != 0 || unit->
stun != 0) {
317#define DJINN_FIRST_PRIORITY 10000
328 for (actionIndex = 0; actionIndex < count; actionIndex++) {
350 for (sortIndex = count - 1; sortIndex > 0; sortIndex--) {
351 if ((
s16)entries[sortIndex].priority >
352 (
s16)entries[sortIndex - 1].priority) {
364 }
while (swapCount != 0);
379#define LINK_PACKET_BYTES 20
380#define LINK_TRANSFER_TIMEOUT 300
382#define LINK_ACTIONS_BYTES(count) \
383 ((u32)((count) * 16 + LINK_PACKET_BYTES - 1) / LINK_PACKET_BYTES * \
408 s32 BattleLink_SendActions(
void) {
428 if (allocationSize) {
449 s32 BattleLink_ReceiveActions(
void) {
475 result = state->
count;
500 for (index = 0; index <
count; index++) {
519 state->
count = count;
524 if (BattleLink_SendActions() < 0 || BattleLink_ReceiveActions() < 0)
526 result = state->
count;
528 if (BattleLink_ReceiveActions() < 0)
530 result = state->
count;
531 state->
count = count;
539 for (index = 0; index < result; index++) {
573 for (colorIndex = 0; colorIndex <= 15; colorIndex++) {
580 paletteIndex = sourceBank * 16 + colorIndex;
582 blue = (color >> 10) & channelMask;
583 green = (color >> 5) & channelMask;
584 red = color & channelMask;
600 adjustedColor = blue << 10;
601 adjustedColor |= green << 5;
602 adjustedColor |= red;
605 ((
s16 *)0x04ffffe0)[paletteIndex] = adjustedColor;
609 }
while (bankIndex <= 1);
626 s32 remainingActions;
637 record[actionIndex].
priority = 0x8000;
686 remainingActions = count;
696 }
while (remainingActions != 0);
714 if ((taggedPlace & 0x80) != 0)
715 return battle->
enemyIds[taggedPlace & 0xf];
730 for (placeIndex = 0;; placeIndex++) {
735 return placeIndex | 0x100;
738 for (placeIndex = 0;; placeIndex++) {
739 listedUnit = battle->
enemyIds[placeIndex];
743 return placeIndex | 0x180;
void BattleCamera_SetRange(s32 centerX, s32 centerY, s32 offsetX, s32 offsetY, s32 scale)
Builds the perspective plane's next page for a zoom of scale about centerX and centerY (all 16....
s32 BattleParty_ListLivingUnits(s32 sideMask, u16 *unitIds)
Lists the units still standing (HP above 0) on the sides in sideMask (BATTLE_SIDE_PARTY,...
s32 BattleParty_ListActorIds(s32 sideMask, u16 *unitIds)
Lists the units in the battle's unit lists on the sides in sideMask, party first, skipping those remo...
void Camera_InitDefaultTransform(void)
Puts the battle camera at its default view of the battlefield's centre.
struct BattlePresentationTransition * gTransitionWork
struct BattleCamera * gCameraWork
What every part of the battle object shares: its includes, in their original order,...
#define GUARD_TOTAL_DEFENSE
#define FIRST_ENEMY_UNIT
Unit ids from 128 are the enemies'; in a link battle the opponent's party takes the first three.
#define TARGET_NONE_ENEMY
BattleActionRecord.target of an action that needs none: a held unit's lost turn (the enemies' and the...
#define LINK_MAX_MISSED_FRAMES
The frames a link battle tolerates without a connection before it gives up.
#define YAW_PARTY_TURN
The camera's yaw while a party member acts, and while an enemy does.
#define LINK_BOTH_CONNECTED
gLinkStatus: both consoles are connected.
#define TARGET_NONE_PARTY
#define GUARD_DEFEND
BattleUnit.guardLevel: Defend and Granite halve the damage, Total Defense and Flash cut it to a tenth...
@ BATTLE_ENCOUNTER_ENEMIES_FIRST
@ BATTLE_ENCOUNTER_PARTY_FIRST
@ BATTLE_UNIT_LIST_END
ends the list
@ BATTLE_UNIT_REMOVED
a place whose unit has left the battle
@ CMD_NONE
nothing this turn
@ CMD_DEFEND2
an enemy's Total Defense (effect 47)
@ ABILITY_EFFECT_TOTAL_DEFENSE
Total Defense, Flash.
@ ABILITY_EFFECT_DEFEND
Defend, Granite.
@ ABILITY_EFFECT_GROUND
Ground: the target's turn lost.
struct BattleWork * gBattleWork
s32 BattleCommand_SelectAutomatic(struct BattleActionRecord *request, s32 retry)
Chooses an enemy's action (a party member's is chosen in the menu): one of the eight in its definitio...
struct BattleUnit * UnitGetFar(u32 owner)
struct BattleAct * BattleActGet(s32 actionId)
Far call to BattleAction_GetDirect.
s32 GameFlag_SetBitFar(s32 flag)
s32 Djinn_GetAbilityIdFar(u32 group, u32 index)
s32 BattleRandomFar(void)
void GameFlag_ClearBitFar(s32 flag)
void UnitCalcFar(s32 owner)
void UiWork_FinalizeSharedSlotFar(void)
s32 Battle_CollectPartyCommandsFar(struct BattleActionRecord *out, u16 *in, s32 count)
#define FLAG_CHOOSING_COMMANDS
set while the commands are chosen
void WaitFrames(s32 frames)
The game's frame loop: runs frames frames and returns.
volatile u32 gFrameCount
Frames WaitFrames has finished.
struct GameState gGameState
void * Runtime_BumpAllocateAlternatePool(s32 size)
As MemAlloc, but tries EWRAM first.
@ HEAP_SLOT_BATTLE_BACKGROUND
#define WORK_SLOT(n)
Reads slots through their own addresses; adjacent slots are reached by offsets.
#define IRQ_UNLOCK(saved)
Restores the IME value saved by IRQ_LOCK.
#define IRQ_LOCK(saved)
Disables interrupts and saves IME in saved; IRQ_UNLOCK restores it.
static s32 Iwram_RatioMulQ14(s32 divisor, s32 value)
All calls go through this layer.
static s32 Iwram_CopyWords(volatile void *destination, const volatile void *source, s32 size)
static s32 Iwram_TransformVector(const void *vector, s32 *result)
s32 BattlePres_WaitSync(void)
In a link battle, shows the "EXEC" signature to the other console and waits up to 30 frames for it to...
u32 Random16(void)
Advances the game's random number generator (the C library's classic linear congruential one),...
void MemFree(void *buffer)
struct Projection gProjection
s32 BattlePresentation_BuildActions(struct BattleActionRecord *actions)
Builds the turn's actions and sorts them into turn order: the party's commands, then the link opponen...
void Palette_UpdatePulseBrightness(void)
Pulses BG palette 14's colours: each takes the matching colour of palette 15 brightened by up to twel...
#define LINK_TRANSFER_TIMEOUT
void Palette_CopyBanksWithBrightnessOffset(s32 offset)
Copies BG palette 15 to 14 and 5 to 4, brightened or darkened.
void BattleQueue_SortByPriority(struct BattleActionRecord *entries, s32 count)
Sorts the round's actions into turn order, fastest first, after moving the guarding and grounding Dji...
#define LINK_PACKET_BYTES
The link sends in packets of 20 bytes (the header is one), and a transfer may take 300 frames.
s32 Battle_GetTaggedSlotValue(s32 taggedPlace)
Returns the unit id at a list place.
s32 BattleEscape_CheckSuccess(void)
Rolls whether the party gets away.
s32 BattlePres_BuildUnitEntries(struct BattleActionRecord *entries)
The party's actions for the round, from the command menu: a member who is asleep, confused,...
#define DJINN_FIRST_PRIORITY
A Djinni whose effect is a guard or Ground acts before everyone else.
void Battle_ReservedNoOp9B2C(void)
Does nothing; Battle_RunEncounter calls it at the start of each round.
void BattleUnit_ResetGuardLevels(void)
Ends every unit's guard (Defend and the like) and recalculates its stats.
#define BATTLE_START_NO_ESCAPE
gGameState.battleStart of a battle the party cannot run from.
void Camera_ConfigureScene(s32 verticalOffset)
Sets the camera up for the fixed view the battle shows while commands are chosen, raised by verticalO...
s32 BattlePresentation_AppendLinkedActions(struct BattleActionRecord *actions, s32 count)
Link battle: exchanges the round's actions with the other console and appends the opponent's after th...
#define LINK_ACTIONS_BYTES(count)
The bytes of count actions, in whole packets.
void BattlePres_AdjustCameraByShoulderKeysAlt(void)
Turns the camera while L or R is held (a second copy).
#define BG_PALETTE_14
Background palette 14, which takes palette 15 brightened.
s32 Battle_FindTaggedSlotByValue(u32 unitId)
Returns the list place of unit id, or -1 if it is in neither list: the party's as 0x100 | place,...
s32 BattlePres_BuildOpponentEntries(struct BattleActionRecord *entries)
The enemies' actions for the round, in a shuffled order, each chosen by BattleCommand_SelectAutomatic...
s32 SerialRuntime_BeginReceive(void *destination)
Starts receiving the partner's blocks into destination: sets the receive destination and clears the r...
s32 SerialRuntime_BeginSend(void *source, s32 size)
Starts sending size bytes from source to the partner in 20-byte blocks: sets the send source,...
void SerialRuntime_RemoveIrqHandlers(void)
Clears gSerialExchangeActive, which stops the per-frame exchange, and removes the link interrupt hand...
void BattleLink_ResetTransferState(void)
s32 SerialRuntime_GetActiveTransfers(void)
Returns bit 0 while a send runs and bit 1 while a receive runs.
volatile u16 gLinkStatus
Link status, updated by the VBlank exchange handler.
volatile u16 gSerialReceivedSize
volatile: tagged where it steers, in BattleLink_ReceiveActions (tbs battle.c) and LinkLobby_ReceivePa...
An ability's definition, a row of gBattleAction_DefinitionTable: its targeting, kind,...
u8 effect
enum AbilityEffect
A unit's chosen action for the turn: its command, the command's parameter, its target and range,...
s16 target
the aimed unit's id
s16 parameter
an ability id, or CMD_ITEM's inventory index (-1 gone)
u16 slot
in a link battle, unitId as the opponent knows it
s16 command
enum BattleCommand
u16 priority
from the unit's Agility; higher acts first
The battle camera: its eye, the focus it turns about, and its pitch, yaw and distance.
s32 distance
eye to focus, 16.16 fixed-point pixels
s32 * followPos
when set, the focus followed instead of pos
s32 pos[3]
the focus, 16.16 fixed-point pixels
s32 unk18
cleared beside followPos; not read by name in TBS
The header a link battle sends before its actions: how many there are, the next random number to chec...
s32 check
the parent's next BattleRandomFar; the child checks its own
s32 seed
the parent's gRandomSeed, the round's battle seed
u8 unused[28]
padding; a header packet carries only the first 20 bytes
s32 count
actions that follow the header
The battle background's work slot as the camera reads it: the yaw the camera eases toward and the fra...
s32 flag
0 lets the camera update set the background's range each frame; 1 for a critical hit's zoom and once ...
s32 active
BattleBackgroundView's busy, at the same offset.
s32 targetYaw
1/65536 turns
A unit in battle: a party member's or an enemy's stats, Psynergy, items, Djinn and ailments.
s8 guardLevel
0, GUARD_DEFEND (1, half the damage) or GUARD_TOTAL_DEFENSE (2, a tenth); set by Defend and Granite,...
u8 level
1 to 99 for a character
u16 agi
Agility, 0 to 999: the order of the round's actions.
u8 actionEntryCount
Actions each round, 0 to 2: baseActionCount, one more while ready, and the equipment's extra turns (U...
The battle's own work: its enemies, units and their objects, the turn's actions, the spoils,...
u8 twoSided
1 in a link battle
u8 ownerSlots[8]
For each party unit, its slot as a link opponent's enemy, minus FIRST_ENEMY_UNIT; 0xff for none.
u8 encounterMode
BATTLE_ENCOUNTER_: who strikes first.
s16 partyUnits[7]
ends with BATTLE_UNIT_LIST_END
u8 escapeFailures
Failed attempts to run; each adds 2000 to the base chance of 5000.
struct BattleActionRecord actions[BATTLE_ACTION_CAPACITY]
The actions chosen for the turn.
u8 linkPaused
set when the link drops or the console's id changes
u8 linkSide
this console's multiplayer id
s16 enemyIds[BATTLE_ENEMY_CAPACITY+1]
ends with BATTLE_UNIT_LIST_END
A position in three coordinates, each in 16.16 fixed point.
u8 battleStart
how the next battle starts (Battle_SetStart)
#define DEFINE_ADJUST_CAMERA(name)
Moves camera yaw by 512 while L/R is held; two identical functions exist.