OpenTTD
ship_cmd.cpp
Go to the documentation of this file.
1 /* $Id: ship_cmd.cpp 27807 2017-03-19 22:02:20Z peter1138 $ */
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"
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"
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 
47 {
48  if (HasTileWaterClass(tile)) return GetWaterClass(tile);
49  if (IsTileType(tile, MP_TUNNELBRIDGE)) {
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 {
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()) {
172  return;
173  }
174 
175  uint max_distance;
177  case VPF_OPF: max_distance = 12; 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)) {
189  }
190  return;
191  }
192 
194  v->dest_tile = depot->xy;
196 }
197 
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 
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 
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 
238 
239  this->profit_this_year -= cost.GetCost();
240  this->running_ticks = 0;
241 
243 
245  /* we need this for the profit */
247 }
248 
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 */
261  }
262 
264 }
265 
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 
281 {
282  PlayShipSound(this);
283 }
284 
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) {
292  } else {
293  this->IncrementRealOrderIndex();
294  return 0;
295  }
296 }
297 
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 bool CheckShipLeaveDepot(Ship *v)
321 {
322  if (!v->IsChainInDepot()) return false;
323 
324  /* We are leaving a depot, but have to go to the exact same one; re-enter */
325  if (v->current_order.IsType(OT_GOTO_DEPOT) &&
328  return true;
329  }
330 
331  TileIndex tile = v->tile;
332  Axis axis = GetShipDepotAxis(tile);
333 
334  DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis));
335  TileIndex north_neighbour = TILE_ADD(tile, TileOffsByDiagDir(north_dir));
336  DiagDirection south_dir = AxisToDiagDir(axis);
337  TileIndex south_neighbour = TILE_ADD(tile, 2 * TileOffsByDiagDir(south_dir));
338 
339  TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour);
340  TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour);
341  if (north_tracks && south_tracks) {
342  /* Ask pathfinder for best direction */
343  bool reverse = false;
344  bool path_found;
346  case VPF_OPF: reverse = OPFShipChooseTrack(v, north_neighbour, north_dir, north_tracks, path_found) == INVALID_TRACK; break; // OPF always allows reversing
347  case VPF_NPF: reverse = NPFShipCheckReverse(v); break;
348  case VPF_YAPF: reverse = YapfShipCheckReverse(v); break;
349  default: NOT_REACHED();
350  }
351  if (reverse) north_tracks = TRACK_BIT_NONE;
352  }
353 
354  if (north_tracks) {
355  /* Leave towards north */
356  v->direction = DiagDirToDir(north_dir);
357  } else if (south_tracks) {
358  /* Leave towards south */
359  v->direction = DiagDirToDir(south_dir);
360  } else {
361  /* Both ways blocked */
362  return false;
363  }
364 
365  v->state = AxisToTrackBits(axis);
366  v->vehstatus &= ~VS_HIDDEN;
367 
368  v->cur_speed = 0;
369  v->UpdateViewport(true, true);
371 
372  PlayShipSound(v);
376 
377  return false;
378 }
379 
380 static bool ShipAccelerate(Vehicle *v)
381 {
382  uint spd;
383  byte t;
384 
385  spd = min(v->cur_speed + 1, v->vcache.cached_max_speed);
386  spd = min(spd, v->current_order.GetMaxSpeed() * 2);
387 
388  /* updates statusbar only if speed have changed to save CPU time */
389  if (spd != v->cur_speed) {
390  v->cur_speed = spd;
392  }
393 
394  /* Convert direction-independent speed into direction-dependent speed. (old movement method) */
395  spd = v->GetOldAdvanceSpeed(spd);
396 
397  if (spd == 0) return false;
398  if ((byte)++spd == 0) return true;
399 
400  v->progress = (t = v->progress) - (byte)spd;
401 
402  return (t < v->progress);
403 }
404 
410 static void ShipArrivesAt(const Vehicle *v, Station *st)
411 {
412  /* Check if station was ever visited before */
413  if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
414  st->had_vehicle_of_type |= HVOT_SHIP;
415 
416  SetDParam(0, st->index);
418  STR_NEWS_FIRST_SHIP_ARRIVAL,
420  v->index,
421  st->index
422  );
423  AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
424  }
425 }
426 
427 
437 static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
438 {
439  assert(IsValidDiagDirection(enterdir));
440 
441  bool path_found = true;
442  Track track;
444  case VPF_OPF: track = OPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
445  case VPF_NPF: track = NPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
446  case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
447  default: NOT_REACHED();
448  }
449 
450  v->HandlePathfindingResult(path_found);
451  return track;
452 }
453 
454 static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
455 {
456  return GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
457 }
458 
459 static const byte _ship_subcoord[4][6][3] = {
460  {
461  {15, 8, 1},
462  { 0, 0, 0},
463  { 0, 0, 0},
464  {15, 8, 2},
465  {15, 7, 0},
466  { 0, 0, 0},
467  },
468  {
469  { 0, 0, 0},
470  { 8, 0, 3},
471  { 7, 0, 2},
472  { 0, 0, 0},
473  { 8, 0, 4},
474  { 0, 0, 0},
475  },
476  {
477  { 0, 8, 5},
478  { 0, 0, 0},
479  { 0, 7, 6},
480  { 0, 0, 0},
481  { 0, 0, 0},
482  { 0, 8, 4},
483  },
484  {
485  { 0, 0, 0},
486  { 8, 15, 7},
487  { 0, 0, 0},
488  { 8, 15, 6},
489  { 0, 0, 0},
490  { 7, 15, 0},
491  }
492 };
493 
494 static void ShipController(Ship *v)
495 {
496  uint32 r;
497  const byte *b;
498  Direction dir;
499  Track track;
500  TrackBits tracks;
501 
502  v->tick_counter++;
503  v->current_order_time++;
504 
505  if (v->HandleBreakdown()) return;
506 
507  if (v->vehstatus & VS_STOPPED) return;
508 
509  ProcessOrders(v);
510  v->HandleLoading();
511 
512  if (v->current_order.IsType(OT_LOADING)) return;
513 
514  if (CheckShipLeaveDepot(v)) return;
515 
516  v->ShowVisualEffect();
517 
518  if (!ShipAccelerate(v)) return;
519 
521  if (v->state != TRACK_BIT_WORMHOLE) {
522  /* Not on a bridge */
523  if (gp.old_tile == gp.new_tile) {
524  /* Staying in tile */
525  if (v->IsInDepot()) {
526  gp.x = v->x_pos;
527  gp.y = v->y_pos;
528  } else {
529  /* Not inside depot */
530  r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
531  if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
532 
533  /* A leave station order only needs one tick to get processed, so we can
534  * always skip ahead. */
535  if (v->current_order.IsType(OT_LEAVESTATION)) {
536  v->current_order.Free();
538  } else if (v->dest_tile != 0) {
539  /* We have a target, let's see if we reached it... */
540  if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
541  DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
542  /* We got within 3 tiles of our target buoy, so let's skip to our
543  * next order */
544  UpdateVehicleTimetable(v, true);
547  } else {
548  /* Non-buoy orders really need to reach the tile */
549  if (v->dest_tile == gp.new_tile) {
550  if (v->current_order.IsType(OT_GOTO_DEPOT)) {
551  if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
553  return;
554  }
555  } else if (v->current_order.IsType(OT_GOTO_STATION)) {
557 
558  /* Process station in the orderlist. */
560  if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
561  ShipArrivesAt(v, st);
562  v->BeginLoading();
563  } else { // leave stations without docks right aways
566  }
567  }
568  }
569  }
570  }
571  }
572  } else {
573  /* New tile */
574  if (!IsValidTile(gp.new_tile)) goto reverse_direction;
575 
577  assert(diagdir != INVALID_DIAGDIR);
578  tracks = GetAvailShipTracks(gp.new_tile, diagdir);
579  if (tracks == TRACK_BIT_NONE) goto reverse_direction;
580 
581  /* Choose a direction, and continue if we find one */
582  track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
583  if (track == INVALID_TRACK) goto reverse_direction;
584 
585  b = _ship_subcoord[diagdir][track];
586 
587  gp.x = (gp.x & ~0xF) | b[0];
588  gp.y = (gp.y & ~0xF) | b[1];
589 
590  /* Call the landscape function and tell it that the vehicle entered the tile */
591  r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
592  if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
593 
594  if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
595  v->tile = gp.new_tile;
596  v->state = TrackToTrackBits(track);
597 
598  /* Update ship cache when the water class changes. Aqueducts are always canals. */
601  if (old_wc != new_wc) v->UpdateCache();
602  }
603 
604  v->direction = (Direction)b[2];
605  }
606  } else {
607  /* On a bridge */
609  v->x_pos = gp.x;
610  v->y_pos = gp.y;
611  v->UpdatePosition();
612  if ((v->vehstatus & VS_HIDDEN) == 0) v->Vehicle::UpdateViewport(true);
613  return;
614  }
615  }
616 
617  /* update image of ship, as well as delta XY */
618  v->x_pos = gp.x;
619  v->y_pos = gp.y;
620  v->z_pos = GetSlopePixelZ(gp.x, gp.y);
621 
622 getout:
623  v->UpdatePosition();
624  v->UpdateViewport(true, true);
625  return;
626 
627 reverse_direction:
628  dir = ReverseDir(v->direction);
629  v->direction = dir;
630  goto getout;
631 }
632 
634 {
635  if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
636 
637  ShipController(this);
638 
639  return true;
640 }
641 
651 CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
652 {
653  tile = GetShipDepotNorthTile(tile);
654  if (flags & DC_EXEC) {
655  int x;
656  int y;
657 
658  const ShipVehicleInfo *svi = &e->u.ship;
659 
660  Ship *v = new Ship();
661  *ret = v;
662 
663  v->owner = _current_company;
664  v->tile = tile;
665  x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
666  y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
667  v->x_pos = x;
668  v->y_pos = y;
669  v->z_pos = GetSlopePixelZ(x, y);
670 
671  v->UpdateDeltaXY(v->direction);
673 
674  v->spritenum = svi->image_index;
676  v->cargo_cap = svi->capacity;
677  v->refit_cap = 0;
678 
679  v->last_station_visited = INVALID_STATION;
680  v->last_loading_station = INVALID_STATION;
681  v->engine_type = e->index;
682 
683  v->reliability = e->reliability;
685  v->max_age = e->GetLifeLengthInDays();
686  _new_vehicle_id = v->index;
687 
688  v->state = TRACK_BIT_DEPOT;
689 
690  v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_ships);
692  v->build_year = _cur_year;
693  v->sprite_seq.Set(SPR_IMG_QUERY);
695 
696  v->UpdateCache();
697 
699  v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
700 
702 
703  v->cargo_cap = e->DetermineCapacity(v);
704 
706 
707  v->UpdatePosition();
708  }
709 
710  return CommandCost();
711 }
712 
713 bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
714 {
715  const Depot *depot = FindClosestShipDepot(this, 0);
716 
717  if (depot == NULL) return false;
718 
719  if (location != NULL) *location = depot->xy;
720  if (destination != NULL) *destination = depot->index;
721 
722  return true;
723 }