(svn r6137) -Codechange: some very minor cleanups:
authortruelight
Sat, 26 Aug 2006 14:22:54 +0000
changeset 4384 293c0d26294c
parent 4383 3dcf52b1efd9
child 4385 911321439fb4
(svn r6137) -Codechange: some very minor cleanups:
- Start using DeleteXXX for every pool item, not manually doing it
- Use some wrapper to improve logic
- Rewrote some pieces to improve logic
engine.c
engine.h
order.h
order_cmd.c
signs.c
signs.h
town_cmd.c
train.h
vehicle.h
--- a/engine.c	Sat Aug 26 11:06:54 2006 +0000
+++ b/engine.c	Sat Aug 26 14:22:54 2006 +0000
@@ -503,10 +503,12 @@
 void RemoveAllEngineReplacement(EngineRenewList *erl)
 {
 	EngineRenew *er = (EngineRenew *)(*erl);
+	EngineRenew *next;
 
 	while (er) {
-		er->from = INVALID_ENGINE; // "Deallocate" elements
-		er = er->next;
+		next = er->next;
+		DeleteEngineRenew(er);
+		er = next;
 	}
 	*erl = NULL; // Empty list
 }
@@ -559,7 +561,7 @@
 					/* Cut this element out */
 					prev->next = er->next;
 				}
-				er->from = INVALID_ENGINE; // Deallocate
+				DeleteEngineRenew(er);
 			}
 			return 0;
 		}
--- a/engine.h	Sat Aug 26 11:06:54 2006 +0000
+++ b/engine.h	Sat Aug 26 14:22:54 2006 +0000
@@ -255,6 +255,11 @@
 	return er->from != INVALID_ENGINE;
 }
 
+static inline void DeleteEngineRenew(EngineRenew *er)
+{
+	er->from = INVALID_ENGINE;
+}
+
 #define FOR_ALL_ENGINE_RENEWS_FROM(er, start) for (er = GetEngineRenew(start); er != NULL; er = (er->index + 1 < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1) : NULL) if (er->from != INVALID_ENGINE) if (IsValidEngineRenew(er))
 #define FOR_ALL_ENGINE_RENEWS(er) FOR_ALL_ENGINE_RENEWS_FROM(er, 0)
 
--- a/order.h	Sat Aug 26 11:06:54 2006 +0000
+++ b/order.h	Sat Aug 26 14:22:54 2006 +0000
@@ -135,6 +135,12 @@
 	return o->type != OT_NOTHING;
 }
 
+static inline void DeleteOrder(Order *o)
+{
+	o->type = OT_NOTHING;
+	o->next = NULL;
+}
+
 #define FOR_ALL_ORDERS_FROM(order, start) for (order = GetOrder(start); order != NULL; order = (order->index + 1 < GetOrderPoolSize()) ? GetOrder(order->index + 1) : NULL) if (IsValidOrder(order))
 #define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0)
 
--- a/order_cmd.c	Sat Aug 26 11:06:54 2006 +0000
+++ b/order_cmd.c	Sat Aug 26 14:22:54 2006 +0000
@@ -1028,7 +1028,7 @@
  */
 void DeleteVehicleOrders(Vehicle *v)
 {
-	Order *order, *cur;
+	Order *cur, *next;
 
 	DeleteOrderWarnings(v);
 
@@ -1066,20 +1066,10 @@
 	v->orders = NULL;
 	v->num_orders = 0;
 
-	order = NULL;
-	while (cur != NULL) {
-		if (order != NULL) {
-			order->type = OT_NOTHING;
-			order->next = NULL;
-		}
-
-		order = cur;
-		cur = cur->next;
-	}
-
-	if (order != NULL) {
-		order->type = OT_NOTHING;
-		order->next = NULL;
+		while (cur != NULL) {
+		next = cur->next;
+		DeleteOrder(cur);
+		cur = next;
 	}
 }
 
--- a/signs.c	Sat Aug 26 11:06:54 2006 +0000
+++ b/signs.c	Sat Aug 26 14:22:54 2006 +0000
@@ -179,11 +179,9 @@
 		if (flags & DC_EXEC) {
 			Sign *si = GetSign(p1);
 
-			/* Delete the name */
-			DeleteName(si->str);
-			si->str = 0;
+			MarkSignDirty(si);
+			DeleteSign(si);
 
-			MarkSignDirty(si);
 			InvalidateWindow(WC_SIGN_LIST, 0);
 			_sign_sort_dirty = true;
 		}
--- a/signs.h	Sat Aug 26 11:06:54 2006 +0000
+++ b/signs.h	Sat Aug 26 14:22:54 2006 +0000
@@ -57,6 +57,12 @@
 	return index < GetSignPoolSize() && IsValidSign(GetSign(index));
 }
 
