1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
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/>.
20 #include "flora_sheet.h"
22 #include "nel/georges/u_form_elm.h"
24 using namespace NLGEORGES
;
26 // ***************************************************************************************************
27 CFloraSheet::CFloraSheet()
33 // ***************************************************************************************************
34 void CFloraSheet::build(const NLGEORGES::UFormElm
&item
)
36 const UFormElm
*plantArray
= NULL
;
37 if (item
.getNodeByName(&plantArray
, "Plants") && plantArray
)
40 nlverify(plantArray
->getArraySize(numPlants
));
41 _Plants
.reserve(numPlants
);
42 for(uint k
= 0; k
< numPlants
; ++k
)
44 const UFormElm
*subNode
= NULL
;
45 if (plantArray
->getArrayNode(&subNode
, k
) && subNode
)
49 pi
.CumulatedWeight
= _TotalWeight
;
50 _TotalWeight
+= pi
.Weight
;
51 _Plants
.push_back(pi
);
55 item
.getValueByName(MicroLifeThreshold
, "MicroLifeThreshold");
58 // ***************************************************************************************************
59 void CFloraSheet::serial(NLMISC::IStream
&f
)
61 f
.serialCont(_Plants
);
62 f
.serial(MicroLifeThreshold
);
63 f
.serial(_TotalWeight
);
66 // ***************************************************************************************************
67 void CPlantInfo::build(const NLGEORGES::UFormElm
&item
)
69 item
.getValueByName(SheetName
, "File name");
70 item
.getValueByName(Weight
, "MicroLifeWeight");
73 nlwarning("Plant with weight equal to 0");
77 // ***************************************************************************************************
78 void CPlantInfo::serial(NLMISC::IStream
&f
)
81 f
.serial(CumulatedWeight
);
84 // ***************************************************************************************************
85 const CPlantInfo
*CFloraSheet::getPlantInfoFromWeightedIndex(uint64 index
) const
87 if (_TotalWeight
== 0) return NULL
;
89 comp
.CumulatedWeight
= index
;
90 std::vector
<CPlantInfo
>::const_iterator it
= std::lower_bound(_Plants
.begin(), _Plants
.end(), comp
);
91 if (it
== _Plants
.end()) return &(_Plants
.back());
92 if (it
== _Plants
.begin()) return &(_Plants
.front());
93 return it
->CumulatedWeight
== index
? &(*it
) : &(*(it
- 1));