/** * Reverts the unit to the previous state, if possible. * * @return True if the reversion succeeds. */ public boolean revert() { if (unit.isDisposed() || unit.getType() != type) return false; if (unit.getLocation() != loc) { unit.setLocation(loc); } if (unit.getWorkType() != work) { unit.setWorkType(work); } if (unit.getRole() != role) { // Try to restore role equipment. if (colony == null || unit.getColony() != colony) return false; Set<EquipmentType> eq = new HashSet<EquipmentType>(); TypeCountMap<EquipmentType> unitEquipment = unit.getEquipment(); eq.addAll(equipment.keySet()); eq.addAll(unitEquipment.keySet()); // Give back first, avoiding incompatible equipment problems. for (EquipmentType et : eq) { int count = equipment.getCount(et) - unitEquipment.getCount(et); if (count < 0) { unit.changeEquipment(et, count); colony.addEquipmentGoods(et, -count); } } for (EquipmentType et : eq) { int count = equipment.getCount(et) - unitEquipment.getCount(et); if (count > 0 && colony.canProvideEquipment(et) && unit.canBeEquippedWith(et)) { unit.changeEquipment(et, count); colony.addEquipmentGoods(et, -count); } } } return unit.getRole() == role; }