
//////////////////////////////////////////////////////////////////////////////////
//										
// 1) Scenario script for Frenzy AI Mod version 1.04		
//										
// 2) City Expansion							
//										
// by BlueOrange 								
//														
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////
// GLOBAL VARIABLES
//////////////////////////////////////////////	

//
// There is only one variable actuall needed here (FAI_INIT) and it is expendable! I will remove it later.
//

int_t	FAI_INIT;
int_t	FAI_ATTACK_SIZE;		// minimum stack size for what I consider BIG
int_t FAI_SECOND_ARMY_COEF;		// ai needs this many (coef x human units) to form SecondAttackArmy;
int_t	FAI_HATE1_COEF;			// level of hate to reach level 1 FAI_FRENZY state
int_t	FAI_HATE2_COEF;			// level of hate to reach level 2
int_t	FAI_HATE3_COEF;			// level of hate to reach level 3
int_t	FAI_HATE4_COEF;			// level of hate to reach level 4
int_t	FAI_FRENZY;				// FAI_FRENZY state of AI players.
int_t	FAI_MAXPLAYERS;

int_t 	FAI_PlayerAge[];
int_t		FAI_HateLevel[];
army_t	FAI_FirstArmy[];
army_t	FAI_SecondArmy[];
location_t	FAI_AttackTarget[];
int_t		FAI_HumanNumber;

HandleEvent(BeginTurn) 'FAI_Initialization' pre {
int_t		i;
army_t	tmpArmy;

	FAI_ATTACK_SIZE = 5;
	FAI_SECOND_ARMY_COEF = 2;
	FAI_HATE1_COEF = 20;			//PEDRUNN: set to 20
	FAI_HATE2_COEF = 45;
	FAI_HATE3_COEF = 75;
	FAI_HATE4_COEF = 100;
	FAI_FRENZY = 0;
	FAI_MAXPLAYERS = 31;

	i = 0;
	while (i <= FAI_MAXPLAYERS) {	// try all possible players
		player[1] = i;
		if (player[1].units > 0) {
			GetArmyByIndex(player[1], 0, tmpArmy);
			i = FAI_MAXPLAYERS;
		}
		i = i + 1;
	}
	for (i = 0; i <= FAI_MAXPLAYERS; i = i + 1) {	// initialize all playes, so new players from revolts will be 'supported' as well	// much simpler & faster like this
		FAI_PlayerAge[i] = 1;	
		FAI_HateLevel[i] = 0;
		FAI_FirstArmy[i] = tmpArmy;
		FAI_SecondArmy[i] = tmpArmy;
		FAI_AttackTarget[i] = tmpArmy.location;
	}						
	FAI_INIT = 1000;
	DisableTrigger('FAI_Initialization');
}

int_f FAI_UnitsCount(int_t tmpPlayer) {
	player[5] = tmpPlayer;	// 5 is unlikely to overwrite an existing value
	return player[5].units;
}

////////////////////////////////////////////////////////////////////////////////////////////////
// 2)City Expansion Code 
//
// ExpandCity Fuction:
// Given the location of the city 'cityLoc', fuction will attempt to fill the 'numOfType' of the subcity 'tileType'
// around the city.  The 'owner' parameter ensures that the tiles belong to the city owner.
//
// Fuction begins by searching all surrounding tiles 1 radius away from the city and look for proper tiles to put
// the new subcity tile 'tileType' into.  If it fails to find such a tile 1 radius away, it'll look 2 radius away, 
// then 3, and finally 4 radius away.
// If it cannot put in all the subcity tiles, then that means there are no more room for the city to expand.
//
// If all the tiles are placed, returns 1.
// If all the tiles are not placed, returns 0.
// ('addDelete' == 1) adds the tile
// ('addDelete' == 2) deletes a tile
// ('addDelete' == 3) adds a wonder
//  
////////////////////////////////////////////////////////////////////////////////////////////////
//
// PlaceCity Fuction:
// Will search through the tiles with certain 'range' and 'direction' starting from 'checkLoc' for player 'owner'.
// If applicable terrain type is found for 'checkType', that terrain will be replaced accordingly.
// The 'newType' parameter determines what to replace.  
// ('addDelete' == 1) adds the tile
// ('addDelete' == 2) deletes the tile
// ('addDelete' == 3) adds a wonder
//
// Function returns 1 if any tile replacement took place.  Returns 0 if it doesn't.
//

int_f TileHasWonder(location_t tmpLoc) {
	if(TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_PYRAMIDS))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_HANGING_GARDENS))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_CHICHEN_ITZA))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_LIGHTHOUSE))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_COLOSSUS))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_COLISEUM))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_OLYMPICS))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_GREAT_LIBRARY))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_THE_lABYRINTH))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_THE_FORBIDDEN_CITY))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_HAGIA_SOPHIA))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_ANGKOR_WAT))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_THE_VATICAN))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_DOME_OF_THE_ROCK))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_TAJ_MAHAL))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_EAST_INDIA_COMPANY))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_COPERNICUS_OBSERVATORY))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_EMANCIPATION_PROCLAMATION))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_HOLY_INQUISITION))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_ADAM_SMITH_COMPANY))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_DISNEYLAND))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_EMPIRE_STATE_BUILDING))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_HOLLYWOOD))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_PASTEUR_INSTITUTE))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_APOLLO_PROGRAM))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_GLOBESAT))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_GENOME_PROJECT))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_THE_AGENCY))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_UNITED_NATIONS))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_FIELD_DYNAMICS_LABORATORY))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_NATIONAL_SHIELD))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_DATA_HAVEN))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_NANITE_DEFUSER))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_GLOBAL_SURVEILLANCE_CENTER))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_DINOSAUR_PARK))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_CENTRAL_MATTER_DECOMPILER))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_THE_SOLARIS_PROJECT))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_AI_ENTITY))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_GREAT_WALL_A))
	|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_WONDER_GREAT_WALL_B))) {
		return 1;
	} else {
		return 0;
	}
}

