X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fserver%2Fthings.c;h=bbe9dbffe39388399b741965c7ead8bb2aa61baa;hb=491e8bc8e7a9b1d312256817c0b8be7e05b127be;hp=09000ed34e2073128a1442befe942c1d23ceb40c;hpb=3451efffc74f1fbf2453d89e7858e71c97343c22;p=plomrogue diff --git a/src/server/things.c b/src/server/things.c index 09000ed..bbe9dbf 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 (!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,23 +59,41 @@ 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); + for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next); *nai_ptr_ptr = nai; return nai; } -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; } @@ -124,9 +136,22 @@ 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) + if (!ta) { return; } @@ -139,7 +164,7 @@ extern void free_thing_actions(struct ThingAction * ta) extern void free_thing_types(struct ThingType * tt) { - if (NULL == tt) + if (!tt) { return; } @@ -152,13 +177,15 @@ extern void free_thing_types(struct ThingType * tt) extern void free_things(struct Thing * t) { - if (NULL == t) + if (!t) { return; } 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 */ @@ -171,7 +198,7 @@ extern void free_things(struct Thing * t) extern struct ThingAction * get_thing_action(uint8_t id) { struct ThingAction * ta = world.thing_actions; - for (; NULL != ta && id != ta->id; ta = ta->next); + for (; ta && id != ta->id; ta = ta->next); return ta; } @@ -180,7 +207,7 @@ extern struct ThingAction * get_thing_action(uint8_t id) extern struct ThingType * get_thing_type(uint8_t id) { struct ThingType * tt = world.thing_types; - for (; NULL != tt && id != tt->id; tt = tt->next); + for (; tt && id != tt->id; tt = tt->next); return tt; } @@ -189,7 +216,7 @@ extern struct ThingType * get_thing_type(uint8_t id) extern uint8_t get_thing_action_id_by_name(char * name) { struct ThingAction * ta = world.thing_actions; - while (NULL != ta) + while (ta) { if (0 == strcmp(ta->name, name)) { @@ -210,14 +237,14 @@ extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep) { while (1) { - if (NULL == ptr || id == ptr->id) + if (!ptr || id == ptr->id) { return ptr; } if (deep) { struct Thing * owned_thing = get_thing(ptr->owns, id, 1); - if (NULL != owned_thing) + if (owned_thing) { return ptr; } @@ -297,7 +324,7 @@ extern void own_thing(struct Thing ** target, struct Thing ** source, penult->next = t->next; } struct Thing ** t_ptr_ptr = target; - for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next); + for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next); * t_ptr_ptr = t; t->next = NULL; } @@ -308,5 +335,5 @@ extern void set_thing_position(struct Thing * t, struct yx_uint8 pos) { t->pos = pos; struct Thing * owned = t->owns; - for (; owned != NULL; set_thing_position(owned, pos), owned = owned->next); + for (; owned; set_thing_position(owned, pos), owned = owned->next); }