refactor list item removal into a library fn

develop
myk002 2021-08-19 19:51:25 -07:00
parent 8b824244ac
commit e9eef31344
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 26 additions and 16 deletions

@ -310,6 +310,27 @@ Link *linked_list_insert_after(Link *pos, Link *link)
return link;
}
/**
* Returns true if the item with id idToRemove was found, deleted, and removed
* from the list. Otherwise returns false.
*/
template<typename Link>
bool linked_list_remove(Link *head, int32_t idToRemove)
{
for (Link *link = head; link; link = link->next)
{
if (!link->item || link->item->id != idToRemove)
continue;
link->prev->next = link->next;
if (link->next)
link->next->prev = link->prev;
delete(link);
return true;
}
return false;
}
template<typename T>
inline typename T::mapped_type map_find(
const T &map, const typename T::key_type &key,

@ -832,23 +832,12 @@ static bool detachItem(MapExtras::MapCache &mc, df::item *item)
}
if (auto *ref =
virtual_cast<df::general_ref_projectile>(Items::getGeneralRef(item, general_ref_type::PROJECTILE)))
virtual_cast<df::general_ref_projectile>(
Items::getGeneralRef(item, general_ref_type::PROJECTILE)))
{
int32_t proj_id = ((df::general_ref_projectile *)ref)->projectile_id;
df::proj_list_link *link = world->proj_list.next;
for (; link; link = link->next)
{
if (link->item->id != proj_id)
continue;
link->prev->next = link->next;
if (link->next)
link->next->prev = link->prev;
delete(link);
break;
}
return DFHack::removeRef(item->general_refs,
general_ref_type::PROJECTILE, ref->getID());
return linked_list_remove(&world->proj_list, ref->projectile_id) &&
DFHack::removeRef(item->general_refs,
general_ref_type::PROJECTILE, ref->getID());
}
if (item->flags.bits.on_ground)