author | tron |
Thu, 06 Jan 2005 18:45:28 +0000 | |
changeset 915 | 013cb2d74800 |
parent 909 | 81bc9ef93f50 |
child 919 | b0d6c7642f99 |
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" |
0 | 5 |
#include "vehicle.h" |
6 |
#include "command.h" |
|
7 |
#include "pathfind.h" |
|
8 |
#include "station.h" |
|
9 |
#include "gfx.h" |
|
10 |
#include "news.h" |
|
11 |
#include "engine.h" |
|
12 |
#include "gui.h" |
|
13 |
#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
|
14 |
#include "sound.h" |
0 | 15 |
|
16 |
static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D}; |
|
17 |
static const byte _ship_sometracks[4] = {0x19, 0x16, 0x25, 0x2A}; |
|
18 |
||
19 |
static byte GetTileShipTrackStatus(uint tile) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
20 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); |
0 | 21 |
return r | r >> 8; |
22 |
} |
|
23 |
||
24 |
void DrawShipEngine(int x, int y, int engine, uint32 image_ormod) |
|
25 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
26 |
int spritenum = ShipVehInfo(engine)->image_index; |
0 | 27 |
|
28 |
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
|
29 |
int sprite = GetCustomVehicleIcon(engine, 6); |
0 | 30 |
|
31 |
if (sprite) { |
|
32 |
DrawSprite(sprite | image_ormod, x, y); |
|
33 |
return; |
|
34 |
} |
|
35 |
spritenum = _engine_original_sprites[engine]; |
|
36 |
} |
|
37 |
DrawSprite((6 + _ship_sprites[spritenum]) | image_ormod, x, y); |
|
38 |
} |
|
39 |
||
40 |
void DrawShipEngineInfo(int engine, int x, int y, int maxw) |
|
41 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
SetDParam(3, svi->capacity); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
47 |
SetDParam(4, svi->running_cost * _price.ship_running >> 8); |
0 | 48 |
DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw); |
49 |
} |
|
50 |
||
51 |
int GetShipImage(Vehicle *v, byte direction) |
|
52 |
{ |
|
53 |
int spritenum = v->spritenum; |
|
54 |
||
55 |
if (is_custom_sprite(spritenum)) { |
|
56 |
int sprite = GetCustomVehicleSprite(v, direction); |
|
57 |
||
58 |
if (sprite) return sprite; |
|
59 |
spritenum = _engine_original_sprites[v->engine_type]; |
|
60 |
} |
|
61 |
return _ship_sprites[spritenum] + direction; |
|
62 |
} |
|
63 |
||
64 |
static int FindClosestShipDepot(Vehicle *v) |
|
65 |
{ |
|
66 |
uint tile, dist, best_dist = (uint)-1; |
|
67 |
int best_depot = -1; |
|
68 |
int i; |
|
69 |
byte owner = v->owner; |
|
70 |
uint tile2 = v->tile; |
|
71 |
||
72 |
for(i=0; i!=lengthof(_depots); i++) { |
|
73 |
tile = _depots[i].xy; |
|
74 |
if (IS_TILETYPE(tile, MP_WATER) && _map_owner[tile] == owner) { |
|
75 |
dist = GetTileDist(tile, tile2); |
|
76 |
if (dist < best_dist) { |
|
77 |
best_dist = dist; |
|
78 |
best_depot = i; |
|
79 |
} |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
return best_depot; |
|
84 |
} |
|
85 |
||
86 |
static void CheckIfShipNeedsService(Vehicle *v) |
|
87 |
{ |
|
88 |
int i; |
|
89 |
||
76
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
19
diff
changeset
|
90 |
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
|
91 |
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
|
92 |
|
593 | 93 |
if (!VehicleNeedsService(v)) |
0 | 94 |
return; |
95 |
||
96 |
if (v->vehstatus & VS_STOPPED) |
|
97 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
98 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
99 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
100 |
v->current_order.flags & OF_FULL_LOAD) |
0 | 101 |
return; |
102 |
||
76
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
19
diff
changeset
|
103 |
if (_patches.gotodepot && ScheduleHasDepotOrders(v->schedule_ptr)) |
0 | 104 |
return; |
105 |
||
106 |
i = FindClosestShipDepot(v); |
|
107 |
||
108 |
if (i < 0 || GetTileDist(v->tile, (&_depots[i])->xy) > 12) { |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
109 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
110 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
111 |
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
|
112 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 113 |
} |
114 |
return; |
|
115 |
} |
|
116 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
117 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
118 |
v->current_order.flags = OF_NON_STOP; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
119 |
v->current_order.station = (byte)i; |
0 | 120 |
v->dest_tile = (&_depots[i])->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
|
121 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 122 |
} |
123 |
||
124 |
void OnNewDay_Ship(Vehicle *v) |
|
125 |
{ |
|
126 |
int32 cost; |
|
127 |
||
128 |
if ((++v->day_counter & 7) == 0) |
|
129 |
DecreaseVehicleValue(v); |
|
130 |
||
131 |
CheckVehicleBreakdown(v); |
|
132 |
AgeVehicle(v); |
|
133 |
CheckIfShipNeedsService(v); |
|
134 |
||
19
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
135 |
CheckOrders(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
136 |
|
0 | 137 |
if (v->vehstatus & VS_STOPPED) |
138 |
return; |
|
139 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
140 |
|
19
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
141 |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
142 |
cost = ShipVehInfo(v->engine_type)->running_cost * _price.ship_running / 364; |
0 | 143 |
v->profit_this_year -= cost >> 8; |
144 |
||
145 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN); |
|
146 |
SubtractMoneyFromPlayerFract(v->owner, cost); |
|
147 |
||
148 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
149 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
|
150 |
} |
|
151 |
||
152 |
static void HandleBrokenShip(Vehicle *v) |
|
153 |
{ |
|
154 |
if (v->breakdown_ctr != 1) { |
|
155 |
v->breakdown_ctr = 1; |
|
156 |
v->cur_speed = 0; |
|
157 |
||
158 |
if (v->breakdowns_since_last_service != 255) |
|
159 |
v->breakdowns_since_last_service++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
160 |
|
0 | 161 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
162 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
163 |
|
541 | 164 |
SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? |
165 |
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v); |
|
0 | 166 |
|
167 |
if (!(v->vehstatus & VS_HIDDEN)) { |
|
168 |
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); |
|
169 |
if (u) |
|
170 |
u->u.special.unk0 = v->breakdown_delay * 2; |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
if (!(v->tick_counter & 1)) { |
|
175 |
if (!--v->breakdown_delay) { |
|
176 |
v->breakdown_ctr = 0; |
|
177 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
static void MarkShipDirty(Vehicle *v) |
|
183 |
{ |
|
184 |
v->cur_image = GetShipImage(v, v->direction); |
|
185 |
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); |
|
186 |
} |
|
187 |
||
188 |
static void PlayShipSound(Vehicle *v) |
|
189 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
190 |
SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v); |
0 | 191 |
} |
192 |
||
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
193 |
static const TileIndexDiffC _dock_offs[] = { |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
194 |
{ 2, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
195 |
{-2, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
196 |
{ 0, 2}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
197 |
{ 2, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
198 |
{ 0, -2}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
199 |
{ 0, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
200 |
{ 0, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
201 |
{ 0, 0} |
0 | 202 |
}; |
203 |
||
204 |
static void ProcessShipOrder(Vehicle *v) |
|
205 |
{ |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
206 |
Order order; |
0 | 207 |
Station *st; |
208 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
209 |
if (v->current_order.type >= OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
210 |
v->current_order.type <= OT_LEAVESTATION) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
211 |
if (v->current_order.type != OT_GOTO_DEPOT || |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
212 |
!(v->current_order.flags & OF_UNLOAD)) |
0 | 213 |
return; |
214 |
} |
|
215 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
216 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
217 |
(v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) && |
593 | 218 |
!VehicleNeedsService(v)) { |
0 | 219 |
v->cur_order_index++; |
220 |
} |
|
221 |
||
222 |
||
223 |
if (v->cur_order_index >= v->num_orders) |
|
224 |
v->cur_order_index = 0; |
|
225 |
||
226 |
order = v->schedule_ptr[v->cur_order_index]; |
|
227 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
228 |
if (order.type == OT_NOTHING) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
229 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
230 |
v->current_order.flags = 0; |
0 | 231 |
v->dest_tile = 0; |
232 |
return; |
|
233 |
} |
|
234 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
235 |
if (order.type == v->current_order.type && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
236 |
order.flags == v->current_order.flags && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
237 |
order.station == v->current_order.station) |
0 | 238 |
return; |
239 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
240 |
v->current_order = order; |
0 | 241 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
242 |
if (order.type == OT_GOTO_STATION) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
243 |
if (order.station == v->last_station_visited) |
817
4f9377b7fd2b
(svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents:
812
diff
changeset
|
244 |
v->last_station_visited = 0xFFFF; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
245 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
246 |
st = DEREF_STATION(order.station); |
0 | 247 |
if (st->dock_tile != 0) { |
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
248 |
v->dest_tile = TILE_ADD(st->dock_tile, ToTileIndexDiff(_dock_offs[_map5[st->dock_tile]-0x4B])); |
0 | 249 |
} |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
250 |
} else if (order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
251 |
v->dest_tile = _depots[order.station].xy; |
0 | 252 |
} else { |
253 |
v->dest_tile = 0; |
|
254 |
} |
|
255 |
InvalidateVehicleOrderWidget(v); |
|
256 |
} |
|
257 |
||
258 |
static void HandleShipLoading(Vehicle *v) |
|
259 |
{ |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
260 |
if (v->current_order.type == OT_NOTHING) |
0 | 261 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
262 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
263 |
if (v->current_order.type != OT_DUMMY) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
264 |
if (v->current_order.type != OT_LOADING) |
0 | 265 |
return; |
266 |
||
267 |
if (--v->load_unload_time_rem) |
|
268 |
return; |
|
269 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
270 |
if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) { |
0 | 271 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); |
272 |
if (LoadUnloadVehicle(v)) { |
|
273 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
|
274 |
MarkShipDirty(v); |
|
275 |
} |
|
276 |
return; |
|
277 |
} |
|
278 |
PlayShipSound(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
279 |
|
0 | 280 |
{ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
281 |
Order b = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
282 |
v->current_order.type = OT_LEAVESTATION; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
283 |
v->current_order.flags = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
284 |
if (!(b.flags & OF_NON_STOP)) |
0 | 285 |
return; |
286 |
} |
|
287 |
} |
|
288 |
||
289 |
v->cur_order_index++; |
|
290 |
InvalidateVehicleOrderWidget(v); |
|
291 |
} |
|
292 |
||
293 |
static void UpdateShipDeltaXY(Vehicle *v, int dir) |
|
294 |
{ |
|
295 |
#define MKIT(d,c,b,a) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0) |
|
296 |
static const uint32 _delta_xy_table[8] = { |
|
297 |
MKIT( -3, -3, 6, 6), |
|
298 |
MKIT(-16, -3, 32, 6), |
|
299 |
MKIT( -3, -3, 6, 6), |
|
300 |
MKIT( -3,-16, 6, 32), |
|
301 |
MKIT( -3, -3, 6, 6), |
|
302 |
MKIT(-16, -3, 32, 6), |
|
303 |
MKIT( -3, -3, 6, 6), |
|
304 |
MKIT( -3,-16, 6, 32), |
|
305 |
}; |
|
306 |
#undef MKIT |
|
307 |
uint32 x = _delta_xy_table[dir]; |
|
308 |
v->x_offs = (byte)x; |
|
309 |
v->y_offs = (byte)(x>>=8); |
|
310 |
v->sprite_width = (byte)(x>>=8); |
|
311 |
v->sprite_height = (byte)(x>>=8); |
|
312 |
} |
|
313 |
||
314 |
static void RecalcShipStuff(Vehicle *v) |
|
315 |
{ |
|
316 |
UpdateShipDeltaXY(v, v->direction); |
|
317 |
v->cur_image = GetShipImage(v, v->direction); |
|
318 |
MarkShipDirty(v); |
|
319 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
320 |
} |
|
321 |
||
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
322 |
static const TileIndexDiffC _ship_leave_depot_offs[] = { |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
323 |
{-1, 0}, |
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
324 |
{ 0, -1} |
0 | 325 |
}; |
326 |
||
327 |
static void CheckShipLeaveDepot(Vehicle *v) |
|
328 |
{ |
|
329 |
uint tile; |
|
330 |
int d; |
|
331 |
uint m; |
|
332 |
||
333 |
if (v->u.ship.state != 0x80) |
|
334 |
return; |
|
335 |
||
336 |
tile = v->tile; |
|
337 |
d = (_map5[tile]&2) ? 1 : 0; |
|
338 |
||
339 |
// Check first side |
|
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
340 |
if (_ship_sometracks[d] & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[d])))) { |
0 | 341 |
m = (d==0) ? 0x101 : 0x207; |
342 |
// Check second side |
|
909
81bc9ef93f50
(svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents:
900
diff
changeset
|
343 |
} else if (_ship_sometracks[d+2] & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[d])))) { |
0 | 344 |
m = (d==0) ? 0x105 : 0x203; |
345 |
} else { |
|
346 |
return; |
|
347 |
} |
|
348 |
v->direction = (byte)m; |
|
349 |
v->u.ship.state = (byte)(m >> 8); |
|
350 |
v->vehstatus &= ~VS_HIDDEN; |
|
351 |
||
352 |
v->cur_speed = 0; |
|
353 |
RecalcShipStuff(v); |
|
354 |
||
355 |
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
|
356 |
VehicleServiceInDepot(v); |
0 | 357 |
} |
358 |
||
359 |
static bool ShipAccelerate(Vehicle *v) |
|
360 |
{ |
|
361 |
uint spd; |
|
362 |
byte t; |
|
363 |
||
364 |
spd = min(v->cur_speed + 1, v->max_speed); |
|
365 |
||
366 |
//updates statusbar only if speed have changed to save CPU time |
|
367 |
if (spd != v->cur_speed) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
368 |
v->cur_speed = spd; |
0 | 369 |
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
|
370 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 371 |
} |
372 |
||
373 |
// Decrease somewhat when turning |
|
374 |
if (!(v->direction&1)) { |
|
375 |
spd = spd * 3 >> 2; |
|
376 |
} |
|
377 |
||
378 |
if (spd == 0) |
|
379 |
return false; |
|
380 |
||
381 |
if ((byte)++spd == 0) |
|
382 |
return true; |
|
383 |
||
384 |
v->progress = (t = v->progress) - (byte)spd; |
|
385 |
||
386 |
return (t < v->progress); |
|
387 |
} |
|
388 |
||
389 |
||
812
79c99885ad00
(svn r1283) -Add: AutoRenew is now a client-side patch instead of a game-side patch
truelight
parents:
755
diff
changeset
|
390 |
int32 EstimateShipCost(uint16 engine_type); |
0 | 391 |
|
392 |
static void ShipEnterDepot(Vehicle *v) |
|
393 |
{ |
|
394 |
v->u.ship.state = 0x80; |
|
395 |
v->vehstatus |= VS_HIDDEN; |
|
396 |
v->cur_speed = 0; |
|
397 |
RecalcShipStuff(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
398 |
|
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
|
399 |
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
|
400 |
|
0 | 401 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
402 |
||
842 | 403 |
MaybeReplaceVehicle(v); |
0 | 404 |
|
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
|
405 |
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
|
406 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
407 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
408 |
Order t; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
409 |
|
0 | 410 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
411 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
412 |
t = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
413 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
414 |
v->current_order.flags = 0; |
0 | 415 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
416 |
if (t.flags & OF_UNLOAD) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
417 |
v->cur_order_index++; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
418 |
} else if (t.flags & OF_FULL_LOAD) { |
0 | 419 |
v->vehstatus |= VS_STOPPED; |
420 |
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
|
421 |
SetDParam(0, v->unitnumber); |
0 | 422 |
AddNewsItem( |
423 |
STR_981C_SHIP_IS_WAITING_IN_DEPOT, |
|
424 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
425 |
v->index, |
|
426 |
0); |
|
427 |
} |
|
428 |
} |
|
429 |
} |
|
430 |
} |
|
431 |
||
432 |
static void ShipArrivesAt(Vehicle *v, Station *st) |
|
433 |
{ |
|
434 |
/* Check if station was ever visited before */ |
|
435 |
if (!(st->had_vehicle_of_type & HVOT_SHIP)) { |
|
436 |
uint32 flags; |
|
437 |
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
|
438 |
SetDParam(0, st->index); |
0 | 439 |
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); |
440 |
AddNewsItem( |
|
441 |
STR_9833_CITIZENS_CELEBRATE_FIRST, |
|
442 |
flags, |
|
443 |
v->index, |
|
444 |
0); |
|
445 |
} |
|
446 |
} |
|
447 |
||
448 |
typedef struct { |
|
449 |
uint skiptile; |
|
450 |
uint dest_coords; |
|
451 |
uint best_bird_dist; |
|
452 |
uint best_length; |
|
453 |
} PathFindShip; |
|
454 |
||
455 |
//extern void dbg_store_path(); |
|
456 |
||
457 |
static bool ShipTrackFollower(uint tile, PathFindShip *pfs, int track, uint length, byte *state) |
|
458 |
{ |
|
459 |
// Found dest? |
|
460 |
if (tile == pfs->dest_coords) { |
|
461 |
pfs->best_bird_dist = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
462 |
|
0 | 463 |
// if (length < pfs->best_length) |
464 |
// dbg_store_path(); |
|
465 |
||
466 |
pfs->best_length = minu(pfs->best_length, length); |
|
467 |
return true; |
|
468 |
} |
|
469 |
||
470 |
// Skip this tile in the calculation |
|
471 |
if (tile != pfs->skiptile) { |
|
472 |
pfs->best_bird_dist = minu(pfs->best_bird_dist, GetTileDist1Db(pfs->dest_coords, tile)); |
|
473 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
474 |
|
0 | 475 |
return false; |
476 |
} |
|
477 |
||
478 |
static const byte _ship_search_directions[6][4] = { |
|
479 |
{ 0, 9, 2, 9 }, |
|
480 |
{ 9, 1, 9, 3 }, |
|
481 |
{ 9, 0, 3, 9 }, |
|
482 |
{ 1, 9, 9, 2 }, |
|
483 |
{ 3, 2, 9, 9 }, |
|
484 |
{ 9, 9, 1, 0 }, |
|
485 |
}; |
|
486 |
||
487 |
static const byte _pick_shiptrack_table[6] = {1, 3, 2, 2, 0, 0}; |
|
488 |
||
489 |
static uint FindShipTrack(Vehicle *v, uint tile, int dir, uint bits, uint skiptile, int *track) |
|
490 |
{ |
|
491 |
PathFindShip pfs; |
|
492 |
int i, best_track; |
|
493 |
uint best_bird_dist = 0; |
|
494 |
uint best_length = 0; |
|
495 |
uint r; |
|
496 |
byte ship_dir = v->direction & 3; |
|
497 |
||
498 |
pfs.dest_coords = v->dest_tile; |
|
499 |
pfs.skiptile = skiptile; |
|
500 |
||
501 |
best_track = -1; |
|
502 |
||
503 |
do { |
|
504 |
i = FIND_FIRST_BIT(bits); |
|
505 |
bits = KILL_FIRST_BIT(bits); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
506 |
|
0 | 507 |
pfs.best_bird_dist = (uint)-1; |
508 |
pfs.best_length = (uint)-1; |
|
509 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
510 |
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
|
511 |
|
0 | 512 |
if (best_track >= 0) { |
513 |
if (pfs.best_bird_dist != 0) { |
|
514 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
|
515 |
if (pfs.best_bird_dist > best_bird_dist) goto bad; |
|
516 |
if (pfs.best_bird_dist < best_bird_dist) goto good; |
|
517 |
} else { |
|
518 |
if (pfs.best_length > best_length) goto bad; |
|
519 |
if (pfs.best_length < best_length) goto good; |
|
520 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
521 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
522 |
/* if we reach this position, there's two paths of equal value so far. |
0 | 523 |
* pick one randomly. */ |
524 |
r = (byte)Random(); |
|
525 |
if (_pick_shiptrack_table[i] == ship_dir) r += 80; |
|
526 |
if (_pick_shiptrack_table[best_track] == ship_dir) r -= 80; |
|
527 |
if (r <= 127) goto bad; |
|
528 |
} |
|
529 |
good:; |
|
530 |
best_track = i; |
|
531 |
best_bird_dist = pfs.best_bird_dist; |
|
532 |
best_length = pfs.best_length; |
|
533 |
bad:; |
|
534 |
||
535 |
} while (bits != 0); |
|
536 |
||
537 |
*track = best_track; |
|
538 |
return best_bird_dist; |
|
539 |
} |
|
540 |
||
541 |
static int ChooseShipTrack(Vehicle *v, uint tile, int dir, uint tracks) |
|
542 |
{ |
|
543 |
uint b; |
|
544 |
uint tot_dist, dist; |
|
545 |
int track; |
|
546 |
uint tile2; |
|
547 |
||
548 |
assert(dir>=0 && dir<=3); |
|
900 | 549 |
tile2 = TILE_ADD(tile, -TileOffsByDir(dir)); |
0 | 550 |
dir ^= 2; |
551 |
tot_dist = (uint)-1; |
|
552 |
b = GetTileShipTrackStatus(tile2) & _ship_sometracks[dir] & v->u.ship.state; |
|
553 |
if (b != 0) { |
|
554 |
dist = FindShipTrack(v, tile2, dir, b, tile, &track); |
|
555 |
if (dist != (uint)-1) |
|
556 |
tot_dist = dist + 1; |
|
557 |
} |
|
558 |
dist = FindShipTrack(v, tile, dir^2, tracks, 0, &track); |
|
559 |
if (dist > tot_dist) |
|
560 |
return -1; |
|
561 |
return track; |
|
562 |
} |
|
563 |
||
564 |
static const byte _new_vehicle_direction_table[11] = { |
|
565 |
0, 7, 6, 0, |
|
566 |
1, 0, 5, 0, |
|
567 |
2, 3, 4, |
|
568 |
}; |
|
569 |
||
570 |
static int ShipGetNewDirectionFromTiles(uint new_tile, uint old_tile) |
|
571 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
572 |
uint offs = (GET_TILE_Y(new_tile) - GET_TILE_Y(old_tile) + 1) * 4 + |
0 | 573 |
GET_TILE_X(new_tile) - GET_TILE_X(old_tile) + 1; |
574 |
assert(offs < 11 && offs != 3 && offs != 7); |
|
575 |
return _new_vehicle_direction_table[offs]; |
|
576 |
} |
|
577 |
||
578 |
static int ShipGetNewDirection(Vehicle *v, int x, int y) |
|
579 |
{ |
|
580 |
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); |
|
581 |
assert(offs < 11 && offs != 3 && offs != 7); |
|
582 |
return _new_vehicle_direction_table[offs]; |
|
583 |
} |
|
584 |
||
585 |
static int GetAvailShipTracks(uint tile, int dir) |
|
586 |
{ |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
587 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); |
0 | 588 |
return (byte) ((r | r >> 8)) & _ship_sometracks[dir]; |
589 |
} |
|
590 |
||
591 |
static const byte _ship_subcoord[4][6][3] = { |
|
592 |
{ |
|
593 |
{15, 8, 1}, |
|
594 |
{ 0, 0, 0}, |
|
595 |
{ 0, 0, 0}, |
|
596 |
{15, 8, 2}, |
|
597 |
{15, 7, 0}, |
|
598 |
{ 0, 0, 0}, |
|
599 |
}, |
|
600 |
{ |
|
601 |
{ 0, 0, 0}, |
|
602 |
{ 8, 0, 3}, |
|
603 |
{ 7, 0, 2}, |
|
604 |
{ 0, 0, 0}, |
|
605 |
{ 8, 0, 4}, |
|
606 |
{ 0, 0, 0}, |
|
607 |
}, |
|
608 |
{ |
|
609 |
{ 0, 8, 5}, |
|
610 |
{ 0, 0, 0}, |
|
611 |
{ 0, 7, 6}, |
|
612 |
{ 0, 0, 0}, |
|
613 |
{ 0, 0, 0}, |
|
614 |
{ 0, 8, 4}, |
|
615 |
}, |
|
616 |
{ |
|
617 |
{ 0, 0, 0}, |
|
618 |
{ 8,15, 7}, |
|
619 |
{ 0, 0, 0}, |
|
620 |
{ 8,15, 6}, |
|
621 |
{ 0, 0, 0}, |
|
622 |
{ 7,15, 0}, |
|
623 |
} |
|
624 |
}; |
|
625 |
||
626 |
||
627 |
static void ShipController(Vehicle *v) |
|
628 |
{ |
|
629 |
GetNewVehiclePosResult gp; |
|
630 |
uint32 r; |
|
631 |
const byte *b; |
|
632 |
int dir,track,tracks; |
|
633 |
||
634 |
v->tick_counter++; |
|
635 |
||
636 |
if (v->breakdown_ctr != 0) { |
|
637 |
if (v->breakdown_ctr <= 2) { |
|
638 |
HandleBrokenShip(v); |
|
639 |
return; |
|
640 |
} |
|
641 |
v->breakdown_ctr--; |
|
642 |
} |
|
643 |
||
644 |
if (v->vehstatus & VS_STOPPED) |
|
645 |
return; |
|
646 |
||
647 |
ProcessShipOrder(v); |
|
648 |
HandleShipLoading(v); |
|
649 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
650 |
if (v->current_order.type == OT_LOADING) |
0 | 651 |
return; |
652 |
||
653 |
CheckShipLeaveDepot(v); |
|
654 |
||
655 |
if (!ShipAccelerate(v)) |
|
656 |
return; |
|
657 |
||
658 |
BeginVehicleMove(v); |
|
659 |
||
660 |
if (GetNewVehiclePos(v, &gp)) { |
|
661 |
// staying in tile |
|
662 |
if (v->u.ship.state == 0x80) { |
|
663 |
gp.x = v->x_pos; |
|
664 |
gp.y = v->y_pos; |
|
665 |
} else { |
|
666 |
/* isnot inside depot */ |
|
667 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
668 |
if (r & 0x8) goto reverse_direction; |
|
669 |
||
670 |
if (v->dest_tile != 0 && v->dest_tile == gp.new_tile) { |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
671 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 672 |
if ((gp.x&0xF)==8 && (gp.y&0xF)==8) { |
673 |
ShipEnterDepot(v); |
|
674 |
return; |
|
675 |
} |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
676 |
} else if (v->current_order.type == OT_GOTO_STATION) { |
0 | 677 |
Station *st; |
678 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
679 |
v->last_station_visited = v->current_order.station; |
0 | 680 |
|
297
5a89d926db2d
(svn r303) Fix: [ 1022227 ] Ships could unload cargo at stations without docks
dominik
parents:
193
diff
changeset
|
681 |
/* Process station in the schedule. Don't do that for buoys (HVOT_BUOY) */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
682 |
st = DEREF_STATION(v->current_order.station); |
812
79c99885ad00
(svn r1283) -Add: AutoRenew is now a client-side patch instead of a game-side patch
truelight
parents:
755
diff
changeset
|
683 |
if (!(st->had_vehicle_of_type & HVOT_BUOY) |
297
5a89d926db2d
(svn r303) Fix: [ 1022227 ] Ships could unload cargo at stations without docks
dominik
parents:
193
diff
changeset
|
684 |
&& (st->facilities & FACIL_DOCK)) { /* ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
685 |
v->current_order.type = OT_LOADING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
686 |
v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
687 |
v->current_order.flags |= OF_NON_STOP; |
0 | 688 |
ShipArrivesAt(v, st); |
689 |
||
690 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); |
|
691 |
if (LoadUnloadVehicle(v)) { |
|
692 |
InvalidateWindow(WC_SHIPS_LIST, v->owner); |
|
693 |
MarkShipDirty(v); |
|
694 |
} |
|
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
|
695 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
297
5a89d926db2d
(svn r303) Fix: [ 1022227 ] Ships could unload cargo at stations without docks
dominik
parents:
193
diff
changeset
|
696 |
} else { /* leave buoys right aways */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
697 |
v->current_order.type = OT_LEAVESTATION; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
698 |
v->current_order.flags = 0; |
0 | 699 |
v->cur_order_index++; |
700 |
InvalidateVehicleOrderWidget(v); |
|
701 |
} |
|
702 |
goto else_end; |
|
703 |
} |
|
704 |
} |
|
705 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
706 |
if (v->current_order.type == OT_LEAVESTATION) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
707 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
708 |
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
|
709 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 710 |
} |
711 |
} |
|
712 |
} else { |
|
713 |
// new tile |
|
714 |
if (GET_TILE_X(gp.new_tile) == 0xFF || |
|
715 |
(byte)GET_TILE_Y(gp.new_tile) == 0xFF) |
|
716 |
goto reverse_direction; |
|
717 |
||
718 |
dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile); |
|
719 |
assert(dir == 1 || dir == 3 || dir == 5 || dir == 7); |
|
720 |
dir>>=1; |
|
721 |
tracks = GetAvailShipTracks(gp.new_tile, dir); |
|
722 |
if (tracks == 0) |
|
723 |
goto reverse_direction; |
|
724 |
||
725 |
// Choose a direction, and continue if we find one |
|
726 |
track = ChooseShipTrack(v, gp.new_tile, dir, tracks); |
|
727 |
if (track < 0) |
|
728 |
goto reverse_direction; |
|
729 |
||
730 |
b = _ship_subcoord[dir][track]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
731 |
|
0 | 732 |
gp.x = (gp.x&~0xF) | b[0]; |
733 |
gp.y = (gp.y&~0xF) | b[1]; |
|
734 |
||
735 |
/* Call the landscape function and tell it that the vehicle entered the tile */ |
|
736 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
737 |
if (r&0x8) goto reverse_direction; |
|
738 |
||
739 |
if (!(r&0x4)) { |
|
740 |
v->tile = gp.new_tile; |
|
741 |
v->u.ship.state = 1 << track; |
|
742 |
} |
|
743 |
||
744 |
v->direction = b[2]; |
|
745 |
} |
|
746 |
else_end:; |
|
747 |
||
748 |
/* update image of ship, as well as delta XY */ |
|
749 |
dir = ShipGetNewDirection(v, gp.x, gp.y); |
|
750 |
v->x_pos = gp.x; |
|
751 |
v->y_pos = gp.y; |
|
752 |
v->z_pos = GetSlopeZ(gp.x, gp.y); |
|
753 |
||
754 |
getout: |
|
755 |
UpdateShipDeltaXY(v, dir); |
|
756 |
v->cur_image = GetShipImage(v, dir); |
|
757 |
VehiclePositionChanged(v); |
|
758 |
EndVehicleMove(v); |
|
759 |
return; |
|
760 |
||
761 |
reverse_direction: |
|
762 |
dir = v->direction ^ 4; |
|
763 |
v->direction = dir; |
|
764 |
goto getout; |
|
765 |
} |
|
766 |
||
767 |
static void AgeShipCargo(Vehicle *v) |
|
768 |
{ |
|
769 |
if (_age_cargo_skip_counter != 0) |
|
770 |
return; |
|
771 |
if (v->cargo_days != 255) |
|
772 |
v->cargo_days++; |
|
773 |
} |
|
774 |
||
775 |
void Ship_Tick(Vehicle *v) |
|
776 |
{ |
|
777 |
AgeShipCargo(v); |
|
778 |
ShipController(v); |
|
779 |
} |
|
780 |
||
781 |
void HandleClickOnShip(Vehicle *v) |
|
782 |
{ |
|
783 |
ShowShipViewWindow(v); |
|
784 |
} |
|
785 |
||
786 |
void ShipsYearlyLoop() |
|
787 |
{ |
|
788 |
Vehicle *v; |
|
789 |
||
790 |
FOR_ALL_VEHICLES(v) { |
|
791 |
if (v->type == VEH_Ship) { |
|
792 |
v->profit_last_year = v->profit_this_year; |
|
793 |
v->profit_this_year = 0; |
|
794 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
795 |
} |
|
796 |
} |
|
797 |
} |
|
798 |
||
812
79c99885ad00
(svn r1283) -Add: AutoRenew is now a client-side patch instead of a game-side patch
truelight
parents:
755
diff
changeset
|
799 |
int32 EstimateShipCost(uint16 engine_type) |
0 | 800 |
{ |
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
801 |
return ShipVehInfo(engine_type)->base_cost * (_price.ship_base>>3)>>5; |
0 | 802 |
} |
803 |
||
804 |
// p1 = type to build |
|
805 |
int32 CmdBuildShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
806 |
{ |
|
807 |
int32 value; |
|
808 |
Vehicle *v; |
|
809 |
uint unit_num; |
|
810 |
uint tile = TILE_FROM_XY(x,y); |
|
811 |
Engine *e; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
812 |
|
0 | 813 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
814 |
||
815 |
value = EstimateShipCost(p1); |
|
816 |
if (flags & DC_QUERY_COST) |
|
817 |
return value; |
|
818 |
||
819 |
v = AllocateVehicle(); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
820 |
if (v == NULL || _ptr_to_next_order >= endof(_order_array) || |
0 | 821 |
(unit_num = GetFreeUnitNumber(VEH_Ship)) > _patches.max_ships) |
822 |
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
|
823 |
|
0 | 824 |
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
|
825 |
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
|
826 |
|
0 | 827 |
v->unitnumber = unit_num; |
828 |
||
829 |
v->owner = _current_player; |
|
830 |
v->tile = tile; |
|
831 |
x = GET_TILE_X(tile)*16 + 8; |
|
832 |
y = GET_TILE_Y(tile)*16 + 8; |
|
833 |
v->x_pos = x; |
|
834 |
v->y_pos = y; |
|
835 |
v->z_pos = GetSlopeZ(x,y); |
|
836 |
||
837 |
v->z_height = 6; |
|
838 |
v->sprite_width = 6; |
|
839 |
v->sprite_height = 6; |
|
840 |
v->x_offs = -3; |
|
841 |
v->y_offs = -3; |
|
842 |
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
|
843 |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
844 |
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
|
845 |
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
|
846 |
v->cargo_cap = svi->capacity; |
0 | 847 |
v->value = value; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
848 |
|
817
4f9377b7fd2b
(svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents:
812
diff
changeset
|
849 |
v->last_station_visited = 0xFFFF; |
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
850 |
v->max_speed = svi->max_speed; |
0 | 851 |
v->engine_type = (byte)p1; |
852 |
||
853 |
e = &_engines[p1]; |
|
854 |
v->reliability = e->reliability; |
|
855 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
856 |
v->max_age = e->lifelength * 366; |
|
857 |
_new_ship_id = v->index; |
|
858 |
||
859 |
v->string_id = STR_SV_SHIP_NAME; |
|
860 |
v->u.ship.state = 0x80; |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
861 |
_ptr_to_next_order->type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
862 |
_ptr_to_next_order->flags = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
863 |
v->schedule_ptr = _ptr_to_next_order++; |
0 | 864 |
|
865 |
v->service_interval = _patches.servint_ships; |
|
866 |
v->date_of_last_service = _date; |
|
867 |
v->build_year = _cur_year; |
|
868 |
v->cur_image = 0x0E5E; |
|
869 |
v->type = VEH_Ship; |
|
870 |
||
871 |
VehiclePositionChanged(v); |
|
872 |
||
873 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 874 |
RebuildVehicleLists(); |
0 | 875 |
InvalidateWindow(WC_COMPANY, v->owner); |
876 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
877 |
|
0 | 878 |
return value; |
879 |
} |
|
880 |
||
881 |
int32 CmdSellShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
882 |
{ |
|
883 |
Vehicle *v; |
|
884 |
||
885 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
886 |
|
0 | 887 |
v = &_vehicles[p1]; |
888 |
||
889 |
if (!CheckOwnership(v->owner)) |
|
890 |
return CMD_ERROR; |
|
891 |
||
892 |
if (!IsShipDepotTile(v->tile) || v->u.road.state != 0x80 || !(v->vehstatus&VS_STOPPED)) |
|
893 |
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
|
894 |
|
0 | 895 |
if (flags & DC_EXEC) { |
896 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 897 |
RebuildVehicleLists(); |
0 | 898 |
InvalidateWindow(WC_COMPANY, v->owner); |
899 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
900 |
DeleteVehicle(v); |
|
901 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
902 |
|
0 | 903 |
return -(int32)v->value; |
904 |
} |
|
905 |
||
906 |
// p1 = vehicle |
|
907 |
int32 CmdStartStopShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
908 |
{ |
|
909 |
Vehicle *v; |
|
910 |
||
911 |
v = &_vehicles[p1]; |
|
912 |
||
913 |
if (!CheckOwnership(v->owner)) |
|
914 |
return CMD_ERROR; |
|
915 |
||
916 |
if (flags & DC_EXEC) { |
|
917 |
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
|
918 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 919 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
920 |
} |
|
921 |
||
922 |
return 0; |
|
923 |
} |
|
924 |
||
925 |
int32 CmdSendShipToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
926 |
{ |
|
927 |
Vehicle *v; |
|
928 |
int depot; |
|
929 |
||
930 |
v = &_vehicles[p1]; |
|
931 |
||
932 |
if (!CheckOwnership(v->owner)) |
|
933 |
return CMD_ERROR; |
|
934 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
935 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 936 |
if (flags & DC_EXEC) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
937 |
if (v->current_order.flags & OF_UNLOAD) v->cur_order_index++; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
938 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
939 |
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
|
940 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 941 |
} |
942 |
} else { |
|
943 |
depot = FindClosestShipDepot(v); |
|
944 |
if (depot < 0) |
|
945 |
return_cmd_error(STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT); |
|
946 |
||
947 |
if (flags & DC_EXEC) { |
|
948 |
v->dest_tile = _depots[depot].xy; |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
949 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
950 |
v->current_order.flags = OF_NON_STOP | OF_FULL_LOAD; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
951 |
v->current_order.station = depot; |
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
|
952 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 953 |
} |
954 |
} |
|
955 |
||
956 |
return 0; |
|
957 |
} |
|
958 |
||
959 |
int32 CmdChangeShipServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
960 |
{ |
|
961 |
Vehicle *v; |
|
962 |
||
963 |
v = &_vehicles[p1]; |
|
964 |
||
965 |
if (!CheckOwnership(v->owner)) |
|
966 |
return CMD_ERROR; |
|
967 |
||
968 |
if (flags & DC_EXEC) { |
|
969 |
v->service_interval = (uint16)p2; |
|
970 |
InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7); |
|
971 |
} |
|
972 |
||
973 |
return 0; |
|
974 |
} |
|
975 |
||
976 |
||
977 |
// p1 = vehicle |
|
842 | 978 |
// p2 = new cargo (0xFF) |
979 |
// p2 = skip check for stopped in hanger (0x0100) |
|
0 | 980 |
int32 CmdRefitShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
981 |
{ |
|
982 |
Vehicle *v; |
|
983 |
int32 cost; |
|
842 | 984 |
byte SkipStoppedInDepotCheck = (p2 & 0x100) >> 8; //excludes the cargo value |
0 | 985 |
|
842 | 986 |
p2 = p2 & 0xFF; |
0 | 987 |
SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
988 |
|
0 | 989 |
v = &_vehicles[p1]; |
990 |
if (!CheckOwnership(v->owner)) |
|
991 |
return CMD_ERROR; |
|
992 |
||
842 | 993 |
if (!( SkipStoppedInDepotCheck )) { |
994 |
if (!IsShipDepotTile(v->tile) || |
|
995 |
!(v->vehstatus&VS_STOPPED) || |
|
996 |
v->u.ship.state != 0x80) |
|
997 |
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); |
|
998 |
} |
|
915 | 999 |
|
0 | 1000 |
cost = 0; |
1001 |
if (IS_HUMAN_PLAYER(v->owner) && (byte)p2 != v->cargo_type) { |
|
1002 |
cost = _price.ship_base >> 7; |
|
1003 |
} |
|
1004 |
||
1005 |
if (flags & DC_EXEC) { |
|
842 | 1006 |
//autorefitted ships wants to keep the cargo |
1007 |
//it will be checked if the cargo is valid in CmdRenewVehicle |
|
1008 |
if (!(SkipStoppedInDepotCheck)) |
|
915 | 1009 |
v->cargo_count = 0; |
0 | 1010 |
v->cargo_type = (byte)p2; |
1011 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1012 |
} |
|
1013 |
||
1014 |
return cost; |
|
1015 |
||
1016 |
} |
|
1017 |
||
1018 |