+static inline void DeleteSign(Sign *si)
+{
+	DeleteName(si->str);
+	si->str = STR_NULL;
+}
+
 #define FOR_ALL_SIGNS_FROM(ss, start) for (ss = GetSign(start); ss != NULL; ss = (ss->index + 1 < GetSignPoolSize()) ? GetSign(ss->index + 1) : NULL) if (IsValidSign(ss))
 #define FOR_ALL_SIGNS(ss) FOR_ALL_SIGNS_FROM(ss, 0)
 
--- a/town_cmd.c	Sat Aug 26 11:06:54 2006 +0000
+++ b/town_cmd.c	Sat Aug 26 14:22:54 2006 +0000
@@ -1062,13 +1062,12 @@
 
 	// give it a last try, but now more aggressive
 	if (num == 0 && CreateRandomTown(10000, 0) == NULL) {
-		if (GetTownArraySize() > 0) return true;
+		if (GetTownArraySize() == 0) {
+			/* XXX - can we handle that more gracefully? */
+			if (_game_mode != GM_EDITOR) error("Could not generate any town");
 
-		//XXX can we handle that more gracefully?
-		if (num == 0 && _game_mode != GM_EDITOR) {
-			error("Could not generate any town");
+			return false;
 		}
-		return false;
 	}
 
 	return true;
--- a/train.h	Sat Aug 26 11:06:54 2006 +0000
+++ b/train.h	Sat Aug 26 14:22:54 2006 +0000
@@ -173,19 +173,6 @@
 	CLRBIT(v->subtype, Train_Multiheaded);
 }
 
-/** Get the next real (non-articulated part) vehicle in the consist.
- * @param v Vehicle.
- * @return Next vehicle in the consist.
- */
-static inline Vehicle *GetNextVehicle(const Vehicle *v)
-{
-	Vehicle *u = v->next;
-	while (u != NULL && IsArticulatedPart(u)) {
-		u = u->next;
-	}
-	return u;
-}
-
 /** Check if an engine has an articulated part.
  * @param v Vehicle.
  * @return True if the engine has an articulated part.
@@ -195,16 +182,39 @@
 	return (v->next != NULL && IsArticulatedPart(v->next));
 }
 
+/**
+ * Get the next part of a multi-part engine.
+ * Will only work on a multi-part engine (EngineHasArticPart(v) == true),
+ * Result is undefined for normal engine.
+ */
+static inline Vehicle *GetNextArticPart(const Vehicle *v)
+{
+	assert(EngineHasArticPart(v));
+	return v->next;
+}
+
 /** Get the last part of a multi-part engine.
  * @param v Vehicle.
  * @return Last part of the engine.
  */
 static inline Vehicle *GetLastEnginePart(Vehicle *v)
 {
-	while (EngineHasArticPart(v)) v = v->next;
+	while (EngineHasArticPart(v)) v = GetNextArticPart(v);
 	return v;
 }
 
+/** Get the next real (non-articulated part) vehicle in the consist.
+ * @param v Vehicle.
+ * @return Next vehicle in the consist.
+ */
+static inline Vehicle *GetNextVehicle(const Vehicle *v)
+{
+	while (EngineHasArticPart(v)) v = GetNextArticPart(v);
+
+	/* v now contains the last artic part in the engine */
+	return v->next;
+}
+
 void ConvertOldMultiheadToNew(void);
 void ConnectMultiheadedTrains(void);
 
--- a/vehicle.h	Sat Aug 26 11:06:54 2006 +0000
+++ b/vehicle.h	Sat Aug 26 14:22:54 2006 +0000
@@ -430,7 +430,9 @@
 VARDEF VehicleID _new_vehicle_id;
 VARDEF uint16 _returned_refit_capacity;
 
-#define INVALID_VEHICLE 0xFFFF
+enum {
+	INVALID_VEHICLE = 0xFFFF,
+};
 
 /**
  * Get the colour map for an engine. This used for unbuilt engines in the user interface.