/** * 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; }
/** Fire any property changes resulting from actions of a unit. */ public void fireChanges() { UnitType newType = null; Unit.Role newRole = null; Location newLoc = null; GoodsType newWork = null; int newWorkAmount = 0; TypeCountMap<EquipmentType> newEquipment = null; if (!unit.isDisposed()) { newLoc = unit.getLocation(); if (colony != null) { newType = unit.getType(); newRole = unit.getRole(); newWork = unit.getWorkType(); newWorkAmount = (newWork == null) ? 0 : getAmount(newLoc, newWork); newEquipment = unit.getEquipment(); } } if (loc != newLoc) { FreeColGameObject oldFcgo = (FreeColGameObject) loc; oldFcgo.firePropertyChange(change(oldFcgo), unit, null); if (newLoc != null) { FreeColGameObject newFcgo = (FreeColGameObject) newLoc; newFcgo.firePropertyChange(change(newFcgo), null, unit); } } if (colony != null) { if (type != newType && newType != null) { String pc = ColonyChangeEvent.UNIT_TYPE_CHANGE.toString(); colony.firePropertyChange(pc, type, newType); } else if (role != newRole && newRole != null) { String pc = Tile.UNIT_CHANGE.toString(); colony.firePropertyChange(pc, role.toString(), newRole.toString()); } if (work == newWork) { if (work != null && workAmount != newWorkAmount) { colony.firePropertyChange(work.getId(), workAmount, newWorkAmount); } } else { if (work != null) { colony.firePropertyChange(work.getId(), workAmount, 0); } if (newWork != null) { colony.firePropertyChange(newWork.getId(), 0, newWorkAmount); } } } if (newEquipment != null) { Set<EquipmentType> keys = new HashSet<EquipmentType>(); keys.addAll(equipment.keySet()); keys.addAll(newEquipment.keySet()); for (EquipmentType e : keys) { int cOld = equipment.getCount(e); int cNew = newEquipment.getCount(e); if (cOld != cNew) { unit.firePropertyChange(Unit.EQUIPMENT_CHANGE, cOld, cNew); } } } if (unit.getGoodsContainer() != null) { unit.getGoodsContainer().fireChanges(); } }