00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "landscape.h"
00014 #include "command_func.h"
00015 #include "viewport_func.h"
00016 #include "company_base.h"
00017 #include "town.h"
00018 #include "bridge_map.h"
00019 #include "genworld.h"
00020 #include "autoslope.h"
00021 #include "clear_func.h"
00022 #include "water.h"
00023 #include "window_func.h"
00024 #include "company_gui.h"
00025 #include "cheat_type.h"
00026 #include "object.h"
00027 #include "cargopacket.h"
00028 #include "sprite.h"
00029 #include "core/random_func.hpp"
00030 #include "core/pool_func.hpp"
00031 #include "object_map.h"
00032 #include "object_base.h"
00033 #include "newgrf_config.h"
00034 #include "newgrf_object.h"
00035 #include "date_func.h"
00036 #include "newgrf_debug.h"
00037
00038 #include "table/strings.h"
00039 #include "table/object_land.h"
00040
00041 ObjectPool _object_pool("Object");
00042 INSTANTIATE_POOL_METHODS(Object)
00043 uint16 Object::counts[NUM_OBJECTS];
00044
00050 Object *Object::GetByTile(TileIndex tile)
00051 {
00052 return Object::Get(GetObjectIndex(tile));
00053 }
00054
00056 void InitializeObjects()
00057 {
00058 _object_pool.CleanPool();
00059 Object::ResetTypeCounts();
00060 }
00061
00072 void BuildObject(ObjectType type, TileIndex tile, CompanyID owner, Town *town, uint8 view)
00073 {
00074 const ObjectSpec *spec = ObjectSpec::Get(type);
00075
00076 TileArea ta(tile, GB(spec->size, HasBit(view, 0) ? 4 : 0, 4), GB(spec->size, HasBit(view, 0) ? 0 : 4, 4));
00077 Object *o = new Object();
00078 o->location = ta;
00079 o->town = town == NULL ? CalcClosestTownFromTile(tile) : town;
00080 o->build_date = _date;
00081 o->view = view;
00082
00083
00084
00085 if (owner == OWNER_NONE) {
00086 o->colour = Random();
00087 } else {
00088 const Livery *l = Company::Get(owner)->livery;
00089 o->colour = l->colour1 + l->colour2 * 16;
00090 }
00091
00092
00093 if ((spec->flags & OBJECT_FLAG_2CC_COLOUR) == 0) o->colour &= 0xF;
00094
00095 if (HasBit(spec->callback_mask, CBM_OBJ_COLOUR)) {
00096 uint16 res = GetObjectCallback(CBID_OBJECT_COLOUR, o->colour, 0, spec, o, tile);
00097 if (res != CALLBACK_FAILED) o->colour = GB(res, 0, 8);
00098 }
00099
00100 assert(o->town != NULL);
00101
00102 TILE_AREA_LOOP(t, ta) {
00103 WaterClass wc = (IsWaterTile(t) ? GetWaterClass(t) : WATER_CLASS_INVALID);
00104 MakeObject(t, type, owner, o->index, wc, Random());
00105 MarkTileDirtyByTile(t);
00106 }
00107
00108 Object::IncTypeCount(type);
00109 if (spec->flags & OBJECT_FLAG_ANIMATION) TriggerObjectAnimation(o, OAT_BUILT, spec);
00110 }
00111
00116 static void IncreaseAnimationStage(TileIndex tile)
00117 {
00118 TileArea ta = Object::GetByTile(tile)->location;
00119 TILE_AREA_LOOP(t, ta) {
00120 SetAnimationFrame(t, GetAnimationFrame(t) + 1);
00121 MarkTileDirtyByTile(t);
00122 }
00123 }
00124
00126 #define GetCompanyHQSize GetAnimationFrame
00127
00128 #define IncreaseCompanyHQSize IncreaseAnimationStage
00129
00135 void UpdateCompanyHQ(TileIndex tile, uint score)
00136 {
00137 if (tile == INVALID_TILE) return;
00138
00139 byte val;
00140 (val = 0, score < 170) ||
00141 (val++, score < 350) ||
00142 (val++, score < 520) ||
00143 (val++, score < 720) ||
00144 (val++, true);
00145
00146 while (GetCompanyHQSize(tile) < val) {
00147 IncreaseCompanyHQSize(tile);
00148 }
00149 }
00150
00155 void UpdateObjectColours(const Company *c)
00156 {
00157 Object *obj;
00158 FOR_ALL_OBJECTS(obj) {
00159 Owner owner = GetTileOwner(obj->location.tile);
00160
00161 if (owner != c->index) continue;
00162
00163 const ObjectSpec *spec = ObjectSpec::GetByTile(obj->location.tile);
00164
00165 if (HasBit(spec->callback_mask, CBM_OBJ_COLOUR)) continue;
00166
00167 const Livery *l = c->livery;
00168 obj->colour = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? (l->colour2 * 16) : 0) + l->colour1;
00169 }
00170 }
00171
00172 extern CommandCost CheckBuildableTile(TileIndex tile, uint invalid_dirs, int &allowed_z, bool check_bridge);
00173 static CommandCost ClearTile_Object(TileIndex tile, DoCommandFlag flags);
00174
00184 CommandCost CmdBuildObject(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00185 {
00186 CommandCost cost(EXPENSES_PROPERTY);
00187
00188 ObjectType type = (ObjectType)GB(p1, 0, 8);
00189 uint8 view = GB(p2, 0, 2);
00190 const ObjectSpec *spec = ObjectSpec::Get(type);
00191 if (!spec->IsAvailable()) return CMD_ERROR;
00192
00193 if (spec->flags & OBJECT_FLAG_ONLY_IN_SCENEDIT && (_game_mode != GM_EDITOR || _current_company != OWNER_NONE)) return CMD_ERROR;
00194 if (spec->flags & OBJECT_FLAG_ONLY_IN_GAME && (_game_mode != GM_NORMAL || _current_company > MAX_COMPANIES)) return CMD_ERROR;
00195 if (view >= spec->views) return CMD_ERROR;
00196
00197 if (!Object::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_OBJECTS);
00198 if (Town::GetNumItems() == 0) return_cmd_error(STR_ERROR_MUST_FOUND_TOWN_FIRST);
00199
00200 int size_x = GB(spec->size, HasBit(view, 0) ? 4 : 0, 4);
00201 int size_y = GB(spec->size, HasBit(view, 0) ? 0 : 4, 4);
00202 TileArea ta(tile, size_x, size_y);
00203
00204 if (type == OBJECT_OWNED_LAND) {
00205
00206 cost.AddCost(DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR));
00207 } else {
00208
00209 bool allow_water = (spec->flags & (OBJECT_FLAG_BUILT_ON_WATER | OBJECT_FLAG_NOT_ON_LAND)) != 0;
00210 bool allow_ground = (spec->flags & OBJECT_FLAG_NOT_ON_LAND) == 0;
00211 TILE_AREA_LOOP(t, ta) {
00212 if (HasTileWaterGround(t)) {
00213 if (!allow_water) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
00214 if (!IsWaterTile(t)) {
00215
00216
00217 cost.AddCost(DoCommand(t, 0, 0, flags & ~DC_NO_WATER, CMD_LANDSCAPE_CLEAR));
00218 } else {
00219
00220 Owner o = GetTileOwner(t);
00221 if (o != OWNER_NONE && o != OWNER_WATER) cost.AddCost(CheckOwnership(o, t));
00222 }
00223 } else {
00224 if (!allow_ground) return_cmd_error(STR_ERROR_MUST_BE_BUILT_ON_WATER);
00225
00226 cost.AddCost(DoCommand(t, 0, 0, flags, CMD_LANDSCAPE_CLEAR));
00227 }
00228 }
00229
00230
00231 int allowed_z;
00232 if (GetTileSlope(tile, (uint*)&allowed_z) != SLOPE_FLAT) allowed_z += TILE_HEIGHT;
00233
00234 TILE_AREA_LOOP(t, ta) {
00235 uint16 callback = CALLBACK_FAILED;
00236 if (HasBit(spec->callback_mask, CBM_OBJ_SLOPE_CHECK)) {
00237 TileIndex diff = t - tile;
00238 callback = GetObjectCallback(CBID_OBJECT_LAND_SLOPE_CHECK, GetTileSlope(t, NULL), TileY(diff) << 4 | TileX(diff), spec, NULL, t, view);
00239 }
00240
00241 if (callback == CALLBACK_FAILED) {
00242 cost.AddCost(CheckBuildableTile(t, 0, allowed_z, false));
00243 } else if (callback != 0) {
00244 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00245 }
00246 }
00247 }
00248 if (cost.Failed()) return cost;
00249
00250
00251 TILE_AREA_LOOP(t, ta) {
00252 if (MayHaveBridgeAbove(t) && IsBridgeAbove(t) && (
00253 !(spec->flags & OBJECT_FLAG_ALLOW_UNDER_BRIDGE) ||
00254 (GetTileMaxZ(t) + spec->height * TILE_HEIGHT >= GetBridgeHeight(GetSouthernBridgeEnd(t))))) {
00255 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00256 }
00257 }
00258
00259 int hq_score = 0;
00260 switch (type) {
00261 case OBJECT_TRANSMITTER:
00262 case OBJECT_LIGHTHOUSE:
00263 if (GetTileSlope(tile, NULL) != SLOPE_FLAT) return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
00264 break;
00265
00266 case OBJECT_OWNED_LAND:
00267 if (IsTileType(tile, MP_OBJECT) &&
00268 IsTileOwner(tile, _current_company) &&
00269 IsOwnedLand(tile)) {
00270 return_cmd_error(STR_ERROR_YOU_ALREADY_OWN_IT);
00271 }
00272 break;
00273
00274 case OBJECT_HQ: {
00275 Company *c = Company::Get(_current_company);
00276 if (c->location_of_HQ != INVALID_TILE) {
00277
00278 _current_company = OWNER_WATER;
00279 cost.AddCost(ClearTile_Object(c->location_of_HQ, flags));
00280 _current_company = c->index;
00281 }
00282
00283 if (flags & DC_EXEC) {
00284 hq_score = UpdateCompanyRatingAndValue(c, false);
00285 c->location_of_HQ = tile;
00286 SetWindowDirty(WC_COMPANY, c->index);
00287 }
00288 break;
00289 }
00290
00291 case OBJECT_STATUE:
00292
00293 return CMD_ERROR;
00294
00295 default:
00296 break;
00297 }
00298
00299 if (flags & DC_EXEC) {
00300 BuildObject(type, tile, _current_company, NULL, view);
00301
00302
00303 if (type == OBJECT_HQ) UpdateCompanyHQ(tile, hq_score);
00304 }
00305
00306 cost.AddCost(ObjectSpec::Get(type)->GetBuildCost() * size_x * size_y);
00307 return cost;
00308 }
00309
00310
00311 static Foundation GetFoundation_Object(TileIndex tile, Slope tileh);
00312
00313 static void DrawTile_Object(TileInfo *ti)
00314 {
00315 ObjectType type = GetObjectType(ti->tile);
00316 const ObjectSpec *spec = ObjectSpec::Get(type);
00317
00318
00319 if (!spec->enabled) type = OBJECT_TRANSMITTER;
00320
00321 if ((spec->flags & OBJECT_FLAG_HAS_NO_FOUNDATION) == 0) DrawFoundation(ti, GetFoundation_Object(ti->tile, ti->tileh));
00322
00323 if (type < NEW_OBJECT_OFFSET) {
00324 const DrawTileSprites *dts = NULL;
00325 Owner to = GetTileOwner(ti->tile);
00326 PaletteID palette = to == OWNER_NONE ? PAL_NONE : COMPANY_SPRITE_COLOUR(to);
00327
00328 if (type == OBJECT_HQ) {
00329 TileIndex diff = ti->tile - Object::GetByTile(ti->tile)->location.tile;
00330 dts = &_object_hq[GetCompanyHQSize(ti->tile) << 2 | TileY(diff) << 1 | TileX(diff)];
00331 } else {
00332 dts = &_objects[type];
00333 }
00334
00335 if (spec->flags & OBJECT_FLAG_HAS_NO_FOUNDATION) {
00336
00337
00338 switch (dts->ground.sprite) {
00339 case SPR_FLAT_BARE_LAND: DrawClearLandTile(ti, 0); break;
00340 case SPR_FLAT_1_THIRD_GRASS_TILE: DrawClearLandTile(ti, 1); break;
00341 case SPR_FLAT_2_THIRD_GRASS_TILE: DrawClearLandTile(ti, 2); break;
00342 case SPR_FLAT_GRASS_TILE: DrawClearLandTile(ti, 3); break;
00343 default: DrawGroundSprite(dts->ground.sprite, palette); break;
00344 }
00345 } else {
00346 DrawGroundSprite(dts->ground.sprite, palette);
00347 }
00348
00349 if (!IsInvisibilitySet(TO_STRUCTURES)) {
00350 const DrawTileSeqStruct *dtss;
00351 foreach_draw_tile_seq(dtss, dts->seq) {
00352 AddSortableSpriteToDraw(
00353 dtss->image.sprite, palette,
00354 ti->x + dtss->delta_x, ti->y + dtss->delta_y,
00355 dtss->size_x, dtss->size_y,
00356 dtss->size_z, ti->z + dtss->delta_z,
00357 IsTransparencySet(TO_STRUCTURES)
00358 );
00359 }
00360 }
00361 } else {
00362 DrawNewObjectTile(ti, spec);
00363 }
00364
00365 if (spec->flags & OBJECT_FLAG_ALLOW_UNDER_BRIDGE) DrawBridgeMiddle(ti);
00366 }
00367
00368 static uint GetSlopeZ_Object(TileIndex tile, uint x, uint y)
00369 {
00370 if (IsOwnedLand(tile)) {
00371 uint z;
00372 Slope tileh = GetTileSlope(tile, &z);
00373
00374 return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
00375 } else {
00376 return GetTileMaxZ(tile);
00377 }
00378 }
00379
00380 static Foundation GetFoundation_Object(TileIndex tile, Slope tileh)
00381 {
00382 return IsOwnedLand(tile) ? FOUNDATION_NONE : FlatteningFoundation(tileh);
00383 }
00384
00389 static void ReallyClearObjectTile(Object *o)
00390 {
00391 Object::DecTypeCount(GetObjectType(o->location.tile));
00392 TILE_AREA_LOOP(tile_cur, o->location) {
00393 DeleteNewGRFInspectWindow(GSF_OBJECTS, tile_cur);
00394
00395 MakeWaterKeepingClass(tile_cur, GetTileOwner(tile_cur));
00396 }
00397 delete o;
00398 }
00399
00400 SmallVector<ClearedObjectArea, 4> _cleared_object_areas;
00401
00407 ClearedObjectArea *FindClearedObject(TileIndex tile)
00408 {
00409 TileArea ta = TileArea(tile, 1, 1);
00410
00411 const ClearedObjectArea *end = _cleared_object_areas.End();
00412 for (ClearedObjectArea *coa = _cleared_object_areas.Begin(); coa != end; coa++) {
00413 if (coa->area.Intersects(ta)) return coa;
00414 }
00415
00416 return NULL;
00417 }
00418
00419 static CommandCost ClearTile_Object(TileIndex tile, DoCommandFlag flags)
00420 {
00421 ObjectType type = GetObjectType(tile);
00422 const ObjectSpec *spec = ObjectSpec::Get(type);
00423
00424
00425 Object *o = Object::GetByTile(tile);
00426 TileArea ta = o->location;
00427
00428 ClearedObjectArea *cleared_area = _cleared_object_areas.Append();
00429 cleared_area->first_tile = tile;
00430 cleared_area->area = ta;
00431
00432 CommandCost cost(EXPENSES_CONSTRUCTION, spec->GetClearCost() * ta.w * ta.h / 5);
00433 if (spec->flags & OBJECT_FLAG_CLEAR_INCOME) cost.MultiplyCost(-1);
00434
00435
00436 if (_current_company != OWNER_WATER) {
00437 if ((flags & DC_NO_WATER) && IsTileOnWater(tile)) {
00438
00439 return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
00440 } else if (!(spec->flags & OBJECT_FLAG_AUTOREMOVE) && (flags & DC_AUTO)) {
00441
00442 return_cmd_error(type == OBJECT_HQ ? STR_ERROR_COMPANY_HEADQUARTERS_IN : STR_ERROR_OBJECT_IN_THE_WAY);
00443 } else if (_game_mode == GM_EDITOR) {
00444
00445 } else if (GetTileOwner(tile) == OWNER_NONE) {
00446
00447 if (!_cheats.magic_bulldozer.value) return CMD_ERROR;
00448 } else if (CheckTileOwnership(tile).Failed()) {
00449
00450 return_cmd_error(STR_ERROR_OWNED_BY);
00451 } else if ((spec->flags & OBJECT_FLAG_CANNOT_REMOVE) != 0 && (spec->flags & OBJECT_FLAG_AUTOREMOVE) == 0) {
00452
00453 if (!_cheats.magic_bulldozer.value) return CMD_ERROR;
00454
00455
00456 cost.MultiplyCost(25);
00457 }
00458 } else if ((spec->flags & (OBJECT_FLAG_BUILT_ON_WATER | OBJECT_FLAG_NOT_ON_LAND)) != 0) {
00459
00460 return CMD_ERROR;
00461 }
00462
00463 switch (type) {
00464 case OBJECT_HQ: {
00465 Company *c = Company::Get(GetTileOwner(tile));
00466 if (flags & DC_EXEC) {
00467 c->location_of_HQ = INVALID_TILE;
00468 SetWindowDirty(WC_COMPANY, c->index);
00469 CargoPacket::InvalidateAllFrom(ST_HEADQUARTERS, c->index);
00470 }
00471
00472
00473 cost = CommandCost(EXPENSES_PROPERTY, CalculateCompanyValue(c) / 100);
00474 break;
00475 }
00476
00477 case OBJECT_STATUE:
00478 if (flags & DC_EXEC) {
00479 Town *town = o->town;
00480 ClrBit(town->statues, GetTileOwner(tile));
00481 SetWindowDirty(WC_TOWN_AUTHORITY, town->index);
00482 }
00483 break;
00484
00485 default:
00486 break;
00487 }
00488
00489 if (flags & DC_EXEC) ReallyClearObjectTile(o);
00490
00491 return cost;
00492 }
00493
00494 static void AddAcceptedCargo_Object(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
00495 {
00496 if (!IsCompanyHQ(tile)) return;
00497
00498
00499
00500
00501
00502 uint level = GetCompanyHQSize(tile) + 1;
00503
00504
00505
00506 acceptance[CT_PASSENGERS] += max(1U, level);
00507 SetBit(*always_accepted, CT_PASSENGERS);
00508
00509
00510
00511
00512
00513 acceptance[CT_MAIL] += max(1U, level / 2);
00514 SetBit(*always_accepted, CT_MAIL);
00515 }
00516
00517
00518 static void GetTileDesc_Object(TileIndex tile, TileDesc *td)
00519 {
00520 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00521 td->str = spec->name;
00522 td->owner[0] = GetTileOwner(tile);
00523 td->build_date = Object::GetByTile(tile)->build_date;
00524
00525 if (spec->grf_prop.grffile != NULL) {
00526 td->grf = GetGRFConfig(spec->grf_prop.grffile->grfid)->GetName();
00527 }
00528 }
00529
00530 static void TileLoop_Object(TileIndex tile)
00531 {
00532 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00533 if (spec->flags & OBJECT_FLAG_ANIMATION) {
00534 const Object *o = Object::GetByTile(tile);
00535 TriggerObjectTileAnimation(o, tile, OAT_TILELOOP, spec);
00536 if (o->location.tile == tile) TriggerObjectAnimation(o, OAT_256_TICKS, spec);
00537 }
00538
00539 if (IsTileOnWater(tile)) TileLoop_Water(tile);
00540
00541 if (!IsCompanyHQ(tile)) return;
00542
00543
00544
00545
00546
00547 uint level = GetCompanyHQSize(tile) + 1;
00548 assert(level < 6);
00549
00550 StationFinder stations(TileArea(tile, 2, 2));
00551
00552 uint r = Random();
00553
00554 if (GB(r, 0, 8) < (256 / 4 / (6 - level))) {
00555 uint amt = GB(r, 0, 8) / 8 / 4 + 1;
00556 if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
00557 MoveGoodsToStation(CT_PASSENGERS, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
00558 }
00559
00560
00561
00562
00563 if (GB(r, 8, 8) < (196 / 4 / (6 - level))) {
00564 uint amt = GB(r, 8, 8) / 8 / 4 + 1;
00565 if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
00566 MoveGoodsToStation(CT_MAIL, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
00567 }
00568 }
00569
00570
00571 static TrackStatus GetTileTrackStatus_Object(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00572 {
00573 return 0;
00574 }
00575
00576 static bool ClickTile_Object(TileIndex tile)
00577 {
00578 if (!IsCompanyHQ(tile)) return false;
00579
00580 ShowCompany(GetTileOwner(tile));
00581 return true;
00582 }
00583
00584 static void AnimateTile_Object(TileIndex tile)
00585 {
00586 AnimateNewObjectTile(tile);
00587 }
00588
00595 static bool HasTransmitter(TileIndex tile, void *user)
00596 {
00597 return IsTransmitterTile(tile);
00598 }
00599
00600 void GenerateObjects()
00601 {
00602 if (_settings_game.game_creation.landscape == LT_TOYLAND) return;
00603
00604
00605 int radiotower_to_build = ScaleByMapSize(15);
00606 int lighthouses_to_build = _settings_game.game_creation.landscape == LT_TROPIC ? 0 : ScaleByMapSize1D((Random() & 3) + 7);
00607
00608
00609 if (_settings_game.construction.freeform_edges && lighthouses_to_build != 0) {
00610 uint num_water_tiles = 0;
00611 for (uint x = 0; x < MapMaxX(); x++) {
00612 if (IsTileType(TileXY(x, 1), MP_WATER)) num_water_tiles++;
00613 if (IsTileType(TileXY(x, MapMaxY() - 1), MP_WATER)) num_water_tiles++;
00614 }
00615 for (uint y = 1; y < MapMaxY() - 1; y++) {
00616 if (IsTileType(TileXY(1, y), MP_WATER)) num_water_tiles++;
00617 if (IsTileType(TileXY(MapMaxX() - 1, y), MP_WATER)) num_water_tiles++;
00618 }
00619
00620
00621 lighthouses_to_build = lighthouses_to_build * num_water_tiles / (2 * MapMaxY() + 2 * MapMaxX() - 6);
00622 }
00623
00624 SetGeneratingWorldProgress(GWP_OBJECT, radiotower_to_build + lighthouses_to_build);
00625
00626 for (uint i = ScaleByMapSize(1000); i != 0 && Object::CanAllocateItem(); i--) {
00627 TileIndex tile = RandomTile();
00628
00629 uint h;
00630 if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h >= TILE_HEIGHT * 4 && !IsBridgeAbove(tile)) {
00631 TileIndex t = tile;
00632 if (CircularTileSearch(&t, 9, HasTransmitter, NULL)) continue;
00633
00634 BuildObject(OBJECT_TRANSMITTER, tile);
00635 IncreaseGeneratingWorldProgress(GWP_OBJECT);
00636 if (--radiotower_to_build == 0) break;
00637 }
00638 }
00639
00640
00641 uint maxx = MapMaxX();
00642 uint maxy = MapMaxY();
00643 for (int loop_count = 0; loop_count < 1000 && lighthouses_to_build != 0 && Object::CanAllocateItem(); loop_count++) {
00644 uint r = Random();
00645
00646
00647 int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
00648 DiagDirection dir;
00649 for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
00650 perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
00651 }
00652
00653 TileIndex tile;
00654 switch (dir) {
00655 default:
00656 case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break;
00657 case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break;
00658 case DIAGDIR_SW: tile = TileXY(1, r % maxy); break;
00659 case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break;
00660 }
00661
00662
00663 if (!IsTileType(tile, MP_WATER)) continue;
00664
00665 for (int j = 0; j < 19; j++) {
00666 uint h;
00667 if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h <= TILE_HEIGHT * 2 && !IsBridgeAbove(tile)) {
00668 BuildObject(OBJECT_LIGHTHOUSE, tile);
00669 IncreaseGeneratingWorldProgress(GWP_OBJECT);
00670 lighthouses_to_build--;
00671 assert(tile < MapSize());
00672 break;
00673 }
00674 tile = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDiagDir(dir));
00675 if (tile == INVALID_TILE) break;
00676 }
00677 }
00678 }
00679
00680 static void ChangeTileOwner_Object(TileIndex tile, Owner old_owner, Owner new_owner)
00681 {
00682 if (!IsTileOwner(tile, old_owner)) return;
00683
00684 if (IsOwnedLand(tile) && new_owner != INVALID_OWNER) {
00685 SetTileOwner(tile, new_owner);
00686 } else if (IsStatueTile(tile)) {
00687 Town *t = Object::GetByTile(tile)->town;
00688 ClrBit(t->statues, old_owner);
00689 if (new_owner != INVALID_OWNER && !HasBit(t->statues, new_owner)) {
00690
00691 SetBit(t->statues, new_owner);
00692 SetTileOwner(tile, new_owner);
00693 } else {
00694 ReallyClearObjectTile(Object::GetByTile(tile));
00695 }
00696
00697 SetWindowDirty(WC_TOWN_AUTHORITY, t->index);
00698 } else {
00699 ReallyClearObjectTile(Object::GetByTile(tile));
00700 }
00701 }
00702
00703 static CommandCost TerraformTile_Object(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
00704 {
00705 ObjectType type = GetObjectType(tile);
00706
00707 if (type == OBJECT_OWNED_LAND) {
00708
00709 CommandCost ret = CheckTileOwnership(tile);
00710 if (ret.Succeeded()) return CommandCost();
00711 } else if (AutoslopeEnabled() && type != OBJECT_TRANSMITTER && type != OBJECT_LIGHTHOUSE) {
00712
00713
00714
00715
00716
00717
00718 Slope tileh_old = GetTileSlope(tile, NULL);
00719
00720 if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
00721 const ObjectSpec *spec = ObjectSpec::Get(type);
00722
00723
00724 if (HasBit(spec->callback_mask, CBM_OBJ_AUTOSLOPE)) {
00725
00726 uint16 res = GetObjectCallback(CBID_OBJECT_AUTOSLOPE, 0, 0, spec, Object::GetByTile(tile), tile);
00727 if ((res == 0) || (res == CALLBACK_FAILED)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00728 } else if (spec->enabled) {
00729
00730 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00731 }
00732 }
00733 }
00734
00735 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00736 }
00737
00738 extern const TileTypeProcs _tile_type_object_procs = {
00739 DrawTile_Object,
00740 GetSlopeZ_Object,
00741 ClearTile_Object,
00742 AddAcceptedCargo_Object,
00743 GetTileDesc_Object,
00744 GetTileTrackStatus_Object,
00745 ClickTile_Object,
00746 AnimateTile_Object,
00747 TileLoop_Object,
00748 ChangeTileOwner_Object,
00749 NULL,
00750 NULL,
00751 GetFoundation_Object,
00752 TerraformTile_Object,
00753 };