a lot of pointless changes but also a basic tick system

This commit is contained in:
snit 2024-11-14 09:12:23 -06:00
parent b03e194a34
commit f702065a7c
12 changed files with 35 additions and 24 deletions

View File

@ -10,3 +10,5 @@ add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE simworld) target_link_libraries(${PROJECT_NAME} PRIVATE simworld)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/common/include) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/common/include)
install(TARGETS ${PROJECT_NAME})

View File

@ -9,7 +9,7 @@
void render_world(struct world_t const *world) { void render_world(struct world_t const *world) {
assert(world != NULL); 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++) { for (size_t i = 0; i < MAX_ENTITIES; i++) {
struct entity_t const *const entity = &world->entities[i]; struct entity_t const *const entity = &world->entities[i];

View File

@ -11,8 +11,10 @@ target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/common/inc
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_check_modules(LUA REQUIRED lua) pkg_check_modules(LUA REQUIRED lua5.4)
pkg_check_modules(JANSSON REQUIRED jansson) pkg_check_modules(JANSSON REQUIRED jansson)
target_link_libraries(${PROJECT_NAME} ${LUA_LIBRARIES} ${JANSSON_LIBRARIES}) target_link_libraries(${PROJECT_NAME} ${LUA_LIBRARIES} ${JANSSON_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${LUA_INCLUDE_DIRS} ${JANSSON_INCLUDE_DIRS}) target_include_directories(${PROJECT_NAME} PRIVATE ${LUA_INCLUDE_DIRS} ${JANSSON_INCLUDE_DIRS})
install(TARGETS ${PROJECT_NAME} DESTINATION lib)

View File

@ -15,6 +15,8 @@ struct world_t {
struct entity_t entities[MAX_ENTITIES]; struct entity_t entities[MAX_ENTITIES];
struct entity_registry_t registered_entities; struct entity_registry_t registered_entities;
size_t tick;
size_t height; size_t height;
size_t width; size_t width;
}; };

View File

@ -4,7 +4,6 @@
#include <jansson.h> #include <jansson.h>
#include "request.h" #include "request.h"
#include "request/get_world_data.h"
static char const *REQUEST_JSON_FMT = "{si, so}"; static char const *REQUEST_JSON_FMT = "{si, so}";
static char const *RESPONSE_JSON_FMT = "{sb, si, so}"; static char const *RESPONSE_JSON_FMT = "{sb, si, so}";

View File

@ -5,12 +5,14 @@
#include "world.h" #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) { enum error_t world_init(struct world_t *self, size_t height, size_t width) {
assert(self != NULL); assert(self != NULL);
self->tick = 0;
self->height = height; self->height = height;
self->width = width; self->width = width;
@ -84,7 +86,7 @@ static enum error_t world_serialise_entities(
static enum error_t world_serialise_parts( static enum error_t world_serialise_parts(
struct json_t *entities_json, struct json_t *entities_json,
struct json_t *registered_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 struct json_t **json
) { ) {
assert(entities_json != NULL); 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, struct json_t *world_json = json_pack(WORLD_JSON_FMT,
"entities", entities_json, "entities", entities_json,
"registered-entities", registered_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; if (world_json == NULL) return ERR_JSON_SERIALISE;
@ -131,7 +133,7 @@ enum error_t world_serialise(
err = world_serialise_parts( err = world_serialise_parts(
entities_json, entities_json,
registered_entities_json, registered_entities_json,
self->height, self->width, self->tick, self->height, self->width,
&world_json &world_json
); );
if (err != ERR_OK) goto error_world; if (err != ERR_OK) goto error_world;
@ -150,7 +152,7 @@ error:
static enum error_t world_deserialise_parts( static enum error_t world_deserialise_parts(
struct json_t **entities_json, struct json_t **entities_json,
struct json_t **registered_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 struct json_t *json
) { ) {
assert(entities_json != NULL); assert(entities_json != NULL);
@ -162,7 +164,7 @@ static enum error_t world_deserialise_parts(
int err = json_unpack(json, WORLD_JSON_FMT, int err = json_unpack(json, WORLD_JSON_FMT,
"entities", entities_json, "entities", entities_json,
"registered-entities", registered_entities_json, "registered-entities", registered_entities_json,
"height", height, "width", width "tick", tick, "height", height, "width", width
); );
if (err < 0) return ERR_JSON_DESERIALISE; 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( enum error_t err = world_deserialise_parts(
&entities_json, &entities_json,
&registered_entities_json, &registered_entities_json,
&temp_world.height, &temp_world.width, &temp_world.tick, &temp_world.height, &temp_world.width,
json json
); );
if (err != ERR_OK) return err; if (err != ERR_OK) return err;

View File

@ -2,6 +2,7 @@
A list of things I'd like to accomplish A list of things I'd like to accomplish
## In Progress ## In Progress
* Tick-based game loop
## Completed ## Completed
* Split game ticking and socket connecting logic (can only tick on connection atm) * 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 * Client-side resource files for each mod
* Real error handling (right now I just pass up to main and immediately exit) * 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) * 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

View File

@ -19,4 +19,8 @@ echo "### CLIENT 1 ###"
echo "### CLIENT 2 ###" echo "### CLIENT 2 ###"
./build/client/simworld-client ./build/client/simworld-client
echo "### CLIENT 3 ###"
sleep 1
./build/client/simworld-client
killall simworld-daemon killall simworld-daemon

View File

@ -9,3 +9,5 @@ file(GLOB_RECURSE SOURCES ${SOURCE_DIR}/*.c)
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE simworld) target_link_libraries(${PROJECT_NAME} PRIVATE simworld)
install(TARGETS ${PROJECT_NAME})

View File

@ -24,15 +24,14 @@ static void handle_signal(int signal_no) {
} }
static enum error_t simulation_thread_body(struct game_data_t *data) { static enum error_t simulation_thread_body(struct game_data_t *game) {
assert(data != NULL); assert(game != NULL);
// TODO: An actual game loop lol // TODO: An actual game loop lol
while (true) { while (true) {
if (pthread_mutex_lock(&data->world_lock) != 0) goto error; if (pthread_mutex_lock(&game->world_lock) != 0) goto error;
data->world.width = (data->world.width + 1) % 100; game->world.tick += 1;
data->world.height = (data->world.height + 1) % 100; if (pthread_mutex_unlock(&game->world_lock) != 0) goto error;
if (pthread_mutex_unlock(&data->world_lock) != 0) goto error;
sleep(1); sleep(1);
} }
@ -44,7 +43,6 @@ error:
} }
static enum error_t simulation_thread( static enum error_t simulation_thread(
pthread_t *pthread, pthread_t *pthread,
struct game_data_t *data struct game_data_t *data

View File

@ -5,11 +5,9 @@
#include "request.h" #include "request.h"
static struct response_t response_failure( static struct response_t response_failure(
struct request_t const *request enum request_type_t request
) { ) {
assert(request != NULL); return (struct response_t){ request, false, NULL };
return (struct response_t){ request->type, false, NULL };
} }
static void handle_request_get_world_data( static void handle_request_get_world_data(
@ -41,7 +39,7 @@ static void handle_request_get_world_data(
error: error:
free(body); free(body);
*response = response_failure(request); *response = response_failure(request->type);
} }
@ -60,6 +58,6 @@ struct response_t handle_request(
return response; return response;
default: default:
return response_failure(request); return response_failure(request->type);
} }
} }

View File

@ -1,4 +1,4 @@
// sock.c // socket.c
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>