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