item &inventory::add_item(item newit, bool keep_invlet, bool assign_invlet) { binned = false; bool reuse_cached_letter = false; // Avoid letters that have been manually assigned to other things. if( !keep_invlet && g->u.assigned_invlet.count(newit.invlet) ) { newit.invlet = '\0'; } // Check how many stacks of this type already are in our inventory. if(!keep_invlet && assign_invlet) { // Do we have this item in our inventory favourites cache? char temp_invlet = find_usable_cached_invlet(newit.typeId()); if( temp_invlet != 0 ) { newit.invlet = temp_invlet; reuse_cached_letter = true; } // If it's not in our cache and not a lowercase letter, try to give it a low letter. if(!reuse_cached_letter && (newit.invlet < 'a' || newit.invlet > 'z')) { assign_empty_invlet(newit); } // Make sure the assigned invlet doesn't exist already. if(this == &g->u.inv && g->u.invlet_to_position(newit.invlet) != INT_MIN) { assign_empty_invlet(newit); } } // See if we can't stack this item. for( auto &elem : items ) { std::list<item>::iterator it_ref = elem.begin(); if( it_ref->stacks_with( newit ) ) { if( it_ref->merge_charges( newit ) ) { return *it_ref; } newit.invlet = it_ref->invlet; elem.push_back( newit ); return elem.back(); } else if( keep_invlet && assign_invlet && it_ref->invlet == newit.invlet ) { // If keep_invlet is true, we'll be forcing other items out of their current invlet. assign_empty_invlet(*it_ref); } } // Couldn't stack the item, proceed. if(!reuse_cached_letter) { update_cache_with_item(newit); } std::list<item> newstack; newstack.push_back(newit); items.push_back(newstack); return items.back().back(); }
void inventory::update_invlet( item &newit, bool assign_invlet ) { // Avoid letters that have been manually assigned to other things. if( newit.invlet && assigned_invlet.find( newit.invlet ) != assigned_invlet.end() && assigned_invlet[newit.invlet] != newit.typeId() ) { newit.invlet = '\0'; } // Remove letters that are not in the favorites cache if( newit.invlet ) { auto invlet_list_iter = invlet_cache.find( newit.typeId() ); bool found = false; if( invlet_list_iter != invlet_cache.end() ) { auto &invlet_list = invlet_list_iter->second; found = std::find( invlet_list.begin(), invlet_list.end(), newit.invlet ) != invlet_list.end(); } if( !found ) { newit.invlet = '\0'; } } // Remove letters that have been assigned to other items in the inventory if( newit.invlet ) { char tmp_invlet = newit.invlet; newit.invlet = '\0'; if( g->u.invlet_to_position( tmp_invlet ) == INT_MIN ) { newit.invlet = tmp_invlet; } } if( assign_invlet ) { // Assign a cached letter to the item if( !newit.invlet ) { newit.invlet = find_usable_cached_invlet( newit.typeId() ); } // Give the item an invlet if it has none if( !newit.invlet ) { assign_empty_invlet( newit, g->u ); } } }
void inventory::update_invlet( item &newit, bool assign_invlet ) { // Avoid letters that have been manually assigned to other things. if( newit.invlet && assigned_invlet.find( newit.invlet ) != assigned_invlet.end() && assigned_invlet[newit.invlet] != newit.typeId() ) { newit.invlet = '\0'; } // Remove letters that are not in the favorites cache if( newit.invlet ) { if( !invlet_cache.contains( newit.invlet, newit.typeId() ) ) { newit.invlet = '\0'; } } // Remove letters that have been assigned to other items in the inventory if( newit.invlet ) { char tmp_invlet = newit.invlet; newit.invlet = '\0'; if( g->u.invlet_to_position( tmp_invlet ) == INT_MIN ) { newit.invlet = tmp_invlet; } } if( assign_invlet ) { // Assign a cached letter to the item if( !newit.invlet ) { newit.invlet = find_usable_cached_invlet( newit.typeId() ); } // Give the item an invlet if it has none if( !newit.invlet ) { assign_empty_invlet( newit, g->u ); } } }