00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "cmd_helper.h"
00015 #include "command_func.h"
00016 #include "company_func.h"
00017 #include "news_func.h"
00018 #include "strings_func.h"
00019 #include "timetable.h"
00020 #include "vehicle_func.h"
00021 #include "depot_base.h"
00022 #include "core/pool_func.hpp"
00023 #include "aircraft.h"
00024 #include "roadveh.h"
00025 #include "station_base.h"
00026 #include "waypoint_base.h"
00027 #include "company_base.h"
00028 #include "order_backup.h"
00029
00030 #include "table/strings.h"
00031
00032
00033
00034
00035 assert_compile(sizeof(DestinationID) >= sizeof(DepotID));
00036 assert_compile(sizeof(DestinationID) >= sizeof(StationID));
00037
00038 OrderPool _order_pool("Order");
00039 INSTANTIATE_POOL_METHODS(Order)
00040 OrderListPool _orderlist_pool("OrderList");
00041 INSTANTIATE_POOL_METHODS(OrderList)
00042
00044 Order::~Order()
00045 {
00046 if (CleaningPool()) return;
00047
00048
00049
00050 if (this->IsType(OT_GOTO_STATION) || this->IsType(OT_GOTO_WAYPOINT)) {
00051 BaseStation *bs = BaseStation::GetIfValid(this->GetDestination());
00052 if (bs != NULL && bs->owner == OWNER_NONE) InvalidateWindowClassesData(WC_STATION_LIST, 0);
00053 }
00054 }
00055
00060 void Order::Free()
00061 {
00062 this->type = OT_NOTHING;
00063 this->flags = 0;
00064 this->dest = 0;
00065 this->next = NULL;
00066 }
00067
00072 void Order::MakeGoToStation(StationID destination)
00073 {
00074 this->type = OT_GOTO_STATION;
00075 this->flags = 0;
00076 this->dest = destination;
00077 }
00078
00088 void Order::MakeGoToDepot(DepotID destination, OrderDepotTypeFlags order, OrderNonStopFlags non_stop_type, OrderDepotActionFlags action, CargoID cargo, byte subtype)
00089 {
00090 this->type = OT_GOTO_DEPOT;
00091 this->SetDepotOrderType(order);
00092 this->SetDepotActionType(action);
00093 this->SetNonStopType(non_stop_type);
00094 this->dest = destination;
00095 this->SetRefit(cargo, subtype);
00096 }
00097
00102 void Order::MakeGoToWaypoint(StationID destination)
00103 {
00104 this->type = OT_GOTO_WAYPOINT;
00105 this->flags = 0;
00106 this->dest = destination;
00107 }
00108
00113 void Order::MakeLoading(bool ordered)
00114 {
00115 this->type = OT_LOADING;
00116 if (!ordered) this->flags = 0;
00117 }
00118
00122 void Order::MakeLeaveStation()
00123 {
00124 this->type = OT_LEAVESTATION;
00125 this->flags = 0;
00126 }
00127
00131 void Order::MakeDummy()
00132 {
00133 this->type = OT_DUMMY;
00134 this->flags = 0;
00135 }
00136
00141 void Order::MakeConditional(VehicleOrderID order)
00142 {
00143 this->type = OT_CONDITIONAL;
00144 this->flags = order;
00145 this->dest = 0;
00146 }
00147
00152 void Order::MakeImplicit(StationID destination)
00153 {
00154 this->type = OT_IMPLICIT;
00155 this->dest = destination;
00156 }
00157
00164 void Order::SetRefit(CargoID cargo, byte subtype)
00165 {
00166 this->refit_cargo = cargo;
00167 this->refit_subtype = subtype;
00168 }
00169
00175 bool Order::Equals(const Order &other) const
00176 {
00177
00178
00179
00180
00181
00182 if ((this->IsType(OT_GOTO_DEPOT) && this->type == other.type) &&
00183 ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0 ||
00184 (other.GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0)) {
00185 return this->GetDepotOrderType() == other.GetDepotOrderType() &&
00186 (this->GetDepotActionType() & ~ODATFB_NEAREST_DEPOT) == (other.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT);
00187 }
00188
00189 return this->type == other.type && this->flags == other.flags && this->dest == other.dest;
00190 }
00191
00198 uint32 Order::Pack() const
00199 {
00200 return this->dest << 16 | this->flags << 8 | this->type;
00201 }
00202
00208 uint16 Order::MapOldOrder() const
00209 {
00210 uint16 order = this->GetType();
00211 switch (this->type) {
00212 case OT_GOTO_STATION:
00213 if (this->GetUnloadType() & OUFB_UNLOAD) SetBit(order, 5);
00214 if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
00215 if (this->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) SetBit(order, 7);
00216 order |= GB(this->GetDestination(), 0, 8) << 8;
00217 break;
00218 case OT_GOTO_DEPOT:
00219 if (!(this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) SetBit(order, 6);
00220 SetBit(order, 7);
00221 order |= GB(this->GetDestination(), 0, 8) << 8;
00222 break;
00223 case OT_LOADING:
00224 if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
00225 break;
00226 }
00227 return order;
00228 }
00229
00234 Order::Order(uint32 packed)
00235 {
00236 this->type = (OrderType)GB(packed, 0, 8);
00237 this->flags = GB(packed, 8, 8);
00238 this->dest = GB(packed, 16, 16);
00239 this->next = NULL;
00240 this->refit_cargo = CT_NO_REFIT;
00241 this->refit_subtype = 0;
00242 this->wait_time = 0;
00243 this->travel_time = 0;
00244 }
00245
00251 void InvalidateVehicleOrder(const Vehicle *v, int data)
00252 {
00253 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
00254
00255 if (data != 0) {
00256
00257 InvalidateWindowData(WC_VEHICLE_ORDERS, v->index, data);
00258 InvalidateWindowData(WC_VEHICLE_TIMETABLE, v->index, data);
00259 return;
00260 }
00261
00262 SetWindowDirty(WC_VEHICLE_ORDERS, v->index);
00263 SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
00264 }
00265
00273 void Order::AssignOrder(const Order &other)
00274 {
00275 this->type = other.type;
00276 this->flags = other.flags;
00277 this->dest = other.dest;
00278
00279 this->refit_cargo = other.refit_cargo;
00280 this->refit_subtype = other.refit_subtype;
00281
00282 this->wait_time = other.wait_time;
00283 this->travel_time = other.travel_time;
00284 }
00285
00291 void OrderList::Initialize(Order *chain, Vehicle *v)
00292 {
00293 this->first = chain;
00294 this->first_shared = v;
00295
00296 this->num_orders = 0;
00297 this->num_manual_orders = 0;
00298 this->num_vehicles = 1;
00299 this->timetable_duration = 0;
00300
00301 for (Order *o = this->first; o != NULL; o = o->next) {
00302 ++this->num_orders;
00303 if (!o->IsType(OT_IMPLICIT)) ++this->num_manual_orders;
00304 this->timetable_duration += o->wait_time + o->travel_time;
00305 }
00306
00307 for (Vehicle *u = this->first_shared->PreviousShared(); u != NULL; u = u->PreviousShared()) {
00308 ++this->num_vehicles;
00309 this->first_shared = u;
00310 }
00311
00312 for (const Vehicle *u = v->NextShared(); u != NULL; u = u->NextShared()) ++this->num_vehicles;
00313 }
00314
00320 void OrderList::FreeChain(bool keep_orderlist)
00321 {
00322 Order *next;
00323 for (Order *o = this->first; o != NULL; o = next) {
00324 next = o->next;
00325 delete o;
00326 }
00327
00328 if (keep_orderlist) {
00329 this->first = NULL;
00330 this->num_orders = 0;
00331 this->num_manual_orders = 0;
00332 this->timetable_duration = 0;
00333 } else {
00334 delete this;
00335 }
00336 }
00337
00343 Order *OrderList::GetOrderAt(int index) const
00344 {
00345 if (index < 0) return NULL;
00346
00347 Order *order = this->first;
00348
00349 while (order != NULL && index-- > 0) {
00350 order = order->next;
00351 }
00352 return order;
00353 }
00354
00360 void OrderList::InsertOrderAt(Order *new_order, int index)
00361 {
00362 if (this->first == NULL) {
00363 this->first = new_order;
00364 } else {
00365 if (index == 0) {
00366
00367 new_order->next = this->first;
00368 this->first = new_order;
00369 } else if (index >= this->num_orders) {
00370
00371 this->GetLastOrder()->next = new_order;
00372 } else {
00373
00374 Order *order = this->GetOrderAt(index - 1);
00375 new_order->next = order->next;
00376 order->next = new_order;
00377 }
00378 }
00379 ++this->num_orders;
00380 if (!new_order->IsType(OT_IMPLICIT)) ++this->num_manual_orders;
00381 this->timetable_duration += new_order->wait_time + new_order->travel_time;
00382
00383
00384
00385 if (new_order->IsType(OT_GOTO_STATION) || new_order->IsType(OT_GOTO_WAYPOINT)) {
00386 BaseStation *bs = BaseStation::Get(new_order->GetDestination());
00387 if (bs->owner == OWNER_NONE) InvalidateWindowClassesData(WC_STATION_LIST, 0);
00388 }
00389
00390 }
00391
00392
00397 void OrderList::DeleteOrderAt(int index)
00398 {
00399 if (index >= this->num_orders) return;
00400
00401 Order *to_remove;
00402
00403 if (index == 0) {
00404 to_remove = this->first;
00405 this->first = to_remove->next;
00406 } else {
00407 Order *prev = GetOrderAt(index - 1);
00408 to_remove = prev->next;
00409 prev->next = to_remove->next;
00410 }
00411 --this->num_orders;
00412 if (!to_remove->IsType(OT_IMPLICIT)) --this->num_manual_orders;
00413 this->timetable_duration -= (to_remove->wait_time + to_remove->travel_time);
00414 delete to_remove;
00415 }
00416
00422 void OrderList::MoveOrder(int from, int to)
00423 {
00424 if (from >= this->num_orders || to >= this->num_orders || from == to) return;
00425
00426 Order *moving_one;
00427
00428
00429 if (from == 0) {
00430 moving_one = this->first;
00431 this->first = moving_one->next;
00432 } else {
00433 Order *one_before = GetOrderAt(from - 1);
00434 moving_one = one_before->next;
00435 one_before->next = moving_one->next;
00436 }
00437
00438
00439 if (to == 0) {
00440 moving_one->next = this->first;
00441 this->first = moving_one;
00442 } else {
00443 Order *one_before = GetOrderAt(to - 1);
00444 moving_one->next = one_before->next;
00445 one_before->next = moving_one;
00446 }
00447 }
00448
00454 void OrderList::RemoveVehicle(Vehicle *v)
00455 {
00456 --this->num_vehicles;
00457 if (v == this->first_shared) this->first_shared = v->NextShared();
00458 }
00459
00464 bool OrderList::IsVehicleInSharedOrdersList(const Vehicle *v) const
00465 {
00466 for (const Vehicle *v_shared = this->first_shared; v_shared != NULL; v_shared = v_shared->NextShared()) {
00467 if (v_shared == v) return true;
00468 }
00469
00470 return false;
00471 }
00472
00478 int OrderList::GetPositionInSharedOrderList(const Vehicle *v) const
00479 {
00480 int count = 0;
00481 for (const Vehicle *v_shared = v->PreviousShared(); v_shared != NULL; v_shared = v_shared->PreviousShared()) count++;
00482 return count;
00483 }
00484
00489 bool OrderList::IsCompleteTimetable() const
00490 {
00491 for (Order *o = this->first; o != NULL; o = o->next) {
00492
00493 if (o->IsType(OT_IMPLICIT)) continue;
00494 if (!o->IsCompletelyTimetabled()) return false;
00495 }
00496 return true;
00497 }
00498
00502 void OrderList::DebugCheckSanity() const
00503 {
00504 VehicleOrderID check_num_orders = 0;
00505 VehicleOrderID check_num_manual_orders = 0;
00506 uint check_num_vehicles = 0;
00507 Ticks check_timetable_duration = 0;
00508
00509 DEBUG(misc, 6, "Checking OrderList %hu for sanity...", this->index);
00510
00511 for (const Order *o = this->first; o != NULL; o = o->next) {
00512 ++check_num_orders;
00513 if (!o->IsType(OT_IMPLICIT)) ++check_num_manual_orders;
00514 check_timetable_duration += o->wait_time + o->travel_time;
00515 }
00516 assert(this->num_orders == check_num_orders);
00517 assert(this->num_manual_orders == check_num_manual_orders);
00518 assert(this->timetable_duration == check_timetable_duration);
00519
00520 for (const Vehicle *v = this->first_shared; v != NULL; v = v->NextShared()) {
00521 ++check_num_vehicles;
00522 assert(v->orders.list == this);
00523 }
00524 assert(this->num_vehicles == check_num_vehicles);
00525 DEBUG(misc, 6, "... detected %u orders (%u manual), %u vehicles, %i ticks",
00526 (uint)this->num_orders, (uint)this->num_manual_orders,
00527 this->num_vehicles, this->timetable_duration);
00528 }
00529
00537 static inline bool OrderGoesToStation(const Vehicle *v, const Order *o)
00538 {
00539 return o->IsType(OT_GOTO_STATION) ||
00540 (v->type == VEH_AIRCRAFT && o->IsType(OT_GOTO_DEPOT) && !(o->GetDepotActionType() & ODATFB_NEAREST_DEPOT));
00541 }
00542
00549 static void DeleteOrderWarnings(const Vehicle *v)
00550 {
00551 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS);
00552 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_VOID_ORDER);
00553 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY);
00554 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY);
00555 }
00556
00563 TileIndex Order::GetLocation(const Vehicle *v, bool airport) const
00564 {
00565 switch (this->GetType()) {
00566 case OT_GOTO_WAYPOINT:
00567 case OT_GOTO_STATION:
00568 case OT_IMPLICIT:
00569 if (airport && v->type == VEH_AIRCRAFT) return Station::Get(this->GetDestination())->airport.tile;
00570 return BaseStation::Get(this->GetDestination())->xy;
00571
00572 case OT_GOTO_DEPOT:
00573 if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) return INVALID_TILE;
00574 return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination())->xy : Depot::Get(this->GetDestination())->xy;
00575
00576 default:
00577 return INVALID_TILE;
00578 }
00579 }
00580
00590 uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle *v, int conditional_depth)
00591 {
00592 if (cur->IsType(OT_CONDITIONAL)) {
00593 if (conditional_depth > v->GetNumOrders()) return 0;
00594
00595 conditional_depth++;
00596
00597 int dist1 = GetOrderDistance(prev, v->GetOrder(cur->GetConditionSkipToOrder()), v, conditional_depth);
00598 int dist2 = GetOrderDistance(prev, cur->next == NULL ? v->orders.list->GetFirstOrder() : cur->next, v, conditional_depth);
00599 return max(dist1, dist2);
00600 }
00601
00602 TileIndex prev_tile = prev->GetLocation(v, true);
00603 TileIndex cur_tile = cur->GetLocation(v, true);
00604 if (prev_tile == INVALID_TILE || cur_tile == INVALID_TILE) return 0;
00605 return v->type == VEH_AIRCRAFT ? DistanceSquare(prev_tile, cur_tile) : DistanceManhattan(prev_tile, cur_tile);
00606 }
00607
00621 CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00622 {
00623 VehicleID veh = GB(p1, 0, 20);
00624 VehicleOrderID sel_ord = GB(p1, 20, 8);
00625 Order new_order(p2);
00626
00627 Vehicle *v = Vehicle::GetIfValid(veh);
00628 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00629
00630 CommandCost ret = CheckOwnership(v->owner);
00631 if (ret.Failed()) return ret;
00632
00633
00634
00635 switch (new_order.GetType()) {
00636 case OT_GOTO_STATION: {
00637 const Station *st = Station::GetIfValid(new_order.GetDestination());
00638 if (st == NULL) return CMD_ERROR;
00639
00640 if (st->owner != OWNER_NONE) {
00641 CommandCost ret = CheckOwnership(st->owner);
00642 if (ret.Failed()) return ret;
00643 }
00644
00645 if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00646 for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
00647 if (!CanVehicleUseStation(u, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED);
00648 }
00649
00650
00651 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
00652
00653
00654 switch (new_order.GetLoadType()) {
00655 case OLF_LOAD_IF_POSSIBLE: case OLFB_FULL_LOAD: case OLF_FULL_LOAD_ANY: case OLFB_NO_LOAD: break;
00656 default: return CMD_ERROR;
00657 }
00658 switch (new_order.GetUnloadType()) {
00659 case OUF_UNLOAD_IF_POSSIBLE: case OUFB_UNLOAD: case OUFB_TRANSFER: case OUFB_NO_UNLOAD: break;
00660 default: return CMD_ERROR;
00661 }
00662
00663
00664 switch (new_order.GetStopLocation()) {
00665 case OSL_PLATFORM_NEAR_END:
00666 case OSL_PLATFORM_MIDDLE:
00667 if (v->type != VEH_TRAIN) return CMD_ERROR;
00668
00669 case OSL_PLATFORM_FAR_END:
00670 break;
00671
00672 default:
00673 return CMD_ERROR;
00674 }
00675
00676 break;
00677 }
00678
00679 case OT_GOTO_DEPOT: {
00680 if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
00681 if (v->type == VEH_AIRCRAFT) {
00682 const Station *st = Station::GetIfValid(new_order.GetDestination());
00683
00684 if (st == NULL) return CMD_ERROR;
00685
00686 CommandCost ret = CheckOwnership(st->owner);
00687 if (ret.Failed()) return ret;
00688
00689 if (!CanVehicleUseStation(v, st) || !st->airport.HasHangar()) {
00690 return CMD_ERROR;
00691 }
00692 } else {
00693 const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
00694
00695 if (dp == NULL) return CMD_ERROR;
00696
00697 CommandCost ret = CheckOwnership(GetTileOwner(dp->xy));
00698 if (ret.Failed()) return ret;
00699
00700 switch (v->type) {
00701 case VEH_TRAIN:
00702 if (!IsRailDepotTile(dp->xy)) return CMD_ERROR;
00703 break;
00704
00705 case VEH_ROAD:
00706 if (!IsRoadDepotTile(dp->xy)) return CMD_ERROR;
00707 break;
00708
00709 case VEH_SHIP:
00710 if (!IsShipDepotTile(dp->xy)) return CMD_ERROR;
00711 break;
00712
00713 default: return CMD_ERROR;
00714 }
00715 }
00716 }
00717
00718 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
00719 if (new_order.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS | ((new_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0 ? ODTFB_SERVICE : 0))) return CMD_ERROR;
00720 if (new_order.GetDepotActionType() & ~(ODATFB_HALT | ODATFB_NEAREST_DEPOT)) return CMD_ERROR;
00721 if ((new_order.GetDepotOrderType() & ODTFB_SERVICE) && (new_order.GetDepotActionType() & ODATFB_HALT)) return CMD_ERROR;
00722 break;
00723 }
00724
00725 case OT_GOTO_WAYPOINT: {
00726 const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
00727 if (wp == NULL) return CMD_ERROR;
00728
00729 switch (v->type) {
00730 default: return CMD_ERROR;
00731
00732 case VEH_TRAIN: {
00733 if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00734
00735 CommandCost ret = CheckOwnership(wp->owner);
00736 if (ret.Failed()) return ret;
00737 break;
00738 }
00739
00740 case VEH_SHIP:
00741 if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00742 if (wp->owner != OWNER_NONE) {
00743 CommandCost ret = CheckOwnership(wp->owner);
00744 if (ret.Failed()) return ret;
00745 }
00746 break;
00747 }
00748
00749
00750
00751
00752 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
00753 break;
00754 }
00755
00756 case OT_CONDITIONAL: {
00757 VehicleOrderID skip_to = new_order.GetConditionSkipToOrder();
00758 if (skip_to != 0 && skip_to >= v->GetNumOrders()) return CMD_ERROR;
00759 if (new_order.GetConditionVariable() >= OCV_END) return CMD_ERROR;
00760
00761 OrderConditionComparator occ = new_order.GetConditionComparator();
00762 if (occ >= OCC_END) return CMD_ERROR;
00763 switch (new_order.GetConditionVariable()) {
00764 case OCV_REQUIRES_SERVICE:
00765 if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
00766 break;
00767
00768 case OCV_UNCONDITIONALLY:
00769 if (occ != OCC_EQUALS) return CMD_ERROR;
00770 if (new_order.GetConditionValue() != 0) return CMD_ERROR;
00771 break;
00772
00773 case OCV_LOAD_PERCENTAGE:
00774 case OCV_RELIABILITY:
00775 if (new_order.GetConditionValue() > 100) return CMD_ERROR;
00776
00777 default:
00778 if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) return CMD_ERROR;
00779 break;
00780 }
00781 break;
00782 }
00783
00784 default: return CMD_ERROR;
00785 }
00786
00787 if (sel_ord > v->GetNumOrders()) return CMD_ERROR;
00788
00789 if (v->GetNumOrders() >= MAX_VEH_ORDER_ID) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS);
00790 if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
00791 if (v->orders.list == NULL && !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
00792
00793 if (v->type == VEH_SHIP && _settings_game.pf.pathfinder_for_ships != VPF_NPF) {
00794
00795 const Order *prev = NULL;
00796 uint n = 0;
00797
00798
00799
00800
00801 const Order *o;
00802 FOR_VEHICLE_ORDERS(v, o) {
00803 switch (o->GetType()) {
00804 case OT_GOTO_STATION:
00805 case OT_GOTO_DEPOT:
00806 case OT_GOTO_WAYPOINT:
00807 prev = o;
00808 break;
00809
00810 default: break;
00811 }
00812 if (++n == sel_ord && prev != NULL) break;
00813 }
00814 if (prev != NULL) {
00815 uint dist;
00816 if (new_order.IsType(OT_CONDITIONAL)) {
00817
00818 dist = GetOrderDistance(prev, v->GetOrder(new_order.GetConditionSkipToOrder()), v);
00819 } else {
00820 dist = GetOrderDistance(prev, &new_order, v);
00821 }
00822
00823 if (dist >= 130) {
00824 return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION);
00825 }
00826 }
00827 }
00828
00829 if (flags & DC_EXEC) {
00830 Order *new_o = new Order();
00831 new_o->AssignOrder(new_order);
00832 InsertOrder(v, new_o, sel_ord);
00833 }
00834
00835 return CommandCost();
00836 }
00837
00844 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord)
00845 {
00846
00847 if (v->orders.list == NULL) {
00848 v->orders.list = new OrderList(new_o, v);
00849 } else {
00850 v->orders.list->InsertOrderAt(new_o, sel_ord);
00851 }
00852
00853 Vehicle *u = v->FirstShared();
00854 DeleteOrderWarnings(u);
00855 for (; u != NULL; u = u->NextShared()) {
00856 assert(v->orders.list == u->orders.list);
00857
00858
00859
00860
00861
00862 if (sel_ord <= u->cur_real_order_index) {
00863 uint cur = u->cur_real_order_index + 1;
00864
00865 if (cur < u->GetNumOrders()) {
00866 u->cur_real_order_index = cur;
00867 }
00868 }
00869 if (sel_ord == u->cur_implicit_order_index && u->IsGroundVehicle()) {
00870
00871
00872
00873 uint16 &gv_flags = u->GetGroundVehicleFlags();
00874 SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
00875 }
00876 if (sel_ord <= u->cur_implicit_order_index) {
00877 uint cur = u->cur_implicit_order_index + 1;
00878
00879 if (cur < u->GetNumOrders()) {
00880 u->cur_implicit_order_index = cur;
00881 }
00882 }
00883
00884 InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
00885 }
00886
00887
00888 VehicleOrderID cur_order_id = 0;
00889 Order *order;
00890 FOR_VEHICLE_ORDERS(v, order) {
00891 if (order->IsType(OT_CONDITIONAL)) {
00892 VehicleOrderID order_id = order->GetConditionSkipToOrder();
00893 if (order_id >= sel_ord) {
00894 order->SetConditionSkipToOrder(order_id + 1);
00895 }
00896 if (order_id == cur_order_id) {
00897 order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
00898 }
00899 }
00900 cur_order_id++;
00901 }
00902
00903
00904 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00905 }
00906
00912 static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags)
00913 {
00914 if (flags & DC_EXEC) {
00915 DeleteVehicleOrders(dst);
00916 InvalidateVehicleOrder(dst, -1);
00917 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
00918 }
00919 return CommandCost();
00920 }
00921
00931 CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00932 {
00933 VehicleID veh_id = GB(p1, 0, 20);
00934 VehicleOrderID sel_ord = GB(p2, 0, 8);
00935
00936 Vehicle *v = Vehicle::GetIfValid(veh_id);
00937
00938 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00939
00940 CommandCost ret = CheckOwnership(v->owner);
00941 if (ret.Failed()) return ret;
00942
00943
00944 if (sel_ord >= v->GetNumOrders()) return DecloneOrder(v, flags);
00945
00946 if (v->GetOrder(sel_ord) == NULL) return CMD_ERROR;
00947
00948 if (flags & DC_EXEC) DeleteOrder(v, sel_ord);
00949 return CommandCost();
00950 }
00951
00956 static void CancelLoadingDueToDeletedOrder(Vehicle *v)
00957 {
00958 assert(v->current_order.IsType(OT_LOADING));
00959
00960
00961 v->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
00962
00963
00964 if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) v->current_order.SetLoadType(OLF_LOAD_IF_POSSIBLE);
00965 }
00966
00972 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord)
00973 {
00974 v->orders.list->DeleteOrderAt(sel_ord);
00975
00976 Vehicle *u = v->FirstShared();
00977 DeleteOrderWarnings(u);
00978 for (; u != NULL; u = u->NextShared()) {
00979 assert(v->orders.list == u->orders.list);
00980
00981 if (sel_ord == u->cur_real_order_index && u->current_order.IsType(OT_LOADING)) {
00982 CancelLoadingDueToDeletedOrder(u);
00983 }
00984
00985 if (sel_ord < u->cur_real_order_index) {
00986 u->cur_real_order_index--;
00987 } else if (sel_ord == u->cur_real_order_index) {
00988 u->UpdateRealOrderIndex();
00989 }
00990
00991 if (sel_ord < u->cur_implicit_order_index) {
00992 u->cur_implicit_order_index--;
00993 } else if (sel_ord == u->cur_implicit_order_index) {
00994
00995 if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
00996
00997
00998 while (u->cur_implicit_order_index != u->cur_real_order_index && !u->GetOrder(u->cur_implicit_order_index)->IsType(OT_IMPLICIT)) {
00999 u->cur_implicit_order_index++;
01000 if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
01001 }
01002 }
01003
01004
01005 InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
01006 }
01007
01008
01009 VehicleOrderID cur_order_id = 0;
01010 Order *order = NULL;
01011 FOR_VEHICLE_ORDERS(v, order) {
01012 if (order->IsType(OT_CONDITIONAL)) {
01013 VehicleOrderID order_id = order->GetConditionSkipToOrder();
01014 if (order_id >= sel_ord) {
01015 order_id = max(order_id - 1, 0);
01016 }
01017 if (order_id == cur_order_id) {
01018 order_id = (order_id + 1) % v->GetNumOrders();
01019 }
01020 order->SetConditionSkipToOrder(order_id);
01021 }
01022 cur_order_id++;
01023 }
01024
01025 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
01026 }
01027
01037 CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01038 {
01039 VehicleID veh_id = GB(p1, 0, 20);
01040 VehicleOrderID sel_ord = GB(p2, 0, 8);
01041
01042 Vehicle *v = Vehicle::GetIfValid(veh_id);
01043
01044 if (v == NULL || !v->IsPrimaryVehicle() || sel_ord == v->cur_implicit_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR;
01045
01046 CommandCost ret = CheckOwnership(v->owner);
01047 if (ret.Failed()) return ret;
01048
01049 if (flags & DC_EXEC) {
01050 v->cur_implicit_order_index = v->cur_real_order_index = sel_ord;
01051 v->UpdateRealOrderIndex();
01052
01053 if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
01054
01055 InvalidateVehicleOrder(v, -2);
01056 }
01057
01058
01059 if (v->type == VEH_AIRCRAFT) SetWindowClassesDirty(WC_AIRCRAFT_LIST);
01060 if (v->type == VEH_SHIP) SetWindowClassesDirty(WC_SHIPS_LIST);
01061
01062 return CommandCost();
01063 }
01064
01078 CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01079 {
01080 VehicleID veh = GB(p1, 0, 20);
01081 VehicleOrderID moving_order = GB(p2, 0, 16);
01082 VehicleOrderID target_order = GB(p2, 16, 16);
01083
01084 Vehicle *v = Vehicle::GetIfValid(veh);
01085 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01086
01087 CommandCost ret = CheckOwnership(v->owner);
01088 if (ret.Failed()) return ret;
01089
01090
01091 if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() ||
01092 moving_order == target_order || v->GetNumOrders() <= 1) return CMD_ERROR;
01093
01094 Order *moving_one = v->GetOrder(moving_order);
01095
01096 if (moving_one == NULL) return CMD_ERROR;
01097
01098 if (flags & DC_EXEC) {
01099 v->orders.list->MoveOrder(moving_order, target_order);
01100
01101
01102 Vehicle *u = v->FirstShared();
01103
01104 DeleteOrderWarnings(u);
01105
01106 for (; u != NULL; u = u->NextShared()) {
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123 if (u->cur_real_order_index == moving_order) {
01124 u->cur_real_order_index = target_order;
01125 } else if (u->cur_real_order_index > moving_order && u->cur_real_order_index <= target_order) {
01126 u->cur_real_order_index--;
01127 } else if (u->cur_real_order_index < moving_order && u->cur_real_order_index >= target_order) {
01128 u->cur_real_order_index++;
01129 }
01130
01131 if (u->cur_implicit_order_index == moving_order) {
01132 u->cur_implicit_order_index = target_order;
01133 } else if (u->cur_implicit_order_index > moving_order && u->cur_implicit_order_index <= target_order) {
01134 u->cur_implicit_order_index--;
01135 } else if (u->cur_implicit_order_index < moving_order && u->cur_implicit_order_index >= target_order) {
01136 u->cur_implicit_order_index++;
01137 }
01138
01139 assert(v->orders.list == u->orders.list);
01140
01141 InvalidateVehicleOrder(u, moving_order | (target_order << 8));
01142 }
01143
01144
01145 Order *order;
01146 FOR_VEHICLE_ORDERS(v, order) {
01147 if (order->IsType(OT_CONDITIONAL)) {
01148 VehicleOrderID order_id = order->GetConditionSkipToOrder();
01149 if (order_id == moving_order) {
01150 order_id = target_order;
01151 } else if (order_id > moving_order && order_id <= target_order) {
01152 order_id--;
01153 } else if (order_id < moving_order && order_id >= target_order) {
01154 order_id++;
01155 }
01156 order->SetConditionSkipToOrder(order_id);
01157 }
01158 }
01159
01160
01161 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
01162 }
01163
01164 return CommandCost();
01165 }
01166
01182 CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01183 {
01184 VehicleOrderID sel_ord = GB(p1, 20, 8);
01185 VehicleID veh = GB(p1, 0, 20);
01186 ModifyOrderFlags mof = Extract<ModifyOrderFlags, 0, 4>(p2);
01187 uint16 data = GB(p2, 4, 11);
01188
01189 if (mof >= MOF_END) return CMD_ERROR;
01190
01191 Vehicle *v = Vehicle::GetIfValid(veh);
01192 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01193
01194 CommandCost ret = CheckOwnership(v->owner);
01195 if (ret.Failed()) return ret;
01196
01197
01198 if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
01199
01200 Order *order = v->GetOrder(sel_ord);
01201 switch (order->GetType()) {
01202 case OT_GOTO_STATION:
01203 if (mof == MOF_COND_VARIABLE || mof == MOF_COND_COMPARATOR || mof == MOF_DEPOT_ACTION || mof == MOF_COND_VALUE) return CMD_ERROR;
01204 break;
01205
01206 case OT_GOTO_DEPOT:
01207 if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR;
01208 break;
01209
01210 case OT_GOTO_WAYPOINT:
01211 if (mof != MOF_NON_STOP) return CMD_ERROR;
01212 break;
01213
01214 case OT_CONDITIONAL:
01215 if (mof != MOF_COND_VARIABLE && mof != MOF_COND_COMPARATOR && mof != MOF_COND_VALUE && mof != MOF_COND_DESTINATION) return CMD_ERROR;
01216 break;
01217
01218 default:
01219 return CMD_ERROR;
01220 }
01221
01222 switch (mof) {
01223 default: NOT_REACHED();
01224
01225 case MOF_NON_STOP:
01226 if (!v->IsGroundVehicle()) return CMD_ERROR;
01227 if (data >= ONSF_END) return CMD_ERROR;
01228 if (data == order->GetNonStopType()) return CMD_ERROR;
01229 break;
01230
01231 case MOF_STOP_LOCATION:
01232 if (v->type != VEH_TRAIN) return CMD_ERROR;
01233 if (data >= OSL_END) return CMD_ERROR;
01234 break;
01235
01236 case MOF_UNLOAD:
01237 if ((data & ~(OUFB_UNLOAD | OUFB_TRANSFER | OUFB_NO_UNLOAD)) != 0) return CMD_ERROR;
01238
01239 if (data != 0 && ((data & (OUFB_UNLOAD | OUFB_TRANSFER)) != 0) == ((data & OUFB_NO_UNLOAD) != 0)) return CMD_ERROR;
01240 if (data == order->GetUnloadType()) return CMD_ERROR;
01241 break;
01242
01243 case MOF_LOAD:
01244 if (data > OLFB_NO_LOAD || data == 1) return CMD_ERROR;
01245 if (data == order->GetLoadType()) return CMD_ERROR;
01246 break;
01247
01248 case MOF_DEPOT_ACTION:
01249 if (data >= DA_END) return CMD_ERROR;
01250 break;
01251
01252 case MOF_COND_VARIABLE:
01253 if (data >= OCV_END) return CMD_ERROR;
01254 break;
01255
01256 case MOF_COND_COMPARATOR:
01257 if (data >= OCC_END) return CMD_ERROR;
01258 switch (order->GetConditionVariable()) {
01259 case OCV_UNCONDITIONALLY: return CMD_ERROR;
01260
01261 case OCV_REQUIRES_SERVICE:
01262 if (data != OCC_IS_TRUE && data != OCC_IS_FALSE) return CMD_ERROR;
01263 break;
01264
01265 default:
01266 if (data == OCC_IS_TRUE || data == OCC_IS_FALSE) return CMD_ERROR;
01267 break;
01268 }
01269 break;
01270
01271 case MOF_COND_VALUE:
01272 switch (order->GetConditionVariable()) {
01273 case OCV_UNCONDITIONALLY: return CMD_ERROR;
01274
01275 case OCV_LOAD_PERCENTAGE:
01276 case OCV_RELIABILITY:
01277 if (data > 100) return CMD_ERROR;
01278 break;
01279
01280 default:
01281 if (data > 2047) return CMD_ERROR;
01282 break;
01283 }
01284 break;
01285
01286 case MOF_COND_DESTINATION:
01287 if (data >= v->GetNumOrders()) return CMD_ERROR;
01288 break;
01289 }
01290
01291 if (flags & DC_EXEC) {
01292 switch (mof) {
01293 case MOF_NON_STOP:
01294 order->SetNonStopType((OrderNonStopFlags)data);
01295 if (data & ONSF_NO_STOP_AT_DESTINATION_STATION) order->SetRefit(CT_NO_REFIT);
01296 break;
01297
01298 case MOF_STOP_LOCATION:
01299 order->SetStopLocation((OrderStopLocation)data);
01300 break;
01301
01302 case MOF_UNLOAD:
01303 order->SetUnloadType((OrderUnloadFlags)data);
01304 break;
01305
01306 case MOF_LOAD:
01307 order->SetLoadType((OrderLoadFlags)data);
01308 if (data & OLFB_NO_LOAD) order->SetRefit(CT_NO_REFIT);
01309 break;
01310
01311 case MOF_DEPOT_ACTION: {
01312 switch (data) {
01313 case DA_ALWAYS_GO:
01314 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01315 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01316 break;
01317
01318 case DA_SERVICE:
01319 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() | ODTFB_SERVICE));
01320 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01321 order->SetRefit(CT_NO_REFIT);
01322 break;
01323
01324 case DA_STOP:
01325 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01326 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() | ODATFB_HALT));
01327 order->SetRefit(CT_NO_REFIT);
01328 break;
01329
01330 default:
01331 NOT_REACHED();
01332 }
01333 break;
01334 }
01335
01336 case MOF_COND_VARIABLE: {
01337 order->SetConditionVariable((OrderConditionVariable)data);
01338
01339 OrderConditionComparator occ = order->GetConditionComparator();
01340 switch (order->GetConditionVariable()) {
01341 case OCV_UNCONDITIONALLY:
01342 order->SetConditionComparator(OCC_EQUALS);
01343 order->SetConditionValue(0);
01344 break;
01345
01346 case OCV_REQUIRES_SERVICE:
01347 if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
01348 break;
01349
01350 case OCV_LOAD_PERCENTAGE:
01351 case OCV_RELIABILITY:
01352 if (order->GetConditionValue() > 100) order->SetConditionValue(100);
01353
01354 default:
01355 if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) order->SetConditionComparator(OCC_EQUALS);
01356 break;
01357 }
01358 break;
01359 }
01360
01361 case MOF_COND_COMPARATOR:
01362 order->SetConditionComparator((OrderConditionComparator)data);
01363 break;
01364
01365 case MOF_COND_VALUE:
01366 order->SetConditionValue(data);
01367 break;
01368
01369 case MOF_COND_DESTINATION:
01370 order->SetConditionSkipToOrder(data);
01371 break;
01372
01373 default: NOT_REACHED();
01374 }
01375
01376
01377 Vehicle *u = v->FirstShared();
01378 DeleteOrderWarnings(u);
01379 for (; u != NULL; u = u->NextShared()) {
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389 if (sel_ord == u->cur_real_order_index &&
01390 (u->current_order.IsType(OT_GOTO_STATION) || u->current_order.IsType(OT_LOADING)) &&
01391 u->current_order.GetLoadType() != order->GetLoadType()) {
01392 u->current_order.SetLoadType(order->GetLoadType());
01393 }
01394 InvalidateVehicleOrder(u, -2);
01395 }
01396 }
01397
01398 return CommandCost();
01399 }
01400
01407 bool CheckAircraftOrderDistance(const Aircraft *v, const Order *first)
01408 {
01409 if (first == NULL || v->acache.cached_max_range == 0) return true;
01410
01411
01412
01413 for (const Order *o = first; o != NULL; o = o->next) {
01414 switch (o->GetType()) {
01415 case OT_GOTO_STATION:
01416 case OT_GOTO_DEPOT:
01417 case OT_GOTO_WAYPOINT:
01418
01419 if (GetOrderDistance(o, o->next != NULL ? o->next : first, v) > v->acache.cached_max_range_sqr) return false;
01420 break;
01421
01422 default: break;
01423 }
01424 }
01425
01426 return true;
01427 }
01428
01440 CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01441 {
01442 VehicleID veh_src = GB(p2, 0, 20);
01443 VehicleID veh_dst = GB(p1, 0, 20);
01444
01445 Vehicle *dst = Vehicle::GetIfValid(veh_dst);
01446 if (dst == NULL || !dst->IsPrimaryVehicle()) return CMD_ERROR;
01447
01448 CommandCost ret = CheckOwnership(dst->owner);
01449 if (ret.Failed()) return ret;
01450
01451 switch (GB(p1, 30, 2)) {
01452 case CO_SHARE: {
01453 Vehicle *src = Vehicle::GetIfValid(veh_src);
01454
01455
01456 if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
01457
01458 CommandCost ret = CheckOwnership(src->owner);
01459 if (ret.Failed()) return ret;
01460
01461
01462 if (src->type == VEH_ROAD && RoadVehicle::From(src)->IsBus() != RoadVehicle::From(dst)->IsBus()) {
01463 return CMD_ERROR;
01464 }
01465
01466
01467 if (src->FirstShared() == dst->FirstShared()) return CMD_ERROR;
01468
01469 const Order *order;
01470
01471 FOR_VEHICLE_ORDERS(src, order) {
01472 if (OrderGoesToStation(dst, order) &&
01473 !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
01474 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
01475 }
01476 }
01477
01478
01479 if (dst->type == VEH_AIRCRAFT && !CheckAircraftOrderDistance(Aircraft::From(dst), src->GetFirstOrder())) {
01480 return_cmd_error(STR_ERROR_AIRCRAFT_NOT_ENOUGH_RANGE);
01481 }
01482
01483 if (src->orders.list == NULL && !OrderList::CanAllocateItem()) {
01484 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
01485 }
01486
01487 if (flags & DC_EXEC) {
01488
01489
01490
01491 DeleteVehicleOrders(dst, false, dst->GetNumOrders() != src->GetNumOrders());
01492
01493 dst->orders.list = src->orders.list;
01494
01495
01496 dst->AddToShared(src);
01497
01498 InvalidateVehicleOrder(dst, -1);
01499 InvalidateVehicleOrder(src, -2);
01500
01501 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
01502 }
01503 break;
01504 }
01505
01506 case CO_COPY: {
01507 Vehicle *src = Vehicle::GetIfValid(veh_src);
01508
01509
01510 if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
01511
01512 CommandCost ret = CheckOwnership(src->owner);
01513 if (ret.Failed()) return ret;
01514
01515
01516
01517 const Order *order;
01518 FOR_VEHICLE_ORDERS(src, order) {
01519 if (OrderGoesToStation(dst, order) &&
01520 !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
01521 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
01522 }
01523 }
01524
01525
01526 if (dst->type == VEH_AIRCRAFT && !CheckAircraftOrderDistance(Aircraft::From(dst), src->GetFirstOrder())) {
01527 return_cmd_error(STR_ERROR_AIRCRAFT_NOT_ENOUGH_RANGE);
01528 }
01529
01530
01531 if (!Order::CanAllocateItem(src->GetNumOrders()) || !OrderList::CanAllocateItem()) {
01532 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
01533 }
01534
01535 if (flags & DC_EXEC) {
01536 const Order *order;
01537 Order *first = NULL;
01538 Order **order_dst;
01539
01540
01541
01542
01543 DeleteVehicleOrders(dst, true, dst->GetNumOrders() != src->GetNumOrders());
01544
01545 order_dst = &first;
01546 FOR_VEHICLE_ORDERS(src, order) {
01547 *order_dst = new Order();
01548 (*order_dst)->AssignOrder(*order);
01549 order_dst = &(*order_dst)->next;
01550 }
01551 if (dst->orders.list == NULL) {
01552 dst->orders.list = new OrderList(first, dst);
01553 } else {
01554 assert(dst->orders.list->GetFirstOrder() == NULL);
01555 assert(!dst->orders.list->IsShared());
01556 delete dst->orders.list;
01557 assert(OrderList::CanAllocateItem());
01558 dst->orders.list = new OrderList(first, dst);
01559 }
01560
01561 InvalidateVehicleOrder(dst, -1);
01562
01563 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
01564 }
01565 break;
01566 }
01567
01568 case CO_UNSHARE: return DecloneOrder(dst, flags);
01569 default: return CMD_ERROR;
01570 }
01571
01572 return CommandCost();
01573 }
01574
01587 CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01588 {
01589 VehicleID veh = GB(p1, 0, 20);
01590 VehicleOrderID order_number = GB(p2, 16, 8);
01591 CargoID cargo = GB(p2, 0, 8);
01592 byte subtype = GB(p2, 8, 8);
01593
01594 if (cargo >= NUM_CARGO && cargo != CT_NO_REFIT && cargo != CT_AUTO_REFIT) return CMD_ERROR;
01595
01596 const Vehicle *v = Vehicle::GetIfValid(veh);
01597 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01598
01599 CommandCost ret = CheckOwnership(v->owner);
01600 if (ret.Failed()) return ret;
01601
01602 Order *order = v->GetOrder(order_number);
01603 if (order == NULL) return CMD_ERROR;
01604
01605
01606 if (cargo == CT_AUTO_REFIT && !order->IsType(OT_GOTO_STATION)) return CMD_ERROR;
01607
01608 if (flags & DC_EXEC) {
01609 order->SetRefit(cargo, subtype);
01610
01611
01612 if (cargo != CT_NO_REFIT) {
01613 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01614 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01615 }
01616
01617 for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
01618
01619 InvalidateVehicleOrder(u, -2);
01620
01621
01622 if (u->cur_real_order_index == order_number && (u->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) {
01623 u->current_order.SetRefit(cargo, subtype);
01624 }
01625 }
01626 }
01627
01628 return CommandCost();
01629 }
01630
01631
01637 void CheckOrders(const Vehicle *v)
01638 {
01639
01640 if (_settings_client.gui.order_review_system == 0) return;
01641
01642
01643 if (v->vehstatus & VS_CRASHED) return;
01644
01645
01646 if (_settings_client.gui.order_review_system == 1 && (v->vehstatus & VS_STOPPED)) return;
01647
01648
01649 if (v->FirstShared() != v) return;
01650
01651
01652 if (v->owner == _local_company && v->day_counter % 20 == 0) {
01653 int n_st, problem_type = -1;
01654 const Order *order;
01655 int message = 0;
01656
01657
01658 n_st = 0;
01659
01660 FOR_VEHICLE_ORDERS(v, order) {
01661
01662 if (order->IsType(OT_DUMMY)) {
01663 problem_type = 1;
01664 break;
01665 }
01666
01667 if (order->IsType(OT_GOTO_STATION)) {
01668 const Station *st = Station::Get(order->GetDestination());
01669
01670 n_st++;
01671 if (!CanVehicleUseStation(v, st)) problem_type = 3;
01672 }
01673 }
01674
01675
01676 if (v->GetNumOrders() > 1) {
01677 const Order *last = v->GetLastOrder();
01678
01679 if (v->orders.list->GetFirstOrder()->Equals(*last)) {
01680 problem_type = 2;
01681 }
01682 }
01683
01684
01685 if (n_st < 2 && problem_type == -1) problem_type = 0;
01686
01687 #ifndef NDEBUG
01688 if (v->orders.list != NULL) v->orders.list->DebugCheckSanity();
01689 #endif
01690
01691
01692 if (problem_type < 0) return;
01693
01694 message = STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS + problem_type;
01695
01696
01697 SetDParam(0, v->index);
01698 AddVehicleNewsItem(
01699 message,
01700 NS_ADVICE,
01701 v->index
01702 );
01703 }
01704 }
01705
01711 void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination)
01712 {
01713 Vehicle *v;
01714
01715
01716
01717
01718
01719
01720 FOR_ALL_VEHICLES(v) {
01721 Order *order;
01722
01723 order = &v->current_order;
01724 if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
01725 v->current_order.GetDestination() == destination) {
01726 order->MakeDummy();
01727 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
01728 }
01729
01730
01731 int id = -1;
01732 FOR_VEHICLE_ORDERS(v, order) {
01733 id++;
01734 restart:
01735
01736 OrderType ot = order->GetType();
01737 if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
01738 if (ot == OT_IMPLICIT || (v->type == VEH_AIRCRAFT && ot == OT_GOTO_DEPOT)) ot = OT_GOTO_STATION;
01739 if (ot == type && order->GetDestination() == destination) {
01740
01741
01742
01743 if (order->IsType(OT_IMPLICIT)) {
01744 order = order->next;
01745 DeleteOrder(v, id);
01746 if (order != NULL) goto restart;
01747 break;
01748 }
01749
01750 order->MakeDummy();
01751 for (const Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
01752
01753 InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
01754 InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
01755 }
01756 }
01757 }
01758 }
01759
01760 OrderBackup::RemoveOrder(type, destination);
01761 }
01762
01767 bool Vehicle::HasDepotOrder() const
01768 {
01769 const Order *order;
01770
01771 FOR_VEHICLE_ORDERS(this, order) {
01772 if (order->IsType(OT_GOTO_DEPOT)) return true;
01773 }
01774
01775 return false;
01776 }
01777
01787 void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist, bool reset_order_indices)
01788 {
01789 DeleteOrderWarnings(v);
01790
01791 if (v->IsOrderListShared()) {
01792
01793 v->RemoveFromShared();
01794 v->orders.list = NULL;
01795 } else if (v->orders.list != NULL) {
01796
01797 v->orders.list->FreeChain(keep_orderlist);
01798 if (!keep_orderlist) v->orders.list = NULL;
01799 }
01800
01801 if (reset_order_indices) {
01802 v->cur_implicit_order_index = v->cur_real_order_index = 0;
01803 if (v->current_order.IsType(OT_LOADING)) {
01804 CancelLoadingDueToDeletedOrder(v);
01805 }
01806 }
01807 }
01808
01816 uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id)
01817 {
01818 return (Company::Get(company_id)->settings.vehicle.servint_ispercent) ? Clamp(interval, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : Clamp(interval, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
01819 }
01820
01829 static bool CheckForValidOrders(const Vehicle *v)
01830 {
01831 const Order *order;
01832
01833 FOR_VEHICLE_ORDERS(v, order) {
01834 switch (order->GetType()) {
01835 case OT_GOTO_STATION:
01836 case OT_GOTO_DEPOT:
01837 case OT_GOTO_WAYPOINT:
01838 return true;
01839
01840 default:
01841 break;
01842 }
01843 }
01844
01845 return false;
01846 }
01847
01851 static bool OrderConditionCompare(OrderConditionComparator occ, int variable, int value)
01852 {
01853 switch (occ) {
01854 case OCC_EQUALS: return variable == value;
01855 case OCC_NOT_EQUALS: return variable != value;
01856 case OCC_LESS_THAN: return variable < value;
01857 case OCC_LESS_EQUALS: return variable <= value;
01858 case OCC_MORE_THAN: return variable > value;
01859 case OCC_MORE_EQUALS: return variable >= value;
01860 case OCC_IS_TRUE: return variable != 0;
01861 case OCC_IS_FALSE: return variable == 0;
01862 default: NOT_REACHED();
01863 }
01864 }
01865
01872 VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
01873 {
01874 if (order->GetType() != OT_CONDITIONAL) return INVALID_VEH_ORDER_ID;
01875
01876 bool skip_order = false;
01877 OrderConditionComparator occ = order->GetConditionComparator();
01878 uint16 value = order->GetConditionValue();
01879
01880 switch (order->GetConditionVariable()) {
01881 case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break;
01882 case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break;
01883 case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break;
01884 case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break;
01885 case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break;
01886 case OCV_UNCONDITIONALLY: skip_order = true; break;
01887 case OCV_REMAINING_LIFETIME: skip_order = OrderConditionCompare(occ, max(v->max_age - v->age + DAYS_IN_LEAP_YEAR - 1, 0) / DAYS_IN_LEAP_YEAR, value); break;
01888 default: NOT_REACHED();
01889 }
01890
01891 return skip_order ? order->GetConditionSkipToOrder() : (VehicleOrderID)INVALID_VEH_ORDER_ID;
01892 }
01893
01901 bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool pbs_look_ahead)
01902 {
01903 if (conditional_depth > v->GetNumOrders()) return false;
01904
01905 switch (order->GetType()) {
01906 case OT_GOTO_STATION:
01907 v->dest_tile = v->GetOrderStationLocation(order->GetDestination());
01908 return true;
01909
01910 case OT_GOTO_DEPOT:
01911 if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !v->NeedsServicing()) {
01912 assert(!pbs_look_ahead);
01913 UpdateVehicleTimetable(v, true);
01914 v->IncrementRealOrderIndex();
01915 break;
01916 }
01917
01918 if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
01919
01920 TileIndex location;
01921 DestinationID destination;
01922 bool reverse;
01923
01924 if (v->FindClosestDepot(&location, &destination, &reverse)) {
01925
01926 if (pbs_look_ahead && reverse) return false;
01927
01928 v->dest_tile = location;
01929 v->current_order.MakeGoToDepot(destination, v->current_order.GetDepotOrderType(), v->current_order.GetNonStopType(), (OrderDepotActionFlags)(v->current_order.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT), v->current_order.GetRefitCargo(), v->current_order.GetRefitSubtype());
01930
01931
01932 if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
01933
01934 if (v->type == VEH_AIRCRAFT) {
01935 Aircraft *a = Aircraft::From(v);
01936 if (a->state == FLYING && a->targetairport != destination) {
01937
01938 extern void AircraftNextAirportPos_and_Order(Aircraft *a);
01939 AircraftNextAirportPos_and_Order(a);
01940 }
01941 }
01942 return true;
01943 }
01944
01945
01946 if (pbs_look_ahead) return false;
01947
01948 UpdateVehicleTimetable(v, true);
01949 v->IncrementRealOrderIndex();
01950 } else {
01951 if (v->type != VEH_AIRCRAFT) {
01952 v->dest_tile = Depot::Get(order->GetDestination())->xy;
01953 }
01954 return true;
01955 }
01956 break;
01957
01958 case OT_GOTO_WAYPOINT:
01959 v->dest_tile = Waypoint::Get(order->GetDestination())->xy;
01960 return true;
01961
01962 case OT_CONDITIONAL: {
01963 assert(!pbs_look_ahead);
01964 VehicleOrderID next_order = ProcessConditionalOrder(order, v);
01965 if (next_order != INVALID_VEH_ORDER_ID) {
01966
01967
01968 UpdateVehicleTimetable(v, false);
01969 v->cur_implicit_order_index = v->cur_real_order_index = next_order;
01970 v->UpdateRealOrderIndex();
01971 v->current_order_time += v->GetOrder(v->cur_real_order_index)->travel_time;
01972
01973
01974
01975 if (v->IsGroundVehicle()) {
01976 uint16 &gv_flags = v->GetGroundVehicleFlags();
01977 SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
01978 }
01979 } else {
01980 UpdateVehicleTimetable(v, true);
01981 v->IncrementRealOrderIndex();
01982 }
01983 break;
01984 }
01985
01986 default:
01987 v->dest_tile = 0;
01988 return false;
01989 }
01990
01991 assert(v->cur_implicit_order_index < v->GetNumOrders());
01992 assert(v->cur_real_order_index < v->GetNumOrders());
01993
01994
01995 order = v->GetOrder(v->cur_real_order_index);
01996 if (order != NULL && order->IsType(OT_IMPLICIT)) {
01997 assert(v->GetNumManualOrders() == 0);
01998 order = NULL;
01999 }
02000
02001 if (order == NULL) {
02002 v->current_order.Free();
02003 v->dest_tile = 0;
02004 return false;
02005 }
02006
02007 v->current_order = *order;
02008 return UpdateOrderDest(v, order, conditional_depth + 1, pbs_look_ahead);
02009 }
02010
02018 bool ProcessOrders(Vehicle *v)
02019 {
02020 switch (v->current_order.GetType()) {
02021 case OT_GOTO_DEPOT:
02022
02023 if (!(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return false;
02024 break;
02025
02026 case OT_LOADING:
02027 return false;
02028
02029 case OT_LEAVESTATION:
02030 if (v->type != VEH_AIRCRAFT) return false;
02031 break;
02032
02033 default: break;
02034 }
02035
02043 bool may_reverse = v->current_order.IsType(OT_NOTHING);
02044
02045
02046 if (((v->current_order.IsType(OT_GOTO_STATION) && (v->current_order.GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) || v->current_order.IsType(OT_GOTO_WAYPOINT)) &&
02047 IsTileType(v->tile, MP_STATION) &&
02048 v->current_order.GetDestination() == GetStationIndex(v->tile)) {
02049 v->DeleteUnreachedImplicitOrders();
02050
02051
02052
02053
02054 v->last_station_visited = v->current_order.GetDestination();
02055 UpdateVehicleTimetable(v, true);
02056 v->IncrementImplicitOrderIndex();
02057 }
02058
02059
02060 assert(v->cur_implicit_order_index == 0 || v->cur_implicit_order_index < v->GetNumOrders());
02061 v->UpdateRealOrderIndex();
02062
02063 const Order *order = v->GetOrder(v->cur_real_order_index);
02064 if (order != NULL && order->IsType(OT_IMPLICIT)) {
02065 assert(v->GetNumManualOrders() == 0);
02066 order = NULL;
02067 }
02068
02069
02070 if (order == NULL || (v->type == VEH_AIRCRAFT && !CheckForValidOrders(v))) {
02071 if (v->type == VEH_AIRCRAFT) {
02072
02073 extern void HandleMissingAircraftOrders(Aircraft *v);
02074 HandleMissingAircraftOrders(Aircraft::From(v));
02075 return false;
02076 }
02077
02078 v->current_order.Free();
02079 v->dest_tile = 0;
02080 return false;
02081 }
02082
02083
02084 if (order->Equals(v->current_order) && (v->type == VEH_AIRCRAFT || v->dest_tile != 0) &&
02085 (v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || Station::Get(order->GetDestination())->dock_tile != INVALID_TILE)) {
02086 return false;
02087 }
02088
02089
02090 v->current_order = *order;
02091
02092 InvalidateVehicleOrder(v, -2);
02093 switch (v->type) {
02094 default:
02095 NOT_REACHED();
02096
02097 case VEH_ROAD:
02098 case VEH_TRAIN:
02099 break;
02100
02101 case VEH_AIRCRAFT:
02102 case VEH_SHIP:
02103 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
02104 break;
02105 }
02106
02107 return UpdateOrderDest(v, order) && may_reverse;
02108 }
02109
02117 bool Order::ShouldStopAtStation(const Vehicle *v, StationID station) const
02118 {
02119 bool is_dest_station = this->IsType(OT_GOTO_STATION) && this->dest == station;
02120
02121 return (!this->IsType(OT_GOTO_DEPOT) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0) &&
02122 v->last_station_visited != station &&
02123
02124 !(this->GetNonStopType() & (is_dest_station ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));
02125 }