ecsnet
Loading...
Searching...
No Matches
network_map.h
1// network_map.h
2#pragma once
3
4#include <stdint.h>
5#include <stdlib.h>
6#include "ecs.h"
7
11typedef struct {
12 uint32_t network_id;
13 entity_t local_id;
15
22typedef struct {
24 size_t count;
25 size_t capacity;
27
32static inline void network_map_init(network_map_t *map) {
33 map->pairs = NULL;
34 map->count = 0;
35 map->capacity = 0;
36}
37
42static inline void network_map_destroy(network_map_t *map) {
43 if (map && map->pairs) {
44 free(map->pairs);
45 map->pairs = NULL;
46 map->count = 0;
47 map->capacity = 0;
48 }
49}
50
51
58static inline entity_t network_map_lookup(network_map_t *map, uint32_t network_id) {
59 if (!map || !map->pairs) return (entity_t)-1;
60 for (size_t i = 0; i < map->count; ++i) {
61 if (map->pairs[i].network_id == network_id) {
62 return map->pairs[i].local_id;
63 }
64 }
65 return (entity_t)-1;
66}
67
76static inline void network_map_insert(network_map_t *map, uint32_t network_id, entity_t local_id) {
77 if (!map) return;
78 if (map->count >= map->capacity) {
79 size_t new_cap = map->capacity == 0 ? 16 : map->capacity * 2;
80 network_id_pair_t *new_pairs = (network_id_pair_t *)realloc(map->pairs, new_cap * sizeof(network_id_pair_t));
81 if (!new_pairs) return;
82 map->pairs = new_pairs;
83 map->capacity = new_cap;
84 }
85 map->pairs[map->count].network_id = network_id;
86 map->pairs[map->count].local_id = local_id;
87 map->count++;
88}
89
96static inline int network_map_remove(network_map_t *map, uint32_t network_id) {
97 if (!map || !map->pairs) return 0;
98 for (size_t i = 0; i < map->count; ++i) {
99 if (map->pairs[i].network_id == network_id) {
100 // Move the last element into this position and shrink the count
101 map->pairs[i] = map->pairs[map->count - 1];
102 map->count--;
103 return 1;
104 }
105 }
106 return 0;
107}
Pair mapping between a global network ID and a local entity ID.
Definition network_map.h:11
entity_t local_id
Corresponding local entity ID in the ECS.
Definition network_map.h:13
uint32_t network_id
Unique ID assigned by the server for an entity.
Definition network_map.h:12
Dynamic mapping structure from network IDs to local entity IDs. This map grows dynamically as new net...
Definition network_map.h:22
network_id_pair_t * pairs
Array of ID pairs.
Definition network_map.h:23
size_t count
Array of ID pairs.
Definition network_map.h:24
size_t capacity
Allocated capacity of the pairs array.
Definition network_map.h:25