author | Darkvater |
Thu, 12 May 2005 00:11:37 +0000 | |
changeset 1793 | 8ac8a8c9ec0f |
parent 1790 | 4afb4b4e4278 |
child 1802 | 448f187042d3 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
445
diff
changeset
|
3 |
#include "table/strings.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
|
4 |
#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
|
5 |
#include "tile.h" |
0 | 6 |
#include "vehicle.h" |
7 |
#include "command.h" |
|
8 |
#include "pathfind.h" |
|
9 |
#include "station.h" |
|
10 |
#include "gfx.h" |
|
11 |
#include "news.h" |
|
12 |
#include "engine.h" |
|
13 |
#include "gui.h" |
|
14 |
#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
|
15 |
#include "sound.h" |
1247 | 16 |
#include "npf.h" |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
17 |
#include "depot.h" |
0 | 18 |
|
19 |
static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D}; |
|
20 |
static const byte _ship_sometracks[4] = {0x19, 0x16, 0x25, 0x2A}; |
|
21 |
||
22 |
static byte GetTileShipTrackStatus(uint tile) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
23 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); |
0 | 24 |
return r | r >> 8; |
25 |
} |
|
26 |
||
27 |
void DrawShipEngine(int x, int y, int engine, uint32 image_ormod) |
|
28 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
29 |
int spritenum = ShipVehInfo(engine)->image_index; |
0 | 30 |
|
31 |
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
|
32 |
int sprite = GetCustomVehicleIcon(engine, 6); |
0 | 33 |
|
34 |
if (sprite) { |
|
35 |
DrawSprite(sprite | image_ormod, x, y); |
|
36 |
return; |
|
37 |
} |
|
38 |
spritenum = _engine_original_sprites[engine]; |
|
39 |
} |
|
40 |
DrawSprite((6 + _ship_sprites[spritenum]) | image_ormod, x, y); |
|
41 |
} |
|
42 |
||
43 |
void DrawShipEngineInfo(int engine, int x, int y, int maxw) |
|
44 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
45 |
const ShipVehicleInfo *svi = ShipVehInfo(engine); |
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
46 |
SetDParam(0, svi->base_cost * (_price.ship_base>>3)>>5); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
47 |
SetDParam(1, svi->max_speed * 10 >> 5); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
48 |
SetDParam(2, _cargoc.names_long_p[svi->cargo_type]); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
49 |
SetDParam(3, svi->capacity); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
50 |
SetDParam(4, svi->running_cost * _price.ship_running >> 8); |
0 | 51 |
DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw); |
52 |
} |
|
53 |
||
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
|
54 |
int GetShipImage(const Vehicle *v, byte direction) |
0 | 55 |
{ |
56 |
int spritenum = v->spritenum; |
|
57 |
||
58 |
if (is_custom_sprite(spritenum)) { |
|
59 |
int sprite = GetCustomVehicleSprite(v, direction); |
|
60 |
||
61 |
if (sprite) return sprite; |
|
62 |
spritenum = _engine_original_sprites[v->engine_type]; |
|
63 |
} |
|
64 |
return _ship_sprites[spritenum] + direction; |
|
65 |
} |
|
66 |
||
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
67 |
static Depot *FindClosestShipDepot(Vehicle *v) |
0 | 68 |
{ |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
69 |
Depot *depot; |
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
70 |
Depot *best_depot = NULL; |
0 | 71 |
uint tile, dist, best_dist = (uint)-1; |
72 |
uint tile2 = v->tile; |
|
73 |
||
1247 | 74 |
if (_patches.new_pathfinding_all) { |
75 |
NPFFoundTargetData ftd; |
|
1752
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1751
diff
changeset
|
76 |
byte trackdir = GetVehicleTrackdir(v); |
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
|
77 |
ftd = NPFRouteToDepotTrialError(v->tile, trackdir, TRANSPORT_WATER, v->owner); |
1247 | 78 |
if (ftd.best_bird_dist == 0) |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
79 |
best_depot = GetDepotByTile(ftd.node.tile); /* Found target */ |
1247 | 80 |
else |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
81 |
best_depot = NULL; /* Did not find target */ |
1247 | 82 |
} else { |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
83 |
FOR_ALL_DEPOTS(depot) { |
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
84 |
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
|
85 |
if (IsValidDepot(depot) && IsTileDepotType(tile, TRANSPORT_WATER) && IsTileOwner(tile, v->owner)) { |
1247 | 86 |
dist = DistanceManhattan(tile, tile2); |
87 |
if (dist < best_dist) { |
|
88 |
best_dist = dist; |
|
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
89 |
best_depot = depot; |
1247 | 90 |
} |
0 | 91 |
} |
92 |
} |
|
93 |
} |
|
94 |
return best_depot; |
|
95 |
} |
|
96 |
||
97 |
static void CheckIfShipNeedsService(Vehicle *v) |
|
98 |
{ |
|
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
99 |
Depot *depot; |
0 | 100 |
|
1520
c85dc79795e0
(svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents:
1443
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
|
1520
c85dc79795e0
(svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents:
1443
diff
changeset
|
104 |
if (!VehicleNeedsService(v)) |
0 | 105 |
return; |
106 |
||
107 |
if (v->vehstatus & VS_STOPPED) |
|
108 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
109 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
110 |
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
|
111 |
v->current_order.flags & OF_HALT_IN_DEPOT) |
0 | 112 |
return; |
113 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
114 |
if (_patches.gotodepot && VehicleHasDepotOrders(v)) |
0 | 115 |
return; |
116 |
||
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
117 |
depot = FindClosestShipDepot(v); |
0 | 118 |
|
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
119 |
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
|
120 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
121 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
122 |
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
|
123 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 124 |
} |
125 |
return; |
|
126 |
} |
|
127 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
128 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
129 |
v->current_order.flags = OF_NON_STOP; |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
130 |
v->current_order.station = depot->index; |
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
131 |
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
|
132 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 133 |
} |
134 |
||
135 |
void OnNewDay_Ship(Vehicle *v) |
|
136 |
{ |
|
137 |
int32 cost; |
|
138 |
||
139 |
if ((++v->day_counter & 7) == 0) |
|
140 |
DecreaseVehicleValue(v); |
|
141 |
||
142 |
CheckVehicleBreakdown(v); |
|
143 |
AgeVehicle(v); |
|
144 |
CheckIfShipNeedsService(v); |
|
145 |
||
1053
dfb5243315f1
(svn r1554) -Fix: [ 1103187 ] Order Check messages are now validated before
celestar
parents:
1035
diff
changeset
|
146 |
CheckOrders(v->index, OC_INIT); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
147 |
|
0 | 148 |
if (v->vehstatus & VS_STOPPED) |
149 |
return; |
|
150 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
151 |
|
19
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
152 |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
153 |
cost = ShipVehInfo(v->engine_type)->running_cost * _price.ship_running / 364; |
0 | 154 |
v->profit_this_year -= cost >> 8; |
155 |
||
156 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN); |
|
157 |
SubtractMoneyFromPlayerFract(v->owner, cost); |
|
158 |
||
159 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1055
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
160 |
//we need this for the profit |
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
161 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 162 |
} |
163 |
||
164 |
static void HandleBrokenShip(Vehicle *v) |
|
165 |
{ |
|
166 |
if (v->breakdown_ctr != 1) { |
|
167 |
v->breakdown_ctr = 1; |
|
168 |
v->cur_speed = 0; |
|
169 |
||
170 |
if (v->breakdowns_since_last_service != 255) |
|
171 |
v->breakdowns_since_last_service++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
172 |
|
0 | 173 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
174 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
175 |
|
541 | 176 |
SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? |
177 |
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v); |
|
0 | 178 |
|
179 |
if (!(v->vehstatus & VS_HIDDEN)) { |
|
180 |
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); |
|
181 |
if (u) |
|
182 |
u->u.special.unk0 = v->breakdown_delay * 2; |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
if (!(v->tick_counter & 1)) { |
|
187 |
if (!--v->breakdown_delay) { |
|
188 |
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
|
189 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
0 | 190 |
} |
191 |
} |
|
192 |
} |
|
193 |
||
194 |
static void MarkShipDirty(Vehicle *v) |
|
195 |
{ |
|
196 |
v->cur_image = GetShipImage(v, v->direction); |
|
197 |
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); |
|
198 |
} |
|
199 |
||
200 |
static void PlayShipSound(Vehicle *v) |
|
201 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
202 |
SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v); |
0 | 203 |
} |
204 |
||
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
205 |
static const TileIndexDiffC _dock_offs[] = { |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
206 |
{ 2, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
207 |
{-2, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
208 |
{ 0, 2}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
209 |
{ 2, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
210 |
{ 0, -2}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
211 |
{ 0, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
212 |
{ 0, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
213 |
{ 0, 0} |
0 | 214 |
}; |
215 |
||
216 |
static void ProcessShipOrder(Vehicle *v) |
|
217 |
{ |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
218 |
const Order *order; |
0 | 219 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
220 |
if (v->current_order.type >= OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
221 |
v->current_order.type <= OT_LEAVESTATION) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
222 |
if (v->current_order.type != OT_GOTO_DEPOT || |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
223 |
!(v->current_order.flags & OF_UNLOAD)) |
0 | 224 |
return; |
225 |
} |
|
226 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
227 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
228 |
(v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) && |
1520
c85dc79795e0
(svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents:
1443
diff
changeset
|
229 |
!VehicleNeedsService(v)) { |
0 | 230 |
v->cur_order_index++; |
231 |
} |
|
232 |
||
233 |
||
234 |
if (v->cur_order_index >= v->num_orders) |
|
235 |
v->cur_order_index = 0; |
|
236 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
237 |
order = GetVehicleOrder(v, v->cur_order_index); |
0 | 238 |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
239 |
if (order == NULL) { |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
240 |
v->current_order.type = OT_NOTHING; |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
241 |
v->current_order.flags = 0; |
0 | 242 |
v->dest_tile = 0; |
243 |
return; |
|
244 |
} |
|
245 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
246 |
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
|
247 |
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
|
248 |
order->station == v->current_order.station) |
0 | 249 |
return; |
250 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
251 |
v->current_order = *order; |
0 | 252 |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
253 |
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
|
254 |
const Station *st; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
255 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
256 |
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
|
257 |
v->last_station_visited = INVALID_STATION; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
258 |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
259 |
st = GetStation(order->station); |
0 | 260 |
if (st->dock_tile != 0) { |
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
261 |
v->dest_tile = TILE_ADD(st->dock_tile, ToTileIndexDiff(_dock_offs[_map5[st->dock_tile]-0x4B])); |
0 | 262 |
} |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
263 |
} else if (order->type == OT_GOTO_DEPOT) { |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
264 |
v->dest_tile = GetDepot(order->station)->xy; |
0 | 265 |
} else { |
266 |
v->dest_tile = 0; |
|
267 |
} |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
268 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
269 |
InvalidateVehicleOrder(v); |
1020
1223cd937174
(svn r1521) -Fix: Ship Vehicle Lists are now redrawn correctly
celestar
parents:
926
diff
changeset
|
270 |
|
1055
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
271 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 272 |
} |
273 |
||
274 |
static void HandleShipLoading(Vehicle *v) |
|
275 |
{ |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
276 |
if (v->current_order.type == OT_NOTHING) |
0 | 277 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
278 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
279 |
if (v->current_order.type != OT_DUMMY) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
280 |
if (v->current_order.type != OT_LOADING) |
0 | 281 |
return; |
282 |
||
283 |
if (--v->load_unload_time_rem) |
|
284 |
return; |
|
285 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
286 |
if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) { |
0 | 287 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); |
288 |
if (LoadUnloadVehicle(v)) { |
|
289 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
|
290 |
MarkShipDirty(v); |
|
291 |
} |
|
292 |
return; |
|
293 |
} |
|
294 |
PlayShipSound(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
295 |
|
0 | 296 |
{ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
297 |
Order b = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
298 |
v->current_order.type = OT_LEAVESTATION; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
299 |
v->current_order.flags = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
300 |
if (!(b.flags & OF_NON_STOP)) |
0 | 301 |
return; |
302 |
} |
|
303 |
} |
|
304 |
||
305 |
v->cur_order_index++; |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
306 |
InvalidateVehicleOrder(v); |
0 | 307 |
} |
308 |
||
309 |
static void UpdateShipDeltaXY(Vehicle *v, int dir) |
|
310 |
{ |
|
311 |
#define MKIT(d,c,b,a) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0) |
|
312 |
static const uint32 _delta_xy_table[8] = { |
|
313 |
MKIT( -3, -3, 6, 6), |
|
314 |
MKIT(-16, -3, 32, 6), |
|
315 |
MKIT( -3, -3, 6, 6), |
|
316 |
MKIT( -3,-16, 6, 32), |
|
317 |
MKIT( -3, -3, 6, 6), |
|
318 |
MKIT(-16, -3, 32, 6), |
|
319 |
MKIT( -3, -3, 6, 6), |
|
320 |
MKIT( -3,-16, 6, 32), |
|
321 |
}; |
|
322 |
#undef MKIT |
|
323 |
uint32 x = _delta_xy_table[dir]; |
|
324 |
v->x_offs = (byte)x; |
|
325 |
v->y_offs = (byte)(x>>=8); |
|
326 |
v->sprite_width = (byte)(x>>=8); |
|
327 |
v->sprite_height = (byte)(x>>=8); |
|
328 |
} |
|
329 |
||
330 |
static void RecalcShipStuff(Vehicle *v) |
|
331 |
{ |
|
332 |
UpdateShipDeltaXY(v, v->direction); |
|
333 |
v->cur_image = GetShipImage(v, v->direction); |
|
334 |
MarkShipDirty(v); |
|
335 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
336 |
} |
|
337 |
||
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
338 |
static const TileIndexDiffC _ship_leave_depot_offs[] = { |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
339 |
{-1, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
340 |
{ 0, -1} |
0 | 341 |
}; |
342 |
||
343 |
static void CheckShipLeaveDepot(Vehicle *v) |
|
344 |
{ |
|
345 |
uint tile; |
|
346 |
int d; |
|
347 |
uint m; |
|
348 |
||
349 |
if (v->u.ship.state != 0x80) |
|
350 |
return; |
|
351 |
||
352 |
tile = v->tile; |
|
353 |
d = (_map5[tile]&2) ? 1 : 0; |
|
354 |
||
355 |
// Check first side |
|
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
356 |
if (_ship_sometracks[d] & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[d])))) { |
0 | 357 |
m = (d==0) ? 0x101 : 0x207; |
358 |
// Check second side |
|
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
359 |
} else if (_ship_sometracks[d+2] & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[d])))) { |
0 | 360 |
m = (d==0) ? 0x105 : 0x203; |
361 |
} else { |
|
362 |
return; |
|
363 |
} |
|
364 |
v->direction = (byte)m; |
|
365 |
v->u.ship.state = (byte)(m >> 8); |
|
366 |
v->vehstatus &= ~VS_HIDDEN; |
|
367 |
||
368 |
v->cur_speed = 0; |
|
369 |
RecalcShipStuff(v); |
|
370 |
||
371 |
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
|
372 |
VehicleServiceInDepot(v); |
1055
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
373 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 374 |
} |
375 |
||
376 |
static bool ShipAccelerate(Vehicle *v) |
|
377 |
{ |
|
378 |
uint spd; |
|
379 |
byte t; |
|
380 |
||
381 |
spd = min(v->cur_speed + 1, v->max_speed); |
|
382 |
||
383 |
//updates statusbar only if speed have changed to save CPU time |
|
384 |
if (spd != v->cur_speed) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
385 |
v->cur_speed = spd; |
0 | 386 |
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
|
387 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 388 |
} |
389 |
||
390 |
// Decrease somewhat when turning |
|
391 |
if (!(v->direction&1)) { |
|
392 |
spd = spd * 3 >> 2; |
|
393 |
} |
|
394 |
||
395 |
if (spd == 0) |
|
396 |
return false; |
|
397 |
||
398 |
if ((byte)++spd == 0) |
|
399 |
return true; |
|
400 |
||
401 |
v->progress = (t = v->progress) - (byte)spd; |
|
402 |
||
403 |
return (t < v->progress); |
|
404 |
} |
|
405 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
406 |
int32 EstimateShipCost(EngineID engine_type) |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
407 |
{ |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
408 |
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
|
409 |
} |
0 | 410 |
|
411 |
static void ShipEnterDepot(Vehicle *v) |
|
412 |
{ |
|
413 |
v->u.ship.state = 0x80; |
|
414 |
v->vehstatus |= VS_HIDDEN; |
|
415 |
v->cur_speed = 0; |
|
416 |
RecalcShipStuff(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
417 |
|
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
|
418 |
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
|
419 |
|
0 | 420 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
421 |
||
842 | 422 |
MaybeReplaceVehicle(v); |
0 | 423 |
|
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
|
424 |
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
|
425 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
426 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
427 |
Order t; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
428 |
|
0 | 429 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
430 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
431 |
t = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
432 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
433 |
v->current_order.flags = 0; |
0 | 434 |
|
1530
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
435 |
if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
436 |
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
|
437 |
} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) { |
0 | 438 |
v->vehstatus |= VS_STOPPED; |
439 |
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
|
440 |
SetDParam(0, v->unitnumber); |
0 | 441 |
AddNewsItem( |
442 |
STR_981C_SHIP_IS_WAITING_IN_DEPOT, |
|
443 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
444 |
v->index, |
|
445 |
0); |
|
446 |
} |
|
447 |
} |
|
448 |
} |
|
1055
3aae1d6f12aa
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
449 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 450 |
} |
451 |
||
452 |
static void ShipArrivesAt(Vehicle *v, Station *st) |
|
453 |
{ |
|
454 |
/* Check if station was ever visited before */ |
|
455 |
if (!(st->had_vehicle_of_type & HVOT_SHIP)) { |
|
456 |
uint32 flags; |
|
457 |
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
|
458 |
SetDParam(0, st->index); |
0 | 459 |
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); |
460 |
AddNewsItem( |
|
461 |
STR_9833_CITIZENS_CELEBRATE_FIRST, |
|
462 |
flags, |
|
463 |
v->index, |
|
464 |
0); |
|
465 |
} |
|
466 |
} |
|
467 |
||
468 |
typedef struct { |
|
469 |
uint skiptile; |
|
470 |
uint dest_coords; |
|
471 |
uint best_bird_dist; |
|
472 |
uint best_length; |
|
473 |
} PathFindShip; |
|
474 |
||
475 |
//extern void dbg_store_path(); |
|
476 |
||
477 |
static bool ShipTrackFollower(uint tile, PathFindShip *pfs, int track, uint length, byte *state) |
|
478 |
{ |
|
479 |
// Found dest? |
|
480 |
if (tile == pfs->dest_coords) { |
|
481 |
pfs->best_bird_dist = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
482 |
|
0 | 483 |
// if (length < pfs->best_length) |
484 |
// dbg_store_path(); |
|
485 |
||
486 |
pfs->best_length = minu(pfs->best_length, length); |
|
487 |
return true; |
|
488 |
} |
|
489 |
||
490 |
// Skip this tile in the calculation |
|
491 |
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
|
492 |
pfs->best_bird_dist = minu(pfs->best_bird_dist, DistanceMaxPlusManhattan(pfs->dest_coords, tile)); |
0 | 493 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
494 |
|
0 | 495 |
return false; |
496 |
} |
|
497 |
||
498 |
static const byte _ship_search_directions[6][4] = { |
|
499 |
{ 0, 9, 2, 9 }, |
|
500 |
{ 9, 1, 9, 3 }, |
|
501 |
{ 9, 0, 3, 9 }, |
|
502 |
{ 1, 9, 9, 2 }, |
|
503 |
{ 3, 2, 9, 9 }, |
|
504 |
{ 9, 9, 1, 0 }, |
|
505 |
}; |
|
506 |
||
507 |
static const byte _pick_shiptrack_table[6] = {1, 3, 2, 2, 0, 0}; |
|
508 |
||
509 |
static uint FindShipTrack(Vehicle *v, uint tile, int dir, uint bits, uint skiptile, int *track) |
|
510 |
{ |
|
511 |
PathFindShip pfs; |
|
512 |
int i, best_track; |
|
513 |
uint best_bird_dist = 0; |
|
514 |
uint best_length = 0; |
|
515 |
uint r; |
|
516 |
byte ship_dir = v->direction & 3; |
|
517 |
||
518 |
pfs.dest_coords = v->dest_tile; |
|
519 |
pfs.skiptile = skiptile; |
|
520 |
||
521 |
best_track = -1; |
|
522 |
||
523 |
do { |
|
524 |
i = FIND_FIRST_BIT(bits); |
|
525 |
bits = KILL_FIRST_BIT(bits); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
526 |
|
0 | 527 |
pfs.best_bird_dist = (uint)-1; |
528 |
pfs.best_length = (uint)-1; |
|
529 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
530 |
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
|
531 |
|
0 | 532 |
if (best_track >= 0) { |
533 |
if (pfs.best_bird_dist != 0) { |
|
534 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
|
535 |
if (pfs.best_bird_dist > best_bird_dist) goto bad; |
|
536 |
if (pfs.best_bird_dist < best_bird_dist) goto good; |
|
537 |
} else { |
|
538 |
if (pfs.best_length > best_length) goto bad; |
|
539 |
if (pfs.best_length < best_length) goto good; |
|
540 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
541 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
542 |
/* if we reach this position, there's two paths of equal value so far. |
0 | 543 |
* pick one randomly. */ |
544 |
r = (byte)Random(); |
|
545 |
if (_pick_shiptrack_table[i] == ship_dir) r += 80; |
|
546 |
if (_pick_shiptrack_table[best_track] == ship_dir) r -= 80; |
|
547 |
if (r <= 127) goto bad; |
|
548 |
} |
|
549 |
good:; |
|
550 |
best_track = i; |
|
551 |
best_bird_dist = pfs.best_bird_dist; |
|
552 |
best_length = pfs.best_length; |
|
553 |
bad:; |
|
554 |
||
555 |
} while (bits != 0); |
|
556 |
||
557 |
*track = best_track; |
|
558 |
return best_bird_dist; |
|
559 |
} |
|
560 |
||
1247 | 561 |
/* returns the track to choose on the next tile, or -1 when it's better to |
562 |
* reverse. The tile given is the tile we are about to enter, enterdir is the |
|
563 |
* direction in which we are entering the tile */ |
|
564 |
static int ChooseShipTrack(Vehicle *v, uint tile, int enterdir, uint tracks) |
|
0 | 565 |
{ |
1247 | 566 |
assert(enterdir>=0 && enterdir<=3); |
0 | 567 |
|
1247 | 568 |
if (_patches.new_pathfinding_all) { |
569 |
NPFFindStationOrTileData fstd; |
|
570 |
NPFFoundTargetData ftd; |
|
571 |
uint src_tile = TILE_ADD(tile, TileOffsByDir(_reverse_dir[enterdir])); |
|
1752
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1751
diff
changeset
|
572 |
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
|
573 |
assert (trackdir != 0xFF); /* Check that we are not in a depot */ |
1247 | 574 |
|
575 |
NPFFillWithOrderData(&fstd, v); |
|
576 |
||
1752
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1751
diff
changeset
|
577 |
ftd = NPFRouteToStationOrTile(src_tile, trackdir, &fstd, TRANSPORT_WATER, v->owner); |
1247 | 578 |
|
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
|
579 |
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
|
580 |
/* 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
|
581 |
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
|
582 |
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
|
583 |
to the tile closest to our target. */ |
1247 | 584 |
return ftd.best_trackdir & 7; /* TODO: Wrapper function? */ |
585 |
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
|
586 |
return -1; /* Already at target, reverse? */ |
1247 | 587 |
} else { |
588 |
uint b; |
|
589 |
uint tot_dist, dist; |
|
590 |
int track; |
|
591 |
uint tile2; |
|
592 |
||
593 |
tile2 = TILE_ADD(tile, -TileOffsByDir(enterdir)); |
|
594 |
tot_dist = (uint)-1; |
|
595 |
||
596 |
/* Let's find out how far it would be if we would reverse first */ |
|
597 |
b = GetTileShipTrackStatus(tile2) & _ship_sometracks[_reverse_dir[enterdir]] & v->u.ship.state; |
|
598 |
if (b != 0) { |
|
599 |
dist = FindShipTrack(v, tile2, _reverse_dir[enterdir], b, tile, &track); |
|
600 |
if (dist != (uint)-1) |
|
601 |
tot_dist = dist + 1; |
|
602 |
} |
|
603 |
/* And if we would not reverse? */ |
|
604 |
dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track); |
|
605 |
if (dist > tot_dist) |
|
606 |
/* We could better reverse */ |
|
607 |
return -1; |
|
608 |
return track; |
|
0 | 609 |
} |
610 |
} |
|
611 |
||
612 |
static const byte _new_vehicle_direction_table[11] = { |
|
613 |
0, 7, 6, 0, |
|
614 |
1, 0, 5, 0, |
|
615 |
2, 3, 4, |
|
616 |
}; |
|
617 |
||
618 |
static int ShipGetNewDirectionFromTiles(uint new_tile, uint old_tile) |
|
619 |
{ |
|
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
|
620 |
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
|
621 |
TileX(new_tile) - TileX(old_tile) + 1; |
0 | 622 |
assert(offs < 11 && offs != 3 && offs != 7); |
623 |
return _new_vehicle_direction_table[offs]; |
|
624 |
} |
|
625 |
||
626 |
static int ShipGetNewDirection(Vehicle *v, int x, int y) |
|
627 |
{ |
|
628 |
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); |
|
629 |
assert(offs < 11 && offs != 3 && offs != 7); |
|
630 |
return _new_vehicle_direction_table[offs]; |
|
631 |
} |
|
632 |
||
633 |
static int GetAvailShipTracks(uint tile, int dir) |
|
634 |
{ |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
635 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); |
0 | 636 |
return (byte) ((r | r >> 8)) & _ship_sometracks[dir]; |
637 |
} |
|
638 |
||
639 |
static const byte _ship_subcoord[4][6][3] = { |
|
640 |
{ |
|
641 |
{15, 8, 1}, |
|
642 |
{ 0, 0, 0}, |
|
643 |
{ 0, 0, 0}, |
|
644 |
{15, 8, 2}, |
|
645 |
{15, 7, 0}, |
|
646 |
{ 0, 0, 0}, |
|
647 |
}, |
|
648 |
{ |
|
649 |
{ 0, 0, 0}, |
|
650 |
{ 8, 0, 3}, |
|
651 |
{ 7, 0, 2}, |
|
652 |
{ 0, 0, 0}, |
|
653 |
{ 8, 0, 4}, |
|
654 |
{ 0, 0, 0}, |
|
655 |
}, |
|
656 |
{ |
|
657 |
{ 0, 8, 5}, |
|
658 |
{ 0, 0, 0}, |
|
659 |
{ 0, 7, 6}, |
|
660 |
{ 0, 0, 0}, |
|
661 |
{ 0, 0, 0}, |
|
662 |
{ 0, 8, 4}, |
|
663 |
}, |
|
664 |
{ |
|
665 |
{ 0, 0, 0}, |
|
666 |
{ 8,15, 7}, |
|
667 |
{ 0, 0, 0}, |
|
668 |
{ 8,15, 6}, |
|
669 |
{ 0, 0, 0}, |
|
670 |
{ 7,15, 0}, |
|
671 |
} |
|
672 |
}; |
|
673 |
||
674 |
static void ShipController(Vehicle *v) |
|
675 |
{ |
|
676 |
GetNewVehiclePosResult gp; |
|
677 |
uint32 r; |
|
678 |
const byte *b; |
|
679 |
int dir,track,tracks; |
|
680 |
||
681 |
v->tick_counter++; |
|
682 |
||
683 |
if (v->breakdown_ctr != 0) { |
|
684 |
if (v->breakdown_ctr <= 2) { |
|
685 |
HandleBrokenShip(v); |
|
686 |
return; |
|
687 |
} |
|
688 |
v->breakdown_ctr--; |
|
689 |
} |
|
690 |
||
691 |
if (v->vehstatus & VS_STOPPED) |
|
692 |
return; |
|
693 |
||
694 |
ProcessShipOrder(v); |
|
695 |
HandleShipLoading(v); |
|
696 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
697 |
if (v->current_order.type == OT_LOADING) |
0 | 698 |
return; |
699 |
||
700 |
CheckShipLeaveDepot(v); |
|
701 |
||
702 |
if (!ShipAccelerate(v)) |
|
703 |
return; |
|
704 |
||
705 |
BeginVehicleMove(v); |
|
706 |
||
707 |
if (GetNewVehiclePos(v, &gp)) { |
|
708 |
// staying in tile |
|
709 |
if (v->u.ship.state == 0x80) { |
|
710 |
gp.x = v->x_pos; |
|
711 |
gp.y = v->y_pos; |
|
712 |
} else { |
|
713 |
/* isnot inside depot */ |
|
714 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
715 |
if (r & 0x8) goto reverse_direction; |
|
716 |
||
1751
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
717 |
/* 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
|
718 |
* always skip ahead. */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
719 |
if (v->current_order.type == OT_LEAVESTATION) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
720 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
721 |
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
|
722 |
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
|
723 |
} else if (v->dest_tile != 0) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
724 |
/* 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
|
725 |
if (v->current_order.type == OT_GOTO_STATION && |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
726 |
IsBuoyTile(v->dest_tile) && |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
727 |
DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
728 |
/* 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
|
729 |
* next order */ |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
730 |
v->cur_order_index++; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
731 |
v->current_order.type = OT_DUMMY; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
732 |
InvalidateVehicleOrder(v); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
733 |
} else { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
734 |
/* 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
|
735 |
if (v->dest_tile == gp.new_tile) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
736 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
737 |
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
|
738 |
ShipEnterDepot(v); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
739 |
return; |
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 |
} 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
|
742 |
Station *st; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
743 |
|
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
744 |
v->last_station_visited = v->current_order.station; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
745 |
|
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
746 |
/* Process station in the orderlist. */ |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
747 |
st = GetStation(v->current_order.station); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
748 |
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
|
749 |
v->current_order.type = OT_LOADING; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
750 |
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
|
751 |
v->current_order.flags |= OF_NON_STOP; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
752 |
ShipArrivesAt(v, st); |
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 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
755 |
if (LoadUnloadVehicle(v)) { |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
756 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
757 |
MarkShipDirty(v); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
758 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
759 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
760 |
} else { /* leave stations without docks right aways */ |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
761 |
v->current_order.type = OT_LEAVESTATION; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
762 |
v->current_order.flags = 0; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
763 |
v->cur_order_index++; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
764 |
InvalidateVehicleOrder(v); |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
765 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
766 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
767 |
} |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1698
diff
changeset
|
768 |
} |
0 | 769 |
} |
770 |
} |
|
771 |
} else { |
|
772 |
// 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
|
773 |
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
|
774 |
goto reverse_direction; |
0 | 775 |
|
776 |
dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile); |
|
777 |
assert(dir == 1 || dir == 3 || dir == 5 || dir == 7); |
|
778 |
dir>>=1; |
|
779 |
tracks = GetAvailShipTracks(gp.new_tile, dir); |
|
780 |
if (tracks == 0) |
|
781 |
goto reverse_direction; |
|
782 |
||
783 |
// Choose a direction, and continue if we find one |
|
784 |
track = ChooseShipTrack(v, gp.new_tile, dir, tracks); |
|
785 |
if (track < 0) |
|
786 |
goto reverse_direction; |
|
787 |
||
788 |
b = _ship_subcoord[dir][track]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
789 |
|
0 | 790 |
gp.x = (gp.x&~0xF) | b[0]; |
791 |
gp.y = (gp.y&~0xF) | b[1]; |
|
792 |
||
793 |
/* Call the landscape function and tell it that the vehicle entered the tile */ |
|
794 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
795 |
if (r&0x8) goto reverse_direction; |
|
796 |
||
797 |
if (!(r&0x4)) { |
|
798 |
v->tile = gp.new_tile; |
|
799 |
v->u.ship.state = 1 << track; |
|
800 |
} |
|
801 |
||
802 |
v->direction = b[2]; |
|
803 |
} |
|
804 |
||
805 |
/* update image of ship, as well as delta XY */ |
|
806 |
dir = ShipGetNewDirection(v, gp.x, gp.y); |
|
807 |
v->x_pos = gp.x; |
|
808 |
v->y_pos = gp.y; |
|
809 |
v->z_pos = GetSlopeZ(gp.x, gp.y); |
|
810 |
||
811 |
getout: |
|
812 |
UpdateShipDeltaXY(v, dir); |
|
813 |
v->cur_image = GetShipImage(v, dir); |
|
814 |
VehiclePositionChanged(v); |
|
815 |
EndVehicleMove(v); |
|
816 |
return; |
|
817 |
||
818 |
reverse_direction: |
|
819 |
dir = v->direction ^ 4; |
|
820 |
v->direction = dir; |
|
821 |
goto getout; |
|
822 |
} |
|
823 |
||
824 |
static void AgeShipCargo(Vehicle *v) |
|
825 |
{ |
|
826 |
if (_age_cargo_skip_counter != 0) |
|
827 |
return; |
|
828 |
if (v->cargo_days != 255) |
|
829 |
v->cargo_days++; |
|
830 |
} |
|
831 |
||
832 |
void Ship_Tick(Vehicle *v) |
|
833 |
{ |
|
834 |
AgeShipCargo(v); |
|
835 |
ShipController(v); |
|
836 |
} |
|
837 |
||
838 |
void HandleClickOnShip(Vehicle *v) |
|
839 |
{ |
|
840 |
ShowShipViewWindow(v); |
|
841 |
} |
|
842 |
||
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
|
843 |
void ShipsYearlyLoop(void) |
0 | 844 |
{ |
845 |
Vehicle *v; |
|
846 |
||
847 |
FOR_ALL_VEHICLES(v) { |
|
848 |
if (v->type == VEH_Ship) { |
|
849 |
v->profit_last_year = v->profit_this_year; |
|
850 |
v->profit_this_year = 0; |
|
851 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
852 |
} |
|
853 |
} |
|
854 |
} |
|
855 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
856 |
/** Build a ship. |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
857 |
* @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
|
858 |
* @param p1 ship type being built (engine) |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
859 |
* @param p2 unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
860 |
*/ |
0 | 861 |
int32 CmdBuildShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
862 |
{ |
|
863 |
int32 value; |
|
864 |
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
|
865 |
UnitID unit_num; |
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
|
866 |
TileIndex tile = TILE_FROM_XY(x,y); |
0 | 867 |
Engine *e; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
868 |
|
1197 | 869 |
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
|
870 |
|
0 | 871 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
872 |
||
873 |
value = EstimateShipCost(p1); |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
874 |
if (flags & DC_QUERY_COST) return value; |
0 | 875 |
|
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
|
876 |
/* 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
|
877 |
* 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
|
878 |
if (!IsTileDepotType(tile, TRANSPORT_WATER)) return CMD_ERROR; |
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
|
879 |
if (_map_owner[tile] != _current_player) return CMD_ERROR; |
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
|
880 |
|
0 | 881 |
v = AllocateVehicle(); |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
882 |
if (v == NULL || IsOrderPoolFull() || |
0 | 883 |
(unit_num = GetFreeUnitNumber(VEH_Ship)) > _patches.max_ships) |
884 |
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
|
885 |
|
0 | 886 |
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
|
887 |
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
|
888 |
|
0 | 889 |
v->unitnumber = unit_num; |
890 |
||
891 |
v->owner = _current_player; |
|
892 |
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
|
893 |
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
|
894 |
y = TileY(tile) * 16 + 8; |
0 | 895 |
v->x_pos = x; |
896 |
v->y_pos = y; |
|
897 |
v->z_pos = GetSlopeZ(x,y); |
|
898 |
||
899 |
v->z_height = 6; |
|
900 |
v->sprite_width = 6; |
|
901 |
v->sprite_height = 6; |
|
902 |
v->x_offs = -3; |
|
903 |
v->y_offs = -3; |
|
904 |
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
|
905 |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
906 |
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
|
907 |
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
|
908 |
v->cargo_cap = svi->capacity; |
0 | 909 |
v->value = value; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
910 |
|
1266
9dc5638fe8cc
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1247
diff
changeset
|
911 |
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
|
912 |
v->max_speed = svi->max_speed; |
0 | 913 |
v->engine_type = (byte)p1; |
914 |
||
915 |
e = &_engines[p1]; |
|
916 |
v->reliability = e->reliability; |
|
917 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
918 |
v->max_age = e->lifelength * 366; |
|
919 |
_new_ship_id = v->index; |
|
920 |
||
921 |
v->string_id = STR_SV_SHIP_NAME; |
|
922 |
v->u.ship.state = 0x80; |
|
923 |
||
924 |
v->service_interval = _patches.servint_ships; |
|
925 |
v->date_of_last_service = _date; |
|
926 |
v->build_year = _cur_year; |
|
927 |
v->cur_image = 0x0E5E; |
|
928 |
v->type = VEH_Ship; |
|
929 |
||
930 |
VehiclePositionChanged(v); |
|
931 |
||
932 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 933 |
RebuildVehicleLists(); |
0 | 934 |
InvalidateWindow(WC_COMPANY, v->owner); |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
935 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Ship); // updates the replace Ship window |
0 | 936 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
937 |
|
0 | 938 |
return value; |
939 |
} |
|
940 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
941 |
/** Sell a ship. |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
942 |
* @param x,y unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
943 |
* @param p1 vehicle ID to be sold |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
944 |
* @param p2 unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
945 |
*/ |
0 | 946 |
int32 CmdSellShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
947 |
{ |
|
948 |
Vehicle *v; |
|
949 |
||
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
|
950 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
951 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
952 |
v = GetVehicle(p1); |
0 | 953 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
954 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 955 |
|
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
|
956 |
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
|
957 |
|
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
|
958 |
if (!IsTileDepotType(v->tile, TRANSPORT_WATER) || v->u.road.state != 0x80 || !(v->vehstatus&VS_STOPPED)) |
0 | 959 |
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
|
960 |
|
0 | 961 |
if (flags & DC_EXEC) { |
962 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 963 |
RebuildVehicleLists(); |
0 | 964 |
InvalidateWindow(WC_COMPANY, v->owner); |
965 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
966 |
DeleteVehicle(v); |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
967 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Ship); // updates the replace Ship window |
0 | 968 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
969 |
|
0 | 970 |
return -(int32)v->value; |
971 |
} |
|
972 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
973 |
/** Start/Stop a ship. |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
974 |
* @param x,y unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
975 |
* @param p1 ship ID to start/stop |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
976 |
* @param p2 unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
977 |
*/ |
0 | 978 |
int32 CmdStartStopShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
979 |
{ |
|
980 |
Vehicle *v; |
|
981 |
||
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
|
982 |
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
|
983 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
984 |
v = GetVehicle(p1); |
0 | 985 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
986 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 987 |
|
988 |
if (flags & DC_EXEC) { |
|
989 |
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
|
990 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 991 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
992 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 993 |
} |
994 |
||
995 |
return 0; |
|
996 |
} |
|
997 |
||
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
998 |
/** Send a ship to the depot. |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
999 |
* @param x,y unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1000 |
* @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
|
1001 |
* @param p2 unused |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1002 |
*/ |
0 | 1003 |
int32 CmdSendShipToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1004 |
{ |
|
1005 |
Vehicle *v; |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1006 |
const Depot *dep; |
0 | 1007 |
|
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
|
1008 |
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
|
1009 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1010 |
v = GetVehicle(p1); |
0 | 1011 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1012 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1013 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1014 |
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
|
1015 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1016 |
/* If the current orders are already goto-depot */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1017 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 1018 |
if (flags & DC_EXEC) { |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1019 |
/* 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
|
1020 |
* 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
|
1021 |
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
|
1022 |
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
|
1023 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1024 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1025 |
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
|
1026 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1027 |
} |
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1028 |
return 0; |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1029 |
} |
0 | 1030 |
|
1793
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1031 |
dep = FindClosestShipDepot(v); |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1032 |
if (dep == NULL) |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1033 |
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
|
1034 |
|
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1035 |
if (flags & DC_EXEC) { |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1036 |
v->dest_tile = dep->xy; |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1037 |
v->current_order.type = OT_GOTO_DEPOT; |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1038 |
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
|
1039 |
v->current_order.station = dep->index; |
8ac8a8c9ec0f
(svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents:
1790
diff
changeset
|
1040 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1041 |
} |
1042 |
||
1043 |
return 0; |
|
1044 |
} |
|
1045 |
||
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
|
1046 |
/** 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
|
1047 |
* @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
|
1048 |
* @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
|
1049 |
* @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
|
1050 |
*/ |
0 | 1051 |
int32 CmdChangeShipServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1052 |
{ |
|
1053 |
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
|
1054 |
uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */ |
0 | 1055 |
|
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
|
1056 |
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
|
1057 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1058 |
v = GetVehicle(p1); |
0 | 1059 |
|
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
|
1060 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1061 |
|
1062 |
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
|
1063 |
v->service_interval = serv_int; |
0 | 1064 |
InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7); |
1065 |
} |
|
1066 |
||
1067 |
return 0; |
|
1068 |
} |
|
1069 |
||
1070 |
||
1071 |
// p1 = vehicle |
|
842 | 1072 |
// p2 = new cargo (0xFF) |
1073 |
// p2 = skip check for stopped in hanger (0x0100) |
|
0 | 1074 |
int32 CmdRefitShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1075 |
{ |
|
1076 |
Vehicle *v; |
|
1077 |
int32 cost; |
|
842 | 1078 |
byte SkipStoppedInDepotCheck = (p2 & 0x100) >> 8; //excludes the cargo value |
0 | 1079 |
|
842 | 1080 |
p2 = p2 & 0xFF; |
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
|
1081 |
|
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1082 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1083 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1084 |
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
|
1085 |
|
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1086 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) |
0 | 1087 |
return CMD_ERROR; |
1088 |
||
842 | 1089 |
if (!( SkipStoppedInDepotCheck )) { |
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
|
1090 |
if (!IsTileDepotType(v->tile, TRANSPORT_WATER) || |
842 | 1091 |
!(v->vehstatus&VS_STOPPED) || |
1092 |
v->u.ship.state != 0x80) |
|
1093 |
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); |
|
1094 |
} |
|
915 | 1095 |
|
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
|
1096 |
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
|
1097 |
|
0 | 1098 |
cost = 0; |
1099 |
if (IS_HUMAN_PLAYER(v->owner) && (byte)p2 != v->cargo_type) { |
|
1100 |
cost = _price.ship_base >> 7; |
|
1101 |
} |
|
1102 |
||
1103 |
if (flags & DC_EXEC) { |
|
842 | 1104 |
//autorefitted ships wants to keep the cargo |
1105 |
//it will be checked if the cargo is valid in CmdRenewVehicle |
|
1106 |
if (!(SkipStoppedInDepotCheck)) |
|
915 | 1107 |
v->cargo_count = 0; |
0 | 1108 |
v->cargo_type = (byte)p2; |
1109 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1110 |
} |
|
1111 |
||
1112 |
return cost; |
|
1113 |
||
1114 |
} |
|
1115 |
||
1116 |