2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
/** @file object_cmd.cpp Handling of object tiles. */
|
2007-04-04 04:08:47 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "viewport_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_base.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "town.h"
|
2007-07-08 18:40:15 +00:00
|
|
|
#include "bridge_map.h"
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
#include "genworld.h"
|
2007-09-14 22:27:40 +00:00
|
|
|
#include "autoslope.h"
|
2011-02-07 22:38:02 +00:00
|
|
|
#include "clear_func.h"
|
2010-08-27 22:29:13 +00:00
|
|
|
#include "water.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "window_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_gui.h"
|
2009-01-31 20:16:06 +00:00
|
|
|
#include "cheat_type.h"
|
2010-08-08 10:59:30 +00:00
|
|
|
#include "object.h"
|
2009-08-08 16:42:55 +00:00
|
|
|
#include "cargopacket.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "core/random_func.hpp"
|
2010-08-13 12:45:26 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2010-08-08 10:59:30 +00:00
|
|
|
#include "object_map.h"
|
2010-08-13 12:45:26 +00:00
|
|
|
#include "object_base.h"
|
2010-08-28 18:54:12 +00:00
|
|
|
#include "newgrf_config.h"
|
2010-08-27 22:21:23 +00:00
|
|
|
#include "newgrf_object.h"
|
2010-08-13 12:45:26 +00:00
|
|
|
#include "date_func.h"
|
2010-09-03 22:28:11 +00:00
|
|
|
#include "newgrf_debug.h"
|
2012-01-06 19:49:44 +00:00
|
|
|
#include "vehicle_func.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
2010-08-08 10:59:30 +00:00
|
|
|
#include "table/object_land.h"
|
2008-01-13 01:21:35 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2010-08-13 12:45:26 +00:00
|
|
|
ObjectPool _object_pool("Object");
|
|
|
|
INSTANTIATE_POOL_METHODS(Object)
|
2010-08-28 18:23:14 +00:00
|
|
|
uint16 Object::counts[NUM_OBJECTS];
|
2010-08-13 12:45:26 +00:00
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Get the object associated with a tile.
|
|
|
|
* @param tile The tile to fetch the object for.
|
|
|
|
* @return The object.
|
|
|
|
*/
|
2010-08-13 12:45:26 +00:00
|
|
|
/* static */ Object *Object::GetByTile(TileIndex tile)
|
|
|
|
{
|
|
|
|
return Object::Get(GetObjectIndex(tile));
|
|
|
|
}
|
|
|
|
|
2013-10-12 16:30:42 +00:00
|
|
|
/**
|
|
|
|
* Gets the ObjectType of the given object tile
|
|
|
|
* @param t the tile to get the type from.
|
|
|
|
* @pre IsTileType(t, MP_OBJECT)
|
|
|
|
* @return the type.
|
|
|
|
*/
|
|
|
|
ObjectType GetObjectType(TileIndex t)
|
|
|
|
{
|
|
|
|
assert(IsTileType(t, MP_OBJECT));
|
|
|
|
return Object::GetByTile(t)->type;
|
|
|
|
}
|
|
|
|
|
2010-08-13 12:45:26 +00:00
|
|
|
/** Initialize/reset the objects. */
|
|
|
|
void InitializeObjects()
|
|
|
|
{
|
2010-08-28 18:23:14 +00:00
|
|
|
Object::ResetTypeCounts();
|
2010-08-13 12:45:26 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Actually build the object.
|
|
|
|
* @param type The type of object to build.
|
|
|
|
* @param tile The tile to build the northern tile of the object on.
|
|
|
|
* @param owner The owner of the object.
|
|
|
|
* @param town Town the tile is related with.
|
|
|
|
* @param view The view for the object.
|
|
|
|
* @pre All preconditions for building the object at that location
|
|
|
|
* are met, e.g. slope and clearness of tiles are checked.
|
|
|
|
*/
|
2010-12-10 21:32:04 +00:00
|
|
|
void BuildObject(ObjectType type, TileIndex tile, CompanyID owner, Town *town, uint8 view)
|
2010-08-03 08:58:12 +00:00
|
|
|
{
|
2010-08-08 10:59:30 +00:00
|
|
|
const ObjectSpec *spec = ObjectSpec::Get(type);
|
2010-08-03 08:58:12 +00:00
|
|
|
|
2010-12-10 21:32:04 +00:00
|
|
|
TileArea ta(tile, GB(spec->size, HasBit(view, 0) ? 4 : 0, 4), GB(spec->size, HasBit(view, 0) ? 0 : 4, 4));
|
2010-08-13 12:45:26 +00:00
|
|
|
Object *o = new Object();
|
2013-10-12 16:30:42 +00:00
|
|
|
o->type = type;
|
2010-08-28 18:28:34 +00:00
|
|
|
o->location = ta;
|
|
|
|
o->town = town == NULL ? CalcClosestTownFromTile(tile) : town;
|
|
|
|
o->build_date = _date;
|
2010-12-10 21:32:04 +00:00
|
|
|
o->view = view;
|
2010-08-28 18:28:34 +00:00
|
|
|
|
|
|
|
/* If nothing owns the object, the colour will be random. Otherwise
|
|
|
|
* get the colour from the company's livery settings. */
|
|
|
|
if (owner == OWNER_NONE) {
|
|
|
|
o->colour = Random();
|
|
|
|
} else {
|
|
|
|
const Livery *l = Company::Get(owner)->livery;
|
|
|
|
o->colour = l->colour1 + l->colour2 * 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the object wants only one colour, then give it that colour. */
|
|
|
|
if ((spec->flags & OBJECT_FLAG_2CC_COLOUR) == 0) o->colour &= 0xF;
|
2010-08-13 12:45:26 +00:00
|
|
|
|
2010-08-28 18:50:32 +00:00
|
|
|
if (HasBit(spec->callback_mask, CBM_OBJ_COLOUR)) {
|
|
|
|
uint16 res = GetObjectCallback(CBID_OBJECT_COLOUR, o->colour, 0, spec, o, tile);
|
2011-11-08 17:27:13 +00:00
|
|
|
if (res != CALLBACK_FAILED) {
|
|
|
|
if (res >= 0x100) ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, CBID_OBJECT_COLOUR, res);
|
|
|
|
o->colour = GB(res, 0, 8);
|
|
|
|
}
|
2010-08-28 18:50:32 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 12:45:26 +00:00
|
|
|
assert(o->town != NULL);
|
|
|
|
|
2010-08-03 08:58:12 +00:00
|
|
|
TILE_AREA_LOOP(t, ta) {
|
2010-08-27 22:26:21 +00:00
|
|
|
WaterClass wc = (IsWaterTile(t) ? GetWaterClass(t) : WATER_CLASS_INVALID);
|
2011-12-03 23:40:23 +00:00
|
|
|
/* Update company infrastructure counts for objects build on canals owned by nobody. */
|
|
|
|
if (wc == WATER_CLASS_CANAL && owner != OWNER_NONE && (IsTileOwner(tile, OWNER_NONE) || IsTileOwner(tile, OWNER_WATER))) {
|
|
|
|
Company::Get(owner)->infrastructure.water++;
|
|
|
|
DirtyCompanyInfrastructureWindows(owner);
|
|
|
|
}
|
2013-10-12 16:30:42 +00:00
|
|
|
MakeObject(t, owner, o->index, wc, Random());
|
2010-08-03 08:58:12 +00:00
|
|
|
MarkTileDirtyByTile(t);
|
|
|
|
}
|
2010-08-28 18:23:14 +00:00
|
|
|
|
|
|
|
Object::IncTypeCount(type);
|
2010-08-28 18:51:47 +00:00
|
|
|
if (spec->flags & OBJECT_FLAG_ANIMATION) TriggerObjectAnimation(o, OAT_BUILT, spec);
|
2010-08-03 08:58:12 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 08:32:58 +00:00
|
|
|
/**
|
|
|
|
* Increase the animation stage of a whole structure.
|
2010-08-13 12:45:26 +00:00
|
|
|
* @param tile The tile of the structure.
|
2010-08-03 08:32:58 +00:00
|
|
|
*/
|
2010-08-13 12:45:26 +00:00
|
|
|
static void IncreaseAnimationStage(TileIndex tile)
|
2010-08-03 08:32:58 +00:00
|
|
|
{
|
2010-08-13 12:45:26 +00:00
|
|
|
TileArea ta = Object::GetByTile(tile)->location;
|
2010-08-03 08:32:58 +00:00
|
|
|
TILE_AREA_LOOP(t, ta) {
|
2010-08-26 14:45:45 +00:00
|
|
|
SetAnimationFrame(t, GetAnimationFrame(t) + 1);
|
2010-08-03 08:32:58 +00:00
|
|
|
MarkTileDirtyByTile(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-03 08:16:06 +00:00
|
|
|
/** We encode the company HQ size in the animation stage. */
|
2010-08-26 14:45:45 +00:00
|
|
|
#define GetCompanyHQSize GetAnimationFrame
|
2010-08-03 08:32:58 +00:00
|
|
|
/** We encode the company HQ size in the animation stage. */
|
|
|
|
#define IncreaseCompanyHQSize IncreaseAnimationStage
|
2010-08-03 08:16:06 +00:00
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Update the CompanyHQ to the state associated with the given score
|
|
|
|
* @param tile The (northern) tile of the company HQ, or INVALID_TILE.
|
|
|
|
* @param score The current (performance) score of the company.
|
|
|
|
*/
|
2010-08-03 12:36:40 +00:00
|
|
|
void UpdateCompanyHQ(TileIndex tile, uint score)
|
2006-03-31 08:59:19 +00:00
|
|
|
{
|
2009-01-03 17:11:52 +00:00
|
|
|
if (tile == INVALID_TILE) return;
|
2006-03-31 08:59:19 +00:00
|
|
|
|
2010-08-03 12:36:40 +00:00
|
|
|
byte val;
|
2006-03-31 09:09:26 +00:00
|
|
|
(val = 0, score < 170) ||
|
|
|
|
(val++, score < 350) ||
|
|
|
|
(val++, score < 520) ||
|
|
|
|
(val++, score < 720) ||
|
|
|
|
(val++, true);
|
2006-03-31 08:59:19 +00:00
|
|
|
|
2010-08-03 08:32:58 +00:00
|
|
|
while (GetCompanyHQSize(tile) < val) {
|
|
|
|
IncreaseCompanyHQSize(tile);
|
|
|
|
}
|
2006-03-31 08:59:19 +00:00
|
|
|
}
|
|
|
|
|
2010-10-12 19:48:42 +00:00
|
|
|
/**
|
|
|
|
* Updates the colour of the object whenever a company changes.
|
|
|
|
* @param c The company the company colour changed of.
|
|
|
|
*/
|
|
|
|
void UpdateObjectColours(const Company *c)
|
|
|
|
{
|
|
|
|
Object *obj;
|
|
|
|
FOR_ALL_OBJECTS(obj) {
|
|
|
|
Owner owner = GetTileOwner(obj->location.tile);
|
|
|
|
/* Not the current owner, so colour doesn't change. */
|
|
|
|
if (owner != c->index) continue;
|
|
|
|
|
|
|
|
const ObjectSpec *spec = ObjectSpec::GetByTile(obj->location.tile);
|
|
|
|
/* Using the object colour callback, so not using company colour. */
|
|
|
|
if (HasBit(spec->callback_mask, CBM_OBJ_COLOUR)) continue;
|
|
|
|
|
|
|
|
const Livery *l = c->livery;
|
|
|
|
obj->colour = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? (l->colour2 * 16) : 0) + l->colour1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-25 13:35:17 +00:00
|
|
|
extern CommandCost CheckBuildableTile(TileIndex tile, uint invalid_dirs, int &allowed_z, bool allow_steep, bool check_bridge);
|
2010-08-08 10:59:30 +00:00
|
|
|
static CommandCost ClearTile_Object(TileIndex tile, DoCommandFlag flags);
|
2007-04-04 04:08:47 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2010-08-08 10:59:30 +00:00
|
|
|
* Build an object object
|
2010-08-03 12:41:24 +00:00
|
|
|
* @param tile tile where the object will be located
|
2007-04-04 04:08:47 +00:00
|
|
|
* @param flags type of operation
|
2010-08-03 12:41:24 +00:00
|
|
|
* @param p1 the object type to build
|
2010-12-10 21:32:04 +00:00
|
|
|
* @param p2 the view for the object
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 23:46:01 +00:00
|
|
|
*/
|
2010-08-08 10:59:30 +00:00
|
|
|
CommandCost CmdBuildObject(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2005-05-12 23:46:01 +00:00
|
|
|
{
|
2008-01-09 16:55:48 +00:00
|
|
|
CommandCost cost(EXPENSES_PROPERTY);
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2013-10-12 16:31:55 +00:00
|
|
|
ObjectType type = (ObjectType)GB(p1, 0, 16);
|
|
|
|
if (type >= NUM_OBJECTS) return CMD_ERROR;
|
2010-12-10 21:32:04 +00:00
|
|
|
uint8 view = GB(p2, 0, 2);
|
2010-08-08 10:59:30 +00:00
|
|
|
const ObjectSpec *spec = ObjectSpec::Get(type);
|
2013-10-18 15:23:41 +00:00
|
|
|
if (_game_mode == GM_NORMAL && !spec->IsAvailable() && !_generating_world) return CMD_ERROR;
|
|
|
|
if ((_game_mode == GM_EDITOR || _generating_world) && !spec->WasEverAvailable()) return CMD_ERROR;
|
2010-08-28 17:29:12 +00:00
|
|
|
|
2013-10-18 15:23:41 +00:00
|
|
|
if ((spec->flags & OBJECT_FLAG_ONLY_IN_SCENEDIT) != 0 && ((!_generating_world && _game_mode != GM_EDITOR) || _current_company != OWNER_NONE)) return CMD_ERROR;
|
|
|
|
if ((spec->flags & OBJECT_FLAG_ONLY_IN_GAME) != 0 && (_generating_world || _game_mode != GM_NORMAL || _current_company > MAX_COMPANIES)) return CMD_ERROR;
|
2010-12-10 21:32:04 +00:00
|
|
|
if (view >= spec->views) return CMD_ERROR;
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2010-08-13 12:45:26 +00:00
|
|
|
if (!Object::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_OBJECTS);
|
|
|
|
if (Town::GetNumItems() == 0) return_cmd_error(STR_ERROR_MUST_FOUND_TOWN_FIRST);
|
|
|
|
|
2010-12-10 21:32:04 +00:00
|
|
|
int size_x = GB(spec->size, HasBit(view, 0) ? 4 : 0, 4);
|
|
|
|
int size_y = GB(spec->size, HasBit(view, 0) ? 0 : 4, 4);
|
2010-08-03 12:41:24 +00:00
|
|
|
TileArea ta(tile, size_x, size_y);
|
2006-03-31 08:44:53 +00:00
|
|
|
|
2010-08-27 22:51:16 +00:00
|
|
|
if (type == OBJECT_OWNED_LAND) {
|
2010-08-27 22:43:27 +00:00
|
|
|
/* Owned land is special as it can be placed on any slope. */
|
|
|
|
cost.AddCost(DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR));
|
2010-08-03 12:41:24 +00:00
|
|
|
} else {
|
2011-10-04 20:18:12 +00:00
|
|
|
/* Check the surface to build on. At this time we can't actually execute the
|
|
|
|
* the CLEAR_TILE commands since the newgrf callback later on can check
|
|
|
|
* some information about the tiles. */
|
2010-09-01 20:09:15 +00:00
|
|
|
bool allow_water = (spec->flags & (OBJECT_FLAG_BUILT_ON_WATER | OBJECT_FLAG_NOT_ON_LAND)) != 0;
|
2010-08-28 18:55:20 +00:00
|
|
|
bool allow_ground = (spec->flags & OBJECT_FLAG_NOT_ON_LAND) == 0;
|
|
|
|
TILE_AREA_LOOP(t, ta) {
|
2010-11-21 18:38:45 +00:00
|
|
|
if (HasTileWaterGround(t)) {
|
2010-08-28 18:55:20 +00:00
|
|
|
if (!allow_water) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
|
2010-09-05 14:53:20 +00:00
|
|
|
if (!IsWaterTile(t)) {
|
|
|
|
/* Normal water tiles don't have to be cleared. For all other tile types clear
|
|
|
|
* the tile but leave the water. */
|
2011-10-04 20:18:12 +00:00
|
|
|
cost.AddCost(DoCommand(t, 0, 0, flags & ~DC_NO_WATER & ~DC_EXEC, CMD_LANDSCAPE_CLEAR));
|
2011-07-08 14:42:18 +00:00
|
|
|
} else {
|
|
|
|
/* Can't build on water owned by another company. */
|
|
|
|
Owner o = GetTileOwner(t);
|
|
|
|
if (o != OWNER_NONE && o != OWNER_WATER) cost.AddCost(CheckOwnership(o, t));
|
2012-01-06 19:49:44 +00:00
|
|
|
|
|
|
|
/* However, the tile has to be clear of vehicles. */
|
|
|
|
cost.AddCost(EnsureNoVehicleOnGround(t));
|
2010-09-05 14:53:20 +00:00
|
|
|
}
|
2010-08-28 18:55:20 +00:00
|
|
|
} else {
|
|
|
|
if (!allow_ground) return_cmd_error(STR_ERROR_MUST_BE_BUILT_ON_WATER);
|
|
|
|
/* For non-water tiles, we'll have to clear it before building. */
|
2011-10-04 20:18:12 +00:00
|
|
|
cost.AddCost(DoCommand(t, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR));
|
2010-08-28 18:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* So, now the surface is checked... check the slope of said surface. */
|
2010-11-24 23:08:12 +00:00
|
|
|
int allowed_z;
|
2011-11-04 11:30:37 +00:00
|
|
|
if (GetTileSlope(tile, &allowed_z) != SLOPE_FLAT) allowed_z++;
|
2010-11-24 23:08:12 +00:00
|
|
|
|
|
|
|
TILE_AREA_LOOP(t, ta) {
|
|
|
|
uint16 callback = CALLBACK_FAILED;
|
|
|
|
if (HasBit(spec->callback_mask, CBM_OBJ_SLOPE_CHECK)) {
|
2010-08-28 18:55:20 +00:00
|
|
|
TileIndex diff = t - tile;
|
2011-11-04 10:22:27 +00:00
|
|
|
callback = GetObjectCallback(CBID_OBJECT_LAND_SLOPE_CHECK, GetTileSlope(t), TileY(diff) << 4 | TileX(diff), spec, NULL, t, view);
|
2010-08-28 18:55:20 +00:00
|
|
|
}
|
2010-11-24 23:08:12 +00:00
|
|
|
|
|
|
|
if (callback == CALLBACK_FAILED) {
|
2011-09-25 13:35:17 +00:00
|
|
|
cost.AddCost(CheckBuildableTile(t, 0, allowed_z, false, false));
|
2011-11-08 17:24:31 +00:00
|
|
|
} else {
|
|
|
|
/* The meaning of bit 10 is inverted for a grf version < 8. */
|
|
|
|
if (spec->grf_prop.grffile->grf_version < 8) ToggleBit(callback, 10);
|
2014-01-12 18:00:19 +00:00
|
|
|
CommandCost ret = GetErrorMessageFromLocationCallbackResult(callback, spec->grf_prop.grffile, STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
|
2011-11-16 20:39:30 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2010-08-28 18:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-04 20:18:12 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* This is basically a copy of the loop above with the exception that we now
|
|
|
|
* execute the commands and don't check for errors, since that's already done. */
|
|
|
|
TILE_AREA_LOOP(t, ta) {
|
|
|
|
if (HasTileWaterGround(t)) {
|
|
|
|
if (!IsWaterTile(t)) {
|
|
|
|
DoCommand(t, 0, 0, (flags & ~DC_NO_WATER) | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DoCommand(t, 0, 0, flags | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
2010-03-14 12:58:51 +00:00
|
|
|
if (cost.Failed()) return cost;
|
2008-01-07 00:45:05 +00:00
|
|
|
|
2010-08-28 19:02:21 +00:00
|
|
|
/* Finally do a check for bridges. */
|
|
|
|
TILE_AREA_LOOP(t, ta) {
|
2014-09-21 11:24:51 +00:00
|
|
|
if (IsBridgeAbove(t) && (
|
2010-08-28 19:02:21 +00:00
|
|
|
!(spec->flags & OBJECT_FLAG_ALLOW_UNDER_BRIDGE) ||
|
2011-11-04 10:28:20 +00:00
|
|
|
(GetTileMaxZ(t) + spec->height >= GetBridgeHeight(GetSouthernBridgeEnd(t))))) {
|
2010-08-28 19:02:21 +00:00
|
|
|
return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-03 12:41:24 +00:00
|
|
|
int hq_score = 0;
|
2010-08-02 22:10:05 +00:00
|
|
|
switch (type) {
|
2010-08-27 22:43:27 +00:00
|
|
|
case OBJECT_TRANSMITTER:
|
|
|
|
case OBJECT_LIGHTHOUSE:
|
2013-10-12 22:07:58 +00:00
|
|
|
if (!IsTileFlat(tile)) return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
|
2010-08-27 22:43:27 +00:00
|
|
|
break;
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
case OBJECT_OWNED_LAND:
|
|
|
|
if (IsTileType(tile, MP_OBJECT) &&
|
2010-08-03 12:41:24 +00:00
|
|
|
IsTileOwner(tile, _current_company) &&
|
2013-10-12 16:30:22 +00:00
|
|
|
IsObjectType(tile, OBJECT_OWNED_LAND)) {
|
2010-08-03 12:41:24 +00:00
|
|
|
return_cmd_error(STR_ERROR_YOU_ALREADY_OWN_IT);
|
|
|
|
}
|
|
|
|
break;
|
2010-08-02 22:10:05 +00:00
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
case OBJECT_HQ: {
|
2010-08-03 12:41:24 +00:00
|
|
|
Company *c = Company::Get(_current_company);
|
2010-08-03 13:03:25 +00:00
|
|
|
if (c->location_of_HQ != INVALID_TILE) {
|
|
|
|
/* We need to persuade a bit harder to remove the old HQ. */
|
|
|
|
_current_company = OWNER_WATER;
|
2010-08-08 10:59:30 +00:00
|
|
|
cost.AddCost(ClearTile_Object(c->location_of_HQ, flags));
|
2010-08-03 13:03:25 +00:00
|
|
|
_current_company = c->index;
|
2010-08-03 12:41:24 +00:00
|
|
|
}
|
2010-08-02 22:10:05 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2010-08-03 12:41:24 +00:00
|
|
|
hq_score = UpdateCompanyRatingAndValue(c, false);
|
|
|
|
c->location_of_HQ = tile;
|
|
|
|
SetWindowDirty(WC_COMPANY, c->index);
|
2010-08-02 22:10:05 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-08-03 12:41:24 +00:00
|
|
|
}
|
2010-08-02 22:10:05 +00:00
|
|
|
|
2010-08-28 18:55:20 +00:00
|
|
|
case OBJECT_STATUE:
|
|
|
|
/* This may never be constructed using this method. */
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
default: // i.e. NewGRF provided.
|
|
|
|
break;
|
2010-08-03 12:41:24 +00:00
|
|
|
}
|
2010-08-02 22:10:05 +00:00
|
|
|
|
2010-08-03 12:41:24 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2010-12-10 21:32:04 +00:00
|
|
|
BuildObject(type, tile, _current_company, NULL, view);
|
2010-08-02 22:10:05 +00:00
|
|
|
|
2010-08-03 12:41:24 +00:00
|
|
|
/* Make sure the HQ starts at the right size. */
|
2010-08-08 10:59:30 +00:00
|
|
|
if (type == OBJECT_HQ) UpdateCompanyHQ(tile, hq_score);
|
2010-08-02 22:10:05 +00:00
|
|
|
}
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
cost.AddCost(ObjectSpec::Get(type)->GetBuildCost() * size_x * size_y);
|
2010-08-02 22:10:05 +00:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
static Foundation GetFoundation_Object(TileIndex tile, Slope tileh);
|
2007-09-09 11:30:44 +00:00
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
static void DrawTile_Object(TileInfo *ti)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-08 10:59:30 +00:00
|
|
|
ObjectType type = GetObjectType(ti->tile);
|
|
|
|
const ObjectSpec *spec = ObjectSpec::Get(type);
|
2010-08-28 17:29:12 +00:00
|
|
|
|
|
|
|
/* Fall back for when the object doesn't exist anymore. */
|
|
|
|
if (!spec->enabled) type = OBJECT_TRANSMITTER;
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
if ((spec->flags & OBJECT_FLAG_HAS_NO_FOUNDATION) == 0) DrawFoundation(ti, GetFoundation_Object(ti->tile, ti->tileh));
|
2006-04-03 12:20:55 +00:00
|
|
|
|
2010-08-28 18:57:32 +00:00
|
|
|
if (type < NEW_OBJECT_OFFSET) {
|
|
|
|
const DrawTileSprites *dts = NULL;
|
|
|
|
Owner to = GetTileOwner(ti->tile);
|
|
|
|
PaletteID palette = to == OWNER_NONE ? PAL_NONE : COMPANY_SPRITE_COLOUR(to);
|
|
|
|
|
|
|
|
if (type == OBJECT_HQ) {
|
|
|
|
TileIndex diff = ti->tile - Object::GetByTile(ti->tile)->location.tile;
|
|
|
|
dts = &_object_hq[GetCompanyHQSize(ti->tile) << 2 | TileY(diff) << 1 | TileX(diff)];
|
|
|
|
} else {
|
|
|
|
dts = &_objects[type];
|
|
|
|
}
|
2008-04-03 19:55:40 +00:00
|
|
|
|
2010-08-28 18:57:32 +00:00
|
|
|
if (spec->flags & OBJECT_FLAG_HAS_NO_FOUNDATION) {
|
|
|
|
/* If an object has no foundation, but tries to draw a (flat) ground
|
|
|
|
* type... we have to be nice and convert that for them. */
|
|
|
|
switch (dts->ground.sprite) {
|
|
|
|
case SPR_FLAT_BARE_LAND: DrawClearLandTile(ti, 0); break;
|
|
|
|
case SPR_FLAT_1_THIRD_GRASS_TILE: DrawClearLandTile(ti, 1); break;
|
|
|
|
case SPR_FLAT_2_THIRD_GRASS_TILE: DrawClearLandTile(ti, 2); break;
|
|
|
|
case SPR_FLAT_GRASS_TILE: DrawClearLandTile(ti, 3); break;
|
|
|
|
default: DrawGroundSprite(dts->ground.sprite, palette); break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DrawGroundSprite(dts->ground.sprite, palette);
|
2010-08-03 12:07:55 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-08-28 18:57:32 +00:00
|
|
|
if (!IsInvisibilitySet(TO_STRUCTURES)) {
|
|
|
|
const DrawTileSeqStruct *dtss;
|
|
|
|
foreach_draw_tile_seq(dtss, dts->seq) {
|
|
|
|
AddSortableSpriteToDraw(
|
|
|
|
dtss->image.sprite, palette,
|
|
|
|
ti->x + dtss->delta_x, ti->y + dtss->delta_y,
|
|
|
|
dtss->size_x, dtss->size_y,
|
|
|
|
dtss->size_z, ti->z + dtss->delta_z,
|
|
|
|
IsTransparencySet(TO_STRUCTURES)
|
|
|
|
);
|
|
|
|
}
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2010-08-28 18:57:32 +00:00
|
|
|
} else {
|
|
|
|
DrawNewObjectTile(ti, spec);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2010-08-03 11:35:57 +00:00
|
|
|
|
2013-09-02 18:37:44 +00:00
|
|
|
DrawBridgeMiddle(ti);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-11-04 11:36:10 +00:00
|
|
|
static int GetSlopePixelZ_Object(TileIndex tile, uint x, uint y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2013-10-12 16:30:22 +00:00
|
|
|
if (IsObjectType(tile, OBJECT_OWNED_LAND)) {
|
2011-11-04 11:30:37 +00:00
|
|
|
int z;
|
2011-11-04 10:18:13 +00:00
|
|
|
Slope tileh = GetTilePixelSlope(tile, &z);
|
2006-08-06 16:32:49 +00:00
|
|
|
|
2011-11-04 10:18:13 +00:00
|
|
|
return z + GetPartialPixelZ(x & 0xF, y & 0xF, tileh);
|
2006-03-21 20:02:05 +00:00
|
|
|
} else {
|
2011-11-04 10:18:13 +00:00
|
|
|
return GetTileMaxPixelZ(tile);
|
2006-03-21 20:02:05 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
static Foundation GetFoundation_Object(TileIndex tile, Slope tileh)
|
2004-08-13 18:27:33 +00:00
|
|
|
{
|
2013-10-12 16:30:22 +00:00
|
|
|
return IsObjectType(tile, OBJECT_OWNED_LAND) ? FOUNDATION_NONE : FlatteningFoundation(tileh);
|
2004-08-13 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 06:15:26 +00:00
|
|
|
/**
|
|
|
|
* Perform the actual removal of the object from the map.
|
|
|
|
* @param o The object to really clear.
|
|
|
|
*/
|
|
|
|
static void ReallyClearObjectTile(Object *o)
|
|
|
|
{
|
2013-10-12 16:30:42 +00:00
|
|
|
Object::DecTypeCount(o->type);
|
2010-09-03 22:28:11 +00:00
|
|
|
TILE_AREA_LOOP(tile_cur, o->location) {
|
|
|
|
DeleteNewGRFInspectWindow(GSF_OBJECTS, tile_cur);
|
|
|
|
|
|
|
|
MakeWaterKeepingClass(tile_cur, GetTileOwner(tile_cur));
|
|
|
|
}
|
2010-09-01 06:15:26 +00:00
|
|
|
delete o;
|
|
|
|
}
|
|
|
|
|
2010-09-05 13:31:39 +00:00
|
|
|
SmallVector<ClearedObjectArea, 4> _cleared_object_areas;
|
2010-09-03 21:50:51 +00:00
|
|
|
|
2010-11-21 17:05:23 +00:00
|
|
|
/**
|
|
|
|
* Find the entry in _cleared_object_areas which occupies a certain tile.
|
|
|
|
* @param tile Tile of interest
|
|
|
|
* @return Occupying entry, or NULL if none
|
|
|
|
*/
|
|
|
|
ClearedObjectArea *FindClearedObject(TileIndex tile)
|
|
|
|
{
|
|
|
|
TileArea ta = TileArea(tile, 1, 1);
|
|
|
|
|
|
|
|
const ClearedObjectArea *end = _cleared_object_areas.End();
|
|
|
|
for (ClearedObjectArea *coa = _cleared_object_areas.Begin(); coa != end; coa++) {
|
|
|
|
if (coa->area.Intersects(ta)) return coa;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
static CommandCost ClearTile_Object(TileIndex tile, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-03 13:03:25 +00:00
|
|
|
/* Get to the northern most tile. */
|
2010-08-13 12:45:26 +00:00
|
|
|
Object *o = Object::GetByTile(tile);
|
|
|
|
TileArea ta = o->location;
|
2010-08-03 13:03:25 +00:00
|
|
|
|
2013-10-12 16:30:42 +00:00
|
|
|
ObjectType type = o->type;
|
|
|
|
const ObjectSpec *spec = ObjectSpec::Get(type);
|
|
|
|
|
2010-08-28 18:56:07 +00:00
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION, spec->GetClearCost() * ta.w * ta.h / 5);
|
|
|
|
if (spec->flags & OBJECT_FLAG_CLEAR_INCOME) cost.MultiplyCost(-1); // They get an income!
|
|
|
|
|
2012-01-22 21:26:42 +00:00
|
|
|
/* Towns can't remove any objects. */
|
|
|
|
if (_current_company == OWNER_TOWN) return CMD_ERROR;
|
|
|
|
|
2010-08-02 22:03:28 +00:00
|
|
|
/* Water can remove everything! */
|
|
|
|
if (_current_company != OWNER_WATER) {
|
2010-09-05 13:28:59 +00:00
|
|
|
if ((flags & DC_NO_WATER) && IsTileOnWater(tile)) {
|
|
|
|
/* There is water under the object, treat it as water tile. */
|
|
|
|
return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
|
|
|
|
} else if (!(spec->flags & OBJECT_FLAG_AUTOREMOVE) && (flags & DC_AUTO)) {
|
2010-08-02 22:03:28 +00:00
|
|
|
/* No automatic removal by overbuilding stuff. */
|
2010-08-08 10:59:30 +00:00
|
|
|
return_cmd_error(type == OBJECT_HQ ? STR_ERROR_COMPANY_HEADQUARTERS_IN : STR_ERROR_OBJECT_IN_THE_WAY);
|
2010-08-02 23:05:54 +00:00
|
|
|
} else if (_game_mode == GM_EDITOR) {
|
|
|
|
/* No further limitations for the editor. */
|
|
|
|
} else if (GetTileOwner(tile) == OWNER_NONE) {
|
2013-10-17 21:47:16 +00:00
|
|
|
/* Owned by nobody and unremovable, so we can only remove it with brute force! */
|
|
|
|
if (!_cheats.magic_bulldozer.value && (spec->flags & OBJECT_FLAG_CANNOT_REMOVE) != 0) return CMD_ERROR;
|
2010-08-02 23:05:54 +00:00
|
|
|
} else if (CheckTileOwnership(tile).Failed()) {
|
|
|
|
/* We don't own it!. */
|
|
|
|
return_cmd_error(STR_ERROR_OWNED_BY);
|
2010-08-28 18:56:07 +00:00
|
|
|
} else if ((spec->flags & OBJECT_FLAG_CANNOT_REMOVE) != 0 && (spec->flags & OBJECT_FLAG_AUTOREMOVE) == 0) {
|
2010-08-02 22:03:28 +00:00
|
|
|
/* In the game editor or with cheats we can remove, otherwise we can't. */
|
2010-08-28 18:56:07 +00:00
|
|
|
if (!_cheats.magic_bulldozer.value) return CMD_ERROR;
|
|
|
|
|
|
|
|
/* Removing with the cheat costs more in TTDPatch / the specs. */
|
|
|
|
cost.MultiplyCost(25);
|
2010-08-02 22:03:28 +00:00
|
|
|
}
|
2010-09-02 20:45:54 +00:00
|
|
|
} else if ((spec->flags & (OBJECT_FLAG_BUILT_ON_WATER | OBJECT_FLAG_NOT_ON_LAND)) != 0) {
|
|
|
|
/* Water can't remove objects that are buildable on water. */
|
|
|
|
return CMD_ERROR;
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-08-02 23:05:54 +00:00
|
|
|
switch (type) {
|
2010-08-08 10:59:30 +00:00
|
|
|
case OBJECT_HQ: {
|
2010-08-03 13:03:25 +00:00
|
|
|
Company *c = Company::Get(GetTileOwner(tile));
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
c->location_of_HQ = INVALID_TILE; // reset HQ position
|
|
|
|
SetWindowDirty(WC_COMPANY, c->index);
|
|
|
|
CargoPacket::InvalidateAllFrom(ST_HEADQUARTERS, c->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cost of relocating company is 1% of company value */
|
|
|
|
cost = CommandCost(EXPENSES_PROPERTY, CalculateCompanyValue(c) / 100);
|
|
|
|
break;
|
|
|
|
}
|
2008-01-31 21:16:40 +00:00
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
case OBJECT_STATUE:
|
2010-08-02 23:05:54 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2010-08-13 12:45:26 +00:00
|
|
|
Town *town = o->town;
|
|
|
|
ClrBit(town->statues, GetTileOwner(tile));
|
|
|
|
SetWindowDirty(WC_TOWN_AUTHORITY, town->index);
|
2010-08-02 23:05:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2007-03-08 14:34:32 +00:00
|
|
|
}
|
|
|
|
|
2011-07-30 12:28:49 +00:00
|
|
|
ClearedObjectArea *cleared_area = _cleared_object_areas.Append();
|
|
|
|
cleared_area->first_tile = tile;
|
|
|
|
cleared_area->area = ta;
|
|
|
|
|
2010-09-01 06:15:26 +00:00
|
|
|
if (flags & DC_EXEC) ReallyClearObjectTile(o);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-02 23:05:54 +00:00
|
|
|
return cost;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
static void AddAcceptedCargo_Object(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2013-10-12 16:30:22 +00:00
|
|
|
if (!IsObjectType(tile, OBJECT_HQ)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* HQ accepts passenger and mail; but we have to divide the values
|
|
|
|
* between 4 tiles it occupies! */
|
|
|
|
|
2008-04-25 15:25:28 +00:00
|
|
|
/* HQ level (depends on company performance) in the range 1..5. */
|
|
|
|
uint level = GetCompanyHQSize(tile) + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* Top town building generates 10, so to make HQ interesting, the top
|
|
|
|
* type makes 20. */
|
2009-06-27 18:26:50 +00:00
|
|
|
acceptance[CT_PASSENGERS] += max(1U, level);
|
2009-09-20 17:44:33 +00:00
|
|
|
SetBit(*always_accepted, CT_PASSENGERS);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* Top town building generates 4, HQ can make up to 8. The
|
|
|
|
* proportion passengers:mail is different because such a huge
|
|
|
|
* commercial building generates unusually high amount of mail
|
|
|
|
* correspondence per physical visitor. */
|
2009-06-27 18:26:50 +00:00
|
|
|
acceptance[CT_MAIL] += max(1U, level / 2);
|
2009-09-20 17:44:33 +00:00
|
|
|
SetBit(*always_accepted, CT_MAIL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
static void GetTileDesc_Object(TileIndex tile, TileDesc *td)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-28 18:54:12 +00:00
|
|
|
const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
|
|
|
|
td->str = spec->name;
|
2008-05-21 22:15:39 +00:00
|
|
|
td->owner[0] = GetTileOwner(tile);
|
2010-08-13 12:45:26 +00:00
|
|
|
td->build_date = Object::GetByTile(tile)->build_date;
|
2010-08-28 18:54:12 +00:00
|
|
|
|
|
|
|
if (spec->grf_prop.grffile != NULL) {
|
|
|
|
td->grf = GetGRFConfig(spec->grf_prop.grffile->grfid)->GetName();
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
static void TileLoop_Object(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-28 18:51:47 +00:00
|
|
|
const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
|
2010-08-28 19:13:20 +00:00
|
|
|
if (spec->flags & OBJECT_FLAG_ANIMATION) {
|
2011-06-12 20:32:52 +00:00
|
|
|
Object *o = Object::GetByTile(tile);
|
2010-08-28 19:13:20 +00:00
|
|
|
TriggerObjectTileAnimation(o, tile, OAT_TILELOOP, spec);
|
|
|
|
if (o->location.tile == tile) TriggerObjectAnimation(o, OAT_256_TICKS, spec);
|
|
|
|
}
|
2010-08-28 18:51:47 +00:00
|
|
|
|
2010-08-27 22:29:13 +00:00
|
|
|
if (IsTileOnWater(tile)) TileLoop_Water(tile);
|
|
|
|
|
2013-10-12 16:30:22 +00:00
|
|
|
if (!IsObjectType(tile, OBJECT_HQ)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* HQ accepts passenger and mail; but we have to divide the values
|
|
|
|
* between 4 tiles it occupies! */
|
|
|
|
|
2008-04-25 15:25:28 +00:00
|
|
|
/* HQ level (depends on company performance) in the range 1..5. */
|
|
|
|
uint level = GetCompanyHQSize(tile) + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(level < 6);
|
|
|
|
|
2010-01-04 18:12:10 +00:00
|
|
|
StationFinder stations(TileArea(tile, 2, 2));
|
2009-11-15 21:06:13 +00:00
|
|
|
|
2008-04-25 15:25:28 +00:00
|
|
|
uint r = Random();
|
2007-04-04 04:08:47 +00:00
|
|
|
/* Top town buildings generate 250, so the top HQ type makes 256. */
|
2005-07-21 06:31:02 +00:00
|
|
|
if (GB(r, 0, 8) < (256 / 4 / (6 - level))) {
|
|
|
|
uint amt = GB(r, 0, 8) / 8 / 4 + 1;
|
2010-11-13 09:45:20 +00:00
|
|
|
if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
|
2009-11-15 21:06:13 +00:00
|
|
|
MoveGoodsToStation(CT_PASSENGERS, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* Top town building generates 90, HQ can make up to 196. The
|
|
|
|
* proportion passengers:mail is about the same as in the acceptance
|
|
|
|
* equations. */
|
2005-07-21 06:31:02 +00:00
|
|
|
if (GB(r, 8, 8) < (196 / 4 / (6 - level))) {
|
|
|
|
uint amt = GB(r, 8, 8) / 8 / 4 + 1;
|
2010-11-13 09:45:20 +00:00
|
|
|
if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
|
2009-11-15 21:06:13 +00:00
|
|
|
MoveGoodsToStation(CT_MAIL, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
static TrackStatus GetTileTrackStatus_Object(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
static bool ClickTile_Object(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2013-10-12 16:30:22 +00:00
|
|
|
if (!IsObjectType(tile, OBJECT_HQ)) return false;
|
2009-01-02 22:42:05 +00:00
|
|
|
|
|
|
|
ShowCompany(GetTileOwner(tile));
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-28 18:51:47 +00:00
|
|
|
static void AnimateTile_Object(TileIndex tile)
|
|
|
|
{
|
|
|
|
AnimateNewObjectTile(tile);
|
|
|
|
}
|
|
|
|
|
2010-12-12 18:18:50 +00:00
|
|
|
/**
|
|
|
|
* Helper function for \c CircularTileSearch.
|
|
|
|
* @param tile The tile to check.
|
|
|
|
* @param user Ignored.
|
|
|
|
* @return True iff the tile has a radio tower.
|
|
|
|
*/
|
|
|
|
static bool HasTransmitter(TileIndex tile, void *user)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2013-10-12 16:30:22 +00:00
|
|
|
return IsObjectTypeTile(tile, OBJECT_TRANSMITTER);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2013-10-17 21:25:15 +00:00
|
|
|
/**
|
|
|
|
* Try to build a lighthouse.
|
|
|
|
* @return True iff building a lighthouse succeeded.
|
|
|
|
*/
|
|
|
|
static bool TryBuildLightHouse()
|
|
|
|
{
|
|
|
|
uint maxx = MapMaxX();
|
|
|
|
uint maxy = MapMaxY();
|
|
|
|
uint r = Random();
|
|
|
|
|
|
|
|
/* Scatter the lighthouses more evenly around the perimeter */
|
|
|
|
int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
|
|
|
|
DiagDirection dir;
|
|
|
|
for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
|
|
|
|
perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
TileIndex tile;
|
|
|
|
switch (dir) {
|
|
|
|
default:
|
|
|
|
case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break;
|
|
|
|
case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break;
|
|
|
|
case DIAGDIR_SW: tile = TileXY(1, r % maxy); break;
|
|
|
|
case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only build lighthouses at tiles where the border is sea. */
|
|
|
|
if (!IsTileType(tile, MP_WATER)) return false;
|
|
|
|
|
|
|
|
for (int j = 0; j < 19; j++) {
|
|
|
|
int h;
|
|
|
|
if (IsTileType(tile, MP_CLEAR) && IsTileFlat(tile, &h) && h <= 2 && !IsBridgeAbove(tile)) {
|
|
|
|
BuildObject(OBJECT_LIGHTHOUSE, tile);
|
|
|
|
assert(tile < MapSize());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
tile += TileOffsByDiagDir(dir);
|
|
|
|
if (!IsValidTile(tile)) return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-17 21:22:39 +00:00
|
|
|
/**
|
|
|
|
* Try to build a transmitter.
|
|
|
|
* @return True iff a transmitter was built.
|
|
|
|
*/
|
|
|
|
static bool TryBuildTransmitter()
|
|
|
|
{
|
|
|
|
TileIndex tile = RandomTile();
|
|
|
|
int h;
|
|
|
|
if (IsTileType(tile, MP_CLEAR) && IsTileFlat(tile, &h) && h >= 4 && !IsBridgeAbove(tile)) {
|
|
|
|
TileIndex t = tile;
|
|
|
|
if (CircularTileSearch(&t, 9, HasTransmitter, NULL)) return false;
|
|
|
|
|
|
|
|
BuildObject(OBJECT_TRANSMITTER, tile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
void GenerateObjects()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2013-10-17 21:41:17 +00:00
|
|
|
/* Set a guestimate on how much we progress */
|
|
|
|
SetGeneratingWorldProgress(GWP_OBJECT, NUM_OBJECTS);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-10-17 21:41:17 +00:00
|
|
|
/* Determine number of water tiles at map border needed for freeform_edges */
|
|
|
|
uint num_water_tiles = 0;
|
|
|
|
if (_settings_game.construction.freeform_edges) {
|
2009-01-21 02:31:55 +00:00
|
|
|
for (uint x = 0; x < MapMaxX(); x++) {
|
|
|
|
if (IsTileType(TileXY(x, 1), MP_WATER)) num_water_tiles++;
|
|
|
|
if (IsTileType(TileXY(x, MapMaxY() - 1), MP_WATER)) num_water_tiles++;
|
|
|
|
}
|
|
|
|
for (uint y = 1; y < MapMaxY() - 1; y++) {
|
|
|
|
if (IsTileType(TileXY(1, y), MP_WATER)) num_water_tiles++;
|
|
|
|
if (IsTileType(TileXY(MapMaxX() - 1, y), MP_WATER)) num_water_tiles++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-17 21:41:17 +00:00
|
|
|
/* Iterate over all possible object types */
|
|
|
|
for (uint i = 0; i < NUM_OBJECTS; i++) {
|
|
|
|
const ObjectSpec *spec = ObjectSpec::Get(i);
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
|
2013-10-17 21:41:17 +00:00
|
|
|
/* Continue, if the object was never available till now or shall not be placed */
|
|
|
|
if (!spec->WasEverAvailable() || spec->generate_amount == 0) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-10-17 21:41:17 +00:00
|
|
|
uint16 amount = spec->generate_amount;
|
|
|
|
|
|
|
|
/* Scale by map size */
|
|
|
|
if ((spec->flags & OBJECT_FLAG_SCALE_BY_WATER) && _settings_game.construction.freeform_edges) {
|
|
|
|
/* Scale the amount of lighthouses with the amount of land at the borders.
|
|
|
|
* The -6 is because the top borders are MP_VOID (-2) and all corners
|
|
|
|
* are counted twice (-4). */
|
|
|
|
amount = ScaleByMapSize1D(amount * num_water_tiles) / (2 * MapMaxY() + 2 * MapMaxX() - 6);
|
|
|
|
} else if (spec->flags & OBJECT_FLAG_SCALE_BY_WATER) {
|
|
|
|
amount = ScaleByMapSize1D(amount);
|
|
|
|
} else {
|
|
|
|
amount = ScaleByMapSize(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now try to place the requested amount of this object */
|
|
|
|
for (uint j = ScaleByMapSize(1000); j != 0 && amount != 0 && Object::CanAllocateItem(); j--) {
|
|
|
|
switch (i) {
|
2013-11-04 17:59:58 +00:00
|
|
|
case OBJECT_TRANSMITTER:
|
2013-10-17 21:41:17 +00:00
|
|
|
if (TryBuildTransmitter()) amount--;
|
|
|
|
break;
|
|
|
|
|
2013-11-04 17:59:58 +00:00
|
|
|
case OBJECT_LIGHTHOUSE:
|
2013-10-17 21:41:17 +00:00
|
|
|
if (TryBuildLightHouse()) amount--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
uint8 view = RandomRange(spec->views);
|
|
|
|
if (CmdBuildObject(RandomTile(), DC_EXEC | DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, i, view, NULL).Succeeded()) amount--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-10-17 21:25:15 +00:00
|
|
|
IncreaseGeneratingWorldProgress(GWP_OBJECT);
|
2008-04-25 15:25:28 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
static void ChangeTileOwner_Object(TileIndex tile, Owner old_owner, Owner new_owner)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!IsTileOwner(tile, old_owner)) return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2013-10-12 16:30:22 +00:00
|
|
|
if (IsObjectType(tile, OBJECT_OWNED_LAND) && new_owner != INVALID_OWNER) {
|
2008-09-30 20:39:50 +00:00
|
|
|
SetTileOwner(tile, new_owner);
|
2013-10-12 16:30:22 +00:00
|
|
|
} else if (IsObjectType(tile, OBJECT_STATUE)) {
|
2010-08-13 12:45:26 +00:00
|
|
|
Town *t = Object::GetByTile(tile)->town;
|
2008-09-30 20:39:50 +00:00
|
|
|
ClrBit(t->statues, old_owner);
|
|
|
|
if (new_owner != INVALID_OWNER && !HasBit(t->statues, new_owner)) {
|
2008-02-01 22:13:59 +00:00
|
|
|
/* Transfer ownership to the new company */
|
2008-09-30 20:39:50 +00:00
|
|
|
SetBit(t->statues, new_owner);
|
|
|
|
SetTileOwner(tile, new_owner);
|
2008-02-01 22:13:59 +00:00
|
|
|
} else {
|
2010-09-01 06:15:26 +00:00
|
|
|
ReallyClearObjectTile(Object::GetByTile(tile));
|
2008-02-01 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
2010-08-12 19:18:58 +00:00
|
|
|
SetWindowDirty(WC_TOWN_AUTHORITY, t->index);
|
2005-07-08 22:25:24 +00:00
|
|
|
} else {
|
2010-09-01 06:15:26 +00:00
|
|
|
ReallyClearObjectTile(Object::GetByTile(tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 11:36:10 +00:00
|
|
|
static CommandCost TerraformTile_Object(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
|
2007-08-30 17:17:04 +00:00
|
|
|
{
|
2010-08-08 10:59:30 +00:00
|
|
|
ObjectType type = GetObjectType(tile);
|
2010-08-03 12:07:55 +00:00
|
|
|
|
2010-08-27 22:43:27 +00:00
|
|
|
if (type == OBJECT_OWNED_LAND) {
|
2010-08-03 12:07:55 +00:00
|
|
|
/* Owned land remains unsold */
|
2010-03-07 20:44:05 +00:00
|
|
|
CommandCost ret = CheckTileOwnership(tile);
|
|
|
|
if (ret.Succeeded()) return CommandCost();
|
2010-08-27 22:43:27 +00:00
|
|
|
} else if (AutoslopeEnabled() && type != OBJECT_TRANSMITTER && type != OBJECT_LIGHTHOUSE) {
|
2010-08-28 19:00:21 +00:00
|
|
|
/* Behaviour:
|
|
|
|
* - Both new and old slope must not be steep.
|
|
|
|
* - TileMaxZ must not be changed.
|
|
|
|
* - Allow autoslope by default.
|
|
|
|
* - Disallow autoslope if callback succeeds and returns non-zero.
|
|
|
|
*/
|
2011-11-04 10:22:27 +00:00
|
|
|
Slope tileh_old = GetTileSlope(tile);
|
2010-08-28 19:00:21 +00:00
|
|
|
/* TileMaxZ must not be changed. Slopes must not be steep. */
|
2011-11-04 10:30:10 +00:00
|
|
|
if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
|
2010-08-28 19:00:21 +00:00
|
|
|
const ObjectSpec *spec = ObjectSpec::Get(type);
|
|
|
|
|
|
|
|
/* Call callback 'disable autosloping for objects'. */
|
|
|
|
if (HasBit(spec->callback_mask, CBM_OBJ_AUTOSLOPE)) {
|
|
|
|
/* If the callback fails, allow autoslope. */
|
|
|
|
uint16 res = GetObjectCallback(CBID_OBJECT_AUTOSLOPE, 0, 0, spec, Object::GetByTile(tile), tile);
|
2011-11-08 17:26:49 +00:00
|
|
|
if (res == CALLBACK_FAILED || !ConvertBooleanCallback(spec->grf_prop.grffile, CBID_OBJECT_AUTOSLOPE, res)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
|
2010-08-28 19:00:21 +00:00
|
|
|
} else if (spec->enabled) {
|
|
|
|
/* allow autoslope */
|
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 22:27:40 +00:00
|
|
|
}
|
|
|
|
|
2007-08-30 17:17:04 +00:00
|
|
|
return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
|
|
}
|
|
|
|
|
2010-08-08 10:59:30 +00:00
|
|
|
extern const TileTypeProcs _tile_type_object_procs = {
|
|
|
|
DrawTile_Object, // draw_tile_proc
|
2011-11-04 10:18:13 +00:00
|
|
|
GetSlopePixelZ_Object, // get_slope_z_proc
|
2010-08-08 10:59:30 +00:00
|
|
|
ClearTile_Object, // clear_tile_proc
|
|
|
|
AddAcceptedCargo_Object, // add_accepted_cargo_proc
|
|
|
|
GetTileDesc_Object, // get_tile_desc_proc
|
|
|
|
GetTileTrackStatus_Object, // get_tile_track_status_proc
|
|
|
|
ClickTile_Object, // click_tile_proc
|
2010-08-28 18:51:47 +00:00
|
|
|
AnimateTile_Object, // animate_tile_proc
|
2011-11-08 17:37:32 +00:00
|
|
|
TileLoop_Object, // tile_loop_proc
|
|
|
|
ChangeTileOwner_Object, // change_tile_owner_proc
|
2010-08-08 10:59:30 +00:00
|
|
|
NULL, // add_produced_cargo_proc
|
|
|
|
NULL, // vehicle_enter_tile_proc
|
|
|
|
GetFoundation_Object, // get_foundation_proc
|
|
|
|
TerraformTile_Object, // terraform_tile_proc
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|