Alchemy
Loading...
Searching...
No Matches

actor_motion.c

Go to the documentation of this file.
1/// @file
2/// Actor scripts: moving, turning and the built-in actions.
3/// Part of psynergy_effects.c, compiled only through it.
4
6
7/// Script step: clears an object's sprite flags and its collision flags.
10 ((struct ScriptObjectRuntime *)obj)->flags59 = 0;
11 return 0;
12}
13
14/// Script step of action 5: follows the linked object. While at least action
15/// pixels away the object walks toward it (a step of 16 / action of the
16/// distance, in 16-pixel units); closer, it stands.
18 s32 step;
19 struct ObjectRuntime *target;
20 s32 deltaX;
21 s32 deltaZ;
22 s32 pixelsX;
23 s32 pixelsZ;
24 s32 newX;
25 s32 newZ;
26 s32 distance;
27
28 target = object->linkedObject;
29 if (target != 0) {
30 deltaX = target->x - object->x;
31 if (deltaX < 0)
32 deltaX += 0xffff;
33 pixelsX = deltaX >> 16;
34 deltaZ = target->z - object->z;
35 if (deltaZ < 0)
36 deltaZ += 0xffff;
37 pixelsZ = deltaZ >> 16;
38 distance = Iwram_Sqrt(pixelsX * pixelsX + pixelsZ * pixelsZ);
39 step = object->action;
40 if (distance >= step) {
41 // Offsets can be negative. Shift their word representation before
42 // reading it as a signed displacement, rather than shifting a negative.
43 newX = object->x + (s32)((u32)pixelsX << 20) / step;
44 newZ = object->z + (s32)((u32)pixelsZ << 20) / step;
45 Object_SetMoveTargetFar(object, newX, object->y, newZ);
47 } else {
49 }
50 }
51 return 1;
52}
53
54/// Script step of action 6: turns the object toward its linked object, at most
55/// 1/16 turn a frame.
57 s16 turnStep;
58 u16 currentAngle;
59 s32 targetAngle;
60 struct ObjectRuntime *target;
61
62 target = object->linkedObject;
63 if (target != NULL) {
64 object->actionFlags &= 0xfe;
65 targetAngle = (u16)ArcTan2(target->z - object->z, target->x - object->x);
66 currentAngle = object->angle;
67 turnStep = targetAngle - currentAngle;
68 if (turnStep != 0) {
69 if (turnStep > 0x1000)
70 turnStep = 0x1000;
71 if (turnStep < -0x1000)
72 turnStep = -0x1000;
73 object->angle = (currentAngle + turnStep);
74 }
75 }
76 return 1;
77}
78
79/// Starts an actor's action script: 1 to 4 the fixed action scripts, 5 follow
80/// the linked object, 6 face the leader, 7 reset. Any other value is itself
81/// the script to run.
83 switch ((action - 1)) {
84 case 0:
86 break;
87 case 1:
89 break;
90 case 2:
92 break;
93 case 3:
95 break;
96 case 4:
98 break;
99 case 5:
100 object->linkedObject =
103 break;
104 case 6:
106 break;
107 default:
108 break;
109 }
111 (const s32 *)action);
112}
113
114/// Finds the nearest animated object to self within 40 pixels and 48 pixels of
115/// height, ignoring objects more than 3/16 turn to either side once they are
116/// over 23 pixels away. Returns it if it shows sprite, otherwise NULL.
118 s32 sprite) {
119 struct ObjectRuntime *entry;
120 struct ObjectRuntime *found;
121 s32 slotIndex;
122 s32 nearestDistance;
123 s32 heightDifference;
124 s32 pixelX;
125 s32 pixelZ;
126 s32 distance;
127 s32 angle;
128 s32 angleDifference;
129
130 found = NULL;
131 nearestDistance = 40;
132 entry = gObjectSlots;
133 for (slotIndex = 0; slotIndex < 64; slotIndex++, entry++) {
134 if (entry->script == NULL)
135 continue;
136 if (entry == self)
137 continue;
138 if (entry->animationKind != 1)
139 continue;
140 heightDifference = entry->y - self->y;
141 if (heightDifference >= 0) {
142 if (heightDifference > 0x2fffff)
143 continue;
144 } else {
145 if (self->y - entry->y > 0x2fffff)
146 continue;
147 }
148 pixelX = (entry->x - self->x) / 0x10000;
149 pixelZ = (entry->z - self->z) / 0x10000;
150 distance = Iwram_Sqrt(pixelX * pixelX + pixelZ * pixelZ);
151 if (distance >= nearestDistance)
152 continue;
153 angle = (u16)ArcTan2(entry->z - self->z, entry->x - self->x);
154 if (distance > 23) {
155 angleDifference = (s16)(angle - self->angle);
156 if (angleDifference < -0x2fff)
157 continue;
158 if (angleDifference > 0x2fff)
159 continue;
160 }
161 found = entry;
162 nearestDistance = distance;
163 }
164 if (found == NULL)
165 return NULL;
166 if (((struct AnimationObject *)found->animation)->entries[0]->animId !=
167 sprite)
168 return NULL;
169 return found;
170}
struct FieldActor * Object_GetById(s32 actor)
Returns an actor, or NULL if it does not exist.
struct ObjectRuntime * Object_FindNearestFacingTarget(struct ObjectRuntime *self, s32 sprite)
Finds the nearest animated object to self within 40 pixels and 48 pixels of height,...
s32 Object_ClearChildAndCollisionFlags(void *obj)
Script step: clears an object's sprite flags and its collision flags.
Definition actor_motion.c:8
void ObjectMotion_SetActionCallback(struct ObjectRuntime *object, s32 action)
Starts an actor's action script: 1 to 4 the fixed action scripts, 5 follow the linked object,...
s32 ObjectMotion_TurnTowardLinkedTarget(struct ObjectRuntime *object)
Script step of action 6: turns the object toward its linked object, at most 1/16 turn a frame.
s32 ObjectMotion_MoveTowardTarget(struct ObjectRuntime *object)
Script step of action 5: follows the linked object.
void ObjectDispatch_SetSingleChildFlagsFar(struct DispatchObject *obj, u32 flags)
void ObjectDispatch_SelectAnimationFar(void *raw, s32 no)
void ObjectDispatch_InitializeFar(struct DispatchObject *obj, const s32 *script)
void Object_SetMoveTargetFar(struct ObjectRuntime *object, s32 x, s32 y, s32 z)
const u8 gObjectMotion_ActionKind4Script[]
const u8 gObjectMotion_ActionKind1Script[]
const u8 gObjectMotion_MoveTowardTargetScript[]
const u8 gObjectMotion_TurnTowardLinkedScript[]
const u8 gObjectMotion_ResetActionScript[]
const u8 gObjectMotion_ActionKind2Script[]
const u8 gObjectMotion_ActionKind3Script[]
struct GameState gGameState
static s32 Iwram_Sqrt(s32 value)
Definition iwram_call.h:63
s32 ArcTan2(s32 x, s32 y)
Returns the angle of the vector (x, y), in 1/65536 turns, measured from the y axis: 0 along +y,...
Definition math.c:36
What every part of psynergy_effects.c shares: its includes and the macros and types more than one par...
#define NULL
Definition bilibin.h:6
An animated sprite: its image and shadow, scale, rotation, size and up to four animation layers.
Definition animspr.h:108
A field object as the object dispatcher's far calls take it.
s32 selectedActor
the leader: its unit and its actor
Definition game_state.h:59
One of the 64 field objects in the object pool (heap slot 5): its script, position,...
u16 angle
1/65536 turns
s32 z
16.16 fixed-point pixels
s32 * script
the object script it runs; null when the object is free
void * animation
its sprite, or its list of four, by animationKind
s32 x
16.16 fixed-point pixels
s16 step
the script cursor, in words
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.
struct ObjectRuntime * linkedObject
The object its action script follows or faces.
s16 action
The parameter its action script reads (a follow distance or an angle), or the player's frames to its ...
A field object as its object script commands see it: the script and its place, position,...
u8 flags59
FieldActor.collisionFlags.
signed int s32
Definition types.h:14
unsigned int u32
Definition types.h:15
signed short s16
Definition types.h:12
unsigned short u16
Definition types.h:13
struct ScriptMotionObject * gObjectSlots