author | tron |
Sat, 01 Oct 2005 12:43:34 +0000 | |
changeset 2477 | d454d5152386 |
parent 2464 | 3dee59d241c0 |
child 2552 | fb174febb0b1 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 3 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1802
diff
changeset
|
4 |
#include "openttd.h" |
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
445
diff
changeset
|
5 |
#include "table/strings.h" |
2163
637ec3c361f5
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
6 |
#include "functions.h" |
679
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
593
diff
changeset
|
7 |
#include "map.h" |
1209
a1ac96655b79
(svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents:
1206
diff
changeset
|
8 |
#include "tile.h" |
0 | 9 |
#include "vehicle.h" |
10 |
#include "command.h" |
|
11 |
#include "pathfind.h" |
|
12 |
#include "station.h" |
|
13 |
#include "news.h" |
|
14 |
#include "engine.h" |
|
15 |
#include "gui.h" |
|
16 |
#include "player.h" |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
297
diff
changeset
|
17 |
#include "sound.h" |
1247 | 18 |
#include "npf.h" |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
19 |
#include "depot.h" |
2159
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2150
diff
changeset
|
20 |
#include "vehicle_gui.h" |
0 | 21 |
|
22 |
static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D}; |
|
23 |
static const byte _ship_sometracks[4] = {0x19, 0x16, 0x25, 0x2A}; |
|
24 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
25 |
static byte GetTileShipTrackStatus(TileIndex tile) |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
26 |
{ |
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
27 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); |
0 | 28 |
return r | r >> 8; |
29 |
} |
|
30 |
||
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2464
diff
changeset
|
31 |
void DrawShipEngine(int x, int y, EngineID engine, uint32 image_ormod) |
0 | 32 |
{ |
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
33 |
int spritenum = ShipVehInfo(engine)->image_index; |
0 | 34 |
|
35 |
if (is_custom_sprite(spritenum)) { |
|
358
6eb0d5ec99b1
(svn r546) -newgrf: Use GetCustomVehicleIcon() for fetching sprites for vehicle selection dialogs. (Idea by octo, done by pasky).
darkvater
parents:
337
diff
changeset
|
36 |
int sprite = GetCustomVehicleIcon(engine, 6); |
0 | 37 |
|
38 |
if (sprite) { |
|
39 |
DrawSprite(sprite | image_ormod, x, y); |
|
40 |
return; |
|
41 |
} |
|
2464
3dee59d241c0
(svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents:
2214
diff
changeset
|
42 |
spritenum = orig_ship_vehicle_info[engine - SHIP_ENGINES_INDEX].image_index; |
0 | 43 |
} |
44 |
DrawSprite((6 + _ship_sprites[spritenum]) | image_ormod, x, y); |
|
45 |
} |
|
46 |
||
1790
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1757
diff
changeset
|
47 |
int GetShipImage(const Vehicle *v, byte direction) |
0 | 48 |
{ |
49 |
int spritenum = v->spritenum; |
|
50 |
||
51 |
if (is_custom_sprite(spritenum)) { |
|
52 |
int sprite = GetCustomVehicleSprite(v, direction); |
|
53 |
||
54 |
if (sprite) return sprite; |
|
2464
3dee59d241c0
(svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents:
2214
diff
changeset
|
55 |
spritenum = orig_ship_vehicle_info[v->engine_type - SHIP_ENGINES_INDEX].image_index; |
0 | 56 |
} |
57 |
return _ship_sprites[spritenum] + direction; |
|
58 |
} |
|
59 |
||
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
60 |
static Depot *FindClosestShipDepot(Vehicle *v) |
0 | 61 |
{ |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
62 |
Depot *depot; |
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
63 |
Depot *best_depot = NULL; |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
64 |
uint dist; |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
65 |
uint best_dist = (uint)-1; |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
66 |
TileIndex tile; |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
67 |
TileIndex tile2 = v->tile; |
0 | 68 |
|
1247 | 69 |
if (_patches.new_pathfinding_all) { |
70 |
NPFFoundTargetData ftd; |
|
1752
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1751
diff
changeset
|
71 |
byte trackdir = GetVehicleTrackdir(v); |
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1998
diff
changeset
|
72 |
ftd = NPFRouteToDepotTrialError(v->tile, trackdir, TRANSPORT_WATER, v->owner, INVALID_RAILTYPE); |
1247 | 73 |
if (ftd.best_bird_dist == 0) |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
74 |
best_depot = GetDepotByTile(ftd.node.tile); /* Found target */ |
1247 | 75 |
else |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
76 |
best_depot = NULL; /* Did not find target */ |
1247 | 77 |
} else { |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
78 |
FOR_ALL_DEPOTS(depot) { |
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
79 |
tile = depot->xy; |
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
80 |
if (IsValidDepot(depot) && IsTileDepotType(tile, TRANSPORT_WATER) && IsTileOwner(tile, v->owner)) { |
1247 | 81 |
dist = DistanceManhattan(tile, tile2); |
82 |
if (dist < best_dist) { |
|
83 |
best_dist = dist; |
|
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
84 |
best_depot = depot; |
1247 | 85 |
} |
0 | 86 |
} |
87 |
} |
|
88 |
} |
|
89 |
return best_depot; |
|
90 |
} |
|
91 |
||
92 |
static void CheckIfShipNeedsService(Vehicle *v) |
|
93 |
{ |
|
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
94 |
Depot *depot; |
0 | 95 |
|
1520
c85dc79795e0
(svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents:
1443
diff
changeset
|
96 |
if (_patches.servint_ships == 0) |
11
836bc4b37b5b
(svn r12) Change: removed patch no_train_service. Instead you can set the default service interval for any vehicle type to 'disabled'.
dominik
parents:
0
diff
changeset
|
97 |
return; |
836bc4b37b5b
(svn r12) Change: removed patch no_train_service. Instead you can set the default service interval for any vehicle type to 'disabled'.
dominik
parents:
0
diff
changeset
|
98 |
|
1520
c85dc79795e0
(svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents:
1443
diff
changeset
|
99 |
if (!VehicleNeedsService(v)) |
0 | 100 |
return; |
101 |
||
102 |
if (v->vehstatus & VS_STOPPED) |
|
103 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
104 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
105 |
if (v->current_order.type == OT_GOTO_DEPOT && |
1530
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
106 |
v->current_order.flags & OF_HALT_IN_DEPOT) |
0 | 107 |
return; |
108 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
109 |
if (_patches.gotodepot && VehicleHasDepotOrders(v)) |
0 | 110 |
return; |
111 |
||
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
112 |
depot = FindClosestShipDepot(v); |
0 | 113 |
|
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
114 |
if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
115 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
116 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
117 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
118 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 119 |
} |
120 |
return; |
|
121 |
} |
|
122 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
123 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
124 |
v->current_order.flags = OF_NON_STOP; |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
125 |
v->current_order.station = depot->index; |
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
126 |
v->dest_tile = depot->xy; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
127 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 128 |
} |
129 |
||
130 |
void OnNewDay_Ship(Vehicle *v) |
|
131 |
{ |
|
132 |
int32 cost; |
|
133 |
||
134 |
if ((++v->day_counter & 7) == 0) |
|
135 |
DecreaseVehicleValue(v); |
|
136 |
||
137 |
CheckVehicleBreakdown(v); |
|
138 |
AgeVehicle(v); |
|
139 |
CheckIfShipNeedsService(v); |
|
140 |
||
1053
dfb5243315f1
(svn r1554) -Fix: [ 1103187 ] Order Check messages are now validated before
celestar
parents:
1035
diff
changeset
|
141 |
CheckOrders(v->index, OC_INIT); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
142 |
|
0 | 143 |
if (v->vehstatus & VS_STOPPED) |
144 |
return; |
|
145 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
146 |
|
19
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
147 |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
148 |
cost = ShipVehInfo(v->engine_type)->running_cost * _price.ship_running / 364; |
0 | 149 |
v->profit_this_year -= cost >> 8; |
150 |
||
151 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN); |
|
152 |
SubtractMoneyFromPlayerFract(v->owner, cost); |
|
153 |
||
154 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1055
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
155 |
//we need this for the profit |
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
156 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 157 |
} |
158 |
||
159 |
static void HandleBrokenShip(Vehicle *v) |
|
160 |
{ |
|
161 |
if (v->breakdown_ctr != 1) { |
|
162 |
v->breakdown_ctr = 1; |
|
163 |
v->cur_speed = 0; |
|
164 |
||
165 |
if (v->breakdowns_since_last_service != 255) |
|
166 |
v->breakdowns_since_last_service++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
167 |
|
0 | 168 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
169 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
170 |
|
541 | 171 |
SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? |
172 |
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v); |
|
0 | 173 |
|
174 |
if (!(v->vehstatus & VS_HIDDEN)) { |
|
175 |
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); |
|
176 |
if (u) |
|
177 |
u->u.special.unk0 = v->breakdown_delay * 2; |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
if (!(v->tick_counter & 1)) { |
|
182 |
if (!--v->breakdown_delay) { |
|
183 |
v->breakdown_ctr = 0; |
|
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1128
diff
changeset
|
184 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
0 | 185 |
} |
186 |
} |
|
187 |
} |
|
188 |
||
189 |
static void MarkShipDirty(Vehicle *v) |
|
190 |
{ |
|
191 |
v->cur_image = GetShipImage(v, v->direction); |
|
192 |
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); |
|
193 |
} |
|
194 |
||
195 |
static void PlayShipSound(Vehicle *v) |
|
196 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
197 |
SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v); |
0 | 198 |
} |
199 |
||
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
200 |
static const TileIndexDiffC _dock_offs[] = { |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
201 |
{ 2, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
202 |
{-2, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
203 |
{ 0, 2}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
204 |
{ 2, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
205 |
{ 0, -2}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
206 |
{ 0, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
207 |
{ 0, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
208 |
{ 0, 0} |
0 | 209 |
}; |
210 |
||
211 |
static void ProcessShipOrder(Vehicle *v) |
|
212 |
{ |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
213 |
const Order *order; |
0 | 214 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
215 |
if (v->current_order.type >= OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
216 |
v->current_order.type <= OT_LEAVESTATION) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
217 |
if (v->current_order.type != OT_GOTO_DEPOT || |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
218 |
!(v->current_order.flags & OF_UNLOAD)) |
0 | 219 |
return; |
220 |
} |
|
221 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
222 |
if (v->current_order.type == OT_GOTO_DEPOT && |
2214
75d037f8dcc4
(svn r2732) -Codechange: Cleaned the order flags some more. Hit some inconsistencies while trying to locate bug 1244167
celestar
parents:
2186
diff
changeset
|
223 |
(v->current_order.flags & (OF_PART_OF_ORDERS | OF_SERVICE_IF_NEEDED)) == (OF_PART_OF_ORDERS | OF_SERVICE_IF_NEEDED) && |
1520
c85dc79795e0
(svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents:
1443
diff
changeset
|
224 |
!VehicleNeedsService(v)) { |
0 | 225 |
v->cur_order_index++; |
226 |
} |
|
227 |
||
228 |
||
229 |
if (v->cur_order_index >= v->num_orders) |
|
230 |
v->cur_order_index = 0; |
|
231 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
232 |
order = GetVehicleOrder(v, v->cur_order_index); |
0 | 233 |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
234 |
if (order == NULL) { |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
235 |
v->current_order.type = OT_NOTHING; |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
236 |
v->current_order.flags = 0; |
0 | 237 |
v->dest_tile = 0; |
238 |
return; |
|
239 |
} |
|
240 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
241 |
if (order->type == v->current_order.type && |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
242 |
order->flags == v->current_order.flags && |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
243 |
order->station == v->current_order.station) |
0 | 244 |
return; |
245 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
246 |
v->current_order = *order; |
0 | 247 |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
248 |
if (order->type == OT_GOTO_STATION) { |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
249 |
const Station *st; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
250 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
251 |
if (order->station == v->last_station_visited) |
1266
9dc5638fe8cc
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1247
diff
changeset
|
252 |
v->last_station_visited = INVALID_STATION; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
253 |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
254 |
st = GetStation(order->station); |
0 | 255 |
if (st->dock_tile != 0) { |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2008
diff
changeset
|
256 |
v->dest_tile = TILE_ADD(st->dock_tile, ToTileIndexDiff(_dock_offs[_m[st->dock_tile].m5-0x4B])); |
0 | 257 |
} |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
258 |
} else if (order->type == OT_GOTO_DEPOT) { |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
259 |
v->dest_tile = GetDepot(order->station)->xy; |
0 | 260 |
} else { |
261 |
v->dest_tile = 0; |
|
262 |
} |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
263 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
264 |
InvalidateVehicleOrder(v); |
1020
1223cd937174
(svn r1521) -Fix: Ship Vehicle Lists are now redrawn correctly
celestar
parents:
926
diff
changeset
|
265 |
|
1055
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
266 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 267 |
} |
268 |
||
269 |
static void HandleShipLoading(Vehicle *v) |
|
270 |
{ |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
271 |
if (v->current_order.type == OT_NOTHING) |
0 | 272 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
273 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
274 |
if (v->current_order.type != OT_DUMMY) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
275 |
if (v->current_order.type != OT_LOADING) |
0 | 276 |
return; |
277 |
||
278 |
if (--v->load_unload_time_rem) |
|
279 |
return; |
|
280 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
281 |
if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) { |
0 | 282 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); |
283 |
if (LoadUnloadVehicle(v)) { |
|
284 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
|
285 |
MarkShipDirty(v); |
|
286 |
} |
|
287 |
return; |
|
288 |
} |
|
289 |
PlayShipSound(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
290 |
|
0 | 291 |
{ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
292 |
Order b = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
293 |
v->current_order.type = OT_LEAVESTATION; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
294 |
v->current_order.flags = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
295 |
if (!(b.flags & OF_NON_STOP)) |
0 | 296 |
return; |
297 |
} |
|
298 |
} |
|
299 |
||
300 |
v->cur_order_index++; |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
301 |
InvalidateVehicleOrder(v); |
0 | 302 |
} |
303 |
||
304 |
static void UpdateShipDeltaXY(Vehicle *v, int dir) |
|
305 |
{ |
|
306 |
#define MKIT(d,c,b,a) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0) |
|
307 |
static const uint32 _delta_xy_table[8] = { |
|
308 |
MKIT( -3, -3, 6, 6), |
|
309 |
MKIT(-16, -3, 32, 6), |
|
310 |
MKIT( -3, -3, 6, 6), |
|
311 |
MKIT( -3,-16, 6, 32), |
|
312 |
MKIT( -3, -3, 6, 6), |
|
313 |
MKIT(-16, -3, 32, 6), |
|
314 |
MKIT( -3, -3, 6, 6), |
|
315 |
MKIT( -3,-16, 6, 32), |
|
316 |
}; |
|
317 |
#undef MKIT |
|
318 |
uint32 x = _delta_xy_table[dir]; |
|
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2049
diff
changeset
|
319 |
v->x_offs = GB(x, 0, 8); |
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2049
diff
changeset
|
320 |
v->y_offs = GB(x, 8, 8); |
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2049
diff
changeset
|
321 |
v->sprite_width = GB(x, 16, 8); |
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2049
diff
changeset
|
322 |
v->sprite_height = GB(x, 24, 8); |
0 | 323 |
} |
324 |
||
325 |
static void RecalcShipStuff(Vehicle *v) |
|
326 |
{ |
|
327 |
UpdateShipDeltaXY(v, v->direction); |
|
328 |
v->cur_image = GetShipImage(v, v->direction); |
|
329 |
MarkShipDirty(v); |
|
330 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
331 |
} |
|
332 |
||
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
333 |
static const TileIndexDiffC _ship_leave_depot_offs[] = { |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
334 |
{-1, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
335 |
{ 0, -1} |
0 | 336 |
}; |
337 |
||
338 |
static void CheckShipLeaveDepot(Vehicle *v) |
|
339 |
{ |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
340 |
TileIndex tile; |
0 | 341 |
int d; |
342 |
uint m; |
|
343 |
||
344 |
if (v->u.ship.state != 0x80) |
|
345 |
return; |
|
346 |
||
347 |
tile = v->tile; |
|
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2008
diff
changeset
|
348 |
d = (_m[tile].m5&2) ? 1 : 0; |
0 | 349 |
|
350 |
// Check first side |
|
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
351 |
if (_ship_sometracks[d] & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[d])))) { |
0 | 352 |
m = (d==0) ? 0x101 : 0x207; |
353 |
// Check second side |
|
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
354 |
} else if (_ship_sometracks[d+2] & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[d])))) { |
0 | 355 |
m = (d==0) ? 0x105 : 0x203; |
356 |
} else { |
|
357 |
return; |
|
358 |
} |
|
359 |
v->direction = (byte)m; |
|
360 |
v->u.ship.state = (byte)(m >> 8); |
|
361 |
v->vehstatus &= ~VS_HIDDEN; |
|
362 |
||
363 |
v->cur_speed = 0; |
|
364 |
RecalcShipStuff(v); |
|
365 |
||
366 |
PlayShipSound(v); |
|
578
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
367 |
VehicleServiceInDepot(v); |
1055
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
368 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 369 |
} |
370 |
||
371 |
static bool ShipAccelerate(Vehicle *v) |
|
372 |
{ |
|
373 |
uint spd; |
|
374 |
byte t; |
|
375 |
||
376 |
spd = min(v->cur_speed + 1, v->max_speed); |
|
377 |
||
378 |
//updates statusbar only if speed have changed to save CPU time |
|
379 |
if (spd != v->cur_speed) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
380 |
v->cur_speed = spd; |
0 | 381 |
if (_patches.vehicle_speed) |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
382 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 383 |
} |
384 |
||
385 |
// Decrease somewhat when turning |
|
386 |
if (!(v->direction&1)) { |
|
387 |
spd = spd * 3 >> 2; |
|
388 |
} |
|
389 |
||
390 |
if (spd == 0) |
|
391 |
return false; |
|
392 |
||
393 |
if ((byte)++spd == 0) |
|
394 |
return true; |
|
395 |
||
396 |
v->progress = (t = v->progress) - (byte)spd; |
|
397 |
||
398 |
return (t < v->progress); |
|
399 |
} |
|
400 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
401 |
int32 EstimateShipCost(EngineID engine_type) |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
402 |
{ |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
403 |
return ShipVehInfo(engine_type)->base_cost * (_price.ship_base>>3)>>5; |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
404 |
} |
0 | 405 |
|
406 |
static void ShipEnterDepot(Vehicle *v) |
|
407 |
{ |
|
408 |
v->u.ship.state = 0x80; |
|
409 |
v->vehstatus |= VS_HIDDEN; |
|
410 |
v->cur_speed = 0; |
|
411 |
RecalcShipStuff(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
412 |
|
578
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
413 |
VehicleServiceInDepot(v); |
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
414 |
|
0 | 415 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
416 |
||
842 | 417 |
MaybeReplaceVehicle(v); |
0 | 418 |
|
445
0e3fa3da3899
(svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents:
410
diff
changeset
|
419 |
TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT); |
0e3fa3da3899
(svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents:
410
diff
changeset
|
420 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
421 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
422 |
Order t; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
423 |
|
0 | 424 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
425 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
426 |
t = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
427 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
428 |
v->current_order.flags = 0; |
0 | 429 |
|
1530
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
430 |
if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
431 |
v->cur_order_index++; |
1530
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
432 |
} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) { |
0 | 433 |
v->vehstatus |= VS_STOPPED; |
434 |
if (v->owner == _local_player) { |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
435 |
SetDParam(0, v->unitnumber); |
0 | 436 |
AddNewsItem( |
437 |
STR_981C_SHIP_IS_WAITING_IN_DEPOT, |
|
438 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
439 |
v->index, |
|
440 |
0); |
|
441 |
} |
|
442 |
} |
|
443 |
} |
|
1055
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
444 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 445 |
} |
446 |
||
447 |
static void ShipArrivesAt(Vehicle *v, Station *st) |
|
448 |
{ |
|
449 |
/* Check if station was ever visited before */ |
|
450 |
if (!(st->had_vehicle_of_type & HVOT_SHIP)) { |
|
451 |
uint32 flags; |
|
452 |
st->had_vehicle_of_type |= HVOT_SHIP; |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
453 |
SetDParam(0, st->index); |
0 | 454 |
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0); |
455 |
AddNewsItem( |
|
456 |
STR_9833_CITIZENS_CELEBRATE_FIRST, |
|
457 |
flags, |
|
458 |
v->index, |
|
459 |
0); |
|
460 |
} |
|
461 |
} |
|
462 |
||
463 |
typedef struct { |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
464 |
TileIndex skiptile; |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
465 |
TileIndex dest_coords; |
0 | 466 |
uint best_bird_dist; |
467 |
uint best_length; |
|
468 |
} PathFindShip; |
|
469 |
||
470 |
//extern void dbg_store_path(); |
|
471 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
472 |
static bool ShipTrackFollower(TileIndex tile, PathFindShip *pfs, int track, uint length, byte *state) |
0 | 473 |
{ |
474 |
// Found dest? |
|
475 |
if (tile == pfs->dest_coords) { |
|
476 |
pfs->best_bird_dist = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
477 |
|
0 | 478 |
// if (length < pfs->best_length) |
479 |
// dbg_store_path(); |
|
480 |
||
481 |
pfs->best_length = minu(pfs->best_length, length); |
|
482 |
return true; |
|
483 |
} |
|
484 |
||
485 |
// Skip this tile in the calculation |
|
486 |
if (tile != pfs->skiptile) { |
|
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1237
diff
changeset
|
487 |
pfs->best_bird_dist = minu(pfs->best_bird_dist, DistanceMaxPlusManhattan(pfs->dest_coords, tile)); |
0 | 488 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
489 |
|
0 | 490 |
return false; |
491 |
} |
|
492 |
||
493 |
static const byte _ship_search_directions[6][4] = { |
|
494 |
{ 0, 9, 2, 9 }, |
|
495 |
{ 9, 1, 9, 3 }, |
|
496 |
{ 9, 0, 3, 9 }, |
|
497 |
{ 1, 9, 9, 2 }, |
|
498 |
{ 3, 2, 9, 9 }, |
|
499 |
{ 9, 9, 1, 0 }, |
|
500 |
}; |
|
501 |
||
502 |
static const byte _pick_shiptrack_table[6] = {1, 3, 2, 2, 0, 0}; |
|
503 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
504 |
static uint FindShipTrack(Vehicle *v, TileIndex tile, int dir, uint bits, TileIndex skiptile, int *track) |
0 | 505 |
{ |
506 |
PathFindShip pfs; |
|
507 |
int i, best_track; |
|
508 |
uint best_bird_dist = 0; |
|
509 |
uint best_length = 0; |
|
510 |
uint r; |
|
511 |
byte ship_dir = v->direction & 3; |
|
512 |
||
513 |
pfs.dest_coords = v->dest_tile; |
|
514 |
pfs.skiptile = skiptile; |
|
515 |
||
516 |
best_track = -1; |
|
517 |
||
518 |
do { |
|
519 |
i = FIND_FIRST_BIT(bits); |
|
520 |
bits = KILL_FIRST_BIT(bits); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
521 |
|
0 | 522 |
pfs.best_bird_dist = (uint)-1; |
523 |
pfs.best_length = (uint)-1; |
|
524 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
525 |
FollowTrack(tile, 0x3800 | TRANSPORT_WATER, _ship_search_directions[i][dir], (TPFEnumProc*)ShipTrackFollower, NULL, &pfs); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
526 |
|
0 | 527 |
if (best_track >= 0) { |
528 |
if (pfs.best_bird_dist != 0) { |
|
529 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
|
530 |
if (pfs.best_bird_dist > best_bird_dist) goto bad; |
|
531 |
if (pfs.best_bird_dist < best_bird_dist) goto good; |
|
532 |
} else { |
|
533 |
if (pfs.best_length > best_length) goto bad; |
|
534 |
if (pfs.best_length < best_length) goto good; |
|
535 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
536 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
537 |
/* if we reach this position, there's two paths of equal value so far. |
0 | 538 |
* pick one randomly. */ |
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2049
diff
changeset
|
539 |
r = GB(Random(), 0, 8); |
0 | 540 |
if (_pick_shiptrack_table[i] == ship_dir) r += 80; |
541 |
if (_pick_shiptrack_table[best_track] == ship_dir) r -= 80; |
|
542 |
if (r <= 127) goto bad; |
|
543 |
} |
|
544 |
good:; |
|
545 |
best_track = i; |
|
546 |
best_bird_dist = pfs.best_bird_dist; |
|
547 |
best_length = pfs.best_length; |
|
548 |
bad:; |
|
549 |
||
550 |
} while (bits != 0); |
|
551 |
||
552 |
*track = best_track; |
|
553 |
return best_bird_dist; |
|
554 |
} |
|
555 |
||
1247 | 556 |
/* returns the track to choose on the next tile, or -1 when it's better to |
557 |
* reverse. The tile given is the tile we are about to enter, enterdir is the |
|
558 |
* direction in which we are entering the tile */ |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
559 |
static int ChooseShipTrack(Vehicle *v, TileIndex tile, int enterdir, uint tracks) |
0 | 560 |
{ |
1247 | 561 |
assert(enterdir>=0 && enterdir<=3); |
0 | 562 |
|
1247 | 563 |
if (_patches.new_pathfinding_all) { |
564 |
NPFFindStationOrTileData fstd; |
|
565 |
NPFFoundTargetData ftd; |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
566 |
TileIndex src_tile = TILE_ADD(tile, TileOffsByDir(ReverseDiagdir(enterdir))); |
1752
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1751
diff
changeset
|
567 |
byte trackdir = GetVehicleTrackdir(v); |
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1751
diff
changeset
|
568 |
assert (trackdir != 0xFF); /* Check that we are not in a depot */ |
1247 | 569 |
|
570 |
NPFFillWithOrderData(&fstd, v); |
|
571 |
||
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
572 |
ftd = NPFRouteToStationOrTile(src_tile, trackdir, &fstd, TRANSPORT_WATER, v->owner, INVALID_RAILTYPE, PBS_MODE_NONE); |
1247 | 573 |
|
1698
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1530
diff
changeset
|
574 |
if (ftd.best_trackdir != 0xff) |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1530
diff
changeset
|
575 |
/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1530
diff
changeset
|
576 |
the direction we need to take to get there, if ftd.best_bird_dist is not 0, |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1530
diff
changeset
|
577 |
we did not find our target, but ftd.best_trackdir contains the direction leading |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1530
diff
changeset
|
578 |
to the tile closest to our target. */ |
1247 | 579 |
return ftd.best_trackdir & 7; /* TODO: Wrapper function? */ |
580 |
else |
|
1698
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1530
diff
changeset
|
581 |
return -1; /* Already at target, reverse? */ |
1247 | 582 |
} else { |
583 |
uint b; |
|
584 |
uint tot_dist, dist; |
|
585 |
int track; |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
586 |
TileIndex tile2; |
1247 | 587 |
|
588 |
tile2 = TILE_ADD(tile, -TileOffsByDir(enterdir)); |
|
589 |
tot_dist = (uint)-1; |
|
590 |
||
591 |
/* Let's find out how far it would be if we would reverse first */ |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1926
diff
changeset
|
592 |
b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagdir(enterdir)] & v->u.ship.state; |
1247 | 593 |
if (b != 0) { |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1926
diff
changeset
|
594 |
dist = FindShipTrack(v, tile2, ReverseDiagdir(enterdir), b, tile, &track); |
1247 | 595 |
if (dist != (uint)-1) |
596 |
tot_dist = dist + 1; |
|
597 |
} |
|
598 |
/* And if we would not reverse? */ |
|
599 |
dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track); |
|
600 |
if (dist > tot_dist) |
|
601 |
/* We could better reverse */ |
|
602 |
return -1; |
|
603 |
return track; |
|
0 | 604 |
} |
605 |
} |
|
606 |
||
607 |
static const byte _new_vehicle_direction_table[11] = { |
|
608 |
0, 7, 6, 0, |
|
609 |
1, 0, 5, 0, |
|
610 |
2, 3, 4, |
|
611 |
}; |
|
612 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
613 |
static int ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile) |
0 | 614 |
{ |
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
615 |
uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 + |
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
616 |
TileX(new_tile) - TileX(old_tile) + 1; |
0 | 617 |
assert(offs < 11 && offs != 3 && offs != 7); |
618 |
return _new_vehicle_direction_table[offs]; |
|
619 |
} |
|
620 |
||
621 |
static int ShipGetNewDirection(Vehicle *v, int x, int y) |
|
622 |
{ |
|
623 |
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); |
|
624 |
assert(offs < 11 && offs != 3 && offs != 7); |
|
625 |
return _new_vehicle_direction_table[offs]; |
|
626 |
} |
|
627 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1942
diff
changeset
|
628 |
static int GetAvailShipTracks(TileIndex tile, int dir) |
0 | 629 |
{ |
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
630 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); |
0 | 631 |
return (byte) ((r | r >> 8)) & _ship_sometracks[dir]; |
632 |
} |
|
633 |
||
634 |
static const byte _ship_subcoord[4][6][3] = { |
|
635 |
{ |
|
636 |
{15, 8, 1}, |
|
637 |
{ 0, 0, 0}, |
|
638 |
{ 0, 0, 0}, |
|
639 |
{15, 8, 2}, |
|
640 |
{15, 7, 0}, |
|
641 |
{ 0, 0, 0}, |
|
642 |
}, |
|
643 |
{ |
|
644 |
{ 0, 0, 0}, |
|
645 |
{ 8, 0, 3}, |
|
646 |
{ 7, 0, 2}, |
|
647 |
{ 0, 0, 0}, |
|
648 |
{ 8, 0, 4}, |
|
649 |
{ 0, 0, 0}, |
|
650 |
}, |
|
651 |
{ |
|
652 |
{ 0, 8, 5}, |
|
653 |
{ 0, 0, 0}, |
|
654 |
{ 0, 7, 6}, |
|
655 |
{ 0, 0, 0}, |
|
656 |
{ 0, 0, 0}, |
|
657 |
{ 0, 8, 4}, |
|
658 |
}, |
|
659 |
{ |
|
660 |
{ 0, 0, 0}, |
|
661 |
{ 8,15, 7}, |
|
662 |
{ 0, 0, 0}, |
|
663 |
{ 8,15, 6}, |
|
664 |
{ 0, 0, 0}, |
|
665 |
{ 7,15, 0}, |
|
666 |
} |
|
667 |
}; |
|
668 |
||
669 |
static void ShipController(Vehicle *v) |
|
670 |
{ |
|
671 |
GetNewVehiclePosResult gp; |
|
672 |
uint32 r; |
|
673 |
const byte *b; |
|
674 |
int dir,track,tracks; |
|
675 |
||
676 |
v->tick_counter++; |
|
677 |
||
678 |
if (v->breakdown_ctr != 0) { |
|
679 |
if (v->breakdown_ctr <= 2) { |
|
680 |
HandleBrokenShip(v); |
|
681 |
return; |
|
682 |
} |
|
683 |
v->breakdown_ctr--; |
|
684 |
} |
|
685 |
||
686 |
if (v->vehstatus & VS_STOPPED) |
|
687 |
return; |
|
688 |
||
689 |
ProcessShipOrder(v); |
|
690 |
HandleShipLoading(v); |
|
691 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
692 |
if (v->current_order.type == OT_LOADING) |
0 | 693 |
return; |
694 |
||
695 |
CheckShipLeaveDepot(v); |
|
696 |
||
697 |
if (!ShipAccelerate(v)) |
|
698 |
return; |
|
699 |
||
700 |
BeginVehicleMove(v); |
|
701 |
||
702 |
if (GetNewVehiclePos(v, &gp)) { |
|
703 |
// staying in tile |
|
704 |
if (v->u.ship.state == 0x80) { |
|
705 |
gp.x = v->x_pos; |
|
706 |
gp.y = v->y_pos; |
|
707 |
} else { |
|
708 |
/* isnot inside depot */ |
|
709 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
710 |
if (r & 0x8) goto reverse_direction; |
|
711 |
||
1751
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
712 |
/* A leave station order only needs one tick to get processed, so we can |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
713 |
* always skip ahead. */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
714 |
if (v->current_order.type == OT_LEAVESTATION) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
715 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
716 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
717 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
1751
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
718 |
} else if (v->dest_tile != 0) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
719 |
/* We have a target, let's see if we reached it... */ |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
720 |
if (v->current_order.type == OT_GOTO_STATION && |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
721 |
IsBuoyTile(v->dest_tile) && |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
722 |
DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
723 |
/* We got within 3 tiles of our target buoy, so let's skip to our |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
724 |
* next order */ |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
725 |
v->cur_order_index++; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
726 |
v->current_order.type = OT_DUMMY; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
727 |
InvalidateVehicleOrder(v); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
728 |
} else { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
729 |
/* Non-buoy orders really need to reach the tile */ |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
730 |
if (v->dest_tile == gp.new_tile) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
731 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
732 |
if ((gp.x&0xF)==8 && (gp.y&0xF)==8) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
733 |
ShipEnterDepot(v); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
734 |
return; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
735 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
736 |
} else if (v->current_order.type == OT_GOTO_STATION) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
737 |
Station *st; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
738 |
|
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
739 |
v->last_station_visited = v->current_order.station; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
740 |
|
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
741 |
/* Process station in the orderlist. */ |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
742 |
st = GetStation(v->current_order.station); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
743 |
if (st->facilities & FACIL_DOCK) { /* ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations */ |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
744 |
v->current_order.type = OT_LOADING; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
745 |
v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
746 |
v->current_order.flags |= OF_NON_STOP; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
747 |
ShipArrivesAt(v, st); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
748 |
|
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
749 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
750 |
if (LoadUnloadVehicle(v)) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
751 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
752 |
MarkShipDirty(v); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
753 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
754 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
755 |
} else { /* leave stations without docks right aways */ |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
756 |
v->current_order.type = OT_LEAVESTATION; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
757 |
v->current_order.flags = 0; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
758 |
v->cur_order_index++; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
759 |
InvalidateVehicleOrder(v); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
760 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
761 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
762 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
763 |
} |
0 | 764 |
} |
765 |
} |
|
766 |
} else { |
|
767 |
// new tile |
|
1401
56d372a75b74
(svn r1905) - Fix: [ 1118810 ] openttd: ship_cmd.c:642 ... Assertion failed. Mapwrap fixed in ship_cmd.c (was implicitely ok before biggermaps).
Darkvater
parents:
1330
diff
changeset
|
768 |
if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) |
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
769 |
goto reverse_direction; |
0 | 770 |
|
771 |
dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile); |
|
772 |
assert(dir == 1 || dir == 3 || dir == 5 || dir == 7); |
|
773 |
dir>>=1; |
|
774 |
tracks = GetAvailShipTracks(gp.new_tile, dir); |
|
775 |
if (tracks == 0) |
|
776 |
goto reverse_direction; |
|
777 |
||
778 |
// Choose a direction, and continue if we find one |
|
779 |
track = ChooseShipTrack(v, gp.new_tile, dir, tracks); |
|
780 |
if (track < 0) |
|
781 |
goto reverse_direction; |
|
782 |
||
783 |
b = _ship_subcoord[dir][track]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
784 |
|
0 | 785 |
gp.x = (gp.x&~0xF) | b[0]; |
786 |
gp.y = (gp.y&~0xF) | b[1]; |
|
787 |
||
788 |
/* Call the landscape function and tell it that the vehicle entered the tile */ |
|
789 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
790 |
if (r&0x8) goto reverse_direction; |
|
791 |
||
792 |
if (!(r&0x4)) { |
|
793 |
v->tile = gp.new_tile; |
|
794 |
v->u.ship.state = 1 << track; |
|
795 |
} |
|
796 |
||
797 |
v->direction = b[2]; |
|
798 |
} |
|
799 |
||
800 |
/* update image of ship, as well as delta XY */ |
|
801 |
dir = ShipGetNewDirection(v, gp.x, gp.y); |
|
802 |
v->x_pos = gp.x; |
|
803 |
v->y_pos = gp.y; |
|
804 |
v->z_pos = GetSlopeZ(gp.x, gp.y); |
|
805 |
||
806 |
getout: |
|
807 |
UpdateShipDeltaXY(v, dir); |
|
808 |
v->cur_image = GetShipImage(v, dir); |
|
809 |
VehiclePositionChanged(v); |
|
810 |
EndVehicleMove(v); |
|
811 |
return; |
|
812 |
||
813 |
reverse_direction: |
|
814 |
dir = v->direction ^ 4; |
|
815 |
v->direction = dir; |
|
816 |
goto getout; |
|
817 |
} |
|
818 |
||
819 |
static void AgeShipCargo(Vehicle *v) |
|
820 |
{ |
|
821 |
if (_age_cargo_skip_counter != 0) |
|
822 |
return; |
|
823 |
if (v->cargo_days != 255) |
|
824 |
v->cargo_days++; |
|
825 |
} |
|
826 |
||
827 |
void Ship_Tick(Vehicle *v) |
|
828 |
{ |
|
829 |
AgeShipCargo(v); |
|
830 |
ShipController(v); |
|
831 |
} |
|
832 |
||
833 |
void HandleClickOnShip(Vehicle *v) |
|
834 |
{ |
|
835 |
ShowShipViewWindow(v); |
|
836 |
} |
|
837 |
||
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1055
diff
changeset
|
838 |
void ShipsYearlyLoop(void) |
0 | 839 |
{ |
840 |
Vehicle *v; |
|
841 |
||
842 |
FOR_ALL_VEHICLES(v) { |
|
843 |
if (v->type == VEH_Ship) { |
|
844 |
v->profit_last_year = v->profit_this_year; |
|
845 |
v->profit_this_year = 0; |
|
846 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
847 |
} |
|
848 |
} |
|
849 |
} |
|
850 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
851 |
/** Build a ship. |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
852 |
* @param x,y tile coordinates of depot where ship is built |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
853 |
* @param p1 ship type being built (engine) |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
854 |
* @param p2 unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
855 |
*/ |
0 | 856 |
int32 CmdBuildShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
857 |
{ |
|
858 |
int32 value; |
|
859 |
Vehicle *v; |
|
1282
e7a73ee62d2f
(svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents:
1266
diff
changeset
|
860 |
UnitID unit_num; |
1980
9ea0c89fbb58
(svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents:
1977
diff
changeset
|
861 |
TileIndex tile = TileVirtXY(x, y); |
0 | 862 |
Engine *e; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
863 |
|
1197 | 864 |
if (!IsEngineBuildable(p1, VEH_Ship)) return CMD_ERROR; |
1196
115f46e3807d
(svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents:
1151
diff
changeset
|
865 |
|
0 | 866 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
867 |
||
868 |
value = EstimateShipCost(p1); |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
869 |
if (flags & DC_QUERY_COST) return value; |
0 | 870 |
|
1443
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1401
diff
changeset
|
871 |
/* The ai_new queries the vehicle cost before building the route, |
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1401
diff
changeset
|
872 |
* so we must check against cheaters no sooner than now. --pasky */ |
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1401
diff
changeset
|
873 |
if (!IsTileDepotType(tile, TRANSPORT_WATER)) return CMD_ERROR; |
1901
fb05044cf5c3
(svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents:
1891
diff
changeset
|
874 |
if (!IsTileOwner(tile, _current_player)) return CMD_ERROR; |
1443
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1401
diff
changeset
|
875 |
|
0 | 876 |
v = AllocateVehicle(); |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
877 |
if (v == NULL || IsOrderPoolFull() || |
0 | 878 |
(unit_num = GetFreeUnitNumber(VEH_Ship)) > _patches.max_ships) |
879 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
880 |
|
0 | 881 |
if (flags & DC_EXEC) { |
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
882 |
const ShipVehicleInfo *svi = ShipVehInfo(p1); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
883 |
|
0 | 884 |
v->unitnumber = unit_num; |
885 |
||
886 |
v->owner = _current_player; |
|
887 |
v->tile = tile; |
|
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
888 |
x = TileX(tile) * 16 + 8; |
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
889 |
y = TileY(tile) * 16 + 8; |
0 | 890 |
v->x_pos = x; |
891 |
v->y_pos = y; |
|
892 |
v->z_pos = GetSlopeZ(x,y); |
|
893 |
||
894 |
v->z_height = 6; |
|
895 |
v->sprite_width = 6; |
|
896 |
v->sprite_height = 6; |
|
897 |
v->x_offs = -3; |
|
898 |
v->y_offs = -3; |
|
899 |
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
900 |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
901 |
v->spritenum = svi->image_index; |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
902 |
v->cargo_type = svi->cargo_type; |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
903 |
v->cargo_cap = svi->capacity; |
0 | 904 |
v->value = value; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
905 |
|
1266
9dc5638fe8cc
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1247
diff
changeset
|
906 |
v->last_station_visited = INVALID_STATION; |
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
907 |
v->max_speed = svi->max_speed; |
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2464
diff
changeset
|
908 |
v->engine_type = p1; |
0 | 909 |
|
1926
68d60188a22f
(svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents:
1901
diff
changeset
|
910 |
e = GetEngine(p1); |
0 | 911 |
v->reliability = e->reliability; |
912 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
913 |
v->max_age = e->lifelength * 366; |
|
914 |
_new_ship_id = v->index; |
|
915 |
||
916 |
v->string_id = STR_SV_SHIP_NAME; |
|
917 |
v->u.ship.state = 0x80; |
|
918 |
||
919 |
v->service_interval = _patches.servint_ships; |
|
920 |
v->date_of_last_service = _date; |
|
921 |
v->build_year = _cur_year; |
|
922 |
v->cur_image = 0x0E5E; |
|
923 |
v->type = VEH_Ship; |
|
924 |
||
925 |
VehiclePositionChanged(v); |
|
926 |
||
927 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 928 |
RebuildVehicleLists(); |
0 | 929 |
InvalidateWindow(WC_COMPANY, v->owner); |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
930 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Ship); // updates the replace Ship window |
0 | 931 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
932 |
|
0 | 933 |
return value; |
934 |
} |
|
935 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
936 |
/** Sell a ship. |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
937 |
* @param x,y unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
938 |
* @param p1 vehicle ID to be sold |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
939 |
* @param p2 unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
940 |
*/ |
0 | 941 |
int32 CmdSellShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
942 |
{ |
|
943 |
Vehicle *v; |
|
944 |
||
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
945 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
946 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
947 |
v = GetVehicle(p1); |
0 | 948 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
949 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 950 |
|
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
951 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
952 |
|
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
953 |
if (!IsTileDepotType(v->tile, TRANSPORT_WATER) || v->u.road.state != 0x80 || !(v->vehstatus&VS_STOPPED)) |
0 | 954 |
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
955 |
|
0 | 956 |
if (flags & DC_EXEC) { |
957 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 958 |
RebuildVehicleLists(); |
0 | 959 |
InvalidateWindow(WC_COMPANY, v->owner); |
960 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
961 |
DeleteVehicle(v); |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
962 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Ship); // updates the replace Ship window |
0 | 963 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
964 |
|
0 | 965 |
return -(int32)v->value; |
966 |
} |
|
967 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
968 |
/** Start/Stop a ship. |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
969 |
* @param x,y unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
970 |
* @param p1 ship ID to start/stop |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
971 |
* @param p2 unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
972 |
*/ |
0 | 973 |
int32 CmdStartStopShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
974 |
{ |
|
975 |
Vehicle *v; |
|
976 |
||
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
977 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
978 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
979 |
v = GetVehicle(p1); |
0 | 980 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
981 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 982 |
|
983 |
if (flags & DC_EXEC) { |
|
984 |
v->vehstatus ^= VS_STOPPED; |
|
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
985 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 986 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
987 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 988 |
} |
989 |
||
990 |
return 0; |
|
991 |
} |
|
992 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
993 |
/** Send a ship to the depot. |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
994 |
* @param x,y unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
995 |
* @param p1 vehicle ID to send to the depot |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
996 |
* @param p2 unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
997 |
*/ |
0 | 998 |
int32 CmdSendShipToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
999 |
{ |
|
1000 |
Vehicle *v; |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1001 |
const Depot *dep; |
0 | 1002 |
|
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1003 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1004 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1005 |
v = GetVehicle(p1); |
0 | 1006 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1007 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1008 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1009 |
if (v->vehstatus & VS_CRASHED) return CMD_ERROR; |
1757
b179d50241dc
(svn r2261) - Fix: When crashed vehicles try to find a depot for servicing, openttd asserts.
matthijs
parents:
1752
diff
changeset
|
1010 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1011 |
/* If the current orders are already goto-depot */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1012 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 1013 |
if (flags & DC_EXEC) { |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1014 |
/* If the orders to 'goto depot' are in the orders list (forced servicing), |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1015 |
* then skip to the next order; effectively cancelling this forced service */ |
1530
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
1016 |
if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) |
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
1017 |
v->cur_order_index++; |
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
1018 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1019 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1020 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
1021 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1022 |
} |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1023 |
return 0; |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1024 |
} |
0 | 1025 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1026 |
dep = FindClosestShipDepot(v); |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1027 |
if (dep == NULL) |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1028 |
return_cmd_error(STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT); |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1029 |
|
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1030 |
if (flags & DC_EXEC) { |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1031 |
v->dest_tile = dep->xy; |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1032 |
v->current_order.type = OT_GOTO_DEPOT; |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1033 |
v->current_order.flags = OF_NON_STOP | OF_HALT_IN_DEPOT; |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1034 |
v->current_order.station = dep->index; |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1035 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1036 |
} |
1037 |
||
1038 |
return 0; |
|
1039 |
} |
|
1040 |
||
1790
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1757
diff
changeset
|
1041 |
/** Change the service interval for ships. |
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1757
diff
changeset
|
1042 |
* @param x,y unused |
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1757
diff
changeset
|
1043 |
* @param p1 vehicle ID that is being service-interval-changed |
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1757
diff
changeset
|
1044 |
* @param p2 new service interval |
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1757
diff
changeset
|
1045 |
*/ |
0 | 1046 |
int32 CmdChangeShipServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1047 |
{ |
|
1048 |
Vehicle *v; |
|
1790
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1757
diff
changeset
|
1049 |
uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */ |
0 | 1050 |
|
1790
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1757
diff
changeset
|
1051 |
if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR; |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1052 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1053 |
v = GetVehicle(p1); |
0 | 1054 |
|
1790
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1757
diff
changeset
|
1055 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1056 |
|
1057 |
if (flags & DC_EXEC) { |
|
1790
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1757
diff
changeset
|
1058 |
v->service_interval = serv_int; |
0 | 1059 |
InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7); |
1060 |
} |
|
1061 |
||
1062 |
return 0; |
|
1063 |
} |
|
1064 |
||
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1065 |
/** Refits a ship to the specified cargo type. |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1066 |
* @param x,y unused |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1067 |
* @param p1 vehicle ID of the ship to refit |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1068 |
* @param p2 various bitstuffed elements |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1069 |
* - p2 = (bit 0-7) - the new cargo type to refit to (p2 & 0xFF) |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1070 |
* - p2 = (bit 8) - skip check for stopped in depot, used by autoreplace (p2 & 0x100) |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1071 |
* @todo p2 bit8 check <b>NEEDS TO GO</b> |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1072 |
*/ |
0 | 1073 |
int32 CmdRefitShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1074 |
{ |
|
1075 |
Vehicle *v; |
|
1076 |
int32 cost; |
|
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1077 |
CargoID new_cid = p2 & 0xFF; //gets the cargo number |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1078 |
bool SkipStoppedInDepotCheck = !!HASBIT(p2, 8); // XXX - needs to go, yes? |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1079 |
|
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1080 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1081 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1082 |
v = GetVehicle(p1); |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1083 |
|
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1084 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1085 |
if (!SkipStoppedInDepotCheck) { |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1086 |
if (!IsTileDepotType(v->tile, TRANSPORT_WATER) || !(v->vehstatus&VS_STOPPED) || v->u.ship.state != 0x80) |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1087 |
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1088 |
} |
0 | 1089 |
|
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1090 |
/* Check cargo */ |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1091 |
if (!ShipVehInfo(v->engine_type)->refittable) return CMD_ERROR; |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1092 |
if (new_cid > NUM_CARGO || !CanRefitTo(v, new_cid)) return CMD_ERROR; |
915 | 1093 |
|
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1094 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN); |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1095 |
|
0 | 1096 |
cost = 0; |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1097 |
if (IS_HUMAN_PLAYER(v->owner) && new_cid != v->cargo_type) { |
0 | 1098 |
cost = _price.ship_base >> 7; |
1099 |
} |
|
1100 |
||
1101 |
if (flags & DC_EXEC) { |
|
842 | 1102 |
//autorefitted ships wants to keep the cargo |
1103 |
//it will be checked if the cargo is valid in CmdRenewVehicle |
|
1104 |
if (!(SkipStoppedInDepotCheck)) |
|
915 | 1105 |
v->cargo_count = 0; |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1793
diff
changeset
|
1106 |
v->cargo_type = new_cid; |
0 | 1107 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
1108 |
} |
|
1109 |
||
1110 |
return cost; |
|
1111 |
||
1112 |
} |