int_f TileHasCity(location_t tmpLoc) {		// Will be erased for sure since it wasnt used. It is here just in case :)
	if(TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_ONE))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_DEAD))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_ONE))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_TWO))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_THREE))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_DEAD))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_ONE))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_TWO))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_DEAD))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_ONE))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_TWO))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_DEAD))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_SEA_CITY_ONE))
	 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_SEA_CITY_DEAD))) {
		return 1;
	} else {
		return 0;
	}
}

int_t		ReplaceOld;
int_t		DontReduce1;

int_f PlaceCity(int_t owner, location_t checkLoc, int_t range, int_t direction, int_t newType, int_t addDelete) {
location_t 	tmpLoc;
location_t	tmp2Loc;
int_t		searchDirection;
int_t		TType;
int_t		count;
int_t		sucess;
int_t		ReplaceTile;
unit_t		tmpUnit;
	tmpLoc = checkLoc;
	count = range;
									// Searching through the tiles, clockwise.
	if (direction == 0) {					// 
		searchDirection = 4; 	}			// 		   NORTH	
	if (direction == 1) {					// 		     0
		searchDirection = 4;	}			//		  3    1
	if (direction == 2) {					//     WEST     5   +    2     EAST
		searchDirection = 6;	}			//                6    4
	if (direction == 3) {					//                  7
		searchDirection = 1;	}			//		  SOUTH
	if (direction == 4) {					//
		searchDirection = 6;	}
	if (direction == 5) {
		searchDirection = 1;	}
	if (direction == 6) {
		searchDirection = 3;	}
	if (direction == 7) {
		searchDirection = 3;	}

	while (count > 0) {
		TType = TerrainType(tmpLoc);	
		if (cellOwner(tmpLoc) == owner && addDelete == 3 && TileHasWonder(tmpLoc) == 0) { 
			if (TType == TerrainDB(TERRAIN_GRASSLAND)
			 || TType == TerrainDB(TERRAIN_PLAINS) 
			 || TType == TerrainDB(TERRAIN_DESERT)
			 || TType == TerrainDB(TERRAIN_TUNDRA)
			 || TType == TerrainDB(TERRAIN_GLACIER)
			 || TType == TerrainDB(TERRAIN_HILL)
			 || TType == TerrainDB(TERRAIN_BROWN_HILL) 
			 || TType == TerrainDB(TERRAIN_WHITE_HILL) 
			 || TType == TerrainDB(TERRAIN_FOREST) 
			 || TType == TerrainDB(TERRAIN_JUNGLE)
			 || TType == TerrainDB(TERRAIN_MOUNTAIN)
			 || TType == TerrainDB(TERRAIN_BROWN_MOUNTAIN)
			 || TType == TerrainDB(TERRAIN_WHITE_MOUNTAIN)
			 || TType == TerrainDB(TERRAIN_SWAMP)){
			 	Event:CreateImprovement(owner, tmpLoc, newType, 0);
				return 1;
			}
		}
		if (HasAdvance(owner, ID_ADVANCE_AGRICULTURE)
		 && !HasAdvance(owner, ID_ADVANCE_FEUDALISM)
		 && !HasAdvance(owner, ID_ADVANCE_MASS_MEDIA)
		 && !HasAdvance(owner, ID_ADVANCE_GAIA_THEORY)
		 && !HasAdvance(owner, ID_ADVANCE_FUTURE_TECHNOLOGY)) {
			 if (cellOwner(tmpLoc) == owner && addDelete == 2) { 
				if(newType == 1 && TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_ONE))){
					Event: CutImprovements(tmpLoc);
					return 1;
				}
			}
			if (newType == 1 && cellOwner(tmpLoc) == owner && addDelete == 1 
			&& !TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_ONE))
			&& TileHasWonder(tmpLoc) == 0) {	
				if (TType == TerrainDB(TERRAIN_FOREST) 
				 || TType == TerrainDB(TERRAIN_JUNGLE)
				 || TType == TerrainDB(TERRAIN_SWAMP)){				
					ReplaceTile = 0;
					if(ReplaceTile < 2) {
						Terraform(tmpLoc, TerrainDB(TERRAIN_PLAINS));
						ReplaceTile = ReplaceTile + 1;
					}
				} 
				elseif (TType == TerrainDB(TERRAIN_GRASSLAND)
				 || TType == TerrainDB(TERRAIN_PLAINS) 
				 || TType == TerrainDB(TERRAIN_DESERT)
				 || TType == TerrainDB(TERRAIN_TUNDRA)
				 || TType == TerrainDB(TERRAIN_HILL)
				 || TType == TerrainDB(TERRAIN_BROWN_HILL)) {					
					Event:CreateImprovement(owner,tmpLoc,TerrainImprovementDB(TILEIMP_ANCIENT_CITY_ONE),0 );
					return 1;
				}
			}
		}
		if (HasAdvance(owner, ID_ADVANCE_AGRICULTURE)
		 && HasAdvance(owner, ID_ADVANCE_FEUDALISM)
		 && !HasAdvance(owner, ID_ADVANCE_MASS_MEDIA)
		 && !HasAdvance(owner, ID_ADVANCE_GAIA_THEORY)
		 && !HasAdvance(owner, ID_ADVANCE_FUTURE_TECHNOLOGY)) {
		 	if (replaceOld < 1 && cellOwner(tmpLoc) == owner) {
				if (TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_ONE))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_DEAD))) {			 	
					Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_MEDIEVAL_CITY_ONE),0 );// Create Railroad
					replaceOld = replaceOld + 1;
				}		
			}
			if (cellOwner(tmpLoc) == owner && addDelete == 2) { 
				if(newType == 1 && TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MEDIEVAL_CITY_ONE))){
					Event: CutImprovements(tmpLoc);
					return 1;
				}
			}
			if (newType == 1 && cellOwner(tmpLoc) == owner && addDelete == 1 
			&& !TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MEDIEVAL_CITY_ONE))
			&& TileHasWonder(tmpLoc) == 0) {	
				if (TType == TerrainDB(TERRAIN_FOREST) 
				 || TType == TerrainDB(TERRAIN_JUNGLE)
				 || TType == TerrainDB(TERRAIN_SWAMP)){				
					ReplaceTile = 0;
					if(ReplaceTile < 2) {
						Terraform(tmpLoc, TerrainDB(TERRAIN_PLAINS));
						ReplaceTile = ReplaceTile + 1;
					}
				} 
				elseif (TType == TerrainDB(TERRAIN_GRASSLAND)
				 || TType == TerrainDB(TERRAIN_PLAINS) 
				 || TType == TerrainDB(TERRAIN_DESERT)
				 || TType == TerrainDB(TERRAIN_TUNDRA)
				 || TType == TerrainDB(TERRAIN_HILL)
				 || TType == TerrainDB(TERRAIN_BROWN_HILL)) {					
					Event:CreateImprovement(owner,tmpLoc,TerrainImprovementDB(TILEIMP_MEDIEVAL_CITY_ONE),0 );
					return 1;
				}
			}
		}
		if (HasAdvance(owner, ID_ADVANCE_AGRICULTURE)
		 && HasAdvance(owner, ID_ADVANCE_FEUDALISM)
		 && HasAdvance(owner, ID_ADVANCE_MASS_MEDIA)
		 && !HasAdvance(owner, ID_ADVANCE_GAIA_THEORY)
		 && !HasAdvance(owner, ID_ADVANCE_FUTURE_TECHNOLOGY)) {
			if (replaceOld < 1 && cellOwner(tmpLoc) == owner) {
				if (TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_ONE))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MEDIEVAL_CITY_ONE))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_DEAD))) {			 	
					Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_ONE),0 );// Create Railroad
					replaceOld = replaceOld + 1;
				}		
			}
			 if (cellOwner(tmpLoc) == owner && addDelete == 2) { 
				if(newType == 1 && TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_ONE))){
					Event: CutImprovements(tmpLoc);
					return 1;
				}
				elseif(newType == 2 && TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_TWO))){
					Event: CutImprovements(tmpLoc);
					return 1;
				}
				elseif(newType == 3 && TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_THREE))){
					Event: CutImprovements(tmpLoc);
					return 1;
				}
			}
			if (newType == 1 && cellOwner(tmpLoc) == owner && addDelete == 1) { 
				if(!TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_ONE)) 
				 && !TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_TWO)) 
				 && !TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_THREE))
				 && TileHasWonder(tmpLoc) == 0) {	
					if (TType == TerrainDB(TERRAIN_FOREST) 
					|| TType == TerrainDB(TERRAIN_JUNGLE) 
					|| TType == TerrainDB(TERRAIN_SWAMP)) {
						ReplaceTile = 0;
						if(ReplaceTile < 2) {
							Terraform(tmpLoc, TerrainDB(TERRAIN_PLAINS));
							ReplaceTile = ReplaceTile + 1;
						}
					} 
					elseif (TType == TerrainDB(TERRAIN_GRASSLAND)
					 || TType == TerrainDB(TERRAIN_PLAINS) 
					 || TType == TerrainDB(TERRAIN_DESERT)
					 || TType == TerrainDB(TERRAIN_TUNDRA)
					 || TType == TerrainDB(TERRAIN_HILL)
					 || TType == TerrainDB(TERRAIN_BROWN_HILL)) {	
						Event:CreateImprovement(owner,tmpLoc,TerrainImprovementDB(TILEIMP_MODERN_CITY_ONE),0 ); // Create Road
						return 1;
					}
				}
			}
			if (newType == 2 && cellOwner(tmpLoc) == owner && addDelete == 1)	{	
				if (TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_ONE))) {	
				 	Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_TWO),0 );// Create Railroad				
					return 1;
				}
			}
			if (newType == 3 && cellOwner(tmpLoc) == owner && addDelete == 1) {	
				if (TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_TWO))) {	
				 	Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_THREE),0 );// Create Railroad				
					return 1;
				}
			}
		}
		if (HasAdvance(owner, ID_ADVANCE_AGRICULTURE)
		 && HasAdvance(owner, ID_ADVANCE_FEUDALISM)
		 && HasAdvance(owner, ID_ADVANCE_MASS_MEDIA)
		 && HasAdvance(owner, ID_ADVANCE_GAIA_THEORY)
		 && !HasAdvance(owner, ID_ADVANCE_FUTURE_TECHNOLOGY)) {
			if (replaceOld < 2 && cellOwner(tmpLoc) == owner) {
				if (TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_ONE))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MEDIEVAL_CITY_ONE))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_DEAD))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_ONE))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_TWO))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_DEAD))) {
					Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_ONE),0 );
					replaceOld = replaceOld + 1;
				} 
				elseif (TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_THREE))) {
					Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_TWO),0 );
					replaceOld = replaceOld + 1;
				} 
			}
			if (cellOwner(tmpLoc) == owner && addDelete == 2) { 
				if(newType == 1 && TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_ONE))){
					Event: CutImprovements(tmpLoc);
					return 1;
				}
				elseif(newType > 1 && TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_TWO))){
					Event: CutImprovements(tmpLoc);
					return 1;
				}
			}
			if (newType == 1 && cellOwner(tmpLoc) == owner && addDelete == 1) { 
				if(!TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_ONE)) 
				 && !TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_TWO))
				 && TileHasWonder(tmpLoc) == 0) {	
					if (TType == TerrainDB(TERRAIN_FOREST) 
					 || TType == TerrainDB(TERRAIN_JUNGLE) 
					 || TType == TerrainDB(TERRAIN_SWAMP)
					 || TType == TerrainDB(TERRAIN_PLAINS) 
					 || TType == TerrainDB(TERRAIN_GRASSLAND) 
					 || TType == TerrainDB(TERRAIN_DESERT) 
					 || TType == TerrainDB(TERRAIN_TUNDRA)
					 || TType == TerrainDB(TERRAIN_HILL)
					 || TType == TerrainDB(TERRAIN_BROWN_HILL)) {
						Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_ONE),0 );
						return 1;	
					}
				}
			}
			if (newType > 1 && cellOwner(tmpLoc) == owner && addDelete == 1) {
				if (TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_ONE))) {
					Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_TWO),0 );
					return 1;	
				}
			}
			if (newType > 0 && cellOwner(tmpLoc) == owner && addDelete == 1)	{	
				if (TType == TerrainDB(TERRAIN_WATER_DEEP)
				 || TType == TerrainDB(TERRAIN_WATER_TRENCH) 
				 || TType == TerrainDB(TERRAIN_WATER_RIFT)) {
					Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_SEA_CITY_ONE),0 );
					return 1;	
				}
			}
		} 
		if (HasAdvance(owner, ID_ADVANCE_AGRICULTURE) 
		&& HasAdvance(owner, ID_ADVANCE_FEUDALISM)
		&& HasAdvance(owner, ID_ADVANCE_MASS_MEDIA)
		&& HasAdvance(owner, ID_ADVANCE_GAIA_THEORY)
		&& HasAdvance(owner, ID_ADVANCE_FUTURE_TECHNOLOGY)) {
			if (replaceOld < 2 && cellOwner(tmpLoc) == owner) {
				if (TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_ONE))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MEDIEVAL_CITY_ONE))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_ANCIENT_CITY_DEAD))
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_ONE))	
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_TWO))	
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_DEAD))	
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_ONE))	
				 || TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_DEAD))) {
					Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_ONE),0 );
					replaceOld = replaceOld + 1;
				} 
				elseif (TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_GENETIC_CITY_TWO))
				|| TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_MODERN_CITY_THREE))) {
					Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_TWO),0 );
					replaceOld = replaceOld + 1;
				}
			}
			if (cellOwner(tmpLoc) == owner && addDelete == 2) { 
				if(newType == 1 && TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_ONE))){
					Event: CutImprovements(tmpLoc);
					return 1;
				}
				elseif(newType > 1 && TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_TWO))){
					Event: CutImprovements(tmpLoc);
					return 1;
				}
			}
			if (newType == 1 && cellOwner(tmpLoc) == owner && addDelete == 1) { 
				if(!TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_ONE)) 
				 && !TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_TWO))
				 && TileHasWonder(tmpLoc) == 0) {	
					if (TType == TerrainDB(TERRAIN_FOREST) 
					 || TType == TerrainDB(TERRAIN_JUNGLE) 
					 || TType == TerrainDB(TERRAIN_SWAMP)
					 || TType == TerrainDB(TERRAIN_PLAINS) 
					 || TType == TerrainDB(TERRAIN_GRASSLAND) 
					 || TType == TerrainDB(TERRAIN_DESERT) 
					 || TType == TerrainDB(TERRAIN_TUNDRA)
					 || TType == TerrainDB(TERRAIN_HILL)
					 || TType == TerrainDB(TERRAIN_BROWN_HILL)) {
						Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_ONE),0 );
						return 1;	
					}
				}
			}
			if (newType > 1 && cellOwner(tmpLoc) == owner && addDelete == 1) {
				if (TileHasImprovement(tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_ONE))) {	
					Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_FUTURE_CITY_TWO),0 );
					return 1;	
				}
			}
			if (newType > 0 && cellOwner(tmpLoc) == owner && addDelete == 1) {	
				if (TType == TerrainDB(TERRAIN_WATER_DEEP)
				 || TType == TerrainDB(TERRAIN_WATER_TRENCH) 
				 || TType == TerrainDB(TERRAIN_WATER_RIFT)) {	
					Event:CreateImprovement(owner,tmpLoc, TerrainImprovementDB(TILEIMP_SEA_CITY_ONE),0 );
					return 1;	
				}
			}
		}
		GetNeighbor(tmpLoc, searchDirection, tmp2Loc);
		tmpLoc = tmp2Loc;
		count = count - 1;
	}
	return 0;
}

