1 
2 #ifndef ENTITY_H
3 #define ENTITY_H
4 
5 #include <stdint.h>
6 #include <stdlib.h>
7 
8 #include "core/base_types.h"
9 
10 struct room_sector_s;
11 struct obb_s;
12 struct character_s;
13 struct ss_animation_s;
14 struct ss_bone_frame_s;
15 struct physics_data_s;
16 struct inventory_node_s;
17 
18 #define ENTITY_ID_NONE                              (0xFFFFFFFF)
19 
20 #define ENTITY_STATE_ENABLED                        (0x0001)    // Entity is enabled.
21 #define ENTITY_STATE_ACTIVE                         (0x0002)    // Entity is animated.
22 #define ENTITY_STATE_VISIBLE                        (0x0004)    // Entity is visible.
23 #define ENTITY_STATE_COLLIDABLE                     (0x0008)    // Collisions enabled.
24 #define ENTITY_STATE_NO_CAM_TARGETABLE              (0x0010)    // Disallow targeting by the camera.
25 #define ENTITY_STATE_DELETED                        (0x1000)    // Will be deleted on update.
26 
27 #define ENTITY_TYPE_GENERIC                         (0x0000)    // Just an animating.
28 #define ENTITY_TYPE_INTERACTIVE                     (0x0001)    // Can respond to other entity's commands.
29 #define ENTITY_TYPE_TRIGGER_ACTIVATOR               (0x0002)    // Can activate triggers.
30 #define ENTITY_TYPE_HEAVYTRIGGER_ACTIVATOR          (0x0004)    // Can activate heavy triggers.
31 #define ENTITY_TYPE_PICKABLE                        (0x0008)    // Can be picked up.
32 #define ENTITY_TYPE_TRAVERSE                        (0x0010)    // Can be pushed/pulled.
33 #define ENTITY_TYPE_TRAVERSE_FLOOR                  (0x0020)    // Can be walked upon.
34 #define ENTITY_TYPE_DYNAMIC                         (0x0040)    // Acts as a physical dynamic object.
35 #define ENTITY_TYPE_ACTOR                           (0x0080)    // Is actor.
36 
37 #define ENTITY_TYPE_SPAWNED                         (0x8000)    // Was spawned.
38 
39 /*
40  * SURFACE MOVEMENT DIRECTIONS
41  */
42 #define ENT_STAY 0x00000000
43 #define ENT_MOVE_FORWARD 0x00000001
44 #define ENT_MOVE_BACKWARD 0x00000002
45 #define ENT_MOVE_LEFT 0x00000004
46 #define ENT_MOVE_RIGHT 0x00000008
47 #define ENT_MOVE_JUMP 0x00000010
48 #define ENT_MOVE_CROUCH 0x00000020
49 
50 //   ====== ANIMATION COMMANDS ======
51 #define TR_ANIMCOMMAND_SETPOSITION  1
52 #define TR_ANIMCOMMAND_JUMPDISTANCE 2
53 #define TR_ANIMCOMMAND_EMPTYHANDS   3
54 #define TR_ANIMCOMMAND_KILL         4
55 #define TR_ANIMCOMMAND_PLAYSOUND    5
56 #define TR_ANIMCOMMAND_PLAYEFFECT   6
57 #define TR_ANIMCOMMAND_INTERACT     7
58 
59 //   ====== ANIMATION EFFECTS FLAGS ======
60 #define TR_ANIMCOMMAND_CONDITION_LAND  0x4000
61 #define TR_ANIMCOMMAND_CONDITION_WATER 0X8000
62 
63 //   ====== ANIMATION EFFECTS / FLIPEFFECTS ======
64 #define TR_EFFECT_CHANGEDIRECTION       0
65 #define TR_EFFECT_SHAKESCREEN           1
66 #define TR_EFFECT_PLAYFLOODSOUND        2
67 #define TR_EFFECT_BUBBLE                3
68 #define TR_EFFECT_ENDLEVEL              4
69 #define TR_EFFECT_ACTIVATECAMERA        5
70 #define TR_EFFECT_ACTIVATEKEY           6
71 #define TR_EFFECT_ENABLEEARTHQUAKES     7
72 #define TR_EFFECT_GETCROWBAR            8
73 #define TR_EFFECT_CURTAINFX             9  // Effect 9 is empty in TR4.
74 #define TR_EFFECT_PLAYSOUND_TIMERFIELD  10
75 #define TR_EFFECT_PLAYEXPLOSIONSOUND    11
76 #define TR_EFFECT_DISABLEGUNS           12
77 #define TR_EFFECT_ENABLEGUNS            13
78 #define TR_EFFECT_GETRIGHTGUN           14
79 #define TR_EFFECT_GETLEFTGUN            15
80 #define TR_EFFECT_FIRERIGHTGUN          16
81 #define TR_EFFECT_FIRELEFTGUN           17
82 #define TR_EFFECT_MESHSWAP1             18
83 #define TR_EFFECT_MESHSWAP2             19
84 #define TR_EFFECT_MESHSWAP3             20
85 #define TR_EFFECT_INV_ON                21 // Effect 21 is unknown at offset 4376F0.
86 #define TR_EFFECT_INV_OFF               22 // Effect 22 is unknown at offset 437700.
87 #define TR_EFFECT_HIDEOBJECT            23
88 #define TR_EFFECT_SHOWOBJECT            24
89 #define TR_EFFECT_STATUEFX              25 // Effect 25 is empty in TR4.
90 #define TR_EFFECT_RESETHAIR             26
91 #define TR_EFFECT_BOILERFX              27 // Effect 27 is empty in TR4.
92 #define TR_EFFECT_SETFOGCOLOUR          28
93 #define TR_EFFECT_GHOSTTRAP             29 // Effect 29 is unknown at offset 4372F0
94 #define TR_EFFECT_LARALOCATION          30
95 #define TR_EFFECT_CLEARSCARABS          31
96 #define TR_EFFECT_PLAYSTEPSOUND         32 // Also called FOOTPRINT_FX in TR4 source code.
97 
98 // Effects 33 - 42 are assigned to FLIP_MAP0-FLIP_MAP9 in TR4 source code,
99 // but are empty in TR4 binaries.
100 #define TR_EFFECT_GETWATERSKIN          43
101 #define TR_EFFECT_REMOVEWATERSKIN       44
102 #define TR_EFFECT_LARALOCATIONPAD       45
103 #define TR_EFFECT_KILLALLENEMIES        46
104 
105 #define ENTITY_CALLBACK_NONE                        (0x00000000)
106 #define ENTITY_CALLBACK_ACTIVATE                    (0x00000001)
107 #define ENTITY_CALLBACK_DEACTIVATE                  (0x00000002)
108 #define ENTITY_CALLBACK_COLLISION                   (0x00000004)
109 #define ENTITY_CALLBACK_STAND                       (0x00000008)
110 #define ENTITY_CALLBACK_HIT                         (0x00000010)
111 #define ENTITY_CALLBACK_ATTACK                      (0x00000020)
112 #define ENTITY_CALLBACK_SHOOT                       (0x00000040)
113 
114 #define ENTITY_SUBSTANCE_NONE                     0
115 #define ENTITY_SUBSTANCE_WATER_SHALLOW            1
116 #define ENTITY_SUBSTANCE_WATER_WADE               2
117 #define ENTITY_SUBSTANCE_WATER_SWIM               3
118 #define ENTITY_SUBSTANCE_QUICKSAND_SHALLOW        4
119 #define ENTITY_SUBSTANCE_QUICKSAND_CONSUMED       5
120 
121 #define ENTITY_TLAYOUT_MASK     0x1F    // Activation mask
122 #define ENTITY_TLAYOUT_EVENT    0x20    // Last trigger event
123 #define ENTITY_TLAYOUT_LOCK     0x40    // Activity lock
124 #define ENTITY_TLAYOUT_SSTATUS  0x80    // Sector status
125 
126 
127 typedef void (*collision_callback_t)(struct entity_s *ent, struct collision_node_s *cn);
128 
129 // Specific in-game entity structure.
130 
131 typedef struct activation_point_s
132 {
133     float                               offset[4];       // where we can activate object (dx, dy, dz, r)
134     float                               direction[4];    // direction_xyz, cos(lim)
135 }activation_point_t, *activation_point_p;
136 
137 
138 typedef struct entity_s
139 {
140     uint32_t                            id;                     // Unique entity ID
141     int32_t                             OCB;                    // Object code bit (since TR4)
142 
143     uint32_t                            trigger_layout : 8;     // Mask + once + event + sector status flags
144     uint32_t                            dir_flag : 8;           // (move direction)
145     uint32_t                            move_type : 4;          // on floor / free fall / swim ....
146     uint32_t                            no_fix_all : 1;         // only setPos and anim command can ignore that
147     uint32_t                            no_move : 1;
148     uint32_t                            no_anim_pos_autocorrection : 1;
149 
150     float                               timer;              // Set by "timer" trigger field
151     uint32_t                            callback_flags;     // information about scripts callbacks
152     uint16_t                            type_flags;
153     uint16_t                            state_flags;
154 
155     float                               linear_speed;
156     float                               anim_linear_speed;  // current linear speed from animation info
157     float                               speed[3];           // speed of the entity XYZ
158 
159     uint32_t                            no_fix_skeletal_parts;
160     struct ss_bone_frame_s             *bf;                 // current boneframe with full frame information
161     struct physics_data_s              *physics;
162     struct engine_transform_s           transform;
163 
164     struct obb_s                       *obb;                // oriented bounding box
165     struct engine_container_s          *self;
166 
167     struct activation_point_s          *activation_point;
168     struct inventory_node_s            *inventory;
169     struct character_s                 *character;
170 }entity_t, *entity_p;
171 
172 
173 entity_p Entity_Create();
174 void Entity_InitActivationPoint(entity_p entity);
175 void Entity_Delete(entity_p entity);
176 void Entity_Enable(entity_p ent);
177 void Entity_Disable(entity_p ent);
178 void Entity_EnableCollision(entity_p ent);
179 void Entity_DisableCollision(entity_p ent);
180 void Entity_UpdateRoomPos(entity_p ent);
181 void Entity_MoveToRoom(entity_p entity, struct room_s *new_room);
182 
183 void Entity_Frame(entity_p entity, float time);  // process frame + trying to change state
184 
185 void Entity_RebuildBV(entity_p ent);
186 void Entity_UpdateTransform(entity_p entity);
187 int  Entity_CanTrigger(entity_p activator, entity_p trigger);
188 void Entity_RotateToTriggerZ(entity_p activator, entity_p trigger);
189 void Entity_RotateToTrigger(entity_p activator, entity_p trigger, int bone_to);
190 void Entity_CheckActivators(struct entity_s *ent);
191 int  Entity_Activate(struct entity_s *entity_object, struct entity_s *entity_activator, uint16_t trigger_mask, uint16_t trigger_op, uint16_t trigger_lock, uint16_t trigger_timer);
192 int  Entity_Deactivate(struct entity_s *entity_object, struct entity_s *entity_activator);
193 
194 int  Entity_GetSubstanceState(entity_p entity);
195 
196 void Entity_UpdateRigidBody(struct entity_s *ent, int force);
197 void Entity_GhostUpdate(struct entity_s *ent);
198 
199 int  Entity_GetPenetrationFixVector(struct entity_s *ent, collision_callback_t callback, float reaction[3], float ent_move[3], int16_t filter);
200 int  Entity_CheckNextPenetration(struct entity_s *ent, collision_callback_t callback, float move[3], float reaction[3], int16_t filter);
201 void Entity_FixPenetrations(struct entity_s *ent, collision_callback_t callback, float move[3], int16_t filter);
202 
203 void Entity_CheckCollisionCallbacks(entity_p ent);
204 void Entity_DoAnimCommands(entity_p entity, struct ss_animation_s *ss_anim);
205 void Entity_DoFlipEffect(entity_p entity, uint16_t effect_id, int16_t param);
206 void Entity_ProcessSector(entity_p ent);
207 void Entity_SetAnimation(entity_p entity, int anim_type, int animation, int frame, float new_transform[16] = NULL);
208 void Entity_MoveToSink(entity_p entity, struct static_camera_sink_s *sink);
209 void Entity_MoveForward(entity_p ent, float dist);
210 void Entity_MoveStrafe(entity_p ent, float dist);
211 void Entity_MoveVertical(entity_p ent, float dist);
212 
213 #endif
214