Skip to content

Commit

Permalink
fixed crash on empty engine loading;
Browse files Browse the repository at this point in the history
avl refactored;
  • Loading branch information
TeslaRus committed Jan 31, 2018
1 parent 56cd054 commit 96f9d84
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
2 changes: 1 addition & 1 deletion autoexec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ noclip(0);
--loadMap(base_path .. "tests/heavy1/LEVEL1.PHD");
--loadMap(base_path .. "tests/TRIGGERS.PHD");
--setgamef(1, 0);
dofile(base_path .. "save/qsave.lua");
--dofile(base_path .. "save/qsave.lua");
15 changes: 12 additions & 3 deletions src/core/avl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ static avl_node_p RotateRight(avl_node_p node);
static avl_node_p RotateLeft(avl_node_p node);
static avl_node_p BalanceNode(avl_node_p node);
static void BalanceTree(avl_header_p header, avl_node_p p);
static avl_node_p AVL_NewNode(uint32_t key, void *data);

#define AVL_UPDATE_NODE_HEIGHT(node, t) \
(node)->height = ((node)->left) ? ((node)->left->height) : 0;\
Expand All @@ -18,7 +19,7 @@ static void BalanceTree(avl_header_p header, avl_node_p p);
t = (node->right) ? (node->right->height) : (0);\
t -= ((node->left) ? (node->left->height) : (0));\

avl_header_p AVL_Init()
avl_header_p AVL_Create()
{
avl_header_p p = (avl_header_p)malloc(sizeof(struct avl_header_s));
if(p == NULL)
Expand All @@ -30,12 +31,20 @@ avl_header_p AVL_Init()
p->root = NULL;
p->list = NULL;
p->nodes_count = 0;

return p;
}


void AVL_Free(avl_header_p p)
void AVL_Init(avl_header_p p)
{
p->free_data = NULL;
p->root = NULL;
p->list = NULL;
p->nodes_count = 0;
}

void AVL_Delete(avl_header_p p)
{
AVL_MakeEmpty(p);
free(p);
Expand Down
8 changes: 4 additions & 4 deletions src/core/avl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ typedef struct avl_header_s
void (*free_data)(void *data);
} avl_header_t, *avl_header_p;

avl_header_p AVL_Init();
void AVL_Free(avl_header_p p);
avl_header_p AVL_Create();
void AVL_Init(avl_header_p p);
void AVL_Delete(avl_header_p p);
void AVL_MakeEmpty(avl_header_p header);

avl_node_p AVL_NewNode(uint32_t key, void *data);
avl_node_p AVL_SearchNode(avl_header_p header, uint32_t key);
avl_node_p AVL_InsertReplace(avl_header_p header, uint32_t key, void *data);
void AVL_DeleteNode(avl_header_p header, avl_node_p node);
void AVL_MakeEmpty(avl_header_p header);

#endif /* AVL_TREE_H */

