Added aqua_speed for rite geo 50 tryker
[ryzomcore.git] / nel / tools / 3d / zone_lib / zone_utility.cpp
blob7fcfbf363a95eae9c8a0a5dd252d249e541ce91e
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/>.
18 #include "nel/misc/types_nl.h"
19 #include "nel/misc/file.h"
20 #include "nel/3d/quad_tree.h"
21 #include "nel/3d/zone.h"
22 #include <vector>
23 #include <set>
24 #include <iostream>
27 using namespace NL3D;
28 using namespace NLMISC;
29 using namespace std;
32 /*******************************************************************\
33 getDir()
34 \*******************************************************************/
35 std::string getDir (const std::string& path)
37 char tmpPath[512];
38 strcpy (tmpPath, path.c_str());
39 char* slash=strrchr (tmpPath, '/');
40 if (!slash)
42 slash=strrchr (tmpPath, '\\');
45 if (!slash)
46 return "";
48 slash++;
49 *slash=0;
50 return tmpPath;
54 /*******************************************************************\
55 getName()
56 \*******************************************************************/
57 std::string getName (const std::string& path)
59 std::string dir=getDir (path);
61 char tmpPath[512];
62 strcpy (tmpPath, path.c_str());
64 char *name=tmpPath;
65 nlassert (dir.length()<=strlen(tmpPath));
66 name+=dir.length();
68 char* point=strrchr (name, '.');
69 if (point)
70 *point=0;
72 return name;
76 /*******************************************************************\
77 getExt()
78 \*******************************************************************/
79 std::string getExt (const std::string& path)
81 std::string dir=getDir (path);
82 std::string name=getName (path);
84 char tmpPath[512];
85 strcpy (tmpPath, path.c_str());
87 char *ext=tmpPath;
88 nlassert (dir.length()+name.length()<=strlen(tmpPath));
89 ext+=dir.length()+name.length();
91 return ext;
94 /*******************************************************************\
95 getZoneCoordByName()
96 \*******************************************************************/
97 bool getZoneCoordByName(const char * name, uint16& x, uint16& y)
99 uint i;
101 std::string zoneName(name);
103 // y
104 string::size_type ind1 = zoneName.find("_");
105 if(ind1 == string::npos || ind1>=zoneName.length())
107 nlwarning("bad file name");
108 return false;
110 std::string ystr = zoneName.substr(0,ind1);
111 for(i=0; i<ystr.length(); i++)
113 if(!isdigit(ystr[i]))
115 nlwarning("y code size is not a 2 characters code");
116 return false;
120 NLMISC::fromString(ystr, y);
122 // x
123 x = 0;
124 uint ind2 = (uint)zoneName.length();
125 if((ind2-ind1-1)!=2)
127 nlwarning("x code size is not a 2 characters code");
128 return false;
130 std::string xstr = zoneName.substr(ind1+1,ind2-ind1-1);
131 for(i=0; i<xstr.length(); i++)
133 if (isalpha(xstr[i]))
135 x *= 26;
136 x += (tolower(xstr[i])-'a');
138 else
140 nlwarning("invalid");
141 return false;
144 return true;
148 /*******************************************************************\
149 getLettersFromNum()
150 \*******************************************************************/
151 void getLettersFromNum(uint16 num, std::string& code)
153 if(num>26*26)
155 nlwarning("zone index too high");
156 return;
158 code.resize(0);
159 uint16 remainder = num%26;
160 code += 'A' + num/26;
161 code += 'A' + remainder;
165 /*******************************************************************\
166 getZoneNameByCoord()
167 \*******************************************************************/
168 void getZoneNameByCoord(uint16 x, uint16 y, std::string& zoneName)
170 // y str
171 char stmp[10];
172 sprintf(stmp,"%d",y);
173 std::string ystrtmp = std::string(stmp);
175 // x str
176 std::string xstrtmp;
177 getLettersFromNum(x, xstrtmp);
179 // name
180 zoneName = ystrtmp;
181 zoneName +="_";
182 zoneName +=xstrtmp;
187 /*******************************************************************\
188 getAdjacentZonesName()
189 \*******************************************************************/
190 void getAdjacentZonesName(const std::string& zoneName, std::vector<std::string>& names)
192 uint16 x,y;
193 int xtmp,ytmp;
194 std::string nametmp;
195 std::string empty("empty");
197 names.reserve(8);
199 getZoneCoordByName(zoneName.c_str(), x, y);
201 // top left
202 xtmp = x-1;
203 ytmp = y-1;
204 if(xtmp<0||ytmp<0)
205 nametmp = empty;
206 else
207 getZoneNameByCoord(xtmp, ytmp, nametmp);
208 names.push_back(nametmp);
210 // top
211 xtmp = x;
212 ytmp = y-1;
213 if(ytmp<0)
214 nametmp = empty;
215 else
216 getZoneNameByCoord(xtmp, ytmp, nametmp);
217 names.push_back(nametmp);
219 // top right
220 xtmp = x+1;
221 ytmp = y-1;
222 if(ytmp<0)
223 nametmp = empty;
224 else
225 getZoneNameByCoord(xtmp, ytmp, nametmp);
226 names.push_back(nametmp);
228 // left
229 xtmp = x-1;
230 ytmp = y;
231 if(xtmp<0)
232 nametmp = empty;
233 else
234 getZoneNameByCoord(xtmp, ytmp, nametmp);
235 names.push_back(nametmp);
237 // right
238 xtmp = x+1;
239 ytmp = y;
240 getZoneNameByCoord(xtmp, ytmp, nametmp);
241 names.push_back(nametmp);
243 // bottom left
244 xtmp = x-1;
245 ytmp = y+1;
246 if(xtmp<0)
247 nametmp = empty;
248 else
249 getZoneNameByCoord(xtmp, ytmp, nametmp);
250 names.push_back(nametmp);
252 // bottom
253 xtmp = x;
254 ytmp = y+1;
255 getZoneNameByCoord(xtmp, ytmp, nametmp);
256 names.push_back(nametmp);
258 // bottom right
259 xtmp = x+1;
260 ytmp = y+1;
261 getZoneNameByCoord(xtmp, ytmp, nametmp);
262 names.push_back(nametmp);
266 /*******************************************************************\
267 createZoneId()
268 \*******************************************************************/
269 uint16 createZoneId(std::string zoneName)
271 uint16 x,y;
272 getZoneCoordByName(zoneName.c_str(), x, y);
273 return ((y-1)<<8) + x;