int_f ExpandCity(int_t owner, location_t cityLoc, int_t cityRadius, int_t tileType, int_t addDelete) {
location_t	tmpLoc;
location_t	tmp2Loc;
int_t		i;
int_t		j;
int_t		k;
int_t		sucess;
int_t		newType;
int_t		searchRadius;
int_t		addDeleteYN;
int_t		ownerNum;

	if (FAI_INIT == 1000)
	{
		searchRadius = 1;
		sucess = 0;
		newType = tileType;
		addDeleteYN = addDelete;
		ownerNum = owner;

		j = random(6);
		if (j == 0 || j == 5 || j == 2 || j == 7) {
			j = 6;
		}
		while (searchRadius <= cityRadius && sucess == 0) {
			tmpLoc = cityLoc;
			for (k = 0; k < searchRadius; k = k + 1) {		
				GetNeighbor(tmpLoc, j, tmp2Loc);
				tmpLoc = tmp2Loc;
				sucess = PlaceCity(ownerNum, tmpLoc, searchRadius, j, newType, addDeleteYN);
			}
			i = j + 1;
			while (i != j && sucess == 0) {
				tmpLoc = cityLoc;
				for (k = 0; k < searchRadius; k = k + 1) {		
					GetNeighbor(tmpLoc, i, tmp2Loc);
					tmpLoc = tmp2Loc;
					sucess = PlaceCity(ownerNum, tmpLoc, searchRadius, j, newType, addDeleteYN);
				}
				i = i + 1;
				if (i == 8) {
					i = 0; 
				}
			}
			searchRadius = searchRadius + 1;
		}	
		return sucess;		
	} else {
		return 0;
	}
}