Expand Down
55 changes: 25 additions & 30 deletions src/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ extern "C" {
struct entity_s *player; // this is an unique Lara's pointer =)
struct skeletal_model_s *sky_box; // global skybox

struct avl_header_s *entity_tree;
struct avl_header_s *items_tree;
struct avl_header_s entity_tree;
struct avl_header_s items_tree;

uint32_t type;

Expand Down Expand Up @@ -126,6 +126,9 @@ void World_FixRooms();
void World_BuildNearRoomsList(struct room_s *room);
void World_BuildOverlappedRoomsList(struct room_s *room);

extern "C" void AVL_DeleteEntity(void *p) { Entity_Delete((entity_p)p); }
extern "C" void AVL_DeleteItem(void *p) { BaseItem_Delete((base_item_p)p); }

void World_Prepare()
{
global_world.id = 0;
Expand Down Expand Up @@ -164,14 +167,13 @@ void World_Prepare()
global_world.skeletal_models = NULL;
global_world.skeletal_models_count = 0;
global_world.sky_box = NULL;
global_world.entity_tree = NULL;
global_world.items_tree = NULL;
AVL_Init(&global_world.entity_tree);
global_world.entity_tree.free_data = AVL_DeleteEntity;
AVL_Init(&global_world.items_tree);
global_world.items_tree.free_data = AVL_DeleteItem;
}


extern "C" void AVL_DeleteEntity(void *p) { Entity_Delete((entity_p)p); }
extern "C" void AVL_DeleteItem(void *p) { BaseItem_Delete((base_item_p)p); }

void World_Open(const char *path, int trv)
{
VT_Level *tr = new VT_Level();
Expand All @@ -181,11 +183,6 @@ void World_Open(const char *path, int trv)
World_Clear();

global_world.version = tr->game_version;

global_world.entity_tree = AVL_Init();
global_world.entity_tree->free_data = AVL_DeleteEntity;
global_world.items_tree = AVL_Init();
global_world.items_tree->free_data = AVL_DeleteItem;

World_ScriptsOpen(path); // Open configuration scripts.
Gui_DrawLoadScreen(200);
Expand Down Expand Up @@ -245,7 +242,7 @@ void World_Open(const char *path, int trv)
Gui_DrawLoadScreen(860);

// Generate entity functions.
for(avl_node_p p = global_world.entity_tree->list; p; p = p->next)
for(avl_node_p p = global_world.entity_tree.list; p; p = p->next)
{
World_SetEntityFunction((entity_p)p->data);
}
Expand Down Expand Up @@ -292,8 +289,7 @@ void World_Clear()
global_world.player = NULL;

/* entity empty must be done before rooms destroy */
AVL_Free(global_world.entity_tree);
global_world.entity_tree = NULL;
AVL_MakeEmpty(&global_world.entity_tree);

/* Now we can delete physics misc objects */
Physics_CleanUpObjects();
Expand Down Expand Up @@ -369,8 +365,7 @@ void World_Clear()
}

/*items empty*/
AVL_Free(global_world.items_tree);
global_world.items_tree = NULL;
AVL_MakeEmpty(&global_world.items_tree);

if(global_world.skeletal_models_count)
{
Expand Down Expand Up @@ -471,7 +466,7 @@ uint32_t World_SpawnEntity(uint32_t model_id, uint32_t room_id, float pos[3], fl
entity = Entity_Create();
if(id < 0)
{
avl_node_p max = global_world.entity_tree->root;
avl_node_p max = global_world.entity_tree.root;
while(max)
{
entity->id = max->key + 1;
Expand Down Expand Up @@ -535,7 +530,7 @@ uint32_t World_SpawnEntity(uint32_t model_id, uint32_t room_id, float pos[3], fl

struct entity_s *World_GetEntityByID(uint32_t id)
{
avl_node_p p = AVL_SearchNode(global_world.entity_tree, id);
avl_node_p p = AVL_SearchNode(&global_world.entity_tree, id);
return (p) ? ((entity_p)p->data) : (NULL);
}

Expand Down Expand Up @@ -565,13 +560,13 @@ struct entity_s *World_GetPlayer()

void World_IterateAllEntities(int (*iterator)(struct entity_s *ent, void *data), void *data)
{
for(avl_node_p p = global_world.entity_tree->list; p; )
for(avl_node_p p = global_world.entity_tree.list; p; )
{
entity_p ent = (entity_p)p->data;
if(ent->state_flags & ENTITY_STATE_DELETED)
{
avl_node_p next = p->next;
AVL_DeleteNode(global_world.entity_tree, p);
AVL_DeleteNode(&global_world.entity_tree, p);
p = next;
continue;
}
Expand All @@ -592,14 +587,14 @@ struct flyby_camera_sequence_s *World_GetFlyBySequences()

struct base_item_s *World_GetBaseItemByID(uint32_t id)
{
avl_node_p p = AVL_SearchNode(global_world.items_tree, id);
avl_node_p p = AVL_SearchNode(&global_world.items_tree, id);
return (p) ? ((base_item_p)p->data) : (NULL);
}


struct base_item_s *World_GetBaseItemByWorldModelID(uint32_t id)
{
for(avl_node_p p = global_world.items_tree->list; p; p = p->next)
for(avl_node_p p = global_world.items_tree.list; p; p = p->next)
{
if(p->data && (id == ((base_item_p)p->data)->world_model_id))
{
Expand Down Expand Up @@ -692,16 +687,16 @@ int World_AddAnimSeq(struct anim_seq_s *seq)

int World_AddEntity(struct entity_s *entity)
{
return (AVL_InsertReplace(global_world.entity_tree, entity->id, entity)) ? (0x01) : (0x00);
return (AVL_InsertReplace(&global_world.entity_tree, entity->id, entity)) ? (0x01) : (0x00);
}


int World_DeleteEntity(uint32_t id)
{
avl_node_p p = AVL_SearchNode(global_world.entity_tree, id);
avl_node_p p = AVL_SearchNode(&global_world.entity_tree, id);
if(p)
{
AVL_DeleteNode(global_world.entity_tree, p);
AVL_DeleteNode(&global_world.entity_tree, p);
return 1;
}
return 0;
Expand All @@ -711,7 +706,7 @@ int World_DeleteEntity(uint32_t id)
int World_CreateItem(uint32_t item_id, uint32_t model_id, uint32_t world_model_id, uint16_t type, uint16_t count, const char *name)
{
skeletal_model_p model = World_GetModelByID(model_id);
if(model && (!AVL_SearchNode(global_world.items_tree, item_id)))
if(model && (!AVL_SearchNode(&global_world.items_tree, item_id)))
{
base_item_p item = BaseItem_Create(model, item_id);
item->world_model_id = world_model_id;
Expand All @@ -721,7 +716,7 @@ int World_CreateItem(uint32_t item_id, uint32_t model_id, uint32_t world_model_i
{
strncpy(item->name, name, sizeof(item->name));
}
return (AVL_InsertReplace(global_world.items_tree, item_id, item)) ? (0x01) : (0x00);
return (AVL_InsertReplace(&global_world.items_tree, item_id, item)) ? (0x01) : (0x00);
}

return 0;
Expand All @@ -730,10 +725,10 @@ int World_CreateItem(uint32_t item_id, uint32_t model_id, uint32_t world_model_i

int World_DeleteItem(uint32_t item_id)
{
avl_node_p p = AVL_SearchNode(global_world.items_tree, item_id);
avl_node_p p = AVL_SearchNode(&global_world.items_tree, item_id);
if(p)
{
AVL_DeleteNode(global_world.items_tree, p);
AVL_DeleteNode(&global_world.items_tree, p);
return 1;
}
return 0;
Expand Down

0 comments on commit 96f9d84

Please sign in to comment.