eco2d/code/game/source/game.c

200 lines
5.4 KiB
C
Raw Normal View History

2021-01-10 16:42:01 +00:00
#include "game.h"
2021-05-06 15:30:38 +00:00
#include "zpl.h"
2021-01-11 20:11:03 +00:00
#include "platform.h"
2021-05-04 19:22:55 +00:00
#include "world/world.h"
2021-05-05 09:25:05 +00:00
#include "packet.h"
2021-05-04 19:22:55 +00:00
#include "signal_handling.h"
#include "network.h"
2021-05-08 09:05:15 +00:00
#include "entity.h"
2021-05-06 15:30:38 +00:00
#include "world_view.h"
2021-05-05 09:25:05 +00:00
#include "entity_view.h"
2021-05-05 10:14:52 +00:00
#include "camera.h"
2021-05-13 13:44:08 +00:00
#include "profiler.h"
2021-01-10 16:42:01 +00:00
2021-05-04 19:22:55 +00:00
#include "flecs/flecs.h"
#include "flecs/flecs_dash.h"
#include "flecs/flecs_systems_civetweb.h"
#include "flecs/flecs_os_api_stdcpp.h"
2021-05-08 09:05:15 +00:00
#include "modules/general.h"
#include "modules/physics.h"
#include "modules/controllers.h"
2021-05-06 15:30:38 +00:00
#include "packets/pkt_00_init.h"
2021-05-05 13:14:02 +00:00
#include "packets/pkt_01_welcome.h"
2021-05-07 14:43:54 +00:00
#include "packets/pkt_send_keystate.h"
2021-05-05 13:14:02 +00:00
2021-05-12 14:42:22 +00:00
static uint8_t game_mode;
2021-05-06 15:30:38 +00:00
static world_view *world_viewers;
static world_view *active_viewer;
2021-05-04 19:22:55 +00:00
static WORLD_PKT_READER(pkt_reader) {
pkt_header header = {0};
uint32_t ok = pkt_header_decode(&header, data, datalen);
2021-05-06 15:30:38 +00:00
header.udata = udata;
2021-05-05 13:14:02 +00:00
2021-05-04 19:22:55 +00:00
if (ok && header.ok) {
return pkt_handlers[header.id].handler(&header) >= 0;
} else {
zpl_printf("[warn] unknown packet id %d (header %d data %d)\n", header.id, ok, header.ok);
}
return -1;
}
static WORLD_PKT_WRITER(sp_pkt_writer) {
2021-05-12 14:42:22 +00:00
(void)udata;
2021-05-07 14:43:54 +00:00
return world_read(pkt->data, pkt->datalen, (void*)game_world_view_get_active()->owner_id);
2021-05-04 19:22:55 +00:00
}
static WORLD_PKT_WRITER(mp_pkt_writer) {
2021-05-12 14:42:22 +00:00
(void)udata;
2021-05-04 19:22:55 +00:00
if (pkt->is_reliable) {
return network_msg_send(pkt->data, pkt->datalen);
}
else {
return network_msg_send_unreliable(pkt->data, pkt->datalen);
}
}
2021-05-06 15:30:38 +00:00
void world_viewers_init(uint32_t num_viewers) {
zpl_buffer_init(world_viewers, zpl_heap(), num_viewers);
for (uint32_t i = 0; i < num_viewers; i++) {
2021-05-07 20:48:15 +00:00
zpl_buffer_append(world_viewers, world_view_create(i));
2021-05-06 15:30:38 +00:00
}
}
void world_viewers_destroy() {
for (uint32_t i = 0; i < zpl_buffer_count(world_viewers); i++) {
world_view_destroy(&world_viewers[i]);
}
zpl_buffer_free(world_viewers);
}
world_view *game_world_view_get(uint16_t idx) {
return &world_viewers[idx];
}
world_view *game_world_view_get_active(void) {
return active_viewer;
}
2021-05-10 11:27:01 +00:00
void game_world_view_cycle_active(int8_t dir) {
2021-05-07 20:48:15 +00:00
uint16_t idx = (uint16_t)(active_viewer - world_viewers);
game_world_view_set_active_by_idx((idx+dir)%zpl_buffer_count(world_viewers));
}
2021-05-07 11:47:45 +00:00
void game_world_view_set_active_by_idx(uint16_t idx) {
ZPL_ASSERT(idx >= 0 && idx < zpl_buffer_count(world_viewers));
game_world_view_set_active(&world_viewers[idx]);
}
2021-05-09 22:40:25 +00:00
void game_world_view_active_entity_map(void (*map_proc)(uint64_t key, entity_view * value)) {
entity_view_map(&active_viewer->entities, map_proc);
}
2021-05-07 11:47:45 +00:00
void game_world_view_set_active(world_view *view) {
active_viewer = view;
camera_set_follow(view->owner_id);
}
2021-05-06 15:30:38 +00:00
void flecs_dash_init() {
ECS_IMPORT(world_ecs(), FlecsDash);
ECS_IMPORT(world_ecs(), FlecsSystemsCivetweb);
ecs_set(world_ecs(), 0, EcsDashServer, {.port = 27001});
}
2021-05-12 16:28:39 +00:00
void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) {
2021-05-12 14:42:22 +00:00
game_mode = play_mode;
2021-01-11 20:11:03 +00:00
platform_init();
2021-05-06 15:30:38 +00:00
world_viewers_init(num_viewers);
active_viewer = &world_viewers[0];
2021-05-05 10:14:52 +00:00
camera_reset();
2021-05-05 13:14:02 +00:00
2021-05-12 14:42:22 +00:00
if (game_mode == GAMEKIND_CLIENT) {
2021-05-06 15:30:38 +00:00
world_setup_pkt_handlers(pkt_reader, mp_pkt_writer);
2021-05-04 19:22:55 +00:00
network_init();
network_client_connect("127.0.0.1", 27000);
} else {
stdcpp_set_os_api();
2021-05-06 15:30:38 +00:00
world_setup_pkt_handlers(pkt_reader, sp_pkt_writer);
2021-05-12 16:28:39 +00:00
world_init(seed, chunk_size, chunk_amount);
if (is_dash_enabled) flecs_dash_init();
2021-05-10 14:41:13 +00:00
ecs_set_target_fps(world_ecs(), 60);
2021-05-06 15:30:38 +00:00
}
for (uint32_t i = 0; i < num_viewers; i++) {
pkt_00_init_send(i);
2021-05-04 19:22:55 +00:00
}
2021-01-10 16:42:01 +00:00
}
2021-05-04 19:45:51 +00:00
int8_t game_is_networked() {
2021-05-12 14:42:22 +00:00
return game_mode > 0;
2021-05-04 19:45:51 +00:00
}
2021-01-11 13:47:14 +00:00
void game_shutdown() {
2021-05-06 15:30:38 +00:00
world_viewers_destroy();
2021-05-05 09:25:05 +00:00
2021-05-12 14:42:22 +00:00
if (game_mode == GAMEKIND_CLIENT) {
2021-05-04 19:22:55 +00:00
network_client_disconnect();
network_destroy();
} else {
world_destroy();
}
2021-01-10 16:42:01 +00:00
}
2021-01-11 20:08:16 +00:00
uint8_t game_is_running() {
2021-01-11 20:11:03 +00:00
return platform_is_running();
2021-01-11 20:08:16 +00:00
}
2021-01-10 16:42:01 +00:00
2021-01-11 13:47:14 +00:00
void game_input() {
2021-05-07 12:27:51 +00:00
platform_input();
2021-01-10 16:42:01 +00:00
}
2021-01-11 13:47:14 +00:00
void game_update() {
2021-05-12 14:42:22 +00:00
if (game_mode == GAMEKIND_CLIENT) {
network_client_tick();
}
2021-05-04 19:22:55 +00:00
else world_update();
2021-05-13 13:44:08 +00:00
2021-05-09 22:40:25 +00:00
game_world_cleanup_entities();
2021-01-10 16:42:01 +00:00
}
2021-01-11 13:47:14 +00:00
void game_render() {
2021-01-11 20:11:03 +00:00
platform_render();
2021-01-10 16:42:01 +00:00
}
2021-05-08 15:42:47 +00:00
void game_action_send_keystate(float x, float y, uint8_t use, uint8_t sprint) {
2021-05-07 20:48:15 +00:00
pkt_send_keystate_send(active_viewer->view_id, x, y, use, sprint);
2021-05-09 22:40:25 +00:00
}
2021-05-13 13:44:08 +00:00
#define GAME_ENT_REMOVAL_TIME 10000
#define GAME_ENT_REMOVAL_TRESHOLD 500
2021-05-09 22:40:25 +00:00
void game_world_cleanup_entities(void) {
2021-05-13 13:05:48 +00:00
// TODO(zaklaus): not the best approach to do a cleanup, let memory stay for a while as it might be reused later on anyway.
2021-05-13 13:44:08 +00:00
#if 1
profile(PROF_ENTITY_REMOVAL) {
static uint64_t last_removal_time = 0;
if (last_removal_time > zpl_time_rel_ms()) return;
last_removal_time = zpl_time_rel_ms() + GAME_ENT_REMOVAL_TIME;
for (int i = 0; i < zpl_buffer_count(world_viewers); i += 1){
entity_view_tbl *view = &world_viewers[i].entities;
uint32_t deletions = 0;
for (int j = 0; j < zpl_array_count(view->entries); j += 1) {
if (deletions > GAME_ENT_REMOVAL_TRESHOLD) return;
entity_view *e = &view->entries[j].value;
if (e->tran_effect == ETRAN_REMOVE) {
entity_view_tbl_remove(view, e->ent_id);
deletions++;
}
}
}
2021-05-09 22:40:25 +00:00
}
2021-05-13 13:05:48 +00:00
#endif
2021-05-10 07:10:26 +00:00
}