HandleEvent(MakePop) 'expanding city' post {
city_t	theCity;
int_t		sucess;

	if (CityIsValid(city[0]))
	{
		ReplaceOld = 0;
		theCity = city[0];
		sucess = 1;
		if (theCity.population == 6) {
			sucess = ExpandCity(theCity.owner, theCity.location, 1, 1, 1);
		} elseif (theCity.population == 9) {
			sucess = ExpandCity(theCity.owner, theCity.location, 1, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 1, 2, 1);
			}
		} elseif (theCity.population == 12) {
			sucess = ExpandCity(theCity.owner, theCity.location, 2, 1, 1);
		} elseif (theCity.population == 15) {
			sucess = ExpandCity(theCity.owner, theCity.location, 2, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 2, 2, 1);
			}
		} elseif (theCity.population == 18) {
			sucess = ExpandCity(theCity.owner, theCity.location, 3, 1, 1);
		} elseif (theCity.population == 21) {
			sucess = ExpandCity(theCity.owner, theCity.location, 3, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 3, 2, 1);
			}
		} elseif (theCity.population == 24) {
			sucess = ExpandCity(theCity.owner, theCity.location, 3, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 3, 2, 1);
				ExpandCity(theCity.owner, theCity.location, 3, 2, 1);
				ExpandCity(theCity.owner, theCity.location, 3, 3, 1);
			}
		} elseif (theCity.population == 27) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 1);
		} elseif (theCity.population == 30) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
			}
		} elseif (theCity.population == 33) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
				ExpandCity(theCity.owner, theCity.location, 4, 3, 1);
			}
		} elseif (theCity.population == 36) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
			}
		} elseif (theCity.population == 39) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 1);
		} elseif (theCity.population == 42) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
			}
		} elseif (theCity.population == 45) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
				ExpandCity(theCity.owner, theCity.location, 4, 3, 1);
			}
		} elseif (theCity.population == 50) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
			}
		} elseif (theCity.population == 55) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
				ExpandCity(theCity.owner, theCity.location, 4, 3, 1);
			}
		} elseif (theCity.population == 60) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 1);
			if (sucess == 1) {
				ExpandCity(theCity.owner, theCity.location, 4, 2, 1);
			}
		}
		if (sucess == 0) {
			DontReduce1 = 1;
			Event: KillPop(theCity);
		}
	}
}

