src/town_cmd.cpp
changeset 6982 c414fad6c440
parent 6980 6b5dee376733
child 6987 b0f13039bda2
equal deleted inserted replaced
6981:5dfb8540e66a 6982:c414fad6c440
   952 {
   952 {
   953 	t->max_pass = t->population >> 3;
   953 	t->max_pass = t->population >> 3;
   954 	t->max_mail = t->population >> 4;
   954 	t->max_mail = t->population >> 4;
   955 }
   955 }
   956 
   956 
   957 static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, uint size_mode)
   957 /**
       
   958  * Does the actual town creation.
       
   959  *
       
   960  * @param t The town
       
   961  * @param tile Where to put it
       
   962  * @param townnameparts The town name
       
   963  * @param size_mode How the size should be determined
       
   964  * @param size Parameter for size determination
       
   965  */
       
   966 static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSizeMode size_mode, uint size)
   958 {
   967 {
   959 	int x, i;
   968 	int x, i;
   960 
   969 
   961 	/* clear the town struct */
   970 	/* clear the town struct */
   962 	i = t->index;
   971 	i = t->index;
  1001 	t->townnameparts = townnameparts;
  1010 	t->townnameparts = townnameparts;
  1002 
  1011 
  1003 	UpdateTownVirtCoord(t);
  1012 	UpdateTownVirtCoord(t);
  1004 	_town_sort_dirty = true;
  1013 	_town_sort_dirty = true;
  1005 
  1014 
  1006 	if (size_mode == 0) {
  1015 	/* Random town size. */
  1007 		x = (Random() & 0xF) + 8;
  1016 	x = (Random() & 0xF) + 8;
  1008 	} else {
  1017 
  1009 		x = (size_mode - 1) * 16 + 3;
  1018 	switch (size_mode) {
       
  1019 		default: NOT_REACHED();
       
  1020 
       
  1021 		case TSM_RANDOM:
       
  1022 			t->larger_town = false;
       
  1023 			break;
       
  1024 
       
  1025 		case TSM_FIXED:
       
  1026 			x = size * 16 + 3;
       
  1027 			t->larger_town = false;
       
  1028 			break;
       
  1029 
       
  1030 		case TSM_CITY:
       
  1031 			x *= _patches.initial_city_size;
       
  1032 			t->larger_town = true;
       
  1033 			break;
  1010 	}
  1034 	}
  1011 
  1035 
  1012 	t->num_houses += x;
  1036 	t->num_houses += x;
  1013 	UpdateTownRadius(t);
  1037 	UpdateTownRadius(t);
  1014 
  1038 
  1049 /** Create a new town.
  1073 /** Create a new town.
  1050  * This obviously only works in the scenario editor. Function not removed
  1074  * This obviously only works in the scenario editor. Function not removed
  1051  * as it might be possible in the future to fund your own town :)
  1075  * as it might be possible in the future to fund your own town :)
  1052  * @param tile coordinates where town is built
  1076  * @param tile coordinates where town is built
  1053  * @param flags type of operation
  1077  * @param flags type of operation
  1054  * @param p1 size of the town (0 = random, 1 = small, 2 = medium, 3 = large)
  1078  * @param p1 size of the town (0 = small, 1 = medium, 2 = large)
  1055  * @param p2 unused
  1079  * @param p2 size mode (@see TownSizeMode)
  1056  */
  1080  */
  1057 int32 CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1081 int32 CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1058 {
  1082 {
  1059 	Town *t;
  1083 	Town *t;
  1060 	uint32 townnameparts;
  1084 	uint32 townnameparts;
  1061 
  1085 
  1062 	/* Only in the scenario editor */
  1086 	/* Only in the scenario editor */
  1063 	if (_game_mode != GM_EDITOR) return CMD_ERROR;
  1087 	if (_game_mode != GM_EDITOR) return CMD_ERROR;
       
  1088 	if (p2 > TSM_CITY) return CMD_ERROR;
  1064 
  1089 
  1065 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
  1090 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
  1066 
  1091 
  1067 	/* Check if too close to the edge of map */
  1092 	/* Check if too close to the edge of map */
  1068 	if (DistanceFromEdge(tile) < 12)
  1093 	if (DistanceFromEdge(tile) < 12)
  1086 	if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
  1111 	if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
  1087 
  1112 
  1088 	/* Create the town */
  1113 	/* Create the town */
  1089 	if (flags & DC_EXEC) {
  1114 	if (flags & DC_EXEC) {
  1090 		_generating_world = true;
  1115 		_generating_world = true;
  1091 		DoCreateTown(t, tile, townnameparts, p1);
  1116 		DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1);
  1092 		_generating_world = false;
  1117 		_generating_world = false;
  1093 	}
  1118 	}
  1094 	return 0;
  1119 	return 0;
  1095 }
  1120 }
  1096 
  1121 
  1097 Town *CreateRandomTown(uint attempts, uint size_mode)
  1122 Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size)
  1098 {
  1123 {
  1099 	TileIndex tile;
  1124 	TileIndex tile;
  1100 	Town *t;
  1125 	Town *t;
  1101 	uint32 townnameparts;
  1126 	uint32 townnameparts;
  1102 
  1127 
  1116 
  1141 
  1117 		/* Allocate a town struct */
  1142 		/* Allocate a town struct */
  1118 		t = AllocateTown();
  1143 		t = AllocateTown();
  1119 		if (t == NULL) break;
  1144 		if (t == NULL) break;
  1120 
  1145 
  1121 		DoCreateTown(t, tile, townnameparts, size_mode);
  1146 		DoCreateTown(t, tile, townnameparts, mode, size);
  1122 		return t;
  1147 		return t;
  1123 	} while (--attempts);
  1148 	} while (--attempts);
  1124 	return NULL;
  1149 	return NULL;
  1125 }
  1150 }
  1126 
  1151 
  1128 
  1153 
  1129 bool GenerateTowns()
  1154 bool GenerateTowns()
  1130 {
  1155 {
  1131 	uint num = 0;
  1156 	uint num = 0;
  1132 	uint n = ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
  1157 	uint n = ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
       
  1158 	uint num_cities = _patches.larger_towns == 0 ? 0 : n / _patches.larger_towns;
  1133 
  1159 
  1134 	SetGeneratingWorldProgress(GWP_TOWN, n);
  1160 	SetGeneratingWorldProgress(GWP_TOWN, n);
  1135 
  1161 
  1136 	do {
  1162 	do {
  1137 		IncreaseGeneratingWorldProgress(GWP_TOWN);
  1163 		IncreaseGeneratingWorldProgress(GWP_TOWN);
  1138 		/* try 20 times to create a random-sized town for the first loop. */
  1164 		/* try 20 times to create a random-sized town for the first loop. */
  1139 		if (CreateRandomTown(20, 0) != NULL) num++;
  1165 		TownSizeMode mode = num_cities > 0 ? TSM_CITY : TSM_RANDOM;
       
  1166 		if (CreateRandomTown(20, mode, _patches.initial_city_size) != NULL) num++;
       
  1167 		if (num_cities > 0) num_cities--;
  1140 	} while (--n);
  1168 	} while (--n);
  1141 
  1169 
  1142 	/* give it a last try, but now more aggressive */
  1170 	/* give it a last try, but now more aggressive */
  1143 	if (num == 0 && CreateRandomTown(10000, 0) == NULL) {
  1171 	if (num == 0 && CreateRandomTown(10000, TSM_RANDOM, 0) == NULL) {
  1144 		if (GetNumTowns() == 0) {
  1172 		if (GetNumTowns() == 0) {
  1145 			/* XXX - can we handle that more gracefully? */
  1173 			/* XXX - can we handle that more gracefully? */
  1146 			if (_game_mode != GM_EDITOR) error("Could not generate any town");
  1174 			if (_game_mode != GM_EDITOR) error("Could not generate any town");
  1147 
  1175 
  1148 			return false;
  1176 			return false;
  1716 	/* Use the normal growth rate values if new buildings have been funded in
  1744 	/* Use the normal growth rate values if new buildings have been funded in
  1717 	 * this town and the growth rate is set to none. */
  1745 	 * this town and the growth rate is set to none. */
  1718 	uint growth_multiplier = _patches.town_growth_rate != 0 ? _patches.town_growth_rate - 1 : 1;
  1746 	uint growth_multiplier = _patches.town_growth_rate != 0 ? _patches.town_growth_rate - 1 : 1;
  1719 
  1747 
  1720 	m >>= growth_multiplier;
  1748 	m >>= growth_multiplier;
  1721 	if (_patches.larger_towns != 0 && (t->index % _patches.larger_towns) == 0) m /= 2;
  1749 	if (t->larger_town) m /= 2;
  1722 
  1750 
  1723 	t->growth_rate = m / (t->num_houses / 50 + 1);
  1751 	t->growth_rate = m / (t->num_houses / 50 + 1);
  1724 	if (m <= t->grow_counter)
  1752 	if (m <= t->grow_counter)
  1725 		t->grow_counter = m;
  1753 		t->grow_counter = m;
  1726 
  1754 
  1975 	    SLE_VAR(Town, fund_buildings_months, SLE_UINT8),
  2003 	    SLE_VAR(Town, fund_buildings_months, SLE_UINT8),
  1976 	    SLE_VAR(Town, road_build_months,     SLE_UINT8),
  2004 	    SLE_VAR(Town, road_build_months,     SLE_UINT8),
  1977 
  2005 
  1978 	    SLE_VAR(Town, exclusivity,           SLE_UINT8),
  2006 	    SLE_VAR(Town, exclusivity,           SLE_UINT8),
  1979 	    SLE_VAR(Town, exclusive_counter,     SLE_UINT8),
  2007 	    SLE_VAR(Town, exclusive_counter,     SLE_UINT8),
       
  2008 
       
  2009 	SLE_CONDVAR(Town, larger_town,           SLE_BOOL,                  56, SL_MAX_VERSION),
       
  2010 
  1980 	/* reserve extra space in savegame here. (currently 30 bytes) */
  2011 	/* reserve extra space in savegame here. (currently 30 bytes) */
  1981 	SLE_CONDNULL(30, 2, SL_MAX_VERSION),
  2012 	SLE_CONDNULL(30, 2, SL_MAX_VERSION),
  1982 
  2013 
  1983 	SLE_END()
  2014 	SLE_END()
  1984 };
  2015 };