OpenTTD Source 20261112-master-gbcbec5e63f
effectvehicle.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "landscape.h"
12#include "core/random_func.hpp"
13#include "industry_map.h"
14#include "sound_func.h"
15#include "animated_tile_func.h"
16#include "effectvehicle_func.h"
17#include "effectvehicle_base.h"
18
19#include "safeguards.h"
20
21
29{
30 if (v->sprite_cache.sprite_seq.seq[0].sprite != last) {
31 v->sprite_cache.sprite_seq.seq[0].sprite++;
32 return true;
33 } else {
34 return false;
35 }
36}
37
40{
41 uint32_t r = Random();
43 v->progress = GB(r, 16, 3);
44}
45
48{
49 if (v->progress > 0) {
50 v->progress--;
51 } else {
52 TileIndex tile = TileVirtXY(v->x_pos, v->y_pos);
53 if (!IsTileType(tile, TileType::Industry)) {
54 delete v;
55 return false;
56 }
57
60 }
61 v->progress = 7;
63 }
64
65 return true;
66}
67
74
77{
78 bool moved = false;
79
80 v->progress++;
81
82 if ((v->progress & 7) == 0) {
83 v->z_pos++;
84 moved = true;
85 }
86
87 if ((v->progress & 0xF) == 4) {
89 delete v;
90 return false;
91 }
92 moved = true;
93 }
94
95 if (moved) v->UpdatePositionAndViewport();
96
97 return true;
98}
99
106
109{
110 v->progress++;
111
112 if ((v->progress & 3) == 0) {
113 v->z_pos++;
115 } else if ((v->progress & 7) == 1) {
117 delete v;
118 return false;
119 }
121 }
122
123 return true;
124}
125
132
135{
136 if (v->progress < 2) {
137 v->progress++;
138 } else {
139 v->progress = 0;
140
142 delete v;
143 return false;
144 }
146 }
147
148 return true;
149}
150
153{
155 v->progress = 12;
156}
157
160{
161 bool moved = false;
162
163 v->progress++;
164
165 if ((v->progress & 3) == 0) {
166 v->z_pos++;
167 moved = true;
168 }
169
170 if ((v->progress & 0xF) == 4) {
171 if (!IncrementSprite(v, SPR_SMOKE_4)) {
172 delete v;
173 return false;
174 }
175 moved = true;
176 }
177
178 if (moved) v->UpdatePositionAndViewport();
179
180 return true;
181}
182
189
192{
193 v->progress++;
194 if ((v->progress & 3) == 0) {
196 delete v;
197 return false;
198 }
200 }
201
202 return true;
203}
204
211
214{
215 v->progress++;
216 if ((v->progress & 7) == 0) {
219 }
221 }
222
223 v->animation_state--;
224 if (v->animation_state == 0) {
225 delete v;
226 return false;
227 }
228
229 return true;
230}
231
238
241{
242 v->progress++;
243 if ((v->progress & 3) == 0) {
245 delete v;
246 return false;
247 }
249 }
250
251 return true;
252}
253
256{
258 v->progress = 0;
259 v->animation_state = 0;
260 v->animation_substate = 0;
261}
262
264 uint8_t direction:2;
265 uint8_t image:2;
266 uint8_t duration:3;
267};
268
269static const BulldozerMovement _bulldozer_movement[] = {
270 { 0, 0, 4 },
271 { 3, 3, 4 },
272 { 2, 2, 7 },
273 { 0, 2, 7 },
274 { 1, 1, 3 },
275 { 2, 2, 7 },
276 { 0, 2, 7 },
277 { 1, 1, 3 },
278 { 2, 2, 7 },
279 { 0, 2, 7 },
280 { 3, 3, 6 },
281 { 2, 2, 6 },
282 { 1, 1, 7 },
283 { 3, 1, 7 },
284 { 0, 0, 3 },
285 { 1, 1, 7 },
286 { 3, 1, 7 },
287 { 0, 0, 3 },
288 { 1, 1, 7 },
289 { 3, 1, 7 }
290};
291
292static const Coord2D<int8_t> _inc_by_dir[] = {
293 { -1, 0 },
294 { 0, 1 },
295 { 1, 0 },
296 { 0, -1 }
297};
298
301{
302 v->progress++;
303 if ((v->progress & 7) == 0) {
304 const BulldozerMovement *b = &_bulldozer_movement[v->animation_state];
305
307
308 v->x_pos += _inc_by_dir[b->direction].x;
309 v->y_pos += _inc_by_dir[b->direction].y;
310
312 if (v->animation_substate >= b->duration) {
313 v->animation_substate = 0;
314 v->animation_state++;
315 if (v->animation_state == lengthof(_bulldozer_movement)) {
316 delete v;
317 return false;
318 }
319 }
321 }
322
323 return true;
324}
325
328{
330 v->spritenum = 0;
331 v->progress = 0;
332}
333
335 int8_t x:4;
336 int8_t y:4;
337 int8_t z:4;
338 uint8_t image:4;
339};
340
341#define MK(x, y, z, i) { x, y, z, i }
342#define ME(i) { i, 4, 0, 0 }
343
344static const BubbleMovement _bubble_float_sw[] = {
345 MK(0, 0, 1, 0),
346 MK(1, 0, 1, 1),
347 MK(0, 0, 1, 0),
348 MK(1, 0, 1, 2),
349 ME(1)
350};
351
352
353static const BubbleMovement _bubble_float_ne[] = {
354 MK( 0, 0, 1, 0),
355 MK(-1, 0, 1, 1),
356 MK( 0, 0, 1, 0),
357 MK(-1, 0, 1, 2),
358 ME(1)
359};
360
361static const BubbleMovement _bubble_float_se[] = {
362 MK(0, 0, 1, 0),
363 MK(0, 1, 1, 1),
364 MK(0, 0, 1, 0),
365 MK(0, 1, 1, 2),
366 ME(1)
367};
368
369static const BubbleMovement _bubble_float_nw[] = {
370 MK(0, 0, 1, 0),
371 MK(0, -1, 1, 1),
372 MK(0, 0, 1, 0),
373 MK(0, -1, 1, 2),
374 ME(1)
375};
376
377static const BubbleMovement _bubble_burst[] = {
378 MK(0, 0, 1, 2),
379 MK(0, 0, 1, 7),
380 MK(0, 0, 1, 8),
381 MK(0, 0, 1, 9),
382 ME(0)
383};
384
385static const BubbleMovement _bubble_absorb[] = {
386 MK(0, 0, 1, 0),
387 MK(0, 0, 1, 1),
388 MK(0, 0, 1, 0),
389 MK(0, 0, 1, 2),
390 MK(0, 0, 1, 0),
391 MK(0, 0, 1, 1),
392 MK(0, 0, 1, 0),
393 MK(0, 0, 1, 2),
394 MK(0, 0, 1, 0),
395 MK(0, 0, 1, 1),
396 MK(0, 0, 1, 0),
397 MK(0, 0, 1, 2),
398 MK(0, 0, 1, 0),
399 MK(0, 0, 1, 1),
400 MK(0, 0, 1, 0),
401 MK(0, 0, 1, 2),
402 MK(0, 0, 1, 0),
403 MK(0, 0, 1, 1),
404 MK(0, 0, 1, 0),
405 MK(0, 0, 1, 2),
406 MK(0, 0, 1, 0),
407 MK(0, 0, 1, 1),
408 MK(0, 0, 1, 0),
409 MK(0, 0, 1, 2),
410 MK(0, 0, 1, 0),
411 MK(0, 0, 1, 1),
412 MK(0, 0, 1, 0),
413 MK(0, 0, 1, 2),
414 MK(0, 0, 1, 0),
415 MK(0, 0, 1, 1),
416 MK(0, 0, 1, 0),
417 MK(0, 0, 1, 2),
418 MK(0, 0, 1, 0),
419 MK(0, 0, 1, 1),
420 MK(0, 0, 1, 0),
421 MK(0, 0, 1, 2),
422 MK(0, 0, 1, 0),
423 MK(0, 0, 1, 1),
424 MK(0, 0, 1, 0),
425 MK(0, 0, 1, 2),
426 MK(0, 0, 1, 0),
427 MK(0, 0, 1, 1),
428 MK(0, 0, 1, 0),
429 MK(0, 0, 1, 2),
430 MK(0, 0, 1, 0),
431 MK(0, 0, 1, 1),
432 MK(0, 0, 1, 0),
433 MK(0, 0, 1, 2),
434 MK(0, 0, 1, 0),
435 MK(0, 0, 1, 1),
436 MK(0, 0, 1, 0),
437 MK(0, 0, 1, 2),
438 MK(0, 0, 1, 0),
439 MK(0, 0, 1, 1),
440 MK(0, 0, 1, 0),
441 MK(0, 0, 1, 2),
442 MK(0, 0, 1, 0),
443 MK(0, 0, 1, 1),
444 MK(0, 0, 1, 0),
445 MK(0, 0, 1, 2),
446 MK(0, 0, 1, 0),
447 MK(0, 0, 1, 1),
448 MK(2, 1, 3, 0),
449 MK(1, 1, 3, 1),
450 MK(2, 1, 3, 0),
451 MK(1, 1, 3, 2),
452 MK(2, 1, 3, 0),
453 MK(1, 1, 3, 1),
454 MK(2, 1, 3, 0),
455 MK(1, 0, 1, 2),
456 MK(0, 0, 1, 0),
457 MK(1, 0, 1, 1),
458 MK(0, 0, 1, 0),
459 MK(1, 0, 1, 2),
460 MK(0, 0, 1, 0),
461 MK(1, 0, 1, 1),
462 MK(0, 0, 1, 0),
463 MK(1, 0, 1, 2),
464 ME(2),
465 MK(0, 0, 0, 0xA),
466 MK(0, 0, 0, 0xB),
467 MK(0, 0, 0, 0xC),
468 MK(0, 0, 0, 0xD),
469 MK(0, 0, 0, 0xE),
470 ME(0)
471};
472#undef ME
473#undef MK
474
475static const BubbleMovement * const _bubble_movement[] = {
476 _bubble_float_sw,
477 _bubble_float_ne,
478 _bubble_float_se,
479 _bubble_float_nw,
480 _bubble_burst,
481 _bubble_absorb,
482};
483
486{
487 uint anim_state;
488
489 v->progress++;
490 if ((v->progress & 3) != 0) return true;
491
492 if (v->spritenum == 0) {
493 v->sprite_cache.sprite_seq.seq[0].sprite++;
494 if (v->sprite_cache.sprite_seq.seq[0].sprite < SPR_BUBBLE_GENERATE_3) {
496 return true;
497 }
498 if (v->animation_substate != 0) {
499 v->spritenum = GB(Random(), 0, 2) + 1;
500 } else {
501 v->spritenum = 6;
502 }
503 anim_state = 0;
504 } else {
505 anim_state = v->animation_state + 1;
506 }
507
508 const BubbleMovement *b = &_bubble_movement[v->spritenum - 1][anim_state];
509
510 if (b->y == 4 && b->x == 0) {
511 delete v;
512 return false;
513 }
514
515 if (b->y == 4 && b->x == 1) {
516 if (v->z_pos > 180 || Chance16I(1, 96, Random())) {
517 v->spritenum = 5;
518 if (_settings_client.sound.ambient) SndPlayVehicleFx(SND_2F_BUBBLE_GENERATOR_FAIL, v);
519 }
520 anim_state = 0;
521 }
522
523 if (b->y == 4 && b->x == 2) {
524 TileIndex tile;
525
526 anim_state++;
527 if (_settings_client.sound.ambient) SndPlayVehicleFx(SND_31_BUBBLE_GENERATOR_SUCCESS, v);
528
529 tile = TileVirtXY(v->x_pos, v->y_pos);
531 }
532
533 v->animation_state = anim_state;
534 b = &_bubble_movement[v->spritenum - 1][anim_state];
535
536 v->x_pos += b->x;
537 v->y_pos += b->y;
538 v->z_pos += b->z;
540
542
543 return true;
544}
545
568
570static const std::array<EffectProcs, EV_END> _effect_procs = {{
575 { SmokeInit, SmokeTick, TransparencyOption::Invalid }, // EV_CRASH_SMOKE
581 { SmokeInit, SmokeTick, TransparencyOption::Invalid }, // EV_BREAKDOWN_SMOKE_AIRCRAFT
582 { SmokeInit, SmokeTick, TransparencyOption::Industries }, // EV_COPPER_MINE_SMOKE
583}};
584
594{
595 if (!Vehicle::CanAllocateItem()) return nullptr;
596
598 v->subtype = type;
599 v->x_pos = x;
600 v->y_pos = y;
601 v->z_pos = z;
602 v->tile = TileIndex{};
603 v->UpdateDeltaXY();
605
606 _effect_procs[type].init_proc(v);
607
609
610 return v;
611}
612
622{
623 int safe_x = Clamp(x, 0, Map::MaxX() * TILE_SIZE);
624 int safe_y = Clamp(y, 0, Map::MaxY() * TILE_SIZE);
625 return CreateEffectVehicle(x, y, GetSlopePixelZ(safe_x, safe_y) + z, type);
626}
627
638{
639 return CreateEffectVehicle(v->x_pos + x, v->y_pos + y, v->z_pos + z, type);
640}
641
643{
644 return _effect_procs[this->subtype].tick_proc(this);
645}
646
648{
649 this->bounds = {{}, {1, 1, 1}, {}};
650}
651
657{
658 return _effect_procs[this->subtype].transparency;
659}
void AddAnimatedTile(TileIndex tile, bool mark_dirty)
Add the given tile to the animated tile table (if it does not exist yet).
Tile animation!
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
#define MK(bt, label, colour, weight, mult, ip, td1, td2, freight, tae, str_plural, str_singular, str_volume, classes)
Construction macro for a CargoSpec structure.
Definition cargo_const.h:51
static void ElectricSparkInit(EffectVehicle *v)
Initialise the sparks of a train.
static void ExplosionLargeInit(EffectVehicle *v)
Initialise a large explosion.
static void BubbleInit(EffectVehicle *v)
Initialise the bubbles of the bubble generator industry.
static void DieselSmokeInit(EffectVehicle *v)
Initialise the smoke of a diesel engine.
static void ChimneySmokeInit(EffectVehicle *v)
Initialise the smoke of a chimney.
static bool BulldozerTick(EffectVehicle *v)
Run a single tick of a bulldozer (road works).
EffectVehicle * CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular vehicle.
static bool DieselSmokeTick(EffectVehicle *v)
Run a single tick of the smoke of a diesel engine.
EffectVehicle * CreateEffectVehicle(int x, int y, int z, EffectVehicleType type)
Create an effect vehicle at a particular location.
static bool IncrementSprite(EffectVehicle *v, SpriteID last)
Increment the sprite unless it has reached the end of the animation.
static bool BreakdownSmokeTick(EffectVehicle *v)
Run a single tick of the smoke of a broken down vehicle.
static const std::array< EffectProcs, EV_END > _effect_procs
Per-EffectVehicleType handling.
EffectVehicle * CreateEffectVehicleAbove(int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular location.
static void SteamSmokeInit(EffectVehicle *v)
Initialise the smoke of a steam engine.
static bool ElectricSparkTick(EffectVehicle *v)
Run a single tick of the sparks of a train.
static void BreakdownSmokeInit(EffectVehicle *v)
Initialise the smoke of a broken down vehicle.
static bool SteamSmokeTick(EffectVehicle *v)
Run a single tick of the smoke of a steam engine.
static void ExplosionSmallInit(EffectVehicle *v)
Initialise a small explosion.
static void BulldozerInit(EffectVehicle *v)
Initialise the bulldozer (road works).
static bool ChimneySmokeTick(EffectVehicle *v)
Run a single tick of the smoke of a chimney.
static bool SmokeTick(EffectVehicle *v)
Run a single tick of some smoke.
static bool BubbleTick(EffectVehicle *v)
Run a single tick of bubbles of the bubble generator industry.
static void SmokeInit(EffectVehicle *v)
Initialise some smoke.
static bool ExplosionSmallTick(EffectVehicle *v)
Run a single tick of a small explosion.
static bool ExplosionLargeTick(EffectVehicle *v)
Run a single tick of a large explosion.
Base class for all effect vehicles.
Functions related to effect vehicles.
EffectVehicleType
Effect vehicle types.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
Accessors to map for industries.
IndustryGfx GetIndustryGfx(Tile t)
Get the industry graphics ID for the given industry tile.
static constexpr IndustryGfx GFX_BUBBLE_CATCHER
int GetSlopePixelZ(int x, int y, bool ground_vehicle)
Return world Z coordinate of a given point of a tile.
Functions related to OTTD's landscape.
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition map_func.h:407
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
Pseudo random number generator.
bool Chance16I(const uint32_t a, const uint32_t b, const uint32_t r)
Checks if a given randomize-number is below a given probability.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Functions related to sound.
@ SND_31_BUBBLE_GENERATOR_SUCCESS
49 == 0x31 Industry animation: bubble generator (2b): bubble slurped
Definition sound_type.h:96
@ SND_2F_BUBBLE_GENERATOR_FAIL
47 == 0x2F Industry animation: bubble generator (2a): bubble pop
Definition sound_type.h:94
static const SpriteID SPR_BUBBLE_0
Definition sprites.h:1431
static const SpriteID SPR_STEAM_SMOKE_0
Definition sprites.h:1374
static const SpriteID SPR_EXPLOSION_LARGE_0
Definition sprites.h:1396
static const SpriteID SPR_BUBBLE_GENERATE_3
Definition sprites.h:1437
static const SpriteID SPR_EXPLOSION_SMALL_0
Definition sprites.h:1413
static const SpriteID SPR_STEAM_SMOKE_4
Definition sprites.h:1378
static const SpriteID SPR_ELECTRIC_SPARK_0
Definition sprites.h:1380
static const SpriteID SPR_EXPLOSION_SMALL_B
Definition sprites.h:1424
static const SpriteID SPR_ELECTRIC_SPARK_5
Definition sprites.h:1385
static const SpriteID SPR_BREAKDOWN_SMOKE_0
Definition sprites.h:1426
static const SpriteID SPR_BUBBLE_GENERATE_0
Definition sprites.h:1434
static const SpriteID SPR_CHIMNEY_SMOKE_0
Definition sprites.h:1387
static const SpriteID SPR_BULLDOZER_NE
Definition sprites.h:1356
static const SpriteID SPR_DIESEL_SMOKE_0
Definition sprites.h:1367
static const SpriteID SPR_SMOKE_4
Definition sprites.h:1365
static const SpriteID SPR_CHIMNEY_SMOKE_7
Definition sprites.h:1394
static const SpriteID SPR_SMOKE_0
Definition sprites.h:1361
static const SpriteID SPR_EXPLOSION_LARGE_F
Definition sprites.h:1411
static const SpriteID SPR_BREAKDOWN_SMOKE_3
Definition sprites.h:1429
static const SpriteID SPR_DIESEL_SMOKE_5
Definition sprites.h:1372
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:261
A coordinate with two dimensions.
bool(EffectVehicle *v) TickProc
Run the actions/perform the behaviour of an effect vehicle for a tick.
void(EffectVehicle *v) InitProc
Initialises effect vehicle for a specific type.
InitProc * init_proc
Function to initialise an effect vehicle after construction.
TickProc * tick_proc
Functions for controlling effect vehicles at each tick.
TransparencyOption transparency
Transparency option affecting the effect.
A special vehicle is one of the following:
void UpdateDeltaXY() override
Updates the x and y offsets and the size of the sprite used for this vehicle.
TransparencyOption GetTransparencyOption() const
Determines the transparency option affecting the effect.
uint8_t animation_substate
Sub state to time the change of the graphics/behaviour.
bool Tick() override
Calls the tick handler of the vehicle.
uint16_t animation_state
State primarily used to change the graphics/behaviour.
static uint MaxY()
Gets the maximum Y coordinate within the map, including TileType::Void.
Definition map_func.h:298
static uint MaxX()
Gets the maximum X coordinate within the map, including TileType::Void.
Definition map_func.h:289
VehicleSpriteSeq sprite_seq
Vehicle appearance.
static T * Create(Targs &&... args)
static bool CanAllocateItem(size_t n=1)
void Set(SpriteID sprite)
Assign a single sprite to the sequence.
Vehicle data structure.
int32_t z_pos
z coordinate.
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
VehStates vehstatus
Status.
int32_t y_pos
y coordinate.
int32_t x_pos
x coordinate.
SpriteBounds bounds
Bounding box of vehicle.
uint8_t spritenum
currently displayed sprite index 0xfd == custom sprite, 0xfe == custom second head sprite 0xff == res...
MutableSpriteCache sprite_cache
Cache of sprites and values related to recalculating them, see MutableSpriteCache.
uint8_t progress
The percentage (if divided by 256) this vehicle already crossed the tile unit.
void UpdatePositionAndViewport()
Update the position of the vehicle, and update the viewport.
Definition vehicle.cpp:1784
TileIndex tile
Current tile index.
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
@ Industry
Part of an industry.
Definition tile_type.h:57
TransparencyOption
Transparency option bits: which position in _transparency_opt stands for which transparency.
@ Invalid
Invalid transparency option.
@ Industries
industries
@ Unclickable
Vehicle is not clickable by the user (shadow vehicles).