src/unmovable_cmd.c
changeset 5475 2e6990a8c7c4
parent 5385 3868f2e6db9b
equal deleted inserted replaced
5474:ac55aefc54f3 5475:2e6990a8c7c4
       
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 #include "openttd.h"
       
     5 #include "bridge_map.h"
       
     6 #include "table/strings.h"
       
     7 #include "table/sprites.h"
       
     8 #include "functions.h"
       
     9 #include "map.h"
       
    10 #include "tile.h"
       
    11 #include "command.h"
       
    12 #include "viewport.h"
       
    13 #include "player.h"
       
    14 #include "gui.h"
       
    15 #include "station.h"
       
    16 #include "economy.h"
       
    17 #include "town.h"
       
    18 #include "sprite.h"
       
    19 #include "unmovable_map.h"
       
    20 #include "variables.h"
       
    21 #include "table/unmovable_land.h"
       
    22 #include "genworld.h"
       
    23 
       
    24 /** Destroy a HQ.
       
    25  * During normal gameplay you can only implicitely destroy a HQ when you are
       
    26  * rebuilding it. Otherwise, only water can destroy it.
       
    27  * @param tile tile coordinates where HQ is located to destroy
       
    28  * @param flags docommand flags of calling function
       
    29  */
       
    30 static int32 DestroyCompanyHQ(PlayerID pid, uint32 flags)
       
    31 {
       
    32 	Player* p = GetPlayer(pid);
       
    33 
       
    34 	SET_EXPENSES_TYPE(EXPENSES_PROPERTY);
       
    35 
       
    36 	if (flags & DC_EXEC) {
       
    37 		TileIndex t = p->location_of_house;
       
    38 
       
    39 		DoClearSquare(t + TileDiffXY(0, 0));
       
    40 		DoClearSquare(t + TileDiffXY(0, 1));
       
    41 		DoClearSquare(t + TileDiffXY(1, 0));
       
    42 		DoClearSquare(t + TileDiffXY(1, 1));
       
    43 		p->location_of_house = 0; // reset HQ position
       
    44 		InvalidateWindow(WC_COMPANY, pid);
       
    45 	}
       
    46 
       
    47 	// cost of relocating company is 1% of company value
       
    48 	return CalculateCompanyValue(p) / 100;
       
    49 }
       
    50 
       
    51 void UpdateCompanyHQ(Player *p, uint score)
       
    52 {
       
    53 	byte val;
       
    54 	TileIndex tile = p->location_of_house;
       
    55 
       
    56 	if (tile == 0) return;
       
    57 
       
    58 	(val = 0, score < 170) ||
       
    59 	(val++, score < 350) ||
       
    60 	(val++, score < 520) ||
       
    61 	(val++, score < 720) ||
       
    62 	(val++, true);
       
    63 
       
    64 	EnlargeCompanyHQ(tile, val);
       
    65 
       
    66 	MarkTileDirtyByTile(tile + TileDiffXY(0, 0));
       
    67 	MarkTileDirtyByTile(tile + TileDiffXY(0, 1));
       
    68 	MarkTileDirtyByTile(tile + TileDiffXY(1, 0));
       
    69 	MarkTileDirtyByTile(tile + TileDiffXY(1, 1));
       
    70 }
       
    71 
       
    72 /** Build or relocate the HQ. This depends if the HQ is already built or not
       
    73  * @param tile tile where the HQ will be built or relocated to
       
    74  * @param p1 unused
       
    75  * @param p2 unused
       
    76  */
       
    77 extern int32 CheckFlatLandBelow(TileIndex tile, uint w, uint h, uint flags, uint invalid_dirs, int *);
       
    78 int32 CmdBuildCompanyHQ(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
       
    79 {
       
    80 	Player *p = GetPlayer(_current_player);
       
    81 	int cost;
       
    82 	int32 ret;
       
    83 
       
    84 	SET_EXPENSES_TYPE(EXPENSES_PROPERTY);
       
    85 
       
    86 	ret = CheckFlatLandBelow(tile, 2, 2, flags, 0, NULL);
       
    87 	if (CmdFailed(ret)) return ret;
       
    88 	cost = ret;
       
    89 
       
    90 	if (p->location_of_house != 0) { /* Moving HQ */
       
    91 		cost += DestroyCompanyHQ(_current_player, flags);
       
    92 	}
       
    93 
       
    94 	if (flags & DC_EXEC) {
       
    95 		int score = UpdateCompanyRatingAndValue(p, false);
       
    96 
       
    97 		p->location_of_house = tile;
       
    98 
       
    99 		MakeCompanyHQ(tile, _current_player);
       
   100 
       
   101 		UpdateCompanyHQ(p, score);
       
   102 		InvalidateWindow(WC_COMPANY, p->index);
       
   103 	}
       
   104 
       
   105 	return cost;
       
   106 }
       
   107 
       
   108 static void DrawTile_Unmovable(TileInfo *ti)
       
   109 {
       
   110 	uint32 image, ormod;
       
   111 
       
   112 	switch (GetUnmovableType(ti->tile)) {
       
   113 		case UNMOVABLE_TRANSMITTER:
       
   114 		case UNMOVABLE_LIGHTHOUSE: {
       
   115 			const DrawTileUnmovableStruct* dtus;
       
   116 
       
   117 			if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, ti->tileh);
       
   118 			DrawClearLandTile(ti, 2);
       
   119 
       
   120 			dtus = &_draw_tile_unmovable_data[GetUnmovableType(ti->tile)];
       
   121 
       
   122 			image = dtus->image;
       
   123 			if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
       
   124 
       
   125 			AddSortableSpriteToDraw(
       
   126 				image, ti->x | dtus->subcoord_x, ti->y | dtus->subcoord_y,
       
   127 				dtus->width, dtus->height, dtus->z_size, ti->z
       
   128 			);
       
   129 			break;
       
   130 		}
       
   131 
       
   132 		case UNMOVABLE_STATUE:
       
   133 			DrawGroundSprite(SPR_CONCRETE_GROUND);
       
   134 
       
   135 			image = PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile));
       
   136 			image += PALETTE_MODIFIER_COLOR | SPR_STATUE_COMPANY;
       
   137 			if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
       
   138 			AddSortableSpriteToDraw(image, ti->x, ti->y, 16, 16, 25, ti->z);
       
   139 			break;
       
   140 
       
   141 		case UNMOVABLE_OWNED_LAND:
       
   142 			DrawClearLandTile(ti, 0);
       
   143 
       
   144 			AddSortableSpriteToDraw(
       
   145 				PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile)) + PALETTE_MODIFIER_COLOR + SPR_BOUGHT_LAND,
       
   146 				ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2, 1, 1, 10, GetSlopeZ(ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2)
       
   147 			);
       
   148 			DrawBridgeMiddle(ti);
       
   149 			break;
       
   150 
       
   151 		default: {
       
   152 			const DrawTileSeqStruct* dtss;
       
   153 			const DrawTileSprites* t;
       
   154 
       
   155 			assert(IsCompanyHQ(ti->tile));
       
   156 			if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, ti->tileh);
       
   157 
       
   158 			ormod = PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile));
       
   159 
       
   160 			t = &_unmovable_display_datas[GetCompanyHQSection(ti->tile)];
       
   161 			DrawGroundSprite(t->ground_sprite | ormod);
       
   162 
       
   163 			foreach_draw_tile_seq(dtss, t->seq) {
       
   164 				image = dtss->image;
       
   165 				if (_display_opt & DO_TRANS_BUILDINGS) {
       
   166 					MAKE_TRANSPARENT(image);
       
   167 				} else {
       
   168 					image |= ormod;
       
   169 				}
       
   170 				AddSortableSpriteToDraw(
       
   171 					image,
       
   172 					ti->x + dtss->delta_x, ti->y + dtss->delta_y,
       
   173 					dtss->size_x, dtss->size_y,
       
   174 					dtss->size_z, ti->z + dtss->delta_z
       
   175 				);
       
   176 			}
       
   177 			break;
       
   178 		}
       
   179 	}
       
   180 }
       
   181 
       
   182 static uint GetSlopeZ_Unmovable(TileIndex tile, uint x, uint y)
       
   183 {
       
   184 	if (IsOwnedLand(tile)) {
       
   185 		uint z;
       
   186 		uint tileh = GetTileSlope(tile, &z);
       
   187 
       
   188 		return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
       
   189 	} else {
       
   190 		return GetTileMaxZ(tile);
       
   191 	}
       
   192 }
       
   193 
       
   194 static Slope GetSlopeTileh_Unmovable(TileIndex tile, Slope tileh)
       
   195 {
       
   196 	return IsOwnedLand(tile) ? tileh : SLOPE_FLAT;
       
   197 }
       
   198 
       
   199 static int32 ClearTile_Unmovable(TileIndex tile, byte flags)
       
   200 {
       
   201 	if (IsCompanyHQ(tile)) {
       
   202 		if (_current_player == OWNER_WATER) {
       
   203 			return DestroyCompanyHQ(GetTileOwner(tile), DC_EXEC);
       
   204 		} else {
       
   205 			return_cmd_error(STR_5804_COMPANY_HEADQUARTERS_IN);
       
   206 		}
       
   207 	}
       
   208 
       
   209 	if (IsOwnedLand(tile)) {
       
   210 		return DoCommand(tile, 0, 0, flags, CMD_SELL_LAND_AREA);
       
   211 	}
       
   212 
       
   213 	// checks if you're allowed to remove unmovable things
       
   214 	if (_game_mode != GM_EDITOR && _current_player != OWNER_WATER && ((flags & DC_AUTO || !_cheats.magic_bulldozer.value)) )
       
   215 		return_cmd_error(STR_5800_OBJECT_IN_THE_WAY);
       
   216 
       
   217 	if (flags & DC_EXEC) {
       
   218 		DoClearSquare(tile);
       
   219 	}
       
   220 
       
   221 	return 0;
       
   222 }
       
   223 
       
   224 static void GetAcceptedCargo_Unmovable(TileIndex tile, AcceptedCargo ac)
       
   225 {
       
   226 	uint level; // HQ level (depends on company performance) in the range 1..5.
       
   227 
       
   228 	if (!IsCompanyHQ(tile)) return;
       
   229 
       
   230 	/* HQ accepts passenger and mail; but we have to divide the values
       
   231 	 * between 4 tiles it occupies! */
       
   232 
       
   233 	level = GetCompanyHQSize(tile) + 1;
       
   234 
       
   235 	// Top town building generates 10, so to make HQ interesting, the top
       
   236 	// type makes 20.
       
   237 	ac[CT_PASSENGERS] = max(1, level);
       
   238 
       
   239 	// Top town building generates 4, HQ can make up to 8. The
       
   240 	// proportion passengers:mail is different because such a huge
       
   241 	// commercial building generates unusually high amount of mail
       
   242 	// correspondence per physical visitor.
       
   243 	ac[CT_MAIL] = max(1, level / 2);
       
   244 }
       
   245 
       
   246 
       
   247 static void GetTileDesc_Unmovable(TileIndex tile, TileDesc *td)
       
   248 {
       
   249 	switch (GetUnmovableType(tile)) {
       
   250 		case UNMOVABLE_TRANSMITTER: td->str = STR_5801_TRANSMITTER; break;
       
   251 		case UNMOVABLE_LIGHTHOUSE:  td->str = STR_5802_LIGHTHOUSE; break;
       
   252 		case UNMOVABLE_STATUE:      td->str = STR_2016_STATUE; break;
       
   253 		case UNMOVABLE_OWNED_LAND:  td->str = STR_5805_COMPANY_OWNED_LAND; break;
       
   254 		default:                    td->str = STR_5803_COMPANY_HEADQUARTERS; break;
       
   255 	}
       
   256 	td->owner = GetTileOwner(tile);
       
   257 }
       
   258 
       
   259 static void AnimateTile_Unmovable(TileIndex tile)
       
   260 {
       
   261 	/* not used */
       
   262 }
       
   263 
       
   264 static void TileLoop_Unmovable(TileIndex tile)
       
   265 {
       
   266 	uint level; // HQ level (depends on company performance) in the range 1..5.
       
   267 	uint32 r;
       
   268 
       
   269 	if (!IsCompanyHQ(tile)) return;
       
   270 
       
   271 	/* HQ accepts passenger and mail; but we have to divide the values
       
   272 	 * between 4 tiles it occupies! */
       
   273 
       
   274 	level = GetCompanyHQSize(tile) + 1;
       
   275 	assert(level < 6);
       
   276 
       
   277 	r = Random();
       
   278 	// Top town buildings generate 250, so the top HQ type makes 256.
       
   279 	if (GB(r, 0, 8) < (256 / 4 / (6 - level))) {
       
   280 		uint amt = GB(r, 0, 8) / 8 / 4 + 1;
       
   281 		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
       
   282 		MoveGoodsToStation(tile, 2, 2, CT_PASSENGERS, amt);
       
   283 	}
       
   284 
       
   285 	// Top town building generates 90, HQ can make up to 196. The
       
   286 	// proportion passengers:mail is about the same as in the acceptance
       
   287 	// equations.
       
   288 	if (GB(r, 8, 8) < (196 / 4 / (6 - level))) {
       
   289 		uint amt = GB(r, 8, 8) / 8 / 4 + 1;
       
   290 		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
       
   291 		MoveGoodsToStation(tile, 2, 2, CT_MAIL, amt);
       
   292 	}
       
   293 }
       
   294 
       
   295 
       
   296 static uint32 GetTileTrackStatus_Unmovable(TileIndex tile, TransportType mode)
       
   297 {
       
   298 	return 0;
       
   299 }
       
   300 
       
   301 static void ClickTile_Unmovable(TileIndex tile)
       
   302 {
       
   303 	if (IsCompanyHQ(tile)) ShowPlayerCompany(GetTileOwner(tile));
       
   304 }
       
   305 
       
   306 
       
   307 /* checks, if a radio tower is within a 9x9 tile square around tile */
       
   308 static bool IsRadioTowerNearby(TileIndex tile)
       
   309 {
       
   310 	TileIndex tile_s = tile - TileDiffXY(4, 4);
       
   311 
       
   312 	BEGIN_TILE_LOOP(tile, 9, 9, tile_s)
       
   313 		if (IsTransmitterTile(tile)) return true;
       
   314 	END_TILE_LOOP(tile, 9, 9, tile_s)
       
   315 
       
   316 	return false;
       
   317 }
       
   318 
       
   319 void GenerateUnmovables(void)
       
   320 {
       
   321 	int i, li, j, loop_count;
       
   322 	TileIndex tile;
       
   323 	uint h;
       
   324 	uint maxx;
       
   325 	uint maxy;
       
   326 
       
   327 	if (_opt.landscape == LT_CANDY) return;
       
   328 
       
   329 	/* add radio tower */
       
   330 	i = ScaleByMapSize(1000);
       
   331 	j = ScaleByMapSize(15); // maximum number of radio towers on the map
       
   332 	li = ScaleByMapSize1D((Random() & 3) + 7);
       
   333 	SetGeneratingWorldProgress(GWP_UNMOVABLE, j + li);
       
   334 
       
   335 	do {
       
   336 		tile = RandomTile();
       
   337 		if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h >= TILE_HEIGHT * 4) {
       
   338 			if (IsRadioTowerNearby(tile)) continue;
       
   339 			MakeTransmitter(tile);
       
   340 			IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
       
   341 			if (--j == 0) break;
       
   342 		}
       
   343 	} while (--i);
       
   344 
       
   345 	if (_opt.landscape == LT_DESERT) return;
       
   346 
       
   347 	/* add lighthouses */
       
   348 	i = li;
       
   349 	maxx = MapMaxX();
       
   350 	maxy = MapMaxY();
       
   351 	loop_count = 0;
       
   352 	do {
       
   353 		uint32 r;
       
   354 		DiagDirection dir;
       
   355 		int perimeter;
       
   356 
       
   357 restart:
       
   358 		/* Avoid infinite loops */
       
   359 		if (++loop_count > 1000) break;
       
   360 
       
   361 		r = Random();
       
   362 
       
   363 		/* Scatter the lighthouses more evenly around the perimeter */
       
   364 		perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
       
   365 		for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
       
   366 			perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
       
   367 		}
       
   368 
       
   369 		switch (dir) {
       
   370 			default:
       
   371 			case DIAGDIR_NE: tile = TileXY(maxx,     r % maxy); break;
       
   372 			case DIAGDIR_SE: tile = TileXY(r % maxx, 0);        break;
       
   373 			case DIAGDIR_SW: tile = TileXY(0,        r % maxy); break;
       
   374 			case DIAGDIR_NW: tile = TileXY(r % maxx, maxy);     break;
       
   375 		}
       
   376 		j = 20;
       
   377 		do {
       
   378 			if (--j == 0) goto restart;
       
   379 			tile = TILE_MASK(tile + TileOffsByDiagDir(dir));
       
   380 		} while (!(IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h <= TILE_HEIGHT * 2));
       
   381 
       
   382 		assert(tile == TILE_MASK(tile));
       
   383 
       
   384 		MakeLighthouse(tile);
       
   385 		IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
       
   386 	} while (--i);
       
   387 }
       
   388 
       
   389 static void ChangeTileOwner_Unmovable(TileIndex tile, PlayerID old_player, PlayerID new_player)
       
   390 {
       
   391 	if (!IsTileOwner(tile, old_player)) return;
       
   392 
       
   393 	if (IsOwnedLand(tile) && new_player != PLAYER_SPECTATOR) {
       
   394 		SetTileOwner(tile, new_player);
       
   395 	} else {
       
   396 		DoClearSquare(tile);
       
   397 	}
       
   398 }
       
   399 
       
   400 const TileTypeProcs _tile_type_unmovable_procs = {
       
   401 	DrawTile_Unmovable,             /* draw_tile_proc */
       
   402 	GetSlopeZ_Unmovable,            /* get_slope_z_proc */
       
   403 	ClearTile_Unmovable,            /* clear_tile_proc */
       
   404 	GetAcceptedCargo_Unmovable,     /* get_accepted_cargo_proc */
       
   405 	GetTileDesc_Unmovable,          /* get_tile_desc_proc */
       
   406 	GetTileTrackStatus_Unmovable,   /* get_tile_track_status_proc */
       
   407 	ClickTile_Unmovable,            /* click_tile_proc */
       
   408 	AnimateTile_Unmovable,          /* animate_tile_proc */
       
   409 	TileLoop_Unmovable,             /* tile_loop_clear */
       
   410 	ChangeTileOwner_Unmovable,      /* change_tile_owner_clear */
       
   411 	NULL,                           /* get_produced_cargo_proc */
       
   412 	NULL,                           /* vehicle_enter_tile_proc */
       
   413 	GetSlopeTileh_Unmovable,        /* get_slope_tileh_proc */
       
   414 };