X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fserver%2Fthings.c;h=7d0f62d9b19be98ab2da6d1a145887b529aa04a2;hb=3dedf6344c941891491773d1cc5d647aa664b218;hp=6df9b18efe5fe32ab5bd441ac6e2232032540fcf;hpb=891ba8fbca53d920f6b3704827fa6b8aee737de4;p=plomrogue diff --git a/src/server/things.c b/src/server/things.c index 6df9b18..7d0f62d 100644 --- a/src/server/things.c +++ b/src/server/things.c @@ -28,15 +28,14 @@ struct NextAndId -/* Return lowest unused id for new thing ("sel"==0), thing type ("sel"==1) or - * thing action ("sel"==2). - */ -static uint8_t get_unused_id(uint8_t sel); +/* Free ThingsInMemory chain starting at "tm". */ +static void free_things_in_memory(struct ThingInMemory * tm); /* To linked list of NextAndId structs (or rather structs whose start region is * compatible to it) starting at "start", add newly allocated element of * "n_size" and an ID that is either "id" or, if "id" is <= UINT8_MAX and >= - * "id_start", get ID from get_unused_id("struct_id"). + * "id_start", get lowest ID >= "start_id" and <= UINT8_MAX for new thing + * ("struct_id"=0), thing type ("struct_id"=1) or thing action ("struct_id"=2). */ static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id, int16_t id, uint8_t struct_id, @@ -44,20 +43,14 @@ static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id, -static uint8_t get_unused_id(uint8_t sel) +static void free_things_in_memory(struct ThingInMemory * tm) { - uint8_t i = 0; - while (1) + if (NULL == tm) { - if ( (0 == sel && !get_thing(world.things, i, 1)) - || (1 == sel && !get_thing_type(i)) - || (2 == sel && !get_thing_action(i))) - { - return i; - } - exit_err(i == UINT8_MAX, "No unused ID available to add to ID list."); - i++; + return; } + free_things_in_memory(tm->next); + free(tm); } @@ -66,10 +59,28 @@ static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id, int16_t id, uint8_t struct_id, struct NextAndId ** start) { - char * f_name = "add_to_struct_list()"; - struct NextAndId * nai = try_malloc(n_size, f_name); + struct NextAndId * nai = try_malloc(n_size, __func__); memset(nai, 0, n_size); - nai->id = (start_id<=id && id<=UINT8_MAX) ? id : get_unused_id(struct_id); + if (start_id <= id && id <= UINT8_MAX) + { + nai->id = id; + } + else + { + while (1) + { + if ( (0 == struct_id && !get_thing(world.things, start_id, 1)) + || (1 == struct_id && !get_thing_type(start_id)) + || (2 == struct_id && !get_thing_action(start_id))) + { + nai->id = start_id; + break; + } + char * err = "No unused ID available to add to ID list."; + exit_err(start_id == UINT8_MAX, err); + start_id++; + } + } struct NextAndId ** nai_ptr_ptr = start; for (; NULL != * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next); *nai_ptr_ptr = nai; @@ -78,11 +89,11 @@ static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id, -extern struct ThingAction * add_thing_action(int16_t id) +extern struct ThingAction * add_thing_action(uint8_t id) { struct ThingAction * ta; ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction), - 1, id, 2, + 1, (int16_t) id, 2, (struct NextAndId **) &world.thing_actions); set_cleanup_flag(CLEANUP_THING_ACTIONS); @@ -103,6 +114,7 @@ extern struct ThingType * add_thing_type(int16_t id) &world.thing_types); set_cleanup_flag(CLEANUP_THING_TYPES); tt->name = strdup("(none)"); + tt->corpse_id = tt->id; return tt; } @@ -112,7 +124,7 @@ extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x) { struct Thing * t; t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0, - (struct NextAndId **) &world.things); + (struct NextAndId **)&world.things); struct ThingType * tt = get_thing_type(type); set_cleanup_flag(CLEANUP_THINGS); t->type = tt->id; @@ -124,6 +136,19 @@ extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x) +extern void add_thing_to_memory_map(struct Thing * t, uint8_t type, + uint8_t y, uint8_t x) +{ + struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__); + tm->type = type; + tm->pos.y = y; + tm->pos.x = x; + tm->next = t->t_mem; + t->t_mem = tm; +} + + + extern void free_thing_actions(struct ThingAction * ta) { if (NULL == ta) @@ -159,6 +184,8 @@ extern void free_things(struct Thing * t) free_things(t->owns); free_things(t->next); free(t->fov_map); + free(t->mem_map); + free_things_in_memory(t->t_mem); free(t); if (t == world.things) /* So add_things()' NULL-delimited thing */ { /* iteration loop does not iterate over */