src/order_cmd.cpp
author convert-repo
Mon, 07 Apr 2008 16:21:55 +0000
changeset 10076 dfd70e42c4ae
parent 9347 e57f43150eaa
child 10079 99aba130db3c
permissions -rw-r--r--
update tags
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6677
0578c2e31ed1 (svn r9390) -Documentation : correct Doxygen of comments and @file inclusion. This time, brought to you by the letter O
belugas
parents: 6663
diff changeset
     3
/** @file order_cmd.cpp */
0578c2e31ed1 (svn r9390) -Documentation : correct Doxygen of comments and @file inclusion. This time, brought to you by the letter O
belugas
parents: 6663
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1884
diff changeset
     6
#include "openttd.h"
9280
9c03416d26b1 (svn r12488) -Codechange: split order.h into order_base.h and order_func.h.
rubidium
parents: 9263
diff changeset
     7
#include "order_base.h"
9c03416d26b1 (svn r12488) -Codechange: split order.h into order_base.h and order_func.h.
rubidium
parents: 9263
diff changeset
     8
#include "order_func.h"
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
     9
#include "airport.h"
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
    10
#include "depot.h"
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
    11
#include "waypoint.h"
8612
6414fc21c2f3 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8610
diff changeset
    12
#include "command_func.h"
8750
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8710
diff changeset
    13
