OpenTTD: src/ship_cmd.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

ship_cmd.cpp
Go to the documentation of this file.
1 /* $Id: ship_cmd.cpp 27667 2016-10-16 14:58:38Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "stdafx.h"
13 #include "ship.h"
14 #include "landscape.h"
15 #include "timetable.h"
16 #include "news_func.h"
17 #include "company_func.h"
18 #include "pathfinder/npf/npf_func.h"
19 #include "depot_base.h"
20 #include "station_base.h"
21 #include "newgrf_engine.h"
22 #include "pathfinder/yapf/yapf.h"
23 #include "newgrf_sound.h"
24 #include "spritecache.h"
25 #include "strings_func.h"
26 #include "window_func.h"
27 #include "date_func.h"
28 #include "vehicle_func.h"
29 #include "sound_func.h"
30 #include "ai/ai.hpp"
31 #include "pathfinder/opf/opf_ship.h"
32 #include "engine_base.h"
33 #include "company_base.h"
34 #include "tunnelbridge_map.h"
35 #include "zoom_func.h"
36
37 #include "table/strings.h"
38
39 #include "safeguards.h"
40
46 WaterClass GetEffectiveWaterClass(TileIndex tile)
47 {
48 if (HasTileWaterClass(tile)) return GetWaterClass(tile);
49 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
50 assert(GetTunnelBridgeTransportType(tile) == TRANSPORT_WATER);
51 return WATER_CLASS_CANAL;
52 }
53 if (IsTileType(tile, MP_RAILWAY)) {
54 assert(GetRailGroundType(tile) == RAIL_GROUND_WATER);
55 return WATER_CLASS_SEA;
56 }
57 NOT_REACHED();
58 }
59
60 static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
61
62 template <>
63 bool IsValidImageIndex<VEH_SHIP>(uint8 image_index)
64 {
65 return image_index < lengthof(_ship_sprites);
66 }
67
68 static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
69 {
70 return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
71 }
72
73 static void GetShipIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
74 {
75 const Engine *e = Engine::Get(engine);
76 uint8 spritenum = e->u.ship.image_index;
77
78 if (is_custom_sprite(spritenum)) {
79 GetCustomVehicleIcon(engine, DIR_W, image_type, result);
80 if (result->IsValid()) return;
81
82 spritenum = e->original_image_index;
83 }
84
85 assert(IsValidImageIndex<VEH_SHIP>(spritenum));
86 result->Set(DIR_W + _ship_sprites[spritenum]);
87 }
88
89 void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
90 {
91 VehicleSpriteSeq seq;
92 GetShipIcon(engine, image_type, &seq);
93
94 Rect rect;
95 seq.GetBounds(&rect);
96 preferred_x = Clamp(preferred_x,
97 left - UnScaleGUI(rect.left),
98 right - UnScaleGUI(rect.right));
99
100 seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
101 }
102
112 void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
113 {
114 VehicleSpriteSeq seq;
115 GetShipIcon(engine, image_type, &seq);
116
117 Rect rect;
118 seq.GetBounds(&rect);
119
120 width = UnScaleGUI(rect.right - rect.left + 1);
121 height = UnScaleGUI(rect.bottom - rect.top + 1);
122 xoffs = UnScaleGUI(rect.left);
123 yoffs = UnScaleGUI(rect.top);
124 }
125
126 void Ship::GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
127 {
128 uint8 spritenum = this->spritenum;
129
130 if (is_custom_sprite(spritenum)) {
131 GetCustomVehicleSprite(this, direction, image_type, result);
132 if (result->IsValid()) return;
133
134 spritenum = this->GetEngine()->original_image_index;
135 }
136
137 assert(IsValidImageIndex<VEH_SHIP>(spritenum));
138 result->Set(_ship_sprites[spritenum] + direction);
139 }
140
141 static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
142 {
143 /* Find the closest depot */
144 const Depot *depot;
145 const Depot *best_depot = NULL;
146 /* If we don't have a maximum distance, i.e. distance = 0,
147 * we want to find any depot so the best distance of no
148 * depot must be more than any correct distance. On the
149 * other hand if we have set a maximum distance, any depot
150 * further away than max_distance can safely be ignored. */
151 uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;
152
153 FOR_ALL_DEPOTS(depot) {
154 TileIndex tile = depot->xy;
155 if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
156 uint dist = DistanceManhattan(tile, v->tile);
157 if (dist < best_dist) {
158 best_dist = dist;
159 best_depot = depot;
160 }
161 }
162 }
163
164 return best_depot;
165 }
166
167 static void CheckIfShipNeedsService(Vehicle *v)
168 {
169 if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
170 if (v->IsChainInDepot()) {
171 VehicleServiceInDepot(v);
172 return;
173 }
174
175 uint max_distance;
176 switch (_settings_game.pf.pathfinder_for_ships) {
177 case VPF_OPF: max_distance = 12; break;
178 case VPF_NPF: max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty / NPF_TILE_LENGTH; break;
179 case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
180 default: NOT_REACHED();
181 }
182
183 const Depot *depot = FindClosestShipDepot(v, max_distance);
184
185 if (depot == NULL) {
186 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
187 v->current_order.MakeDummy();
188 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
189 }
190 return;
191 }
192
193 v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
194 v->dest_tile = depot->xy;
195 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
196 }
197
201 void Ship::UpdateCache()
202 {
203 const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);
204
205 /* Get speed fraction for the current water type. Aqueducts are always canals. */
206 bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA;
207 uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
208 this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);
209
210 /* Update cargo aging period. */
211 this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period);
212
213 this->UpdateVisualEffect();
214 }
215
216 Money Ship::GetRunningCost() const
217 {
218 const Engine *e = this->GetEngine();
219 uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
220 return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
221 }
222
223 void Ship::OnNewDay()
224 {
225 if ((++this->day_counter & 7) == 0) {
226 DecreaseVehicleValue(this);
227 }
228
229 CheckVehicleBreakdown(this);
230 AgeVehicle(this);
231 CheckIfShipNeedsService(this);
232
233 CheckOrders(this);
234
235 if (this->running_ticks == 0) return;
236
237 CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
238
239 this->profit_this_year -= cost.GetCost();
240 this->running_ticks = 0;
241
242 SubtractMoneyFromCompanyFract(this->owner, cost);
243
244 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
245 /* we need this for the profit */
246 SetWindowClassesDirty(WC_SHIPS_LIST);
247 }
248
249 Trackdir Ship::GetVehicleTrackdir() const
250 {
251 if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
252
253 if (this->IsInDepot()) {
254 /* We'll assume the ship is facing outwards */
255 return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
256 }
257
258 if (this->state == TRACK_BIT_WORMHOLE) {
259 /* ship on aqueduct, so just use his direction and assume a diagonal track */
260 return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
261 }
262
263 return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
264 }
265
266 void Ship::MarkDirty()
267 {
268 this->colourmap = PAL_NONE;
269 this->UpdateViewport(true, false);
270 this->UpdateCache();
271 }
272
273 static void PlayShipSound(const Vehicle *v)
274 {
275 if (!PlayVehicleSound(v, VSE_START)) {
276 SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
277 }
278 }
279
280 void Ship::PlayLeaveStationSound() const
281 {
282 PlayShipSound(this);
283 }
284
285 TileIndex Ship::GetOrderStationLocation(StationID station)
286 {
287 if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
288
289 const Station *st = Station::Get(station);
290 if (st->dock_tile != INVALID_TILE) {
291 return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
292 } else {
293 this->IncrementRealOrderIndex();
294 return 0;
295 }
296 }
297
298 void Ship::UpdateDeltaXY(Direction direction)
299 {
300 static const int8 _delta_xy_table[8][4] = {
301 /* y_extent, x_extent, y_offs, x_offs */
302 { 6, 6, -3, -3}, // N
303 { 6, 32, -3, -16}, // NE
304 { 6, 6, -3, -3}, // E
305 {32, 6, -16, -3}, // SE
306 { 6, 6, -3, -3}, // S
307 { 6, 32, -3, -16}, // SW
308 { 6, 6, -3, -3}, // W
309 {32, 6, -16, -3}, // NW
310 };
311
312 const int8 *bb = _delta_xy_table[direction];
313 this->x_offs = bb[3];
314 this->y_offs = bb[2];
315 this->x_extent = bb[1];
316 this->y_extent = bb[0];
317 this->z_extent = 6;
318 }
319
320 static const TileIndexDiffC _ship_leave_depot_offs[] = {
321 {-1, 0},
322 { 0, -1}
323 };
324
325 static bool CheckShipLeaveDepot(Ship *v)
326 {
327 if (!v->IsChainInDepot()) return false;
328
329 /* We are leaving a depot, but have to go to the exact same one; re-enter */
330 if (v->current_order.IsType(OT_GOTO_DEPOT) &&
331 IsShipDepotTile(v->tile) && GetDepotIndex(v->tile) == v->current_order.GetDestination()) {
332 VehicleEnterDepot(v);
333 return true;
334 }
335
336 TileIndex tile = v->tile;
337 Axis axis = GetShipDepotAxis(tile);
338
339 DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis));
340 TileIndex north_neighbour = TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis]));
341 DiagDirection south_dir = AxisToDiagDir(axis);
342 TileIndex south_neighbour = TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[axis]));
343
344 TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour);
345 TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour);
346 if (north_tracks && south_tracks) {
347 /* Ask pathfinder for best direction */
348 bool reverse = false;
349 bool path_found;
350 switch (_settings_game.pf.pathfinder_for_ships) {
351 case VPF_OPF: reverse = OPFShipChooseTrack(v, north_neighbour, north_dir, north_tracks, path_found) == INVALID_TRACK; break; // OPF always allows reversing
352 case VPF_NPF: reverse = NPFShipCheckReverse(v); break;
353 case VPF_YAPF: reverse = YapfShipCheckReverse(v); break;
354 default: NOT_REACHED();
355 }
356 if (reverse) north_tracks = TRACK_BIT_NONE;
357 }
358
359 if (north_tracks) {
360 /* Leave towards north */
361 v->direction = DiagDirToDir(north_dir);
362 } else if (south_tracks) {
363 /* Leave towards south */
364 v->direction = DiagDirToDir(south_dir);
365 } else {
366 /* Both ways blocked */
367 return false;
368 }
369
370 v->state = AxisToTrackBits(axis);
371 v->vehstatus &= ~VS_HIDDEN;
372
373 v->cur_speed = 0;
374 v->UpdateViewport(true, true);
375 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
376
377 PlayShipSound(v);
378 VehicleServiceInDepot(v);
379 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
380 SetWindowClassesDirty(WC_SHIPS_LIST);
381
382 return false;
383 }
384
385 static bool ShipAccelerate(Vehicle *v)
386 {
387 uint spd;
388 byte t;
389
390 spd = min(v->cur_speed + 1, v->vcache.cached_max_speed);
391 spd = min(spd, v->current_order.GetMaxSpeed() * 2);
392
393 /* updates statusbar only if speed have changed to save CPU time */
394 if (spd != v->cur_speed) {
395 v->cur_speed = spd;
396 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
397 }
398
399 /* Convert direction-independent speed into direction-dependent speed. (old movement method) */
400 spd = v->GetOldAdvanceSpeed(spd);
401
402 if (spd == 0) return false;
403 if ((byte)++spd == 0) return true;
404
405 v->progress = (t = v->progress) - (byte)spd;
406
407 return (t < v->progress);
408 }
409
415 static void ShipArrivesAt(const Vehicle *v, Station *st)
416 {
417 /* Check if station was ever visited before */
418 if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
419 st->had_vehicle_of_type |= HVOT_SHIP;
420
421 SetDParam(0, st->index);
422 AddVehicleNewsItem(
423 STR_NEWS_FIRST_SHIP_ARRIVAL,
424 (v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
425 v->index,
426 st->index
427 );
428 AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
429 }
430 }
431
432
442 static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
443 {
444 assert(IsValidDiagDirection(enterdir));
445
446 bool path_found = true;
447 Track track;
448 switch (_settings_game.pf.pathfinder_for_ships) {
449 case VPF_OPF: track = OPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
450 case VPF_NPF: track = NPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
451 case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
452 default: NOT_REACHED();
453 }
454
455 v->HandlePathfindingResult(path_found);
456 return track;
457 }
458
459 static const Direction _new_vehicle_direction_table[] = {
460 DIR_N , DIR_NW, DIR_W , INVALID_DIR,
461 DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
462 DIR_E , DIR_SE, DIR_S
463 };
464
465 static Direction ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
466 {
467 uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
468 TileX(new_tile) - TileX(old_tile) + 1;
469 assert(offs < 11 && offs != 3 && offs != 7);
470 return _new_vehicle_direction_table[offs];
471 }
472
473 static Direction ShipGetNewDirection(Vehicle *v, int x, int y)
474 {
475 uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
476 assert(offs < 11 && offs != 3 && offs != 7);
477 return _new_vehicle_direction_table[offs];
478 }
479
480 static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
481 {
482 return GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
483 }
484
485 static const byte _ship_subcoord[4][6][3] = {
486 {
487 {15, 8, 1},
488 { 0, 0, 0},
489 { 0, 0, 0},
490 {15, 8, 2},
491 {15, 7, 0},
492 { 0, 0, 0},
493 },
494 {
495 { 0, 0, 0},
496 { 8, 0, 3},
497 { 7, 0, 2},
498 { 0, 0, 0},
499 { 8, 0, 4},
500 { 0, 0, 0},
501 },
502 {
503 { 0, 8, 5},
504 { 0, 0, 0},
505 { 0, 7, 6},
506 { 0, 0, 0},
507 { 0, 0, 0},
508 { 0, 8, 4},
509 },
510 {
511 { 0, 0, 0},
512 { 8, 15, 7},
513 { 0, 0, 0},
514 { 8, 15, 6},
515 { 0, 0, 0},
516 { 7, 15, 0},
517 }
518 };
519
520 static void ShipController(Ship *v)
521 {
522 uint32 r;
523 const byte *b;
524 Direction dir;
525 Track track;
526 TrackBits tracks;
527
528 v->tick_counter++;
529 v->current_order_time++;
530
531 if (v->HandleBreakdown()) return;
532
533 if (v->vehstatus & VS_STOPPED) return;
534
535 ProcessOrders(v);
536 v->HandleLoading();
537
538 if (v->current_order.IsType(OT_LOADING)) return;
539
540 if (CheckShipLeaveDepot(v)) return;
541
542 v->ShowVisualEffect();
543
544 if (!ShipAccelerate(v)) return;
545
546 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
547 if (v->state != TRACK_BIT_WORMHOLE) {
548 /* Not on a bridge */
549 if (gp.old_tile == gp.new_tile) {
550 /* Staying in tile */
551 if (v->IsInDepot()) {
552 gp.x = v->x_pos;
553 gp.y = v->y_pos;
554 } else {
555 /* Not inside depot */
556 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
557 if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
558
559 /* A leave station order only needs one tick to get processed, so we can
560 * always skip ahead. */
561 if (v->current_order.IsType(OT_LEAVESTATION)) {
562 v->current_order.Free();
563 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
564 } else if (v->dest_tile != 0) {
565 /* We have a target, let's see if we reached it... */
566 if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
567 DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
568 /* We got within 3 tiles of our target buoy, so let's skip to our
569 * next order */
570 UpdateVehicleTimetable(v, true);
571 v->IncrementRealOrderIndex();
572 v->current_order.MakeDummy();
573 } else {
574 /* Non-buoy orders really need to reach the tile */
575 if (v->dest_tile == gp.new_tile) {
576 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
577 if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
578 VehicleEnterDepot(v);
579 return;
580 }
581 } else if (v->current_order.IsType(OT_GOTO_STATION)) {
582 v->last_station_visited = v->current_order.GetDestination();
583
584 /* Process station in the orderlist. */
585 Station *st = Station::Get(v->current_order.GetDestination());
586 if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
587 ShipArrivesAt(v, st);
588 v->BeginLoading();
589 } else { // leave stations without docks right aways
590 v->current_order.MakeLeaveStation();
591 v->IncrementRealOrderIndex();
592 }
593 }
594 }
595 }
596 }
597 }
598 } else {
599 /* New tile */
600 if (!IsValidTile(gp.new_tile)) goto reverse_direction;
601
602 dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
603 assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
604 DiagDirection diagdir = DirToDiagDir(dir);
605 tracks = GetAvailShipTracks(gp.new_tile, diagdir);
606 if (tracks == TRACK_BIT_NONE) goto reverse_direction;
607
608 /* Choose a direction, and continue if we find one */
609 track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
610 if (track == INVALID_TRACK) goto reverse_direction;
611
612 b = _ship_subcoord[diagdir][track];
613
614 gp.x = (gp.x & ~0xF) | b[0];
615 gp.y = (gp.y & ~0xF) | b[1];
616
617 /* Call the landscape function and tell it that the vehicle entered the tile */
618 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
619 if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
620
621 if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
622 v->tile = gp.new_tile;
623 v->state = TrackToTrackBits(track);
624
625 /* Update ship cache when the water class changes. Aqueducts are always canals. */
626 WaterClass old_wc = GetEffectiveWaterClass(gp.old_tile);
627 WaterClass new_wc = GetEffectiveWaterClass(gp.new_tile);
628 if (old_wc != new_wc) v->UpdateCache();
629 }
630
631 v->direction = (Direction)b[2];
632 }
633 } else {
634 /* On a bridge */
635 if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
636 v->x_pos = gp.x;
637 v->y_pos = gp.y;
638 v->UpdatePosition();
639 if ((v->vehstatus & VS_HIDDEN) == 0) v->Vehicle::UpdateViewport(true);
640 return;
641 }
642 }
643
644 /* update image of ship, as well as delta XY */
645 dir = ShipGetNewDirection(v, gp.x, gp.y);
646 v->x_pos = gp.x;
647 v->y_pos = gp.y;
648 v->z_pos = GetSlopePixelZ(gp.x, gp.y);
649
650 getout:
651 v->UpdatePosition();
652 v->UpdateViewport(true, true);
653 return;
654
655 reverse_direction:
656 dir = ReverseDir(v->direction);
657 v->direction = dir;
658 goto getout;
659 }
660
661 bool Ship::Tick()
662 {
663 if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
664
665 ShipController(this);
666
667 return true;
668 }
669
679 CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
680 {
681 tile = GetShipDepotNorthTile(tile);
682 if (flags & DC_EXEC) {
683 int x;
684 int y;
685
686 const ShipVehicleInfo *svi = &e->u.ship;
687
688 Ship *v = new Ship();
689 *ret = v;
690
691 v->owner = _current_company;
692 v->tile = tile;
693 x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
694 y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
695 v->x_pos = x;
696 v->y_pos = y;
697 v->z_pos = GetSlopePixelZ(x, y);
698
699 v->UpdateDeltaXY(v->direction);
700 v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
701
702 v->spritenum = svi->image_index;
703 v->cargo_type = e->GetDefaultCargoType();
704 v->cargo_cap = svi->capacity;
705 v->refit_cap = 0;
706
707 v->last_station_visited = INVALID_STATION;
708 v->last_loading_station = INVALID_STATION;
709 v->engine_type = e->index;
710
711 v->reliability = e->reliability;
712 v->reliability_spd_dec = e->reliability_spd_dec;
713 v->max_age = e->GetLifeLengthInDays();
714 _new_vehicle_id = v->index;
715
716 v->state = TRACK_BIT_DEPOT;
717
718 v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_ships);
719 v->date_of_last_service = _date;
720 v->build_year = _cur_year;
721 v->sprite_seq.Set(SPR_IMG_QUERY);
722 v->random_bits = VehicleRandomBits();
723
724 v->UpdateCache();
725
726 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
727 v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
728
729 v->InvalidateNewGRFCacheOfChain();
730
731 v->cargo_cap = e->DetermineCapacity(v);
732
733 v->InvalidateNewGRFCacheOfChain();
734
735 v->UpdatePosition();
736 }
737
738 return CommandCost();
739 }
740
741 bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
742 {
743 const Depot *depot = FindClosestShipDepot(this, 0);
744
745 if (depot == NULL) return false;
746
747 if (location != NULL) *location = depot->xy;
748 if (destination != NULL) *destination = depot->index;
749
750 return true;
751 }