--To manually re-generate and replace the current campaign mission. For contingency only, not required for normal campaign play.
--Initiated by RedoMission.bat
------------------------------------------------------------------------------------------------------- 
-- Miguel Fichier Revision  M35.b
-------------------------------------------------------------------------------------------------------
-- debug01 EndMission

-- Miguel21 modification M35.b version ScriptsMod
-- Miguel21 modification Mod14 : Versionning
-- Miguel21 modification Mod11.i : Multiplayer

local function AcceptMission()

		repeat
			print("\n\n Night or Day ? : "..daytime)								-- info day or not
			print("\n\nAccept Mission ?:")	
			
			print("a".." - Accept mission")
			print("s".." - Skip mission")
			
			m = tostring(io.read())
			m = string.lower(m)
			
		until m ~= nil and ( m == "a" or m == "s" )
		
		if  m == "s" then
			TaskRefused = true	
			
			return false
		else
			return true
		end
end

----- random seed -----
math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6)))
math.random(); math.random(); math.random()

versionPackageICM = os.getenv('versionPackageICM')														-- Miguel21 modification M35.b version ScriptsMod
	   
dofile("Init/conf_mod.lua")
dofile("Init/oob_air_init.lua")
dofile("../../../ScriptsMod."..versionPackageICM.."/UTIL_Functions.lua")

if versionPackageICM then
	print("= = = = = = = = = = = = = = = = = = = = Script: "..versionPackageICM.." = = = = = = = = = = = = = = = = = =") 