void_f ReduceCity (city_t redCity) {
city_t	theCity;
int_t		sucess;

	theCity = redCity;
	sucess = 1;

	if (theCity.population == 5) {
		sucess = ExpandCity(theCity.owner, theCity.location, 1, 1, 2);
	} elseif (theCity.population == 8) {
		sucess = ExpandCity(theCity.owner, theCity.location, 2, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 2, 1, 2);
		}
	} elseif (theCity.population == 11) {
		sucess = ExpandCity(theCity.owner, theCity.location, 3, 1, 2);
	} elseif (theCity.population == 14) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
		}
	} elseif (theCity.population == 17) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
	} elseif (theCity.population == 20) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
		}
	} elseif (theCity.population == 23) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 3, 2);
		if (sucess == 0) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
			if (sucess == 0) {
				ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
			}			
		}
	} elseif (theCity.population == 26) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
	} elseif (theCity.population == 29) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
		}
	} elseif (theCity.population == 32) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 3, 2);
		if (sucess == 0) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
			if (sucess == 0) {
				ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
			}			
		}
	} elseif (theCity.population == 35) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
		}
	} elseif (theCity.population == 38) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
	} elseif (theCity.population == 41) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
		}
	} elseif (theCity.population == 44) {
		sucess = ExpandCity(theCity.owner, theCity.location, 3, 3, 2);
		if (sucess == 0) {
			sucess = ExpandCity(theCity.owner, theCity.location, 3, 2, 2);
			if (sucess == 0) {
				ExpandCity(theCity.owner, theCity.location, 3, 1, 2);
			}			
		}
	} elseif (theCity.population == 49) {
		sucess = ExpandCity(theCity.owner, theCity.location, 2, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 2, 1, 2);
		}
	} elseif (theCity.population == 54) {
		sucess = ExpandCity(theCity.owner, theCity.location, 1, 3, 2);
		if (sucess == 0) {
			sucess = ExpandCity(theCity.owner, theCity.location, 1, 2, 2);
			if (sucess == 0) {
				ExpandCity(theCity.owner, theCity.location, 1, 1, 2);
			}			
		}
	} elseif (theCity.population == 59) {
		sucess = ExpandCity(theCity.owner, theCity.location, 2, 3, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 2, 2, 2);
		}
	}
}



