(svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use

replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
bjarni 18 years ago
parent daeac3d310
commit c2b7d0192d

@ -1273,8 +1273,7 @@ static void ProcessAircraftOrder(Vehicle *v)
if (CmdFailed(ret)) CrashAirplane(v); if (CmdFailed(ret)) CrashAirplane(v);
} else if (v->current_order.type != OT_GOTO_DEPOT) { } else if (v->current_order.type != OT_GOTO_DEPOT) {
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
} }
return; return;
} }
@ -1326,8 +1325,7 @@ static void HandleAircraftLoading(Vehicle *v, int mode)
} }
Order b = v->current_order; Order b = v->current_order;
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
MarkAircraftDirty(v); MarkAircraftDirty(v);
if (!(b.flags & OF_NON_STOP)) return; if (!(b.flags & OF_NON_STOP)) return;
break; break;
@ -1552,8 +1550,7 @@ static void AircraftEventHandler_InHangar(Vehicle *v, const AirportFTAClass *apc
/* if we were sent to the depot, stay there */ /* if we were sent to the depot, stay there */
if (v->current_order.type == OT_GOTO_DEPOT && (v->vehstatus & VS_STOPPED)) { if (v->current_order.type == OT_GOTO_DEPOT && (v->vehstatus & VS_STOPPED)) {
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
return; return;
} }
@ -1601,7 +1598,7 @@ static void AircraftEventHandler_AtTerminal(Vehicle *v, const AirportFTAClass *a
return; 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 the block of the next position is busy, stay put */
if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return; 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; break;
default: // orders have been deleted (no orders), goto depot and don't bother us default: // orders have been deleted (no orders), goto depot and don't bother us
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
v->u.air.state = HANGAR; v->u.air.state = HANGAR;
} }
AirportMove(v, apc); AirportMove(v, apc);

@ -135,9 +135,7 @@ static void InitializeDisasterVehicle(Vehicle *v, int x, int y, byte z, Directio
v->owner = OWNER_NONE; v->owner = OWNER_NONE;
v->vehstatus = VS_UNCLICKABLE; v->vehstatus = VS_UNCLICKABLE;
v->u.disaster.image_override = 0; v->u.disaster.image_override = 0;
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
v->current_order.dest = 0;
DisasterVehicleUpdateImage(v); DisasterVehicleUpdateImage(v);
VehiclePositionChanged(v); VehiclePositionChanged(v);

@ -486,9 +486,9 @@ static bool LoadOldOrder(LoadgameState *ls, int num)
AssignOrder(GetOrder(num), UnpackOldOrder(_old_order)); AssignOrder(GetOrder(num), UnpackOldOrder(_old_order));
/* Relink the orders to eachother (in TTD(Patch) the orders for one /* 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 */ 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); GetOrder(num - 1)->next = GetOrder(num);
return true; return true;

