if (extraLocks != 0) { TrackTile tt = world.getTile((Point) e.getValue()).getTrackTile(); if (! tt.getLock(extraLocks)) { undoNeeded = true; break; } undoList.put(e.getKey(), new Byte(extraLocks)); }
undoList.put(e.getKey(), new Byte(extraLocks));
    private void updateTrainPosition(TrainModel tm) {	System.out.println("updating position of " + tm);	final HashMap newMapTiles = new HashMap();	final HashMap oldMapTiles = new HashMap();	final HashMap undoList = new HashMap();	GameTime now = (GameTime) world.get(ITEM.TIME, Player.AUTHORITATIVE);	TrainMotionModel tmm = tm.getTrainMotionModel();	if (tmm.isBlocked() &&		! acquireAllLocks(tm))	    return;	/* work out where we should be */	/* speed in tiles per BigTick */	float speed = ((float) ((EngineType) world.get(KEY.ENGINE_TYPES,		    tm.getEngineType(), Player.AUTHORITATIVE)).getMaxSpeed()) /			EngineType.TILE_HOURS_PER_MILE_BIGTICKS;	/* speed in deltas per tick */	speed *= TrackTile.DELTAS_PER_TILE / GameTime.TICKS_PER_BIG_TICK;	int ticksSinceLastSync = now.getTime() -	    tmm.getTimeOfLastSync().getTime();	int distanceToTarget = (int) (tmm.getPathToDestination().getLength() +	    tmm.getPathTraversedSinceLastSync().getLength() - speed *	    ticksSinceLastSync);	if (distanceToTarget < 0)	    distanceToTarget = 0;	assert distanceToTarget <= tmm.getPathToDestination().getLength();	TrainPath tp =	    tmm.getPathToDestination().truncateTail(distanceToTarget);	/* check to see if we have crossed a tile boundary */	newMapTiles.clear();	oldMapTiles.clear();	undoList.clear();	tp.getMapCoordsAndDirections(newMapTiles);	Iterator i = newMapTiles.entrySet().iterator();	boolean undoNeeded = false;	while (i.hasNext()) {	    Byte b;	    byte extraLocks;	    Entry e = (Entry) i.next();	    if ((b = (Byte) oldMapTiles.get(e.getValue())) != null) {		extraLocks = (byte) ((~b.byteValue()) & ((Byte)			e.getValue()).byteValue());	    } else {		extraLocks = ((Byte) e.getValue()).byteValue();	    }	    if (extraLocks != 0) {		TrackTile tt = world.getTile((Point)			e.getValue()).getTrackTile();		if (! tt.getLock(extraLocks)) {		    undoNeeded = true;		    break;		}		undoList.put(e.getKey(), new Byte(extraLocks));	    }	}	if (undoNeeded) {	    /* undo all locks, restore the pathToDestination and bail out */	    i = undoList.entrySet().iterator();	    while (i.hasNext()) {		Entry e = (Entry) i.next();		world.getTile((Point) e.getKey()).		    getTrackTile().releaseLock		    (((Byte) e.getValue()).byteValue());	    }	    tmm.getPathToDestination().append(tp);	    return;	}	TrainPath pos = tm.getPosition();	TrainPath removed = pos.moveHeadTo(tp);	/* unlock any tiles we are no longer using */	newMapTiles.clear();	pos.getMapCoordsAndDirections(newMapTiles);	removed.getMapCoordsAndDirections(oldMapTiles);	i = oldMapTiles.entrySet().iterator();	while (i.hasNext()) {	    Entry e = (Entry) i.next();	    Byte b;	    byte locksToRemove = ((Byte) e.getValue()).byteValue();	    if ((b = (Byte) newMapTiles.get((Point) e.getKey())) != null)		locksToRemove &= ~b.byteValue();	    TrackTile tt = world.getTile((Point) e.getKey()).getTrackTile();	    tt.releaseLock(locksToRemove);	}	/* increment the pathTraversedSinceLastSync */	tmm.getPathTraversedSinceLastSync().prepend(removed);    }