tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" celestar@5385: #include "bridge_map.h" truelight@4300: #include "heightmap.h" tron@3144: #include "clear_map.h" tron@1349: #include "spritecache.h" truelight@0: #include rubidium@8225: #include "viewport_func.h" rubidium@8116: #include "command_func.h" maedhros@6343: #include "landscape.h" tron@2159: #include "variables.h" tron@3144: #include "void_map.h" tron@3111: #include "water_map.h" truelight@4300: #include "tgp.h" truelight@4300: #include "genworld.h" rubidium@8119: #include "tile_cmd.h" rubidium@8130: #include "core/alloc_func.hpp" belugas@8151: #include "fios.h" rubidium@8131: #include "window_func.h" rubidium@8131: #include "functions.h" rubidium@8140: #include "date_func.h" rubidium@8144: #include "vehicle_func.h" rubidium@8270: #include "settings_type.h" truelight@0: rubidium@8264: #include "table/sprites.h" rubidium@8264: truelight@183: extern const TileTypeProcs truelight@0: _tile_type_clear_procs, truelight@0: _tile_type_rail_procs, truelight@0: _tile_type_road_procs, truelight@0: _tile_type_town_procs, truelight@0: _tile_type_trees_procs, truelight@0: _tile_type_station_procs, truelight@0: _tile_type_water_procs, truelight@0: _tile_type_dummy_procs, truelight@0: _tile_type_industry_procs, truelight@0: _tile_type_tunnelbridge_procs, truelight@0: _tile_type_unmovable_procs; truelight@0: truelight@0: const TileTypeProcs * const _tile_type_procs[16] = { truelight@0: &_tile_type_clear_procs, truelight@0: &_tile_type_rail_procs, truelight@0: &_tile_type_road_procs, truelight@0: &_tile_type_town_procs, truelight@0: &_tile_type_trees_procs, truelight@0: &_tile_type_station_procs, truelight@0: &_tile_type_water_procs, truelight@0: &_tile_type_dummy_procs, truelight@0: &_tile_type_industry_procs, truelight@0: &_tile_type_tunnelbridge_procs, truelight@0: &_tile_type_unmovable_procs, truelight@0: }; truelight@0: truelight@0: /* landscape slope => sprite */ truelight@0: const byte _tileh_to_sprite[32] = { rubidium@4344: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, rubidium@4344: 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 17, 0, 15, 18, 0, truelight@0: }; truelight@0: rubidium@7335: SnowLine *_snow_line = NULL; tron@1958: rubidium@7335: /** rubidium@7335: * Applys a foundation to a slope. rubidium@7335: * rubidium@7335: * @pre Foundation and slope must be valid combined. rubidium@7335: * @param f The #Foundation. rubidium@7335: * @param s The #Slope to modify. rubidium@7335: * @return Increment to the tile Z coordinate. rubidium@7335: */ rubidium@7335: uint ApplyFoundationToSlope(Foundation f, Slope *s) rubidium@7335: { rubidium@7335: rubidium@7335: if (!IsFoundation(f)) return 0; rubidium@7335: rubidium@7335: if (IsLeveledFoundation(f)) { rubidium@7335: *s = SLOPE_FLAT; rubidium@7335: return TILE_HEIGHT; rubidium@7335: } rubidium@7335: rubidium@7770: if (f != FOUNDATION_STEEP_BOTH && IsNonContinuousFoundation(f)) { rubidium@7770: *s = HalftileSlope(*s, GetHalftileFoundationCorner(f)); rubidium@7770: return 0; rubidium@7770: } rubidium@7770: rubidium@7770: if (IsSpecialRailFoundation(f)) { rubidium@7770: *s = SlopeWithThreeCornersRaised(OppositeCorner(GetRailFoundationCorner(f))); rubidium@7770: return 0; rubidium@7770: } rubidium@7770: rubidium@7335: uint dz = IsSteepSlope(*s) ? TILE_HEIGHT : 0; rubidium@7678: Corner highest_corner = GetHighestSlopeCorner(*s); rubidium@7335: rubidium@7335: switch (f) { rubidium@7335: case FOUNDATION_INCLINED_X: rubidium@7678: *s = (((highest_corner == CORNER_W) || (highest_corner == CORNER_S)) ? SLOPE_SW : SLOPE_NE); rubidium@7335: break; rubidium@7335: rubidium@7335: case FOUNDATION_INCLINED_Y: rubidium@7678: *s = (((highest_corner == CORNER_S) || (highest_corner == CORNER_E)) ? SLOPE_SE : SLOPE_NW); rubidium@7335: break; rubidium@7335: rubidium@7335: case FOUNDATION_STEEP_LOWER: rubidium@7678: *s = SlopeWithOneCornerRaised(highest_corner); rubidium@7335: break; rubidium@7335: rubidium@7770: case FOUNDATION_STEEP_BOTH: rubidium@7770: *s = HalftileSlope(SlopeWithOneCornerRaised(highest_corner), highest_corner); rubidium@7335: break; rubidium@7335: rubidium@7335: default: NOT_REACHED(); rubidium@7335: } rubidium@7335: return dz; rubidium@7335: } rubidium@7335: truelight@0: tron@3636: uint GetPartialZ(int x, int y, Slope corners) truelight@0: { rubidium@7764: if (IsHalftileSlope(corners)) { rubidium@7764: switch (GetHalftileSlopeCorner(corners)) { rubidium@7764: case CORNER_W: rubidium@7764: if (x - y >= 0) return GetSlopeMaxZ(corners); rubidium@7764: break; rubidium@7764: rubidium@7764: case CORNER_S: rubidium@7764: if (x - (y ^ 0xF) >= 0) return GetSlopeMaxZ(corners); rubidium@7764: break; rubidium@7764: rubidium@7764: case CORNER_E: rubidium@7764: if (y - x >= 0) return GetSlopeMaxZ(corners); rubidium@7764: break; rubidium@7764: rubidium@7764: case CORNER_N: rubidium@7764: if ((y ^ 0xF) - x >= 0) return GetSlopeMaxZ(corners); rubidium@7764: break; rubidium@7764: rubidium@7764: default: NOT_REACHED(); rubidium@7764: } rubidium@7764: } rubidium@7764: truelight@0: int z = 0; truelight@0: rubidium@7764: switch (corners & ~SLOPE_HALFTILE_MASK) { tron@3636: case SLOPE_W: truelight@0: if (x - y >= 0) truelight@0: z = (x - y) >> 1; truelight@0: break; truelight@183: tron@3636: case SLOPE_S: rubidium@6491: y ^= 0xF; truelight@0: if ( (x - y) >= 0) truelight@0: z = (x - y) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_SW: rubidium@6491: z = (x >> 1) + 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_E: truelight@0: if (y - x >= 0) truelight@0: z = (y - x) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_EW: tron@3636: case SLOPE_NS: tron@3636: case SLOPE_ELEVATED: truelight@0: z = 4; truelight@0: break; truelight@0: tron@3636: case SLOPE_SE: rubidium@6491: z = (y >> 1) + 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_WSE: truelight@0: z = 8; rubidium@6491: y ^= 0xF; truelight@0: if (x - y < 0) truelight@0: z += (x - y) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_N: truelight@0: y ^= 0xF; truelight@0: if (y - x >= 0) truelight@0: z = (y - x) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_NW: rubidium@6491: z = (y ^ 0xF) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_NWS: truelight@0: z = 8; truelight@0: if (x - y < 0) truelight@0: z += (x - y) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_NE: rubidium@6491: z = (x ^ 0xF) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_ENW: truelight@0: z = 8; truelight@0: y ^= 0xF; truelight@0: if (y - x < 0) truelight@0: z += (y - x) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_SEN: truelight@0: z = 8; truelight@0: if (y - x < 0) truelight@0: z += (y - x) >> 1; truelight@0: break; truelight@0: tron@3636: case SLOPE_STEEP_S: rubidium@6491: z = 1 + ((x + y) >> 1); truelight@0: break; truelight@0: tron@3636: case SLOPE_STEEP_W: rubidium@6491: z = 1 + ((x + (y ^ 0xF)) >> 1); truelight@0: break; truelight@0: tron@3636: case SLOPE_STEEP_N: rubidium@6491: z = 1 + (((x ^ 0xF) + (y ^ 0xF)) >> 1); truelight@0: break; truelight@183: tron@3636: case SLOPE_STEEP_E: truelight@7304: z = 1 + (((x ^ 0xF) + y) >> 1); truelight@0: break; tron@3636: tron@3636: default: break; truelight@0: } truelight@0: truelight@0: return z; truelight@0: } truelight@0: tron@2951: uint GetSlopeZ(int x, int y) truelight@0: { tron@4231: TileIndex tile = TileVirtXY(x, y); truelight@0: tron@4231: return _tile_type_procs[GetTileType(tile)]->get_slope_z_proc(tile, x, y); truelight@0: } truelight@0: rubidium@7728: /** rubidium@7770: * Determine the Z height of a corner relative to TileZ. rubidium@7770: * rubidium@7770: * @pre The slope must not be a halftile slope. rubidium@7770: * rubidium@7770: * @param tileh The slope. rubidium@7770: * @param corner The corner. rubidium@7770: * @return Z position of corner relative to TileZ. rubidium@7770: */ rubidium@7770: int GetSlopeZInCorner(Slope tileh, Corner corner) rubidium@7770: { rubidium@7770: assert(!IsHalftileSlope(tileh)); rubidium@7770: static const int _corner_slopes[4][2] = { rubidium@7770: { SLOPE_W, SLOPE_STEEP_W }, { SLOPE_S, SLOPE_STEEP_S }, { SLOPE_E, SLOPE_STEEP_E }, { SLOPE_N, SLOPE_STEEP_N } rubidium@7770: }; rubidium@7770: return ((tileh & _corner_slopes[corner][0]) != 0 ? TILE_HEIGHT : 0) + (tileh == _corner_slopes[corner][1] ? TILE_HEIGHT : 0); rubidium@7770: } rubidium@7770: rubidium@7770: /** rubidium@7728: * Determine the Z height of the corners of a specific tile edge rubidium@7728: * rubidium@7764: * @note If a tile has a non-continuous halftile foundation, a corner can have different heights wrt. it's edges. rubidium@7764: * rubidium@7728: * @pre z1 and z2 must be initialized (typ. with TileZ). The corner heights just get added. rubidium@7728: * rubidium@7728: * @param tileh The slope of the tile. rubidium@7728: * @param edge The edge of interest. rubidium@7728: * @param z1 Gets incremented by the height of the first corner of the edge. (near corner wrt. the camera) rubidium@7728: * @param z2 Gets incremented by the height of the second corner of the edge. (far corner wrt. the camera) rubidium@7728: */ rubidium@7728: void GetSlopeZOnEdge(Slope tileh, DiagDirection edge, int *z1, int *z2) rubidium@7728: { rubidium@7728: static const Slope corners[4][4] = { rubidium@7728: /* corner | steep slope rubidium@7728: * z1 z2 | z1 z2 */ rubidium@7728: {SLOPE_E, SLOPE_N, SLOPE_STEEP_E, SLOPE_STEEP_N}, // DIAGDIR_NE, z1 = E, z2 = N rubidium@7728: {SLOPE_S, SLOPE_E, SLOPE_STEEP_S, SLOPE_STEEP_E}, // DIAGDIR_SE, z1 = S, z2 = E rubidium@7728: {SLOPE_S, SLOPE_W, SLOPE_STEEP_S, SLOPE_STEEP_W}, // DIAGDIR_SW, z1 = S, z2 = W rubidium@7728: {SLOPE_W, SLOPE_N, SLOPE_STEEP_W, SLOPE_STEEP_N}, // DIAGDIR_NW, z1 = W, z2 = N rubidium@7728: }; rubidium@7728: rubidium@7764: int halftile_test = (IsHalftileSlope(tileh) ? SlopeWithOneCornerRaised(GetHalftileSlopeCorner(tileh)) : 0); rubidium@7764: if (halftile_test == corners[edge][0]) *z2 += TILE_HEIGHT; // The slope is non-continuous in z2. z2 is on the upper side. rubidium@7764: if (halftile_test == corners[edge][1]) *z1 += TILE_HEIGHT; // The slope is non-continuous in z1. z1 is on the upper side. rubidium@7764: rubidium@7728: if ((tileh & corners[edge][0]) != 0) *z1 += TILE_HEIGHT; // z1 is raised rubidium@7728: if ((tileh & corners[edge][1]) != 0) *z2 += TILE_HEIGHT; // z2 is raised rubidium@7764: if ((tileh & ~SLOPE_HALFTILE_MASK) == corners[edge][2]) *z1 += TILE_HEIGHT; // z1 is highest corner of a steep slope rubidium@7764: if ((tileh & ~SLOPE_HALFTILE_MASK) == corners[edge][3]) *z2 += TILE_HEIGHT; // z2 is highest corner of a steep slope rubidium@7728: } tron@4061: tron@4061: static Slope GetFoundationSlope(TileIndex tile, uint* z) dominik@37: { tron@4061: Slope tileh = GetTileSlope(tile, z); rubidium@7335: Foundation f = _tile_type_procs[GetTileType(tile)]->get_foundation_proc(tile, tileh); rubidium@7335: *z += ApplyFoundationToSlope(f, &tileh); rubidium@7335: return tileh; tron@4061: } dominik@39: tron@4061: tron@4061: static bool HasFoundationNW(TileIndex tile, Slope slope_here, uint z_here) tron@4061: { tron@4061: uint z; tron@4061: rubidium@7728: int z_W_here = z_here; rubidium@7728: int z_N_here = z_here; rubidium@7728: GetSlopeZOnEdge(slope_here, DIAGDIR_NW, &z_W_here, &z_N_here); rubidium@7728: rubidium@7728: Slope slope = GetFoundationSlope(TILE_ADDXY(tile, 0, -1), &z); rubidium@7728: int z_W = z; rubidium@7728: int z_N = z; rubidium@7728: GetSlopeZOnEdge(slope, DIAGDIR_SE, &z_W, &z_N); rubidium@7728: rubidium@7728: return (z_N_here > z_N) || (z_W_here > z_W); dominik@37: } dominik@37: tron@4061: tron@4061: static bool HasFoundationNE(TileIndex tile, Slope slope_here, uint z_here) tron@4061: { tron@4061: uint z; tron@4061: rubidium@7728: int z_E_here = z_here; rubidium@7728: int z_N_here = z_here; rubidium@7728: GetSlopeZOnEdge(slope_here, DIAGDIR_NE, &z_E_here, &z_N_here); rubidium@7728: rubidium@7728: Slope slope = GetFoundationSlope(TILE_ADDXY(tile, -1, 0), &z); rubidium@7728: int z_E = z; rubidium@7728: int z_N = z; rubidium@7728: GetSlopeZOnEdge(slope, DIAGDIR_SW, &z_E, &z_N); rubidium@7728: rubidium@7728: return (z_N_here > z_N) || (z_E_here > z_E); tron@4061: } tron@4061: tron@4061: rubidium@7335: void DrawFoundation(TileInfo *ti, Foundation f) truelight@0: { rubidium@7335: if (!IsFoundation(f)) return; rubidium@7335: rubidium@7770: /* Two part foundations must be drawn separately */ rubidium@7770: assert(f != FOUNDATION_STEEP_BOTH); rubidium@7770: rubidium@7767: uint sprite_block = 0; tron@4061: uint z; rubidium@7335: Slope slope = GetFoundationSlope(ti->tile, &z); dominik@39: rubidium@7767: /* Select the needed block of foundations sprites rubidium@7767: * Block 0: Walls at NW and NE edge rubidium@7767: * Block 1: Wall at NE edge rubidium@7767: * Block 2: Wall at NW edge rubidium@7767: * Block 3: No walls at NW or NE edge rubidium@7767: */ rubidium@7767: if (!HasFoundationNW(ti->tile, slope, z)) sprite_block += 1; rubidium@7767: if (!HasFoundationNE(ti->tile, slope, z)) sprite_block += 2; rubidium@7767: rubidium@7767: /* Use the original slope sprites if NW and NE borders should be visible */ rubidium@7767: SpriteID leveled_base = (sprite_block == 0 ? (int)SPR_FOUNDATION_BASE : (SPR_SLOPES_VIRTUAL_BASE + sprite_block * SPR_TRKFOUND_BLOCK_SIZE)); rubidium@7767: SpriteID inclined_base = SPR_SLOPES_VIRTUAL_BASE + SPR_SLOPES_INCLINED_OFFSET + sprite_block * SPR_TRKFOUND_BLOCK_SIZE; rubidium@7770: SpriteID halftile_base = SPR_HALFTILE_FOUNDATION_BASE + sprite_block * SPR_HALFTILE_BLOCK_SIZE; dominik@37: tron@4253: if (IsSteepSlope(ti->tileh)) { rubidium@7770: if (!IsNonContinuousFoundation(f)) { rubidium@7770: /* Lower part of foundation */ rubidium@7770: AddSortableSpriteToDraw( rubidium@7770: leveled_base + (ti->tileh & ~SLOPE_STEEP), PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z rubidium@7770: ); rubidium@7770: } rubidium@7335: rubidium@7678: Corner highest_corner = GetHighestSlopeCorner(ti->tileh); rubidium@7335: ti->z += ApplyFoundationToSlope(f, &ti->tileh); rubidium@7335: rubidium@7335: if (IsInclinedFoundation(f)) { rubidium@7335: /* inclined foundation */ rubidium@7335: byte inclined = highest_corner * 2 + (f == FOUNDATION_INCLINED_Y ? 1 : 0); rubidium@7335: rubidium@7767: AddSortableSpriteToDraw(inclined_base + inclined, PAL_NONE, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: OffsetGroundSprite(31, 9); rubidium@7770: } else if (f == FOUNDATION_STEEP_LOWER) { belugas@6201: /* one corner raised */ tron@4253: OffsetGroundSprite(31, 1); rubidium@7770: } else { rubidium@7770: /* halftile foundation */ rubidium@7770: int x_bb = (((highest_corner == CORNER_W) || (highest_corner == CORNER_S)) ? 8 : 0); rubidium@7770: int y_bb = (((highest_corner == CORNER_S) || (highest_corner == CORNER_E)) ? 8 : 0); rubidium@7770: rubidium@7770: AddSortableSpriteToDraw(halftile_base + highest_corner, PAL_NONE, ti->x + x_bb, ti->y + y_bb, 8, 8, 7, ti->z + TILE_HEIGHT); rubidium@7770: OffsetGroundSprite(31, 9); tron@4253: } truelight@0: } else { rubidium@7335: if (IsLeveledFoundation(f)) { rubidium@7767: /* leveled foundation */ rubidium@7767: AddSortableSpriteToDraw(leveled_base + ti->tileh, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z); tron@4253: OffsetGroundSprite(31, 1); rubidium@7770: } else if (IsNonContinuousFoundation(f)) { rubidium@7770: /* halftile foundation */ rubidium@7770: Corner halftile_corner = GetHalftileFoundationCorner(f); rubidium@7770: int x_bb = (((halftile_corner == CORNER_W) || (halftile_corner == CORNER_S)) ? 8 : 0); rubidium@7770: int y_bb = (((halftile_corner == CORNER_S) || (halftile_corner == CORNER_E)) ? 8 : 0); rubidium@7770: rubidium@7770: AddSortableSpriteToDraw(halftile_base + halftile_corner, PAL_NONE, ti->x + x_bb, ti->y + y_bb, 8, 8, 7, ti->z); rubidium@7770: OffsetGroundSprite(31, 9); rubidium@7770: } else if (IsSpecialRailFoundation(f)) { rubidium@7770: /* anti-zig-zag foundation */ rubidium@7770: SpriteID spr; rubidium@7770: if (ti->tileh == SLOPE_NS || ti->tileh == SLOPE_EW) { rubidium@7770: /* half of leveled foundation under track corner */ rubidium@7770: spr = leveled_base + SlopeWithThreeCornersRaised(GetRailFoundationCorner(f)); rubidium@7770: } else { rubidium@7770: /* tile-slope = sloped along X/Y, foundation-slope = three corners raised */ rubidium@7770: spr = inclined_base + 2 * GetRailFoundationCorner(f) + ((ti->tileh == SLOPE_SW || ti->tileh == SLOPE_NE) ? 1 : 0); rubidium@7770: } rubidium@7770: AddSortableSpriteToDraw(spr, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z); rubidium@7770: OffsetGroundSprite(31, 9); tron@4253: } else { belugas@6201: /* inclined foundation */ rubidium@7335: byte inclined = GetHighestSlopeCorner(ti->tileh) * 2 + (f == FOUNDATION_INCLINED_Y ? 1 : 0); rubidium@7335: rubidium@7767: AddSortableSpriteToDraw(inclined_base + inclined, PAL_NONE, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: OffsetGroundSprite(31, 9); tron@4246: } rubidium@7335: ti->z += ApplyFoundationToSlope(f, &ti->tileh); truelight@0: } truelight@0: } truelight@0: tron@1589: void DoClearSquare(TileIndex tile) truelight@0: { tron@3447: MakeClear(tile, CLEAR_GRASS, _generating_world ? 3 : 0); tron@2955: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: rubidium@6683: uint32 GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode) truelight@0: { rubidium@6683: return _tile_type_procs[GetTileType(tile)]->get_tile_track_status_proc(tile, mode, sub_mode); truelight@0: } truelight@0: rubidium@5587: void ChangeTileOwner(TileIndex tile, PlayerID old_player, PlayerID new_player) truelight@0: { tron@1214: _tile_type_procs[GetTileType(tile)]->change_tile_owner_proc(tile, old_player, new_player); truelight@0: } truelight@0: tron@1589: void GetAcceptedCargo(TileIndex tile, AcceptedCargo ac) truelight@0: { truelight@0: memset(ac, 0, sizeof(AcceptedCargo)); tron@1214: _tile_type_procs[GetTileType(tile)]->get_accepted_cargo_proc(tile, ac); truelight@0: } truelight@0: tron@1589: void AnimateTile(TileIndex tile) truelight@0: { tron@1214: _tile_type_procs[GetTileType(tile)]->animate_tile_proc(tile); truelight@0: } truelight@0: tron@1589: void ClickTile(TileIndex tile) truelight@0: { tron@1214: _tile_type_procs[GetTileType(tile)]->click_tile_proc(tile); truelight@0: } truelight@0: tron@1589: void GetTileDesc(TileIndex tile, TileDesc *td) truelight@0: { tron@1214: _tile_type_procs[GetTileType(tile)]->get_tile_desc_proc(tile, td); truelight@0: } truelight@0: maedhros@6343: /** maedhros@6343: * Has a snow line table already been loaded. maedhros@6343: * @return true if the table has been loaded already. maedhros@6343: */ maedhros@6343: bool IsSnowLineSet(void) maedhros@6343: { maedhros@6343: return _snow_line != NULL; maedhros@6343: } maedhros@6343: maedhros@6343: /** maedhros@6343: * Set a variable snow line, as loaded from a newgrf file. maedhros@6343: * @param table the 12 * 32 byte table containing the snowline for each day maedhros@6343: */ maedhros@6343: void SetSnowLine(byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]) maedhros@6343: { maedhros@6343: _snow_line = CallocT(1); maedhros@6343: memcpy(_snow_line->table, table, sizeof(_snow_line->table)); maedhros@6343: maedhros@6343: for (uint i = 0; i < SNOW_LINE_MONTHS; i++) { maedhros@6343: for (uint j = 0; j < SNOW_LINE_DAYS; j++) { maedhros@6343: _snow_line->highest_value = max(_snow_line->highest_value, table[i][j]); maedhros@6343: } maedhros@6343: } maedhros@6343: } maedhros@6343: maedhros@6343: /** maedhros@6343: * Get the current snow line, either variable or static. maedhros@6343: * @return the snow line height. maedhros@6343: */ maedhros@6343: byte GetSnowLine(void) maedhros@6343: { maedhros@6343: if (_snow_line == NULL) return _opt.snow_line; maedhros@6343: maedhros@6343: YearMonthDay ymd; maedhros@6343: ConvertDateToYMD(_date, &ymd); maedhros@6343: return _snow_line->table[ymd.month][ymd.day]; maedhros@6343: } maedhros@6343: maedhros@6343: /** maedhros@6343: * Get the highest possible snow line height, either variable or static. maedhros@6343: * @return the highest snow line height. maedhros@6343: */ maedhros@6343: byte HighestSnowLine(void) maedhros@6343: { maedhros@6343: return _snow_line == NULL ? _opt.snow_line : _snow_line->highest_value; maedhros@6343: } maedhros@6343: maedhros@6343: /** maedhros@6343: * Clear the variable snow line table and free the memory. maedhros@6343: */ maedhros@6343: void ClearSnowLine(void) maedhros@6343: { maedhros@6343: free(_snow_line); maedhros@6343: _snow_line = NULL; maedhros@6343: } maedhros@6343: Darkvater@1775: /** Clear a piece of landscape tron@3491: * @param tile tile to clear belugas@6201: * @param flags of operation to conduct Darkvater@1775: * @param p1 unused Darkvater@1775: * @param p2 unused truelight@0: */ rubidium@6943: CommandCost CmdLandscapeClear(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { tron@1214: return _tile_type_procs[GetTileType(tile)]->clear_tile_proc(tile, flags); truelight@0: } truelight@0: Darkvater@1793: /** Clear a big piece of landscape tron@3491: * @param tile end tile of area dragging Darkvater@1793: * @param p1 start tile of area dragging belugas@6201: * @param flags of operation to conduct Darkvater@1793: * @param p2 unused Darkvater@1793: */ rubidium@6943: CommandCost CmdClearArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { rubidium@8230: CommandCost ret, money; rubidium@8230: CommandCost cost(EXPENSES_CONSTRUCTION); tron@3491: int ex; tron@3491: int ey; rubidium@6491: int sx, sy; rubidium@6491: int x, y; truelight@0: bool success = false; truelight@0: tron@2934: if (p1 >= MapSize()) return CMD_ERROR; Darkvater@1793: belugas@6201: /* make sure sx,sy are smaller than ex,ey */ tron@3493: ex = TileX(tile); tron@3493: ey = TileY(tile); tron@3493: sx = TileX(p1); tron@3493: sy = TileY(p1); tron@6106: if (ex < sx) Swap(ex, sx); tron@6106: if (ey < sy) Swap(ey, sy); truelight@0: rubidium@6950: money.AddCost(GetAvailableMoneyForCommand()); truelight@0: tron@3493: for (x = sx; x <= ex; ++x) { tron@3493: for (y = sy; y <= ey; ++y) { tron@3493: ret = DoCommand(TileXY(x, y), 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR); Darkvater@1793: if (CmdFailed(ret)) continue; truelight@0: success = true; truelight@0: truelight@0: if (flags & DC_EXEC) { rubidium@6950: money.AddCost(-ret.GetCost()); rubidium@6950: if (ret.GetCost() > 0 && money.GetCost() < 0) { rubidium@6950: _additional_cash_required = ret.GetCost(); rubidium@6950: return cost; truelight@0: } tron@3493: DoCommand(TileXY(x, y), 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: belugas@6201: /* draw explosion animation... */ Darkvater@1793: if ((x == sx || x == ex) && (y == sy || y == ey)) { belugas@6201: /* big explosion in each corner, or small explosion for single tiles */ tron@3645: CreateEffectVehicleAbove(x * TILE_SIZE + TILE_SIZE / 2, y * TILE_SIZE + TILE_SIZE / 2, 2, tron@1359: sy == ey && sx == ex ? EV_EXPLOSION_SMALL : EV_EXPLOSION_LARGE tron@1359: ); truelight@0: } truelight@0: } rubidium@6950: cost.AddCost(ret); truelight@0: } truelight@0: } truelight@0: Darkvater@1793: return (success) ? cost : CMD_ERROR; truelight@0: } truelight@0: truelight@0: truelight@0: #define TILELOOP_BITS 4 truelight@0: #define TILELOOP_SIZE (1 << TILELOOP_BITS) rubidium@6491: #define TILELOOP_ASSERTMASK ((TILELOOP_SIZE - 1) + ((TILELOOP_SIZE - 1) << MapLogX())) tron@927: #define TILELOOP_CHKMASK (((1 << (MapLogX() - TILELOOP_BITS))-1) << TILELOOP_BITS) truelight@0: rubidium@6247: void RunTileLoop() truelight@0: { tron@1589: TileIndex tile; truelight@0: uint count; truelight@0: truelight@0: tile = _cur_tileloop_tile; truelight@0: truelight@0: assert( (tile & ~TILELOOP_ASSERTMASK) == 0); tron@863: count = (MapSizeX() / TILELOOP_SIZE) * (MapSizeY() / TILELOOP_SIZE); truelight@0: do { tron@1214: _tile_type_procs[GetTileType(tile)]->tile_loop_proc(tile); truelight@0: tron@926: if (TileX(tile) < MapSizeX() - TILELOOP_SIZE) { belugas@6201: tile += TILELOOP_SIZE; // no overflow truelight@0: } else { tron@1981: tile = TILE_MASK(tile - TILELOOP_SIZE * (MapSizeX() / TILELOOP_SIZE - 1) + TileDiffXY(0, TILELOOP_SIZE)); /* x would overflow, also increase y */ truelight@0: } truelight@0: } while (--count); truelight@0: assert( (tile & ~TILELOOP_ASSERTMASK) == 0); truelight@0: truelight@0: tile += 9; truelight@0: if (tile & TILELOOP_CHKMASK) tron@863: tile = (tile + MapSizeX()) & TILELOOP_ASSERTMASK; truelight@0: _cur_tileloop_tile = tile; truelight@0: } truelight@0: rubidium@6247: void InitializeLandscape() truelight@0: { tron@3078: uint maxx = MapMaxX(); tron@3078: uint maxy = MapMaxY(); tron@3078: uint sizex = MapSizeX(); tron@3078: uint x; tron@3078: uint y; tron@1218: tron@3078: for (y = 0; y < maxy; y++) { tron@3078: for (x = 0; x < maxx; x++) { tron@3447: MakeClear(sizex * y + x, CLEAR_GRASS, 3); tron@3078: SetTileHeight(sizex * y + x, 0); belugas@5596: SetTropicZone(sizex * y + x, TROPICZONE_INVALID); celestar@5385: ClearBridgeMiddle(sizex * y + x); tron@3078: } tron@3078: MakeVoid(sizex * y + x); tron@2049: } tron@3078: for (x = 0; x < sizex; x++) MakeVoid(sizex * y + x); truelight@0: } truelight@0: rubidium@6247: void ConvertGroundTilesIntoWaterTiles() truelight@0: { tron@4000: TileIndex tile; truelight@4300: uint z; truelight@4300: Slope slope; truelight@0: tron@1275: for (tile = 0; tile < MapSize(); ++tile) { truelight@4300: slope = GetTileSlope(tile, &z); truelight@4300: if (IsTileType(tile, MP_CLEAR) && z == 0) { truelight@4300: /* Make both water for tiles at level 0 truelight@4300: * and make shore, as that looks much better truelight@4300: * during the generation. */ truelight@4300: switch (slope) { truelight@4300: case SLOPE_FLAT: truelight@4300: MakeWater(tile); truelight@4300: break; truelight@4300: truelight@4300: case SLOPE_N: truelight@4300: case SLOPE_E: truelight@4300: case SLOPE_S: truelight@4300: case SLOPE_W: rubidium@7887: MakeShore(tile); rubidium@7887: break; rubidium@7887: truelight@4300: case SLOPE_NW: rubidium@7887: if (GetTileSlope(TileAddByDiagDir(tile, DIAGDIR_SE), NULL) != SLOPE_SE) MakeShore(tile); rubidium@7887: break; rubidium@7887: truelight@4300: case SLOPE_SW: rubidium@7887: if (GetTileSlope(TileAddByDiagDir(tile, DIAGDIR_NE), NULL) != SLOPE_NE) MakeShore(tile); rubidium@7887: break; rubidium@7887: truelight@4300: case SLOPE_SE: rubidium@7887: if (GetTileSlope(TileAddByDiagDir(tile, DIAGDIR_NW), NULL) != SLOPE_NW) MakeShore(tile); rubidium@7887: break; rubidium@7887: truelight@4300: case SLOPE_NE: rubidium@7887: if (GetTileSlope(TileAddByDiagDir(tile, DIAGDIR_SW), NULL) != SLOPE_SW) MakeShore(tile); truelight@4300: break; truelight@4300: truelight@4300: default: truelight@4300: break; truelight@4300: } truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@4344: static const byte _genterrain_tbl_1[5] = { 10, 22, 33, 37, 4 }; rubidium@4344: static const byte _genterrain_tbl_2[5] = { 0, 0, 0, 0, 33 }; truelight@0: truelight@0: static void GenerateTerrain(int type, int flag) truelight@0: { truelight@0: uint32 r; tron@1275: uint x; tron@1275: uint y; tron@1275: uint w; tron@1275: uint h; rubidium@5587: const Sprite* templ; tron@1275: const byte *p; tron@2049: Tile* tile; truelight@0: byte direction; truelight@0: truelight@0: r = Random(); rubidium@5587: templ = GetSprite((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845); truelight@0: tron@856: x = r & MapMaxX(); tron@927: y = (r >> MapLogX()) & MapMaxY(); truelight@0: truelight@0: tron@2951: if (x < 2 || y < 2) return; truelight@0: tron@2140: direction = GB(r, 22, 2); tron@1275: if (direction & 1) { rubidium@5587: w = templ->height; rubidium@5587: h = templ->width; tron@1275: } else { rubidium@5587: w = templ->width; rubidium@5587: h = templ->height; tron@1275: } rubidium@5587: p = templ->data; truelight@0: truelight@0: if (flag & 4) { tron@1273: uint xw = x * MapSizeY(); tron@1273: uint yw = y * MapSizeX(); tron@1273: uint bias = (MapSizeX() + MapSizeY()) * 16; tron@1273: tron@1273: switch (flag & 3) { tron@1273: case 0: tron@1273: if (xw + yw > MapSize() - bias) return; tron@1273: break; tron@1273: tron@1273: case 1: tron@1273: if (yw < xw + bias) return; tron@1273: break; tron@1273: tron@1273: case 2: tron@1273: if (xw + yw < MapSize() + bias) return; tron@1273: break; tron@1273: tron@1273: case 3: tron@1273: if (xw < yw + bias) return; tron@1273: break; truelight@0: } truelight@0: } truelight@0: tron@2951: if (x + w >= MapMaxX() - 1) return; tron@2951: if (y + h >= MapMaxY() - 1) return; truelight@0: tron@2049: tile = &_m[TileXY(x, y)]; truelight@0: tron@1275: switch (direction) { tron@1275: case 0: truelight@0: do { tron@2049: Tile* tile_cur = tile; tron@1275: uint w_cur; tron@1275: tron@1275: for (w_cur = w; w_cur != 0; --w_cur) { tron@2049: if (*p >= tile_cur->type_height) tile_cur->type_height = *p; tron@1275: p++; tron@1275: tile_cur++; tron@1275: } tron@1981: tile += TileDiffXY(0, 1); tron@1275: } while (--h != 0); tron@1275: break; tron@1275: tron@1275: case 1: truelight@0: do { tron@2049: Tile* tile_cur = tile; tron@1275: uint h_cur; tron@1275: tron@1275: for (h_cur = h; h_cur != 0; --h_cur) { tron@2049: if (*p >= tile_cur->type_height) tile_cur->type_height = *p; tron@1275: p++; tron@1981: tile_cur += TileDiffXY(0, 1); tron@1275: } tron@1275: tile++; tron@1275: } while (--w != 0); tron@1275: break; tron@1275: tron@1275: case 2: tron@1981: tile += TileDiffXY(w - 1, 0); truelight@0: do { tron@2049: Tile* tile_cur = tile; tron@1275: uint w_cur; tron@1275: tron@1275: for (w_cur = w; w_cur != 0; --w_cur) { tron@2049: if (*p >= tile_cur->type_height) tile_cur->type_height = *p; tron@1275: p++; tron@1275: tile_cur--; tron@1275: } tron@1981: tile += TileDiffXY(0, 1); tron@1275: } while (--h != 0); tron@1275: break; tron@1275: tron@1275: case 3: tron@1981: tile += TileDiffXY(0, h - 1); tron@1275: do { tron@2049: Tile* tile_cur = tile; tron@1275: uint h_cur; tron@1275: tron@1275: for (h_cur = h; h_cur != 0; --h_cur) { tron@2049: if (*p >= tile_cur->type_height) tile_cur->type_height = *p; tron@1275: p++; tron@1981: tile_cur -= TileDiffXY(0, 1); tron@1278: } tron@1275: tile++; tron@1275: } while (--w != 0); tron@1275: break; truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: #include "table/genland.h" truelight@0: rubidium@6247: static void CreateDesertOrRainForest() truelight@0: { tron@1275: TileIndex tile; truelight@4300: TileIndex update_freq = MapSize() / 4; tron@909: const TileIndexDiffC *data; tron@1275: uint i; truelight@0: tron@909: for (tile = 0; tile != MapSize(); ++tile) { truelight@4300: if ((tile % update_freq) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: tron@909: for (data = _make_desert_or_rainforest_data; tron@909: data != endof(_make_desert_or_rainforest_data); ++data) { tron@1184: TileIndex t = TILE_MASK(tile + ToTileIndexDiff(*data)); tron@1044: if (TileHeight(t) >= 4 || IsTileType(t, MP_WATER)) break; tron@909: } tron@909: if (data == endof(_make_desert_or_rainforest_data)) belugas@3379: SetTropicZone(tile, TROPICZONE_DESERT); tron@909: } truelight@183: truelight@4300: for (i = 0; i != 256; i++) { truelight@4300: if ((i % 64) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: truelight@0: RunTileLoop(); truelight@4300: } truelight@0: tron@909: for (tile = 0; tile != MapSize(); ++tile) { truelight@4300: if ((tile % update_freq) == 0) IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: tron@909: for (data = _make_desert_or_rainforest_data; tron@909: data != endof(_make_desert_or_rainforest_data); ++data) { tron@909: TileIndex t = TILE_MASK(tile + ToTileIndexDiff(*data)); tron@3447: if (IsTileType(t, MP_CLEAR) && IsClearGround(t, CLEAR_DESERT)) break; tron@909: } tron@909: if (data == endof(_make_desert_or_rainforest_data)) belugas@3379: SetTropicZone(tile, TROPICZONE_RAINFOREST); tron@909: } truelight@0: } truelight@0: truelight@4300: void GenerateLandscape(byte mode) truelight@0: { truelight@4300: const int gwp_desert_amount = 4 + 8; tron@1275: uint i; tron@1275: uint flag; truelight@0: uint32 r; truelight@183: truelight@4300: if (mode == GW_HEIGHTMAP) { belugas@6357: SetGeneratingWorldProgress(GWP_LANDSCAPE, (_opt.landscape == LT_TROPIC) ? 1 + gwp_desert_amount : 1); truelight@4300: LoadHeightmap(_file_to_saveload.name); truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: } else if (_patches.land_generator == LG_TERRAGENESIS) { belugas@6357: SetGeneratingWorldProgress(GWP_LANDSCAPE, (_opt.landscape == LT_TROPIC) ? 3 + gwp_desert_amount : 3); truelight@4300: GenerateTerrainPerlin(); truelight@4300: } else { truelight@4300: switch (_opt.landscape) { belugas@6357: case LT_ARCTIC: truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 2); truelight@0: truelight@4300: for (i = ScaleByMapSize((Random() & 0x7F) + 950); i != 0; --i) { truelight@4300: GenerateTerrain(2, 0); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); tron@3017: truelight@4300: r = Random(); truelight@4300: flag = GB(r, 0, 2) | 4; truelight@4300: for (i = ScaleByMapSize(GB(r, 16, 7) + 450); i != 0; --i) { truelight@4300: GenerateTerrain(4, flag); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: break; tron@3017: belugas@6357: case LT_TROPIC: truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 3 + gwp_desert_amount); truelight@4300: truelight@4300: for (i = ScaleByMapSize((Random() & 0x7F) + 170); i != 0; --i) { truelight@4300: GenerateTerrain(0, 0); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: truelight@4300: r = Random(); truelight@4300: flag = GB(r, 0, 2) | 4; truelight@4300: for (i = ScaleByMapSize(GB(r, 16, 8) + 1700); i != 0; --i) { truelight@4300: GenerateTerrain(0, flag); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: truelight@4300: flag ^= 2; truelight@4300: truelight@4300: for (i = ScaleByMapSize((Random() & 0x7F) + 410); i != 0; --i) { truelight@4300: GenerateTerrain(3, flag); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: break; truelight@4300: truelight@4300: default: truelight@4300: SetGeneratingWorldProgress(GWP_LANDSCAPE, 1); truelight@4300: truelight@4300: i = ScaleByMapSize((Random() & 0x7F) + (3 - _opt.diff.quantity_sea_lakes) * 256 + 100); truelight@4300: for (; i != 0; --i) { truelight@4300: GenerateTerrain(_opt.diff.terrain_type, 0); truelight@4300: } truelight@4300: IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); truelight@4300: break; truelight@4300: } truelight@0: } truelight@0: truelight@0: ConvertGroundTilesIntoWaterTiles(); truelight@0: belugas@6357: if (_opt.landscape == LT_TROPIC) CreateDesertOrRainForest(); truelight@0: } truelight@0: rubidium@6247: void OnTick_Town(); rubidium@6247: void OnTick_Trees(); rubidium@6247: void OnTick_Station(); rubidium@6247: void OnTick_Industry(); truelight@0: rubidium@6247: void OnTick_Players(); rubidium@6247: void OnTick_Train(); truelight@0: rubidium@6247: void CallLandscapeTick() truelight@0: { truelight@0: OnTick_Town(); truelight@0: OnTick_Trees(); truelight@0: OnTick_Station(); truelight@0: OnTick_Industry(); truelight@0: truelight@0: OnTick_Players(); truelight@0: OnTick_Train(); truelight@0: } truelight@0: truelight@0: TileIndex AdjustTileCoordRandomly(TileIndex a, byte rng) truelight@0: { truelight@0: int rn = rng; truelight@0: uint32 r = Random(); truelight@0: tron@1981: return TILE_MASK(TileXY( tron@2150: TileX(a) + (GB(r, 0, 8) * rn * 2 >> 8) - rn, tron@2150: TileY(a) + (GB(r, 8, 8) * rn * 2 >> 8) - rn tron@1174: )); truelight@0: } truelight@0: tron@1589: bool IsValidTile(TileIndex tile) truelight@0: { tron@926: return (tile < MapSizeX() * MapMaxY() && TileX(tile) != MapMaxX()); truelight@0: }