//////////////////////////////////////////////////////////////////////////////////////////
//
// 1) Elite Unit Code
//
// 2) Refugee Code (pop flees)
//
//  by Chris Whitaker (Gedrin)
//
/////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////
//  1) Elite Unit Code      //    
/////////////////////////////


int_t unitOwner[];	// index is UnitDB(UNIT_name);		// outcommented by Wouter Snijders (2001-5-22), already declared in file AS_SLC_Wouter.slc.bak
			// value is PlayerCivilization(owner) iff >-1
			// value is anyCiv iff =-1
			// value is noCiv iff <-1
int_t ANYCIV;		// -1 constant value representing a unit anyone can build.
int_t NOCIV;		// -2 constant value representing a unit no-one can build.

// run once to set units that can/cannot be built at startup.
HandleEvent(BeginTurn) 'BuildableUnitsSetup' Pre {
	int_t i;
	ANYCIV = -1;
	NOCIV = -2;
	// enable all units for anyCiv.  
	for(i = 0;i < 200;i = i + 1) {
		unitOwner[i] = ANYCIV;
	}

	// disable all unbuildable Units in this mod
	unitOwner[UnitDB(UNIT_ZULU_WARRIOR)] = NOCIV;
	unitOwner[UnitDB(UNIT_WARRIOR)] = NOCIV;
      unitOwner[UnitDB(UNIT_CHARIOT)] = NOCIV;
	unitOwner[UnitDB(UNIT_LEGION)] = NOCIV;
	unitOwner[UnitDB(UNIT_HEAVY_WARRIOR)] = NOCIV;
      unitOwner[UnitDB(UNIT_ELEPHANT)] = NOCIV;
	unitOwner[UnitDB(UNIT_SAMURAI)] = NOCIV;
	unitOwner[UnitDB(UNIT_BERSERKER)] = NOCIV;
      unitOwner[UnitDB(UNIT_TEUTONIC_KNIGHT)] = NOCIV;
      unitOwner[UnitDB(UNIT_INFANTRYMAN)] = NOCIV;
	unitOwner[UnitDB(UNIT_ARQUEBUSIER)] = NOCIV;
      unitOwner[UnitDB(UNIT_CULVERIN)] = NOCIV;
	unitOwner[UnitDB(UNIT_RIFLEMAN)] = NOCIV;
	unitOwner[UnitDB(UNIT_FASCIST)] = NOCIV;
	unitOwner[UnitDB(UNIT_MECHANIZED_INFANTRY)] = NOCIV;
      unitOwner[UnitDB(UNIT_PARATROOPER)] = NOCIV;
      unitOwner[UnitDB(UNIT_WAR_WALKER)] = NOCIV;
      unitOwner[UnitDB(UNIT_MECH_TROOPER)] = NOCIV;
      unitOwner[UnitDB(UNIT_SWARM)] = NOCIV;
      unitOwner[UnitDB(UNIT_RANGER)] = NOCIV;
	unitOwner[UnitDB(UNIT_BOMBER)] = NOCIV;
	unitOwner[UnitDB(UNIT_ATTACK_HELICOPTER)] = NOCIV;
      unitOwner[UnitDB(UNIT_JETMECH)] = NOCIV;
	unitOwner[UnitDB(UNIT_SPACE_BOMBER)] = NOCIV;
	
	DisableTrigger('BuildableUnitsSetup' );
}

int_f mod_CanCityBuildUnit(city_t theCity, int_t theUnit) {
	city_t  tmpCity;
	tmpCity = theCity;
	int_t   tmpUnit;
	tmpUnit = theUnit;
	if (unitOwner[tmpUnit] == ANYCIV) {
		return 1;		 // ANYCIV can build it
	} elseif  (PlayerCivilization(tmpCity.owner) == unitOwner[tmpUnit]) {
		return 1;	 	// this civ can build it
	} else {
		return 0;		 // this civ can't build it (and perhaps NOCIV can).
	}
}

