note
	description: "[
		Represents one of the air force attack units in VITP.
		]"
	author: "Jimmy J. Johnson"

deferred class
	AMPHIBIOUS_UNIT

inherit

	ATTACK_UNIT
		redefine
			landable_ports,
			arrival_location,
--			arrival_position,
			internal_is_movable
		end

feature -- Access

	arrival_location: LOCATION
			-- The port at which this unit may enter the game
		do
			if is_returning_unit then
				if nationality = game.japanese then
					Result := game.uncommitted_japanese_location
				else
					Result := game.uncommitted_allied_location
				end
			else
				Result := Precursor
			end
		end

--	arrival_position: like position
--			-- The location (latitude/longitude-like coordinates) where Current
--			-- will enter the game
--		do
--			if nationality = game.japanese then
--				Result := [300, 70]
--			else
--				Result := [680, 300]
--			end
--		end

	patrollable_sea_areas: LINKED_SET [SEA_AREA]
			-- A list containing each {SEA_AREA} which Current can patrol
			-- from its `home_port' at this point in the game
		local
			s: LINEAR [SEA_AREA]
			s2: LINEAR [SEA_AREA]
			a: SEA_AREA
		do
			create Result.make
			if attached {MOVE_AMPHIBIOUS_STEP} game.sequence_of_play then
					-- Current can go to any sea area adjacent to its `home_port'
				check attached home_port as p then
					s := p.adjacent_sea_areas
					from s.start
					until s.after
					loop
						a := s.item
						Result.extend (a)
							-- It can also go to a second sea area if first is not enemy's
						if not a.is_enemy_controlled (nationality) then
							s2 := a.adjacent_sea_areas
							from s2.start
							until s2.exhausted
							loop
								Result.extend (s2.item)
								s2.forth
							end
						end
						s.forth
					end
				end
			end
		end

	raidable_sea_areas: LINKED_SET [SEA_AREA]
			-- A list containing each {SEA_AREA} which Current can raid
			-- from its `home_port' at this point in the game
		local
			s: like patrollable_sea_areas
		do
			create Result.make
		end

	landable_ports: LINKED_SET [PORT]
			-- A list containing each {PORT} at which Current can
			-- make a landing at this point in the game
		local
			lin: LINEAR [PORT]
		do
io.put_string ("AMPHIBIOUS_UNIT.landable_ports:  fix to account for retreats--rule 13.4 %N")
			create Result.make
--			if game.stage = {GAME_SEQUENCE}.Landing_amphibious or
--				game.stage = {GAME_SEQUENCE}.Post_combat_amphibious_landing or
--				game.stage = {GAME_SEQUENCE}.End_of_turn_amphibious_landing then
--				if attached {SEA_AREA} location as s then
--					lin := s.adjoining_ports
--					from lin.start
--					until lin.exhausted
--					loop
--						Result.extend (lin.item)
--						lin.forth
--					end
--				end
--			end
		end

feature -- Status report

	is_sunk: BOOLEAN
			-- Is this unit sunk?
		do
			Result := (is_at_sea or is_in_port) and damage > defense_factor
		end

	is_patrollable: BOOLEAN = False
			-- Can this unit be a patroller for area control purposes?

	is_raidable: BOOLEAN = False
			-- Can this unit be a raider?

	is_patrolling: BOOLEAN = False
			-- Is this unit a patroller (as opposed to a raider)?

	is_raiding: BOOLEAN = False
			-- Is this unit a raider?

feature -- Query

	is_incompatible_position (a_position: VITP_POSITION): BOOLEAN
			-- Is `a_position' located on a unit with which Current is NOT
			-- allowed to `join_force' (e.g. on a submarine or an enemy)?
		local
			tf: TASK_FORCE
			u_list: VITP_TABLE [ATTACK_UNIT]
			au: ATTACK_UNIT
		do
			if not is_in_game then
				Result := false
			elseif game.i_boat.contains_position (a_position) or game.f_boat.contains_position (a_position) then
				Result := true
			else
				if nationality = game.japanese then
					u_list := game.allied_attack_units
				else
					u_list := game.japanese_attack_units
				end
				from u_list.start
				until u_list.after or else Result
				loop
					au := u_list.item_for_iteration
					Result := au.contains_position (a_position)
					u_list.forth
				end
			end
				-- Temp for testing
			Result := false
		end

feature {NONE} -- Implementation

	internal_can_patrol: BOOLEAN = False
			-- Can this game piece be a patroller?	

	internal_is_movable: BOOLEAN
			-- Can Current (not the `task_force') move at this time?
		local
			sop: VITP_SEQUENCE_OF_PLAY
		do
			sop := game.sequence_of_play
			Result := is_in_game and not (is_sunk or is_bottomed) and then
						((sop.is_allied_player and is_allied) or
						(sop.is_japanese_player and is_japanese)) and then
						((sop.is_moving_amphibious_step))
			if Result and sop.turn = 1 and sop.is_allied_player then
					-- Only some allied ships can move on turn 1
				Result := (Current = game.de_ruyter or Current = game.exeter or
						 Current = game.houston or Current = game.australia or
						 Current = game.canberra)
			end
		end


end
