Page 1 of 1

_M:hasWeapon()

Posted: Mon Feb 18, 2013 6:34 am
by lukep
Make a generic function that returns the character's weapon (or unarmed combat). It's a pain having to do it manually. For bonus points, let it take arguments like "staff", "dual", or "twohanded" as input, and have it return "wrong type" (or something) if it is the wrong type.

Re: _M:hasWeapon()

Posted: Tue Feb 19, 2013 6:21 pm
by nate
I don't understand. If you've done it once, what's keeping you from using the one time you've done it as a function?

Re: _M:hasWeapon()

Posted: Tue Feb 19, 2013 11:16 pm
by lukep
It would be much more accessible if it was included in the main code base.

If I were to make that function, it would probably be within one of the files (probably a talent one) in one of my addons, meaning that I would copy/paste it to the other files, and other people could not use it easily.

Re: _M:hasWeapon()

Posted: Sat Feb 23, 2013 2:49 am
by aardvark
How's this?

Code: Select all

do
	local weapon_classes = {
		onehanded = {waraxe = true, dagger = true, mace = true, mindstar = true, longsword = true, whip = true},
		twohanded = {battleaxe = true, greatmaul = true, greatsword = true, trident = true, staff = true},
		melee     = {waraxe = true, dagger = true, mace = true, mindstar = true, longsword = true, whip = true,
		             battleaxe = true, greatmaul = true, greatsword = true, trident = true, staff = true},
		ranged    = {bow = true, sling = true},
		warrior   = {waraxe = true, battleaxe = true, mace = true,
		             greatmaul = true, longsword = true, greatsword = true},
		exotic    = {trident = true, whip = true},
		axe       = {waraxe = true, battleaxe = true},
		blunt     = {mace = true, greatmaul = true},
		sword     = {longsword = true, greatsword = true},
		dagger    = {dagger = true},
		shield    = {shield = false},	-- a shield is a weapon sometimes, but not here
		any       = setmetatable({}, {__index = function (t, idx) return type(idx) == "string" and idx ~= "shield" end})
	}
	weapon_classes.missile    = weapon_classes.ranged	-- synonyms
	weapon_classes.projectile = weapon_classes.ranged
	weapon_classes.knife      = weapon_classes.dagger

	local retmeta = {
		__len = function (t)
			local i = 0
			for _ in pairs(t) do
				i = i + 1
			end
			return i
		end
	}

	function _M:hasWeapon(weapon_type)
		local hands = {self.INVEN_MAINHAND, self.INVEN_OFFHAND, self.INVEN_PSIONIC_FOCUS}
		local ret, match
		if weapon_type then
			ret = setmetatable({}, retmeta)
			match = weapon_classes[weapon_type] or {[weapon_type] = true}
		end

		for i = 1, #hands do
			local hand = hands[i]
			local inven = self:getInven(hand)
			if inven and inven[1] then
				if not weapon_type then
					return true
				elseif match[inven[1].subtype] then
					ret[hand] = {inven = inven, weapon = inven[1]}
				end
			end
		end

		return ret or false
	end
end
Returns straight true/false when called without arguments, or a table containing the inventory(-ies) holding the type passed as a string argument. You can also pass classes of weapons to match multiple types, including the special "any" class when you don't really care about the type but still want the table returned.

I haven't tested this, but it's a starting point, anyway. Superload this, or your preferred modification, into Actor.lua and/or lobby darkgod to pull it into trunk.

Re: _M:hasWeapon()

Posted: Sat Feb 23, 2013 5:27 pm
by Hachem_Muche
A simpler form of this function would be:

Code: Select all

function _M:hasweapon(self,Iid,filter)
	if Iid then
		for i, o in ipairs(self:getInven(Iid)) do
			if game.zone:checkFilter(o,filter or {},"object") then return o end
		end
	else
		-- check MAINHAND, OFFHAND, and PSIONIC_FOCUS in order
		for k, inv in ipairs({self.INVEN_MAINHAND, self.INVEN_OFFHAND, self.INVEN_PSIONIC_FOCUS}) do
			local o = self:hasweapon(inv,filter)
			if o then return o end
		end
	end
end
This returns the first weapon it finds that matches the filter or nil if no weapon(s) are found.
So, for example, to find an off hand dagger you'd use:
w = game.player:hasweapon(game.player.INVEN_OFFHAND,{subtype="dagger"})
which would put the dagger object or nil in w.