From 0ad237611e9d3809160364cad75e401aad5b8b10 Mon Sep 17 00:00:00 2001 From: bjarni Date: Thu, 8 Mar 2007 21:39:34 +0000 Subject: (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use --- src/aircraft_cmd.cpp | 14 +++++--------- src/disaster_cmd.cpp | 4 +--- src/oldloader.cpp | 4 ++-- src/order.h | 26 +++++++++++++++++++------- src/order_cmd.cpp | 30 +++++++++++------------------- src/order_gui.cpp | 5 ++--- src/roadveh_cmd.cpp | 6 ++---- src/ship_cmd.cpp | 6 ++---- src/train_cmd.cpp | 6 ++---- 9 files changed, 46 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index ea7a236c3..e4d5b44ab 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -1273,8 +1273,7 @@ static void ProcessAircraftOrder(Vehicle *v) if (CmdFailed(ret)) CrashAirplane(v); } else if (v->current_order.type != OT_GOTO_DEPOT) { - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; + v->current_order.Free(); } return; } @@ -1326,8 +1325,7 @@ static void HandleAircraftLoading(Vehicle *v, int mode) } Order b = v->current_order; - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; + v->current_order.Free(); MarkAircraftDirty(v); if (!(b.flags & OF_NON_STOP)) return; break; @@ -1552,8 +1550,7 @@ static void AircraftEventHandler_InHangar(Vehicle *v, const AirportFTAClass *apc /* if we were sent to the depot, stay there */ if (v->current_order.type == OT_GOTO_DEPOT && (v->vehstatus & VS_STOPPED)) { - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; + v->current_order.Free(); return; } @@ -1601,7 +1598,7 @@ static void AircraftEventHandler_AtTerminal(Vehicle *v, const AirportFTAClass *a return; } - if (v->current_order.type == OT_NOTHING) return; + if (!v->current_order.IsValid()) return; /* if the block of the next position is busy, stay put */ if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return; @@ -1622,8 +1619,7 @@ static void AircraftEventHandler_AtTerminal(Vehicle *v, const AirportFTAClass *a } break; default: // orders have been deleted (no orders), goto depot and don't bother us - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; + v->current_order.Free(); v->u.air.state = HANGAR; } AirportMove(v, apc); diff --git a/src/disaster_cmd.cpp b/src/disaster_cmd.cpp index 7a302ee61..bab9f4138 100644 --- a/src/disaster_cmd.cpp +++ b/src/disaster_cmd.cpp @@ -135,9 +135,7 @@ static void InitializeDisasterVehicle(Vehicle *v, int x, int y, byte z, Directio v->owner = OWNER_NONE; v->vehstatus = VS_UNCLICKABLE; v->u.disaster.image_override = 0; - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; - v->current_order.dest = 0; + v->current_order.Free(); DisasterVehicleUpdateImage(v); VehiclePositionChanged(v); diff --git a/src/oldloader.cpp b/src/oldloader.cpp index bdf1cf9e6..940661a95 100644 --- a/src/oldloader.cpp +++ b/src/oldloader.cpp @@ -486,9 +486,9 @@ static bool LoadOldOrder(LoadgameState *ls, int num) AssignOrder(GetOrder(num), UnpackOldOrder(_old_order)); /* Relink the orders to eachother (in TTD(Patch) the orders for one - vehicle are behind eachother, with OT_NOTHING as indication that + vehicle are behind eachother, with an invalid order (OT_NOTHING) as indication that it is the last order */ - if (num > 0 && GetOrder(num)->type != OT_NOTHING) + if (num > 0 && GetOrder(num)->IsValid()) GetOrder(num - 1)->next = GetOrder(num); return true; diff --git a/src/order.h b/src/order.h index b996fb9d6..3a2ddb1f3 100644 --- a/src/order.h +++ b/src/order.h @@ -99,6 +99,10 @@ struct Order { CargoID refit_cargo; // Refit CargoID byte refit_subtype; // Refit subtype + + bool IsValid() const; + void Free(); + void FreeChain(); }; #define MAX_BACKUP_ORDER_COUNT 40 @@ -134,18 +138,26 @@ static inline VehicleOrderID GetNumOrders() /** * Check if a Order really exists. */ -static inline bool IsValidOrder(const Order *o) +inline bool Order::IsValid() const +{ + return type != OT_NOTHING; +} + +inline void Order::Free() { - return o->type != OT_NOTHING; + type = OT_NOTHING; + flags = 0; + dest = 0; + next = NULL; } -static inline void DeleteOrder(Order *o) +inline void Order::FreeChain() { - o->type = OT_NOTHING; - o->next = NULL; + if (next != NULL) next->FreeChain(); + Free(); } -#define FOR_ALL_ORDERS_FROM(order, start) for (order = GetOrder(start); order != NULL; order = (order->index + 1U < GetOrderPoolSize()) ? GetOrder(order->index + 1U) : NULL) if (IsValidOrder(order)) +#define FOR_ALL_ORDERS_FROM(order, start) for (order = GetOrder(start); order != NULL; order = (order->index + 1U < GetOrderPoolSize()) ? GetOrder(order->index + 1U) : NULL) if (order->IsValid()) #define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0) @@ -160,7 +172,7 @@ static inline bool HasOrderPoolFree(uint amount) return true; FOR_ALL_ORDERS(order) - if (order->type == OT_NOTHING) + if (!order->IsValid()) if (--amount == 0) return true; diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 7903d0f57..44d5142c4 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -49,7 +49,7 @@ Order UnpackOldOrder(uint16 packed) // Sanity check // TTD stores invalid orders as OT_NOTHING with non-zero flags/station - if (order.type == OT_NOTHING && (order.flags != 0 || order.dest != 0)) { + if (!order.IsValid() && (order.flags != 0 || order.dest != 0)) { order.type = OT_DUMMY; order.flags = 0; } @@ -116,7 +116,7 @@ static Order *AllocateOrder() /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. * TODO - This is just a temporary stage, this will be removed. */ for (order = GetOrder(0); order != NULL; order = (order->index + 1U < GetOrderPoolSize()) ? GetOrder(order->index + 1U) : NULL) { - if (!IsValidOrder(order)) { + if (!order->IsValid()) { OrderID index = order->index; memset(order, 0, sizeof(*order)); @@ -496,8 +496,7 @@ int32 CmdDeleteOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) } /* Give the item free */ - order->type = OT_NOTHING; - order->next = NULL; + order->Free(); u = GetFirstVehicleFromSharedList(v); DeleteOrderWarnings(u); @@ -871,9 +870,8 @@ void BackupVehicleOrders(const Vehicle *v, BackuppedOrders *bak) *dest = *order; dest++; } - /* End the list with an OT_NOTHING */ - dest->type = OT_NOTHING; - dest->next = NULL; + /* End the list with an empty order */ + dest->Free(); } } @@ -902,7 +900,7 @@ void RestoreVehicleOrders(const Vehicle* v, const BackuppedOrders* bak) * order number is one more than the current amount of orders, and because * in network the commands are queued before send, the second insert always * fails in test mode. By bypassing the test-mode, that no longer is a problem. */ - for (i = 0; bak->order[i].type != OT_NOTHING; i++) { + for (i = 0; bak->order[i].IsValid(); i++) { if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)) break; } @@ -1112,8 +1110,6 @@ bool VehicleHasDepotOrders(const Vehicle *v) */ void DeleteVehicleOrders(Vehicle *v) { - Order *cur, *next; - DeleteOrderWarnings(v); /* If we have a shared order-list, don't delete the list, but just @@ -1146,7 +1142,7 @@ void DeleteVehicleOrders(Vehicle *v) } /* Remove the orders */ - cur = v->orders; + Order *cur = v->orders; v->orders = NULL; v->num_orders = 0; @@ -1162,12 +1158,8 @@ void DeleteVehicleOrders(Vehicle *v) case VEH_AIRCRAFT: window_class = WC_AIRCRAFT_LIST; break; } DeleteWindowById(window_class, (cur->index << 16) | (v->type << 11) | VLW_SHARED_ORDERS | v->owner); - } - while (cur != NULL) { - next = cur->next; - DeleteOrder(cur); - cur = next; + cur->FreeChain(); // Free the orders. } } @@ -1274,9 +1266,9 @@ static void Load_ORDR() /* Update all the next pointer */ for (i = 1; i < len; ++i) { /* The orders were built like this: - * Vehicle one had order[0], and as long as order++.type was not - * OT_NOTHING, it was part of the order-list of that vehicle */ - if (GetOrder(i)->type != OT_NOTHING) + * While the order is valid, set the previous will get it's next pointer set + * We start with index 1 because no order will have the first in it's next pointer */ + if (GetOrder(i)->IsValid()) GetOrder(i - 1)->next = GetOrder(i); } } else { diff --git a/src/order_gui.cpp b/src/order_gui.cpp index abfe792dd..167c754f0 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -309,8 +309,7 @@ static Order GetOrderCmdFromTile(const Vehicle *v, TileIndex tile) } // not found - order.type = OT_NOTHING; - order.flags = 0; + order.Free(); order.dest = INVALID_STATION; return order; } @@ -347,7 +346,7 @@ static void OrdersPlaceObj(const Vehicle *v, TileIndex tile, Window *w) if (u != NULL && HandleOrderVehClick(v, u, w)) return; cmd = GetOrderCmdFromTile(v, tile); - if (cmd.type == OT_NOTHING) return; + if (!cmd.IsValid()) return; if (DoCommandP(v->tile, v->index + (OrderGetSel(w) << 16), PackOrder(&cmd), NULL, CMD_INSERT_ORDER | CMD_MSG(STR_8833_CAN_T_INSERT_NEW_ORDER))) { if (WP(w,order_d).sel != -1) WP(w,order_d).sel++; diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index f056ef6bf..af50dc9b9 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -682,8 +682,7 @@ static void ProcessRoadVehOrder(Vehicle *v) order = GetVehicleOrder(v, v->cur_order_index); if (order == NULL) { - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; + v->current_order.Free(); v->dest_tile = 0; ClearSlot(v); return; @@ -1618,8 +1617,7 @@ again: v->cur_speed = 0; return; } - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; + v->current_order.Free(); ClearSlot(v); } diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 54c1b0cfa..6aa488138 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -264,8 +264,7 @@ static void ProcessShipOrder(Vehicle *v) order = GetVehicleOrder(v, v->cur_order_index); if (order == NULL) { - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; + v->current_order.Free(); v->dest_tile = 0; return; } @@ -705,8 +704,7 @@ static void ShipController(Vehicle *v) /* A leave station order only needs one tick to get processed, so we can * always skip ahead. */ if (v->current_order.type == OT_LEAVESTATION) { - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; + v->current_order.Free(); InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); } else if (v->dest_tile != 0) { /* We have a target, let's see if we reached it... */ diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index ad511aa07..e00f03e92 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -2454,8 +2454,7 @@ static bool ProcessTrainOrder(Vehicle *v) // If no order, do nothing. if (order == NULL) { - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; + v->current_order.Free(); v->dest_tile = 0; return false; } @@ -2910,8 +2909,7 @@ static void TrainController(Vehicle *v, bool update_image) } if (v->current_order.type == OT_LEAVESTATION) { - v->current_order.type = OT_NOTHING; - v->current_order.flags = 0; + v->current_order.Free(); InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); } } -- cgit v1.2.3-70-g09d2