src/industry_cmd.cpp
changeset 7212 5d99296fb545
parent 7207 277c6c315403
child 7216 2bb13ff061b1
equal deleted inserted replaced
7211:ebabbe55f70e 7212:5d99296fb545
  1163 	}
  1163 	}
  1164 
  1164 
  1165 	return t;
  1165 	return t;
  1166 }
  1166 }
  1167 
  1167 
  1168 static bool CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, int type)
  1168 static bool CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, int type, bool *custom_shape_check = NULL)
  1169 {
  1169 {
  1170 	_error_message = STR_0239_SITE_UNSUITABLE;
  1170 	_error_message = STR_0239_SITE_UNSUITABLE;
  1171 
  1171 
  1172 	do {
  1172 	do {
  1173 		TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
  1173 		TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
  1184 			}
  1184 			}
  1185 		} else {
  1185 		} else {
  1186 			if (!EnsureNoVehicle(cur_tile)) return false;
  1186 			if (!EnsureNoVehicle(cur_tile)) return false;
  1187 			if (MayHaveBridgeAbove(cur_tile) && IsBridgeAbove(cur_tile)) return false;
  1187 			if (MayHaveBridgeAbove(cur_tile) && IsBridgeAbove(cur_tile)) return false;
  1188 
  1188 
       
  1189 			const IndustryTileSpec *its = GetIndustryTileSpec(it->gfx);
  1189 			IndustyBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
  1190 			IndustyBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
  1190 
  1191 
  1191 			if (ind_behav & INDUSTRYBEH_BUILT_ONWATER) {
  1192 			if (HASBIT(its->callback_flags, CBM_INDT_SHAPE_CHECK)) {
  1192 				/* As soon as the tile is not water, bail out.
  1193 				if (custom_shape_check != NULL) *custom_shape_check = true;
  1193 				 * But that does not mean the search is over.  You have
  1194 				if (!PerformIndustryTileSlopeCheck(cur_tile, its, it->gfx)) return false;
  1194 				 * to make sure every tile of the industry will be only water*/
       
  1195 				if (!IsClearWaterTile(cur_tile)) return false;
       
  1196 			} else {
  1195 			} else {
  1197 				Slope tileh;
  1196 				if (ind_behav & INDUSTRYBEH_BUILT_ONWATER) {
  1198 
  1197 					/* As soon as the tile is not water, bail out.
  1199 				if (IsClearWaterTile(cur_tile)) return false;
  1198 					* But that does not mean the search is over.  You have
  1200 
  1199 					* to make sure every tile of the industry will be only water*/
  1201 				tileh = GetTileSlope(cur_tile, NULL);
  1200 					if (!IsClearWaterTile(cur_tile)) return false;
  1202 				if (IsSteepSlope(tileh)) return false;
  1201 				} else {
  1203 
  1202 					Slope tileh;
  1204 				if (_patches.land_generator != LG_TERRAGENESIS || !_generating_world) {
  1203 
  1205 					/* It is almost impossible to have a fully flat land in TG, so what we
  1204 					if (IsClearWaterTile(cur_tile)) return false;
  1206 					 *  do is that we check if we can make the land flat later on. See
  1205 
  1207 					 *  CheckIfCanLevelIndustryPlatform(). */
  1206 					tileh = GetTileSlope(cur_tile, NULL);
  1208 					if (tileh != SLOPE_FLAT) {
  1207 					if (IsSteepSlope(tileh)) return false;
  1209 						Slope t;
  1208 
  1210 						byte bits = GetIndustryTileSpec(it->gfx)->slopes_refused;
  1209 					if (_patches.land_generator != LG_TERRAGENESIS || !_generating_world) {
  1211 
  1210 						/* It is almost impossible to have a fully flat land in TG, so what we
  1212 						if (bits & 0x10) return false;
  1211 						*  do is that we check if we can make the land flat later on. See
  1213 
  1212 						*  CheckIfCanLevelIndustryPlatform(). */
  1214 						t = ComplementSlope(tileh);
  1213 						if (tileh != SLOPE_FLAT) {
  1215 
  1214 							Slope t;
  1216 						if (bits & 1 && (t & SLOPE_NW)) return false;
  1215 							byte bits = its->slopes_refused;
  1217 						if (bits & 2 && (t & SLOPE_NE)) return false;
  1216 
  1218 						if (bits & 4 && (t & SLOPE_SW)) return false;
  1217 							if (bits & 0x10) return false;
  1219 						if (bits & 8 && (t & SLOPE_SE)) return false;
  1218 
       
  1219 							t = ComplementSlope(tileh);
       
  1220 
       
  1221 							if (bits & 1 && (t & SLOPE_NW)) return false;
       
  1222 							if (bits & 2 && (t & SLOPE_NE)) return false;
       
  1223 							if (bits & 4 && (t & SLOPE_SW)) return false;
       
  1224 							if (bits & 8 && (t & SLOPE_SE)) return false;
       
  1225 						}
  1220 					}
  1226 					}
  1221 				}
  1227 				}
  1222 
  1228 			}
  1223 				if (ind_behav & INDUSTRYBEH_ONLY_INTOWN) {
  1229 
  1224 					if (!IsTileType(cur_tile, MP_HOUSE)) {
  1230 			if (ind_behav & INDUSTRYBEH_ONLY_INTOWN) {
  1225 						_error_message = STR_029D_CAN_ONLY_BE_BUILT_IN_TOWNS;
  1231 				if (!IsTileType(cur_tile, MP_HOUSE)) {
  1226 						return false;
  1232 					_error_message = STR_029D_CAN_ONLY_BE_BUILT_IN_TOWNS;
  1227 					}
  1233 					return false;
  1228 				} else {
  1234 				}
  1229 					if (ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) {
  1235 			}
  1230 						if (!IsTileType(cur_tile, MP_HOUSE)) goto do_clear;
  1236 			if (ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) {
  1231 					} else {
  1237 				if (!IsTileType(cur_tile, MP_HOUSE)) goto do_clear;
       
  1238 			} else {
  1232 do_clear:
  1239 do_clear:
  1233 						if (CmdFailed(DoCommand(cur_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR)))
  1240 				if (CmdFailed(DoCommand(cur_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR)))
  1234 							return false;
  1241 					return false;
  1235 					}
       
  1236 				}
       
  1237 			}
  1242 			}
  1238 		}
  1243 		}
  1239 	} while ((++it)->ti.x != -0x80);
  1244 	} while ((++it)->ti.x != -0x80);
  1240 
  1245 
  1241 	return true;
  1246 	return true;
  1494  * @return the pointer of the newly created industry, or NULL if it failed
  1499  * @return the pointer of the newly created industry, or NULL if it failed
  1495  */
  1500  */
  1496 static Industry *CreateNewIndustryHelper(TileIndex tile, IndustryType type, uint32 flags, const IndustrySpec *indspec, uint itspec_index)
  1501 static Industry *CreateNewIndustryHelper(TileIndex tile, IndustryType type, uint32 flags, const IndustrySpec *indspec, uint itspec_index)
  1497 {
  1502 {
  1498 	const IndustryTileTable *it = indspec->table[itspec_index];
  1503 	const IndustryTileTable *it = indspec->table[itspec_index];
       
  1504 	bool custom_shape_check = false;
       
  1505 
       
  1506 	if (!CheckIfIndustryTilesAreFree(tile, it, type, &custom_shape_check)) return NULL;
       
  1507 
  1499 	if (HASBIT(GetIndustrySpec(type)->callback_flags, CBM_IND_LOCATION)) {
  1508 	if (HASBIT(GetIndustrySpec(type)->callback_flags, CBM_IND_LOCATION)) {
  1500 		if (!CheckIfCallBackAllowsCreation(tile, type, itspec_index)) return NULL;
  1509 		if (!CheckIfCallBackAllowsCreation(tile, type, itspec_index)) return NULL;
  1501 		/* TODO: what with things like quarries and other stuff that must not be on level ground? */
  1510 	} else {
  1502 	}
  1511 		if (!_check_new_industry_procs[indspec->check_proc](tile)) return NULL;
  1503 
  1512 	}
  1504 	if (!CheckIfIndustryTilesAreFree(tile, it, type)) return NULL;
  1513 
  1505 	if (_patches.land_generator == LG_TERRAGENESIS && _generating_world && !CheckIfCanLevelIndustryPlatform(tile, 0, it, type)) return NULL;
  1514 	if (!custom_shape_check && _patches.land_generator == LG_TERRAGENESIS && _generating_world && !CheckIfCanLevelIndustryPlatform(tile, 0, it, type)) return NULL;
  1506 	if (!_check_new_industry_procs[indspec->check_proc](tile)) return NULL;
       
  1507 	if (!CheckIfTooCloseToIndustry(tile, type)) return NULL;
  1515 	if (!CheckIfTooCloseToIndustry(tile, type)) return NULL;
  1508 
  1516 
  1509 	const Town *t = CheckMultipleIndustryInTown(tile, type);
  1517 	const Town *t = CheckMultipleIndustryInTown(tile, type);
  1510 	if (t == NULL) return NULL;
  1518 	if (t == NULL) return NULL;
  1511 
  1519 
  1514 
  1522 
  1515 	Industry *i = AllocateIndustry();
  1523 	Industry *i = AllocateIndustry();
  1516 	if (i == NULL) return NULL;
  1524 	if (i == NULL) return NULL;
  1517 
  1525 
  1518 	if (flags & DC_EXEC) {
  1526 	if (flags & DC_EXEC) {
  1519 		CheckIfCanLevelIndustryPlatform(tile, DC_EXEC, it, type);
  1527 		if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_EXEC, it, type);
  1520 		DoCreateNewIndustry(i, tile, type, it, t, OWNER_NONE);
  1528 		DoCreateNewIndustry(i, tile, type, it, t, OWNER_NONE);
  1521 	}
  1529 	}
  1522 
  1530 
  1523 	return i;
  1531 	return i;
  1524 }
  1532 }