diff --git a/game/src/chunk/chunkman.c b/game/src/chunk/chunkman.c index cfe3612..b8b6bb8 100644 --- a/game/src/chunk/chunkman.c +++ b/game/src/chunk/chunkman.c @@ -256,8 +256,7 @@ chunkman_generate_Chunk_at (int cx, int cz) if (rand() % 100 < 2) { - Entity *tree = malloc (sizeof (Entity)); - *tree = ETEMPLGET ("tree"); + Entity *tree = ent_templates_clone ("tree"); tree->position[0] = x + xoffset; tree->position[1] = chunk->heightmap [INDEX (x, z)]; tree->position[2] = z + zoffset; @@ -268,8 +267,7 @@ chunkman_generate_Chunk_at (int cx, int cz) if (rand() % 100 < 10) { - Entity *grass = malloc (sizeof (Entity)); - *grass = ETEMPLGET ("grass"); + Entity *grass = ent_templates_clone ("grass"); grass->position[0] = x + xoffset; grass->position[1] = chunk->heightmap [INDEX (x, z)]; grass->position[2] = z + zoffset; diff --git a/game/src/entity/templates.c b/game/src/entity/templates.c index c23fc15..8b215e1 100644 --- a/game/src/entity/templates.c +++ b/game/src/entity/templates.c @@ -8,10 +8,16 @@ ent_templates_add (char *key, Entity *entity) g_hash_table_insert (templates, key, entity); } -Entity +Entity * ent_templates_get (char *key) { - return *(Entity *) g_hash_table_lookup (templates, key); + return (Entity *) g_hash_table_lookup (templates, key); +} + +Entity * +ent_templates_clone (char *key) +{ + return g_memdup2 (ent_templates_get (key), sizeof (Entity)); } void diff --git a/game/src/entity/templates.h b/game/src/entity/templates.h index f8fee39..b530442 100644 --- a/game/src/entity/templates.h +++ b/game/src/entity/templates.h @@ -8,7 +8,6 @@ #include "entity.h" #include "../resources.h" -#define ETEMPLGET(key) (ent_templates_get (key)) - void ent_templates_init (); -Entity ent_templates_get (char *key); +Entity *ent_templates_clone (char *key); +Entity *ent_templates_get (char *key);