///////////////////////////////
//  Elite Unit Trigger    
/////////////////////////////

// fixed by Wouter Snijders (2001-5-13)
HandleEvent(GrantAdvance) 'GrantEliteUnit' Post {
	// trigger off certain advances and grant the elite unit group if they are not already granted
	int_t advanceType;
	advanceType = value[0];
	advance[0] = advanceType;
	unitrecord[1] = UnitDB(UNIT_ARCHER);
	int_t tmpPlayer;
	tmpPlayer = player[0];
	tmpPlayer = PlayerCivilization(tmpPlayer);
      	if ((advanceType == AdvanceDB(ADVANCE_BRONZE_WORKING)) &&
	    (unitOwner[UnitDB(UNIT_ZULU_WARRIOR)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_WARRIOR)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_CHARIOT)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_ZULU_WARRIOR)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_ZULU_WARRIOR);
		Message(1, 'MM2_EliteMessage');

	} elseif ((advanceType == AdvanceDB(ADVANCE_BRONZE_WORKING)) &&
	    (unitOwner[UnitDB(UNIT_WARRIOR)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_ZULU_WARRIOR)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_CHARIOT)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_WARRIOR)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_WARRIOR);
		Message(1, 'MM2_EliteMessage');

	} elseif ((advanceType == AdvanceDB(ADVANCE_HORSE_RIDING)) &&
	    (unitOwner[UnitDB(UNIT_CHARIOT)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_WARRIOR)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_ZULU_WARRIOR)] != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_CHARIOT)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_CHARIOT);
		Message(1, 'MM2_EliteMessage');

	} elseif ((advanceType == AdvanceDB(ADVANCE_INFANTRY_TACTICS)) &&
	    (unitOwner[UnitDB(UNIT_LEGION)] == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_HEAVY_WARRIOR)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_ELEPHANT)] != tmpPlayer) &&
	    (Random(6) == 0)) {					
		unitOwner[UnitDB(UNIT_LEGION)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_LEGION);
		Message(1, 'MM2_EliteMessage');

	} elseif ((advanceType == AdvanceDB(ADVANCE_CITZENSHIP)) &&
	    (unitOwner[UnitDB(UNIT_HEAVY_WARRIOR)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_LEGION)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_ELEPHANT)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_HEAVY_WARRIOR)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_HEAVY_WARRIOR);
		Message(1, 'MM2_EliteMessage');

      } elseif ((advanceType == AdvanceDB(ADVANCE_STIRRUP)) &&
	    (unitOwner[UnitDB(UNIT_ELEPHANT)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_HEAVY_WARRIOR)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_LEGION)] != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_ELEPHANT)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_ELEPHANT);
		Message(1, 'MM2_EliteMessage');

	} elseif ((advanceType == AdvanceDB(ADVANCE_JOINERY)) &&
	    (unitOwner[UnitDB(UNIT_BERSERKER)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_SAMURAI)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_TEUTONIC_KNIGHT)] != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_BERSERKER)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_BERSERKER);
		Message(1, 'MM2_EliteMessage');

      } elseif ((advanceType == AdvanceDB(ADVANCE_FEUDALISM)) &&
	    (unitOwner[UnitDB(UNIT_SAMURAI)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_BERSERKER)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_TEUTONIC_KNIGHT)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_SAMURAI)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_SAMURAI);
		Message(1, 'MM2_EliteMessage');

      } elseif ((advanceType == AdvanceDB(ADVANCE_CAVALRY_TACTICS)) &&
	    (unitOwner[UnitDB(UNIT_TEUTONIC_KNIGHT)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_BERSERKER)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_SAMURAI)] != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_TEUTONIC_KNIGHT)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_TEUTONIC_KNIGHT);
		Message(1, 'MM2_EliteMessage');

 } elseif ((advanceType == AdvanceDB(ADVANCE_CONSCRIPTION)) &&
	    (unitOwner[UnitDB(UNIT_MUSKETEER)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_ARQUEBUSIER)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_CULVERIN)] != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_MUSKETEER)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_MUSKETEER);
		Message(1, 'MM2_EliteMessage');

 } elseif ((advanceType == AdvanceDB(ADVANCE_NATIONALISM)) &&
	    (unitOwner[UnitDB(UNIT_ARQUEBUSIER)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_MUSKETEER)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_CULVERIN)] != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_ARQUEBUSIER)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_ARQUEBUSIER);
		Message(1, 'MM2_EliteMessage');

 } elseif ((advanceType == AdvanceDB(ADVANCE_CANNON_MAKING)) &&
	    (unitOwner[UnitDB(UNIT_CULVERIN)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_ARQUEBUSIER)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_MUSKETEER)] != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_CULVERIN)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_CULVERIN);
		Message(1, 'MM2_EliteMessage');

} elseif ((advanceType == AdvanceDB(ADVANCE_CRIMINAL_CODE)) &&
	    (unitOwner[UnitDB(UNIT_FASCIST)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_RIFLEMAN)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_BOMBER)] != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_FASCIST)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_FASCIST);
		Message(1, 'MM2_EliteMessage');

 } elseif ((advanceType == AdvanceDB(ADVANCE_REPEATING_RIFLES)) &&
           (unitOwner[UnitDB(UNIT_RIFLEMAN)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_FASCIST)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_BOMBER)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
            unitOwner[UnitDB(UNIT_RIFLEMAN)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_RIFLEMAN);
		Message(1, 'MM2_EliteMessage');

} elseif ((advanceType == AdvanceDB(ADVANCE_AERODYNAMICS)) &&
          (unitOwner[UnitDB(UNIT_BOMBER)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_FASCIST)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_RIFLEMAN)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_BOMBER)]  = tmpPlayer;
		unitrecord[1] = UnitDB(UNIT_BOMBER);
		Message(1, 'MM2_EliteMessage');

} elseif ((advanceType == AdvanceDB(ADVANCE_ROBOTICS)) &&
	    (unitOwner[UnitDB(UNIT_PARATROOPER)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_MECHANIZED_INFANTRY)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_ATTACK_HELICOPTER)] != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_PARATROOPER)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_PARATROOPER);
		Message(1, 'MM2_EliteMessage');

 } elseif ((advanceType == AdvanceDB(ADVANCE_COMPUTER)) &&
           (unitOwner[UnitDB(UNIT_MECHANIZED_INFANTRY)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_ATTACK_HELICOPTER)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_PARATROOPER)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
            unitOwner[UnitDB(UNIT_MECHANIZED_INFANTRY)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_MECHANIZED_INFANTRY);
		Message(1, 'MM2_EliteMessage');

} elseif ((advanceType == AdvanceDB(ADVANCE_VERTICAL_FLIGHT_AIRCRAFT)) &&
          (unitOwner[UnitDB(UNIT_ATTACK_HELICOPTER)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_MECHANIZED_INFANTRY)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_PARATROOPER)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_ATTACK_HELICOPTER)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_ATTACK_HELICOPTER);
		Message(1, 'MM2_EliteMessage');

} elseif ((advanceType == AdvanceDB(ADVANCE_CYBERNETICS)) &&
          (unitOwner[UnitDB(UNIT_WAR_WALKER)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_MECH_TROOPER)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_JETMECH)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_WAR_WALKER)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_WAR_WALKER);
		Message(1, 'MM2_EliteMessage');

} elseif ((advanceType == AdvanceDB(ADVANCE_NANO_MACHINES)) &&
          (unitOwner[UnitDB(UNIT_MECH_TROOPER)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_WAR_WALKER)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_JETMECH)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_MECH_TROOPER)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_MECH_TROOPER);
		Message(1, 'MM2_EliteMessage');

} elseif ((advanceType == AdvanceDB(ADVANCE_NANO_MACHINES)) &&
          (unitOwner[UnitDB(UNIT_JETMECH)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_WAR_WALKER)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_MECH_TROOPER)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_JETMECH)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_JETMECH);
		Message(1, 'MM2_EliteMessage');

} elseif ((advanceType == AdvanceDB(ADVANCE_PLASMA_WEAPONRY)) &&
          (unitOwner[UnitDB(UNIT_SWARM)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_RANGER)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_SPACE_BOMBER)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_SWARM)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_SWARM);
		Message(1, 'MM2_EliteMessage');

} elseif ((advanceType == AdvanceDB(ADVANCE_QUANTUM_MECHANICS)) &&
          (unitOwner[UnitDB(UNIT_RANGER)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_SWARM)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_SPACE_BOMBER)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_RANGER)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_RANGER);
		Message(1, 'MM2_EliteMessage');

} elseif ((advanceType == AdvanceDB(ADVANCE_SMART_MATERIALS)) &&
          (unitOwner[UnitDB(UNIT_SPACE_BOMBER)]  == NOCIV) &&
	    (unitOwner[UnitDB(UNIT_SWARM)]  != tmpPlayer) &&
	    (unitOwner[UnitDB(UNIT_RANGER)]  != tmpPlayer) &&
	    (Random(6) == 0)) {
		unitOwner[UnitDB(UNIT_SPACE_BOMBER)]  = tmpPlayer;
		unitrecord[0] = UnitDB(UNIT_SPACE_BOMBER);
		Message(1, 'MM2_EliteMessage');
}
}


