From f702065a7c823e699e3d72540d5ee537d4b75aca Mon Sep 17 00:00:00 2001 From: snit Date: Thu, 14 Nov 2024 09:12:23 -0600 Subject: [PATCH] a lot of pointless changes but also a basic tick system --- client/CMakeLists.txt | 2 ++ client/src/render/render.c | 2 +- common/CMakeLists.txt | 4 +++- common/include/world.h | 2 ++ common/src/request.c | 1 - common/src/world.c | 16 +++++++++------- doc/TODO.gmi | 2 ++ scripts/run.sh | 4 ++++ server/CMakeLists.txt | 2 ++ server/src/main.c | 12 +++++------- server/src/request.c | 10 ++++------ server/src/socket.c | 2 +- 12 files changed, 35 insertions(+), 24 deletions(-) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 0a657a1..3b7bd34 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -10,3 +10,5 @@ add_executable(${PROJECT_NAME} ${SOURCES}) target_link_libraries(${PROJECT_NAME} PRIVATE simworld) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/common/include) + +install(TARGETS ${PROJECT_NAME}) diff --git a/client/src/render/render.c b/client/src/render/render.c index 4cd8e42..3440538 100644 --- a/client/src/render/render.c +++ b/client/src/render/render.c @@ -9,7 +9,7 @@ void render_world(struct world_t const *world) { assert(world != NULL); - printf("%zu %zu\n", world->height, world->width); + printf("TICK: %zu\n", world->tick); for (size_t i = 0; i < MAX_ENTITIES; i++) { struct entity_t const *const entity = &world->entities[i]; diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 7b035fa..d59fa79 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -11,8 +11,10 @@ target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/common/inc find_package(PkgConfig REQUIRED) -pkg_check_modules(LUA REQUIRED lua) +pkg_check_modules(LUA REQUIRED lua5.4) pkg_check_modules(JANSSON REQUIRED jansson) target_link_libraries(${PROJECT_NAME} ${LUA_LIBRARIES} ${JANSSON_LIBRARIES}) target_include_directories(${PROJECT_NAME} PRIVATE ${LUA_INCLUDE_DIRS} ${JANSSON_INCLUDE_DIRS}) + +install(TARGETS ${PROJECT_NAME} DESTINATION lib) diff --git a/common/include/world.h b/common/include/world.h index fb313cd..d6365ed 100644 --- a/common/include/world.h +++ b/common/include/world.h @@ -15,6 +15,8 @@ struct world_t { struct entity_t entities[MAX_ENTITIES]; struct entity_registry_t registered_entities; + size_t tick; + size_t height; size_t width; }; diff --git a/common/src/request.c b/common/src/request.c index b33bb33..a0e16d1 100644 --- a/common/src/request.c +++ b/common/src/request.c @@ -4,7 +4,6 @@ #include #include "request.h" -#include "request/get_world_data.h" static char const *REQUEST_JSON_FMT = "{si, so}"; static char const *RESPONSE_JSON_FMT = "{sb, si, so}"; diff --git a/common/src/world.c b/common/src/world.c index 03b6262..083ed85 100644 --- a/common/src/world.c +++ b/common/src/world.c @@ -5,12 +5,14 @@ #include "world.h" -static char const *const WORLD_JSON_FMT = "{so, so, sI, sI}"; +static char const *const WORLD_JSON_FMT = "{so, so, sI, sI, sI}"; enum error_t world_init(struct world_t *self, size_t height, size_t width) { assert(self != NULL); + self->tick = 0; + self->height = height; self->width = width; @@ -84,7 +86,7 @@ static enum error_t world_serialise_entities( static enum error_t world_serialise_parts( struct json_t *entities_json, struct json_t *registered_entities_json, - size_t height, size_t width, + size_t tick, size_t height, size_t width, struct json_t **json ) { assert(entities_json != NULL); @@ -94,7 +96,7 @@ static enum error_t world_serialise_parts( struct json_t *world_json = json_pack(WORLD_JSON_FMT, "entities", entities_json, "registered-entities", registered_entities_json, - "height", height, "width", width + "tick", tick, "height", height, "width", width ); if (world_json == NULL) return ERR_JSON_SERIALISE; @@ -131,7 +133,7 @@ enum error_t world_serialise( err = world_serialise_parts( entities_json, registered_entities_json, - self->height, self->width, + self->tick, self->height, self->width, &world_json ); if (err != ERR_OK) goto error_world; @@ -150,7 +152,7 @@ error: static enum error_t world_deserialise_parts( struct json_t **entities_json, struct json_t **registered_entities_json, - size_t *height, size_t *width, + size_t *tick, size_t *height, size_t *width, struct json_t *json ) { assert(entities_json != NULL); @@ -162,7 +164,7 @@ static enum error_t world_deserialise_parts( int err = json_unpack(json, WORLD_JSON_FMT, "entities", entities_json, "registered-entities", registered_entities_json, - "height", height, "width", width + "tick", tick, "height", height, "width", width ); if (err < 0) return ERR_JSON_DESERIALISE; @@ -230,7 +232,7 @@ enum error_t world_deserialise(struct world_t *self, struct json_t *json) { enum error_t err = world_deserialise_parts( &entities_json, ®istered_entities_json, - &temp_world.height, &temp_world.width, + &temp_world.tick, &temp_world.height, &temp_world.width, json ); if (err != ERR_OK) return err; diff --git a/doc/TODO.gmi b/doc/TODO.gmi index ee985b7..dc98ef6 100644 --- a/doc/TODO.gmi +++ b/doc/TODO.gmi @@ -2,6 +2,7 @@ A list of things I'd like to accomplish ## In Progress +* Tick-based game loop ## Completed * Split game ticking and socket connecting logic (can only tick on connection atm) @@ -32,3 +33,4 @@ A list of things I'd like to accomplish * Client-side resource files for each mod * Real error handling (right now I just pass up to main and immediately exit) * Portability to other UNIX-like operating systems (if changes are needed; I don't care about DOS-likes) +* Reduce repetitiveness of JSON (de)serialisation code diff --git a/scripts/run.sh b/scripts/run.sh index c4b9132..7a3ed48 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -19,4 +19,8 @@ echo "### CLIENT 1 ###" echo "### CLIENT 2 ###" ./build/client/simworld-client +echo "### CLIENT 3 ###" +sleep 1 +./build/client/simworld-client + killall simworld-daemon diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 3bb5250..36e999d 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -9,3 +9,5 @@ file(GLOB_RECURSE SOURCES ${SOURCE_DIR}/*.c) add_executable(${PROJECT_NAME} ${SOURCES}) target_link_libraries(${PROJECT_NAME} PRIVATE simworld) + +install(TARGETS ${PROJECT_NAME}) diff --git a/server/src/main.c b/server/src/main.c index dd77e23..0a89654 100644 --- a/server/src/main.c +++ b/server/src/main.c @@ -24,15 +24,14 @@ static void handle_signal(int signal_no) { } -static enum error_t simulation_thread_body(struct game_data_t *data) { - assert(data != NULL); +static enum error_t simulation_thread_body(struct game_data_t *game) { + assert(game != NULL); // TODO: An actual game loop lol while (true) { - if (pthread_mutex_lock(&data->world_lock) != 0) goto error; - data->world.width = (data->world.width + 1) % 100; - data->world.height = (data->world.height + 1) % 100; - if (pthread_mutex_unlock(&data->world_lock) != 0) goto error; + if (pthread_mutex_lock(&game->world_lock) != 0) goto error; + game->world.tick += 1; + if (pthread_mutex_unlock(&game->world_lock) != 0) goto error; sleep(1); } @@ -44,7 +43,6 @@ error: } - static enum error_t simulation_thread( pthread_t *pthread, struct game_data_t *data diff --git a/server/src/request.c b/server/src/request.c index c5865a6..8ad4f03 100644 --- a/server/src/request.c +++ b/server/src/request.c @@ -5,11 +5,9 @@ #include "request.h" static struct response_t response_failure( - struct request_t const *request + enum request_type_t request ) { - assert(request != NULL); - - return (struct response_t){ request->type, false, NULL }; + return (struct response_t){ request, false, NULL }; } static void handle_request_get_world_data( @@ -41,7 +39,7 @@ static void handle_request_get_world_data( error: free(body); - *response = response_failure(request); + *response = response_failure(request->type); } @@ -60,6 +58,6 @@ struct response_t handle_request( return response; default: - return response_failure(request); + return response_failure(request->type); } } diff --git a/server/src/socket.c b/server/src/socket.c index b1eb9db..37f1072 100644 --- a/server/src/socket.c +++ b/server/src/socket.c @@ -1,4 +1,4 @@ -// sock.c +// socket.c #include #include