Destroys a unit. Removes any traces of it. If the unit is a rocket or factory, also destroys units in its garrison.
(&mut self, id: UnitID)
| 927 | /// |
| 928 | /// If the unit is a rocket or factory, also destroys units in its garrison. |
| 929 | fn destroy_unit(&mut self, id: UnitID) { |
| 930 | match self.unit(id) |
| 931 | .expect("Unit does not exist and cannot be destroyed.") |
| 932 | .location() { |
| 933 | OnMap(loc) => { |
| 934 | self.my_planet_mut().units_by_loc.remove(&loc); |
| 935 | }, |
| 936 | InSpace => { |
| 937 | // Units only die in space after a landing on their turn. |
| 938 | // Thus we are guaranteed that my_unit() will find the unit. |
| 939 | for utd_id in self.my_unit(id).unwrap().structure_garrison() |
| 940 | .expect("only rockets can die in space") { |
| 941 | self.my_team_mut().units_in_space.remove(&utd_id); |
| 942 | } |
| 943 | self.my_team_mut().units_in_space.remove(&id); |
| 944 | return; |
| 945 | }, |
| 946 | _ => panic!("Unit is in ???, this should not be possible"), |
| 947 | }; |
| 948 | |
| 949 | // If this unit's garrison is visible, destroy those units too. |
| 950 | let unit_type = self.unit(id).unwrap().unit_type(); |
| 951 | if unit_type == UnitType::Rocket || unit_type == UnitType::Factory { |
| 952 | let units_to_destroy = self.unit_mut(id).unwrap() |
| 953 | .structure_garrison().unwrap(); |
| 954 | for utd_id in units_to_destroy.iter() { |
| 955 | self.my_planet_mut().units.remove(&utd_id); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | self.my_planet_mut().units.remove(&id); |
| 960 | } |
| 961 | |
| 962 | /// Disintegrates the unit and removes it from the map. If the unit is a |
| 963 | /// factory or a rocket, also disintegrates any units garrisoned inside it. |