JNR
laLitterBox.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: laLitterBox.cpp
32 //
33 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
34 //
35 #include "stdafx.h"
36 #include "Core-Level-JR.h"
37 
38 laLitterPools::laLitterPools(void)
39 {
40 }
41 
42 laLitterPools::~laLitterPools(void)
43 {
44 }
45 
46 laPoint3 laLitterPools::getRandomPosition()
47 {
48  laPoint3 pos = _ptPivot;
49 
50  pos[0] += ((rand()%1000)/1000.0) * __ptSize.x();
51  pos[1] += ((rand()%1000)/1000.0) * __ptSize.y();
52  pos[2] += ((rand()%1000)/1000.0) * __ptSize.z();
53 
54  return pos;
55 }
56 
57 unsigned laLitterPools::getRandomPopulation()
58 {
59  if(_nMaxPolulation!=_nMinPolulation)
60  return rand()%(_nMaxPolulation-_nMinPolulation) + _nMinPolulation + 1;
61  else return _nMinPolulation;
62 }
63 
64 unsigned laLitterPools::getRandomElement()
65 {
66  return _arPools[rand()%_nPoolsz];
67 }
68 
69 double laLitterPools::getRandomAngle()
70 {
71  return ((rand()%1000)/1000.0) * _dAngleVar;
72 }
73 
74 
75 void laLitterPools::setTS(laTileset* pTileset)
76 {
77  _pTileset = pTileset;
78 
79  for(unsigned i=0; i<_nPoolsz; i++)
80  {
81  _arPools[i] = _pTileset->getElementIndex(_strNames+i*64);
82 
83  if(_arPools[i]==_pTileset->getElementCnt())
84  throw laError("Element '%s' requested as litter Pools element was not found",
85  _strNames+i*64);
86  }
87 }
88 
89 void laLitterPools::load(class laFileParser *fp)
90 {
91  fp->readText(_strName);
92  fp->readObj(&_ptPivot);
93  fp->readObj(&__ptSize, M_FALSE);
94 
95  fp->readDouble(&_dAngleVar);
96  fp->readObj(&_AngleVector, M_FALSE);
97 
98  _ptPivot *= M_UNIT;
99  __ptSize *= M_UNIT;
100 
101  fp->readUnsigned(&_nMinPolulation);
102  fp->readUnsigned(&_nMaxPolulation, M_FALSE);
103  fp->readUnsigned(&_nPoolsz);
104 
105  _arPools = new unsigned[_nPoolsz];
106  _strNames = new char[_nPoolsz*64];
107 
108  for(unsigned i=0; i<_nPoolsz; i++)
109  {
110  fp->readText(_strNames+i*64);
111  }
112 }
113 
114 laLitterBox::laLitterBox(void)
115 {
116  _pTileset = NULL;
117  _strPoolsName[0] = '\0';
118  _nObjectCount = 0;
119 }
120 
121 laLitterBox::~laLitterBox(void)
122 {
123 }
124 
125 //Populate the litter box
126 void laLitterBox::litter(laTileset* pTileset)
127 {
128  //Loacte the Pools
129  unsigned nLitterPools;
130  laLitterPools* pPools;
131 
132  _pTileset = pTileset;
133  nLitterPools = _pTileset->getLitterPoolsIndex(_strPoolsName);
134 
135  if(nLitterPools>=_pTileset->getLitterPoolsCount())
136  throw laError("Invalid litter Pools index %d for Pools size %d",
137  nLitterPools, _pTileset->getLitterPoolsCount());
138 
139  if(nLitterPools>=_pTileset->getLitterPoolsCount())
140  throw laError("Litter Pools '%s' was requested but not found in the tileset",
141  _strPoolsName);
142 
143  pPools = _pTileset->getLitterPools(nLitterPools);
144 
145  //Litter
146  _nObjectCount = pPools->getRandomPopulation();
147 
148  _arObjects = new unsigned[_nObjectCount];
149  _arPositions = new laPoint3[_nObjectCount];
150  _arAngles = new double[_nObjectCount];
151 
152  _AngleVector = pPools->getAngleVector();
153 
154  for(unsigned i=0; i<_nObjectCount; i++)
155  {
156  _arObjects[i] = pPools->getRandomElement();
157  _arPositions[i] = pPools->getRandomPosition();
158  _arAngles[i] = pPools->getRandomAngle();
159  }
160 }
161 
162 //draw
163 void laLitterBox::drawGeometry(laRenderer *r, laPoint3 pos)
164 {
165  for(unsigned i=0; i<_nObjectCount; i++)
166  {
167  laPoint3 mod_pos = pos;
168  mod_pos += _arPositions[i];
169 
170  r->transPush();
171  r->transTranslate(mod_pos);
172  r->transRotate(_arAngles[i], _AngleVector);
173 
174  _pTileset->getElement(_arObjects[i])->drawGeometry(r);
175 
176  r->transPop();
177  }
178 }
179 
180 void laLitterBox::drawFx(laRenderer *r, laPoint3 pos)
181 {
182  for(unsigned i=0; i<_nObjectCount; i++)
183  {
184  laPoint3 mod_pos = pos;
185  mod_pos += _arPositions[i];
186 
187  r->transPush();
188  r->transTranslate(mod_pos);
189  r->transRotate(_arAngles[i], _AngleVector);
190 
191  _pTileset->getElement(_arObjects[i])->drawFx(r);
192 
193  r->transPop();
194  }
195 }
196 
197 //This method is part of the laLoadableObj interface
198 void laLitterBox::load(class laFileParser *fp)
199 {
200  fp->readText(_strPoolsName, M_FALSE);
201 }
#define M_UNIT
Unit of 1 meter.
"Litter pools" to be used for populating laLitterBox
Definition: laLitterBox.h:40
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
Error handling class.
Definition: laError.h:46
Tileset Class.
Definition: laTileset.h:46
File Parser.
Definition: laFileParser.h:41