From ac6b44eb0ab844d280748e81ec69b2577489d834 Mon Sep 17 00:00:00 2001 From: Vladyslav Hrytsenko Date: Sun, 12 Mar 2023 10:52:44 +0200 Subject: [PATCH] updated draft --- code/games/survival/src/main.c | 432 ++++++++++++++++++--------------- foundation.md | 20 +- 2 files changed, 246 insertions(+), 206 deletions(-) diff --git a/code/games/survival/src/main.c b/code/games/survival/src/main.c index e757a6b..7a7b901 100644 --- a/code/games/survival/src/main.c +++ b/code/games/survival/src/main.c @@ -9,23 +9,101 @@ typedef int efd_app; typedef uint16_t efd_entity_type; typedef int32_t efd_result; -typedef uint64_t efd_entity; - -typedef enum efd_asset_type { - // EFD_ASSET_NONE = 0, - EFD_ASSET_TEXTURE, - EFD_ASSET_ANIMATION, - EFD_ASSET_SOUND, - EFD_ASSET_FONT, - EFD_ASSET_SHADER, - EFD_ASSET_COUNT_TYPES, -} efd_asset_type; +typedef void * efd_world; // define amount of memeory reserved for??? // custom data within COMMAND (cli->ser) and SNAPSHOT (ser->cli) #define EFD_COMMAND_SIZE 64 * 1024 #define EFD_SNAPSHOT_SIZE 128 * 1024 +#define EFD_TYPE_SHIFT 16 +#define EFD_ID(type) (((type) << EFD_TYPE_SHIFT) + 1) +#define EFD_TYPE(id) ((id - 1) >> EFD_TYPE_SHIFT) +#define EFD_END 0 + +typedef enum efd_asset_type { + EFD_NONE = 0, + EFD_TEXTURE, + EFD_TEXTURE_ATLAS, + EFD_ANIMATION, + EFD_SOUND, + EFD_FONT, + EFD_SHADER, +} efd_asset_type; + +typedef enum efd_object_type { + EFD_TYPE_NONE, + EFD_TYPE_TILE, + EFD_TYPE_BLOCK, + EFD_TYPE_ENTITY, + EFD_TYPE_ITEM, + EFD_TYPE_CRAFT, + EFD_TYPE_CHUNK, + + EFD_TYPE_TYPE_LAST, + EFD_TYPE_TYPE_MAX = 0xFFFF, +} efd_object_type; + +typedef enum efd_flags { + EFD_FLAG_COLLISION = (1 << 1), + EFD_FLAG_HAZARD = (1 << 2), + EFD_FLAG_ESSENTIAL = (1 << 3), + EFD_FLAG_DESTROY_ON_COLLISION = (1 << 4), + EFD_FLAG_AI = (1 << 5), + EFD_FLAG_PLAYER = (1 << 6), + EFD_FLAG_VEHICLE = (1 << 7), +} efd_flags; + +typedef struct efd_asset { + efd_asset_type type; + int id; + const char *path; + void *data; /* TODO: make a union */ +} efd_asset; + +typedef struct efd_tile { + int id; + int flags; + float drag; + float friction; +} efd_tile; + +typedef struct efd_block { + int id; + int flags; + float bounce; + int velx; + int vely; +} efd_block; + +typedef struct efd_entity { + int id; + int flags; +} efd_entity; + +typedef struct efd_item { + int id; + const char *t1; + const char *t2; +} efd_item; + +typedef struct efd_craft_item { + int id; + int qty; +} efd_craft_item; + +typedef struct efd_craft { + int producer; + int ticks; + efd_craft_item *input; + efd_craft_item *output; +} efd_craft; + +typedef struct efd_tooltip { + const char *title; + const char *description; +} efd_tooltip; + typedef struct efd_app_desc { const char *name; int debug_ui; @@ -42,6 +120,7 @@ typedef struct efd_app_desc { int world_seed_random; efd_result (*init_cb)(); + efd_result (*system_cb)(); efd_result (*update_cb)(); efd_result (*render_cb)(efd_entity_type type); efd_result (*player_join_cb)(efd_entity player_id); @@ -55,105 +134,66 @@ typedef struct efd_app_desc { float item_attract_radius; float item_attract_force; } rules; + + efd_asset *assets; + efd_tile *tiles; + efd_block *blocks; + efd_entity *entities; + efd_item *items; + efd_craft *crafting; + efd_tooltip *tooltips; } efd_app_desc; -// assets - -// #define EFD_ASSET_TEXTURE_LAST 15 -// #define EFD_ASSET_ANIMATION_LAST 2222222 -// #define EFD_ASSET_ANIMATION_LAST 2222222 - -// #define CONC(a, b) a##b -/* + CONC(type, _LAST) */ - -#define EFD_ASSET_SHIFT 16 -#define EFD_ASSET(type) ((type) << EFD_ASSET_SHIFT) -#define EFD_ASSET_TYPE(id) ((id) >> EFD_ASSET_SHIFT) - -typedef struct efd_asset { - int id; - const char *path; - void *data; /* TODO: make a union */ -} efd_asset; - -efd_result efd_asset_add(int id, const char *path); -efd_result efd_asset_remove(int id); -efd_asset *efd_asset_get(int id); - -// notifications - -efd_result efd_notify_push(efd_entity actor, const char *title, const char *text, float duration); -efd_result efd_notify_clear(efd_entity actor); - -// tooltips - -efd_result efd_tooltip_add(const char *name, const char *text); -efd_result efd_tooltip_remove(const char *name); - -// entities - -enum efd_entity_type_builtins { - EFD_ENTITY_NONE = 0, - EFD_ENTITY_PLAYER = 1, - EFD_ENTITY_CHUNK, - EFD_ENTITY_OBJECT, - EFD_ENTITY_ITEM, - EFD_ENTITY_NPC, - EFD_ENTITY_VEHICLE, - - EFD_ENTITY_TYPE_LAST, - EFD_ENTITY_TYPE_MAX = 0xFFFF, -}; - /// app.c enum { /* textures */ - ASSET_TILE_DIRT = EFD_ASSET(EFD_ASSET_TEXTURE), - ASSET_TILE_GRASS, - ASSET_TILE_STONE, + TILE_AIR = EFD_ID(EFD_TYPE_TILE), + TILE_DIRT, + TILE_GRASS, + TILE_STONE, + TILE_WATER, + TILE_LAVA, - ASSET_BLOCK_STONE, - ASSET_BLOCK_BRICK, + BLOCK_STONE = EFD_ID(EFD_TYPE_BLOCK), + BLOCK_BRICK, + BLOCK_HILL, + BLOCK_HILL_SNOW, + BLOCK_FENCE, + BLOCK_WOOD, + BLOCK_WALL, + BLOCK_BELT_LEFT, + BLOCK_BELT_RIGHT, + BLOCK_BELT_UP, + BLOCK_BELT_DOWN, - ASSET_PLAYER, - ASSET_TREE, - ASSET_CHEST, - ASSET_MONSTER, + ENTITY_PLAYER = EFD_ID(EFD_TYPE_ENTITY), + ENTITY_MONSTER, + ENTITY_TRUCK, + ENTITY_TREE, + ENTITY_CHEST, + ENTITY_FURNACE, + ENTITY_CRAFTBENCH, + ENTITY_ASSEMBLER, - ASSET_ITEM_WOOD, - ASSET_ITEM_STONE, - ASSET_ITEM_IRON, - - /* animations */ - ASSET_PLAYER_ANIM = EFD_ASSET(EFD_ASSET_ANIMATION), - - /* sounds */ - ASSET_PLAYER_SOUND = EFD_ASSET(EFD_ASSET_SOUND), - ASSET_TREE_SOUND, - ASSET_CHEST_SOUND, - - /* fonts */ - ASSET_FONT_DEFAULT = EFD_ASSET(EFD_ASSET_FONT), + ITEM_IRON_ORE = EFD_ID(EFD_TYPE_ITEM), + ITEM_IRON_INGOT, + ITEM_IRON_PLATE, + ITEM_SCREW, + ITEM_BELT, }; -enum { - ENTITY_MONSTER = EFD_ENTITY_TYPE_LAST, - ENTITY_WEAPON, -}; - -void Move(ecs_iter_t *it) { - // Get fields from system query - Position *p = ecs_field(it, Position, 1); - Velocity *v = ecs_field(it, Velocity, 2); - - // Iterate matched entities - for (int i = 0; i < it->count, i++) { - p[i].x += v[i].x; - p[i].y += v[i].y; - } -} +// void Move(ecs_iter_t *it) { +// // Get fields from system query +// Position *p = ecs_field(it, Position, 1); +// Velocity *v = ecs_field(it, Velocity, 2); +// // Iterate matched entities +// for (int i = 0; i < it->count, i++) { +// p[i].x += v[i].x; +// p[i].y += v[i].y; +// } +// } efd_result init() { // NOTE: control sets could be implemented later on @@ -177,7 +217,7 @@ efd_result init() { } efd_result systems(efd_world *w) { - ECS_SYSTEM(w, Move, EcsOnUpdate, Position, [in] Velocity); + // ECS_SYSTEM(w, Move, EcsOnUpdate, Position, [in] Velocity); return 0; } @@ -187,29 +227,29 @@ efd_result update() { } efd_result render(efd_entity_type type) { - switch (type) { - case EFD_ENTITY_PLAYER: - /* additional/replacable rendering code on top of what EFD provides for built-in types */ - efd_render_texture(ASSET_PLAYER, 0, 0, 0, 0, 0, 0, 0, 0); + // switch (type) { + // case EFD_ENTITY_PLAYER: + // /* additional/replacable rendering code on top of what EFD provides for built-in types */ + // efd_render_texture(ENTITY_PLAYER, 0, 0, 0, 0, 0, 0, 0, 0); - return 1; /* we handled the rendering, don't render with the default renderer */ - break; + // return 1; /* we handled the rendering, don't render with the default renderer */ + // break; - case ENTITY_MONSTER: - /* our custom rendering code for monster */ - efd_render_texture(ASSET_MONSTER, 0, 0, 0, 0, 0, 0, 0, 0); - break; + // case ENTITY_MONSTER: + // /* our custom rendering code for monster */ + // efd_render_texture(ENTITY_MONSTER, 0, 0, 0, 0, 0, 0, 0, 0); + // break; - case ENTITY_WEAPON: - /* our custom rendering code for weapon */ - break; - } + // case ENTITY_WEAPON: + // /* our custom rendering code for weapon */ + // break; + // } return 0; } efd_result player_join(efd_entity player) { - efd_notify_push(player, "Test", "Welcome to the game!", 5.0f); + // efd_notify_push(player, "Test", "Welcome to the game!", 5.0f); return 0; } @@ -256,91 +296,107 @@ efd_app_desc efd_main() { .item_attract_force = 0.1f, }, - .assets = { - {ASSET_PLAYER, "assets/player.png"}, - {ASSET_MONSTER, "assets/monster.png"}, + .assets = (efd_asset[]){ + {EFD_TEXTURE, ENTITY_TREE, "assets/tree.png"}, + {EFD_TEXTURE, ENTITY_CHEST, "assets/chest.png"}, + {EFD_TEXTURE, ENTITY_FURNACE, "assets/furnace.png"}, - {ASSET_ITEM_WOOD, "assets/item_wood.png"}, - {ASSET_ITEM_STONE, "assets/item_stone.png"}, - {ASSET_ITEM_IRON, "assets/item_iron.png"}, + {EFD_TEXTURE, ENTITY_PLAYER, "assets/player.png"}, + {EFD_TEXTURE, ENTITY_MONSTER, "assets/monster.png"}, - {ASSET_TREE, "assets/tree.png"}, - {ASSET_CHEST, "assets/chest.png"}, + {EFD_END}, }, - .tiles = { - {ASSET_EMPTY, EFD_FLAG_NONE, 'E'}, - {ASSET_GROUND, EFD_FLAG_NONE, '.', .drag = 1.0f, .friction = 1.0f}, - {ASSET_DIRT, EFD_FLAG_NONE, ',', .drag = 2.1f , .friction = 1.0f}, - {ASSET_WATER, EFD_FLAG_NONE, '~', .drag = 0.11f , .friction = 10.0f}, - {ASSET_LAVA, EFD_FLAG_HAZARD, '!', .drag = 6.2f , .friction = 40.0f}, + .tiles = (efd_tile[]){ + {TILE_AIR}, + {TILE_DIRT, .drag = 1.0f, .friction = 1.0f}, + {TILE_GRASS, .drag = 1.0f, .friction = 1.0f}, + {TILE_WATER, .drag = 0.11f , .friction = 10.0f}, + {TILE_LAVA, EFD_FLAG_HAZARD, .drag = 6.2f , .friction = 40.0f}, + + {EFD_END}, }, - .blocks = { - {ASSET_WALL, EFD_FLAG_COLLISION, '#', .bounce = 1.0f}, - {ASSET_HILL, EFD_FLAG_COLLISION, '^'}, - {ASSET_HILL_SNOW, EFD_FLAG_COLLISION, '*'}, - {ASSET_FENCE, EFD_FLAG_COLLISION, '[', .bounce = 1.0f}, - {ASSET_WOOD, EFD_FLAG_COLLISION, ']', .bounce = 0.0f}, - {ASSET_TREE, EFD_FLAG_COLLISION|EFD_FLAG_DESTROY_ON_COLLISION, '@', .bounce = 0.0f}, - {ASSET_CHEST, EFD_FLAG_ENTITY, 'C'}, - {ASSET_FURNACE, EFD_FLAG_ENTITY, 'F'}, - {ASSET_TEST_TALL, EFD_FLAG_COLLISION, '.'}, + .blocks = (efd_block[]){ + {BLOCK_WALL, EFD_FLAG_COLLISION, .bounce = 1.0f}, + {BLOCK_HILL, EFD_FLAG_COLLISION}, + {BLOCK_HILL_SNOW, EFD_FLAG_COLLISION}, + {BLOCK_FENCE, EFD_FLAG_COLLISION, .bounce = 1.0f}, + {BLOCK_WOOD, EFD_FLAG_COLLISION, .bounce = 0.0f}, - {ASSET_BELT_LEFT, EFD_FLAG_NONE, '@', .velx = -150.0f}, - {ASSET_BELT_RIGHT, EFD_FLAG_NONE, '@', .velx = 150.0f}, - {ASSET_BELT_UP, EFD_FLAG_NONE, '@', .vely = -150.0f}, - {ASSET_BELT_DOWN, EFD_FLAG_NONE, '@', .vely = 150.0f}, + {BLOCK_BELT_LEFT, .velx = -150.0f}, + {BLOCK_BELT_RIGHT, .velx = 150.0f}, + {BLOCK_BELT_UP, .vely = -150.0f}, + {BLOCK_BELT_DOWN, .vely = 150.0f}, + + {EFD_END}, }, - .items = { - {ITEM_WOOD, ASSET_ITEM_WOOD, "Wood", "A piece of wood."}, - {ITEM_STONE, ASSET_ITEM_STONE, "Stone", "A piece of stone."}, + .entities = (efd_entity[]){ + {ENTITY_TREE, EFD_FLAG_DESTROY_ON_COLLISION}, + {ENTITY_CHEST}, + {ENTITY_FURNACE}, + + {ENTITY_PLAYER, EFD_FLAG_PLAYER}, + {ENTITY_TRUCK, EFD_FLAG_VEHICLE}, + {ENTITY_MONSTER, EFD_FLAG_AI}, + + {EFD_END}, }, - .crafting = { + .items = (efd_item[]){ + // {ITEM_WOOD, "Wood", "A piece of wood."}, + // {ITEM_STONE, "Stone", "A piece of stone."}, + {EFD_END}, + }, + + .crafting = (efd_craft[]){ { - .producer = ASSET_FURNACE, - .process_ticks = 20, - .reagents = { - { .item = ASSET_IRON_ORE, .qty = 1 }, + .producer = ENTITY_FURNACE, + .ticks = 20, + .input = (efd_craft_item[]){ + {ITEM_IRON_ORE, 1}, + {EFD_END}, }, - .products = { - { .item = ASSET_IRON_PLATES, .qty = 4 }, + .output = (efd_craft_item[]){ + {ITEM_IRON_PLATE, 4}, + {EFD_END}, }, }, { - .producer = ASSET_CRAFTBENCH, - .process_ticks = 40, - .reagents = { - { .item = ASSET_IRON_PLATES, .qty = 1 }, + .producer = ENTITY_CRAFTBENCH, + .ticks = 40, + .input = (efd_craft_item[]){ + {ITEM_IRON_PLATE, 1}, + {EFD_END}, }, - .products = { - { .item = ASSET_SCREWS, .qty = 8 }, + .output = (efd_craft_item[]){ + {ITEM_SCREW, 8}, + {EFD_END}, }, }, { - .producer = ASSET_ASSEMBLER, - .production_ticks = 120, - .reagents = { - { .item = ASSET_FENCE, .qty = 1 }, - { .item = ASSET_SCREWS, .qty = 4 }, - { .item = ASSET_IRON_PLATES, .qty = 2 }, + .producer = ENTITY_ASSEMBLER, + .ticks = 120, + .input = (efd_craft_item[]){ + {BLOCK_FENCE, 1}, + {ITEM_SCREW, 4}, + {ITEM_IRON_PLATE, 2}, + {EFD_END}, }, - .products = { - { .item = ASSET_BELT, .qty = 1 }, + .output = (efd_craft_item[]){ + {ITEM_BELT, 1}, + {EFD_END}, }, - } + }, + + {EFD_END}, }, - .entities = { - {ENTITY_TREE, ASSET_TREE, "Tree", "A tree."}, - {ENTITY_CHEST, ASSET_CHEST, "Chest", "A chest."}, - }, - - .tooltips = { - {"ASSET_BLOCK_STONE", "It's a block of stone, what did you expect?"}, - {"ASSET_BLOCK_BRICK", "It's a block of brick, what did you expect?"}, + .tooltips = (efd_tooltip[]){ + {"BLOCK_STONE", "It's a block of stone, what did you expect?"}, + {"BLOCK_BRICK", "It's a block of brick, what did you expect?"}, + {EFD_END}, }, }; } @@ -349,33 +405,15 @@ efd_app_desc efd_main() { #include -efd_result efd_asset_add(int id, const char *path) { - printf("asset_add: %d, %s\n", id, path); - return 0; -} - -efd_result efd_tooltip_add(const char *id, const char *text) { - printf("tooltip_add: %s, %s\n", id, text); - return 0; -} - -efd_result efd_notify_push(efd_entity player, const char *title, const char *text, float duration) { - printf("notify_push: %llu, %s, %s, %f\n", player, title, text, duration); - return 0; -} - void test(efd_entity_type type) { printf("type: %d\n", type); } int main(int argc, char **argv) { efd_app_desc desc = efd_main(); printf("value: %f\n", desc.rules.item_pickup_radius); - printf("ASSET_TILE_DIRT: %d\n", ASSET_TILE_DIRT); - printf("ASSET_TILE_GRASS: %d\n", ASSET_TILE_GRASS); - printf("ASSET_PLAYER: %d\n", ASSET_PLAYER); - printf("ASSET_CHEST: %d\n", ASSET_CHEST); - printf("ASSET_PLAYER_ANIM: %d\n", ASSET_PLAYER_ANIM); - printf("ASSET_PLAYER_SOUND: %d\n", ASSET_PLAYER_SOUND); - printf("ASSET_TREE_SOUND: %d\n", ASSET_TREE_SOUND); + printf("TILE_DIRT: %d\n", TILE_DIRT); + printf("TILE_GRASS: %d\n", TILE_GRASS); + printf("ENTITY_PLAYER: %d\n", ENTITY_PLAYER); + printf("ENTITY_CHEST: %d\n", ENTITY_CHEST); desc.init_cb(); diff --git a/foundation.md b/foundation.md index e4f52cf..fd1dbab 100644 --- a/foundation.md +++ b/foundation.md @@ -41,12 +41,12 @@ * world - a map of chunks within the game world * world-view - a representation of the world recreated by the client ---------- +* chunk - entity that contains set of tiles and blocks * tile - basic thing that makes up the chunks * block - 2nd level of things that make up the chunk -* chunk - entity that contains set of tiles and blocks -* item - an entity in the world, that can have a different state when its picked up -* crafting recipe - a recipe that can be used to craft an item * entity - an grid-independant static or dynamic entity that can exist in the world and has some systems controlling it +* item - an entity in the world, that can have a different state when its picked up +* crafting - a recipe that can be used to craft an item ## Naming @@ -58,12 +58,14 @@ * prefix: efd_ ## Objects -* entity - * objects - * players - * npc - * vehicles - * items + * tile + * block + * entity + * npc + * player + * vehicle + * item + * craft ## Features