_M:hasWeapon()

All development conversation and discussion takes place here

Moderator: Moderator

Post Reply
Message
Author
lukep
Sher'Tul Godslayer
Posts: 1712
Joined: Mon Mar 14, 2011 10:32 am
Location: Canada

_M:hasWeapon()

#1 Post 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.
Some of my tools for helping make talents:
Melee Talent Creator
Annotated Talent Code (incomplete)

nate
Wyrmic
Posts: 261
Joined: Fri Jan 18, 2013 8:35 am

Re: _M:hasWeapon()

#2 Post 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?
Proud father of Fx4fx and Chronometer add-ons; proud mother of Fated add-on

lukep
Sher'Tul Godslayer
Posts: 1712
Joined: Mon Mar 14, 2011 10:32 am
Location: Canada

Re: _M:hasWeapon()

#3 Post 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.
Some of my tools for helping make talents:
Melee Talent Creator
Annotated Talent Code (incomplete)

aardvark
Wyrmic
Posts: 200
Joined: Wed Aug 22, 2012 12:16 am

Re: _M:hasWeapon()

#4 Post 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.
Attachments
Actor.txt
Superload version
(1.9 KiB) Downloaded 174 times

Hachem_Muche
Uruivellas
Posts: 744
Joined: Thu Nov 18, 2010 6:42 pm

Re: _M:hasWeapon()

#5 Post 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.
Author of the Infinite 500 and PlenumTooltip addons, and the joys of Scaling in ToME.

Post Reply