void_f ReduceAllCities (city_t redCity) {
city_t	theCity;
int_t		sucess;

	theCity = redCity;
	sucess = 1;

	if (theCity.population >= 5) {
		sucess = ExpandCity(theCity.owner, theCity.location, 1, 1, 2);
	} if (theCity.population >= 8) {
		sucess = ExpandCity(theCity.owner, theCity.location, 2, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 2, 1, 2);
		}
	} if (theCity.population >= 11) {
		sucess = ExpandCity(theCity.owner, theCity.location, 3, 1, 2);
	} if (theCity.population >= 14) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
		}
	} if (theCity.population >= 17) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
	} if (theCity.population >= 20) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
		}
	} if (theCity.population >= 23) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 3, 2);
		if (sucess == 0) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
			if (sucess == 0) {
				ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
			}			
		}
	} if (theCity.population >= 26) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
	} if (theCity.population >= 29) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
		}
	} if (theCity.population >= 32) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 3, 2);
		if (sucess == 0) {
			sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
			if (sucess == 0) {
				ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
			}			
		}
	} if (theCity.population >= 35) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
		}
	} if (theCity.population >= 38) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
	} if (theCity.population >= 41) {
		sucess = ExpandCity(theCity.owner, theCity.location, 4, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 4, 1, 2);
		}
	} if (theCity.population >= 44) {
		sucess = ExpandCity(theCity.owner, theCity.location, 3, 3, 2);
		if (sucess == 0) {
			sucess = ExpandCity(theCity.owner, theCity.location, 3, 2, 2);
			if (sucess == 0) {
				ExpandCity(theCity.owner, theCity.location, 3, 1, 2);
			}			
		}
	} if (theCity.population >= 49) {
		sucess = ExpandCity(theCity.owner, theCity.location, 2, 2, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 2, 1, 2);
		}
	} if (theCity.population >= 54) {
		sucess = ExpandCity(theCity.owner, theCity.location, 1, 3, 2);
		if (sucess == 0) {
			sucess = ExpandCity(theCity.owner, theCity.location, 1, 2, 2);
			if (sucess == 0) {
				ExpandCity(theCity.owner, theCity.location, 1, 1, 2);
			}			
		}
	} if (theCity.population >= 59) {
		sucess = ExpandCity(theCity.owner, theCity.location, 2, 3, 2);
		if (sucess == 0) {
			ExpandCity(theCity.owner, theCity.location, 2, 2, 2);
		}
	}
}


HandleEvent(KillPop) 'reducing city' post {
city_t tmpCity;
	city[0] = tmpCity;
	if (DontReduce1 == 0) {
		ReduceCity (city[0]);
	} else {
		DontReduce1 = 0;
	}
}

HandleEvent(SlaveRaidCity) 'slave raid' post {
city_t tmpCity;
	city[0] = tmpCity;
	ReduceCity (city[0]);
}

HandleEvent(UndergroundRailwayUnit) 'free slaves' post {
city_t tmpCity;
	city[0] = tmpCity;
	ReduceCity (city[0]);
}

HandleEvent(PlagueCity) 'city plagued' pre {
city_t tmpCity;
	city[0] = tmpCity;
	ReduceCity (city[0]);
}

