Techneck/code/source-c/world/update/promise_reservation.c

218 lines
6.7 KiB
C

#include <world/update/world_update.h>
#include <stddef.h>
#include <stdlib.h>
u32_t tc_promise_identifier_counter = 0;
typedef struct
{
u32_t identifier;
tc_world_s *world;
} tc_worldupdate_world_promise_s;
typedef struct
{
u32_t identifier;
tc_chunkloader_s *chunkloader;
} tc_worldupdate_chunkloader_promise_s;
typedef struct
{
u32_t identifier;
tc_chunklist_s *chunklist;
} tc_worldupdate_chunklist_promise_s;
tc_worldupdate_world_promise_s *tc_worldupdate_worlds_promise_registry = NULL;
tc_worldupdate_chunkloader_promise_s *tc_worldupdate_chunkloader_promise_registry = NULL;
tc_worldupdate_chunklist_promise_s *tc_worldupdate_chunklist_promise_registry = NULL;
#define TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE 4096
void tc_worldupdate_initialize_promises()
{
tc_worldupdate_worlds_promise_registry = calloc(sizeof(tc_worldupdate_world_promise_s), TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE);
tc_worldupdate_chunkloader_promise_registry = calloc(sizeof(tc_worldupdate_chunkloader_promise_s), TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE);
tc_worldupdate_chunklist_promise_registry = calloc(sizeof(tc_worldupdate_chunklist_promise_s), TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE);
}
tc_promise_s tc_worldupdate_reserve_world_promise()
{
u32_t index = 0;
while(index < TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE)
{
tc_worldupdate_world_promise_s *entry = &tc_worldupdate_worlds_promise_registry[index];
if(entry->identifier == 0)
if(entry->world != NULL)
{
++tc_promise_identifier_counter;
tc_promise_s promise;
promise.identifier = tc_promise_identifier_counter;
entry->identifier = promise.identifier;
entry->world = NULL;
return promise;
}
++index;
}
tc_promise_s promise;
promise.identifier = 0;
promise.issueing_tick = 0;
return promise;
}
tc_promise_s tc_worldupdate_reserve_chunkloader_promise()
{
u32_t index = 0;
while(index < TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE)
{
tc_worldupdate_chunkloader_promise_s *entry = &tc_worldupdate_chunkloader_promise_registry[index];
if(entry->identifier == 0)
if(entry->chunkloader != NULL)
{
++tc_promise_identifier_counter;
tc_promise_s promise;
promise.identifier = tc_promise_identifier_counter;
entry->identifier = promise.identifier;
entry->chunkloader = NULL;
return promise;
}
++index;
}
tc_promise_s promise;
promise.identifier = 0;
promise.issueing_tick = 0;
return promise;
}
tc_promise_s tc_worldupdate_reserve_chunklist_promise()
{
u32_t index = 0;
while(index < TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE)
{
tc_worldupdate_chunklist_promise_s *entry = &tc_worldupdate_chunklist_promise_registry[index];
if(entry->identifier == 0)
if(entry->chunklist != NULL)
{
++tc_promise_identifier_counter;
tc_promise_s promise;
promise.identifier = tc_promise_identifier_counter;
entry->identifier = promise.identifier;
entry->chunklist = NULL;
return promise;
}
++index;
}
tc_promise_s promise;
promise.identifier = 0;
promise.issueing_tick = 0;
return promise;
}
const tc_world_s * tc_worldupdate_claim_world_promise(tc_promise_s promise)
{
u32_t index = 0;
while(index < TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE)
{
tc_worldupdate_world_promise_s *entry = &tc_worldupdate_worlds_promise_registry[index];
if(entry->identifier == promise.identifier)
if(entry->world != NULL)
{
entry->identifier = 0;
return entry->world;
}
++index;
}
return NULL;
}
const tc_chunkloader_s * tc_worldupdate_claim_chunkloader_promise(tc_promise_s promise)
{
u32_t index = 0;
while(index < TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE)
{
tc_worldupdate_chunkloader_promise_s *entry = &tc_worldupdate_chunkloader_promise_registry[index];
if(entry->identifier == promise.identifier)
if(entry->chunkloader != NULL)
{
entry->identifier = 0;
return entry->chunkloader;
}
++index;
}
return NULL;
}
const tc_chunklist_s * tc_worldupdate_claim_chunks_promise(tc_promise_s promise)
{
u32_t index = 0;
while(index < TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE)
{
tc_worldupdate_chunklist_promise_s *entry = &tc_worldupdate_chunklist_promise_registry[index];
if(entry->identifier == promise.identifier)
if(entry->chunklist != NULL)
{
entry->identifier = 0;
return entry->chunklist;
}
++index;
}
return NULL;
}
void tc_worldupate_set_world_promise(u32_t identifier, tc_world_s *world)
{
u32_t index = 0;
while(index < TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE)
{
tc_worldupdate_world_promise_s *entry = &tc_worldupdate_worlds_promise_registry[index];
if(entry->identifier == identifier)
entry->world = world;
++index;
}
}
void tc_worldupate_set_chunkloader_promise(u32_t identifier, tc_chunkloader_s *chunkloader)
{u32_t index = 0;
while(index < TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE)
{
tc_worldupdate_chunkloader_promise_s *entry = &tc_worldupdate_chunkloader_promise_registry[index];
if(entry->identifier == identifier)
entry->chunkloader = chunkloader;
++index;
}
}
void tc_worldupate_set_chunklist_promise(u32_t identifier, tc_chunklist_s *chunklist)
{
u32_t index = 0;
while(index < TC_WORLDUPDATE_PROMISE_REGISTRY_SIZE)
{
tc_worldupdate_chunklist_promise_s *entry = &tc_worldupdate_chunklist_promise_registry[index];
if(entry->identifier == identifier)
entry->chunklist = chunklist;
++index;
}
}