Page 1 of 1

Extend ActorProject:canProject

Posted: Tue Nov 30, 2010 2:04 am
by yufra
The target/project refactor that finally seems to be stable never got extended into the canProject function. I am proposing we have canProject additionally return the stop radius values (so right in front of a wall for example). Rush could benefit from this extension. The extended function would look like this:

Code: Select all

--- Can we project to this grid ?
-- @param t a type table describing the attack, passed to engine.Target:getType() for interpretation
-- @param x target coords
-- @param y target coords
function _M:canProject(t, x, y)
	local typ = Target:getType(t)
	typ.source_actor = self

	-- Stop at range or on block
	local lx, ly = x, y
	local stop_radius_x, stop_radius_y = self.x, self.y
	local l = line.new(self.x, self.y, x, y)
	lx, ly = l()
	while lx and ly do
		if typ.block_path and typ:block_path(lx, ly) then break end
		stop_radius_x, stop_radius_y = lx, ly

		lx, ly = l()
	end
	-- Ok if we are at the end reset lx and ly for the next code
	if not lx and not ly then lx, ly = x, y end

	-- Correct the explosion source position if we exploded on terrain
	local radius_x, radius_y
	if typ.block_path then
		_, radius_x, radius_y = typ:block_path(lx, ly)
	end
	if not radius_x then
		radius_x, radius_y = stop_radius_x, stop_radius_y
	end

	if lx == x and ly == y then return true, lx, ly end
	return false, lx, ly, radius_x, radius_y
end
The basic while loop etc is the same as in project, and maybe they should be consolidated into a single private function that gets called by both. Decreases readability, increases maintenance (assuming we ever change this code, doesn't require changing it in multiple places).

Re: Extend ActorProject:canProject

Posted: Fri Dec 03, 2010 12:08 am
by darkgod
done