Iterating through target tables

All development conversation and discussion takes place here

Moderator: Moderator

Post Reply
Message
Author
Razakai
Uruivellas
Posts: 889
Joined: Tue May 14, 2013 3:45 pm

Iterating through target tables

#1 Post by Razakai »

I have an ability that's a spin on the usual 'make X random attacks against targets in an area'. Effectively this will fire up to X projectiles against targets in a cone, and if there are more projectiles than targets a target can be hit up to 3 times. However, currently it seems a little buggy and sometimes exceeds the number of projectiles. Is there a better way to write this?

Code: Select all

newTalent{
	name = "Fan of Knives",
	type = {"technique/throwing-knives", 2},
	require = techs_dex_req2,
	points = 5,
	tactical = { ATTACKAREA = 3 },
	getDamage = function (self, t) return self:combatTalentWeaponDamage(t, 0.6, 1.2) end,
	getNb = function(self, t) return math.floor(self:combatTalentScale(t, 4, 8)) end,
	range = 0,
	cooldown = 10,
	stamina = 20,
	radius = function(self, t) return math.floor(self:combatTalentScale(t, 3, 7)) end,
	target = function(self, t)
		return {type="cone", range=self:getTalentRange(t), friendlyfire=false, radius=self:getTalentRadius(t)}
	end,
	action = function(self, t)
		local tg = self:getTalentTarget(t)
		local x, y = self:getTarget(tg)
		if not x or not y then return end


		local nb = t.getNb(self,t)
		local count = t.getNb(self,t)
		
		local tgts = {}
		grids = self:project(tg, x, y, function(px, py)
			local target = game.level.map(px, py, engine.Map.ACTOR)
			if not target then return end
			tgts[#tgts+1] = target
		end)

		table.shuffle(tgts)
		
		while count > 0 do
			if #tgts <= 0 then break end
			for i = 1, math.min(nb, #tgts) do
				local a, id = tgts[i]
				local tg2 = {type="bolt", range=self:getTalentRadius(t), selffire=false, talent=t, display={display='', particle="arrow", particle_args={tile="shockbolt/object/knife_steel"} }}
					self:projectile(tg2, a.x, a.y, function(px, py, tg2, self)
						local target = game.level.map(px, py, engine.Map.ACTOR)
						if target and target ~= self then
							local t2 = self:getTalentFromId(self.T_THROWING_KNIVES)
							self:attackTargetWith(target, t2.getKnives(self, t), nil, t.getDamage(self,t))
							target.turn_procs.fan_of_knives = 1 + (target.turn_procs.fan_of_knives or 0)
							if target.turn_procs.fan_of_knives==3 then table.remove(tgts, id) end
						end
					end)
					count = count - 1
			end
		end



		return true
	end,
	info = function(self, t)
		return ([[Throw up to %d throwing knives at enemies within a radius %d cone, for %d%% damage each. If the number of knives exceeds the number of enemies, a target can be hit up to 3 times.]]):
		format(t.getNb(self,t), self:getTalentRadius(t), t.getDamage(self, t)*100)
	end,
}

HousePet
Perspiring Physicist
Posts: 6215
Joined: Sun Sep 09, 2012 7:43 am

Re: Iterating through target tables

#2 Post by HousePet »

Oh god, yes it can be better.
But I can't write it for you at the moment.
My feedback meter decays into coding. Give me feedback and I make mods.

Post Reply