WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   "Sorting" coordinates? (https://www.wowinterface.com/forums/showthread.php?t=50294)

mekasha 10-26-14 12:01 PM

"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.

semlar 10-26-14 12:16 PM

Quote:

Originally Posted by mekasha (Post 299018)
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)

Mazzop 10-26-14 12:52 PM

nvm, semlar edited :)

mekasha 10-26-14 06:09 PM

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?

Lombra 10-26-14 06:39 PM

I don't know how much of this you already figured out, but

Quote:

Originally Posted by mekasha (Post 299033)
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)

Quote:

Originally Posted by mekasha (Post 299033)
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.

Quote:

Originally Posted by mekasha (Post 299033)
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.


All times are GMT -6. The time now is 07:12 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI