All pastes #78258 Raw Edit

zespri

public lua v1 · immutable
·published 2006-07-03 23:09 UTC
rendered paste body
-- This function return reagent count and reagen name for a given button. If a button doean't contain spell,-- which dpends on a reagent it returns 0, nil. The function goes through tooltip for the action button-- and looks if the action requires a reagent. If yes, the functions check begs to find out the total-- number of this reagent availiable and reutns result.function SpellMats_GetActionCount(pagedid)	-- Sometimes this function failed on load because SpellMatsTooltip wasn't initialized yet	if (not SpellMatsTooltip) then		return 0;	end;	-- I have no idea I but DAB seem somehow to screw up 'this' piointer when reacting on the	-- SetAction event. I don't really understand the nature of this occurance, nor I have time	-- to investigate why it's happening. So I just preserve the value before, and then restore it	-- after	local oldThis = this;	SpellMatsTooltip:SetOwner(SpellMatsTooltip, "ANCHOR_NONE");	SpellMatsTooltip:SetAction(pagedid);	this = oldThis;	-- for all tooltip lines	for index = 1, 30, 1 do		field = getglobal("SpellMatsTooltipTextLeft"..index);		if( field ) then			text = field:GetText();			if( text ) then				-- this is supposed to be locale independent				if (string.sub(text, 1, string.len(SPELL_REAGENTS)) == SPELL_REAGENTS) then					local iStart, iEnd, reagent;					local result = nil;					local index = 0;					local reagentsString = "," .. string.sub(text,string.len(SPELL_REAGENTS),string.len(text));					local resultReagent;					-- Intention of this loop is to select reagent with minimum count if a spell require					-- more then one reagent. In reality there are no spells that require more then one					-- reagen as of now, so it's kind of useless					repeat						-- more precise regex not always work for specal caracters such as						-- umlauts and accents. this one seems to work fine always						iStart, iEnd, reagent = string.find(reagentsString,",%s*(.*)",index);						if (iEnd) then							-- If player ran out of the reagaent it will be displayed in red,							-- so we need to strip out the colour codes							_,_,reagent_ = string.find(reagent,"|c%x+(.*)|r",index);							reagent = reagent_ or reagent;							index = iEnd + 1;							-- Here if we don't have a record for the reagent yet, we create one							if (not SpellMats_Data.Reagents[reagent]) then								SpellMats_Data.Reagents[reagent] = {};								SpellMats_Data.Reagents[reagent].yellow = SpellMats_YellowDefault;								SpellMats_Data.Reagents[reagent].red = SpellMats_RedDefault;							end			                                -- Get reagent count from bags			                                local count = SpellMats_GetReagentCount(reagent);							-- Select minimum value							if ((not result) or (count < result)) then								resultReagent = reagent;								result = count;							end;						end;					until not reagent;					if (not result) then						result = 0;					end;					return result, resultReagent;				end			end		end	end	return 0;end