1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Matt RAYKOWSKI (sfb) <matt.raykowski@gmail.com>
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/>.
21 #include "nel/ligo/zone_region.h"
23 using namespace NLMISC
;
29 string
CZoneRegion::_StringOutOfBound
;
31 // ***************************************************************************
33 // ***************************************************************************
35 // ---------------------------------------------------------------------------
36 CZoneRegion::SZoneUnit::SZoneUnit()
38 ZoneName
= STRING_UNUSED
;
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
;
75 for (uint32 i
= 0; i
< 4; ++i
)
77 this->SharingMatNames
[i
] = zu
.SharingMatNames
[i
];
78 this->SharingCutEdges
[i
] = zu
.SharingCutEdges
[i
];
84 // ***************************************************************************
86 // ***************************************************************************
88 // ---------------------------------------------------------------------------
89 CZoneRegion::SZoneUnit2::SZoneUnit2()
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
;
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
;
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
;
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
];
141 // ***************************************************************************
143 // ***************************************************************************
145 // ---------------------------------------------------------------------------
146 CZoneRegion::CZoneRegion()
148 _StringOutOfBound
= STRING_OUT_OF_BOUND
;
153 // ---------------------------------------------------------------------------
154 void CZoneRegion::serial (NLMISC::IStream
&f
)
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");
168 f
.serialCont (_Zones
);
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
];
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
;
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
))
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
))
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
))
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
))
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
))
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
))
278 return _Zones
[(x
-_MinX
)+(y
-_MinY
)*(1+_MaxX
-_MinX
)].DateLow
;
280 return _Zones
[(x
-_MinX
)+(y
-_MinY
)*(1+_MaxX
-_MinX
)].DateHigh
;
284 // ---------------------------------------------------------------------------
285 void CZoneRegion::resize (sint32 newMinX
, sint32 newMaxX
, sint32 newMinY
, sint32 newMaxY
)
288 vector
<SZoneUnit2
> newZones
;
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
];
303 newZones
[(i
-newMinX
)+(j
-newMinY
)*newStride
] = zuTmp
;
306 _MinX
= newMinX
; _MaxX
= newMaxX
;
307 _MinY
= newMinY
; _MaxY
= newMaxY
;
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
)
338 for (j
= 0; j
< h
; ++j
)
339 for (i
= 0; i
< (w
/2); ++i
)
342 Tab
[i
+j
*w
] = Tab
[(w
-1-i
)+j
*w
];
343 Tab
[(w
-1-i
)+j
*w
] = nTmp
;
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
];
362 for (j
= 0; j
< (h
/2); ++j
)
363 for (i
= 0; i
< w
; ++i
)
366 Tab
[i
+j
*w
] = Tab
[(w
-1-i
)+(h
-1-j
)*w
];
367 Tab
[(w
-1-i
)+(h
-1-j
)*w
] = nTmp
;
372 for (i
= 0; i
< (w
/2); ++i
)
375 Tab
[i
+j
*w
] = Tab
[(w
-1-i
)+j
*w
];
376 Tab
[(w
-1-i
)+j
*w
] = nTmp
;
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
];
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
;
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
))
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
))
436 _Zones
[(x
-_MinX
)+(y
-_MinY
)*(1+_MaxX
-_MinX
)].ZoneName
= newValue
;
441 // ***************************************************************************
443 bool CZoneRegion::setPosX (sint32 x
, sint32 y
, uint8 newValue
)
445 if ((x
< _MinX
) || (x
> _MaxX
) ||
446 (y
< _MinY
) || (y
> _MaxY
))
452 _Zones
[(x
-_MinX
)+(y
-_MinY
)*(1+_MaxX
-_MinX
)].PosX
= newValue
;
457 // ***************************************************************************
459 bool CZoneRegion::setPosY (sint32 x
, sint32 y
, uint8 newValue
)
461 if ((x
< _MinX
) || (x
> _MaxX
) ||
462 (y
< _MinY
) || (y
> _MaxY
))
468 _Zones
[(x
-_MinX
)+(y
-_MinY
)*(1+_MaxX
-_MinX
)].PosY
= newValue
;
473 // ***************************************************************************
475 bool CZoneRegion::setRot (sint32 x
, sint32 y
, uint8 newValue
)
477 if ((x
< _MinX
) || (x
> _MaxX
) ||
478 (y
< _MinY
) || (y
> _MaxY
))
484 _Zones
[(x
-_MinX
)+(y
-_MinY
)*(1+_MaxX
-_MinX
)].Rot
= newValue
;
489 // ***************************************************************************
491 bool CZoneRegion::setFlip (sint32 x
, sint32 y
, uint8 newValue
)
493 if ((x
< _MinX
) || (x
> _MaxX
) ||
494 (y
< _MinY
) || (y
> _MaxY
))
500 _Zones
[(x
-_MinX
)+(y
-_MinY
)*(1+_MaxX
-_MinX
)].Flip
= newValue
;
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
))
516 _Zones
[(x
-_MinX
)+(y
-_MinY
)*(1+_MaxX
-_MinX
)].SharingMatNames
[edge
] = newValue
;
521 // ***************************************************************************
523 bool CZoneRegion::setSharingCutEdges (sint32 x
, sint32 y
, uint edge
, uint8 newValue
)
525 if ((x
< _MinX
) || (x
> _MaxX
) ||
526 (y
< _MinY
) || (y
> _MaxY
))
532 _Zones
[(x
-_MinX
)+(y
-_MinY
)*(1+_MaxX
-_MinX
)].SharingCutEdges
[edge
] = newValue
;
537 // ***************************************************************************
540 } // namespace NLLIGO