diff --git a/urogue/state.c b/urogue/state.c index 6535b44..fe40e13 100644 --- a/urogue/state.c +++ b/urogue/state.c @@ -415,6 +415,45 @@ ur_write_object(FILE *savef, struct object *o) ur_write_object_stack(savef, o->next_obj); } +/* + * Puts objects with identifiers into the global ident_list, keeping it sorted + * by type and identifier. + */ +void +add_to_ident(struct object *o) +{ + extern linked_list *ident_list; + linked_list *obj_ll; + linked_list *list_p, *prev_p = NULL; + struct object *list_o; + + obj_ll = new_list(); + obj_ll->data.obj = o; + + for (list_p = ident_list; list_p != NULL; list_p = next(list_p)) + { + list_o = list_p->data.obj; + if (list_o->o_type == o->o_type) + { + if (list_o->o_ident > o->o_ident) + { + prev_p = list_p->l_prev; + break; + } + else if (next(list_p) && + next(list_p)->data.obj->o_type != o->o_type) + { + prev_p = list_p; + break; + } + } + if (!next(list_p)) + prev_p = list_p; + } + _attach_after(&ident_list, prev_p, obj_ll); + return; +} + struct object * ur_read_object(FILE *savef) { @@ -454,6 +493,11 @@ ur_read_object(FILE *savef) other = ur_read_int(savef); + if (o->o_ident != 0) { + /* The object needs to be placed in the identifiers list. */ + add_to_ident(o); + } + if (other & 1) o->o_bag = ur_read_bag(savef); if (other & 2)