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 switch (new_order.GetLoadType()) {
00627 case OLF_LOAD_IF_POSSIBLE: case OLFB_FULL_LOAD: case OLF_FULL_LOAD_ANY: case OLFB_NO_LOAD: break;
00628 default: return CMD_ERROR;
00629 }
00630 switch (new_order.GetUnloadType()) {
00631 case OUF_UNLOAD_IF_POSSIBLE: case OUFB_UNLOAD: case OUFB_TRANSFER: case OUFB_NO_UNLOAD: break;
00632 default: return CMD_ERROR;
00633 }
00634
00635
00636 switch (new_order.GetStopLocation()) {
00637 case OSL_PLATFORM_NEAR_END:
00638 case OSL_PLATFORM_MIDDLE:
00639 if (v->type != VEH_TRAIN) return CMD_ERROR;
00640
00641 case OSL_PLATFORM_FAR_END:
00642 break;
00643
00644 default:
00645 return CMD_ERROR;
00646 }
00647
00648 break;
00649 }
00650
00651 case OT_GOTO_DEPOT: {
00652 if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
00653 if (v->type == VEH_AIRCRAFT) {
00654 const Station *st = Station::GetIfValid(new_order.GetDestination());
00655
00656 if (st == NULL) return CMD_ERROR;
00657
00658 CommandCost ret = CheckOwnership(st->owner);
00659 if (ret.Failed()) return ret;
00660
00661 if (!CanVehicleUseStation(v, st) || !st->airport.HasHangar()) {
00662 return CMD_ERROR;
00663 }
00664 } else {
00665 const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
00666
00667 if (dp == NULL) return CMD_ERROR;
00668
00669 CommandCost ret = CheckOwnership(GetTileOwner(dp->xy));
00670 if (ret.Failed()) return ret;
00671
00672 switch (v->type) {
00673 case VEH_TRAIN:
00674 if (!IsRailDepotTile(dp->xy)) return CMD_ERROR;
00675 break;
00676
00677 case VEH_ROAD:
00678 if (!IsRoadDepotTile(dp->xy)) return CMD_ERROR;
00679 break;
00680
00681 case VEH_SHIP:
00682 if (!IsShipDepotTile(dp->xy)) return CMD_ERROR;
00683 break;
00684
00685 default: return CMD_ERROR;
00686 }
00687 }
00688 }
00689
00690 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
00691 if (new_order.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS | ((new_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0 ? ODTFB_SERVICE : 0))) return CMD_ERROR;
00692 if (new_order.GetDepotActionType() & ~(ODATFB_HALT | ODATFB_NEAREST_DEPOT)) return CMD_ERROR;
00693 if ((new_order.GetDepotOrderType() & ODTFB_SERVICE) && (new_order.GetDepotActionType() & ODATFB_HALT)) return CMD_ERROR;
00694 break;
00695 }
00696
00697 case OT_GOTO_WAYPOINT: {
00698 const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
00699 if (wp == NULL) return CMD_ERROR;
00700
00701 switch (v->type) {
00702 default: return CMD_ERROR;
00703
00704 case VEH_TRAIN: {
00705 if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00706
00707 CommandCost ret = CheckOwnership(wp->owner);
00708 if (ret.Failed()) return ret;
00709 break;
00710 }
00711
00712 case VEH_SHIP:
00713 if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
00714 if (wp->owner != OWNER_NONE) {
00715 CommandCost ret = CheckOwnership(wp->owner);
00716 if (ret.Failed()) return ret;
00717 }
00718 break;
00719 }
00720
00721
00722
00723
00724 if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
00725 break;
00726 }
00727
00728 case OT_CONDITIONAL: {
00729 VehicleOrderID skip_to = new_order.GetConditionSkipToOrder();
00730 if (skip_to != 0 && skip_to >= v->GetNumOrders()) return CMD_ERROR;
00731 if (new_order.GetConditionVariable() > OCV_END) return CMD_ERROR;
00732
00733 OrderConditionComparator occ = new_order.GetConditionComparator();
00734 if (occ > OCC_END) return CMD_ERROR;
00735 switch (new_order.GetConditionVariable()) {
00736 case OCV_REQUIRES_SERVICE:
00737 if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
00738 break;
00739
00740 case OCV_UNCONDITIONALLY:
00741 if (occ != OCC_EQUALS) return CMD_ERROR;
00742 if (new_order.GetConditionValue() != 0) return CMD_ERROR;
00743 break;
00744
00745 case OCV_LOAD_PERCENTAGE:
00746 case OCV_RELIABILITY:
00747 if (new_order.GetConditionValue() > 100) return CMD_ERROR;
00748
00749 default:
00750 if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) return CMD_ERROR;
00751 break;
00752 }
00753 break;
00754 }
00755
00756 default: return CMD_ERROR;
00757 }
00758
00759 if (sel_ord > v->GetNumOrders()) return CMD_ERROR;
00760
00761 if (v->GetNumOrders() >= MAX_VEH_ORDER_ID) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS);
00762 if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
00763 if (v->orders.list == NULL && !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
00764
00765 if (v->type == VEH_SHIP && _settings_game.pf.pathfinder_for_ships != VPF_NPF) {
00766
00767 const Order *prev = NULL;
00768 uint n = 0;
00769
00770
00771
00772
00773 const Order *o;
00774 FOR_VEHICLE_ORDERS(v, o) {
00775 switch (o->GetType()) {
00776 case OT_GOTO_STATION:
00777 case OT_GOTO_DEPOT:
00778 case OT_GOTO_WAYPOINT:
00779 prev = o;
00780 break;
00781
00782 default: break;
00783 }
00784 if (++n == sel_ord && prev != NULL) break;
00785 }
00786 if (prev != NULL) {
00787 uint dist = GetOrderDistance(prev, &new_order, v);
00788 if (dist >= 130) {
00789 return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION);
00790 }
00791 }
00792 }
00793
00794 if (flags & DC_EXEC) {
00795 Order *new_o = new Order();
00796 new_o->AssignOrder(new_order);
00797 InsertOrder(v, new_o, sel_ord);
00798 }
00799
00800 return CommandCost();
00801 }
00802
00809 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord)
00810 {
00811
00812 if (v->orders.list == NULL) {
00813 v->orders.list = new OrderList(new_o, v);
00814 } else {
00815 v->orders.list->InsertOrderAt(new_o, sel_ord);
00816 }
00817
00818 Vehicle *u = v->FirstShared();
00819 DeleteOrderWarnings(u);
00820 for (; u != NULL; u = u->NextShared()) {
00821 assert(v->orders.list == u->orders.list);
00822
00823
00824
00825
00826
00827 if (sel_ord <= u->cur_real_order_index) {
00828 uint cur = u->cur_real_order_index + 1;
00829
00830 if (cur < u->GetNumOrders()) {
00831 u->cur_real_order_index = cur;
00832 }
00833 }
00834 if (sel_ord <= u->cur_auto_order_index) {
00835 uint cur = u->cur_auto_order_index + 1;
00836
00837 if (cur < u->GetNumOrders()) {
00838 u->cur_auto_order_index = cur;
00839 }
00840 }
00841
00842 InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
00843 }
00844
00845
00846 VehicleOrderID cur_order_id = 0;
00847 Order *order;
00848 FOR_VEHICLE_ORDERS(v, order) {
00849 if (order->IsType(OT_CONDITIONAL)) {
00850 VehicleOrderID order_id = order->GetConditionSkipToOrder();
00851 if (order_id >= sel_ord) {
00852 order->SetConditionSkipToOrder(order_id + 1);
00853 }
00854 if (order_id == cur_order_id) {
00855 order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
00856 }
00857 }
00858 cur_order_id++;
00859 }
00860
00861
00862 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00863 }
00864
00870 static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags)
00871 {
00872 if (flags & DC_EXEC) {
00873 DeleteVehicleOrders(dst);
00874 InvalidateVehicleOrder(dst, -1);
00875 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
00876 }
00877 return CommandCost();
00878 }
00879
00889 CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00890 {
00891 VehicleID veh_id = GB(p1, 0, 20);
00892 VehicleOrderID sel_ord = GB(p2, 0, 8);
00893
00894 Vehicle *v = Vehicle::GetIfValid(veh_id);
00895
00896 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
00897
00898 CommandCost ret = CheckOwnership(v->owner);
00899 if (ret.Failed()) return ret;
00900
00901
00902 if (sel_ord >= v->GetNumOrders()) return DecloneOrder(v, flags);
00903
00904 if (v->GetOrder(sel_ord) == NULL) return CMD_ERROR;
00905
00906 if (flags & DC_EXEC) DeleteOrder(v, sel_ord);
00907 return CommandCost();
00908 }
00909
00915 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord)
00916 {
00917 v->orders.list->DeleteOrderAt(sel_ord);
00918
00919 Vehicle *u = v->FirstShared();
00920 DeleteOrderWarnings(u);
00921 for (; u != NULL; u = u->NextShared()) {
00922 assert(v->orders.list == u->orders.list);
00923
00924
00925
00926 if (sel_ord == u->cur_real_order_index && u->current_order.IsType(OT_LOADING)) {
00927 u->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
00928
00929
00930 if (u->current_order.GetLoadType() & OLFB_FULL_LOAD) u->current_order.SetLoadType(OLF_LOAD_IF_POSSIBLE);
00931 }
00932
00933 if (sel_ord < u->cur_real_order_index) {
00934 u->cur_real_order_index--;
00935 } else if (sel_ord == u->cur_real_order_index) {
00936 u->UpdateRealOrderIndex();
00937 }
00938
00939 if (sel_ord < u->cur_auto_order_index) {
00940 u->cur_auto_order_index--;
00941 } else if (sel_ord == u->cur_auto_order_index) {
00942
00943 if (u->cur_auto_order_index >= u->GetNumOrders()) u->cur_auto_order_index = 0;
00944
00945
00946 while (u->cur_auto_order_index != u->cur_real_order_index && !u->GetOrder(u->cur_auto_order_index)->IsType(OT_AUTOMATIC)) {
00947 u->cur_auto_order_index++;
00948 if (u->cur_auto_order_index >= u->GetNumOrders()) u->cur_auto_order_index = 0;
00949 }
00950 }
00951
00952
00953 InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
00954 }
00955
00956
00957 VehicleOrderID cur_order_id = 0;
00958 Order *order = NULL;
00959 FOR_VEHICLE_ORDERS(v, order) {
00960 if (order->IsType(OT_CONDITIONAL)) {
00961 VehicleOrderID order_id = order->GetConditionSkipToOrder();
00962 if (order_id >= sel_ord) {
00963 order->SetConditionSkipToOrder(max(order_id - 1, 0));
00964 }
00965 if (order_id == cur_order_id) {
00966 order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
00967 }
00968 }
00969 cur_order_id++;
00970 }
00971
00972 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
00973 }
00974
00984 CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00985 {
00986 VehicleID veh_id = GB(p1, 0, 20);
00987 VehicleOrderID sel_ord = GB(p2, 0, 8);
00988
00989 Vehicle *v = Vehicle::GetIfValid(veh_id);
00990
00991 if (v == NULL || !v->IsPrimaryVehicle() || sel_ord == v->cur_auto_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR;
00992
00993 CommandCost ret = CheckOwnership(v->owner);
00994 if (ret.Failed()) return ret;
00995
00996 if (flags & DC_EXEC) {
00997 v->cur_auto_order_index = v->cur_real_order_index = sel_ord;
00998 v->UpdateRealOrderIndex();
00999
01000 if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
01001
01002 InvalidateVehicleOrder(v, -2);
01003 }
01004
01005
01006 if (v->type == VEH_AIRCRAFT) SetWindowClassesDirty(WC_AIRCRAFT_LIST);
01007 if (v->type == VEH_SHIP) SetWindowClassesDirty(WC_SHIPS_LIST);
01008
01009 return CommandCost();
01010 }
01011
01025 CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01026 {
01027 VehicleID veh = GB(p1, 0, 20);
01028 VehicleOrderID moving_order = GB(p2, 0, 16);
01029 VehicleOrderID target_order = GB(p2, 16, 16);
01030
01031 Vehicle *v = Vehicle::GetIfValid(veh);
01032 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01033
01034 CommandCost ret = CheckOwnership(v->owner);
01035 if (ret.Failed()) return ret;
01036
01037
01038 if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() ||
01039 moving_order == target_order || v->GetNumOrders() <= 1) return CMD_ERROR;
01040
01041 Order *moving_one = v->GetOrder(moving_order);
01042
01043 if (moving_one == NULL) return CMD_ERROR;
01044
01045 if (flags & DC_EXEC) {
01046 v->orders.list->MoveOrder(moving_order, target_order);
01047
01048
01049 Vehicle *u = v->FirstShared();
01050
01051 DeleteOrderWarnings(u);
01052
01053 for (; u != NULL; u = u->NextShared()) {
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070 if (u->cur_real_order_index == moving_order) {
01071 u->cur_real_order_index = target_order;
01072 } else if (u->cur_real_order_index > moving_order && u->cur_real_order_index <= target_order) {
01073 u->cur_real_order_index--;
01074 } else if (u->cur_real_order_index < moving_order && u->cur_real_order_index >= target_order) {
01075 u->cur_real_order_index++;
01076 }
01077
01078 if (u->cur_auto_order_index == moving_order) {
01079 u->cur_auto_order_index = target_order;
01080 } else if (u->cur_auto_order_index > moving_order && u->cur_auto_order_index <= target_order) {
01081 u->cur_auto_order_index--;
01082 } else if (u->cur_auto_order_index < moving_order && u->cur_auto_order_index >= target_order) {
01083 u->cur_auto_order_index++;
01084 }
01085
01086 assert(v->orders.list == u->orders.list);
01087
01088 InvalidateVehicleOrder(u, moving_order | (target_order << 8));
01089 }
01090
01091
01092 Order *order;
01093 FOR_VEHICLE_ORDERS(v, order) {
01094 if (order->IsType(OT_CONDITIONAL)) {
01095 VehicleOrderID order_id = order->GetConditionSkipToOrder();
01096 if (order_id == moving_order) {
01097 order_id = target_order;
01098 } else if (order_id > moving_order && order_id <= target_order) {
01099 order_id--;
01100 } else if (order_id < moving_order && order_id >= target_order) {
01101 order_id++;
01102 }
01103 order->SetConditionSkipToOrder(order_id);
01104 }
01105 }
01106
01107
01108 InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
01109 }
01110
01111 return CommandCost();
01112 }
01113
01129 CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01130 {
01131 VehicleOrderID sel_ord = GB(p1, 20, 8);
01132 VehicleID veh = GB(p1, 0, 20);
01133 ModifyOrderFlags mof = Extract<ModifyOrderFlags, 0, 4>(p2);
01134 uint16 data = GB(p2, 4, 11);
01135
01136 if (mof >= MOF_END) return CMD_ERROR;
01137
01138 Vehicle *v = Vehicle::GetIfValid(veh);
01139 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01140
01141 CommandCost ret = CheckOwnership(v->owner);
01142 if (ret.Failed()) return ret;
01143
01144
01145 if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
01146
01147 Order *order = v->GetOrder(sel_ord);
01148 switch (order->GetType()) {
01149 case OT_GOTO_STATION:
01150 if (mof == MOF_COND_VARIABLE || mof == MOF_COND_COMPARATOR || mof == MOF_DEPOT_ACTION || mof == MOF_COND_VALUE) return CMD_ERROR;
01151 break;
01152
01153 case OT_GOTO_DEPOT:
01154 if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR;
01155 break;
01156
01157 case OT_GOTO_WAYPOINT:
01158 if (mof != MOF_NON_STOP) return CMD_ERROR;
01159 break;
01160
01161 case OT_CONDITIONAL:
01162 if (mof != MOF_COND_VARIABLE && mof != MOF_COND_COMPARATOR && mof != MOF_COND_VALUE && mof != MOF_COND_DESTINATION) return CMD_ERROR;
01163 break;
01164
01165 default:
01166 return CMD_ERROR;
01167 }
01168
01169 switch (mof) {
01170 default: NOT_REACHED();
01171
01172 case MOF_NON_STOP:
01173 if (!v->IsGroundVehicle()) return CMD_ERROR;
01174 if (data >= ONSF_END) return CMD_ERROR;
01175 if (data == order->GetNonStopType()) return CMD_ERROR;
01176 break;
01177
01178 case MOF_STOP_LOCATION:
01179 if (v->type != VEH_TRAIN) return CMD_ERROR;
01180 if (data >= OSL_END) return CMD_ERROR;
01181 break;
01182
01183 case MOF_UNLOAD:
01184 if ((data & ~(OUFB_UNLOAD | OUFB_TRANSFER | OUFB_NO_UNLOAD)) != 0) return CMD_ERROR;
01185
01186 if (data != 0 && ((data & (OUFB_UNLOAD | OUFB_TRANSFER)) != 0) == ((data & OUFB_NO_UNLOAD) != 0)) return CMD_ERROR;
01187 if (data == order->GetUnloadType()) return CMD_ERROR;
01188 break;
01189
01190 case MOF_LOAD:
01191 if (data > OLFB_NO_LOAD || data == 1) return CMD_ERROR;
01192 if (data == order->GetLoadType()) return CMD_ERROR;
01193 break;
01194
01195 case MOF_DEPOT_ACTION:
01196 if (data >= DA_END) return CMD_ERROR;
01197 break;
01198
01199 case MOF_COND_VARIABLE:
01200 if (data >= OCV_END) return CMD_ERROR;
01201 break;
01202
01203 case MOF_COND_COMPARATOR:
01204 if (data >= OCC_END) return CMD_ERROR;
01205 switch (order->GetConditionVariable()) {
01206 case OCV_UNCONDITIONALLY: return CMD_ERROR;
01207
01208 case OCV_REQUIRES_SERVICE:
01209 if (data != OCC_IS_TRUE && data != OCC_IS_FALSE) return CMD_ERROR;
01210 break;
01211
01212 default:
01213 if (data == OCC_IS_TRUE || data == OCC_IS_FALSE) return CMD_ERROR;
01214 break;
01215 }
01216 break;
01217
01218 case MOF_COND_VALUE:
01219 switch (order->GetConditionVariable()) {
01220 case OCV_UNCONDITIONALLY: return CMD_ERROR;
01221
01222 case OCV_LOAD_PERCENTAGE:
01223 case OCV_RELIABILITY:
01224 if (data > 100) return CMD_ERROR;
01225 break;
01226
01227 default:
01228 if (data > 2047) return CMD_ERROR;
01229 break;
01230 }
01231 break;
01232
01233 case MOF_COND_DESTINATION:
01234 if (data >= v->GetNumOrders()) return CMD_ERROR;
01235 break;
01236 }
01237
01238 if (flags & DC_EXEC) {
01239 switch (mof) {
01240 case MOF_NON_STOP:
01241 order->SetNonStopType((OrderNonStopFlags)data);
01242 break;
01243
01244 case MOF_STOP_LOCATION:
01245 order->SetStopLocation((OrderStopLocation)data);
01246 break;
01247
01248 case MOF_UNLOAD:
01249 order->SetUnloadType((OrderUnloadFlags)data);
01250 break;
01251
01252 case MOF_LOAD:
01253 order->SetLoadType((OrderLoadFlags)data);
01254 break;
01255
01256 case MOF_DEPOT_ACTION: {
01257 switch (data) {
01258 case DA_ALWAYS_GO:
01259 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01260 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01261 break;
01262
01263 case DA_SERVICE:
01264 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() | ODTFB_SERVICE));
01265 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
01266 break;
01267
01268 case DA_STOP:
01269 order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
01270 order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() | ODATFB_HALT));
01271 break;
01272
01273 default:
01274 NOT_REACHED();
01275 }
01276 break;
01277 }
01278
01279 case MOF_COND_VARIABLE: {
01280 order->SetConditionVariable((OrderConditionVariable)data);
01281
01282 OrderConditionComparator occ = order->GetConditionComparator();
01283 switch (order->GetConditionVariable()) {
01284 case OCV_UNCONDITIONALLY:
01285 order->SetConditionComparator(OCC_EQUALS);
01286 order->SetConditionValue(0);
01287 break;
01288
01289 case OCV_REQUIRES_SERVICE:
01290 if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
01291 break;
01292
01293 case OCV_LOAD_PERCENTAGE:
01294 case OCV_RELIABILITY:
01295 if (order->GetConditionValue() > 100) order->SetConditionValue(100);
01296
01297 default:
01298 if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) order->SetConditionComparator(OCC_EQUALS);
01299 break;
01300 }
01301 break;
01302 }
01303
01304 case MOF_COND_COMPARATOR:
01305 order->SetConditionComparator((OrderConditionComparator)data);
01306 break;
01307
01308 case MOF_COND_VALUE:
01309 order->SetConditionValue(data);
01310 break;
01311
01312 case MOF_COND_DESTINATION:
01313 order->SetConditionSkipToOrder(data);
01314 break;
01315
01316 default: NOT_REACHED();
01317 }
01318
01319
01320 Vehicle *u = v->FirstShared();
01321 DeleteOrderWarnings(u);
01322 for (; u != NULL; u = u->NextShared()) {
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332 if (sel_ord == u->cur_real_order_index &&
01333 (u->current_order.IsType(OT_GOTO_STATION) || u->current_order.IsType(OT_LOADING)) &&
01334 u->current_order.GetLoadType() != order->GetLoadType()) {
01335 u->current_order.SetLoadType(order->GetLoadType());
01336 }
01337 InvalidateVehicleOrder(u, -2);
01338 }
01339 }
01340
01341 return CommandCost();
01342 }
01343
01355 CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01356 {
01357 VehicleID veh_src = GB(p2, 0, 20);
01358 VehicleID veh_dst = GB(p1, 0, 20);
01359
01360 Vehicle *dst = Vehicle::GetIfValid(veh_dst);
01361 if (dst == NULL || !dst->IsPrimaryVehicle()) return CMD_ERROR;
01362
01363 CommandCost ret = CheckOwnership(dst->owner);
01364 if (ret.Failed()) return ret;
01365
01366 switch (GB(p1, 30, 2)) {
01367 case CO_SHARE: {
01368 Vehicle *src = Vehicle::GetIfValid(veh_src);
01369
01370
01371 if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
01372
01373 CommandCost ret = CheckOwnership(src->owner);
01374 if (ret.Failed()) return ret;
01375
01376
01377 if (src->type == VEH_ROAD && RoadVehicle::From(src)->IsBus() != RoadVehicle::From(dst)->IsBus()) {
01378 return CMD_ERROR;
01379 }
01380
01381
01382 if (src->FirstShared() == dst->FirstShared()) return CMD_ERROR;
01383
01384 const Order *order;
01385
01386 FOR_VEHICLE_ORDERS(src, order) {
01387 if (OrderGoesToStation(dst, order) &&
01388 !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
01389 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
01390 }
01391 }
01392
01393 if (flags & DC_EXEC) {
01394
01395 DeleteVehicleOrders(dst);
01396
01397 dst->orders.list = src->orders.list;
01398
01399
01400 dst->AddToShared(src);
01401
01402 InvalidateVehicleOrder(dst, -1);
01403 InvalidateVehicleOrder(src, -2);
01404
01405 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
01406 }
01407 break;
01408 }
01409
01410 case CO_COPY: {
01411 Vehicle *src = Vehicle::GetIfValid(veh_src);
01412
01413
01414 if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
01415
01416 CommandCost ret = CheckOwnership(src->owner);
01417 if (ret.Failed()) return ret;
01418
01419
01420
01421 const Order *order;
01422 FOR_VEHICLE_ORDERS(src, order) {
01423 if (OrderGoesToStation(dst, order) &&
01424 !CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
01425 return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
01426 }
01427 }
01428
01429
01430 int delta = dst->IsOrderListShared() ? src->GetNumOrders() + 1 : src->GetNumOrders() - dst->GetNumOrders();
01431 if (!Order::CanAllocateItem(delta) ||
01432 ((dst->orders.list == NULL || dst->IsOrderListShared()) && !OrderList::CanAllocateItem())) {
01433 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
01434 }
01435
01436 if (flags & DC_EXEC) {
01437 const Order *order;
01438 Order *first = NULL;
01439 Order **order_dst;
01440
01441
01442 DeleteVehicleOrders(dst, true);
01443
01444 order_dst = &first;
01445 FOR_VEHICLE_ORDERS(src, order) {
01446 *order_dst = new Order();
01447 (*order_dst)->AssignOrder(*order);
01448 order_dst = &(*order_dst)->next;
01449 }
01450 if (dst->orders.list == NULL) {
01451 dst->orders.list = new OrderList(first, dst);
01452 } else {
01453 assert(dst->orders.list->GetFirstOrder() == NULL);
01454 assert(!dst->orders.list->IsShared());
01455 delete dst->orders.list;
01456 dst->orders.list = new OrderList(first, dst);
01457 }
01458
01459 InvalidateVehicleOrder(dst, -1);
01460
01461 InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
01462 }
01463 break;
01464 }
01465
01466 case CO_UNSHARE: return DecloneOrder(dst, flags);
01467 default: return CMD_ERROR;
01468 }
01469
01470 return CommandCost();
01471 }
01472
01485 CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01486 {
01487 VehicleID veh = GB(p1, 0, 20);
01488 VehicleOrderID order_number = GB(p2, 16, 8);
01489 CargoID cargo = GB(p2, 0, 8);
01490 byte subtype = GB(p2, 8, 8);
01491
01492 if (cargo >= NUM_CARGO && cargo != CT_NO_REFIT) return CMD_ERROR;
01493
01494 const Vehicle *v = Vehicle::GetIfValid(veh);
01495 if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
01496
01497 CommandCost ret = CheckOwnership(v->owner);
01498 if (ret.Failed()) return ret;
01499
01500 Order *order = v->GetOrder(order_number);
01501 if (order == NULL) return CMD_ERROR;
01502
01503 if (flags & DC_EXEC) {
01504 order->SetRefit(cargo, subtype);
01505
01506 for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
01507
01508 InvalidateVehicleOrder(u, -2);
01509
01510
01511 if (u->cur_real_order_index == order_number && (u->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) {
01512 u->current_order.SetRefit(cargo, subtype);
01513 }
01514 }
01515 }
01516
01517 return CommandCost();
01518 }
01519
01520
01526 void CheckOrders(const Vehicle *v)
01527 {
01528
01529 if (_settings_client.gui.order_review_system == 0) return;
01530
01531
01532 if (v->vehstatus & VS_CRASHED) return;
01533
01534
01535 if (_settings_client.gui.order_review_system == 1 && (v->vehstatus & VS_STOPPED)) return;
01536
01537
01538 if (v->FirstShared() != v) return;
01539
01540
01541 if (v->owner == _local_company && v->day_counter % 20 == 0) {
01542 int n_st, problem_type = -1;
01543 const Order *order;
01544 int message = 0;
01545
01546
01547 n_st = 0;
01548
01549 FOR_VEHICLE_ORDERS(v, order) {
01550
01551 if (order->IsType(OT_DUMMY)) {
01552 problem_type = 1;
01553 break;
01554 }
01555
01556 if (order->IsType(OT_GOTO_STATION)) {
01557 const Station *st = Station::Get(order->GetDestination());
01558
01559 n_st++;
01560 if (!CanVehicleUseStation(v, st)) problem_type = 3;
01561 }
01562 }
01563
01564
01565 if (v->GetNumOrders() > 1) {
01566 const Order *last = v->GetLastOrder();
01567
01568 if (v->orders.list->GetFirstOrder()->Equals(*last)) {
01569 problem_type = 2;
01570 }
01571 }
01572
01573
01574 if (n_st < 2 && problem_type == -1) problem_type = 0;
01575
01576 #ifndef NDEBUG
01577 if (v->orders.list != NULL) v->orders.list->DebugCheckSanity();
01578 #endif
01579
01580
01581 if (problem_type < 0) return;
01582
01583 message = STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS + problem_type;
01584
01585
01586 SetDParam(0, v->index);
01587 AddVehicleNewsItem(
01588 message,
01589 NS_ADVICE,
01590 v->index
01591 );
01592 }
01593 }
01594
01600 void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination)
01601 {
01602 Vehicle *v;
01603
01604
01605
01606
01607
01608
01609 FOR_ALL_VEHICLES(v) {
01610 Order *order;
01611
01612 order = &v->current_order;
01613 if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
01614 v->current_order.GetDestination() == destination) {
01615 order->MakeDummy();
01616 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
01617 }
01618
01619
01620 int id = -1;
01621 FOR_VEHICLE_ORDERS(v, order) {
01622 id++;
01623
01624 OrderType ot = order->GetType();
01625 if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
01626 if (ot == OT_AUTOMATIC || (v->type == VEH_AIRCRAFT && ot == OT_GOTO_DEPOT)) ot = OT_GOTO_STATION;
01627 if (ot == type && order->GetDestination() == destination) {
01628
01629
01630
01631 if (order->IsType(OT_AUTOMATIC)) {
01632 DeleteOrder(v, id);
01633 id--;
01634 continue;
01635 }
01636
01637 order->MakeDummy();
01638 for (const Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
01639
01640 InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
01641 InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
01642 }
01643 }
01644 }
01645 }
01646 }
01647
01652 bool Vehicle::HasDepotOrder() const
01653 {
01654 const Order *order;
01655
01656 FOR_VEHICLE_ORDERS(this, order) {
01657 if (order->IsType(OT_GOTO_DEPOT)) return true;
01658 }
01659
01660 return false;
01661 }
01662
01668 void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist)
01669 {
01670 DeleteOrderWarnings(v);
01671
01672 if (v->IsOrderListShared()) {
01673
01674 v->RemoveFromShared();
01675 v->orders.list = NULL;
01676 } else if (v->orders.list != NULL) {
01677
01678 v->orders.list->FreeChain(keep_orderlist);
01679 if (!keep_orderlist) v->orders.list = NULL;
01680 }
01681 }
01682
01690 uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id)
01691 {
01692 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);
01693 }
01694
01703 static bool CheckForValidOrders(const Vehicle *v)
01704 {
01705 const Order *order;
01706
01707 FOR_VEHICLE_ORDERS(v, order) {
01708 switch (order->GetType()) {
01709 case OT_GOTO_STATION:
01710 case OT_GOTO_DEPOT:
01711 case OT_GOTO_WAYPOINT:
01712 return true;
01713
01714 default:
01715 break;
01716 }
01717 }
01718
01719 return false;
01720 }
01721
01725 static bool OrderConditionCompare(OrderConditionComparator occ, int variable, int value)
01726 {
01727 switch (occ) {
01728 case OCC_EQUALS: return variable == value;
01729 case OCC_NOT_EQUALS: return variable != value;
01730 case OCC_LESS_THAN: return variable < value;
01731 case OCC_LESS_EQUALS: return variable <= value;
01732 case OCC_MORE_THAN: return variable > value;
01733 case OCC_MORE_EQUALS: return variable >= value;
01734 case OCC_IS_TRUE: return variable != 0;
01735 case OCC_IS_FALSE: return variable == 0;
01736 default: NOT_REACHED();
01737 }
01738 }
01739
01746 VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
01747 {
01748 if (order->GetType() != OT_CONDITIONAL) return INVALID_VEH_ORDER_ID;
01749
01750 bool skip_order = false;
01751 OrderConditionComparator occ = order->GetConditionComparator();
01752 uint16 value = order->GetConditionValue();
01753
01754 switch (order->GetConditionVariable()) {
01755 case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break;
01756 case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break;
01757 case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break;
01758 case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break;
01759 case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break;
01760 case OCV_UNCONDITIONALLY: skip_order = true; break;
01761 default: NOT_REACHED();
01762 }
01763
01764 return skip_order ? order->GetConditionSkipToOrder() : (VehicleOrderID)INVALID_VEH_ORDER_ID;
01765 }
01766
01773 bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth)
01774 {
01775 if (conditional_depth > v->GetNumOrders()) return false;
01776
01777 switch (order->GetType()) {
01778 case OT_GOTO_STATION:
01779 v->dest_tile = v->GetOrderStationLocation(order->GetDestination());
01780 return true;
01781
01782 case OT_GOTO_DEPOT:
01783 if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !v->NeedsServicing()) {
01784 UpdateVehicleTimetable(v, true);
01785 v->IncrementRealOrderIndex();
01786 break;
01787 }
01788
01789 if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
01790
01791 TileIndex location;
01792 DestinationID destination;
01793 bool reverse;
01794
01795 if (v->FindClosestDepot(&location, &destination, &reverse)) {
01796 v->dest_tile = location;
01797 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());
01798
01799
01800 if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
01801
01802 if (v->type == VEH_AIRCRAFT) {
01803 Aircraft *a = Aircraft::From(v);
01804 if (a->state == FLYING && a->targetairport != destination) {
01805
01806 extern void AircraftNextAirportPos_and_Order(Aircraft *a);
01807 AircraftNextAirportPos_and_Order(a);
01808 }
01809 }
01810 return true;
01811 }
01812
01813 UpdateVehicleTimetable(v, true);
01814 v->IncrementRealOrderIndex();
01815 } else {
01816 if (v->type != VEH_AIRCRAFT) {
01817 v->dest_tile = Depot::Get(order->GetDestination())->xy;
01818 }
01819 return true;
01820 }
01821 break;
01822
01823 case OT_GOTO_WAYPOINT:
01824 v->dest_tile = Waypoint::Get(order->GetDestination())->xy;
01825 return true;
01826
01827 case OT_CONDITIONAL: {
01828 VehicleOrderID next_order = ProcessConditionalOrder(order, v);
01829 if (next_order != INVALID_VEH_ORDER_ID) {
01830
01831
01832 UpdateVehicleTimetable(v, false);
01833 v->cur_auto_order_index = v->cur_real_order_index = next_order;
01834 v->UpdateRealOrderIndex();
01835 v->current_order_time += v->GetOrder(v->cur_real_order_index)->travel_time;
01836 } else {
01837 UpdateVehicleTimetable(v, true);
01838 v->IncrementRealOrderIndex();
01839 }
01840 break;
01841 }
01842
01843 default:
01844 v->dest_tile = 0;
01845 return false;
01846 }
01847
01848 assert(v->cur_auto_order_index < v->GetNumOrders());
01849 assert(v->cur_real_order_index < v->GetNumOrders());
01850
01851
01852 order = v->GetOrder(v->cur_real_order_index);
01853 if (order != NULL && order->IsType(OT_AUTOMATIC)) {
01854 assert(v->GetNumManualOrders() == 0);
01855 order = NULL;
01856 }
01857
01858 if (order == NULL) {
01859 v->current_order.Free();
01860 v->dest_tile = 0;
01861 return false;
01862 }
01863
01864 v->current_order = *order;
01865 return UpdateOrderDest(v, order, conditional_depth + 1);
01866 }
01867
01875 bool ProcessOrders(Vehicle *v)
01876 {
01877 switch (v->current_order.GetType()) {
01878 case OT_GOTO_DEPOT:
01879
01880 if (!(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return false;
01881 break;
01882
01883 case OT_LOADING:
01884 return false;
01885
01886 case OT_LEAVESTATION:
01887 if (v->type != VEH_AIRCRAFT) return false;
01888 break;
01889
01890 default: break;
01891 }
01892
01900 bool may_reverse = v->current_order.IsType(OT_NOTHING);
01901
01902
01903 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)) &&
01904 IsTileType(v->tile, MP_STATION) &&
01905 v->current_order.GetDestination() == GetStationIndex(v->tile)) {
01906 v->DeleteUnreachedAutoOrders();
01907
01908
01909
01910
01911 v->last_station_visited = v->current_order.GetDestination();
01912 UpdateVehicleTimetable(v, true);
01913 v->IncrementAutoOrderIndex();
01914 }
01915
01916
01917 v->UpdateRealOrderIndex();
01918
01919 const Order *order = v->GetOrder(v->cur_real_order_index);
01920 if (order != NULL && order->IsType(OT_AUTOMATIC)) {
01921 assert(v->GetNumManualOrders() == 0);
01922 order = NULL;
01923 }
01924
01925
01926 if (order == NULL || (v->type == VEH_AIRCRAFT && !CheckForValidOrders(v))) {
01927 if (v->type == VEH_AIRCRAFT) {
01928
01929 extern void HandleMissingAircraftOrders(Aircraft *v);
01930 HandleMissingAircraftOrders(Aircraft::From(v));
01931 return false;
01932 }
01933
01934 v->current_order.Free();
01935 v->dest_tile = 0;
01936 return false;
01937 }
01938
01939
01940 if (order->Equals(v->current_order) && (v->type == VEH_AIRCRAFT || v->dest_tile != 0) &&
01941 (v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || Station::Get(order->GetDestination())->dock_tile != INVALID_TILE)) {
01942 return false;
01943 }
01944
01945
01946 v->current_order = *order;
01947
01948 InvalidateVehicleOrder(v, -2);
01949 switch (v->type) {
01950 default:
01951 NOT_REACHED();
01952
01953 case VEH_ROAD:
01954 case VEH_TRAIN:
01955 break;
01956
01957 case VEH_AIRCRAFT:
01958 case VEH_SHIP:
01959 SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
01960 break;
01961 }
01962
01963 return UpdateOrderDest(v, order) && may_reverse;
01964 }
01965
01973 bool Order::ShouldStopAtStation(const Vehicle *v, StationID station) const
01974 {
01975 bool is_dest_station = this->IsType(OT_GOTO_STATION) && this->dest == station;
01976
01977 return (!this->IsType(OT_GOTO_DEPOT) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0) &&
01978 v->last_station_visited != station &&
01979
01980 !(this->GetNonStopType() & (is_dest_station ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));
01981 }
01982
01983 void InitializeOrders()
01984 {
01985 _order_pool.CleanPool();
01986 _orderlist_pool.CleanPool();
01987 }