(svn r4190) -Codechange: Add and make use of an accessor function to create houses
authorcelestar
Fri, 31 Mar 2006 08:18:14 +0000
changeset 3382 be14efb0dfd7
parent 3381 91ee7484deff
child 3383 a4828297b799
(svn r4190) -Codechange: Add and make use of an accessor function to create houses
town_cmd.c
town_map.h
--- a/town_cmd.c	Fri Mar 31 06:16:04 2006 +0000
+++ b/town_cmd.c	Fri Mar 31 08:18:14 2006 +0000
@@ -1281,73 +1281,22 @@
 	t->flags12 |= oneof;
 
 	{
-		int m3lo,m5,eflags;
+		byte construction_counter = 0, construction_stage = 0, size_flags;
 
-		// ENDING_2
-		m3lo = 0;
-		m5 = 0;
 		if (_generating_world) {
 			uint32 r = Random();
 
-			// Value for map3lo
-			m3lo = 0xC0;
-			if (GB(r, 0, 8) >= 220) m3lo &= (r>>8);
-
-			if (m3lo == 0xC0)
-				ChangePopulation(t, _housetype_population[house]);
-
-			// Initial value for map5.
-			m5 = GB(r, 16, 6);
-		}
-
-		assert(IsTileType(tile, MP_CLEAR));
-
-		ModifyTile(tile,
-			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 */
-		);
-
-		eflags = _housetype_extra_flags[house];
+			construction_stage = 3; /* House is finished */
+			if (CHANCE16(1, 7)) construction_stage = GB(r, 0, 2);
 
-		if (eflags&0x18) {
-			assert(IsTileType(tile + TileDiffXY(0, 1), MP_CLEAR));
-			ModifyTile(tile + TileDiffXY(0, 1),
-				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 */
-			);
+			if (construction_stage == 3) {
+				ChangePopulation(t, _housetype_population[house]);
+			} else {
+				construction_counter = GB(r, 2, 2);
+			}
 		}
-
-		if (eflags&0x14) {
-			assert(IsTileType(tile + TileDiffXY(1, 0), MP_CLEAR));
-			ModifyTile(tile + TileDiffXY(1, 0),
-				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 */
-			);
-		}
-
-		if (eflags&0x10) {
-			assert(IsTileType(tile + TileDiffXY(1, 1), MP_CLEAR));
-			ModifyTile(tile + TileDiffXY(1, 1),
-				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 */
-			);
-		}
+		size_flags = GB(_housetype_extra_flags[house], 2, 3);
+		MakeTownHouse(tile, t->index, construction_counter, construction_stage, size_flags, house);
 	}
 
 	// ENDING
--- a/town_map.h	Fri Mar 31 06:16:04 2006 +0000
+++ b/town_map.h	Fri Mar 31 08:18:14 2006 +0000
@@ -18,3 +18,31 @@
 {
 	return GetTown(GetTownIndex(t));
 }
+
+static inline void MakeHouseTile(TileIndex t, TownID tid, byte counter, byte stage, byte type)
+{
+	assert(IsTileType(t, MP_CLEAR));
+
+	SetTileType(t, MP_HOUSE);
+	_m[t].m1 = 0;
+	_m[t].m2 = tid;
+	SB(_m[t].m3, 6, 2, stage);
+	_m[t].m4 = type;
+	SB(_m[t].m5, 0, 2, counter);
+
+	MarkTileDirtyByTile(t);
+}
+
+enum {
+	TWO_BY_TWO_BIT = 2, ///< House is two tiles in X and Y directions
+	ONE_BY_TWO_BIT = 1, ///< House is two tiles in Y direction
+	TWO_BY_ONE_BIT = 0, ///< House is two tiles in X direction
+};
+
+static inline void MakeTownHouse(TileIndex t, TownID tid, byte counter, byte stage, byte size, byte type)
+{
+	MakeHouseTile(t, tid, counter, stage, type);
+	if (HASBIT(size, TWO_BY_TWO_BIT) || HASBIT(size, ONE_BY_TWO_BIT)) MakeHouseTile(t + TileDiffXY(0, 1), tid, counter, stage, ++type);
+	if (HASBIT(size, TWO_BY_TWO_BIT) || HASBIT(size, TWO_BY_ONE_BIT)) MakeHouseTile(t + TileDiffXY(1, 0), tid, counter, stage, ++type);
+	if (HASBIT(size, TWO_BY_TWO_BIT)) MakeHouseTile(t + TileDiffXY(1, 1), tid, counter, stage, ++type);
+}