From 602c0d40b38c83e520667ec1585b7745278fbbf8 Mon Sep 17 00:00:00 2001 From: truelight Date: Sat, 26 Aug 2006 14:22:54 +0000 Subject: [PATCH] (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 | 8 +++++--- engine.h | 5 +++++ order.h | 6 ++++++ order_cmd.c | 20 +++++--------------- signs.c | 6 ++---- signs.h | 6 ++++++ town_cmd.c | 9 ++++----- train.h | 38 ++++++++++++++++++++++++-------------- vehicle.h | 4 +++- 9 files changed, 60 insertions(+), 42 deletions(-) diff --git a/engine.c b/engine.c index 0bcce29042..878d521a29 100644 --- a/engine.c +++ b/engine.c @@ -503,10 +503,12 @@ static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine) 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 @@ int32 RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, uint32 flag /* Cut this element out */ prev->next = er->next; } - er->from = INVALID_ENGINE; // Deallocate + DeleteEngineRenew(er); } return 0; } diff --git a/engine.h b/engine.h index 42131b368e..7ea7325880 100644 --- a/engine.h +++ b/engine.h @@ -255,6 +255,11 @@ static inline bool IsValidEngineRenew(const EngineRenew *er) 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) diff --git a/order.h b/order.h index bf96e92eef..508b7dd119 100644 --- a/order.h +++ b/order.h @@ -135,6 +135,12 @@ static inline bool IsValidOrder(const Order *o) 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) diff --git a/order_cmd.c b/order_cmd.c index 3cca2aa0f0..f2b1924b9b 100644 --- a/order_cmd.c +++ b/order_cmd.c @@ -1028,7 +1028,7 @@ bool VehicleHasDepotOrders(const Vehicle *v) */ void DeleteVehicleOrders(Vehicle *v) { - Order *order, *cur; + Order *cur, *next; DeleteOrderWarnings(v); @@ -1066,20 +1066,10 @@ void DeleteVehicleOrders(Vehicle *v) 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; } } diff --git a/signs.c b/signs.c index 7ce30a8102..d507ffe4c4 100644 --- a/signs.c +++ b/signs.c @@ -179,11 +179,9 @@ int32 CmdRenameSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) if (flags & DC_EXEC) { Sign *si = GetSign(p1); - /* Delete the name */ - DeleteName(si->str); - si->str = 0; - MarkSignDirty(si); + DeleteSign(si); + InvalidateWindow(WC_SIGN_LIST, 0); _sign_sort_dirty = true; } diff --git a/signs.h b/signs.h index f61db4511c..e2d5f9f7fa 100644 --- a/signs.h +++ b/signs.h @@ -57,6 +57,12 @@ static inline bool IsValidSignID(uint index) 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) diff --git a/town_cmd.c b/town_cmd.c index 94cce70fd3..eb7b199660 100644 --- a/town_cmd.c +++ b/town_cmd.c @@ -1062,13 +1062,12 @@ bool GenerateTowns(void) // 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; diff --git a/train.h b/train.h index 8399e8cc2c..792c5e9373 100644 --- a/train.h +++ b/train.h @@ -173,19 +173,6 @@ static inline void ClearMultiheaded(Vehicle *v) 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 @@ static inline bool EngineHasArticPart(const Vehicle *v) 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); diff --git a/vehicle.h b/vehicle.h index baeccd70c0..c02a319fd2 100644 --- a/vehicle.h +++ b/vehicle.h @@ -430,7 +430,9 @@ static inline Vehicle *GetFirstVehicleFromSharedList(Vehicle *v) 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.