HandleEvent(CreateUnit) 'settler built' post {
unit_t tmpUnit;
city_t tmpCity;
	city[0] = tmpCity;
	unit[0] = tmpUnit;
	if ((unit[0].type == UnitDB(UNIT_SETTLER))	// if a unit is one of those listed here
	 ||(unit[0].type == UnitDB(UNIT_URBAN_PLANNER))	// ...
	 ||(unit[0].type == UnitDB(UNIT_SEA_ENGINEER))
	 ||(unit[0].type == UnitDB(UNIT_ARCHER))
	 ||(unit[0].type == UnitDB(UNIT_BOWMAN))
	 ||(unit[0].type == UnitDB(UNIT_LONGBOWMAN))
	 ||(unit[0].type == UnitDB(UNIT_LIGHT_CAVALRY))
	 ||(unit[0].type == UnitDB(UNIT_HEAVY_CAVALRY))
	 ||(unit[0].type == UnitDB(UNIT_KNIGHT))
	 ||(unit[0].type == UnitDB(UNIT_CAVALRY))
	 ||(unit[0].type == UnitDB(UNIT_SPEARMAN))
	 ||(unit[0].type == UnitDB(UNIT_WARRIOR))
	 ||(unit[0].type == UnitDB(UNIT_HOPLITE))
	 ||(unit[0].type == UnitDB(UNIT_PIKEMEN))
	 ||(unit[0].type == UnitDB(UNIT_INFANTRYMAN))
	 ||(unit[0].type == UnitDB(UNIT_MACHINE_GUNNER))
	 ||(unit[0].type == UnitDB(UNIT_MARINE))
	 ||(unit[0].type == UnitDB(UNIT_MECH_WARRIOR))
	 ||(unit[0].type == UnitDB(UNIT_HOVER_INFANTRY))
	 ||(unit[0].type == UnitDB(UNIT_ZULU_WARRIOR))
	 ||(unit[0].type == UnitDB(UNIT_CHARIOT))
	 ||(unit[0].type == UnitDB(UNIT_WARRIOR))
	 ||(unit[0].type == UnitDB(UNIT_HEAVY_WARRIOR))
	 ||(unit[0].type == UnitDB(UNIT_LEGION))
	 ||(unit[0].type == UnitDB(UNIT_ELEPHANT))
	 ||(unit[0].type == UnitDB(UNIT_BERSERKER))
	 ||(unit[0].type == UnitDB(UNIT_SAMURAI))
	 ||(unit[0].type == UnitDB(UNIT_TEUTONIC_KNIGHT))
	 ||(unit[0].type == UnitDB(UNIT_ARQUEBUSIER))
	 ||(unit[0].type == UnitDB(UNIT_MUSKETEER))
	 ||(unit[0].type == UnitDB(UNIT_RIFLEMAN))
	 ||(unit[0].type == UnitDB(UNIT_FASCIST))
	 ||(unit[0].type == UnitDB(UNIT_MECHANIZED_INFANTRY))
	 ||(unit[0].type == UnitDB(UNIT_PARATROOPER))
	 ||(unit[0].type == UnitDB(UNIT_WAR_WALKER))
	 ||(unit[0].type == UnitDB(UNIT_MECH_TROOPER))
	 ||(unit[0].type == UnitDB(UNIT_RANGER))
	 ||(unit[0].type == UnitDB(UNIT_SWARM))) {	//...
		ReduceCity(city[0]);
	}
}

HandleEvent(NukeCity) 'city nuked' pre {
city_t tmpCity;
	city[0] = tmpCity;
	ReduceAllCities (city[0]);
}

HandleEvent(KillCity) 'city destroyed' pre {
city_t tmpCity;
	city[0] = tmpCity;
	ReduceAllCities (city[0]);
}

HandleEvent(CreateParkUnit) 'park created' pre {
city_t tmpCity;
	city[0] = tmpCity;
	ReduceAllCities (city[0]);
}

void_f IWW_DoGreatWall(city_t	walledcity)	// from IW's Wonder Code
{
	location_t	CityLoc;
	location_t	tmpLoc;
	int_t	tmpPlayer;
	int_T	theWall;
	int_T	rand;
	int_t	TType;
	tmpPlayer = walledcity.owner;
	CityLoc = walledcity.location;
	int_T	i;
	int_t	j;
	int_T	check;
	check = 1;

	for(j=4; j < 7; j = j + 1){		// Make sure there are no wonders in the Wall gaps.
		if(j != 5){
			getNeighbor(CityLoc, j, tmpLoc);
			if(TileHasWonder(tmpLoc)== 1){	// PEDRUNN: Didnt like the way IW checked the Improvements
				check = 0;
			}
		}
	}
	if(check > 0){
		theWall = TerrainImprovementDB(TILEIMP_WONDER_GREAT_WALL_A);
		GetNeighbor(CityLoc, 6, tmpLoc);
		TType = TerrainType(tmpLoc);
		if (TType == TerrainDB(TERRAIN_GRASSLAND)	// PEDRUNN: more types of terrains.
		 || TType == TerrainDB(TERRAIN_PLAINS)		// I dont like the idea of terraform
		 || TType == TerrainDB(TERRAIN_DESERT)		// mountains in the early ages
		 || TType == TerrainDB(TERRAIN_TUNDRA)
		 || TType == TerrainDB(TERRAIN_GLACIER)
		 || TType == TerrainDB(TERRAIN_HILL)
		 || TType == TerrainDB(TERRAIN_BROWN_HILL) 
		 || TType == TerrainDB(TERRAIN_WHITE_HILL) 
		 || TType == TerrainDB(TERRAIN_FOREST) 
		 || TType == TerrainDB(TERRAIN_JUNGLE)
		 || TType == TerrainDB(TERRAIN_MOUNTAIN)
		 || TType == TerrainDB(TERRAIN_BROWN_MOUNTAIN)
		 || TType == TerrainDB(TERRAIN_WHITE_MOUNTAIN)	
		 || TType == TerrainDB(TERRAIN_SWAMP)){
			Event:CreateImprovement(tmpPlayer, tmpLoc, theWall, 0);	
		} else {		// PEDRUNN: what about deep sea cities???
			rand = random(2);
			if(rand == 0){
				terraform(tmpLoc, TerrainDB(TERRAIN_PLAINS));
				Event:CreateImprovement(tmpPlayer, tmpLoc, theWall, 0);	
			} else {
				terraform(tmpLoc, TerrainDB(TERRAIN_GRASSLAND));
				Event:CreateImprovement(tmpPlayer, tmpLoc, theWall, 0);	
			}
		}

		theWall = TerrainImprovementDB(TILEIMP_WONDER_GREAT_WALL_B);
		GetNeighbor(CityLoc, 4, tmpLoc);
		TType = TerrainType(tmpLoc);
		if (TType == TerrainDB(TERRAIN_GRASSLAND)
		 || TType == TerrainDB(TERRAIN_PLAINS) 
		 || TType == TerrainDB(TERRAIN_DESERT)
		 || TType == TerrainDB(TERRAIN_TUNDRA)
		 || TType == TerrainDB(TERRAIN_GLACIER)
		 || TType == TerrainDB(TERRAIN_HILL)
		 || TType == TerrainDB(TERRAIN_BROWN_HILL) 
		 || TType == TerrainDB(TERRAIN_WHITE_HILL) 
		 || TType == TerrainDB(TERRAIN_FOREST) 
		 || TType == TerrainDB(TERRAIN_JUNGLE)
		 || TType == TerrainDB(TERRAIN_MOUNTAIN)
		 || TType == TerrainDB(TERRAIN_BROWN_MOUNTAIN)
		 || TType == TerrainDB(TERRAIN_WHITE_MOUNTAIN)
		 || TType == TerrainDB(TERRAIN_SWAMP)){
			Event:CreateImprovement(tmpPlayer, tmpLoc, theWall, 0);	
		} else {
			rand = random(2);
			if(rand == 0){
				terraform(tmpLoc, TerrainDB(TERRAIN_PLAINS));
				Event:CreateImprovement(tmpPlayer, tmpLoc, theWall, 0);	
			} else {
				terraform(tmpLoc, TerrainDB(TERRAIN_GRASSLAND));
				Event:CreateImprovement(tmpPlayer, tmpLoc, theWall, 0);	
			}
		}
	}
}

