stoneharry

Untitled

Aug 26th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.72 KB | None | 0 0
  1. --[[
  2. Status list:
  3. - CURRENT
  4. - REACHABLE
  5. - DISTANT
  6. - NONE
  7. ]]
  8. -- X, Y, Name, NumRoutes, Cost (100 = 1 silver), nodes accessable, npc name, status
  9. local coords_m = {
  10.     {0.5, 0.3, "Blackrock Mountain", 3, 250, {2, 3, 4}, "Lae Dualade", "NONE"},
  11.     {0.9, 0.3, "The Race Track", 1, 1050, {1}, "stuff3", "NONE"},
  12.     {0.52, 0.25, "Redridge", 1, 20, {1}, "Black War Gryphon", "NONE"},
  13.     {0.56, 0.28, "Badlands", 1, 25, {1}, "Gorrik", "NONE"}
  14.     --{0.9, 0.1, "Testing", 2, 100, {1, 3}, "stuff", "NONE"},
  15.     --{0.9, 0.2, "Test2", 3, 1000, {1, 2, 4}, "stuff2", "DISTANT"},
  16. }
  17. local current = 0
  18.  
  19. TAXI_MAP_WIDTH = 316;
  20. TAXI_MAP_HEIGHT = 352;
  21. NUM_TAXI_BUTTONS = 0;
  22. NUM_TAXI_ROUTES = 0;
  23.  
  24. TaxiButtonTypes = { };
  25. TaxiButtonTypes["CURRENT"] = {
  26.     file = "Interface\\TaxiFrame\\UI-Taxi-Icon-Green"
  27. }
  28. TaxiButtonTypes["REACHABLE"] = {
  29.     file = "Interface\\TaxiFrame\\UI-Taxi-Icon-White"
  30. }
  31. TaxiButtonTypes["DISTANT"] = {
  32.     file = "Interface\\TaxiFrame\\UI-Taxi-Icon-Yellow"
  33. }
  34.  
  35. TAXI_BUTTON_HALF_WIDTH = 8;
  36. TAXI_BUTTON_HALF_HEIGHT = 8;
  37.  
  38.  
  39. function TaxiFrame_OnLoad(self)
  40.     self:RegisterEvent("TAXIMAP_OPENED");
  41.     self:RegisterEvent("TAXIMAP_CLOSED");
  42. end
  43.  
  44. function TaxiFrame_OnEvent(self, event, ...)
  45.     if ( event == "TAXIMAP_OPENED" ) then
  46.         -- Show the merchant we're dealing with
  47.         local name = UnitName("npc")
  48.         TaxiMerchant:SetText(name);
  49.         SetPortraitTexture(TaxiPortrait, "npc");
  50.        
  51.         current = 0
  52.         for i=1, #coords_m do
  53.             coords_m[i][8] = "NONE";
  54.             if name == coords_m[i][7] then
  55.                 current = i
  56.                 coords_m[current][8] = "CURRENT";
  57.             end
  58.         end
  59.         if current == 0 then
  60.             UIErrorsFrame:AddMessage("ERROR: No path found for this creature.", 1.0, 0.1, 0.1, 1.0);
  61.             HideUIPanel(TaxiFrame);
  62.             return;
  63.         end
  64.         for k,v in pairs(coords_m[current][6]) do
  65.             coords_m[v][8] = "REACHABLE";
  66.         end
  67.  
  68.         -- Set the texture coords on the map
  69.         TaxiMap:SetTexCoord(0,1,0,1);
  70.         SetTaxiMap(TaxiMap);
  71.  
  72.         -- Show the taxi node map and buttons
  73.         local num_nodes = #coords_m
  74.         if ( num_nodes > NUM_TAXI_BUTTONS ) then
  75.             local button;
  76.             for i = NUM_TAXI_BUTTONS+1, num_nodes do
  77.                 button = CreateFrame("Button", "TaxiButton"..i, TaxiRouteMap, "TaxiButtonTemplate");
  78.                 button:SetID(i);
  79.             end
  80.         end
  81.        
  82.         -- Draw nodes
  83.         local taxiNodePositions = {};
  84.         local numValidFlightNodes = 0;
  85.         for index = 1, #coords_m do
  86.             local type = coords_m[index][8]
  87.             local button = _G["TaxiButton"..index];
  88.             taxiNodePositions[index] = {};
  89.             if ( type ~= "NONE" ) then
  90.                 numValidFlightNodes = numValidFlightNodes + 1;
  91.                 local x, y = coords_m[index][1], coords_m[index][2]
  92.                 local currX = x*TAXI_MAP_WIDTH;
  93.                 local currY = y*TAXI_MAP_HEIGHT;
  94.                 taxiNodePositions[index].x = currX;
  95.                 taxiNodePositions[index].y = currY;
  96.                 -- check if we are obscuring a previous placement (eg: Ebon Hold and Light's Hope Chapel)
  97.                 --[[if ( numValidFlightNodes > 1 ) then
  98.                     for checkNode = 1, index do
  99.                         local checkX = taxiNodePositions[checkNode].x;
  100.                         local checkY = taxiNodePositions[checkNode].y;
  101.                         if ( taxiNodePositions[checkNode].x ) then
  102.                             if ( (currX > checkX - TAXI_BUTTON_HALF_WIDTH) and (currX < checkX + TAXI_BUTTON_HALF_WIDTH) ) then
  103.                                 if ( (currY > checkY - TAXI_BUTTON_HALF_HEIGHT) and (currY < checkY + TAXI_BUTTON_HALF_HEIGHT) ) then
  104.                                     taxiNodePositions[index].x = currX + (currX - checkX) * 0.5;
  105.                                     taxiNodePositions[index].y = currY + (currY - checkY) * 0.5;
  106.                                     taxiNodePositions[checkNode].x = checkX + (checkX - currX) * 0.5;
  107.                                     taxiNodePositions[checkNode].y = checkY + (checkY - currY) * 0.5;
  108.                                 end
  109.                             end
  110.                         end
  111.                     end
  112.                 end]]
  113.                 -- set the button position
  114.                 button:ClearAllPoints();
  115.                 button:SetPoint("CENTER", "TaxiMap", "BOTTOMLEFT", taxiNodePositions[index].x, taxiNodePositions[index].y);
  116.                 button:SetNormalTexture(TaxiButtonTypes[type].file);
  117.                 button:Show();
  118.             else
  119.                 button:Hide();
  120.             end
  121.         end
  122.  
  123.         if ( num_nodes > NUM_TAXI_BUTTONS ) then
  124.             NUM_TAXI_BUTTONS = num_nodes
  125.         end
  126.  
  127.         -- All set...
  128.         ShowUIPanel(self);
  129.         if ( not self:IsShown() ) then
  130.             CloseTaxiMap();
  131.         end
  132.         return;
  133.     end
  134.     if ( event == "TAXIMAP_CLOSED" ) then
  135.         HideUIPanel(self);
  136.         return;
  137.     end
  138. end
  139.  
  140. function TaxiNodeOnButtonEnter(button)
  141.     local index = button:GetID();
  142.     GameTooltip:SetOwner(button, "ANCHOR_RIGHT");
  143.     GameTooltip:AddLine(coords_m[index][3], "", 1.0, 1.0, 1.0);
  144.    
  145.     -- Setup variables
  146.     local numRoutes = coords_m[current][4];
  147.     local line;
  148.     local sX, sY, dX, dY;
  149.     local w = TaxiRouteMap:GetWidth();
  150.     local h = TaxiRouteMap:GetHeight();
  151.    
  152.     local type = coords_m[index][8]
  153.     if ( type == "REACHABLE" ) then
  154.         SetTooltipMoney(GameTooltip, coords_m[index][5]);
  155.         TaxiNodeSetCurrent(index);
  156.        
  157.         if ( numRoutes > NUM_TAXI_ROUTES ) then
  158.             for i = NUM_TAXI_ROUTES+1, numRoutes do
  159.                 line = TaxiRouteMap:CreateTexture("TaxiRoute"..i, "BACKGROUND");
  160.                 line:SetTexture("Interface\\TaxiFrame\\UI-Taxi-Line");
  161.             end
  162.             NUM_TAXI_ROUTES = numRoutes;
  163.         end
  164.  
  165.         for i=1, NUM_TAXI_ROUTES do
  166.             line = _G["TaxiRoute"..i];
  167.             if ( i <= numRoutes ) then
  168.                 sX = coords_m[current][1]*w;
  169.                 sY = coords_m[current][2]*h;
  170.                 dX = coords_m[index][1]*w;
  171.                 dY = coords_m[index][2]*h;
  172.                 DrawRouteLine(line, "TaxiRouteMap", sX, sY, dX, dY, 32);
  173.                 line:Show();
  174.             else
  175.                 line:Hide();
  176.             end
  177.         end
  178.     elseif ( type == "CURRENT" ) then
  179.         GameTooltip:AddLine(TAXINODEYOUAREHERE, "", 0.5, 1.0, 0.5);
  180.         DrawOneHopLines();
  181.     end
  182.  
  183.     GameTooltip:Show();
  184. end
  185.  
  186. -- Draw all flightpaths within one hop of current location
  187. function DrawOneHopLines()
  188.     local line;
  189.     local sX, sY, dX, dY;
  190.     local w = TaxiRouteMap:GetWidth();
  191.     local h = TaxiRouteMap:GetHeight();
  192.     local numNodes = #coords_m
  193.     local numLines = 0;
  194.     local numSingleHops = 0;
  195.     for i=1, numNodes  do
  196.         --if ( GetNumRoutes(i) == 1 ) then
  197.             numSingleHops = numSingleHops + 1;
  198.             numLines = numLines + 1;
  199.             if ( numLines > NUM_TAXI_ROUTES ) then
  200.                 line = TaxiRouteMap:CreateTexture("TaxiRoute"..numLines, "BACKGROUND");
  201.                 line:SetTexture("Interface\\TaxiFrame\\UI-Taxi-Line");
  202.                 NUM_TAXI_ROUTES = numLines;
  203.             else
  204.                 line = _G["TaxiRoute"..numLines];
  205.             end
  206.             if ( line ) then
  207.                 sX = coords_m[current][1]*w;
  208.                 sY = coords_m[current][2]*h;
  209.                 dX = coords_m[current][1]*w;
  210.                 dY = coords_m[current][2]*h;
  211.                 DrawRouteLine(line, "TaxiRouteMap", sX, sY, dX, dY, 32);
  212.                 line:Show();
  213.             end
  214.         --end
  215.     end
  216.     for i=numLines+1, NUM_TAXI_ROUTES do
  217.         _G["TaxiRoute"..i]:Hide();
  218.     end
  219.     if ( numSingleHops == 0 ) then
  220.         UIErrorsFrame:AddMessage(ERR_TAXINOPATHS, 1.0, 0.1, 0.1, 1.0);
  221.         HideUIPanel(TaxiFrame);
  222.     end
  223. end
  224.  
  225.  
  226. -- The following function is used with permission from Daniel Stephens <[email protected]>
  227. TAXIROUTE_LINEFACTOR = 32/30; -- Multiplying factor for texture coordinates
  228. TAXIROUTE_LINEFACTOR_2 = TAXIROUTE_LINEFACTOR / 2; -- Half o that
  229.  
  230. -- T        - Texture
  231. -- C        - Canvas Frame (for anchoring)
  232. -- sx,sy    - Coordinate of start of line
  233. -- ex,ey    - Coordinate of end of line
  234. -- w        - Width of line
  235. -- relPoint - Relative point on canvas to interpret coords (Default BOTTOMLEFT)
  236. function DrawRouteLine(T, C, sx, sy, ex, ey, w, relPoint)
  237.    if (not relPoint) then relPoint = "BOTTOMLEFT"; end
  238.  
  239.    -- Determine dimensions and center point of line
  240.    local dx,dy = ex - sx, ey - sy;
  241.    local cx,cy = (sx + ex) / 2, (sy + ey) / 2;
  242.  
  243.    -- Normalize direction if necessary
  244.    if (dx < 0) then
  245.       dx,dy = -dx,-dy;
  246.    end
  247.  
  248.    -- Calculate actual length of line
  249.    local l = sqrt((dx * dx) + (dy * dy));
  250.  
  251.    -- Quick escape if it's zero length
  252.    if (l == 0) then
  253.       T:SetTexCoord(0,0,0,0,0,0,0,0);
  254.       T:SetPoint("BOTTOMLEFT", C, relPoint, cx,cy);
  255.       T:SetPoint("TOPRIGHT",   C, relPoint, cx,cy);
  256.       return;
  257.    end
  258.  
  259.    -- Sin and Cosine of rotation, and combination (for later)
  260.    local s,c = -dy / l, dx / l;
  261.    local sc = s * c;
  262.  
  263.    -- Calculate bounding box size and texture coordinates
  264.    local Bwid, Bhgt, BLx, BLy, TLx, TLy, TRx, TRy, BRx, BRy;
  265.    if (dy >= 0) then
  266.       Bwid = ((l * c) - (w * s)) * TAXIROUTE_LINEFACTOR_2;
  267.       Bhgt = ((w * c) - (l * s)) * TAXIROUTE_LINEFACTOR_2;
  268.       BLx, BLy, BRy = (w / l) * sc, s * s, (l / w) * sc;
  269.       BRx, TLx, TLy, TRx = 1 - BLy, BLy, 1 - BRy, 1 - BLx;
  270.       TRy = BRx;
  271.    else
  272.       Bwid = ((l * c) + (w * s)) * TAXIROUTE_LINEFACTOR_2;
  273.       Bhgt = ((w * c) + (l * s)) * TAXIROUTE_LINEFACTOR_2;
  274.       BLx, BLy, BRx = s * s, -(l / w) * sc, 1 + (w / l) * sc;
  275.       BRy, TLx, TLy, TRy = BLx, 1 - BRx, 1 - BLx, 1 - BLy;
  276.       TRx = TLy;
  277.    end
  278.  
  279.    -- Set texture coordinates and anchors
  280.    T:ClearAllPoints();
  281.    T:SetTexCoord(TLx, TLy, BLx, BLy, TRx, TRy, BRx, BRy);
  282.    T:SetPoint("BOTTOMLEFT", C, relPoint, cx - Bwid, cy - Bhgt);
  283.    T:SetPoint("TOPRIGHT",   C, relPoint, cx + Bwid, cy + Bhgt);
  284. end
Advertisement
Add Comment
Please, Sign In to add comment