(svn r1768) -Codechange: Store town index in _map2 of town tiles
authorcelestar
Wed, 02 Feb 2005 14:17:13 +0000
changeset 1264 280792016b02
parent 1263 f1812befa312
child 1265 0430e635b116
(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
docs/landscape.html
road_cmd.c
saveload.c
town_cmd.c
ttd.c
--- a/docs/landscape.html	Tue Feb 01 22:17:38 2005 +0000
+++ b/docs/landscape.html	Wed Feb 02 14:17:13 2005 +0000
@@ -151,7 +151,9 @@
 <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 @@
 <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 @@
 <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>
--- a/road_cmd.c	Tue Feb 01 22:17:38 2005 +0000
+++ b/road_cmd.c	Wed Feb 02 14:17:13 2005 +0000
@@ -333,6 +333,7 @@
 
 /* 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 @@
 		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 @@
 		if (ti.type != MP_STREET) {
 			SetTileType(tile, MP_STREET);
 			_map5[tile] = 0;
+			_map2[tile] = p2;
 			_map_owner[tile] = _current_player;
 		}
 
@@ -751,7 +754,7 @@
 			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 @@
 		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 @@
 	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 @@
 					!(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 @@
 
 		{
 			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 @@
 			} else {
 				b = 0;
 			}
-			_map2[tile] = (_map2[tile] & ~7) | b;
+			_map3_hi[tile] = (_map3_hi[tile] & ~0x70) | (b << 4);
 			MarkTileDirtyByTile(tile);
 		}
 	} else {
 		// Handle road work
+		//XXX undocumented
 
-		uint16 b = _map2[tile];
-		if (b < 0x80) {
-			_map2[tile] = b + 8;
+		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 @@
 		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 @@
 {
 	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];
 }
--- a/saveload.c	Tue Feb 01 22:17:38 2005 +0000
+++ b/saveload.c	Wed Feb 02 14:17:13 2005 +0000
@@ -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
 };
--- a/town_cmd.c	Tue Feb 01 22:17:38 2005 +0000
+++ b/town_cmd.c	Wed Feb 02 14:17:13 2005 +0000
@@ -85,7 +85,7 @@
 	/* 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 @@
 	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 @@
 
 	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 @@
 		return;
 	}
 
-	house = _map2[tile];
+	house = _map3_hi[tile];
 	if (_housetype_extra_flags[house] & 0x20 &&
 			!(_map5[tile] & 0x80) &&
 			CHANCE16(1,2) &&
@@ -340,7 +340,7 @@
 	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 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 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 @@
 			(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 @@
 		// 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 @@
 		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 @@
 		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 @@
 		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 @@
 		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 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 @@
 	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);
--- a/ttd.c	Tue Feb 01 22:17:38 2005 +0000
+++ b/ttd.c	Wed Feb 02 14:17:13 2005 +0000
@@ -1407,6 +1407,27 @@
 		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;
 }