survival game wip

efd/v1
Dominik Madarász 2023-02-01 15:28:30 +01:00
parent 719b002989
commit 3f5d999d44
17 changed files with 344 additions and 210 deletions

BIN
art/gen/player.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -23,6 +23,9 @@ void game_input();
void game_update(); void game_update();
void game_render(); void game_render();
void game_player_joined(uint64_t ent);
void game_player_departed(uint64_t ent);
//~ Called from platform.c //~ Called from platform.c
void game_draw_ui(); void game_draw_ui();

View File

@ -3,7 +3,7 @@
static debug_draw_queue draw_queue = {0}; static debug_draw_queue draw_queue = {0};
#if !defined(_DEBUG) && 1 #if !defined(_DEBUG) && 1
static bool draw_is_enabled = false; static bool draw_is_enabled = false;
#else #else
static bool draw_is_enabled = true; static bool draw_is_enabled = true;

View File

@ -139,7 +139,6 @@ void tooltip_clear(void) {
inline void tooltip_draw_contents(tooltip *desc) { inline void tooltip_draw_contents(tooltip *desc) {
if (!desc) return; if (!desc) return;
nk_layout_row_dynamic(game_ui, 0, 1); nk_layout_row_dynamic(game_ui, 0, 1);
nk_label_wrap(game_ui, desc->content); nk_label_wrap(game_ui, desc->content);
} }

View File

@ -32,6 +32,7 @@ static asset assets[] = {
ASSET_TEX(ASSET_BLUEPRINT), ASSET_TEX(ASSET_BLUEPRINT),
ASSET_TEX(ASSET_BLUEPRINT_DEMO_HOUSE), ASSET_TEX(ASSET_BLUEPRINT_DEMO_HOUSE),
ASSET_TEX(ASSET_MOB), ASSET_TEX(ASSET_MOB),
ASSET_TEX(ASSET_PLAYER),
// NOTE(zaklaus): blocks // NOTE(zaklaus): blocks
ASSET_TEX(ASSET_FENCE), ASSET_TEX(ASSET_FENCE),

View File

@ -6,6 +6,8 @@
#include "models/entity.h" #include "models/entity.h"
#include "models/components.h" #include "models/components.h"
#include "core/game.h"
#define PLAYER_MAX_HP 100.0f #define PLAYER_MAX_HP 100.0f
uint64_t player_spawn(char *name) { uint64_t player_spawn(char *name) {
@ -30,5 +32,6 @@ uint64_t player_spawn(char *name) {
} }
void player_despawn(uint64_t ent_id) { void player_despawn(uint64_t ent_id) {
game_player_departed(ent_id);
entity_despawn(ent_id); entity_despawn(ent_id);
} }

View File

@ -31,6 +31,8 @@ int32_t pkt_00_init_handler(pkt_header *header) {
entity_set_position(ent_id, world_dim()/2.0f + rand()%15*15.0f, world_dim()/2.0f + rand()%15*15.0f); entity_set_position(ent_id, world_dim()/2.0f + rand()%15*15.0f, world_dim()/2.0f + rand()%15*15.0f);
game_player_joined(ent_id);
zpl_printf("[INFO] initializing player entity id: %d with view id: %d for peer id: %d...\n", ent_id, table.view_id, peer_id); zpl_printf("[INFO] initializing player entity id: %d with view id: %d for peer id: %d...\n", ent_id, table.view_id, peer_id);
ecs_set(world_ecs(), ent_id, ClientInfo, {.peer = peer_id, .view_id = header->view_id, .active = false }); ecs_set(world_ecs(), ent_id, ClientInfo, {.peer = peer_id, .view_id = header->view_id, .active = false });
pkt_01_welcome_send(world_seed(), peer_id, header->view_id, ent_id, world_chunk_size(), world_chunk_amount()); pkt_01_welcome_send(world_seed(), peer_id, header->view_id, ent_id, world_chunk_size(), world_chunk_amount());

View File

@ -14,12 +14,12 @@ ZPL_DIAGNOSTIC_PUSH_WARNLEVEL(0)
ZPL_DIAGNOSTIC_POP ZPL_DIAGNOSTIC_POP
#define PHY_BLOCK_COLLISION 1 #define PHY_BLOCK_COLLISION 1
#define PHY_C2_BLOCK_COLLISION 0
#define PHY_WALK_DRAG 4.23f #define PHY_WALK_DRAG 4.23f
#define PHY_LOOKAHEAD(x) (zpl_sign(x)*16.0f) #define PHY_LOOKAHEAD(x) (zpl_sign(x)*16.0f)
ecs_query_t *ecs_rigidbodies; ecs_query_t *ecs_rigidbodies = 0;
ecs_entity_t ecs_timer = 0;
#define ECO2D_TICK_RATE (1.0f/20.f)
#include "modules/system_onfoot.c" #include "modules/system_onfoot.c"
#include "modules/system_demo.c" #include "modules/system_demo.c"
@ -28,7 +28,6 @@ ecs_query_t *ecs_rigidbodies;
#include "modules/system_logistics.c" #include "modules/system_logistics.c"
#include "modules/system_producer.c" #include "modules/system_producer.c"
#include "modules/system_blueprint.c" #include "modules/system_blueprint.c"
#include "modules/system_mob.c"
static inline float physics_correction(float x, float vx, float bounce, float dim) { static inline float physics_correction(float x, float vx, float bounce, float dim) {
float r = (((zpl_max(0.0f, dim - zpl_abs(x))*zpl_sign(x)))*dim); float r = (((zpl_max(0.0f, dim - zpl_abs(x))*zpl_sign(x)))*dim);
@ -39,6 +38,23 @@ static inline bool physics_check_aabb(float a1x, float a2x, float a1y, float a2y
return (a1x < b2x && a2x > b1x && a1y < b2y && a2y > b1y); return (a1x < b2x && a2x > b1x && a1y < b2y && a2y > b1y);
} }
static inline bool BlockCollisionIslandTest(Position *p, librg_chunk ch_p) {
// collect islands
collision_island islands[16];
uint8_t num_islands = world_chunk_collision_islands(ch_p, islands);
for (uint8_t i = 0; i < num_islands; i++) {
#if 1
{
zpl_printf("px %f py %f minx %f miny %f\n", p->x, p->y, islands[i].minx, islands[i].miny);
debug_v2 a = {islands[i].minx, islands[i].miny};
debug_v2 b = {islands[i].maxx+WORLD_BLOCK_SIZE, islands[i].maxy+WORLD_BLOCK_SIZE};
debug_push_rect(a, b, 0xFFFFFFFF);
}
#endif
}
return 0;
}
void BlockCollisions(ecs_iter_t *it) { void BlockCollisions(ecs_iter_t *it) {
profile(PROF_PHYS_BLOCK_COLS) { profile(PROF_PHYS_BLOCK_COLS) {
@ -58,6 +74,16 @@ void BlockCollisions(ecs_iter_t *it) {
p[i].x = zpl_clamp(p[i].x, 0, w-1); p[i].x = zpl_clamp(p[i].x, 0, w-1);
p[i].y = zpl_clamp(p[i].y, 0, w-1); p[i].y = zpl_clamp(p[i].y, 0, w-1);
} }
#if PHY_C2_BLOCK_COLLISION==1
// collision islands
{
librg_chunk chunk_id = librg_chunk_from_realpos(world_tracker(), p[i].x, p[i].y, 0);
if (BlockCollisionIslandTest((p+i), chunk_id))
continue;
}
#endif
#if PHY_BLOCK_COLLISION==1 #if PHY_BLOCK_COLLISION==1
// NOTE(zaklaus): X axis // NOTE(zaklaus): X axis
@ -141,25 +167,25 @@ void BodyCollisions(ecs_iter_t *it) {
float p2_y = p2[j].y /*+ v2[j].y*/; float p2_y = p2[j].y /*+ v2[j].y*/;
c2AABB box_a = { c2AABB box_a = {
.min = { p_x - WORLD_BLOCK_SIZE / 2, p_y - WORLD_BLOCK_SIZE / 2 }, .min = { p_x - WORLD_BLOCK_SIZE / 2, p_y - WORLD_BLOCK_SIZE / 4 },
.max = { p_x + WORLD_BLOCK_SIZE / 2, p_y + WORLD_BLOCK_SIZE / 2 }, .max = { p_x + WORLD_BLOCK_SIZE / 2, p_y + WORLD_BLOCK_SIZE / 4 },
}; };
c2AABB box_b = { c2AABB box_b = {
.min = { p2_x - WORLD_BLOCK_SIZE / 2, p2_y - WORLD_BLOCK_SIZE / 2 }, .min = { p2_x - WORLD_BLOCK_SIZE / 2, p2_y - WORLD_BLOCK_SIZE / 4 },
.max = { p2_x + WORLD_BLOCK_SIZE / 2, p2_y + WORLD_BLOCK_SIZE / 2 }, .max = { p2_x + WORLD_BLOCK_SIZE / 2, p2_y + WORLD_BLOCK_SIZE / 4 },
}; };
// do a basic sweep first // do a basic sweep first
float r1x = (box_a.max.x-box_a.min.x);
float r1y = (box_a.max.y-box_a.min.y);
float r1 = (r1x*r1x + r1y*r1y)*.5f;
float r2x = (box_b.max.x-box_b.min.x);
float r2y = (box_b.max.y-box_b.min.y);
float r2 = (r2x*r2x + r2y*r2y)*.5f;
{ {
float r1x = (box_a.max.x-box_a.min.x);
float r1y = (box_a.max.y-box_a.min.y);
float r1 = (r1x*r1x + r1y*r1y)*.5f;
float r2x = (box_b.max.x-box_b.min.x);
float r2y = (box_b.max.y-box_b.min.y);
float r2 = (r2x*r2x + r2y*r2y)*.5f;
float dx = (p2_x-p_x); float dx = (p2_x-p_x);
float dy = (p2_y-p_y); float dy = (p2_y-p_y);
float d = (dx*dx + dy*dy); float d = (dx*dx + dy*dy);
@ -170,12 +196,12 @@ void BodyCollisions(ecs_iter_t *it) {
c2Circle circle_a = { c2Circle circle_a = {
.p = { p_x, p_y }, .p = { p_x, p_y },
.r = b[i].circle.r, .r = r1/2.f,
}; };
c2Circle circle_b = { c2Circle circle_b = {
.p = { p2_x, p2_y }, .p = { p2_x, p2_y },
.r = b2[j].circle.r, .r = r2/2.f,
}; };
const void *shapes_a[] = { &circle_a, &box_a }; const void *shapes_a[] = { &circle_a, &box_a };
@ -365,19 +391,10 @@ void DisableWorldEdit(ecs_iter_t *it) {
world_set_stage(NULL); world_set_stage(NULL);
} }
#define ECS_SYSTEM_TICKED(world, id, stage, ...)\
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
ecs_set_tick_source(world, id, timer);
#define ECS_SYSTEM_TICKED_EX(world, id, stage, time, ...)\
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
ecs_entity_t timer_##id = ecs_set_interval(ecs, 0, ECO2D_TICK_RATE*time);\
ecs_set_tick_source(world, id, timer_##id);
void SystemsImport(ecs_world_t *ecs) { void SystemsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Systems); ECS_MODULE(ecs, Systems);
ecs_entity_t timer = ecs_set_interval(ecs, 0, ECO2D_TICK_RATE); ecs_timer = ecs_set_interval(ecs, 0, ECO2D_TICK_RATE);
ecs_rigidbodies = ecs_query_new(ecs, "components.Position, components.Velocity, components.PhysicsBody"); ecs_rigidbodies = ecs_query_new(ecs, "components.Position, components.Velocity, components.PhysicsBody");
@ -418,11 +435,7 @@ void SystemsImport(ecs_world_t *ecs) {
ECS_SYSTEM_TICKED(ecs, CreatureSeekFood, EcsPostUpdate, components.Creature, components.Position, components.Velocity, components.SeeksFood, !components.SeeksCompanion); ECS_SYSTEM_TICKED(ecs, CreatureSeekFood, EcsPostUpdate, components.Creature, components.Position, components.Velocity, components.SeeksFood, !components.SeeksCompanion);
ECS_SYSTEM_TICKED(ecs, CreatureSeekCompanion, EcsPostUpdate, components.Creature, components.Position, components.Velocity, components.SeeksCompanion, !components.SeeksFood); ECS_SYSTEM_TICKED(ecs, CreatureSeekCompanion, EcsPostUpdate, components.Creature, components.Position, components.Velocity, components.SeeksCompanion, !components.SeeksFood);
ECS_SYSTEM(ecs, CreatureRoamAround, EcsPostUpdate, components.Velocity, components.Creature, !components.SeeksFood, !components.SeeksCompanion); ECS_SYSTEM(ecs, CreatureRoamAround, EcsPostUpdate, components.Velocity, components.Creature, !components.SeeksFood, !components.SeeksCompanion);
ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob);
ECS_SYSTEM(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer);
ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee);
ECS_SYSTEM(ecs, ResetActivators, EcsPostUpdate, components.Input); ECS_SYSTEM(ecs, ResetActivators, EcsPostUpdate, components.Input);
ECS_SYSTEM(ecs, ClearVehicle, EcsUnSet, components.Vehicle); ECS_SYSTEM(ecs, ClearVehicle, EcsUnSet, components.Vehicle);

View File

@ -5,6 +5,21 @@ static inline float safe_dt(ecs_iter_t *it) {
return zpl_min(it->delta_time, 0.03334f); return zpl_min(it->delta_time, 0.03334f);
} }
extern ecs_query_t *ecs_rigidbodies;
extern ecs_entity_t ecs_timer;
#define TICK_VAR(var) (var) = zpl_max((var)-1, 0) #define TICK_VAR(var) (var) = zpl_max((var)-1, 0)
#define ECO2D_TICK_RATE (1.0f/20.f)
#define ECS_SYSTEM_TICKED(world, id, stage, ...)\
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
ecs_set_tick_source(world, id, ecs_timer);
#define ECS_SYSTEM_TICKED_EX(world, id, stage, time, ...)\
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
ecs_entity_t timer_##id = ecs_set_interval(ecs, 0, ECO2D_TICK_RATE*time);\
ecs_set_tick_source(world, id, timer_##id);
void SystemsImport(ecs_world_t *ecs); void SystemsImport(ecs_world_t *ecs);

View File

@ -21,53 +21,53 @@
ZPL_TABLE(static, world_snapshot, world_snapshot_, entity_view); ZPL_TABLE(static, world_snapshot, world_snapshot_, entity_view);
ZPL_TABLE(static, world_component_cache, world_component_cache_, zpl_uintptr); // TODO(inlife): not use for long ZPL_TABLE(static, world_component_cache, world_component_cache_, zpl_uintptr); // TODO(inlife): not use for long
static world_data world = {0}; static world_data world = { 0 };
static world_snapshot streamer_snapshot; static world_snapshot streamer_snapshot;
static world_component_cache component_cache; static world_component_cache component_cache;
entity_view *world_build_entity_view(int64_t e) { entity_view* world_build_entity_view(int64_t e) {
entity_view *cached_ev = world_snapshot_get(&streamer_snapshot, e); entity_view* cached_ev = world_snapshot_get(&streamer_snapshot, e);
if (cached_ev) return cached_ev; if (cached_ev) return cached_ev;
entity_view view = {0}; entity_view view = { 0 };
const Classify *classify = ecs_get(world_ecs(), e, Classify); const Classify* classify = ecs_get(world_ecs(), e, Classify);
ZPL_ASSERT(classify); ZPL_ASSERT(classify);
view.kind = classify->id; view.kind = classify->id;
const Position *pos = ecs_get(world_ecs(), e, Position); const Position* pos = ecs_get(world_ecs(), e, Position);
if (pos) { if (pos) {
view.x = pos->x; view.x = pos->x;
view.y = pos->y; view.y = pos->y;
} }
const Velocity *vel = ecs_get(world_ecs(), e, Velocity); const Velocity* vel = ecs_get(world_ecs(), e, Velocity);
if (vel) { if (vel) {
view.flag |= EFLAG_INTERP; view.flag |= EFLAG_INTERP;
view.vx = vel->x; view.vx = vel->x;
view.vy = vel->y; view.vy = vel->y;
} }
const Health *health = ecs_get(world_ecs(), e, Health); const Health* health = ecs_get(world_ecs(), e, Health);
if (health) { if (health) {
view.hp = health->hp; view.hp = health->hp;
view.max_hp = health->max_hp; view.max_hp = health->max_hp;
} }
if (ecs_get(world_ecs(), e, Vehicle)) { if (ecs_get(world_ecs(), e, Vehicle)) {
Vehicle const* veh = ecs_get(world_ecs(), e, Vehicle); Vehicle const* veh = ecs_get(world_ecs(), e, Vehicle);
view.heading = veh->heading; view.heading = veh->heading;
view.veh_kind = veh->veh_kind; view.veh_kind = veh->veh_kind;
} }
if (ecs_get(world_ecs(), e, Item)) { if (ecs_get(world_ecs(), e, Item)) {
Item const* dr = ecs_get(world_ecs(), e, Item); Item const* dr = ecs_get(world_ecs(), e, Item);
view.asset = dr->kind; view.asset = dr->kind;
view.quantity = dr->quantity; view.quantity = dr->quantity;
view.durability = dr->durability; view.durability = dr->durability;
} }
if (ecs_get(world_ecs(), e, Device)) { if (ecs_get(world_ecs(), e, Device)) {
Device const* dev = ecs_get(world_ecs(), e, Device); Device const* dev = ecs_get(world_ecs(), e, Device);
view.asset = dev->asset; view.asset = dev->asset;
@ -75,54 +75,54 @@ entity_view *world_build_entity_view(int64_t e) {
view.progress_value = dev->progress_value; view.progress_value = dev->progress_value;
view.is_producer = ecs_get(world_ecs(), e, Producer) != 0; view.is_producer = ecs_get(world_ecs(), e, Producer) != 0;
} }
view.inside_vehicle = ecs_get(world_ecs(), e, IsInVehicle) != 0 ? true : false; view.inside_vehicle = ecs_get(world_ecs(), e, IsInVehicle) != 0 ? true : false;
Inventory *inv = 0; Inventory* inv = 0;
if ((inv = ecs_get_mut_if_ex(world_ecs(), e, Inventory))) { if ((inv = ecs_get_mut_if_ex(world_ecs(), e, Inventory))) {
view.has_items = true; view.has_items = true;
for (int i = 0; i < ITEMS_INVENTORY_SIZE; i += 1) { for (int i = 0; i < ITEMS_INVENTORY_SIZE; i += 1) {
const Item *it = ecs_get_if(world_ecs(), inv->items[i], Item); const Item* it = ecs_get_if(world_ecs(), inv->items[i], Item);
view.items[i] = it ? *it : (Item){0}; view.items[i] = it ? *it : (Item) { 0 };
} }
const Input *in = ecs_get(world_ecs(), e, Input); const Input* in = ecs_get(world_ecs(), e, Input);
if (in){ if (in) {
view.selected_item = in->selected_item; view.selected_item = in->selected_item;
view.pick_ent = (uint64_t)in->pick_ent; view.pick_ent = (uint64_t)in->pick_ent;
view.sel_ent = (uint64_t)in->sel_ent; view.sel_ent = (uint64_t)in->sel_ent;
if (world_entity_valid(in->storage_ent)){ if (world_entity_valid(in->storage_ent)) {
ItemContainer *ic = 0; ItemContainer* ic = 0;
if ((ic = ecs_get_mut_if_ex(world_ecs(), in->storage_ent, ItemContainer))){ if ((ic = ecs_get_mut_if_ex(world_ecs(), in->storage_ent, ItemContainer))) {
view.has_storage_items = true; view.has_storage_items = true;
for (int i = 0; i < ITEMS_CONTAINER_SIZE; i += 1) { for (int i = 0; i < ITEMS_CONTAINER_SIZE; i += 1) {
const Item *it = ecs_get_if(world_ecs(), ic->items[i], Item); const Item* it = ecs_get_if(world_ecs(), ic->items[i], Item);
view.storage_items[i] = it ? *it : (Item){0}; view.storage_items[i] = it ? *it : (Item) { 0 };
} }
view.storage_selected_item = in->storage_selected_item; view.storage_selected_item = in->storage_selected_item;
if (ecs_get(world_ecs(), in->storage_ent, Producer)) { if (ecs_get(world_ecs(), in->storage_ent, Producer)) {
Device const* dev = ecs_get(world_ecs(), in->storage_ent, Device); Device const* dev = ecs_get(world_ecs(), in->storage_ent, Device);
zpl_zero_array(view.craftables, MAX_CRAFTABLES); zpl_zero_array(view.craftables, MAX_CRAFTABLES);
for (uint16_t i = 0, si = 0; i < craft_get_num_recipes(); i++) { for (uint16_t i = 0, si = 0; i < craft_get_num_recipes(); i++) {
ZPL_ASSERT(si < MAX_CRAFTABLES); ZPL_ASSERT(si < MAX_CRAFTABLES);
asset_id pid = craft_get_recipe_asset(i); asset_id pid = craft_get_recipe_asset(i);
if (craft_is_item_produced_by_producer(pid, dev->asset)) { if (craft_is_item_produced_by_producer(pid, dev->asset)) {
view.craftables[si++] = pid; view.craftables[si++] = pid;
} }
} }
} }
} }
} }
} }
} }
Chunk *chunk = 0; Chunk* chunk = 0;
if ((chunk = ecs_get_mut_if(world_ecs(), e, Chunk))) { if ((chunk = ecs_get_mut_if(world_ecs(), e, Chunk))) {
view.chk_id = chunk->id; view.chk_id = chunk->id;
view.x = chunk->x; view.x = chunk->x;
@ -130,39 +130,39 @@ entity_view *world_build_entity_view(int64_t e) {
view.blocks_used = 1; view.blocks_used = 1;
view.is_dirty = chunk->is_dirty; view.is_dirty = chunk->is_dirty;
chunk->is_dirty = false; chunk->is_dirty = false;
for (int i = 0; i < world.chunk_size*world.chunk_size; i += 1) { for (int i = 0; i < world.chunk_size * world.chunk_size; i += 1) {
view.blocks[i] = world.block_mapping[chunk->id][i]; view.blocks[i] = world.block_mapping[chunk->id][i];
} }
for (int i = 0; i < world.chunk_size*world.chunk_size; i += 1) { for (int i = 0; i < world.chunk_size * world.chunk_size; i += 1) {
view.outer_blocks[i] = world.outer_block_mapping[chunk->id][i]; view.outer_blocks[i] = world.outer_block_mapping[chunk->id][i];
} }
} }
world_snapshot_set(&streamer_snapshot, e, view); world_snapshot_set(&streamer_snapshot, e, view);
return world_snapshot_get(&streamer_snapshot, e); return world_snapshot_get(&streamer_snapshot, e);
} }
int32_t tracker_write_create(librg_world *w, librg_event *e) { int32_t tracker_write_create(librg_world* w, librg_event* e) {
int64_t entity_id = librg_event_entity_get(w, e); int64_t entity_id = librg_event_entity_get(w, e);
#ifdef WORLD_LAYERING #ifdef WORLD_LAYERING
if (world.active_layer_id != WORLD_TRACKER_LAYERS-1) { if (world.active_layer_id != WORLD_TRACKER_LAYERS - 1) {
// NOTE(zaklaus): reject updates from smaller layers // NOTE(zaklaus): reject updates from smaller layers
return LIBRG_WRITE_REJECT; return LIBRG_WRITE_REJECT;
} }
#endif #endif
size_t actual_length = librg_event_size_get(w, e); size_t actual_length = librg_event_size_get(w, e);
char *buffer = librg_event_buffer_get(w, e); char* buffer = librg_event_buffer_get(w, e);
return (int32_t)entity_view_pack_struct(buffer, actual_length, world_build_entity_view(entity_id)); return (int32_t)entity_view_pack_struct(buffer, actual_length, world_build_entity_view(entity_id));
} }
int32_t tracker_write_remove(librg_world *w, librg_event *e) { int32_t tracker_write_remove(librg_world* w, librg_event* e) {
(void)e; (void)e;
(void)w; (void)w;
#ifdef WORLD_LAYERING #ifdef WORLD_LAYERING
if (world.active_layer_id != WORLD_TRACKER_LAYERS-1) { if (world.active_layer_id != WORLD_TRACKER_LAYERS - 1) {
// NOTE(zaklaus): reject updates from smaller layers // NOTE(zaklaus): reject updates from smaller layers
return LIBRG_WRITE_REJECT; return LIBRG_WRITE_REJECT;
} }
@ -170,19 +170,19 @@ int32_t tracker_write_remove(librg_world *w, librg_event *e) {
return 0; return 0;
} }
int32_t tracker_write_update(librg_world *w, librg_event *e) { int32_t tracker_write_update(librg_world* w, librg_event* e) {
int64_t entity_id = librg_event_entity_get(w, e); int64_t entity_id = librg_event_entity_get(w, e);
size_t actual_length = librg_event_size_get(w, e); size_t actual_length = librg_event_size_get(w, e);
char *buffer = librg_event_buffer_get(w, e); char* buffer = librg_event_buffer_get(w, e);
entity_view *view = world_build_entity_view(entity_id); entity_view* view = world_build_entity_view(entity_id);
// NOTE(zaklaus): exclude chunks from updates as they never move // NOTE(zaklaus): exclude chunks from updates as they never move
{ {
if (view->kind == EKIND_CHUNK && !view->is_dirty) { if (view->kind == EKIND_CHUNK && !view->is_dirty) {
return LIBRG_WRITE_REJECT; return LIBRG_WRITE_REJECT;
} }
} }
// NOTE(zaklaus): action-based updates // NOTE(zaklaus): action-based updates
#if ECO2D_STREAM_ACTIONFILTER #if ECO2D_STREAM_ACTIONFILTER
{ {
@ -191,56 +191,98 @@ int32_t tracker_write_update(librg_world *w, librg_event *e) {
} }
} }
#endif #endif
return (int32_t)entity_view_pack_struct(buffer, actual_length, view); return (int32_t)entity_view_pack_struct(buffer, actual_length, view);
} }
void world_setup_pkt_handlers(world_pkt_reader_proc *reader_proc, world_pkt_writer_proc *writer_proc) { void world_setup_pkt_handlers(world_pkt_reader_proc* reader_proc, world_pkt_writer_proc* writer_proc) {
world.reader_proc = reader_proc; world.reader_proc = reader_proc;
world.writer_proc = writer_proc; world.writer_proc = writer_proc;
} }
void world_rebuild_chunk_islands(librg_chunk chunk_id) {
uint16_t ch_x, ch_y;
librg_chunk_to_chunkpos(world.tracker, chunk_id, &ch_x, &ch_y, NULL);
float wp_x = (float)ch_x * world_dim(), wp_y = (float)ch_y * world_dim();
world.islands_count[chunk_id] = 0;
collision_island clr_island = { ZPL_F32_MAX, ZPL_F32_MAX, ZPL_F32_MIN, ZPL_F32_MIN };
collision_island new_island = clr_island;
for (int y = 0; y < world.chunk_size; y += 1) {
for (int x = 0; x < world.chunk_size; x += 1) {
block_id c = world.block_mapping[chunk_id][(y * world.chunk_size) + x];
float wx = x * WORLD_BLOCK_SIZE + wp_x;
float wy = y * WORLD_BLOCK_SIZE + wp_y;
if (blocks_get_flags(c) & BLOCK_FLAG_COLLISION) {
if (new_island.minx > wx)
new_island.minx = wx;
if (new_island.miny > wy)
new_island.miny = wy;
if (new_island.maxx < wx)
new_island.maxx = wx;
if (new_island.maxy < wy)
new_island.maxy = wy;
}
else if (zpl_memcompare(&new_island, &clr_island, sizeof(collision_island))) {
world.islands[chunk_id * 16 + world.islands_count[chunk_id]] = new_island;
world.islands_count[chunk_id]++;
new_island = clr_island;
}
}
}
if (zpl_memcompare(&new_island, &clr_island, sizeof(collision_island))) {
world.islands[chunk_id * 16 + world.islands_count[chunk_id]] = new_island;
world.islands_count[chunk_id]++;
}
}
static inline static inline
void world_chunk_setup_grid(void) { void world_chunk_setup_grid(void) {
for (int i = 0; i < zpl_square(world.chunk_amount); ++i) { for (int i = 0; i < zpl_square(world.chunk_amount); ++i) {
ecs_entity_t e = ecs_new(world.ecs, 0); ecs_entity_t e = ecs_new(world.ecs, 0);
ecs_set(world.ecs, e, Classify, {.id = EKIND_CHUNK }); ecs_set(world.ecs, e, Classify, { .id = EKIND_CHUNK });
Chunk *chunk = ecs_get_mut(world.ecs, e, Chunk); Chunk* chunk = ecs_get_mut(world.ecs, e, Chunk);
librg_entity_track(world.tracker, e); librg_entity_track(world.tracker, e);
librg_entity_chunk_set(world.tracker, e, i); librg_entity_chunk_set(world.tracker, e, i);
librg_chunk_to_chunkpos(world.tracker, i, &chunk->x, &chunk->y, NULL); librg_chunk_to_chunkpos(world.tracker, i, &chunk->x, &chunk->y, NULL);
world.chunk_mapping[i] = e; world.chunk_mapping[i] = e;
world.block_mapping[i] = zpl_malloc(sizeof(block_id)*zpl_square(world.chunk_size)); world.block_mapping[i] = zpl_malloc(sizeof(block_id) * zpl_square(world.chunk_size));
world.outer_block_mapping[i] = zpl_malloc(sizeof(block_id)*zpl_square(world.chunk_size)); world.outer_block_mapping[i] = zpl_malloc(sizeof(block_id) * zpl_square(world.chunk_size));
chunk->id = i; chunk->id = i;
chunk->is_dirty = false; chunk->is_dirty = false;
for (int y = 0; y < world.chunk_size; y += 1) { for (int y = 0; y < world.chunk_size; y += 1) {
for (int x = 0; x < world.chunk_size; x += 1) { for (int x = 0; x < world.chunk_size; x += 1) {
int chk_x = chunk->x * world.chunk_size; int chk_x = chunk->x * world.chunk_size;
int chk_y = chunk->y * world.chunk_size; int chk_y = chunk->y * world.chunk_size;
block_id *c = &world.block_mapping[i][(y*world.chunk_size)+x]; block_id* c = &world.block_mapping[i][(y * world.chunk_size) + x];
*c = world.data[(chk_y+y)*world.dim + (chk_x+x)]; *c = world.data[(chk_y + y) * world.dim + (chk_x + x)];
c = &world.outer_block_mapping[i][(y*world.chunk_size)+x]; c = &world.outer_block_mapping[i][(y * world.chunk_size) + x];
*c = world.outer_data[(chk_y+y)*world.dim + (chk_x+x)]; *c = world.outer_data[(chk_y + y) * world.dim + (chk_x + x)];
} }
} }
world_rebuild_chunk_islands(i);
} }
} }
static inline static inline
void world_configure_tracker(void) { void world_configure_tracker(void) {
world.tracker = librg_world_create(); world.tracker = librg_world_create();
ZPL_ASSERT_MSG(world.tracker, "[ERROR] An error occurred while trying to create a server world."); ZPL_ASSERT_MSG(world.tracker, "[ERROR] An error occurred while trying to create a server world.");
/* config our world grid */ /* config our world grid */
librg_config_chunksize_set(world.tracker, WORLD_BLOCK_SIZE * world.chunk_size, WORLD_BLOCK_SIZE * world.chunk_size, 1); librg_config_chunksize_set(world.tracker, WORLD_BLOCK_SIZE * world.chunk_size, WORLD_BLOCK_SIZE * world.chunk_size, 1);
librg_config_chunkamount_set(world.tracker, world.chunk_amount, world.chunk_amount, 0); librg_config_chunkamount_set(world.tracker, world.chunk_amount, world.chunk_amount, 0);
librg_config_chunkoffset_set(world.tracker, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG); librg_config_chunkoffset_set(world.tracker, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG);
librg_event_set(world.tracker, LIBRG_WRITE_CREATE, tracker_write_create); librg_event_set(world.tracker, LIBRG_WRITE_CREATE, tracker_write_create);
librg_event_set(world.tracker, LIBRG_WRITE_REMOVE, tracker_write_remove); librg_event_set(world.tracker, LIBRG_WRITE_REMOVE, tracker_write_remove);
librg_event_set(world.tracker, LIBRG_WRITE_UPDATE, tracker_write_update); librg_event_set(world.tracker, LIBRG_WRITE_UPDATE, tracker_write_update);
@ -248,16 +290,16 @@ void world_configure_tracker(void) {
static inline static inline
void world_init_worldgen_data(void) { void world_init_worldgen_data(void) {
world.data = zpl_malloc(sizeof(block_id)*world.size); world.data = zpl_malloc(sizeof(block_id) * world.size);
world.outer_data = zpl_malloc(sizeof(block_id)*world.size); world.outer_data = zpl_malloc(sizeof(block_id) * world.size);
ZPL_ASSERT(world.data && world.outer_data); ZPL_ASSERT(world.data && world.outer_data);
} }
static inline static inline
void world_setup_ecs(void) { void world_setup_ecs(void) {
world.ecs = ecs_init(); world.ecs = ecs_init();
ECS_IMPORT(world.ecs, Components); ECS_IMPORT(world.ecs, Components);
ECS_IMPORT(world.ecs, Systems); ECS_IMPORT(world.ecs, Systems);
world.ecs_update = ecs_query_new(world.ecs, "components.ClientInfo, components.Position"); world.ecs_update = ecs_query_new(world.ecs, "components.ClientInfo, components.Position");
@ -266,9 +308,11 @@ void world_setup_ecs(void) {
static inline static inline
void world_init_mapping(void) { void world_init_mapping(void) {
world.chunk_mapping = zpl_malloc(sizeof(ecs_entity_t)*zpl_square(world.chunk_amount)); world.chunk_mapping = zpl_malloc(sizeof(ecs_entity_t) * zpl_square(world.chunk_amount));
world.block_mapping = zpl_malloc(sizeof(block_id*)*zpl_square(world.chunk_amount)); world.block_mapping = zpl_malloc(sizeof(block_id*) * zpl_square(world.chunk_amount));
world.outer_block_mapping = zpl_malloc(sizeof(block_id*)*zpl_square(world.chunk_amount)); world.outer_block_mapping = zpl_malloc(sizeof(block_id*) * zpl_square(world.chunk_amount));
world.islands_count = zpl_malloc(sizeof(world.islands_count[0]) * zpl_square(world.chunk_amount));
world.islands = zpl_malloc(sizeof(collision_island) * 16 * zpl_square(world.chunk_amount));
world_snapshot_init(&streamer_snapshot, zpl_heap()); world_snapshot_init(&streamer_snapshot, zpl_heap());
world_component_cache_init(&component_cache, zpl_heap()); world_component_cache_init(&component_cache, zpl_heap());
} }
@ -277,7 +321,7 @@ static inline
void world_generate_instance(void) { void world_generate_instance(void) {
int32_t world_build_status = worldgen_build(&world); int32_t world_build_status = worldgen_build(&world);
ZPL_ASSERT(world_build_status >= 0); ZPL_ASSERT(world_build_status >= 0);
for (int i = 0; i < zpl_square(world.dim); ++i) { for (int i = 0; i < zpl_square(world.dim); ++i) {
if (world.data[i] == 0) { if (world.data[i] == 0) {
ZPL_PANIC("Worldgen failure! Block %d is unset!\n", i); ZPL_PANIC("Worldgen failure! Block %d is unset!\n", i);
@ -299,10 +343,10 @@ int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) {
world.seed = seed; world.seed = seed;
world.chunk_size = chunk_size; world.chunk_size = chunk_size;
world.chunk_amount = chunk_amount; world.chunk_amount = chunk_amount;
world.dim = (world.chunk_size * world.chunk_amount); world.dim = (world.chunk_size * world.chunk_amount);
world.size = world.dim * world.dim; world.size = world.dim * world.dim;
world_configure_tracker(); world_configure_tracker();
world_setup_ecs(); world_setup_ecs();
world_init_worldgen_data(); world_init_worldgen_data();
@ -310,9 +354,9 @@ int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) {
world_init_mapping(); world_init_mapping();
world_chunk_setup_grid(); world_chunk_setup_grid();
world_free_worldgen_data(); world_free_worldgen_data();
zpl_printf("[INFO] Created a new server world\n"); zpl_printf("[INFO] Created a new server world\n");
return WORLD_ERROR_NONE; return WORLD_ERROR_NONE;
} }
@ -320,17 +364,19 @@ int32_t world_destroy(void) {
librg_world_destroy(world.tracker); librg_world_destroy(world.tracker);
ecs_fini(world.ecs); ecs_fini(world.ecs);
zpl_mfree(world.chunk_mapping); zpl_mfree(world.chunk_mapping);
for (int i = 0; i < zpl_square(world.chunk_amount); i+=1) { for (int i = 0; i < zpl_square(world.chunk_amount); i += 1) {
zpl_mfree(world.block_mapping[i]); zpl_mfree(world.block_mapping[i]);
zpl_mfree(world.outer_block_mapping[i]); zpl_mfree(world.outer_block_mapping[i]);
} }
zpl_mfree(world.block_mapping); zpl_mfree(world.block_mapping);
zpl_mfree(world.outer_block_mapping); zpl_mfree(world.outer_block_mapping);
zpl_mfree(world.islands_count);
zpl_mfree(world.islands);
world_snapshot_destroy(&streamer_snapshot); world_snapshot_destroy(&streamer_snapshot);
world_component_cache_destroy(&component_cache); world_component_cache_destroy(&component_cache);
zpl_memset(&world, 0, sizeof(world)); zpl_memset(&world, 0, sizeof(world));
zpl_printf("[INFO] World was destroyed.\n"); zpl_printf("[INFO] World was destroyed.\n");
return WORLD_ERROR_NONE; return WORLD_ERROR_NONE;
} }
@ -339,47 +385,48 @@ int32_t world_destroy(void) {
static void world_tracker_update(uint8_t ticker, float freq, uint8_t radius) { static void world_tracker_update(uint8_t ticker, float freq, uint8_t radius) {
if (world.tracker_update[ticker] > (float)(get_cached_time())) return; if (world.tracker_update[ticker] > (float)(get_cached_time())) return;
world.tracker_update[ticker] = (float)(get_cached_time()) + freq; world.tracker_update[ticker] = (float)(get_cached_time()) + freq;
profile(PROF_WORLD_WRITE) { profile(PROF_WORLD_WRITE) {
ecs_iter_t it = ecs_query_iter(world_ecs(), world.ecs_update); ecs_iter_t it = ecs_query_iter(world_ecs(), world.ecs_update);
static char buffer[WORLD_LIBRG_BUFSIZ] = {0}; static char buffer[WORLD_LIBRG_BUFSIZ] = { 0 };
world.active_layer_id = ticker; world.active_layer_id = ticker;
while (ecs_query_next(&it)) { while (ecs_query_next(&it)) {
ClientInfo *p = ecs_field(&it, ClientInfo, 1); ClientInfo* p = ecs_field(&it, ClientInfo, 1);
for (int i = 0; i < it.count; i++) { for (int i = 0; i < it.count; i++) {
size_t datalen = WORLD_LIBRG_BUFSIZ; size_t datalen = WORLD_LIBRG_BUFSIZ;
if (!p[i].active) if (!p[i].active)
continue; continue;
int32_t result = librg_world_write(world_tracker(), it.entities[i], radius, buffer, &datalen, NULL); int32_t result = librg_world_write(world_tracker(), it.entities[i], radius, buffer, &datalen, NULL);
if (result > 0) { if (result > 0) {
zpl_printf("[info] buffer size was not enough, please increase it by at least: %d\n", result); zpl_printf("[info] buffer size was not enough, please increase it by at least: %d\n", result);
} else if (result < 0) { }
else if (result < 0) {
zpl_printf("[error] an error happened writing the world %d\n", result); zpl_printf("[error] an error happened writing the world %d\n", result);
} }
pkt_send_librg_update((uint64_t)p[i].peer, p[i].view_id, ticker, buffer, datalen); pkt_send_librg_update((uint64_t)p[i].peer, p[i].view_id, ticker, buffer, datalen);
} }
} }
world_snapshot_clear(&streamer_snapshot); world_snapshot_clear(&streamer_snapshot);
} }
} }
int32_t world_update() { int32_t world_update() {
profile (PROF_UPDATE_SYSTEMS) { profile(PROF_UPDATE_SYSTEMS) {
world_component_cache_clear(&component_cache); world_component_cache_clear(&component_cache);
ecs_progress(world.ecs, 0.0f); ecs_progress(world.ecs, 0.0f);
} }
float fast_ms = WORLD_TRACKER_UPDATE_MP_FAST_MS; float fast_ms = WORLD_TRACKER_UPDATE_MP_FAST_MS;
float normal_ms = WORLD_TRACKER_UPDATE_MP_NORMAL_MS; float normal_ms = WORLD_TRACKER_UPDATE_MP_NORMAL_MS;
float slow_ms = WORLD_TRACKER_UPDATE_MP_SLOW_MS; float slow_ms = WORLD_TRACKER_UPDATE_MP_SLOW_MS;
#if 1 #if 1
if (game_get_kind() == GAMEKIND_SINGLE) { if (game_get_kind() == GAMEKIND_SINGLE) {
fast_ms = WORLD_TRACKER_UPDATE_FAST_MS; fast_ms = WORLD_TRACKER_UPDATE_FAST_MS;
@ -387,31 +434,31 @@ int32_t world_update() {
slow_ms = WORLD_TRACKER_UPDATE_SLOW_MS; slow_ms = WORLD_TRACKER_UPDATE_SLOW_MS;
} }
#endif #endif
world_tracker_update(0, fast_ms, 1); world_tracker_update(0, fast_ms, 1);
world_tracker_update(1, normal_ms, 2); world_tracker_update(1, normal_ms, 2);
world_tracker_update(2, slow_ms, 3); world_tracker_update(2, slow_ms, 3);
entity_update_action_timers(); entity_update_action_timers();
debug_replay_update(); debug_replay_update();
return 0; return 0;
} }
int32_t world_read(void* data, uint32_t datalen, void *udata) { int32_t world_read(void* data, uint32_t datalen, void* udata) {
if (world.reader_proc) { if (world.reader_proc) {
return world.reader_proc(data, datalen, udata); return world.reader_proc(data, datalen, udata);
} }
return -1; return -1;
} }
int32_t world_write(pkt_header *pkt, void *udata) { int32_t world_write(pkt_header* pkt, void* udata) {
if (world.writer_proc) { if (world.writer_proc) {
return world.writer_proc(pkt, udata); return world.writer_proc(pkt, udata);
} }
return -1; return -1;
} }
uint32_t world_buf(block_id const **ptr, uint32_t *width) { uint32_t world_buf(block_id const** ptr, uint32_t* width) {
ZPL_ASSERT_NOT_NULL(world.data); ZPL_ASSERT_NOT_NULL(world.data);
ZPL_ASSERT_NOT_NULL(ptr); ZPL_ASSERT_NOT_NULL(ptr);
*ptr = world.data; *ptr = world.data;
@ -423,26 +470,26 @@ uint32_t world_seed(void) {
return world.seed; return world.seed;
} }
ecs_world_t * world_ecs() { ecs_world_t* world_ecs() {
if (world.ecs_stage != NULL) { if (world.ecs_stage != NULL) {
return world.ecs_stage; return world.ecs_stage;
} }
return world.ecs; return world.ecs;
} }
ecs_query_t *world_ecs_player(void) { ecs_query_t* world_ecs_player(void) {
return world.ecs_update; return world.ecs_update;
} }
ecs_query_t *world_ecs_clientinfo(void) { ecs_query_t* world_ecs_clientinfo(void) {
return world.ecs_clientinfo; return world.ecs_clientinfo;
} }
void world_set_stage(ecs_world_t *ecs) { void world_set_stage(ecs_world_t* ecs) {
world.ecs_stage = ecs; world.ecs_stage = ecs;
} }
librg_world *world_tracker() { librg_world* world_tracker() {
return world.tracker; return world.tracker;
} }
@ -484,36 +531,36 @@ ecs_entity_t world_chunk_mapping(librg_chunk id) {
} }
world_block_lookup world_block_from_realpos(float x, float y) { world_block_lookup world_block_from_realpos(float x, float y) {
x = zpl_clamp(x, 0, world_dim()-1); x = zpl_clamp(x, 0, world_dim() - 1);
y = zpl_clamp(y, 0, world_dim()-1); y = zpl_clamp(y, 0, world_dim() - 1);
librg_chunk chunk_id = librg_chunk_from_realpos(world.tracker, x, y, 0); librg_chunk chunk_id = librg_chunk_from_realpos(world.tracker, x, y, 0);
ecs_entity_t e = world.chunk_mapping[chunk_id]; ecs_entity_t e = world.chunk_mapping[chunk_id];
int32_t size = world.chunk_size * WORLD_BLOCK_SIZE; int32_t size = world.chunk_size * WORLD_BLOCK_SIZE;
int16_t chunk_x, chunk_y; int16_t chunk_x, chunk_y;
librg_chunk_to_chunkpos(world.tracker, chunk_id, &chunk_x, &chunk_y, NULL); librg_chunk_to_chunkpos(world.tracker, chunk_id, &chunk_x, &chunk_y, NULL);
// NOTE(zaklaus): pos relative to chunk // NOTE(zaklaus): pos relative to chunk
float chx = x - chunk_x * size; float chx = x - chunk_x * size;
float chy = y - chunk_y * size; float chy = y - chunk_y * size;
uint16_t bx = (uint16_t)chx / WORLD_BLOCK_SIZE; uint16_t bx = (uint16_t)chx / WORLD_BLOCK_SIZE;
uint16_t by = (uint16_t)chy / WORLD_BLOCK_SIZE; uint16_t by = (uint16_t)chy / WORLD_BLOCK_SIZE;
uint16_t block_idx = (by*world.chunk_size)+bx; uint16_t block_idx = (by * world.chunk_size) + bx;
block_id bid = world.outer_block_mapping[chunk_id][block_idx]; block_id bid = world.outer_block_mapping[chunk_id][block_idx];
bool is_outer = true; bool is_outer = true;
if (bid == 0) { if (bid == 0) {
bid = world.block_mapping[chunk_id][block_idx]; bid = world.block_mapping[chunk_id][block_idx];
is_outer = false; is_outer = false;
} }
// NOTE(zaklaus): pos relative to block's center // NOTE(zaklaus): pos relative to block's center
float box = chx - bx * WORLD_BLOCK_SIZE - WORLD_BLOCK_SIZE/2.0f; float box = chx - bx * WORLD_BLOCK_SIZE - WORLD_BLOCK_SIZE / 2.0f;
float boy = chy - by * WORLD_BLOCK_SIZE - WORLD_BLOCK_SIZE/2.0f; float boy = chy - by * WORLD_BLOCK_SIZE - WORLD_BLOCK_SIZE / 2.0f;
// NOTE(zaklaus): absolute pos in world. // NOTE(zaklaus): absolute pos in world.
float abox = (uint16_t)(x / WORLD_BLOCK_SIZE) * (float)WORLD_BLOCK_SIZE + WORLD_BLOCK_SIZE/2.0f; float abox = (uint16_t)(x / WORLD_BLOCK_SIZE) * (float)WORLD_BLOCK_SIZE + WORLD_BLOCK_SIZE / 2.0f;
float aboy = (uint16_t)(y / WORLD_BLOCK_SIZE) * (float)WORLD_BLOCK_SIZE + WORLD_BLOCK_SIZE/2.0f; float aboy = (uint16_t)(y / WORLD_BLOCK_SIZE) * (float)WORLD_BLOCK_SIZE + WORLD_BLOCK_SIZE / 2.0f;
world_block_lookup lookup = { world_block_lookup lookup = {
.id = block_idx, .id = block_idx,
.bid = bid, .bid = bid,
@ -525,7 +572,7 @@ world_block_lookup world_block_from_realpos(float x, float y) {
.aoy = aboy, .aoy = aboy,
.is_outer = is_outer, .is_outer = is_outer,
}; };
return lookup; return lookup;
} }
@ -533,13 +580,13 @@ void world_chunk_destroy_block(float x, float y, bool drop_item) {
world_block_lookup l = world_block_from_realpos(x, y); world_block_lookup l = world_block_from_realpos(x, y);
if (blocks_get_flags(l.bid) & BLOCK_FLAG_ESSENTIAL) return; if (blocks_get_flags(l.bid) & BLOCK_FLAG_ESSENTIAL) return;
world_chunk_replace_block(l.chunk_id, l.id, 0); world_chunk_replace_block(l.chunk_id, l.id, 0);
if (l.is_outer && l.bid > 0 && drop_item) { if (l.is_outer && l.bid > 0 && drop_item) {
asset_id item_asset = blocks_get_asset(l.bid); asset_id item_asset = blocks_get_asset(l.bid);
if (item_find(item_asset) == ASSET_INVALID) return; if (item_find(item_asset) == ASSET_INVALID) return;
uint64_t e = item_spawn(item_asset, 1); uint64_t e = item_spawn(item_asset, 1);
Position *dest = ecs_get_mut(world_ecs(), e, Position); Position* dest = ecs_get_mut(world_ecs(), e, Position);
dest->x = x; dest->x = x;
dest->y = y; dest->y = y;
entity_set_position(e, dest->x, dest->y); entity_set_position(e, dest->x, dest->y);
@ -551,20 +598,20 @@ world_block_lookup world_block_from_index(int64_t id, uint16_t block_idx) {
if (bid == 0) { if (bid == 0) {
bid = world.block_mapping[id][block_idx]; bid = world.block_mapping[id][block_idx];
} }
int32_t size = world.chunk_size * WORLD_BLOCK_SIZE; int32_t size = world.chunk_size * WORLD_BLOCK_SIZE;
int16_t chunk_x, chunk_y; int16_t chunk_x, chunk_y;
librg_chunk_to_chunkpos(world.tracker, id, &chunk_x, &chunk_y, NULL); librg_chunk_to_chunkpos(world.tracker, id, &chunk_x, &chunk_y, NULL);
float chx = (float)chunk_x * size; float chx = (float)chunk_x * size;
float chy = (float)chunk_y * size; float chy = (float)chunk_y * size;
float bx = (float)(block_idx % world.chunk_size) * WORLD_BLOCK_SIZE; float bx = (float)(block_idx % world.chunk_size) * WORLD_BLOCK_SIZE;
float by = (float)(block_idx / world.chunk_size) * WORLD_BLOCK_SIZE; float by = (float)(block_idx / world.chunk_size) * WORLD_BLOCK_SIZE;
float box = chx + bx + WORLD_BLOCK_SIZE/2.0f; float box = chx + bx + WORLD_BLOCK_SIZE / 2.0f;
float boy = chy + by + WORLD_BLOCK_SIZE/2.0f; float boy = chy + by + WORLD_BLOCK_SIZE / 2.0f;
world_block_lookup lookup = { world_block_lookup lookup = {
.id = block_idx, .id = block_idx,
.bid = bid, .bid = bid,
@ -573,7 +620,7 @@ world_block_lookup world_block_from_index(int64_t id, uint16_t block_idx) {
.oy = boy, .oy = boy,
.chunk_e = world.chunk_mapping[id], .chunk_e = world.chunk_mapping[id],
}; };
return lookup; return lookup;
} }
@ -582,6 +629,11 @@ int64_t world_chunk_from_realpos(float x, float y) {
return world.chunk_mapping[chunk_id]; return world.chunk_mapping[chunk_id];
} }
uint8_t world_chunk_collision_islands(librg_chunk id, collision_island* islands) {
zpl_memcopy(islands, world.islands + id * 16, sizeof(collision_island) * world.islands_count[id]);
return world.islands_count[id];
}
int64_t world_chunk_from_entity(ecs_entity_t id) { int64_t world_chunk_from_entity(ecs_entity_t id) {
return librg_entity_chunk_get(world.tracker, id); return librg_entity_chunk_get(world.tracker, id);
} }
@ -599,7 +651,8 @@ void world_chunk_replace_block(int64_t id, uint16_t block_idx, block_id bid) {
ecs_entity_t e = entity_spawn_id(blocks_get_asset(bid)); ecs_entity_t e = entity_spawn_id(blocks_get_asset(bid));
world_block_lookup l = world_block_from_index(id, block_idx); world_block_lookup l = world_block_from_index(id, block_idx);
entity_set_position(e, l.ox, l.oy); entity_set_position(e, l.ox, l.oy);
} else { }
else {
world.outer_block_mapping[id][block_idx] = bid; world.outer_block_mapping[id][block_idx] = bid;
world_chunk_mark_dirty(world.chunk_mapping[id]); world_chunk_mark_dirty(world.chunk_mapping[id]);
} }
@ -612,31 +665,32 @@ bool world_chunk_place_block(int64_t id, uint16_t block_idx, block_id bid) {
ecs_entity_t e = entity_spawn_id(blocks_get_asset(bid)); ecs_entity_t e = entity_spawn_id(blocks_get_asset(bid));
world_block_lookup l = world_block_from_index(id, block_idx); world_block_lookup l = world_block_from_index(id, block_idx);
entity_set_position(e, l.ox, l.oy); entity_set_position(e, l.ox, l.oy);
} else { }
else {
world.outer_block_mapping[id][block_idx] = bid; world.outer_block_mapping[id][block_idx] = bid;
world_chunk_mark_dirty(world.chunk_mapping[id]); world_chunk_mark_dirty(world.chunk_mapping[id]);
} }
return true; return true;
} }
block_id *world_chunk_get_blocks(int64_t id) { block_id* world_chunk_get_blocks(int64_t id) {
return world.block_mapping[id]; return world.block_mapping[id];
} }
void world_chunk_mark_dirty(ecs_entity_t e) { void world_chunk_mark_dirty(ecs_entity_t e) {
bool was_added=false; bool was_added = false;
Chunk *chunk = ecs_get_mut(world_ecs(), e, Chunk); Chunk* chunk = ecs_get_mut(world_ecs(), e, Chunk);
if (chunk) chunk->is_dirty = true; if (chunk) chunk->is_dirty = true;
} }
bool world_chunk_is_dirty(ecs_entity_t e) { bool world_chunk_is_dirty(ecs_entity_t e) {
bool was_added=false; bool was_added = false;
Chunk *chunk = ecs_get_mut(world_ecs(), e, Chunk); Chunk* chunk = ecs_get_mut(world_ecs(), e, Chunk);
if (chunk) return chunk->is_dirty; if (chunk) return chunk->is_dirty;
return false; return false;
} }
int64_t *world_chunk_fetch_entities(librg_chunk chunk_id, size_t *ents_len) { int64_t* world_chunk_fetch_entities(librg_chunk chunk_id, size_t* ents_len) {
ZPL_ASSERT_NOT_NULL(ents_len); ZPL_ASSERT_NOT_NULL(ents_len);
static int64_t ents[UINT16_MAX]; static int64_t ents[UINT16_MAX];
*ents_len = UINT16_MAX; *ents_len = UINT16_MAX;
@ -644,11 +698,11 @@ int64_t *world_chunk_fetch_entities(librg_chunk chunk_id, size_t *ents_len) {
return ents; return ents;
} }
int64_t *world_chunk_fetch_entities_realpos(float x, float y, size_t *ents_len) { int64_t* world_chunk_fetch_entities_realpos(float x, float y, size_t* ents_len) {
return world_chunk_fetch_entities(librg_chunk_from_realpos(world.tracker, x, y, 0), ents_len); return world_chunk_fetch_entities(librg_chunk_from_realpos(world.tracker, x, y, 0), ents_len);
} }
int64_t *world_chunk_query_entities(int64_t e, size_t *ents_len, int8_t radius) { int64_t* world_chunk_query_entities(int64_t e, size_t* ents_len, int8_t radius) {
ZPL_ASSERT_NOT_NULL(ents_len); ZPL_ASSERT_NOT_NULL(ents_len);
static int64_t ents[UINT16_MAX]; static int64_t ents[UINT16_MAX];
*ents_len = UINT16_MAX; *ents_len = UINT16_MAX;
@ -661,22 +715,22 @@ bool world_entity_valid(ecs_entity_t e) {
return ecs_is_alive(world_ecs(), e); return ecs_is_alive(world_ecs(), e);
} }
void * world_component_cached(ecs_world_t *world_ecs, ecs_entity_t entity, ecs_id_t id) { void* world_component_cached(ecs_world_t* world_ecs, ecs_entity_t entity, ecs_id_t id) {
if (!component_cache.entries || world.ecs_stage == NULL) { if (!component_cache.entries || world.ecs_stage == NULL) {
return ecs_get_mut_id(world_ecs, entity, id); return ecs_get_mut_id(world_ecs, entity, id);
} }
static char buffer[256] = {0}; static char buffer[256] = { 0 };
zpl_snprintf(buffer, 256, "%llu_%llu", entity, id); zpl_snprintf(buffer, 256, "%llu_%llu", entity, id);
uint64_t uid = zpl_crc64(buffer, zpl_strlen(buffer)); uint64_t uid = zpl_crc64(buffer, zpl_strlen(buffer));
zpl_uintptr *value = world_component_cache_get(&component_cache, uid); zpl_uintptr* value = world_component_cache_get(&component_cache, uid);
if (!value) { if (!value) {
void *the_value = ecs_get_mut_id(world_ecs, entity, id); void* the_value = ecs_get_mut_id(world_ecs, entity, id);
world_component_cache_set(&component_cache, uid, (zpl_uintptr)the_value); world_component_cache_set(&component_cache, uid, (zpl_uintptr)the_value);
value = world_component_cache_get(&component_cache, uid); value = world_component_cache_get(&component_cache, uid);
} }
return (void *)*value; return (void*)*value;
} }

View File

@ -29,6 +29,11 @@ typedef WORLD_PKT_READER(world_pkt_reader_proc);
#define WORLD_PKT_WRITER(name) int32_t name(pkt_header *pkt, void *udata) #define WORLD_PKT_WRITER(name) int32_t name(pkt_header *pkt, void *udata)
typedef WORLD_PKT_WRITER(world_pkt_writer_proc); typedef WORLD_PKT_WRITER(world_pkt_writer_proc);
typedef struct {
float minx, miny;
float maxx, maxy;
} collision_island;
typedef struct { typedef struct {
bool is_paused; bool is_paused;
block_id *data; block_id *data;
@ -40,6 +45,8 @@ typedef struct {
block_id **block_mapping; block_id **block_mapping;
block_id **outer_block_mapping; block_id **outer_block_mapping;
uint16_t dim; uint16_t dim;
uint8_t *islands_count;
collision_island *islands;
float tracker_update[3]; float tracker_update[3];
uint8_t active_layer_id; uint8_t active_layer_id;
ecs_world_t *ecs; ecs_world_t *ecs;
@ -78,6 +85,8 @@ uint16_t world_chunk_size(void);
uint16_t world_chunk_amount(void); uint16_t world_chunk_amount(void);
uint16_t world_dim(void); uint16_t world_dim(void);
ecs_entity_t world_chunk_mapping(librg_chunk id); ecs_entity_t world_chunk_mapping(librg_chunk id);
void world_rebuild_chunk_islands(librg_chunk chunk_id);
uint8_t world_chunk_collision_islands(librg_chunk id, collision_island *islands);
typedef struct { typedef struct {
uint16_t id; uint16_t id;

View File

@ -80,3 +80,7 @@ int main(int argc, char** argv) {
zpl_opts_free(&opts); zpl_opts_free(&opts);
return 0; return 0;
} }
//------------------------------------------------------------------------
void game_player_joined(uint64_t ent) {}
void game_player_departed(uint64_t ent) {}

View File

@ -84,3 +84,7 @@ int main(int argc, char** argv) {
zpl_opts_free(&opts); zpl_opts_free(&opts);
return 0; return 0;
} }
//------------------------------------------------------------------------
void game_player_joined(uint64_t ent) {}
void game_player_departed(uint64_t ent) {}

View File

@ -16,6 +16,10 @@
#include "platform/arch.h" #include "platform/arch.h"
ZPL_DIAGNOSTIC_PUSH_WARNLEVEL(0)
#include "tinyc2.h"
ZPL_DIAGNOSTIC_POP
#define DEFAULT_WORLD_SEED 302097 #define DEFAULT_WORLD_SEED 302097
#define DEFAULT_CHUNK_SIZE 16 /* amount of blocks within a chunk (single axis) */ #define DEFAULT_CHUNK_SIZE 16 /* amount of blocks within a chunk (single axis) */
#define DEFAULT_WORLD_SIZE 5 /* amount of chunks within a world (single axis) */ #define DEFAULT_WORLD_SIZE 5 /* amount of chunks within a world (single axis) */
@ -33,6 +37,15 @@
- somewhat believable world gen, small hamlets with cols, etc - somewhat believable world gen, small hamlets with cols, etc
*/ */
#include "system_mob.c"
void mob_systems(ecs_world_t *ecs) {
ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob);
ECS_SYSTEM(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer);
ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee);
ECS_OBSERVER(ecs, MobDetectPlayers1, EcsOnAdd, components.Mob);
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
zpl_opts opts={0}; zpl_opts opts={0};
zpl_opts_init(&opts, zpl_heap(), argv[0]); zpl_opts_init(&opts, zpl_heap(), argv[0]);
@ -84,6 +97,10 @@ int main(int argc, char** argv) {
sighandler_register(); sighandler_register();
game_init(host, port, play_mode, 1, seed, chunk_size, world_size, 0); game_init(host, port, play_mode, 1, seed, chunk_size, world_size, 0);
{
mob_systems(world_ecs());
}
game_run(); game_run();
game_shutdown(); game_shutdown();
@ -93,3 +110,7 @@ int main(int argc, char** argv) {
zpl_opts_free(&opts); zpl_opts_free(&opts);
return 0; return 0;
} }
//------------------------------------------------------------------------
void game_player_joined(uint64_t ent) {}
void game_player_departed(uint64_t ent) {}

View File

@ -88,18 +88,19 @@ void renderer_draw_entry(uint64_t key, entity_view *data, game_world_render_entr
float x = data->x; float x = data->x;
float y = data->y; float y = data->y;
float health = (data->hp / data->max_hp); float health = (data->hp / data->max_hp);
DrawNametag("Player", key, data, x, y); DrawNametag("Player", key, data, x, y-16);
DrawCircleEco(x, y, size, ColorAlpha(YELLOW, data->tran_time)); DrawTextureRec(GetSpriteTexture2D(assets_find(ASSET_PLAYER)), ASSET_SRC_RECT(), (Vector2){data->x-(WORLD_BLOCK_SIZE/2), data->y-(WORLD_BLOCK_SIZE/2)}, ColorAlpha(WHITE, data->tran_time));
//DrawCircleEco(x, y, size, ColorAlpha(YELLOW, data->tran_time));
if (data->has_items && !data->inside_vehicle) { //if (data->has_items && !data->inside_vehicle) {
float ix = data->x; // float ix = data->x;
float iy = data->y; // float iy = data->y;
if (data->items[data->selected_item].quantity > 0) { // if (data->items[data->selected_item].quantity > 0) {
asset_id it_kind = data->items[data->selected_item].kind; // asset_id it_kind = data->items[data->selected_item].kind;
uint32_t qty = data->items[data->selected_item].quantity; // uint32_t qty = data->items[data->selected_item].quantity;
DrawTexturePro(GetSpriteTexture2D(assets_find(it_kind)), ASSET_SRC_RECT(), ((Rectangle){ix, iy, 32, 32}), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE)); // DrawTexturePro(GetSpriteTexture2D(assets_find(it_kind)), ASSET_SRC_RECT(), ((Rectangle){ix, iy, 32, 32}), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE));
} // }
} //}
}break; }break;
case EKIND_ITEM: { case EKIND_ITEM: {
float x = data->x - 32.f; float x = data->x - 32.f;

View File

@ -27,6 +27,10 @@ void MobDetectPlayers(ecs_iter_t *it) {
} }
} }
void MobDetectPlayers1(ecs_iter_t *it) {
MobDetectPlayers(it);
}
#define MOB_MOVEMENT_SPEED 300.0f #define MOB_MOVEMENT_SPEED 300.0f
void MobMovement(ecs_iter_t *it) { void MobMovement(ecs_iter_t *it) {
@ -50,7 +54,7 @@ void MobMovement(ecs_iter_t *it) {
} }
#define MOB_MELEE_DIST 4000.0f #define MOB_MELEE_DIST 4000.0f
#define MOB_MELEE_DMG 5.0f #define MOB_MELEE_DMG 1.5f
#define MOB_ATK_DELAY 10 #define MOB_ATK_DELAY 10
void MobMeleeAtk(ecs_iter_t *it) { void MobMeleeAtk(ecs_iter_t *it) {
@ -73,7 +77,7 @@ void MobMeleeAtk(ecs_iter_t *it) {
Health *hp = ecs_get_mut_ex(it->world, m->plr, Health); Health *hp = ecs_get_mut_ex(it->world, m->plr, Health);
hp->hp = zpl_max(hp->hp-MOB_MELEE_DMG, 0.0f); hp->hp = zpl_max(hp->hp-MOB_MELEE_DMG, 0.0f);
ecs_add(it->world, m->plr, HealthDecreased); ecs_add(it->world, m->plr, HealthDecreased);
mob[i].atk_delay = MOB_ATK_DELAY;
} }
mob[i].atk_delay = MOB_ATK_DELAY;
} }
} }

View File

@ -12,6 +12,7 @@ Texture2D texgen_build_anim(asset_id id, int64_t counter) {
Texture2D texgen_build_sprite(asset_id id) { Texture2D texgen_build_sprite(asset_id id) {
switch (id) { switch (id) {
case ASSET_PLAYER: return LoadTexEco("player");
default: return texgen_build_sprite_fallback(id); break; default: return texgen_build_sprite_fallback(id); break;
} }
} }