Quantcast

Added Recipe_HasState(), Recipe_AddState(), Recipe_RemoveState() and Recipe_IsFlagged() - assigned as members of each recipe table to simplify flag/state handling.

James D. Callahan III [04-02-10 - 01:24]
Added Recipe_HasState(), Recipe_AddState(), Recipe_RemoveState() and Recipe_IsFlagged() - assigned as members of each recipe table to simplify flag/state handling.
Filename
ARL.lua
Frame.lua
Waypoint.lua
diff --git a/ARL.lua b/ARL.lua
index f61de15..5f97662 100644
--- a/ARL.lua
+++ b/ARL.lua
@@ -517,10 +517,8 @@ function addon:OnInitialize()
 				       if scanned then
 					       local skill_level = Player.professions[recipe_prof]
 					       local has_level = skill_level and (type(skill_level) == "boolean" and true or skill_level >= recipe.skill_level)
-					       local SF = private.recipe_state_flags
-					       local is_known = (bit.band(recipe.state, SF.KNOWN) == SF.KNOWN)

-					       if ((not is_known and has_level) or shifted) and Player:IsCorrectFaction(recipe) then
+					       if ((not recipe:HasState("KNOWN") and has_level) or shifted) and Player:IsCorrectFaction(recipe) then
 						       local _, _, _, hex = GetItemQualityColor(recipe.quality)

 						       self:AddLine(string.format("%s: %s%s|r (%d)", recipe.profession, hex, recipe.name, recipe.skill_level))
@@ -826,59 +824,116 @@ end
 -- Recipe DB Structures are defined in Documentation.lua
 -------------------------------------------------------------------------------

