Alchemy
Loading...
Searching...
No Matches

vault_range.h

Go to the documentation of this file.
1/// @file
2/// Vault, before and after the thieves are caught: villagers who walk about
3/// stop when someone is close in front of them.
4#include "types.h"
5#include "field_obj.h"
6
7/// Stops the actor (paused, mode 1) when the target is nearer than range pixels
8/// and lies ahead of it, within a sixteenth of a turn either side of its
9/// heading; force stops it regardless. Returns 1 when it stopped. A target out
10/// of range sets it walking again (mode 2).
11s32 Vault_StopIfFacingNear(struct FieldActor *actor, struct FieldActor *target,
12 s32 range, s32 force) {
13 s32 result = 0;
14 const u8 *targetPosition =
15 (const u8 *)target + offsetof(struct FieldActor, x);
16 const u8 *actorPosition = (const u8 *)actor + offsetof(struct FieldActor, x);
17 if (FixedPoint_Distance(targetPosition, actorPosition) < range ||
18 force != 0) {
19 u32 angle = (u16)ArcTan2(target->z.fixed - actor->z.fixed,
20 *(const s32 *)targetPosition -
21 *(const s32 *)actorPosition);
22 u32 left = (angle - FACING_STEP) & 0xf000;
23 u32 right = (angle + FACING_STEP) & 0xf000;
24 u32 forward = angle & 0xf000;
25 u32 facing = actor->facing & 0xf000;
26 if (forward == facing || right == facing || left == facing || force != 0) {
27 actor->paused = 1;
28 Object_SetMode(actor, 1);
29 result = 1;
30 }
31 } else {
32 actor->paused = 0;
33 Object_SetMode(actor, 2);
34 }
35 return result;
36}
void Object_SetMode(struct FieldActor *object, s32 animation)
Field objects and actors, and the scene transition styles.
@ FACING_STEP
Definition field_scene.h:36
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
s32 Vault_StopIfFacingNear(struct FieldActor *actor, struct FieldActor *target, s32 range, s32 force)
Stops the actor (paused, mode 1) when the target is nearer than range pixels and lies ahead of it,...
Definition vault_range.h:11
#define offsetof(type, member)
Definition stddef.h:19
A field actor: a person or thing on the field with a position, motion toward a target,...
Definition field_obj.h:30
u8 paused
nonzero skips its script and motion, not its update
Definition field_obj.h:93
union FieldCoordinate z
16.16 fixed-point pixels
Definition field_obj.h:37
u16 facing
1/65536 turns
Definition field_obj.h:34
The integer types, TRUE, UNUSED and the fixed-address macros.
signed int s32
Definition types.h:14
unsigned int u32
Definition types.h:15
unsigned short u16
Definition types.h:13
unsigned char u8
Definition types.h:11
s32 fixed
16.16 fixed-point pixels