From 94c49e770b15eebbd37f4d3ed7ba96789a79c5c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Fri, 3 Feb 2023 09:54:16 +0100 Subject: [PATCH] add pkt_send_code + system changes --- code/foundation/src/core/game.h | 2 ++ code/foundation/src/packets/pkt_send_code.c | 28 +++++++++++++++++++ code/foundation/src/packets/pkt_send_code.h | 15 ++++++++++ code/foundation/src/packets/pkt_send_notif.c | 2 -- code/foundation/src/pkt/packet.c | 2 ++ code/foundation/src/pkt/packet.h | 1 + .../src/systems/modules/system_health.c | 1 + code/games/minimal/src/game.c | 4 +++ code/games/sandbox/src/game.c | 4 +++ code/games/survival/src/game.c | 10 ++++++- code/games/survival/src/game.h | 4 +++ code/games/survival/src/system_mob.c | 5 ++++ 12 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 code/foundation/src/packets/pkt_send_code.c create mode 100644 code/foundation/src/packets/pkt_send_code.h diff --git a/code/foundation/src/core/game.h b/code/foundation/src/core/game.h index 8ae274a..6109237 100644 --- a/code/foundation/src/core/game.h +++ b/code/foundation/src/core/game.h @@ -2,6 +2,7 @@ #include "platform/system.h" #include "world/world_view.h" #include "packets/pkt_send_keystate.h" +#include "packets/pkt_send_code.h" typedef enum { GAMEKIND_SINGLE, @@ -25,6 +26,7 @@ void game_update(); void game_render(); void game_player_joined(uint64_t ent); void game_player_departed(uint64_t ent); +void game_client_receive_code(pkt_send_code data); // base methods called from games void game_core_input(); diff --git a/code/foundation/src/packets/pkt_send_code.c b/code/foundation/src/packets/pkt_send_code.c new file mode 100644 index 0000000..fac1794 --- /dev/null +++ b/code/foundation/src/packets/pkt_send_code.c @@ -0,0 +1,28 @@ +#include "packets/pkt_send_code.h" +#include "pkt/packet.h" +#include "world/world.h" +#include "core/game.h" +#include "world/entity_view.h" + +#include "models/components.h" +#include "systems/systems.h" + +pkt_desc pkt_send_code_desc[] = { + { PKT_UINT(pkt_send_code, code) }, + { PKT_ARRAY(pkt_send_code, params) }, + { PKT_ARRAY(pkt_send_code, data) }, + { PKT_END }, +}; + +size_t pkt_code_send(uint64_t peer_id, uint16_t view_id, pkt_send_code table) { + return pkt_world_write(MSG_ID_SEND_CODE, pkt_table_encode(pkt_send_code_desc, PKT_STRUCT_PTR(&table)), 1, view_id, (void*)peer_id, 0); +} + +int32_t pkt_send_code_handler(pkt_header *header) { + pkt_send_code table = { 0 }; + PKT_IF(pkt_msg_decode(header, pkt_send_code_desc, pkt_pack_desc_args(pkt_send_code_desc), PKT_STRUCT_PTR(&table))); + + game_client_receive_code(table); + + return 0; +} diff --git a/code/foundation/src/packets/pkt_send_code.h b/code/foundation/src/packets/pkt_send_code.h new file mode 100644 index 0000000..156a493 --- /dev/null +++ b/code/foundation/src/packets/pkt_send_code.h @@ -0,0 +1,15 @@ +#pragma once +#include "platform/system.h" +#include "pkt/packet_utils.h" + +typedef struct { + uint64_t code; + uint32_t params[4]; + char data[128]; +} pkt_send_code; + +size_t pkt_code_send(uint64_t peer_id, uint16_t view_id, pkt_send_code code_data); +extern pkt_desc pkt_send_code_desc[]; + +PKT_HANDLER_PROC(pkt_send_code_handler); + diff --git a/code/foundation/src/packets/pkt_send_notif.c b/code/foundation/src/packets/pkt_send_notif.c index f405106..4506e89 100644 --- a/code/foundation/src/packets/pkt_send_notif.c +++ b/code/foundation/src/packets/pkt_send_notif.c @@ -3,8 +3,6 @@ #include "world/world.h" #include "core/game.h" #include "world/entity_view.h" -#include "core/camera.h" -#include "models/prefabs/player.h" #include "models/components.h" #include "systems/systems.h" diff --git a/code/foundation/src/pkt/packet.c b/code/foundation/src/pkt/packet.c index 9223b2b..0030938 100644 --- a/code/foundation/src/pkt/packet.c +++ b/code/foundation/src/pkt/packet.c @@ -9,6 +9,7 @@ #include "packets/pkt_send_keystate.h" #include "packets/pkt_send_librg_update.h" #include "packets/pkt_send_notif.h" +#include "packets/pkt_send_code.h" #include "packets/pkt_switch_viewer.h" #define PKT_HEADER_ELEMENTS 3 @@ -21,6 +22,7 @@ pkt_handler pkt_handlers[] = { {.id = MSG_ID_SEND_BLOCKPOS, .handler = pkt_send_blockpos_handler}, {.id = MSG_ID_SWITCH_VIEWER, .handler = pkt_switch_viewer_handler}, {.id = MSG_ID_SEND_NOTIFICATION, .handler = pkt_send_notification_handler}, + {.id = MSG_ID_SEND_CODE, .handler = pkt_send_code_handler}, }; uint8_t pkt_buffer[PKT_BUFSIZ]; diff --git a/code/foundation/src/pkt/packet.h b/code/foundation/src/pkt/packet.h index 956bada..7f65932 100644 --- a/code/foundation/src/pkt/packet.h +++ b/code/foundation/src/pkt/packet.h @@ -11,6 +11,7 @@ typedef enum { MSG_ID_SEND_BLOCKPOS, MSG_ID_SWITCH_VIEWER, MSG_ID_SEND_NOTIFICATION, + MSG_ID_SEND_CODE, MSG_NEXT_FREE_ID, MAX_PACKETS = 256, } pkt_messages; diff --git a/code/foundation/src/systems/modules/system_health.c b/code/foundation/src/systems/modules/system_health.c index d48f9f7..38f998d 100644 --- a/code/foundation/src/systems/modules/system_health.c +++ b/code/foundation/src/systems/modules/system_health.c @@ -53,6 +53,7 @@ void OnDead(ecs_iter_t *it) { } if (pi) { + *pi = (Input) { 0 }; pi->is_blocked = 1; } } diff --git a/code/games/minimal/src/game.c b/code/games/minimal/src/game.c index c15e41c..65ea93d 100644 --- a/code/games/minimal/src/game.c +++ b/code/games/minimal/src/game.c @@ -19,4 +19,8 @@ void game_player_joined(uint64_t ent) { void game_player_departed(uint64_t ent) { +} + +void game_client_receive_code(pkt_send_code data) { + } \ No newline at end of file diff --git a/code/games/sandbox/src/game.c b/code/games/sandbox/src/game.c index c15e41c..65ea93d 100644 --- a/code/games/sandbox/src/game.c +++ b/code/games/sandbox/src/game.c @@ -19,4 +19,8 @@ void game_player_joined(uint64_t ent) { void game_player_departed(uint64_t ent) { +} + +void game_client_receive_code(pkt_send_code data) { + } \ No newline at end of file diff --git a/code/games/survival/src/game.c b/code/games/survival/src/game.c index 3a7a323..03f42af 100644 --- a/code/games/survival/src/game.c +++ b/code/games/survival/src/game.c @@ -57,4 +57,12 @@ void game_player_joined(uint64_t ent) { void game_player_departed(uint64_t ent) { -} \ No newline at end of file +} + +void game_client_receive_code(pkt_send_code data) { + switch (data.code) { + case SURV_CODE_SHOW_NOTIF: { + notification_push("TEST", data.data); + } break; + } +} diff --git a/code/games/survival/src/game.h b/code/games/survival/src/game.h index 3d63f13..6fa40d2 100644 --- a/code/games/survival/src/game.h +++ b/code/games/survival/src/game.h @@ -1,3 +1,7 @@ #pragma once +enum { + SURV_CODE_SHOW_NOTIF, +}; + void game_setup_ecs(); diff --git a/code/games/survival/src/system_mob.c b/code/games/survival/src/system_mob.c index 35434c6..ba932c8 100644 --- a/code/games/survival/src/system_mob.c +++ b/code/games/survival/src/system_mob.c @@ -84,5 +84,10 @@ void MobMeleeAtk(ecs_iter_t *it) { void MobOnDead(ecs_iter_t *it) { for (int i = 0; i < it->count; i++) { entity_despawn(it->entities[i]); + + pkt_code_send(0, 0, (pkt_send_code){ + .code = SURV_CODE_SHOW_NOTIF, + .data = "mob died" + }); } } \ No newline at end of file