OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

disaster_vehicle.cpp
Go to the documentation of this file.
1 /* $Id: disaster_vehicle.cpp 27087 2014-12-21 20:49:15Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
28 #include "stdafx.h"
29
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 "sound_func.h"
43 #include "effectvehicle_func.h"
44 #include "roadveh.h"
45 #include "ai/ai.hpp"
46 #include "game/game.hpp"
47 #include "company_base.h"
48 #include "core/random_func.hpp"
49 #include "core/backup_type.hpp"
50
51 #include "table/strings.h"
52
53 #include "safeguards.h"
54
56 uint16 _disaster_delay;
57
58 static void DisasterClearSquare(TileIndex tile)
59 {
60 if (EnsureNoVehicleOnGround(tile).Failed()) return;
61
62 switch (GetTileType(tile)) {
63 case MP_RAILWAY:
64 if (Company::IsHumanID(GetTileOwner(tile)) && !IsRailDepot(tile)) {
65 Backup<CompanyByte> cur_company(_current_company, OWNER_WATER, FILE_LINE);
66 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
67 cur_company.Restore();
68
69 /* update signals in buffer */
70 UpdateSignalsInBuffer();
71 }
72 break;
73
74 case MP_HOUSE: {
75 Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
76 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
77 cur_company.Restore();
78 break;
79 }
80
81 case MP_TREES:
82 case MP_CLEAR:
83 DoClearSquare(tile);
84 break;
85
86 default:
87 break;
88 }
89 }
90
91 static const SpriteID _disaster_images_1[] = {SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP};
92 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};
93 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};
94 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};
95 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};
96 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};
97 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};
98 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};
99 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};
100
101 static const SpriteID * const _disaster_images[] = {
102 _disaster_images_1, _disaster_images_1,
103 _disaster_images_2, _disaster_images_2,
104 _disaster_images_3, _disaster_images_3,
105 _disaster_images_8, _disaster_images_8, _disaster_images_9,
106 _disaster_images_6, _disaster_images_6,
107 _disaster_images_7, _disaster_images_7,
108 _disaster_images_4, _disaster_images_5,
109 };
110
111 void DisasterVehicle::UpdateImage()
112 {
113 SpriteID img = this->image_override;
114 if (img == 0) img = _disaster_images[this->subtype][this->direction];
115 this->cur_image = img;
116 }
117
126 DisasterVehicle::DisasterVehicle(int x, int y, Direction direction, DisasterSubType subtype, VehicleID big_ufo_destroyer_target) :
127 SpecializedVehicleBase(), big_ufo_destroyer_target(big_ufo_destroyer_target)
128 {
129 this->vehstatus = VS_UNCLICKABLE;
130
131 this->x_pos = x;
132 this->y_pos = y;
133 switch (subtype) {
134 case ST_ZEPPELINER:
135 case ST_SMALL_UFO:
136 case ST_AIRPLANE:
137 case ST_HELICOPTER:
138 case ST_BIG_UFO:
139 case ST_BIG_UFO_DESTROYER:
140 GetAircraftFlightLevelBounds(this, &this->z_pos, NULL);
141 break;
142
143 case ST_HELICOPTER_ROTORS:
144 GetAircraftFlightLevelBounds(this, &this->z_pos, NULL);
145 this->z_pos += ROTOR_Z_OFFSET;
146 break;
147
148 case ST_SMALL_SUBMARINE:
149 case ST_BIG_SUBMARINE:
150 this->z_pos = 0;
151 break;
152
153 case ST_ZEPPELINER_SHADOW:
154 case ST_SMALL_UFO_SHADOW:
155 case ST_AIRPLANE_SHADOW:
156 case ST_HELICOPTER_SHADOW:
157 case ST_BIG_UFO_SHADOW:
158 case ST_BIG_UFO_DESTROYER_SHADOW:
159 this->z_pos = 0;
160 this->vehstatus |= VS_SHADOW;
161 break;
162 }
163
164 this->direction = direction;
165 this->tile = TileVirtXY(x, y);
166 this->subtype = subtype;
167 this->UpdateDeltaXY(INVALID_DIR);
168 this->owner = OWNER_NONE;
169 this->image_override = 0;
170 this->current_order.Free();
171
172 this->UpdateImage();
173 this->UpdatePositionAndViewport();
174 }
175
182 void DisasterVehicle::UpdatePosition(int x, int y, int z)
183 {
184 this->x_pos = x;
185 this->y_pos = y;
186 this->z_pos = z;
187 this->tile = TileVirtXY(x, y);
188
189 this->UpdateImage();
190 this->UpdatePositionAndViewport();
191
192 DisasterVehicle *u = this->Next();
193 if (u != NULL) {
194 int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
195 int safe_y = Clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
196
197 u->x_pos = x;
198 u->y_pos = y - 1 - (max(z - GetSlopePixelZ(safe_x, safe_y), 0) >> 3);
199 safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
200 u->z_pos = GetSlopePixelZ(safe_x, safe_y);
201 u->direction = this->direction;
202
203 u->UpdateImage();
204 u->UpdatePositionAndViewport();
205
206 if ((u = u->Next()) != NULL) {
207 u->x_pos = x;
208 u->y_pos = y;
209 u->z_pos = z + ROTOR_Z_OFFSET;
210 u->UpdatePositionAndViewport();
211 }
212 }
213 }
214
223 static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
224 {
225 v->tick_counter++;
226
227 if (v->current_order.GetDestination() < 2) {
228 if (HasBit(v->tick_counter, 0)) return true;
229
230 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
231
232 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
233
234 if (v->current_order.GetDestination() == 1) {
235 if (++v->age == 38) {
236 v->current_order.SetDestination(2);
237 v->age = 0;
238 }
239
240 if (GB(v->tick_counter, 0, 3) == 0) CreateEffectVehicleRel(v, 0, -17, 2, EV_CRASH_SMOKE);
241
242 } else if (v->current_order.GetDestination() == 0) {
243 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
244 v->current_order.SetDestination(1);
245 v->age = 0;
246
247 SetDParam(0, GetStationIndex(v->tile));
248 AddVehicleNewsItem(STR_NEWS_DISASTER_ZEPPELIN, NT_ACCIDENT, v->index); // Delete the news, when the zeppelin is gone
249 AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCrashed(GetStationIndex(v->tile)));
250 }
251 }
252
253 if (v->y_pos >= (int)((MapSizeY() + 9) * TILE_SIZE - 1)) {
254 delete v;
255 return false;
256 }
257
258 return true;
259 }
260
261 if (v->current_order.GetDestination() > 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 CLRBITS(st->airport.flags, RUNWAY_IN_block);
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 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->current_order.SetDestination(3);
299 v->age = 0;
300 }
301
302 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
303 SETBITS(Station::GetByTile(v->tile)->airport.flags, RUNWAY_IN_block);
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->current_order.GetDestination() == 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->current_order.SetDestination(1);
334
335 uint n = 0; // Total number of targetable road vehicles.
336 RoadVehicle *u;
337 FOR_ALL_ROADVEHICLES(u) {
338 if (u->IsFrontEngine()) n++;
339 }
340
341 if (n == 0) {
342 /* If there are no targetable road vehicles, destroy the UFO. */
343 delete v;
344 return false;
345 }
346
347 n = RandomRange(n); // Choose one of them.
348 FOR_ALL_ROADVEHICLES(u) {
349 /* Find (n+1)-th road vehicle. */
350 if (u->IsFrontEngine() && (n-- == 0)) break;
351 }
352
353 /* Target it. */
354 v->dest_tile = u->index;
355 v->age = 0;
356 return true;
357 } else {
358 /* Target a vehicle */
359 RoadVehicle *u = RoadVehicle::Get(v->dest_tile);
360 assert(u != NULL && u->type == VEH_ROAD && u->IsFrontEngine());
361
362 uint dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
363
364 if (dist < TILE_SIZE && !(u->vehstatus & VS_HIDDEN) && u->breakdown_ctr == 0) {
365 u->breakdown_ctr = 3;
366 u->breakdown_delay = 140;
367 }
368
369 v->direction = GetDirectionTowards(v, u->x_pos, u->y_pos);
370 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
371
372 int z = v->z_pos;
373 if (dist <= TILE_SIZE && z > u->z_pos) z--;
374 v->UpdatePosition(gp.x, gp.y, z);
375
376 if (z <= u->z_pos && (u->vehstatus & VS_HIDDEN) == 0) {
377 v->age++;
378 if (u->crashed_ctr == 0) {
379 u->Crash();
380
381 AddVehicleNewsItem(STR_NEWS_DISASTER_SMALL_UFO, NT_ACCIDENT, u->index); // delete the news, when the roadvehicle is gone
382
383 AI::NewEvent(u->owner, new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO));
384 Game::NewEvent(new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO));
385 }
386 }
387
388 /* Destroy? */
389 if (v->age > 50) {
390 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
391 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, v);
392 delete v;
393 return false;
394 }
395 }
396
397 return true;
398 }
399
400 static void DestructIndustry(Industry *i)
401 {
402 for (TileIndex tile = 0; tile != MapSize(); tile++) {
403 if (i->TileBelongsToIndustry(tile)) {
404 ResetIndustryConstructionStage(tile);
405 MarkTileDirtyByTile(tile);
406 }
407 }
408 }
409
423 static bool DisasterTick_Aircraft(DisasterVehicle *v, uint16 image_override, bool leave_at_top, StringID news_message, IndustryBehaviour industry_flag)
424 {
425 v->tick_counter++;
426 v->image_override = (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? image_override : 0;
427
428 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
429 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
430
431 if ((leave_at_top && gp.x < (-10 * (int)TILE_SIZE)) || (!leave_at_top && gp.x > (int)(MapSizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1)) {
432 delete v;
433 return false;
434 }
435
436 if (v->current_order.GetDestination() == 2) {
437 if (GB(v->tick_counter, 0, 2) == 0) {
438 Industry *i = Industry::Get(v->dest_tile); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
439 int x = TileX(i->location.tile) * TILE_SIZE;
440 int y = TileY(i->location.tile) * TILE_SIZE;
441 uint32 r = Random();
442
443 CreateEffectVehicleAbove(
444 GB(r, 0, 6) + x,
445 GB(r, 6, 6) + y,
446 GB(r, 12, 4),
447 EV_EXPLOSION_SMALL);
448
449 if (++v->age >= 55) v->current_order.SetDestination(3);
450 }
451 } else if (v->current_order.GetDestination() == 1) {
452 if (++v->age == 112) {
453 v->current_order.SetDestination(2);
454 v->age = 0;
455
456 Industry *i = Industry::Get(v->dest_tile); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid
457 DestructIndustry(i);
458
459 SetDParam(0, i->town->index);
460 AddIndustryNewsItem(news_message, NT_ACCIDENT, i->index); // delete the news, when the industry closes
461 if (_settings_client.sound.disaster) SndPlayTileFx(SND_12_EXPLOSION, i->location.tile);
462 }
463 } else if (v->current_order.GetDestination() == 0) {
464 int x = v->x_pos + ((leave_at_top ? -15 : 15) * TILE_SIZE);
465 int y = v->y_pos;
466
467 if ((uint)x > MapMaxX() * TILE_SIZE - 1) return true;
468
469 TileIndex tile = TileVirtXY(x, y);
470 if (!IsTileType(tile, MP_INDUSTRY)) return true;
471
472 IndustryID ind = GetIndustryIndex(tile);
473 v->dest_tile = ind;
474
475 if (GetIndustrySpec(Industry::Get(ind)->type)->behaviour & industry_flag) {
476 v->current_order.SetDestination(1);
477 v->age = 0;
478 }
479 }
480
481 return true;
482 }
483
485 static bool DisasterTick_Airplane(DisasterVehicle *v)
486 {
487 return DisasterTick_Aircraft(v, SPR_F_15_FIRING, true, STR_NEWS_DISASTER_AIRPLANE_OIL_REFINERY, INDUSTRYBEH_AIRPLANE_ATTACKS);
488 }
489
491 static bool DisasterTick_Helicopter(DisasterVehicle *v)
492 {
493 return DisasterTick_Aircraft(v, SPR_AH_64A_FIRING, false, STR_NEWS_DISASTER_HELICOPTER_FACTORY, INDUSTRYBEH_CHOPPER_ATTACKS);
494 }
495
497 static bool DisasterTick_Helicopter_Rotors(DisasterVehicle *v)
498 {
499 v->tick_counter++;
500 if (HasBit(v->tick_counter, 0)) return true;
501
502 if (++v->cur_image > SPR_ROTOR_MOVING_3) v->cur_image = SPR_ROTOR_MOVING_1;
503
504 v->UpdatePositionAndViewport();
505
506 return true;
507 }
508
515 static bool DisasterTick_Big_Ufo(DisasterVehicle *v)
516 {
517 v->tick_counter++;
518
519 if (v->current_order.GetDestination() == 1) {
520 int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
521 int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
522 if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
523 v->direction = GetDirectionTowards(v, x, y);
524
525 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
526 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
527 return true;
528 }
529
530 if (!IsValidTile(v->dest_tile)) {
531 /* Make sure we don't land outside the map. */
532 delete v;
533 return false;
534 }
535
536 int z = GetSlopePixelZ(v->x_pos, v->y_pos);
537 if (z < v->z_pos) {
538 v->UpdatePosition(v->x_pos, v->y_pos, v->z_pos - 1);
539 return true;
540 }
541
542 v->current_order.SetDestination(2);
543
544 Vehicle *target;
545 FOR_ALL_VEHICLES(target) {
546 if (target->IsGroundVehicle()) {
547 if (Delta(target->x_pos, v->x_pos) + Delta(target->y_pos, v->y_pos) <= 12 * (int)TILE_SIZE) {
548 target->breakdown_ctr = 5;
549 target->breakdown_delay = 0xF0;
550 }
551 }
552 }
553
554 Town *t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
555 SetDParam(0, t->index);
556 AddTileNewsItem(STR_NEWS_DISASTER_BIG_UFO, NT_ACCIDENT, v->tile);
557
558 if (!Vehicle::CanAllocateItem(2)) {
559 delete v;
560 return false;
561 }
562 DisasterVehicle *u = new DisasterVehicle(-6 * (int)TILE_SIZE, v->y_pos, DIR_SW, ST_BIG_UFO_DESTROYER, v->index);
563 DisasterVehicle *w = new DisasterVehicle(-6 * (int)TILE_SIZE, v->y_pos, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
564 u->SetNext(w);
565 } else if (v->current_order.GetDestination() == 0) {
566 int x = TileX(v->dest_tile) * TILE_SIZE;
567 int y = TileY(v->dest_tile) * TILE_SIZE;
568 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= (int)TILE_SIZE) {
569 v->direction = GetDirectionTowards(v, x, y);
570 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
571 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
572 return true;
573 }
574
575 if (++v->age < 6) {
576 v->dest_tile = RandomTile();
577 return true;
578 }
579 v->current_order.SetDestination(1);
580
581 TileIndex tile_org = RandomTile();
582 TileIndex tile = tile_org;
583 do {
584 if (IsPlainRailTile(tile) &&
585 Company::IsHumanID(GetTileOwner(tile))) {
586 break;
587 }
588 tile = TILE_MASK(tile + 1);
589 } while (tile != tile_org);
590 v->dest_tile = tile;
591 v->age = 0;
592 }
593
594 return true;
595 }
596
601 static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle *v)
602 {
603 v->tick_counter++;
604
605 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
606 v->UpdatePosition(gp.x, gp.y, GetAircraftFlightLevel(v));
607
608 if (gp.x > (int)(MapSizeX() * TILE_SIZE + 9 * TILE_SIZE) - 1) {
609 delete v;
610 return false;
611 }
612
613 if (v->current_order.GetDestination() == 0) {
614 Vehicle *u = Vehicle::Get(v->big_ufo_destroyer_target);
615 if (Delta(v->x_pos, u->x_pos) > (int)TILE_SIZE) return true;
616 v->current_order.SetDestination(1);
617
618 CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
619 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, u);
620
621 delete u;
622
623 for (int i = 0; i != 80; i++) {
624 uint32 r = Random();
625 CreateEffectVehicleAbove(
626 GB(r, 0, 6) + v->x_pos - 32,
627 GB(r, 5, 6) + v->y_pos - 32,
628 0,
629 EV_EXPLOSION_SMALL);
630 }
631
632 for (int dy = -3; dy < 3; dy++) {
633 for (int dx = -3; dx < 3; dx++) {
634 TileIndex tile = TileAddWrap(v->tile, dx, dy);
635 if (tile != INVALID_TILE) DisasterClearSquare(tile);
636 }
637 }
638 }
639
640 return true;
641 }
642
647 static bool DisasterTick_Submarine(DisasterVehicle *v)
648 {
649 v->tick_counter++;
650
651 if (++v->age > 8880) {
652 delete v;
653 return false;
654 }
655
656 if (!HasBit(v->tick_counter, 0)) return true;
657
658 TileIndex tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
659 if (IsValidTile(tile)) {
660 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
661 if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
662 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
663 v->UpdatePosition(gp.x, gp.y, v->z_pos);
664 return true;
665 }
666 }
667
668 v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
669
670 return true;
671 }
672
673
674 static bool DisasterTick_NULL(DisasterVehicle *v)
675 {
676 return true;
677 }
678
679 typedef bool DisasterVehicleTickProc(DisasterVehicle *v);
680
681 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
682 DisasterTick_Zeppeliner, DisasterTick_NULL,
683 DisasterTick_Ufo, DisasterTick_NULL,
684 DisasterTick_Airplane, DisasterTick_NULL,
685 DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
686 DisasterTick_Big_Ufo, DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
687 DisasterTick_NULL,
688 DisasterTick_Submarine,
689 DisasterTick_Submarine,
690 };
691
692
693 bool DisasterVehicle::Tick()
694 {
695 return _disastervehicle_tick_procs[this->subtype](this);
696 }
697
698 typedef void DisasterInitProc();
699
700
705 static void Disaster_Zeppeliner_Init()
706 {
707 if (!Vehicle::CanAllocateItem(2)) return;
708
709 /* Pick a random place, unless we find a small airport */
710 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
711
712 Station *st;
713 FOR_ALL_STATIONS(st) {
714 if (st->airport.tile != INVALID_TILE && (st->airport.type == AT_SMALL || st->airport.type == AT_LARGE)) {
715 x = (TileX(st->airport.tile) + 2) * TILE_SIZE;
716 break;
717 }
718 }
719
720 DisasterVehicle *v = new DisasterVehicle(x, 0, DIR_SE, ST_ZEPPELINER);
721 /* Allocate shadow */
722 DisasterVehicle *u = new DisasterVehicle(x, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
723 v->SetNext(u);
724 }
725
726
731 static void Disaster_Small_Ufo_Init()
732 {
733 if (!Vehicle::CanAllocateItem(2)) return;
734
735 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
736 DisasterVehicle *v = new DisasterVehicle(x, 0, DIR_SE, ST_SMALL_UFO);
737 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
738
739 /* Allocate shadow */
740 DisasterVehicle *u = new DisasterVehicle(x, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
741 v->SetNext(u);
742 }
743
744
745 /* Combat airplane which destroys an oil refinery */
746 static void Disaster_Airplane_Init()
747 {
748 if (!Vehicle::CanAllocateItem(2)) return;
749
750 Industry *i, *found = NULL;
751
752 FOR_ALL_INDUSTRIES(i) {
753 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
754 (found == NULL || Chance16(1, 2))) {
755 found = i;
756 }
757 }
758
759 if (found == NULL) return;
760
761 /* Start from the bottom (south side) of the map */
762 int x = (MapSizeX() + 9) * TILE_SIZE - 1;
763 int y = TileY(found->location.tile) * TILE_SIZE + 37;
764
765 DisasterVehicle *v = new DisasterVehicle(x, y, DIR_NE, ST_AIRPLANE);
766 DisasterVehicle *u = new DisasterVehicle(x, y, DIR_NE, ST_AIRPLANE_SHADOW);
767 v->SetNext(u);
768 }
769
770
772 static void Disaster_Helicopter_Init()
773 {
774 if (!Vehicle::CanAllocateItem(3)) return;
775
776 Industry *i, *found = NULL;
777
778 FOR_ALL_INDUSTRIES(i) {
779 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
780 (found == NULL || Chance16(1, 2))) {
781 found = i;
782 }
783 }
784
785 if (found == NULL) return;
786
787 int x = -16 * (int)TILE_SIZE;
788 int y = TileY(found->location.tile) * TILE_SIZE + 37;
789
790 DisasterVehicle *v = new DisasterVehicle(x, y, DIR_SW, ST_HELICOPTER);
791 DisasterVehicle *u = new DisasterVehicle(x, y, DIR_SW, ST_HELICOPTER_SHADOW);
792 v->SetNext(u);
793
794 DisasterVehicle *w = new DisasterVehicle(x, y, DIR_SW, ST_HELICOPTER_ROTORS);
795 u->SetNext(w);
796 }
797
798
799 /* Big Ufo which lands on a piece of rail and will consequently be shot
800 * down by a combat airplane, destroying the surroundings */
801 static void Disaster_Big_Ufo_Init()
802 {
803 if (!Vehicle::CanAllocateItem(2)) return;
804
805 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
806 int y = MapMaxX() * TILE_SIZE - 1;
807
808 DisasterVehicle *v = new DisasterVehicle(x, y, DIR_NW, ST_BIG_UFO);
809 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
810
811 /* Allocate shadow */
812 DisasterVehicle *u = new DisasterVehicle(x, y, DIR_NW, ST_BIG_UFO_SHADOW);
813 v->SetNext(u);
814 }
815
816
817 static void Disaster_Submarine_Init(DisasterSubType subtype)
818 {
819 if (!Vehicle::CanAllocateItem()) return;
820
821 int y;
822 Direction dir;
823 uint32 r = Random();
824 int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
825
826 if (HasBit(r, 31)) {
827 y = MapMaxY() * TILE_SIZE - TILE_SIZE / 2 - 1;
828 dir = DIR_NW;
829 } else {
830 y = TILE_SIZE / 2;
831 if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
832 dir = DIR_SE;
833 }
834 if (!IsWaterTile(TileVirtXY(x, y))) return;
835
836 new DisasterVehicle(x, y, dir, subtype);
837 }
838
839 /* Curious submarine #1, just floats around */
840 static void Disaster_Small_Submarine_Init()
841 {
842 Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
843 }
844
845
846 /* Curious submarine #2, just floats around */
847 static void Disaster_Big_Submarine_Init()
848 {
849 Disaster_Submarine_Init(ST_BIG_SUBMARINE);
850 }
851
852
857 static void Disaster_CoalMine_Init()
858 {
859 int index = GB(Random(), 0, 4);
860 uint m;
861
862 for (m = 0; m < 15; m++) {
863 const Industry *i;
864
865 FOR_ALL_INDUSTRIES(i) {
866 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
867 SetDParam(0, i->town->index);
868 AddTileNewsItem(STR_NEWS_DISASTER_COAL_MINE_SUBSIDENCE, NT_ACCIDENT, i->location.tile + TileDiffXY(1, 1)); // keep the news, even when the mine closes
869
870 {
871 TileIndex tile = i->location.tile;
872 TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
873
874 for (uint n = 0; n < 30; n++) {
875 DisasterClearSquare(tile);
876 tile += step;
877 if (!IsValidTile(tile)) break;
878 }
879 }
880 return;
881 }
882 }
883 }
884 }
885
886 struct Disaster {
887 DisasterInitProc *init_proc;
888 Year min_year;
889 Year max_year;
890 };
891
892 static const Disaster _disasters[] = {
893 {Disaster_Zeppeliner_Init, 1930, 1955}, // zeppeliner
894 {Disaster_Small_Ufo_Init, 1940, 1970}, // ufo (small)
895 {Disaster_Airplane_Init, 1960, 1990}, // airplane
896 {Disaster_Helicopter_Init, 1970, 2000}, // helicopter
897 {Disaster_Big_Ufo_Init, 2000, 2100}, // ufo (big)
898 {Disaster_Small_Submarine_Init, 1940, 1965}, // submarine (small)
899 {Disaster_Big_Submarine_Init, 1975, 2010}, // submarine (big)
900 {Disaster_CoalMine_Init, 1950, 1985}, // coalmine
901 };
902
903 static void DoDisaster()
904 {
905 byte buf[lengthof(_disasters)];
906
907 byte j = 0;
908 for (size_t i = 0; i != lengthof(_disasters); i++) {
909 if (_cur_year >= _disasters[i].min_year && _cur_year < _disasters[i].max_year) buf[j++] = (byte)i;
910 }
911
912 if (j == 0) return;
913
914 _disasters[buf[RandomRange(j)]].init_proc();
915 }
916
917
918 static void ResetDisasterDelay()
919 {
920 _disaster_delay = GB(Random(), 0, 9) + 730;
921 }
922
923 void DisasterDailyLoop()
924 {
925 if (--_disaster_delay != 0) return;
926
927 ResetDisasterDelay();
928
929 if (_settings_game.difficulty.disasters != 0) DoDisaster();
930 }
931
932 void StartupDisasters()
933 {
934 ResetDisasterDelay();
935 }
936
942 void ReleaseDisastersTargetingIndustry(IndustryID i)
943 {
944 DisasterVehicle *v;
945 FOR_ALL_DISASTERVEHICLES(v) {
946 /* primary disaster vehicles that have chosen target */
947 if (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER) {
948 /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */
949 if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
950 }
951 }
952 }
953
958 void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
959 {
960 DisasterVehicle *v;
961 FOR_ALL_DISASTERVEHICLES(v) {
962 /* primary disaster vehicles that have chosen target */
963 if (v->subtype == ST_SMALL_UFO) {
964 if (v->current_order.GetDestination() != 0 && v->dest_tile == vehicle) {
965 /* Revert to target-searching */
966 v->current_order.SetDestination(0);
967 v->dest_tile = RandomTile();
968 GetAircraftFlightLevelBounds(v, &v->z_pos, NULL);
969 v->age = 0;
970 }
971 }
972 }
973 }
974
975 void DisasterVehicle::UpdateDeltaXY(Direction direction)
976 {
977 this->x_offs = -1;
978 this->y_offs = -1;
979 this->x_extent = 2;
980 this->y_extent = 2;
981 this->z_extent = 5;
982 }