OpenTTD Source: src/disaster_vehicle.cpp Source File

OpenTTD Source 15.0-RC2

disaster_vehicle.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
26#include "stdafx.h"
27
28#include "aircraft.h"
29#include "disaster_vehicle.h"
30#include "industry.h"
31#include "station_base.h"
32#include "command_func.h"
33#include "news_func.h"
34#include "town.h"
35#include "company_func.h"
36#include "strings_func.h"
37#include "viewport_func.h"
38#include "vehicle_func.h"
39#include "sound_func.h"
40#include "effectvehicle_func.h"
41#include "roadveh.h"
42#include "train.h"
43#include "ai/ai.hpp"
44#include "game/game.hpp"
45#include "company_base.h"
46#include "core/random_func.hpp"
47#include "core/backup_type.hpp"
48#include "landscape_cmd.h"
49#include "timer/timer.h"
50#include "timer/timer_game_economy.h"
51
52#include "table/strings.h"
53
54#include "safeguards.h"
55
57uint16_t _disaster_delay;
58
59static void DisasterClearSquare(TileIndex tile)
60{
61 if (EnsureNoVehicleOnGround(tile).Failed()) return;
62
63 switch (GetTileType(tile)) {
64 case MP_RAILWAY:
65 if (Company::IsHumanID(GetTileOwner(tile)) && !IsRailDepot(tile)) {
66 Backup<CompanyID> cur_company(_current_company, OWNER_WATER);
67 Command<CMD_LANDSCAPE_CLEAR>::Do(DoCommandFlag::Execute, tile);
68 cur_company.Restore();
69
70 /* update signals in buffer */
71 UpdateSignalsInBuffer();
72 }
73 break;
74
75 case MP_HOUSE: {
76 Backup<CompanyID> cur_company(_current_company, OWNER_NONE);
77 Command<CMD_LANDSCAPE_CLEAR>::Do(DoCommandFlag::Execute, tile);
78 cur_company.Restore();
79 break;
80 }
81
82 case MP_TREES:
83 case MP_CLEAR:
84 DoClearSquare(tile);
85 break;
86
87 default:
88 break;
89 }
90}
91
92static const SpriteID _disaster_images_1[] = {SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP};
93static const SpriteID _disaster_images_2[] = {SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT, SPR_UFO_SMALL_SCOUT};
94static const SpriteID _disaster_images_3[] = {SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15, SPR_F_15};
95static const SpriteID _disaster_images_4[] = {SPR_SUB_SMALL_NE, SPR_SUB_SMALL_NE, SPR_SUB_SMALL_SE, SPR_SUB_SMALL_SE, SPR_SUB_SMALL_SW, SPR_SUB_SMALL_SW, SPR_SUB_SMALL_NW, SPR_SUB_SMALL_NW};
96static const SpriteID _disaster_images_5[] = {SPR_SUB_LARGE_NE, SPR_SUB_LARGE_NE, SPR_SUB_LARGE_SE, SPR_SUB_LARGE_SE, SPR_SUB_LARGE_SW, SPR_SUB_LARGE_SW, SPR_SUB_LARGE_NW, SPR_SUB_LARGE_NW};
97static const SpriteID _disaster_images_6[] = {SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER, SPR_UFO_HARVESTER};
98static const SpriteID _disaster_images_7[] = {SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER, SPR_XCOM_SKYRANGER};
99static const SpriteID _disaster_images_8[] = {SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A, SPR_AH_64A};
100static const SpriteID _disaster_images_9[] = {SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1, SPR_ROTOR_MOVING_1};
101
102static const SpriteID * const _disaster_images[] = {
103 _disaster_images_1, _disaster_images_1,
104 _disaster_images_2, _disaster_images_2,
105 _disaster_images_3, _disaster_images_3,
106 _disaster_images_8, _disaster_images_8, _disaster_images_9,
107 _disaster_images_6, _disaster_images_6,
108 _disaster_images_7, _disaster_images_7,
109 _disaster_images_4, _disaster_images_5,
110};
111
112void DisasterVehicle::UpdateImage()
113{
114 SpriteID img = this->image_override;
115 if (img == 0) img = _disaster_images[this->subtype][this->direction];
116 this->sprite_cache.sprite_seq.Set(img);
117}
118
127 DisasterVehicle::DisasterVehicle(int x, int y, Direction direction, DisasterSubType subtype, VehicleID big_ufo_destroyer_target) :
128 SpecializedVehicleBase(), big_ufo_destroyer_target(big_ufo_destroyer_target)
129{
130 this->vehstatus = VehState::Unclickable;
131
132 this->x_pos = x;
133 this->y_pos = y;
134 switch (subtype) {
135 case ST_ZEPPELINER:
136 case ST_SMALL_UFO:
137 case ST_AIRPLANE:
138 case ST_HELICOPTER:
139 case ST_BIG_UFO:
140 case ST_BIG_UFO_DESTROYER:
141 GetAircraftFlightLevelBounds(this, &this->z_pos, nullptr);
142 break;
143
144 case ST_HELICOPTER_ROTORS:
145 GetAircraftFlightLevelBounds(this, &this->z_pos, nullptr);
146 this->z_pos += ROTOR_Z_OFFSET;
147 break;
148
149 case ST_SMALL_SUBMARINE:
150 case ST_BIG_SUBMARINE:
151 this->z_pos = 0;
152 break;
153
154 case ST_ZEPPELINER_SHADOW:
155 case ST_SMALL_UFO_SHADOW:
156 case ST_AIRPLANE_SHADOW:
157 case ST_HELICOPTER_SHADOW:
158 case ST_BIG_UFO_SHADOW:
159 case ST_BIG_UFO_DESTROYER_SHADOW:
160 this->z_pos = 0;
161 this->vehstatus.Set(VehState::Shadow);
162 break;
163 }
164
165 this->direction = direction;
166 this->tile = TileVirtXY(x, y);
167 this->subtype = subtype;
168 this->UpdateDeltaXY();
169 this->owner = OWNER_NONE;
170 this->image_override = 0;
171 this->state = 0;
172
173 this->UpdateImage();
174 this->UpdatePositionAndViewport();
175}
176
183 void DisasterVehicle::UpdatePosition(int x, int y, int z)
184{
185 this->x_pos = x;
186 this->y_pos = y;
187 this->z_pos = z;
188 this->tile = TileVirtXY(x, y);
189
190 this->UpdateImage();
191 this->UpdatePositionAndViewport();
192
193 DisasterVehicle *u = this->Next();
194 if (u != nullptr) {
195 int safe_x = Clamp(x, 0, Map::MaxX() * TILE_SIZE);
196 int safe_y = Clamp(y - 1, 0, Map::MaxY() * TILE_SIZE);
197
198 u->x_pos = x;
199 u->y_pos = y - 1 - (std::max(z - GetSlopePixelZ(safe_x, safe_y), 0) >> 3);
200 safe_y = Clamp(u->y_pos, 0, Map::MaxY() * TILE_SIZE);
201 u->z_pos = GetSlopePixelZ(safe_x, safe_y);
202 u->direction = this->direction;
203
204 u->UpdateImage();
205 u->UpdatePositionAndViewport();
206
207 if ((u = u->Next()) != nullptr) {
208 u->x_pos = x;
209 u->y_pos = y;
210 u->z_pos = z + ROTOR_Z_OFFSET;
211 u->UpdatePositionAndViewport();
212 }
213 }
214}
215
224 static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
225{
226 v->tick_counter++;
227
228 if (v->state < 2) {
229 if (HasBit(v->tick_counter, 0)) return true;
230
231 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
232
233 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
234
235 if (v->state == 1) {
236 if (++v->age == 38) {
237 v->state = 2;
238 v->age = CalendarTime::MIN_DATE;
239 }
240
241 if (GB(v->tick_counter, 0, 3) == 0) CreateEffectVehicleRel(v, 0, -17, 2, EV_CRASH_SMOKE);
242
243 } else if (v->state == 0) {
244 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
245 v->state = 1;
246 v->age = CalendarTime::MIN_DATE;
247
248 AddTileNewsItem(GetEncodedString(STR_NEWS_DISASTER_ZEPPELIN, GetStationIndex(v->tile)), NewsType::Accident, v->tile);
249 AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCrashed(GetStationIndex(v->tile)));
250 }
251 }
252
253 if (v->y_pos >= (int)((Map::SizeY() + 9) * TILE_SIZE - 1)) {
254 delete v;
255 return false;
256 }
257
258 return true;
259 }
260
261 if (v->state > 2) {
262 if (++v->age <= 13320) return true;
263
264 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
265 Station *st = Station::GetByTile(v->tile);
266 st->airport.blocks.Reset({AirportBlock::Zeppeliner, AirportBlock::RunwayIn});
267 AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCleared(st->index));
268 }
269
270 v->UpdatePosition(v->x_pos, v->y_pos, GetAircraftFlightLevel(v));
271 delete v;
272 return false;
273 }
274
275 int x = v->x_pos;
276 int y = v->y_pos;
277 int z = GetSlopePixelZ(x, y);
278 if (z < v->z_pos) z = v->z_pos - 1;
279 v->UpdatePosition(x, y, z);
280
281 if (++v->age == 1) {
282 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
283 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, v);
284 v->image_override = SPR_BLIMP_CRASHING;
285 } else if (v->age == 70) {
286 v->image_override = SPR_BLIMP_CRASHED;
287 } else if (v->age <= 300) {
288 if (GB(v->tick_counter, 0, 3) == 0) {
289 uint32_t r = Random();
290
291 CreateEffectVehicleRel(v,
292 GB(r, 0, 4) - 7,
293 GB(r, 4, 4) - 7,
294 GB(r, 8, 3) + 5,
295 EV_EXPLOSION_SMALL);
296 }
297 } else if (v->age == 350) {
298 v->state = 3;
299 v->age = CalendarTime::MIN_DATE;
300 }
301
302 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
303 Station::GetByTile(v->tile)->airport.blocks.Reset({AirportBlock::Zeppeliner, AirportBlock::RunwayIn});
304 }
305
306 return true;
307}
308
315 static bool DisasterTick_Ufo(DisasterVehicle *v)
316{
317 v->image_override = (HasBit(++v->tick_counter, 3)) ? SPR_UFO_SMALL_SCOUT_DARKER : SPR_UFO_SMALL_SCOUT;
318
319 if (v->state == 0) {
320 /* Fly around randomly */
321 int x = TileX(v->dest_tile) * TILE_SIZE;
322 int y = TileY(v->dest_tile) * TILE_SIZE;
323 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= (int)TILE_SIZE) {
324 v->direction = GetDirectionTowards(v, x, y);
325 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
326 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
327 return true;
328 }
329 if (++v->age < 6) {
330 v->dest_tile = RandomTile();
331 return true;
332 }
333 v->state = 1;
334
335 uint n = 0; // Total number of targetable road vehicles.
336 for (const Company *c : Company::Iterate()) {
337 n += c->group_all[VEH_ROAD].num_vehicle;
338 }
339
340 if (n == 0) {
341 /* If there are no targetable road vehicles, destroy the UFO. */
342 delete v;
343 return false;
344 }
345
346 n = RandomRange(n); // Choose one of them.
347 for (RoadVehicle *u : RoadVehicle::Iterate()) {
348 /* Find (n+1)-th road vehicle. */
349 if (u->IsFrontEngine() && (n-- == 0)) {
350 if (u->crashed_ctr != 0 || u->disaster_vehicle != VehicleID::Invalid()) {
351 /* Targetted vehicle is crashed or already a target, destroy the UFO. */
352 delete v;
353 return false;
354 }
355 /* Target it. */
356 v->dest_tile = TileIndex{u->index.base()};
357 v->age = CalendarTime::MIN_DATE;
358 u->disaster_vehicle = v->index;
359 break;
360 }
361 }
362
363 return true;
364 } else {
365 /* Target a vehicle */
366 RoadVehicle *u = RoadVehicle::Get(v->dest_tile.base());
367 assert(u != nullptr && u->type == VEH_ROAD && u->IsFrontEngine());
368
369 uint dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
370
371 if (dist < TILE_SIZE && !u->vehstatus.Test(VehState::Hidden) && u->breakdown_ctr == 0) {
372 u->breakdown_ctr = 3;
373 u->breakdown_delay = 140;
374 }
375
376 v->direction = GetDirectionTowards(v, u->x_pos, u->y_pos);
377 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
378
379 int z = v->z_pos;
380 if (dist <= TILE_SIZE && z > u->z_pos) z--;
381 v->UpdatePosition(gp.x, gp.y, z);
382
383 if (z <= u->z_pos && !u->vehstatus.Test(VehState::Hidden)) {
384 v->age++;
385 if (u->crashed_ctr == 0) {
386 uint victims = u->Crash();
387 u->disaster_vehicle = VehicleID::Invalid();
388
389 AddTileNewsItem(GetEncodedString(STR_NEWS_DISASTER_SMALL_UFO), NewsType::Accident, u->tile);
390
391 AI::NewEvent(u->owner, new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO, victims, u->owner));
392 Game::NewEvent(new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO, victims, u->owner));
393 }
394 }
395
396 /* Destroy? */
397 if (v->age > 50) {
398 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
399 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, v);
400 delete v;
401 return false;
402 }
403 }
404
405 return true;
406}
407
408static void DestructIndustry(Industry *i)
409{
410 for (const auto tile : Map::Iterate()) {
411 if (i->TileBelongsToIndustry(tile)) {
412 ResetIndustryConstructionStage(tile);
413 MarkTileDirtyByTile(tile);
414 }
415 }
416}
417
431 static bool DisasterTick_Aircraft(DisasterVehicle *v, uint16_t image_override, bool leave_at_top, StringID news_message, IndustryBehaviour behaviour)
432{
433 v->tick_counter++;
434 v->image_override = (v->state == 1 && HasBit(v->tick_counter, 2)) ? image_override : 0;
435
436 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
437 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
438
439 if ((leave_at_top && gp.x < (-10 * (int)TILE_SIZE)) || (!leave_at_top && gp.x > (int)(Map::SizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1)) {
440 delete v;
441 return false;
442 }
443
444 if (v->state == 2) {
445 if (GB(v->tick_counter, 0, 2) == 0) {
446 Industry *i = Industry::Get(v->dest_tile.base()); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
447 int x = TileX(i->location.tile) * TILE_SIZE;
448 int y = TileY(i->location.tile) * TILE_SIZE;
449 uint32_t r = Random();
450
451 CreateEffectVehicleAbove(
452 GB(r, 0, 6) + x,
453 GB(r, 6, 6) + y,
454 GB(r, 12, 4),
455 EV_EXPLOSION_SMALL);
456
457 if (++v->age >= 55) v->state = 3;
458 }
459 } else if (v->state == 1) {
460 if (++v->age == 112) {
461 v->state = 2;
462 v->age = CalendarTime::MIN_DATE;
463
464 Industry *i = Industry::Get(v->dest_tile.base()); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
465 DestructIndustry(i);
466
467 AddIndustryNewsItem(GetEncodedString(news_message, i->town->index), NewsType::Accident, i->index);
468 if (_settings_client.sound.disaster) SndPlayTileFx(SND_12_EXPLOSION, i->location.tile);
469 }
470 } else if (v->state == 0) {
471 int x = v->x_pos + ((leave_at_top ? -15 : 15) * TILE_SIZE);
472 int y = v->y_pos;
473
474 if ((uint)x > Map::MaxX() * TILE_SIZE - 1) return true;
475
476 TileIndex tile = TileVirtXY(x, y);
477 if (!IsTileType(tile, MP_INDUSTRY)) return true;
478
479 IndustryID ind = GetIndustryIndex(tile);
480 v->dest_tile = TileIndex{ind.base()};
481
482 if (GetIndustrySpec(Industry::Get(ind)->type)->behaviour.Test(behaviour)) {
483 v->state = 1;
484 v->age = CalendarTime::MIN_DATE;
485 }
486 }
487
488 return true;
489}
490
492 static bool DisasterTick_Airplane(DisasterVehicle *v)
493{
494 return DisasterTick_Aircraft(v, SPR_F_15_FIRING, true, STR_NEWS_DISASTER_AIRPLANE_OIL_REFINERY, IndustryBehaviour::AirplaneAttacks);
495}
496
498 static bool DisasterTick_Helicopter(DisasterVehicle *v)
499{
500 return DisasterTick_Aircraft(v, SPR_AH_64A_FIRING, false, STR_NEWS_DISASTER_HELICOPTER_FACTORY, IndustryBehaviour::ChopperAttacks);
501}
502
504 static bool DisasterTick_Helicopter_Rotors(DisasterVehicle *v)
505{
506 v->tick_counter++;
507 if (HasBit(v->tick_counter, 0)) return true;
508
509 SpriteID &cur_image = v->sprite_cache.sprite_seq.seq[0].sprite;
510 if (++cur_image > SPR_ROTOR_MOVING_3) cur_image = SPR_ROTOR_MOVING_1;
511
512 v->UpdatePositionAndViewport();
513
514 return true;
515}
516
523 static bool DisasterTick_Big_Ufo(DisasterVehicle *v)
524{
525 v->tick_counter++;
526
527 if (v->state == 1) {
528 int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
529 int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
530 if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
531 v->direction = GetDirectionTowards(v, x, y);
532
533 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
534 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
535 return true;
536 }
537
538 if (!IsValidTile(v->dest_tile)) {
539 /* Make sure we don't land outside the map. */
540 delete v;
541 return false;
542 }
543
544 int z = GetSlopePixelZ(v->x_pos, v->y_pos);
545 if (z < v->z_pos) {
546 v->UpdatePosition(v->x_pos, v->y_pos, v->z_pos - 1);
547 return true;
548 }
549
550 v->state = 2;
551
552 for (Vehicle *target : Vehicle::Iterate()) {
553 if (target->IsGroundVehicle()) {
554 if (Delta(target->x_pos, v->x_pos) + Delta(target->y_pos, v->y_pos) <= 12 * (int)TILE_SIZE) {
555 target->breakdown_ctr = 5;
556 target->breakdown_delay = 0xF0;
557 }
558 }
559 }
560
561 Town *t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
562 AddTileNewsItem(GetEncodedString(STR_NEWS_DISASTER_BIG_UFO, t->index), NewsType::Accident, v->tile);
563
564 if (!Vehicle::CanAllocateItem(2)) {
565 delete v;
566 return false;
567 }
568 DisasterVehicle *u = new DisasterVehicle(-6 * (int)TILE_SIZE, v->y_pos, DIR_SW, ST_BIG_UFO_DESTROYER, v->index);
569 DisasterVehicle *w = new DisasterVehicle(-6 * (int)TILE_SIZE, v->y_pos, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
570 u->SetNext(w);
571 } else if (v->state == 0) {
572 int x = TileX(v->dest_tile) * TILE_SIZE;
573 int y = TileY(v->dest_tile) * TILE_SIZE;
574 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= (int)TILE_SIZE) {
575 v->direction = GetDirectionTowards(v, x, y);
576 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
577 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
578 return true;
579 }
580
581 if (++v->age < 6) {
582 v->dest_tile = RandomTile();
583 return true;
584 }
585 v->state = 1;
586
587 const auto is_valid_target = [](const Train *t) {
588 return t->IsFrontEngine() // Only the engines
589 && Company::IsHumanID(t->owner) // Don't break AIs
590 && IsPlainRailTile(t->tile) // No tunnels
591 && !t->vehstatus.Test(VehState::Crashed); // Not crashed
592 };
593
594 uint n = 0; // Total number of targetable trains.
595 for (const Train *t : Train::Iterate()) {
596 if (is_valid_target(t)) n++;
597 }
598
599 if (n == 0) {
600 /* If there are no targetable trains, destroy the UFO. */
601 delete v;
602 return false;
603 }
604
605 n = RandomRange(n); // Choose one of them.
606 for (const Train *t : Train::Iterate()) {
607 /* Find (n+1)-th train. */
608 if (is_valid_target(t) && (n-- == 0)) {
609 /* Target it. */
610 v->dest_tile = t->tile;
611 v->age = CalendarTime::MIN_DATE;
612 break;
613 }
614 }
615 }
616
617 return true;
618}
619
624 static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle *v)
625{
626 v->tick_counter++;
627
628 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
629 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
630
631 if (gp.x > (int)(Map::SizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1) {
632 delete v;
633 return false;
634 }
635
636 if (v->state == 0) {
637 Vehicle *u = Vehicle::Get(v->big_ufo_destroyer_target);
638 if (Delta(v->x_pos, u->x_pos) > (int)TILE_SIZE) return true;
639 v->state = 1;
640
641 CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
642 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, u);
643
644 delete u;
645
646 for (int i = 0; i != 80; i++) {
647 uint32_t r = Random();
648 CreateEffectVehicleAbove(
649 GB(r, 0, 6) + v->x_pos - 32,
650 GB(r, 5, 6) + v->y_pos - 32,
651 0,
652 EV_EXPLOSION_SMALL);
653 }
654
655 for (int dy = -3; dy < 3; dy++) {
656 for (int dx = -3; dx < 3; dx++) {
657 TileIndex tile = TileAddWrap(v->tile, dx, dy);
658 if (tile != INVALID_TILE) DisasterClearSquare(tile);
659 }
660 }
661 }
662
663 return true;
664}
665
670 static bool DisasterTick_Submarine(DisasterVehicle *v)
671{
672 v->tick_counter++;
673
674 if (++v->age > 8880) {
675 delete v;
676 return false;
677 }
678
679 if (!HasBit(v->tick_counter, 0)) return true;
680
681 TileIndex tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
682 if (IsValidTile(tile)) {
683 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
684 if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
685 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
686 v->UpdatePosition(gp.x, gp.y, v->z_pos);
687 return true;
688 }
689 }
690
691 v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
692
693 return true;
694}
695
696
697static bool DisasterTick_NULL(DisasterVehicle *)
698{
699 return true;
700}
701
702typedef bool DisasterVehicleTickProc(DisasterVehicle *v);
703
704static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
705 DisasterTick_Zeppeliner, DisasterTick_NULL,
706 DisasterTick_Ufo, DisasterTick_NULL,
707 DisasterTick_Airplane, DisasterTick_NULL,
708 DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
709 DisasterTick_Big_Ufo, DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
710 DisasterTick_NULL,
711 DisasterTick_Submarine,
712 DisasterTick_Submarine,
713};
714
715
716 bool DisasterVehicle::Tick()
717{
718 return _disastervehicle_tick_procs[this->subtype](this);
719}
720
721typedef void DisasterInitProc();
722
723
728 static void Disaster_Zeppeliner_Init()
729{
730 if (!Vehicle::CanAllocateItem(2)) return;
731
732 /* Pick a random place, unless we find an airport */
733 int x = TileX(RandomTile()) * TILE_SIZE + TILE_SIZE / 2;
734
735 for (const Station *st : Station::Iterate()) {
736 if (st->airport.tile != INVALID_TILE && (st->airport.type == AT_SMALL || st->airport.type == AT_LARGE)) {
737 x = (TileX(st->airport.tile) + 2) * TILE_SIZE;
738 break;
739 }
740 }
741
742 DisasterVehicle *v = new DisasterVehicle(x, 0, DIR_SE, ST_ZEPPELINER);
743 /* Allocate shadow */
744 DisasterVehicle *u = new DisasterVehicle(x, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
745 v->SetNext(u);
746}
747
748
753 static void Disaster_Small_Ufo_Init()
754{
755 if (!Vehicle::CanAllocateItem(2)) return;
756
757 int x = TileX(RandomTile()) * TILE_SIZE + TILE_SIZE / 2;
758 DisasterVehicle *v = new DisasterVehicle(x, 0, DIR_SE, ST_SMALL_UFO);
759 v->dest_tile = TileXY(Map::SizeX() / 2, Map::SizeY() / 2);
760
761 /* Allocate shadow */
762 DisasterVehicle *u = new DisasterVehicle(x, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
763 v->SetNext(u);
764}
765
766
767/* Combat airplane which destroys an oil refinery */
768static void Disaster_Airplane_Init()
769{
770 if (!Vehicle::CanAllocateItem(2)) return;
771
772 Industry *found = nullptr;
773
774 for (Industry *i : Industry::Iterate()) {
775 if (GetIndustrySpec(i->type)->behaviour.Test(IndustryBehaviour::AirplaneAttacks) &&
776 (found == nullptr || Chance16(1, 2))) {
777 found = i;
778 }
779 }
780
781 if (found == nullptr) return;
782
783 /* Start from the bottom (south side) of the map */
784 int x = (Map::SizeX() + 9) * TILE_SIZE - 1;
785 int y = TileY(found->location.tile) * TILE_SIZE + 37;
786
787 DisasterVehicle *v = new DisasterVehicle(x, y, DIR_NE, ST_AIRPLANE);
788 DisasterVehicle *u = new DisasterVehicle(x, y, DIR_NE, ST_AIRPLANE_SHADOW);
789 v->SetNext(u);
790}
791
792
794 static void Disaster_Helicopter_Init()
795{
796 if (!Vehicle::CanAllocateItem(3)) return;
797
798 Industry *found = nullptr;
799
800 for (Industry *i : Industry::Iterate()) {
801 if (GetIndustrySpec(i->type)->behaviour.Test(IndustryBehaviour::ChopperAttacks) &&
802 (found == nullptr || Chance16(1, 2))) {
803 found = i;
804 }
805 }
806
807 if (found == nullptr) return;
808
809 int x = -16 * (int)TILE_SIZE;
810 int y = TileY(found->location.tile) * TILE_SIZE + 37;
811
812 DisasterVehicle *v = new DisasterVehicle(x, y, DIR_SW, ST_HELICOPTER);
813 DisasterVehicle *u = new DisasterVehicle(x, y, DIR_SW, ST_HELICOPTER_SHADOW);
814 v->SetNext(u);
815
816 DisasterVehicle *w = new DisasterVehicle(x, y, DIR_SW, ST_HELICOPTER_ROTORS);
817 u->SetNext(w);
818}
819
820
821/* Big Ufo which lands on a piece of rail and will consequently be shot
822 * down by a combat airplane, destroying the surroundings */
823static void Disaster_Big_Ufo_Init()
824{
825 if (!Vehicle::CanAllocateItem(2)) return;
826
827 int x = TileX(RandomTile()) * TILE_SIZE + TILE_SIZE / 2;
828 int y = Map::MaxX() * TILE_SIZE - 1;
829
830 DisasterVehicle *v = new DisasterVehicle(x, y, DIR_NW, ST_BIG_UFO);
831 v->dest_tile = TileXY(Map::SizeX() / 2, Map::SizeY() / 2);
832
833 /* Allocate shadow */
834 DisasterVehicle *u = new DisasterVehicle(x, y, DIR_NW, ST_BIG_UFO_SHADOW);
835 v->SetNext(u);
836}
837
838
839static void Disaster_Submarine_Init(DisasterSubType subtype)
840{
841 if (!Vehicle::CanAllocateItem()) return;
842
843 int y;
844 Direction dir;
845 uint32_t r = Random();
846 int x = TileX(RandomTileSeed(r)) * TILE_SIZE + TILE_SIZE / 2;
847
848 if (HasBit(r, 31)) {
849 y = Map::MaxY() * TILE_SIZE - TILE_SIZE / 2 - 1;
850 dir = DIR_NW;
851 } else {
852 y = TILE_SIZE / 2;
853 if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
854 dir = DIR_SE;
855 }
856 if (!IsWaterTile(TileVirtXY(x, y))) return;
857
858 new DisasterVehicle(x, y, dir, subtype);
859}
860
861/* Curious submarine #1, just floats around */
862static void Disaster_Small_Submarine_Init()
863{
864 Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
865}
866
867
868/* Curious submarine #2, just floats around */
869static void Disaster_Big_Submarine_Init()
870{
871 Disaster_Submarine_Init(ST_BIG_SUBMARINE);
872}
873
874
879 static void Disaster_CoalMine_Init()
880{
881 int index = GB(Random(), 0, 4);
882 uint m;
883
884 for (m = 0; m < 15; m++) {
885 for (const Industry *i : Industry::Iterate()) {
886 if (GetIndustrySpec(i->type)->behaviour.Test(IndustryBehaviour::CanSubsidence) && --index < 0) {
887 AddTileNewsItem(GetEncodedString(STR_NEWS_DISASTER_COAL_MINE_SUBSIDENCE, i->town->index), NewsType::Accident, i->location.tile + TileDiffXY(1, 1)); // keep the news, even when the mine closes
888
889 {
890 TileIndex tile = i->location.tile;
891 TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
892
893 for (uint n = 0; n < 30; n++) {
894 DisasterClearSquare(tile);
895 tile += step;
896 if (!IsValidTile(tile)) break;
897 }
898 }
899 return;
900 }
901 }
902 }
903}
904
905 struct Disaster {
906 DisasterInitProc *init_proc;
907 TimerGameCalendar::Year min_year;
908 TimerGameCalendar::Year max_year;
909};
910
911static const Disaster _disasters[] = {
912 {Disaster_Zeppeliner_Init, TimerGameCalendar::Year{1930}, TimerGameCalendar::Year{1955}}, // zeppeliner
913 {Disaster_Small_Ufo_Init, TimerGameCalendar::Year{1940}, TimerGameCalendar::Year{1970}}, // ufo {small}
914 {Disaster_Airplane_Init, TimerGameCalendar::Year{1960}, TimerGameCalendar::Year{1990}}, // airplane
915 {Disaster_Helicopter_Init, TimerGameCalendar::Year{1970}, TimerGameCalendar::Year{2000}}, // helicopter
916 {Disaster_Big_Ufo_Init, TimerGameCalendar::Year{2000}, TimerGameCalendar::Year{2100}}, // ufo {big}
917 {Disaster_Small_Submarine_Init, TimerGameCalendar::Year{1940}, TimerGameCalendar::Year{1965}}, // submarine {small}
918 {Disaster_Big_Submarine_Init, TimerGameCalendar::Year{1975}, TimerGameCalendar::Year{2010}}, // submarine {big}
919 {Disaster_CoalMine_Init, TimerGameCalendar::Year{1950}, TimerGameCalendar::Year{1985}}, // coalmine
920};
921
922static void DoDisaster()
923{
924 std::vector<DisasterInitProc *> available_disasters;
925
926 for (auto &disaster : _disasters) {
927 if (TimerGameCalendar::year >= disaster.min_year && TimerGameCalendar::year < disaster.max_year) {
928 available_disasters.push_back(disaster.init_proc);
929 }
930 }
931
932 if (available_disasters.empty()) return;
933
934 available_disasters[RandomRange(static_cast<uint32_t>(available_disasters.size()))]();
935}
936
937
938static void ResetDisasterDelay()
939{
940 _disaster_delay = GB(Random(), 0, 9) + 730;
941}
942
943static const IntervalTimer<TimerGameEconomy> _economy_disaster_daily({TimerGameEconomy::DAY, TimerGameEconomy::Priority::DISASTER}, [](auto)
944{
945 if (--_disaster_delay != 0) return;
946
947 ResetDisasterDelay();
948
949 if (_settings_game.difficulty.disasters != 0) DoDisaster();
950});
951
952void StartupDisasters()
953{
954 ResetDisasterDelay();
955}
956
962 void ReleaseDisastersTargetingIndustry(IndustryID i)
963{
964 for (DisasterVehicle *v : DisasterVehicle::Iterate()) {
965 /* primary disaster vehicles that have chosen target */
966 if (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER) {
967 /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
968 if (v->state > 0 && v->dest_tile == i.base()) v->state = 3;
969 }
970 }
971}
972
977 void ReleaseDisasterVehicle(VehicleID vehicle)
978{
979 DisasterVehicle *v = DisasterVehicle::GetIfValid(vehicle);
980 if (v == nullptr) return;
981
982 /* primary disaster vehicles that have chosen target */
983 assert(v->subtype == ST_SMALL_UFO);
984 assert(v->state != 0);
985
986 /* Revert to target-searching */
987 v->state = 0;
988 v->dest_tile = RandomTile();
989 GetAircraftFlightLevelBounds(v, &v->z_pos, nullptr);
990 v->age = CalendarTime::MIN_DATE;
991}
992
993 void DisasterVehicle::UpdateDeltaXY()
994{
995 this->bounds = {{-1, -1, 0}, {2, 2, 5}, {}};
996}
ai.hpp
Base functions for all AIs.
aircraft.h
Base for aircraft.
GetAircraftFlightLevelBounds
void GetAircraftFlightLevelBounds(const Vehicle *v, int *min, int *max)
Get the 'flight level' bounds, in pixels from 'z_pos' 0 for a particular vehicle for normal flight si...
Definition aircraft_cmd.cpp:726
ROTOR_Z_OFFSET
static const int ROTOR_Z_OFFSET
Z Offset between helicopter- and rotorsprite.
Definition aircraft.h:48
AirportBlock::Zeppeliner
@ Zeppeliner
Block for the zeppeliner disaster vehicle.
AT_SMALL
@ AT_SMALL
Small airport.
Definition airport.h:29
AT_LARGE
@ AT_LARGE
Large airport.
Definition airport.h:30
backup_type.hpp
Class for backupping variables and making sure they are restored later.
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
AI::NewEvent
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
Definition ai_core.cpp:235
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Reset
constexpr Timpl & Reset()
Reset all bits.
Definition base_bitset_type.hpp:83
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
Game::NewEvent
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition game_core.cpp:136
IntervalTimer
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
TimerGameCalendar::year
static Year year
Current year, starting at 0.
Definition timer_game_calendar.h:31
TimerGameConst< struct Calendar >::MIN_DATE
static constexpr TimerGame< struct Calendar >::Date MIN_DATE
The date on January 1, year 0.
Definition timer_game_common.h:190
command_func.h
Functions related to commands.
DoCommandFlag::Execute
@ Execute
execute the given command
company_base.h
Definition of stuff that is very close to a company, like the company struct itself.
_current_company
CompanyID _current_company
Company currently doing an action.
Definition company_cmd.cpp:56
company_func.h
Functions related to companies.
OWNER_NONE
static constexpr Owner OWNER_NONE
The tile has no ownership.
Definition company_type.h:26
OWNER_WATER
static constexpr Owner OWNER_WATER
The tile/execution is done by "water".
Definition company_type.h:27
ChangeDir
Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
Definition direction_func.h:104
DirToDiagDir
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
Definition direction_func.h:166
DIRDIFF_90LEFT
@ DIRDIFF_90LEFT
Angle of 90 degrees left.
Definition direction_type.h:68
DIRDIFF_90RIGHT
@ DIRDIFF_90RIGHT
Angle of 90 degrees right.
Definition direction_type.h:66
Direction
Direction
Defines the 8 directions on the map.
Definition direction_type.h:24
DIR_SW
@ DIR_SW
Southwest.
Definition direction_type.h:31
DIR_NW
@ DIR_NW
Northwest.
Definition direction_type.h:33
DIR_SE
@ DIR_SE
Southeast.
Definition direction_type.h:29
DIR_NE
@ DIR_NE
Northeast.
Definition direction_type.h:27
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
DisasterTick_Big_Ufo_Destroyer
static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle *v)
Skyranger destroying (Big) Ufo handling, v->state states: 0: Home in on landed Ufo and shoot it down.
Definition disaster_vehicle.cpp:624
Disaster_Small_Ufo_Init
static void Disaster_Small_Ufo_Init()
Ufo which flies around aimlessly from the middle of the map a bit until it locates a road vehicle whi...
Definition disaster_vehicle.cpp:753
DisasterTick_Big_Ufo
static bool DisasterTick_Big_Ufo(DisasterVehicle *v)
(Big) Ufo handling, v->state states: 0: Fly around to the middle of the map, then randomly for a whil...
Definition disaster_vehicle.cpp:523
Disaster_Helicopter_Init
static void Disaster_Helicopter_Init()
Combat helicopter that destroys a factory.
Definition disaster_vehicle.cpp:794
DisasterTick_Zeppeliner
static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
Zeppeliner handling, v->state states: 0: Zeppeliner initialization has found an airport,...
Definition disaster_vehicle.cpp:224
DisasterTick_Helicopter_Rotors
static bool DisasterTick_Helicopter_Rotors(DisasterVehicle *v)
Helicopter rotor blades; keep these spinning.
Definition disaster_vehicle.cpp:504
DisasterTick_Ufo
static bool DisasterTick_Ufo(DisasterVehicle *v)
(Small) Ufo handling, v->state states: 0: Fly around to the middle of the map, then randomly,...
Definition disaster_vehicle.cpp:315
DisasterTick_Airplane
static bool DisasterTick_Airplane(DisasterVehicle *v)
Airplane handling.
Definition disaster_vehicle.cpp:492
Disaster_Zeppeliner_Init
static void Disaster_Zeppeliner_Init()
Zeppeliner which crashes on an airport if one found, otherwise crashes on a random tile.
Definition disaster_vehicle.cpp:728
_disaster_delay
uint16_t _disaster_delay
Delay counter for considering the next disaster.
Definition disaster_vehicle.cpp:57
ReleaseDisasterVehicle
void ReleaseDisasterVehicle(VehicleID vehicle)
Notify disasters that we are about to delete a vehicle.
Definition disaster_vehicle.cpp:977
DisasterTick_Aircraft
static bool DisasterTick_Aircraft(DisasterVehicle *v, uint16_t image_override, bool leave_at_top, StringID news_message, IndustryBehaviour behaviour)
Aircraft handling, v->state states: 0: Fly towards the targeted industry 1: If within 15 tiles,...
Definition disaster_vehicle.cpp:431
DisasterTick_Submarine
static bool DisasterTick_Submarine(DisasterVehicle *v)
Submarine, v->state states: Unused, just float around aimlessly and pop up at different places,...
Definition disaster_vehicle.cpp:670
DisasterTick_Helicopter
static bool DisasterTick_Helicopter(DisasterVehicle *v)
Helicopter handling.
Definition disaster_vehicle.cpp:498
ReleaseDisastersTargetingIndustry
void ReleaseDisastersTargetingIndustry(IndustryID i)
Marks all disasters targeting this industry in such a way they won't call Industry::Get(v->dest_tile)...
Definition disaster_vehicle.cpp:962
Disaster_CoalMine_Init
static void Disaster_CoalMine_Init()
Coal mine catastrophe, destroys a stretch of 30 tiles of land in a certain direction.
Definition disaster_vehicle.cpp:879
disaster_vehicle.h
All disaster vehicles.
DisasterSubType
DisasterSubType
Different sub types of disaster vehicles.
Definition disaster_vehicle.h:16
ST_ZEPPELINER_SHADOW
@ ST_ZEPPELINER_SHADOW
Shadow of the zeppelin.
Definition disaster_vehicle.h:18
ST_HELICOPTER_SHADOW
@ ST_HELICOPTER_SHADOW
Shadow of helicopter.
Definition disaster_vehicle.h:24
ST_SMALL_UFO_SHADOW
@ ST_SMALL_UFO_SHADOW
Shadow of small UFO.
Definition disaster_vehicle.h:20
ST_SMALL_UFO
@ ST_SMALL_UFO
Small UFO, tries to find a road vehicle to destroy.
Definition disaster_vehicle.h:19
ST_BIG_UFO
@ ST_BIG_UFO
Big UFO, finds a piece of railroad to "park" on.
Definition disaster_vehicle.h:26
ST_BIG_UFO_SHADOW
@ ST_BIG_UFO_SHADOW
Shadow of the big UFO.
Definition disaster_vehicle.h:27
ST_ZEPPELINER
@ ST_ZEPPELINER
Zeppelin, crashes at airports.
Definition disaster_vehicle.h:17
ST_BIG_UFO_DESTROYER_SHADOW
@ ST_BIG_UFO_DESTROYER_SHADOW
Shadow of the aircraft.
Definition disaster_vehicle.h:29
ST_BIG_UFO_DESTROYER
@ ST_BIG_UFO_DESTROYER
Aircraft the will bomb the big UFO.
Definition disaster_vehicle.h:28
ST_BIG_SUBMARINE
@ ST_BIG_SUBMARINE
Big submarine, pops up in the oceans but doesn't do anything.
Definition disaster_vehicle.h:31
ST_SMALL_SUBMARINE
@ ST_SMALL_SUBMARINE
Small submarine, pops up in the oceans but doesn't do anything.
Definition disaster_vehicle.h:30
ST_HELICOPTER
@ ST_HELICOPTER
Helicopter destroying a factory.
Definition disaster_vehicle.h:23
ST_AIRPLANE
@ ST_AIRPLANE
Airplane destroying an oil refinery.
Definition disaster_vehicle.h:21
ST_HELICOPTER_ROTORS
@ ST_HELICOPTER_ROTORS
Rotors of helicopter.
Definition disaster_vehicle.h:25
ST_AIRPLANE_SHADOW
@ ST_AIRPLANE_SHADOW
Shadow of airplane.
Definition disaster_vehicle.h:22
CreateEffectVehicleRel
EffectVehicle * CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular vehicle.
Definition effectvehicle.cpp:610
CreateEffectVehicleAbove
EffectVehicle * CreateEffectVehicleAbove(int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular location.
Definition effectvehicle.cpp:594
effectvehicle_func.h
Functions related to effect vehicles.
EV_CRASH_SMOKE
@ EV_CRASH_SMOKE
Smoke of disasters.
Definition effectvehicle_func.h:21
EV_EXPLOSION_SMALL
@ EV_EXPLOSION_SMALL
Various explosions.
Definition effectvehicle_func.h:24
EV_EXPLOSION_LARGE
@ EV_EXPLOSION_LARGE
Various explosions.
Definition effectvehicle_func.h:22
game.hpp
Base functions for all Games.
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
MarkTileDirtyByTile
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition viewport.cpp:2128
industry.h
Base of all industries.
GetIndustrySpec
const IndustrySpec * GetIndustrySpec(IndustryType thistype)
Accessor for array _industry_specs.
Definition industry_cmd.cpp:126
ResetIndustryConstructionStage
void ResetIndustryConstructionStage(Tile tile)
Reset the construction stage counter of the industry, as well as the completion bit.
Definition industry_map.h:187
GetIndustryIndex
IndustryID GetIndustryIndex(Tile t)
Get the industry ID of the given tile.
Definition industry_map.h:63
IndustryBehaviour
IndustryBehaviour
Various industry behaviours mostly to represent original TTD specialities.
Definition industrytype.h:59
IndustryBehaviour::AirplaneAttacks
@ AirplaneAttacks
can be exploded by a military airplane (oil refinery)
IndustryBehaviour::CanSubsidence
@ CanSubsidence
can cause a subsidence (coal mine, shaft that collapses)
IndustryBehaviour::ChopperAttacks
@ ChopperAttacks
can be exploded by a military helicopter (factory)
GetTileTrackStatus
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Definition landscape.cpp:559
GetSlopePixelZ
int GetSlopePixelZ(int x, int y, bool ground_vehicle)
Return world Z coordinate of a given point of a tile.
Definition landscape.cpp:306
landscape_cmd.h
Command definitions related to landscape (slopes etc.).
BorderFlag::Random
@ Random
Randomise borders.
TileAddWrap
TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
This function checks if we add addx/addy to tile, if we do wrap around the edges.
Definition map.cpp:109
TileVirtXY
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition map_func.h:416
RandomTileSeed
TileIndex RandomTileSeed(uint32_t r)
Get a random tile out of a given seed.
Definition map_func.h:653
TileDiffXY
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition map_func.h:401
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:385
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:437
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:427
RandomTile
#define RandomTile()
Get a valid random tile.
Definition map_func.h:664
TileOffsByDiagDir
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:582
TileIndexDiff
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
Delta
constexpr T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition math_func.hpp:234
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
news_func.h
Functions related to news.
NewsType::Accident
@ Accident
An accident or disaster has occurred.
IsRailDepot
static bool IsRailDepot(Tile t)
Is this rail tile a rail depot?
Definition rail_map.h:95
IsPlainRailTile
static bool IsPlainRailTile(Tile t)
Checks whether the tile is a rail tile or rail tile with signals.
Definition rail_map.h:60
random_func.hpp
Pseudo random number generator.
RandomRange
uint32_t RandomRange(uint32_t limit, const std::source_location location=std::source_location::current())
Pick a random number between 0 and limit - 1, inclusive.
Definition random_func.hpp:88
Chance16
bool Chance16(const uint32_t a, const uint32_t b, const std::source_location location=std::source_location::current())
Flips a coin with given probability.
Definition random_func.hpp:134
roadveh.h
Road vehicle states.
safeguards.h
A number of safeguards to prevent using unsafe methods.
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
UpdateSignalsInBuffer
void UpdateSignalsInBuffer()
Update signals in buffer Called from 'outside'.
Definition signal.cpp:573
sound_func.h
Functions related to sound.
SND_12_EXPLOSION
@ SND_12_EXPLOSION
16 == 0x10 Destruction, crashes, disasters, ...
Definition sound_type.h:63
station_base.h
Base classes/functions for stations.
IsAirportTile
bool IsAirportTile(Tile t)
Is this tile a station tile and an airport tile?
Definition station_map.h:167
GetStationIndex
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:28
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
GetEncodedString
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
strings_func.h
Functions related to OTTD's strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
Airport::blocks
AirportBlocks blocks
stores which blocks on the airport are taken. was 16 bit earlier on, then 32
Definition station_base.h:354
Backup
Class to backup a specific variable and restore it later.
Definition backup_type.hpp:21
Backup::Restore
void Restore()
Restore the variable.
Definition backup_type.hpp:110
BaseStation::GetByTile
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
Definition base_station_base.h:160
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition vehicle_type.h:51
ClientSettings::sound
SoundSettings sound
sound effect settings
Definition settings_type.h:653
CommandHelper
Definition command_func.h:83
Company
Definition company_base.h:127
Company::IsHumanID
static bool IsHumanID(auto index)
Is this company a company not controlled by a NoAI program?
Definition company_base.h:178
ConstructionSettings::freeform_edges
bool freeform_edges
allow terraforming the tiles at the map edges
Definition settings_type.h:423
DifficultySettings::disasters
bool disasters
are disasters enabled
Definition settings_type.h:141
DisasterVehicle
Disasters, like submarines, skyrangers and their shadows, belong to this class.
Definition disaster_vehicle.h:37
DisasterVehicle::UpdateDeltaXY
void UpdateDeltaXY() override
Updates the x and y offsets and the size of the sprite used for this vehicle.
Definition disaster_vehicle.cpp:993
DisasterVehicle::image_override
SpriteID image_override
Override for the default disaster vehicle sprite.
Definition disaster_vehicle.h:38
DisasterVehicle::UpdatePosition
void UpdatePosition(int x, int y, int z)
Update the position of the vehicle.
Definition disaster_vehicle.cpp:183
DisasterVehicle::DisasterVehicle
DisasterVehicle()
For use by saveload.
Definition disaster_vehicle.h:44
DisasterVehicle::big_ufo_destroyer_target
VehicleID big_ufo_destroyer_target
The big UFO that this destroyer is supposed to bomb.
Definition disaster_vehicle.h:39
DisasterVehicle::Tick
bool Tick() override
Calls the tick handler of the vehicle.
Definition disaster_vehicle.cpp:716
DisasterVehicle::state
uint16_t state
Action stage of the disaster vehicle.
Definition disaster_vehicle.h:41
Disaster
Definition disaster_vehicle.cpp:905
Disaster::init_proc
DisasterInitProc * init_proc
The init function for this disaster.
Definition disaster_vehicle.cpp:906
Disaster::min_year
TimerGameCalendar::Year min_year
The first year this disaster will occur.
Definition disaster_vehicle.cpp:907
Disaster::max_year
TimerGameCalendar::Year max_year
The last year this disaster will occur.
Definition disaster_vehicle.cpp:908
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition settings_type.h:635
GameSettings::difficulty
DifficultySettings difficulty
settings related to the difficulty
Definition settings_type.h:633
GetNewVehiclePosResult
Position information of a vehicle after it moved.
Definition vehicle_func.h:214
GetNewVehiclePosResult::y
int y
x and y position of the vehicle after moving
Definition vehicle_func.h:215
IndustrySpec::behaviour
IndustryBehaviours behaviour
How this industry will behave, and how others entities can use it.
Definition industrytype.h:121
Industry
Defines the internal data of a functional industry.
Definition industry.h:62
Industry::type
IndustryType type
type of industry.
Definition industry.h:115
Industry::town
Town * town
Nearest town.
Definition industry.h:107
Industry::location
TileArea location
Location of the industry.
Definition industry.h:106
Industry::TileBelongsToIndustry
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
Definition industry.h:148
Map
Size related data of the map.
Definition map_func.h:206
Map::SizeX
static uint SizeX()
Get the size of the map along the X.
Definition map_func.h:272
Map::SizeY
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:281
Map::MaxY
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition map_func.h:308
Map::MaxX
static uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition map_func.h:299
MutableSpriteCache::sprite_seq
VehicleSpriteSeq sprite_seq
Vehicle appearance.
Definition vehicle_base.h:162
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition tilearea_type.h:19
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF >
Pool::PoolItem<&_company_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition pool_type.hpp:436
Pool::PoolItem<&_industry_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
Pool::PoolItem<&_vehicle_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition pool_type.hpp:357
Pool::PoolItem<&_vehicle_pool >::GetIfValid
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Definition pool_type.hpp:398
RoadVehicle
Buses, trucks and trams belong to this class.
Definition roadveh.h:98
RoadVehicle::Crash
uint Crash(bool flooded=false) override
Common code executed for crashed ground vehicles.
Definition roadveh_cmd.cpp:540
RoadVehicle::crashed_ctr
uint16_t crashed_ctr
Animation counter when the vehicle has crashed.
Definition roadveh.h:105
RoadVehicle::disaster_vehicle
VehicleID disaster_vehicle
NOSAVE: Disaster vehicle targetting this vehicle.
Definition roadveh.h:109
SoundSettings::disaster
bool disaster
Play disaster and accident sounds.
Definition settings_type.h:277
SpecializedStation< Station, false >::Iterate
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
Definition base_station_base.h:299
SpecializedVehicle
Class defining several overloaded accessors so we don't have to cast vehicle types that often.
Definition vehicle_base.h:1014
SpecializedVehicle< DisasterVehicle, VEH_DISASTER >::Next
DisasterVehicle * Next() const
Get next vehicle in the chain.
Definition vehicle_base.h:1049
SpecializedVehicle::Iterate
static Pool::IterateWrapper< T > Iterate(size_t from=0)
Returns an iterable ensemble of all valid vehicles of type T.
Definition vehicle_base.h:1203
SpecializedVehicle::Get
static T * Get(auto index)
Gets vehicle with given index.
Definition vehicle_base.h:1115
Station
Station data structure.
Definition station_base.h:500
Station::airport
Airport airport
Tile area the airport covers.
Definition station_base.h:514
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Town
Town data structure.
Definition town.h:63
Train
'Train' is either a loco or a wagon.
Definition train.h:91
VehicleSpriteSeq::Set
void Set(SpriteID sprite)
Assign a single sprite to the sequence.
Definition vehicle_base.h:130
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
Vehicle::z_pos
int32_t z_pos
z coordinate.
Definition vehicle_base.h:267
Vehicle::direction
Direction direction
facing
Definition vehicle_base.h:268
Vehicle::subtype
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
Definition vehicle_base.h:310
Vehicle::breakdown_ctr
uint8_t breakdown_ctr
Counter for managing breakdown events.
Definition vehicle_base.h:260
Vehicle::breakdown_delay
uint8_t breakdown_delay
Counter for managing breakdown length.
Definition vehicle_base.h:261
Vehicle::vehstatus
VehStates vehstatus
Status.
Definition vehicle_base.h:309
Vehicle::y_pos
int32_t y_pos
y coordinate.
Definition vehicle_base.h:266
Vehicle::x_pos
int32_t x_pos
x coordinate.
Definition vehicle_base.h:265
Vehicle::bounds
SpriteBounds bounds
Bounding box of vehicle.
Definition vehicle_base.h:277
Vehicle::IsFrontEngine
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Definition vehicle_base.h:913
Vehicle::age
TimerGameCalendar::Date age
Age in calendar days.
Definition vehicle_base.h:253
Vehicle::SetNext
void SetNext(Vehicle *next)
Set the next vehicle of this vehicle.
Definition vehicle.cpp:2927
Vehicle::sprite_cache
MutableSpriteCache sprite_cache
Cache of sprites and values related to recalculating them, see MutableSpriteCache.
Definition vehicle_base.h:323
Vehicle::UpdatePositionAndViewport
void UpdatePositionAndViewport()
Update the position of the vehicle, and update the viewport.
Definition vehicle.cpp:1745
Vehicle::tick_counter
uint8_t tick_counter
Increased by one for each tick.
Definition vehicle_base.h:305
Vehicle::tile
TileIndex tile
Current tile index.
Definition vehicle_base.h:225
Vehicle::dest_tile
TileIndex dest_tile
Heading for this tile.
Definition vehicle_base.h:232
Vehicle::UpdatePosition
void UpdatePosition()
Update the position of the vehicle.
Definition vehicle.cpp:1662
Vehicle::owner
Owner owner
Which company owns the vehicle?
Definition vehicle_base.h:270
IsTileType
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
GetTileOwner
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
IsValidTile
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition tile_map.h:161
GetTileType
static TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
TILE_SIZE
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
MP_TREES
@ MP_TREES
Tile got trees.
Definition tile_type.h:52
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:48
MP_HOUSE
@ MP_HOUSE
A house by a town.
Definition tile_type.h:51
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition tile_type.h:49
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition tile_type.h:56
timer.h
Definition of Interval and OneShot timers.
timer_game_economy.h
Definition of the game-economy-timer.
town.h
Base of the town class.
ClosestTownFromTile
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
Definition town_cmd.cpp:3952
TrackStatusToTrackBits
TrackBits TrackStatusToTrackBits(TrackStatus ts)
Returns the present-track-information of a TrackStatus.
Definition track_func.h:363
TrackBits
TrackBits
Allow incrementing of Track variables.
Definition track_type.h:35
TRACK_BIT_ALL
@ TRACK_BIT_ALL
All possible tracks.
Definition track_type.h:50
train.h
Base for the train class.
TRANSPORT_WATER
@ TRANSPORT_WATER
Transport over water.
Definition transport_type.h:27
EnsureNoVehicleOnGround
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition vehicle.cpp:527
GetNewVehiclePos
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
Get position information of a vehicle when moving one pixel in the direction it is facing.
Definition vehicle.cpp:1765
VehState::Unclickable
@ Unclickable
Vehicle is not clickable by the user (shadow vehicles).
VehState::Crashed
@ Crashed
Vehicle is crashed.
VehState::Shadow
@ Shadow
Vehicle is a shadow vehicle.
VehState::Hidden
@ Hidden
Vehicle is not visible.
vehicle_func.h
Functions related to vehicles.
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition vehicle_type.h:26
viewport_func.h
Functions related to (drawing on) viewports.
IsWaterTile
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition water_map.h:192