diff Modules/Mover.lua @ 122:6724bc8eface

Reduced usage of global functions by defining them locally.
author Zerotorescue
date Sat, 15 Jan 2011 18:52:01 +0100
parents 00cf4fc1697f
children 84e38318f569
line wrap: on
line diff
--- a/Modules/Mover.lua	Sat Jan 15 17:09:13 2011 +0100
+++ b/Modules/Mover.lua	Sat Jan 15 18:52:01 2011 +0100
@@ -1,6 +1,10 @@
 local addon = select(2, ...);
 local mod = addon:NewModule("Mover", "AceEvent-3.0", "AceTimer-3.0");
 
+local _G = _G;
+local select, pairs = _G.select, _G.pairs;
+local tinsert, twipe, treverse, tsort, tremove = _G.table.insert, _G.table.wipe, _G.table.reverse, _G.table.sort, _G.table.remove;
+
 local Scanner;
 local queuedMoves = {}; -- table storing all queued moves before BeginMove is called
 local combinedMoves = {}; -- table storing all combined moves (with source and target) that is to be processed by the actual mover in the order of the index (1 to #)
@@ -58,7 +62,7 @@
 };
 
 function mod:AddMove(itemId, amount, numMissing, numAvailable, cost)
-	table.insert(queuedMoves, {
+	tinsert(queuedMoves, {
 		["itemId"] = itemId,
 		["num"] = amount, -- can not be unlimited
 		["missing"] = numMissing,
@@ -76,11 +80,11 @@
 end
 
 function mod:ResetQueue()
-	table.wipe(queuedMoves);
+	twipe(queuedMoves);
 end
 
-if not table.reverse then
-	table.reverse = function(orig)
+if not treverse then
+	treverse = function(orig)
 		local temp = {};
 		local origLength = #orig;
 		for i = 1, origLength do
@@ -106,7 +110,7 @@
 			local bagFamily = select(2, GetContainerNumFreeSlots(bagId));
 			
 			if not itemId then
-				table.insert(emptySlots, {
+				tinsert(emptySlots, {
 					["container"] = bagId,
 					["slot"] = slotId,
 					["family"] = bagFamily,
@@ -154,7 +158,7 @@
 			addon:Print(("Can't move %s, this doesn't exist in the source."):format(IdToItemLink(singleMove.itemId)), addon.Colors.Red);
 		else
 			-- We want to move the smallest stacks first to keep stuff pretty (and minimize space usage, splitting a stack takes 2 slots, moving something only 1)
-			table.sort(sourceItem.locations, function(a, b)
+			tsort(sourceItem.locations, function(a, b)
 				-- -1 indicates unlimited, this is always more than an actual amount
 				if a.count == -1 then
 					return false;
@@ -179,7 +183,7 @@
 						while movingNum > stackSize do
 							-- Move a single stack size while the amount remaining to be moved is above the stack size num
 							
-							table.insert(outgoingMoves, {
+							tinsert(outgoingMoves, {
 								["itemId"] = singleMove.itemId,
 								["num"] = stackSize,
 								["container"] = itemLocation.container,
@@ -192,7 +196,7 @@
 					end
 				end
 				
-				table.insert(outgoingMoves, {
+				tinsert(outgoingMoves, {
 					["itemId"] = singleMove.itemId,
 					["num"] = movingNum,
 					["container"] = itemLocation.container,
@@ -212,7 +216,7 @@
 	addon:Debug("%d outgoing moves are possible.", #outgoingMoves);
 	
 	-- No longer needed
-	table.wipe(queuedMoves);
+	twipe(queuedMoves);
 	
 	
 	
@@ -264,7 +268,7 @@
 					else
 						-- Consume empty slot
 						
-						table.insert(combinedMoves, {
+						tinsert(combinedMoves, {
 							["itemId"] = outgoingMove.itemId,
 							["num"] = outgoingMove.num,
 							["sourceContainer"] = outgoingMove.container,
@@ -279,7 +283,7 @@
 						itemMove:AddLocation(firstAvailableSlot.container, firstAvailableSlot.slot, outgoingMove.num);
 						targetContents[outgoingMove.itemId] = itemMove;
 						
-						table.remove(emptySlots, 1); -- no longer empty
+						tremove(emptySlots, 1); -- no longer empty
 						
 						outgoingMove.num = 0; -- nothing remaining - sanity check
 						outgoingMove.itemId = nil; -- remove this record from the outgoingMoves-table
@@ -289,7 +293,7 @@
 					local itemStackCount = select(8, GetItemInfo(outgoingMove.itemId));
 					
 					-- We want to move to the largest stacks first to keep stuff pretty
-					table.sort(targetItem.locations, function(a, b)
+					tsort(targetItem.locations, function(a, b)
 						return a.count > b.count;
 					end);
 					
@@ -302,7 +306,7 @@
 								-- Enough room to move this entire stack
 								-- Deposit this item and then forget this outgoing move as everything in it was processed
 								
-								table.insert(combinedMoves, {
+								tinsert(combinedMoves, {
 									["itemId"] = outgoingMove.itemId,
 									["num"] = outgoingMove.num,
 									["sourceContainer"] = outgoingMove.container,
@@ -318,7 +322,7 @@
 							else
 								-- Deposit this item but don't remove the outgoing move as there are some items left to move
 								
-								table.insert(combinedMoves, {
+								tinsert(combinedMoves, {
 									["itemId"] = outgoingMove.itemId,
 									["num"] = outgoingMove.num,
 									["sourceContainer"] = outgoingMove.container,
@@ -351,7 +355,7 @@
 			-- Check if the item id is nil, this is set to nil when this outgoing move has been processed
 			if not outgoingMoves[numOutgoingMoves].itemId or outgoingMoves[numOutgoingMoves].num == 0 then
 				-- Remove this element from the array
-				table.remove(outgoingMoves, numOutgoingMoves);
+				tremove(outgoingMoves, numOutgoingMoves);
 			end
 			
 			-- Proceed with the next element (or previous considering we're going from last to first)
@@ -362,7 +366,7 @@
 		
 		backup = (backup + 1);
 		if backup > 1000 then
-			table.wipe(outgoingMoves);
+			twipe(outgoingMoves);
 			self:Abort("mover crashed", "Error preparing moves, hit an endless loop");
 			onFinish();
 			return;
@@ -370,12 +374,12 @@
 	end
 	
 	-- Reverse table, we need to go through it from last to first because we'll be removing elements, but we don't want the actions to be executed in a different order
-	combinedMoves = table.reverse(combinedMoves);
+	combinedMoves = treverse(combinedMoves);
 
 	addon:Debug("%d moves should be possible.", #combinedMoves);
 	
 	-- No longer needed
-	table.wipe(emptySlots);
+	twipe(emptySlots);
 	
 	self:ProcessMove();
 	
@@ -409,7 +413,7 @@
 		MailAddonBusy = addon:GetName();
 		
 		-- Since mailbox indexes change as mail is emptied (emptied mail is automatically deleted, thus number 50 would become 49 when 1 disappears), we must start with the last mail first
-		table.sort(combinedMoves, function(a, b)
+		tsort(combinedMoves, function(a, b)
 			return a.sourceContainer < b.sourceContainer;
 		end);
 	end
@@ -482,7 +486,7 @@
 					targetLocationsLocked[move.targetContainer][move.targetSlot] = true;
 					
 					-- This move was processed
-					table.remove(combinedMoves, numCurrentMove);
+					tremove(combinedMoves, numCurrentMove);
 				else
 					self:Abort("item disappeared from mouse", "Couldn't move " .. IdToItemLink(move.itemId) .. ", CursorHasItem() is false");
 					return;
@@ -491,7 +495,7 @@
 				-- When items are deposit automatically we still need to remember when a move has been processed
 				
 				-- This move was processed
-				table.remove(combinedMoves, numCurrentMove);
+				tremove(combinedMoves, numCurrentMove);
 			end
 		end
 		
@@ -549,7 +553,7 @@
 	self:UnregisterEvent("UI_ERROR_MESSAGE");
 	
 	-- Reset vars
-	table.wipe(combinedMoves);
+	twipe(combinedMoves);
 	movesSource = nil;
 	if MailAddonBusy == addon:GetName() then
 		MailAddonBusy = nil;