Jo Engine 2026.11.1
Jo Sega Saturn Engine
Loading...
Searching...
No Matches
entity.h
Go to the documentation of this file.
1/*
2** Jo Sega Saturn Engine
3** Copyright (c) 2012-2026, Johannes Fetz (johannesfetz@gmail.com)
4** All rights reserved.
5**
6** Redistribution and use in source and binary forms, with or without
7** modification, are permitted provided that the following conditions are met:
8** * Redistributions of source code must retain the above copyright
9** notice, this list of conditions and the following disclaimer.
10** * Redistributions in binary form must reproduce the above copyright
11** notice, this list of conditions and the following disclaimer in the
12** documentation and/or other materials provided with the distribution.
13** * Neither the name of the Johannes Fetz nor the
14** names of its contributors may be used to endorse or promote products
15** derived from this software without specific prior written permission.
16**
17** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
44
45#ifndef __JO_ENTITY_H__
46# define __JO_ENTITY_H__
47
48#if defined(JO_COMPILE_WITH_LEVEL_SUPPORT) && defined(JO_COMPILE_WITH_3D_SUPPORT)
49
50# include "level.h"
51
53# define JO_ENTITY_MAX (1024)
55# define JO_ENTITY_MAX_CLASSES (24)
57# define JO_ENTITY_MAX_PENDING (32)
59# define JO_ENTITY_MAX_MODELS (16)
61# define JO_ENTITY_WAKE_DISTANCE (3000)
63# define JO_ENTITY_DRAW_DISTANCE (2000)
65# define JO_ENTITY_FAR_DISTANCE (600)
66
68# define JO_ENTITY_FLAG_DISABLED (0x0001)
71# define JO_ENTITY_FLAG_SOLID (0x0002)
72# define JO_ENTITY_FLAG_HIDDEN (0x0004)
74# define JO_ENTITY_FLAG_DEAD (0x0100)
75# define JO_ENTITY_FLAG_PLAYER_INSIDE (0x0200)
78# define JO_ENTITY_FLAG_DOORWAY (0x0400)
79
80typedef struct __jo_entity jo_entity;
81
83typedef struct
84{
85 const char *name; // Name in the level (ENTI.cls)
86 unsigned int data_size; // Bytes allocated for each entity (entity->data), 0: none
88 void (*spawn)(jo_entity * const entity);
92 void (*update)(jo_entity * const entity, const int elapsed);
96 bool (*action)(jo_entity * const entity, const int action, const int param, jo_entity * const source);
98 void (*death)(jo_entity * const entity, jo_entity * const source);
100 void (*draw)(jo_entity * const entity);
101} jo_entity_class;
102
104struct __jo_entity
105{
106 // (the first 32 bytes: what the collisions read for each entity (jo_entities_is_blocked_at()), 2 lines of the cache)
107 unsigned int flags; // JO_ENTITY_FLAG_xxx
108 jo_pos3D_fixed position; // Foot (world space, Saturn Y down)
109 int radius; // Cylinder: collisions, bullets, zone of a trigger (world units)
110 int height;
111 short box_x; // Box turned with the entity (half sizes, world units) for the collisions
112 short box_z; // of the player and the entities (jo_entities_get_blocking()), 0: the cylinder
113 int yaw; // Degrees
114 const jo_entity_class *cls;
115 const char *name;
116 int health; // 0: can't be damaged (or dead: JO_ENTITY_FLAG_DEAD)
117 jo_3d_mesh *mesh; // Model drawn (JO_NULL: none)
118 jo_3d_mesh *far_mesh; // Model drawn far away (JO_NULL: mesh)
119 jo_pos3D_fixed center; // Bounding sphere of the model (world space)
120 int mesh_radius;
121 int far_distance; // The far model is drawn farther (world units, on the ground)
122 int draw_distance; // The model is not drawn farther (world units, on the ground)
123 int timer; // Free for the class (milliseconds): the entity stays awake while > 0
124 int state; // Free for the class
125 void *data; // Data of the class (jo_entity_class.data_size)
126 unsigned short first_prop;
127 unsigned short prop_count;
128 unsigned short first_link;
129 unsigned short link_count;
130 // (internal) The cells of the entity for its depth bias (jo_entities_draw()): at its foot, then on the 4 sides of a
131 // door (-2: not found yet), found at this position in this generation of the cells (jo_cells_generation())
132 jo_pos3D_fixed cell_position;
133 short cells[5];
134 unsigned short cell_generation;
135} __attribute__((aligned(16))); // (128 bytes: an entity of the table on 8 lines of the cache, see jo_entities_load())
136
143typedef void (*jo_entity_area_damage_callback)(const jo_pos3D_fixed * const center, const int radius,
144 const int damage, jo_entity * const source);
145
149void jo_entity_register_class(const jo_entity_class * const cls);
150
157int jo_entities_load(jo_level * const level);
158
160void jo_entities_free(void);
161
166void jo_entities_update(const jo_pos3D_fixed * const player, const int elapsed);
167
171int jo_entities_draw(void);
172
174int jo_entity_count(void);
175
179jo_entity *jo_entity_get(const int index);
180
184jo_entity *jo_entity_find(const char * const name);
185
194void jo_entity_send(jo_entity * const target, const int action, const int param, jo_entity * const source);
195
201void jo_entity_fire(jo_entity * const source, const int event, const int param);
202
208void jo_entity_damage(jo_entity * const entity, const int damage, jo_entity * const source);
209
216void jo_entities_damage_area(const jo_pos3D_fixed * const center, const int radius, const int damage,
217 jo_entity * const source);
218
222void jo_entities_set_area_damage_callback(const jo_entity_area_damage_callback callback);
223
232jo_entity *jo_entities_ray_hit(const jo_pos3D_fixed * const origin, const jo_pos3D_fixed * const dir,
233 const jo_fixed max_t, jo_fixed * const t, jo_pos3D_fixed * const normal);
234
241bool jo_entities_is_blocked(const jo_fixed x, const jo_fixed z, const int radius);
242
253bool jo_entities_is_blocked_at(const jo_fixed x, const jo_fixed y, const jo_fixed z, const int radius,
254 const int height, const jo_entity * const except);
255
260void jo_entity_set_solid(jo_entity * const entity, const bool solid);
261
269jo_entity *jo_entities_get_blocking(const jo_fixed x, const jo_fixed z, const int radius,
270 const jo_entity * const except);
271
278int jo_entity_get_int(const jo_entity * const entity, const char * const key, const int default_value);
279jo_fixed jo_entity_get_fixed(const jo_entity * const entity, const char * const key, const jo_fixed default_value);
280const char *jo_entity_get_string(const jo_entity * const entity, const char * const key,
281 const char * const default_value);
282
290bool jo_entities_get_zone_request(const char ** const level, const char ** const spawn);
291
293void jo_entities_clear_zone_request(void);
294
299jo_3d_mesh *jo_entity_load_model(const char * const filename);
300
305jo_3d_model *jo_entity_get_model(const char * const filename);
306
312void jo_entity_set_mesh(jo_entity * const entity, jo_3d_mesh * const mesh);
313
319int jo_entity_view_distance(const jo_entity * const entity);
320
324void jo_entities_set_lod_scale(const int scale);
325
334int jo_entity_turn_to(jo_entity * const entity, const jo_fixed x, const jo_fixed z, const int max_step);
335
336/*
337** Actors: entities with an animated model (a skeleton, see jo_3d_model_draw_pose()). The class "actor" of the engine plays
338** the animation of its property animation (a character who waits, a flag); the classes of the game (enemies) start their
339** data with a jo_actor and call jo_actor_spawn(), jo_actor_update() and jo_actor_draw()
340*/
341
347typedef void (*jo_actor_bone_callback)(jo_entity * const entity, const int bone, const jo_fixed (* const matrix)[3]);
348
350typedef struct
351{
352 jo_3d_model *model;
353 jo_3d_model *far_model; // (property far_model, with a skeleton too) JO_NULL: model
354 short animation; // Index in the model, -1: none
355 short previous; // Animation before the change (blended), -1: none
356 int time; // Milliseconds from the start of the animation
357 int previous_time;
358 int blend; // Milliseconds left of the blend with the previous animation
359 int blend_duration;
360 jo_actor_bone_callback bone_callback; // JO_NULL: none
361} jo_actor;
362
367void jo_actor_spawn(jo_entity * const entity);
368
373void jo_actor_update(jo_entity * const entity, const int elapsed);
374
378void jo_actor_draw(jo_entity * const entity);
379
386bool jo_actor_play(jo_entity * const entity, const char * const name, const int blend);
387
394bool jo_actor_restart(jo_entity * const entity, const char * const name, const int blend);
395
400bool jo_actor_is_done(const jo_entity * const entity);
401
407bool jo_actor_is_playing(const jo_entity * const entity, const char * const name);
408
409#endif /* !JO_COMPILE_WITH_LEVEL_SUPPORT && JO_COMPILE_WITH_3D_SUPPORT */
410
411#endif /* !__JO_ENTITY_H__ */
412
413/*
414** END OF FILE
415*/
Mesh structure.
Definition 3d.h:216
Levels (.JOL files): the data of a level of a game (terrain, models, plants, decals....
3D model loaded from a JOM file
Definition model.h:195
int jo_fixed
Fixed point Q16.16 number.
Definition types.h:48
3D position using fixed numbers
Definition types.h:92