# object_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    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 "core/random_func.hpp"
    00029 #include "core/pool_func.hpp"
    00030 #include "object_map.h"
    00031 #include "object_base.h"
    00032 #include "newgrf_config.h"
    00033 #include "newgrf_object.h"
    00034 #include "date_func.h"
    00035 #include "newgrf_debug.h"
    00036 #include "vehicle_func.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 /* static */ Object *Object::GetByTile(TileIndex tile)
    00051 {
    00052   return Object::Get(GetObjectIndex(tile));
    00053 }
    00054
    00056 void InitializeObjects()
    00057 {
    00058   Object::ResetTypeCounts();
    00059 }
    00060
    00071 void BuildObject(ObjectType type, TileIndex tile, CompanyID owner, Town *town, uint8 view)
    00072 {
    00073   const ObjectSpec *spec = ObjectSpec::Get(type);
    00074
    00075   TileArea ta(tile, GB(spec->size, HasBit(view, 0) ? 4 : 0, 4), GB(spec->size, HasBit(view, 0) ? 0 : 4, 4));
    00076   Object *o = new Object();
    00077   o->location      = ta;
    00078   o->town          = town == NULL ? CalcClosestTownFromTile(tile) : town;
    00079   o->build_date    = _date;
    00080   o->view          = view;
    00081
    00082   /* If nothing owns the object, the colour will be random. Otherwise
    00083    * get the colour from the company's livery settings. */
    00084   if (owner == OWNER_NONE) {
    00085     o->colour = Random();
    00086   } else {
    00087     const Livery *l = Company::Get(owner)->livery;
    00088     o->colour = l->colour1 + l->colour2 * 16;
    00089   }
    00090
    00091   /* If the object wants only one colour, then give it that colour. */
    00092   if ((spec->flags & OBJECT_FLAG_2CC_COLOUR) == 0) o->colour &= 0xF;
    00093
    00094   if (HasBit(spec->callback_mask, CBM_OBJ_COLOUR)) {
    00095     uint16 res = GetObjectCallback(CBID_OBJECT_COLOUR, o->colour, 0, spec, o, tile);
    00096     if (res != CALLBACK_FAILED) {
    00097       if (res >= 0x100) ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, CBID_OBJECT_COLOUR, res);
    00098       o->colour = GB(res, 0, 8);
    00099     }
    00100   }
    00101
    00102   assert(o->town != NULL);
    00103
    00104   TILE_AREA_LOOP(t, ta) {
    00105     WaterClass wc = (IsWaterTile(t) ? GetWaterClass(t) : WATER_CLASS_INVALID);
    00106     /* Update company infrastructure counts for objects build on canals owned by nobody. */
    00107     if (wc == WATER_CLASS_CANAL && owner != OWNER_NONE && (IsTileOwner(tile, OWNER_NONE) || IsTileOwner(tile, OWNER_WATER))) {
    00108       Company::Get(owner)->infrastructure.water++;
    00109       DirtyCompanyInfrastructureWindows(owner);
    00110     }
    00111     MakeObject(t, type, owner, o->index, wc, Random());
    00112     MarkTileDirtyByTile(t);
    00113   }
    00114
    00115   Object::IncTypeCount(type);
    00116   if (spec->flags & OBJECT_FLAG_ANIMATION) TriggerObjectAnimation(o, OAT_BUILT, spec);
    00117 }
    00118
    00123 static void IncreaseAnimationStage(TileIndex tile)
    00124 {
    00125   TileArea ta = Object::GetByTile(tile)->location;
    00126   TILE_AREA_LOOP(t, ta) {
    00127     SetAnimationFrame(t, GetAnimationFrame(t) + 1);
    00128     MarkTileDirtyByTile(t);
    00129   }
    00130 }
    00131
    00133 #define GetCompanyHQSize GetAnimationFrame
    00134
    00135 #define IncreaseCompanyHQSize IncreaseAnimationStage
    00136
    00142 void UpdateCompanyHQ(TileIndex tile, uint score)
    00143 {
    00144   if (tile == INVALID_TILE) return;
    00145
    00146   byte val;
    00147   (val = 0, score < 170) ||
    00148   (val++, score < 350) ||
    00149   (val++, score < 520) ||
    00150   (val++, score < 720) ||
    00151   (val++, true);
    00152
    00153   while (GetCompanyHQSize(tile) < val) {
    00154     IncreaseCompanyHQSize(tile);
    00155   }
    00156 }
    00157
    00162 void UpdateObjectColours(const Company *c)
    00163 {
    00164   Object *obj;
    00165   FOR_ALL_OBJECTS(obj) {
    00166     Owner owner = GetTileOwner(obj->location.tile);
    00167     /* Not the current owner, so colour doesn't change. */
    00168     if (owner != c->index) continue;
    00169
    00170     const ObjectSpec *spec = ObjectSpec::GetByTile(obj->location.tile);
    00171     /* Using the object colour callback, so not using company colour. */
    00172     if (HasBit(spec->callback_mask, CBM_OBJ_COLOUR)) continue;
    00173
    00174     const Livery *l = c->livery;
    00175     obj->colour = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? (l->colour2 * 16) : 0) + l->colour1;
    00176   }
    00177 }
    00178
    00179 extern CommandCost CheckBuildableTile(TileIndex tile, uint invalid_dirs, int &allowed_z, bool allow_steep, bool check_bridge);
    00180 static CommandCost ClearTile_Object(TileIndex tile, DoCommandFlag flags);
    00181
    00191 CommandCost CmdBuildObject(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00192 {
    00193   CommandCost cost(EXPENSES_PROPERTY);
    00194
    00195   ObjectType type = (ObjectType)GB(p1, 0, 8);
    00196   uint8 view = GB(p2, 0, 2);
    00197   const ObjectSpec *spec = ObjectSpec::Get(type);
    00198   if (!spec->IsAvailable()) return CMD_ERROR;
    00199
    00200   if (spec->flags & OBJECT_FLAG_ONLY_IN_SCENEDIT && (_game_mode != GM_EDITOR || _current_company != OWNER_NONE)) return CMD_ERROR;
    00201   if (spec->flags & OBJECT_FLAG_ONLY_IN_GAME && (_game_mode != GM_NORMAL || _current_company > MAX_COMPANIES)) return CMD_ERROR;
    00202   if (view >= spec->views) return CMD_ERROR;
    00203
    00204   if (!Object::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_OBJECTS);
    00205   if (Town::GetNumItems() == 0) return_cmd_error(STR_ERROR_MUST_FOUND_TOWN_FIRST);
    00206
    00207   int size_x = GB(spec->size, HasBit(view, 0) ? 4 : 0, 4);
    00208   int size_y = GB(spec->size, HasBit(view, 0) ? 0 : 4, 4);
    00209   TileArea ta(tile, size_x, size_y);
    00210
    00211   if (type == OBJECT_OWNED_LAND) {
    00212     /* Owned land is special as it can be placed on any slope. */
    00213     cost.AddCost(DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR));
    00214   } else {
    00215     /* Check the surface to build on. At this time we can't actually execute the
    00216      * the CLEAR_TILE commands since the newgrf callback later on can check
    00217      * some information about the tiles. */
    00218     bool allow_water = (spec->flags & (OBJECT_FLAG_BUILT_ON_WATER | OBJECT_FLAG_NOT_ON_LAND)) != 0;
    00219     bool allow_ground = (spec->flags & OBJECT_FLAG_NOT_ON_LAND) == 0;
    00220     TILE_AREA_LOOP(t, ta) {
    00221       if (HasTileWaterGround(t)) {
    00222         if (!allow_water) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
    00223         if (!IsWaterTile(t)) {
    00224           /* Normal water tiles don't have to be cleared. For all other tile types clear
    00225            * the tile but leave the water. */
    00226           cost.AddCost(DoCommand(t, 0, 0, flags & ~DC_NO_WATER & ~DC_EXEC, CMD_LANDSCAPE_CLEAR));
    00227         } else {
    00228           /* Can't build on water owned by another company. */
    00229           Owner o = GetTileOwner(t);
    00230           if (o != OWNER_NONE && o != OWNER_WATER) cost.AddCost(CheckOwnership(o, t));
    00231
    00232           /* However, the tile has to be clear of vehicles. */
    00233           cost.AddCost(EnsureNoVehicleOnGround(t));
    00234         }
    00235       } else {
    00236         if (!allow_ground) return_cmd_error(STR_ERROR_MUST_BE_BUILT_ON_WATER);
    00237         /* For non-water tiles, we'll have to clear it before building. */
    00238         cost.AddCost(DoCommand(t, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR));
    00239       }
    00240     }
    00241
    00242     /* So, now the surface is checked... check the slope of said surface. */
    00243     int allowed_z;
    00244     if (GetTileSlope(tile, &allowed_z) != SLOPE_FLAT) allowed_z++;
    00245
    00246     TILE_AREA_LOOP(t, ta) {
    00247       uint16 callback = CALLBACK_FAILED;
    00248       if (HasBit(spec->callback_mask, CBM_OBJ_SLOPE_CHECK)) {
    00249         TileIndex diff = t - tile;
    00250         callback = GetObjectCallback(CBID_OBJECT_LAND_SLOPE_CHECK, GetTileSlope(t), TileY(diff) << 4 | TileX(diff), spec, NULL, t, view);
    00251       }
    00252
    00253       if (callback == CALLBACK_FAILED) {
    00254         cost.AddCost(CheckBuildableTile(t, 0, allowed_z, false, false));
    00255       } else {
    00256         /* The meaning of bit 10 is inverted for a grf version < 8. */
    00257         if (spec->grf_prop.grffile->grf_version < 8) ToggleBit(callback, 10);
    00258         CommandCost ret = GetErrorMessageFromLocationCallbackResult(callback, spec->grf_prop.grffile->grfid, STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00259         if (ret.Failed()) return ret;
    00260       }
    00261     }
    00262
    00263     if (flags & DC_EXEC) {
    00264       /* This is basically a copy of the loop above with the exception that we now
    00265        * execute the commands and don't check for errors, since that's already done. */
    00266       TILE_AREA_LOOP(t, ta) {
    00267         if (HasTileWaterGround(t)) {
    00268           if (!IsWaterTile(t)) {
    00269             DoCommand(t, 0, 0, (flags & ~DC_NO_WATER) | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
    00270           }
    00271         } else {
    00272           DoCommand(t, 0, 0, flags | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
    00273         }
    00274       }
    00275     }
    00276   }
    00277   if (cost.Failed()) return cost;
    00278
    00279   /* Finally do a check for bridges. */
    00280   TILE_AREA_LOOP(t, ta) {
    00281     if (MayHaveBridgeAbove(t) && IsBridgeAbove(t) && (
    00282         !(spec->flags & OBJECT_FLAG_ALLOW_UNDER_BRIDGE) ||
    00283         (GetTileMaxZ(t) + spec->height >= GetBridgeHeight(GetSouthernBridgeEnd(t))))) {
    00284       return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
    00285     }
    00286   }
    00287
    00288   int hq_score = 0;
    00289   switch (type) {
    00290     case OBJECT_TRANSMITTER:
    00291     case OBJECT_LIGHTHOUSE:
    00292       if (GetTileSlope(tile) != SLOPE_FLAT) return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
    00293       break;
    00294
    00295     case OBJECT_OWNED_LAND:
    00296       if (IsTileType(tile, MP_OBJECT) &&
    00297           IsTileOwner(tile, _current_company) &&
    00298           IsOwnedLand(tile)) {
    00299         return_cmd_error(STR_ERROR_YOU_ALREADY_OWN_IT);
    00300       }
    00301       break;
    00302
    00303     case OBJECT_HQ: {
    00304       Company *c = Company::Get(_current_company);
    00305       if (c->location_of_HQ != INVALID_TILE) {
    00306         /* We need to persuade a bit harder to remove the old HQ. */
    00307         _current_company = OWNER_WATER;
    00308         cost.AddCost(ClearTile_Object(c->location_of_HQ, flags));
    00309         _current_company = c->index;
    00310       }
    00311
    00312       if (flags & DC_EXEC) {
    00313         hq_score = UpdateCompanyRatingAndValue(c, false);
    00314         c->location_of_HQ = tile;
    00315         SetWindowDirty(WC_COMPANY, c->index);
    00316       }
    00317       break;
    00318     }
    00319
    00320     case OBJECT_STATUE:
    00321       /* This may never be constructed using this method. */
    00322       return CMD_ERROR;
    00323
    00324     default: // i.e. NewGRF provided.
    00325       break;
    00326   }
    00327
    00328   if (flags & DC_EXEC) {
    00329     BuildObject(type, tile, _current_company, NULL, view);
    00330
    00331     /* Make sure the HQ starts at the right size. */
    00332     if (type == OBJECT_HQ) UpdateCompanyHQ(tile, hq_score);
    00333   }
    00334
    00335   cost.AddCost(ObjectSpec::Get(type)->GetBuildCost() * size_x * size_y);
    00336   return cost;
    00337 }
    00338
    00339
    00340 static Foundation GetFoundation_Object(TileIndex tile, Slope tileh);
    00341
    00342 static void DrawTile_Object(TileInfo *ti)
    00343 {
    00344   ObjectType type = GetObjectType(ti->tile);
    00345   const ObjectSpec *spec = ObjectSpec::Get(type);
    00346
    00347   /* Fall back for when the object doesn't exist anymore. */
    00348   if (!spec->enabled) type = OBJECT_TRANSMITTER;
    00349
    00350   if ((spec->flags & OBJECT_FLAG_HAS_NO_FOUNDATION) == 0) DrawFoundation(ti, GetFoundation_Object(ti->tile, ti->tileh));
    00351
    00352   if (type < NEW_OBJECT_OFFSET) {
    00353     const DrawTileSprites *dts = NULL;
    00354     Owner to = GetTileOwner(ti->tile);
    00355     PaletteID palette = to == OWNER_NONE ? PAL_NONE : COMPANY_SPRITE_COLOUR(to);
    00356
    00357     if (type == OBJECT_HQ) {
    00358       TileIndex diff = ti->tile - Object::GetByTile(ti->tile)->location.tile;
    00359       dts = &_object_hq[GetCompanyHQSize(ti->tile) << 2 | TileY(diff) << 1 | TileX(diff)];
    00360     } else {
    00361       dts = &_objects[type];
    00362     }
    00363
    00364     if (spec->flags & OBJECT_FLAG_HAS_NO_FOUNDATION) {
    00365       /* If an object has no foundation, but tries to draw a (flat) ground
    00366        * type... we have to be nice and convert that for them. */
    00367       switch (dts->ground.sprite) {
    00368         case SPR_FLAT_BARE_LAND:          DrawClearLandTile(ti, 0); break;
    00369         case SPR_FLAT_1_THIRD_GRASS_TILE: DrawClearLandTile(ti, 1); break;
    00370         case SPR_FLAT_2_THIRD_GRASS_TILE: DrawClearLandTile(ti, 2); break;
    00371         case SPR_FLAT_GRASS_TILE:         DrawClearLandTile(ti, 3); break;
    00372         default: DrawGroundSprite(dts->ground.sprite, palette);     break;
    00373       }
    00374     } else {
    00375       DrawGroundSprite(dts->ground.sprite, palette);
    00376     }
    00377
    00378     if (!IsInvisibilitySet(TO_STRUCTURES)) {
    00379       const DrawTileSeqStruct *dtss;
    00380       foreach_draw_tile_seq(dtss, dts->seq) {
    00381         AddSortableSpriteToDraw(
    00382           dtss->image.sprite, palette,
    00383           ti->x + dtss->delta_x, ti->y + dtss->delta_y,
    00384           dtss->size_x, dtss->size_y,
    00385           dtss->size_z, ti->z + dtss->delta_z,
    00386           IsTransparencySet(TO_STRUCTURES)
    00387         );
    00388       }
    00389     }
    00390   } else {
    00391     DrawNewObjectTile(ti, spec);
    00392   }
    00393
    00394   if (spec->flags & OBJECT_FLAG_ALLOW_UNDER_BRIDGE) DrawBridgeMiddle(ti);
    00395 }
    00396
    00397 static int GetSlopePixelZ_Object(TileIndex tile, uint x, uint y)
    00398 {
    00399   if (IsOwnedLand(tile)) {
    00400     int z;
    00401     Slope tileh = GetTilePixelSlope(tile, &z);
    00402
    00403     return z + GetPartialPixelZ(x & 0xF, y & 0xF, tileh);
    00404   } else {
    00405     return GetTileMaxPixelZ(tile);
    00406   }
    00407 }
    00408
    00409 static Foundation GetFoundation_Object(TileIndex tile, Slope tileh)
    00410 {
    00411   return IsOwnedLand(tile) ? FOUNDATION_NONE : FlatteningFoundation(tileh);
    00412 }
    00413
    00418 static void ReallyClearObjectTile(Object *o)
    00419 {
    00420   Object::DecTypeCount(GetObjectType(o->location.tile));
    00421   TILE_AREA_LOOP(tile_cur, o->location) {
    00422     DeleteNewGRFInspectWindow(GSF_OBJECTS, tile_cur);
    00423
    00424     MakeWaterKeepingClass(tile_cur, GetTileOwner(tile_cur));
    00425   }
    00426   delete o;
    00427 }
    00428
    00429 SmallVector<ClearedObjectArea, 4> _cleared_object_areas;
    00430
    00436 ClearedObjectArea *FindClearedObject(TileIndex tile)
    00437 {
    00438   TileArea ta = TileArea(tile, 1, 1);
    00439
    00440   const ClearedObjectArea *end = _cleared_object_areas.End();
    00441   for (ClearedObjectArea *coa = _cleared_object_areas.Begin(); coa != end; coa++) {
    00442     if (coa->area.Intersects(ta)) return coa;
    00443   }
    00444
    00445   return NULL;
    00446 }
    00447
    00448 static CommandCost ClearTile_Object(TileIndex tile, DoCommandFlag flags)
    00449 {
    00450   ObjectType type = GetObjectType(tile);
    00451   const ObjectSpec *spec = ObjectSpec::Get(type);
    00452
    00453   /* Get to the northern most tile. */
    00454   Object *o = Object::GetByTile(tile);
    00455   TileArea ta = o->location;
    00456
    00457   CommandCost cost(EXPENSES_CONSTRUCTION, spec->GetClearCost() * ta.w * ta.h / 5);
    00458   if (spec->flags & OBJECT_FLAG_CLEAR_INCOME) cost.MultiplyCost(-1); // They get an income!
    00459
    00460   /* Towns can't remove any objects. */
    00461   if (_current_company == OWNER_TOWN) return CMD_ERROR;
    00462
    00463   /* Water can remove everything! */
    00464   if (_current_company != OWNER_WATER) {
    00465     if ((flags & DC_NO_WATER) && IsTileOnWater(tile)) {
    00466       /* There is water under the object, treat it as water tile. */
    00467       return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
    00468     } else if (!(spec->flags & OBJECT_FLAG_AUTOREMOVE) && (flags & DC_AUTO)) {
    00469       /* No automatic removal by overbuilding stuff. */
    00470       return_cmd_error(type == OBJECT_HQ ? STR_ERROR_COMPANY_HEADQUARTERS_IN : STR_ERROR_OBJECT_IN_THE_WAY);
    00471     } else if (_game_mode == GM_EDITOR) {
    00472       /* No further limitations for the editor. */
    00473     } else if (GetTileOwner(tile) == OWNER_NONE) {
    00474       /* Owned by nobody, so we can only remove it with brute force! */
    00475       if (!_cheats.magic_bulldozer.value) return CMD_ERROR;
    00476     } else if (CheckTileOwnership(tile).Failed()) {
    00477       /* We don't own it!. */
    00478       return_cmd_error(STR_ERROR_OWNED_BY);
    00479     } else if ((spec->flags & OBJECT_FLAG_CANNOT_REMOVE) != 0 && (spec->flags & OBJECT_FLAG_AUTOREMOVE) == 0) {
    00480       /* In the game editor or with cheats we can remove, otherwise we can't. */
    00481       if (!_cheats.magic_bulldozer.value) return CMD_ERROR;
    00482
    00483       /* Removing with the cheat costs more in TTDPatch / the specs. */
    00484       cost.MultiplyCost(25);
    00485     }
    00486   } else if ((spec->flags & (OBJECT_FLAG_BUILT_ON_WATER | OBJECT_FLAG_NOT_ON_LAND)) != 0) {
    00487     /* Water can't remove objects that are buildable on water. */
    00488     return CMD_ERROR;
    00489   }
    00490
    00491   switch (type) {
    00492     case OBJECT_HQ: {
    00493       Company *c = Company::Get(GetTileOwner(tile));
    00494       if (flags & DC_EXEC) {
    00495         c->location_of_HQ = INVALID_TILE; // reset HQ position
    00496         SetWindowDirty(WC_COMPANY, c->index);
    00497         CargoPacket::InvalidateAllFrom(ST_HEADQUARTERS, c->index);
    00498       }
    00499
    00500       /* cost of relocating company is 1% of company value */
    00501       cost = CommandCost(EXPENSES_PROPERTY, CalculateCompanyValue(c) / 100);
    00502       break;
    00503     }
    00504
    00505     case OBJECT_STATUE:
    00506       if (flags & DC_EXEC) {
    00507         Town *town = o->town;
    00508         ClrBit(town->statues, GetTileOwner(tile));
    00509         SetWindowDirty(WC_TOWN_AUTHORITY, town->index);
    00510       }
    00511       break;
    00512
    00513     default:
    00514       break;
    00515   }
    00516
    00517   ClearedObjectArea *cleared_area = _cleared_object_areas.Append();
    00518   cleared_area->first_tile = tile;
    00519   cleared_area->area = ta;
    00520
    00521   if (flags & DC_EXEC) ReallyClearObjectTile(o);
    00522
    00523   return cost;
    00524 }
    00525
    00526 static void AddAcceptedCargo_Object(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
    00527 {
    00528   if (!IsCompanyHQ(tile)) return;
    00529
    00530   /* HQ accepts passenger and mail; but we have to divide the values
    00531    * between 4 tiles it occupies! */
    00532
    00533   /* HQ level (depends on company performance) in the range 1..5. */
    00534   uint level = GetCompanyHQSize(tile) + 1;
    00535
    00536   /* Top town building generates 10, so to make HQ interesting, the top
    00537    * type makes 20. */
    00538   acceptance[CT_PASSENGERS] += max(1U, level);
    00539   SetBit(*always_accepted, CT_PASSENGERS);
    00540
    00541   /* Top town building generates 4, HQ can make up to 8. The
    00542    * proportion passengers:mail is different because such a huge
    00543    * commercial building generates unusually high amount of mail
    00544    * correspondence per physical visitor. */
    00545   acceptance[CT_MAIL] += max(1U, level / 2);
    00546   SetBit(*always_accepted, CT_MAIL);
    00547 }
    00548
    00549
    00550 static void GetTileDesc_Object(TileIndex tile, TileDesc *td)
    00551 {
    00552   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
    00553   td->str = spec->name;
    00554   td->owner[0] = GetTileOwner(tile);
    00555   td->build_date = Object::GetByTile(tile)->build_date;
    00556
    00557   if (spec->grf_prop.grffile != NULL) {
    00558     td->grf = GetGRFConfig(spec->grf_prop.grffile->grfid)->GetName();
    00559   }
    00560 }
    00561
    00562 static void TileLoop_Object(TileIndex tile)
    00563 {
    00564   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
    00565   if (spec->flags & OBJECT_FLAG_ANIMATION) {
    00566     Object *o = Object::GetByTile(tile);
    00567     TriggerObjectTileAnimation(o, tile, OAT_TILELOOP, spec);
    00568     if (o->location.tile == tile) TriggerObjectAnimation(o, OAT_256_TICKS, spec);
    00569   }
    00570
    00571   if (IsTileOnWater(tile)) TileLoop_Water(tile);
    00572
    00573   if (!IsCompanyHQ(tile)) return;
    00574
    00575   /* HQ accepts passenger and mail; but we have to divide the values
    00576    * between 4 tiles it occupies! */
    00577
    00578   /* HQ level (depends on company performance) in the range 1..5. */
    00579   uint level = GetCompanyHQSize(tile) + 1;
    00580   assert(level < 6);
    00581
    00582   StationFinder stations(TileArea(tile, 2, 2));
    00583
    00584   uint r = Random();
    00585   /* Top town buildings generate 250, so the top HQ type makes 256. */
    00586   if (GB(r, 0, 8) < (256 / 4 / (6 - level))) {
    00587     uint amt = GB(r, 0, 8) / 8 / 4 + 1;
    00588     if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
    00589     MoveGoodsToStation(CT_PASSENGERS, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
    00590   }
    00591
    00592   /* Top town building generates 90, HQ can make up to 196. The
    00593    * proportion passengers:mail is about the same as in the acceptance
    00594    * equations. */
    00595   if (GB(r, 8, 8) < (196 / 4 / (6 - level))) {
    00596     uint amt = GB(r, 8, 8) / 8 / 4 + 1;
    00597     if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
    00598     MoveGoodsToStation(CT_MAIL, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
    00599   }
    00600 }
    00601
    00602
    00603 static TrackStatus GetTileTrackStatus_Object(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    00604 {
    00605   return 0;
    00606 }
    00607
    00608 static bool ClickTile_Object(TileIndex tile)
    00609 {
    00610   if (!IsCompanyHQ(tile)) return false;
    00611
    00612   ShowCompany(GetTileOwner(tile));
    00613   return true;
    00614 }
    00615
    00616 static void AnimateTile_Object(TileIndex tile)
    00617 {
    00618   AnimateNewObjectTile(tile);
    00619 }
    00620
    00627 static bool HasTransmitter(TileIndex tile, void *user)
    00628 {
    00629   return IsTransmitterTile(tile);
    00630 }
    00631
    00632 void GenerateObjects()
    00633 {
    00634   if (_settings_game.game_creation.landscape == LT_TOYLAND) return;
    00635
    00636   /* add radio tower */
    00637   int radiotower_to_build = ScaleByMapSize(15); // maximum number of radio towers on the map
    00638   int lighthouses_to_build = _settings_game.game_creation.landscape == LT_TROPIC ? 0 : ScaleByMapSize1D((Random() & 3) + 7);
    00639
    00640   /* Scale the amount of lighthouses with the amount of land at the borders. */
    00641   if (_settings_game.construction.freeform_edges && lighthouses_to_build != 0) {
    00642     uint num_water_tiles = 0;
    00643     for (uint x = 0; x < MapMaxX(); x++) {
    00644       if (IsTileType(TileXY(x, 1), MP_WATER)) num_water_tiles++;
    00645       if (IsTileType(TileXY(x, MapMaxY() - 1), MP_WATER)) num_water_tiles++;
    00646     }
    00647     for (uint y = 1; y < MapMaxY() - 1; y++) {
    00648       if (IsTileType(TileXY(1, y), MP_WATER)) num_water_tiles++;
    00649       if (IsTileType(TileXY(MapMaxX() - 1, y), MP_WATER)) num_water_tiles++;
    00650     }
    00651     /* The -6 is because the top borders are MP_VOID (-2) and all corners
    00652      * are counted twice (-4). */
    00653     lighthouses_to_build = lighthouses_to_build * num_water_tiles / (2 * MapMaxY() + 2 * MapMaxX() - 6);
    00654   }
    00655
    00656   SetGeneratingWorldProgress(GWP_OBJECT, radiotower_to_build + lighthouses_to_build);
    00657
    00658   for (uint i = ScaleByMapSize(1000); i != 0 && Object::CanAllocateItem(); i--) {
    00659     TileIndex tile = RandomTile();
    00660
    00661     int h;
    00662     if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h >= 4 && !IsBridgeAbove(tile)) {
    00663       TileIndex t = tile;
    00664       if (CircularTileSearch(&t, 9, HasTransmitter, NULL)) continue;
    00665
    00666       BuildObject(OBJECT_TRANSMITTER, tile);
    00667       IncreaseGeneratingWorldProgress(GWP_OBJECT);
    00668       if (--radiotower_to_build == 0) break;
    00669     }
    00670   }
    00671
    00672   /* add lighthouses */
    00673   uint maxx = MapMaxX();
    00674   uint maxy = MapMaxY();
    00675   for (int loop_count = 0; loop_count < 1000 && lighthouses_to_build != 0 && Object::CanAllocateItem(); loop_count++) {
    00676     uint r = Random();
    00677
    00678     /* Scatter the lighthouses more evenly around the perimeter */
    00679     int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
    00680     DiagDirection dir;
    00681     for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
    00682       perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
    00683     }
    00684
    00685     TileIndex tile;
    00686     switch (dir) {
    00687       default:
    00688       case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break;
    00689       case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break;
    00690       case DIAGDIR_SW: tile = TileXY(1,        r % maxy); break;
    00691       case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break;
    00692     }
    00693
    00694     /* Only build lighthouses at tiles where the border is sea. */
    00695     if (!IsTileType(tile, MP_WATER)) continue;
    00696
    00697     for (int j = 0; j < 19; j++) {
    00698       int h;
    00699       if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h <= 2 && !IsBridgeAbove(tile)) {
    00700         BuildObject(OBJECT_LIGHTHOUSE, tile);
    00701         IncreaseGeneratingWorldProgress(GWP_OBJECT);
    00702         lighthouses_to_build--;
    00703         assert(tile < MapSize());
    00704         break;
    00705       }
    00706       tile += TileOffsByDiagDir(dir);
    00707       if (!IsValidTile(tile)) break;
    00708     }
    00709   }
    00710 }
    00711
    00712 static void ChangeTileOwner_Object(TileIndex tile, Owner old_owner, Owner new_owner)
    00713 {
    00714   if (!IsTileOwner(tile, old_owner)) return;
    00715
    00716   if (IsOwnedLand(tile) && new_owner != INVALID_OWNER) {
    00717     SetTileOwner(tile, new_owner);
    00718   } else if (IsStatueTile(tile)) {
    00719     Town *t = Object::GetByTile(tile)->town;
    00720     ClrBit(t->statues, old_owner);
    00721     if (new_owner != INVALID_OWNER && !HasBit(t->statues, new_owner)) {
    00722       /* Transfer ownership to the new company */
    00723       SetBit(t->statues, new_owner);
    00724       SetTileOwner(tile, new_owner);
    00725     } else {
    00726       ReallyClearObjectTile(Object::GetByTile(tile));
    00727     }
    00728
    00729     SetWindowDirty(WC_TOWN_AUTHORITY, t->index);
    00730   } else {
    00731     ReallyClearObjectTile(Object::GetByTile(tile));
    00732   }
    00733 }
    00734
    00735 static CommandCost TerraformTile_Object(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
    00736 {
    00737   ObjectType type = GetObjectType(tile);
    00738
    00739   if (type == OBJECT_OWNED_LAND) {
    00740     /* Owned land remains unsold */
    00741     CommandCost ret = CheckTileOwnership(tile);
    00742     if (ret.Succeeded()) return CommandCost();
    00743   } else if (AutoslopeEnabled() && type != OBJECT_TRANSMITTER && type != OBJECT_LIGHTHOUSE) {
    00744     /* Behaviour:
    00745      *  - Both new and old slope must not be steep.
    00746      *  - TileMaxZ must not be changed.
    00747      *  - Allow autoslope by default.
    00748      *  - Disallow autoslope if callback succeeds and returns non-zero.
    00749      */
    00750     Slope tileh_old = GetTileSlope(tile);
    00751     /* TileMaxZ must not be changed. Slopes must not be steep. */
    00752     if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
    00753       const ObjectSpec *spec = ObjectSpec::Get(type);
    00754
    00755       /* Call callback 'disable autosloping for objects'. */
    00756       if (HasBit(spec->callback_mask, CBM_OBJ_AUTOSLOPE)) {
    00757         /* If the callback fails, allow autoslope. */
    00758         uint16 res = GetObjectCallback(CBID_OBJECT_AUTOSLOPE, 0, 0, spec, Object::GetByTile(tile), tile);
    00759         if (res == CALLBACK_FAILED || !ConvertBooleanCallback(spec->grf_prop.grffile, CBID_OBJECT_AUTOSLOPE, res)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    00760       } else if (spec->enabled) {
    00761         /* allow autoslope */
    00762         return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    00763       }
    00764     }
    00765   }
    00766
    00767   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00768 }
    00769
    00770 extern const TileTypeProcs _tile_type_object_procs = {
    00771   DrawTile_Object,             // draw_tile_proc
    00772   GetSlopePixelZ_Object,       // get_slope_z_proc
    00773   ClearTile_Object,            // clear_tile_proc
    00774   AddAcceptedCargo_Object,     // add_accepted_cargo_proc
    00775   GetTileDesc_Object,          // get_tile_desc_proc
    00776   GetTileTrackStatus_Object,   // get_tile_track_status_proc
    00777   ClickTile_Object,            // click_tile_proc
    00778   AnimateTile_Object,          // animate_tile_proc
    00779   TileLoop_Object,             // tile_loop_proc
    00780   ChangeTileOwner_Object,      // change_tile_owner_proc
    00781   NULL,                        // add_produced_cargo_proc
    00782   NULL,                        // vehicle_enter_tile_proc
    00783   GetFoundation_Object,        // get_foundation_proc
    00784   TerraformTile_Object,        // terraform_tile_proc
    00785 };