note
	description: "[
		This class represents a coordinate grid placed over the playing
		area around a particular `location'.  The grid contains cells 
		that an attack unit may occupy.  A cell may belong to one or more
		locations.  Only one unit may occupy a cell at any given time.
		
		This class is used to place an {ATTACK_UNIT_WIDGET} in at a given
		`location' without covering or overlapping some other widget at 
		that same location.
		]"
	author:    "Jimmy J. Johnson"
	date:      "4/11/24"
	copyright: "Copyright (c) 2024, Jimmy J. Johnson"
	license:   "Eiffel Forum v2 (http://www.eiffel.com/licensing/forum.txt)"


class
	VITP_GRID

feature {NONE} -- Initialization

	make (a_location: like location)
			-- Set up a grid of cells around Current
		do
			create sorted_cells.make (0)
			location := a_location
		end

feature -- Access

	location: LOCATION
			-- The location to which Current is attached.

feature -- Element change

	extend (a_cell: VITP_CELL)
			-- Add `a_cell' to this grid
		require
			not_has_cell: not has (a_cell)
		local
			d: INTEGER
			set: LINKED_SET [VITP_CELL]
		do
			d := manhattan_distance (a_cell)
			if attached cell_table.item (d) as s then
				set := s
			else
				create set
				cell_table.extend (set, d)
			end
			check
				not_in_set: not set.has (a_cell)
					-- because of precondition
			end
			set.extend (a_cell)
		ensure
			has_cell: has (a_cell)
		end

feature -- Query

	has (a_cell: VITP_CELL): BOOLEAN
			-- Does Current contain `a_cell' (i.e. is `a_cell', and
			-- its position, associated with this grid's `location')?
		local
			d: INTEGER
		do
			d := manhattan_distance (a_cell)
			Result := cell_table.has (d) and then
					cell_table.definite_item (d).has (a_cell)
		end

feature {NONE} -- Implementation

	manhattan_distance (a_cell: VITP_CELL): INTEGER
			-- The manhattan distance of `a_cell' from the position
			-- of the `location'
		local
			lat, long: INTEGER
		do
			lat := location.position.latitude
			long := location.position.longitude
			Result := (lat - a_cell.latitude).abs + (long - a_cell.longitude).abs
		end

	cell_table: HASH_TABLE [LINKED_SET [VITP_CELL], INTEGER]
			-- Table of cells where each cell is in a list that is indexed
			-- by the Manhattan_distance of that cell's `position' from the
			-- from the `position' of `location'

end
