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 "vehicle_gui.h"
00019 #include "strings_func.h"
00020 #include "window_func.h"
00021 #include "timetable.h"
00022 #include "vehicle_func.h"
00023 #include "depot_base.h"
00024 #include "core/pool_func.hpp"
00025 #include "aircraft.h"
00026 #include "roadveh.h"
00027 #include "station_base.h"
00028 #include "waypoint_base.h"
00029 #include "company_base.h"
00030 #include "cargodest_func.h"
00031
00032 #include "table/strings.h"
00033
00034
00035
00036
00037 assert_compile(sizeof(DestinationID) >= sizeof(DepotID));
00038 assert_compile(sizeof(DestinationID) >= sizeof(StationID));
00039
00040 OrderPool _order_pool("Order");
00041 INSTANTIATE_POOL_METHODS(Order)
00042 OrderListPool _orderlist_pool("OrderList");
00043 INSTANTIATE_POOL_METHODS(OrderList)
00044
00049 void Order::Free()
00050 {
00051 this->type = OT_NOTHING;
00052 this->flags = 0;
00053 this->dest = 0;
00054 this->next = NULL;
00055 }
00056
00061 void Order::MakeGoToStation(StationID destination)
00062 {
00063 this->type = OT_GOTO_STATION;
00064 this->flags = 0;
00065 this->dest = destination;
00066 }
00067
00077 void Order::MakeGoToDepot(DepotID destination, OrderDepotTypeFlags order, OrderNonStopFlags non_stop_type, OrderDepotActionFlags action, CargoID cargo, byte subtype)
00078 {
00079 this->type = OT_GOTO_DEPOT;
00080 this->SetDepotOrderType(order);
00081 this->SetDepotActionType(action);
00082 this->SetNonStopType(non_stop_type);
00083 this->dest = destination;
00084 this->SetRefit(cargo, subtype);
00085 }
00086
00091 void Order::MakeGoToWaypoint(StationID destination)
00092 {
00093 this->type = OT_GOTO_WAYPOINT;
00094 this->flags = 0;
00095 this->dest = destination;
00096 }
00097
00102 void Order::MakeLoading(bool ordered)
00103 {
00104 this->type = OT_LOADING;
00105 if (!ordered) this->flags = 0;
00106 }
00107
00111 void Order::MakeLeaveStation()
00112 {
00113 this->type = OT_LEAVESTATION;
00114 this->flags = 0;
00115 }
00116
00120 void Order::MakeDummy()
00121 {
00122 this->type = OT_DUMMY;
00123 this->flags = 0;
00124 }
00125
00130 void Order::MakeConditional(VehicleOrderID order)
00131 {
00132 this->type = OT_CONDITIONAL;
00133 this->flags = order;
00134 this->dest = 0;
00135 }
00136
00141 void Order::MakeImplicit(StationID destination)
00142 {
00143 this->type = OT_IMPLICIT;
00144 this->dest = destination;
00145 }
00146
00153 void Order::SetRefit(CargoID cargo, byte subtype)
00154 {
00155 this->refit_cargo = cargo;
00156 this->refit_subtype = subtype;
00157 }
00158
00164 bool Order::Equals(const Order &other) const
00165 {
00166
00167
00168
00169
00170
00171 if ((this->IsType(OT_GOTO_DEPOT) && this->type == other.type) &&
00172 ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0 ||
00173 (other.GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0)) {
00174 return this->GetDepotOrderType() == other.GetDepotOrderType() &&
00175 (this->GetDepotActionType() & ~ODATFB_NEAREST_DEPOT) == (other.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT);
00176 }
00177
00178 return this->type == other.type && this->flags == other.flags && this->dest == other.dest;
00179 }
00180
00187 uint32 Order::Pack() const
00188 {
00189 return this->dest << 16 | this->flags << 8 | this->type;
00190 }
00191
00197 uint16 Order::MapOldOrder() const
00198 {
00199 uint16 order = this->GetType();
00200 switch (this->type) {
00201 case OT_GOTO_STATION:
00202 if (this->GetUnloadType() & OUFB_UNLOAD) SetBit(order, 5);
00203 if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
00204 if (this->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) SetBit(order, 7);
00205 order |= GB(this->GetDestination(), 0, 8) << 8;
00206 break;
00207 case OT_GOTO_DEPOT:
00208 if (!(this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) SetBit(order, 6);
00209 SetBit(order, 7);
00210 order |= GB(this->GetDestination(), 0, 8) << 8;
00211 break;
00212 case OT_LOADING:
00213 if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
00214 break;
00215 }
00216 return order;
00217 }
00218
00223 Order::Order(uint32 packed)
00224 {
00225 this->type = (OrderType)GB(packed, 0, 8);
00226 this->flags = GB(packed, 8, 8);
00227 this->dest = GB(packed, 16, 16);
00228 this->next = NULL;
00229 this->refit_cargo = CT_NO_REFIT;
00230 this->refit_subtype = 0;
00231 this->wait_time = 0;
00232 this->travel_time = 0;
00233 }
00234
00239 void Order::PostDestructor(size_t index)
00240 {
00241 Vehicle *v;
00242 FOR_ALL_VEHICLES(v) {
00243 if (v->current_order.index == index) v->current_order.index = INVALID_ORDER;
00244 if (v->last_order_id == index) v->last_order_id = INVALID_ORDER;
00245 }
00246
00247 InvalidateOrderRouteLinks((OrderID)index);
00248 }
00249
00255 void InvalidateVehicleOrder(const Vehicle *v, int data)
00256 {
00257 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
00258
00259 if (data != 0) {
00260
00261 InvalidateWindowData(WC_VEHICLE_ORDERS, v->index, data);
00262 InvalidateWindowData(WC_VEHICLE_TIMETABLE, v->index, data);
00263 return;
00264 }
00265
00266 SetWindowDirty(WC_VEHICLE_ORDERS, v->index);
00267 SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
00268 }
00269
00277 void Order::AssignOrder(const Order &other)
00278 {
00279 this->type = other.type;
00280 this->flags = other.flags;
00281 this->dest = other.dest;
00282
00283 this->refit_cargo = other.refit_cargo;
00284 this->refit_subtype = other.refit_subtype;
00285
00286 this->wait_time = other.wait_time;
00287 this->travel_time = other.travel_time;
00288 }
00289
00295 void OrderList::Initialize(Order *chain, Vehicle *v)
00296 {
00297 this->first = chain;
00298 this->first_shared = v;
00299
00300 this->num_orders = 0;
00301 this->num_manual_orders = 0;
00302 this->num_vehicles = 1;
00303 this->timetable_duration = 0;
00304
00305 for (Order *o = this->first; o != NULL; o = o->next) {
00306 ++this->num_orders;
00307 if (!o->IsType(OT_IMPLICIT)) ++this->num_manual_orders;
00308 this->timetable_duration += o->wait_time + o->travel_time;
00309 }
00310
00311 for (Vehicle *u = this->first_shared->PreviousShared(); u != NULL; u = u->PreviousShared()) {
00312 ++this->num_vehicles;
00313 this->first_shared = u;
00314 }
00315
00316 for (const Vehicle *u = v->NextShared(); u != NULL; u = u->NextShared()) ++this->num_vehicles;
00317 }
00318
00324 void OrderList::FreeChain(bool keep_orderlist)
00325 {
00326 Order *next;
00327 for (Order *o = this->first; o != NULL; o = next) {
00328 next = o->next;
00329 delete o;
00330 }
00331
00332 if (keep_orderlist) {
00333 this->first = NULL;
00334 this->num_orders = 0;
00335 this->num_manual_orders = 0;
00336 this->timetable_duration = 0;
00337 } else {
00338 delete this;
00339 }
00340 }
00341
00347 Order *OrderList::GetOrderAt(int index) const
00348 {
00349 if (index < 0) return NULL;
00350
00351 Order *order = this->first;
00352
00353 while (order != NULL && index-- > 0) {
00354 order = order->next;
00355 }
00356 return order;
00357 }
00358
00364 void OrderList::InsertOrderAt(Order *new_order, int index)
00365 {
00366 if (this->first == NULL) {
00367 this->first = new_order;
00368 } else {
00369 if (index == 0) {
00370
00371 new_order->next = this->first;
00372 this->first = new_order;
00373 } else if (index >= this->num_orders) {
00374
00375 this->GetLastOrder()->next = new_order;
00376 } else {
00377
00378 Order *order = this->GetOrderAt(index - 1);
00379 new_order->next = order->next;
00380 order->next = new_order;
00381 }
00382 }
00383 ++this->num_orders;
00384 if (!new_order->IsType(OT_IMPLICIT)) ++this->num_manual_orders;
00385 this->timetable_duration += new_order->wait_time + new_order->travel_time;
00386 }
00387
00388
00393 void OrderList::DeleteOrderAt(int index)
00394 {
00395 if (index >= this->num_orders) return;
00396
00397 Order *to_remove;
00398
00399 if (index == 0) {
00400 to_remove = this->first;
00401 this->first = to_remove->next;
00402 } else {
00403 Order *prev = GetOrderAt(index - 1);
00404 to_remove = prev->next;
00405 prev->next = to_remove->next;
00406 }
00407 --this->num_orders;
00408 if (!to_remove->IsType(OT_IMPLICIT)) --this->num_manual_orders;
00409 this->timetable_duration -= (to_remove->wait_time + to_remove->travel_time);
00410 delete to_remove;
00411 }
00412
00418 void OrderList::MoveOrder(int from, int to)
00419 {
00420 if (from >= this->num_orders || to >= this->num_orders || from == to) return;
00421
00422 Order *moving_one;
00423
00424
00425 if (from == 0) {
00426 moving_one = this->first;
00427 this->first = moving_one->next;
00428 } else {
00429 Order *one_before = GetOrderAt(from - 1);
00430 moving_one = one_before->next;
00431 one_before->next = moving_one->next;
00432 }
00433
00434
00435 if (to == 0) {
00436 moving_one->next = this->first;
00437 this->first = moving_one;
00438 } else {
00439 Order *one_before = GetOrderAt(to - 1);
00440 moving_one->next = one_before->next;
00441 one_before->next = moving_one;
00442 }
00443 }
00444
00450 void OrderList::RemoveVehicle(Vehicle *v)
00451 {
00452 --this->num_vehicles;
00453 if (v == this->first_shared) this->first_shared = v->NextShared();
00454 }
00455
00460 bool OrderList::IsVehicleInSharedOrdersList(const Vehicle *v) const
00461 {
00462 for (const Vehicle *v_shared = this->first_shared; v_shared != NULL; v_shared = v_shared->NextShared()) {
00463 if (v_shared == v) return true;
00464 }
00465
00466 return false;
00467 }
00468
00474 int OrderList::GetPositionInSharedOrderList(const Vehicle *v) const
00475 {
00476 int count = 0;
00477 for (const Vehicle *v_shared = v->PreviousShared(); v_shared != NULL; v_shared = v_shared->PreviousShared()) count++;
00478 return count;
00479 }
00480
00485 bool OrderList::IsCompleteTimetable() const
00486 {
00487 for (Order *o = this->first; o != NULL; o = o->next) {
00488
00489 if (o->IsType(OT_IMPLICIT)) continue;
00490 if (!o->IsCompletelyTimetabled()) return false;
00491 }
00492 return true;
00493 }
00494
00498 void OrderList::DebugCheckSanity() const
00499 {
00500 VehicleOrderID check_num_orders = 0;
00501 VehicleOrderID check_num_manual_orders = 0;
00502 uint check_num_vehicles = 0;
00503 Ticks check_timetable_duration = 0;
00504
00505 DEBUG(misc, 6, "Checking OrderList %hu for sanity...", this->index);
00506
00507 for (const Order *o = this->first; o != NULL; o = o->next) {
00508 ++check_num_orders;
00509 if (!o->IsType(OT_IMPLICIT)) ++check_num_manual_orders;
00510 check_timetable_duration += o->wait_time + o->travel_time;
00511 }
00512 assert(this->num_orders == check_num_orders);
00513 assert(this->num_manual_orders == check_num_manual_orders);
00514 assert(this->timetable_duration == check_timetable_duration);
00515
00516 for (const Vehicle *v = this->first_shared; v != NULL; v = v->NextShared()) {
00517 ++check_num_vehicles;
00518 assert(v->orders.list == this);
00519 }
00520 assert(this->num_vehicles == check_num_vehicles);
00521 DEBUG(misc, 6, "... detected %u orders (%u manual), %u vehicles, %i ticks",
00522 (uint)this->num_orders, (uint)this->num_manual_orders,
00523 this->num_vehicles, this->timetable_duration);
00524 }
00525
00533 static inline bool OrderGoesToStation(const Vehicle *v, const Order *o)
00534 {
00535 return o->IsType(OT_GOTO_STATION) ||
00536 (v->type == VEH_AIRCRAFT && o->IsType(OT_GOTO_DEPOT) && !(o->GetDepotActionType() & ODATFB_NEAREST_DEPOT));
00537 }
00538
00545 static void DeleteOrderWarnings(const Vehicle *v)
00546 {
00547 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS);
00548 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_VOID_ORDER);
00549 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY);
00550 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY);
00551 }
00552
00558 TileIndex Order::GetLocation(const Vehicle *v) const
00559 {
00560 switch (this->GetType()) {
00561 case OT_GOTO_WAYPOINT:
00562 case OT_GOTO_STATION:
00563 case OT_IMPLICIT:
00564 return BaseStation::Get(this->GetDestination())->xy;
00565
00566 case OT_GOTO_DEPOT:
00567 if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) return INVALID_TILE;
00568 return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination())->xy : Depot::Get(this->GetDestination())->xy;
00569
00570 default:
00571 return INVALID_TILE;
00572 }
00573 }
00574
00575 static uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle *v, int conditional_depth = 0)
00576 {
00577 assert(v->type == VEH_SHIP);
00578
00579 if (cur->IsType(OT_CONDITIONAL)) {
00580 if (conditional_depth > v->GetNumOrders()) return 0;
00581
00582 conditional_depth++;
00583
00584 int dist1 = GetOrderDistance(prev, v->GetOrder(cur->GetConditionSkipToOrder()), v, conditional_depth);
00585 int dist2 = GetOrderDistance(prev, cur->next == NULL ? v->orders.list->GetFirstOrder() : cur->next, v, conditional_depth);
00586 return max(dist1, dist2);
00587 }
00588
00589 TileIndex prev_tile = prev->GetLocation(v);
00590 TileIndex cur_tile = cur->GetLocation(v);
00591 if (prev_tile == INVALID_TILE || cur_tile == INVALID_TILE) return 0;
00592 return DistanceManhattan(prev_tile, cur_tile);
00593 }
00594
00608 CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00609 {
00610 VehicleID veh = GB(p1, 0, 20);
00611 VehicleOrderID sel_ord = GB(p1, 20, 8);
00612 Order new_order(p2);
00613
00614 Vehicle *v = Vehicle::GetIfValid(veh);
00615 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00616
00617 CommandCost ret = CheckOwnership(v->owner);
00618 if (ret.Failed()) return ret;
00619
00620
00621
00622 switch (new_order.GetType()) {
00623 case OT_GOTO_STATION: {
00624 const Station *st = Station::GetIfValid(new_order.GetDestination());
00625 if (st == NULL) return CMD_ERROR;
00626
00627 if (st->owner != OWNER_NONE) {
00628 CommandCost ret = CheckOwnership(st->owner);
00629 if (ret.Failed()) return ret;
00630 }
00631
00632 if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00633 for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
00634 if (!CanVehicleUseStation(u, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED);
00635 }
00636
00637
00638 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
00639
00640
00641 switch (new_order.GetLoadType()) {
00642 case OLF_LOAD_IF_POSSIBLE: case OLFB_FULL_LOAD: case OLF_FULL_LOAD_ANY: case OLFB_NO_LOAD: break;
00643 default: return CMD_ERROR;
00644 }
00645 switch (new_order.GetUnloadType()) {
00646 case OUF_UNLOAD_IF_POSSIBLE: case OUFB_UNLOAD: case OUFB_TRANSFER: case OUFB_NO_UNLOAD: break;
00647 default: return CMD_ERROR;
00648 }
00649
00650
00651 switch (new_order.GetStopLocation()) {
00652 case OSL_PLATFORM_NEAR_END:
00653 case OSL_PLATFORM_MIDDLE:
00654 if (v->type != VEH_TRAIN) return CMD_ERROR;
00655
00656 case OSL_PLATFORM_FAR_END:
00657 break;
00658
00659 default:
00660 return CMD_ERROR;
00661 }
00662
00663 break;
00664 }
00665
00666 case OT_GOTO_DEPOT: {
00667 if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
00668 if (v->type == VEH_AIRCRAFT) {
00669 const Station *st = Station::GetIfValid(new_order.GetDestination());
00670
00671 if (st == NULL) return CMD_ERROR;
00672
00673 CommandCost ret = CheckOwnership(st->owner);
00674 if (ret.Failed()) return ret;
00675
00676 if (!CanVehicleUseStation(v, st) || !st->airport.HasHangar()) {
00677 return CMD_ERROR;
00678 }
00679 } else {
00680 const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
00681
00682 if (dp == NULL) return CMD_ERROR;
00683
00684 CommandCost ret = CheckOwnership(GetTileOwner(dp->xy));
00685 if (ret.Failed()) return ret;
00686
00687 switch (v->type) {
00688 case VEH_TRAIN:
00689 if (!IsRailDepotTile(dp->xy)) return CMD_ERROR;
00690 break;
00691
00692 case VEH_ROAD:
00693 if (!IsRoadDepotTile(dp->xy)) return CMD_ERROR;
00694 break;
00695
00696 case VEH_SHIP:
00697 if (!IsShipDepotTile(dp->xy)) return CMD_ERROR;
00698 break;
00699
00700 default: return CMD_ERROR;
00701 }
00702 }
00703 }
00704
00705 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
00706 if (new_order.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS | ((new_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0 ? ODTFB_SERVICE : 0))) return CMD_ERROR;
00707 if (new_order.GetDepotActionType() & ~(ODATFB_HALT | ODATFB_NEAREST_DEPOT)) return CMD_ERROR;
00708 if ((new_order.GetDepotOrderType() & ODTFB_SERVICE) && (new_order.GetDepotActionType() & ODATFB_HALT)) return CMD_ERROR;
00709 break;
00710 }
00711
00712 case OT_GOTO_WAYPOINT: {
00713 const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
00714 if (wp == NULL) return CMD_ERROR;
00715
00716 switch (v->type) {
00717 default: return CMD_ERROR;
00718
00719 case VEH_TRAIN: {
00720 if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00721
00722 CommandCost ret = CheckOwnership(wp->owner);
00723 if (ret.Failed()) return ret;
00724 break;
00725 }
00726
00727 case VEH_SHIP:
00728 if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00729 if (wp->owner != OWNER_NONE) {
00730 CommandCost ret = CheckOwnership(wp->owner);
00731 if (ret.Failed()) return ret;
00732 }
00733 break;
00734 }
00735
00736
00737
00738
00739 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
00740 break;
00741 }
00742
00743 case OT_CONDITIONAL: {
00744 VehicleOrderID skip_to = new_order.GetConditionSkipToOrder();
00745 if (skip_to != 0 && skip_to >= v->GetNumOrders()) return CMD_ERROR;
00746 if (new_order.GetConditionVariable() > OCV_END) return CMD_ERROR;
00747
00748 OrderConditionComparator occ = new_order.GetConditionComparator();
00749 if (occ > OCC_END) return CMD_ERROR;
00750 switch (new_order.GetConditionVariable()) {
00751 case OCV_REQUIRES_SERVICE:
00752 if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
00753 break;
00754
00755 case OCV_UNCONDITIONALLY:
00756 if (occ != OCC_EQUALS) return CMD_ERROR;
00757 if (new_order.GetConditionValue() != 0) return CMD_ERROR;
00758 break;
00759
00760 case OCV_LOAD_PERCENTAGE:
00761 case OCV_RELIABILITY:
00762 if (new_order.GetConditionValue() > 100) return CMD_ERROR;
00763
00764 default:
00765 if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) return CMD_ERROR;
00766 break;
00767 }
00768 break;
00769 }
00770
00771 default: return CMD_ERROR;
00772 }
00773
00774 if (sel_ord > v->GetNumOrders()) return CMD_ERROR;
00775
00776 if (v->GetNumOrders() >= MAX_VEH_ORDER_ID) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS);
00777 if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
00778 if (v->orders.list == NULL && !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
00779
00780 if (v->type == VEH_SHIP && _settings_game.pf.pathfinder_for_ships != VPF_NPF) {
00781
00782 const Order *prev = NULL;
00783 uint n = 0;
00784
00785
00786
00787
00788 const Order *o;
00789 FOR_VEHICLE_ORDERS(v, o) {
00790 switch (o->GetType()) {
00791 case OT_GOTO_STATION:
00792 case OT_GOTO_DEPOT:
00793 case OT_GOTO_WAYPOINT:
00794 prev = o;
00795 break;
00796
00797 default: break;
00798 }
00799 if (++n == sel_ord && prev != NULL) break;
00800 }
00801 if (prev != NULL) {
00802 uint dist = GetOrderDistance(prev, &new_order, v);
00803 if (dist >= 130) {
00804 return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION);
00805 }
00806 }
00807 }
00808
00809 if (flags & DC_EXEC) {
00810 Order *new_o = new Order();
00811 new_o->AssignOrder(new_order);
00812 InsertOrder(v, new_o, sel_ord);
00813 }
00814
00815 return CommandCost();
00816 }
00817
00824 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord)
00825 {
00826
00827 if (v->orders.list == NULL) {
00828 v->orders.list = new OrderList(new_o, v);
00829 } else {
00830 v->orders.list->InsertOrderAt(new_o, sel_ord);
00831 }
00832
00833 Vehicle *u = v->FirstShared();
00834 DeleteOrderWarnings(u);
00835 for (; u != NULL; u = u->NextShared()) {
00836 assert(v->orders.list == u->orders.list);
00837
00838
00839
00840
00841
00842 if (sel_ord <= u->cur_real_order_index) {
00843 uint cur = u->cur_real_order_index + 1;
00844
00845 if (cur < u->GetNumOrders()) {
00846 u->cur_real_order_index = cur;
00847 }
00848 }
00849 if (sel_ord == u->cur_implicit_order_index && u->IsGroundVehicle()) {
00850
00851
00852
00853 uint16 &gv_flags = u->GetGroundVehicleFlags();
00854 SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
00855 }
00856 if (sel_ord <= u->cur_implicit_order_index) {
00857 uint cur = u->cur_implicit_order_index + 1;
00858
00859 if (cur < u->GetNumOrders()) {
00860 u->cur_implicit_order_index = cur;
00861 }
00862 }
00863
00864 InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
00865 }
00866
00867
00868 VehicleOrderID cur_order_id = 0;
00869 Order *order;
00870 FOR_VEHICLE_ORDERS(v, order) {
00871 if (order->IsType(OT_CONDITIONAL)) {
00872 VehicleOrderID order_id = order->GetConditionSkipToOrder();
00873 if (order_id >= sel_ord) {
00874 order->SetConditionSkipToOrder(order_id + 1);
00875 }
00876 if (order_id == cur_order_id) {
00877 order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
00878 }
00879 }
00880 cur_order_id++;
00881 }
00882
00883 PrefillRouteLinks(v);
00884
00885
00886 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00887 }
00888
00894 static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags)
00895 {
00896 if (flags & DC_EXEC) {
00897 DeleteVehicleOrders(dst);
00898 InvalidateVehicleOrder(dst, -1);
00899 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
00900 }
00901 return CommandCost();
00902 }
00903
00913 CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00914 {
00915 VehicleID veh_id = GB(p1, 0, 20);
00916 VehicleOrderID sel_ord = GB(p2, 0, 8);
00917
00918 Vehicle *v = Vehicle::GetIfValid(veh_id);
00919
00920 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00921
00922 CommandCost ret = CheckOwnership(v->owner);
00923 if (ret.Failed()) return ret;
00924
00925
00926 if (sel_ord >= v->GetNumOrders()) return DecloneOrder(v, flags);
00927
00928 if (v->GetOrder(sel_ord) == NULL) return CMD_ERROR;
00929
00930 if (flags & DC_EXEC) DeleteOrder(v, sel_ord);
00931 return CommandCost();
00932 }
00933
00938 static void CancelLoadingDueToDeletedOrder(Vehicle *v)
00939 {
00940 assert(v->current_order.IsType(OT_LOADING));
00941
00942
00943 v->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
00944
00945
00946 if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) v->current_order.SetLoadType(OLF_LOAD_IF_POSSIBLE);
00947 }
00948
00953 static void InvalidateNextStation(Vehicle *v)
00954 {
00955 for (; v != NULL; v = v->Next()) {
00956 v->cargo.InvalidateNextStation();
00957 }
00958 }
00959
00965 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord)
00966 {
00967 v->orders.list->DeleteOrderAt(sel_ord);
00968
00969 Vehicle *u = v->FirstShared();
00970 DeleteOrderWarnings(u);
00971 PrefillRouteLinks(u);
00972 for (; u != NULL; u = u->NextShared()) {
00973 assert(v->orders.list == u->orders.list);
00974
00975 if (sel_ord == u->cur_real_order_index && u->current_order.IsType(OT_LOADING)) {
00976 CancelLoadingDueToDeletedOrder(u);
00977 }
00978
00979 if (sel_ord < u->cur_real_order_index) {
00980 u->cur_real_order_index--;
00981 } else if (sel_ord == u->cur_real_order_index) {
00982 u->UpdateRealOrderIndex();
00983 }
00984
00985 if (sel_ord < u->cur_implicit_order_index) {
00986 u->cur_implicit_order_index--;
00987 } else if (sel_ord == u->cur_implicit_order_index) {
00988
00989 if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
00990
00991
00992 while (u->cur_implicit_order_index != u->cur_real_order_index && !u->GetOrder(u->cur_implicit_order_index)->IsType(OT_IMPLICIT)) {
00993 u->cur_implicit_order_index++;
00994 if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
00995 }
00996 }
00997
00998
00999 InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
01000
01001
01002 InvalidateNextStation(u);
01003 }
01004
01005
01006 VehicleOrderID cur_order_id = 0;
01007 Order *order = NULL;
01008 FOR_VEHICLE_ORDERS(v, order) {
01009 if (order->IsType(OT_CONDITIONAL)) {
01010 VehicleOrderID order_id = order->GetConditionSkipToOrder();
01011 if (order_id >= sel_ord) {
01012 order_id = max(order_id - 1, 0);
01013 }
01014 if (order_id == cur_order_id) {
01015 order_id = (order_id + 1) % v->GetNumOrders();
01016 }
01017 order->SetConditionSkipToOrder(order_id);
01018 }
01019 cur_order_id++;
01020 }
01021
01022 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
01023 }
01024
01034 CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01035 {
01036 VehicleID veh_id = GB(p1, 0, 20);
01037 VehicleOrderID sel_ord = GB(p2, 0, 8);
01038
01039 Vehicle *v = Vehicle::GetIfValid(veh_id);
01040
01041 if (v == NULL || !v->IsPrimaryVehicle() || sel_ord == v->cur_implicit_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR;
01042
01043 CommandCost ret = CheckOwnership(v->owner);
01044 if (ret.Failed()) return ret;
01045
01046 if (flags & DC_EXEC) {
01047 v->cur_implicit_order_index = v->cur_real_order_index = sel_ord;
01048 v->UpdateRealOrderIndex();
01049
01050 if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
01051
01052 InvalidateVehicleOrder(v, -2);
01053 }
01054
01055
01056 if (v->type == VEH_AIRCRAFT) SetWindowClassesDirty(WC_AIRCRAFT_LIST);
01057 if (v->type == VEH_SHIP) SetWindowClassesDirty(WC_SHIPS_LIST);
01058
01059 return CommandCost();
01060 }
01061
01075 CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01076 {
01077 VehicleID veh = GB(p1, 0, 20);
01078 VehicleOrderID moving_order = GB(p2, 0, 16);
01079 VehicleOrderID target_order = GB(p2, 16, 16);
01080
01081 Vehicle *v = Vehicle::GetIfValid(veh);
01082 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01083
01084 CommandCost ret = CheckOwnership(v->owner);
01085 if (ret.Failed()) return ret;
01086
01087
01088 if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() ||
01089 moving_order == target_order || v->GetNumOrders() <= 1) return CMD_ERROR;
01090
01091 Order *moving_one = v->GetOrder(moving_order);
01092
01093 if (moving_one == NULL) return CMD_ERROR;
01094
01095 if (flags & DC_EXEC) {
01096 v->orders.list->MoveOrder(moving_order, target_order);
01097
01098
01099 Vehicle *u = v->FirstShared();
01100
01101 DeleteOrderWarnings(u);
01102
01103 for (; u != NULL; u = u->NextShared()) {
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120 if (u->cur_real_order_index == moving_order) {
01121 u->cur_real_order_index = target_order;
01122 } else if (u->cur_real_order_index > moving_order && u->cur_real_order_index <= target_order) {
01123 u->cur_real_order_index--;
01124 } else if (u->cur_real_order_index < moving_order && u->cur_real_order_index >= target_order) {
01125 u->cur_real_order_index++;
01126 }
01127
01128 if (u->cur_implicit_order_index == moving_order) {
01129 u->cur_implicit_order_index = target_order;
01130 } else if (u->cur_implicit_order_index > moving_order && u->cur_implicit_order_index <= target_order) {
01131 u->cur_implicit_order_index--;
01132 } else if (u->cur_implicit_order_index < moving_order && u->cur_implicit_order_index >= target_order) {
01133 u->cur_implicit_order_index++;
01134 }
01135
01136 assert(v->orders.list == u->orders.list);
01137
01138 InvalidateVehicleOrder(u, moving_order | (target_order << 8));
01139 }
01140
01141
01142 Order *order;
01143 FOR_VEHICLE_ORDERS(v, order) {
01144 if (order->IsType(OT_CONDITIONAL)) {
01145 VehicleOrderID order_id = order->GetConditionSkipToOrder();
01146 if (order_id == moving_order) {
01147 order_id = target_order;
01148 } else if (order_id > moving_order && order_id <= target_order) {
01149 order_id--;
01150 } else if (order_id < moving_order && order_id >= target_order) {
01151 order_id++;
01152 }
01153 order->SetConditionSkipToOrder(order_id);
01154 }
01155 }
01156
01157
01158 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
01159 }
01160
01161 return CommandCost();
01162 }
01163
01179 CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01180 {
01181 VehicleOrderID sel_ord = GB(p1, 20, 8);
01182 VehicleID veh = GB(p1, 0, 20);
01183 ModifyOrderFlags mof = Extract<ModifyOrderFlags, 0, 4>(p2);
01184 uint16 data = GB(p2, 4, 11);
01185
01186 if (mof >= MOF_END) return CMD_ERROR;
01187
01188 Vehicle *v = Vehicle::GetIfValid(veh);
01189 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01190
01191 CommandCost ret = CheckOwnership(v->owner);
01192 if (ret.Failed()) return ret;
01193
01194
01195 if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
01196
01197 Order *order = v->GetOrder(sel_ord);
01198 switch (order->GetType()) {
01199 case OT_GOTO_STATION:
01200 if (mof == MOF_COND_VARIABLE || mof == MOF_COND_COMPARATOR || mof == MOF_DEPOT_ACTION || mof == MOF_COND_VALUE) return CMD_ERROR;
01201 break;
01202
01203 case OT_GOTO_DEPOT:
01204 if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR;
01205 break;
01206
01207 case OT_GOTO_WAYPOINT:
01208 if (mof != MOF_NON_STOP) return CMD_ERROR;
01209 break;
01210
01211 case OT_CONDITIONAL:
01212 if (mof != MOF_COND_VARIABLE && mof != MOF_COND_COMPARATOR && mof != MOF_COND_VALUE && mof != MOF_COND_DESTINATION) return CMD_ERROR;
01213 break;
01214
01215 default:
01216 return CMD_ERROR;
01217 }
01218
01219 switch (mof) {
01220 default: NOT_REACHED();
01221
01222 case MOF_NON_STOP:
01223 if (!v->IsGroundVehicle()) return CMD_ERROR;
01224 if (data >= ONSF_END) return CMD_ERROR;
01225 if (data == order->GetNonStopType()) return CMD_ERROR;
01226 break;
01227
01228 case MOF_STOP_LOCATION:
01229 if (v->type != VEH_TRAIN) return CMD_ERROR;
01230 if (data >= OSL_END) return CMD_ERROR;
01231 break;
01232
01233 case MOF_UNLOAD:
01234 if ((data & ~(OUFB_UNLOAD | OUFB_TRANSFER | OUFB_NO_UNLOAD)) != 0) return CMD_ERROR;
01235
01236 if (data != 0 && ((data & (OUFB_UNLOAD | OUFB_TRANSFER)) != 0) == ((data & OUFB_NO_UNLOAD) != 0)) return CMD_ERROR;
01237 if (data == order->GetUnloadType()) return CMD_ERROR;
01238 break;
01239
01240 case MOF_LOAD:
01241 if (data > OLFB_NO_LOAD || data == 1) return CMD_ERROR;
01242 if (data == order->GetLoadType()) return CMD_ERROR;
01243 break;
01244
01245 case MOF_DEPOT_ACTION:
01246 if (data >= DA_END) return CMD_ERROR;
01247 break;
01248
01249 case MOF_COND_VARIABLE:
01250 if (data >= OCV_END) return CMD_ERROR;
01251 break;
01252
01253 case MOF_COND_COMPARATOR:
01254 if (data >= OCC_END) return CMD_ERROR;
01255 switch (order->GetConditionVariable()) {
01256 case OCV_UNCONDITIONALLY: return CMD_ERROR;
01257
01258 case OCV_REQUIRES_SERVICE:
01259 if (data != OCC_IS_TRUE && data != OCC_IS_FALSE) return CMD_ERROR;
01260 break;
01261
01262 default:
01263 if (data == OCC_IS_TRUE || data == OCC_IS_FALSE) return CMD_ERROR;
01264 break;
01265 }
01266 break;
01267
01268 case MOF_COND_VALUE:
01269 switch (order->GetConditionVariable()) {
01270 case OCV_UNCONDITIONALLY: return CMD_ERROR;
01271
01272 case OCV_LOAD_PERCENTAGE:
01273 case OCV_RELIABILITY:
01274 if (data > 100) return CMD_ERROR;
01275 break;
01276
01277 default:
01278 if (data > 2047) return CMD_ERROR;
01279 break;
01280 }
01281 break;
01282
01283 case MOF_COND_DESTINATION:
01284 if (data >= v->GetNumOrders()) return CMD_ERROR;
01285 break;
01286 }
01287
01288 if (flags & DC_EXEC) {
01289 switch (mof) {
01290 case MOF_NON_STOP:
01291 order->SetNonStopType((OrderNonStopFlags)data);
01292 if (data & ONSF_NO_STOP_AT_DESTINATION_STATION) InvalidateOrderRouteLinks(order->index);
01293 PrefillRouteLinks(v);
01294 break;
01295
01296 case MOF_STOP_LOCATION:
01297 order->SetStopLocation((OrderStopLocation)data);
01298 break;
01299
01300 case MOF_UNLOAD:
01301 order->SetUnloadType((OrderUnloadFlags)data);
01302 break;
01303
01304 case MOF_LOAD:
01305 order->SetLoadType((OrderLoadFlags)data);
01306 break;
01307
01308 case MOF_DEPOT_ACTION: {
01309 switch (data) {
01310 case DA_ALWAYS_GO:
01311 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01312 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01313 break;
01314
01315 case DA_SERVICE:
01316 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() | ODTFB_SERVICE));
01317 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01318 break;
01319
01320 case DA_STOP:
01321 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01322 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() | ODATFB_HALT));
01323 break;
01324
01325 default:
01326 NOT_REACHED();
01327 }
01328 break;
01329 }
01330
01331 case MOF_COND_VARIABLE: {
01332 order->SetConditionVariable((OrderConditionVariable)data);
01333
01334 OrderConditionComparator occ = order->GetConditionComparator();
01335 switch (order->GetConditionVariable()) {
01336 case OCV_UNCONDITIONALLY:
01337 order->SetConditionComparator(OCC_EQUALS);
01338 order->SetConditionValue(0);
01339 break;
01340
01341 case OCV_REQUIRES_SERVICE:
01342 if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
01343 break;
01344
01345 case OCV_LOAD_PERCENTAGE:
01346 case OCV_RELIABILITY:
01347 if (order->GetConditionValue() > 100) order->SetConditionValue(100);
01348
01349 default:
01350 if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) order->SetConditionComparator(OCC_EQUALS);
01351 break;
01352 }
01353 break;
01354 }
01355
01356 case MOF_COND_COMPARATOR:
01357 order->SetConditionComparator((OrderConditionComparator)data);
01358 break;
01359
01360 case MOF_COND_VALUE:
01361 order->SetConditionValue(data);
01362 break;
01363
01364 case MOF_COND_DESTINATION:
01365 order->SetConditionSkipToOrder(data);
01366 break;
01367
01368 default: NOT_REACHED();
01369 }
01370
01371
01372 Vehicle *u = v->FirstShared();
01373 DeleteOrderWarnings(u);
01374 for (; u != NULL; u = u->NextShared()) {
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384 if (sel_ord == u->cur_real_order_index &&
01385 (u->current_order.IsType(OT_GOTO_STATION) || u->current_order.IsType(OT_LOADING)) &&
01386 u->current_order.GetLoadType() != order->GetLoadType()) {
01387 u->current_order.SetLoadType(order->GetLoadType());
01388 }
01389 InvalidateVehicleOrder(u, -2);
01390
01391
01392 InvalidateNextStation(u);
01393 }
01394 }
01395
01396 return CommandCost();
01397 }
01398
01410 CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01411 {
01412 VehicleID veh_src = GB(p2, 0, 20);
01413 VehicleID veh_dst = GB(p1, 0, 20);
01414
01415 Vehicle *dst = Vehicle::GetIfValid(veh_dst);
01416 if (dst == NULL || !dst->IsPrimaryVehicle()) return CMD_ERROR;
01417
01418 CommandCost ret = CheckOwnership(dst->owner);
01419 if (ret.Failed()) return ret;
01420
01421 switch (GB(p1, 30, 2)) {
01422 case CO_SHARE: {
01423 Vehicle *src = Vehicle::GetIfValid(veh_src);
01424
01425
01426 if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
01427
01428 CommandCost ret = CheckOwnership(src->owner);
01429 if (ret.Failed()) return ret;
01430
01431
01432 if (src->type == VEH_ROAD && RoadVehicle::From(src)->IsBus() != RoadVehicle::From(dst)->IsBus()) {
01433 return CMD_ERROR;
01434 }
01435
01436
01437 if (src->FirstShared() == dst->FirstShared()) return CMD_ERROR;
01438
01439 const Order *order;
01440
01441 FOR_VEHICLE_ORDERS(src, order) {
01442 if (OrderGoesToStation(dst, order) &&
01443 !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
01444 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
01445 }
01446 }
01447
01448 if (src->orders.list == NULL && !OrderList::CanAllocateItem()) {
01449 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
01450 }
01451
01452 if (flags & DC_EXEC) {
01453
01454
01455
01456 DeleteVehicleOrders(dst, false, dst->GetNumOrders() != src->GetNumOrders());
01457
01458 dst->orders.list = src->orders.list;
01459
01460
01461 dst->AddToShared(src);
01462 PrefillRouteLinks(dst);
01463
01464 InvalidateVehicleOrder(dst, -1);
01465 InvalidateVehicleOrder(src, -2);
01466
01467 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
01468 }
01469 break;
01470 }
01471
01472 case CO_COPY: {
01473 Vehicle *src = Vehicle::GetIfValid(veh_src);
01474
01475
01476 if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
01477
01478 CommandCost ret = CheckOwnership(src->owner);
01479 if (ret.Failed()) return ret;
01480
01481
01482
01483 const Order *order;
01484 FOR_VEHICLE_ORDERS(src, order) {
01485 if (OrderGoesToStation(dst, order) &&
01486 !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
01487 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
01488 }
01489 }
01490
01491
01492 int delta = dst->IsOrderListShared() ? src->GetNumOrders() + 1 : src->GetNumOrders() - dst->GetNumOrders();
01493 if (!Order::CanAllocateItem(delta) ||
01494 ((dst->orders.list == NULL || dst->IsOrderListShared()) && !OrderList::CanAllocateItem())) {
01495 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
01496 }
01497
01498 if (flags & DC_EXEC) {
01499 const Order *order;
01500 Order *first = NULL;
01501 Order **order_dst;
01502
01503
01504
01505
01506 DeleteVehicleOrders(dst, true, dst->GetNumOrders() != src->GetNumOrders());
01507
01508 order_dst = &first;
01509 FOR_VEHICLE_ORDERS(src, order) {
01510 *order_dst = new Order();
01511 (*order_dst)->AssignOrder(*order);
01512 order_dst = &(*order_dst)->next;
01513 }
01514 if (dst->orders.list == NULL) {
01515 dst->orders.list = new OrderList(first, dst);
01516 } else {
01517 assert(dst->orders.list->GetFirstOrder() == NULL);
01518 assert(!dst->orders.list->IsShared());
01519 delete dst->orders.list;
01520 assert(OrderList::CanAllocateItem());
01521 dst->orders.list = new OrderList(first, dst);
01522 }
01523
01524 PrefillRouteLinks(dst);
01525 InvalidateVehicleOrder(dst, -1);
01526
01527 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
01528 }
01529 break;
01530 }
01531
01532 case CO_UNSHARE: return DecloneOrder(dst, flags);
01533 default: return CMD_ERROR;
01534 }
01535
01536 return CommandCost();
01537 }
01538
01551 CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01552 {
01553 VehicleID veh = GB(p1, 0, 20);
01554 VehicleOrderID order_number = GB(p2, 16, 8);
01555 CargoID cargo = GB(p2, 0, 8);
01556 byte subtype = GB(p2, 8, 8);
01557
01558 if (cargo >= NUM_CARGO && cargo != CT_NO_REFIT) return CMD_ERROR;
01559
01560 const Vehicle *v = Vehicle::GetIfValid(veh);
01561 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01562
01563 CommandCost ret = CheckOwnership(v->owner);
01564 if (ret.Failed()) return ret;
01565
01566 Order *order = v->GetOrder(order_number);
01567 if (order == NULL) return CMD_ERROR;
01568
01569 if (flags & DC_EXEC) {
01570 order->SetRefit(cargo, subtype);
01571
01572 for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
01573
01574 InvalidateVehicleOrder(u, -2);
01575
01576
01577 if (u->cur_real_order_index == order_number && (u->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) {
01578 u->current_order.SetRefit(cargo, subtype);
01579 }
01580 }
01581 }
01582
01583 return CommandCost();
01584 }
01585
01586
01592 void CheckOrders(const Vehicle *v)
01593 {
01594
01595 if (_settings_client.gui.order_review_system == 0) return;
01596
01597
01598 if (v->vehstatus & VS_CRASHED) return;
01599
01600
01601 if (_settings_client.gui.order_review_system == 1 && (v->vehstatus & VS_STOPPED)) return;
01602
01603
01604 if (v->FirstShared() != v) return;
01605
01606
01607 if (v->owner == _local_company && v->day_counter % 20 == 0) {
01608 int n_st, problem_type = -1;
01609 const Order *order;
01610 int message = 0;
01611
01612
01613 n_st = 0;
01614
01615 FOR_VEHICLE_ORDERS(v, order) {
01616
01617 if (order->IsType(OT_DUMMY)) {
01618 problem_type = 1;
01619 break;
01620 }
01621
01622 if (order->IsType(OT_GOTO_STATION)) {
01623 const Station *st = Station::Get(order->GetDestination());
01624
01625 n_st++;
01626 if (!CanVehicleUseStation(v, st)) problem_type = 3;
01627 }
01628 }
01629
01630
01631 if (v->GetNumOrders() > 1) {
01632 const Order *last = v->GetLastOrder();
01633
01634 if (v->orders.list->GetFirstOrder()->Equals(*last)) {
01635 problem_type = 2;
01636 }
01637 }
01638
01639
01640 if (n_st < 2 && problem_type == -1) problem_type = 0;
01641
01642 #ifndef NDEBUG
01643 if (v->orders.list != NULL) v->orders.list->DebugCheckSanity();
01644 #endif
01645
01646
01647 if (problem_type < 0) return;
01648
01649 message = STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS + problem_type;
01650
01651
01652 SetDParam(0, v->index);
01653 AddVehicleNewsItem(
01654 message,
01655 NS_ADVICE,
01656 v->index
01657 );
01658 }
01659 }
01660
01666 void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination)
01667 {
01668 Vehicle *v;
01669
01670
01671
01672
01673
01674
01675 FOR_ALL_VEHICLES(v) {
01676 Order *order;
01677
01678 order = &v->current_order;
01679 if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
01680 v->current_order.GetDestination() == destination) {
01681 order->MakeDummy();
01682 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
01683 }
01684
01685
01686 int id = -1;
01687 FOR_VEHICLE_ORDERS(v, order) {
01688 id++;
01689 restart:
01690
01691 OrderType ot = order->GetType();
01692 if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
01693 if (ot == OT_IMPLICIT || (v->type == VEH_AIRCRAFT && ot == OT_GOTO_DEPOT)) ot = OT_GOTO_STATION;
01694 if (ot == type && order->GetDestination() == destination) {
01695
01696
01697
01698 if (order->IsType(OT_IMPLICIT)) {
01699 order = order->next;
01700 DeleteOrder(v, id);
01701 if (order != NULL) goto restart;
01702 break;
01703 }
01704
01705 order->MakeDummy();
01706 for (const Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
01707
01708 InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
01709 InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
01710 }
01711 }
01712 }
01713 }
01714 }
01715
01720 bool Vehicle::HasDepotOrder() const
01721 {
01722 const Order *order;
01723
01724 FOR_VEHICLE_ORDERS(this, order) {
01725 if (order->IsType(OT_GOTO_DEPOT)) return true;
01726 }
01727
01728 return false;
01729 }
01730
01740 void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist, bool reset_order_indices)
01741 {
01742 DeleteOrderWarnings(v);
01743
01744 if (v->IsOrderListShared()) {
01745
01746 v->RemoveFromShared();
01747 v->orders.list = NULL;
01748 } else if (v->orders.list != NULL) {
01749
01750 v->orders.list->FreeChain(keep_orderlist);
01751 if (!keep_orderlist) v->orders.list = NULL;
01752 }
01753
01754
01755 InvalidateNextStation(v);
01756
01757 if (reset_order_indices) {
01758 v->cur_implicit_order_index = v->cur_real_order_index = 0;
01759 if (v->current_order.IsType(OT_LOADING)) {
01760 CancelLoadingDueToDeletedOrder(v);
01761 }
01762 }
01763 }
01764
01772 uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id)
01773 {
01774 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);
01775 }
01776
01785 static bool CheckForValidOrders(const Vehicle *v)
01786 {
01787 const Order *order;
01788
01789 FOR_VEHICLE_ORDERS(v, order) {
01790 switch (order->GetType()) {
01791 case OT_GOTO_STATION:
01792 case OT_GOTO_DEPOT:
01793 case OT_GOTO_WAYPOINT:
01794 return true;
01795
01796 default:
01797 break;
01798 }
01799 }
01800
01801 return false;
01802 }
01803
01807 static bool OrderConditionCompare(OrderConditionComparator occ, int variable, int value)
01808 {
01809 switch (occ) {
01810 case OCC_EQUALS: return variable == value;
01811 case OCC_NOT_EQUALS: return variable != value;
01812 case OCC_LESS_THAN: return variable < value;
01813 case OCC_LESS_EQUALS: return variable <= value;
01814 case OCC_MORE_THAN: return variable > value;
01815 case OCC_MORE_EQUALS: return variable >= value;
01816 case OCC_IS_TRUE: return variable != 0;
01817 case OCC_IS_FALSE: return variable == 0;
01818 default: NOT_REACHED();
01819 }
01820 }
01821
01828 VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
01829 {
01830 if (order->GetType() != OT_CONDITIONAL) return INVALID_VEH_ORDER_ID;
01831
01832 bool skip_order = false;
01833 OrderConditionComparator occ = order->GetConditionComparator();
01834 uint16 value = order->GetConditionValue();
01835
01836 switch (order->GetConditionVariable()) {
01837 case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break;
01838 case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break;
01839 case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break;
01840 case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break;
01841 case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break;
01842 case OCV_UNCONDITIONALLY: skip_order = true; break;
01843 default: NOT_REACHED();
01844 }
01845
01846 return skip_order ? order->GetConditionSkipToOrder() : (VehicleOrderID)INVALID_VEH_ORDER_ID;
01847 }
01848
01855 bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth)
01856 {
01857 if (conditional_depth > v->GetNumOrders()) return false;
01858
01859 switch (order->GetType()) {
01860 case OT_GOTO_STATION:
01861 v->dest_tile = v->GetOrderStationLocation(order->GetDestination());
01862 return true;
01863
01864 case OT_GOTO_DEPOT:
01865 if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !v->NeedsServicing()) {
01866 UpdateVehicleTimetable(v, true);
01867 v->IncrementRealOrderIndex();
01868 break;
01869 }
01870
01871 if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
01872
01873 TileIndex location;
01874 DestinationID destination;
01875 bool reverse;
01876
01877 if (v->FindClosestDepot(&location, &destination, &reverse)) {
01878 v->dest_tile = location;
01879 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());
01880
01881
01882 if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
01883
01884 if (v->type == VEH_AIRCRAFT) {
01885 Aircraft *a = Aircraft::From(v);
01886 if (a->state == FLYING && a->targetairport != destination) {
01887
01888 extern void AircraftNextAirportPos_and_Order(Aircraft *a);
01889 AircraftNextAirportPos_and_Order(a);
01890 }
01891 }
01892 return true;
01893 }
01894
01895 UpdateVehicleTimetable(v, true);
01896 v->IncrementRealOrderIndex();
01897 } else {
01898 if (v->type != VEH_AIRCRAFT) {
01899 v->dest_tile = Depot::Get(order->GetDestination())->xy;
01900 }
01901 return true;
01902 }
01903 break;
01904
01905 case OT_GOTO_WAYPOINT:
01906 v->dest_tile = Waypoint::Get(order->GetDestination())->xy;
01907 return true;
01908
01909 case OT_CONDITIONAL: {
01910 VehicleOrderID next_order = ProcessConditionalOrder(order, v);
01911 if (next_order != INVALID_VEH_ORDER_ID) {
01912
01913
01914 UpdateVehicleTimetable(v, false);
01915 v->cur_implicit_order_index = v->cur_real_order_index = next_order;
01916 v->UpdateRealOrderIndex();
01917 v->current_order_time += v->GetOrder(v->cur_real_order_index)->travel_time;
01918
01919
01920
01921 if (v->IsGroundVehicle()) {
01922 uint16 &gv_flags = v->GetGroundVehicleFlags();
01923 SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
01924 }
01925 } else {
01926 UpdateVehicleTimetable(v, true);
01927 v->IncrementRealOrderIndex();
01928 }
01929 break;
01930 }
01931
01932 default:
01933 v->dest_tile = 0;
01934 return false;
01935 }
01936
01937 assert(v->cur_implicit_order_index < v->GetNumOrders());
01938 assert(v->cur_real_order_index < v->GetNumOrders());
01939
01940
01941 order = v->GetOrder(v->cur_real_order_index);
01942 if (order != NULL && order->IsType(OT_IMPLICIT)) {
01943 assert(v->GetNumManualOrders() == 0);
01944 order = NULL;
01945 }
01946
01947 if (order == NULL) {
01948 v->current_order.Free();
01949 v->dest_tile = 0;
01950 return false;
01951 }
01952
01953 v->current_order = *order;
01954
01955
01956 Order *implicit_order = v->GetOrder(v->cur_implicit_order_index);
01957 v->current_order.index = implicit_order->index;
01958
01959 return UpdateOrderDest(v, order, conditional_depth + 1);
01960 }
01961
01969 bool ProcessOrders(Vehicle *v)
01970 {
01971 switch (v->current_order.GetType()) {
01972 case OT_GOTO_DEPOT:
01973
01974 if (!(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return false;
01975 break;
01976
01977 case OT_LOADING:
01978 return false;
01979
01980 case OT_LEAVESTATION:
01981 if (v->type != VEH_AIRCRAFT) return false;
01982 break;
01983
01984 default: break;
01985 }
01986
01994 bool may_reverse = v->current_order.IsType(OT_NOTHING);
01995
01996
01997 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)) &&
01998 IsTileType(v->tile, MP_STATION) &&
01999 v->current_order.GetDestination() == GetStationIndex(v->tile)) {
02000 v->DeleteUnreachedImplicitOrders();
02001
02002
02003
02004
02005 v->last_station_visited = v->current_order.GetDestination();
02006 UpdateVehicleTimetable(v, true);
02007 v->IncrementImplicitOrderIndex();
02008 }
02009
02010
02011 assert(v->cur_implicit_order_index == 0 || v->cur_implicit_order_index < v->GetNumOrders());
02012 v->UpdateRealOrderIndex();
02013
02014 const Order *order = v->GetOrder(v->cur_real_order_index);
02015 if (order != NULL && order->IsType(OT_IMPLICIT)) {
02016 assert(v->GetNumManualOrders() == 0);
02017 order = NULL;
02018 }
02019
02020
02021 if (order == NULL || (v->type == VEH_AIRCRAFT && !CheckForValidOrders(v))) {
02022 if (v->type == VEH_AIRCRAFT) {
02023
02024 extern void HandleMissingAircraftOrders(Aircraft *v);
02025 HandleMissingAircraftOrders(Aircraft::From(v));
02026 return false;
02027 }
02028
02029 v->current_order.Free();
02030 v->dest_tile = 0;
02031 return false;
02032 }
02033
02034
02035 if (order->Equals(v->current_order) && (v->type == VEH_AIRCRAFT || v->dest_tile != 0) &&
02036 (v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || Station::Get(order->GetDestination())->dock_tile != INVALID_TILE)) {
02037 return false;
02038 }
02039
02040
02041 v->current_order = *order;
02042
02043
02044 Order *implicit_order = v->GetOrder(v->cur_implicit_order_index);
02045 v->current_order.index = implicit_order->index;
02046
02047 InvalidateVehicleOrder(v, -2);
02048 switch (v->type) {
02049 default:
02050 NOT_REACHED();
02051
02052 case VEH_ROAD:
02053 case VEH_TRAIN:
02054 break;
02055
02056 case VEH_AIRCRAFT:
02057 case VEH_SHIP:
02058 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
02059 break;
02060 }
02061
02062 return UpdateOrderDest(v, order) && may_reverse;
02063 }
02064
02072 bool Order::ShouldStopAtStation(const Vehicle *v, StationID station) const
02073 {
02074 bool is_dest_station = this->IsType(OT_GOTO_STATION) && this->dest == station;
02075
02076 return (!this->IsType(OT_GOTO_DEPOT) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0) &&
02077 v->last_station_visited != station &&
02078
02079 !(this->GetNonStopType() & (is_dest_station ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));
02080 }