1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "nel/3d/zone_search.h"
35 * Constructor : Initialize some privates members
37 CZoneSearch::CZoneSearch()
39 /// Size X is named of AA to ZZ, current size is IB = 26 * 8 + 2 (AA is zone number 1, AZ zone number 26, BA zone number 27...)
40 // TMP fix for level designer (nico ...)
41 _NbZoneX
= 26 * ('Z'-'A') + ('Z'-'A');
43 /// Number zones on Y axis of landscape
46 /// Size X of one zone (in meters)
49 /// Size X of one zone (in meters)
55 * Get the zone name corresponding to coordinate
56 * \param x is axis X coordinate (in meters)
57 * \param y is axis Y coordinate (in meters)
58 * \param cx is axis X coordinate of center area (in meters)
59 * \param cy is axis Y coordinate of center area (in meters)
60 * \return a pair of the zone name and square distance between zone and center area (in zone unit)
62 pair
<string
, uint32
> CZoneSearch::getZoneName(uint x
, uint y
, uint cx
, uint cy
)
64 uint zoneY
= y
/ _SizeZoneY
+ 1;
65 uint zoneX
= x
/ _SizeZoneX
;
67 uint zoneCenterY
= cy
/ _SizeZoneY
+ 1;
68 uint zoneCenterX
= cx
/ _SizeZoneX
;
70 uint32 distance
= (zoneX
- zoneCenterX
) * (zoneX
- zoneCenterX
) + (zoneY
- zoneCenterY
) * (zoneY
- zoneCenterY
);
72 char firstLetter
= zoneX
/ 26 + 'A';
73 char secondLetter
= zoneX
% 26 + 'A';
75 return std::pair
<string
, uint32
>(NLMISC::toString("%u_%c%c.zonel", zoneY
, firstLetter
, secondLetter
), distance
);
80 * Get a list of zone name around a position
81 * \param x is axis X ccordinate (in meter)
82 * \param y is axis Y coordinate (in meter)
83 * \param sizeArea is area of zone research (in meter)
84 * \param l is a reference to a list of pair of string and uint32
85 * \return a liste contained name of all zone around indicated position and and square distance between zone and center area (in zone unit)
87 void CZoneSearch::getListZoneName(uint x
, uint y
, uint sizeArea
, list
<pair
<string
, uint32
> >& l
)
89 sint startPosX
, startPosY
;
90 uint lastPosX
, lastPosY
, sizeAreaX
, sizeAreaY
;
92 startPosX
= x
- sizeArea
;
93 startPosY
= y
- sizeArea
;
96 sizeAreaX
= sizeAreaY
= sizeArea
;
100 sizeAreaX
+= startPosX
;
104 lastPosX
= startPosX
+ sizeAreaX
;
105 if(lastPosX
>= (_NbZoneX
* _SizeZoneX
))
107 sizeAreaX
-= _NbZoneX
* _SizeZoneX
- lastPosX
;
108 lastPosX
= _NbZoneX
* _SizeZoneX
- 1;
113 sizeAreaY
+= startPosY
;
117 lastPosY
= startPosY
+ sizeAreaY
;
118 if(lastPosY
>= (_NbZoneY
* _SizeZoneY
))
120 sizeAreaY
-= _NbZoneY
* _SizeZoneY
- lastPosY
;
121 lastPosY
= _NbZoneY
* _SizeZoneY
- 1;
126 for(uint i
= startPosY
; i
<= lastPosY
; i
+= _SizeZoneY
)
128 for(uint j
= startPosX
; j
<= lastPosX
; j
+= _SizeZoneX
)
130 l
.push_back(getZoneName(j
, i
, x
, y
));
135 uint16
CZoneSearch::getZoneId (uint x
, uint y
) const
137 uint zoneY
= y
/ _SizeZoneY
;
138 uint zoneX
= x
/ _SizeZoneX
;
140 return (zoneX
&255)+(zoneY
<<8);
143 void CZoneSearch::getZonePos (uint16 zoneId
, uint
&x
, uint
&y
) const
145 x
= _SizeZoneY
*(zoneId
&255);
146 y
= _SizeZoneY
*(zoneId
>>8);
149 void CZoneSearch::getListZoneId (uint x
, uint y
, uint sizeArea
, vector
<uint16
> &l
, const std::vector
<uint16
> *validZoneIds
)
151 sint startPosX
, startPosY
;
152 uint lastPosX
, lastPosY
, sizeAreaX
, sizeAreaY
;
154 startPosX
= x
- sizeArea
;
155 startPosY
= y
- sizeArea
;
157 sizeArea
+= sizeArea
;
158 sizeAreaX
= sizeAreaY
= sizeArea
;
162 sizeAreaX
+= startPosX
;
166 lastPosX
= startPosX
+ sizeAreaX
;
167 if(lastPosX
>= (_NbZoneX
* _SizeZoneX
))
169 sizeAreaX
-= _NbZoneX
* _SizeZoneX
- lastPosX
;
170 lastPosX
= _NbZoneX
* _SizeZoneX
- 1;
175 sizeAreaY
+= startPosY
;
179 lastPosY
= startPosY
+ sizeAreaY
;
180 if(lastPosY
>= (_NbZoneY
* _SizeZoneY
))
182 sizeAreaY
-= _NbZoneY
* _SizeZoneY
- lastPosY
;
183 lastPosY
= _NbZoneY
* _SizeZoneY
- 1;
188 for(uint i
= startPosY
; i
<= lastPosY
; i
+= _SizeZoneY
)
190 for(uint j
= startPosX
; j
<= lastPosX
; j
+= _SizeZoneX
)
192 uint16 zoneId
= getZoneId(j
, i
);
196 for(uint k
= 0; k
< validZoneIds
->size(); ++k
)
198 if (zoneId
== (*validZoneIds
)[k
])
204 if (!found
) continue;
211 std::string
CZoneSearch::getZoneNameFromId (uint16 zoneid
)
213 sint x
= zoneid
& 255;
214 sint y
= zoneid
>> 8;
215 return NLMISC::toString("%d_%c%c.zonel", y
+ 1, (char)('A' + (x
/ 26)), (char)('A' + (x
% 26)));