2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file order_cmd.cpp Handling of orders. */
|
2007-03-21 15:19:33 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2009-01-03 13:52:06 +00:00
|
|
|
#include "debug.h"
|
2010-04-17 13:31:41 +00:00
|
|
|
#include "cmd_helper.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2005-05-02 23:59:11 +00:00
|
|
|
#include "vehicle_gui.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
2008-04-05 10:55:50 +00:00
|
|
|
#include "timetable.h"
|
|
|
|
#include "vehicle_func.h"
|
2008-04-17 19:10:30 +00:00
|
|
|
#include "depot_base.h"
|
2009-05-22 15:13:50 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2009-05-22 20:03:26 +00:00
|
|
|
#include "aircraft.h"
|
2009-06-06 16:54:22 +00:00
|
|
|
#include "roadveh.h"
|
2009-06-24 17:39:54 +00:00
|
|
|
#include "station_base.h"
|
2009-07-22 10:18:19 +00:00
|
|
|
#include "waypoint_base.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "company_base.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2008-03-28 16:31:26 +00:00
|
|
|
/* DestinationID must be at least as large as every these below, because it can
|
|
|
|
* be any of them
|
|
|
|
*/
|
|
|
|
assert_compile(sizeof(DestinationID) >= sizeof(DepotID));
|
|
|
|
assert_compile(sizeof(DestinationID) >= sizeof(StationID));
|
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
OrderPool _order_pool("Order");
|
|
|
|
INSTANTIATE_POOL_METHODS(Order)
|
|
|
|
OrderListPool _orderlist_pool("OrderList");
|
|
|
|
INSTANTIATE_POOL_METHODS(OrderList)
|
2008-04-07 08:59:04 +00:00
|
|
|
|
2008-04-05 20:57:01 +00:00
|
|
|
void Order::Free()
|
|
|
|
{
|
|
|
|
this->type = OT_NOTHING;
|
|
|
|
this->flags = 0;
|
|
|
|
this->dest = 0;
|
|
|
|
this->next = NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-05 23:36:54 +00:00
|
|
|
void Order::MakeGoToStation(StationID destination)
|
|
|
|
{
|
|
|
|
this->type = OT_GOTO_STATION;
|
|
|
|
this->flags = 0;
|
|
|
|
this->dest = destination;
|
|
|
|
}
|
|
|
|
|
2009-05-02 00:10:24 +00:00
|
|
|
void Order::MakeGoToDepot(DepotID destination, OrderDepotTypeFlags order, OrderNonStopFlags non_stop_type, OrderDepotActionFlags action, CargoID cargo, byte subtype)
|
2008-04-05 23:36:54 +00:00
|
|
|
{
|
|
|
|
this->type = OT_GOTO_DEPOT;
|
2008-04-07 19:18:56 +00:00
|
|
|
this->SetDepotOrderType(order);
|
2008-11-21 18:16:19 +00:00
|
|
|
this->SetDepotActionType(action);
|
2009-05-02 00:10:24 +00:00
|
|
|
this->SetNonStopType(non_stop_type);
|
2008-04-05 23:36:54 +00:00
|
|
|
this->dest = destination;
|
2008-04-06 07:07:21 +00:00
|
|
|
this->SetRefit(cargo, subtype);
|
2008-04-05 23:36:54 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 21:06:38 +00:00
|
|
|
void Order::MakeGoToWaypoint(StationID destination)
|
2008-04-05 23:36:54 +00:00
|
|
|
{
|
|
|
|
this->type = OT_GOTO_WAYPOINT;
|
|
|
|
this->flags = 0;
|
|
|
|
this->dest = destination;
|
|
|
|
}
|
|
|
|
|
2008-04-06 15:09:45 +00:00
|
|
|
void Order::MakeLoading(bool ordered)
|
2008-04-05 23:36:54 +00:00
|
|
|
{
|
|
|
|
this->type = OT_LOADING;
|
2008-04-06 15:09:45 +00:00
|
|
|
if (!ordered) this->flags = 0;
|
2008-04-05 23:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Order::MakeLeaveStation()
|
|
|
|
{
|
|
|
|
this->type = OT_LEAVESTATION;
|
|
|
|
this->flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Order::MakeDummy()
|
|
|
|
{
|
|
|
|
this->type = OT_DUMMY;
|
|
|
|
this->flags = 0;
|
|
|
|
}
|
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
void Order::MakeConditional(VehicleOrderID order)
|
|
|
|
{
|
|
|
|
this->type = OT_CONDITIONAL;
|
2008-04-12 12:19:53 +00:00
|
|
|
this->flags = order;
|
|
|
|
this->dest = 0;
|
2008-04-12 11:58:19 +00:00
|
|
|
}
|
|
|
|
|
2008-04-06 07:07:21 +00:00
|
|
|
void Order::SetRefit(CargoID cargo, byte subtype)
|
|
|
|
{
|
|
|
|
this->refit_cargo = cargo;
|
|
|
|
this->refit_subtype = subtype;
|
|
|
|
}
|
|
|
|
|
2008-04-05 21:45:05 +00:00
|
|
|
bool Order::Equals(const Order &other) const
|
|
|
|
{
|
2008-11-23 21:46:27 +00:00
|
|
|
/* In case of go to nearest depot orders we need "only" compare the flags
|
|
|
|
* with the other and not the nearest depot order bit or the actual
|
|
|
|
* destination because those get clear/filled in during the order
|
|
|
|
* evaluation. If we do not do this the order will continuously be seen as
|
|
|
|
* a different order and it will try to find a "nearest depot" every tick. */
|
2010-08-15 13:17:04 +00:00
|
|
|
if ((this->IsType(OT_GOTO_DEPOT) && this->type == other.type) &&
|
2008-11-23 21:46:27 +00:00
|
|
|
((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0 ||
|
|
|
|
(other.GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0)) {
|
|
|
|
return
|
|
|
|
this->GetDepotOrderType() == other.GetDepotOrderType() &&
|
|
|
|
(this->GetDepotActionType() & ~ODATFB_NEAREST_DEPOT) == (other.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT);
|
|
|
|
}
|
|
|
|
|
2008-04-05 21:45:05 +00:00
|
|
|
return
|
|
|
|
this->type == other.type &&
|
|
|
|
this->flags == other.flags &&
|
|
|
|
this->dest == other.dest;
|
|
|
|
}
|
|
|
|
|
2008-04-06 07:22:26 +00:00
|
|
|
uint32 Order::Pack() const
|
2008-04-05 20:57:01 +00:00
|
|
|
{
|
2008-04-06 07:22:26 +00:00
|
|
|
return this->dest << 16 | this->flags << 8 | this->type;
|
2008-04-05 20:57:01 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 10:11:04 +00:00
|
|
|
uint16 Order::MapOldOrder() const
|
|
|
|
{
|
|
|
|
uint16 order = this->GetType();
|
|
|
|
switch (this->type) {
|
|
|
|
case OT_GOTO_STATION:
|
|
|
|
if (this->GetUnloadType() & OUFB_UNLOAD) SetBit(order, 5);
|
|
|
|
if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
|
|
|
|
if (this->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) SetBit(order, 7);
|
|
|
|
order |= GB(this->GetDestination(), 0, 8) << 8;
|
|
|
|
break;
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
|
if (!(this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) SetBit(order, 6);
|
|
|
|
SetBit(order, 7);
|
|
|
|
order |= GB(this->GetDestination(), 0, 8) << 8;
|
|
|
|
break;
|
|
|
|
case OT_LOADING:
|
|
|
|
if (this->GetLoadType() & OLFB_FULL_LOAD) SetBit(order, 6);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return order;
|
|
|
|
}
|
|
|
|
|
2008-04-06 07:22:26 +00:00
|
|
|
Order::Order(uint32 packed)
|
2008-04-05 20:57:01 +00:00
|
|
|
{
|
2008-04-06 07:22:26 +00:00
|
|
|
this->type = (OrderType)GB(packed, 0, 8);
|
|
|
|
this->flags = GB(packed, 8, 8);
|
|
|
|
this->dest = GB(packed, 16, 16);
|
|
|
|
this->next = NULL;
|
|
|
|
this->refit_cargo = CT_NO_REFIT;
|
|
|
|
this->refit_subtype = 0;
|
|
|
|
this->wait_time = 0;
|
|
|
|
this->travel_time = 0;
|
2008-04-05 20:57:01 +00:00
|
|
|
}
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Updates the widgets of a vehicle which contains the order-data
|
|
|
|
*
|
|
|
|
*/
|
2008-09-24 16:40:06 +00:00
|
|
|
void InvalidateVehicleOrder(const Vehicle *v, int data)
|
2005-01-15 19:06:22 +00:00
|
|
|
{
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
|
2008-09-24 16:40:06 +00:00
|
|
|
|
|
|
|
if (data != 0) {
|
|
|
|
/* Calls SetDirty() too */
|
|
|
|
InvalidateWindowData(WC_VEHICLE_ORDERS, v->index, data);
|
|
|
|
InvalidateWindowData(WC_VEHICLE_TIMETABLE, v->index, data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_ORDERS, v->index);
|
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2010-01-01 18:45:40 +00:00
|
|
|
* Assign data to an order (from another order)
|
2005-01-15 19:06:22 +00:00
|
|
|
* This function makes sure that the index is maintained correctly
|
|
|
|
*
|
|
|
|
*/
|
2008-04-05 21:45:05 +00:00
|
|
|
void Order::AssignOrder(const Order &other)
|
2005-01-15 19:06:22 +00:00
|
|
|
{
|
2008-04-05 21:45:05 +00:00
|
|
|
this->type = other.type;
|
|
|
|
this->flags = other.flags;
|
|
|
|
this->dest = other.dest;
|
2006-10-03 14:52:39 +00:00
|
|
|
|
2008-04-05 21:45:05 +00:00
|
|
|
this->refit_cargo = other.refit_cargo;
|
|
|
|
this->refit_subtype = other.refit_subtype;
|
2007-06-20 19:17:22 +00:00
|
|
|
|
2008-04-05 21:45:05 +00:00
|
|
|
this->wait_time = other.wait_time;
|
|
|
|
this->travel_time = other.travel_time;
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
void OrderList::Initialize(Order *chain, Vehicle *v)
|
2009-01-03 13:52:06 +00:00
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
this->first = chain;
|
|
|
|
this->first_shared = v;
|
|
|
|
|
|
|
|
this->num_orders = 0;
|
|
|
|
this->num_vehicles = 1;
|
|
|
|
this->timetable_duration = 0;
|
|
|
|
|
2009-01-03 13:52:06 +00:00
|
|
|
for (Order *o = this->first; o != NULL; o = o->next) {
|
|
|
|
++this->num_orders;
|
|
|
|
this->timetable_duration += o->wait_time + o->travel_time;
|
|
|
|
}
|
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
for (Vehicle *u = this->first_shared->PreviousShared(); u != NULL; u = u->PreviousShared()) {
|
2009-01-03 13:52:06 +00:00
|
|
|
++this->num_vehicles;
|
|
|
|
this->first_shared = u;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const Vehicle *u = v->NextShared(); u != NULL; u = u->NextShared()) ++this->num_vehicles;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OrderList::FreeChain(bool keep_orderlist)
|
|
|
|
{
|
|
|
|
Order *next;
|
2009-05-22 15:13:50 +00:00
|
|
|
for (Order *o = this->first; o != NULL; o = next) {
|
2009-01-03 13:52:06 +00:00
|
|
|
next = o->next;
|
|
|
|
delete o;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keep_orderlist) {
|
|
|
|
this->first = NULL;
|
|
|
|
this->num_orders = 0;
|
|
|
|
this->timetable_duration = 0;
|
|
|
|
} else {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Order *OrderList::GetOrderAt(int index) const
|
|
|
|
{
|
|
|
|
if (index < 0) return NULL;
|
|
|
|
|
|
|
|
Order *order = this->first;
|
|
|
|
|
2010-07-24 10:14:39 +00:00
|
|
|
while (order != NULL && index-- > 0) {
|
2009-01-03 13:52:06 +00:00
|
|
|
order = order->next;
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2009-01-03 13:52:06 +00:00
|
|
|
return order;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OrderList::InsertOrderAt(Order *new_order, int index)
|
|
|
|
{
|
|
|
|
if (this->first == NULL) {
|
|
|
|
this->first = new_order;
|
|
|
|
} else {
|
|
|
|
if (index == 0) {
|
|
|
|
/* Insert as first or only order */
|
|
|
|
new_order->next = this->first;
|
|
|
|
this->first = new_order;
|
|
|
|
} else if (index >= this->num_orders) {
|
|
|
|
/* index is after the last order, add it to the end */
|
|
|
|
this->GetLastOrder()->next = new_order;
|
|
|
|
} else {
|
|
|
|
/* Put the new order in between */
|
|
|
|
Order *order = this->GetOrderAt(index - 1);
|
|
|
|
new_order->next = order->next;
|
|
|
|
order->next = new_order;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++this->num_orders;
|
|
|
|
this->timetable_duration += new_order->wait_time + new_order->travel_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OrderList::DeleteOrderAt(int index)
|
|
|
|
{
|
|
|
|
if (index >= this->num_orders) return;
|
|
|
|
|
|
|
|
Order *to_remove;
|
|
|
|
|
|
|
|
if (index == 0) {
|
|
|
|
to_remove = this->first;
|
|
|
|
this->first = to_remove->next;
|
|
|
|
} else {
|
|
|
|
Order *prev = GetOrderAt(index - 1);
|
|
|
|
to_remove = prev->next;
|
|
|
|
prev->next = to_remove->next;
|
|
|
|
}
|
|
|
|
--this->num_orders;
|
|
|
|
this->timetable_duration -= (to_remove->wait_time + to_remove->travel_time);
|
|
|
|
delete to_remove;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OrderList::MoveOrder(int from, int to)
|
|
|
|
{
|
|
|
|
if (from >= this->num_orders || to >= this->num_orders || from == to) return;
|
|
|
|
|
|
|
|
Order *moving_one;
|
|
|
|
|
|
|
|
/* Take the moving order out of the pointer-chain */
|
|
|
|
if (from == 0) {
|
|
|
|
moving_one = this->first;
|
|
|
|
this->first = moving_one->next;
|
|
|
|
} else {
|
|
|
|
Order *one_before = GetOrderAt(from - 1);
|
|
|
|
moving_one = one_before->next;
|
|
|
|
one_before->next = moving_one->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert the moving_order again in the pointer-chain */
|
|
|
|
if (to == 0) {
|
|
|
|
moving_one->next = this->first;
|
|
|
|
this->first = moving_one;
|
|
|
|
} else {
|
|
|
|
Order *one_before = GetOrderAt(to - 1);
|
|
|
|
moving_one->next = one_before->next;
|
|
|
|
one_before->next = moving_one;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OrderList::RemoveVehicle(Vehicle *v)
|
|
|
|
{
|
|
|
|
--this->num_vehicles;
|
|
|
|
if (v == this->first_shared) this->first_shared = v->NextShared();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OrderList::IsVehicleInSharedOrdersList(const Vehicle *v) const
|
|
|
|
{
|
|
|
|
for (const Vehicle *v_shared = this->first_shared; v_shared != NULL; v_shared = v_shared->NextShared()) {
|
|
|
|
if (v_shared == v) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int OrderList::GetPositionInSharedOrderList(const Vehicle *v) const
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
for (const Vehicle *v_shared = v->PreviousShared(); v_shared != NULL; v_shared = v_shared->PreviousShared()) count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OrderList::IsCompleteTimetable() const
|
|
|
|
{
|
|
|
|
for (Order *o = this->first; o != NULL; o = o->next) {
|
|
|
|
if (!o->IsCompletelyTimetabled()) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OrderList::DebugCheckSanity() const
|
|
|
|
{
|
|
|
|
VehicleOrderID check_num_orders = 0;
|
2009-01-03 17:28:22 +00:00
|
|
|
uint check_num_vehicles = 0;
|
2009-11-21 12:43:09 +00:00
|
|
|
Ticks check_timetable_duration = 0;
|
2009-01-03 13:52:06 +00:00
|
|
|
|
|
|
|
DEBUG(misc, 6, "Checking OrderList %hu for sanity...", this->index);
|
|
|
|
|
|
|
|
for (const Order *o = this->first; o != NULL; o = o->next) {
|
|
|
|
++check_num_orders;
|
|
|
|
check_timetable_duration += o->wait_time + o->travel_time;
|
|
|
|
}
|
|
|
|
assert(this->num_orders == check_num_orders);
|
|
|
|
assert(this->timetable_duration == check_timetable_duration);
|
|
|
|
|
|
|
|
for (const Vehicle *v = this->first_shared; v != NULL; v = v->NextShared()) {
|
|
|
|
++check_num_vehicles;
|
|
|
|
assert(v->orders.list == this);
|
|
|
|
}
|
|
|
|
assert(this->num_vehicles == check_num_vehicles);
|
2009-11-21 12:43:09 +00:00
|
|
|
DEBUG(misc, 6, "... detected %u orders, %u vehicles, %i ticks", (uint)this->num_orders,
|
2009-01-03 13:52:06 +00:00
|
|
|
this->num_vehicles, this->timetable_duration);
|
|
|
|
}
|
|
|
|
|
2009-01-10 13:00:38 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the order goes to a station or not, i.e. whether the
|
|
|
|
* destination is a station
|
|
|
|
* @param v the vehicle to check for
|
|
|
|
* @param o the order to check
|
|
|
|
* @return true if the destination is a station
|
|
|
|
*/
|
|
|
|
static inline bool OrderGoesToStation(const Vehicle *v, const Order *o)
|
|
|
|
{
|
|
|
|
return o->IsType(OT_GOTO_STATION) ||
|
|
|
|
(v->type == VEH_AIRCRAFT && o->IsType(OT_GOTO_DEPOT) && !(o->GetDepotActionType() & ODATFB_NEAREST_DEPOT));
|
|
|
|
}
|
|
|
|
|
2006-03-04 11:01:35 +00:00
|
|
|
/**
|
|
|
|
* Delete all news items regarding defective orders about a vehicle
|
|
|
|
* This could kill still valid warnings (for example about void order when just
|
2008-09-30 20:39:50 +00:00
|
|
|
* another order gets added), but assume the company will notice the problems,
|
2006-03-04 11:01:35 +00:00
|
|
|
* when (s)he's changing the orders.
|
|
|
|
*/
|
2009-01-10 00:31:47 +00:00
|
|
|
static void DeleteOrderWarnings(const Vehicle *v)
|
2006-03-04 11:01:35 +00:00
|
|
|
{
|
2009-08-04 18:04:33 +00:00
|
|
|
DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS);
|
|
|
|
DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_VOID_ORDER);
|
|
|
|
DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY);
|
|
|
|
DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY);
|
2006-03-04 11:01:35 +00:00
|
|
|
}
|
|
|
|
|
2010-02-22 20:36:20 +00:00
|
|
|
/**
|
|
|
|
* Returns a tile somewhat representing the order destination (not suitable for pathfinding).
|
|
|
|
* @param v The vehicle to get the location for.
|
|
|
|
* @return destination of order, or INVALID_TILE if none.
|
|
|
|
*/
|
|
|
|
TileIndex Order::GetLocation(const Vehicle *v) const
|
2007-02-18 16:24:29 +00:00
|
|
|
{
|
2010-02-22 20:36:20 +00:00
|
|
|
switch (this->GetType()) {
|
|
|
|
case OT_GOTO_WAYPOINT:
|
|
|
|
case OT_GOTO_STATION:
|
|
|
|
return BaseStation::Get(this->GetDestination())->xy;
|
|
|
|
|
2010-02-22 20:08:16 +00:00
|
|
|
case OT_GOTO_DEPOT:
|
2010-02-22 20:36:20 +00:00
|
|
|
if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) return INVALID_TILE;
|
|
|
|
return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination())->xy : Depot::Get(this->GetDestination())->xy;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return INVALID_TILE;
|
2007-02-18 16:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-30 14:49:50 +00:00
|
|
|
static uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle *v, int conditional_depth = 0)
|
|
|
|
{
|
2009-07-10 18:30:02 +00:00
|
|
|
assert(v->type == VEH_SHIP);
|
|
|
|
|
2008-06-30 14:49:50 +00:00
|
|
|
if (cur->IsType(OT_CONDITIONAL)) {
|
2009-01-03 13:20:32 +00:00
|
|
|
if (conditional_depth > v->GetNumOrders()) return 0;
|
2008-06-30 14:49:50 +00:00
|
|
|
|
|
|
|
conditional_depth++;
|
2008-09-14 08:47:18 +00:00
|
|
|
|
2009-05-23 12:27:42 +00:00
|
|
|
int dist1 = GetOrderDistance(prev, v->GetOrder(cur->GetConditionSkipToOrder()), v, conditional_depth);
|
2009-01-03 13:52:06 +00:00
|
|
|
int dist2 = GetOrderDistance(prev, cur->next == NULL ? v->orders.list->GetFirstOrder() : cur->next, v, conditional_depth);
|
2008-09-14 08:47:18 +00:00
|
|
|
return max(dist1, dist2);
|
2008-06-30 14:49:50 +00:00
|
|
|
}
|
|
|
|
|
2010-02-22 20:36:20 +00:00
|
|
|
TileIndex prev_tile = prev->GetLocation(v);
|
|
|
|
TileIndex cur_tile = cur->GetLocation(v);
|
2010-02-22 20:08:16 +00:00
|
|
|
if (prev_tile == INVALID_TILE || cur_tile == INVALID_TILE) return 0;
|
|
|
|
return DistanceManhattan(prev_tile, cur_tile);
|
2008-06-30 14:49:50 +00:00
|
|
|
}
|
2007-02-18 16:24:29 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Add an order to the orderlist of a vehicle.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p1 various bitstuffed elements
|
2010-08-19 20:39:12 +00:00
|
|
|
* - p1 = (bit 0 - 19) - ID of the vehicle
|
|
|
|
* - p1 = (bit 24 - 31) - the selected order (if any). If the last order is given,
|
2005-07-20 15:29:28 +00:00
|
|
|
* the order will be inserted before that one
|
2009-02-04 13:52:17 +00:00
|
|
|
* the maximum vehicle order id is 254.
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p2 packed order to insert
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-01-15 19:06:22 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2005-01-15 19:06:22 +00:00
|
|
|
{
|
2010-08-19 20:39:12 +00:00
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
|
|
|
VehicleOrderID sel_ord = GB(p1, 20, 8);
|
2008-04-06 07:22:26 +00:00
|
|
|
Order new_order(p2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
2010-04-17 14:55:49 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
/* Check if the inserted order is to the correct destination (owner, type),
|
|
|
|
* and has the correct flags if any */
|
2008-04-05 23:36:54 +00:00
|
|
|
switch (new_order.GetType()) {
|
2005-04-22 05:41:09 +00:00
|
|
|
case OT_GOTO_STATION: {
|
2009-05-18 16:21:28 +00:00
|
|
|
const Station *st = Station::GetIfValid(new_order.GetDestination());
|
|
|
|
if (st == NULL) return CMD_ERROR;
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2010-03-13 17:11:28 +00:00
|
|
|
if (st->owner != OWNER_NONE) {
|
|
|
|
CommandCost ret = CheckOwnership(st->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
}
|
2009-01-10 13:00:38 +00:00
|
|
|
|
2009-04-21 23:40:56 +00:00
|
|
|
if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
|
2009-01-10 13:00:38 +00:00
|
|
|
for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
|
2009-04-21 23:40:56 +00:00
|
|
|
if (!CanVehicleUseStation(u, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED);
|
2009-01-10 13:00:38 +00:00
|
|
|
}
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2008-04-07 20:18:51 +00:00
|
|
|
/* Non stop not allowed for non-trains. */
|
2008-04-13 16:54:19 +00:00
|
|
|
if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN && v->type != VEH_ROAD) return CMD_ERROR;
|
2008-04-06 21:54:22 +00:00
|
|
|
|
2008-07-20 07:41:43 +00:00
|
|
|
/* No load and no unload are mutual exclusive. */
|
|
|
|
if ((new_order.GetLoadType() & OLFB_NO_LOAD) && (new_order.GetUnloadType() & OUFB_NO_UNLOAD)) return CMD_ERROR;
|
2005-09-16 16:03:18 +00:00
|
|
|
|
2008-04-07 20:18:51 +00:00
|
|
|
/* Filter invalid load/unload types. */
|
2008-04-09 18:26:19 +00:00
|
|
|
switch (new_order.GetLoadType()) {
|
2008-05-04 22:57:50 +00:00
|
|
|
case OLF_LOAD_IF_POSSIBLE: case OLFB_FULL_LOAD: case OLF_FULL_LOAD_ANY: case OLFB_NO_LOAD: break;
|
|
|
|
default: return CMD_ERROR;
|
|
|
|
}
|
|
|
|
switch (new_order.GetUnloadType()) {
|
|
|
|
case OUF_UNLOAD_IF_POSSIBLE: case OUFB_UNLOAD: case OUFB_TRANSFER: case OUFB_NO_UNLOAD: break;
|
2008-04-09 18:26:19 +00:00
|
|
|
default: return CMD_ERROR;
|
|
|
|
}
|
2009-04-12 14:11:14 +00:00
|
|
|
|
|
|
|
/* Filter invalid stop locations */
|
|
|
|
switch (new_order.GetStopLocation()) {
|
|
|
|
case OSL_PLATFORM_NEAR_END:
|
|
|
|
case OSL_PLATFORM_MIDDLE:
|
|
|
|
if (v->type != VEH_TRAIN) return CMD_ERROR;
|
|
|
|
/* FALL THROUGH */
|
|
|
|
case OSL_PLATFORM_FAR_END:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2005-04-22 05:41:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OT_GOTO_DEPOT: {
|
2010-08-10 16:45:32 +00:00
|
|
|
if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
|
2008-04-11 15:58:46 +00:00
|
|
|
if (v->type == VEH_AIRCRAFT) {
|
2009-05-18 16:21:28 +00:00
|
|
|
const Station *st = Station::GetIfValid(new_order.GetDestination());
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2010-03-13 17:11:28 +00:00
|
|
|
if (st == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(st->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
2010-03-19 09:48:44 +00:00
|
|
|
if (!CanVehicleUseStation(v, st) || !st->airport.HasHangar()) {
|
2008-04-11 15:58:46 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
2009-05-18 16:21:28 +00:00
|
|
|
const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2010-03-13 17:11:28 +00:00
|
|
|
if (dp == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(GetTileOwner(dp->xy));
|
|
|
|
if (ret.Failed()) return ret;
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2008-04-11 15:58:46 +00:00
|
|
|
switch (v->type) {
|
|
|
|
case VEH_TRAIN:
|
2008-04-17 18:24:45 +00:00
|
|
|
if (!IsRailDepotTile(dp->xy)) return CMD_ERROR;
|
2008-04-11 15:58:46 +00:00
|
|
|
break;
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2008-04-11 15:58:46 +00:00
|
|
|
case VEH_ROAD:
|
2008-04-17 18:24:45 +00:00
|
|
|
if (!IsRoadDepotTile(dp->xy)) return CMD_ERROR;
|
2008-04-11 15:58:46 +00:00
|
|
|
break;
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2008-04-11 15:58:46 +00:00
|
|
|
case VEH_SHIP:
|
2008-04-17 18:24:45 +00:00
|
|
|
if (!IsShipDepotTile(dp->xy)) return CMD_ERROR;
|
2008-04-11 15:58:46 +00:00
|
|
|
break;
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2008-04-11 15:58:46 +00:00
|
|
|
default: return CMD_ERROR;
|
|
|
|
}
|
2005-04-22 05:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-13 16:54:19 +00:00
|
|
|
if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN && v->type != VEH_ROAD) return CMD_ERROR;
|
2008-06-25 19:33:11 +00:00
|
|
|
if (new_order.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS | ((new_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0 ? ODTFB_SERVICE : 0))) return CMD_ERROR;
|
2009-04-20 23:49:27 +00:00
|
|
|
if (new_order.GetDepotActionType() & ~(ODATFB_HALT | ODATFB_NEAREST_DEPOT)) return CMD_ERROR;
|
|
|
|
if ((new_order.GetDepotOrderType() & ODTFB_SERVICE) && (new_order.GetDepotActionType() & ODATFB_HALT)) return CMD_ERROR;
|
2005-04-22 05:41:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OT_GOTO_WAYPOINT: {
|
2009-07-22 08:59:57 +00:00
|
|
|
const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
|
|
|
|
if (wp == NULL) return CMD_ERROR;
|
|
|
|
|
2009-07-10 18:30:02 +00:00
|
|
|
switch (v->type) {
|
|
|
|
default: return CMD_ERROR;
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2010-03-13 17:11:28 +00:00
|
|
|
case VEH_TRAIN: {
|
2009-12-05 19:21:43 +00:00
|
|
|
if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(wp->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2009-07-22 08:59:57 +00:00
|
|
|
break;
|
2010-03-13 17:11:28 +00:00
|
|
|
}
|
2009-07-10 18:30:02 +00:00
|
|
|
|
2009-07-22 08:59:57 +00:00
|
|
|
case VEH_SHIP:
|
2009-12-05 19:21:43 +00:00
|
|
|
if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (wp->owner != OWNER_NONE) {
|
|
|
|
CommandCost ret = CheckOwnership(wp->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
}
|
2009-07-22 08:59:57 +00:00
|
|
|
break;
|
2009-07-10 18:30:02 +00:00
|
|
|
}
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2005-09-16 16:03:18 +00:00
|
|
|
/* Order flags can be any of the following for waypoints:
|
|
|
|
* [non-stop]
|
|
|
|
* non-stop orders (if any) are only valid for trains */
|
2008-04-10 08:30:15 +00:00
|
|
|
if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
|
2008-04-08 16:15:31 +00:00
|
|
|
break;
|
2005-04-22 05:41:09 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
case OT_CONDITIONAL: {
|
|
|
|
VehicleOrderID skip_to = new_order.GetConditionSkipToOrder();
|
2009-01-03 13:20:32 +00:00
|
|
|
if (skip_to != 0 && skip_to >= v->GetNumOrders()) return CMD_ERROR; // Always allow jumping to the first (even when there is no order).
|
2008-07-20 12:59:27 +00:00
|
|
|
if (new_order.GetConditionVariable() > OCV_END) return CMD_ERROR;
|
2008-07-20 07:41:43 +00:00
|
|
|
|
|
|
|
OrderConditionComparator occ = new_order.GetConditionComparator();
|
2008-07-20 12:59:27 +00:00
|
|
|
if (occ > OCC_END) return CMD_ERROR;
|
2008-07-20 07:41:43 +00:00
|
|
|
switch (new_order.GetConditionVariable()) {
|
|
|
|
case OCV_REQUIRES_SERVICE:
|
2008-07-20 12:59:27 +00:00
|
|
|
if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
|
2008-07-20 07:41:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OCV_UNCONDITIONALLY:
|
2008-07-20 12:59:27 +00:00
|
|
|
if (occ != OCC_EQUALS) return CMD_ERROR;
|
|
|
|
if (new_order.GetConditionValue() != 0) return CMD_ERROR;
|
2008-07-20 07:41:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OCV_LOAD_PERCENTAGE:
|
|
|
|
case OCV_RELIABILITY:
|
2008-07-20 12:59:27 +00:00
|
|
|
if (new_order.GetConditionValue() > 100) return CMD_ERROR;
|
2008-07-20 07:41:43 +00:00
|
|
|
/* FALL THROUGH */
|
|
|
|
default:
|
2008-07-20 12:59:27 +00:00
|
|
|
if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) return CMD_ERROR;
|
2008-07-20 07:41:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-04-12 11:58:19 +00:00
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
default: return CMD_ERROR;
|
2005-04-22 05:41:09 +00:00
|
|
|
}
|
|
|
|
|
2009-01-03 13:20:32 +00:00
|
|
|
if (sel_ord > v->GetNumOrders()) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-04-21 23:40:56 +00:00
|
|
|
if (v->GetNumOrders() >= MAX_VEH_ORDER_ID) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS);
|
|
|
|
if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
|
|
|
|
if (v->orders.list == NULL && !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2009-02-04 13:52:17 +00:00
|
|
|
if (v->type == VEH_SHIP && _settings_game.pf.pathfinder_for_ships != VPF_NPF) {
|
2008-02-13 14:21:36 +00:00
|
|
|
/* Make sure the new destination is not too far away from the previous */
|
2007-02-18 16:24:29 +00:00
|
|
|
const Order *prev = NULL;
|
|
|
|
uint n = 0;
|
|
|
|
|
|
|
|
/* Find the last goto station or depot order before the insert location.
|
|
|
|
* If the order is to be inserted at the beginning of the order list this
|
|
|
|
* finds the last order in the list. */
|
2009-01-03 13:52:06 +00:00
|
|
|
const Order *o;
|
|
|
|
FOR_VEHICLE_ORDERS(v, o) {
|
2009-07-10 18:30:02 +00:00
|
|
|
switch (o->GetType()) {
|
|
|
|
case OT_GOTO_STATION:
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
|
case OT_GOTO_WAYPOINT:
|
|
|
|
prev = o;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
2007-02-18 16:24:29 +00:00
|
|
|
if (++n == sel_ord && prev != NULL) break;
|
|
|
|
}
|
|
|
|
if (prev != NULL) {
|
2008-06-30 14:49:50 +00:00
|
|
|
uint dist = GetOrderDistance(prev, &new_order, v);
|
2007-02-18 16:24:29 +00:00
|
|
|
if (dist >= 130) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION);
|
2007-02-18 16:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2007-08-02 23:40:19 +00:00
|
|
|
Order *new_o = new Order();
|
2008-04-05 21:45:05 +00:00
|
|
|
new_o->AssignOrder(new_order);
|
2005-01-15 19:06:22 +00:00
|
|
|
|
|
|
|
/* Create new order and link in list */
|
2009-01-03 13:52:06 +00:00
|
|
|
if (v->orders.list == NULL) {
|
|
|
|
v->orders.list = new OrderList(new_o, v);
|
2005-01-15 19:06:22 +00:00
|
|
|
} else {
|
2009-01-03 13:52:06 +00:00
|
|
|
v->orders.list->InsertOrderAt(new_o, sel_ord);
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-08-17 19:56:17 +00:00
|
|
|
Vehicle *u = v->FirstShared();
|
2006-03-04 11:01:35 +00:00
|
|
|
DeleteOrderWarnings(u);
|
2008-08-17 19:56:17 +00:00
|
|
|
for (; u != NULL; u = u->NextShared()) {
|
2009-01-03 13:52:06 +00:00
|
|
|
assert(v->orders.list == u->orders.list);
|
2005-01-15 19:06:22 +00:00
|
|
|
|
|
|
|
/* If there is added an order before the current one, we need
|
|
|
|
to update the selected order */
|
2005-05-11 00:00:27 +00:00
|
|
|
if (sel_ord <= u->cur_order_index) {
|
2005-01-15 19:06:22 +00:00
|
|
|
uint cur = u->cur_order_index + 1;
|
|
|
|
/* Check if we don't go out of bound */
|
2010-07-24 10:14:39 +00:00
|
|
|
if (cur < u->GetNumOrders()) {
|
2005-01-15 19:06:22 +00:00
|
|
|
u->cur_order_index = cur;
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Update any possible open window of the vehicle */
|
2008-09-24 16:40:06 +00:00
|
|
|
InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-12-10 18:16:08 +00:00
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
/* As we insert an order, the order to skip to will be 'wrong'. */
|
|
|
|
VehicleOrderID cur_order_id = 0;
|
|
|
|
Order *order;
|
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
|
|
|
if (order->IsType(OT_CONDITIONAL)) {
|
|
|
|
VehicleOrderID order_id = order->GetConditionSkipToOrder();
|
|
|
|
if (order_id >= sel_ord) {
|
|
|
|
order->SetConditionSkipToOrder(order_id + 1);
|
|
|
|
}
|
|
|
|
if (order_id == cur_order_id) {
|
2009-01-03 13:20:32 +00:00
|
|
|
order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
|
2008-04-12 11:58:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cur_order_id++;
|
|
|
|
}
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Make sure to rebuild the whole list */
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Declone an order-list
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param *dst delete the orders of this vehicle
|
|
|
|
* @param flags execution flags
|
2005-01-15 19:06:22 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (flags & DC_EXEC) {
|
2005-01-15 19:06:22 +00:00
|
|
|
DeleteVehicleOrders(dst);
|
2008-09-24 16:40:06 +00:00
|
|
|
InvalidateVehicleOrder(dst, -1);
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Delete an order from the orderlist of a vehicle.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p1 the ID of the vehicle
|
|
|
|
* @param p2 the order to delete (max 255)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-19 20:39:12 +00:00
|
|
|
VehicleID veh_id = GB(p1, 0, 20);
|
|
|
|
VehicleOrderID sel_ord = GB(p2, 0, 8);
|
2005-01-15 19:06:22 +00:00
|
|
|
Order *order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh_id);
|
2006-08-22 18:15:17 +00:00
|
|
|
|
2010-04-17 14:55:49 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* If we did not select an order, we maybe want to de-clone the orders */
|
2010-07-24 10:14:39 +00:00
|
|
|
if (sel_ord >= v->GetNumOrders()) return DecloneOrder(v, flags);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-23 12:27:42 +00:00
|
|
|
order = v->GetOrder(sel_ord);
|
2005-05-11 00:00:27 +00:00
|
|
|
if (order == NULL) return CMD_ERROR;
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2009-01-03 13:52:06 +00:00
|
|
|
v->orders.list->DeleteOrderAt(sel_ord);
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2008-08-17 19:56:17 +00:00
|
|
|
Vehicle *u = v->FirstShared();
|
2006-03-04 11:01:35 +00:00
|
|
|
DeleteOrderWarnings(u);
|
2008-08-17 19:56:17 +00:00
|
|
|
for (; u != NULL; u = u->NextShared()) {
|
2010-07-24 10:14:39 +00:00
|
|
|
if (sel_ord < u->cur_order_index) u->cur_order_index--;
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2009-01-03 13:52:06 +00:00
|
|
|
assert(v->orders.list == u->orders.list);
|
2005-01-15 19:06:22 +00:00
|
|
|
|
|
|
|
/* NON-stop flag is misused to see if a train is in a station that is
|
2005-05-11 00:00:27 +00:00
|
|
|
* on his order list or not */
|
2008-04-06 15:09:45 +00:00
|
|
|
if (sel_ord == u->cur_order_index && u->current_order.IsType(OT_LOADING)) {
|
2008-04-07 08:59:04 +00:00
|
|
|
u->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
|
|
|
|
/* Update any possible open window of the vehicle */
|
2008-09-24 16:40:06 +00:00
|
|
|
InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-12-10 18:16:08 +00:00
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
/* As we delete an order, the order to skip to will be 'wrong'. */
|
|
|
|
VehicleOrderID cur_order_id = 0;
|
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
|
|
|
if (order->IsType(OT_CONDITIONAL)) {
|
|
|
|
VehicleOrderID order_id = order->GetConditionSkipToOrder();
|
|
|
|
if (order_id >= sel_ord) {
|
|
|
|
order->SetConditionSkipToOrder(max(order_id - 1, 0));
|
|
|
|
}
|
|
|
|
if (order_id == cur_order_id) {
|
2009-01-03 13:20:32 +00:00
|
|
|
order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
|
2008-04-12 11:58:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cur_order_id++;
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Goto order of order-list.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p1 The ID of the vehicle which order is skipped
|
2007-06-04 19:32:45 +00:00
|
|
|
* @param p2 the selected order to which we want to skip
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-01-15 19:06:22 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-19 20:39:12 +00:00
|
|
|
VehicleID veh_id = GB(p1, 0, 20);
|
|
|
|
VehicleOrderID sel_ord = GB(p2, 0, 8);
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh_id);
|
2006-08-22 18:15:17 +00:00
|
|
|
|
2010-04-17 14:55:49 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle() || sel_ord == v->cur_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 09:28:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2007-06-04 19:32:45 +00:00
|
|
|
v->cur_order_index = sel_ord;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-04-06 15:09:45 +00:00
|
|
|
if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-02 17:54:40 +00:00
|
|
|
InvalidateVehicleOrder(v, -2);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-15 09:28:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* We have an aircraft/ship, they have a mini-schedule, so update them all */
|
2009-09-13 19:15:59 +00:00
|
|
|
if (v->type == VEH_AIRCRAFT) SetWindowClassesDirty(WC_AIRCRAFT_LIST);
|
|
|
|
if (v->type == VEH_SHIP) SetWindowClassesDirty(WC_SHIPS_LIST);
|
2005-01-15 09:28:08 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-08 18:59:29 +00:00
|
|
|
/**
|
|
|
|
* Move an order inside the orderlist
|
|
|
|
* @param tile unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param flags operation to perform
|
2007-06-08 18:59:29 +00:00
|
|
|
* @param p1 the ID of the vehicle
|
|
|
|
* @param p2 order to move and target
|
|
|
|
* bit 0-15 : the order to move
|
|
|
|
* bit 16-31 : the target order
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2007-06-08 18:59:29 +00:00
|
|
|
* @note The target order will move one place down in the orderlist
|
|
|
|
* if you move the order upwards else it'll move it one place down
|
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-06-08 18:59:29 +00:00
|
|
|
{
|
2010-08-19 20:39:12 +00:00
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
2007-06-08 18:59:29 +00:00
|
|
|
VehicleOrderID moving_order = GB(p2, 0, 16);
|
|
|
|
VehicleOrderID target_order = GB(p2, 16, 16);
|
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
2010-04-17 14:55:49 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2007-06-08 18:59:29 +00:00
|
|
|
|
|
|
|
/* Don't make senseless movements */
|
2009-01-03 13:20:32 +00:00
|
|
|
if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() ||
|
2010-03-20 15:30:57 +00:00
|
|
|
moving_order == target_order || v->GetNumOrders() <= 1) return CMD_ERROR;
|
2007-06-08 18:59:29 +00:00
|
|
|
|
2009-05-23 12:27:42 +00:00
|
|
|
Order *moving_one = v->GetOrder(moving_order);
|
2007-06-08 18:59:29 +00:00
|
|
|
/* Don't move an empty order */
|
|
|
|
if (moving_one == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2009-01-03 13:52:06 +00:00
|
|
|
v->orders.list->MoveOrder(moving_order, target_order);
|
2007-06-08 18:59:29 +00:00
|
|
|
|
|
|
|
/* Update shared list */
|
2008-08-17 19:56:17 +00:00
|
|
|
Vehicle *u = v->FirstShared();
|
2007-06-08 18:59:29 +00:00
|
|
|
|
|
|
|
DeleteOrderWarnings(u);
|
|
|
|
|
2008-08-17 19:56:17 +00:00
|
|
|
for (; u != NULL; u = u->NextShared()) {
|
2007-06-08 18:59:29 +00:00
|
|
|
/* Update the current order */
|
|
|
|
if (u->cur_order_index == moving_order) {
|
|
|
|
u->cur_order_index = target_order;
|
2009-05-09 13:37:18 +00:00
|
|
|
} else if (u->cur_order_index > moving_order && u->cur_order_index <= target_order) {
|
2007-06-08 18:59:29 +00:00
|
|
|
u->cur_order_index--;
|
2009-05-09 13:37:18 +00:00
|
|
|
} else if (u->cur_order_index < moving_order && u->cur_order_index >= target_order) {
|
2007-06-08 18:59:29 +00:00
|
|
|
u->cur_order_index++;
|
|
|
|
}
|
|
|
|
|
2009-01-03 13:52:06 +00:00
|
|
|
assert(v->orders.list == u->orders.list);
|
2007-06-08 18:59:29 +00:00
|
|
|
/* Update any possible open window of the vehicle */
|
2008-09-24 16:40:06 +00:00
|
|
|
InvalidateVehicleOrder(u, moving_order | (target_order << 8));
|
2007-06-08 18:59:29 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
/* As we move an order, the order to skip to will be 'wrong'. */
|
|
|
|
Order *order;
|
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
|
|
|
if (order->IsType(OT_CONDITIONAL)) {
|
|
|
|
VehicleOrderID order_id = order->GetConditionSkipToOrder();
|
|
|
|
if (order_id == moving_order) {
|
|
|
|
order_id = target_order;
|
2009-08-14 17:11:59 +00:00
|
|
|
} else if (order_id > moving_order && order_id <= target_order) {
|
2008-04-12 11:58:19 +00:00
|
|
|
order_id--;
|
2009-08-14 17:11:59 +00:00
|
|
|
} else if (order_id < moving_order && order_id >= target_order) {
|
2008-04-12 11:58:19 +00:00
|
|
|
order_id++;
|
|
|
|
}
|
|
|
|
order->SetConditionSkipToOrder(order_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-08 18:59:29 +00:00
|
|
|
/* Make sure to rebuild the whole list */
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
|
2007-06-08 18:59:29 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2007-06-08 18:59:29 +00:00
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Modify an order in the orderlist of a vehicle.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p1 various bitstuffed elements
|
2010-08-19 20:39:12 +00:00
|
|
|
* - p1 = (bit 0 - 19) - ID of the vehicle
|
|
|
|
* - p1 = (bit 24 - 31) - the selected order (if any). If the last order is given,
|
2005-07-20 15:29:28 +00:00
|
|
|
* the order will be inserted before that one
|
2010-08-19 20:39:12 +00:00
|
|
|
* the maximum vehicle order id is 254.
|
2008-04-09 12:46:43 +00:00
|
|
|
* @param p2 various bitstuffed elements
|
2008-04-12 11:58:19 +00:00
|
|
|
* - p2 = (bit 0 - 3) - what data to modify (@see ModifyOrderFlags)
|
|
|
|
* - p2 = (bit 4 - 15) - the data to modify
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-19 20:39:12 +00:00
|
|
|
VehicleOrderID sel_ord = GB(p1, 20, 8);
|
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
2010-04-17 13:31:41 +00:00
|
|
|
ModifyOrderFlags mof = Extract<ModifyOrderFlags, 0, 4>(p2);
|
2010-08-19 20:39:12 +00:00
|
|
|
uint16 data = GB(p2, 4, 11);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
if (mof >= MOF_END) return CMD_ERROR;
|
2006-08-22 18:15:17 +00:00
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
2010-04-17 14:55:49 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2005-04-22 05:41:09 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Is it a valid order? */
|
2009-01-03 13:20:32 +00:00
|
|
|
if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-23 12:27:42 +00:00
|
|
|
Order *order = v->GetOrder(sel_ord);
|
2008-04-09 12:46:43 +00:00
|
|
|
switch (order->GetType()) {
|
|
|
|
case OT_GOTO_STATION:
|
2009-07-10 18:30:02 +00:00
|
|
|
if (mof == MOF_COND_VARIABLE || mof == MOF_COND_COMPARATOR || mof == MOF_DEPOT_ACTION || mof == MOF_COND_VALUE) return CMD_ERROR;
|
2008-04-09 12:46:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OT_GOTO_DEPOT:
|
2008-04-12 11:58:19 +00:00
|
|
|
if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR;
|
2008-04-09 12:46:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OT_GOTO_WAYPOINT:
|
|
|
|
if (mof != MOF_NON_STOP) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
case OT_CONDITIONAL:
|
2009-04-15 17:37:31 +00:00
|
|
|
if (mof != MOF_COND_VARIABLE && mof != MOF_COND_COMPARATOR && mof != MOF_COND_VALUE && mof != MOF_COND_DESTINATION) return CMD_ERROR;
|
2008-04-12 11:58:19 +00:00
|
|
|
break;
|
|
|
|
|
2008-04-09 12:46:43 +00:00
|
|
|
default:
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mof) {
|
2008-04-12 11:58:19 +00:00
|
|
|
default: NOT_REACHED();
|
|
|
|
|
2008-04-09 12:46:43 +00:00
|
|
|
case MOF_NON_STOP:
|
2008-04-13 16:54:19 +00:00
|
|
|
if (v->type != VEH_TRAIN && v->type != VEH_ROAD) return CMD_ERROR;
|
2008-04-09 12:46:43 +00:00
|
|
|
if (data >= ONSF_END) return CMD_ERROR;
|
|
|
|
if (data == order->GetNonStopType()) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
|
2009-04-12 14:11:14 +00:00
|
|
|
case MOF_STOP_LOCATION:
|
|
|
|
if (v->type != VEH_TRAIN) return CMD_ERROR;
|
|
|
|
if (data >= OSL_END) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
|
2008-04-09 12:46:43 +00:00
|
|
|
case MOF_UNLOAD:
|
2008-04-10 10:18:03 +00:00
|
|
|
if ((data & ~(OUFB_UNLOAD | OUFB_TRANSFER | OUFB_NO_UNLOAD)) != 0) return CMD_ERROR;
|
|
|
|
/* Unload and no-unload are mutual exclusive and so are transfer and no unload. */
|
|
|
|
if (data != 0 && ((data & (OUFB_UNLOAD | OUFB_TRANSFER)) != 0) == ((data & OUFB_NO_UNLOAD) != 0)) return CMD_ERROR;
|
2008-04-09 12:46:43 +00:00
|
|
|
if (data == order->GetUnloadType()) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOF_LOAD:
|
2008-04-10 10:18:03 +00:00
|
|
|
if (data > OLFB_NO_LOAD || data == 1) return CMD_ERROR;
|
2008-04-09 12:46:43 +00:00
|
|
|
if (data == order->GetLoadType()) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOF_DEPOT_ACTION:
|
2008-10-24 14:49:45 +00:00
|
|
|
if (data >= DA_END) return CMD_ERROR;
|
2008-04-09 12:46:43 +00:00
|
|
|
break;
|
2008-04-12 11:58:19 +00:00
|
|
|
|
|
|
|
case MOF_COND_VARIABLE:
|
|
|
|
if (data >= OCV_END) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOF_COND_COMPARATOR:
|
|
|
|
if (data >= OCC_END) return CMD_ERROR;
|
|
|
|
switch (order->GetConditionVariable()) {
|
2008-04-12 13:07:25 +00:00
|
|
|
case OCV_UNCONDITIONALLY: return CMD_ERROR;
|
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
case OCV_REQUIRES_SERVICE:
|
|
|
|
if (data != OCC_IS_TRUE && data != OCC_IS_FALSE) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (data == OCC_IS_TRUE || data == OCC_IS_FALSE) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOF_COND_VALUE:
|
|
|
|
switch (order->GetConditionVariable()) {
|
2008-04-12 13:07:25 +00:00
|
|
|
case OCV_UNCONDITIONALLY: return CMD_ERROR;
|
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
case OCV_LOAD_PERCENTAGE:
|
|
|
|
case OCV_RELIABILITY:
|
|
|
|
if (data > 100) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (data > 2047) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2008-07-20 07:41:43 +00:00
|
|
|
|
|
|
|
case MOF_COND_DESTINATION:
|
2009-01-03 13:20:32 +00:00
|
|
|
if (data >= v->GetNumOrders()) return CMD_ERROR;
|
2008-07-20 07:41:43 +00:00
|
|
|
break;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2008-04-09 12:46:43 +00:00
|
|
|
switch (mof) {
|
|
|
|
case MOF_NON_STOP:
|
|
|
|
order->SetNonStopType((OrderNonStopFlags)data);
|
2008-04-06 21:54:22 +00:00
|
|
|
break;
|
2008-04-09 12:46:43 +00:00
|
|
|
|
2009-04-12 14:11:14 +00:00
|
|
|
case MOF_STOP_LOCATION:
|
|
|
|
order->SetStopLocation((OrderStopLocation)data);
|
|
|
|
break;
|
|
|
|
|
2008-04-09 12:46:43 +00:00
|
|
|
case MOF_UNLOAD:
|
|
|
|
order->SetUnloadType((OrderUnloadFlags)data);
|
2008-04-10 17:23:51 +00:00
|
|
|
if ((data & OUFB_NO_UNLOAD) != 0 && (order->GetLoadType() & OLFB_NO_LOAD) != 0) {
|
2008-04-10 10:18:03 +00:00
|
|
|
order->SetLoadType((OrderLoadFlags)(order->GetLoadType() & ~OLFB_NO_LOAD));
|
|
|
|
}
|
2008-04-06 21:54:22 +00:00
|
|
|
break;
|
2008-04-09 12:46:43 +00:00
|
|
|
|
|
|
|
case MOF_LOAD:
|
|
|
|
order->SetLoadType((OrderLoadFlags)data);
|
2008-04-10 17:23:51 +00:00
|
|
|
if ((data & OLFB_NO_LOAD) != 0 && (order->GetUnloadType() & OUFB_NO_UNLOAD) != 0) {
|
|
|
|
/* No load + no unload isn't compatible */
|
|
|
|
order->SetUnloadType((OrderUnloadFlags)(order->GetUnloadType() & ~OUFB_NO_UNLOAD));
|
2008-04-10 10:18:03 +00:00
|
|
|
}
|
2008-04-06 21:54:22 +00:00
|
|
|
break;
|
2008-04-09 12:46:43 +00:00
|
|
|
|
2008-10-24 14:49:45 +00:00
|
|
|
case MOF_DEPOT_ACTION: {
|
|
|
|
switch (data) {
|
|
|
|
case DA_ALWAYS_GO:
|
|
|
|
order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
|
|
|
|
order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DA_SERVICE:
|
|
|
|
order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() | ODTFB_SERVICE));
|
|
|
|
order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() & ~ODATFB_HALT));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DA_STOP:
|
|
|
|
order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() & ~ODTFB_SERVICE));
|
|
|
|
order->SetDepotActionType((OrderDepotActionFlags)(order->GetDepotActionType() | ODATFB_HALT));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-04-09 12:46:43 +00:00
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
case MOF_COND_VARIABLE: {
|
|
|
|
order->SetConditionVariable((OrderConditionVariable)data);
|
|
|
|
|
|
|
|
OrderConditionComparator occ = order->GetConditionComparator();
|
|
|
|
switch (order->GetConditionVariable()) {
|
2008-04-12 13:07:25 +00:00
|
|
|
case OCV_UNCONDITIONALLY:
|
|
|
|
order->SetConditionComparator(OCC_EQUALS);
|
|
|
|
order->SetConditionValue(0);
|
|
|
|
break;
|
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
case OCV_REQUIRES_SERVICE:
|
|
|
|
if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) order->SetConditionComparator(OCC_IS_TRUE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCV_LOAD_PERCENTAGE:
|
|
|
|
case OCV_RELIABILITY:
|
|
|
|
if (order->GetConditionValue() > 100) order->SetConditionValue(100);
|
|
|
|
/* FALL THROUGH */
|
|
|
|
default:
|
|
|
|
if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) order->SetConditionComparator(OCC_EQUALS);
|
|
|
|
break;
|
|
|
|
}
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-04-12 11:58:19 +00:00
|
|
|
|
|
|
|
case MOF_COND_COMPARATOR:
|
|
|
|
order->SetConditionComparator((OrderConditionComparator)data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOF_COND_VALUE:
|
|
|
|
order->SetConditionValue(data);
|
|
|
|
break;
|
|
|
|
|
2008-07-20 07:41:43 +00:00
|
|
|
case MOF_COND_DESTINATION:
|
|
|
|
order->SetConditionSkipToOrder(data);
|
|
|
|
break;
|
|
|
|
|
2008-04-06 21:54:22 +00:00
|
|
|
default: NOT_REACHED();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-03 22:18:40 +00:00
|
|
|
/* Update the windows and full load flags, also for vehicles that share the same order list */
|
2008-08-17 19:56:17 +00:00
|
|
|
Vehicle *u = v->FirstShared();
|
2008-04-09 12:46:43 +00:00
|
|
|
DeleteOrderWarnings(u);
|
2008-08-17 19:56:17 +00:00
|
|
|
for (; u != NULL; u = u->NextShared()) {
|
2008-04-09 17:48:22 +00:00
|
|
|
/* Toggle u->current_order "Full load" flag if it changed.
|
|
|
|
* However, as the same flag is used for depot orders, check
|
|
|
|
* whether we are not going to a depot as there are three
|
|
|
|
* cases where the full load flag can be active and only
|
|
|
|
* one case where the flag is used for depot orders. In the
|
|
|
|
* other cases for the OrderTypeByte the flags are not used,
|
|
|
|
* so do not care and those orders should not be active
|
|
|
|
* when this function is called.
|
|
|
|
*/
|
|
|
|
if (sel_ord == u->cur_order_index &&
|
|
|
|
(u->current_order.IsType(OT_GOTO_STATION) || u->current_order.IsType(OT_LOADING)) &&
|
|
|
|
u->current_order.GetLoadType() != order->GetLoadType()) {
|
|
|
|
u->current_order.SetLoadType(order->GetLoadType());
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
2009-09-02 17:54:40 +00:00
|
|
|
InvalidateVehicleOrder(u, -2);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Clone/share/copy an order-list of another vehicle.
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param tile unused
|
|
|
|
* @param flags operation to perform
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 various bitstuffed elements
|
2010-08-19 20:39:12 +00:00
|
|
|
* - p1 = (bit 0-19) - destination vehicle to clone orders to
|
|
|
|
* - p1 = (bit 30-31) - action to perform
|
|
|
|
* @param p2 source vehicle to clone orders from, if any (none for CO_UNSHARE)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-01-15 19:06:22 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2005-01-15 19:06:22 +00:00
|
|
|
{
|
2010-08-19 20:39:12 +00:00
|
|
|
VehicleID veh_src = GB(p2, 0, 20);
|
|
|
|
VehicleID veh_dst = GB(p1, 0, 20);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *dst = Vehicle::GetIfValid(veh_dst);
|
2010-04-17 14:55:49 +00:00
|
|
|
if (dst == NULL || !dst->IsPrimaryVehicle()) return CMD_ERROR;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
2010-03-13 17:11:28 +00:00
|
|
|
CommandCost ret = CheckOwnership(dst->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
2010-08-19 20:39:12 +00:00
|
|
|
switch (GB(p1, 30, 2)) {
|
2005-01-15 19:06:22 +00:00
|
|
|
case CO_SHARE: {
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *src = Vehicle::GetIfValid(veh_src);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Sanity checks */
|
2010-04-17 14:55:49 +00:00
|
|
|
if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(src->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Trucks can't share orders with busses (and visa versa) */
|
2009-12-02 17:37:02 +00:00
|
|
|
if (src->type == VEH_ROAD && RoadVehicle::From(src)->IsBus() != RoadVehicle::From(dst)->IsBus()) {
|
2009-12-02 17:35:54 +00:00
|
|
|
return CMD_ERROR;
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Is the vehicle already in the shared list? */
|
2008-08-17 19:56:17 +00:00
|
|
|
if (src->FirstShared() == dst->FirstShared()) return CMD_ERROR;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-10 13:00:38 +00:00
|
|
|
const Order *order;
|
|
|
|
|
|
|
|
FOR_VEHICLE_ORDERS(src, order) {
|
|
|
|
if (OrderGoesToStation(dst, order) &&
|
2009-05-16 23:34:14 +00:00
|
|
|
!CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
|
2009-01-10 13:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* If the destination vehicle had a OrderList, destroy it */
|
|
|
|
DeleteVehicleOrders(dst);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-03 13:52:06 +00:00
|
|
|
dst->orders.list = src->orders.list;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Link this vehicle in the shared-list */
|
2008-08-17 19:56:17 +00:00
|
|
|
dst->AddToShared(src);
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2008-09-24 16:40:06 +00:00
|
|
|
InvalidateVehicleOrder(dst, -1);
|
2009-09-02 17:54:40 +00:00
|
|
|
InvalidateVehicleOrder(src, -2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
case CO_COPY: {
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *src = Vehicle::GetIfValid(veh_src);
|
2005-05-12 00:11:37 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Sanity checks */
|
2010-04-17 14:55:49 +00:00
|
|
|
if (src == NULL || !src->IsPrimaryVehicle() || dst->type != src->type || dst == src) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(src->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-10 13:00:38 +00:00
|
|
|
/* Trucks can't copy all the orders from busses (and visa versa),
|
|
|
|
* and neither can helicopters and aircarft. */
|
|
|
|
const Order *order;
|
|
|
|
FOR_VEHICLE_ORDERS(src, order) {
|
|
|
|
if (OrderGoesToStation(dst, order) &&
|
2009-05-16 23:34:14 +00:00
|
|
|
!CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
|
2004-08-12 21:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* make sure there are orders available */
|
2009-05-18 16:21:28 +00:00
|
|
|
int delta = dst->IsOrderListShared() ? src->GetNumOrders() + 1 : src->GetNumOrders() - dst->GetNumOrders();
|
2009-01-03 13:52:06 +00:00
|
|
|
if (!Order::CanAllocateItem(delta) ||
|
|
|
|
((dst->orders.list == NULL || dst->IsOrderListShared()) && !OrderList::CanAllocateItem())) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
|
2008-10-30 12:32:32 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
const Order *order;
|
2009-01-03 13:52:06 +00:00
|
|
|
Order *first = NULL;
|
2005-01-15 19:06:22 +00:00
|
|
|
Order **order_dst;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-03 13:52:06 +00:00
|
|
|
/* If the destination vehicle had an order list, destroy the chain but keep the OrderList */
|
|
|
|
DeleteVehicleOrders(dst, true);
|
2004-12-10 18:16:08 +00:00
|
|
|
|
2009-01-03 13:52:06 +00:00
|
|
|
order_dst = &first;
|
2005-01-15 19:06:22 +00:00
|
|
|
FOR_VEHICLE_ORDERS(src, order) {
|
2007-08-02 23:40:19 +00:00
|
|
|
*order_dst = new Order();
|
2008-04-05 21:45:05 +00:00
|
|
|
(*order_dst)->AssignOrder(*order);
|
2005-01-15 19:06:22 +00:00
|
|
|
order_dst = &(*order_dst)->next;
|
|
|
|
}
|
2009-05-22 15:13:50 +00:00
|
|
|
if (dst->orders.list == NULL) {
|
|
|
|
dst->orders.list = new OrderList(first, dst);
|
|
|
|
} else {
|
2009-01-03 13:52:06 +00:00
|
|
|
assert(dst->orders.list->GetFirstOrder() == NULL);
|
2009-05-22 15:13:50 +00:00
|
|
|
assert(!dst->orders.list->IsShared());
|
|
|
|
delete dst->orders.list;
|
|
|
|
dst->orders.list = new OrderList(first, dst);
|
2009-01-03 13:52:06 +00:00
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2008-09-24 16:40:06 +00:00
|
|
|
InvalidateVehicleOrder(dst, -1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
case CO_UNSHARE: return DecloneOrder(dst, flags);
|
2005-05-12 23:46:01 +00:00
|
|
|
default: return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Add/remove refit orders from an order
|
2006-10-03 14:52:39 +00:00
|
|
|
* @param tile Not used
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perform
|
2006-10-03 14:52:39 +00:00
|
|
|
* @param p1 VehicleIndex of the vehicle having the order
|
|
|
|
* @param p2 bitmask
|
|
|
|
* - bit 0-7 CargoID
|
|
|
|
* - bit 8-15 Cargo subtype
|
|
|
|
* - bit 16-23 number of order to modify
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2006-10-03 14:52:39 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2006-10-03 14:52:39 +00:00
|
|
|
{
|
2010-08-19 20:39:12 +00:00
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
2006-10-03 14:52:39 +00:00
|
|
|
VehicleOrderID order_number = GB(p2, 16, 8);
|
|
|
|
CargoID cargo = GB(p2, 0, 8);
|
|
|
|
byte subtype = GB(p2, 8, 8);
|
|
|
|
|
2010-04-17 14:55:49 +00:00
|
|
|
if (cargo >= NUM_CARGO) return CMD_ERROR;
|
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
const Vehicle *v = Vehicle::GetIfValid(veh);
|
2010-04-17 14:55:49 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2006-10-03 14:52:39 +00:00
|
|
|
|
2009-05-23 12:27:42 +00:00
|
|
|
Order *order = v->GetOrder(order_number);
|
2006-10-03 14:52:39 +00:00
|
|
|
if (order == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2008-04-06 07:07:21 +00:00
|
|
|
order->SetRefit(cargo, subtype);
|
2006-10-03 14:52:39 +00:00
|
|
|
|
2008-08-17 19:56:17 +00:00
|
|
|
for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) {
|
2006-10-03 14:52:39 +00:00
|
|
|
/* Update any possible open window of the vehicle */
|
2009-09-02 17:54:40 +00:00
|
|
|
InvalidateVehicleOrder(u, -2);
|
2006-10-03 14:52:39 +00:00
|
|
|
|
|
|
|
/* If the vehicle already got the current depot set as current order, then update current order as well */
|
2009-06-01 11:43:36 +00:00
|
|
|
if (u->cur_order_index == order_number && (u->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) {
|
2008-04-06 07:07:21 +00:00
|
|
|
u->current_order.SetRefit(cargo, subtype);
|
2006-10-03 14:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2006-10-03 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
2006-07-26 10:00:33 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Check the orders of a vehicle, to see if there are invalid orders and stuff
|
|
|
|
*
|
|
|
|
*/
|
2009-01-10 00:31:47 +00:00
|
|
|
void CheckOrders(const Vehicle *v)
|
2004-08-11 10:15:38 +00:00
|
|
|
{
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Does the user wants us to check things? */
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_client.gui.order_review_system == 0) return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Do nothing for crashed vehicles */
|
2006-03-04 11:15:44 +00:00
|
|
|
if (v->vehstatus & VS_CRASHED) return;
|
2004-12-19 09:30:05 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Do nothing for stopped vehicles if setting is '1' */
|
2010-07-24 10:14:39 +00:00
|
|
|
if (_settings_client.gui.order_review_system == 1 && (v->vehstatus & VS_STOPPED)) return;
|
2004-08-11 10:15:38 +00:00
|
|
|
|
2005-01-17 21:54:45 +00:00
|
|
|
/* do nothing we we're not the first vehicle in a share-chain */
|
2008-08-17 19:56:17 +00:00
|
|
|
if (v->FirstShared() != v) return;
|
2005-01-17 21:54:45 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Only check every 20 days, so that we don't flood the message log */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (v->owner == _local_company && v->day_counter % 20 == 0) {
|
2005-01-15 19:06:22 +00:00
|
|
|
int n_st, problem_type = -1;
|
|
|
|
const Order *order;
|
|
|
|
int message = 0;
|
|
|
|
|
|
|
|
/* Check the order list */
|
2004-08-15 13:21:18 +00:00
|
|
|
n_st = 0;
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
|
|
|
/* Dummy order? */
|
2008-04-05 23:36:54 +00:00
|
|
|
if (order->IsType(OT_DUMMY)) {
|
2004-08-15 13:21:18 +00:00
|
|
|
problem_type = 1;
|
|
|
|
break;
|
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Does station have a load-bay for this vehicle? */
|
2008-04-05 23:36:54 +00:00
|
|
|
if (order->IsType(OT_GOTO_STATION)) {
|
2009-05-16 23:34:14 +00:00
|
|
|
const Station *st = Station::Get(order->GetDestination());
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2004-08-11 10:15:38 +00:00
|
|
|
n_st++;
|
2009-12-29 23:13:36 +00:00
|
|
|
if (!CanVehicleUseStation(v, st)) problem_type = 3;
|
2004-08-11 10:15:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Check if the last and the first order are the same */
|
2009-01-03 13:20:32 +00:00
|
|
|
if (v->GetNumOrders() > 1) {
|
2009-05-23 12:27:42 +00:00
|
|
|
const Order *last = v->GetLastOrder();
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2009-01-03 13:52:06 +00:00
|
|
|
if (v->orders.list->GetFirstOrder()->Equals(*last)) {
|
2006-06-27 21:25:53 +00:00
|
|
|
problem_type = 2;
|
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Do we only have 1 station in our order list? */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (n_st < 2 && problem_type == -1) problem_type = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-03 13:52:06 +00:00
|
|
|
#ifndef NDEBUG
|
2009-02-05 15:58:42 +00:00
|
|
|
if (v->orders.list != NULL) v->orders.list->DebugCheckSanity();
|
2009-01-03 13:52:06 +00:00
|
|
|
#endif
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* We don't have a problem */
|
2006-03-04 11:15:44 +00:00
|
|
|
if (problem_type < 0) return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-08-04 18:04:33 +00:00
|
|
|
message = STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS + problem_type;
|
2006-12-26 17:36:18 +00:00
|
|
|
//DEBUG(misc, 3, "Triggered News Item for vehicle %d", v->index);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-04 17:42:46 +00:00
|
|
|
SetDParam(0, v->index);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(
|
2004-08-15 13:21:18 +00:00
|
|
|
message,
|
2008-05-15 13:39:36 +00:00
|
|
|
NS_ADVICE,
|
2009-05-24 16:52:42 +00:00
|
|
|
v->index
|
2006-03-04 11:15:44 +00:00
|
|
|
);
|
2004-08-11 10:15:38 +00:00
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-08-22 17:13:49 +00:00
|
|
|
* Removes an order from all vehicles. Triggers when, say, a station is removed.
|
|
|
|
* @param type The type of the order (OT_GOTO_[STATION|DEPOT|WAYPOINT]).
|
|
|
|
* @param destination The destination. Can be a StationID, DepotID or WaypointID.
|
2005-01-15 19:06:22 +00:00
|
|
|
*/
|
2006-08-26 16:34:03 +00:00
|
|
|
void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination)
|
2005-01-15 19:06:22 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2006-09-03 10:30:38 +00:00
|
|
|
/* Aircraft have StationIDs for depot orders and never use DepotIDs
|
|
|
|
* This fact is handled specially below
|
|
|
|
*/
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
/* Go through all vehicles */
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2006-09-03 19:29:31 +00:00
|
|
|
Order *order;
|
|
|
|
|
|
|
|
order = &v->current_order;
|
2008-04-05 23:36:54 +00:00
|
|
|
if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
|
2008-04-06 07:48:51 +00:00
|
|
|
v->current_order.GetDestination() == destination) {
|
2008-04-05 23:36:54 +00:00
|
|
|
order->MakeDummy();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear the order from the order-list */
|
2008-09-24 16:40:06 +00:00
|
|
|
int id = -1;
|
2005-01-15 19:06:22 +00:00
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
2008-09-24 16:40:06 +00:00
|
|
|
id++;
|
2008-08-27 19:21:01 +00:00
|
|
|
if (order->IsType(OT_GOTO_DEPOT) && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
|
2008-04-05 23:36:54 +00:00
|
|
|
if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
|
2008-04-06 07:48:51 +00:00
|
|
|
order->GetDestination() == destination) {
|
2008-04-05 23:36:54 +00:00
|
|
|
order->MakeDummy();
|
2008-09-24 16:40:06 +00:00
|
|
|
for (const Vehicle *w = v->FirstShared(); w != NULL; w = w->NextShared()) {
|
|
|
|
/* In GUI, simulate by removing the order and adding it back */
|
|
|
|
InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
|
|
|
|
InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
|
|
|
|
}
|
2008-08-27 19:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Checks if a vehicle has a GOTO_DEPOT in his order list
|
|
|
|
*
|
|
|
|
* @return True if this is true (lol ;))
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool VehicleHasDepotOrders(const Vehicle *v)
|
|
|
|
{
|
|
|
|
const Order *order;
|
|
|
|
|
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
2010-07-24 10:14:39 +00:00
|
|
|
if (order->IsType(OT_GOTO_DEPOT)) return true;
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Delete all orders from a vehicle
|
|
|
|
*
|
|
|
|
*/
|
2009-01-03 13:52:06 +00:00
|
|
|
void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist)
|
2005-01-15 19:06:22 +00:00
|
|
|
{
|
2006-03-04 11:01:35 +00:00
|
|
|
DeleteOrderWarnings(v);
|
|
|
|
|
2008-02-02 02:45:09 +00:00
|
|
|
if (v->IsOrderListShared()) {
|
2008-08-17 19:56:17 +00:00
|
|
|
/* Remove ourself from the shared order list. */
|
|
|
|
v->RemoveFromShared();
|
2009-01-03 13:52:06 +00:00
|
|
|
v->orders.list = NULL;
|
|
|
|
} else if (v->orders.list != NULL) {
|
2008-08-17 19:56:17 +00:00
|
|
|
/* Remove the orders */
|
2009-01-03 13:52:06 +00:00
|
|
|
v->orders.list->FreeChain(keep_orderlist);
|
|
|
|
if (!keep_orderlist) v->orders.list = NULL;
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-26 21:59:49 +00:00
|
|
|
uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id)
|
2008-01-07 00:57:19 +00:00
|
|
|
{
|
2009-05-26 21:59:49 +00:00
|
|
|
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);
|
2008-01-07 00:57:19 +00:00
|
|
|
}
|
|
|
|
|
2008-04-05 12:01:34 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Check if a vehicle has any valid orders
|
|
|
|
*
|
|
|
|
* @return false if there are no valid orders
|
2009-12-23 18:43:38 +00:00
|
|
|
* @note Conditional orders are not considered valid destination orders
|
2008-04-05 12:01:34 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static bool CheckForValidOrders(const Vehicle *v)
|
|
|
|
{
|
|
|
|
const Order *order;
|
|
|
|
|
2009-12-23 18:43:38 +00:00
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
|
|
|
switch (order->GetType()) {
|
|
|
|
case OT_GOTO_STATION:
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
|
case OT_GOTO_WAYPOINT:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-04-05 12:01:34 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-12 11:58:19 +00:00
|
|
|
/**
|
|
|
|
* Compare the variable and value based on the given comparator.
|
|
|
|
*/
|
|
|
|
static bool OrderConditionCompare(OrderConditionComparator occ, int variable, int value)
|
|
|
|
{
|
|
|
|
switch (occ) {
|
|
|
|
case OCC_EQUALS: return variable == value;
|
|
|
|
case OCC_NOT_EQUALS: return variable != value;
|
|
|
|
case OCC_LESS_THAN: return variable < value;
|
|
|
|
case OCC_LESS_EQUALS: return variable <= value;
|
|
|
|
case OCC_MORE_THAN: return variable > value;
|
|
|
|
case OCC_MORE_EQUALS: return variable >= value;
|
|
|
|
case OCC_IS_TRUE: return variable != 0;
|
|
|
|
case OCC_IS_FALSE: return variable == 0;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-02 08:33:38 +00:00
|
|
|
/**
|
|
|
|
* Process a conditional order and determine the next order.
|
|
|
|
* @param order the order the vehicle currently has
|
|
|
|
* @param v the vehicle to update
|
|
|
|
* @return index of next order to jump to, or INVALID_VEH_ORDER_ID to use the next order
|
|
|
|
*/
|
2008-08-02 22:53:05 +00:00
|
|
|
VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
|
2008-06-02 08:33:38 +00:00
|
|
|
{
|
|
|
|
if (order->GetType() != OT_CONDITIONAL) return INVALID_VEH_ORDER_ID;
|
|
|
|
|
|
|
|
bool skip_order = false;
|
|
|
|
OrderConditionComparator occ = order->GetConditionComparator();
|
|
|
|
uint16 value = order->GetConditionValue();
|
|
|
|
|
|
|
|
switch (order->GetConditionVariable()) {
|
|
|
|
case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break;
|
2009-08-27 13:31:26 +00:00
|
|
|
case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break;
|
2009-02-01 17:14:39 +00:00
|
|
|
case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break;
|
2009-01-13 22:58:03 +00:00
|
|
|
case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break;
|
2008-06-02 08:33:38 +00:00
|
|
|
case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break;
|
|
|
|
case OCV_UNCONDITIONALLY: skip_order = true; break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
return skip_order ? order->GetConditionSkipToOrder() : (VehicleOrderID)INVALID_VEH_ORDER_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the vehicle's destination tile from an order.
|
|
|
|
* @param order the order the vehicle currently has
|
|
|
|
* @param v the vehicle to update
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param conditional_depth the depth (amount of steps) to go with conditional orders. This to prevent infinite loops.
|
2008-06-02 08:33:38 +00:00
|
|
|
*/
|
2008-08-02 22:53:05 +00:00
|
|
|
bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth)
|
2008-06-02 08:33:38 +00:00
|
|
|
{
|
2009-07-13 09:18:07 +00:00
|
|
|
if (conditional_depth > v->GetNumOrders()) return false;
|
|
|
|
|
2008-06-02 08:33:38 +00:00
|
|
|
switch (order->GetType()) {
|
|
|
|
case OT_GOTO_STATION:
|
|
|
|
v->dest_tile = v->GetOrderStationLocation(order->GetDestination());
|
2009-07-13 09:18:07 +00:00
|
|
|
return true;
|
2008-06-02 08:33:38 +00:00
|
|
|
|
|
|
|
case OT_GOTO_DEPOT:
|
2010-07-27 21:46:55 +00:00
|
|
|
if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !v->NeedsServicing()) {
|
|
|
|
UpdateVehicleTimetable(v, true);
|
|
|
|
v->IncrementOrderIndex();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-06-02 08:33:38 +00:00
|
|
|
if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
|
|
|
|
/* We need to search for the nearest depot (hangar). */
|
|
|
|
TileIndex location;
|
|
|
|
DestinationID destination;
|
|
|
|
bool reverse;
|
|
|
|
|
|
|
|
if (v->FindClosestDepot(&location, &destination, &reverse)) {
|
|
|
|
v->dest_tile = location;
|
2009-05-02 00:10:24 +00:00
|
|
|
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());
|
2008-06-02 08:33:38 +00:00
|
|
|
|
|
|
|
/* If there is no depot in front, reverse automatically (trains only) */
|
|
|
|
if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
|
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
if (v->type == VEH_AIRCRAFT) {
|
2009-06-06 16:54:22 +00:00
|
|
|
Aircraft *a = Aircraft::From(v);
|
2009-05-22 20:07:26 +00:00
|
|
|
if (a->state == FLYING && a->targetairport != destination) {
|
|
|
|
/* The aircraft is now heading for a different hangar than the next in the orders */
|
|
|
|
extern void AircraftNextAirportPos_and_Order(Aircraft *a);
|
|
|
|
AircraftNextAirportPos_and_Order(a);
|
|
|
|
}
|
2008-06-02 08:33:38 +00:00
|
|
|
}
|
2009-07-13 09:18:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-05-28 21:12:54 +00:00
|
|
|
|
2009-07-13 09:18:07 +00:00
|
|
|
UpdateVehicleTimetable(v, true);
|
|
|
|
v->IncrementOrderIndex();
|
|
|
|
} else {
|
|
|
|
if (v->type != VEH_AIRCRAFT) {
|
|
|
|
v->dest_tile = Depot::Get(order->GetDestination())->xy;
|
2008-06-02 08:33:38 +00:00
|
|
|
}
|
2009-07-13 09:18:07 +00:00
|
|
|
return true;
|
2008-06-02 08:33:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OT_GOTO_WAYPOINT:
|
2009-07-24 15:18:25 +00:00
|
|
|
v->dest_tile = Waypoint::Get(order->GetDestination())->xy;
|
2009-07-13 09:18:07 +00:00
|
|
|
return true;
|
2008-06-02 08:33:38 +00:00
|
|
|
|
|
|
|
case OT_CONDITIONAL: {
|
|
|
|
VehicleOrderID next_order = ProcessConditionalOrder(order, v);
|
|
|
|
if (next_order != INVALID_VEH_ORDER_ID) {
|
2008-07-14 21:01:49 +00:00
|
|
|
UpdateVehicleTimetable(v, false);
|
2008-06-02 08:33:38 +00:00
|
|
|
v->cur_order_index = next_order;
|
2009-05-23 12:27:42 +00:00
|
|
|
v->current_order_time += v->GetOrder(next_order)->travel_time;
|
2008-06-02 08:33:38 +00:00
|
|
|
} else {
|
2008-07-14 21:01:49 +00:00
|
|
|
UpdateVehicleTimetable(v, true);
|
2009-05-09 13:37:18 +00:00
|
|
|
v->IncrementOrderIndex();
|
2008-06-02 08:33:38 +00:00
|
|
|
}
|
2009-07-13 09:18:07 +00:00
|
|
|
break;
|
2008-06-02 08:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
v->dest_tile = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-13 09:18:07 +00:00
|
|
|
|
|
|
|
assert(v->cur_order_index < v->GetNumOrders());
|
|
|
|
|
|
|
|
/* Get the current order */
|
|
|
|
order = v->GetOrder(v->cur_order_index);
|
|
|
|
v->current_order = *order;
|
|
|
|
return UpdateOrderDest(v, order, conditional_depth + 1);
|
2008-06-02 08:33:38 +00:00
|
|
|
}
|
|
|
|
|
2008-04-05 10:55:50 +00:00
|
|
|
/**
|
|
|
|
* Handle the orders of a vehicle and determine the next place
|
|
|
|
* to go to if needed.
|
|
|
|
* @param v the vehicle to do this for.
|
|
|
|
* @return true *if* the vehicle is eligible for reversing
|
|
|
|
* (basically only when leaving a station).
|
|
|
|
*/
|
|
|
|
bool ProcessOrders(Vehicle *v)
|
|
|
|
{
|
2008-04-05 23:36:54 +00:00
|
|
|
switch (v->current_order.GetType()) {
|
2008-04-05 10:55:50 +00:00
|
|
|
case OT_GOTO_DEPOT:
|
|
|
|
/* Let a depot order in the orderlist interrupt. */
|
2008-04-07 19:18:56 +00:00
|
|
|
if (!(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return false;
|
2008-04-05 10:55:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OT_LOADING:
|
|
|
|
return false;
|
|
|
|
|
2008-04-05 12:01:34 +00:00
|
|
|
case OT_LEAVESTATION:
|
|
|
|
if (v->type != VEH_AIRCRAFT) return false;
|
|
|
|
break;
|
|
|
|
|
2008-04-05 10:55:50 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reversing because of order change is allowed only just after leaving a
|
|
|
|
* station (and the difficulty setting to allowed, of course)
|
|
|
|
* this can be detected because only after OT_LEAVESTATION, current_order
|
|
|
|
* will be reset to nothing. (That also happens if no order, but in that case
|
|
|
|
* it won't hit the point in code where may_reverse is checked)
|
|
|
|
*/
|
2008-04-05 23:36:54 +00:00
|
|
|
bool may_reverse = v->current_order.IsType(OT_NOTHING);
|
2008-04-05 10:55:50 +00:00
|
|
|
|
2010-08-20 15:30:21 +00:00
|
|
|
/* Check if we've reached a 'via' destination. */
|
2009-07-24 15:18:25 +00:00
|
|
|
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)) &&
|
2008-04-05 10:55:50 +00:00
|
|
|
IsTileType(v->tile, MP_STATION) &&
|
2008-04-06 07:48:51 +00:00
|
|
|
v->current_order.GetDestination() == GetStationIndex(v->tile)) {
|
2010-08-20 15:30:21 +00:00
|
|
|
/* We set the last visited station here because we do not want
|
|
|
|
* the train to stop at this 'via' station if the next order
|
|
|
|
* is a no-non-stop order; in that case not setting the last
|
|
|
|
* visited station will cause the vehicle to still stop. */
|
|
|
|
v->last_station_visited = v->current_order.GetDestination();
|
2008-04-05 10:55:50 +00:00
|
|
|
UpdateVehicleTimetable(v, true);
|
2009-05-09 13:37:18 +00:00
|
|
|
v->IncrementOrderIndex();
|
2008-04-05 10:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the current order */
|
2009-01-03 13:20:32 +00:00
|
|
|
if (v->cur_order_index >= v->GetNumOrders()) v->cur_order_index = 0;
|
2008-04-05 10:55:50 +00:00
|
|
|
|
2009-05-23 12:27:42 +00:00
|
|
|
const Order *order = v->GetOrder(v->cur_order_index);
|
2008-04-05 10:55:50 +00:00
|
|
|
|
|
|
|
/* If no order, do nothing. */
|
2009-12-23 18:43:38 +00:00
|
|
|
if (order == NULL || (v->type == VEH_AIRCRAFT && !CheckForValidOrders(v))) {
|
2008-04-05 12:01:34 +00:00
|
|
|
if (v->type == VEH_AIRCRAFT) {
|
|
|
|
/* Aircraft do something vastly different here, so handle separately */
|
2009-05-22 20:03:26 +00:00
|
|
|
extern void HandleMissingAircraftOrders(Aircraft *v);
|
2009-06-06 16:54:22 +00:00
|
|
|
HandleMissingAircraftOrders(Aircraft::From(v));
|
2008-04-05 12:01:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-05 10:55:50 +00:00
|
|
|
v->current_order.Free();
|
|
|
|
v->dest_tile = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it is unchanged, keep it. */
|
2008-04-13 10:26:39 +00:00
|
|
|
if (order->Equals(v->current_order) && (v->type == VEH_AIRCRAFT || v->dest_tile != 0) &&
|
2009-05-16 23:34:14 +00:00
|
|
|
(v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || Station::Get(order->GetDestination())->dock_tile != INVALID_TILE)) {
|
2008-04-05 10:55:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise set it, and determine the destination tile. */
|
|
|
|
v->current_order = *order;
|
|
|
|
|
2009-09-02 17:54:40 +00:00
|
|
|
InvalidateVehicleOrder(v, -2);
|
2008-04-05 10:55:50 +00:00
|
|
|
switch (v->type) {
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
|
|
|
|
case VEH_ROAD:
|
|
|
|
case VEH_TRAIN:
|
|
|
|
break;
|
|
|
|
|
2008-04-05 12:01:34 +00:00
|
|
|
case VEH_AIRCRAFT:
|
2008-04-05 10:55:50 +00:00
|
|
|
case VEH_SHIP:
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowClassesDirty(GetWindowClassForVehicleType(v->type));
|
2008-04-05 10:55:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-06-02 08:33:38 +00:00
|
|
|
return UpdateOrderDest(v, order) && may_reverse;
|
2008-04-05 10:55:50 +00:00
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2008-04-05 15:30:15 +00:00
|
|
|
/**
|
|
|
|
* Check whether the given vehicle should stop at the given station
|
|
|
|
* based on this order and the non-stop settings.
|
|
|
|
* @param v the vehicle that might be stopping.
|
|
|
|
* @param station the station to stop at.
|
|
|
|
* @return true if the vehicle should stop.
|
|
|
|
*/
|
|
|
|
bool Order::ShouldStopAtStation(const Vehicle *v, StationID station) const
|
|
|
|
{
|
2008-04-15 12:25:35 +00:00
|
|
|
bool is_dest_station = this->IsType(OT_GOTO_STATION) && this->dest == station;
|
2008-04-05 15:30:15 +00:00
|
|
|
return
|
2008-05-02 08:15:36 +00:00
|
|
|
(!this->IsType(OT_GOTO_DEPOT) || (this->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0) &&
|
2008-04-05 15:30:15 +00:00
|
|
|
v->last_station_visited != station && // Do stop only when we've not just been there
|
2008-04-07 08:59:04 +00:00
|
|
|
/* Finally do stop when there is no non-stop flag set for this type of station. */
|
2008-04-15 12:25:35 +00:00
|
|
|
!(this->GetNonStopType() & (is_dest_station ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));
|
2008-04-05 15:30:15 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitializeOrders()
|
2005-01-15 19:06:22 +00:00
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
_order_pool.CleanPool();
|
|
|
|
_orderlist_pool.CleanPool();
|
2005-01-15 19:06:22 +00:00
|
|
|
}
|