From ad2d86def8eb857a899a49ce88a0c2a9d6256d2c Mon Sep 17 00:00:00 2001
From: jsalmon3 <>
Date: Fri, 3 Oct 2003 17:48:23 +0000
Subject: [PATCH] Cleanup

---
 src/action/command.cpp | 933 ++++++++++++++++++++---------------------
 1 file changed, 466 insertions(+), 467 deletions(-)

diff --git a/src/action/command.cpp b/src/action/command.cpp
index 7b1372a22..c79292fc8 100644
--- a/src/action/command.cpp
+++ b/src/action/command.cpp
@@ -61,13 +61,13 @@
 */
 local void ReleaseOrder(Order* order)
 {
-    if( order->Goal ) {
-	RefsDebugCheck( !order->Goal->Refs );
-	if( !--order->Goal->Refs ) {
-	    DebugCheck( !order->Goal->Destroyed );
+    if (order->Goal) {
+	RefsDebugCheck(!order->Goal->Refs);
+	if (!--order->Goal->Refs) {
+	    DebugCheck(!order->Goal->Destroyed);
 	    ReleaseUnit(order->Goal);
 	}
-	order->Goal=NoUnitP;
+	order->Goal = NoUnitP;
     }
 }
 
@@ -80,13 +80,13 @@ local void ReleaseOrders(Unit* unit)
 {
     int n;
 
-    if( (n=unit->OrderCount)>1 ) {
-	while( --n ) {
+    if ((n = unit->OrderCount) > 1) {
+	while (--n) {
 	    ReleaseOrder(&unit->Orders[n]);
 	}
-	unit->OrderCount=1;
+	unit->OrderCount = 1;
     }
-    unit->OrderFlush=1;
+    unit->OrderFlush = 1;
     // Order 0 must be stopped in the action loop.
 }
 
@@ -98,15 +98,16 @@ local void ReleaseOrders(Unit* unit)
 **
 **	@return		Pointer to next free order slot.
 */
-local Order* GetNextOrder(Unit* unit,int flush)
+local Order* GetNextOrder(Unit* unit, int flush)
 {
-    if( flush ) {			// empty command queue
+    if (flush) {			// empty command queue
 	ReleaseOrders(unit);
-    } else if( unit->OrderCount==MAX_ORDERS ) {
+    } else if (unit->OrderCount == MAX_ORDERS) {
 	// FIXME: johns: wrong place for an error message.
 	// FIXME: johns: should be checked by AI or the user interface
 	// NOTE: But must still be checked here.
-	NotifyPlayer(unit->Player,NotifyYellow,unit->X,unit->Y,"Unit order list is full" );
+	NotifyPlayer(unit->Player, NotifyYellow, unit->X, unit->Y,
+	    "Unit order list is full");
 	return NULL;
     }
 
@@ -125,10 +126,10 @@ local void ClearSavedAction(Unit* unit)
 {
     ReleaseOrder(&unit->SavedOrder);
 
-    unit->SavedOrder.Action=UnitActionStill;	// clear saved action
-    unit->SavedOrder.X=unit->SavedOrder.Y=-1;
-    unit->SavedOrder.Type=NULL;
-    unit->SavedOrder.Arg1=NULL;
+    unit->SavedOrder.Action = UnitActionStill;	// clear saved action
+    unit->SavedOrder.X = unit->SavedOrder.Y = -1;
+    unit->SavedOrder.Type = NULL;
+    unit->SavedOrder.Arg1 = NULL;
 }
 
 /*----------------------------------------------------------------------------
@@ -145,37 +146,37 @@ global void CommandQuit(int player)
     int i;
     int j;
 
-    if( Players[player].TotalNumUnits!=0 ) {
+    if (Players[player].TotalNumUnits != 0) {
 	// Set player to neutral, remove allied/enemy/shared vision status
-	Players[player].Type=PlayerNeutral;
-	for( i=0; i<NumPlayers; ++i) {
-	    if( i==player ) {
+	Players[player].Type = PlayerNeutral;
+	for (i = 0; i < NumPlayers; ++i) {
+	    if (i == player) {
 		continue;
 	    }
-	    Players[i].Allied&=~(1 << player);
-	    Players[i].Enemy&=~(1 << player);
+	    Players[i].Allied &= ~(1 << player);
+	    Players[i].Enemy &= ~(1 << player);
 	    // Check all tiles and mark unseen ones as explored.
-	    if( Players[player].SharedVision&(1<<i)
-		    && (Players[i].SharedVision&(1<<player)) ) {
-		for( j=0; j<TheMap.Width*TheMap.Height; ++j) {
-		    if( TheMap.Fields[j].Visible[i]
-			    && !TheMap.Fields[j].Visible[player] ) {
-			TheMap.Fields[j].Visible[player]=1;
+	    if (Players[player].SharedVision & (1 << i) &&
+		    (Players[i].SharedVision & (1 << player))) {
+		for (j = 0; j < TheMap.Width*TheMap.Height; ++j) {
+		    if (TheMap.Fields[j].Visible[i] &&
+			    !TheMap.Fields[j].Visible[player]) {
+			TheMap.Fields[j].Visible[player] = 1;
 		    }
-		    if( TheMap.Fields[j].Visible[player]
-			    && !TheMap.Fields[j].Visible[i] ) {
-			TheMap.Fields[j].Visible[i]=1;
+		    if (TheMap.Fields[j].Visible[player] &&
+			    !TheMap.Fields[j].Visible[i]) {
+			TheMap.Fields[j].Visible[i] = 1;
 		    }
 		}
 	    }
-	    Players[i].SharedVision&=~(1<<player);
-	    Players[player].Allied&=~(1<<i);
-	    Players[player].Enemy&=~(1<<i);
-	    Players[player].SharedVision&=~(1<<i);
+	    Players[i].SharedVision &= ~(1 << player);
+	    Players[player].Allied &= ~(1 << i);
+	    Players[player].Enemy & =~(1 << i);
+	    Players[player].SharedVision &= ~(1 << i);
 	}
-	SetMessage("Player \"%s\" has left the game",Players[player].Name);
+	SetMessage("Player \"%s\" has left the game", Players[player].Name);
     } else {
-	SetMessage("Player \"%s\" has been killed",Players[player].Name);
+	SetMessage("Player \"%s\" has been killed", Players[player].Name);
     }
 }
 
@@ -190,18 +191,18 @@ global void CommandStopUnit(Unit* unit)
 
     // Ignore that the unit could be removed.
 
-    order=GetNextOrder(unit,FlushCommands);	// Flush them.
-    order->Action=UnitActionStill;
-    order->X=order->Y=-1;
-    order->Goal=NoUnitP;
-    order->Type=NULL;
-    order->Arg1=NULL;
+    order = GetNextOrder(unit, FlushCommands);	// Flush them.
+    order->Action = UnitActionStill;
+    order->X = order->Y = -1;
+    order->Goal = NoUnitP;
+    order->Type = NULL;
+    order->Arg1 = NULL;
     ReleaseOrder(&unit->SavedOrder);
     ReleaseOrder(&unit->NewOrder);
 #ifdef HIERARCHIC_PATHFINDER
     PfHierReleaseData(unit);
 #endif
-    unit->SavedOrder=unit->NewOrder=*order;
+    unit->SavedOrder = unit->NewOrder = *order;
 }
 
 /**
@@ -210,24 +211,24 @@ global void CommandStopUnit(Unit* unit)
 **	@param unit	pointer to unit.
 **	@param flush	if true, flush command queue.
 */
-global void CommandStandGround(Unit* unit,int flush)
+global void CommandStandGround(Unit* unit, int flush)
 {
     Order* order;
 
     // Ignore that the unit could be removed.
 
-    if( unit->Type->Building ) {
+    if (unit->Type->Building) {
 	// FIXME: should find a better way for pending orders.
-	order=&unit->NewOrder;
+	order = &unit->NewOrder;
 	ReleaseOrder(order);
-    } else if( !(order=GetNextOrder(unit,flush)) ) {
+    } else if (!(order = GetNextOrder(unit, flush))) {
 	return;
     }
-    order->Action=UnitActionStandGround;
-    order->X=order->Y=-1;
-    order->Goal=NoUnitP;
-    order->Type=NULL;
-    order->Arg1=NULL;
+    order->Action = UnitActionStandGround;
+    order->X = order->Y = -1;
+    order->Goal = NoUnitP;
+    order->Type = NULL;
+    order->Arg1 = NULL;
     ClearSavedAction(unit);
 }
 
@@ -238,42 +239,42 @@ global void CommandStandGround(Unit* unit,int flush)
 **	@param dest	unit to be followed
 **	@param flush	if true, flush command queue.
 */
-global void CommandFollow(Unit* unit,Unit* dest,int flush)
+global void CommandFollow(Unit* unit, Unit* dest, int flush)
 {
     Order* order;
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	if( unit->Type->Building ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionFollow;
+	order->Action = UnitActionFollow;
 	//
 	//	Destination could be killed.
 	//	Should be handled in action, but is not possible!
 	//		Unit::Refs is used as timeout counter.
 	//
-	if( dest->Destroyed ) {
-	    order->X=dest->X+dest->Type->TileWidth/2;
-	    order->Y=dest->Y+dest->Type->TileHeight/2;
-	    order->Goal=NoUnitP;
-	    order->RangeX=order->RangeY=0;
+	if (dest->Destroyed) {
+	    order->X = dest->X + dest->Type->TileWidth / 2;
+	    order->Y = dest->Y + dest->Type->TileHeight / 2;
+	    order->Goal = NoUnitP;
+	    order->RangeX = order->RangeY = 0;
 	} else {
-	    order->X=order->Y=-1;
-	    order->Goal=dest;
-	    RefsDebugCheck( !dest->Refs );
+	    order->X = order->Y = -1;
+	    order->Goal = dest;
+	    RefsDebugCheck(!dest->Refs);
 	    dest->Refs++;
-	    order->RangeX=order->RangeY=1;
+	    order->RangeX = order->RangeY = 1;
 	}
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->Type = NULL;
+	order->Arg1 = NULL;
     }
     ClearSavedAction(unit);
 }
@@ -286,36 +287,36 @@ global void CommandFollow(Unit* unit,Unit* dest,int flush)
 **	@param y	Y map position to move to.
 **	@param flush	if true, flush command queue.
 */
-global void CommandMove(Unit* unit,int x,int y,int flush)
+global void CommandMove(Unit* unit, int x, int y, int flush)
 {
     Order* order;
 
-    IfDebug(
-	if( x<0 || y<0 || x>=TheMap.Width || y>=TheMap.Height ) {
-	    DebugLevel0Fn("Internal movement error\n");
-	    return;
-	}
-    );
+#ifdef DEBUG
+    if (x < 0 || y < 0 || x >= TheMap.Width || y >= TheMap.Height) {
+	DebugLevel0Fn("Internal movement error\n");
+	return;
+    }
+#endif
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	if( unit->Type->Building ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionMove;
-	order->Goal=NoUnitP;
-	order->X=x;
-	order->Y=y;
-	order->RangeX=order->RangeY=0;
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->Action = UnitActionMove;
+	order->Goal = NoUnitP;
+	order->X = x;
+	order->Y = y;
+	order->RangeX = order->RangeY = 0;
+	order->Type = NULL;
+	order->Arg1 = NULL;
 #ifdef HIERARCHIC_PATHFINDER
 	/*
 	 * EXPERIMENTAL: This indicates this unit's intent to move.
@@ -325,7 +326,7 @@ global void CommandMove(Unit* unit,int x,int y,int flush)
 	 * that this unit intents to move asap.
 	 */
 	unit->Moving = 1;
-	PfHierReleaseData (unit);
+	PfHierReleaseData(unit);
 #endif
     }
     ClearSavedAction(unit);
@@ -340,49 +341,49 @@ global void CommandMove(Unit* unit,int x,int y,int flush)
 **	@param dest	or unit to be repaired. FIXME: not supported
 **	@param flush	if true, flush command queue.
 */
-global void CommandRepair(Unit* unit,int x,int y,Unit* dest,int flush)
+global void CommandRepair(Unit* unit, int x, int y, Unit* dest, int flush)
 {
     Order* order;
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	if( unit->Type->Building ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionRepair;
+	order->Action = UnitActionRepair;
 	//
 	//	Destination could be killed.
 	//	Should be handled in action, but is not possible!
 	//		Unit::Refs is used as timeout counter.
 	//
-	if( dest ) {
-	    if( dest->Destroyed ) {
-		order->X=dest->X+dest->Type->TileWidth/2;
-		order->Y=dest->Y+dest->Type->TileHeight/2;
-		order->Goal=NoUnitP;
-		order->RangeX=order->RangeY=0;
+	if (dest) {
+	    if (dest->Destroyed) {
+		order->X = dest->X + dest->Type->TileWidth / 2;
+		order->Y = dest->Y + dest->Type->TileHeight / 2;
+		order->Goal = NoUnitP;
+		order->RangeX = order->RangeY = 0;
 	    } else {
-		order->X=order->Y=-1;
-		order->Goal=dest;
-		RefsDebugCheck( !dest->Refs );
+		order->X = order->Y = -1;
+		order->Goal = dest;
+		RefsDebugCheck(!dest->Refs);
 		dest->Refs++;
-		order->RangeX=order->RangeY=unit->Type->RepairRange;
+		order->RangeX = order->RangeY = unit->Type->RepairRange;
 	    }
 	} else {
-	    order->X=x;
-	    order->Y=y;
-	    order->Goal=NoUnitP;
-	    order->RangeX=order->RangeY=unit->Type->RepairRange;
+	    order->X = x;
+	    order->Y = y;
+	    order->Goal = NoUnitP;
+	    order->RangeX = order->RangeY = unit->Type->RepairRange;
 	}
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->Type = NULL;
+	order->Arg1 = NULL;
     }
     ClearSavedAction(unit);
 }
@@ -396,65 +397,65 @@ global void CommandRepair(Unit* unit,int x,int y,Unit* dest,int flush)
 **	@param attack	or unit to be attacked.
 **	@param flush	if true, flush command queue.
 */
-global void CommandAttack(Unit* unit,int x,int y,Unit* attack,int flush)
+global void CommandAttack(Unit* unit, int x, int y, Unit* attack, int flush)
 {
     Order* order;
 
-#if DEBUG
-	if( x<0 || y<0 || x>=TheMap.Width || y>=TheMap.Height ) {
-	    DebugLevel0Fn("Internal movement error\n");
-	    return;
-	}
+#ifdef DEBUG
+    if (x < 0 || y < 0 || x >= TheMap.Width || y >= TheMap.Height) {
+	DebugLevel0Fn("Internal movement error\n");
+	return;
+    }
 #endif
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	if( unit->Type->Building ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionAttack;
-	if( attack ) {
+	order->Action = UnitActionAttack;
+	if (attack) {
 	    //
 	    //	Destination could be killed.
 	    //	Should be handled in action, but is not possible!
 	    //		Unit::Refs is used as timeout counter.
 	    //
-	    if( attack->Destroyed ) {
-		order->X=attack->X+attack->Type->TileWidth/2;
-		order->Y=attack->Y+attack->Type->TileHeight/2;
-		order->Goal=NoUnitP;
-		order->RangeX=order->RangeY=0;
+	    if (attack->Destroyed) {
+		order->X = attack->X + attack->Type->TileWidth / 2;
+		order->Y = attack->Y + attack->Type->TileHeight / 2;
+		order->Goal = NoUnitP;
+		order->RangeX = order->RangeY = 0;
 	    } else {
 		// Removed, Dying handled by action routine.
-		order->X=order->Y=-1;
-		order->Goal=attack;
-		RefsDebugCheck( !attack->Refs );
+		order->X = order->Y = -1;
+		order->Goal = attack;
+		RefsDebugCheck(!attack->Refs);
 		attack->Refs++;
-		order->RangeX=order->RangeY=unit->Stats->AttackRange;
-		order->MinRange=unit->Type->MinAttackRange;
+		order->RangeX = order->RangeY = unit->Stats->AttackRange;
+		order->MinRange = unit->Type->MinAttackRange;
 	    }
-	} else if( WallOnMap(x,y) ) {
+	} else if (WallOnMap(x,y)) {
 	    // FIXME: look into action_attack.c about this ugly problem
-	    order->X=x;
-	    order->Y=y;
-	    order->RangeX=order->RangeY=unit->Stats->AttackRange;
-	    order->MinRange=unit->Type->MinAttackRange;
-	    order->Goal=NoUnitP;
+	    order->X = x;
+	    order->Y = y;
+	    order->RangeX = order->RangeY = unit->Stats->AttackRange;
+	    order->MinRange = unit->Type->MinAttackRange;
+	    order->Goal = NoUnitP;
 	} else {
-	    order->X=x;
-	    order->Y=y;
-	    order->RangeX=order->RangeY=0;
-	    order->Goal=NoUnitP;
+	    order->X = x;
+	    order->Y = y;
+	    order->RangeX = order->RangeY = 0;
+	    order->Goal = NoUnitP;
 	}
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->Type = NULL;
+	order->Arg1 = NULL;
 
     }
     ClearSavedAction(unit);
@@ -468,37 +469,37 @@ global void CommandAttack(Unit* unit,int x,int y,Unit* attack,int flush)
 **	@param y	Y map position to fire on.
 **	@param flush	if true, flush command queue.
 */
-global void CommandAttackGround(Unit* unit,int x,int y,int flush)
+global void CommandAttackGround(Unit* unit, int x, int y, int flush)
 {
     Order* order;
 
-    IfDebug(
-	if( x<0 || y<0 || x>=TheMap.Width || y>=TheMap.Height ) {
-	    DebugLevel0Fn("Internal movement error\n");
-	    return;
-	}
-    );
+#ifdef DEBUG
+    if (x < 0 || y < 0 || x >= TheMap.Width || y >= TheMap.Height) {
+	DebugLevel0Fn("Internal movement error\n");
+	return;
+    }
+#endif
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	if( unit->Type->Building ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionAttackGround;
-	order->X=x;
-	order->Y=y;
-	order->RangeX=order->RangeY=unit->Stats->AttackRange;
-	order->MinRange=unit->Type->MinAttackRange;
-	order->Goal=NoUnitP;
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->Action = UnitActionAttackGround;
+	order->X = x;
+	order->Y = y;
+	order->RangeX = order->RangeY = unit->Stats->AttackRange;
+	order->MinRange = unit->Type->MinAttackRange;
+	order->Goal = NoUnitP;
+	order->Type = NULL;
+	order->Arg1 = NULL;
 
 	DebugLevel0("FIXME this next\n");
     }
@@ -515,38 +516,38 @@ global void CommandAttackGround(Unit* unit,int x,int y,int flush)
 **	@param y	Y map position to patrol between.
 **	@param flush	if true, flush command queue.
 */
-global void CommandPatrolUnit(Unit* unit,int x,int y,int flush)
+global void CommandPatrolUnit(Unit* unit, int x, int y, int flush)
 {
     Order* order;
 
-    IfDebug(
-	if( x<0 || y<0 || x>=TheMap.Width || y>=TheMap.Height ) {
-	    DebugLevel0Fn("Internal movement error\n");
-	    return;
-	}
-    );
+#ifdef DEBUG
+    if (x < 0 || y < 0 || x >= TheMap.Width || y >= TheMap.Height) {
+	DebugLevel0Fn("Internal movement error\n");
+	return;
+    }
+#endif
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	if( unit->Type->Building ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionPatrol;
-	order->Goal=NoUnitP;
-	order->X=x;
-	order->Y=y;
-	order->RangeX=order->RangeY=0;
-	order->Type=NULL;
-	DebugCheck( unit->X&~0xFFFF || unit->Y&~0xFFFF );
+	order->Action = UnitActionPatrol;
+	order->Goal = NoUnitP;
+	order->X = x;
+	order->Y = y;
+	order->RangeX = order->RangeY = 0;
+	order->Type = NULL;
+	DebugCheck(unit->X & ~0xFFFF || unit->Y & ~0xFFFF);
 	// BUG-ALERT: encode source into arg1 as two 16 bit values!
-	order->Arg1=(void*)((unit->X<<16)|unit->Y);
+	order->Arg1 = (void*)((unit->X << 16) | unit->Y);
     }
     ClearSavedAction(unit);
 }
@@ -558,39 +559,39 @@ global void CommandPatrolUnit(Unit* unit,int x,int y,int flush)
 **	@param dest	unit to be boarded.
 **	@param flush	if true, flush command queue.
 */
-global void CommandBoard(Unit* unit,Unit* dest,int flush)
+global void CommandBoard(Unit* unit, Unit* dest, int flush)
 {
     Order* order;
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
 	//
 	//	Destination could be killed.
 	//	Should be handled in action, but is not possible!
 	//		Unit::Refs is used as timeout counter.
 	//
-	if( dest->Destroyed ) {
+	if (dest->Destroyed) {
 	    return;
 	}
 
-	if( unit->Type->Building ) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionBoard;
-	order->X=order->Y=-1;
-	order->Goal=dest;
-	RefsDebugCheck( !dest->Refs );
+	order->Action = UnitActionBoard;
+	order->X = order->Y = -1;
+	order->Goal = dest;
+	RefsDebugCheck(!dest->Refs);
 	dest->Refs++;
-	order->RangeX=order->RangeY=1;
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->RangeX = order->RangeY = 1;
+	order->Type = NULL;
+	order->Arg1 = NULL;
     }
     ClearSavedAction(unit);
 }
@@ -604,39 +605,39 @@ global void CommandBoard(Unit* unit,Unit* dest,int flush)
 **	@param what	unit to be unloaded, NoUnitP all.
 **	@param flush	if true, flush command queue.
 */
-global void CommandUnload(Unit* unit,int x,int y,Unit* what,int flush)
+global void CommandUnload(Unit* unit, int x, int y, Unit* what, int flush)
 {
     Order* order;
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	if( unit->Type->Building ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionUnload;
-	order->X=x;
-	order->Y=y;
+	order->Action = UnitActionUnload;
+	order->X = x;
+	order->Y = y;
 	//
 	//	Destination could be killed.
 	//	Should be handled in action, but is not possible!
 	//		Unit::Refs is used as timeout counter.
 	//
-	order->Goal=NoUnitP;
-	if( what && !what->Destroyed ) {
-	    order->Goal=what;
-	    RefsDebugCheck( !what->Refs );
+	order->Goal = NoUnitP;
+	if (what && !what->Destroyed) {
+	    order->Goal = what;
+	    RefsDebugCheck(!what->Refs);
 	    what->Refs++;
 	}
-	order->RangeX=order->RangeY=0;
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->RangeX = order->RangeY = 0;
+	order->Type = NULL;
+	order->Arg1 = NULL;
     }
     ClearSavedAction(unit);
 }
@@ -650,38 +651,38 @@ global void CommandUnload(Unit* unit,int x,int y,Unit* what,int flush)
 **	@param what	Unit type to build.
 **	@param flush	if true, flush command queue.
 */
-global void CommandBuildBuilding(Unit* unit,int x,int y
-	,UnitType* what,int flush)
+global void CommandBuildBuilding(Unit* unit, int x, int y,
+    UnitType* what, int flush)
 {
     Order* order;
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	if( unit->Type->Building ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionBuild;
-	order->Goal=NoUnitP;
-	order->IsRect=1;
-	order->X=x;
-	order->Y=y;
-	if(what->BuilderOutside) {
-	    order->RangeX=unit->Type->RepairRange;
-	    order->RangeY=unit->Type->RepairRange;
+	order->Action = UnitActionBuild;
+	order->Goal = NoUnitP;
+	order->IsRect = 1;
+	order->X = x;
+	order->Y = y;
+	if (what->BuilderOutside) {
+	    order->RangeX = unit->Type->RepairRange;
+	    order->RangeY = unit->Type->RepairRange;
 	} else {
 	    // If building inside, but be next to stop
-	    order->RangeX=1;
-	    order->RangeY=1;
+	    order->RangeX = 1;
+	    order->RangeY = 1;
 	}
-	order->Type=what;
-	order->Arg1=NULL;
+	order->Type = what;
+	order->Arg1 = NULL;
     }
     ClearSavedAction(unit);
 }
@@ -693,13 +694,13 @@ global void CommandBuildBuilding(Unit* unit,int x,int y
 **	@param worker	pointer to unit.
 */
 global void CommandCancelBuilding(Unit* unit,
-	Unit* worker __attribute__((unused)))
+    Unit* worker __attribute__((unused)))
 {
     //
     //	Check if building is still under construction? (NETWORK!)
     //
-    if( unit->Orders[0].Action==UnitActionBuilded ) {
-	unit->Data.Builded.Cancel=1;
+    if (unit->Orders[0].Action == UnitActionBuilded) {
+	unit->Data.Builded.Cancel = 1;
     }
     ClearSavedAction(unit);
 }
@@ -712,7 +713,7 @@ global void CommandCancelBuilding(Unit* unit,
 **	@param y	Y map position for harvest.
 **	@param flush	if true, flush command queue.
 */
-global void CommandResourceLoc(Unit* unit,int x,int y,int flush)
+global void CommandResourceLoc(Unit* unit, int x, int y, int flush)
 {
     Order* order;
     int nx;
@@ -721,40 +722,42 @@ global void CommandResourceLoc(Unit* unit,int x,int y,int flush)
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	if( unit->Type->Building ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionResource;
+	order->Action = UnitActionResource;
 	
 	//  Find the closest piece of wood next to a tile where the unit can move
 	DebugLevel3("Want to harvest from %d,%d.\n" _C_ x _C_ y);
-	if (!FindTerrainType(0,(unit->Type->MovementMask),1,20,unit->Player,x,y,&nx,&ny)) {
+	if (!FindTerrainType(0, (unit->Type->MovementMask), 1, 20,
+		unit->Player, x, y, &nx, &ny)) {
 	    DebugLevel0Fn("FIXME: Give up???\n");
 	}
-	if (max(abs(nx-x),abs(ny-y))>1) {
+	if (max(abs(nx - x), abs(ny - y)) > 1) {
 	    DebugLevel3("Closest tile reachable is at %d,%d.\n" _C_ x _C_ y);
-	    if (!FindTerrainType(0,MapFieldForest,0,20,unit->Player,nx,ny,&nx,&ny)) {
+	    if (!FindTerrainType(0, MapFieldForest, 0, 20, unit->Player,
+		    nx, ny, &nx, &ny)) {
 		DebugLevel0Fn("FIXME: Give up???\n");
 	    }
 	} else {
 	    // The destination is next to a reacahble tile.
-	    nx=x;
-	    ny=y;
+	    nx = x;
+	    ny = y;
 	}
 	DebugLevel3("So the final destination is %d,%d.\n" _C_ nx _C_ ny);
-	order->X=nx;
-	order->Y=ny;
+	order->X = nx;
+	order->Y = ny;
 
-	order->RangeX=order->RangeY=1;
-	order->Goal=NoUnitP;
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->RangeX = order->RangeY = 1;
+	order->Goal = NoUnitP;
+	order->Type = NULL;
+	order->Arg1 = NULL;
     }
     ClearSavedAction(unit);
 }
@@ -766,39 +769,39 @@ global void CommandResourceLoc(Unit* unit,int x,int y,int flush)
 **	@param dest	destination unit.
 **	@param flush	if true, flush command queue.
 */
-global void CommandResource(Unit* unit,Unit* dest,int flush)
+global void CommandResource(Unit* unit, Unit* dest, int flush)
 {
     Order* order;
 
     //
     //	Check if unit is still valid and Goal still alive? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie
-	     && !dest->Destroyed ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie &&
+	    !dest->Destroyed) {
 	// FIXME: more races, could happen with many orders in queue.
-	if( !unit->Type->Building && !unit->Type->Harvester ) {
+	if (!unit->Type->Building && !unit->Type->Harvester) {
 	    ClearSavedAction(unit);
 	    return;
 	}
 
 	// FIXME: if low-level supports searching, pass NoUnitP down.
 
-	if( unit->Type->Building ) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionResource;
-	order->X=order->Y=-1;
-	order->Goal=dest;
-	RefsDebugCheck( !dest->Refs );
+	order->Action = UnitActionResource;
+	order->X = order->Y = -1;
+	order->Goal = dest;
+	RefsDebugCheck(!dest->Refs);
 	dest->Refs++;
-	order->RangeX=order->RangeY=1;
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->RangeX = order->RangeY = 1;
+	order->Type = NULL;
+	order->Arg1 = NULL;
     }
     ClearSavedAction(unit);
 }
@@ -810,44 +813,42 @@ global void CommandResource(Unit* unit,Unit* dest,int flush)
 **	@param goal	bring goods to this depot.
 **	@param flush	if true, flush command queue.
 */
-global void CommandReturnGoods(Unit* unit,Unit* goal,int flush)
+global void CommandReturnGoods(Unit* unit, Unit* goal, int flush)
 {
     Order* order;
 
     //
     //	Check if unit is still valid and Goal still alive? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
 	// FIXME: more races, could happen with many orders in queue.
-	if( !unit->Type->Building
-		&& !unit->Type->Harvester
-		&& !unit->Value ) { 
+	if (!unit->Type->Building && !unit->Type->Harvester && !unit->Value) {
 	    ClearSavedAction(unit);
 	    return;
 	}
 
-	if( unit->Type->Building ) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionReturnGoods;
-	order->X=order->Y=-1;
-	order->Goal=NoUnitP;
+	order->Action = UnitActionReturnGoods;
+	order->X = order->Y = -1;
+	order->Goal = NoUnitP;
 	//
 	//	Destination could be killed. NETWORK!
 	//
-	if( goal && !goal->Destroyed ) {
-	    order->Goal=goal;
-	    RefsDebugCheck( !goal->Refs );
+	if (goal && !goal->Destroyed) {
+	    order->Goal = goal;
+	    RefsDebugCheck(!goal->Refs);
 	    goal->Refs++;
 	}
-	order->RangeX=order->RangeY=1;
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->RangeX = order->RangeY = 1;
+	order->Type = NULL;
+	order->Arg1 = NULL;
     }
     ClearSavedAction(unit);
 }
@@ -859,64 +860,63 @@ global void CommandReturnGoods(Unit* unit,Unit* goal,int flush)
 **	@param type	unit type to train.
 **	@param flush	if true, flush command queue.
 */
-global void CommandTrainUnit(Unit* unit,UnitType* type,
-	int flush __attribute__((unused)))
+global void CommandTrainUnit(Unit* unit, UnitType* type,
+    int flush __attribute__((unused)))
 {
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
 	//
 	//	Check if enough resources remains? (NETWORK!)
 	//	FIXME: wrong if append to message queue!!!
 	//
-	if( !PlayerCheckFood(unit->Player,type)
-		|| !PlayerCheckLimits(unit->Player,type)
-		|| PlayerCheckUnitType(unit->Player,type) ) {
+	if (!PlayerCheckFood(unit->Player, type) ||
+		!PlayerCheckLimits(unit->Player, type) ||
+		PlayerCheckUnitType(unit->Player, type)) {
 	    return;
 	}
 
 	//
 	//	Not already training?
 	//
-	if( unit->Orders[0].Action!=UnitActionTrain ) {
+	if (unit->Orders[0].Action != UnitActionTrain) {
+	    DebugCheck(unit->Wait > 6);
 
-	    DebugCheck( unit->Wait>6 );
-
-	    if( unit->OrderCount==2 && unit->Orders[1].Action==UnitActionTrain ) {
+	    if (unit->OrderCount == 2 && unit->Orders[1].Action == UnitActionTrain) {
 		DebugLevel0Fn("FIXME: not supported. Unit queue full!\n");
 		return;
 	    } else {
 		ReleaseOrders(unit);
-		unit->Orders[1].Action=UnitActionTrain;
+		unit->Orders[1].Action = UnitActionTrain;
 	    }
-	    DebugCheck( unit->OrderCount!=1 || unit->OrderFlush!=1 );
+	    DebugCheck(unit->OrderCount != 1 || unit->OrderFlush != 1);
 
-	    unit->OrderCount=2;
-	    unit->Orders[1].Type=type;
-	    unit->Orders[1].X=unit->Orders[1].Y=-1;
-	    unit->Orders[1].Goal=NoUnitP;
-	    unit->Orders[1].Arg1=NULL;
+	    unit->OrderCount = 2;
+	    unit->Orders[1].Type = type;
+	    unit->Orders[1].X = unit->Orders[1].Y = -1;
+	    unit->Orders[1].Goal = NoUnitP;
+	    unit->Orders[1].Arg1 = NULL;
 	} else {
 	    //
 	    //	Update interface.
 	    //
-	    if( unit->Player==ThisPlayer && unit->Selected ) {
-		MustRedraw|=RedrawInfoPanel;
+	    if (unit->Player == ThisPlayer && unit->Selected) {
+		MustRedraw |= RedrawInfoPanel;
 	    }
 
 	    //
 	    //	Training slots are all already full. (NETWORK!)
 	    //
-	    if( !EnableTrainingQueue || unit->Data.Train.Count>=MAX_UNIT_TRAIN ) {
+	    if (!EnableTrainingQueue || unit->Data.Train.Count >= MAX_UNIT_TRAIN) {
 		DebugLevel0Fn("Unit queue full!\n");
 		return;
 	    }
 
-	    unit->Data.Train.What[unit->Data.Train.Count++]=type;
+	    unit->Data.Train.What[unit->Data.Train.Count++] = type;
 	}
 	// FIXME: if you give quick an other order, the resources are lost!
-	PlayerSubUnitType(unit->Player,type);
+	PlayerSubUnitType(unit->Player, type);
     }
     ClearSavedAction(unit);
 }
@@ -934,7 +934,7 @@ global void CommandCancelTraining(Unit* unit, int slot, const UnitType* type)
     int n;
 
     DebugLevel0Fn("Cancel %d type: %s\n" _C_ slot _C_
-	    type ? type->Ident : "-any-");
+	type ? type->Ident : "-any-");
 
     ClearSavedAction(unit);
 
@@ -958,18 +958,17 @@ global void CommandCancelTraining(Unit* unit, int slot, const UnitType* type)
 	DebugLevel0Fn("Cancel training\n");
 
 	PlayerAddCostsFactor(unit->Player,
-		unit->Data.Train.What[slot]->Stats[unit->Player->Player].Costs,
-		CancelTrainingCostsFactor);
+	    unit->Data.Train.What[slot]->Stats[unit->Player->Player].Costs,
+	    CancelTrainingCostsFactor);
 
 	if (--n) {
 	    // Copy the other slots down
-	    for (i = slot; i < n; i++) {
+	    for (i = slot; i < n; ++i) {
 		unit->Data.Train.What[i] = unit->Data.Train.What[i + 1];
 	    }
 	    if (!slot) {		// Canceled in work slot
 		unit->Data.Train.Ticks = 0;
-		unit->Wait = 
-		    unit->Reset = 1;	// immediately start next training
+		unit->Wait =  unit->Reset = 1;	// immediately start next training
 	    }
 	    unit->Data.Train.Count = n;
 	} else {
@@ -996,36 +995,36 @@ global void CommandCancelTraining(Unit* unit, int slot, const UnitType* type)
 **	@param type	upgrade to type
 **	@param flush	if true, flush command queue.
 */
-global void CommandUpgradeTo(Unit* unit,UnitType* type,int flush)
+global void CommandUpgradeTo(Unit* unit, UnitType* type, int flush)
 {
     Order* order;
 
     //
     //	Check if unit is still valid and Goal still alive? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
 	//
 	//	Check if enough resources remains? (NETWORK!)
 	//
-	if( PlayerCheckUnitType(unit->Player,type) ) {
+	if (PlayerCheckUnitType(unit->Player, type)) {
 	    return;
 	}
 
-	if( !flush ) {
+	if (!flush) {
 	    DebugLevel0Fn("FIXME: must support order queing!!");
 	}
-	if( !(order=GetNextOrder(unit,flush)) ) {
+	if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
 	// FIXME: if you give quick an other order, the resources are lost!
-	PlayerSubUnitType(unit->Player,type);
+	PlayerSubUnitType(unit->Player, type);
 
-	order->Action=UnitActionUpgradeTo;
-	order->X=order->Y=-1;
-	order->Goal=NoUnitP;
-	order->Type=type;
-	order->Arg1=NULL;
+	order->Action = UnitActionUpgradeTo;
+	order->X = order->Y = -1;
+	order->Goal = NoUnitP;
+	order->Type = type;
+	order->Arg1 = NULL;
     }
     ClearSavedAction(unit);
 }
@@ -1042,29 +1041,29 @@ global void CommandCancelUpgradeTo(Unit* unit)
     //
     //	Check if unit is still upgrading? (NETWORK!)
     //
-    if( unit->Orders[0].Action == UnitActionUpgradeTo ) {
+    if (unit->Orders[0].Action == UnitActionUpgradeTo) {
 
 	PlayerAddCostsFactor(unit->Player,
-		unit->Orders[0].Type->Stats->Costs,
-		CancelUpgradeCostsFactor);
+	    unit->Orders[0].Type->Stats->Costs,
+	    CancelUpgradeCostsFactor);
 
-	unit->Orders[0].Action=UnitActionStill;
-	unit->Orders[0].X=unit->Orders[0].Y=-1;
-	unit->Orders[0].Goal=NoUnitP;
-	unit->Orders[0].Type=NULL;
-	unit->Orders[0].Arg1=NULL;
+	unit->Orders[0].Action = UnitActionStill;
+	unit->Orders[0].X = unit->Orders[0].Y = -1;
+	unit->Orders[0].Goal = NoUnitP;
+	unit->Orders[0].Type = NULL;
+	unit->Orders[0].Arg1 = NULL;
 
-	unit->SubAction=0;
+	unit->SubAction = 0;
 
 	//
 	//	Update interface.
 	//
-	if( unit->Player==ThisPlayer && unit->Selected ) {
+	if (unit->Player == ThisPlayer && unit->Selected) {
 	    SelectedUnitChanged();
-	    MustRedraw|=RedrawInfoPanel;
+	    MustRedraw |= RedrawInfoPanel;
 	}
 
-	unit->Wait=unit->Reset=1;	// immediately start next command.
+	unit->Wait = unit->Reset = 1;	// immediately start next command.
     }
     ClearSavedAction(unit);
 }
@@ -1076,49 +1075,49 @@ global void CommandCancelUpgradeTo(Unit* unit)
 **	@param what	what to research.
 **	@param flush	if true, flush command queue.
 */
-global void CommandResearch(Unit* unit,Upgrade* what,int flush)
+global void CommandResearch(Unit* unit, Upgrade* what, int flush)
 {
     Order* order;
 
     //
     //	Check if unit is still valid and Goal still alive? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
 	//
 	//	Check if enough resources remains? (NETWORK!)
 	//
-	if( PlayerCheckCosts(unit->Player,what->Costs) ) {
+	if (PlayerCheckCosts(unit->Player, what->Costs)) {
 	    return;
 	}
 
-	if( !flush ) {
+	if (!flush) {
 	    DebugLevel0Fn("FIXME: must support order queing!!");
 	} else {
-	    if( unit->Orders[0].Action==UnitActionResearch ) {
+	    if (unit->Orders[0].Action == UnitActionResearch) {
 		const Upgrade* upgrade;
 
 		// Cancel current research
-		upgrade=unit->Data.Research.Upgrade;
-		unit->Player->UpgradeTimers.Upgrades[upgrade-Upgrades]=0;
+		upgrade = unit->Data.Research.Upgrade;
+		unit->Player->UpgradeTimers.Upgrades[upgrade-Upgrades] = 0;
 		PlayerAddCostsFactor(unit->Player,upgrade->Costs,
-			CancelResearchCostsFactor);
-		unit->SubAction=0;
-		unit->Wait=unit->Reset=1;    // immediately start next command.
+		    CancelResearchCostsFactor);
+		unit->SubAction = 0;
+		unit->Wait = unit->Reset = 1;    // immediately start next command.
 	    }
 	}
 
-	if( !(order=GetNextOrder(unit,flush)) ) {
+	if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
 	// FIXME: if you give quick an other order, the resources are lost!
-	PlayerSubCosts(unit->Player,what->Costs);
+	PlayerSubCosts(unit->Player, what->Costs);
 
-	order->Action=UnitActionResearch;
-	order->X=order->Y=-1;
-	order->Goal=NoUnitP;
-	order->Type=NULL;
-	order->Arg1=what;
+	order->Action = UnitActionResearch;
+	order->X = order->Y = -1;
+	order->Goal = NoUnitP;
+	order->Type = NULL;
+	order->Arg1 = what;
     }
     ClearSavedAction(unit);
 }
@@ -1135,32 +1134,32 @@ global void CommandCancelResearch(Unit* unit)
     //
     //	Check if unit is still researching? (NETWORK!)
     //
-    if( unit->Orders[0].Action == UnitActionResearch ) {
+    if (unit->Orders[0].Action == UnitActionResearch) {
 	const Upgrade* upgrade;
 
-	upgrade=unit->Data.Research.Upgrade;
-	unit->Player->UpgradeTimers.Upgrades[upgrade-Upgrades]=0;
+	upgrade = unit->Data.Research.Upgrade;
+	unit->Player->UpgradeTimers.Upgrades[upgrade-Upgrades] = 0;
 
 	PlayerAddCostsFactor(unit->Player,upgrade->Costs,
-		CancelResearchCostsFactor);
+	    CancelResearchCostsFactor);
 
-	unit->Orders[0].Action=UnitActionStill;
-	unit->Orders[0].X=unit->Orders[0].Y=-1;
-	unit->Orders[0].Goal=NoUnitP;
-	unit->Orders[0].Type=NULL;
-	unit->Orders[0].Arg1=NULL;
+	unit->Orders[0].Action = UnitActionStill;
+	unit->Orders[0].X = unit->Orders[0].Y = -1;
+	unit->Orders[0].Goal = NoUnitP;
+	unit->Orders[0].Type = NULL;
+	unit->Orders[0].Arg1 = NULL;
 
-	unit->SubAction=0;
+	unit->SubAction = 0;
 
 	//
 	//	Update interface.
 	//
-	if( unit->Player==ThisPlayer && unit->Selected ) {
+	if (unit->Player == ThisPlayer && unit->Selected) {
 	    SelectedUnitChanged();
-	    MustRedraw|=RedrawInfoPanel;
+	    MustRedraw |= RedrawInfoPanel;
 	}
 
-	unit->Wait=unit->Reset=1;	// immediately start next command.
+	unit->Wait = unit->Reset = 1;	// immediately start next command.
     }
     ClearSavedAction(unit);
 }
@@ -1174,61 +1173,61 @@ global void CommandCancelResearch(Unit* unit)
 **	@param dest	if != NULL, pointer to unit to destroy.
 **	@param flush	if true, flush command queue.
 */
-global void CommandDemolish(Unit* unit,int x,int y,Unit* dest,int flush)
+global void CommandDemolish(Unit* unit, int x, int y, Unit* dest, int flush)
 {
     Order* order;
 
-    IfDebug(
-	if( x<0 || y<0 || x>=TheMap.Width || y>=TheMap.Height ) {
-	    DebugLevel0Fn("Internal movement error\n");
-	    return;
-	}
-    );
+#ifdef DEBUG
+    if (x < 0 || y < 0 || x >= TheMap.Width || y >= TheMap.Height) {
+	DebugLevel0Fn("Internal movement error\n");
+	return;
+    }
+#endif
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	if( unit->Type->Building ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
-	    order=&unit->NewOrder;
+	    order = &unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionDemolish;
-	if( dest ) {
+	order->Action = UnitActionDemolish;
+	if (dest) {
 	    //
 	    //	Destination could be killed.
 	    //	Should be handled in action, but is not possible!
 	    //		Unit::Refs is used as timeout counter.
 	    //
-	    if( dest->Destroyed ) {
-		order->X=dest->X+dest->Type->TileWidth/2;
-		order->Y=dest->Y+dest->Type->TileHeight/2;
-		order->Goal=NoUnitP;
-		order->RangeX=order->RangeY=0;
+	    if (dest->Destroyed) {
+		order->X = dest->X + dest->Type->TileWidth / 2;
+		order->Y = dest->Y + dest->Type->TileHeight / 2;
+		order->Goal = NoUnitP;
+		order->RangeX = order->RangeY = 0;
 	    } else {
-		order->X=order->Y=-1;
-		order->Goal=dest;
-		RefsDebugCheck( !dest->Refs );
+		order->X = order->Y = -1;
+		order->Goal = dest;
+		RefsDebugCheck(!dest->Refs);
 		dest->Refs++;
-		order->RangeX=order->RangeY=1;
+		order->RangeX = order->RangeY = 1;
 	    }
-	} else if( WallOnMap(x,y) || ForestOnMap(x,y) || RockOnMap(x,y) ) {
-	    order->X=x;
-	    order->Y=y;
-	    order->RangeX=order->RangeY=1;
-	    order->Goal=NoUnitP;
+	} else if (WallOnMap(x,y) || ForestOnMap(x,y) || RockOnMap(x,y)) {
+	    order->X = x;
+	    order->Y = y;
+	    order->RangeX = order->RangeY = 1;
+	    order->Goal = NoUnitP;
 	} else {
-	    order->X=x;
-	    order->Y=y;
-	    order->RangeX=order->RangeY=0;
-	    order->Goal=NoUnitP;
+	    order->X = x;
+	    order->Y = y;
+	    order->RangeX = order->RangeY = 0;
+	    order->Goal = NoUnitP;
 	}
-	order->Type=NULL;
-	order->Arg1=NULL;
+	order->Type = NULL;
+	order->Arg1 = NULL;
     }
     ClearSavedAction(unit);
 }
@@ -1243,71 +1242,71 @@ global void CommandDemolish(Unit* unit,int x,int y,Unit* dest,int flush)
 **	@param spell	Spell type pointer.
 **	@param flush	if true, flush command queue.
 */
-global void CommandSpellCast(Unit* unit,int x,int y,Unit* dest
-	,SpellType* spell,int flush)
+global void CommandSpellCast(Unit* unit, int x, int y, Unit* dest,
+    SpellType* spell, int flush)
 {
     Order* order;
 
-    IfDebug(
-	if( x<0 || y<0 || x>=TheMap.Width || y>=TheMap.Height ) {
-	    DebugLevel0("Internal movement error\n");
-	    return;
-	}
-    );
+#ifdef DEBUG
+    if (x < 0 || y < 0 || x >= TheMap.Width || y >= TheMap.Height) {
+	DebugLevel0("Internal movement error\n");
+	return;
+    }
+#endif
 
-    DebugLevel3Fn(": %d spell-casts on %d\n"
-	_C_ UnitNumber(unit) _C_ dest ? UnitNumber(dest) : 0);
+    DebugLevel3Fn(": %d spell-casts on %d\n" _C_
+	UnitNumber(unit) _C_ dest ? UnitNumber(dest) : 0);
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
 	// FIXME: should I check here, if there is still enough mana?
 
 	/*
 	FIXME: vladi: any unit can cast spell, like dark-portal
-	if( unit->Type->Building ) {
+	if (unit->Type->Building) {
 	    // FIXME: should find a better way for pending orders.
 	    order=&unit->NewOrder;
 	    ReleaseOrder(order);
-	} else if( !(order=GetNextOrder(unit,flush)) ) {
+	} else if (!(order=GetNextOrder(unit,flush))) {
 	    return;
 	}
 	*/
-	if( !(order=GetNextOrder(unit,flush)) ) {
+	if (!(order = GetNextOrder(unit, flush))) {
 	    return;
 	}
 
-	order->Action=UnitActionSpellCast;
-	order->RangeX=order->RangeY=spell->Range;
-	if( dest ) {
+	order->Action = UnitActionSpellCast;
+	order->RangeX = order->RangeY = spell->Range;
+	if (dest) {
 	    //
 	    //	Destination could be killed.
 	    //	Should be handled in action, but is not possible!
 	    //		Unit::Refs is used as timeout counter.
 	    //
-	    if( dest->Destroyed ) {
+	    if (dest->Destroyed) {
 		// FIXME: where check if spell needs an unit as destination?
-		order->X=dest->X+dest->Type->TileWidth/2-order->RangeX;
-		order->Y=dest->Y+dest->Type->TileHeight/2-order->RangeY;
-		order->Goal=NoUnitP;
-		order->RangeX<<=1;
-		order->RangeY<<=1;
+		order->X = dest->X + dest->Type->TileWidth / 2 - order->RangeX;
+		order->Y = dest->Y + dest->Type->TileHeight / 2 - order->RangeY;
+		order->Goal = NoUnitP;
+		order->RangeX <<= 1;
+		order->RangeY <<= 1;
 	    } else {
-		order->X=order->Y=-1;
-		order->Goal=dest;
-		RefsDebugCheck( !dest->Refs );
+		order->X = order->Y = -1;
+		order->Goal = dest;
+		RefsDebugCheck(!dest->Refs);
 		dest->Refs++;
 	    }
 	} else {
-	    order->X=x-order->RangeX;
-	    order->Y=y-order->RangeY;
-	    order->Goal=NoUnitP;
-	    order->RangeX<<=1;
-	    order->RangeY<<=1;
+	    order->X = x-order->RangeX;
+	    order->Y = y-order->RangeY;
+	    order->Goal = NoUnitP;
+	    order->RangeX <<= 1;
+	    order->RangeY <<= 1;
 	}
-	order->Type=NULL;
-	order->Arg1=spell;
+	order->Type = NULL;
+	order->Arg1 = spell;
     }
     ClearSavedAction(unit);
 }
@@ -1318,16 +1317,16 @@ global void CommandSpellCast(Unit* unit,int x,int y,Unit* dest
 **	@param unit	pointer to unit.
 **	@param spell	Spell type pointer.
 */
-global void CommandAutoSpellCast(Unit* unit,SpellType* spell)
+global void CommandAutoSpellCast(Unit* unit, SpellType* spell)
 {
-    DebugLevel3Fn(": %d auto-spell-casts %s\n"
-	_C_ UnitNumber(unit) _C_ spell->Ident);
+    DebugLevel3Fn(": %d auto-spell-casts %s\n" _C_
+	UnitNumber(unit) _C_ spell->Ident);
 
     //
     //	Check if unit is still valid? (NETWORK!)
     //
-    if( !unit->Removed && unit->Orders[0].Action!=UnitActionDie ) {
-	unit->AutoCastSpell=spell;
+    if (!unit->Removed && unit->Orders[0].Action != UnitActionDie) {
+	unit->AutoCastSpell = spell;
     }
 }
 
@@ -1338,24 +1337,24 @@ global void CommandAutoSpellCast(Unit* unit,SpellType* spell)
 **	@param state	New diplomacy state.
 **	@param opponent	Opponent.
 */
-global void CommandDiplomacy(int player,int state,int opponent)
+global void CommandDiplomacy(int player, int state, int opponent)
 {
-    switch( state ) {
+    switch (state) {
 	case DiplomacyNeutral:
-	    Players[player].Enemy&=~(1<<opponent);
-	    Players[player].Allied&=~(1<<opponent);
+	    Players[player].Enemy &= ~(1 << opponent);
+	    Players[player].Allied &= ~(1 << opponent);
 	    break;
 	case DiplomacyAllied:
-	    Players[player].Enemy&=~(1<<opponent);
-	    Players[player].Allied|=1<<opponent;
+	    Players[player].Enemy &= ~(1 << opponent);
+	    Players[player].Allied |= 1 << opponent;
 	    break;
 	case DiplomacyEnemy:
-	    Players[player].Enemy|=1<<opponent;
-	    Players[player].Allied&=~(1<<opponent);
+	    Players[player].Enemy |= 1 << opponent;
+	    Players[player].Allied &= ~(1 << opponent);
 	    break;
 	case DiplomacyCrazy:
-	    Players[player].Enemy|=1<<opponent;
-	    Players[player].Allied|=1<<opponent;
+	    Players[player].Enemy |= 1 << opponent;
+	    Players[player].Allied |= 1 << opponent;
 	    break;
     }
     // FIXME: Should we display a message?
@@ -1368,48 +1367,48 @@ global void CommandDiplomacy(int player,int state,int opponent)
 **	@param state	New shared vision state.
 **	@param opponent	Opponent.
 */
-global void CommandSharedVision(int player,int state,int opponent)
+global void CommandSharedVision(int player, int state, int opponent)
 {
     int i;
     Unit* unit;
     
-    if( state==0 ) {
+    if (state == 0) {
 	// Check all tiles and mark unseen ones as explored.
-	if( Players[player].SharedVision&(1<<opponent)
-		&& (Players[opponent].SharedVision&(1<<player)) ) {
-	    for( i=0; i<TheMap.Width*TheMap.Height; ++i) {
-		if( TheMap.Fields[i].Visible[opponent]
-			&& !TheMap.Fields[i].Visible[player] ) {
-		    TheMap.Fields[i].Visible[player]=1;
+	if (Players[player].SharedVision & (1 << opponent) &&
+		(Players[opponent].SharedVision & (1 << player))) {
+	    for (i = 0; i < TheMap.Width * TheMap.Height; ++i) {
+		if (TheMap.Fields[i].Visible[opponent] &&
+			!TheMap.Fields[i].Visible[player]) {
+		    TheMap.Fields[i].Visible[player] = 1;
 		}
-		if( TheMap.Fields[i].Visible[player]
-			&& !TheMap.Fields[i].Visible[opponent] ) {
-		    TheMap.Fields[i].Visible[opponent]=1;
+		if (TheMap.Fields[i].Visible[player] &&
+			!TheMap.Fields[i].Visible[opponent]) {
+		    TheMap.Fields[i].Visible[opponent] = 1;
 		}
 	    }
 	}
-	Players[player].SharedVision&=~(1<<opponent);
+	Players[player].SharedVision &= ~(1 << opponent);
     } else {
-	Players[player].SharedVision|=(1<<opponent);
+	Players[player].SharedVision |= (1 << opponent);
 	// Check all tiles and mark SeenTiles for wood
-	if( Players[player].SharedVision&(1<<opponent)
-		&& Players[opponent].SharedVision&(1<<player)
-		&& (player==ThisPlayer->Player
-		    || opponent==ThisPlayer->Player) ) {
+	if (Players[player].SharedVision & (1 << opponent) &&
+		Players[opponent].SharedVision & (1 << player) &&
+		(player == ThisPlayer->Player ||
+		    opponent == ThisPlayer->Player)) {
 	    int y;
-	    for( i=0; i<TheMap.Width; ++i) {
-		for( y=0; y<TheMap.Height; ++y) {
-		    if( IsMapFieldVisible(ThisPlayer,i,y) ) {
-			MapMarkSeenTile(i,y);
-			UnitsMarkSeen(i,y);
+	    for (i = 0; i < TheMap.Width; ++i) {
+		for (y = 0; y < TheMap.Height; ++y) {
+		    if (IsMapFieldVisible(ThisPlayer, i, y)) {
+			MapMarkSeenTile(i, y);
+			UnitsMarkSeen(i, y);
 		    }
 		}
 	    }
 	}
     }
     // MUST update seen buildings when vision is shared or unshared
-    for( i=0; i<NumUnits; ++i ) {
-	unit=Units[i];
+    for (i = 0; i < NumUnits; ++i) {
+	unit = Units[i];
 	UnitMarkSeen(unit);
     }