object_cmd.cpp

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

Generated on Sun Jan 9 16:01:58 2011 for OpenTTD by  doxygen 1.6.1