00001
00002
00003
00004
00005
00006
00007
00008
00009
00026 #include "stdafx.h"
00027 #include "landscape.h"
00028
00029 #include "industry.h"
00030 #include "station_base.h"
00031 #include "command_func.h"
00032 #include "news_func.h"
00033 #include "town.h"
00034 #include "company_func.h"
00035 #include "variables.h"
00036 #include "strings_func.h"
00037 #include "date_func.h"
00038 #include "functions.h"
00039 #include "vehicle_func.h"
00040 #include "sound_func.h"
00041 #include "effectvehicle_func.h"
00042 #include "roadveh.h"
00043 #include "ai/ai.hpp"
00044 #include "company_base.h"
00045 #include "core/random_func.hpp"
00046
00047 #include "table/strings.h"
00048 #include "table/sprites.h"
00049
00050 enum DisasterSubType {
00051 ST_ZEPPELINER,
00052 ST_ZEPPELINER_SHADOW,
00053 ST_SMALL_UFO,
00054 ST_SMALL_UFO_SHADOW,
00055 ST_AIRPLANE,
00056 ST_AIRPLANE_SHADOW,
00057 ST_HELICOPTER,
00058 ST_HELICOPTER_SHADOW,
00059 ST_HELICOPTER_ROTORS,
00060 ST_BIG_UFO,
00061 ST_BIG_UFO_SHADOW,
00062 ST_BIG_UFO_DESTROYER,
00063 ST_BIG_UFO_DESTROYER_SHADOW,
00064 ST_SMALL_SUBMARINE,
00065 ST_BIG_SUBMARINE,
00066 };
00067
00068 static void DisasterClearSquare(TileIndex tile)
00069 {
00070 if (!EnsureNoVehicleOnGround(tile)) return;
00071
00072 switch (GetTileType(tile)) {
00073 case MP_RAILWAY:
00074 if (Company::IsHumanID(GetTileOwner(tile))) {
00075 CompanyID old_company = _current_company;
00076 _current_company = OWNER_WATER;
00077 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00078 _current_company = old_company;
00079
00080
00081 UpdateSignalsInBuffer();
00082 }
00083 break;
00084
00085 case MP_HOUSE: {
00086 CompanyID old_company = _current_company;
00087 _current_company = OWNER_NONE;
00088 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00089 _current_company = old_company;
00090 break;
00091 }
00092
00093 case MP_TREES:
00094 case MP_CLEAR:
00095 DoClearSquare(tile);
00096 break;
00097
00098 default:
00099 break;
00100 }
00101 }
00102
00103 static const SpriteID _disaster_images_1[] = {SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP, SPR_BLIMP};
00104 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};
00105 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};
00106 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};
00107 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};
00108 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};
00109 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};
00110 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};
00111 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};
00112
00113 static const SpriteID * const _disaster_images[] = {
00114 _disaster_images_1, _disaster_images_1,
00115 _disaster_images_2, _disaster_images_2,
00116 _disaster_images_3, _disaster_images_3,
00117 _disaster_images_8, _disaster_images_8, _disaster_images_9,
00118 _disaster_images_6, _disaster_images_6,
00119 _disaster_images_7, _disaster_images_7,
00120 _disaster_images_4, _disaster_images_5,
00121 };
00122
00123 static void DisasterVehicleUpdateImage(DisasterVehicle *v)
00124 {
00125 SpriteID img = v->image_override;
00126 if (img == 0) img = _disaster_images[v->subtype][v->direction];
00127 v->cur_image = img;
00128 }
00129
00132 static void InitializeDisasterVehicle(DisasterVehicle *v, int x, int y, byte z, Direction direction, byte subtype)
00133 {
00134 v->x_pos = x;
00135 v->y_pos = y;
00136 v->z_pos = z;
00137 v->tile = TileVirtXY(x, y);
00138 v->direction = direction;
00139 v->subtype = subtype;
00140 v->UpdateDeltaXY(INVALID_DIR);
00141 v->owner = OWNER_NONE;
00142 v->vehstatus = VS_UNCLICKABLE;
00143 v->image_override = 0;
00144 v->current_order.Free();
00145
00146 DisasterVehicleUpdateImage(v);
00147 VehicleMove(v, false);
00148 MarkSingleVehicleDirty(v);
00149 }
00150
00151 static void SetDisasterVehiclePos(DisasterVehicle *v, int x, int y, byte z)
00152 {
00153 v->x_pos = x;
00154 v->y_pos = y;
00155 v->z_pos = z;
00156 v->tile = TileVirtXY(x, y);
00157
00158 DisasterVehicleUpdateImage(v);
00159 VehicleMove(v, true);
00160
00161 DisasterVehicle *u = v->Next();
00162 if (u != NULL) {
00163 int safe_x = Clamp(x, 0, MapMaxX() * TILE_SIZE);
00164 int safe_y = Clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
00165
00166 u->x_pos = x;
00167 u->y_pos = y - 1 - (max(z - GetSlopeZ(safe_x, safe_y), 0U) >> 3);
00168 safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
00169 u->z_pos = GetSlopeZ(safe_x, safe_y);
00170 u->direction = v->direction;
00171
00172 DisasterVehicleUpdateImage(u);
00173 VehicleMove(u, true);
00174
00175 if ((u = u->Next()) != NULL) {
00176 u->x_pos = x;
00177 u->y_pos = y;
00178 u->z_pos = z + 5;
00179 VehicleMove(u, true);
00180 }
00181 }
00182 }
00183
00192 static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
00193 {
00194 v->tick_counter++;
00195
00196 if (v->current_order.GetDestination() < 2) {
00197 if (HasBit(v->tick_counter, 0)) return true;
00198
00199 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00200
00201 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00202
00203 if (v->current_order.GetDestination() == 1) {
00204 if (++v->age == 38) {
00205 v->current_order.SetDestination(2);
00206 v->age = 0;
00207 }
00208
00209 if (GB(v->tick_counter, 0, 3) == 0) CreateEffectVehicleRel(v, 0, -17, 2, EV_SMOKE);
00210
00211 } else if (v->current_order.GetDestination() == 0) {
00212 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
00213 v->current_order.SetDestination(1);
00214 v->age = 0;
00215
00216 SetDParam(0, GetStationIndex(v->tile));
00217 AddVehicleNewsItem(STR_NEWS_DISASTER_ZEPPELIN,
00218 NS_ACCIDENT,
00219 v->index);
00220 AI::NewEvent(GetTileOwner(v->tile), new AIEventDisasterZeppelinerCrashed(GetStationIndex(v->tile)));
00221 }
00222 }
00223
00224 if (v->y_pos >= ((int)MapSizeY() + 9) * TILE_SIZE - 1) {
00225 delete v;
00226 return false;
00227 }
00228
00229 return true;
00230 }
00231
00232 if (v->current_order.GetDestination() > 2) {
00233 if (++v->age <= 13320) return true;
00234
00235 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
00236 Station *st = Station::GetByTile(v->tile);
00237 CLRBITS(st->airport_flags, RUNWAY_IN_block);
00238 AI::NewEvent(GetTileOwner(v->tile), new AIEventDisasterZeppelinerCleared(st->index));
00239 }
00240
00241 SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos);
00242 delete v;
00243 return false;
00244 }
00245
00246 int x = v->x_pos;
00247 int y = v->y_pos;
00248 byte z = GetSlopeZ(x, y);
00249 if (z < v->z_pos) z = v->z_pos - 1;
00250 SetDisasterVehiclePos(v, x, y, z);
00251
00252 if (++v->age == 1) {
00253 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
00254 SndPlayVehicleFx(SND_12_EXPLOSION, v);
00255 v->image_override = SPR_BLIMP_CRASHING;
00256 } else if (v->age == 70) {
00257 v->image_override = SPR_BLIMP_CRASHED;
00258 } else if (v->age <= 300) {
00259 if (GB(v->tick_counter, 0, 3) == 0) {
00260 uint32 r = Random();
00261
00262 CreateEffectVehicleRel(v,
00263 GB(r, 0, 4) - 7,
00264 GB(r, 4, 4) - 7,
00265 GB(r, 8, 3) + 5,
00266 EV_EXPLOSION_SMALL);
00267 }
00268 } else if (v->age == 350) {
00269 v->current_order.SetDestination(3);
00270 v->age = 0;
00271 }
00272
00273 if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
00274 SETBITS(Station::GetByTile(v->tile)->airport_flags, RUNWAY_IN_block);
00275 }
00276
00277 return true;
00278 }
00279
00286 static bool DisasterTick_Ufo(DisasterVehicle *v)
00287 {
00288 v->image_override = (HasBit(++v->tick_counter, 3)) ? SPR_UFO_SMALL_SCOUT_DARKER : SPR_UFO_SMALL_SCOUT;
00289
00290 if (v->current_order.GetDestination() == 0) {
00291
00292 int x = TileX(v->dest_tile) * TILE_SIZE;
00293 int y = TileY(v->dest_tile) * TILE_SIZE;
00294 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= TILE_SIZE) {
00295 v->direction = GetDirectionTowards(v, x, y);
00296 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00297 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00298 return true;
00299 }
00300 if (++v->age < 6) {
00301 v->dest_tile = RandomTile();
00302 return true;
00303 }
00304 v->current_order.SetDestination(1);
00305
00306 RoadVehicle *u;
00307 FOR_ALL_ROADVEHICLES(u) {
00308 if (u->IsRoadVehFront()) {
00309 v->dest_tile = u->index;
00310 v->age = 0;
00311 return true;
00312 }
00313 }
00314
00315 delete v;
00316 return false;
00317 } else {
00318
00319 RoadVehicle *u = RoadVehicle::Get(v->dest_tile);
00320 assert(u != NULL && u->type == VEH_ROAD && u->IsRoadVehFront());
00321
00322 uint dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos);
00323
00324 if (dist < TILE_SIZE && !(u->vehstatus & VS_HIDDEN) && u->breakdown_ctr == 0) {
00325 u->breakdown_ctr = 3;
00326 u->breakdown_delay = 140;
00327 }
00328
00329 v->direction = GetDirectionTowards(v, u->x_pos, u->y_pos);
00330 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00331
00332 byte z = v->z_pos;
00333 if (dist <= TILE_SIZE && z > u->z_pos) z--;
00334 SetDisasterVehiclePos(v, gp.x, gp.y, z);
00335
00336 if (z <= u->z_pos && (u->vehstatus & VS_HIDDEN) == 0) {
00337 v->age++;
00338 if (u->crashed_ctr == 0) {
00339 u->Crash();
00340
00341 AddVehicleNewsItem(STR_NEWS_DISASTER_SMALL_UFO,
00342 NS_ACCIDENT,
00343 u->index);
00344
00345 AI::NewEvent(u->owner, new AIEventVehicleCrashed(u->index, u->tile, AIEventVehicleCrashed::CRASH_RV_UFO));
00346 }
00347 }
00348
00349
00350 if (v->age > 50) {
00351 CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
00352 SndPlayVehicleFx(SND_12_EXPLOSION, v);
00353 delete v;
00354 return false;
00355 }
00356 }
00357
00358 return true;
00359 }
00360
00361 static void DestructIndustry(Industry *i)
00362 {
00363 for (TileIndex tile = 0; tile != MapSize(); tile++) {
00364 if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == i->index) {
00365 ResetIndustryConstructionStage(tile);
00366 MarkTileDirtyByTile(tile);
00367 }
00368 }
00369 }
00370
00384 static bool DisasterTick_Aircraft(DisasterVehicle *v, uint16 image_override, bool leave_at_top, StringID news_message, IndustryBehaviour industry_flag)
00385 {
00386 v->tick_counter++;
00387 v->image_override = (v->current_order.GetDestination() == 1 && HasBit(v->tick_counter, 2)) ? image_override : 0;
00388
00389 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00390 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00391
00392 if ((leave_at_top && gp.x < (-10 * TILE_SIZE)) || (!leave_at_top && gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1)) {
00393 delete v;
00394 return false;
00395 }
00396
00397 if (v->current_order.GetDestination() == 2) {
00398 if (GB(v->tick_counter, 0, 2) == 0) {
00399 Industry *i = Industry::Get(v->dest_tile);
00400 int x = TileX(i->location.tile) * TILE_SIZE;
00401 int y = TileY(i->location.tile) * TILE_SIZE;
00402 uint32 r = Random();
00403
00404 CreateEffectVehicleAbove(
00405 GB(r, 0, 6) + x,
00406 GB(r, 6, 6) + y,
00407 GB(r, 12, 4),
00408 EV_EXPLOSION_SMALL);
00409
00410 if (++v->age >= 55) v->current_order.SetDestination(3);
00411 }
00412 } else if (v->current_order.GetDestination() == 1) {
00413 if (++v->age == 112) {
00414 v->current_order.SetDestination(2);
00415 v->age = 0;
00416
00417 Industry *i = Industry::Get(v->dest_tile);
00418 DestructIndustry(i);
00419
00420 SetDParam(0, i->town->index);
00421 AddIndustryNewsItem(news_message, NS_ACCIDENT, i->index);
00422 SndPlayTileFx(SND_12_EXPLOSION, i->location.tile);
00423 }
00424 } else if (v->current_order.GetDestination() == 0) {
00425 int x = v->x_pos + ((leave_at_top ? -15 : 15) * TILE_SIZE);
00426 int y = v->y_pos;
00427
00428 if ((uint)x > MapMaxX() * TILE_SIZE - 1) return true;
00429
00430 TileIndex tile = TileVirtXY(x, y);
00431 if (!IsTileType(tile, MP_INDUSTRY)) return true;
00432
00433 IndustryID ind = GetIndustryIndex(tile);
00434 v->dest_tile = ind;
00435
00436 if (GetIndustrySpec(Industry::Get(ind)->type)->behaviour & industry_flag) {
00437 v->current_order.SetDestination(1);
00438 v->age = 0;
00439 }
00440 }
00441
00442 return true;
00443 }
00444
00446 static bool DisasterTick_Airplane(DisasterVehicle *v)
00447 {
00448 return DisasterTick_Aircraft(v, SPR_F_15_FIRING, true, STR_NEWS_DISASTER_AIRPLANE_OIL_REFINERY, INDUSTRYBEH_AIRPLANE_ATTACKS);
00449 }
00450
00452 static bool DisasterTick_Helicopter(DisasterVehicle *v)
00453 {
00454 return DisasterTick_Aircraft(v, SPR_AH_64A_FIRING, false, STR_NEWS_DISASTER_HELICOPTER_FACTORY, INDUSTRYBEH_CHOPPER_ATTACKS);
00455 }
00456
00458 static bool DisasterTick_Helicopter_Rotors(DisasterVehicle *v)
00459 {
00460 v->tick_counter++;
00461 if (HasBit(v->tick_counter, 0)) return true;
00462
00463 if (++v->cur_image > SPR_ROTOR_MOVING_3) v->cur_image = SPR_ROTOR_MOVING_1;
00464
00465 VehicleMove(v, true);
00466
00467 return true;
00468 }
00469
00476 static bool DisasterTick_Big_Ufo(DisasterVehicle *v)
00477 {
00478 v->tick_counter++;
00479
00480 if (v->current_order.GetDestination() == 1) {
00481 int x = TileX(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00482 int y = TileY(v->dest_tile) * TILE_SIZE + TILE_SIZE / 2;
00483 if (Delta(v->x_pos, x) + Delta(v->y_pos, y) >= 8) {
00484 v->direction = GetDirectionTowards(v, x, y);
00485
00486 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00487 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00488 return true;
00489 }
00490
00491 if (!IsValidTile(v->dest_tile)) {
00492
00493 delete v;
00494 return false;
00495 }
00496
00497 byte z = GetSlopeZ(v->x_pos, v->y_pos);
00498 if (z < v->z_pos) {
00499 SetDisasterVehiclePos(v, v->x_pos, v->y_pos, v->z_pos - 1);
00500 return true;
00501 }
00502
00503 v->current_order.SetDestination(2);
00504
00505 Vehicle *target;
00506 FOR_ALL_VEHICLES(target) {
00507 if (target->type == VEH_TRAIN || target->type == VEH_ROAD) {
00508 if (Delta(target->x_pos, v->x_pos) + Delta(target->y_pos, v->y_pos) <= 12 * TILE_SIZE) {
00509 target->breakdown_ctr = 5;
00510 target->breakdown_delay = 0xF0;
00511 }
00512 }
00513 }
00514
00515 Town *t = ClosestTownFromTile(v->dest_tile, UINT_MAX);
00516 SetDParam(0, t->index);
00517 AddNewsItem(STR_NEWS_DISASTER_BIG_UFO,
00518 NS_ACCIDENT,
00519 NR_TILE,
00520 v->tile);
00521
00522 if (!Vehicle::CanAllocateItem(2)) {
00523 delete v;
00524 return false;
00525 }
00526 DisasterVehicle *u = new DisasterVehicle();
00527
00528 InitializeDisasterVehicle(u, -6 * TILE_SIZE, v->y_pos, 135, DIR_SW, ST_BIG_UFO_DESTROYER);
00529 u->big_ufo_destroyer_target = v->index;
00530
00531 DisasterVehicle *w = new DisasterVehicle();
00532
00533 u->SetNext(w);
00534 InitializeDisasterVehicle(w, -6 * TILE_SIZE, v->y_pos, 0, DIR_SW, ST_BIG_UFO_DESTROYER_SHADOW);
00535 w->vehstatus |= VS_SHADOW;
00536 } else if (v->current_order.GetDestination() == 0) {
00537 int x = TileX(v->dest_tile) * TILE_SIZE;
00538 int y = TileY(v->dest_tile) * TILE_SIZE;
00539 if (Delta(x, v->x_pos) + Delta(y, v->y_pos) >= TILE_SIZE) {
00540 v->direction = GetDirectionTowards(v, x, y);
00541 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00542 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00543 return true;
00544 }
00545
00546 if (++v->age < 6) {
00547 v->dest_tile = RandomTile();
00548 return true;
00549 }
00550 v->current_order.SetDestination(1);
00551
00552 TileIndex tile_org = RandomTile();
00553 TileIndex tile = tile_org;
00554 do {
00555 if (IsPlainRailTile(tile) &&
00556 Company::IsHumanID(GetTileOwner(tile))) {
00557 break;
00558 }
00559 tile = TILE_MASK(tile + 1);
00560 } while (tile != tile_org);
00561 v->dest_tile = tile;
00562 v->age = 0;
00563 }
00564
00565 return true;
00566 }
00567
00572 static bool DisasterTick_Big_Ufo_Destroyer(DisasterVehicle *v)
00573 {
00574 v->tick_counter++;
00575
00576 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00577 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00578
00579 if (gp.x > (int)MapSizeX() * TILE_SIZE + 9 * TILE_SIZE - 1) {
00580 delete v;
00581 return false;
00582 }
00583
00584 if (v->current_order.GetDestination() == 0) {
00585 Vehicle *u = Vehicle::Get(v->big_ufo_destroyer_target);
00586 if (Delta(v->x_pos, u->x_pos) > TILE_SIZE) return true;
00587 v->current_order.SetDestination(1);
00588
00589 CreateEffectVehicleRel(u, 0, 7, 8, EV_EXPLOSION_LARGE);
00590 SndPlayVehicleFx(SND_12_EXPLOSION, u);
00591
00592 delete u;
00593
00594 for (int i = 0; i != 80; i++) {
00595 uint32 r = Random();
00596 CreateEffectVehicleAbove(
00597 GB(r, 0, 6) + v->x_pos - 32,
00598 GB(r, 5, 6) + v->y_pos - 32,
00599 0,
00600 EV_EXPLOSION_SMALL);
00601 }
00602
00603 for (int dy = -3; dy < 3; dy++) {
00604 for (int dx = -3; dx < 3; dx++) {
00605 TileIndex tile = TileAddWrap(v->tile, dx, dy);
00606 if (tile != INVALID_TILE) DisasterClearSquare(tile);
00607 }
00608 }
00609 }
00610
00611 return true;
00612 }
00613
00618 static bool DisasterTick_Submarine(DisasterVehicle *v)
00619 {
00620 v->tick_counter++;
00621
00622 if (++v->age > 8880) {
00623 delete v;
00624 return false;
00625 }
00626
00627 if (!HasBit(v->tick_counter, 0)) return true;
00628
00629 TileIndex tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
00630 if (IsValidTile(tile)) {
00631 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
00632 if (trackbits == TRACK_BIT_ALL && !Chance16(1, 90)) {
00633 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00634 SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
00635 return true;
00636 }
00637 }
00638
00639 v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT);
00640
00641 return true;
00642 }
00643
00644
00645 static bool DisasterTick_NULL(DisasterVehicle *v)
00646 {
00647 return true;
00648 }
00649
00650 typedef bool DisasterVehicleTickProc(DisasterVehicle *v);
00651
00652 static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = {
00653 DisasterTick_Zeppeliner, DisasterTick_NULL,
00654 DisasterTick_Ufo, DisasterTick_NULL,
00655 DisasterTick_Airplane, DisasterTick_NULL,
00656 DisasterTick_Helicopter, DisasterTick_NULL, DisasterTick_Helicopter_Rotors,
00657 DisasterTick_Big_Ufo, DisasterTick_NULL, DisasterTick_Big_Ufo_Destroyer,
00658 DisasterTick_NULL,
00659 DisasterTick_Submarine,
00660 DisasterTick_Submarine,
00661 };
00662
00663
00664 bool DisasterVehicle::Tick()
00665 {
00666 return _disastervehicle_tick_procs[this->subtype](this);
00667 }
00668
00669 typedef void DisasterInitProc();
00670
00671
00674 static void Disaster_Zeppeliner_Init()
00675 {
00676 if (!Vehicle::CanAllocateItem(2)) return;
00677
00678
00679 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00680
00681 Station *st;
00682 FOR_ALL_STATIONS(st) {
00683 if (st->airport_tile != INVALID_TILE && (st->airport_type == AT_SMALL || st->airport_type == AT_LARGE)) {
00684 x = (TileX(st->airport_tile) + 2) * TILE_SIZE;
00685 break;
00686 }
00687 }
00688
00689 DisasterVehicle *v = new DisasterVehicle();
00690 InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_ZEPPELINER);
00691
00692
00693 DisasterVehicle *u = new DisasterVehicle();
00694 v->SetNext(u);
00695 InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_ZEPPELINER_SHADOW);
00696 u->vehstatus |= VS_SHADOW;
00697 }
00698
00699
00702 static void Disaster_Small_Ufo_Init()
00703 {
00704 if (!Vehicle::CanAllocateItem(2)) return;
00705
00706 DisasterVehicle *v = new DisasterVehicle();
00707 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00708
00709 InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, ST_SMALL_UFO);
00710 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00711 v->age = 0;
00712
00713
00714 DisasterVehicle *u = new DisasterVehicle();
00715 v->SetNext(u);
00716 InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, ST_SMALL_UFO_SHADOW);
00717 u->vehstatus |= VS_SHADOW;
00718 }
00719
00720
00721
00722 static void Disaster_Airplane_Init()
00723 {
00724 if (!Vehicle::CanAllocateItem(2)) return;
00725
00726 Industry *i, *found = NULL;
00727
00728 FOR_ALL_INDUSTRIES(i) {
00729 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
00730 (found == NULL || Chance16(1, 2))) {
00731 found = i;
00732 }
00733 }
00734
00735 if (found == NULL) return;
00736
00737 DisasterVehicle *v = new DisasterVehicle();
00738
00739
00740 int x = (MapSizeX() + 9) * TILE_SIZE - 1;
00741 int y = TileY(found->location.tile) * TILE_SIZE + 37;
00742
00743 InitializeDisasterVehicle(v, x, y, 135, DIR_NE, ST_AIRPLANE);
00744
00745 DisasterVehicle *u = new DisasterVehicle();
00746 v->SetNext(u);
00747 InitializeDisasterVehicle(u, x, y, 0, DIR_SE, ST_AIRPLANE_SHADOW);
00748 u->vehstatus |= VS_SHADOW;
00749 }
00750
00751
00753 static void Disaster_Helicopter_Init()
00754 {
00755 if (!Vehicle::CanAllocateItem(3)) return;
00756
00757 Industry *i, *found = NULL;
00758
00759 FOR_ALL_INDUSTRIES(i) {
00760 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
00761 (found == NULL || Chance16(1, 2))) {
00762 found = i;
00763 }
00764 }
00765
00766 if (found == NULL) return;
00767
00768 DisasterVehicle *v = new DisasterVehicle();
00769
00770 int x = -16 * TILE_SIZE;
00771 int y = TileY(found->location.tile) * TILE_SIZE + 37;
00772
00773 InitializeDisasterVehicle(v, x, y, 135, DIR_SW, ST_HELICOPTER);
00774
00775 DisasterVehicle *u = new DisasterVehicle();
00776 v->SetNext(u);
00777 InitializeDisasterVehicle(u, x, y, 0, DIR_SW, ST_HELICOPTER_SHADOW);
00778 u->vehstatus |= VS_SHADOW;
00779
00780 DisasterVehicle *w = new DisasterVehicle();
00781 u->SetNext(w);
00782 InitializeDisasterVehicle(w, x, y, 140, DIR_SW, ST_HELICOPTER_ROTORS);
00783 }
00784
00785
00786
00787
00788 static void Disaster_Big_Ufo_Init()
00789 {
00790 if (!Vehicle::CanAllocateItem(2)) return;
00791
00792 DisasterVehicle *v = new DisasterVehicle();
00793 int x = TileX(Random()) * TILE_SIZE + TILE_SIZE / 2;
00794 int y = MapMaxX() * TILE_SIZE - 1;
00795
00796 InitializeDisasterVehicle(v, x, y, 135, DIR_NW, ST_BIG_UFO);
00797 v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2);
00798 v->age = 0;
00799
00800
00801 DisasterVehicle *u = new DisasterVehicle();
00802 v->SetNext(u);
00803 InitializeDisasterVehicle(u, x, y, 0, DIR_NW, ST_BIG_UFO_SHADOW);
00804 u->vehstatus |= VS_SHADOW;
00805 }
00806
00807
00808 static void Disaster_Submarine_Init(DisasterSubType subtype)
00809 {
00810 if (!Vehicle::CanAllocateItem()) return;
00811
00812 int y;
00813 Direction dir;
00814 uint32 r = Random();
00815 int x = TileX(r) * TILE_SIZE + TILE_SIZE / 2;
00816
00817 if (HasBit(r, 31)) {
00818 y = MapMaxY() * TILE_SIZE - TILE_SIZE / 2 - 1;
00819 dir = DIR_NW;
00820 } else {
00821 y = TILE_SIZE / 2;
00822 if (_settings_game.construction.freeform_edges) y += TILE_SIZE;
00823 dir = DIR_SE;
00824 }
00825 if (!IsWaterTile(TileVirtXY(x, y))) return;
00826
00827 DisasterVehicle *v = new DisasterVehicle();
00828 InitializeDisasterVehicle(v, x, y, 0, dir, subtype);
00829 v->age = 0;
00830 }
00831
00832
00833 static void Disaster_Small_Submarine_Init()
00834 {
00835 Disaster_Submarine_Init(ST_SMALL_SUBMARINE);
00836 }
00837
00838
00839
00840 static void Disaster_Big_Submarine_Init()
00841 {
00842 Disaster_Submarine_Init(ST_BIG_SUBMARINE);
00843 }
00844
00845
00848 static void Disaster_CoalMine_Init()
00849 {
00850 int index = GB(Random(), 0, 4);
00851 uint m;
00852
00853 for (m = 0; m < 15; m++) {
00854 const Industry *i;
00855
00856 FOR_ALL_INDUSTRIES(i) {
00857 if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
00858 SetDParam(0, i->town->index);
00859 AddNewsItem(STR_NEWS_DISASTER_COAL_MINE_SUBSIDENCE,
00860 NS_ACCIDENT, NR_TILE, i->location.tile + TileDiffXY(1, 1));
00861
00862 {
00863 TileIndex tile = i->location.tile;
00864 TileIndexDiff step = TileOffsByDiagDir((DiagDirection)GB(Random(), 0, 2));
00865
00866 for (uint n = 0; n < 30; n++) {
00867 DisasterClearSquare(tile);
00868 tile += step;
00869 if (!IsValidTile(tile)) break;
00870 }
00871 }
00872 return;
00873 }
00874 }
00875 }
00876 }
00877
00878 struct Disaster {
00879 DisasterInitProc *init_proc;
00880 Year min_year;
00881 Year max_year;
00882 };
00883
00884 static const Disaster _disasters[] = {
00885 {Disaster_Zeppeliner_Init, 1930, 1955},
00886 {Disaster_Small_Ufo_Init, 1940, 1970},
00887 {Disaster_Airplane_Init, 1960, 1990},
00888 {Disaster_Helicopter_Init, 1970, 2000},
00889 {Disaster_Big_Ufo_Init, 2000, 2100},
00890 {Disaster_Small_Submarine_Init, 1940, 1965},
00891 {Disaster_Big_Submarine_Init, 1975, 2010},
00892 {Disaster_CoalMine_Init, 1950, 1985},
00893 };
00894
00895 static void DoDisaster()
00896 {
00897 byte buf[lengthof(_disasters)];
00898
00899 byte j = 0;
00900 for (size_t i = 0; i != lengthof(_disasters); i++) {
00901 if (_cur_year >= _disasters[i].min_year && _cur_year < _disasters[i].max_year) buf[j++] = (byte)i;
00902 }
00903
00904 if (j == 0) return;
00905
00906 _disasters[buf[RandomRange(j)]].init_proc();
00907 }
00908
00909
00910 static void ResetDisasterDelay()
00911 {
00912 _disaster_delay = GB(Random(), 0, 9) + 730;
00913 }
00914
00915 void DisasterDailyLoop()
00916 {
00917 if (--_disaster_delay != 0) return;
00918
00919 ResetDisasterDelay();
00920
00921 if (_settings_game.difficulty.disasters != 0) DoDisaster();
00922 }
00923
00924 void StartupDisasters()
00925 {
00926 ResetDisasterDelay();
00927 }
00928
00933 void ReleaseDisastersTargetingIndustry(IndustryID i)
00934 {
00935 DisasterVehicle *v;
00936 FOR_ALL_DISASTERVEHICLES(v) {
00937
00938 if (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER) {
00939
00940 if (v->current_order.GetDestination() > 0 && v->dest_tile == i) v->current_order.SetDestination(3);
00941 }
00942 }
00943 }
00944
00948 void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
00949 {
00950 DisasterVehicle *v;
00951 FOR_ALL_DISASTERVEHICLES(v) {
00952
00953 if (v->subtype == ST_SMALL_UFO) {
00954 if (v->current_order.GetDestination() != 0 && v->dest_tile == vehicle) {
00955
00956 v->current_order.SetDestination(0);
00957 v->dest_tile = RandomTile();
00958 v->z_pos = 135;
00959 v->age = 0;
00960 }
00961 }
00962 }
00963 }
00964
00965 void DisasterVehicle::UpdateDeltaXY(Direction direction)
00966 {
00967 this->x_offs = -1;
00968 this->y_offs = -1;
00969 this->x_extent = 2;
00970 this->y_extent = 2;
00971 this->z_extent = 5;
00972 }