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