2018-04-13 21:13:48 +02:00
|
|
|
/*
|
2019-08-03 19:43:48 +02:00
|
|
|
* This software is licensed under the terms of the MIT License.
|
2018-04-13 21:13:48 +02:00
|
|
|
* See COPYING for further information.
|
|
|
|
* ---
|
2024-05-16 23:30:41 +02:00
|
|
|
* Copyright (c) 2011-2024, Lukas Weber <laochailan@web.de>.
|
|
|
|
* Copyright (c) 2012-2024, Andrei Alexeyev <akari@taisei-project.org>.
|
2018-04-13 21:13:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "entity.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "renderer/api.h"
|
2018-07-30 09:04:09 +02:00
|
|
|
#include "global.h"
|
2020-04-05 04:51:00 +02:00
|
|
|
#include "dynarray.h"
|
2018-04-13 21:13:48 +02:00
|
|
|
|
2019-01-04 23:59:39 +01:00
|
|
|
typedef struct EntityDrawHook EntityDrawHook;
|
|
|
|
typedef LIST_ANCHOR(EntityDrawHook) EntityDrawHookList;
|
|
|
|
|
|
|
|
struct EntityDrawHook {
|
|
|
|
LIST_INTERFACE(EntityDrawHook);
|
|
|
|
EntityDrawHookCallback callback;
|
|
|
|
void *arg;
|
|
|
|
};
|
|
|
|
|
2018-04-13 21:13:48 +02:00
|
|
|
static struct {
|
2020-04-05 04:51:00 +02:00
|
|
|
DYNAMIC_ARRAY(EntityInterface*) registered;
|
2018-04-13 21:13:48 +02:00
|
|
|
uint32_t total_spawns;
|
2019-01-04 23:59:39 +01:00
|
|
|
|
|
|
|
struct {
|
|
|
|
EntityDrawHookList pre_draw;
|
|
|
|
EntityDrawHookList post_draw;
|
|
|
|
} hooks;
|
2018-04-13 21:13:48 +02:00
|
|
|
} entities;
|
|
|
|
|
2019-01-04 23:59:39 +01:00
|
|
|
static void add_hook(EntityDrawHookList *list, EntityDrawHookCallback cb, void *arg) {
|
2023-01-09 04:19:31 +01:00
|
|
|
auto hook = ALLOC(EntityDrawHook);
|
2019-01-04 23:59:39 +01:00
|
|
|
hook->callback = cb;
|
|
|
|
hook->arg = arg;
|
2020-04-17 09:18:53 +02:00
|
|
|
|
2019-01-04 23:59:39 +01:00
|
|
|
alist_append(list, hook);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_hook(EntityDrawHookList *list, EntityDrawHookCallback cb) {
|
|
|
|
for(EntityDrawHook *hook = list->first; hook; hook = hook->next) {
|
|
|
|
if(hook->callback == cb) {
|
|
|
|
alist_unlink(list, hook);
|
2023-01-09 04:19:31 +01:00
|
|
|
mem_free(hook);
|
2019-01-04 23:59:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void call_hooks(EntityDrawHookList *list, EntityInterface *ent) {
|
|
|
|
for(EntityDrawHook *hook = list->first; hook; hook = hook->next) {
|
|
|
|
hook->callback(ent, hook->arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 21:13:48 +02:00
|
|
|
void ent_init(void) {
|
|
|
|
memset(&entities, 0, sizeof(entities));
|
2020-04-05 04:51:00 +02:00
|
|
|
dynarray_ensure_capacity(&entities.registered, 1024);
|
2018-04-13 21:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ent_shutdown(void) {
|
2020-04-05 04:51:00 +02:00
|
|
|
if(entities.registered.num_elements) {
|
|
|
|
log_fatal_if_debug("%u entities were not properly unregistered, this is a bug!", entities.registered.num_elements);
|
2018-04-13 21:13:48 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 04:51:00 +02:00
|
|
|
dynarray_free_data(&entities.registered);
|
2019-01-04 23:59:39 +01:00
|
|
|
|
|
|
|
assert(entities.hooks.post_draw.first == NULL);
|
|
|
|
assert(entities.hooks.pre_draw.first == NULL);
|
2018-04-13 21:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ent_register(EntityInterface *ent, EntityType type) {
|
|
|
|
assert(type > _ENT_TYPE_ENUM_BEGIN && type < _ENT_TYPE_ENUM_END);
|
|
|
|
ent->type = type;
|
|
|
|
ent->spawn_id = ++entities.total_spawns;
|
2020-04-05 04:51:00 +02:00
|
|
|
ent->index = entities.registered.num_elements;
|
|
|
|
assume(ent->spawn_id > 0);
|
2023-09-23 13:49:26 +02:00
|
|
|
dynarray_append(&entities.registered, ent);
|
2018-04-13 21:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ent_unregister(EntityInterface *ent) {
|
2019-07-16 07:07:30 +02:00
|
|
|
ent->spawn_id = 0;
|
2020-04-05 04:51:00 +02:00
|
|
|
|
|
|
|
// Fast non-order-preserving removal by moving the last element into the removed element's position.
|
|
|
|
|
|
|
|
assert(ent->index < entities.registered.num_elements);
|
|
|
|
assert(dynarray_get(&entities.registered, ent->index) == ent);
|
|
|
|
EntityInterface *sub = entities.registered.data[--entities.registered.num_elements];
|
|
|
|
entities.registered.data[sub->index = ent->index] = sub;
|
2018-04-13 21:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ent_cmp(const void *ptr1, const void *ptr2) {
|
|
|
|
const EntityInterface *ent1 = *(const EntityInterface**)ptr1;
|
|
|
|
const EntityInterface *ent2 = *(const EntityInterface**)ptr2;
|
|
|
|
|
|
|
|
int r = (ent1->draw_layer > ent2->draw_layer) - (ent1->draw_layer < ent2->draw_layer);
|
|
|
|
|
|
|
|
if(r == 0) {
|
|
|
|
// Same layer? Put whatever spawned later on top, then.
|
|
|
|
r = (ent1->spawn_id > ent2->spawn_id) - (ent1->spawn_id < ent2->spawn_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-12-11 10:25:57 +01:00
|
|
|
static inline bool ent_is_drawable(EntityInterface *ent) {
|
|
|
|
return (ent->draw_layer & ~LAYER_LOW_MASK) > LAYER_NODRAW && ent->draw_func;
|
|
|
|
}
|
|
|
|
|
2018-04-13 21:13:48 +02:00
|
|
|
void ent_draw(EntityPredicate predicate) {
|
2019-01-04 23:59:39 +01:00
|
|
|
call_hooks(&entities.hooks.pre_draw, NULL);
|
2020-04-05 04:51:00 +02:00
|
|
|
dynarray_qsort(&entities.registered, ent_cmp);
|
2018-04-13 21:13:48 +02:00
|
|
|
|
|
|
|
if(predicate) {
|
2020-04-05 04:51:00 +02:00
|
|
|
dynarray_foreach(&entities.registered, int i, EntityInterface **pent, {
|
|
|
|
EntityInterface *ent = *pent;
|
|
|
|
ent->index = i;
|
2018-04-13 21:13:48 +02:00
|
|
|
|
2019-12-11 10:25:57 +01:00
|
|
|
if(ent_is_drawable(ent) && predicate(ent)) {
|
2019-01-04 23:59:39 +01:00
|
|
|
call_hooks(&entities.hooks.pre_draw, ent);
|
2018-04-13 21:13:48 +02:00
|
|
|
r_state_push();
|
|
|
|
ent->draw_func(ent);
|
|
|
|
r_state_pop();
|
2019-01-04 23:59:39 +01:00
|
|
|
call_hooks(&entities.hooks.post_draw, ent);
|
2018-04-13 21:13:48 +02:00
|
|
|
}
|
2020-04-05 04:51:00 +02:00
|
|
|
});
|
2018-04-13 21:13:48 +02:00
|
|
|
} else {
|
2020-04-05 04:51:00 +02:00
|
|
|
dynarray_foreach(&entities.registered, int i, EntityInterface **pent, {
|
|
|
|
EntityInterface *ent = *pent;
|
|
|
|
ent->index = i;
|
2018-04-13 21:13:48 +02:00
|
|
|
|
2019-12-11 10:25:57 +01:00
|
|
|
if(ent_is_drawable(ent)) {
|
2019-01-04 23:59:39 +01:00
|
|
|
call_hooks(&entities.hooks.pre_draw, ent);
|
2018-04-13 21:13:48 +02:00
|
|
|
r_state_push();
|
|
|
|
ent->draw_func(ent);
|
|
|
|
r_state_pop();
|
2019-01-04 23:59:39 +01:00
|
|
|
call_hooks(&entities.hooks.post_draw, ent);
|
2018-04-13 21:13:48 +02:00
|
|
|
}
|
2020-04-05 04:51:00 +02:00
|
|
|
});
|
2018-04-13 21:13:48 +02:00
|
|
|
}
|
2019-01-04 23:59:39 +01:00
|
|
|
|
|
|
|
call_hooks(&entities.hooks.post_draw, NULL);
|
2018-04-13 21:13:48 +02:00
|
|
|
}
|
2018-07-30 09:04:09 +02:00
|
|
|
|
|
|
|
DamageResult ent_damage(EntityInterface *ent, const DamageInfo *damage) {
|
|
|
|
if(ent->damage_func == NULL) {
|
|
|
|
return DMG_RESULT_INAPPLICABLE;
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
DamageInfo new_damage;
|
|
|
|
|
|
|
|
if(DAMAGETYPE_IS_PLAYER(damage->type)) {
|
|
|
|
new_damage = *damage;
|
|
|
|
player_damage_hook(&global.plr, ent, &new_damage);
|
|
|
|
damage = &new_damage;
|
|
|
|
}
|
|
|
|
|
2018-08-11 21:13:48 +02:00
|
|
|
DamageResult res = ent->damage_func(ent, damage);
|
|
|
|
|
|
|
|
if(res == DMG_RESULT_OK) {
|
|
|
|
player_register_damage(&global.plr, ent, damage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2018-07-30 09:04:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 04:37:11 +01:00
|
|
|
void ent_area_damage(cmplx origin, float radius, const DamageInfo *damage, EntityAreaDamageCallback callback, void *callback_arg) {
|
2018-07-30 09:04:09 +02:00
|
|
|
for(Enemy *e = global.enemies.first; e; e = e->next) {
|
2019-01-10 00:56:33 +01:00
|
|
|
if(
|
|
|
|
cabs(origin - e->pos) < radius &&
|
|
|
|
ent_damage(&e->ent, damage) == DMG_RESULT_OK &&
|
|
|
|
callback != NULL
|
|
|
|
) {
|
|
|
|
callback(&e->entity_interface, e->pos, callback_arg);
|
2018-07-30 09:04:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 00:56:33 +01:00
|
|
|
if(
|
|
|
|
global.boss != NULL &&
|
|
|
|
cabs(origin - global.boss->pos) < radius &&
|
|
|
|
ent_damage(&global.boss->ent, damage) == DMG_RESULT_OK &&
|
|
|
|
callback != NULL
|
|
|
|
) {
|
|
|
|
callback(&global.boss->entity_interface, global.boss->pos, callback_arg);
|
2018-07-30 09:04:09 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-04 23:59:39 +01:00
|
|
|
|
2019-03-26 16:58:38 +01:00
|
|
|
void ent_area_damage_ellipse(Ellipse ellipse, const DamageInfo *damage, EntityAreaDamageCallback callback, void *callback_arg) {
|
|
|
|
for(Enemy *e = global.enemies.first; e; e = e->next) {
|
|
|
|
if(
|
|
|
|
point_in_ellipse(e->pos, ellipse) &&
|
|
|
|
ent_damage(&e->ent, damage) == DMG_RESULT_OK &&
|
|
|
|
callback != NULL
|
|
|
|
) {
|
|
|
|
callback(&e->entity_interface, e->pos, callback_arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(
|
|
|
|
global.boss != NULL &&
|
|
|
|
point_in_ellipse(global.boss->pos, ellipse) &&
|
|
|
|
ent_damage(&global.boss->ent, damage) == DMG_RESULT_OK &&
|
|
|
|
callback != NULL
|
|
|
|
) {
|
|
|
|
callback(&global.boss->entity_interface, global.boss->pos, callback_arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 23:59:39 +01:00
|
|
|
void ent_hook_pre_draw(EntityDrawHookCallback callback, void *arg) {
|
|
|
|
add_hook(&entities.hooks.pre_draw, callback, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ent_unhook_pre_draw(EntityDrawHookCallback callback) {
|
|
|
|
remove_hook(&entities.hooks.pre_draw, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ent_hook_post_draw(EntityDrawHookCallback callback, void *arg) {
|
|
|
|
add_hook(&entities.hooks.post_draw, callback, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ent_unhook_post_draw(EntityDrawHookCallback callback) {
|
|
|
|
remove_hook(&entities.hooks.post_draw, callback);
|
|
|
|
}
|
2020-09-29 19:11:36 +02:00
|
|
|
|
|
|
|
void _ent_array_compact_Entity(BoxedEntityArray *a) {
|
|
|
|
for(int i = 0; i < a->size; ++i) {
|
|
|
|
while(ENT_UNBOX(a->array[i]) == NULL) {
|
|
|
|
if(i >= --a->size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memmove(a->array + i, a->array + i + 1, (a->size - i) * sizeof(a->array[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 17:35:46 +02:00
|
|
|
|
|
|
|
int _ent_array_add_firstfree_BoxedEntity(BoxedEntity box, BoxedEntityArray *a) {
|
|
|
|
for(int i = 0; i < a->size; ++i) {
|
|
|
|
if(!ENT_UNBOX(a->array[i])) {
|
|
|
|
a->array[i] = box;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _ent_array_add_BoxedEntity(box, a);
|
|
|
|
}
|