Thread Tools Display Modes
10-26-14, 12:01 PM   #1
mekasha
A Defias Bandit
Join Date: Oct 2014
Posts: 2
"Sorting" coordinates?

Hi all,

This is my first foray into modding. I've put together a super simple mod that list that just prints out a list of the new rare mobs/one-time-loot world objects in WoD. Here's a sample of what I have so far:

Lua Code:
  1. if (((zoneName == "Frostfire Ridge") and msg == "") or msg == "ff") then
  2.         print("Items in Frostfire")
  3.    
  4.         if (IsQuestFlaggedCompleted(34132) == false) then
  5.         print("Scout Goreseeker[76.5, 63.5]")
  6.         end
  7.         if (IsQuestFlaggedCompleted(34931) == false) then
  8.         print("Pale Loot Sack[21.7, 50.8]")
  9.         end
  10.         if (IsQuestFlaggedCompleted(34133) == false) then
  11.         print("The Beater[26.9, 31.9]")
  12.         end
  13.          end

I have the above working for every zone using data I scraped from Wowpedia. My question is, is there any way to kind of sort the items based on a relative closeness to player position? I don't need to find "distance", I was just thinking something simple like ((player X coord - item X coord) or (item X coord - player X coord)) based on whichever is higher, then sorting based on the lower result.

Are there any examples of how I would store my values into an array/table and sort that, then write that out to the screen? I'm mainly an SQL guy, so I've been learning the basics of lua/non-scripted programming as I go.
  Reply With Quote
10-26-14, 12:16 PM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Originally Posted by mekasha View Post
My question is, is there any way to kind of sort the items based on a relative closeness to player position? I don't need to find "distance", I was just thinking something simple like ((player X coord - item X coord) or (item X coord - player X coord)) based on whichever is higher, then sorting based on the lower result.
Distance is pretty much exactly what you're trying to sort by.

If you have a table like
Lua Code:
  1. {
  2.   {34132, 76.5, 63.5},
  3.   {34931, 21.7, 50.8},
  4.   {34133, 26.9, 31.9},
  5. }
And you only want to sort by the X coordinate, you would get the player's coordinates and then sort the table..
Lua Code:
  1. local coords = {
  2.   {34132, 76.5, 63.5},
  3.   {34931, 21.7, 50.8},
  4.   {34133, 26.9, 31.9},
  5. }
  6. local playerX, playerY = GetPlayerMapPosition('player')
  7. table.sort(coords, function(a, b)
  8.   return (a[2] - playerX) < (b[2] - playerX)
  9. end)
For actual distance you'd sort like this
Lua Code:
  1. table.sort(coords, function(a, b)
  2.   return ((a[2] - playerX)^2 + (a[3] - playerY)^2) < ((b[2] - playerX)^2 + (b[3] - playerY)^2)
  3. end)

Last edited by semlar : 10-26-14 at 12:44 PM.
  Reply With Quote
10-26-14, 12:52 PM   #3
Mazzop
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 74
nvm, semlar edited
  Reply With Quote
10-26-14, 06:09 PM   #4
mekasha
A Defias Bandit
Join Date: Oct 2014
Posts: 2
That is quite helpful. I understand how the table.sort is working, and how you populate the initial table, but I'm having a lot of trouble adding subsequent items to the table.

I'm especially having trouble getting it to work since I'll have 4 values per line in the table, and almost every example I've come across just shows how to handle a key and single value.

I'm assuming that using my initial code, I'll be changing each Print to some method of inserting a new row into my table, then doing the sort at the end, then printing the sorted table.

edit: I think I have it working, here is my concept code:

Lua Code:
  1. local coords = {}
  2.  
  3. table.insert (coords , {34132, 70.5, 63.5, "scout"})
  4. table.insert (coords , {34931, 21.7, 50.8, "warrior"})
  5. table.insert (coords , {34133, 26.9, 31.9, "hunter"})
  6. table.insert (coords , {34134, 1000, 1000, "unknown location"})
  7.  
  8.  local playerX = 2
  9.  local playerY = 1
  10.  
  11.     table.sort(coords, function(a, b)
  12.       return ((a[2] - playerX)^2 + (a[3] - playerY)^2) > ((b[2] - playerX)^2 + (b[3] - playerY)^2)
  13.     end)
  14.  
  15.     for i,line in ipairs(coords) do
  16.       print(line[1], line[2], line[3], "npc", line[4], "near place")
  17.     end

Is the "for ipairs" the best way for printing out my sorted table? Also, for the items I do not have the coordinates for, is there a more elegant method than setting their coordinates to 1000, 1000?

Last edited by mekasha : 10-26-14 at 06:26 PM.
  Reply With Quote
10-26-14, 06:39 PM   #5
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
I don't know how much of this you already figured out, but

Originally Posted by mekasha View Post
I'm especially having trouble getting it to work since I'll have 4 values per line in the table, and almost every example I've come across just shows how to handle a key and single value.
You shouldn't be thinking of it like that. You have only one value, which is the table.

Code:
local value = {34132, 70.5, 63.5, "scout"}
table.insert(coords, value)
Originally Posted by mekasha View Post
Is the "for ipairs" the best way for printing out my sorted table?
If you're going to do stuff with a table very frequently there is another way of doing it than ipairs, but for a single or just a few iterations there's really no reason to complicate things. ipairs is fine.

Originally Posted by mekasha View Post
Also, for the items I do not have the coordinates for, is there a more elegant method than setting their coordinates to 1000, 1000?
Probably. Depends on how you want to treat those items.
__________________
Grab your sword and fight the Horde!
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » "Sorting" coordinates?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off