else 
	print("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
end			
	--===================================================================================
	-- Ecran N°0 Choix next campaign mission
print("Skip current mission and generate next campaign mission. Continue? y(es)/n(o):\n")				--ask for user confirmation

local input																								--user input
repeat
	input = string.lower(io.read())
	if input == "y" or input == "yes" or input == "n" or input == "no" then
		break
	else
		print("\n\nInvalid entry. Respond with y(es) or n(o):\n")
	end
until input == "y" or input == "yes" or input == "n" or input == "no"		

coopFlag01 = false
coopFlag02 = false
coop_flight2 = 0	

if input == "y" or input == "yes" then																	--user confirmed
	repeat																								--user input
		--===================================================================================
		-- Ecran N°1 Choix entre Single ou Multi player
	  print("Select :\ns Singleplayer  \nm Multiplayer (In test)")

		choix1 = io.read()
		choix1 = string.lower(choix1)
		
		--===================================================================================
			-- Ecran N°2 Selection Nombre d'avion Flight 1	
	   if choix1 == "m"  then
			type_flight_02 = "Nothing"
			coopFlag01 = true
			local u_base = ""

			for nSide , nUnit in pairs(oob_air) do
				for m , unit in pairs(nUnit) do
					if   unit.player == true then
						type_flight_01 = unit.type
						u_base = unit.base.." "..unit.name
						idType = m
						side = nSide
					end
				end
			end
			
			
			print("Select number of players on "..type_flight_01.." "..u_base..":\n1 - "..type_flight_01..":\n2 - "..type_flight_01.."\n3 - "..type_flight_01.."\n4 - "..type_flight_01.."\nq(uit)")
			input = tonumber(io.read())

			if  input >= 1 and  input <= 4 then
				coop = tonumber(input)
			else 
				print("\nInvalid entry.\n")
				break
			end

		--===================================================================================
			-- -- Ecran N°3 Selection Task du Flight 1
			-- print("Side: "..side.." Select Type of Task")
			-- print( "0 : Random ")
			-- task = {}
			-- ti = 1
			
			-- for taskStr , nbool in pairs(oob_air[side][idType].tasks) do
				-- if   nbool == true  then
					-- task[ti] = taskStr
					-- print( ti.." : "..tostring(taskStr))
					-- ti = ti+1
				-- end
			-- end
			
			-- input = tonumber(io.read())
			
			-- if  input >= 0 and  input <= ti then
				-- task_flight_01 =  tostring(task[input])
				-- if input == 0 then task_flight_01 = "Random"  end 				
				
			-- else 
				-- print("\nInvalid entry.\n")
				-- break
			-- end

		--===================================================================================
			-- Ecran N°4 Selection Type d'avion du Flight 2
			-- print(task_flight_01)
			print("Select Type of second playable plane:\n q(uit)")
			
			print( "0 : No 2nd Type: ")
			playable_type = {}
			ti = 1 
			for m , unit in pairs(oob_air["blue"]) do
				if   playable_m[unit.type] and unit.inactive ~= true and unit.player ~= true then
					playable_type[ti] = unit.type
					io.write( "\n".. ti.." (blue): "..unit.type..": " ..unit.base .." "..unit.name.."    ")
						for taskStr , nbool in pairs(oob_air["blue"][m].tasks) do
							if   nbool == true  then
								io.write( " ("..tostring(taskStr)..")")
							end
						end
					ti = ti+1
				end
			end
			for m , unit in pairs(oob_air["red"]) do
				if   playable_m[unit.type] and unit.inactive ~= true and unit.player ~= true then
					playable_type[ti] = unit.type
					io.write( "\n".. ti.." (red): "..unit.type..": " ..unit.base .." "..unit.name.."    ")
						for taskStr , nbool in pairs(oob_air["red"][m].tasks) do
							if   nbool == true  then
								io.write( " ("..tostring(taskStr)..")")
							end
						end
					ti = ti+1
				end
			end
			io.write( "\n")
			
			input = io.read()
			input = tonumber(input)

			if input <= (ti ) and input >= 1  then
				type_flight_02 = playable_type[input]
				coopFlag02 = true
			elseif input == 0 then
				type_flight_02 = "Nothing"
			else
				print("\nInvalid entry.\n")
				break
			end
		--===================================================================================
			-- Ecran N°5 Selection Nombre d'avion Flight 2
			if type_flight_02 ~= "Nothing" then
				print("Select number of players on "..type_flight_02.."\n1 - "..type_flight_02.."\n2 - "..type_flight_02.."\n3 - "..type_flight_02.."\n4 - "..type_flight_02.."\nq(uit)")
				input = io.read()
				input = tonumber(input)
				
				coop_flight2 = input
			end

		elseif choix1 == "s" then
		  coop = 1
		elseif choix1 == "q" or choix1 == "quit" then
			break
		else
			print("\nInvalid entry Choix1.\n")
			break
		end

			MissionInstance = 0
			print("\n\n")
			repeat
				print("Generating Next Mission.\n")
			
				MissionInstance = MissionInstance + 1															--count the number of times the mission is generated
				dofile("Active/camp_status.lua")
				camp.versionPackageICM = tostring(versionPackageICM)											-- Miguel21 modification M35 version ScriptsMod -- ajoute la version du script dans camp_status pour utilisation en fin de mission
								
				camp.coop = tonumber(coop)																		--set amount of players
				
				dofile("../../../ScriptsMod."..versionPackageICM.."/MAIN_NextMission.lua")																--generate mission
				
				if v_EndCampaign then 																			-- debug01.b EndMission
					if AcceptMission() then
						  print("\nEND OF THE CAMPAIGN, SEE THE BRIEFING IN THE MISSION..\n")					-- end of camapaign
						 break
					end
				
				elseif coopFlag02 and MultiPlayerFlight2 then 
					if AcceptMission() then
						 print("\nNext mission generated.\n")												--confirmation text
						 break
					end
				-- si multijFlight1 demandé et multijFlight1 confirmé, si multijFlight2 refusé, si  tacheFlight1 confirmé
				elseif coopFlag01 and MultiPlayerFlight1 and coopFlag02 ~= true  and taskFlag01  then					
					if AcceptMission() then
						 print("\nMultiplayerCampaign1 Next mission generated.\n")												--confirmation text
						 break
					end
				elseif camp.coop == 1 and PlayerFlight and coopFlag01 ~= true then								--mission has a player flight
					if AcceptMission() then
						print("\nNext mission generated.\n")												--confirmation text
						 break
					end
				elseif stopBug then																			--mission has a player flight
					print("\n\n stopBug .\n")																--confirmation text
					break
					
				elseif MissionInstance == 20 then																--no player flight could be assigned in 20 tries, stop it
					print("Mission Generation Error. No eligible player flight in 20 attempts. Try again.\n\n")
					break
				else																							--no player flight could be assigned, advance time and try again
					if playability_criterium.active_unit == nil then
						print("Player unit is not active.\n\n")
					elseif playability_criterium.base == nil then
						print("Player airbase is not operational.\n\n")
					elseif playability_criterium.ready_aircraft == nil then
						print("Player unit has no ready aircraft.\n\n")
					elseif playability_criterium.tot == nil then
						print("Player aircraft type cannot operate at this time of day.\n\n")
					elseif playability_criterium.target == nil then
						print("No eligible mission available for player.\n\n")
					elseif playability_criterium.target_firepower == nil then
						print("Not enough ready aircraft for this mission.\n\n")
					elseif playability_criterium.weather == nil then
						print("Player aircraft type cannot operate in this weather.\n\n")
					elseif playability_criterium.target_range == nil then
						print("No eligible mission available for player.\n\n")
					elseif playability_criterium.coop == nil then
						print("Not enough ready aircraft for all clients.\n\n")
					elseif MultiPlayerFlight1 == false then
						print("Not enough ready aircraft for all clients to 1st Flight.\n\n")
					elseif coopFlag02 == true and MultiPlayerFlight2 == false then
						print("Not enough ready aircraft for all clients to 2nd Flight.\n\n")
					elseif coopFlag01 == true  and type_flight_02 == "Nothing" then
						print("No eligible mission available for 1st Flight.\n\n")															   
					elseif playability_criterium.intercept == nil then
						print("Ground alert intercept duty without launch.\n\n")
					end
				end
			if versionPackageICM then
				print("= = = = = = = = = = = = = = = = = = = = Script: "..versionPackageICM.." = = = = = = = = = = = = = = = = = =") 
			else 
				print("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
			end
			until 1 == 2
			break
		until 1 == 2
end
os.execute 'pause'																					--pause command window for user to read text
os.exit()