---- Adds a tradeskill recipe into the specified recipe database
--- @name AckisRecipeList:AddRecipe
--- @usage AckisRecipeList:AddRecipe(28927, 305, 23109, Q.UNCOMMON, V.TBC, 305, 305, 325, 345)
--- @param spell_id The [[http://www.wowwiki.com/SpellLink|Spell ID]] of the recipe being added to the database
--- @param skill_level The skill level at which the recipe can be initially learned
--- @param item_id The [[http://www.wowwiki.com/ItemLink|Item ID]] that is created by the recipe, or nil
--- @param quality The quality/rarity of the recipe
--- @param profession The profession ID that uses the recipe.  See [[API/database-documentation]] for a listing of profession IDs
--- @param specialty The specialty that uses the recipe (ie: goblin engineering) or nil or blank
--- @param genesis Game version that the recipe was first introduced in, for example, Original, BC, or WoTLK
--- @param optimal_level Level at which recipe is considered orange
--- @param medium_level Level at which recipe is considered yellow
--- @param easy_level Level at which recipe is considered green
--- @param trivial_level Level at which recipe is considered grey
--- @return None, array is passed as a reference
-function addon:AddRecipe(spell_id, skill_level, item_id, quality, profession, specialty, genesis, optimal_level, medium_level, easy_level, trivial_level)
-	local recipe_list = private.recipe_list
+do
+	local SF = private.recipe_state_flags
+
+	-------------------------------------------------------------------------------
+	-- Recipe member functions for bit flags.
+	-------------------------------------------------------------------------------
+	local function Recipe_HasState(self, state_name)
+		return self.state and (bit.band(self.state, SF[state_name]) == SF[state_name]) or false
+	end

-	if recipe_list[spell_id] then
-		--@alpha@
-		self:Print("Duplicate recipe: "..recipe_list[spell_id].profession.." "..tostring(spell_id).." "..recipe_list[spell_id].name)
-		--@end-alpha@
-		return
+	local function Recipe_AddState(self, state_name)
+		if not self.state then
+			self.state = 0
+		end
+
+		if bit.band(self.state, SF[state_name]) == SF[state_name] then
+			return
+		end
+		self.state = bit.bxor(self.state, SF[state_name])
 	end

-	local recipe = {
-		["spell_id"]		= spell_id,
-		["skill_level"]		= skill_level,
-		["item_id"]		= item_id,
-		["quality"]		= quality,
-		["profession"]		= GetSpellInfo(profession),
-		["spell_link"]		= GetSpellLink(spell_id),
-		["name"]		= GetSpellInfo(spell_id),
-		["flags"]		= {},
-		["acquire_data"]	= {},
-		["specialty"]		= specialty,			-- Assumption: there will only be 1 speciality for a trade skill
-		["genesis"]		= genesis,
-		["optimal_level"]	= optimal_level or skill_level,
-		["medium_level"]	= medium_level or skill_level + 10,
-		["easy_level"]		= easy_level or skill_level + 15,
-		["trivial_level"]	= trivial_level or skill_level + 20,
-		["state"]		= 0,				-- State flags.
+	local function Recipe_RemoveState(self, state_name)
+		if not self.state then
+			return
+		end
+
+		if bit.band(self.state, SF[state_name]) ~= SF[state_name] then
+			return
+		end
+		self.state = bit.bxor(self.state, SF[state_name])
+
+		if self.state == 0 then
+			self.state = nil
+		end
+	end
+
+	local BITFIELD_MAP = {
+		["common1"]	= private.common_flags_word1,
+		["class1"]	= private.class_flags_word1,
+		["reputation1"]	= private.rep_flags_word1,
+		["reputation2"]	= private.rep_flags_word2,
+		["item1"]	= private.item_flags_word1,
 	}
-	local SF = private.recipe_state_flags

-	-- Set the "relevant" flag for searches, until I peer at the search logic to make this unnecessary.
-	recipe.state = bit.bxor(recipe.state, SF.RELEVANT)
+	local function Recipe_IsFlagged(self, field_name, flag_name)
+		local bitfield = self[field_name]
+		local bitset = BITFIELD_MAP[field_name]
+		local value = bitset[flag_name]

-	if not recipe.name then
-		self:Print(strformat(L["SpellIDCache"], spell_id))
+		return bitfield and (bit.band(bitfield, value) == value) or false
 	end
-	recipe_list[spell_id] = recipe
-end
+
+	--- Adds a tradeskill recipe into the specified recipe database
+	-- @name AckisRecipeList:AddRecipe
+	-- @usage AckisRecipeList:AddRecipe(28927, 305, 23109, Q.UNCOMMON, V.TBC, 305, 305, 325, 345)
+	-- @param spell_id The [[http://www.wowwiki.com/SpellLink|Spell ID]] of the recipe being added to the database
+	-- @param skill_level The skill level at which the recipe can be initially learned
+	-- @param item_id The [[http://www.wowwiki.com/ItemLink|Item ID]] that is created by the recipe, or nil
+	-- @param quality The quality/rarity of the recipe
+	-- @param profession The profession ID that uses the recipe.  See [[API/database-documentation]] for a listing of profession IDs
+	-- @param specialty The specialty that uses the recipe (ie: goblin engineering) or nil or blank
+	-- @param genesis Game version that the recipe was first introduced in, for example, Original, BC, or WoTLK
+	-- @param optimal_level Level at which recipe is considered orange
+	-- @param medium_level Level at which recipe is considered yellow
+	-- @param easy_level Level at which recipe is considered green
+	-- @param trivial_level Level at which recipe is considered grey
+	-- @return None, array is passed as a reference
+	function addon:AddRecipe(spell_id, skill_level, item_id, quality, profession, specialty, genesis, optimal_level, medium_level, easy_level, trivial_level)
+		local recipe_list = private.recipe_list
+
+		if recipe_list[spell_id] then
+			--@alpha@
+			self:Print("Duplicate recipe: "..recipe_list[spell_id].profession.." "..tostring(spell_id).." "..recipe_list[spell_id].name)
+			--@end-alpha@
+			return
+		end
+
+		local recipe = {
+			["spell_id"]		= spell_id,
+			["skill_level"]		= skill_level,
+			["item_id"]		= item_id,
+			["quality"]		= quality,
+			["profession"]		= GetSpellInfo(profession),
+			["spell_link"]		= GetSpellLink(spell_id),
+			["name"]		= GetSpellInfo(spell_id),
+			["flags"]		= {},
+			["acquire_data"]	= {},
+			["specialty"]		= specialty,			-- Assumption: there will only be 1 speciality for a trade skill
+			["genesis"]		= genesis,
+			["optimal_level"]	= optimal_level or skill_level,
+			["medium_level"]	= medium_level or skill_level + 10,
+			["easy_level"]		= easy_level or skill_level + 15,
+			["trivial_level"]	= trivial_level or skill_level + 20,
+
+			-- Function members
+			["HasState"]		= Recipe_HasState,
+			["AddState"]		= Recipe_AddState,
+			["RemoveState"]		= Recipe_RemoveState,
+			["IsFlagged"]		= Recipe_IsFlagged,
+		}
+
+		-- Set the "relevant" flag for searches, until I peer at the search logic to make this unnecessary.
+		recipe:AddState("RELEVANT")
+
+		if not recipe.name then
+			self:Print(strformat(L["SpellIDCache"], spell_id))
+		end
+		recipe_list[spell_id] = recipe
+	end
+end	-- do

 --- Adds filtering flags to a specific tradeskill.
 -- @name AckisRecipeList:AddRecipeFlags
@@ -1583,7 +1638,7 @@ do

 		for recipe_id, recipe in pairs(recipe_list) do
 			if recipe.profession == current_profession then
-				local is_known = (bit.band(recipe.state, SF.KNOWN) == SF.KNOWN)
+				local is_known = recipe:HasState("KNOWN")

 				can_display = CanDisplayRecipe(recipe)
 				recipes_total = recipes_total + 1
@@ -1604,7 +1659,7 @@ do
 			else
 				can_display = false
 			end
-			local is_visible = (bit.band(recipe.state, SF.VISIBLE) == SF.VISIBLE)
+			local is_visible = recipe:HasState("VISIBLE")

 			if (can_display and not is_visible) or (not can_display and is_visible) then
 				recipe.state = bit.bxor(recipe.state, SF.VISIBLE)
@@ -1771,11 +1826,7 @@ do
 				local recipe = recipe_list[tonumber(SpellString)]

 				if recipe then
-					local is_known = (bit.band(recipe.state, SF.KNOWN) == SF.KNOWN)
-
-					if not is_known then
-						recipe.state = bit.bxor(recipe.state, SF.KNOWN)
-					end
+					recipe:AddState("KNOWN")
 					recipes_found = recipes_found + 1
 				else
 					self:Debug(tradeName .. " " .. SpellString .. L["MissingFromDB"])
@@ -1955,7 +2006,7 @@ do
 		for recipe_id in pairs(recipe_list) do
 			local recipe = recipe_list[recipe_id]
 			local recipe_prof = GetSpellInfo(recipe.profession)
-			local is_known = (bit.band(recipe.state, SF.KNOWN) == SF.KNOWN)
+			local is_known = recipe:HasState("KNOWN")

 			if recipe_prof == profession then
 				-- CSV
diff --git a/Frame.lua b/Frame.lua
index 7a5118a..e0b50c1 100644
--- a/Frame.lua
+++ b/Frame.lua
@@ -1336,9 +1336,7 @@ do
 		pattern = pattern:lower()

 		for index, entry in pairs(private.recipe_list) do
-			if bit.band(entry.state, SF.RELEVANT) == SF.RELEVANT then
-				entry.state = bit.bxor(entry.state, SF.RELEVANT)
-			end
+			entry:RemoveState("RELEVANT")

 			for location_name in pairs(location_list) do
 				local breakout = false
@@ -1416,11 +1414,7 @@ ARL_ClearButton:SetScript("OnClick",

 				  -- Reset the search flags
 				  for index in pairs(recipe_list) do
-					  local recipe = recipe_list[index]
-
-					  if bit.band(recipe.state, SF.RELEVANT) ~= SF.RELEVANT then
-						  recipe.state = bit.bxor(recipe.state, SF.RELEVANT)
-					  end
+					  recipe_list[index]:AddState("RELEVANT")
 				  end
 				  MainPanel.search_editbox:SetText(_G.SEARCH)

@@ -1497,11 +1491,7 @@ MainPanel.search_editbox:SetScript("OnTextSet",
 						   local recipe_list = private.recipe_list

 						   for spell_id in pairs(recipe_list) do
-							   local recipe = recipe_list[spell_id]
-
-							   if bit.band(recipe.state, SF.RELEVANT) ~= SF.RELEVANT then
-								   recipe.state = bit.bxor(recipe.state, SF.RELEVANT)
-							   end
+							   recipe_list[spell_id]:AddState("RELEVANT")
 						   end
 						   ARL_SearchButton:SetNormalFontObject("GameFontDisableSmall")
 						   ARL_SearchButton:Disable()
@@ -2892,10 +2882,8 @@ do
 				for spell_id, affiliation in pairs(private.acquire_list[acquire_type].recipes) do
 					local recipe = private.recipe_list[spell_id]
 					local can_display = HasCredentials(affiliation)
-					local is_visible = (bit.band(recipe.state, SF.VISIBLE) == SF.VISIBLE)
-					local is_relevant = (bit.band(recipe.state, SF.RELEVANT) == SF.RELEVANT)

-					if can_display and is_visible and is_relevant then
+					if can_display and recipe:HasState("VISIBLE") and recipe:HasState("RELEVANT") then
 						count = count + 1

 						if not recipe_registry[recipe] then
@@ -2931,10 +2919,8 @@ do
 				for spell_id, affiliation in pairs(private.location_list[loc_name].recipes) do
 					local recipe = private.recipe_list[spell_id]
 					local can_display = HasCredentials(affiliation)
-					local is_visible = (bit.band(recipe.state, SF.VISIBLE) == SF.VISIBLE)
-					local is_relevant = (bit.band(recipe.state, SF.RELEVANT) == SF.RELEVANT)

-					if can_display and is_visible and is_relevant then
+					if can_display and recipe:HasState("VISIBLE") and recipe:HasState("RELEVANT") then
 						count = count + 1

 						if not recipe_registry[recipe] then
@@ -2959,10 +2945,8 @@ do
 			for i = 1, #sorted_recipes do
 				local recipe_index = sorted_recipes[i]
 				local recipe = recipe_list[recipe_index]
-				local is_visible = (bit.band(recipe.state, SF.VISIBLE) == SF.VISIBLE)
-				local is_relevant = (bit.band(recipe.state, SF.RELEVANT) == SF.RELEVANT)

-				if is_visible and is_relevant then
+				if recipe:HasState("VISIBLE") and recipe:HasState("RELEVANT") then
 					local t = AcquireTable()

 					t.text = FormatRecipeText(recipe)
@@ -3455,10 +3439,8 @@ do
 				for spell_id, affiliation in pairs(private.acquire_list[acquire_id].recipes) do
 					local recipe_entry = private.recipe_list[spell_id]
 					local can_display = HasCredentials(affiliation)
-					local is_visible = (bit.band(recipe_entry.state, SF.VISIBLE) == SF.VISIBLE)
-					local is_relevant = (bit.band(recipe_entry.state, SF.RELEVANT) == SF.RELEVANT)

-					if can_display and is_visible and is_relevant then
+					if can_display and recipe_entry:HasState("VISIBLE") and recipe_entry:HasState("RELEVANT") then
 						local t = AcquireTable()
 						local expand = false
 						local type = "subheader"
@@ -3492,10 +3474,8 @@ do
 				for spell_id, affiliation in pairs(private.location_list[location_id].recipes) do
 					local recipe_entry = private.recipe_list[spell_id]
 					local can_display = HasCredentials(affiliation)
-					local is_visible = (bit.band(recipe_entry.state, SF.VISIBLE) == SF.VISIBLE)
-					local is_relevant = (bit.band(recipe_entry.state, SF.RELEVANT) == SF.RELEVANT)

-					if can_display and is_visible and is_relevant then
+					if can_display and recipe_entry:HasState("VISIBLE") and recipe_entry:HasState("RELEVANT") then
 						local expand = false
 						local type = "subheader"
 						local t = AcquireTable()
diff --git a/Waypoint.lua b/Waypoint.lua
index a57f8d0..80c2581 100644
--- a/Waypoint.lua
+++ b/Waypoint.lua
@@ -355,23 +355,17 @@ local function GetWaypoint(acquire_type, id_num, recipe)
 			waypoint = quest
 		end
 	elseif acquire_type == A.CUSTOM then
-		local bitfield = recipe.flags.common1
-		local COMMON1 = private.common_flags_word1
-
-		if bit.band(bitfield, COMMON1.TRAINER) == COMMON1.TRAINER and maptrainer then
-			waypoint = private.custom_list[id_num]
-		elseif bit.band(bitfield, COMMON1.VENDOR) == COMMON1.VENDOR and mapvendor then
-			waypoint = private.custom_list[id_num]
-		elseif bit.band(bitfield, COMMON1.QUEST) == COMMON1.QUEST and mapquest then
-			waypoint = private.custom_list[id_num]
-		elseif bit.band(bitfield, COMMON1.INSTANCE) == COMMON1.INSTANCE then
-			waypoint = private.custom_list[id_num]
-		elseif bit.band(bitfield, COMMON1.RAID) == COMMON1.RAID then
+		if recipe:IsFlagged("common1", "TRAINER") and maptrainer then
 			waypoint = private.custom_list[id_num]
-		elseif bit.band(bitfield, COMMON1.WORLD_DROP) == COMMON1.WORLD_DROP then
+		elseif recipe:IsFlagged("common1", "VENDOR") and mapvendor then
 			waypoint = private.custom_list[id_num]
-		elseif bit.band(bitfield, COMMON1.MOB_DROP) == COMMON1.MOB_DROP then
+		elseif recipe:IsFlagged("common1", "QUEST") and mapquest then
 			waypoint = private.custom_list[id_num]
+		elseif recipe:IsFlagged("common1", "INSTANCE") or
+			recipe:IsFlagged("common1", "RAID") or
+			recipe:IsFlagged("common1", "WORLD_DROP") or
+			recipe:IsFlagged("common1", "MOB_DROP") then
+				waypoint = private.custom_list[id_num]
 		end
 	end
 	return waypoint
@@ -438,10 +432,8 @@ function addon:SetupMap(single_recipe)
 		-- Scan through all recipes to display, and add the vendors to a list to get their acquire info
 		for i = 1, #sorted_recipes do
 			local recipe = recipe_list[sorted_recipes[i]]
-			local is_visible = (bit.band(recipe.state, SF.VISIBLE) == SF.VISIBLE)
-			local is_relevant = (bit.band(recipe.state, SF.RELEVANT) == SF.RELEVANT)

-			if is_visible and is_relevant then
+			if recipe:HasState("VISIBLE") and recipe:HasState("RELEVANT") then
 				for acquire_type, acquire_info in pairs(recipe.acquire_data) do
 					for id_num, id_info in pairs(acquire_info) do
 						if acquire_type == A.REPUTATION then