// added by Wouter Snijders (2001-5-13)
Messagebox 'MM2_EliteMessage' {
	Show();
	if (player[0] == 1) {
		if (unitrecord[1] == UnitDB(UNIT_ARCHER)) {
			Title(ID_GREAT_NEWS);
			Text(ID_ELITE_UNITS_US_1);
		} else {
			Title(ID_GREAT_NEWS);
			Text(ID_ELITE_UNITS_US_2);
		}
	} else {
		if (unitrecord[1] == UnitDB(UNIT_ARCHER)) {
			Title(ID_BAD_NEWS);
			Text(ID_ELITE_UNITS_THEM_1);
		} else {
			Title(ID_BAD_NEWS);
			Text(ID_ELITE_UNITS_THEM_2);
		}
	}
}

///////////////////////////////////////////////////////////////////
// 2) Refugee trigger for CTP2v1.1 by Chris Whitaker (Gedrin) //
///////////////////////////////////////////////////////////////////

messagebox 'POP_FLEES_MSG' {
	// POP_FLEES defined in info_str.txt or scen_str.txt wherever
	// and reads like:
	// POP_FLEES "Refugees flee in terror from {city[0].name}."
	Text(ID_POP_FLEES);
	Show();
}

// Before CaptureCity(city_t, int_t<player>, int_t) has run move
// at most 1/4 it's pop rounddown to cities of the "owning player".
HandleEvent(CaptureCity) 'Refugees' Pre {
	city_t destCity;
	destCity = city[0];
	int_t popCount;
	popCount = (destCity.population) / 4;
	// I do not exclude the captured city in case it is the last one.
	// Sure refugees can then 'flee' nowhere but why not? Who cares?
	// this also makes it less deterministic which is more fun IMO.
	// Oh and it is easier to code ;)
	int_t cityCount;
	cityCount = Cities(destCity.owner);
	int_t index;
	for (index = 0; index < popCount; index = index + 1) { // do this popCount times
		Event:KillPop(destCity); // kill a pop
	}
	Message (player[0], 'POP_FLEES_MSG');
	Message (city[0].owner, 'POP_FLEES_MSG');
	index = 0;
	while (index < popCount) {
		int_t cityIdx;
		cityIdx = Random(cityCount);
		GetCityByIndex(city[0].owner, cityIdx, destCity);
		AddPops(destCity, 1);
		index = index + 1;
	}
}