src/town_cmd.cpp
branchgamebalance
changeset 9909 dce9a6923bb7
parent 9908 0fa543611bbe
child 9910 0b2aebc8283e
equal deleted inserted replaced
9908:0fa543611bbe 9909:dce9a6923bb7
   947 {
   947 {
   948 	t->max_pass = t->population >> 3;
   948 	t->max_pass = t->population >> 3;
   949 	t->max_mail = t->population >> 4;
   949 	t->max_mail = t->population >> 4;
   950 }
   950 }
   951 
   951 
   952 static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, uint size_mode)
   952 /**
       
   953  * Does the actual town creation.
       
   954  *
       
   955  * @param t The town
       
   956  * @param tile Where to put it
       
   957  * @param townnameparts The town name
       
   958  * @param size_mode How the size should be determined
       
   959  * @param size Parameter for size determination
       
   960  */
       
   961 static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSizeMode size_mode, uint size)
   953 {
   962 {
   954 	int x, i;
   963 	int x, i;
   955 
   964 
   956 	/* clear the town struct */
   965 	/* clear the town struct */
   957 	i = t->index;
   966 	i = t->index;
  1012 	t->townnameparts = townnameparts;
  1021 	t->townnameparts = townnameparts;
  1013 
  1022 
  1014 	UpdateTownVirtCoord(t);
  1023 	UpdateTownVirtCoord(t);
  1015 	_town_sort_dirty = true;
  1024 	_town_sort_dirty = true;
  1016 
  1025 
  1017 	if (size_mode == 0) {
  1026 	/* Random town size. */
  1018 		x = (Random() & 0xF) + 8;
  1027 	x = (Random() & 0xF) + 8;
  1019 	} else {
  1028 
  1020 		x = (size_mode - 1) * 16 + 3;
  1029 	switch (size_mode) {
       
  1030 		default: NOT_REACHED();
       
  1031 
       
  1032 		case TSM_RANDOM:
       
  1033 			t->larger_town = false;
       
  1034 			break;
       
  1035 
       
  1036 		case TSM_FIXED:
       
  1037 			x = size * 16 + 3;
       
  1038 			t->larger_town = false;
       
  1039 			break;
       
  1040 
       
  1041 		case TSM_CITY:
       
  1042 			x *= _patches.initial_city_size;
       
  1043 			t->larger_town = true;
       
  1044 			break;
  1021 	}
  1045 	}
  1022 
  1046 
  1023 	t->num_houses += x;
  1047 	t->num_houses += x;
  1024 	UpdateTownRadius(t);
  1048 	UpdateTownRadius(t);
  1025 
  1049 
  1060 /** Create a new town.
  1084 /** Create a new town.
  1061  * This obviously only works in the scenario editor. Function not removed
  1085  * This obviously only works in the scenario editor. Function not removed
  1062  * as it might be possible in the future to fund your own town :)
  1086  * as it might be possible in the future to fund your own town :)
  1063  * @param tile coordinates where town is built
  1087  * @param tile coordinates where town is built
  1064  * @param flags type of operation
  1088  * @param flags type of operation
  1065  * @param p1 size of the town (0 = random, 1 = small, 2 = medium, 3 = large)
  1089  * @param p1 size of the town (0 = small, 1 = medium, 2 = large)
  1066  * @param p2 unused
  1090  * @param p2 size mode (@see TownSizeMode)
  1067  */
  1091  */
  1068 int32 CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1092 int32 CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1069 {
  1093 {
  1070 	Town *t;
  1094 	Town *t;
  1071 	uint32 townnameparts;
  1095 	uint32 townnameparts;
  1072 
  1096 
  1073 	/* Only in the scenario editor */
  1097 	/* Only in the scenario editor */
  1074 	if (_game_mode != GM_EDITOR) return CMD_ERROR;
  1098 	if (_game_mode != GM_EDITOR) return CMD_ERROR;
       
  1099 	if (p2 > TSM_CITY) return CMD_ERROR;
  1075 
  1100 
  1076 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
  1101 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
  1077 
  1102 
  1078 	/* Check if too close to the edge of map */
  1103 	/* Check if too close to the edge of map */
  1079 	if (DistanceFromEdge(tile) < 12)
  1104 	if (DistanceFromEdge(tile) < 12)
  1097 	if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
  1122 	if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
  1098 
  1123 
  1099 	/* Create the town */
  1124 	/* Create the town */
  1100 	if (flags & DC_EXEC) {
  1125 	if (flags & DC_EXEC) {
  1101 		_generating_world = true;
  1126 		_generating_world = true;
  1102 		DoCreateTown(t, tile, townnameparts, p1);
  1127 		DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1);
  1103 		_generating_world = false;
  1128 		_generating_world = false;
  1104 	}
  1129 	}
  1105 	return 0;
  1130 	return 0;
  1106 }
  1131 }
  1107 
  1132 
  1108 Town *CreateRandomTown(uint attempts, uint size_mode)
  1133 Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size)
  1109 {
  1134 {
  1110 	TileIndex tile;
  1135 	TileIndex tile;
  1111 	Town *t;
  1136 	Town *t;
  1112 	uint32 townnameparts;
  1137 	uint32 townnameparts;
  1113 
  1138 
  1127 
  1152 
  1128 		/* Allocate a town struct */
  1153 		/* Allocate a town struct */
  1129 		t = AllocateTown();
  1154 		t = AllocateTown();
  1130 		if (t == NULL) break;
  1155 		if (t == NULL) break;
  1131 
  1156 
  1132 		DoCreateTown(t, tile, townnameparts, size_mode);
  1157 		DoCreateTown(t, tile, townnameparts, mode, size);
  1133 		return t;
  1158 		return t;
  1134 	} while (--attempts);
  1159 	} while (--attempts);
  1135 	return NULL;
  1160 	return NULL;
  1136 }
  1161 }
  1137 
  1162 
  1139 
  1164 
  1140 bool GenerateTowns()
  1165 bool GenerateTowns()
  1141 {
  1166 {
  1142 	uint num = 0;
  1167 	uint num = 0;
  1143 	uint n = ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
  1168 	uint n = ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
       
  1169 	uint num_cities = _patches.larger_towns == 0 ? 0 : n / _patches.larger_towns;
  1144 
  1170 
  1145 	SetGeneratingWorldProgress(GWP_TOWN, n);
  1171 	SetGeneratingWorldProgress(GWP_TOWN, n);
  1146 
  1172 
  1147 	do {
  1173 	do {
  1148 		IncreaseGeneratingWorldProgress(GWP_TOWN);
  1174 		IncreaseGeneratingWorldProgress(GWP_TOWN);
  1149 		/* try 20 times to create a random-sized town for the first loop. */
  1175 		/* try 20 times to create a random-sized town for the first loop. */
  1150 		if (CreateRandomTown(20, 0) != NULL) num++;
  1176 		TownSizeMode mode = num_cities > 0 ? TSM_CITY : TSM_RANDOM;
       
  1177 		if (CreateRandomTown(20, mode, _patches.initial_city_size) != NULL) num++;
       
  1178 		if (num_cities > 0) num_cities--;
  1151 	} while (--n);
  1179 	} while (--n);
  1152 
  1180 
  1153 	/* give it a last try, but now more aggressive */
  1181 	/* give it a last try, but now more aggressive */
  1154 	if (num == 0 && CreateRandomTown(10000, 0) == NULL) {
  1182 	if (num == 0 && CreateRandomTown(10000, TSM_RANDOM, 0) == NULL) {
  1155 		if (GetNumTowns() == 0) {
  1183 		if (GetNumTowns() == 0) {
  1156 			/* XXX - can we handle that more gracefully? */
  1184 			/* XXX - can we handle that more gracefully? */
  1157 			if (_game_mode != GM_EDITOR) error("Could not generate any town");
  1185 			if (_game_mode != GM_EDITOR) error("Could not generate any town");
  1158 
  1186 
  1159 			return false;
  1187 			return false;
  1537 	return true;
  1565 	return true;
  1538 }
  1566 }
  1539 
  1567 
  1540 /**
  1568 /**
  1541  * Search callback function for TownActionBuildStatue
  1569  * Search callback function for TownActionBuildStatue
       
  1570  * @param tile on which to perform the search
  1542  * @param town_id The town_id for which we want a statue
  1571  * @param town_id The town_id for which we want a statue
  1543  * @return the result of the test
  1572  * @return the result of the test
  1544  */
  1573  */
  1545 static bool SearchTileForStatue(TileIndex tile, uint32 town_id)
  1574 static bool SearchTileForStatue(TileIndex tile, uint32 town_id)
  1546 {
  1575 {
  1711 	/* Use the normal growth rate values if new buildings have been funded in
  1740 	/* Use the normal growth rate values if new buildings have been funded in
  1712 	 * this town and the growth rate is set to none. */
  1741 	 * this town and the growth rate is set to none. */
  1713 	uint growth_multiplier = _patches.town_growth_rate != 0 ? _patches.town_growth_rate - 1 : 1;
  1742 	uint growth_multiplier = _patches.town_growth_rate != 0 ? _patches.town_growth_rate - 1 : 1;
  1714 
  1743 
  1715 	m >>= growth_multiplier;
  1744 	m >>= growth_multiplier;
  1716 	if (_patches.larger_towns != 0 && (t->index % _patches.larger_towns) == 0) m /= 2;
  1745 	if (t->larger_town) m /= 2;
  1717 
  1746 
  1718 	t->growth_rate = m / (t->num_houses / 50 + 1);
  1747 	t->growth_rate = m / (t->num_houses / 50 + 1);
  1719 	if (m <= t->grow_counter)
  1748 	if (m <= t->grow_counter)
  1720 		t->grow_counter = m;
  1749 		t->grow_counter = m;
  1721 
  1750 
  1971 	    SLE_VAR(Town, fund_buildings_months, SLE_UINT8),
  2000 	    SLE_VAR(Town, fund_buildings_months, SLE_UINT8),
  1972 	    SLE_VAR(Town, road_build_months,     SLE_UINT8),
  2001 	    SLE_VAR(Town, road_build_months,     SLE_UINT8),
  1973 
  2002 
  1974 	    SLE_VAR(Town, exclusivity,           SLE_UINT8),
  2003 	    SLE_VAR(Town, exclusivity,           SLE_UINT8),
  1975 	    SLE_VAR(Town, exclusive_counter,     SLE_UINT8),
  2004 	    SLE_VAR(Town, exclusive_counter,     SLE_UINT8),
       
  2005 
       
  2006 	SLE_CONDVAR(Town, larger_town,           SLE_BOOL,                  56, SL_MAX_VERSION),
       
  2007 
  1976 	/* reserve extra space in savegame here. (currently 30 bytes) */
  2008 	/* reserve extra space in savegame here. (currently 30 bytes) */
  1977 	SLE_CONDNULL(30, 2, SL_MAX_VERSION),
  2009 	SLE_CONDNULL(30, 2, SL_MAX_VERSION),
  1978 
  2010 
  1979 	SLE_END()
  2011 	SLE_END()
  1980 };
  2012 };