/** * 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; }
public String toString() { Tile tile = colony.getTile(); String eqStr = "/"; for (EquipmentType e : equipment.keySet()) { eqStr += e.toString().substring(16, 17); } String locStr = (loc == null) ? "" : (loc instanceof Building) ? Utils.lastPart(((Building) loc).getType().toString(), ".") : (loc instanceof ColonyTile) ? tile.getDirection(((ColonyTile) loc).getWorkTile()).toString() : (loc instanceof Tile) ? (loc.getId() + eqStr) : loc.getId(); Location newLoc = unit.getLocation(); String newEqStr = "/"; for (EquipmentType e : unit.getEquipment().keySet()) { newEqStr += e.toString().substring(16, 17); } String newLocStr = (newLoc == null) ? "" : (newLoc instanceof Building) ? Utils.lastPart(((Building) newLoc).getType().toString(), ".") : (newLoc instanceof ColonyTile) ? tile.getDirection(((ColonyTile) newLoc).getWorkTile()).toString() : (newLoc instanceof Tile) ? (newLoc.getId() + newEqStr) : newLoc.getId(); GoodsType newWork = unit.getWorkType(); int newWorkAmount = (newWork == null) ? 0 : getAmount(newLoc, newWork); return String.format( "%-30s %-25s -> %-25s", unit.getId() + ":" + Utils.lastPart(unit.getType().toString(), "."), locStr + ((work == null || workAmount <= 0) ? "" : "(" + Integer.toString(workAmount) + " " + Utils.lastPart(work.toString(), ".") + ")"), newLocStr + ((newWork == null || newWorkAmount <= 0) ? "" : "(" + Integer.toString(newWorkAmount) + " " + Utils.lastPart(newWork.toString(), ".") + ")")) .trim(); }
/** 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(); } }