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

Generated on Fri May 27 04:19:45 2011 for OpenTTD by  doxygen 1.6.1