Alchemy
Loading...
Searching...
No Matches

context.c

Go to the documentation of this file.
1/// @file
2/// An actor's screen position, the encounter transition, context effects.
3/// Part of psynergy_effects.c, compiled only through it.
4
6
7/// Finds where an actor is on screen: position[0] and position[1] receive its
8/// x and y in pixels, y at the top of its sprite. Returns 0, or -1 if there is
9/// no such actor. Called by the message window code in messages.c to place a
10/// speaker's bubble, and through Object_GetScreenPositionFar by the window
11/// frames to open a message window above or below the leader.
12s32 Object_GetScreenPosition(s32 objectId, s32 *position) {
13 struct ObjectRuntime *object = ObjectTable_Get(objectId);
14#ifdef UBFIX
15 struct MapScrollWork *map;
16#else
17 s32 *camera;
18#endif
19 s32 cameraX;
20 s32 cameraZ;
21 s32 x;
22 s32 z;
23
24 if (object == 0)
25 return -1;
26 // UB: camera[1] crosses the viewX scalar; UBFIX reads the named pair.
27#ifdef UBFIX
28 map = gMapWork[0];
29 cameraX = map->viewX & 0xffff0000;
30 cameraZ = map->viewY & 0xffff0000;
31#else
32 /* FAKEMATCH(temporary): the view position read as two adjacent words
33 * through one pointer, so both loads share one base; as viewX and viewY
34 * through gMapWork[0], 28 of 59 instructions differ in tbs-en. */
35 camera = &((struct MapScrollWork *)gMapWork[0])->viewX;
36 cameraX = camera[0] & 0xffff0000;
37 cameraZ = camera[1] & 0xffff0000;
38#endif
39 x = object->x - cameraX;
40 z = object->z - cameraZ - object->y;
41 *position++ = x / 0x10000;
42 *position = z / 0x10000;
43 if ((object->animationKind & 15) == 1)
44 *position -=
46 ((struct AnimationObject *)object->animation)->entries[0]->animId)
47 ->boxX;
48 return 0;
49}
50
52
53// UB: no value is returned, and run_scene.c ignores it; as void, the epilogue
54// changes.
55/// Starts the transition into a random battle: plays the scene's battle cue
56/// and the encounter sounds, closes the screen and flashes the backdrop white,
57/// fading it to black over 16 frames. Called by Field_RunScene when it handles
58/// work->battle; clears work->transitionActive once the screen is closed.
59#ifdef UBFIX
61#else
63#endif
64 struct FieldWork *work = (struct FieldWork *)gEventWork;
65 s32 i;
66 s32 level;
67
71 if (work->mode == MODE_WORLD_MAP) {
72 // On the world map the flash colours BG palette colour 243 instead.
73 // The world map closes with transition 0x401, a field scene with 0x207.
74 Io_Write16(RGB(31, 31, 31), (u16 *)&BG_PLTT_COLOR(15, 3));
75 DisplayTransition_Finish(0x401, 16);
76 work->transitionActive = 0;
77 WaitFrames(16);
78 // Step the white down by 2 a frame: 30 to 0 in 16 frames.
79 for (i = 0; i < 16; i++) {
80 level = 30 - i * 2;
81 Io_Write16(RGB(level, level, level), (u16 *)&BG_PLTT_COLOR(15, 3));
82 WaitFrames(1);
83 }
84 } else {
85 Io_Write16(RGB(31, 31, 31), &PLTT_BACKDROP);
86 DisplayTransition_Finish(0x207, 16);
87 work->transitionActive = 0;
88 WaitFrames(16);
89 for (i = 0; i < 16; i++) {
90 level = 30 - i * 2;
91 Io_Write16(RGB(level, level, level), &PLTT_BACKDROP);
92 WaitFrames(1);
93 }
94 }
95}
96
97/// Starts the leader's animation for leaving through an exit: centres it on
98/// its tile, at the tile's south edge, stops it and plays animation over 18
99/// frames. The leader's sprite gains a second part (metadata 27) for it.
100/// Called by the two ObjectEffect_BeginContextEffect functions below.
102 u32 zero;
103 u8 param;
104 struct ObjectRuntime *object;
105 struct AnimationObject *context;
106 struct AnimationEntry *effect;
107
109 context = object->animation;
110 effect = (struct AnimationEntry *)ResourceMetadata_RegisterFar(context, 27);
111 zero = 0;
112 param = 15;
113
114 context->flags = zero;
115 effect->param = param;
116 // Positions are 16.16 fixed point and tiles 16 pixels: keep the tile and
117 // put the leader at its middle in x (8 pixels) and its south edge in z.
118 object->x = (object->x & 0xFFF00000) + 0x80000;
119 object->z = (object->z & 0xFFF00000) + 0x100000;
120 object->velocityX = zero;
121 object->velocityZ = zero;
122 object->targetX = ACTOR_NO_TARGET;
123 object->targetZ = ACTOR_NO_TARGET;
124 ObjectDispatch_SelectAnimationFar(object, animation);
125 WaitFrames(18);
126}
127
128/// Leaves through an exit of kind 0x20 (animation 26); flag 0x120 makes the
129/// next scene play the matching arrival (ObjectEffect_RunPendingFlagEvent).
130/// Called by the exit handling in events.c, after it plays SE_128.
133 GameFlag_SetBitFar(0x120);
134}
135
136/// Leaves through an exit of kind 0x30 (animation 25), recorded in flag 0x121.
137/// Called by the exit handling in events.c, after it plays SE_129.
140 GameFlag_SetBitFar(0x121);
141}
142
143/// Plays the leader's arrival animation through an exit: centred on its tile
144/// at the tile's north edge, the leader plays animation for 30 frames, loses
145/// the extra sprite part and walks 8 pixels south. Called by
146/// ObjectEffect_RunPendingFlagEvent; frees the sprite part
147/// ObjectEffect_PrepareContextEffect registered.
149 s32 zero;
150 s32 mask;
152 struct AnimationObject *spriteAnimation = leader->animation;
153 struct AnimationEntry *eff =
154 (struct AnimationEntry *)ResourceMetadata_RegisterFar(spriteAnimation,
155 27);
156
157 zero = 0;
158 mask = 0xfff00000;
159 spriteAnimation->flags = zero;
160 eff->param = 15;
161 leader->x = (leader->x & mask) + 0x80000;
162 leader->z &= mask;
163 ObjectDispatch_SelectAnimationFar(leader, animation);
164 WaitFrames(30);
165 spriteAnimation->count = 1;
166 ResourceMetadata_ClearRecordFar(spriteAnimation->entries[1]);
167 spriteAnimation->entries[1] = (void *)zero;
168 spriteAnimation->flags = 1;
169 leader->acceleration = 0x10000;
170 leader->speedLimit = 0x10000;
171 Object_SetMoveTargetFar(leader, leader->x, leader->y, leader->z + 0x80000);
173}
174
175/// Plays the leader's arrival for the way the last scene was left: through an
176/// exit of kind 0x20 (flag 0x120, animation 24, returns 1) or 0x30 (flag
177/// 0x121, animation 23, returns 2), or falling in from 160 pixels above
178/// (flag 0x122, returns 3). Returns 0 when there is nothing to play. Called by
179/// Field_RunScene as the scene fades in, which waits out the fade itself only
180/// when this returns 0. Clears the flag it acts on.
182 s32 result = 0;
183 s32 flag = 0x120;
184
185 if (GameFlag_TestFar(flag) != 0) {
188 result = 1;
189 } else {
190 flag = 0x121;
191 if (GameFlag_TestFar(flag) != 0) {
194 result = 2;
195 } else {
196 flag = 0x122;
197 if (GameFlag_TestFar(flag) != 0) {
198 s32 leaderId;
199 struct ObjectRuntime *leader;
200
202 leaderId = gGameState.selectedActor;
203 leader = ObjectTable_Get(leaderId);
204 // Flag 0x122 is set by scenes the party drops out of (the Suhalla
205 // storm, a floor giving way in the Tret tree): start 160 pixels up
206 // and let gravity bring the leader down to the ground.
207 leader->y += 0x00a00000;
208 Camera_MoveWithinMapBounds(-1, -1, -1, 0);
209 while (leader->y + leader->velocityY > leader->terrainHeight) {
210 WaitFrames(1);
211 }
213 leader->y = leader->terrainHeight;
216 Camera_FollowActor(leaderId, 1);
217 result = 3;
218 }
219 }
220 }
221 return result;
222}
void * ObjectTable_Get(u32 actorIndex)
Returns the actor in slot index (0 to 65), or NULL.
Definition actors.c:462
void Camera_MoveWithinMapBounds(s32 requestedX, s32 requestedY, s32 requestedZ, s32 glide)
The event command "camera move to": sends the camera to the requested axes, 16.16 pixels (-1 keeps th...
Definition camera.c:47
void Camera_FollowActor(s32 actor, s32 keepPosition)
The event command "camera follow actor": the camera (the event's view centre) follows actor.
Definition camera.c:10
s32 Object_GetScreenPosition(s32 objectId, s32 *position)
Finds where an actor is on screen: position[0] and position[1] receive its x and y in pixels,...
Definition context.c:12
void ObjectEffect_PrepareContextEffect(s32 animation)
Starts the leader's animation for leaving through an exit: centres it on its tile,...
Definition context.c:101
s32 Field_StartEncounterTransition(s32 unused)
Starts the transition into a random battle: plays the scene's battle cue and the encounter sounds,...
Definition context.c:62
void ObjectEffect_EndContextEffect(s32 animation)
Plays the leader's arrival animation through an exit: centred on its tile at the tile's north edge,...
Definition context.c:148
void ObjectEffect_BeginContextEffect25(void)
Leaves through an exit of kind 0x30 (animation 25), recorded in flag 0x121.
Definition context.c:138
void ObjectEffect_BeginContextEffect26(void)
Leaves through an exit of kind 0x20 (animation 26); flag 0x120 makes the next scene play the matching...
Definition context.c:131
UNUSED void ObjectEffect_ReservedNoOp941DC(void)
Definition context.c:51
s32 ObjectEffect_RunPendingFlagEvent(void)
Plays the leader's arrival for the way the last scene was left: through an exit of kind 0x20 (flag 0x...
Definition context.c:181
void ObjectDispatch_SelectAnimationFar(void *raw, s32 no)
s32 ResourceMetadata_RegisterFar(struct AnimationObject *state, s32 id)
void ResourceMetadata_ClearRecordFar(struct AnimationEntry *layer)
void Object_SetMoveTargetFar(struct ObjectRuntime *object, s32 x, s32 y, s32 z)
void FieldObject_WaitForMoveFar(void *fieldObject)
s32 GameFlag_TestFar(s32 flag)
s32 GameFlag_SetBitFar(s32 flag)
void GameFlag_ClearBitFar(s32 flag)
struct AnimationMetadata * Resource_GetMetadataRecordFar(u32 number)
void AudioCommand_PlayFar(s32 id)
#define ACTOR_NO_TARGET
A target coordinate that is not set: the object has no target.
Definition field_obj.h:145
void WaitFrames(s32 frames)
The game's frame loop: runs frames frames and returns.
Definition frame.c:97
struct GameState gGameState
void * gMapWork[]
#define RGB(r, g, b)
A 15-bit colour from its red, green and blue levels (0 to 31 each), as the palette stores it: blue in...
Definition io_reg.h:164
#define PLTT_BACKDROP
Definition io_reg.h:155
#define BG_PLTT_COLOR(palette, color)
Definition io_reg.h:158
#define Io_Write16(v, reg)
Writes a value to a 16-bit register or memory location.
Definition iowrite.h:14
void DisplayTransition_Finish(s32 mode, s32 frames)
Fades the screen out over frames frames, as DisplayTransition_Start fades it in.
Definition transition.c:698
void FieldEvent_WaitFrames(s32 frames)
The event command "wait": waits the requested frames, unless debug fast forward (Event_UpdateDebugSki...
Definition events.c:55
What every part of psynergy_effects.c shares: its includes and the macros and types more than one par...
#define SE_159
Definition songs.h:154
#define SE_288
Definition songs.h:281
#define SE_147
Definition songs.h:140
struct EventWork * gEventWork
One animation layer of a sprite: its animation id, frames, running script and timer.
Definition animspr.h:37
u8 param
SpriteColorMode its pixels are drawn with; 0 when taken.
Definition animspr.h:45
An animated sprite: its image and shadow, scale, rotation, size and up to four animation layers.
Definition animspr.h:108
u8 flags
bits: 1 outlined in ObjectSystemWork's edge and fill colours | 0 shadow drawn (set when created)
Definition animspr.h:128
struct AnimationEntry * entries[4]
its layers; empty ones null
Definition animspr.h:130
u8 count
layers in use, up to the last one present
Definition animspr.h:129
Full event work allocated by Field_RunScene (heap slot 27).
u16 transitionActive
set once the opening transition has started
s16 mode
MODE_WORLD_MAP (3) on the world map.
s16 sceneCue
the battle music chosen for the encounter
Definition game_state.h:55
s32 selectedActor
the leader: its unit and its actor
Definition game_state.h:59
The field map's work: the followed position, screen shake, tile animations, blend script,...
Definition map_scroll.h:27
s32 viewY
the view's top left, 16.16 fixed-point pixels
Definition map_scroll.h:40
One of the 64 field objects in the object pool (heap slot 5): its script, position,...
s32 terrainHeight
16.16 fixed-point pixels
s32 velocityY
16.16 fixed-point pixels per frame
s32 z
16.16 fixed-point pixels
s32 acceleration
Added toward the target each frame, or taken off the speed when there is none, in 16....
void * animation
its sprite, or its list of four, by animationKind
s32 x
16.16 fixed-point pixels
s32 speedLimit
16.16 fixed-point pixels per frame
s32 y
16.16 fixed-point pixels
u8 animationKind
Low four bits: 0 no sprite, 1 one sprite, 2 a group of up to four.
signed int s32
Definition types.h:14
unsigned int u32
Definition types.h:15
signed char s8
Definition types.h:10
unsigned short u16
Definition types.h:13
#define UNUSED
Definition types.h:22
unsigned char u8
Definition types.h:11
#define MODE_WORLD_MAP
the world map
Definition object.h:78