CleverRaven/Cataclysm-DDA — pull request #6322 by BevapDin (2014-02-26 08:26:17+00:00)
Title: Fix for 6146

How I reproduced it:
Get a gun, get a gunmod for this gun, get the needed skills.
Get another item, that is sorted after the gun and the gun mod (science id card works).
Drop the gunmod (but not the science id).
Go into advanced inventory, select inventory in one pane and the location of the gunmod in the other pane.
Move the mod into inventory, select it, examine, activate and add it to the gun.
Result should now be that the gunmod still there (despite the message that it had been attached), and the science id card is gone.
Examining the gun will result in a crash, because the science id has been put into the gun instead of the gunmod. The code that displays the gunmods of the gun expects that it contains only items with an `it_gunmod` type. The science id isn't and so the cast to `it_gunmod` fails. The resulting NULL pointer is accessed to display the gun mods name.

I think it comes from the call to `game::inv` in `player::use` that is made to let the player select the gun to be modified. `game::inv` will sort the inventory, items may change their position.
While moving an item into the inventory with the AIM only calls `player::i_add` and does *not* sort the inventory. The new item (the gun mod you picked up) becomes the last item in the inventory (`position == inv.size()-1`). This one gets activated. Than the inventory is sorted (`player::use` -> `game::inv`) and the science id is now the last item.

The line ad the end of `player::use`:
```
gun->contents.push_back(i_rem(pos));
```
removes the last item (because pos is the index of the last item) and adds it to the gun. But because of the sorting the last item is now the science id.

Fixes #6146 by removing the item by pointer, which is still unchanged and adding the result (the gunmod for sure) to the gun.