Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / src / 3d / zone_search.cpp
blob71704d37f8bdebcda4629f5571c87be73b3c1014
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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/>.
17 #include "std3d.h"
19 #include <cstdio>
20 #include <iostream>
22 #include "nel/3d/zone_search.h"
24 using namespace std;
26 #ifdef DEBUG_NEW
27 #define new DEBUG_NEW
28 #endif
30 namespace NL3D
34 /**
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
44 _NbZoneY = 297;
46 /// Size X of one zone (in meters)
47 _SizeZoneX = 160;
49 /// Size X of one zone (in meters)
50 _SizeZoneY = 160;
54 /**
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;
95 sizeArea += sizeArea;
96 sizeAreaX = sizeAreaY = sizeArea;
98 if(startPosX < 0)
100 sizeAreaX += startPosX;
101 startPosX = 0;
104 lastPosX = startPosX + sizeAreaX;
105 if(lastPosX >= (_NbZoneX * _SizeZoneX))
107 sizeAreaX -= _NbZoneX * _SizeZoneX - lastPosX;
108 lastPosX = _NbZoneX * _SizeZoneX - 1;
111 if(startPosY < 0)
113 sizeAreaY += startPosY;
114 startPosY = 0;
117 lastPosY = startPosY + sizeAreaY;
118 if(lastPosY >= (_NbZoneY * _SizeZoneY))
120 sizeAreaY -= _NbZoneY * _SizeZoneY - lastPosY;
121 lastPosY = _NbZoneY * _SizeZoneY - 1;
124 l.clear();
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;
160 if(startPosX < 0)
162 sizeAreaX += startPosX;
163 startPosX = 0;
166 lastPosX = startPosX + sizeAreaX;
167 if(lastPosX >= (_NbZoneX * _SizeZoneX))
169 sizeAreaX -= _NbZoneX * _SizeZoneX - lastPosX;
170 lastPosX = _NbZoneX * _SizeZoneX - 1;
173 if(startPosY < 0)
175 sizeAreaY += startPosY;
176 startPosY = 0;
179 lastPosY = startPosY + sizeAreaY;
180 if(lastPosY >= (_NbZoneY * _SizeZoneY))
182 sizeAreaY -= _NbZoneY * _SizeZoneY - lastPosY;
183 lastPosY = _NbZoneY * _SizeZoneY - 1;
186 l.clear();
188 for(uint i = startPosY; i <= lastPosY; i += _SizeZoneY)
190 for(uint j = startPosX; j <= lastPosX; j += _SizeZoneX)
192 uint16 zoneId = getZoneId(j, i);
193 if (validZoneIds)
195 bool found = false;
196 for(uint k = 0; k < validZoneIds->size(); ++k)
198 if (zoneId == (*validZoneIds)[k])
200 found = true;
201 break;
204 if (!found) continue;
206 l.push_back(zoneId);
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)));
219 } // NL3D