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 "functions.h"
00021 #include "window_func.h"
00022 #include "timetable.h"
00023 #include "vehicle_func.h"
00024 #include "depot_base.h"
00025 #include "core/pool_func.hpp"
00026 #include "aircraft.h"
00027 #include "roadveh.h"
00028 #include "station_base.h"
00029 #include "waypoint_base.h"
00030 #include "company_base.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::MakeAutomatic(StationID destination)
00142 {
00143 this->type = OT_AUTOMATIC;
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
00240 void InvalidateVehicleOrder(const Vehicle *v, int data)
00241 {
00242 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
00243
00244 if (data != 0) {
00245
00246 InvalidateWindowData(WC_VEHICLE_ORDERS, v->index, data);
00247 InvalidateWindowData(WC_VEHICLE_TIMETABLE, v->index, data);
00248 return;
00249 }
00250
00251 SetWindowDirty(WC_VEHICLE_ORDERS, v->index);
00252 SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
00253 }
00254
00262 void Order::AssignOrder(const Order &other)
00263 {
00264 this->type = other.type;
00265 this->flags = other.flags;
00266 this->dest = other.dest;
00267
00268 this->refit_cargo = other.refit_cargo;
00269 this->refit_subtype = other.refit_subtype;
00270
00271 this->wait_time = other.wait_time;
00272 this->travel_time = other.travel_time;
00273 }
00274
00280 void OrderList::Initialize(Order *chain, Vehicle *v)
00281 {
00282 this->first = chain;
00283 this->first_shared = v;
00284
00285 this->num_orders = 0;
00286 this->num_manual_orders = 0;
00287 this->num_vehicles = 1;
00288 this->timetable_duration = 0;
00289
00290 for (Order *o = this->first; o != NULL; o = o->next) {
00291 ++this->num_orders;
00292 if (!o->IsType(OT_AUTOMATIC)) ++this->num_manual_orders;
00293 this->timetable_duration += o->wait_time + o->travel_time;
00294 }
00295
00296 for (Vehicle *u = this->first_shared->PreviousShared(); u != NULL; u = u->PreviousShared()) {
00297 ++this->num_vehicles;
00298 this->first_shared = u;
00299 }
00300
00301 for (const Vehicle *u = v->NextShared(); u != NULL; u = u->NextShared()) ++this->num_vehicles;
00302 }
00303
00309 void OrderList::FreeChain(bool keep_orderlist)
00310 {
00311 Order *next;
00312 for (Order *o = this->first; o != NULL; o = next) {
00313 next = o->next;
00314 delete o;
00315 }
00316
00317 if (keep_orderlist) {
00318 this->first = NULL;
00319 this->num_orders = 0;
00320 this->num_manual_orders = 0;
00321 this->timetable_duration = 0;
00322 } else {
00323 delete this;
00324 }
00325 }
00326
00332 Order *OrderList::GetOrderAt(int index) const
00333 {
00334 if (index < 0) return NULL;
00335
00336 Order *order = this->first;
00337
00338 while (order != NULL && index-- > 0) {
00339 order = order->next;
00340 }
00341 return order;
00342 }
00343
00349 void OrderList::InsertOrderAt(Order *new_order, int index)
00350 {
00351 if (this->first == NULL) {
00352 this->first = new_order;
00353 } else {
00354 if (index == 0) {
00355
00356 new_order->next = this->first;
00357 this->first = new_order;
00358 } else if (index >= this->num_orders) {
00359
00360 this->GetLastOrder()->next = new_order;
00361 } else {
00362
00363 Order *order = this->GetOrderAt(index - 1);
00364 new_order->next = order->next;
00365 order->next = new_order;
00366 }
00367 }
00368 ++this->num_orders;
00369 if (!new_order->IsType(OT_AUTOMATIC)) ++this->num_manual_orders;
00370 this->timetable_duration += new_order->wait_time + new_order->travel_time;
00371 }
00372
00373
00378 void OrderList::DeleteOrderAt(int index)
00379 {
00380 if (index >= this->num_orders) return;
00381
00382 Order *to_remove;
00383
00384 if (index == 0) {
00385 to_remove = this->first;
00386 this->first = to_remove->next;
00387 } else {
00388 Order *prev = GetOrderAt(index - 1);
00389 to_remove = prev->next;
00390 prev->next = to_remove->next;
00391 }
00392 --this->num_orders;
00393 if (!to_remove->IsType(OT_AUTOMATIC)) --this->num_manual_orders;
00394 this->timetable_duration -= (to_remove->wait_time + to_remove->travel_time);
00395 delete to_remove;
00396 }
00397
00403 void OrderList::MoveOrder(int from, int to)
00404 {
00405 if (from >= this->num_orders || to >= this->num_orders || from == to) return;
00406
00407 Order *moving_one;
00408
00409
00410 if (from == 0) {
00411 moving_one = this->first;
00412 this->first = moving_one->next;
00413 } else {
00414 Order *one_before = GetOrderAt(from - 1);
00415 moving_one = one_before->next;
00416 one_before->next = moving_one->next;
00417 }
00418
00419
00420 if (to == 0) {
00421 moving_one->next = this->first;
00422 this->first = moving_one;
00423 } else {
00424 Order *one_before = GetOrderAt(to - 1);
00425 moving_one->next = one_before->next;
00426 one_before->next = moving_one;
00427 }
00428 }
00429
00435 void OrderList::RemoveVehicle(Vehicle *v)
00436 {
00437 --this->num_vehicles;
00438 if (v == this->first_shared) this->first_shared = v->NextShared();
00439 }
00440
00445 bool OrderList::IsVehicleInSharedOrdersList(const Vehicle *v) const
00446 {
00447 for (const Vehicle *v_shared = this->first_shared; v_shared != NULL; v_shared = v_shared->NextShared()) {
00448 if (v_shared == v) return true;
00449 }
00450
00451 return false;
00452 }
00453
00459 int OrderList::GetPositionInSharedOrderList(const Vehicle *v) const
00460 {
00461 int count = 0;
00462 for (const Vehicle *v_shared = v->PreviousShared(); v_shared != NULL; v_shared = v_shared->PreviousShared()) count++;
00463 return count;
00464 }
00465
00470 bool OrderList::IsCompleteTimetable() const
00471 {
00472 for (Order *o = this->first; o != NULL; o = o->next) {
00473
00474 if (o->IsType(OT_AUTOMATIC)) continue;
00475 if (!o->IsCompletelyTimetabled()) return false;
00476 }
00477 return true;
00478 }
00479
00483 void OrderList::DebugCheckSanity() const
00484 {
00485 VehicleOrderID check_num_orders = 0;
00486 VehicleOrderID check_num_manual_orders = 0;
00487 uint check_num_vehicles = 0;
00488 Ticks check_timetable_duration = 0;
00489
00490 DEBUG(misc, 6, "Checking OrderList %hu for sanity...", this->index);
00491
00492 for (const Order *o = this->first; o != NULL; o = o->next) {
00493 ++check_num_orders;
00494 if (!o->IsType(OT_AUTOMATIC)) ++check_num_manual_orders;
00495 check_timetable_duration += o->wait_time + o->travel_time;
00496 }
00497 assert(this->num_orders == check_num_orders);
00498 assert(this->num_manual_orders == check_num_manual_orders);
00499 assert(this->timetable_duration == check_timetable_duration);
00500
00501 for (const Vehicle *v = this->first_shared; v != NULL; v = v->NextShared()) {
00502 ++check_num_vehicles;
00503 assert(v->orders.list == this);
00504 }
00505 assert(this->num_vehicles == check_num_vehicles);
00506 DEBUG(misc, 6, "... detected %u orders (%u manual), %u vehicles, %i ticks",
00507 (uint)this->num_orders, (uint)this->num_manual_orders,
00508 this->num_vehicles, this->timetable_duration);
00509 }
00510
00518 static inline bool OrderGoesToStation(const Vehicle *v, const Order *o)
00519 {
00520 return o->IsType(OT_GOTO_STATION) ||
00521 (v->type == VEH_AIRCRAFT && o->IsType(OT_GOTO_DEPOT) && !(o->GetDepotActionType() & ODATFB_NEAREST_DEPOT));
00522 }
00523
00530 static void DeleteOrderWarnings(const Vehicle *v)
00531 {
00532 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS);
00533 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_VOID_ORDER);
00534 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY);
00535 DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY);
00536 }
00537
00543 TileIndex Order::GetLocation(const Vehicle *v) const
00544 {
00545 switch (this->GetType()) {
00546 case OT_GOTO_WAYPOINT:
00547 case OT_GOTO_STATION:
00548 case OT_AUTOMATIC:
00549 return BaseStation::Get(this->GetDestination())->xy;
00550
00551 case OT_GOTO_DEPOT:
00552 if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) return INVALID_TILE;
00553 return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination())->xy : Depot::Get(this->GetDestination())->xy;
00554
00555 default:
00556 return INVALID_TILE;
00557 }
00558 }
00559
00560 static uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle *v, int conditional_depth = 0)
00561 {
00562 assert(v->type == VEH_SHIP);
00563
00564 if (cur->IsType(OT_CONDITIONAL)) {
00565 if (conditional_depth > v->GetNumOrders()) return 0;
00566
00567 conditional_depth++;
00568
00569 int dist1 = GetOrderDistance(prev, v->GetOrder(cur->GetConditionSkipToOrder()), v, conditional_depth);
00570 int dist2 = GetOrderDistance(prev, cur->next == NULL ? v->orders.list->GetFirstOrder() : cur->next, v, conditional_depth);
00571 return max(dist1, dist2);
00572 }
00573
00574 TileIndex prev_tile = prev->GetLocation(v);
00575 TileIndex cur_tile = cur->GetLocation(v);
00576 if (prev_tile == INVALID_TILE || cur_tile == INVALID_TILE) return 0;
00577 return DistanceManhattan(prev_tile, cur_tile);
00578 }
00579
00593 CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00594 {
00595 VehicleID veh = GB(p1, 0, 20);
00596 VehicleOrderID sel_ord = GB(p1, 20, 8);
00597 Order new_order(p2);
00598
00599 Vehicle *v = Vehicle::GetIfValid(veh);
00600 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00601
00602 CommandCost ret = CheckOwnership(v->owner);
00603 if (ret.Failed()) return ret;
00604
00605
00606
00607 switch (new_order.GetType()) {
00608 case OT_GOTO_STATION: {
00609 const Station *st = Station::GetIfValid(new_order.GetDestination());
00610 if (st == NULL) return CMD_ERROR;
00611
00612 if (st->owner != OWNER_NONE) {
00613 CommandCost ret = CheckOwnership(st->owner);
00614 if (ret.Failed()) return ret;
00615 }
00616
00617 if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00618 for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
00619 if (!CanVehicleUseStation(u, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED);
00620 }
00621
00622
00623 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
00624
00625
00626 if ((new_order.GetLoadType() & OLFB_NO_LOAD) && (new_order.GetUnloadType() & OUFB_NO_UNLOAD)) return CMD_ERROR;
00627
00628
00629 switch (new_order.GetLoadType()) {
00630 case OLF_LOAD_IF_POSSIBLE: case OLFB_FULL_LOAD: case OLF_FULL_LOAD_ANY: case OLFB_NO_LOAD: break;
00631 default: return CMD_ERROR;
00632 }
00633 switch (new_order.GetUnloadType()) {
00634 case OUF_UNLOAD_IF_POSSIBLE: case OUFB_UNLOAD: case OUFB_TRANSFER: case OUFB_NO_UNLOAD: break;
00635 default: return CMD_ERROR;
00636 }
00637
00638
00639 switch (new_order.GetStopLocation()) {
00640 case OSL_PLATFORM_NEAR_END:
00641 case OSL_PLATFORM_MIDDLE:
00642 if (v->type != VEH_TRAIN) return CMD_ERROR;
00643
00644 case OSL_PLATFORM_FAR_END:
00645 break;
00646
00647 default:
00648 return CMD_ERROR;
00649 }
00650
00651 break;
00652 }
00653
00654 case OT_GOTO_DEPOT: {
00655 if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
00656 if (v->type == VEH_AIRCRAFT) {
00657 const Station *st = Station::GetIfValid(new_order.GetDestination());
00658
00659 if (st == NULL) return CMD_ERROR;
00660
00661 CommandCost ret = CheckOwnership(st->owner);
00662 if (ret.Failed()) return ret;
00663
00664 if (!CanVehicleUseStation(v, st) || !st->airport.HasHangar()) {
00665 return CMD_ERROR;
00666 }
00667 } else {
00668 const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
00669
00670 if (dp == NULL) return CMD_ERROR;
00671
00672 CommandCost ret = CheckOwnership(GetTileOwner(dp->xy));
00673 if (ret.Failed()) return ret;
00674
00675 switch (v->type) {
00676 case VEH_TRAIN:
00677 if (!IsRailDepotTile(dp->xy)) return CMD_ERROR;
00678 break;
00679
00680 case VEH_ROAD:
00681 if (!IsRoadDepotTile(dp->xy)) return CMD_ERROR;
00682 break;
00683
00684 case VEH_SHIP:
00685 if (!IsShipDepotTile(dp->xy)) return CMD_ERROR;
00686 break;
00687
00688 default: return CMD_ERROR;
00689 }
00690 }
00691 }
00692
00693 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
00694 if (new_order.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS | ((new_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0 ? ODTFB_SERVICE : 0))) return CMD_ERROR;
00695 if (new_order.GetDepotActionType() & ~(ODATFB_HALT | ODATFB_NEAREST_DEPOT)) return CMD_ERROR;
00696 if ((new_order.GetDepotOrderType() & ODTFB_SERVICE) && (new_order.GetDepotActionType() & ODATFB_HALT)) return CMD_ERROR;
00697 break;
00698 }
00699
00700 case OT_GOTO_WAYPOINT: {
00701 const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
00702 if (wp == NULL) return CMD_ERROR;
00703
00704 switch (v->type) {
00705 default: return CMD_ERROR;
00706
00707 case VEH_TRAIN: {
00708 if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00709
00710 CommandCost ret = CheckOwnership(wp->owner);
00711 if (ret.Failed()) return ret;
00712 break;
00713 }
00714
00715 case VEH_SHIP:
00716 if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00717 if (wp->owner != OWNER_NONE) {
00718 CommandCost ret = CheckOwnership(wp->owner);
00719 if (ret.Failed()) return ret;
00720 }
00721 break;
00722 }
00723
00724
00725
00726
00727 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
00728 break;
00729 }
00730
00731 case OT_CONDITIONAL: {
00732 VehicleOrderID skip_to = new_order.GetConditionSkipToOrder();
00733 if (skip_to != 0 && skip_to >= v->GetNumOrders()) return CMD_ERROR;
00734 if (new_order.GetConditionVariable() > OCV_END) return CMD_ERROR;
00735
00736 OrderConditionComparator occ = new_order.GetConditionComparator();
00737 if (occ > OCC_END) return CMD_ERROR;
00738 switch (new_order.GetConditionVariable()) {
00739 case OCV_REQUIRES_SERVICE:
00740 if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
00741 break;
00742
00743 case OCV_UNCONDITIONALLY:
00744 if (occ != OCC_EQUALS) return CMD_ERROR;
00745 if (new_order.GetConditionValue() != 0) return CMD_ERROR;
00746 break;
00747
00748 case OCV_LOAD_PERCENTAGE:
00749 case OCV_RELIABILITY:
00750 if (new_order.GetConditionValue() > 100) return CMD_ERROR;
00751
00752 default:
00753 if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) return CMD_ERROR;
00754 break;
00755 }
00756 break;
00757 }
00758
00759 default: return CMD_ERROR;
00760 }
00761
00762 if (sel_ord > v->GetNumOrders()) return CMD_ERROR;
00763
00764 if (v->GetNumOrders() >= MAX_VEH_ORDER_ID) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS);
00765 if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
00766 if (v->orders.list == NULL && !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
00767
00768 if (v->type == VEH_SHIP && _settings_game.pf.pathfinder_for_ships != VPF_NPF) {
00769
00770 const Order *prev = NULL;
00771 uint n = 0;
00772
00773
00774
00775
00776 const Order *o;
00777 FOR_VEHICLE_ORDERS(v, o) {
00778 switch (o->GetType()) {
00779 case OT_GOTO_STATION:
00780 case OT_GOTO_DEPOT:
00781 case OT_GOTO_WAYPOINT:
00782 prev = o;
00783 break;
00784
00785 default: break;
00786 }
00787 if (++n == sel_ord && prev != NULL) break;
00788 }
00789 if (prev != NULL) {
00790 uint dist = GetOrderDistance(prev, &new_order, v);
00791 if (dist >= 130) {
00792 return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION);
00793 }
00794 }
00795 }
00796
00797 if (flags & DC_EXEC) {
00798 Order *new_o = new Order();
00799 new_o->AssignOrder(new_order);
00800 InsertOrder(v, new_o, sel_ord);
00801 }
00802
00803 return CommandCost();
00804 }
00805
00812 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord)
00813 {
00814
00815 if (v->orders.list == NULL) {
00816 v->orders.list = new OrderList(new_o, v);
00817 } else {
00818 v->orders.list->InsertOrderAt(new_o, sel_ord);
00819 }
00820
00821 Vehicle *u = v->FirstShared();
00822 DeleteOrderWarnings(u);
00823 for (; u != NULL; u = u->NextShared()) {
00824 assert(v->orders.list == u->orders.list);
00825
00826
00827
00828 if (sel_ord <= u->cur_order_index) {
00829 uint cur = u->cur_order_index + 1;
00830
00831 if (cur < u->GetNumOrders()) {
00832 u->cur_order_index = cur;
00833 }
00834 }
00835
00836 InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
00837 }
00838
00839
00840 VehicleOrderID cur_order_id = 0;
00841 Order *order;
00842 FOR_VEHICLE_ORDERS(v, order) {
00843 if (order->IsType(OT_CONDITIONAL)) {
00844 VehicleOrderID order_id = order->GetConditionSkipToOrder();
00845 if (order_id >= sel_ord) {
00846 order->SetConditionSkipToOrder(order_id + 1);
00847 }
00848 if (order_id == cur_order_id) {
00849 order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
00850 }
00851 }
00852 cur_order_id++;
00853 }
00854
00855
00856 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00857 }
00858
00864 static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags)
00865 {
00866 if (flags & DC_EXEC) {
00867 DeleteVehicleOrders(dst);
00868 InvalidateVehicleOrder(dst, -1);
00869 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
00870 }
00871 return CommandCost();
00872 }
00873
00883 CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00884 {
00885 VehicleID veh_id = GB(p1, 0, 20);
00886 VehicleOrderID sel_ord = GB(p2, 0, 8);
00887
00888 Vehicle *v = Vehicle::GetIfValid(veh_id);
00889
00890 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00891
00892 CommandCost ret = CheckOwnership(v->owner);
00893 if (ret.Failed()) return ret;
00894
00895
00896 if (sel_ord >= v->GetNumOrders()) return DecloneOrder(v, flags);
00897
00898 if (v->GetOrder(sel_ord) == NULL) return CMD_ERROR;
00899
00900 if (flags & DC_EXEC) DeleteOrder(v, sel_ord);
00901 return CommandCost();
00902 }
00903
00909 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord)
00910 {
00911 v->orders.list->DeleteOrderAt(sel_ord);
00912
00913 Vehicle *u = v->FirstShared();
00914 DeleteOrderWarnings(u);
00915 for (; u != NULL; u = u->NextShared()) {
00916 assert(v->orders.list == u->orders.list);
00917
00918
00919
00920 if (sel_ord == u->cur_order_index && u->current_order.IsType(OT_LOADING)) {
00921 u->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
00922
00923
00924 if (u->current_order.GetLoadType() & OLFB_FULL_LOAD) u->current_order.SetLoadType(OLF_LOAD_IF_POSSIBLE);
00925 }
00926
00927 if (sel_ord < u->cur_order_index) u->cur_order_index--;
00928
00929
00930 InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
00931 }
00932
00933
00934 VehicleOrderID cur_order_id = 0;
00935 Order *order = NULL;
00936 FOR_VEHICLE_ORDERS(v, order) {
00937 if (order->IsType(OT_CONDITIONAL)) {
00938 VehicleOrderID order_id = order->GetConditionSkipToOrder();
00939 if (order_id >= sel_ord) {
00940 order->SetConditionSkipToOrder(max(order_id - 1, 0));
00941 }
00942 if (order_id == cur_order_id) {
00943 order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
00944 }
00945 }
00946 cur_order_id++;
00947 }
00948
00949 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00950 }
00951
00961 CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00962 {
00963 VehicleID veh_id = GB(p1, 0, 20);
00964 VehicleOrderID sel_ord = GB(p2, 0, 8);
00965
00966 Vehicle *v = Vehicle::GetIfValid(veh_id);
00967
00968 if (v == NULL || !v->IsPrimaryVehicle() || sel_ord == v->cur_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR;
00969
00970 CommandCost ret = CheckOwnership(v->owner);
00971 if (ret.Failed()) return ret;
00972
00973 if (flags & DC_EXEC) {
00974 v->cur_order_index = sel_ord;
00975
00976 if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
00977
00978 InvalidateVehicleOrder(v, -2);
00979 }
00980
00981
00982 if (v->type == VEH_AIRCRAFT) SetWindowClassesDirty(WC_AIRCRAFT_LIST);
00983 if (v->type == VEH_SHIP) SetWindowClassesDirty(WC_SHIPS_LIST);
00984
00985 return CommandCost();
00986 }
00987
01001 CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01002 {
01003 VehicleID veh = GB(p1, 0, 20);
01004 VehicleOrderID moving_order = GB(p2, 0, 16);
01005 VehicleOrderID target_order = GB(p2, 16, 16);
01006
01007 Vehicle *v = Vehicle::GetIfValid(veh);
01008 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01009
01010 CommandCost ret = CheckOwnership(v->owner);
01011 if (ret.Failed()) return ret;
01012
01013
01014 if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() ||
01015 moving_order == target_order || v->GetNumOrders() <= 1) return CMD_ERROR;
01016
01017 Order *moving_one = v->GetOrder(moving_order);
01018
01019 if (moving_one == NULL) return CMD_ERROR;
01020
01021 if (flags & DC_EXEC) {
01022 v->orders.list->MoveOrder(moving_order, target_order);
01023
01024
01025 Vehicle *u = v->FirstShared();
01026
01027 DeleteOrderWarnings(u);
01028
01029 for (; u != NULL; u = u->NextShared()) {
01030
01031 if (u->cur_order_index == moving_order) {
01032 u->cur_order_index = target_order;
01033 } else if (u->cur_order_index > moving_order && u->cur_order_index <= target_order) {
01034 u->cur_order_index--;
01035 } else if (u->cur_order_index < moving_order && u->cur_order_index >= target_order) {
01036 u->cur_order_index++;
01037 }
01038
01039 assert(v->orders.list == u->orders.list);
01040
01041 InvalidateVehicleOrder(u, moving_order | (target_order << 8));
01042 }
01043
01044
01045 Order *order;
01046 FOR_VEHICLE_ORDERS(v, order) {
01047 if (order->IsType(OT_CONDITIONAL)) {
01048 VehicleOrderID order_id = order->GetConditionSkipToOrder();
01049 if (order_id == moving_order) {
01050 order_id = target_order;
01051 } else if (order_id > moving_order && order_id <= target_order) {
01052 order_id--;
01053 } else if (order_id < moving_order && order_id >= target_order) {
01054 order_id++;
01055 }
01056 order->SetConditionSkipToOrder(order_id);
01057 }
01058 }
01059
01060
01061 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
01062 }
01063
01064 return CommandCost();
01065 }
01066
01082 CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01083 {
01084 VehicleOrderID sel_ord = GB(p1, 20, 8);
01085 VehicleID veh = GB(p1, 0, 20);
01086 ModifyOrderFlags mof = Extract<ModifyOrderFlags, 0, 4>(p2);
01087 uint16 data = GB(p2, 4, 11);
01088
01089 if (mof >= MOF_END) return CMD_ERROR;
01090
01091 Vehicle *v = Vehicle::GetIfValid(veh);
01092 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01093
01094 CommandCost ret = CheckOwnership(v->owner);
01095 if (ret.Failed()) return ret;
01096
01097
01098 if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
01099
01100 Order *order = v->GetOrder(sel_ord);
01101 switch (order->GetType()) {
01102 case OT_GOTO_STATION:
01103 if (mof == MOF_COND_VARIABLE || mof == MOF_COND_COMPARATOR || mof == MOF_DEPOT_ACTION || mof == MOF_COND_VALUE) return CMD_ERROR;
01104 break;
01105
01106 case OT_GOTO_DEPOT:
01107 if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR;
01108 break;
01109
01110 case OT_GOTO_WAYPOINT:
01111 if (mof != MOF_NON_STOP) return CMD_ERROR;
01112 break;
01113
01114 case OT_CONDITIONAL:
01115 if (mof != MOF_COND_VARIABLE && mof != MOF_COND_COMPARATOR && mof != MOF_COND_VALUE && mof != MOF_COND_DESTINATION) return CMD_ERROR;
01116 break;
01117
01118 default:
01119 return CMD_ERROR;
01120 }
01121
01122 switch (mof) {
01123 default: NOT_REACHED();
01124
01125 case MOF_NON_STOP:
01126 if (!v->IsGroundVehicle()) return CMD_ERROR;
01127 if (data >= ONSF_END) return CMD_ERROR;
01128 if (data == order->GetNonStopType()) return CMD_ERROR;
01129 break;
01130
01131 case MOF_STOP_LOCATION:
01132 if (v->type != VEH_TRAIN) return CMD_ERROR;
01133 if (data >= OSL_END) return CMD_ERROR;
01134 break;
01135
01136 case MOF_UNLOAD:
01137 if ((data & ~(OUFB_UNLOAD | OUFB_TRANSFER | OUFB_NO_UNLOAD)) != 0) return CMD_ERROR;
01138
01139 if (data != 0 && ((data & (OUFB_UNLOAD | OUFB_TRANSFER)) != 0) == ((data & OUFB_NO_UNLOAD) != 0)) return CMD_ERROR;
01140 if (data == order->GetUnloadType()) return CMD_ERROR;
01141 break;
01142
01143 case MOF_LOAD:
01144 if (data > OLFB_NO_LOAD || data == 1) return CMD_ERROR;
01145 if (data == order->GetLoadType()) return CMD_ERROR;
01146 break;
01147
01148 case MOF_DEPOT_ACTION:
01149 if (data >= DA_END) return CMD_ERROR;
01150 break;
01151
01152 case MOF_COND_VARIABLE:
01153 if (data >= OCV_END) return CMD_ERROR;
01154 break;
01155
01156 case MOF_COND_COMPARATOR:
01157 if (data >= OCC_END) return CMD_ERROR;
01158 switch (order->GetConditionVariable()) {
01159 case OCV_UNCONDITIONALLY: return CMD_ERROR;
01160
01161 case OCV_REQUIRES_SERVICE:
01162 if (data != OCC_IS_TRUE && data != OCC_IS_FALSE) return CMD_ERROR;
01163 break;
01164
01165 default:
01166 if (data == OCC_IS_TRUE || data == OCC_IS_FALSE) return CMD_ERROR;
01167 break;
01168 }
01169 break;
01170
01171 case MOF_COND_VALUE:
01172 switch (order->GetConditionVariable()) {
01173 case OCV_UNCONDITIONALLY: return CMD_ERROR;
01174
01175 case OCV_LOAD_PERCENTAGE:
01176 case OCV_RELIABILITY:
01177 if (data > 100) return CMD_ERROR;
01178 break;
01179
01180 default:
01181 if (data > 2047) return CMD_ERROR;
01182 break;
01183 }
01184 break;
01185
01186 case MOF_COND_DESTINATION:
01187 if (data >= v->GetNumOrders()) return CMD_ERROR;
01188 break;
01189 }
01190
01191 if (flags & DC_EXEC) {
01192 switch (mof) {
01193 case MOF_NON_STOP:
01194 order->SetNonStopType((OrderNonStopFlags)data);
01195 break;
01196
01197 case MOF_STOP_LOCATION:
01198 order->SetStopLocation((OrderStopLocation)data);
01199 break;
01200
01201 case MOF_UNLOAD:
01202 order->SetUnloadType((OrderUnloadFlags)data);
01203 if ((data & OUFB_NO_UNLOAD) != 0 && (order->GetLoadType() & OLFB_NO_LOAD) != 0) {
01204 order->SetLoadType((OrderLoadFlags)(order->GetLoadType() & ~OLFB_NO_LOAD));
01205 }
01206 break;
01207
01208 case MOF_LOAD:
01209 order->SetLoadType((OrderLoadFlags)data);
01210 if ((data & OLFB_NO_LOAD) != 0 && (order->GetUnloadType() & OUFB_NO_UNLOAD) != 0) {
01211
01212 order->SetUnloadType((OrderUnloadFlags)(order->GetUnloadType() & ~OUFB_NO_UNLOAD));
01213 }
01214 break;
01215
01216 case MOF_DEPOT_ACTION: {
01217 switch (data) {
01218 case DA_ALWAYS_GO:
01219 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01220 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01221 break;
01222
01223 case DA_SERVICE:
01224 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() | ODTFB_SERVICE));
01225 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01226 break;
01227
01228 case DA_STOP:
01229 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01230 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() | ODATFB_HALT));
01231 break;
01232
01233 default:
01234 NOT_REACHED();
01235 }
01236 break;
01237 }
01238
01239 case MOF_COND_VARIABLE: {
01240 order->SetConditionVariable((OrderConditionVariable)data);
01241
01242 OrderConditionComparator occ = order->GetConditionComparator();
01243 switch (order->GetConditionVariable()) {
01244 case OCV_UNCONDITIONALLY:
01245 order->SetConditionComparator(OCC_EQUALS);
01246 order->SetConditionValue(0);
01247 break;
01248
01249 case OCV_REQUIRES_SERVICE:
01250 if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
01251 break;
01252
01253 case OCV_LOAD_PERCENTAGE:
01254 case OCV_RELIABILITY:
01255 if (order->GetConditionValue() > 100) order->SetConditionValue(100);
01256
01257 default:
01258 if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) order->SetConditionComparator(OCC_EQUALS);
01259 break;
01260 }
01261 break;
01262 }
01263
01264 case MOF_COND_COMPARATOR:
01265 order->SetConditionComparator((OrderConditionComparator)data);
01266 break;
01267
01268 case MOF_COND_VALUE:
01269 order->SetConditionValue(data);
01270 break;
01271
01272 case MOF_COND_DESTINATION:
01273 order->SetConditionSkipToOrder(data);
01274 break;
01275
01276 default: NOT_REACHED();
01277 }
01278
01279
01280 Vehicle *u = v->FirstShared();
01281 DeleteOrderWarnings(u);
01282 for (; u != NULL; u = u->NextShared()) {
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292 if (sel_ord == u->cur_order_index &&
01293 (u->current_order.IsType(OT_GOTO_STATION) || u->current_order.IsType(OT_LOADING)) &&
01294 u->current_order.GetLoadType() != order->GetLoadType()) {
01295 u->current_order.SetLoadType(order->GetLoadType());
01296 }
01297 InvalidateVehicleOrder(u, -2);
01298 }
01299 }
01300
01301 return CommandCost();
01302 }
01303
01315 CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01316 {
01317 VehicleID veh_src = GB(p2, 0, 20);
01318 VehicleID veh_dst = GB(p1, 0, 20);
01319
01320 Vehicle *dst = Vehicle::GetIfValid(veh_dst);
01321 if (dst == NULL || !dst->IsPrimaryVehicle()) return CMD_ERROR;
01322
01323 CommandCost ret = CheckOwnership(dst->owner);
01324 if (ret.Failed()) return ret;
01325
01326 switch (GB(p1, 30, 2)) {
01327 case CO_SHARE: {
01328 Vehicle *src = Vehicle::GetIfValid(veh_src);
01329
01330
01331 if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
01332
01333 CommandCost ret = CheckOwnership(src->owner);
01334 if (ret.Failed()) return ret;
01335
01336
01337 if (src->type == VEH_ROAD && RoadVehicle::From(src)->IsBus() != RoadVehicle::From(dst)->IsBus()) {
01338 return CMD_ERROR;
01339 }
01340
01341
01342 if (src->FirstShared() == dst->FirstShared()) return CMD_ERROR;
01343
01344 const Order *order;
01345
01346 FOR_VEHICLE_ORDERS(src, order) {
01347 if (OrderGoesToStation(dst, order) &&
01348 !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
01349 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
01350 }
01351 }
01352
01353 if (flags & DC_EXEC) {
01354
01355 DeleteVehicleOrders(dst);
01356
01357 dst->orders.list = src->orders.list;
01358
01359
01360 dst->AddToShared(src);
01361
01362 InvalidateVehicleOrder(dst, -1);
01363 InvalidateVehicleOrder(src, -2);
01364
01365 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
01366 }
01367 break;
01368 }
01369
01370 case CO_COPY: {
01371 Vehicle *src = Vehicle::GetIfValid(veh_src);
01372
01373
01374 if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
01375
01376 CommandCost ret = CheckOwnership(src->owner);
01377 if (ret.Failed()) return ret;
01378
01379
01380
01381 const Order *order;
01382 FOR_VEHICLE_ORDERS(src, order) {
01383 if (OrderGoesToStation(dst, order) &&
01384 !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
01385 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
01386 }
01387 }
01388
01389
01390 int delta = dst->IsOrderListShared() ? src->GetNumOrders() + 1 : src->GetNumOrders() - dst->GetNumOrders();
01391 if (!Order::CanAllocateItem(delta) ||
01392 ((dst->orders.list == NULL || dst->IsOrderListShared()) && !OrderList::CanAllocateItem())) {
01393 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
01394 }
01395
01396 if (flags & DC_EXEC) {
01397 const Order *order;
01398 Order *first = NULL;
01399 Order **order_dst;
01400
01401
01402 DeleteVehicleOrders(dst, true);
01403
01404 order_dst = &first;
01405 FOR_VEHICLE_ORDERS(src, order) {
01406 *order_dst = new Order();
01407 (*order_dst)->AssignOrder(*order);
01408 order_dst = &(*order_dst)->next;
01409 }
01410 if (dst->orders.list == NULL) {
01411 dst->orders.list = new OrderList(first, dst);
01412 } else {
01413 assert(dst->orders.list->GetFirstOrder() == NULL);
01414 assert(!dst->orders.list->IsShared());
01415 delete dst->orders.list;
01416 dst->orders.list = new OrderList(first, dst);
01417 }
01418
01419 InvalidateVehicleOrder(dst, -1);
01420
01421 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
01422 }
01423 break;
01424 }
01425
01426 case CO_UNSHARE: return DecloneOrder(dst, flags);
01427 default: return CMD_ERROR;
01428 }
01429
01430 return CommandCost();
01431 }
01432
01445 CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01446 {
01447 VehicleID veh = GB(p1, 0, 20);
01448 VehicleOrderID order_number = GB(p2, 16, 8);
01449 CargoID cargo = GB(p2, 0, 8);
01450 byte subtype = GB(p2, 8, 8);
01451
01452 if (cargo >= NUM_CARGO && cargo != CT_NO_REFIT) return CMD_ERROR;
01453
01454 const Vehicle *v = Vehicle::GetIfValid(veh);
01455 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01456
01457 CommandCost ret = CheckOwnership(v->owner);
01458 if (ret.Failed()) return ret;
01459
01460 Order *order = v->GetOrder(order_number);
01461 if (order == NULL) return CMD_ERROR;
01462
01463 if (flags & DC_EXEC) {
01464 order->SetRefit(cargo, subtype);
01465
01466 for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
01467
01468 InvalidateVehicleOrder(u, -2);
01469
01470
01471 if (u->cur_order_index == order_number && (u->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) {
01472 u->current_order.SetRefit(cargo, subtype);
01473 }
01474 }
01475 }
01476
01477 return CommandCost();
01478 }
01479
01480
01486 void CheckOrders(const Vehicle *v)
01487 {
01488
01489 if (_settings_client.gui.order_review_system == 0) return;
01490
01491
01492 if (v->vehstatus & VS_CRASHED) return;
01493
01494
01495 if (_settings_client.gui.order_review_system == 1 && (v->vehstatus & VS_STOPPED)) return;
01496
01497
01498 if (v->FirstShared() != v) return;
01499
01500
01501 if (v->owner == _local_company && v->day_counter % 20 == 0) {
01502 int n_st, problem_type = -1;
01503 const Order *order;
01504 int message = 0;
01505
01506
01507 n_st = 0;
01508
01509 FOR_VEHICLE_ORDERS(v, order) {
01510
01511 if (order->IsType(OT_DUMMY)) {
01512 problem_type = 1;
01513 break;
01514 }
01515
01516 if (order->IsType(OT_GOTO_STATION)) {
01517 const Station *st = Station::Get(order->GetDestination());
01518
01519 n_st++;
01520 if (!CanVehicleUseStation(v, st)) problem_type = 3;
01521 }
01522 }
01523
01524
01525 if (v->GetNumOrders() > 1) {
01526 const Order *last = v->GetLastOrder();
01527
01528 if (v->orders.list->GetFirstOrder()->Equals(*last)) {
01529 problem_type = 2;
01530 }
01531 }
01532
01533
01534 if (n_st < 2 && problem_type == -1) problem_type = 0;
01535
01536 #ifndef NDEBUG
01537 if (v->orders.list != NULL) v->orders.list->DebugCheckSanity();
01538 #endif
01539
01540
01541 if (problem_type < 0) return;
01542
01543 message = STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS + problem_type;
01544
01545
01546 SetDParam(0, v->index);
01547 AddVehicleNewsItem(
01548 message,
01549 NS_ADVICE,
01550 v->index
01551 );
01552 }
01553 }
01554
01560 void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination)
01561 {
01562 Vehicle *v;
01563
01564
01565
01566
01567
01568
01569 FOR_ALL_VEHICLES(v) {
01570 Order *order;
01571
01572 order = &v->current_order;
01573 if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
01574 v->current_order.GetDestination() == destination) {
01575 order->MakeDummy();
01576 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
01577 }
01578
01579
01580 int id = -1;
01581 FOR_VEHICLE_ORDERS(v, order) {
01582 id++;
01583
01584 OrderType ot = order->GetType();
01585 if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
01586 if (ot == OT_AUTOMATIC || (v->type == VEH_AIRCRAFT && ot == OT_GOTO_DEPOT)) ot = OT_GOTO_STATION;
01587 if (ot == type && order->GetDestination() == destination) {
01588
01589
01590
01591 if (order->IsType(OT_AUTOMATIC)) {
01592 DeleteOrder(v, id);
01593 id--;
01594 continue;
01595 }
01596
01597 order->MakeDummy();
01598 for (const Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
01599
01600 InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
01601 InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
01602 }
01603 }
01604 }
01605 }
01606 }
01607
01612 bool Vehicle::HasDepotOrder() const
01613 {
01614 const Order *order;
01615
01616 FOR_VEHICLE_ORDERS(this, order) {
01617 if (order->IsType(OT_GOTO_DEPOT)) return true;
01618 }
01619
01620 return false;
01621 }
01622
01628 void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist)
01629 {
01630 DeleteOrderWarnings(v);
01631
01632 if (v->IsOrderListShared()) {
01633
01634 v->RemoveFromShared();
01635 v->orders.list = NULL;
01636 } else if (v->orders.list != NULL) {
01637
01638 v->orders.list->FreeChain(keep_orderlist);
01639 if (!keep_orderlist) v->orders.list = NULL;
01640 }
01641 }
01642
01650 uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id)
01651 {
01652 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);
01653 }
01654
01663 static bool CheckForValidOrders(const Vehicle *v)
01664 {
01665 const Order *order;
01666
01667 FOR_VEHICLE_ORDERS(v, order) {
01668 switch (order->GetType()) {
01669 case OT_GOTO_STATION:
01670 case OT_GOTO_DEPOT:
01671 case OT_GOTO_WAYPOINT:
01672 return true;
01673
01674 default:
01675 break;
01676 }
01677 }
01678
01679 return false;
01680 }
01681
01685 static bool OrderConditionCompare(OrderConditionComparator occ, int variable, int value)
01686 {
01687 switch (occ) {
01688 case OCC_EQUALS: return variable == value;
01689 case OCC_NOT_EQUALS: return variable != value;
01690 case OCC_LESS_THAN: return variable < value;
01691 case OCC_LESS_EQUALS: return variable <= value;
01692 case OCC_MORE_THAN: return variable > value;
01693 case OCC_MORE_EQUALS: return variable >= value;
01694 case OCC_IS_TRUE: return variable != 0;
01695 case OCC_IS_FALSE: return variable == 0;
01696 default: NOT_REACHED();
01697 }
01698 }
01699
01706 VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
01707 {
01708 if (order->GetType() != OT_CONDITIONAL) return INVALID_VEH_ORDER_ID;
01709
01710 bool skip_order = false;
01711 OrderConditionComparator occ = order->GetConditionComparator();
01712 uint16 value = order->GetConditionValue();
01713
01714 switch (order->GetConditionVariable()) {
01715 case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break;
01716 case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break;
01717 case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break;
01718 case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break;
01719 case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break;
01720 case OCV_UNCONDITIONALLY: skip_order = true; break;
01721 default: NOT_REACHED();
01722 }
01723
01724 return skip_order ? order->GetConditionSkipToOrder() : (VehicleOrderID)INVALID_VEH_ORDER_ID;
01725 }
01726
01733 bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth)
01734 {
01735 if (conditional_depth > v->GetNumOrders()) return false;
01736
01737 switch (order->GetType()) {
01738 case OT_GOTO_STATION:
01739 v->dest_tile = v->GetOrderStationLocation(order->GetDestination());
01740 return true;
01741
01742 case OT_GOTO_DEPOT:
01743 if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !v->NeedsServicing()) {
01744 UpdateVehicleTimetable(v, true);
01745 v->IncrementOrderIndex();
01746 break;
01747 }
01748
01749 if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
01750
01751 TileIndex location;
01752 DestinationID destination;
01753 bool reverse;
01754
01755 if (v->FindClosestDepot(&location, &destination, &reverse)) {
01756 v->dest_tile = location;
01757 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());
01758
01759
01760 if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
01761
01762 if (v->type == VEH_AIRCRAFT) {
01763 Aircraft *a = Aircraft::From(v);
01764 if (a->state == FLYING && a->targetairport != destination) {
01765
01766 extern void AircraftNextAirportPos_and_Order(Aircraft *a);
01767 AircraftNextAirportPos_and_Order(a);
01768 }
01769 }
01770 return true;
01771 }
01772
01773 UpdateVehicleTimetable(v, true);
01774 v->IncrementOrderIndex();
01775 } else {
01776 if (v->type != VEH_AIRCRAFT) {
01777 v->dest_tile = Depot::Get(order->GetDestination())->xy;
01778 }
01779 return true;
01780 }
01781 break;
01782
01783 case OT_GOTO_WAYPOINT:
01784 v->dest_tile = Waypoint::Get(order->GetDestination())->xy;
01785 return true;
01786
01787 case OT_CONDITIONAL: {
01788 VehicleOrderID next_order = ProcessConditionalOrder(order, v);
01789 if (next_order != INVALID_VEH_ORDER_ID) {
01790 UpdateVehicleTimetable(v, false);
01791 v->cur_order_index = next_order;
01792 v->current_order_time += v->GetOrder(next_order)->travel_time;
01793 } else {
01794 UpdateVehicleTimetable(v, true);
01795 v->IncrementOrderIndex();
01796 }
01797 break;
01798 }
01799
01800 default:
01801 v->dest_tile = 0;
01802 return false;
01803 }
01804
01805 assert(v->cur_order_index < v->GetNumOrders());
01806
01807
01808 order = v->GetNextManualOrder(v->cur_order_index);
01809 if (order == NULL) {
01810 order = v->GetNextManualOrder(0);
01811 if (order == NULL) {
01812 v->current_order.Free();
01813 v->dest_tile = 0;
01814 return false;
01815 }
01816 }
01817 v->current_order = *order;
01818 return UpdateOrderDest(v, order, conditional_depth + 1);
01819 }
01820
01828 bool ProcessOrders(Vehicle *v)
01829 {
01830 switch (v->current_order.GetType()) {
01831 case OT_GOTO_DEPOT:
01832
01833 if (!(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return false;
01834 break;
01835
01836 case OT_LOADING:
01837 return false;
01838
01839 case OT_LEAVESTATION:
01840 if (v->type != VEH_AIRCRAFT) return false;
01841 break;
01842
01843 default: break;
01844 }
01845
01853 bool may_reverse = v->current_order.IsType(OT_NOTHING);
01854
01855
01856 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)) &&
01857 IsTileType(v->tile, MP_STATION) &&
01858 v->current_order.GetDestination() == GetStationIndex(v->tile)) {
01859 v->DeleteUnreachedAutoOrders();
01860
01861
01862
01863
01864 v->last_station_visited = v->current_order.GetDestination();
01865 UpdateVehicleTimetable(v, true);
01866 v->IncrementOrderIndex();
01867 }
01868
01869
01870 if (v->cur_order_index >= v->GetNumOrders()) v->cur_order_index = 0;
01871
01872 const Order *order = v->GetNextManualOrder(v->cur_order_index);
01873
01874
01875 if (order == NULL || (v->type == VEH_AIRCRAFT && !CheckForValidOrders(v))) {
01876 if (v->type == VEH_AIRCRAFT) {
01877
01878 extern void HandleMissingAircraftOrders(Aircraft *v);
01879 HandleMissingAircraftOrders(Aircraft::From(v));
01880 return false;
01881 }
01882
01883 v->current_order.Free();
01884 v->dest_tile = 0;
01885 return false;
01886 }
01887
01888
01889 if (order->Equals(v->current_order) && (v->type == VEH_AIRCRAFT || v->dest_tile != 0) &&
01890 (v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || Station::Get(order->GetDestination())->dock_tile != INVALID_TILE)) {
01891 return false;
01892 }
01893
01894
01895 v->current_order = *order;
01896
01897 InvalidateVehicleOrder(v, -2);
01898 switch (v->type) {
01899 default:
01900 NOT_REACHED();
01901
01902 case VEH_ROAD:
01903 case VEH_TRAIN:
01904 break;
01905
01906 case VEH_AIRCRAFT:
01907 case VEH_SHIP:
01908 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
01909 break;
01910 }
01911
01912 return UpdateOrderDest(v, order) && may_reverse;
01913 }
01914
01922 bool Order::ShouldStopAtStation(const Vehicle *v, StationID station) const
01923 {
01924 bool is_dest_station = this->IsType(OT_GOTO_STATION) && this->dest == station;
01925
01926 return (!this->IsType(OT_GOTO_DEPOT) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0) &&
01927 v->last_station_visited != station &&
01928
01929 !(this->GetNonStopType() & (is_dest_station ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));
01930 }
01931
01932 void InitializeOrders()
01933 {
01934 _order_pool.CleanPool();
01935 _orderlist_pool.CleanPool();
01936 }