HandleEvent(CreateWonder) 'WonderCreation' pre { // or should it be CreateWonder(city_t, int_t) event?
city_t theCity;
int_t	tmpStructure;

theCity = city[0];
tmpStructure = value[0].value;
	if(CityIsValid(city[0])) {
		int_t owner;
		location_t cityLoc;
		int_t cityRadius;
		int_t tmpWonder;
		int_t sucess;
		sucess = 1;
		owner = city[0].owner;
		CityLoc = city[0].location;
		cityRadius = 4;		// The biggest city type. although i should do this right it isnt that important
		if(tmpStructure == WonderDB(WONDER_PYRAMIDS)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_PYRAMIDS);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3); 
		} 
		elseif(tmpStructure == WonderDB(WONDER_HANGING_GARDENS)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_HANGING_GARDENS);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3); 
		} 
		elseif(tmpStructure == WonderDB(WONDER_CHICHEN_ITZA)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_CHICHEN_ITZA);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_LIGHTHOUSE)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_LIGHTHOUSE);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_COLOSSUS)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_COLOSSUS);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_COLISEUM)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_COLISEUM);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_OLYMPICS)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_OLYMPICS);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		}
		elseif(tmpStructure == WonderDB(WONDER_GREAT_LIBRARY)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_GREAT_LIBRARY);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		}
		elseif(tmpStructure == WonderDB(WONDER_THE_lABYRINTH)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_THE_lABYRINTH);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		}
		elseif(tmpStructure == WonderDB(WONDER_THE_FORBIDDEN_CITY)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_THE_FORBIDDEN_CITY);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_HAGIA_SOPHIA)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_HAGIA_SOPHIA);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_ANGKOR_WAT)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_ANGKOR_WAT);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		}
		elseif(tmpStructure == WonderDB(WONDER_THE_VATICAN)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_THE_VATICAN);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_DOME_OF_THE_ROCK)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_DOME_OF_THE_ROCK);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_TAJ_MAHAL)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_TAJ_MAHAL);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		}
		elseif(tmpStructure == WonderDB(WONDER_EAST_INDIA_COMPANY)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_EAST_INDIA_COMPANY);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_COPERNICUS_OBSERVATORY)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_COPERNICUS_OBSERVATORY);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_EMANCIPATION_PROCLAMATION)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_EMANCIPATION_PROCLAMATION);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_HOLY_INQUISITION)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_HOLY_INQUISITION);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_ADAM_SMITH_COMPANY)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_ADAM_SMITH_COMPANY);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_DISNEYLAND)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_DISNEYLAND);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_EMPIRE_STATE_BUILDING)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_EMPIRE_STATE_BUILDING);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_HOLLYWOOD)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_HOLLYWOOD);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_PASTEUR_INSTITUTE)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_PASTEUR_INSTITUTE);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_APOLLO_PROGRAM)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_APOLLO_PROGRAM);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_GLOBESAT)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_GLOBESAT);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_GENOME_PROJECT)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_GENOME_PROJECT);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_THE_AGENCY)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_THE_AGENCY);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_UNITED_NATIONS)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_UNITED_NATIONS);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_FIELD_DYNAMICS_LABORATORY)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_FIELD_DYNAMICS_LABORATORY);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_NATIONAL_SHIELD)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_NATIONAL_SHIELD);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_DATA_HAVEN)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_DATA_HAVEN);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_NANITE_DEFUSER)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_NANITE_DEFUSER);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_DINOSAUR_PARK)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_DINOSAUR_PARK);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_CENTRAL_MATTER_DECOMPILER)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_CENTRAL_MATTER_DECOMPILER);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_GLOBAL_SURVEILLANCE_CENTER)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_GLOBAL_SURVEILLANCE_CENTER);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_THE_SOLARIS_PROJECT)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_THE_SOLARIS_PROJECT);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		} 
		elseif(tmpStructure == WonderDB(WONDER_AI_ENTITY)){
			tmpWonder = TerrainImprovementDB(TILEIMP_WONDER_AI_ENTITY);
			sucess = ExpandCity(owner, cityLoc, cityRadius, tmpWonder, 3);
		}
		elseif(tmpStructure == WonderDB(WONDER_GREAT_WALL)){
			IWW_DoGreatWall(city[0]);
		}
	}
}
