1 
2 #ifndef CHARACTER_CONTROLLER_H
3 #define CHARACTER_CONTROLLER_H
4 
5 #include <stdint.h>
6 
7 #include "physics/physics.h"
8 #include "physics/hair.h"
9 #include "physics/ragdoll.h"
10 
11 /*------ Lara's model-------
12              .=.
13             | 14|
14              \ / \
15          / |     | \
16     11  / |   7   | \  8
17        /   |     |   \
18        |    =====    |
19     12 |    =====    | 9
20        |   /  0  \   |
21     13 0  /_______\  0 10
22           |  | |  |
23           |1 | |4 |
24           |  | |  |
25           |__| |__|
26           |  | |  |
27           |2 | |5 |
28           |  | |  |
29           |__| |__|
30        3  |__| |__|  6
31 --------------------------*/
32 
33 #define BODY_PART_BODY_LOW      (0x00000001)            // 0
34 #define BODY_PART_BODY_UPPER    (0x00000002)            // 7
35 #define BODY_PART_HEAD          (0x00000004)            // 14
36 
37 #define BODY_PART_LEFT_HAND_1   (0x00000008)            // 11
38 #define BODY_PART_LEFT_HAND_2   (0x00000010)            // 12
39 #define BODY_PART_LEFT_HAND_3   (0x00000020)            // 13
40 #define BODY_PART_RIGHT_HAND_1  (0x00000040)            // 8
41 #define BODY_PART_RIGHT_HAND_2  (0x00000080)            // 9
42 #define BODY_PART_RIGHT_HAND_3  (0x00000100)            // 10
43 
44 #define BODY_PART_LEFT_LEG_1    (0x00000200)            // 1
45 #define BODY_PART_LEFT_LEG_2    (0x00000400)            // 2
46 #define BODY_PART_LEFT_LEG_3    (0x00000800)            // 3
47 #define BODY_PART_RIGHT_LEG_1   (0x00001000)            // 4
48 #define BODY_PART_RIGHT_LEG_2   (0x00002000)            // 5
49 #define BODY_PART_RIGHT_LEG_3   (0x00004000)            // 6
50 
51 #define BODY_PART_LEGS_1        (BODY_PART_LEFT_LEG_1 | BODY_PART_RIGHT_LEG_1)
52 #define BODY_PART_LEGS_2        (BODY_PART_LEFT_LEG_2 | BODY_PART_RIGHT_LEG_2)
53 #define BODY_PART_LEGS_3        (BODY_PART_LEFT_LEG_3 | BODY_PART_RIGHT_LEG_3)
54 
55 #define BODY_PART_HANDS_1        (BODY_PART_LEFT_HAND_1 | BODY_PART_RIGHT_HAND_1)
56 #define BODY_PART_HANDS_2        (BODY_PART_LEFT_HAND_2 | BODY_PART_RIGHT_HAND_2)
57 #define BODY_PART_HANDS_3        (BODY_PART_LEFT_HAND_3 | BODY_PART_RIGHT_HAND_3)
58 
59 #define BODY_PART_HANDS          (BODY_PART_HANDS_1 | BODY_PART_HANDS_2 | BODY_PART_HANDS_3)
60 #define BODY_PART_LEGS           (BODY_PART_LEGS_1 | BODY_PART_LEGS_2 | BODY_PART_LEGS_3)
61 
62 #define CHARACTER_BOX_HALF_SIZE (128.0)
63 #define CHARACTER_BASE_RADIUS   (128.0)
64 #define CHARACTER_BASE_HEIGHT   (512.0)
65 
66 #define WEAPON_STATE_HIDE                       (0x00)
67 #define WEAPON_STATE_HIDE_TO_READY              (0x01)
68 #define WEAPON_STATE_IDLE                       (0x02)
69 #define WEAPON_STATE_IDLE_TO_FIRE               (0x03)
70 #define WEAPON_STATE_FIRE                       (0x04)
71 #define WEAPON_STATE_FIRE_TO_IDLE               (0x05)
72 #define WEAPON_STATE_IDLE_TO_HIDE               (0x06)
73 
74 /*
75  * ENTITY MOVEMENT TYPES
76  */
77 #define MOVE_STATIC_POS         (0)
78 #define MOVE_KINEMATIC          (1)
79 #define MOVE_ON_FLOOR           (2)
80 #define MOVE_WADE               (3)
81 #define MOVE_QUICKSAND          (4)
82 #define MOVE_ON_WATER           (5)
83 #define MOVE_UNDERWATER         (6)
84 #define MOVE_FREE_FALLING       (7)
85 #define MOVE_CLIMBING           (8)
86 #define MOVE_MONKEYSWING        (9)
87 #define MOVE_WALLS_CLIMB        (10)
88 #define MOVE_DOZY               (11)
89 #define MOVE_FLY                (12)
90 
91 #define CHARACTER_USE_COMPLEX_COLLISION         (1)
92 
93 // Lara's character behavior constants
94 #define DEFAULT_MIN_STEP_UP_HEIGHT              (128.0)                         ///@FIXME: check original
95 #define DEFAULT_MAX_STEP_UP_HEIGHT              (256.0 + 32.0)                  ///@FIXME: check original
96 #define DEFAULT_FALL_DOWN_HEIGHT                (320.0)                         ///@FIXME: check original
97 #define DEFAULT_CLIMB_UP_HEIGHT                 (1920.0)                        ///@FIXME: check original
98 #define DEFAULT_CRITICAL_SLANT_Z_COMPONENT      (0.810)                         ///@FIXME: cos(alpha = 30 deg)
99 #define DEFAULT_CRITICAL_WALL_COMPONENT         (-0.707)                        ///@FIXME: cos(alpha = 45 deg)
100 #define DEFAULT_CHARACTER_SPEED_MULT            (31.5)                          ///@FIXME: magic - not like in original
101 #define DEFAULT_CHARACTER_SLIDE_SPEED_MULT      (75.0)                          ///@FIXME: magic - not like in original
102 #define DEFAULT_CHARACTER_CLIMB_R               (32.0)
103 #define DEFAULT_CHARACTER_WADE_DEPTH            (256.0)
104 // If less than this much of Lara is looking out of the water, she goes from wading to swimming.
105 #define DEFAULT_CHARACTER_SWIM_DEPTH            (100.0) ///@FIXME: Guess
106 
107 // Speed limits
108 
109 #define FREE_FALL_SPEED_1        (2000.0)
110 #define FREE_FALL_SPEED_2        (4500.0)
111 #define FREE_FALL_SPEED_MAXSAFE  (5500.0)
112 #define FREE_FALL_SPEED_CRITICAL (7500.0)
113 #define FREE_FALL_SPEED_MAXIMUM  (7800.0)
114 
115 #define MAX_SPEED_UNDERWATER     (64.0)
116 #define MAX_SPEED_ONWATER        (24.0)
117 #define MAX_SPEED_QUICKSAND      (5.0 )
118 
119 #define ROT_SPEED_UNDERWATER     (1.0)
120 #define ROT_SPEED_ONWATER        (1.5)
121 #define ROT_SPEED_LAND           (2.25)
122 #define ROT_SPEED_FREEFALL       (0.25)
123 #define ROT_SPEED_MONKEYSWING    (1.75)
124 
125 #define INERTIA_SPEED_UNDERWATER (1.0)
126 #define INERTIA_SPEED_ONWATER    (1.5)
127 
128 // flags constants
129 #define CHARACTER_SLIDE_FRONT                   (0x02)
130 #define CHARACTER_SLIDE_BACK                    (0x01)
131 
132 /*
133  * Next step height information
134  */
135 #define CHARACTER_STEP_DOWN_CAN_HANG            (-0x04)                         // enough height to hang here
136 #define CHARACTER_STEP_DOWN_DROP                (-0x03)                         // big height, cannot walk next, drop only
137 #define CHARACTER_STEP_DOWN_BIG                 (-0x02)                         // enough height change, step down is needed
138 #define CHARACTER_STEP_DOWN_LITTLE              (-0x01)                         // too little height change, step down is not needed
139 #define CHARACTER_STEP_HORIZONTAL               (0x00)                          // horizontal plane
140 #define CHARACTER_STEP_UP_LITTLE                (0x01)                          // too little height change, step up is not needed
141 #define CHARACTER_STEP_UP_BIG                   (0x02)                          // enough height change, step up is needed
142 #define CHARACTER_STEP_UP_CLIMB                 (0x03)                          // big height, cannot walk next, climb only
143 #define CHARACTER_STEP_UP_IMPOSSIBLE            (0x04)                          // too big height, no one ways here, or phantom case
144 
145 #define CLIMB_ABSENT                            (0x00)
146 #define CLIMB_HANG_ONLY                         (0x01)
147 #define CLIMB_ALT_HEIGHT                        (0x02)
148 #define CLIMB_FULL_HEIGHT                       (0x03)
149 
150 // CHARACTER PARAMETERS TYPES
151 
152 enum CharParameters
153 {
154     PARAM_HEALTH,
155     PARAM_AIR,
156     PARAM_STAMINA,
157     PARAM_WARMTH,
158     PARAM_HIT_DAMAGE,
159     PARAM_EXTRA1,
160     PARAM_EXTRA2,
161     PARAM_EXTRA3,
162     PARAM_EXTRA4,
163     PARAM_LASTINDEX
164 };
165 
166 // CHARACTER PARAMETERS DEFAULTS
167 
168 #define PARAM_ABSOLUTE_MAX                (0x7FFFFFFF)
169 
170 #define LARA_PARAM_HEALTH_MAX             (1000.0)      // 1000 HP
171 #define LARA_PARAM_AIR_MAX                (3600.0)      // 60 secs of air
172 #define LARA_PARAM_STAMINA_MAX            (120.0)       // 4  secs of sprint
173 #define LARA_PARAM_WARMTH_MAX             (240.0)       // 8  secs of freeze
174 
175 struct engine_container_s;
176 struct entity_s;
177 
178 typedef struct climb_info_s
179 {
180     int8_t                      height_info;
181     int8_t                      can_hang;
182     int8_t                      wall_hit;                                       // 0x00 - none, 0x01 hands only climb, 0x02 - 4 point wall climbing
183     int8_t                      edge_hit;
184 
185     float                       point[3];
186     float                       n[3];
187     float                       t[3];
188     float                       up[3];
189     float                       next_z_space;
190 
191     float                       edge_point[3];
192     float                       edge_normale[3];
193     float                       edge_tan_xy[2];
194     float                       edge_z_ang;
195 
196     struct engine_container_s  *edge_obj;
197 }climb_info_t, *climb_info_p;
198 
199 typedef struct height_info_s
200 {
201     int8_t                                   ceiling_climb;
202     int8_t                                   walls_climb;
203     int16_t                                  walls_climb_dir;
204     struct engine_container_s               *self;
205 
206     struct collision_result_s                floor_hit;
207     struct collision_result_s                ceiling_hit;
208 
209     float                                    transition_level;
210     int16_t                                  water;
211     int16_t                                  quicksand;
212 }height_info_t, *height_info_p;
213 
214 typedef struct character_command_s
215 {
216     int8_t      rot[3];
217     int8_t      move[3];
218     int8_t      flags;
219 
220     uint16_t    roll : 1;
221     uint16_t    jump : 1;
222     uint16_t    crouch : 1;
223     uint16_t    shift : 1;
224     uint16_t    action : 1;
225     uint16_t    ready_weapon : 1;
226     uint16_t    sprint : 1;
227 }character_command_t, *character_command_p;
228 
229 typedef struct character_state_s
230 {
231     uint32_t    floor_collide : 1;
232     uint32_t    ceiling_collide : 1;
233     uint32_t    wall_collide : 1;
234     uint32_t    step_z : 2;     // 0 - none, 1 - dz to step up, 2 - dz to step down;
235     uint32_t    slide : 2;      // 0 - none, 1 - back, 2 - front;
236     uint32_t    uw_current : 1;
237     uint32_t    attack : 1;
238     uint32_t    dead : 2;
239     uint32_t    ragdoll : 1;
240     uint32_t    burn : 1;
241     uint32_t    crouch : 1;
242     uint32_t    sprint : 1;
243     uint32_t    tightrope : 1;
244     uint32_t    can_attack : 2;
245 }character_state_t, *character_state_p;
246 
247 typedef struct character_param_s
248 {
249     float       param[PARAM_LASTINDEX];
250     float       maximum[PARAM_LASTINDEX];
251 }character_param_t, *character_param_p;
252 
253 typedef struct character_stats_s
254 {
255     float       distance;
256     uint32_t    secrets_level;         // Level amount of secrets.
257     uint32_t    secrets_game;          // Overall amount of secrets.
258     uint32_t    ammo_used;
259     uint32_t    hits;
260     uint32_t    kills;
261     uint32_t    medipacks_used;
262     uint32_t    saves_used;
263 }character_stats_t, *character_stats_p;
264 
265 
266 typedef struct character_s
267 {
268     struct entity_s            *ent;                    // actor entity
269     struct character_command_s  cmd;                    // character control commands
270     struct character_state_s    state;                  // character state info (collides, slide, next steps, drops, e.t.c.)
271 
272     struct character_param_s    parameters;
273     struct character_stats_s    statistics;
274 
275     uint32_t                    target_id;
276     int16_t                     cam_follow_center;
277     int8_t                      hair_count;
278     int8_t                      path_dist;                                      // 0 .. n_path - 1
279     struct hair_s             **hairs;
280     struct rd_setup_s          *ragdoll;
281     struct room_box_s          *path[8];
282     struct room_sector_s       *path_target;
283     int16_t                     ai_zone;
284     uint16_t                    ai_zone_type;
285 
286     uint16_t                    bone_head;
287     uint16_t                    bone_torso;
288     uint16_t                    bone_l_hand_start;
289     uint16_t                    bone_l_hand_end;
290     uint16_t                    bone_r_hand_start;
291     uint16_t                    bone_r_hand_end;
292     int16_t                     weapon_id;
293     int16_t                     weapon_state;
294 
295     int                        (*state_func)(struct entity_s *ent, struct ss_animation_s *ss_anim);
296     void                       (*set_key_anim_func)(struct entity_s *ent, struct ss_animation_s *ss_anim, int key_anim);
297     void                       (*set_weapon_model_func)(struct entity_s *ent, int weapon_model, int weapon_state);
298     float                       linear_speed_mult;
299     float                       rotate_speed_mult;
300     float                       min_step_up_height;
301     float                       max_step_up_height;
302     float                       max_climb_height;
303     float                       fall_down_height;
304     float                       critical_slant_z_component;
305     float                       critical_wall_component;
306 
307     float                       climb_r;                // climbing sensor radius
308     float                       forvard_size;           // offset for climbing calculation
309     float                       height;                 // base character height
310     float                       wade_depth;             // water depth that enable wade walk
311     float                       swim_depth;             // depth offset for starting to swim
312 
313     float                       sphere;                 // needs to height calculation
314     float                       climb_sensor;
315 
316     struct height_info_s        height_info;
317     struct climb_info_s         climb;
318 
319     struct entity_s            *traversed_object;
320 }character_t, *character_p;
321 
322 void Character_Create(struct entity_s *ent);
323 void Character_Delete(struct entity_s *ent);
324 void Character_Update(struct entity_s *ent);
325 void Character_UpdatePath(struct entity_s *ent, struct room_sector_s *target);
326 void Character_GoByPathToTarget(struct entity_s *ent, struct entity_s *target);
327 void Character_UpdateAI(struct entity_s *ent);
328 
329 void Character_GetHeightInfo(float pos[3], struct height_info_s *fc, float v_offset = 0.0);
330 int  Character_CheckNextStep(struct entity_s *ent, float offset[3], struct height_info_s *nfc);
331 int  Character_HasStopSlant(struct entity_s *ent, height_info_p next_fc);
332 void Character_GetMiddleHandsPos(const struct entity_s *ent, float pos[3]);
333 void Character_CheckClimbability(struct entity_s *ent, struct climb_info_s *climb, float test_from[3], float test_to[3]);
334 void Character_CheckWallsClimbability(struct entity_s *ent, struct climb_info_s *climb);
335 
336 void Character_UpdateCurrentSpeed(struct entity_s *ent, int zeroVz = 0);
337 void Character_UpdateCurrentHeight(struct entity_s *ent);
338 
339 void Character_SetToJump(struct entity_s *ent, float v_vertical, float v_horizontal);
340 void Character_Lean(struct entity_s *ent, character_command_p cmd, float max_lean);
341 void Character_LookAt(struct entity_s *ent, float target[3]);
342 void Character_ClearLookAt(struct entity_s *ent);
343 void Character_LookAtTarget(struct entity_s *ent, struct entity_s *target);
344 
345 int Character_MoveOnFloor(struct entity_s *ent);
346 int Character_MoveFly(struct entity_s *ent);
347 int Character_FreeFalling(struct entity_s *ent);
348 int Character_MonkeyClimbing(struct entity_s *ent);
349 int Character_WallsClimbing(struct entity_s *ent);
350 int Character_Climbing(struct entity_s *ent);
351 int Character_MoveUnderWater(struct entity_s *ent);
352 int Character_MoveOnWater(struct entity_s *ent);
353 
354 int Character_FindTraverse(struct entity_s *ch);
355 int Sector_AllowTraverse(struct room_sector_s *rs, float floor);
356 int Character_CheckTraverse(struct entity_s *ch, struct entity_s *obj);
357 
358 void Character_ApplyCommands(struct entity_s *ent);
359 void Character_UpdateParams(struct entity_s *ent);
360 
361 float Character_GetParam(struct entity_s *ent, int parameter);
362 int   Character_SetParam(struct entity_s *ent, int parameter, float value);
363 int   Character_ChangeParam(struct entity_s *ent, int parameter, float value);
364 int   Character_SetParamMaximum(struct entity_s *ent, int parameter, float max_value);
365 
366 int Character_IsTargetAccessible(struct entity_s *character, struct entity_s *target);
367 struct entity_s *Character_FindTarget(struct entity_s *ent);
368 void Character_SetTarget(struct entity_s *ent, uint32_t target_id);
369 void Character_ChangeWeapon(struct entity_s *ent, int weapon_model);
370 
371 #endif  // CHARACTER_CONTROLLER_H
372