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