@ -99,6 +99,10 @@ struct Order {
CargoID refit_cargo; // Refit CargoID CargoID refit_cargo; // Refit CargoID
byte refit_subtype; // Refit subtype byte refit_subtype; // Refit subtype
bool IsValid() const;
void Free();
void FreeChain();
}; };
#define MAX_BACKUP_ORDER_COUNT 40 #define MAX_BACKUP_ORDER_COUNT 40
@ -134,18 +138,26 @@ static inline VehicleOrderID GetNumOrders()
/** /**
* Check if a Order really exists. * 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; if (next != NULL) next->FreeChain();
o->next = NULL; 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) #define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0)
@ -160,7 +172,7 @@ static inline bool HasOrderPoolFree(uint amount)
return true; return true;
FOR_ALL_ORDERS(order) FOR_ALL_ORDERS(order)
if (order->type == OT_NOTHING) if (!order->IsValid())
if (--amount == 0) if (--amount == 0)
return true; return true;

@ -49,7 +49,7 @@ Order UnpackOldOrder(uint16 packed)
// Sanity check // Sanity check
// TTD stores invalid orders as OT_NOTHING with non-zero flags/station // 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.type = OT_DUMMY;
order.flags = 0; order.flags = 0;
} }
@ -116,7 +116,7 @@ static Order *AllocateOrder()
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items. /* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */ * 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) { 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; OrderID index = order->index;
memset(order, 0, sizeof(*order)); memset(order, 0, sizeof(*order));
@ -496,8 +496,7 @@ int32 CmdDeleteOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
} }
/* Give the item free */ /* Give the item free */
order->type = OT_NOTHING; order->Free();
order->next = NULL;
u = GetFirstVehicleFromSharedList(v); u = GetFirstVehicleFromSharedList(v);
DeleteOrderWarnings(u); DeleteOrderWarnings(u);
@ -871,9 +870,8 @@ void BackupVehicleOrders(const Vehicle *v, BackuppedOrders *bak)
*dest = *order; *dest = *order;
dest++; dest++;
} }
/* End the list with an OT_NOTHING */ /* End the list with an empty order */
dest->type = OT_NOTHING; dest->Free();
dest->next = NULL;
} }
} }
@ -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 * 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 * 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. */ * 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)) if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK))
break; break;
} }
@ -1112,8 +1110,6 @@ bool VehicleHasDepotOrders(const Vehicle *v)
*/ */
void DeleteVehicleOrders(Vehicle *v) void DeleteVehicleOrders(Vehicle *v)
{ {
Order *cur, *next;
DeleteOrderWarnings(v); DeleteOrderWarnings(v);
/* If we have a shared order-list, don't delete the list, but just /* 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 */ /* Remove the orders */
cur = v->orders; Order *cur = v->orders;
v->orders = NULL; v->orders = NULL;
v->num_orders = 0; v->num_orders = 0;
@ -1162,12 +1158,8 @@ void DeleteVehicleOrders(Vehicle *v)
case VEH_AIRCRAFT: window_class = WC_AIRCRAFT_LIST; break; case VEH_AIRCRAFT: window_class = WC_AIRCRAFT_LIST; break;
} }
DeleteWindowById(window_class, (cur->index << 16) | (v->type << 11) | VLW_SHARED_ORDERS | v->owner); DeleteWindowById(window_class, (cur->index << 16) | (v->type << 11) | VLW_SHARED_ORDERS | v->owner);
}
while (cur != NULL) { cur->FreeChain(); // Free the orders.
next = cur->next;
DeleteOrder(cur);
cur = next;
} }
} }
@ -1274,9 +1266,9 @@ static void Load_ORDR()
/* Update all the next pointer */ /* Update all the next pointer */
for (i = 1; i < len; ++i) { for (i = 1; i < len; ++i) {
/* The orders were built like this: /* The orders were built like this:
* Vehicle one had order[0], and as long as order++.type was not * While the order is valid, set the previous will get it's next pointer set
* OT_NOTHING, it was part of the order-list of that vehicle */ * We start with index 1 because no order will have the first in it's next pointer */
if (GetOrder(i)->type != OT_NOTHING) if (GetOrder(i)->IsValid())
GetOrder(i - 1)->next = GetOrder(i); GetOrder(i - 1)->next = GetOrder(i);
} }
} else { } else {

@ -309,8 +309,7 @@ static Order GetOrderCmdFromTile(const Vehicle *v, TileIndex tile)
} }
// not found // not found
order.type = OT_NOTHING; order.Free();
order.flags = 0;
order.dest = INVALID_STATION; order.dest = INVALID_STATION;
return order; return order;
} }
@ -347,7 +346,7 @@ static void OrdersPlaceObj(const Vehicle *v, TileIndex tile, Window *w)
if (u != NULL && HandleOrderVehClick(v, u, w)) return; if (u != NULL && HandleOrderVehClick(v, u, w)) return;
cmd = GetOrderCmdFromTile(v, tile); 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 (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++; if (WP(w,order_d).sel != -1) WP(w,order_d).sel++;

@ -682,8 +682,7 @@ static void ProcessRoadVehOrder(Vehicle *v)
order = GetVehicleOrder(v, v->cur_order_index); order = GetVehicleOrder(v, v->cur_order_index);
if (order == NULL) { if (order == NULL) {
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
v->dest_tile = 0; v->dest_tile = 0;
ClearSlot(v); ClearSlot(v);
return; return;
@ -1618,8 +1617,7 @@ again:
v->cur_speed = 0; v->cur_speed = 0;
return; return;
} }
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
ClearSlot(v); ClearSlot(v);
} }

@ -264,8 +264,7 @@ static void ProcessShipOrder(Vehicle *v)
order = GetVehicleOrder(v, v->cur_order_index); order = GetVehicleOrder(v, v->cur_order_index);
if (order == NULL) { if (order == NULL) {
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
v->dest_tile = 0; v->dest_tile = 0;
return; return;
} }
@ -705,8 +704,7 @@ static void ShipController(Vehicle *v)
/* A leave station order only needs one tick to get processed, so we can /* A leave station order only needs one tick to get processed, so we can
* always skip ahead. */ * always skip ahead. */
if (v->current_order.type == OT_LEAVESTATION) { if (v->current_order.type == OT_LEAVESTATION) {
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
} else if (v->dest_tile != 0) { } else if (v->dest_tile != 0) {
/* We have a target, let's see if we reached it... */ /* We have a target, let's see if we reached it... */

@ -2454,8 +2454,7 @@ static bool ProcessTrainOrder(Vehicle *v)
// If no order, do nothing. // If no order, do nothing.
if (order == NULL) { if (order == NULL) {
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
v->dest_tile = 0; v->dest_tile = 0;
return false; return false;
} }
@ -2910,8 +2909,7 @@ static void TrainController(Vehicle *v, bool update_image)
} }
if (v->current_order.type == OT_LEAVESTATION) { if (v->current_order.type == OT_LEAVESTATION) {
v->current_order.type = OT_NOTHING; v->current_order.Free();
v->current_order.flags = 0;
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
} }
} }

Loading…
Cancel
Save