summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcelestar <celestar@openttd.org>2005-02-02 14:17:13 +0000
committercelestar <celestar@openttd.org>2005-02-02 14:17:13 +0000
commit30a328705e4428234fff27485c45cef47be95b03 (patch)
tree712a2b32c2765cf02d1eda5afe810c4c73151cb1
parent03095f85bc2d8100e1c4b496dd563f2456c782e6 (diff)
downloadopenttd-30a328705e4428234fff27485c45cef47be95b03.tar.xz
(svn r1768) -Codechange: Store town index in _map2 of town tiles
Moved house type from _map2 to _map3_hi for MP_HOUSE Moved foundation and roadworks from _map2 to _map3 for MP_STREET This increases game speed by a factor of around 15(!) if many cities are around. Converting an old game is done automagically, but can take a while
-rw-r--r--docs/landscape.html10
-rw-r--r--road_cmd.c36
-rw-r--r--saveload.c2
-rw-r--r--town_cmd.c47
-rw-r--r--ttd.c21
5 files changed, 78 insertions, 38 deletions
diff --git a/docs/landscape.html b/docs/landscape.html
index fe2ba5c23..0690b3405 100644
--- a/docs/landscape.html
+++ b/docs/landscape.html
@@ -151,7 +151,9 @@ map5 bits 7..4 clear: road
<tr><td nowrap valign=top>bit 3: </td><td align=left>NE piece</td></tr>
</table></li>
<li>map_owner: <a href="#OwnershipInfo">owner</a> of the road</li>
-<li>map2 bits 0..2: <tt>0</tt> - on bare land, <tt>1</tt> - on grass, <tt>2</tt> - paved, <tt>3</tt> - with streetlights, <tt>5</tt> - tree-lined, <tt>6</tt> - on grass with road works, <tt>7</tt> - paved with road works</li>
+<li>map2: Index into the array of towns, 0 for non-town roads</li>
+<li>map3_hi bits 0..3: counter for the roadworks</li>
+<li>map3_hi bits 4..6: <tt>0</tt> - on bare land, <tt>1</tt> - on grass, <tt>2</tt> - paved, <tt>3</tt> - with streetlights, <tt>5</tt> - tree-lined, <tt>6</tt> - on grass with road works, <tt>7</tt> - paved with road works</li>
<li>map3_hi bit 7 set = on snow or desert</li>
</ul>
map5 bit 4 set, bits 7..5 clear: level crossing
@@ -159,9 +161,10 @@ map5 bit 4 set, bits 7..5 clear: level crossing
<li>map5 bit 3: clear - road in the X direction, set - road in the Y direction (railway track always perpendicular)</li>
<li>map5 bit 2: set if crossing lights are on</li>
<li>map_owner: <a href="#OwnershipInfo">owner</a> of the railway track</li>
-<li>map2 bits 0..2: <tt>0</tt> - on bare land, <tt>1</tt> - on grass, <tt>2</tt> or higher - paved</li>
+<li>map2: Index into the array of towns, 0 for non-town roads</li>
<li>map3_lo bits 0..7: <a href="#OwnershipInfo">owner</a> of the road</li>
<li>map3_hi bits 3..0: <a href="#TrackType">track type</a></li>
+<li>map3_hi bits 4..6: <tt>0</tt> - on bare land, <tt>1</tt> - on grass, <tt>2</tt> or higher - paved</li>
<li>map3_hi bit 7 set = on snow or desert</li>
</ul>
map5 bit 5 set: road depot
@@ -175,7 +178,8 @@ map5 bit 5 set: road depot
<tr><td valign=top nowrap><a name="Class3"><tt> 3 </tt></a></td><td>
Town building
<ul>
-<li>map2: <a name="HouseTypes">town building type</a>:
+<li>map2: Index into the array of towns</li>
+<li>map3_hi: <a name="HouseTypes">town building type</a>:
<p><small>Note: In the climate list, 'sub-arctic' means below the <a href="#_snowline">snow line</a>, and 'snow' means above the snow line in the sub-arctic climate.</small></p>
<table>
<tr><th align=left>Type&nbsp;</th><th align=left>Size&nbsp;</th><th align=left>Climates&nbsp;</th><th align=left>Description</th></tr>
diff --git a/road_cmd.c b/road_cmd.c
index c74c7c931..b195bc63d 100644
--- a/road_cmd.c
+++ b/road_cmd.c
@@ -333,6 +333,7 @@ static uint32 CheckRoadSlope(int tileh, byte *pieces, byte existing)
/* Build a piece of road
* p1 = piece flags
+ * p2 = town which is building the road
*/
int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
@@ -382,7 +383,8 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if (flags & DC_EXEC) {
ModifyTile(tile,
MP_SETTYPE(MP_STREET) |
- MP_MAP2_CLEAR | MP_MAP3LO | MP_MAP3HI | MP_MAP5,
+ MP_MAP2 | MP_MAP3LO | MP_MAP3HI | MP_MAP5,
+ p2,
_current_player, /* map3_lo */
_map3_lo[tile] & 0xF, /* map3_hi */
m5 /* map5 */
@@ -451,6 +453,7 @@ do_clear:;
if (ti.type != MP_STREET) {
SetTileType(tile, MP_STREET);
_map5[tile] = 0;
+ _map2[tile] = p2;
_map_owner[tile] = _current_player;
}
@@ -751,7 +754,7 @@ static void DrawTile_Road(TileInfo *ti)
image = _road_tile_sprites_1[ti->map5 & 0xF];
}
- m2 = _map2[ti->tile] & 7;
+ m2 = (_map3_hi[ti->tile] & 0x70) >> 4;
if (m2 == 0) image |= 0x3178000;
@@ -797,7 +800,7 @@ static void DrawTile_Road(TileInfo *ti)
if ( _map3_hi[ti->tile] & 0x80) {
image += 8;
} else {
- m2 = _map2[ti->tile] & 7;
+ m2 = (_map3_hi[ti->tile] & 0x70) >> 4;
if (m2 == 0) image |= 0x3178000;
if (m2 > 1) image += 4;
}
@@ -960,7 +963,7 @@ static void TileLoop_Road(uint tile)
if (_map5[tile] & 0xE0)
return;
- if ((_map2[tile] & 7) < 6) {
+ if (((_map3_hi[tile] & 0x70) >> 4) < 6) {
t = ClosestTownFromTile(tile, (uint)-1);
grp = 0;
if (t != NULL) {
@@ -976,7 +979,7 @@ static void TileLoop_Road(uint tile)
!(DistanceManhattan(t->xy, tile) >= 8 && grp == 0) &&
(_map5[tile]==5 || _map5[tile]==10)) {
if (GetTileSlope(tile, NULL) == 0 && EnsureNoVehicle(tile) && CHANCE16(1,20)) {
- _map2[tile] = ((_map2[tile]&7) <= 1) ? 6 : 7;
+ _map3_hi[tile] |= ((((_map3_hi[tile] & 0x70) >> 4 ) <= 2) ? 7 : 6) << 4;
SndPlayTileFx(SND_21_JACKHAMMER, tile);
CreateEffectVehicleAbove(
@@ -992,7 +995,7 @@ static void TileLoop_Road(uint tile)
{
const byte *p = (_opt.landscape == LT_CANDY) ? _town_road_types_2[grp] : _town_road_types[grp];
- byte b = _map2[tile] & 7;
+ byte b = (_map3_hi[tile] & 0x70) >> 4;
if (b == p[0])
return;
@@ -1004,18 +1007,23 @@ static void TileLoop_Road(uint tile)
} else {
b = 0;
}
- _map2[tile] = (_map2[tile] & ~7) | b;
+ _map3_hi[tile] = (_map3_hi[tile] & ~0x70) | (b << 4);
MarkTileDirtyByTile(tile);
}
} else {
// Handle road work
-
- uint16 b = _map2[tile];
- if (b < 0x80) {
- _map2[tile] = b + 8;
+ //XXX undocumented
+
+ byte b = _map3_hi[tile];
+ //roadworks take place only
+ //keep roadworks running for 16 loops
+ //lower 4 bits of map3_hi store the counter now
+ if ((b & 0xF) != 0xF) {
+ _map3_hi[tile] = b + 1;
return;
}
- _map2[tile] = ((b&7) == 6) ? 1 : 2;
+ //roadworks finished
+ _map3_hi[tile] = ((((b& 0x70) >> 4)== 6) ? 1 : 2) << 4;
MarkTileDirtyByTile(tile);
}
}
@@ -1042,7 +1050,7 @@ static uint32 GetTileTrackStatus_Road(uint tile, TransportType mode) {
byte b = _map5[tile];
if ((b & 0xF0) == 0) {
/* Ordinary road */
- if (!_road_special_gettrackstatus && (_map2[tile]&7) >= 6)
+ if (!_road_special_gettrackstatus && ((_map3_hi[tile]&0x70) >> 4) >= 6)
return 0;
return _road_trackbits[b&0xF] * 0x101;
} else if ((b&0xF0) == 0x10) {
@@ -1077,7 +1085,7 @@ static void GetTileDesc_Road(uint tile, TileDesc *td)
{
int i = (_map5[tile] >> 4);
if (i == 0)
- i = (_map2[tile] & 7) + 3;
+ i = ((_map3_hi[tile] & 0x70) >> 4) + 3;
td->str = _road_tile_strings[i - 1];
td->owner = _map_owner[tile];
}
diff --git a/saveload.c b/saveload.c
index 6378ea0ff..7ce00b1ee 100644
--- a/saveload.c
+++ b/saveload.c
@@ -8,7 +8,7 @@
enum {
SAVEGAME_MAJOR_VERSION = 6,
- SAVEGAME_MINOR_VERSION = 0,
+ SAVEGAME_MINOR_VERSION = 1,
SAVEGAME_LOADABLE_VERSION = (SAVEGAME_MAJOR_VERSION << 8) + SAVEGAME_MINOR_VERSION
};
diff --git a/town_cmd.c b/town_cmd.c
index af6f66396..da505332b 100644
--- a/town_cmd.c
+++ b/town_cmd.c
@@ -85,7 +85,7 @@ static void DrawTile_Town(TileInfo *ti)
/* Retrieve pointer to the draw town tile struct */
{
/* this "randomizes" on the (up to) 4 variants of a building */
- byte gfx = (byte)_map2[ti->tile];
+ byte gfx = (byte)_map3_hi[ti->tile];
byte stage = _map3_lo[ti->tile] >> 6;
uint variant;
variant = ti->x >> 4;
@@ -153,7 +153,7 @@ static void AnimateTile_Town(uint tile)
if (_tick_counter & 3)
return;
- if (_map2[tile] != 4 && _map2[tile] != 5)
+ if (_map3_hi[tile] != 4 && _map3_hi[tile] != 5)
return;
if (!((old=_map_owner[tile])&0x80)) {
@@ -255,14 +255,14 @@ static void MakeSingleHouseBigger(uint tile)
if ( (_map3_lo[tile] & 0xC0) == 0xC0) {
Town *t = ClosestTownFromTile(tile, (uint)-1);
- ChangePopulation(t, _housetype_population[_map2[tile]]);
+ ChangePopulation(t, _housetype_population[_map3_hi[tile]]);
}
MarkTileDirtyByTile(tile);
}
static void MakeTownHouseBigger(uint tile)
{
- uint flags = _house_more_flags[_map2[tile]];
+ uint flags = _house_more_flags[_map3_hi[tile]];
if (flags & 8) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 0));
if (flags & 4) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 1));
if (flags & 2) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 0));
@@ -280,7 +280,7 @@ static void TileLoop_Town(uint tile)
return;
}
- house = _map2[tile];
+ house = _map3_hi[tile];
if (_housetype_extra_flags[house] & 0x20 &&
!(_map5[tile] & 0x80) &&
CHANCE16(1,2) &&
@@ -340,7 +340,7 @@ static int32 ClearTile_Town(uint tile, byte flags)
if (!EnsureNoVehicle(tile)) return CMD_ERROR;
if (flags&DC_AUTO && !(flags&DC_AI_BUILDING)) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
- house = _map2[tile];
+ house = _map3_hi[tile];
cost = _price.remove_house * _housetype_remove_cost[house] >> 8;
rating = _housetype_remove_ratingmod[house];
@@ -364,7 +364,7 @@ static int32 ClearTile_Town(uint tile, byte flags)
static void GetAcceptedCargo_Town(uint tile, AcceptedCargo ac)
{
- int type = _map2[tile];
+ int type = _map3_hi[tile];
ac[CT_PASSENGERS] = _housetype_cargo_passengers[type];
ac[CT_MAIL] = _housetype_cargo_mail[type];
@@ -374,7 +374,7 @@ static void GetAcceptedCargo_Town(uint tile, AcceptedCargo ac)
static void GetTileDesc_Town(uint tile, TileDesc *td)
{
- td->str = _town_tile_names[_map2[tile]];
+ td->str = _town_tile_names[_map3_hi[tile]];
if ((_map3_lo[tile] & 0xC0) != 0xC0) {
SetDParamX(td->dparam, 0, td->str);
td->str = STR_2058_UNDER_CONSTRUCTION;
@@ -676,7 +676,7 @@ static void GrowTownInTile(uint *tile_ptr, uint mask, int block, Town *t1)
(i++,ti.tileh != 12) &&
(i++,ti.tileh != 6)) {
build_road_and_exit:
- if (DoCommandByTile(tile, rcmd, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD) != CMD_ERROR)
+ if (DoCommandByTile(tile, rcmd, t1->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD) != CMD_ERROR)
_grow_town_result = -1;
return;
}
@@ -809,7 +809,7 @@ bool GrowTown(Town *t)
// Only work with plain land that not already has a house with map5=0
if (ti.tileh == 0 && !(ti.type==MP_HOUSE && ti.map5==0)) {
if (DoCommandByTile(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR) != CMD_ERROR) {
- DoCommandByTile(tile, GenRandomRoadBits(), 0, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD);
+ DoCommandByTile(tile, GenRandomRoadBits(), t->index, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD);
_current_player = old_player;
return true;
}
@@ -1272,9 +1272,10 @@ static void DoBuildTownHouse(Town *t, uint tile)
assert(IsTileType(tile, MP_CLEAR));
ModifyTile(tile,
- MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER,
- house, /* map2 */
- m3lo, /* map3_lo */
+ MP_SETTYPE(MP_HOUSE) | MP_MAP3HI | MP_MAP3LO | MP_MAP2 | MP_MAP5 | MP_MAPOWNER,
+ t->index,
+ m3lo, /* map3_lo */
+ house, /* map3_hi */
0, /* map_owner */
m5 /* map5 */
);
@@ -1284,9 +1285,10 @@ static void DoBuildTownHouse(Town *t, uint tile)
if (eflags&0x18) {
assert(IsTileType(tile + TILE_XY(0,1), MP_CLEAR));
ModifyTile(tile + TILE_XY(0,1),
- MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER,
- ++house, /* map2 */
+ MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI | MP_MAP5 | MP_MAPOWNER,
+ t->index,
m3lo, /* map3_lo */
+ ++house, /* map3_hi */
0, /* map_owner */
m5 /* map5 */
);
@@ -1295,9 +1297,10 @@ static void DoBuildTownHouse(Town *t, uint tile)
if (eflags&0x14) {
assert(IsTileType(tile + TILE_XY(1,0), MP_CLEAR));
ModifyTile(tile + TILE_XY(1,0),
- MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER,
- ++house, /* map2 */
+ MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI | MP_MAP5 | MP_MAPOWNER,
+ t->index,
m3lo, /* map3_lo */
+ ++house, /* map3_hi */
0, /* map_owner */
m5 /* map5 */
);
@@ -1306,9 +1309,10 @@ static void DoBuildTownHouse(Town *t, uint tile)
if (eflags&0x10) {
assert(IsTileType(tile + TILE_XY(1,1), MP_CLEAR));
ModifyTile(tile + TILE_XY(1,1),
- MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER,
- ++house, /* map2 */
+ MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI | MP_MAP5 | MP_MAPOWNER,
+ t->index,
m3lo, /* map3_lo */
+ ++house, /* map3_hi */
0, /* map_owner */
m5 /* map5 */
);
@@ -1342,7 +1346,7 @@ static void DoClearTownHouseHelper(uint tile)
}
static void ClearTownHouse(Town *t, uint tile) {
- uint house = _map2[tile];
+ uint house = _map3_hi[tile];
uint eflags;
assert(IsTileType(tile, MP_HOUSE));
@@ -1777,6 +1781,9 @@ Town *ClosestTownFromTile(uint tile, uint threshold)
uint dist, best = threshold;
Town *best_town = NULL;
+ if ((IsTileType(tile, MP_STREET) && _map_owner[tile] == OWNER_TOWN) || IsTileType(tile, MP_HOUSE))
+ return GetTown(_map2[tile]);
+
FOR_ALL_TOWNS(t) {
if (t->xy != 0) {
dist = DistanceManhattan(tile, t->xy);
diff --git a/ttd.c b/ttd.c
index d78164faa..d95db53be 100644
--- a/ttd.c
+++ b/ttd.c
@@ -1407,6 +1407,27 @@ bool AfterLoadGame(uint version)
UpdateOilRig();
}
+ if (version <= 0x600) {
+ BEGIN_TILE_LOOP(tile, MapSizeX(), MapSizeY(), 0) {
+ if (IsTileType(tile, MP_HOUSE)) {
+ _map3_hi[tile] = _map2[tile];
+ //XXX magic
+ SetTileType(tile, MP_VOID);
+ _map2[tile] = ClosestTownFromTile(tile,(uint)-1)->index;
+ SetTileType(tile, MP_HOUSE);
+ } else if (IsTileType(tile, MP_STREET)) {
+ //XXX magic
+ SetTileType(tile, MP_VOID);
+ _map3_hi[tile] |= (_map2[tile] << 4);
+ if ( _map_owner[tile] == OWNER_TOWN)
+ _map2[tile] = ClosestTownFromTile(tile,(uint)-1)->index;
+ else
+ _map2[tile] = 0;
+ SetTileType(tile, MP_STREET);
+ }
+ } END_TILE_LOOP(tile, MapSizeX(), MapSizeY(), 0);
+ }
+
return true;
}