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" maedhros@6343: #include "date.h" tron@2163: #include "functions.h" tron@679: #include "map.h" tron@2154: #include "player.h" tron@1349: #include "spritecache.h" tron@1363: #include "table/sprites.h" tron@1209: #include "tile.h" truelight@0: #include truelight@0: #include "viewport.h" truelight@0: #include "command.h" maedhros@6343: #include "landscape.h" truelight@0: #include "vehicle.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" truelight@0: 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@5587: const Slope _inclined_tileh[] = { rubidium@4344: SLOPE_SW, SLOPE_NW, SLOPE_SW, SLOPE_SE, SLOPE_NE, SLOPE_SE, SLOPE_NE, SLOPE_NW, rubidium@4344: SLOPE_E, SLOPE_N, SLOPE_W, SLOPE_S, tron@4253: SLOPE_NWS, SLOPE_WSE, SLOPE_SEN, SLOPE_ENW tron@1958: }; tron@1958: maedhros@6343: SnowLine *_snow_line = NULL; truelight@0: tron@3636: uint GetPartialZ(int x, int y, Slope corners) truelight@0: { truelight@0: int z = 0; truelight@0: tron@2952: switch (corners) { 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: rubidium@6491: z = 1 + (((x ^ 0xF) + (y ^ 0xF)) >> 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: tron@4061: tron@4061: static Slope GetFoundationSlope(TileIndex tile, uint* z) dominik@37: { tron@4061: Slope tileh = GetTileSlope(tile, z); tron@3636: Slope slope = _tile_type_procs[GetTileType(tile)]->get_slope_tileh_proc(tile, tileh); dominik@50: belugas@6201: /* Flatter slope -> higher base height */ tron@4061: if (slope < tileh) *z += TILE_HEIGHT; tron@4061: return slope; 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: Slope slope = GetFoundationSlope(TILE_ADDXY(tile, 0, -1), &z); tron@4061: tron@4061: return tron@4990: ( tron@4990: z_here + (slope_here & SLOPE_N ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_N ? TILE_HEIGHT : 0) > tron@4990: z + (slope & SLOPE_E ? TILE_HEIGHT : 0) + (slope == SLOPE_STEEP_E ? TILE_HEIGHT : 0) tron@4990: ) || ( tron@4990: z_here + (slope_here & SLOPE_W ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_W ? TILE_HEIGHT : 0) > tron@4990: z + (slope & SLOPE_S ? TILE_HEIGHT : 0) + (slope == SLOPE_STEEP_S ? TILE_HEIGHT : 0) tron@4990: ); 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: Slope slope = GetFoundationSlope(TILE_ADDXY(tile, -1, 0), &z); tron@4061: tron@4061: return tron@4990: ( tron@4990: z_here + (slope_here & SLOPE_N ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_N ? TILE_HEIGHT : 0) > tron@4990: z + (slope & SLOPE_W ? TILE_HEIGHT : 0) + (slope == SLOPE_STEEP_W ? TILE_HEIGHT : 0) tron@4990: ) || ( tron@4990: z_here + (slope_here & SLOPE_E ? TILE_HEIGHT : 0) + (slope_here == SLOPE_STEEP_E ? TILE_HEIGHT : 0) > tron@4990: z + (slope & SLOPE_S ? TILE_HEIGHT : 0) + (slope == SLOPE_STEEP_S ? TILE_HEIGHT : 0) tron@4990: ); tron@4061: } tron@4061: tron@4061: truelight@0: void DrawFoundation(TileInfo *ti, uint f) truelight@0: { peter1138@5668: SpriteID sprite_base = SPR_SLOPES_BASE - 15; tron@4061: Slope slope; tron@4061: uint z; dominik@39: tron@4061: slope = GetFoundationSlope(ti->tile, &z); tron@4061: if (!HasFoundationNW(ti->tile, slope, z)) sprite_base += 22; tron@4061: if (!HasFoundationNE(ti->tile, slope, z)) sprite_base += 44; dominik@37: tron@4253: if (IsSteepSlope(ti->tileh)) { peter1138@5668: SpriteID lower_base; dominik@37: belugas@6201: /* Lower part of foundation */ tron@4253: lower_base = sprite_base; tron@4253: if (lower_base == SPR_SLOPES_BASE - 15) lower_base = SPR_FOUNDATION_BASE; tron@4253: AddSortableSpriteToDraw( peter1138@5668: lower_base + (ti->tileh & ~SLOPE_STEEP), PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z tron@4253: ); tron@3645: ti->z += TILE_HEIGHT; tron@4253: ti->tileh = _inclined_tileh[f - 15]; tron@4253: if (f < 15 + 8) { belugas@6201: /* inclined */ peter1138@5668: AddSortableSpriteToDraw(sprite_base + f, PAL_NONE, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: OffsetGroundSprite(31, 9); tron@4253: } else if (f >= 15 + 8 + 4) { belugas@6201: /* three corners raised */ peter1138@5668: SpriteID upper = sprite_base + 15 + (f - 15 - 8 - 4) * 2; tron@4253: peter1138@5668: AddSortableSpriteToDraw(upper, PAL_NONE, ti->x, ti->y, 16, 16, 1, ti->z); peter1138@5668: AddChildSpriteScreen(upper + 1, PAL_NONE, 31, 9); tron@4253: OffsetGroundSprite(31, 9); tron@4253: } else { belugas@6201: /* one corner raised */ tron@4253: OffsetGroundSprite(31, 1); tron@4253: } truelight@0: } else { tron@4253: if (f < 15) { belugas@6201: /* leveled foundation belugas@6201: * Use the original slope sprites if NW and NE borders should be visible */ tron@4253: if (sprite_base == SPR_SLOPES_BASE - 15) sprite_base = SPR_FOUNDATION_BASE; tron@4246: peter1138@5668: AddSortableSpriteToDraw(sprite_base + f, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z); tron@4246: ti->z += TILE_HEIGHT; tron@4253: ti->tileh = SLOPE_FLAT; tron@4253: OffsetGroundSprite(31, 1); tron@4253: } else { belugas@6201: /* inclined foundation */ peter1138@5668: AddSortableSpriteToDraw(sprite_base + f, PAL_NONE, ti->x, ti->y, 16, 16, 1, ti->z); tron@4253: ti->tileh = _inclined_tileh[f - 15]; tron@4253: OffsetGroundSprite(31, 9); tron@4246: } 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: { truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); 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@6943: CommandCost cost, ret, money; 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: darkvater@889: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); darkvater@889: 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: truelight@4300: case SLOPE_NW: truelight@4300: case SLOPE_SW: truelight@4300: case SLOPE_SE: truelight@4300: case SLOPE_NE: truelight@4300: 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: }