author | truelight |
Thu, 24 Mar 2005 14:43:40 +0000 | |
changeset 1540 | 6eef12dab5bf |
parent 1530 | 2b46869fc0d4 |
child 1698 | 3f979f9ecf6c |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
507
04b5403aaf6b
(svn r815) Include strings.h only in the files which need it.
tron
parents:
445
diff
changeset
|
3 |
#include "table/strings.h" |
679
04ca2cd69420
(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
2e00193652b2
(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
cbe0c766c947
(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
f1013ec3d318
(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
24fdb517fbe5
(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
da81e0ef68cf
(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
24fdb517fbe5
(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
306bc86eb23e
(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); |
306bc86eb23e
(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); |
306bc86eb23e
(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]); |
306bc86eb23e
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
49 |
SetDParam(3, svi->capacity); |
306bc86eb23e
(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 |
||
54 |
int GetShipImage(Vehicle *v, byte direction) |
|
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
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
67 |
static Depot *FindClosestShipDepot(Vehicle *v) |
0 | 68 |
{ |
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
69 |
Depot *depot; |
f1013ec3d318
(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; |
|
76 |
byte trackdir = _track_direction_to_trackdir[FIND_FIRST_BIT(v->u.ship.state)][v->direction]; |
|
1330
5d76a0522a11
(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
f1013ec3d318
(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
f1013ec3d318
(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
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
83 |
FOR_ALL_DEPOTS(depot) { |
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
84 |
tile = depot->xy; |
1330
5d76a0522a11
(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
f1013ec3d318
(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
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
99 |
Depot *depot; |
0 | 100 |
|
1520
d88442095697
(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
d88442095697
(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
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
110 |
if (v->current_order.type == OT_GOTO_DEPOT && |
1530
2b46869fc0d4
(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
5e446b5b3ec5
(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
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
117 |
depot = FindClosestShipDepot(v); |
0 | 118 |
|
1313
f1013ec3d318
(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
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
120 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
121 |
v->current_order.type = OT_DUMMY; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
122 |
v->current_order.flags = 0; |
755
80091de50044
(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
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
128 |
v->current_order.type = OT_GOTO_DEPOT; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
129 |
v->current_order.flags = OF_NON_STOP; |
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
130 |
v->current_order.station = depot->index; |
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
131 |
v->dest_tile = depot->xy; |
755
80091de50044
(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
8d90844ddc2e
(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
24fdb517fbe5
(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
cc4f60cc9102
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
160 |
//we need this for the profit |
cc4f60cc9102
(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
06c115ce7b7a
(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
24fdb517fbe5
(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
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
205 |
static const TileIndexDiffC _dock_offs[] = { |
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
206 |
{ 2, 0}, |
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
207 |
{-2, 0}, |
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
208 |
{ 0, 2}, |
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
209 |
{ 2, 0}, |
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
210 |
{ 0, -2}, |
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
211 |
{ 0, 0}, |
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
212 |
{ 0, 0}, |
65cdb609b7a6
(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
5e446b5b3ec5
(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
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
220 |
if (v->current_order.type >= OT_GOTO_DEPOT && |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
221 |
v->current_order.type <= OT_LEAVESTATION) { |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
222 |
if (v->current_order.type != OT_GOTO_DEPOT || |
02df8a1b7f33
(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
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
227 |
if (v->current_order.type == OT_GOTO_DEPOT && |
02df8a1b7f33
(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
d88442095697
(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
5e446b5b3ec5
(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
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
239 |
if (order == NULL) { |
5e446b5b3ec5
(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
02df8a1b7f33
(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
5e446b5b3ec5
(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 && |
5e446b5b3ec5
(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 && |
5e446b5b3ec5
(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
5e446b5b3ec5
(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
5e446b5b3ec5
(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) { |
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
254 |
const Station *st; |
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
255 |
|
5e446b5b3ec5
(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
eccd576e322f
(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
5e446b5b3ec5
(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
65cdb609b7a6
(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
5e446b5b3ec5
(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
f1013ec3d318
(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
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
268 |
|
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
269 |
InvalidateVehicleOrder(v); |
1020
59ee8ceac74c
(svn r1521) -Fix: Ship Vehicle Lists are now redrawn correctly
celestar
parents:
926
diff
changeset
|
270 |
|
1055
cc4f60cc9102
(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
02df8a1b7f33
(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
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
279 |
if (v->current_order.type != OT_DUMMY) { |
02df8a1b7f33
(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
02df8a1b7f33
(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
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
297 |
Order b = v->current_order; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
298 |
v->current_order.type = OT_LEAVESTATION; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
299 |
v->current_order.flags = 0; |
02df8a1b7f33
(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
5e446b5b3ec5
(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
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
338 |
static const TileIndexDiffC _ship_leave_depot_offs[] = { |
65cdb609b7a6
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
339 |
{-1, 0}, |
65cdb609b7a6
(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
65cdb609b7a6
(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
65cdb609b7a6
(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
1e66514eb621
(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
cc4f60cc9102
(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
80091de50044
(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 |
||
406 |
||
812
65ecc321b3db
(svn r1283) -Add: AutoRenew is now a client-side patch instead of a game-side patch
truelight
parents:
755
diff
changeset
|
407 |
int32 EstimateShipCost(uint16 engine_type); |
0 | 408 |
|
409 |
static void ShipEnterDepot(Vehicle *v) |
|
410 |
{ |
|
411 |
v->u.ship.state = 0x80; |
|
412 |
v->vehstatus |= VS_HIDDEN; |
|
413 |
v->cur_speed = 0; |
|
414 |
RecalcShipStuff(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
415 |
|
578
1e66514eb621
(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
|
416 |
VehicleServiceInDepot(v); |
1e66514eb621
(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
|
417 |
|
0 | 418 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
419 |
||
842 | 420 |
MaybeReplaceVehicle(v); |
0 | 421 |
|
445
beafc0fb8f12
(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
|
422 |
TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT); |
beafc0fb8f12
(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
|
423 |
|
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
424 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
425 |
Order t; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
426 |
|
0 | 427 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
428 |
|
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
429 |
t = v->current_order; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
430 |
v->current_order.type = OT_DUMMY; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
431 |
v->current_order.flags = 0; |
0 | 432 |
|
1530
2b46869fc0d4
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
433 |
if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) { |
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
434 |
v->cur_order_index++; |
1530
2b46869fc0d4
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
435 |
} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) { |
0 | 436 |
v->vehstatus |= VS_STOPPED; |
437 |
if (v->owner == _local_player) { |
|
534
306bc86eb23e
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
438 |
SetDParam(0, v->unitnumber); |
0 | 439 |
AddNewsItem( |
440 |
STR_981C_SHIP_IS_WAITING_IN_DEPOT, |
|
441 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
442 |
v->index, |
|
443 |
0); |
|
444 |
} |
|
445 |
} |
|
446 |
} |
|
1055
cc4f60cc9102
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
447 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
0 | 448 |
} |
449 |
||
450 |
static void ShipArrivesAt(Vehicle *v, Station *st) |
|
451 |
{ |
|
452 |
/* Check if station was ever visited before */ |
|
453 |
if (!(st->had_vehicle_of_type & HVOT_SHIP)) { |
|
454 |
uint32 flags; |
|
455 |
st->had_vehicle_of_type |= HVOT_SHIP; |
|
534
306bc86eb23e
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
456 |
SetDParam(0, st->index); |
0 | 457 |
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); |
458 |
AddNewsItem( |
|
459 |
STR_9833_CITIZENS_CELEBRATE_FIRST, |
|
460 |
flags, |
|
461 |
v->index, |
|
462 |
0); |
|
463 |
} |
|
464 |
} |
|
465 |
||
466 |
typedef struct { |
|
467 |
uint skiptile; |
|
468 |
uint dest_coords; |
|
469 |
uint best_bird_dist; |
|
470 |
uint best_length; |
|
471 |
} PathFindShip; |
|
472 |
||
473 |
//extern void dbg_store_path(); |
|
474 |
||
475 |
static bool ShipTrackFollower(uint tile, PathFindShip *pfs, int track, uint length, byte *state) |
|
476 |
{ |
|
477 |
// Found dest? |
|
478 |
if (tile == pfs->dest_coords) { |
|
479 |
pfs->best_bird_dist = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
480 |
|
0 | 481 |
// if (length < pfs->best_length) |
482 |
// dbg_store_path(); |
|
483 |
||
484 |
pfs->best_length = minu(pfs->best_length, length); |
|
485 |
return true; |
|
486 |
} |
|
487 |
||
488 |
// Skip this tile in the calculation |
|
489 |
if (tile != pfs->skiptile) { |
|
1245
3822f77cbc53
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1237
diff
changeset
|
490 |
pfs->best_bird_dist = minu(pfs->best_bird_dist, DistanceMaxPlusManhattan(pfs->dest_coords, tile)); |
0 | 491 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
492 |
|
0 | 493 |
return false; |
494 |
} |
|
495 |
||
496 |
static const byte _ship_search_directions[6][4] = { |
|
497 |
{ 0, 9, 2, 9 }, |
|
498 |
{ 9, 1, 9, 3 }, |
|
499 |
{ 9, 0, 3, 9 }, |
|
500 |
{ 1, 9, 9, 2 }, |
|
501 |
{ 3, 2, 9, 9 }, |
|
502 |
{ 9, 9, 1, 0 }, |
|
503 |
}; |
|
504 |
||
505 |
static const byte _pick_shiptrack_table[6] = {1, 3, 2, 2, 0, 0}; |
|
506 |
||
507 |
static uint FindShipTrack(Vehicle *v, uint tile, int dir, uint bits, uint skiptile, int *track) |
|
508 |
{ |
|
509 |
PathFindShip pfs; |
|
510 |
int i, best_track; |
|
511 |
uint best_bird_dist = 0; |
|
512 |
uint best_length = 0; |
|
513 |
uint r; |
|
514 |
byte ship_dir = v->direction & 3; |
|
515 |
||
516 |
pfs.dest_coords = v->dest_tile; |
|
517 |
pfs.skiptile = skiptile; |
|
518 |
||
519 |
best_track = -1; |
|
520 |
||
521 |
do { |
|
522 |
i = FIND_FIRST_BIT(bits); |
|
523 |
bits = KILL_FIRST_BIT(bits); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
524 |
|
0 | 525 |
pfs.best_bird_dist = (uint)-1; |
526 |
pfs.best_length = (uint)-1; |
|
527 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
528 |
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
|
529 |
|
0 | 530 |
if (best_track >= 0) { |
531 |
if (pfs.best_bird_dist != 0) { |
|
532 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
|
533 |
if (pfs.best_bird_dist > best_bird_dist) goto bad; |
|
534 |
if (pfs.best_bird_dist < best_bird_dist) goto good; |
|
535 |
} else { |
|
536 |
if (pfs.best_length > best_length) goto bad; |
|
537 |
if (pfs.best_length < best_length) goto good; |
|
538 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
539 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
540 |
/* if we reach this position, there's two paths of equal value so far. |
0 | 541 |
* pick one randomly. */ |
542 |
r = (byte)Random(); |
|
543 |
if (_pick_shiptrack_table[i] == ship_dir) r += 80; |
|
544 |
if (_pick_shiptrack_table[best_track] == ship_dir) r -= 80; |
|
545 |
if (r <= 127) goto bad; |
|
546 |
} |
|
547 |
good:; |
|
548 |
best_track = i; |
|
549 |
best_bird_dist = pfs.best_bird_dist; |
|
550 |
best_length = pfs.best_length; |
|
551 |
bad:; |
|
552 |
||
553 |
} while (bits != 0); |
|
554 |
||
555 |
*track = best_track; |
|
556 |
return best_bird_dist; |
|
557 |
} |
|
558 |
||
1247 | 559 |
/* returns the track to choose on the next tile, or -1 when it's better to |
560 |
* reverse. The tile given is the tile we are about to enter, enterdir is the |
|
561 |
* direction in which we are entering the tile */ |
|
562 |
static int ChooseShipTrack(Vehicle *v, uint tile, int enterdir, uint tracks) |
|
0 | 563 |
{ |
1247 | 564 |
assert(enterdir>=0 && enterdir<=3); |
0 | 565 |
|
1247 | 566 |
if (_patches.new_pathfinding_all) { |
567 |
NPFFindStationOrTileData fstd; |
|
568 |
NPFFoundTargetData ftd; |
|
569 |
uint src_tile = TILE_ADD(tile, TileOffsByDir(_reverse_dir[enterdir])); |
|
570 |
byte track = FIND_FIRST_BIT(v->u.ship.state); |
|
571 |
assert (KILL_FIRST_BIT(v->u.ship.state) == 0); /* Check that only one bit is set in state */ |
|
572 |
assert (v->u.ship.state != 0x80); /* Check that we are not in a depot */ |
|
573 |
assert (track < 6); |
|
574 |
||
575 |
NPFFillWithOrderData(&fstd, v); |
|
576 |
||
1330
5d76a0522a11
(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
|
577 |
ftd = NPFRouteToStationOrTile(src_tile, _track_direction_to_trackdir[track][v->direction], &fstd, TRANSPORT_WATER, v->owner); |
1247 | 578 |
|
579 |
if (ftd.best_bird_dist == 0 && ftd.best_trackdir != 0xff) |
|
580 |
/* Found the target, and it is not our current tile */ |
|
581 |
return ftd.best_trackdir & 7; /* TODO: Wrapper function? */ |
|
582 |
else |
|
583 |
return -1; /* Couldn't find target, reverse */ |
|
584 |
/* TODO: When the target is unreachable, the ship will keep reversing */ |
|
585 |
} else { |
|
586 |
uint b; |
|
587 |
uint tot_dist, dist; |
|
588 |
int track; |
|
589 |
uint tile2; |
|
590 |
||
591 |
tile2 = TILE_ADD(tile, -TileOffsByDir(enterdir)); |
|
592 |
tot_dist = (uint)-1; |
|
593 |
||
594 |
/* Let's find out how far it would be if we would reverse first */ |
|
595 |
b = GetTileShipTrackStatus(tile2) & _ship_sometracks[_reverse_dir[enterdir]] & v->u.ship.state; |
|
596 |
if (b != 0) { |
|
597 |
dist = FindShipTrack(v, tile2, _reverse_dir[enterdir], b, tile, &track); |
|
598 |
if (dist != (uint)-1) |
|
599 |
tot_dist = dist + 1; |
|
600 |
} |
|
601 |
/* And if we would not reverse? */ |
|
602 |
dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track); |
|
603 |
if (dist > tot_dist) |
|
604 |
/* We could better reverse */ |
|
605 |
return -1; |
|
606 |
return track; |
|
0 | 607 |
} |
608 |
} |
|
609 |
||
610 |
static const byte _new_vehicle_direction_table[11] = { |
|
611 |
0, 7, 6, 0, |
|
612 |
1, 0, 5, 0, |
|
613 |
2, 3, 4, |
|
614 |
}; |
|
615 |
||
616 |
static int ShipGetNewDirectionFromTiles(uint new_tile, uint old_tile) |
|
617 |
{ |
|
926
a6d140a6a4de
(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
|
618 |
uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 + |
a6d140a6a4de
(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
|
619 |
TileX(new_tile) - TileX(old_tile) + 1; |
0 | 620 |
assert(offs < 11 && offs != 3 && offs != 7); |
621 |
return _new_vehicle_direction_table[offs]; |
|
622 |
} |
|
623 |
||
624 |
static int ShipGetNewDirection(Vehicle *v, int x, int y) |
|
625 |
{ |
|
626 |
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); |
|
627 |
assert(offs < 11 && offs != 3 && offs != 7); |
|
628 |
return _new_vehicle_direction_table[offs]; |
|
629 |
} |
|
630 |
||
631 |
static int GetAvailShipTracks(uint tile, int dir) |
|
632 |
{ |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
633 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); |
0 | 634 |
return (byte) ((r | r >> 8)) & _ship_sometracks[dir]; |
635 |
} |
|
636 |
||
637 |
static const byte _ship_subcoord[4][6][3] = { |
|
638 |
{ |
|
639 |
{15, 8, 1}, |
|
640 |
{ 0, 0, 0}, |
|
641 |
{ 0, 0, 0}, |
|
642 |
{15, 8, 2}, |
|
643 |
{15, 7, 0}, |
|
644 |
{ 0, 0, 0}, |
|
645 |
}, |
|
646 |
{ |
|
647 |
{ 0, 0, 0}, |
|
648 |
{ 8, 0, 3}, |
|
649 |
{ 7, 0, 2}, |
|
650 |
{ 0, 0, 0}, |
|
651 |
{ 8, 0, 4}, |
|
652 |
{ 0, 0, 0}, |
|
653 |
}, |
|
654 |
{ |
|
655 |
{ 0, 8, 5}, |
|
656 |
{ 0, 0, 0}, |
|
657 |
{ 0, 7, 6}, |
|
658 |
{ 0, 0, 0}, |
|
659 |
{ 0, 0, 0}, |
|
660 |
{ 0, 8, 4}, |
|
661 |
}, |
|
662 |
{ |
|
663 |
{ 0, 0, 0}, |
|
664 |
{ 8,15, 7}, |
|
665 |
{ 0, 0, 0}, |
|
666 |
{ 8,15, 6}, |
|
667 |
{ 0, 0, 0}, |
|
668 |
{ 7,15, 0}, |
|
669 |
} |
|
670 |
}; |
|
671 |
||
672 |
static void ShipController(Vehicle *v) |
|
673 |
{ |
|
674 |
GetNewVehiclePosResult gp; |
|
675 |
uint32 r; |
|
676 |
const byte *b; |
|
677 |
int dir,track,tracks; |
|
678 |
||
679 |
v->tick_counter++; |
|
680 |
||
681 |
if (v->breakdown_ctr != 0) { |
|
682 |
if (v->breakdown_ctr <= 2) { |
|
683 |
HandleBrokenShip(v); |
|
684 |
return; |
|
685 |
} |
|
686 |
v->breakdown_ctr--; |
|
687 |
} |
|
688 |
||
689 |
if (v->vehstatus & VS_STOPPED) |
|
690 |
return; |
|
691 |
||
692 |
ProcessShipOrder(v); |
|
693 |
HandleShipLoading(v); |
|
694 |
||
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
695 |
if (v->current_order.type == OT_LOADING) |
0 | 696 |
return; |
697 |
||
698 |
CheckShipLeaveDepot(v); |
|
699 |
||
700 |
if (!ShipAccelerate(v)) |
|
701 |
return; |
|
702 |
||
703 |
BeginVehicleMove(v); |
|
704 |
||
705 |
if (GetNewVehiclePos(v, &gp)) { |
|
706 |
// staying in tile |
|
707 |
if (v->u.ship.state == 0x80) { |
|
708 |
gp.x = v->x_pos; |
|
709 |
gp.y = v->y_pos; |
|
710 |
} else { |
|
711 |
/* isnot inside depot */ |
|
712 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
713 |
if (r & 0x8) goto reverse_direction; |
|
714 |
||
715 |
if (v->dest_tile != 0 && v->dest_tile == gp.new_tile) { |
|
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
716 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 717 |
if ((gp.x&0xF)==8 && (gp.y&0xF)==8) { |
718 |
ShipEnterDepot(v); |
|
719 |
return; |
|
720 |
} |
|
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
721 |
} else if (v->current_order.type == OT_GOTO_STATION) { |
0 | 722 |
Station *st; |
723 |
||
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
724 |
v->last_station_visited = v->current_order.station; |
0 | 725 |
|
1024
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
726 |
/* Process station in the orderlist. Don't do that for buoys (HVOT_BUOY) */ |
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
727 |
st = GetStation(v->current_order.station); |
812
65ecc321b3db
(svn r1283) -Add: AutoRenew is now a client-side patch instead of a game-side patch
truelight
parents:
755
diff
changeset
|
728 |
if (!(st->had_vehicle_of_type & HVOT_BUOY) |
297
6c4ab6ff031f
(svn r303) Fix: [ 1022227 ] Ships could unload cargo at stations without docks
dominik
parents:
193
diff
changeset
|
729 |
&& (st->facilities & FACIL_DOCK)) { /* ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations */ |
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
730 |
v->current_order.type = OT_LOADING; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
731 |
v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
732 |
v->current_order.flags |= OF_NON_STOP; |
0 | 733 |
ShipArrivesAt(v, st); |
734 |
||
735 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); |
|
736 |
if (LoadUnloadVehicle(v)) { |
|
737 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
|
738 |
MarkShipDirty(v); |
|
739 |
} |
|
755
80091de50044
(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
|
740 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
297
6c4ab6ff031f
(svn r303) Fix: [ 1022227 ] Ships could unload cargo at stations without docks
dominik
parents:
193
diff
changeset
|
741 |
} else { /* leave buoys right aways */ |
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
742 |
v->current_order.type = OT_LEAVESTATION; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
743 |
v->current_order.flags = 0; |
0 | 744 |
v->cur_order_index++; |
1024
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
745 |
InvalidateVehicleOrder(v); |
0 | 746 |
} |
747 |
goto else_end; |
|
748 |
} |
|
749 |
} |
|
750 |
||
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
751 |
if (v->current_order.type == OT_LEAVESTATION) { |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
752 |
v->current_order.type = OT_NOTHING; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
753 |
v->current_order.flags = 0; |
755
80091de50044
(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
|
754 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 755 |
} |
756 |
} |
|
757 |
} else { |
|
758 |
// new tile |
|
1401
44a7e0f04d28
(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
|
759 |
if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) |
926
a6d140a6a4de
(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
|
760 |
goto reverse_direction; |
0 | 761 |
|
762 |
dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile); |
|
763 |
assert(dir == 1 || dir == 3 || dir == 5 || dir == 7); |
|
764 |
dir>>=1; |
|
765 |
tracks = GetAvailShipTracks(gp.new_tile, dir); |
|
766 |
if (tracks == 0) |
|
767 |
goto reverse_direction; |
|
768 |
||
769 |
// Choose a direction, and continue if we find one |
|
770 |
track = ChooseShipTrack(v, gp.new_tile, dir, tracks); |
|
771 |
if (track < 0) |
|
772 |
goto reverse_direction; |
|
773 |
||
774 |
b = _ship_subcoord[dir][track]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
775 |
|
0 | 776 |
gp.x = (gp.x&~0xF) | b[0]; |
777 |
gp.y = (gp.y&~0xF) | b[1]; |
|
778 |
||
779 |
/* Call the landscape function and tell it that the vehicle entered the tile */ |
|
780 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
781 |
if (r&0x8) goto reverse_direction; |
|
782 |
||
783 |
if (!(r&0x4)) { |
|
784 |
v->tile = gp.new_tile; |
|
785 |
v->u.ship.state = 1 << track; |
|
786 |
} |
|
787 |
||
788 |
v->direction = b[2]; |
|
789 |
} |
|
790 |
else_end:; |
|
791 |
||
792 |
/* update image of ship, as well as delta XY */ |
|
793 |
dir = ShipGetNewDirection(v, gp.x, gp.y); |
|
794 |
v->x_pos = gp.x; |
|
795 |
v->y_pos = gp.y; |
|
796 |
v->z_pos = GetSlopeZ(gp.x, gp.y); |
|
797 |
||
798 |
getout: |
|
799 |
UpdateShipDeltaXY(v, dir); |
|
800 |
v->cur_image = GetShipImage(v, dir); |
|
801 |
VehiclePositionChanged(v); |
|
802 |
EndVehicleMove(v); |
|
803 |
return; |
|
804 |
||
805 |
reverse_direction: |
|
806 |
dir = v->direction ^ 4; |
|
807 |
v->direction = dir; |
|
808 |
goto getout; |
|
809 |
} |
|
810 |
||
811 |
static void AgeShipCargo(Vehicle *v) |
|
812 |
{ |
|
813 |
if (_age_cargo_skip_counter != 0) |
|
814 |
return; |
|
815 |
if (v->cargo_days != 255) |
|
816 |
v->cargo_days++; |
|
817 |
} |
|
818 |
||
819 |
void Ship_Tick(Vehicle *v) |
|
820 |
{ |
|
821 |
AgeShipCargo(v); |
|
822 |
ShipController(v); |
|
823 |
} |
|
824 |
||
825 |
void HandleClickOnShip(Vehicle *v) |
|
826 |
{ |
|
827 |
ShowShipViewWindow(v); |
|
828 |
} |
|
829 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1055
diff
changeset
|
830 |
void ShipsYearlyLoop(void) |
0 | 831 |
{ |
832 |
Vehicle *v; |
|
833 |
||
834 |
FOR_ALL_VEHICLES(v) { |
|
835 |
if (v->type == VEH_Ship) { |
|
836 |
v->profit_last_year = v->profit_this_year; |
|
837 |
v->profit_this_year = 0; |
|
838 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
839 |
} |
|
840 |
} |
|
841 |
} |
|
842 |
||
812
65ecc321b3db
(svn r1283) -Add: AutoRenew is now a client-side patch instead of a game-side patch
truelight
parents:
755
diff
changeset
|
843 |
int32 EstimateShipCost(uint16 engine_type) |
0 | 844 |
{ |
538
24fdb517fbe5
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
845 |
return ShipVehInfo(engine_type)->base_cost * (_price.ship_base>>3)>>5; |
0 | 846 |
} |
847 |
||
848 |
// p1 = type to build |
|
849 |
int32 CmdBuildShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
850 |
{ |
|
851 |
int32 value; |
|
852 |
Vehicle *v; |
|
1282
ea2ae881814c
(svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents:
1266
diff
changeset
|
853 |
UnitID unit_num; |
1330
5d76a0522a11
(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
|
854 |
TileIndex tile = TILE_FROM_XY(x,y); |
0 | 855 |
Engine *e; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
856 |
|
1197 | 857 |
if (!IsEngineBuildable(p1, VEH_Ship)) return CMD_ERROR; |
1196
67f7f3017d99
(svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents:
1151
diff
changeset
|
858 |
|
0 | 859 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
860 |
||
861 |
value = EstimateShipCost(p1); |
|
862 |
if (flags & DC_QUERY_COST) |
|
863 |
return value; |
|
864 |
||
1443
2b8e1b123d91
(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
|
865 |
/* The ai_new queries the vehicle cost before building the route, |
2b8e1b123d91
(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
|
866 |
* so we must check against cheaters no sooner than now. --pasky */ |
2b8e1b123d91
(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
|
867 |
if (!IsTileDepotType(tile, TRANSPORT_WATER)) return CMD_ERROR; |
2b8e1b123d91
(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
|
868 |
if (_map_owner[tile] != _current_player) return CMD_ERROR; |
2b8e1b123d91
(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
|
869 |
|
0 | 870 |
v = AllocateVehicle(); |
1024
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
1020
diff
changeset
|
871 |
if (v == NULL || IsOrderPoolFull() || |
0 | 872 |
(unit_num = GetFreeUnitNumber(VEH_Ship)) > _patches.max_ships) |
873 |
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
|
874 |
|
0 | 875 |
if (flags & DC_EXEC) { |
538
24fdb517fbe5
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
876 |
const ShipVehicleInfo *svi = ShipVehInfo(p1); |
24fdb517fbe5
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
877 |
|
0 | 878 |
v->unitnumber = unit_num; |
879 |
||
880 |
v->owner = _current_player; |
|
881 |
v->tile = tile; |
|
926
a6d140a6a4de
(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
|
882 |
x = TileX(tile) * 16 + 8; |
a6d140a6a4de
(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
|
883 |
y = TileY(tile) * 16 + 8; |
0 | 884 |
v->x_pos = x; |
885 |
v->y_pos = y; |
|
886 |
v->z_pos = GetSlopeZ(x,y); |
|
887 |
||
888 |
v->z_height = 6; |
|
889 |
v->sprite_width = 6; |
|
890 |
v->sprite_height = 6; |
|
891 |
v->x_offs = -3; |
|
892 |
v->y_offs = -3; |
|
893 |
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
|
894 |
|
538
24fdb517fbe5
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
895 |
v->spritenum = svi->image_index; |
24fdb517fbe5
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
896 |
v->cargo_type = svi->cargo_type; |
24fdb517fbe5
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
897 |
v->cargo_cap = svi->capacity; |
0 | 898 |
v->value = value; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
899 |
|
1266
eccd576e322f
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1247
diff
changeset
|
900 |
v->last_station_visited = INVALID_STATION; |
538
24fdb517fbe5
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
901 |
v->max_speed = svi->max_speed; |
0 | 902 |
v->engine_type = (byte)p1; |
903 |
||
904 |
e = &_engines[p1]; |
|
905 |
v->reliability = e->reliability; |
|
906 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
907 |
v->max_age = e->lifelength * 366; |
|
908 |
_new_ship_id = v->index; |
|
909 |
||
910 |
v->string_id = STR_SV_SHIP_NAME; |
|
911 |
v->u.ship.state = 0x80; |
|
912 |
||
913 |
v->service_interval = _patches.servint_ships; |
|
914 |
v->date_of_last_service = _date; |
|
915 |
v->build_year = _cur_year; |
|
916 |
v->cur_image = 0x0E5E; |
|
917 |
v->type = VEH_Ship; |
|
918 |
||
919 |
VehiclePositionChanged(v); |
|
920 |
||
921 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 922 |
RebuildVehicleLists(); |
0 | 923 |
InvalidateWindow(WC_COMPANY, v->owner); |
924 |
} |
|
1128
ca7f860db7ac
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1093
diff
changeset
|
925 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Ship); // updates the replace Ship window |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
926 |
|
0 | 927 |
return value; |
928 |
} |
|
929 |
||
930 |
int32 CmdSellShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
931 |
{ |
|
932 |
Vehicle *v; |
|
933 |
||
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
934 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
935 |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
936 |
v = GetVehicle(p1); |
0 | 937 |
|
1235
b9938f523286
(svn r1739) - Fix: type checking when selling vehicles (TrueLight)
darkvater
parents:
1226
diff
changeset
|
938 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) |
0 | 939 |
return CMD_ERROR; |
940 |
||
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
941 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
942 |
|
1330
5d76a0522a11
(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
|
943 |
if (!IsTileDepotType(v->tile, TRANSPORT_WATER) || v->u.road.state != 0x80 || !(v->vehstatus&VS_STOPPED)) |
0 | 944 |
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
|
945 |
|
0 | 946 |
if (flags & DC_EXEC) { |
947 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 948 |
RebuildVehicleLists(); |
0 | 949 |
InvalidateWindow(WC_COMPANY, v->owner); |
950 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
951 |
DeleteVehicle(v); |
|
952 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
953 |
|
1128
ca7f860db7ac
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1093
diff
changeset
|
954 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Ship); // updates the replace Ship window |
ca7f860db7ac
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1093
diff
changeset
|
955 |
|
0 | 956 |
return -(int32)v->value; |
957 |
} |
|
958 |
||
959 |
// p1 = vehicle |
|
960 |
int32 CmdStartStopShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
961 |
{ |
|
962 |
Vehicle *v; |
|
963 |
||
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
964 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
965 |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
966 |
v = GetVehicle(p1); |
0 | 967 |
|
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
968 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) |
0 | 969 |
return CMD_ERROR; |
970 |
||
971 |
if (flags & DC_EXEC) { |
|
972 |
v->vehstatus ^= VS_STOPPED; |
|
755
80091de50044
(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
|
973 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 974 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
975 |
} |
|
976 |
||
1055
cc4f60cc9102
(svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents:
1053
diff
changeset
|
977 |
InvalidateWindowClasses(WC_SHIPS_LIST); |
1020
59ee8ceac74c
(svn r1521) -Fix: Ship Vehicle Lists are now redrawn correctly
celestar
parents:
926
diff
changeset
|
978 |
|
0 | 979 |
return 0; |
980 |
} |
|
981 |
||
982 |
int32 CmdSendShipToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
983 |
{ |
|
984 |
Vehicle *v; |
|
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
985 |
Depot *depot; |
0 | 986 |
|
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
987 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
988 |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
989 |
v = GetVehicle(p1); |
0 | 990 |
|
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
991 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) |
0 | 992 |
return CMD_ERROR; |
993 |
||
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
994 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 995 |
if (flags & DC_EXEC) { |
1530
2b46869fc0d4
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
996 |
|
2b46869fc0d4
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
997 |
if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) |
2b46869fc0d4
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
998 |
v->cur_order_index++; |
2b46869fc0d4
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
999 |
|
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1000 |
v->current_order.type = OT_DUMMY; |
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1001 |
v->current_order.flags = 0; |
755
80091de50044
(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
|
1002 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1003 |
} |
1004 |
} else { |
|
1005 |
depot = FindClosestShipDepot(v); |
|
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
1006 |
if (depot == NULL) |
0 | 1007 |
return_cmd_error(STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT); |
1008 |
||
1009 |
if (flags & DC_EXEC) { |
|
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
1010 |
v->dest_tile = depot->xy; |
555
02df8a1b7f33
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1011 |
v->current_order.type = OT_GOTO_DEPOT; |
1530
2b46869fc0d4
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
1012 |
v->current_order.flags = OF_NON_STOP | OF_HALT_IN_DEPOT; |
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
1013 |
v->current_order.station = depot->index; |
755
80091de50044
(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
|
1014 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1015 |
} |
1016 |
} |
|
1017 |
||
1018 |
return 0; |
|
1019 |
} |
|
1020 |
||
1021 |
int32 CmdChangeShipServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
1022 |
{ |
|
1023 |
Vehicle *v; |
|
1024 |
||
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1025 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1026 |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1027 |
v = GetVehicle(p1); |
0 | 1028 |
|
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1029 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) |
0 | 1030 |
return CMD_ERROR; |
1031 |
||
1032 |
if (flags & DC_EXEC) { |
|
1033 |
v->service_interval = (uint16)p2; |
|
1034 |
InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7); |
|
1035 |
} |
|
1036 |
||
1037 |
return 0; |
|
1038 |
} |
|
1039 |
||
1040 |
||
1041 |
// p1 = vehicle |
|
842 | 1042 |
// p2 = new cargo (0xFF) |
1043 |
// p2 = skip check for stopped in hanger (0x0100) |
|
0 | 1044 |
int32 CmdRefitShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1045 |
{ |
|
1046 |
Vehicle *v; |
|
1047 |
int32 cost; |
|
842 | 1048 |
byte SkipStoppedInDepotCheck = (p2 & 0x100) >> 8; //excludes the cargo value |
0 | 1049 |
|
842 | 1050 |
p2 = p2 & 0xFF; |
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1051 |
|
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1052 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1053 |
|
919
544f374ee392
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1054 |
v = GetVehicle(p1); |
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1055 |
|
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1056 |
if (v->type != VEH_Ship || !CheckOwnership(v->owner)) |
0 | 1057 |
return CMD_ERROR; |
1058 |
||
842 | 1059 |
if (!( SkipStoppedInDepotCheck )) { |
1330
5d76a0522a11
(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
|
1060 |
if (!IsTileDepotType(v->tile, TRANSPORT_WATER) || |
842 | 1061 |
!(v->vehstatus&VS_STOPPED) || |
1062 |
v->u.ship.state != 0x80) |
|
1063 |
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); |
|
1064 |
} |
|
915 | 1065 |
|
1237
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1066 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN); |
1c7a3f9c94b9
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1235
diff
changeset
|
1067 |
|
0 | 1068 |
cost = 0; |
1069 |
if (IS_HUMAN_PLAYER(v->owner) && (byte)p2 != v->cargo_type) { |
|
1070 |
cost = _price.ship_base >> 7; |
|
1071 |
} |
|
1072 |
||
1073 |
if (flags & DC_EXEC) { |
|
842 | 1074 |
//autorefitted ships wants to keep the cargo |
1075 |
//it will be checked if the cargo is valid in CmdRenewVehicle |
|
1076 |
if (!(SkipStoppedInDepotCheck)) |
|
915 | 1077 |
v->cargo_count = 0; |
0 | 1078 |
v->cargo_type = (byte)p2; |
1079 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1080 |
} |
|
1081 |
||
1082 |
return cost; |
|
1083 |
||
1084 |
} |
|
1085 |
||
1086 |