azkanan Posted January 7, 2013 Author Share Posted January 7, 2013 SituationMy game's world is made of Areas. In these areas are locations. (ie, an area might be a city, where the locations are churches, houses, malls, etc).I need an algorithm that is used when trying to find the nearest locations. That is, if the player has started out with only knowing the location he is at, there is a button that the player can hit to search the immediate space about his location for new locations to travel to.Every location is assigned a random X variable and Y variable.As a result, I need an algorithm that will search within 3 "X,Y" spots in all directions of the player's location for any other locations, then I need to list these.[![](http://www.freemmorpgmaker.com/files/imagehost/pics/87c5ea5349cd954283e904cad9b44ced.png)](http://www.freemmorpgmaker.com/files/imagehost/#87c5ea5349cd954283e904cad9b44ced.png)**Where Black is the Point of Origin.****Where Blue is the search area.****Where Green are the found locations.****Where Red are the unfound locations.** Link to comment Share on other sites More sharing options...
azkanan Posted January 7, 2013 Author Share Posted January 7, 2013 This is what I've come up with so far…```Dim SearchXMin As IntegerDim SearchXMax As IntegerDim SearchYMin As IntegerDim SearchYMax As IntegerDim SearchX As IntegerDim SearchY As IntegerDim NewPlace As BooleanDim Index As IntegerDim Index2 As IntegerDim Index3 As IntegerCall FINDPLAYER 'Assigns VarArea and VarLocation public variables to the player's Area and Location.'Search ParametersSearchXMin = Area(VarArea).location(VarLocation).x - 3SearchXMax = Area(VarArea).location(VarLocation).x + 3SearchYMin = Area(VarArea).location(VarLocation).y - 3SearchYMax = Area(VarArea).location(VarLocation).y + 3'Search Start LocationSearchX = SearchXMinSearchY = SearchYMin'For for each location in the area,For Index = 1 To Area(VarArea).maxlocations 'And for each X... For Index2 = SearchXMin To SearchXMax SearchX = SearchX + 1 '...Search each Y For Index3 = SearchYMin To SearchYMax SearchY = SearchY + 1 If Area(VarArea).location(Index).x = SearchX And Area(VarArea).location(Index).y = SearchY Then Area(VarArea).location(Index).Found = True End If Next Index3 Next Index2Next Index``` Link to comment Share on other sites More sharing options...
azkanan Posted January 7, 2013 Author Share Posted January 7, 2013 On the upside, this piece of code is finding and displaying locations. On the downside, it seems to be duplicating them in my List.```Dim SearchXMin As IntegerDim SearchXMax As IntegerDim SearchYMin As IntegerDim SearchYMax As IntegerDim SearchX As IntegerDim SearchY As IntegerDim NewPlace As BooleanDim Index As IntegerDim Index2 As IntegerDim Index3 As IntegerCall FINDPLAYER 'Assigns VarArea and VarLocation public variables to the players Area and Location.''Search Parameters'SearchXMin = Area(VarArea).location(VarLocation).x - 40SearchXMax = Area(VarArea).location(VarLocation).x + 40SearchYMin = Area(VarArea).location(VarLocation).y - 40SearchYMax = Area(VarArea).location(VarLocation).y + 40'Search Start Location'SearchX = SearchXMinSearchY = SearchYMin'For for each location in the area,'For Index = 1 To Area(VarArea).maxlocations SearchX = SearchXMin'And for each X...'For Index2 = SearchXMin To SearchXMax SearchX = SearchX + 1 '...Search each Y' SearchY = SearchYMin For Index3 = SearchYMin To SearchYMax SearchY = SearchY + 1 If Area(VarArea).location(Index).x = SearchX And Area(VarArea).location(Index).y = SearchY Then Area(VarArea).location(Index).Found = True End If If SearchY = SearchYMax Then Exit For End If Next Index3 If SearchX = SearchXMax Then Exit For End IfNext Index2Next Index```Is there anything in it that may be causing duplications? Otherwise, my bug must be elsewhere…Edit;Here is the output...[![](http://www.freemmorpgmaker.com/files/imagehost/pics/e7a6331cd76da71c54cd012b1da23185.png)](http://www.freemmorpgmaker.com/files/imagehost/#e7a6331cd76da71c54cd012b1da23185.png) Link to comment Share on other sites More sharing options...
Exception Posted January 7, 2013 Share Posted January 7, 2013 You know the distance check already, from what I've given you, so I'll provide this:Graphing a circle on a 2D plane would look like this: x^2 + y^2 = radius^2.Loop through the x - 3 to x + 3, and the same for y, and check if it's within the distance, and fits or is smaller than the radius. Should be simple. Good luck! Link to comment Share on other sites More sharing options...
azkanan Posted January 7, 2013 Author Share Posted January 7, 2013 Also, this is the code that lists the items to the listbox;```Index2 = 0list_locations.ClearFor Index2 = 1 To Area(VarArea).maxlocations If Area(VarArea).location(Index2).Found = True Then If Area(VarArea).location(Index2).Traveled = False Then If Index2 = Actor(0).locationlink Then list_locations.AddItem "(!) ???" & " (" & Area(VarArea).location(Index2).Category & ")" Else list_locations.AddItem "???" & " (" & Area(VarArea).location(Index2).Category & ")" End If ElseIf Area(VarArea).location(Index2).Traveled = True Then If Index2 = Actor(0).locationlink Then list_locations.AddItem "(!)" & Area(VarArea).location(Index2).Name & " (" & Area(VarArea).location(Index2).Category & ")" Else list_locations.AddItem Area(VarArea).location(Index2).Name & " (" & Area(VarArea).location(Index2).Category & ")" End If End If End IfNext Index2``` Link to comment Share on other sites More sharing options...
JeffSventora Posted January 7, 2013 Share Posted January 7, 2013 Maybe a bit much, but why not just implement greedy search or A Star? Link to comment Share on other sites More sharing options...
JohnPony Posted January 8, 2013 Share Posted January 8, 2013 > Maybe a bit much, but why not just implement greedy search or A Star?I suggested this earlier. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now