Factor out OnClick dispatching in Window::HandleEditBoxKey
[openttd/fttd.git] / src / disaster_vehicle.cpp
blob849a3feb4d28953e9964866487795f47b2082bae
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /**
11 * @file disaster_vehicle.cpp
13 * All disaster/easter egg vehicles are handled here.
14 * The general flow of control for the disaster vehicles is as follows:
15 * <ol>
16 * <li>Initialize the disaster in a disaster specific way (eg start position,
17 * possible target, etc.) Disaster_XXX_Init() function
18 * <li>Add a subtype to a disaster, which is an index into the function array
19 * that handles the vehicle's ticks.
20 * <li>Run the disaster vehicles each tick until their target has been reached,
21 * this happens in the DisasterTick_XXX() functions. In here, a vehicle's
22 * state is kept by v->current_order.dest variable. Each achieved sub-target
23 * will increase this value, and the last one will remove the disaster itself
24 * </ol>
28 #include "stdafx.h"
30 #include "aircraft.h"
31 #include "disaster_vehicle.h"
32 #include "industry.h"
33 #include "station_base.h"
34 #include "command_func.h"
35 #include "news_func.h"
36 #include "town.h"
37 #include "company_func.h"
38 #include "strings_func.h"
39 #include "date_func.h"
40 #include "viewport_func.h"
41 #include "vehicle_func.h"
42 #include "signalbuffer.h"
43 #include "sound_func.h"
44 #include "effectvehicle_func.h"
45 #include "roadveh.h"
46 #include "ai/ai.hpp"
47 #include "game/game.hpp"
48 #include "company_base.h"
49 #include "core/random_func.hpp"
50 #include "core/backup_type.hpp"
51 #include "map/water.h"
53 #include "table/strings.h"
55 /** Delay counter for considering the next disaster. */
56 uint16 _disaster_delay;
58 static void DisasterClearSquare(TileIndex tile)
60 if (CheckVehicleOnGround (tile) != STR_NULL) return;
62 if (IsHouseTile(tile)) {
63 Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
64 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
65 cur_company.Restore();
66 } else switch (GetTileType(tile)) {
67 case TT_RAILWAY:
68 if (Company::IsHumanID(GetTileOwner(tile))) {
69 Backup<CompanyByte> cur_company(_current_company, OWNER_WATER, FILE_LINE);
70 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
71 cur_company.Restore();
73 /* update signals in buffer */
74 UpdateSignalsInBuffer();
76 break;
78 case TT_GROUND:
79 DoClearSquare(tile);
80 break;
82 default:
83 break;
87 static const SpriteID _disaster_images_1[] = {SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP};
88 static 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};
89 static 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};
90 static 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};
91 static 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};
92 static 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};
93 static 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};
94 static 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};
95 static 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};
97 static const SpriteID * const _disaster_images[] = {
98 _disaster_images_1, _disaster_images_1, ///< zeppeliner and zeppeliner shadow
99 _disaster_images_2, _disaster_images_2, ///< small ufo and small ufo shadow
100 _disaster_images_3, _disaster_images_3, ///< combat aircraft and shadow
101 _disaster_images_8, _disaster_images_8, _disaster_images_9, ///< combat helicopter, shadow and rotor
102 _disaster_images_6, _disaster_images_6, ///< big ufo and shadow
103 _disaster_images_7, _disaster_images_7, ///< skyranger and shadow
104 _disaster_images_4, _disaster_images_5, ///< small and big submarine sprites
107 void DisasterVehicle::UpdateImage()
109 SpriteID img = this->image_override;
110 if (img == 0) img = _disaster_images[this->subtype][this->direction];
111 this->sprite_seq.Set(img);
115 * Construct the disaster vehicle.
116 * @param x The X coordinate.
117 * @param y The Y coordinate.
118 * @param direction The direction the vehicle is facing.
119 * @param subtype The sub type of vehicle.
120 * @param big_ufo_destroyer_target The target for the UFO destroyer.
122 DisasterVehicle::DisasterVehicle(int x, int y, Direction direction, DisasterSubType subtype, VehicleID big_ufo_destroyer_target) :
123 SpecializedVehicleBase(), big_ufo_destroyer_target(big_ufo_destroyer_target)
125 this->vehstatus = VS_UNCLICKABLE;
127 this->x_pos = x;
128 this->y_pos = y;
129 switch (subtype) {
130 case ST_ZEPPELINER:
131 case ST_SMALL_UFO:
132 case ST_AIRPLANE:
133 case ST_HELICOPTER:
134 case ST_BIG_UFO:
135 case ST_BIG_UFO_DESTROYER:
136 this->z_pos = GetAircraftBaseFlightLevel (this);
137 break;
139 case ST_HELICOPTER_ROTORS:
140 this->z_pos = GetAircraftBaseFlightLevel (this);
141 this->z_pos += ROTOR_Z_OFFSET;
142 break;
144 case ST_SMALL_SUBMARINE:
145 case ST_BIG_SUBMARINE:
146 this->z_pos = 0;
147 break;
149 case ST_ZEPPELINER_SHADOW:
150 case ST_SMALL_UFO_SHADOW:
151 case ST_AIRPLANE_SHADOW:
152 case ST_HELICOPTER_SHADOW:
153 case ST_BIG_UFO_SHADOW:
154 case ST_BIG_UFO_DESTROYER_SHADOW:
155 this->z_pos = 0;
156 this->vehstatus |= VS_SHADOW;
157 break;
160 this->direction = direction;
161 this->tile = TileVirtXY(x, y);
162 this->subtype = subtype;
163 this->UpdateDeltaXY(INVALID_DIR);
164 this->owner = OWNER_NONE;
165 this->image_override = 0;
166 this->current_order.Clear();
168 this->UpdateImage();
169 this->UpdatePositionAndViewport();
173 * Update the position of the vehicle.
174 * @param x The new X-coordinate.
175 * @param y The new Y-coordinate.
176 * @param z The new Z-coordinate.
178 void DisasterVehicle::UpdatePosition(int x, int y, int z)
180 this->x_pos = x;
181 this->y_pos = y;
182 this->z_pos = z;
183 this->tile = TileVirtXY(x, y);
185 this->UpdateImage();
186 this->UpdatePositionAndViewport();
188 DisasterVehicle *u = this->Next();
189 if (u != NULL) {
190 int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
191 int safe_y = Clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
193 u->x_pos = x;
194 u->y_pos = y - 1 - (max(z - GetSlopePixelZ(safe_x, safe_y), 0) >> 3);
195 safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
196 u->z_pos = GetSlopePixelZ(safe_x, safe_y);
197 u->direction = this->direction;
199 u->UpdateImage();
200 u->UpdatePositionAndViewport();
202 if ((u = u->Next()) != NULL) {
203 u->x_pos = x;
204 u->y_pos = y;
205 u->z_pos = z + ROTOR_Z_OFFSET;
206 u->UpdatePositionAndViewport();
211 void DisasterVehicle::Advance (int z)
213 FullPosTile gp = GetNewVehiclePos (this);
215 this->UpdatePosition (gp.xx, gp.yy, z);
218 inline void DisasterVehicle::Advance (void)
220 this->Advance (GetAircraftFlightLevel (this));
224 * Zeppeliner handling, v->current_order.dest states:
225 * 0: Zeppeliner initialization has found a small airport, go there and crash
226 * 1: Create crash and animate falling down for extra dramatic effect
227 * 2: Create more smoke and leave debris on ground
228 * 2: Clear the runway after some time and remove crashed zeppeliner
229 * If not airport was found, only state 0 is reached until zeppeliner leaves map
231 static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
233 v->tick_counter++;
235 if (v->current_order.GetDestination() < 2) {
236 if (HasBit(v->tick_counter, 0)) return true;
238 v->Advance();
240 if (v->current_order.GetDestination() == 1) {
241 if (++v->age == 38) {
242 v->current_order.SetDestination(2);
243 v->age = 0;
246 if (GB(v->tick_counter, 0, 3) == 0) CreateEffectVehicleRel(v, 0, -17, 2, EV_CRASH_SMOKE);
248 } else if (v->current_order.GetDestination() == 0) {
249 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
250 v->current_order.SetDestination(1);
251 v->age = 0;
253 AddNewsItem<VehicleNewsItem> (STR_NEWS_DISASTER_ZEPPELIN,
254 NT_ACCIDENT, v->index, // Delete the news, when the zeppelin is gone
255 GetStationIndex (v->tile));
256 AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCrashed(GetStationIndex(v->tile)));
260 if (v->y_pos >= (int)((MapSizeY() + 9) * TILE_SIZE - 1)) {
261 delete v;
262 return false;
265 return true;
268 if (v->current_order.GetDestination() > 2) {
269 if (++v->age <= 13320) return true;
271 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
272 Station *st = Station::GetByTile(v->tile);
273 CLRBITS(st->airport.flags, RUNWAY_IN_block);
274 AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCleared(st->index));
277 v->UpdatePosition(v->x_pos, v->y_pos, GetAircraftFlightLevel(v));
278 delete v;
279 return false;
282 int x = v->x_pos;
283 int y = v->y_pos;
284 int z = GetSlopePixelZ(x, y);
285 if (z < v->z_pos) z = v->z_pos - 1;
286 v->UpdatePosition(x, y, z);
288 if (++v->age == 1) {
289 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
290 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, v);
291 v->image_override = SPR_BLIMP_CRASHING;
292 } else if (v->age == 70) {
293 v->image_override = SPR_BLIMP_CRASHED;
294 } else if (v->age <= 300) {
295 if (GB(v->tick_counter, 0, 3) == 0) {
296 uint32 r = Random();
298 CreateEffectVehicleRel(v,
299 GB(r, 0, 4) - 7,
300 GB(r, 4, 4) - 7,
301 GB(r, 8, 3) + 5,
302 EV_EXPLOSION_SMALL);
304 } else if (v->age == 350) {
305 v->current_order.SetDestination(3);
306 v->age = 0;
309 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
310 SETBITS(Station::GetByTile(v->tile)->airport.flags, RUNWAY_IN_block);
313 return true;
317 * (Small) Ufo handling, v->current_order.dest states:
318 * 0: Fly around to the middle of the map, then randomly, after a while target a road vehicle
319 * 1: Home in on a road vehicle and crash it >:)
320 * If not road vehicle was found, only state 0 is used and Ufo disappears after a while
322 static bool DisasterTick_Ufo(DisasterVehicle *v)
324 v->image_override = (HasBit(++v->tick_counter, 3)) ? SPR_UFO_SMALL_SCOUT_DARKER : SPR_UFO_SMALL_SCOUT;
326 if (v->current_order.GetDestination() == 0) {
327 /* Fly around randomly */
328 int x = TileX(v->dest_tile) * TILE_SIZE;
329 int y = TileY(v->dest_tile) * TILE_SIZE;
330 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= (int)TILE_SIZE) {
331 v->direction = GetDirectionTowards(v, x, y);
332 v->Advance();
333 return true;
335 if (++v->age < 6) {
336 v->dest_tile = RandomTile();
337 return true;
339 v->current_order.SetDestination(1);
341 uint n = 0; // Total number of targetable road vehicles.
342 RoadVehicle *u;
343 FOR_ALL_ROADVEHICLES(u) {
344 if (u->IsFrontEngine()) n++;
347 if (n == 0) {
348 /* If there are no targetable road vehicles, destroy the UFO. */
349 delete v;
350 return false;
353 n = RandomRange(n); // Choose one of them.
354 FOR_ALL_ROADVEHICLES(u) {
355 /* Find (n+1)-th road vehicle. */
356 if (u->IsFrontEngine() && (n-- == 0)) break;
359 /* Target it. */
360 v->dest_tile = u->index;
361 v->age = 0;
362 return true;
363 } else {
364 /* Target a vehicle */
365 RoadVehicle *u = RoadVehicle::Get(v->dest_tile);
366 assert(u != NULL && u->type == VEH_ROAD && u->IsFrontEngine());
368 uint dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
370 if (dist < TILE_SIZE && !(u->vehstatus & VS_HIDDEN) && u->breakdown_ctr == 0) {
371 u->breakdown_ctr = 3;
372 u->breakdown_delay = 140;
375 v->direction = GetDirectionTowards(v, u->x_pos, u->y_pos);
377 int z = v->z_pos;
378 if (dist <= TILE_SIZE && z > u->z_pos) z--;
379 v->Advance (z);
381 if (z <= u->z_pos && (u->vehstatus & VS_HIDDEN) == 0) {
382 v->age++;
383 if (u->crashed_ctr == 0) {
384 u->Crash();
386 AddNewsItem<VehicleNewsItem> (STR_NEWS_DISASTER_SMALL_UFO,
387 NT_ACCIDENT, u->index); // delete the news, when the roadvehicle is gone
389 AI::NewEvent(u->owner, new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO));
390 Game::NewEvent(new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO));
394 /* Destroy? */
395 if (v->age > 50) {
396 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
397 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, v);
398 delete v;
399 return false;
403 return true;
406 static void DestructIndustry(Industry *i)
408 for (TileIndex tile = 0; tile != MapSize(); tile++) {
409 if (i->TileBelongsToIndustry(tile)) {
410 ResetIndustryConstructionStage(tile);
411 MarkTileDirtyByTile(tile);
417 * Aircraft handling, v->current_order.dest states:
418 * 0: Fly towards the targeted industry
419 * 1: If within 15 tiles, fire away rockets and destroy industry
420 * 2: Industry explosions
421 * 3: Fly out of the map
422 * If the industry was removed in the meantime just fly to the end of the map.
423 * @param v The disaster vehicle.
424 * @param image_override The image at the time the aircraft is firing.
425 * @param leave_at_top True iff the vehicle leaves the map at the north side.
426 * @param news_message The string that's used as news message.
427 * @param industry_flag Only attack industries that have this flag set.
429 static bool DisasterTick_Aircraft(DisasterVehicle *v, uint16 image_override, bool leave_at_top, StringID news_message, IndustryBehaviour industry_flag)
431 v->tick_counter++;
432 v->image_override = (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? image_override : 0;
434 v->Advance();
436 if (leave_at_top ? (v->x_pos < (-10 * (int)TILE_SIZE)) : (v->x_pos > (int)(MapSizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1)) {
437 delete v;
438 return false;
441 if (v->current_order.GetDestination() == 2) {
442 if (GB(v->tick_counter, 0, 2) == 0) {
443 Industry *i = Industry::Get(v->dest_tile); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
444 int x = TileX(i->location.tile) * TILE_SIZE;
445 int y = TileY(i->location.tile) * TILE_SIZE;
446 uint32 r = Random();
448 CreateEffectVehicleAbove(
449 GB(r, 0, 6) + x,
450 GB(r, 6, 6) + y,
451 GB(r, 12, 4),
452 EV_EXPLOSION_SMALL);
454 if (++v->age >= 55) v->current_order.SetDestination(3);
456 } else if (v->current_order.GetDestination() == 1) {
457 if (++v->age == 112) {
458 v->current_order.SetDestination(2);
459 v->age = 0;
461 Industry *i = Industry::Get(v->dest_tile); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
462 DestructIndustry(i);
464 AddNewsItem<IndustryNewsItem> (news_message,
465 NT_ACCIDENT, i->index, i->town->index);
466 if (_settings_client.sound.disaster) SndPlayTileFx(SND_12_EXPLOSION, i->location.tile);
468 } else if (v->current_order.GetDestination() == 0) {
469 int x = v->x_pos + ((leave_at_top ? -15 : 15) * TILE_SIZE);
470 int y = v->y_pos;
472 if ((uint)x > MapMaxX() * TILE_SIZE - 1) return true;
474 TileIndex tile = TileVirtXY(x, y);
475 if (!IsIndustryTile(tile)) return true;
477 IndustryID ind = GetIndustryIndex(tile);
478 v->dest_tile = ind;
480 if (GetIndustrySpec(Industry::Get(ind)->type)->behaviour & industry_flag) {
481 v->current_order.SetDestination(1);
482 v->age = 0;
486 return true;
489 /** Airplane handling. */
490 static bool DisasterTick_Airplane(DisasterVehicle *v)
492 return DisasterTick_Aircraft(v, SPR_F_15_FIRING, true, STR_NEWS_DISASTER_AIRPLANE_OIL_REFINERY, INDUSTRYBEH_AIRPLANE_ATTACKS);
495 /** Helicopter handling. */
496 static bool DisasterTick_Helicopter(DisasterVehicle *v)
498 return DisasterTick_Aircraft(v, SPR_AH_64A_FIRING, false, STR_NEWS_DISASTER_HELICOPTER_FACTORY, INDUSTRYBEH_CHOPPER_ATTACKS);
501 /** Helicopter rotor blades; keep these spinning */
502 static bool DisasterTick_Helicopter_Rotors(DisasterVehicle *v)
504 v->tick_counter++;
505 if (HasBit(v->tick_counter, 0)) return true;
507 SpriteID &cur_image = v->sprite_seq.seq[0].sprite;
508 if (++cur_image > SPR_ROTOR_MOVING_3) cur_image = SPR_ROTOR_MOVING_1;
510 v->UpdatePositionAndViewport();
512 return true;
516 * (Big) Ufo handling, v->current_order.dest states:
517 * 0: Fly around to the middle of the map, then randomly for a while and home in on a piece of rail
518 * 1: Land there and breakdown all trains in a radius of 12 tiles; and now we wait...
519 * because as soon as the Ufo lands, a fighter jet, a Skyranger, is called to clear up the mess
521 static bool DisasterTick_Big_Ufo(DisasterVehicle *v)
523 v->tick_counter++;
525 if (v->current_order.GetDestination() == 1) {
526 int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
527 int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
528 if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
529 v->direction = GetDirectionTowards(v, x, y);
531 v->Advance();
532 return true;
535 if (!IsValidTile(v->dest_tile)) {
536 /* Make sure we don't land outside the map. */
537 delete v;
538 return false;
541 int z = GetSlopePixelZ(v->x_pos, v->y_pos);
542 if (z < v->z_pos) {
543 v->UpdatePosition(v->x_pos, v->y_pos, v->z_pos - 1);
544 return true;
547 v->current_order.SetDestination(2);
549 Vehicle *target;
550 FOR_ALL_VEHICLES(target) {
551 if (target->IsGroundVehicle()) {
552 if (Delta(target->x_pos, v->x_pos) + Delta(target->y_pos, v->y_pos) <= 12 * (int)TILE_SIZE) {
553 target->breakdown_ctr = 5;
554 target->breakdown_delay = 0xF0;
559 AddNewsItem<TileNewsItem> (STR_NEWS_DISASTER_BIG_UFO,
560 NT_ACCIDENT, v->tile,
561 ClosestTownFromTile(v->dest_tile)->index);
563 if (!Vehicle::CanAllocateItem(2)) {
564 delete v;
565 return false;
567 DisasterVehicle *u = new DisasterVehicle(-6 * (int)TILE_SIZE, v->y_pos, DIR_SW, ST_BIG_UFO_DESTROYER, v->index);
568 DisasterVehicle *w = new DisasterVehicle(-6 * (int)TILE_SIZE, v->y_pos, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
569 u->SetNext(w);
570 } else if (v->current_order.GetDestination() == 0) {
571 int x = TileX(v->dest_tile) * TILE_SIZE;
572 int y = TileY(v->dest_tile) * TILE_SIZE;
573 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= (int)TILE_SIZE) {
574 v->direction = GetDirectionTowards(v, x, y);
575 v->Advance();
576 return true;
579 if (++v->age < 6) {
580 v->dest_tile = RandomTile();
581 return true;
583 v->current_order.SetDestination(1);
585 TileIndex tile_org = RandomTile();
586 TileIndex tile = tile_org;
587 do {
588 if (IsNormalRailTile(tile) &&
589 Company::IsHumanID(GetTileOwner(tile))) {
590 break;
592 tile = TILE_MASK(tile + 1);
593 } while (tile != tile_org);
594 v->dest_tile = tile;
595 v->age = 0;
598 return true;
602 * Skyranger destroying (Big) Ufo handling, v->current_order.dest states:
603 * 0: Home in on landed Ufo and shoot it down
605 static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle *v)
607 v->tick_counter++;
609 v->Advance();
611 if (v->x_pos > (int)(MapSizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1) {
612 delete v;
613 return false;
616 if (v->current_order.GetDestination() == 0) {
617 Vehicle *u = Vehicle::Get(v->big_ufo_destroyer_target);
618 if (Delta(v->x_pos, u->x_pos) > (int)TILE_SIZE) return true;
619 v->current_order.SetDestination(1);
621 CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
622 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, u);
624 delete u;
626 for (int i = 0; i != 80; i++) {
627 uint32 r = Random();
628 CreateEffectVehicleAbove(
629 GB(r, 0, 6) + v->x_pos - 32,
630 GB(r, 5, 6) + v->y_pos - 32,
632 EV_EXPLOSION_SMALL);
635 TileArea ta (v->tile);
636 ta.expand (3, 3, 2, 2);
638 TILE_AREA_LOOP(t, ta) {
639 DisasterClearSquare (t);
643 return true;
647 * Submarine, v->current_order.dest states:
648 * Unused, just float around aimlessly and pop up at different places, turning around
650 static bool DisasterTick_Submarine(DisasterVehicle *v)
652 v->tick_counter++;
654 if (++v->age > 8880) {
655 delete v;
656 return false;
659 if (!HasBit(v->tick_counter, 0)) return true;
661 TileIndex tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
662 if (IsValidTile(tile)) {
663 TrackBits trackbits = TrackdirBitsToTrackBits(GetTileWaterwayStatus(tile));
664 if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
665 v->Advance (0);
666 return true;
670 v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
672 return true;
676 static bool DisasterTick_NULL(DisasterVehicle *v)
678 return true;
681 typedef bool DisasterVehicleTickProc(DisasterVehicle *v);
683 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
684 DisasterTick_Zeppeliner, DisasterTick_NULL,
685 DisasterTick_Ufo, DisasterTick_NULL,
686 DisasterTick_Airplane, DisasterTick_NULL,
687 DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
688 DisasterTick_Big_Ufo, DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
689 DisasterTick_NULL,
690 DisasterTick_Submarine,
691 DisasterTick_Submarine,
695 bool DisasterVehicle::Tick()
697 return _disastervehicle_tick_procs[this->subtype](this);
700 typedef void DisasterInitProc();
704 * Zeppeliner which crashes on a small airport if one found,
705 * otherwise crashes on a random tile
707 static void Disaster_Zeppeliner_Init()
709 if (!Vehicle::CanAllocateItem(2)) return;
711 /* Pick a random place, unless we find a small airport */
712 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
714 Station *st;
715 FOR_ALL_STATIONS(st) {
716 if (st->airport.tile != INVALID_TILE && (st->airport.type == AT_SMALL || st->airport.type == AT_LARGE)) {
717 x = (TileX(st->airport.tile) + 2) * TILE_SIZE;
718 break;
722 DisasterVehicle *v = new DisasterVehicle(x, 0, DIR_SE, ST_ZEPPELINER);
723 /* Allocate shadow */
724 DisasterVehicle *u = new DisasterVehicle(x, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
725 v->SetNext(u);
730 * Ufo which flies around aimlessly from the middle of the map a bit
731 * until it locates a road vehicle which it targets and then destroys
733 static void Disaster_Small_Ufo_Init()
735 if (!Vehicle::CanAllocateItem(2)) return;
737 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
738 DisasterVehicle *v = new DisasterVehicle(x, 0, DIR_SE, ST_SMALL_UFO);
739 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
741 /* Allocate shadow */
742 DisasterVehicle *u = new DisasterVehicle(x, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
743 v->SetNext(u);
747 /* Combat airplane which destroys an oil refinery */
748 static void Disaster_Airplane_Init()
750 if (!Vehicle::CanAllocateItem(2)) return;
752 Industry *i, *found = NULL;
754 FOR_ALL_INDUSTRIES(i) {
755 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
756 (found == NULL || Chance16(1, 2))) {
757 found = i;
761 if (found == NULL) return;
763 /* Start from the bottom (south side) of the map */
764 int x = (MapSizeX() + 9) * TILE_SIZE - 1;
765 int y = TileY(found->location.tile) * TILE_SIZE + 37;
767 DisasterVehicle *v = new DisasterVehicle(x, y, DIR_NE, ST_AIRPLANE);
768 DisasterVehicle *u = new DisasterVehicle(x, y, DIR_NE, ST_AIRPLANE_SHADOW);
769 v->SetNext(u);
773 /** Combat helicopter that destroys a factory */
774 static void Disaster_Helicopter_Init()
776 if (!Vehicle::CanAllocateItem(3)) return;
778 Industry *i, *found = NULL;
780 FOR_ALL_INDUSTRIES(i) {
781 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
782 (found == NULL || Chance16(1, 2))) {
783 found = i;
787 if (found == NULL) return;
789 int x = -16 * (int)TILE_SIZE;
790 int y = TileY(found->location.tile) * TILE_SIZE + 37;
792 DisasterVehicle *v = new DisasterVehicle(x, y, DIR_SW, ST_HELICOPTER);
793 DisasterVehicle *u = new DisasterVehicle(x, y, DIR_SW, ST_HELICOPTER_SHADOW);
794 v->SetNext(u);
796 DisasterVehicle *w = new DisasterVehicle(x, y, DIR_SW, ST_HELICOPTER_ROTORS);
797 u->SetNext(w);
801 /* Big Ufo which lands on a piece of rail and will consequently be shot
802 * down by a combat airplane, destroying the surroundings */
803 static void Disaster_Big_Ufo_Init()
805 if (!Vehicle::CanAllocateItem(2)) return;
807 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
808 int y = MapMaxX() * TILE_SIZE - 1;
810 DisasterVehicle *v = new DisasterVehicle(x, y, DIR_NW, ST_BIG_UFO);
811 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
813 /* Allocate shadow */
814 DisasterVehicle *u = new DisasterVehicle(x, y, DIR_NW, ST_BIG_UFO_SHADOW);
815 v->SetNext(u);
819 static void Disaster_Submarine_Init(DisasterSubType subtype)
821 if (!Vehicle::CanAllocateItem()) return;
823 int y;
824 Direction dir;
825 uint32 r = Random();
826 int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
828 if (HasBit(r, 31)) {
829 y = MapMaxY() * TILE_SIZE - TILE_SIZE / 2 - 1;
830 dir = DIR_NW;
831 } else {
832 y = TILE_SIZE / 2;
833 if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
834 dir = DIR_SE;
836 if (!IsPlainWaterTile(TileVirtXY(x, y))) return;
838 new DisasterVehicle(x, y, dir, subtype);
841 /* Curious submarine #1, just floats around */
842 static void Disaster_Small_Submarine_Init()
844 Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
848 /* Curious submarine #2, just floats around */
849 static void Disaster_Big_Submarine_Init()
851 Disaster_Submarine_Init(ST_BIG_SUBMARINE);
856 * Coal mine catastrophe, destroys a stretch of 30 tiles of
857 * land in a certain direction
859 static void Disaster_CoalMine_Init()
861 int index = GB(Random(), 0, 4);
862 uint m;
864 for (m = 0; m < 15; m++) {
865 const Industry *i;
867 FOR_ALL_INDUSTRIES(i) {
868 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
869 /* keep the news, even when the mine closes */
870 AddNewsItem<TileNewsItem> (STR_NEWS_DISASTER_COAL_MINE_SUBSIDENCE,
871 NT_ACCIDENT, i->location.tile + TileDiffXY(1, 1), i->town->index);
874 TileIndex tile = i->location.tile;
875 TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
877 for (uint n = 0; n < 30; n++) {
878 DisasterClearSquare(tile);
879 tile += step;
880 if (!IsValidTile(tile)) break;
883 return;
889 struct Disaster {
890 DisasterInitProc *init_proc; ///< The init function for this disaster.
891 Year min_year; ///< The first year this disaster will occur.
892 Year max_year; ///< The last year this disaster will occur.
895 static const Disaster _disasters[] = {
896 {Disaster_Zeppeliner_Init, 1930, 1955}, // zeppeliner
897 {Disaster_Small_Ufo_Init, 1940, 1970}, // ufo (small)
898 {Disaster_Airplane_Init, 1960, 1990}, // airplane
899 {Disaster_Helicopter_Init, 1970, 2000}, // helicopter
900 {Disaster_Big_Ufo_Init, 2000, 2100}, // ufo (big)
901 {Disaster_Small_Submarine_Init, 1940, 1965}, // submarine (small)
902 {Disaster_Big_Submarine_Init, 1975, 2010}, // submarine (big)
903 {Disaster_CoalMine_Init, 1950, 1985}, // coalmine
906 static void DoDisaster()
908 byte buf[lengthof(_disasters)];
910 byte j = 0;
911 for (size_t i = 0; i != lengthof(_disasters); i++) {
912 if (_cur_year >= _disasters[i].min_year && _cur_year < _disasters[i].max_year) buf[j++] = (byte)i;
915 if (j == 0) return;
917 _disasters[buf[RandomRange(j)]].init_proc();
921 static void ResetDisasterDelay()
923 _disaster_delay = GB(Random(), 0, 9) + 730;
926 void DisasterDailyLoop()
928 if (--_disaster_delay != 0) return;
930 ResetDisasterDelay();
932 if (_settings_game.difficulty.disasters != 0) DoDisaster();
935 void StartupDisasters()
937 ResetDisasterDelay();
941 * Marks all disasters targeting this industry in such a way
942 * they won't call Industry::Get(v->dest_tile) on invalid industry anymore.
943 * @param i deleted industry
945 void ReleaseDisastersTargetingIndustry(IndustryID i)
947 DisasterVehicle *v;
948 FOR_ALL_DISASTERVEHICLES(v) {
949 /* primary disaster vehicles that have chosen target */
950 if (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER) {
951 /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
952 if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
958 * Notify disasters that we are about to delete a vehicle. So make them head elsewhere.
959 * @param vehicle deleted vehicle
961 void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
963 DisasterVehicle *v;
964 FOR_ALL_DISASTERVEHICLES(v) {
965 /* primary disaster vehicles that have chosen target */
966 if (v->subtype == ST_SMALL_UFO) {
967 if (v->current_order.GetDestination() != 0 && v->dest_tile == vehicle) {
968 /* Revert to target-searching */
969 v->current_order.SetDestination(0);
970 v->dest_tile = RandomTile();
971 v->z_pos = GetAircraftBaseFlightLevel (v);
972 v->age = 0;
978 void DisasterVehicle::UpdateDeltaXY(Direction direction)
980 this->x_offs = -1;
981 this->y_offs = -1;
982 this->x_extent = 2;
983 this->y_extent = 2;
984 this->z_extent = 5;