Alchemy
Loading...
Searching...
No Matches

link_timer.c

Go to the documentation of this file.
1/// @file
2/// The link battle timer and the no-escape check.
3/// Part of battle_command.c, the menu object, compiled only through it.
4
6
7#define COUNTDOWN_START_FRAMES 900
8#define FRAMES_PER_SECOND 60
9
10/// The partner console's signature as the link battle timer reads it: four
11/// letters that restart the countdown.
19};
20
21/// Advances the link command-entry timer, slides the battle panels, and redraws
22/// the remaining seconds. Pause flags and peer signatures stop the timer.
24 struct BattleMenuWork *state = gLinkCountdownWork;
25 struct BattleWork *runtime;
26 struct MenuSprite *entry;
27 u8 *active;
28 struct LinkSignature *signature;
29 s32 target;
30 s32 current;
31 s32 difference;
32 s32 step;
33 s32 newOffset;
34 s32 timer;
35 s32 nextTimer;
36 s32 seconds;
37 s32 i;
38
39 if (state != 0) {
40 target = state->targetSceneOffset;
41 current = state->sceneOffset;
42 if (target != current) {
43 difference = target - current;
44 step = difference / 3;
45 if (step == 0) {
46 step--;
47 if (difference >= 0)
48 step = 1;
49 }
50 newOffset = current + step;
51 state->sceneOffset = newOffset;
52 Camera_ConfigureSceneFar(newOffset);
53 }
54
55 entry = state->icons;
56 active = state->iconShown;
57 i = 2;
58 do {
59 if (*active++ != 0)
60 Runtime_PushSlotEntry(entry, 240);
61 i--;
62 entry++;
63 } while (i >= 0);
64 Link_DrawShiftedTilePair(0x06006680);
65
66 // Local battles still animate the UI, but do not run the link timer.
67 if (state->linkTimer == 0)
68 return;
69
70 runtime = gBattleWork;
71 if (runtime->linkPaused == 0)
72 goto load_timer;
73 state->timer = 0;
74 return;
75
76 signature_tail:
77 if (signature->v != 'V')
78 goto signature_done;
79 if (signature->s != 'S')
80 goto signature_done;
82 timer = state->timer;
83 goto signature_done;
84
85 load_timer:
86 timer = state->timer;
87 if (timer >= 0)
88 goto timer_ready;
89 signature =
90 (struct LinkSignature *)gLinkPeerSignatures[runtime->linkSide ^ 1];
91 if (signature->e != 'E')
92 goto signature_done;
93 if (signature->d == 'D')
94 goto signature_tail;
95
96 signature_done:
97 if (timer < 0)
98 return;
99
100 timer_ready:
101 if (state->timerWindow == 0 && state->timerHidden == 0) {
103 timer = state->timer;
104 }
105
106 if (timer > 0) {
107 nextTimer = timer - 1;
108 state->timer = nextTimer;
109 timer = nextTimer;
110 }
111 if (timer < 0)
112 return;
113
114 // Round remaining frames upward so zero appears only after time expires.
115 seconds = (timer + FRAMES_PER_SECOND - 1) / FRAMES_PER_SECOND;
116 if (seconds != 0 && seconds * FRAMES_PER_SECOND == timer)
118
119 if (state->timerWindow != 0)
120 UiText_DrawNumberInWindow(seconds, 2,
121 (struct UiWindow *)state->timerWindow, 16, 8);
122 }
123}
124
125/// Tests whether this battle forbids escape. Show the reason until A/B or the
126/// link timer expires, returning 1 when escape is forbidden and 0 otherwise.
128 s16 buffer[64];
129 struct UiWindow *work;
130 s32 result;
131 struct BattleWork *battle;
132 s32 mode;
133
134 battle = gBattleWork;
135 mode = gGameState.battleStart;
136 if (gGameState.battleStart == 2 || mode <= 2 || (result = 1, mode > 4)) {
137 result = 0;
138 }
139 if (result == 0) {
140 if ((s8)battle->challengeFlag != 0) {
141 result = 1;
142 }
143 }
144 if (result != 0) {
145 work = UiWindow_Create(0, 7, 30, 4, 42);
147 BATTLE_MENU_MESSAGE_COPY(MsgNoTimeToRun, buffer,
149 UiText_RenderWideStringAtOffset(buffer, work, 0, 4);
150 do {
151 WaitFrames(1);
152 } while ((gKeysPressed & (KEY_A | KEY_B)) == 0 &&
154 UiWork_Finalize(work, 1);
155 }
156 return result;
157}
What every part of the menu object shares: its includes, edition values, macros and the link round st...
#define BATTLE_MENU_MESSAGE_COPY
#define BATTLE_MENU_MESSAGE_CAPACITY
struct BattleWork * gBattleWork
void Camera_ConfigureSceneFar(s32 pos)
void AudioCommand_PlayFar(s32 id)
void WaitFrames(s32 frames)
The game's frame loop: runs frames frames and returns.
Definition frame.c:97
struct GameState gGameState
u16 gLinkPeerSignatures[][12]
24 bytes per peer
void UiText_RenderWideStringAtOffset(u16 *text, struct UiWindow *window, s32 x, s32 y)
Draws a zero-terminated string of 16-bit character codes into a text canvas window at pixel (x,...
Definition glyphs.c:258
@ KEY_B
Definition io_reg.h:244
@ KEY_A
Definition io_reg.h:243
volatile u32 gKeysPressed
Keys that went down this frame.
struct BattleMenuWork * gLinkCountdownWork
struct UiWindow * Link_CreateCountdownLabelWindow(void)
Creates the link timer window and draws its fixed time heading.
Definition pictures.c:392
void Link_DrawShiftedTilePair(s32 offset)
Animates the two link indicator tiles with opposing two/three-pixel shifts.
Definition pictures.c:237
void Runtime_PushSlotEntry(void *slotEntry, s32 slot)
Links a sprite entry into depth slot slot (clamped to 0..255) for this frame.
Definition render.c:79
#define SE_TRIPLE_TONE_LOW
Definition songs.h:101
The battle command menus' work, heap slot 57 (0x104 bytes), reached as gLinkCountdownWork.
Definition menu_list.h:107
s32 linkTimer
1 while the link battle's timer runs
Definition menu_list.h:132
s32 timerWindow
The window that shows the link battle's seconds left, a struct UiWindow pointer kept as a word (as a ...
Definition menu_list.h:126
s32 timerHidden
Set while the status screen is open, so the timer opens no window.
Definition menu_list.h:128
s32 sceneOffset
where the slide has reached; a third of the way a frame
Definition menu_list.h:116
s32 timer
Frames left to choose commands in a link battle; -1 outside link battles.
Definition menu_list.h:131
u8 iconShown[3]
whether each of icons is drawn
Definition menu_list.h:111
s32 targetSceneOffset
Where the battle scene slides to (Camera_ConfigureScene's vertical offset): -2 or 16 by the target me...
Definition menu_list.h:115
struct MenuSprite icons[3]
The sprites UpdateLinkSessionCountdown draws each frame: the chosen command's icon,...
Definition menu_list.h:110
The battle's own work: its enemies, units and their objects, the turn's actions, the spoils,...
Definition battle_work.h:71
u8 challengeFlag
bits: 1 a party unit was challenged; nonzero, the party cannot run
Definition battle_work.h:89
u8 linkPaused
set when the link drops or the console's id changes
u8 linkSide
this console's multiplayer id
Definition battle_work.h:98
u8 battleStart
how the next battle starts (Battle_SetStart)
Definition game_state.h:92
The partner console's signature as the link battle timer reads it: four letters that restart the coun...
Definition link_timer.c:12
u8 padding000[8]
Definition link_timer.c:13
u8 padding010[8]
Definition link_timer.c:18
A menu sprite: a list link and its OAM attributes, as two words or as fields.
Definition menu_list.h:24
A window: a framed rectangle of the tilemap with its sprite outputs, its opening or closing animation...
Definition uiwindow.h:11
void UiText_DrawNumberInWindow(s32 value, s32 width, struct UiWindow *window, s32 x, s32 y)
Draws a number (width as UiText_FormatNumber) into a window's tilemap at pixel (x,...
Definition text_tiles.c:678
signed int s32
Definition types.h:14
signed char s8
Definition types.h:10
signed short s16
Definition types.h:12
unsigned short u16
Definition types.h:13
unsigned char u8
Definition types.h:11
s32 Ui_FillVramBlockPattern(void)
Fills the text canvas with the window background colour (4).
Definition windows.c:619
void UiWork_Finalize(struct UiWindow *window, s32 instant)
Closes a window: clears its contents and remembers its last rectangle.
Definition windows.c:349
struct UiWindow * UiWindow_Create(s32 x, s32 y, s32 width, s32 height, s32 attrs)
Opens a window at tile (x, y) of width by height tiles in the first free window slot,...
Definition windows.c:279