X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fserver%2Fthings.c;h=e73a09a283c09fb843292996c0b6ee3561f68d2f;hb=c8841380b53dbd999061a8de399e71d5d8272fb0;hp=6df9b18efe5fe32ab5bd441ac6e2232032540fcf;hpb=891ba8fbca53d920f6b3704827fa6b8aee737de4;p=plomrogue diff --git a/src/server/things.c b/src/server/things.c index 6df9b18..e73a09a 100644 --- a/src/server/things.c +++ b/src/server/things.c @@ -28,15 +28,11 @@ 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); - /* 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,32 +40,32 @@ static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id, -static uint8_t get_unused_id(uint8_t sel) -{ - uint8_t i = 0; - while (1) - { - 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++; - } -} - - - 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 +74,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 +99,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 +109,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; @@ -159,6 +156,7 @@ extern void free_things(struct Thing * t) free_things(t->owns); free_things(t->next); free(t->fov_map); + free(t->mem_map); free(t); if (t == world.things) /* So add_things()' NULL-delimited thing */ { /* iteration loop does not iterate over */