Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / src / ligo / zone_region.cpp
blobcde505af406c062153f3911c11b3a330dfe47dff
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Matt RAYKOWSKI (sfb) <matt.raykowski@gmail.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "stdligo.h"
21 #include "nel/ligo/zone_region.h"
23 using namespace NLMISC;
24 using namespace std;
26 namespace NLLIGO
29 string CZoneRegion::_StringOutOfBound;
31 // ***************************************************************************
32 // SZoneUnit
33 // ***************************************************************************
35 // ---------------------------------------------------------------------------
36 CZoneRegion::SZoneUnit::SZoneUnit()
38 ZoneName = STRING_UNUSED;
39 PosX = PosY = 0;
40 Rot = Flip = 0;
41 SharingMatNames[0] = STRING_UNUSED;
42 SharingMatNames[1] = STRING_UNUSED;
43 SharingMatNames[2] = STRING_UNUSED;
44 SharingMatNames[3] = STRING_UNUSED;
45 SharingCutEdges[0] = 0;
46 SharingCutEdges[1] = 0;
47 SharingCutEdges[2] = 0;
48 SharingCutEdges[3] = 0;
51 // ---------------------------------------------------------------------------
52 void CZoneRegion::SZoneUnit::serial (NLMISC::IStream &f)
54 f.xmlSerial (ZoneName, "NAME");
55 f.xmlSerial (PosX, "X");
56 f.xmlSerial (PosY, "Y");
57 f.xmlSerial (Rot, "ROT");
58 f.xmlSerial (Flip, "FLIP");
60 for (uint32 i = 0; i < 4; ++i)
62 f.xmlSerial (SharingMatNames[i], "MAT_NAMES");
63 f.xmlSerial (SharingCutEdges[i], "CUR_EDGES");
67 // ---------------------------------------------------------------------------
68 const CZoneRegion::SZoneUnit& CZoneRegion::SZoneUnit::operator=(const CZoneRegion::SZoneUnit&zu)
70 this->ZoneName = zu.ZoneName;
71 this->PosX = zu.PosX;
72 this->PosY = zu.PosY;
73 this->Rot = zu.Rot;
74 this->Flip = zu.Flip;
75 for (uint32 i = 0; i < 4; ++i)
77 this->SharingMatNames[i] = zu.SharingMatNames[i];
78 this->SharingCutEdges[i] = zu.SharingCutEdges[i];
80 return *this;
84 // ***************************************************************************
85 // SZoneUnit2
86 // ***************************************************************************
88 // ---------------------------------------------------------------------------
89 CZoneRegion::SZoneUnit2::SZoneUnit2()
91 DateLow = 0;
92 DateHigh = 0;
95 // ---------------------------------------------------------------------------
96 void CZoneRegion::SZoneUnit2::serial (NLMISC::IStream &f)
98 /*sint32 version =*/ f.serialVersion (0);
100 SZoneUnit::serial (f);
101 f.xmlSerial (DateLow, "LOW");
102 f.xmlSerial (DateHigh, "HIGH");
105 // ---------------------------------------------------------------------------
106 const CZoneRegion::SZoneUnit2& CZoneRegion::SZoneUnit2::operator=(const CZoneRegion::SZoneUnit2&zu)
108 this->ZoneName = zu.ZoneName;
109 this->PosX = zu.PosX;
110 this->PosY = zu.PosY;
111 this->Rot = zu.Rot;
112 this->Flip = zu.Flip;
113 for (uint32 i = 0; i < 4; ++i)
115 this->SharingMatNames[i] = zu.SharingMatNames[i];
116 this->SharingCutEdges[i] = zu.SharingCutEdges[i];
118 this->DateLow = zu.DateLow;
119 this->DateHigh = zu.DateHigh;
120 return *this;
123 // ---------------------------------------------------------------------------
124 const CZoneRegion::SZoneUnit2& CZoneRegion::SZoneUnit2::operator=(const CZoneRegion::SZoneUnit&zu)
126 this->ZoneName = zu.ZoneName;
127 this->PosX = zu.PosX;
128 this->PosY = zu.PosY;
129 this->Rot = zu.Rot;
130 this->Flip = zu.Flip;
131 for (uint32 i = 0; i < 4; ++i)
133 this->SharingMatNames[i] = zu.SharingMatNames[i];
134 this->SharingCutEdges[i] = zu.SharingCutEdges[i];
136 this->DateLow = 0;
137 this->DateHigh = 0;
138 return *this;
141 // ***************************************************************************
142 // CZoneRegion
143 // ***************************************************************************
145 // ---------------------------------------------------------------------------
146 CZoneRegion::CZoneRegion()
148 _StringOutOfBound = STRING_OUT_OF_BOUND;
149 _MinX = _MinY = 0;
150 _MaxX = _MaxY = -1;
153 // ---------------------------------------------------------------------------
154 void CZoneRegion::serial (NLMISC::IStream &f)
156 f.xmlPush ("LAND");
158 sint32 version = f.serialVersion (1);
159 f.serialCheck (NELID("DNAL"));
161 f.xmlSerial (_MinX, "MIN_X");
162 f.xmlSerial (_MinY, "MIN_Y");
163 f.xmlSerial (_MaxX, "MAX_X");
164 f.xmlSerial (_MaxY, "MAX_Y");
166 if (version == 1)
168 f.serialCont (_Zones);
171 if (version == 0)
173 std::vector<SZoneUnit> vZonesTmp;
174 f.serialCont (vZonesTmp);
175 _Zones.resize (vZonesTmp.size());
176 for (uint32 i = 0; i < vZonesTmp.size(); ++i)
177 _Zones[i] = vZonesTmp[i];
180 f.xmlPop ();
183 // ---------------------------------------------------------------------------
184 const string &CZoneRegion::getName (sint32 x, sint32 y) const
186 if ((x < _MinX) || (x > _MaxX) ||
187 (y < _MinY) || (y > _MaxY))
189 return _StringOutOfBound;
191 else
193 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].ZoneName;
197 // ---------------------------------------------------------------------------
198 uint8 CZoneRegion::getPosX (sint32 x, sint32 y) const
200 if ((x < _MinX) || (x > _MaxX) ||
201 (y < _MinY) || (y > _MaxY))
203 return 0;
205 else
207 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].PosX;
211 // ---------------------------------------------------------------------------
212 uint8 CZoneRegion::getPosY (sint32 x, sint32 y) const
214 if ((x < _MinX) || (x > _MaxX) ||
215 (y < _MinY) || (y > _MaxY))
217 return 0;
219 else
221 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].PosY;
225 // ---------------------------------------------------------------------------
226 uint8 CZoneRegion::getRot (sint32 x, sint32 y) const
228 if ((x < _MinX) || (x > _MaxX) ||
229 (y < _MinY) || (y > _MaxY))
231 return 0;
233 else
235 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].Rot;
239 // ---------------------------------------------------------------------------
240 uint8 CZoneRegion::getFlip (sint32 x, sint32 y) const
242 if ((x < _MinX) || (x > _MaxX) ||
243 (y < _MinY) || (y > _MaxY))
245 return 0;
247 else
249 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].Flip;
253 // ---------------------------------------------------------------------------
254 uint8 CZoneRegion::getCutEdge (sint32 x, sint32 y, uint8 pos) const
256 if ((x < _MinX) || (x > _MaxX) ||
257 (y < _MinY) || (y > _MaxY))
259 return 0;
261 else
263 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].SharingCutEdges[pos];
267 // ---------------------------------------------------------------------------
268 uint32 CZoneRegion::getDate (sint32 x, sint32 y, uint8 lowOrHigh) const // lowOrHigh == 0 -> low
270 if ((x < _MinX) || (x > _MaxX) ||
271 (y < _MinY) || (y > _MaxY))
273 return 0;
275 else
277 if (lowOrHigh == 0)
278 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].DateLow;
279 else
280 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].DateHigh;
284 // ---------------------------------------------------------------------------
285 void CZoneRegion::resize (sint32 newMinX, sint32 newMaxX, sint32 newMinY, sint32 newMaxY)
287 sint32 i, j;
288 vector<SZoneUnit2> newZones;
289 SZoneUnit2 zuTmp;
291 newZones.resize ((1+newMaxX-newMinX)*(1+newMaxY-newMinY));
292 sint32 newStride = 1+newMaxX-newMinX;
293 sint32 Stride = 1+_MaxX-_MinX;
294 for (j = newMinY; j <= newMaxY; ++j)
295 for (i = newMinX; i <= newMaxX; ++i)
297 if ((i >= _MinX)&&(i <= _MaxX)&&(j >= _MinY)&&(j <= _MaxY))
299 newZones[(i-newMinX)+(j-newMinY)*newStride] = _Zones[(i-_MinX)+(j-_MinY)*Stride];
301 else
303 newZones[(i-newMinX)+(j-newMinY)*newStride] = zuTmp;
306 _MinX = newMinX; _MaxX = newMaxX;
307 _MinY = newMinY; _MaxY = newMaxY;
308 _Zones = newZones;
311 // ---------------------------------------------------------------------------
312 void CZoneRegion::basicSet (sint32 x, sint32 y, sint32 PosX, sint32 PosY, const std::string &ZoneName)
314 // Do we need to resize ?
315 if ((x < _MinX) || (x > _MaxX) ||
316 (y < _MinY) || (y > _MaxY))
318 sint32 newMinX = (x<_MinX?x:_MinX), newMinY = (y<_MinY?y:_MinY);
319 sint32 newMaxX = (x>_MaxX?x:_MaxX), newMaxY = (y>_MaxY?y:_MaxY);
321 resize (newMinX, newMaxX, newMinY, newMaxY);
323 sint32 stride = (1+_MaxX-_MinX); // Nb to go to next line
325 _Zones[(x-_MinX)+(y-_MinY)*stride].ZoneName = ZoneName;
326 _Zones[(x-_MinX)+(y-_MinY)*stride].PosX = (uint8)PosX;
327 _Zones[(x-_MinX)+(y-_MinY)*stride].PosY = (uint8)PosY;
330 // ---------------------------------------------------------------------------
331 void SPiece::rotFlip (uint8 rot, uint8 flip)
333 uint8 nTmp;
334 sint32 i, j;
336 if (flip == 1)
338 for (j = 0; j < h; ++j)
339 for (i = 0; i < (w/2); ++i)
341 nTmp = Tab[i+j*w];
342 Tab[i+j*w] = Tab[(w-1-i)+j*w];
343 Tab[(w-1-i)+j*w] = nTmp;
347 if (rot == 1)
349 vector<uint8> TabDest;
350 TabDest.resize (Tab.size());
351 for (j = 0; j < h; ++j)
352 for (i = 0; i < w; ++i)
353 TabDest[j+i*h] = Tab[i+(h-1-j)*w];
354 Tab = TabDest;
355 i = w;
356 w = h;
357 h = i;
360 if (rot == 2)
362 for (j = 0; j < (h/2); ++j)
363 for (i = 0; i < w; ++i)
365 nTmp = Tab[i+j*w];
366 Tab[i+j*w] = Tab[(w-1-i)+(h-1-j)*w];
367 Tab[(w-1-i)+(h-1-j)*w] = nTmp;
369 if ((h/2)*2 != h)
371 j = (h/2);
372 for (i = 0; i < (w/2); ++i)
374 nTmp = Tab[i+j*w];
375 Tab[i+j*w] = Tab[(w-1-i)+j*w];
376 Tab[(w-1-i)+j*w] = nTmp;
381 if (rot == 3)
383 vector<uint8> TabDest;
384 TabDest.resize (Tab.size());
385 for (j = 0; j < h; ++j)
386 for (i = 0; i < w; ++i)
387 TabDest[j+i*h] = Tab[w-1-i+j*w];
388 Tab = TabDest;
389 i = w;
390 w = h;
391 h = i;
395 // ***************************************************************************
397 std::string CZoneRegion::getSharingMatNames (sint32 x, sint32 y, uint edge)
399 if ((x < _MinX) || (x > _MaxX) ||
400 (y < _MinY) || (y > _MaxY))
402 return _StringOutOfBound;
404 else
406 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].SharingMatNames[edge];
410 // ***************************************************************************
412 uint8 CZoneRegion::getSharingCutEdges (sint32 x, sint32 y, uint edge)
414 if ((x < _MinX) || (x > _MaxX) ||
415 (y < _MinY) || (y > _MaxY))
417 return 0xff;
419 else
421 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].SharingCutEdges[edge];
425 // ***************************************************************************
427 bool CZoneRegion::setName (sint32 x, sint32 y, const std::string &newValue)
429 if ((x < _MinX) || (x > _MaxX) ||
430 (y < _MinY) || (y > _MaxY))
432 return false;
434 else
436 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].ZoneName = newValue;
437 return true;
441 // ***************************************************************************
443 bool CZoneRegion::setPosX (sint32 x, sint32 y, uint8 newValue)
445 if ((x < _MinX) || (x > _MaxX) ||
446 (y < _MinY) || (y > _MaxY))
448 return false;
450 else
452 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].PosX = newValue;
453 return true;
457 // ***************************************************************************
459 bool CZoneRegion::setPosY (sint32 x, sint32 y, uint8 newValue)
461 if ((x < _MinX) || (x > _MaxX) ||
462 (y < _MinY) || (y > _MaxY))
464 return false;
466 else
468 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].PosY = newValue;
469 return true;
473 // ***************************************************************************
475 bool CZoneRegion::setRot (sint32 x, sint32 y, uint8 newValue)
477 if ((x < _MinX) || (x > _MaxX) ||
478 (y < _MinY) || (y > _MaxY))
480 return false;
482 else
484 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].Rot = newValue;
485 return true;
489 // ***************************************************************************
491 bool CZoneRegion::setFlip (sint32 x, sint32 y, uint8 newValue)
493 if ((x < _MinX) || (x > _MaxX) ||
494 (y < _MinY) || (y > _MaxY))
496 return false;
498 else
500 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].Flip = newValue;
501 return true;
505 // ***************************************************************************
507 bool CZoneRegion::setSharingMatNames (sint32 x, sint32 y, uint edge, const std::string &newValue)
509 if ((x < _MinX) || (x > _MaxX) ||
510 (y < _MinY) || (y > _MaxY))
512 return false;
514 else
516 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].SharingMatNames[edge] = newValue;
517 return true;
521 // ***************************************************************************
523 bool CZoneRegion::setSharingCutEdges (sint32 x, sint32 y, uint edge, uint8 newValue)
525 if ((x < _MinX) || (x > _MaxX) ||
526 (y < _MinY) || (y > _MaxY))
528 return false;
530 else
532 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].SharingCutEdges[edge] = newValue;
533 return true;
537 // ***************************************************************************
540 } // namespace NLLIGO