Given a weapon name, assigns that weapon to a specific index into the Weapons array. Returns -1 if the named weapon is not found, 0 if the weapon is already in its place, or 1 if successfully moved
| 658 | // the Weapons array. Returns -1 if the named weapon is not found, 0 if the weapon |
| 659 | // is already in its place, or 1 if successfully moved |
| 660 | int MatchWeaponToIndex(char *name, int dest_index) { |
| 661 | |
| 662 | ASSERT(dest_index >= 0 && dest_index < MAX_WEAPONS); |
| 663 | |
| 664 | int cur_index = FindWeaponName(name); |
| 665 | int new_index; |
| 666 | |
| 667 | if (cur_index == -1) { |
| 668 | // The weapon we are trying to find is not loaded or doesn't exist |
| 669 | Int3(); // Get Jason or Matt |
| 670 | return -1; |
| 671 | } |
| 672 | |
| 673 | if (cur_index == dest_index) |
| 674 | return 0; // hurray, we're already matched up! |
| 675 | |
| 676 | if (Weapons[dest_index].used) { |
| 677 | // This weapon is currently in use. Copy the present data |
| 678 | // into a new weapon and then assign our desired weapon to that index |
| 679 | |
| 680 | new_index = AllocWeapon(); |
| 681 | if (new_index >= 0) // DAJ -1FIX |
| 682 | memcpy(&Weapons[new_index], &Weapons[dest_index], sizeof(weapon)); |
| 683 | |
| 684 | // Now copy our new info over and free the old one |
| 685 | memcpy(&Weapons[dest_index], &Weapons[cur_index], sizeof(weapon)); |
| 686 | FreeWeapon(cur_index); |
| 687 | } else { |
| 688 | // This slot is unused, so just take it |
| 689 | |
| 690 | Weapons[dest_index].used = 1; |
| 691 | Num_weapons++; |
| 692 | |
| 693 | memcpy(&Weapons[dest_index], &Weapons[cur_index], sizeof(weapon)); |
| 694 | FreeWeapon(cur_index); |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | return new_index; // we made it! |
| 699 | } |
| 700 | |
| 701 | // Moves a weapon from a given index into a new one (above MAX_STATIC_WEAPONS) |
| 702 | // returns new index |
no test coverage detected