Mercurial > hg > openttd
changeset 4384:c0add09dd64a draft
(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
author | truelight <truelight@openttd.org> |
---|---|
date | Sat, 26 Aug 2006 14:22:54 +0000 |
parents | c9177e4cdbda |
children | 516d7607cc74 |
files | engine.c engine.h order.h order_cmd.c signs.c signs.h town_cmd.c train.h vehicle.h |
diffstat | 9 files changed, 60 insertions(+), 42 deletions(-) [+] |
line wrap: on
line diff
--- a/engine.c +++ b/engine.c @@ -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 +++ b/engine.h @@ -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 +++ b/order.h @@ -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 +++ b/order_cmd.c @@ -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 +++ b/signs.c @@ -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 +++ b/signs.h @@ -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 +++ b/town_cmd.c @@ -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 +++ b/train.h @@ -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);