From 3406a2dc5e41b11205026798eb23881baa7b37af Mon Sep 17 00:00:00 2001
From: johns <>
Date: Sun, 7 May 2000 16:08:12 +0000
Subject: [PATCH] unit references updated

---
 src/action/action_board.cpp    | 35 +++++++++++++++++++++++++++-
 src/action/action_demolish.cpp | 42 ++++++++++++++++++++++++++++++++--
 2 files changed, 74 insertions(+), 3 deletions(-)

diff --git a/src/action/action_board.cpp b/src/action/action_board.cpp
index 7651e93d8..82cd33462 100644
--- a/src/action/action_board.cpp
+++ b/src/action/action_board.cpp
@@ -57,7 +57,7 @@ local int WaitForTransporter(Unit* unit)
 {
     Unit* trans;
 
-    unit->Wait=1;
+    unit->Wait=6;
     unit->Reset=1;
 
     trans=unit->Command.Data.Move.Goal;
@@ -66,6 +66,21 @@ local int WaitForTransporter(Unit* unit)
 	DebugLevel3("TRANSPORTER NOT REACHED %d,%d\n",unit->X,unit->Y);
 	return 0;
     }
+#ifdef NEW_UNIT
+    if( trans->Destroyed ) {
+	DebugLevel0(__FUNCTION__": destroyed unit\n");
+	if( !--trans->Refs ) {
+	    ReleaseUnit(trans);
+	}
+	unit->Command.Data.Move.Goal=trans=NoUnitP;
+	return 0;
+    } else if( trans->Removed ||
+	    !trans->HP || trans->Command.Action==UnitActionDie ) {
+	--trans->Refs;
+	unit->Command.Data.Move.Goal=trans=NoUnitP;
+	return 0;
+    }
+#endif
 
     if( MapDistanceToUnit(unit->X,unit->Y,trans)==1 ) {
 	DebugLevel3("Enter transporter\n");
@@ -92,6 +107,21 @@ local void EnterTransporter(Unit* unit)
     unit->SubAction=0;
 
     transporter=unit->Command.Data.Move.Goal;
+#ifdef NEW_UNIT
+    if( transporter->Destroyed ) {
+	DebugLevel0(__FUNCTION__": destroyed unit\n");
+	if( !--transporter->Refs ) {
+	    ReleaseUnit(transporter);
+	}
+	return;
+    } else if( transporter->Removed ||
+	    !transporter->HP || transporter->Command.Action==UnitActionDie ) {
+	--transporter->Refs;
+	return;
+    }
+    --transporter->Refs;
+#endif
+
     //
     //	Find free slot in transporter.
     //
@@ -151,6 +181,9 @@ global void HandleActionBoard(Unit* unit)
 		    if( i==-1 ) {
 			if( ++unit->SubAction==200 ) {
 			    unit->Command.Action=UnitActionStill;
+			    if( unit->Command.Data.Move.Goal ) {
+				--unit->Command.Data.Move.Goal->Refs;
+			    }
 			    unit->SubAction=0;
 			}
 		    } else {
diff --git a/src/action/action_demolish.cpp b/src/action/action_demolish.cpp
index bbf4f72d4..4e03bee51 100644
--- a/src/action/action_demolish.cpp
+++ b/src/action/action_demolish.cpp
@@ -57,11 +57,34 @@ global void HandleActionDemolish(Unit* unit)
 	//	Move near to target.
 	//
 	case 0:
-	    // FIXME: RESET FIRST!!
+	    // FIXME: reset first!! why? (johns)
 	    err=HandleActionMove(unit); 
 	    if( unit->Reset ) {
 		goal=unit->Command.Data.Move.Goal;
-
+#ifdef NEW_UNIT
+		//
+		//	Target is dead, stop demolish
+		//
+		if( goal ) {
+		    if( goal->Destroyed ) {
+			DebugLevel0(__FUNCTION__": destroyed unit\n");
+			if( !--goal->Refs ) {
+			    ReleaseUnit(goal);
+			}
+			unit->Command.Data.Move.Goal=goal=NoUnitP;
+			// FIXME: perhaps I should choose an alternative
+			unit->Command.Action=UnitActionStill;
+			return;
+		    } else if( goal->Removed || !goal->HP
+				|| goal->Command.Action==UnitActionDie ) {
+			--goal->Refs;
+			unit->Command.Data.Move.Goal=goal=NoUnitP;
+			// FIXME: perhaps I should choose an alternative
+			unit->Command.Action=UnitActionStill;
+			return;
+		    }
+		}
+#else
 		//
 		//	Target is dead, stop demolish
 		//
@@ -72,6 +95,7 @@ global void HandleActionDemolish(Unit* unit)
 		    unit->Command.Action=UnitActionStill;
 		    return;
 		}
+#endif
 
 		//
 		//	Have reached target?
@@ -97,16 +121,30 @@ global void HandleActionDemolish(Unit* unit)
 	//	Demolish the target.
 	//
 	case 1:
+#ifdef NEW_UNIT
+	    goal=unit->Command.Data.Move.Goal;
+	    if( goal ) {
+		--goal->Refs;
+	    }
+#endif
+
             x=unit->X;
             y=unit->Y;
             DestroyUnit(unit);
 	    // FIXME: Must play explosion sound
+
+	    //
+	    //	 Effect of the explosion on units.
+	    //
             n=SelectUnits(x-2,y-2, x+2, y+2,table);
 	    // FIXME: Don't hit flying units!
             for( i=0; i<n; ++i ) {
                 HitUnit(table[i],DEMOLISH_DAMAGE);
             }
 
+	    //
+	    //	Terrain effect of the explosion
+	    //
             for( ix=x-2; ix<=x+2; ix++ ) {
 		for( iy=y-2; iy<=y+2; iy++ ) {
 		    n=TheMap.Fields[ix+iy*TheMap.Width].Flags;