#include "player_func.h"
9259
088d3649dd4f (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 9175
diff changeset
    14
#include "news_func.h"
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
    15
#include "saveload.h"
1752
cdbfb2f23e72 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1751
diff changeset
    16
#include "vehicle_gui.h"
6655
951b0ba32eb1 (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6589
diff changeset
    17
#include "cargotype.h"
8179
1fa8cc3fbeb8 (svn r11214) -Fix [FS#1296]: planes can't use heliports so refuse these orders
glx
parents: 8149
diff changeset
    18
#include "aircraft.h"
8610
17cc343a23dd (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8560
diff changeset
    19
#include "strings_func.h"
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8612
diff changeset
    20
#include "core/alloc_func.hpp"
8627
448ebf3a8291 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8626
diff changeset
    21
#include "functions.h"
448ebf3a8291 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8626
diff changeset
    22
#include "window_func.h"
8707
55835d8fbfcd (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8640
diff changeset
    23
#include "settings_type.h"
8710
52015340050c (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8707
diff changeset
    24
#include "string_func.h"
9283
624272490f5a (svn r12495) -Codechange: reduce the dependency on newgrf_station.h (especially because newgrf_station.h includes a lot of stuff).
rubidium
parents: 9281
diff changeset
    25
#include "newgrf_cargo.h"
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
    26
#include "timetable.h"
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
    27
#include "vehicle_func.h"
9343
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents: 9341
diff changeset
    28
#include "oldpool_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
8760
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8754
diff changeset
    30
#include "table/strings.h"
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8754
diff changeset
    31
9263
0f1ba0ce5839 (svn r12466) -Codechange: move DestinationID to a more logical location.
rubidium
parents: 9259
diff changeset
    32
/* DestinationID must be at least as large as every these below, because it can
0f1ba0ce5839 (svn r12466) -Codechange: move DestinationID to a more logical location.
rubidium
parents: 9259
diff changeset
    33
 * be any of them
0f1ba0ce5839 (svn r12466) -Codechange: move DestinationID to a more logical location.
rubidium
parents: 9259
diff changeset
    34
 */
0f1ba0ce5839 (svn r12466) -Codechange: move DestinationID to a more logical location.
rubidium
parents: 9259
diff changeset
    35
assert_compile(sizeof(DestinationID) >= sizeof(DepotID));
0f1ba0ce5839 (svn r12466) -Codechange: move DestinationID to a more logical location.
rubidium
parents: 9259
diff changeset
    36
assert_compile(sizeof(DestinationID) >= sizeof(WaypointID));
0f1ba0ce5839 (svn r12466) -Codechange: move DestinationID to a more logical location.
rubidium
parents: 9259
diff changeset
    37
assert_compile(sizeof(DestinationID) >= sizeof(StationID));
0f1ba0ce5839 (svn r12466) -Codechange: move DestinationID to a more logical location.
rubidium
parents: 9259
diff changeset
    38
8764
65746a5248ec (svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents: 8760
diff changeset
    39
TileIndex _backup_orders_tile;
65746a5248ec (svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents: 8760
diff changeset
    40
BackuppedOrders _backup_orders_data;
65746a5248ec (svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents: 8760
diff changeset
    41
9344
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    42
DEFINE_OLD_POOL_GENERIC(Order, Order);
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    43
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    44
OrderNonStopFlags Order::GetNonStopType() const
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    45
{
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    46
	return (this->flags & 0x8) ?
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    47
			((!_patches.new_nonstop || !this->IsType(OT_GOTO_STATION)) ?
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    48
					ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS :
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    49
					ONSF_NO_STOP_AT_DESTINATION_STATION) :
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    50
			ONSF_STOP_EVERYWHERE;
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    51
}
1314
d6a253cf92c3 (svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents: 1297
diff changeset
    52
9330
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
    53
void Order::Free()
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
    54
{
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
    55
	this->type  = OT_NOTHING;
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
    56
	this->flags = 0;
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
    57
	this->dest  = 0;
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
    58
	this->next  = NULL;
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
    59
}
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
    60
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    61
void Order::MakeGoToStation(StationID destination)
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    62
{
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    63
	this->type = OT_GOTO_STATION;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    64
	this->flags = 0;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    65
	this->dest = destination;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    66
}
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    67
9334
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9332
diff changeset
    68
void Order::MakeGoToDepot(DepotID destination, bool order, CargoID cargo, byte subtype)
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    69
{
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    70
	this->type = OT_GOTO_DEPOT;
9344
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    71
	this->flags = 0;
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    72
	if (order) {
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    73
		this->SetDepotOrderType(OFB_PART_OF_ORDERS);
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    74
	} else {
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    75
		this->SetNonStopType(ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
    76
	}
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    77
	this->dest = destination;
9334
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9332
diff changeset
    78
	this->SetRefit(cargo, subtype);
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    79
}
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    80
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    81
void Order::MakeGoToWaypoint(WaypointID destination)
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    82
{
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    83
	this->type = OT_GOTO_WAYPOINT;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    84
	this->flags = 0;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    85
	this->dest = destination;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    86
}
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    87
9339
daaa2adbaf99 (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 9336
diff changeset
    88
void Order::MakeLoading(bool ordered)
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    89
{
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    90
	this->type = OT_LOADING;
9339
daaa2adbaf99 (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 9336
diff changeset
    91
	if (!ordered) this->flags = 0;
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    92
}
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    93
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    94
void Order::MakeLeaveStation()
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    95
{
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    96
	this->type = OT_LEAVESTATION;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    97
	this->flags = 0;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    98
}
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
    99
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   100
void Order::MakeDummy()
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   101
{
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   102
	this->type = OT_DUMMY;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   103
	this->flags = 0;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   104
}
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   105
9334
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9332
diff changeset
   106
void Order::SetRefit(CargoID cargo, byte subtype)
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9332
diff changeset
   107
{
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9332
diff changeset
   108
	this->refit_cargo = cargo;
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9332
diff changeset
   109
	this->refit_subtype = subtype;
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9332
diff changeset
   110
}
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9332
diff changeset
   111
9330
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   112
void Order::FreeChain()
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   113
{
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   114
	if (next != NULL) next->FreeChain();
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   115
	delete this;
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   116
}
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   117
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   118
bool Order::Equals(const Order &other) const
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   119
{
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   120
	return
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   121
			this->type  == other.type &&
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   122
			this->flags == other.flags &&
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   123
			this->dest  == other.dest;
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   124
}
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   125
9330
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   126
static bool HasOrderPoolFree(uint amount)
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   127
{
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   128
	const Order *order;
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   129
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   130
	/* There is always room if not all blocks in the pool are reserved */
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   131
	if (_Order_pool.CanAllocateMoreBlocks()) return true;
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   132
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   133
	FOR_ALL_ORDERS(order) if (!order->IsValid() && --amount == 0) return true;
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   134
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   135
	return false;
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   136
}
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   137
9335
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   138
uint32 Order::Pack() const
9330
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   139
{
9335
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   140
	return this->dest << 16 | this->flags << 8 | this->type;
9330
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   141
}
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   142
9335
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   143
Order::Order(uint32 packed)
9330
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   144
{
9335
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   145
	this->type    = (OrderType)GB(packed,  0,  8);
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   146
	this->flags   = GB(packed,  8,  8);
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   147
	this->dest    = GB(packed, 16, 16);
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   148
	this->next    = NULL;
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   149
	this->refit_cargo   = CT_NO_REFIT;
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   150
	this->refit_subtype = 0;
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   151
	this->wait_time     = 0;
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   152
	this->travel_time   = 0;
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   153
}
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   154
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   155
/**
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   156
 *
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   157
 * Unpacks a order from savegames with version 4 and lower
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   158
 *
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   159
 */
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   160
static Order UnpackVersion4Order(uint16 packed)
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   161
{
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   162
	return Order(GB(packed, 8, 8) << 16 | GB(packed, 4, 4) << 8 | GB(packed, 0, 4));
9330
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   163
}
828cc0ca9654 (svn r12582) -Codechange: move some (needlessly) static inlined functions from order_base.h to order_cmd.cpp.
rubidium
parents: 9329
diff changeset
   164
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   165
/**
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   166
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   167
 * Unpacks a order from savegames made with TTD(Patch)
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   168
 *
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
 */
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   170
Order UnpackOldOrder(uint16 packed)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
{
9335
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   172
	Order order = UnpackVersion4Order(packed);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
9335
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   174
	/*
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   175
	 * Sanity check
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   176
	 * TTD stores invalid orders as OT_NOTHING with non-zero flags/station
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   177
	 */
9339
daaa2adbaf99 (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 9336
diff changeset
   178
	if (!order.IsValid() && (order.GetLoadType() != 0 || order.GetUnloadType() != 0 || order.GetDestination() != 0)) {
9335
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   179
		order.MakeDummy();
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   180
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   182
	return order;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   183
}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   184
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   185
/**
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   186
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   187
 * Updates the widgets of a vehicle which contains the order-data
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   188
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   189
 */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   190
void InvalidateVehicleOrder(const Vehicle *v)
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   191
{
7476
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7446
diff changeset
   192
	InvalidateWindow(WC_VEHICLE_VIEW,      v->index);
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7446
diff changeset
   193
	InvalidateWindow(WC_VEHICLE_ORDERS,    v->index);
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7446
diff changeset
   194
	InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   195
}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   196
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   197
/**
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   198
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   199
 * Swap two orders
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   200
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   201
 */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   202
static void SwapOrders(Order *order1, Order *order2)
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   203
{
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   204
	Order temp_order;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   205
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   206
	temp_order = *order1;
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   207
	order1->AssignOrder(*order2);
1043
123072ba6ced (svn r1544) -Fix: SwapOrder did not use AssignOrder, which caused the saveroutine to
truelight
parents: 1024
diff changeset
   208
	order1->next = order2->next;
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   209
	order2->AssignOrder(temp_order);
1043
123072ba6ced (svn r1544) -Fix: SwapOrder did not use AssignOrder, which caused the saveroutine to
truelight
parents: 1024
diff changeset
   210
	order2->next = temp_order.next;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   211
}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   212
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   213
/**
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   214
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   215
 * Assign data to an order (from an other order)
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   216
 *   This function makes sure that the index is maintained correctly
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   217
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   218
 */
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   219
void Order::AssignOrder(const Order &other)
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   220
{
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   221
	this->type  = other.type;
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   222
	this->flags = other.flags;
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   223
	this->dest  = other.dest;
4712
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   224
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   225
	this->refit_cargo   = other.refit_cargo;
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   226
	this->refit_subtype = other.refit_subtype;
7476
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7446
diff changeset
   227
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   228
	this->wait_time   = other.wait_time;
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   229
	this->travel_time = other.travel_time;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   230
}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   231
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   232
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   233
/**
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   234
 * Delete all news items regarding defective orders about a vehicle
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   235
 * This could kill still valid warnings (for example about void order when just
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   236
 * another order gets added), but assume the player will notice the problems,
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   237
 * when (s)he's changing the orders.
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   238
 */
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   239
static void DeleteOrderWarnings(const Vehicle* v)
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   240
{
6206
67358999d80d (svn r8621) -Codechange: assigned new numbers to the VEH_(type) enum so that VEH_Train is 0, VEH_Road is 1 and so on
bjarni
parents: 6173
diff changeset
   241
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_TOO_FEW_ORDERS  + v->type * 4);
67358999d80d (svn r8621) -Codechange: assigned new numbers to the VEH_(type) enum so that VEH_Train is 0, VEH_Road is 1 and so on
bjarni
parents: 6173
diff changeset
   242
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_VOID_ORDER      + v->type * 4);
67358999d80d (svn r8621) -Codechange: assigned new numbers to the VEH_(type) enum so that VEH_Train is 0, VEH_Road is 1 and so on
bjarni
parents: 6173
diff changeset
   243
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_DUPLICATE_ENTRY + v->type * 4);
67358999d80d (svn r8621) -Codechange: assigned new numbers to the VEH_(type) enum so that VEH_Train is 0, VEH_Road is 1 and so on
bjarni
parents: 6173
diff changeset
   244
	DeleteVehicleNews(v->index, STR_TRAIN_HAS_INVALID_ENTRY   + v->type * 4);
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   245
}
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   246
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   247
6397
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   248
static TileIndex GetOrderLocation(const Order& o)
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   249
{
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   250
	switch (o.GetType()) {
6397
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   251
		default: NOT_REACHED();
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   252
		case OT_GOTO_STATION: return GetStation(o.GetDestination())->xy;
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   253
		case OT_GOTO_DEPOT:   return GetDepot(o.GetDestination())->xy;
6397
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   254
	}
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   255
}
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   256
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   257
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   258
/** Add an order to the orderlist of a vehicle.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3222
diff changeset
   259
 * @param tile unused
6977
67b989528f3d (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6696
diff changeset
   260
 * @param flags operation to perform
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   261
 * @param p1 various bitstuffed elements
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2089
diff changeset
   262
 * - p1 = (bit  0 - 15) - ID of the vehicle
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   263
 * - p1 = (bit 16 - 31) - the selected order (if any). If the last order is given,
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2089
diff changeset
   264
 *                        the order will be inserted before that one
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2089
diff changeset
   265
 *                        only the first 8 bits used currently (bit 16 - 23) (max 255)
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   266
 * @param p2 packed order to insert
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   267
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7328
diff changeset
   268
CommandCost CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   269
{
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   270
	Vehicle *v;
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2089
diff changeset
   271
	VehicleID veh   = GB(p1,  0, 16);
4391
7bfd170bfdf5 (svn r6144) -Codechange: renamed OrderID to VehicleOrderID, because it had nothing to do
truelight
parents: 4389
diff changeset
   272
	VehicleOrderID sel_ord = GB(p1, 16, 16);
9335
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
   273
	Order new_order(p2);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   274
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   275
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   276
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   277
	v = GetVehicle(veh);
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   278
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   279
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   280
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   281
	/* Check if the inserted order is to the correct destination (owner, type),
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   282
	 * and has the correct flags if any */
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   283
	switch (new_order.GetType()) {
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   284
		case OT_GOTO_STATION: {
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   285
			if (!IsValidStationID(new_order.GetDestination())) return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   286
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   287
			const Station *st = GetStation(new_order.GetDestination());
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   288
6391
72e150143c15 (svn r8795) -Fix
tron
parents: 6374
diff changeset
   289
			if (st->owner != OWNER_NONE && !CheckOwnership(st->owner)) {
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   290
				return CMD_ERROR;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   291
			}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   292
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   293
			switch (v->type) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   294
				case VEH_TRAIN:
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   295
					if (!(st->facilities & FACIL_TRAIN)) return CMD_ERROR;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   296
					break;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   297
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   298
				case VEH_ROAD:
6655
951b0ba32eb1 (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6589
diff changeset
   299
					if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) {
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   300
						if (!(st->facilities & FACIL_BUS_STOP)) return CMD_ERROR;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   301
					} else {
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   302
						if (!(st->facilities & FACIL_TRUCK_STOP)) return CMD_ERROR;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   303
					}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   304
					break;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   305
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   306
				case VEH_SHIP:
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   307
					if (!(st->facilities & FACIL_DOCK)) return CMD_ERROR;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   308
					break;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   309
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   310
				case VEH_AIRCRAFT:
8179
1fa8cc3fbeb8 (svn r11214) -Fix [FS#1296]: planes can't use heliports so refuse these orders
glx
parents: 8149
diff changeset
   311
					if (!(st->facilities & FACIL_AIRPORT) || !CanAircraftUseStation(v->engine_type, st)) {
1fa8cc3fbeb8 (svn r11214) -Fix [FS#1296]: planes can't use heliports so refuse these orders
glx
parents: 8149
diff changeset
   312
						return CMD_ERROR;
1fa8cc3fbeb8 (svn r11214) -Fix [FS#1296]: planes can't use heliports so refuse these orders
glx
parents: 8149
diff changeset
   313
					}
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   314
					break;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   315
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   316
				default: return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   317
			}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   318
9344
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
   319
			if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
9341
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   320
2433
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   321
			/* Order flags can be any of the following for stations:
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   322
			 * [full-load | unload] [+ transfer] [+ non-stop]
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   323
			 * non-stop orders (if any) are only valid for trains */
9341
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   324
			switch (new_order.GetLoadType() | new_order.GetUnloadType()) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   325
				case 0:
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8764
diff changeset
   326
				case OFB_FULL_LOAD:
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8764
diff changeset
   327
				case OFB_FULL_LOAD | OFB_TRANSFER:
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8764
diff changeset
   328
				case OFB_UNLOAD:
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8764
diff changeset
   329
				case OFB_UNLOAD | OFB_TRANSFER:
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8764
diff changeset
   330
				case OFB_TRANSFER:
2433
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   331
					break;
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   332
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   333
				default: return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   334
			}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   335
			break;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   336
		}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   337
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   338
		case OT_GOTO_DEPOT: {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   339
			if (v->type == VEH_AIRCRAFT) {
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   340
				if (!IsValidStationID(new_order.GetDestination())) return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   341
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   342
				const Station *st = GetStation(new_order.GetDestination());
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   343
6357
a09d43cd3e6c (svn r8754) -Fix
tron
parents: 6206
diff changeset
   344
				if (!CheckOwnership(st->owner) ||
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   345
						!(st->facilities & FACIL_AIRPORT) ||
8179
1fa8cc3fbeb8 (svn r11214) -Fix [FS#1296]: planes can't use heliports so refuse these orders
glx
parents: 8149
diff changeset
   346
						st->Airport()->nof_depots == 0 ||
1fa8cc3fbeb8 (svn r11214) -Fix [FS#1296]: planes can't use heliports so refuse these orders
glx
parents: 8149
diff changeset
   347
						!CanAircraftUseStation(v->engine_type, st)) {
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   348
					return CMD_ERROR;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   349
				}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   350
			} else {
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   351
				if (!IsValidDepotID(new_order.GetDestination())) return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   352
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   353
				const Depot *dp = GetDepot(new_order.GetDestination());
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   354
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   355
				if (!CheckOwnership(GetTileOwner(dp->xy))) return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   356
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   357
				switch (v->type) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   358
					case VEH_TRAIN:
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   359
						if (!IsTileDepotType(dp->xy, TRANSPORT_RAIL)) return CMD_ERROR;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   360
						break;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   361
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   362
					case VEH_ROAD:
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   363
						if (!IsTileDepotType(dp->xy, TRANSPORT_ROAD)) return CMD_ERROR;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   364
						break;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   365
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   366
					case VEH_SHIP:
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   367
						if (!IsTileDepotType(dp->xy, TRANSPORT_WATER)) return CMD_ERROR;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   368
						break;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   369
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   370
					default: return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   371
				}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   372
			}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   373
9344
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
   374
			if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
9341
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   375
2433
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   376
			/* Order flags can be any of the following for depots:
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   377
			 * order [+ halt] [+ non-stop]
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   378
			 * non-stop orders (if any) are only valid for trains */
9341
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   379
			switch (new_order.GetDepotOrderType() | new_order.GetDepotActionType()) {
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8764
diff changeset
   380
				case OFB_PART_OF_ORDERS:
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8764
diff changeset
   381
				case OFB_PART_OF_ORDERS | OFB_HALT_IN_DEPOT:
2433
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   382
					break;
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   383
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   384
				default: return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   385
			}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   386
			break;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   387
		}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   388
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   389
		case OT_GOTO_WAYPOINT: {
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   390
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   391
			if (v->type != VEH_TRAIN) return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   392
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   393
			if (!IsValidWaypointID(new_order.GetDestination())) return CMD_ERROR;
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   394
			const Waypoint *wp = GetWaypoint(new_order.GetDestination());
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   395
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   396
			if (!CheckOwnership(GetTileOwner(wp->xy))) return CMD_ERROR;
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   397
2433
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   398
			/* Order flags can be any of the following for waypoints:
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   399
			 * [non-stop]
5f55c796b85b (svn r2959) - Fix: [ 1266036 ] Fix [ 1236317 ] Vehicles with and TRANSFER order don't have that order restored (i_dachev). Also disallow non-train type vehicles to have non-stop orders.
Darkvater
parents: 2295
diff changeset
   400
			 * non-stop orders (if any) are only valid for trains */
9344
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
   401
			if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   402
		}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   403
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   404
		default: return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   405
	}
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   406
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   407
	if (sel_ord > v->num_orders) return CMD_ERROR;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   408
6173
c6cf6600a82c (svn r8550) -Fix
tron
parents: 6147
diff changeset
   409
	if (!HasOrderPoolFree(1)) return_cmd_error(STR_8831_NO_MORE_SPACE_FOR_ORDERS);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   410
9047
4dce11366b62 (svn r12129) -Change [FS#1759]: simplified patch settings for pathfinders (Yorick)
smatz
parents: 8965
diff changeset
   411
	if (v->type == VEH_SHIP && IsHumanPlayer(v->owner) && _patches.pathfinder_for_ships != VPF_NPF) {
4dce11366b62 (svn r12129) -Change [FS#1759]: simplified patch settings for pathfinders (Yorick)
smatz
parents: 8965
diff changeset
   412
		/* Make sure the new destination is not too far away from the previous */
6397
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   413
		const Order *prev = NULL;
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   414
		uint n = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 55
diff changeset
   415
6397
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   416
		/* Find the last goto station or depot order before the insert location.
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   417
		 * If the order is to be inserted at the beginning of the order list this
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   418
		 * finds the last order in the list. */
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   419
		for (const Order *o = v->orders; o != NULL; o = o->next) {
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   420
			if (o->IsType(OT_GOTO_STATION) || o->IsType(OT_GOTO_DEPOT)) prev = o;
6397
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   421
			if (++n == sel_ord && prev != NULL) break;
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   422
		}
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   423
		if (prev != NULL) {
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   424
			uint dist = DistanceManhattan(
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   425
				GetOrderLocation(*prev),
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   426
				GetOrderLocation(new_order)
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   427
			);
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   428
			if (dist >= 130) {
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   429
				return_cmd_error(STR_0210_TOO_FAR_FROM_PREVIOUS_DESTINATIO);
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   430
			}
b6aae958da82 (svn r8802) -Fix
tron
parents: 6391
diff changeset
   431
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
		Vehicle *u;
7887
5740662fc5ce (svn r10760) -Codechange: make the order struct use the pool item class as super class.
rubidium
parents: 7545
diff changeset
   436
		Order *new_o = new Order();
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   437
		new_o->AssignOrder(new_order);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 55
diff changeset
   438
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   439
		/* Create new order and link in list */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   440
		if (v->orders == NULL) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   441
			v->orders = new_o;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   442
		} else {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   443
			/* Try to get the previous item (we are inserting above the
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   444
			    selected) */
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   445
			Order *order = GetVehicleOrder(v, sel_ord - 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   447
			if (order == NULL && GetVehicleOrder(v, sel_ord) != NULL) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   448
				/* There is no previous item, so we are altering v->orders itself
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   449
				    But because the orders can be shared, we copy the info over
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   450
				    the v->orders, so we don't have to change the pointers of
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   451
				    all vehicles */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   452
				SwapOrders(v->orders, new_o);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   453
				/* Now update the next pointers */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   454
				v->orders->next = new_o;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   455
			} else if (order == NULL) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   456
				/* 'sel' is a non-existing order, add him to the end */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   457
				order = GetLastVehicleOrder(v);
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   458
				order->next = new_o;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   459
			} else {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   460
				/* Put the new order in between */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   461
				new_o->next = order->next;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   462
				order->next = new_o;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
		}
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 555
diff changeset
   465
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   466
		u = GetFirstVehicleFromSharedList(v);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   467
		DeleteOrderWarnings(u);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   468
		for (; u != NULL; u = u->next_shared) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   469
			/* Increase amount of orders */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   470
			u->num_orders++;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   471
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   472
			/* If the orderlist was empty, assign it */
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   473
			if (u->orders == NULL) u->orders = v->orders;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   474
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   475
			assert(v->orders == u->orders);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   476
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   477
			/* If there is added an order before the current one, we need
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   478
			to update the selected order */
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   479
			if (sel_ord <= u->cur_order_index) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   480
				uint cur = u->cur_order_index + 1;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   481
				/* Check if we don't go out of bound */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   482
				if (cur < u->num_orders)
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   483
					u->cur_order_index = cur;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   484
			}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   485
			/* Update any possible open window of the vehicle */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   486
			InvalidateVehicleOrder(u);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   487
		}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   488
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   489
		/* Make sure to rebuild the whole list */
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 555
diff changeset
   490
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   493
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   494
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   496
/** Declone an order-list
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   497
 * @param *dst delete the orders of this vehicle
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   498
 * @param flags execution flags
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   499
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7328
diff changeset
   500
static CommandCost DecloneOrder(Vehicle *dst, uint32 flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   502
	if (flags & DC_EXEC) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   503
		DeleteVehicleOrders(dst);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   504
		InvalidateVehicleOrder(dst);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 555
diff changeset
   505
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
	}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   507
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
6662
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   510
/**
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   511
 * Remove the VehicleList that shows all the vehicles with the same shared
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   512
 *  orders.
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   513
 */
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   514
static void RemoveSharedOrderVehicleList(Vehicle *v)
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   515
{
6696
3c2120d41b22 (svn r9429) -Fix r9338: when deleting a vehicle which has shared orders with one more vehicle and no orders, segfaulted
truelight
parents: 6677
diff changeset
   516
	assert(v->orders != NULL);
6662
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   517
	WindowClass window_class = WC_NONE;
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   518
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   519
	switch (v->type) {
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   520
		default: NOT_REACHED();
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   521
		case VEH_TRAIN:    window_class = WC_TRAINS_LIST;   break;
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   522
		case VEH_ROAD:     window_class = WC_ROADVEH_LIST;  break;
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   523
		case VEH_SHIP:     window_class = WC_SHIPS_LIST;    break;
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   524
		case VEH_AIRCRAFT: window_class = WC_AIRCRAFT_LIST; break;
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   525
	}
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   526
	DeleteWindowById(window_class, (v->orders->index << 16) | (v->type << 11) | VLW_SHARED_ORDERS | v->owner);
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   527
}
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   528
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   529
/** Delete an order from the orderlist of a vehicle.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3222
diff changeset
   530
 * @param tile unused
6977
67b989528f3d (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6696
diff changeset
   531
 * @param flags operation to perform
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   532
 * @param p1 the ID of the vehicle
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   533
 * @param p2 the order to delete (max 255)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7328
diff changeset
   535
CommandCost CmdDeleteOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
{
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   537
	Vehicle *v, *u;
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   538
	VehicleID veh_id = p1;
4391
7bfd170bfdf5 (svn r6144) -Codechange: renamed OrderID to VehicleOrderID, because it had nothing to do
truelight
parents: 4389
diff changeset
   539
	VehicleOrderID sel_ord = p2;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   540
	Order *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   542
	if (!IsValidVehicleID(veh_id)) return CMD_ERROR;
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   543
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   544
	v = GetVehicle(veh_id);
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   545
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   546
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   547
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   548
	/* If we did not select an order, we maybe want to de-clone the orders */
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   549
	if (sel_ord >= v->num_orders)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
		return DecloneOrder(v, flags);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   552
	order = GetVehicleOrder(v, sel_ord);
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   553
	if (order == NULL) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   555
	if (flags & DC_EXEC) {
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   556
		if (GetVehicleOrder(v, sel_ord - 1) == NULL) {
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   557
			if (GetVehicleOrder(v, sel_ord + 1) != NULL) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   558
				/* First item, but not the last, so we need to alter v->orders
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   559
				    Because we can have shared order, we copy the data
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   560
				    from the next item over the deleted */
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   561
				order = GetVehicleOrder(v, sel_ord + 1);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   562
				SwapOrders(v->orders, order);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   563
			} else {
6662
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   564
				/* XXX -- The system currently can't handle a shared-order vehicle list
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   565
				 *  open when there aren't any orders in the list, so close the window
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   566
				 *  in this case. Of course it needs a better fix later */
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
   567
				RemoveSharedOrderVehicleList(v);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   568
				/* Last item, so clean the list */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   569
				v->orders = NULL;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   570
			}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   571
		} else {
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   572
			GetVehicleOrder(v, sel_ord - 1)->next = order->next;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   573
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   575
		/* Give the item free */
6589
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
   576
		order->Free();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   578
		u = GetFirstVehicleFromSharedList(v);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   579
		DeleteOrderWarnings(u);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   580
		for (; u != NULL; u = u->next_shared) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   581
			u->num_orders--;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   582
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   583
			if (sel_ord < u->cur_order_index)
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   584
				u->cur_order_index--;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   585
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   586
			/* If we removed the last order, make sure the shared vehicles
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   587
			 * also set their orders to NULL */
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   588
			if (v->orders == NULL) u->orders = NULL;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   589
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   590
			assert(v->orders == u->orders);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   591
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   592
			/* NON-stop flag is misused to see if a train is in a station that is
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   593
			 * on his order list or not */
9339
daaa2adbaf99 (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 9336
diff changeset
   594
			if (sel_ord == u->cur_order_index && u->current_order.IsType(OT_LOADING)) {
9344
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
   595
				u->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
			}
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   597
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   598
			/* Update any possible open window of the vehicle */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   599
			InvalidateVehicleOrder(u);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
		}
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 555
diff changeset
   601
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 555
diff changeset
   602
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 55
diff changeset
   604
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   605
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
7290
647d6416cdfe (svn r10033) -Feature [FS#760]: skip to the selected order in the order list when clicking on the "skip" button while pressing CTRL.
rubidium
parents: 7134
diff changeset
   608
/** Goto order of order-list.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3222
diff changeset
   609
 * @param tile unused
6977
67b989528f3d (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6696
diff changeset
   610
 * @param flags operation to perform
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   611
 * @param p1 The ID of the vehicle which order is skipped
7290
647d6416cdfe (svn r10033) -Feature [FS#760]: skip to the selected order in the order list when clicking on the "skip" button while pressing CTRL.
rubidium
parents: 7134
diff changeset
   612
 * @param p2 the selected order to which we want to skip
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   613
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7328
diff changeset
   614
CommandCost CmdSkipToOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
{
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   616
	Vehicle *v;
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   617
	VehicleID veh_id = p1;
7290
647d6416cdfe (svn r10033) -Feature [FS#760]: skip to the selected order in the order list when clicking on the "skip" button while pressing CTRL.
rubidium
parents: 7134
diff changeset
   618
	VehicleOrderID sel_ord = p2;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   619
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   620
	if (!IsValidVehicleID(veh_id)) return CMD_ERROR;
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   621
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   622
	v = GetVehicle(veh_id);
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   623
7290
647d6416cdfe (svn r10033) -Feature [FS#760]: skip to the selected order in the order list when clicking on the "skip" button while pressing CTRL.
rubidium
parents: 7134
diff changeset
   624
	if (!CheckOwnership(v->owner) || sel_ord == v->cur_order_index ||
647d6416cdfe (svn r10033) -Feature [FS#760]: skip to the selected order in the order list when clicking on the "skip" button while pressing CTRL.
rubidium
parents: 7134
diff changeset
   625
			sel_ord >= v->num_orders || v->num_orders < 2) return CMD_ERROR;
1020
1223cd937174 (svn r1521) -Fix: Ship Vehicle Lists are now redrawn correctly
celestar
parents: 919
diff changeset
   626
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
	if (flags & DC_EXEC) {
7290
647d6416cdfe (svn r10033) -Feature [FS#760]: skip to the selected order in the order list when clicking on the "skip" button while pressing CTRL.
rubidium
parents: 7134
diff changeset
   628
		v->cur_order_index = sel_ord;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 55
diff changeset
   629
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   630
		if (v->type == VEH_ROAD) ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
9339
daaa2adbaf99 (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 9336
diff changeset
   632
		if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   634
		InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
	}
1020
1223cd937174 (svn r1521) -Fix: Ship Vehicle Lists are now redrawn correctly
celestar
parents: 919
diff changeset
   636
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   637
	/* We have an aircraft/ship, they have a mini-schedule, so update them all */
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   638
	if (v->type == VEH_AIRCRAFT) InvalidateWindowClasses(WC_AIRCRAFT_LIST);
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   639
	if (v->type == VEH_SHIP) InvalidateWindowClasses(WC_SHIPS_LIST);
1020
1223cd937174 (svn r1521) -Fix: Ship Vehicle Lists are now redrawn correctly
celestar
parents: 919
diff changeset
   640
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   641
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
7328
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   644
/**
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   645
 * Move an order inside the orderlist
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   646
 * @param tile unused
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   647
 * @param p1 the ID of the vehicle
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   648
 * @param p2 order to move and target
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   649
 *           bit 0-15  : the order to move
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   650
 *           bit 16-31 : the target order
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   651
 * @note The target order will move one place down in the orderlist
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   652
 *  if you move the order upwards else it'll move it one place down
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   653
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7328
diff changeset
   654
CommandCost CmdMoveOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
7328
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   655
{
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   656
	VehicleID veh = p1;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   657
	VehicleOrderID moving_order = GB(p2,  0, 16);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   658
	VehicleOrderID target_order = GB(p2, 16, 16);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   659
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   660
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   661
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   662
	Vehicle *v = GetVehicle(veh);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   663
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   664
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   665
	/* Don't make senseless movements */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   666
	if (moving_order >= v->num_orders || target_order >= v->num_orders ||
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   667
			moving_order == target_order || v->num_orders <= 1)
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   668
		return CMD_ERROR;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   669
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   670
	Order *moving_one = GetVehicleOrder(v, moving_order);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   671
	/* Don't move an empty order */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   672
	if (moving_one == NULL) return CMD_ERROR;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   673
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   674
	if (flags & DC_EXEC) {
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   675
		/* Take the moving order out of the pointer-chain */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   676
		Order *one_before = GetVehicleOrder(v, moving_order - 1);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   677
		Order *one_past = GetVehicleOrder(v, moving_order + 1);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   678
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   679
		if (one_before == NULL) {
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   680
			/* First order edit */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   681
			v->orders = moving_one->next;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   682
		} else {
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   683
			one_before->next = moving_one->next;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   684
		}
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   685
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   686
		/* Insert the moving_order again in the pointer-chain */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   687
		one_before = GetVehicleOrder(v, target_order - 1);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   688
		one_past = GetVehicleOrder(v, target_order);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   689
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   690
		moving_one->next = one_past;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   691
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   692
		if (one_before == NULL) {
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   693
			/* first order edit */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   694
			SwapOrders(v->orders, moving_one);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   695
			v->orders->next = moving_one;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   696
		} else {
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   697
			one_before->next = moving_one;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   698
		}
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   699
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   700
		/* Update shared list */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   701
		Vehicle *u = GetFirstVehicleFromSharedList(v);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   702
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   703
		DeleteOrderWarnings(u);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   704
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   705
		for (; u != NULL; u = u->next_shared) {
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   706
			/* Update the first order */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   707
			if (u->orders != v->orders) u->orders = v->orders;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   708
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   709
			/* Update the current order */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   710
			if (u->cur_order_index == moving_order) {
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   711
				u->cur_order_index = target_order;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   712
			} else if(u->cur_order_index > moving_order && u->cur_order_index <= target_order) {
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   713
				u->cur_order_index--;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   714
			} else if(u->cur_order_index < moving_order && u->cur_order_index >= target_order) {
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   715
				u->cur_order_index++;
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   716
			}
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   717
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   718
			assert(v->orders == u->orders);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   719
			/* Update any possible open window of the vehicle */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   720
			InvalidateVehicleOrder(u);
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   721
		}
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   722
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   723
		/* Make sure to rebuild the whole list */
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   724
		RebuildVehicleLists();
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   725
	}
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   726
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   727
	return CommandCost();
7328
8ac64204c17f (svn r10071) -Feature [FS#828]: moving of orders (skidd13).
rubidium
parents: 7290
diff changeset
   728
}
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   729
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   730
/** Modify an order in the orderlist of a vehicle.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3222
diff changeset
   731
 * @param tile unused
6977
67b989528f3d (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6696
diff changeset
   732
 * @param flags operation to perform
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   733
 * @param p1 various bitstuffed elements
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2089
diff changeset
   734
 * - p1 = (bit  0 - 15) - ID of the vehicle
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   735
 * - p1 = (bit 16 - 31) - the selected order (if any). If the last order is given,
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2089
diff changeset
   736
 *                        the order will be inserted before that one
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2089
diff changeset
   737
 *                        only the first 8 bits used currently (bit 16 - 23) (max 255)
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   738
 * @param p2 mode to change the order to (always set)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7328
diff changeset
   740
CommandCost CmdModifyOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
{
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   742
	Vehicle *v;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   743
	Order *order;
4391
7bfd170bfdf5 (svn r6144) -Codechange: renamed OrderID to VehicleOrderID, because it had nothing to do
truelight
parents: 4389
diff changeset
   744
	VehicleOrderID sel_ord = GB(p1, 16, 16); // XXX - automatically truncated to 8 bits.
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2089
diff changeset
   745
	VehicleID veh   = GB(p1,  0, 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   747
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8764
diff changeset
   748
	if (p2 != OF_FULL_LOAD && p2 != OF_UNLOAD && p2 != OF_NON_STOP && p2 != OF_TRANSFER) return CMD_ERROR;
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   749
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   750
	v = GetVehicle(veh);
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   751
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   752
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
   753
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   754
	/* Is it a valid order? */
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   755
	if (sel_ord >= v->num_orders) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   756
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   757
	order = GetVehicleOrder(v, sel_ord);
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   758
	if ((!order->IsType(OT_GOTO_STATION)  || GetStation(order->GetDestination())->IsBuoy()) &&
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   759
			(!order->IsType(OT_GOTO_DEPOT)    || p2 == OF_UNLOAD) &&
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   760
			(!order->IsType(OT_GOTO_WAYPOINT) || p2 != OF_NON_STOP)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   761
		return CMD_ERROR;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   762
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
	if (flags & DC_EXEC) {
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   765
		switch (p2) {
9341
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   766
			case OF_FULL_LOAD:
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   767
				if (order->IsType(OT_GOTO_DEPOT)) {
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   768
					order->SetDepotOrderType(order->GetDepotOrderType() ^ OFB_SERVICE_IF_NEEDED);
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   769
				} else {
9347
e57f43150eaa (svn r12608) -Fix [FS#1909]: pressing 'Load' did not cancel the full load order.
rubidium
parents: 9345
diff changeset
   770
					order->SetLoadType(order->GetLoadType() ^ OFB_FULL_LOAD);
9341
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   771
					order->SetUnloadType(order->GetUnloadType() & ~OFB_UNLOAD);
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   772
				}
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   773
				break;
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   774
			case OF_UNLOAD:
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   775
				order->SetUnloadType(order->GetUnloadType() ^ OFB_UNLOAD);
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   776
				order->SetLoadType(0);
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   777
				break;
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   778
			case OF_NON_STOP:
9344
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
   779
				order->SetNonStopType(order->GetNonStopType() == ONSF_STOP_EVERYWHERE ? ONSF_NO_STOP_AT_ANY_STATION : ONSF_STOP_EVERYWHERE);
9341
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   780
				break;
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   781
			case OF_TRANSFER:
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   782
				order->SetUnloadType(order->GetUnloadType() ^ OFB_TRANSFER);
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   783
				break;
00c8dbaaf0ee (svn r12595) -Codechange: hide Order's flags in the last few cases.
rubidium
parents: 9339
diff changeset
   784
			default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
1760
0aaab0007731 (svn r2264) - Fix: [ 1060686 1187655 ] Changing the full-load flag on the current order doesn't take effect immediately. (glx)
matthijs
parents: 1752
diff changeset
   787
		/* Update the windows and full load flags, also for vehicles that share the same order list */
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   788
		{
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2951
diff changeset
   789
			Vehicle* u;
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2951
diff changeset
   790
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   791
			u = GetFirstVehicleFromSharedList(v);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   792
			DeleteOrderWarnings(u);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
   793
			for (; u != NULL; u = u->next_shared) {
5562
6ad177b6c9ff (svn r7559) -Fix (FS#456): clicking the 'Full Load' button when the 'current' order is selected in the Order GUI and one has instructed the train to go to the depot, via the button in the Train View GUI, changed the depot order by switching from 'service at depot' to 'stop at depot' and vice versa.
rubidium
parents: 5216
diff changeset
   794
				/* Toggle u->current_order "Full load" flag if it changed.
6ad177b6c9ff (svn r7559) -Fix (FS#456): clicking the 'Full Load' button when the 'current' order is selected in the Order GUI and one has instructed the train to go to the depot, via the button in the Train View GUI, changed the depot order by switching from 'service at depot' to 'stop at depot' and vice versa.
rubidium
parents: 5216
diff changeset
   795
				 * However, as the same flag is used for depot orders, check
6ad177b6c9ff (svn r7559) -Fix (FS#456): clicking the 'Full Load' button when the 'current' order is selected in the Order GUI and one has instructed the train to go to the depot, via the button in the Train View GUI, changed the depot order by switching from 'service at depot' to 'stop at depot' and vice versa.
rubidium
parents: 5216
diff changeset
   796
				 * whether we are not going to a depot as there are three
6ad177b6c9ff (svn r7559) -Fix (FS#456): clicking the 'Full Load' button when the 'current' order is selected in the Order GUI and one has instructed the train to go to the depot, via the button in the Train View GUI, changed the depot order by switching from 'service at depot' to 'stop at depot' and vice versa.
rubidium
parents: 5216
diff changeset
   797
				 * cases where the full load flag can be active and only
6ad177b6c9ff (svn r7559) -Fix (FS#456): clicking the 'Full Load' button when the 'current' order is selected in the Order GUI and one has instructed the train to go to the depot, via the button in the Train View GUI, changed the depot order by switching from 'service at depot' to 'stop at depot' and vice versa.
rubidium
parents: 5216
diff changeset
   798
				 * one case where the flag is used for depot orders. In the
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   799
				 * other cases for the OrderTypeByte the flags are not used,
5562
6ad177b6c9ff (svn r7559) -Fix (FS#456): clicking the 'Full Load' button when the 'current' order is selected in the Order GUI and one has instructed the train to go to the depot, via the button in the Train View GUI, changed the depot order by switching from 'service at depot' to 'stop at depot' and vice versa.
rubidium
parents: 5216
diff changeset
   800
				 * so do not care and those orders should not be active
6ad177b6c9ff (svn r7559) -Fix (FS#456): clicking the 'Full Load' button when the 'current' order is selected in the Order GUI and one has instructed the train to go to the depot, via the button in the Train View GUI, changed the depot order by switching from 'service at depot' to 'stop at depot' and vice versa.
rubidium
parents: 5216
diff changeset
   801
				 * when this function is called.
6ad177b6c9ff (svn r7559) -Fix (FS#456): clicking the 'Full Load' button when the 'current' order is selected in the Order GUI and one has instructed the train to go to the depot, via the button in the Train View GUI, changed the depot order by switching from 'service at depot' to 'stop at depot' and vice versa.
rubidium
parents: 5216
diff changeset
   802
				 */
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1760
diff changeset
   803
				if (sel_ord == u->cur_order_index &&
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   804
						!u->current_order.IsType(OT_GOTO_DEPOT) &&
9339
daaa2adbaf99 (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 9336
diff changeset
   805
						u->current_order.GetLoadType() != order->GetLoadType()) {
daaa2adbaf99 (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 9336
diff changeset
   806
					u->current_order.SetLoadType(order->GetLoadType());
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2951
diff changeset
   807
				}
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   808
				InvalidateVehicleOrder(u);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   809
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   810
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   811
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 55
diff changeset
   812
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   813
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   814
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   815
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   816
/** Clone/share/copy an order-list of an other vehicle.
6977
67b989528f3d (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6696
diff changeset
   817
 * @param tile unused
67b989528f3d (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6696
diff changeset
   818
 * @param flags operation to perform
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   819
 * @param p1 various bitstuffed elements
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   820
 * - p1 = (bit  0-15) - destination vehicle to clone orders to (p1 & 0xFFFF)
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   821
 * - p1 = (bit 16-31) - source vehicle to clone orders from, if any (none for CO_UNSHARE)
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   822
 * @param p2 mode of cloning: CO_SHARE, CO_COPY, or CO_UNSHARE
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   823
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7328
diff changeset
   824
CommandCost CmdCloneOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   825
{
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   826
	Vehicle *dst;
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2089
diff changeset
   827
	VehicleID veh_src = GB(p1, 16, 16);
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2089
diff changeset
   828
	VehicleID veh_dst = GB(p1,  0, 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   830
	if (!IsValidVehicleID(veh_dst)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   832
	dst = GetVehicle(veh_dst);
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   833
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   834
	if (!CheckOwnership(dst->owner)) return CMD_ERROR;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   835
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   836
	switch (p2) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   837
		case CO_SHARE: {
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   838
			Vehicle *src;
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   839
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   840
			if (!IsValidVehicleID(veh_src)) return CMD_ERROR;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   841
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   842
			src = GetVehicle(veh_src);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 555
diff changeset
   843
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   844
			/* Sanity checks */
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   845
			if (!CheckOwnership(src->owner) || dst->type != src->type || dst == src)
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   846
				return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   848
			/* Trucks can't share orders with busses (and visa versa) */
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   849
			if (src->type == VEH_ROAD) {
6655
951b0ba32eb1 (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6589
diff changeset
   850
				if (src->cargo_type != dst->cargo_type && (IsCargoInClass(src->cargo_type, CC_PASSENGERS) || IsCargoInClass(dst->cargo_type, CC_PASSENGERS)))
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   851
					return CMD_ERROR;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   852
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 55
diff changeset
   853
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   854
			/* Is the vehicle already in the shared list? */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   855
			{
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2951
diff changeset
   856
				const Vehicle* u;
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2951
diff changeset
   857
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2951
diff changeset
   858
				for (u = GetFirstVehicleFromSharedList(src); u != NULL; u = u->next_shared) {
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2951
diff changeset
   859
					if (u == dst) return CMD_ERROR;
29
b4bf7559cfa7 (svn r30) -Fix [1007272] Copy orders between bus/truck possible. You cannot share orders between busses/trucks. You can only copy orders if all the facilities in schedule allow both types to go there (Celestar)
darkvater
parents: 19
diff changeset
   860
				}
b4bf7559cfa7 (svn r30) -Fix [1007272] Copy orders between bus/truck possible. You cannot share orders between busses/trucks. You can only copy orders if all the facilities in schedule allow both types to go there (Celestar)
darkvater
parents: 19
diff changeset
   861
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   862
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   863
			if (flags & DC_EXEC) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   864
				/* If the destination vehicle had a OrderList, destroy it */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   865
				DeleteVehicleOrders(dst);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   867
				dst->orders = src->orders;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   868
				dst->num_orders = src->num_orders;
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 555
diff changeset
   869
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   870
				/* Link this vehicle in the shared-list */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   871
				dst->next_shared = src->next_shared;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   872
				dst->prev_shared = src;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2817
diff changeset
   873
				if (src->next_shared != NULL) src->next_shared->prev_shared = dst;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   874
				src->next_shared = dst;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   876
				InvalidateVehicleOrder(dst);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   877
				InvalidateVehicleOrder(src);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   878
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   879
				RebuildVehicleLists();
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   880
			}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   881
		} break;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   882
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   883
		case CO_COPY: {
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   884
			Vehicle *src;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   885
			int delta;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   886
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   887
			if (!IsValidVehicleID(veh_src)) return CMD_ERROR;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   888
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   889
			src = GetVehicle(veh_src);
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   890
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   891
			/* Sanity checks */
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   892
			if (!CheckOwnership(src->owner) || dst->type != src->type || dst == src)
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   893
				return CMD_ERROR;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   894
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   895
			/* Trucks can't copy all the orders from busses (and visa versa) */
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
   896
			if (src->type == VEH_ROAD) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   897
				const Order *order;
1297
c828ac28760b (svn r1801) -Fix [Multistop] Fixed a crash that occured when copying orders due to not checking a pointer to be non-NULL
celestar
parents: 1266
diff changeset
   898
				TileIndex required_dst = INVALID_TILE;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   899
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   900
				FOR_VEHICLE_ORDERS(src, order) {
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
   901
					if (order->IsType(OT_GOTO_STATION)) {
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
   902
						const Station *st = GetStation(order->GetDestination());
6655
951b0ba32eb1 (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6589
diff changeset
   903
						if (IsCargoInClass(dst->cargo_type, CC_PASSENGERS)) {
1297
c828ac28760b (svn r1801) -Fix [Multistop] Fixed a crash that occured when copying orders due to not checking a pointer to be non-NULL
celestar
parents: 1266
diff changeset
   904
							if (st->bus_stops != NULL) required_dst = st->bus_stops->xy;
c828ac28760b (svn r1801) -Fix [Multistop] Fixed a crash that occured when copying orders due to not checking a pointer to be non-NULL
celestar
parents: 1266
diff changeset
   905
						} else {
c828ac28760b (svn r1801) -Fix [Multistop] Fixed a crash that occured when copying orders due to not checking a pointer to be non-NULL
celestar
parents: 1266
diff changeset
   906
							if (st->truck_stops != NULL) required_dst = st->truck_stops->xy;
c828ac28760b (svn r1801) -Fix [Multistop] Fixed a crash that occured when copying orders due to not checking a pointer to be non-NULL
celestar
parents: 1266
diff changeset
   907
						}
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   908
						/* This station has not the correct road-bay, so we can't copy! */
1297
c828ac28760b (svn r1801) -Fix [Multistop] Fixed a crash that occured when copying orders due to not checking a pointer to be non-NULL
celestar
parents: 1266
diff changeset
   909
						if (required_dst == INVALID_TILE)
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   910
							return CMD_ERROR;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   911
					}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   912
				}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   913
			}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   914
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   915
			/* make sure there are orders available */
8965
a145f62a6272 (svn r12040) -Codechange: Change IsOrderListShared from a simple function to a class member(MagicBuzz).
belugas
parents: 8869
diff changeset
   916
			delta = dst->IsOrderListShared() ? src->num_orders + 1 : src->num_orders - dst->num_orders;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   917
			if (!HasOrderPoolFree(delta))
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   918
				return_cmd_error(STR_8831_NO_MORE_SPACE_FOR_ORDERS);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   919
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   920
			if (flags & DC_EXEC) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   921
				const Order *order;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   922
				Order **order_dst;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   923
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   924
				/* If the destination vehicle had a OrderList, destroy it */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   925
				DeleteVehicleOrders(dst);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   926
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   927
				order_dst = &dst->orders;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   928
				FOR_VEHICLE_ORDERS(src, order) {
7887
5740662fc5ce (svn r10760) -Codechange: make the order struct use the pool item class as super class.
rubidium
parents: 7545
diff changeset
   929
					*order_dst = new Order();
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
   930
					(*order_dst)->AssignOrder(*order);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   931
					order_dst = &(*order_dst)->next;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   932
				}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   933
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   934
				dst->num_orders = src->num_orders;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   935
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   936
				InvalidateVehicleOrder(dst);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   937
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   938
				RebuildVehicleLists();
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   939
			}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   940
		} break;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   941
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1789
diff changeset
   942
		case CO_UNSHARE: return DecloneOrder(dst, flags);
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
   943
		default: return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   946
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
4712
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   949
/** Add/remove refit orders from an order
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   950
 * @param tile Not used
6977
67b989528f3d (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6696
diff changeset
   951
 * @param flags operation to perform
4712
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   952
 * @param p1 VehicleIndex of the vehicle having the order
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   953
 * @param p2 bitmask
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   954
 *   - bit 0-7 CargoID
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   955
 *   - bit 8-15 Cargo subtype
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   956
 *   - bit 16-23 number of order to modify
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   957
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7328
diff changeset
   958
CommandCost CmdOrderRefit(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
4712
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   959
{
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   960
	const Vehicle *v;
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   961
	Order *order;
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   962
	VehicleID veh = GB(p1, 0, 16);
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   963
	VehicleOrderID order_number  = GB(p2, 16, 8);
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   964
	CargoID cargo = GB(p2, 0, 8);
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   965
	byte subtype  = GB(p2, 8, 8);
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   966
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   967
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   968
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   969
	v = GetVehicle(veh);
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   970
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   971
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   972
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   973
	order = GetVehicleOrder(v, order_number);
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   974
	if (order == NULL) return CMD_ERROR;
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   975
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   976
	if (flags & DC_EXEC) {
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   977
		Vehicle *u;
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   978
9334
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9332
diff changeset
   979
		order->SetRefit(cargo, subtype);
4712
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   980
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   981
		u = GetFirstVehicleFromSharedList(v);
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   982
		for (; u != NULL; u = u->next_shared) {
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   983
			/* Update any possible open window of the vehicle */
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   984
			InvalidateVehicleOrder(u);
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   985
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   986
			/* If the vehicle already got the current depot set as current order, then update current order as well */
9339
daaa2adbaf99 (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 9336
diff changeset
   987
			if (u->cur_order_index == order_number && HasBit(u->current_order.GetDepotOrderType(), OF_PART_OF_ORDERS)) {
9334
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9332
diff changeset
   988
				u->current_order.SetRefit(cargo, subtype);
4712
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   989
			}
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   990
		}
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   991
	}
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   992
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   993
	return CommandCost();
4712
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   994
}
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
   995
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   996
/**
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   997
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   998
 * Backup a vehicle order-list, so you can replace a vehicle
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
   999
 *  without loosing the order-list
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1000
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1001
 */
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1002
void BackupVehicleOrders(const Vehicle *v, BackuppedOrders *bak)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
{
8149
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1004
	/* Make sure we always have freed the stuff */
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1005
	free(bak->order);
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1006
	bak->order = NULL;
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
  1007
	free(bak->name);
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
  1008
	bak->name = NULL;
8149
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1009
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1010
	/* Save general info */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1011
	bak->orderindex       = v->cur_order_index;
9146
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1012
	bak->group            = v->group_id;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
	bak->service_interval = v->service_interval;
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
  1014
	if (v->name != NULL) bak->name = strdup(v->name);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1016
	/* If we have shared orders, store it on a special way */
8965
a145f62a6272 (svn r12040) -Codechange: Change IsOrderListShared from a simple function to a class member(MagicBuzz).
belugas
parents: 8869
diff changeset
  1017
	if (v->IsOrderListShared()) {
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1018
		const Vehicle *u = (v->next_shared) ? v->next_shared : v->prev_shared;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1019
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1020
		bak->clone = u->index;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1021
	} else {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1022
		/* Else copy the orders */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1023
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1024
		/* We do not have shared orders */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1025
		bak->clone = INVALID_VEHICLE;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1026
8149
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1027
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1028
		/* Count the number of orders */
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1029
		uint cnt = 0;
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1030
		const Order *order;
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1031
		FOR_VEHICLE_ORDERS(v, order) cnt++;
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1032
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1033
		/* Allocate memory for the orders plus an end-of-orders marker */
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1034
		bak->order = MallocT<Order>(cnt + 1);
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1035
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1036
		Order *dest = bak->order;
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1037
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1038
		/* Copy the orders */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1039
		FOR_VEHICLE_ORDERS(v, order) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1040
			*dest = *order;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1041
			dest++;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1042
		}
6589
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
  1043
		/* End the list with an empty order */
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
  1044
		dest->Free();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1045
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1046
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1047
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1048
/**
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1049
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1050
 * Restore vehicle orders that are backupped via BackupVehicleOrders
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1051
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1052
 */
8149
eb78fb69096f (svn r11184) -Codechange: cleanup the code related to backup orders.
rubidium
parents: 7897
diff changeset
  1053
void RestoreVehicleOrders(const Vehicle *v, const BackuppedOrders *bak)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
{
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1055
	/* If we have a custom name, process that */
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
  1056
	if (bak->name != NULL) {
1820
9b6458526480 (svn r2324) Introduce _cmd_text for passing strings with a command instead of abusing _decode_parameters as text buffer. This should prevent several possible buffer overruns and is a bit cleaner to use. As bonus it reduces the size of most command packets by 79 bytes.
tron
parents: 1796
diff changeset
  1057
		_cmd_text = bak->name;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
		DoCommandP(0, v->index, 0, NULL, CMD_NAME_VEHICLE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1059
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1060
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1061
	/* If we had shared orders, recover that */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1062
	if (bak->clone != INVALID_VEHICLE) {
8560
8472721356cf (svn r11625) -Codechange: add CO_* enum at some places, add includes of order.h too
smatz
parents: 8553
diff changeset
  1063
		DoCommandP(0, v->index | (bak->clone << 16), CO_SHARE, NULL, CMD_CLONE_ORDER);
8316
1dbc648c7c64 (svn r11370) -Fix [FS1384]: manually replacing a vehicle with shared orders makes it lose it's order index and service interval. Patch by jthill.
rubidium
parents: 8179
diff changeset
  1064
	} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
8316
1dbc648c7c64 (svn r11370) -Fix [FS1384]: manually replacing a vehicle with shared orders makes it lose it's order index and service interval. Patch by jthill.
rubidium
parents: 8179
diff changeset
  1066
		/* CMD_NO_TEST_IF_IN_NETWORK is used here, because CMD_INSERT_ORDER checks if the
1dbc648c7c64 (svn r11370) -Fix [FS1384]: manually replacing a vehicle with shared orders makes it lose it's order index and service interval. Patch by jthill.
rubidium
parents: 8179
diff changeset
  1067
		 *  order number is one more than the current amount of orders, and because
1dbc648c7c64 (svn r11370) -Fix [FS1384]: manually replacing a vehicle with shared orders makes it lose it's order index and service interval. Patch by jthill.
rubidium
parents: 8179
diff changeset
  1068
		 *  in network the commands are queued before send, the second insert always
1dbc648c7c64 (svn r11370) -Fix [FS1384]: manually replacing a vehicle with shared orders makes it lose it's order index and service interval. Patch by jthill.
rubidium
parents: 8179
diff changeset
  1069
		 *  fails in test mode. By bypassing the test-mode, that no longer is a problem. */
1dbc648c7c64 (svn r11370) -Fix [FS1384]: manually replacing a vehicle with shared orders makes it lose it's order index and service interval. Patch by jthill.
rubidium
parents: 8179
diff changeset
  1070
		for (uint i = 0; bak->order[i].IsValid(); i++) {
9335
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
  1071
			if (!DoCommandP(0, v->index + (i << 16), bak->order[i].Pack(), NULL,
9146
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1072
					CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)) {
8316
1dbc648c7c64 (svn r11370) -Fix [FS1384]: manually replacing a vehicle with shared orders makes it lose it's order index and service interval. Patch by jthill.
rubidium
parents: 8179
diff changeset
  1073
				break;
9146
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1074
			}
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1075
9175
5e67fb92d4d6 (svn r12345) -Fix [FS#1828](r12296): don't try to restore backupped timetable when timetabling is disabled
glx
parents: 9146
diff changeset
  1076
			/* Copy timetable if enabled */
5e67fb92d4d6 (svn r12345) -Fix [FS#1828](r12296): don't try to restore backupped timetable when timetabling is disabled
glx
parents: 9146
diff changeset
  1077
			if (_patches.timetabling && !DoCommandP(0, v->index | (i << 16) | (1 << 25),
9146
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1078
					bak->order[i].wait_time << 16 | bak->order[i].travel_time, NULL,
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1079
					CMD_CHANGE_TIMETABLE | CMD_NO_TEST_IF_IN_NETWORK)) {
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1080
				break;
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1081
			}
8316
1dbc648c7c64 (svn r11370) -Fix [FS1384]: manually replacing a vehicle with shared orders makes it lose it's order index and service interval. Patch by jthill.
rubidium
parents: 8179
diff changeset
  1082
		}
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1083
	}
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1084
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1085
	/* Restore vehicle order-index and service interval */
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1086
	DoCommandP(0, v->index, bak->orderindex | (bak->service_interval << 16) , NULL, CMD_RESTORE_ORDER_INDEX);
9146
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1087
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1088
	/* Restore vehicle group */
2c8c94a75544 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 9047
diff changeset
  1089
	DoCommandP(0, bak->group, v->index, NULL, CMD_ADD_VEHICLE_GROUP);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1090
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1091
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1092
/** Restore the current order-index of a vehicle and sets service-interval.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3222
diff changeset
  1093
 * @param tile unused
6977
67b989528f3d (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6696
diff changeset
  1094
 * @param flags operation to perform
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1095
 * @param p1 the ID of the vehicle
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1096
 * @param p2 various bistuffed elements
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1097
 * - p2 = (bit  0-15) - current order-index (p2 & 0xFFFF)
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1098
 * - p2 = (bit 16-31) - service interval (p2 >> 16)
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1099
 * @todo Unfortunately you cannot safely restore the unitnumber or the old vehicle
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1100
 * as far as I can see. We can store it in BackuppedOrders, and restore it, but
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1101
 * but we have no way of seeing it has been tampered with or not, as we have no
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1102
 * legit way of knowing what that ID was.@n
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1103
 * If we do want to backup/restore it, just add UnitID uid to BackuppedOrders, and
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1104
 * restore it as parameter 'y' (ugly hack I know) for example. "v->unitnumber = y;"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1105
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7328
diff changeset
  1106
CommandCost CmdRestoreOrderIndex(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1107
{
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1108
	Vehicle *v;
4391
7bfd170bfdf5 (svn r6144) -Codechange: renamed OrderID to VehicleOrderID, because it had nothing to do
truelight
parents: 4389
diff changeset
  1109
	VehicleOrderID cur_ord = GB(p2,  0, 16);
2635
88b8b74c01ac (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2630
diff changeset
  1110
	uint16 serv_int = GB(p2, 16, 16);
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
  1111
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1112
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1113
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1114
	v = GetVehicle(p1);
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1115
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1116
	/* Check the vehicle type and ownership, and if the service interval and order are in range */
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1117
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1118
	if (serv_int != GetServiceIntervalClamped(serv_int) || cur_ord >= v->num_orders) return CMD_ERROR;
1718
cef0773365eb (svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
tron
parents: 1680
diff changeset
  1119
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
	if (flags & DC_EXEC) {
1796
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1121
		v->cur_order_index = cur_ord;
cae31916ae54 (svn r2300) - CodeChange: check the last number of commands, now only the refit ones remain, and some server-only commands.
Darkvater
parents: 1793
diff changeset
  1122
		v->service_interval = serv_int;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
	}
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1124
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1125
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
}
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 0
diff changeset
  1127
4177
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1128
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1129
static TileIndex GetStationTileForVehicle(const Vehicle* v, const Station* st)
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1130
{
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1131
	switch (v->type) {
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1132
		default: NOT_REACHED();
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
  1133
		case VEH_TRAIN:     return st->train_tile;
8179
1fa8cc3fbeb8 (svn r11214) -Fix [FS#1296]: planes can't use heliports so refuse these orders
glx
parents: 8149
diff changeset
  1134
		case VEH_AIRCRAFT:  return CanAircraftUseStation(v->engine_type, st) ? st->airport_tile : 0;
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
  1135
		case VEH_SHIP:      return st->dock_tile;
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6573
diff changeset
  1136
		case VEH_ROAD:
6655
951b0ba32eb1 (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6589
diff changeset
  1137
			if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) {
4177
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1138
				return (st->bus_stops != NULL) ? st->bus_stops->xy : 0;
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1139
			} else {
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1140
				return (st->truck_stops != NULL) ? st->truck_stops->xy : 0;
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1141
			}
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1142
	}
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1143
}
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1144
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1145
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1146
/**
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1147
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1148
 * Check the orders of a vehicle, to see if there are invalid orders and stuff
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1149
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1150
 */
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1151
void CheckOrders(const Vehicle* v)
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 0
diff changeset
  1152
{
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1153
	/* Does the user wants us to check things? */
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1154
	if (_patches.order_review_system == 0) return;
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 0
diff changeset
  1155
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1156
	/* Do nothing for crashed vehicles */
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1157
	if (v->vehstatus & VS_CRASHED) return;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1158
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1159
	/* Do nothing for stopped vehicles if setting is '1' */
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2433
diff changeset
  1160
	if (_patches.order_review_system == 1 && v->vehstatus & VS_STOPPED)
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1161
		return;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1162
1053
dfb5243315f1 (svn r1554) -Fix: [ 1103187 ] Order Check messages are now validated before
celestar
parents: 1043
diff changeset
  1163
	/* do nothing we we're not the first vehicle in a share-chain */
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1164
	if (v->next_shared != NULL) return;
1053
dfb5243315f1 (svn r1554) -Fix: [ 1103187 ] Order Check messages are now validated before
celestar
parents: 1043
diff changeset
  1165
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1166
	/* Only check every 20 days, so that we don't flood the message log */
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2433
diff changeset
  1167
	if (v->owner == _local_player && v->day_counter % 20 == 0) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1168
		int n_st, problem_type = -1;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1169
		const Order *order;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1170
		int message = 0;
55
73fae6c6568e (svn r56) Improved order checker + patch setting for it (celestar)
dominik
parents: 29
diff changeset
  1171
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1172
		/* Check the order list */
55
73fae6c6568e (svn r56) Improved order checker + patch setting for it (celestar)
dominik
parents: 29
diff changeset
  1173
		n_st = 0;
73fae6c6568e (svn r56) Improved order checker + patch setting for it (celestar)
dominik
parents: 29
diff changeset
  1174
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1175
		FOR_VEHICLE_ORDERS(v, order) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1176
			/* Dummy order? */
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1177
			if (order->IsType(OT_DUMMY)) {
55
73fae6c6568e (svn r56) Improved order checker + patch setting for it (celestar)
dominik
parents: 29
diff changeset
  1178
				problem_type = 1;
73fae6c6568e (svn r56) Improved order checker + patch setting for it (celestar)
dominik
parents: 29
diff changeset
  1179
				break;
73fae6c6568e (svn r56) Improved order checker + patch setting for it (celestar)
dominik
parents: 29
diff changeset
  1180
			}
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1181
			/* Does station have a load-bay for this vehicle? */
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1182
			if (order->IsType(OT_GOTO_STATION)) {
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
  1183
				const Station* st = GetStation(order->GetDestination());
4177
5ec2088edc3a (svn r5615) Move GetStationTileForVehicle() to its only user
tron
parents: 4077
diff changeset
  1184
				TileIndex required_tile = GetStationTileForVehicle(v, st);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1185
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 0
diff changeset
  1186
				n_st++;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1187
				if (required_tile == 0) problem_type = 3;
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 0
diff changeset
  1188
			}
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 0
diff changeset
  1189
		}
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 0
diff changeset
  1190
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1191
		/* Check if the last and the first order are the same */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
  1192
		if (v->num_orders > 1) {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
  1193
			const Order* last = GetLastVehicleOrder(v);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
  1194
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
  1195
			if (v->orders->Equals(*last)) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
  1196
				problem_type = 2;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
  1197
			}
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
  1198
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 55
diff changeset
  1199
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1200
		/* Do we only have 1 station in our order list? */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1201
		if (n_st < 2 && problem_type == -1) problem_type = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 55
diff changeset
  1202
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1203
		/* We don't have a problem */
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1204
		if (problem_type < 0) return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 55
diff changeset
  1205
6206
67358999d80d (svn r8621) -Codechange: assigned new numbers to the VEH_(type) enum so that VEH_Train is 0, VEH_Road is 1 and so on
bjarni
parents: 6173
diff changeset
  1206
		message = STR_TRAIN_HAS_TOO_FEW_ORDERS + (v->type << 2) + problem_type;
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5562
diff changeset
  1207
		//DEBUG(misc, 3, "Triggered News Item for vehicle %d", v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 55
diff changeset
  1208
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1209
		SetDParam(0, v->unitnumber);
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1210
		AddNewsItem(
55
73fae6c6568e (svn r56) Improved order checker + patch setting for it (celestar)
dominik
parents: 29
diff changeset
  1211
			message,
9259
088d3649dd4f (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 9175
diff changeset
  1212
			NM_SMALL, NF_VIEWPORT | NF_VEHICLE, NT_ADVICE, DNC_NONE,
55
73fae6c6568e (svn r56) Improved order checker + patch setting for it (celestar)
dominik
parents: 29
diff changeset
  1213
			v->index,
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1214
			0
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1215
		);
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 0
diff changeset
  1216
	}
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 0
diff changeset
  1217
}
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1218
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1219
/**
4351
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4350
diff changeset
  1220
 * Removes an order from all vehicles. Triggers when, say, a station is removed.
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4350
diff changeset
  1221
 * @param type The type of the order (OT_GOTO_[STATION|DEPOT|WAYPOINT]).
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4350
diff changeset
  1222
 * @param destination The destination. Can be a StationID, DepotID or WaypointID.
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1223
 */
4389
e4555522d257 (svn r6142) -Codechange: added WaypointID (sorry DV, couldn't splits it anymore)
truelight
parents: 4384
diff changeset
  1224
void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination)
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1225
{
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1226
	Vehicle *v;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1227
4528
80cc2712b176 (svn r6355) -Fix: Go to hangar orders for aircraft could get spuriously removed when a road or rail depot got deleted (Darkvater and me)
tron
parents: 4527
diff changeset
  1228
	/* Aircraft have StationIDs for depot orders and never use DepotIDs
80cc2712b176 (svn r6355) -Fix: Go to hangar orders for aircraft could get spuriously removed when a road or rail depot got deleted (Darkvater and me)
tron
parents: 4527
diff changeset
  1229
	 * This fact is handled specially below
80cc2712b176 (svn r6355) -Fix: Go to hangar orders for aircraft could get spuriously removed when a road or rail depot got deleted (Darkvater and me)
tron
parents: 4527
diff changeset
  1230
	 */
80cc2712b176 (svn r6355) -Fix: Go to hangar orders for aircraft could get spuriously removed when a road or rail depot got deleted (Darkvater and me)
tron
parents: 4527
diff changeset
  1231
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1232
	/* Go through all vehicles */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1233
	FOR_ALL_VEHICLES(v) {
4533
5959e77ff4a5 (svn r6360) -Codechange: Polish RemoveOrderFromAllVehicles()
tron
parents: 4528
diff changeset
  1234
		Order *order;
5959e77ff4a5 (svn r6360) -Codechange: Polish RemoveOrderFromAllVehicles()
tron
parents: 4528
diff changeset
  1235
		bool invalidate;
5959e77ff4a5 (svn r6360) -Codechange: Polish RemoveOrderFromAllVehicles()
tron
parents: 4528
diff changeset
  1236
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1237
		/* Forget about this station if this station is removed */
4533
5959e77ff4a5 (svn r6360) -Codechange: Polish RemoveOrderFromAllVehicles()
tron
parents: 4528
diff changeset
  1238
		if (v->last_station_visited == destination && type == OT_GOTO_STATION) {
1266
9dc5638fe8cc (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
  1239
			v->last_station_visited = INVALID_STATION;
4533
5959e77ff4a5 (svn r6360) -Codechange: Polish RemoveOrderFromAllVehicles()
tron
parents: 4528
diff changeset
  1240
		}
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1241
4533
5959e77ff4a5 (svn r6360) -Codechange: Polish RemoveOrderFromAllVehicles()
tron
parents: 4528
diff changeset
  1242
		order = &v->current_order;
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1243
		if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
  1244
				v->current_order.GetDestination() == destination) {
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1245
			order->MakeDummy();
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1246
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1247
		}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1248
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1249
		/* Clear the order from the order-list */
4533
5959e77ff4a5 (svn r6360) -Codechange: Polish RemoveOrderFromAllVehicles()
tron
parents: 4528
diff changeset
  1250
		invalidate = false;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1251
		FOR_VEHICLE_ORDERS(v, order) {
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1252
			if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) ? OT_GOTO_STATION : order->GetType()) == type &&
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
  1253
					order->GetDestination() == destination) {
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1254
				order->MakeDummy();
4533
5959e77ff4a5 (svn r6360) -Codechange: Polish RemoveOrderFromAllVehicles()
tron
parents: 4528
diff changeset
  1255
				invalidate = true;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1256
			}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1257
		}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1258
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1259
		/* Only invalidate once, and if needed */
4533
5959e77ff4a5 (svn r6360) -Codechange: Polish RemoveOrderFromAllVehicles()
tron
parents: 4528
diff changeset
  1260
		if (invalidate) InvalidateWindow(WC_VEHICLE_ORDERS, v->index);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1261
	}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1262
}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1263
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1264
/**
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1265
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1266
 * Checks if a vehicle has a GOTO_DEPOT in his order list
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1267
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1268
 * @return True if this is true (lol ;))
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1269
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1270
 */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1271
bool VehicleHasDepotOrders(const Vehicle *v)
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1272
{
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1273
	const Order *order;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1274
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1275
	FOR_VEHICLE_ORDERS(v, order) {
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1276
		if (order->IsType(OT_GOTO_DEPOT))
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1277
			return true;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1278
	}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1279
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1280
	return false;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1281
}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1282
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1283
/**
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1284
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1285
 * Delete all orders from a vehicle
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1286
 *
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1287
 */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1288
void DeleteVehicleOrders(Vehicle *v)
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1289
{
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
  1290
	DeleteOrderWarnings(v);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3123
diff changeset
  1291
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1292
	/* If we have a shared order-list, don't delete the list, but just
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1293
	    remove our pointer */
8965
a145f62a6272 (svn r12040) -Codechange: Change IsOrderListShared from a simple function to a class member(MagicBuzz).
belugas
parents: 8869
diff changeset
  1294
	if (v->IsOrderListShared()) {
6663
d09762058039 (svn r9338) -Fix: close the Shared Order Vehicle List if you remove the shared link with only 2 vehicles
truelight
parents: 6662
diff changeset
  1295
		Vehicle *u = v;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1296
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1297
		v->orders = NULL;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1298
		v->num_orders = 0;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1299
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1300
		/* Unlink ourself */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1301
		if (v->prev_shared != NULL) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1302
			v->prev_shared->next_shared = v->next_shared;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1303
			u = v->prev_shared;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1304
		}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1305
		if (v->next_shared != NULL) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1306
			v->next_shared->prev_shared = v->prev_shared;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1307
			u = v->next_shared;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1308
		}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1309
		v->prev_shared = NULL;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1310
		v->next_shared = NULL;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1311
6663
d09762058039 (svn r9338) -Fix: close the Shared Order Vehicle List if you remove the shared link with only 2 vehicles
truelight
parents: 6662
diff changeset
  1312
		/* If we are the only one left in the Shared Order Vehicle List,
d09762058039 (svn r9338) -Fix: close the Shared Order Vehicle List if you remove the shared link with only 2 vehicles
truelight
parents: 6662
diff changeset
  1313
		 *  remove it, as we are no longer a Shared Order Vehicle */
6696
3c2120d41b22 (svn r9429) -Fix r9338: when deleting a vehicle which has shared orders with one more vehicle and no orders, segfaulted
truelight
parents: 6677
diff changeset
  1314
		if (u->prev_shared == NULL && u->next_shared == NULL && u->orders != NULL) RemoveSharedOrderVehicleList(u);
6663
d09762058039 (svn r9338) -Fix: close the Shared Order Vehicle List if you remove the shared link with only 2 vehicles
truelight
parents: 6662
diff changeset
  1315
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1316
		/* We only need to update this-one, because if there is a third
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4534
diff changeset
  1317
		 *  vehicle which shares the same order-list, nothing will change. If
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4534
diff changeset
  1318
		 *  this is the last vehicle, the last line of the order-window
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4534
diff changeset
  1319
		 *  will change from Shared order list, to Order list, so it needs
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4534
diff changeset
  1320
		 *  an update */
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1321
		InvalidateVehicleOrder(u);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1322
		return;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1323
	}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1324
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1325
	/* Remove the orders */
6589
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
  1326
	Order *cur = v->orders;
6662
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
  1327
	/* Delete the vehicle list of shared orders, if any */
7ad747545522 (svn r9337) -Fix [FS#542]: assert() when you removed the orders of a shared list while having the shared order vehicle list open.
truelight
parents: 6655
diff changeset
  1328
	if (cur != NULL) RemoveSharedOrderVehicleList(v);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1329
	v->orders = NULL;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1330
	v->num_orders = 0;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1331
4735
cd7be58d0551 (svn r6647) -Fix: [vehicle list windows] Lists of shared orders are now no longer closed by a window event if the list is empty
bjarni
parents: 4715
diff changeset
  1332
	if (cur != NULL) {
6589
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
  1333
		cur->FreeChain(); // Free the orders.
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1334
	}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1335
}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1336
8707
55835d8fbfcd (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8640
diff changeset
  1337
Date GetServiceIntervalClamped(uint index)
55835d8fbfcd (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8640
diff changeset
  1338
{
55835d8fbfcd (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8640
diff changeset
  1339
	return (_patches.servint_ispercent) ? Clamp(index, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : Clamp(index, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
55835d8fbfcd (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8640
diff changeset
  1340
}
55835d8fbfcd (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8640
diff changeset
  1341
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1342
/**
9326
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1343
 *
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1344
 * Check if a vehicle has any valid orders
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1345
 *
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1346
 * @return false if there are no valid orders
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1347
 *
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1348
 */
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1349
static bool CheckForValidOrders(const Vehicle *v)
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1350
{
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1351
	const Order *order;
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1352
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1353
	FOR_VEHICLE_ORDERS(v, order) if (!order->IsType(OT_DUMMY)) return true;
9326
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1354
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1355
	return false;
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1356
}
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1357
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1358
/**
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1359
 * Handle the orders of a vehicle and determine the next place
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1360
 * to go to if needed.
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1361
 * @param v the vehicle to do this for.
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1362
 * @return true *if* the vehicle is eligible for reversing
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1363
 *              (basically only when leaving a station).
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1364
 */
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1365
bool ProcessOrders(Vehicle *v)
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1366
{
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1367
	switch (v->current_order.GetType()) {
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1368
		case OT_GOTO_DEPOT:
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1369
			/* Let a depot order in the orderlist interrupt. */
9339
daaa2adbaf99 (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 9336
diff changeset
  1370
			if (!(v->current_order.GetDepotOrderType() & OFB_PART_OF_ORDERS)) return false;
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1371
9339
daaa2adbaf99 (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 9336
diff changeset
  1372
			if ((v->current_order.GetDepotOrderType() & OFB_SERVICE_IF_NEEDED) && !VehicleNeedsService(v)) {
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1373
				UpdateVehicleTimetable(v, true);
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1374
				v->cur_order_index++;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1375
			}
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1376
			break;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1377
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1378
		case OT_LOADING:
9326
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1379
			return false;
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1380
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1381
		case OT_LEAVESTATION:
9326
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1382
			if (v->type != VEH_AIRCRAFT) return false;
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1383
			break;
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1384
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1385
		default: break;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1386
	}
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1387
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1388
	/**
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1389
	 * Reversing because of order change is allowed only just after leaving a
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1390
	 * station (and the difficulty setting to allowed, of course)
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1391
	 * this can be detected because only after OT_LEAVESTATION, current_order
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1392
	 * will be reset to nothing. (That also happens if no order, but in that case
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1393
	 * it won't hit the point in code where may_reverse is checked)
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1394
	 */
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1395
	bool may_reverse = v->current_order.IsType(OT_NOTHING);
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1396
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1397
	/* Check if we've reached the waypoint? */
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1398
	if (v->current_order.IsType(OT_GOTO_WAYPOINT) && v->tile == v->dest_tile) {
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1399
		UpdateVehicleTimetable(v, true);
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1400
		v->cur_order_index++;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1401
	}
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1402
9344
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
  1403
	/* Check if we've reached a non-stop station.. */
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
  1404
	if ((v->current_order.GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) &&
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1405
			IsTileType(v->tile, MP_STATION) &&
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
  1406
			v->current_order.GetDestination() == GetStationIndex(v->tile)) {
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
  1407
		v->last_station_visited = v->current_order.GetDestination();
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1408
		UpdateVehicleTimetable(v, true);
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1409
		v->cur_order_index++;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1410
	}
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1411
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1412
	/* Get the current order */
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1413
	if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1414
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1415
	const Order *order = GetVehicleOrder(v, v->cur_order_index);
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1416
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1417
	/* If no order, do nothing. */
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1418
	if (order == NULL || (v->type == VEH_AIRCRAFT && order->IsType(OT_DUMMY) && !CheckForValidOrders(v))) {
9326
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1419
		if (v->type == VEH_AIRCRAFT) {
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1420
			/* Aircraft do something vastly different here, so handle separately */
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1421
			extern void HandleMissingAircraftOrders(Vehicle *v);
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1422
			HandleMissingAircraftOrders(v);
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1423
			return false;
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1424
		}
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1425
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1426
		v->current_order.Free();
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1427
		v->dest_tile = 0;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1428
		if (v->type == VEH_ROAD) ClearSlot(v);
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1429
		return false;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1430
	}
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1431
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1432
	/* If it is unchanged, keep it. */
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
  1433
	if (order->Equals(v->current_order) &&
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
  1434
			(v->type != VEH_SHIP || !order->IsType(OT_GOTO_STATION) || GetStation(order->GetDestination())->dock_tile != 0)) {
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1435
		return false;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1436
	}
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1437
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1438
	/* Otherwise set it, and determine the destination tile. */
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1439
	v->current_order = *order;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1440
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1441
	InvalidateVehicleOrder(v);
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1442
	switch (v->type) {
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1443
		default:
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1444
			NOT_REACHED();
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1445
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1446
		case VEH_ROAD:
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1447
		case VEH_TRAIN:
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1448
			break;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1449
9326
326baf219dda (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 9323
diff changeset
  1450
		case VEH_AIRCRAFT:
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1451
		case VEH_SHIP:
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1452
			InvalidateWindowClasses(v->GetVehicleListWindowClass());
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1453
			break;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1454
	}
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1455
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1456
	switch (order->GetType()) {
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1457
		case OT_GOTO_STATION:
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
  1458
			v->dest_tile = v->GetOrderStationLocation(order->GetDestination());
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1459
			break;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1460
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1461
		case OT_GOTO_DEPOT:
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
  1462
			if (v->type != VEH_AIRCRAFT) v->dest_tile = GetDepot(order->GetDestination())->xy;
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1463
			break;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1464
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1465
		case OT_GOTO_WAYPOINT:
9336
3f75a2c5e0d3 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 9335
diff changeset
  1466
			v->dest_tile = GetWaypoint(order->GetDestination())->xy;
9323
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1467
			break;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1468
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1469
		default:
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1470
			v->dest_tile = 0;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1471
			return false;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1472
	}
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1473
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1474
	return may_reverse;
9bc423363f6a (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 9283
diff changeset
  1475
}
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1476
9328
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1477
/**
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1478
 * Check whether the given vehicle should stop at the given station
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1479
 * based on this order and the non-stop settings.
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1480
 * @param v       the vehicle that might be stopping.
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1481
 * @param station the station to stop at.
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1482
 * @return true if the vehicle should stop.
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1483
 */
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1484
bool Order::ShouldStopAtStation(const Vehicle *v, StationID station) const
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1485
{
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1486
	return
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1487
			v->last_station_visited != station && // Do stop only when we've not just been there
9344
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
  1488
			/* Finally do stop when there is no non-stop flag set for this type of station. */
1be7ac6bd504 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 9343
diff changeset
  1489
			!(this->GetNonStopType() & ((this->dest == station) ? ONSF_NO_STOP_AT_DESTINATION_STATION : ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS));
9328
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1490
}
a7ad10a9b0cb (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 9326
diff changeset
  1491
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6397
diff changeset
  1492
void InitializeOrders()
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1493
{
7897
222641b84fc4 (svn r10773) -Codechange: use pool.CleanPool instead of CleanPool(&pool) and similarly for AddBlock*.
rubidium
parents: 7887
diff changeset
  1494
	_Order_pool.CleanPool();
222641b84fc4 (svn r10773) -Codechange: use pool.CleanPool instead of CleanPool(&pool) and similarly for AddBlock*.
rubidium
parents: 7887
diff changeset
  1495
	_Order_pool.AddBlockToPool();
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1496
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1497
	_backup_orders_tile = 0;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1498
}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1499
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1500
const SaveLoad *GetOrderDescription() {
1881
023a134a4b12 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1820
diff changeset
  1501
static const SaveLoad _order_desc[] = {
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4392
diff changeset
  1502
	SLE_VAR(Order, type,  SLE_UINT8),
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4392
diff changeset
  1503
	SLE_VAR(Order, flags, SLE_UINT8),
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4392
diff changeset
  1504
	SLE_VAR(Order, dest,  SLE_UINT16),
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4392
diff changeset
  1505
	SLE_REF(Order, next,  REF_ORDER),
7476
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7446
diff changeset
  1506
	SLE_CONDVAR(Order, refit_cargo,    SLE_UINT8,  36, SL_MAX_VERSION),
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7446
diff changeset
  1507
	SLE_CONDVAR(Order, refit_subtype,  SLE_UINT8,  36, SL_MAX_VERSION),
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7446
diff changeset
  1508
	SLE_CONDVAR(Order, wait_time,      SLE_UINT16, 67, SL_MAX_VERSION),
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7446
diff changeset
  1509
	SLE_CONDVAR(Order, travel_time,    SLE_UINT16, 67, SL_MAX_VERSION),
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1510
4712
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
  1511
	/* Leftover from the minor savegame version stuff
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
  1512
	 * We will never use those free bytes, but we have to keep this line to allow loading of old savegames */
273ec3b182bf (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4549
diff changeset
  1513
	SLE_CONDNULL(10, 5, 35),
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1514
	SLE_END()
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1515
};
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1516
	return _order_desc;
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1517
}
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1518
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6397
diff changeset
  1519
static void Save_ORDR()
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1520
{
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1521
	Order *order;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1522
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1523
	FOR_ALL_ORDERS(order) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  1524
		SlSetArrayIndex(order->index);
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1525
		SlObject(order, GetOrderDescription());
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1526
	}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1527
}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1528
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6397
diff changeset
  1529
static void Load_ORDR()
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1530
{
2685
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2645
diff changeset
  1531
	if (CheckSavegameVersionOldStyle(5, 2)) {
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2645
diff changeset
  1532
		/* Version older than 5.2 did not have a ->next pointer. Convert them
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1533
		    (in the old days, the orderlist was 5000 items big) */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1534
		uint len = SlGetFieldLength();
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1535
		uint i;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1536
2685
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2645
diff changeset
  1537
		if (CheckSavegameVersion(5)) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1538
			/* Pre-version 5 had an other layout for orders
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1539
			    (uint16 instead of uint32) */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1540
			len /= sizeof(uint16);
8869
1e62a45d7599 (svn r11939) -Codechange: some type fixes and very initial steps into supporting NDS by default. Based on work by Dominik.
rubidium
parents: 8798
diff changeset
  1541
			uint16 *orders = MallocT<uint16>(len + 1);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1542
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1543
			SlArray(orders, len, SLE_UINT16);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1544
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1545
			for (i = 0; i < len; ++i) {
7887
5740662fc5ce (svn r10760) -Codechange: make the order struct use the pool item class as super class.
rubidium
parents: 7545
diff changeset
  1546
				Order *order = new (i) Order();
9331
f67d4fd5ed5d (svn r12583) -Codechange: make AssignOrder a class function of order.
rubidium
parents: 9330
diff changeset
  1547
				order->AssignOrder(UnpackVersion4Order(orders[i]));
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1548
			}
8869
1e62a45d7599 (svn r11939) -Codechange: some type fixes and very initial steps into supporting NDS by default. Based on work by Dominik.
rubidium
parents: 8798
diff changeset
  1549
1e62a45d7599 (svn r11939) -Codechange: some type fixes and very initial steps into supporting NDS by default. Based on work by Dominik.
rubidium
parents: 8798
diff changeset
  1550
			free(orders);
2685
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2645
diff changeset
  1551
		} else if (CheckSavegameVersionOldStyle(5, 2)) {
8869
1e62a45d7599 (svn r11939) -Codechange: some type fixes and very initial steps into supporting NDS by default. Based on work by Dominik.
rubidium
parents: 8798
diff changeset
  1552
			len /= sizeof(uint16);
1e62a45d7599 (svn r11939) -Codechange: some type fixes and very initial steps into supporting NDS by default. Based on work by Dominik.
rubidium
parents: 8798
diff changeset
  1553
			uint16 *orders = MallocT<uint16>(len + 1);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1554
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1555
			SlArray(orders, len, SLE_UINT32);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1556
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1557
			for (i = 0; i < len; ++i) {
9335
18d3658adc60 (svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
rubidium
parents: 9334
diff changeset
  1558
				new (i) Order(orders[i]);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1559
			}
8869
1e62a45d7599 (svn r11939) -Codechange: some type fixes and very initial steps into supporting NDS by default. Based on work by Dominik.
rubidium
parents: 8798
diff changeset
  1560
1e62a45d7599 (svn r11939) -Codechange: some type fixes and very initial steps into supporting NDS by default. Based on work by Dominik.
rubidium
parents: 8798
diff changeset
  1561
			free(orders);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1562
		}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1563
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1564
		/* Update all the next pointer */
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1565
		for (i = 1; i < len; ++i) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1566
			/* The orders were built like this:
6589
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
  1567
			 *   While the order is valid, set the previous will get it's next pointer set
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
  1568
			 *   We start with index 1 because no order will have the first in it's next pointer */
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
  1569
			if (GetOrder(i)->IsValid())
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1570
				GetOrder(i - 1)->next = GetOrder(i);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1571
		}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1572
	} else {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1573
		int index;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1574
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1575
		while ((index = SlIterateArray()) != -1) {
7887
5740662fc5ce (svn r10760) -Codechange: make the order struct use the pool item class as super class.
rubidium
parents: 7545
diff changeset
  1576
			Order *order = new (index) Order();
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9331
diff changeset
  1577
			SlObject(order, GetOrderDescription());
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1578
		}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1579
	}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1580
}
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1581
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1582
extern const ChunkHandler _order_chunk_handlers[] = {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1583
	{ 'ORDR', Save_ORDR, Load_ORDR, CH_ARRAY | CH_LAST},
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1584
};