town_cmd.c
changeset 4346 3f00094f2670
parent 4344 5d0e40cd67b9
child 4352 8ddb01bc6075
equal deleted inserted replaced
4345:dd12549ad473 4346:3f00094f2670
    41  */
    41  */
    42 static void TownPoolNewBlock(uint start_item)
    42 static void TownPoolNewBlock(uint start_item)
    43 {
    43 {
    44 	Town *t;
    44 	Town *t;
    45 
    45 
    46 	FOR_ALL_TOWNS_FROM(t, start_item)
    46 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    47 		t->index = start_item++;
    47 	 * TODO - This is just a temporary stage, this will be removed. */
       
    48 	for (t = GetTown(start_item); t != NULL; t = (t->index + 1 < GetTownPoolSize()) ? GetTown(t->index + 1) : NULL) t->index = start_item++;
    48 }
    49 }
    49 
    50 
    50 /* Initialize the town-pool */
    51 /* Initialize the town-pool */
    51 MemoryPool _town_pool = { "Towns", TOWN_POOL_MAX_BLOCKS, TOWN_POOL_BLOCK_SIZE_BITS, sizeof(Town), &TownPoolNewBlock, NULL, 0, 0, NULL };
    52 MemoryPool _town_pool = { "Towns", TOWN_POOL_MAX_BLOCKS, TOWN_POOL_BLOCK_SIZE_BITS, sizeof(Town), &TownPoolNewBlock, NULL, 0, 0, NULL };
    52 
    53 
   166 static bool IsCloseToTown(TileIndex tile, uint dist)
   167 static bool IsCloseToTown(TileIndex tile, uint dist)
   167 {
   168 {
   168 	const Town* t;
   169 	const Town* t;
   169 
   170 
   170 	FOR_ALL_TOWNS(t) {
   171 	FOR_ALL_TOWNS(t) {
   171 		if (t->xy != 0 && DistanceManhattan(tile, t->xy) < dist) return true;
   172 		if (DistanceManhattan(tile, t->xy) < dist) return true;
   172 	}
   173 	}
   173 	return false;
   174 	return false;
   174 }
   175 }
   175 
   176 
   176 static void MarkTownSignDirty(Town *t)
   177 static void MarkTownSignDirty(Town *t)
   413 		if (++_cur_town_ctr >= GetTownPoolSize())
   414 		if (++_cur_town_ctr >= GetTownPoolSize())
   414 			_cur_town_ctr = 0;
   415 			_cur_town_ctr = 0;
   415 
   416 
   416 		t = GetTown(i);
   417 		t = GetTown(i);
   417 
   418 
   418 		if (t->xy != 0) TownTickHandler(t);
   419 		if (IsValidTown(t)) TownTickHandler(t);
   419 	}
   420 	}
   420 }
   421 }
   421 
   422 
   422 static RoadBits GetTownRoadMask(TileIndex tile)
   423 static RoadBits GetTownRoadMask(TileIndex tile)
   423 {
   424 {
   855 
   856 
   856 		// Check size and width
   857 		// Check size and width
   857 		if (strlen(buf1) >= 31 || GetStringWidth(buf1) > 130) continue;
   858 		if (strlen(buf1) >= 31 || GetStringWidth(buf1) > 130) continue;
   858 
   859 
   859 		FOR_ALL_TOWNS(t2) {
   860 		FOR_ALL_TOWNS(t2) {
   860 			if (t2->xy != 0) {
   861 			// We can't just compare the numbers since
   861 				// We can't just compare the numbers since
   862 			// several numbers may map to a single name.
   862 				// several numbers may map to a single name.
   863 			SetDParam(0, t2->index);
   863 				SetDParam(0, t2->index);
   864 			GetString(buf2, STR_TOWN);
   864 				GetString(buf2, STR_TOWN);
   865 			if (strcmp(buf1, buf2) == 0) {
   865 				if (strcmp(buf1, buf2) == 0) {
   866 				if (tries-- < 0) return false;
   866 					if (tries-- < 0) return false;
   867 				goto restart;
   867 					goto restart;
       
   868 				}
       
   869 			}
   868 			}
   870 		}
   869 		}
   871 		*townnameparts = r;
   870 		*townnameparts = r;
   872 		return true;
   871 		return true;
   873 	}
   872 	}
   947 }
   946 }
   948 
   947 
   949 static Town *AllocateTown(void)
   948 static Town *AllocateTown(void)
   950 {
   949 {
   951 	Town *t;
   950 	Town *t;
   952 	FOR_ALL_TOWNS(t) {
   951 
   953 		if (t->xy == 0) {
   952 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
   953 	 * TODO - This is just a temporary stage, this will be removed. */
       
   954 	for (t = GetTown(0); t != NULL; t = (t->index + 1 < GetTownPoolSize()) ? GetTown(t->index + 1) : NULL) {
       
   955 		if (!IsValidTown(t)) {
   954 			TownID index = t->index;
   956 			TownID index = t->index;
   955 
   957 
   956 			if (t->index > _total_towns)
   958 			if (t->index > _total_towns)
   957 				_total_towns = t->index;
   959 				_total_towns = t->index;
   958 
   960 
  1064 
  1066 
  1065 	// give it a last try, but now more aggressive
  1067 	// give it a last try, but now more aggressive
  1066 	if (num == 0 && CreateRandomTown(10000, 0) == NULL) {
  1068 	if (num == 0 && CreateRandomTown(10000, 0) == NULL) {
  1067 		const Town* t;
  1069 		const Town* t;
  1068 
  1070 
  1069 		FOR_ALL_TOWNS(t) if (IsValidTown(t)) return true;
  1071 		FOR_ALL_TOWNS(t) return true;
  1070 
  1072 
  1071 		//XXX can we handle that more gracefully?
  1073 		//XXX can we handle that more gracefully?
  1072 		if (num == 0 && _game_mode != GM_EDITOR) {
  1074 		if (num == 0 && _game_mode != GM_EDITOR) {
  1073 			error("Could not generate any town");
  1075 			error("Could not generate any town");
  1074 		}
  1076 		}
  1378 	DeleteWindowById(WC_TOWN_VIEW, t->index);
  1380 	DeleteWindowById(WC_TOWN_VIEW, t->index);
  1379 	_town_sort_dirty = true;
  1381 	_town_sort_dirty = true;
  1380 
  1382 
  1381 	// Delete all industries belonging to the town
  1383 	// Delete all industries belonging to the town
  1382 	FOR_ALL_INDUSTRIES(i) {
  1384 	FOR_ALL_INDUSTRIES(i) {
  1383 		if (i->xy && i->town == t)
  1385 		if (i->town == t) DeleteIndustry(i);
  1384 			DeleteIndustry(i);
       
  1385 	}
  1386 	}
  1386 
  1387 
  1387 	// Go through all tiles and delete those belonging to the town
  1388 	// Go through all tiles and delete those belonging to the town
  1388 	for (tile = 0; tile < MapSize(); ++tile) {
  1389 	for (tile = 0; tile < MapSize(); ++tile) {
  1389 		switch (GetTileType(tile)) {
  1390 		switch (GetTileType(tile)) {
  1734 	Town *t;
  1735 	Town *t;
  1735 	uint dist, best = threshold;
  1736 	uint dist, best = threshold;
  1736 	Town *best_town = NULL;
  1737 	Town *best_town = NULL;
  1737 
  1738 
  1738 	FOR_ALL_TOWNS(t) {
  1739 	FOR_ALL_TOWNS(t) {
  1739 		if (t->xy != 0) {
  1740 		dist = DistanceManhattan(tile, t->xy);
  1740 			dist = DistanceManhattan(tile, t->xy);
  1741 		if (dist < best) {
  1741 			if (dist < best) {
  1742 			best = dist;
  1742 				best = dist;
  1743 			best_town = t;
  1743 				best_town = t;
       
  1744 			}
       
  1745 		}
  1744 		}
  1746 	}
  1745 	}
  1747 
  1746 
  1748 	return best_town;
  1747 	return best_town;
  1749 }
  1748 }
  1824 
  1823 
  1825 void TownsMonthlyLoop(void)
  1824 void TownsMonthlyLoop(void)
  1826 {
  1825 {
  1827 	Town *t;
  1826 	Town *t;
  1828 
  1827 
  1829 	FOR_ALL_TOWNS(t) if (t->xy != 0) {
  1828 	FOR_ALL_TOWNS(t) {
  1830 		if (t->road_build_months != 0) t->road_build_months--;
  1829 		if (t->road_build_months != 0) t->road_build_months--;
  1831 
  1830 
  1832 		if (t->exclusive_counter != 0)
  1831 		if (t->exclusive_counter != 0)
  1833 			if (--t->exclusive_counter == 0) t->exclusivity = (byte)-1;
  1832 			if (--t->exclusive_counter == 0) t->exclusivity = (byte)-1;
  1834 
  1833 
  1940 static void Save_TOWN(void)
  1939 static void Save_TOWN(void)
  1941 {
  1940 {
  1942 	Town *t;
  1941 	Town *t;
  1943 
  1942 
  1944 	FOR_ALL_TOWNS(t) {
  1943 	FOR_ALL_TOWNS(t) {
  1945 		if (t->xy != 0) {
  1944 		SlSetArrayIndex(t->index);
  1946 			SlSetArrayIndex(t->index);
  1945 		SlObject(t, _town_desc);
  1947 			SlObject(t, _town_desc);
       
  1948 		}
       
  1949 	}
  1946 	}
  1950 }
  1947 }
  1951 
  1948 
  1952 static void Load_TOWN(void)
  1949 static void Load_TOWN(void)
  1953 {
  1950 {
  1976 
  1973 
  1977 void AfterLoadTown(void)
  1974 void AfterLoadTown(void)
  1978 {
  1975 {
  1979 	Town *t;
  1976 	Town *t;
  1980 	FOR_ALL_TOWNS(t) {
  1977 	FOR_ALL_TOWNS(t) {
  1981 		if (t->xy != 0) {
  1978 		UpdateTownRadius(t);
  1982 			UpdateTownRadius(t);
  1979 		UpdateTownVirtCoord(t);
  1983 			UpdateTownVirtCoord(t);
       
  1984 		}
       
  1985 	}
  1980 	}
  1986 	_town_sort_dirty = true;
  1981 	_town_sort_dirty = true;
  1987 }
  1982 }
  1988 
  1983 
  1989 
  1984