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_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) {
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];

View File

@ -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)

View File

@ -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;
};

View File

@ -4,7 +4,6 @@
#include <jansson.h>
#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}";

View File

@ -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,
&registered_entities_json,
&temp_world.height, &temp_world.width,
&temp_world.tick, &temp_world.height, &temp_world.width,
json
);
if (err != ERR_OK) return err;

View File

@ -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

View File

@ -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

View File

@ -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})

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) {
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

View File

@ -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);
}
}

View File

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