]> git.jsancho.org Git - lugaru.git/blob - Source/Objects.cpp
Fixed lots of errors spotted by cppcheck
[lugaru.git] / Source / Objects.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "Objects.h"
21 extern XYZ viewer;
22 extern float viewdistance;
23 extern float fadestart;
24 extern int environment;
25 extern float texscale;
26 extern Light light;
27 extern float multiplier;
28 extern float gravity;
29 extern FRUSTUM frustum;
30 extern Terrain terrain;
31 extern bool foliage;
32 extern int detail;
33 extern float blurness;
34 extern float windvar;
35 extern float playerdist;
36 extern bool skyboxtexture;
37
38 //Functions
39
40 bool Objects::checkcollide(XYZ startpoint, XYZ endpoint, int which)
41 {
42     static XYZ colpoint, colviewer, coltarget;
43     static int i;
44
45     startpoint.y += .1;
46     endpoint.y += .1;
47     startpoint.y -= .1;
48     endpoint.y -= .1;
49
50     for (i = 0; i < numobjects; i++) {
51         if (type[i] != treeleavestype && type[i] != treetrunktype && type[i] != bushtype && type[i] != firetype && i != which) {
52             colviewer = startpoint;
53             coltarget = endpoint;
54             if (model[i].LineCheck(&colviewer, &coltarget, &colpoint, &position[i], &yaw[i]) != -1)
55                 return 1;
56         }
57     }
58
59     return 0;
60 }
61
62 void Objects::SphereCheckPossible(XYZ *p1, float radius)
63 {
64     static int i, j;
65     static int whichpatchx;
66     static int whichpatchz;
67
68     whichpatchx = p1->x / (terrain.size / subdivision * terrain.scale);
69     whichpatchz = p1->z / (terrain.size / subdivision * terrain.scale);
70
71     if (whichpatchx >= 0 && whichpatchz >= 0 && whichpatchx < subdivision && whichpatchz < subdivision)
72         if (terrain.patchobjectnum[whichpatchx][whichpatchz] > 0 && terrain.patchobjectnum[whichpatchx][whichpatchz] < 500)
73             for (j = 0; j < terrain.patchobjectnum[whichpatchx][whichpatchz]; j++) {
74                 i = terrain.patchobjects[whichpatchx][whichpatchz][j];
75                 possible[i] = 0;
76                 if (model[i].SphereCheckPossible(p1, radius, &position[i], &yaw[i]) != -1) {
77                     possible[i] = 1;
78                 }
79             }
80 }
81
82 void Objects::Draw()
83 {
84     static float distance;
85     static int i;
86     static XYZ moved, terrainlight;
87     bool hidden;
88
89     for (i = 0; i < numobjects; i++) {
90         if (type[i] != firetype) {
91             moved = DoRotation(model[i].boundingspherecenter, 0, yaw[i], 0);
92             if (type[i] == tunneltype || frustum.SphereInFrustum(position[i].x + moved.x, position[i].y + moved.y, position[i].z + moved.z, model[i].boundingsphereradius)) {
93                 distance = distsq(&viewer, &position[i]);
94                 distance *= 1.2;
95                 hidden = !(distsqflat(&viewer, &position[i]) > playerdist + 3 || (type[i] != bushtype && type[i] != treeleavestype));
96                 if (!hidden) {
97
98                     if (detail == 2 && distance > viewdistance * viewdistance / 4 && environment == desertenvironment)
99                         glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, blurness );
100                     else
101                         glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
102                     distance = (viewdistance * viewdistance - (distance - (viewdistance * viewdistance * fadestart)) * (1 / (1 - fadestart))) / viewdistance / viewdistance;
103                     if (distance > 1)
104                         distance = 1;
105                     if (distance > 0) {
106
107                         /*if(checkcollide(viewer,DoRotation(model[i].vertex[model[i].vertexNum],0,yaw[i],0)*scale[i]+position[i],i)){
108                         occluded[i]+=1;
109                         }
110                         else occluded[i]=0;*/
111                         if (occluded[i] < 6) {
112                             glMatrixMode(GL_MODELVIEW);
113                             glPushMatrix();
114                             if (!model[i].color)
115                                 glEnable(GL_LIGHTING);
116                             else
117                                 glDisable(GL_LIGHTING);
118                             glDepthMask(1);
119                             glTranslatef(position[i].x, position[i].y, position[i].z);
120                             if (type[i] == bushtype) {
121                                 messedwith[i] -= multiplier;
122                                 if (rotxvel[i] || rotx[i]) {
123                                     if (rotx[i] > 0) rotxvel[i] -= multiplier * 8 * fabs(rotx[i]);
124                                     if (rotx[i] < 0) rotxvel[i] += multiplier * 8 * fabs(rotx[i]);
125                                     if (rotx[i] > 0) rotxvel[i] -= multiplier * 4;
126                                     if (rotx[i] < 0) rotxvel[i] += multiplier * 4;
127                                     if (rotxvel[i] > 0) rotxvel[i] -= multiplier * 4;
128                                     if (rotxvel[i] < 0) rotxvel[i] += multiplier * 4;
129                                     if (fabs(rotx[i]) < multiplier * 4)
130                                         rotx[i] = 0;
131                                     if (fabs(rotxvel[i]) < multiplier * 4)
132                                         rotxvel[i] = 0;
133
134                                     rotx[i] += rotxvel[i] * multiplier * 4;
135                                 }
136                                 if (rotyvel[i] || roty[i]) {
137                                     if (roty[i] > 0) rotyvel[i] -= multiplier * 8 * fabs(roty[i]);
138                                     if (roty[i] < 0) rotyvel[i] += multiplier * 8 * fabs(roty[i]);
139                                     if (roty[i] > 0) rotyvel[i] -= multiplier * 4;
140                                     if (roty[i] < 0) rotyvel[i] += multiplier * 4;
141                                     if (rotyvel[i] > 0) rotyvel[i] -= multiplier * 4;
142                                     if (rotyvel[i] < 0) rotyvel[i] += multiplier * 4;
143                                     if (fabs(roty[i]) < multiplier * 4)
144                                         roty[i] = 0;
145                                     if (fabs(rotyvel[i]) < multiplier * 4)
146                                         rotyvel[i] = 0;
147
148                                     roty[i] += rotyvel[i] * multiplier * 4;
149                                 }
150                                 if (roty[i]) {
151                                     glRotatef(roty[i], 1, 0, 0);
152                                 }
153                                 if (rotx[i]) {
154                                     glRotatef(-rotx[i], 0, 0, 1);
155                                 }
156                                 if (rotx[i] > 10)
157                                     rotx[i] = 10;
158                                 if (rotx[i] < -10)
159                                     rotx[i] = -10;
160                                 if (roty[i] > 10)
161                                     roty[i] = 10;
162                                 if (roty[i] < -10)
163                                     roty[i] = -10;
164                             }
165                             if (type[i] == treetrunktype || type[i] == treeleavestype) {
166                                 if (type[i] == treetrunktype || environment == 2) {
167                                     messedwith[i] -= multiplier;
168                                     if (rotxvel[i] || rotx[i]) {
169                                         if (rotx[i] > 0) rotxvel[i] -= multiplier * 8 * fabs(rotx[i]);
170                                         if (rotx[i] < 0) rotxvel[i] += multiplier * 8 * fabs(rotx[i]);
171                                         if (rotx[i] > 0) rotxvel[i] -= multiplier * 4;
172                                         if (rotx[i] < 0) rotxvel[i] += multiplier * 4;
173                                         if (rotxvel[i] > 0) rotxvel[i] -= multiplier * 4;
174                                         if (rotxvel[i] < 0) rotxvel[i] += multiplier * 4;
175                                         if (fabs(rotx[i]) < multiplier * 4)
176                                             rotx[i] = 0;
177                                         if (fabs(rotxvel[i]) < multiplier * 4)
178                                             rotxvel[i] = 0;
179
180                                         rotx[i] += rotxvel[i] * multiplier * 4;
181                                     }
182                                     if (rotyvel[i] || roty[i]) {
183                                         if (roty[i] > 0) rotyvel[i] -= multiplier * 8 * fabs(roty[i]);
184                                         if (roty[i] < 0) rotyvel[i] += multiplier * 8 * fabs(roty[i]);
185                                         if (roty[i] > 0) rotyvel[i] -= multiplier * 4;
186                                         if (roty[i] < 0) rotyvel[i] += multiplier * 4;
187                                         if (rotyvel[i] > 0) rotyvel[i] -= multiplier * 4;
188                                         if (rotyvel[i] < 0) rotyvel[i] += multiplier * 4;
189                                         if (fabs(roty[i]) < multiplier * 4)
190                                             roty[i] = 0;
191                                         if (fabs(rotyvel[i]) < multiplier * 4)
192                                             rotyvel[i] = 0;
193
194                                         roty[i] += rotyvel[i] * multiplier * 4;
195                                     }
196                                     if (roty[i]) {
197                                         glRotatef(roty[i] / 6, 1, 0, 0);
198                                     }
199                                     if (rotx[i]) {
200                                         glRotatef(-rotx[i] / 6, 0, 0, 1);
201                                     }
202                                     if (rotx[i] > 10)
203                                         rotx[i] = 10;
204                                     if (rotx[i] < -10)
205                                         rotx[i] = -10;
206                                     if (roty[i] > 10)
207                                         roty[i] = 10;
208                                     if (roty[i] < -10)
209                                         roty[i] = -10;
210                                 } else {
211                                     messedwith[i] -= multiplier;
212                                     if (rotxvel[i] || rotx[i]) {
213                                         if (rotx[i] > 0) rotxvel[i] -= multiplier * 8 * fabs(rotx[i]);
214                                         if (rotx[i] < 0) rotxvel[i] += multiplier * 8 * fabs(rotx[i]);
215                                         if (rotx[i] > 0) rotxvel[i] -= multiplier * 4;
216                                         if (rotx[i] < 0) rotxvel[i] += multiplier * 4;
217                                         if (rotxvel[i] > 0) rotxvel[i] -= multiplier * 4;
218                                         if (rotxvel[i] < 0) rotxvel[i] += multiplier * 4;
219                                         if (fabs(rotx[i]) < multiplier * 4)
220                                             rotx[i] = 0;
221                                         if (fabs(rotxvel[i]) < multiplier * 4)
222                                             rotxvel[i] = 0;
223
224                                         rotx[i] += rotxvel[i] * multiplier * 4;
225                                     }
226                                     if (rotyvel[i] || roty[i]) {
227                                         if (roty[i] > 0) rotyvel[i] -= multiplier * 8 * fabs(roty[i]);
228                                         if (roty[i] < 0) rotyvel[i] += multiplier * 8 * fabs(roty[i]);
229                                         if (roty[i] > 0) rotyvel[i] -= multiplier * 4;
230                                         if (roty[i] < 0) rotyvel[i] += multiplier * 4;
231                                         if (rotyvel[i] > 0) rotyvel[i] -= multiplier * 4;
232                                         if (rotyvel[i] < 0) rotyvel[i] += multiplier * 4;
233                                         if (fabs(roty[i]) < multiplier * 4)
234                                             roty[i] = 0;
235                                         if (fabs(rotyvel[i]) < multiplier * 4)
236                                             rotyvel[i] = 0;
237
238                                         roty[i] += rotyvel[i] * multiplier * 4;
239                                     }
240                                     if (roty[i]) {
241                                         glRotatef(roty[i] / 4, 1, 0, 0);
242                                     }
243                                     if (rotx[i]) {
244                                         glRotatef(-rotx[i] / 4, 0, 0, 1);
245                                     }
246                                     if (rotx[i] > 10)
247                                         rotx[i] = 10;
248                                     if (rotx[i] < -10)
249                                         rotx[i] = -10;
250                                     if (roty[i] > 10)
251                                         roty[i] = 10;
252                                     if (roty[i] < -10)
253                                         roty[i] = -10;
254                                 }
255
256                             }
257                             if (/*detail==2&&*/environment == snowyenvironment) {
258                                 if (type[i] == treeleavestype) {
259                                     glRotatef((sin(windvar + position[i].x * .3) + .5) * 1.5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
260                                 }
261                                 if (type[i] == treetrunktype) {
262                                     glRotatef((sin(windvar + position[i].x * .3) + .5)*.5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
263                                 }
264                                 if (type[i] == bushtype) {
265                                     glRotatef((sin(windvar + position[i].x * .3) + .5) * 4 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
266                                 }
267                             }
268                             if (/*detail==2&&*/environment == grassyenvironment) {
269                                 if (type[i] == treeleavestype) {
270                                     glRotatef((sin(windvar + position[i].x * .3) + .5) * 1.5 * .5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
271                                 }
272                                 if (type[i] == treetrunktype) {
273                                     glRotatef((sin(windvar + position[i].x * .3) + .5)*.5 * .5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
274                                 }
275                                 if (type[i] == bushtype) {
276                                     glRotatef((sin(windvar + position[i].x * .3) + .5) * 4 * .5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
277                                 }
278                             }
279                             if (/*detail==2&&*/environment == desertenvironment) {
280                                 if (type[i] == bushtype) {
281                                     glRotatef((sin(windvar + position[i].x * .3) + .5) * 4 * .5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
282                                 }
283                             }
284                             glRotatef(yaw[i], 0, 1, 0);
285                             if (distance > 1)
286                                 distance = 1;
287                             glColor4f((1 - shadowed[i]) / 2 + .5, (1 - shadowed[i]) / 2 + .5, (1 - shadowed[i]) / 2 + .5, distance);
288                             if (distance >= 1) {
289                                 glDisable(GL_BLEND);
290                                 glAlphaFunc(GL_GREATER, 0.5);
291                             }
292                             if (distance < 1) {
293                                 glEnable(GL_BLEND);
294                                 glAlphaFunc(GL_GREATER, 0.1);
295                             }
296                             if (type[i] != treetrunktype && type[i] != treeleavestype && type[i] != bushtype && type[i] != rocktype) {
297                                 glEnable(GL_CULL_FACE);
298                                 glAlphaFunc(GL_GREATER, 0.0001);
299                                 model[i].drawdifftex(boxtextureptr);
300                                 model[i].drawdecals(terrain.shadowtexture, terrain.bloodtexture, terrain.bloodtexture2, terrain.breaktexture);
301                             }
302                             if (type[i] == rocktype) {
303                                 glEnable(GL_CULL_FACE);
304                                 glAlphaFunc(GL_GREATER, 0.0001);
305                                 glColor4f((1 - shadowed[i]) / 2 + light.ambient[0], (1 - shadowed[i]) / 2 + light.ambient[1], (1 - shadowed[i]) / 2 + light.ambient[2], distance);
306                                 model[i].drawdifftex(rocktextureptr);
307                                 model[i].drawdecals(terrain.shadowtexture, terrain.bloodtexture, terrain.bloodtexture2, terrain.breaktexture);
308                             }
309                             if (type[i] == treeleavestype) {
310                                 glDisable(GL_CULL_FACE);
311                                 glDisable(GL_LIGHTING);
312                                 terrainlight = terrain.getLighting(position[i].x, position[i].z);
313                                 if (!hidden) {
314                                     glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance);
315                                     if (distance < 1)
316                                         glAlphaFunc(GL_GREATER, 0.2);
317                                 }
318                                 if (hidden) {
319                                     glDepthMask(0);
320                                     glEnable(GL_BLEND);
321                                     glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance / 3);
322                                     glAlphaFunc(GL_GREATER, 0);
323                                 }
324                                 model[i].drawdifftex(treetextureptr);
325                             }
326                             if (type[i] == bushtype) {
327                                 glDisable(GL_CULL_FACE);
328                                 glDisable(GL_LIGHTING);
329                                 terrainlight = terrain.getLighting(position[i].x, position[i].z);
330                                 if (!hidden) {
331                                     glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance);
332                                     if (distance < 1)
333                                         glAlphaFunc(GL_GREATER, 0.2);
334                                 }
335                                 if (hidden) {
336                                     glDepthMask(0);
337                                     glEnable(GL_BLEND);
338                                     glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance / 3);
339                                     glAlphaFunc(GL_GREATER, 0);
340                                 }
341                                 model[i].drawdifftex(bushtextureptr);
342                             }
343                             if (type[i] == treetrunktype) {
344                                 glEnable(GL_CULL_FACE);
345                                 terrainlight = terrain.getLighting(position[i].x, position[i].z);
346                                 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance);
347                                 model[i].drawdifftex(treetextureptr);
348                             }
349                             glPopMatrix();
350                         }
351                     }
352                 }
353             }
354         }
355     }
356
357     glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
358     for (i = 0; i < numobjects; i++) {
359         if (type[i] == treeleavestype || type[i] == bushtype) {
360             moved = DoRotation(model[i].boundingspherecenter, 0, yaw[i], 0);
361             if (frustum.SphereInFrustum(position[i].x + moved.x, position[i].y + moved.y, position[i].z + moved.z, model[i].boundingsphereradius)) {
362                 hidden = distsqflat(&viewer, &position[i]) <= playerdist + 3;
363                 if (hidden) {
364                     distance = 1;
365                     glMatrixMode(GL_MODELVIEW);
366                     glPushMatrix();
367                     glEnable(GL_LIGHTING);
368                     glDepthMask(1);
369                     glTranslatef(position[i].x, position[i].y, position[i].z);
370                     if (type[i] == bushtype) {
371                         messedwith[i] -= multiplier;
372                         if (rotxvel[i] || rotx[i]) {
373                             if (rotx[i] > 0) rotxvel[i] -= multiplier * 8 * fabs(rotx[i]);
374                             if (rotx[i] < 0) rotxvel[i] += multiplier * 8 * fabs(rotx[i]);
375                             if (rotx[i] > 0) rotxvel[i] -= multiplier * 4;
376                             if (rotx[i] < 0) rotxvel[i] += multiplier * 4;
377                             if (rotxvel[i] > 0) rotxvel[i] -= multiplier * 4;
378                             if (rotxvel[i] < 0) rotxvel[i] += multiplier * 4;
379                             if (fabs(rotx[i]) < multiplier * 4)
380                                 rotx[i] = 0;
381                             if (fabs(rotxvel[i]) < multiplier * 4)
382                                 rotxvel[i] = 0;
383
384                             rotx[i] += rotxvel[i] * multiplier * 4;
385                         }
386                         if (rotyvel[i] || roty[i]) {
387                             if (roty[i] > 0) rotyvel[i] -= multiplier * 8 * fabs(roty[i]);
388                             if (roty[i] < 0) rotyvel[i] += multiplier * 8 * fabs(roty[i]);
389                             if (roty[i] > 0) rotyvel[i] -= multiplier * 4;
390                             if (roty[i] < 0) rotyvel[i] += multiplier * 4;
391                             if (rotyvel[i] > 0) rotyvel[i] -= multiplier * 4;
392                             if (rotyvel[i] < 0) rotyvel[i] += multiplier * 4;
393                             if (fabs(roty[i]) < multiplier * 4)
394                                 roty[i] = 0;
395                             if (fabs(rotyvel[i]) < multiplier * 4)
396                                 rotyvel[i] = 0;
397
398                             roty[i] += rotyvel[i] * multiplier * 4;
399                         }
400                         if (roty[i]) {
401                             glRotatef(roty[i], 1, 0, 0);
402                         }
403                         if (rotx[i]) {
404                             glRotatef(-rotx[i], 0, 0, 1);
405                         }
406                         if (rotx[i] > 10)
407                             rotx[i] = 10;
408                         if (rotx[i] < -10)
409                             rotx[i] = -10;
410                         if (roty[i] > 10)
411                             roty[i] = 10;
412                         if (roty[i] < -10)
413                             roty[i] = -10;
414                     }
415                     if (type[i] == treetrunktype || type[i] == treeleavestype) {
416                         messedwith[i] -= multiplier;
417                         if (rotxvel[i] || rotx[i]) {
418                             if (rotx[i] > 0) rotxvel[i] -= multiplier * 8 * fabs(rotx[i]);
419                             if (rotx[i] < 0) rotxvel[i] += multiplier * 8 * fabs(rotx[i]);
420                             if (rotx[i] > 0) rotxvel[i] -= multiplier * 4;
421                             if (rotx[i] < 0) rotxvel[i] += multiplier * 4;
422                             if (rotxvel[i] > 0) rotxvel[i] -= multiplier * 4;
423                             if (rotxvel[i] < 0) rotxvel[i] += multiplier * 4;
424                             if (fabs(rotx[i]) < multiplier * 4)
425                                 rotx[i] = 0;
426                             if (fabs(rotxvel[i]) < multiplier * 4)
427                                 rotxvel[i] = 0;
428
429                             rotx[i] += rotxvel[i] * multiplier * 4;
430                         }
431                         if (rotyvel[i] || roty[i]) {
432                             if (roty[i] > 0) rotyvel[i] -= multiplier * 8 * fabs(roty[i]);
433                             if (roty[i] < 0) rotyvel[i] += multiplier * 8 * fabs(roty[i]);
434                             if (roty[i] > 0) rotyvel[i] -= multiplier * 4;
435                             if (roty[i] < 0) rotyvel[i] += multiplier * 4;
436                             if (rotyvel[i] > 0) rotyvel[i] -= multiplier * 4;
437                             if (rotyvel[i] < 0) rotyvel[i] += multiplier * 4;
438                             if (fabs(roty[i]) < multiplier * 4)
439                                 roty[i] = 0;
440                             if (fabs(rotyvel[i]) < multiplier * 4)
441                                 rotyvel[i] = 0;
442
443                             roty[i] += rotyvel[i] * multiplier * 4;
444                         }
445                         if (roty[i]) {
446                             glRotatef(roty[i] / 2, 1, 0, 0);
447                         }
448                         if (rotx[i]) {
449                             glRotatef(-rotx[i] / 2, 0, 0, 1);
450                         }
451                         if (rotx[i] > 10)
452                             rotx[i] = 10;
453                         if (rotx[i] < -10)
454                             rotx[i] = -10;
455                         if (roty[i] > 10)
456                             roty[i] = 10;
457                         if (roty[i] < -10)
458                             roty[i] = -10;
459                     }
460                     if (environment == snowyenvironment) {
461                         if (type[i] == treeleavestype) {
462                             glRotatef((sin(windvar + position[i].x * .3) + .5) * 1.5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
463                         }
464                         if (type[i] == treetrunktype) {
465                             glRotatef((sin(windvar + position[i].x * .3) + .5)*.5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
466                         }
467                         if (type[i] == bushtype) {
468                             glRotatef((sin(windvar + position[i].x * .3) + .5) * 4 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
469                         }
470                     }
471                     if (environment == grassyenvironment) {
472                         if (type[i] == treeleavestype) {
473                             glRotatef((sin(windvar + position[i].x * .3) + .5) * 1.5 * .5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
474                         }
475                         if (type[i] == treetrunktype) {
476                             glRotatef((sin(windvar + position[i].x * .3) + .5)*.5 * .5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
477                         }
478                         if (type[i] == bushtype) {
479                             glRotatef((sin(windvar + position[i].x * .3) + .5) * 4 * .5 * (sin(windvar * 2 + position[i].x * .3) + 1) / 2, 1, 0, 0);
480                         }
481                     }
482                     glRotatef(yaw[i], 0, 1, 0);
483                     glColor4f(1, 1, 1, distance);
484                     if (type[i] == treeleavestype) {
485                         glDisable(GL_CULL_FACE);
486                         glDisable(GL_LIGHTING);
487                         terrainlight = terrain.getLighting(position[i].x, position[i].z);
488                         glDepthMask(0);
489                         glEnable(GL_BLEND);
490                         glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, .3);
491                         glAlphaFunc(GL_GREATER, 0);
492                         glDisable(GL_ALPHA_TEST);
493                         model[i].drawdifftex(treetextureptr);
494                     }
495                     if (type[i] == bushtype) {
496                         glDisable(GL_CULL_FACE);
497                         glDisable(GL_LIGHTING);
498                         terrainlight = terrain.getLighting(position[i].x, position[i].z);
499                         glDepthMask(0);
500                         glEnable(GL_BLEND);
501                         glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, .3);
502                         glAlphaFunc(GL_GREATER, 0);
503                         glDisable(GL_ALPHA_TEST);
504                         model[i].drawdifftex(bushtextureptr);
505                     }
506                     glPopMatrix();
507                 }
508             }
509         }
510     }
511     if (environment == desertenvironment)
512         glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
513     glEnable(GL_ALPHA_TEST);
514     SetUpLight(&light, 0);
515 }
516
517 void Objects::DeleteObject(int which)
518 {
519     type[numobjects - 1] = 0;
520     yaw[numobjects - 1] = 0;
521     position[numobjects - 1] = 0;
522     scale[numobjects - 1] = 0;
523     friction[numobjects - 1] = 0;
524
525     numobjects--;
526 }
527
528 void Objects::MakeObject(int atype, XYZ where, float ayaw, float ascale)
529 {
530     if ((atype != treeleavestype && atype != bushtype) || foliage == 1) {
531         scale[numobjects] = ascale;
532         if (atype == treeleavestype)
533             scale[numobjects] += fabs((float)(Random() % 100) / 900) * ascale;
534
535         onfire[numobjects] = 0;
536         flamedelay[numobjects] = 0;
537         type[numobjects] = atype;
538
539         if (atype == firetype)
540             onfire[numobjects] = 1;
541
542         position[numobjects] = where;
543         if (atype == bushtype)
544             position[numobjects].y = terrain.getHeight(position[numobjects].x, position[numobjects].z) - .3;
545         yaw[numobjects] = ayaw;
546
547         rotxvel[numobjects] = 0;
548         rotyvel[numobjects] = 0;
549         rotx[numobjects] = 0;
550         roty[numobjects] = 0;
551
552         if (atype == boxtype)           model[numobjects].loaddecal((char *)":Data:Models:Box.solid", 0);
553         if (atype == cooltype)          model[numobjects].loaddecal((char *)":Data:Models:Cool.solid", 0);
554         if (atype == walltype)          model[numobjects].loaddecal((char *)":Data:Models:Wall.solid", 0);
555         if (atype == tunneltype)        model[numobjects].loaddecal((char *)":Data:Models:Tunnel.solid", 0);
556         if (atype == chimneytype)       model[numobjects].loaddecal((char *)":Data:Models:Chimney.solid", 0);
557         if (atype == spiketype)         model[numobjects].load((char *)":Data:Models:Spike.solid", 0);
558         if (atype == weirdtype)         model[numobjects].loaddecal((char *)":Data:Models:Weird.solid", 0);
559         if (atype == rocktype)          model[numobjects].loaddecal((char *)":Data:Models:Rock.solid", 0);
560         if (atype == treetrunktype)     model[numobjects].load((char *)":Data:Models:Treetrunk.solid", 0);
561         if (atype == treeleavestype)    model[numobjects].load((char *)":Data:Models:Leaves.solid", 0);
562         if (atype == bushtype)          model[numobjects].load((char *)":Data:Models:Bush.solid", 0);
563
564         if (atype == boxtype)           friction[numobjects] = 1.5;
565         if (atype == cooltype)          friction[numobjects] = 1.5;
566         if (atype == walltype)          friction[numobjects] = 1.5;
567         if (atype == platformtype)      friction[numobjects] = 1.5;
568         if (atype == tunneltype)        friction[numobjects] = 1.5;
569         if (atype == chimneytype)       friction[numobjects] = 1.5;
570         if (atype == rocktype)          friction[numobjects] = .5;
571         if (atype == rocktype && ascale>.5) friction[numobjects] = 1.5;
572         if (atype == weirdtype)         friction[numobjects] = 1.5;
573         if (atype == spiketype)         friction[numobjects] = .4;
574         if (atype == treetrunktype)     friction[numobjects] = .4;
575         if (atype == treeleavestype)    friction[numobjects] = 0;
576
577         if (atype == platformtype) {
578             model[numobjects].loaddecal((char *)":Data:Models:Platform.solid", 0);
579             model[numobjects].Rotate(90, 0, 0);
580         }
581
582         if (type[numobjects] == boxtype || type[numobjects] == cooltype || type[numobjects] == spiketype || type[numobjects] == weirdtype || type[numobjects] == walltype || type[numobjects] == chimneytype || type[numobjects] == tunneltype || type[numobjects] == platformtype) {
583             model[numobjects].ScaleTexCoords(scale[numobjects] * 1.5);
584         }
585         if (type[numobjects] == rocktype) {
586             model[numobjects].ScaleTexCoords(scale[numobjects] * 3);
587         }
588         model[numobjects].flat = 1;
589         if (atype == treetrunktype || atype == treeleavestype || atype == rocktype) {
590             model[numobjects].flat = 0;
591         }
592         model[numobjects].Scale(.3 * scale[numobjects], .3 * scale[numobjects], .3 * scale[numobjects]);
593         model[numobjects].Rotate(90, 1, 1);
594         if (type[numobjects] == rocktype) {
595             model[numobjects].Rotate(ayaw * 5, 1, 1);
596         }
597         model[numobjects].CalculateNormals(1);
598         model[numobjects].ScaleNormals(-1, -1, -1);
599
600         if (atype == treetrunktype && position[numobjects].y < terrain.getHeight(position[numobjects].x, position[numobjects].z) + 1) {
601             if (detail == 2)
602                 terrain.MakeDecal(shadowdecalpermanent, position[numobjects], 2, .4, 0);
603         }
604
605         if (atype == bushtype && position[numobjects].y < terrain.getHeight(position[numobjects].x, position[numobjects].z) + 1) {
606             if (detail == 2)
607                 terrain.MakeDecal(shadowdecalpermanent, position[numobjects], 1, .4, 0);
608         }
609
610         if (atype != treeleavestype && atype != bushtype && atype != firetype)
611             terrain.AddObject(where + DoRotation(model[numobjects].boundingspherecenter, 0, ayaw, 0), model[numobjects].boundingsphereradius, numobjects);
612
613         numobjects++;
614     }
615 }
616
617 void Objects::MakeObject(int atype, XYZ where, float ayaw, float apitch, float ascale)
618 {
619     if ((atype != treeleavestype && atype != bushtype) || foliage == 1) {
620         scale[numobjects] = ascale;
621         if (atype == treeleavestype)
622             scale[numobjects] += fabs((float)(Random() % 100) / 900) * ascale;
623
624         onfire[numobjects] = 0;
625         flamedelay[numobjects] = 0;
626         type[numobjects] = atype;
627
628         if (atype == firetype)
629             onfire[numobjects] = 1;
630
631         position[numobjects] = where;
632         if (atype == bushtype)
633             position[numobjects].y = terrain.getHeight(position[numobjects].x, position[numobjects].z) - .3;
634         /*if(atype==firetype){
635         if(position[numobjects].y<terrain.getHeight(position[numobjects].x,position[numobjects].z)-.3)
636         position[numobjects].y=terrain.getHeight(position[numobjects].x,position[numobjects].z)-.3;
637         }*/
638         yaw[numobjects] = ayaw;
639         pitch[numobjects] = apitch;
640
641         rotxvel[numobjects] = 0;
642         rotyvel[numobjects] = 0;
643         rotx[numobjects] = 0;
644         roty[numobjects] = 0;
645
646         if (atype == boxtype)           model[numobjects].loaddecal((char *)":Data:Models:Box.solid", 0);
647         if (atype == cooltype)          model[numobjects].loaddecal((char *)":Data:Models:Cool.solid", 0);
648         if (atype == walltype)          model[numobjects].loaddecal((char *)":Data:Models:Wall.solid", 0);
649         if (atype == tunneltype)        model[numobjects].loaddecal((char *)":Data:Models:Tunnel.solid", 0);
650         if (atype == chimneytype)       model[numobjects].loaddecal((char *)":Data:Models:Chimney.solid", 0);
651         if (atype == spiketype)         model[numobjects].load((char *)":Data:Models:Spike.solid", 0);
652         if (atype == weirdtype)         model[numobjects].loaddecal((char *)":Data:Models:Weird.solid", 0);
653         if (atype == rocktype)          model[numobjects].loaddecal((char *)":Data:Models:Rock.solid", 0);
654         if (atype == treetrunktype)     model[numobjects].load((char *)":Data:Models:Treetrunk.solid", 0);
655         if (atype == treeleavestype)    model[numobjects].load((char *)":Data:Models:Leaves.solid", 0);
656         if (atype == bushtype)          model[numobjects].load((char *)":Data:Models:Bush.solid", 0);
657
658         if (atype == boxtype)           friction[numobjects] = 1.5;
659         if (atype == cooltype)          friction[numobjects] = 1.5;
660         if (atype == walltype)          friction[numobjects] = 1.5;
661         if (atype == platformtype)      friction[numobjects] = 1.5;
662         if (atype == tunneltype)        friction[numobjects] = 1.5;
663         if (atype == chimneytype)       friction[numobjects] = 1.5;
664         if (atype == rocktype)          friction[numobjects] = .5;
665         if (atype == rocktype && ascale>.5) friction[numobjects] = 1.5;
666         if (atype == weirdtype)         friction[numobjects] = 1.5;
667         if (atype == spiketype)         friction[numobjects] = .4;
668         if (atype == treetrunktype)     friction[numobjects] = .4;
669         if (atype == treeleavestype)    friction[numobjects] = 0;
670
671         if (friction[numobjects] == 1.5 && fabs(apitch) > 5)
672             friction[numobjects] = .5;
673
674         if (atype == platformtype) {
675             model[numobjects].loaddecal((char *)":Data:Models:Platform.solid", 0);
676             model[numobjects].Rotate(90, 0, 0);
677         }
678
679         if (type[numobjects] == boxtype || type[numobjects] == cooltype || type[numobjects] == spiketype || type[numobjects] == weirdtype || type[numobjects] == walltype || type[numobjects] == chimneytype || type[numobjects] == tunneltype || type[numobjects] == platformtype) {
680             model[numobjects].ScaleTexCoords(scale[numobjects] * 1.5);
681         }
682         if (type[numobjects] == rocktype) {
683             model[numobjects].ScaleTexCoords(scale[numobjects] * 3);
684         }
685         model[numobjects].flat = 1;
686         if (atype == treetrunktype || atype == treeleavestype || atype == rocktype) {
687             model[numobjects].flat = 0;
688         }
689         model[numobjects].Scale(.3 * scale[numobjects], .3 * scale[numobjects], .3 * scale[numobjects]);
690         model[numobjects].Rotate(90, 1, 1);
691         model[numobjects].Rotate(apitch, 0, 0);
692         if (type[numobjects] == rocktype) {
693             model[numobjects].Rotate(ayaw * 5, 0, 0);
694         }
695         model[numobjects].CalculateNormals(1);
696         model[numobjects].ScaleNormals(-1, -1, -1);
697
698         if (atype == treetrunktype && position[numobjects].y < terrain.getHeight(position[numobjects].x, position[numobjects].z) + 1) {
699             if (detail == 2)
700                 terrain.MakeDecal(shadowdecalpermanent, position[numobjects], 2, .4, 0);
701         }
702
703         if (atype == bushtype && position[numobjects].y < terrain.getHeight(position[numobjects].x, position[numobjects].z) + 1) {
704             if (detail == 2)
705                 terrain.MakeDecal(shadowdecalpermanent, position[numobjects], 1, .4, 0);
706         }
707
708         if (atype != treeleavestype && atype != bushtype && atype != firetype)
709             terrain.AddObject(where + DoRotation(model[numobjects].boundingspherecenter, 0, ayaw, 0), model[numobjects].boundingsphereradius, numobjects);
710
711         numobjects++;
712     }
713 }
714
715 void Objects::DoStuff()
716 {
717     XYZ spawnpoint;
718     for (int i = 0; i < numobjects; i++) {
719         /*if(type[i]==firetype){
720         Sprite::MakeSprite(weaponshinesprite, position[i],position[i]*0, 1,1,1, 5, 1);
721         }*/
722
723         if (type[i] == firetype)
724             onfire[i] = 1;
725         if (onfire[i]) {
726             if (type[i] == bushtype)
727                 flamedelay[i] -= multiplier * 3;
728             if (type[i] == firetype)
729                 flamedelay[i] -= multiplier * 3;
730             if (type[i] == treeleavestype)
731                 flamedelay[i] -= multiplier * 4;
732             while (flamedelay[i] < 0 && onfire[i]) {
733                 flamedelay[i] += .006;
734                 if (type[i] == bushtype || type[i] == firetype) {
735                     spawnpoint.x = ((float)(Random() % 100)) / 30 * scale[i];
736                     spawnpoint.y = ((float)(Random() % 100) + 60) / 30 * scale[i];
737                     spawnpoint.z = 0;
738                     spawnpoint = DoRotation(spawnpoint, 0, Random() % 360, 0);
739                     spawnpoint += position[i];
740                     Sprite::MakeSprite(flamesprite, spawnpoint, spawnpoint * 0, 1, 1, 1, (.6 + (float)abs(Random() % 100) / 200 - .25) * 5 * scale[i], 1);
741                 }
742                 if (type[i] == treeleavestype) {
743                     spawnpoint.x = ((float)(Random() % 100)) / 80 * scale[i];
744                     spawnpoint.y = ((float)(Random() % 100) + 80) / 12 * scale[i];
745                     spawnpoint.z = 0;
746                     spawnpoint = DoRotation(spawnpoint, 0, Random() % 360, 0);
747                     spawnpoint += position[i];
748                     Sprite::MakeSprite(flamesprite, spawnpoint, spawnpoint * 0, 1, 1, 1, (.6 + (float)abs(Random() % 100) / 200 - .25) * 6, 1);
749                 }
750             }
751
752         }
753     }
754 }
755
756 void Objects::DoShadows()
757 {
758     int i, j, k, l;
759     static XYZ testpoint, testpoint2, terrainpoint, lightloc, col;
760     lightloc = light.location;
761     if (!skyboxtexture)
762         lightloc = 0;
763     lightloc.y += 10;
764     Normalise(&lightloc);
765     int patchx, patchz;
766
767     if (numobjects > 0)
768         for (i = 0; i < numobjects; i++) {
769             if (type[i] != treeleavestype && type[i] != treetrunktype && type[i] != bushtype && type[i] != firetype) {
770                 for (j = 0; j < model[i].vertexNum; j++) {
771                     terrainpoint = position[i] + DoRotation(model[i].vertex[j] + model[i].normals[j] * .1, 0, yaw[i], 0);
772                     //terrainpoint.y+=model[i].boundingsphereradius;
773                     shadowed[i] = 0;
774                     patchx = terrainpoint.x / (terrain.size / subdivision * terrain.scale);
775                     patchz = terrainpoint.z / (terrain.size / subdivision * terrain.scale);
776                     if (patchx >= 0 && patchz >= 0 && patchx < subdivision && patchz < subdivision)
777                         if (terrain.patchobjectnum[patchx][patchz])
778                             for (k = 0; k < terrain.patchobjectnum[patchx][patchz]; k++) {
779                                 l = terrain.patchobjects[patchx][patchz][k];
780                                 if (type[l] != treetrunktype/*&&l!=i*/) {
781                                     testpoint = terrainpoint;
782                                     testpoint2 = terrainpoint + lightloc * 50 * (1 - shadowed[i]);
783                                     if (model[l].LineCheck(&testpoint, &testpoint2, &col, &position[l], &yaw[l]) != -1) {
784                                         shadowed[i] = 1 - (findDistance(&terrainpoint, &col) / 50);
785                                     }
786                                 }
787                             }
788                     if (shadowed[i] > 0) {
789                         col = model[i].normals[j] - DoRotation(lightloc * shadowed[i], 0, -yaw[i], 0);
790                         Normalise(&col);
791                         for (k = 0; k < model[i].TriangleNum; k++) {
792                             if (model[i].Triangles[k].vertex[0] == j) {
793                                 l = k * 24;
794                                 model[i].vArray[l + 2] = col.x;
795                                 model[i].vArray[l + 3] = col.y;
796                                 model[i].vArray[l + 4] = col.z;
797                             }
798                             if (model[i].Triangles[k].vertex[1] == j) {
799                                 l = k * 24;
800                                 model[i].vArray[l + 10] = col.x;
801                                 model[i].vArray[l + 11] = col.y;
802                                 model[i].vArray[l + 12] = col.z;
803                             }
804                             if (model[i].Triangles[k].vertex[2] == j) {
805                                 l = k * 24;
806                                 model[i].vArray[l + 18] = col.x;
807                                 model[i].vArray[l + 19] = col.y;
808                                 model[i].vArray[l + 20] = col.z;
809                             }
810                         }
811                     }
812                 }
813             }
814             shadowed[i] = 0;
815         }
816 }
817
818 Objects::Objects()
819 {
820     center = 0;
821     radius = 0;
822     numobjects = 0;
823
824     memset(position, 0, sizeof(position));
825     memset(type, 0, sizeof(type));
826     memset(yaw, 0, sizeof(yaw));
827     memset(pitch, 0, sizeof(pitch));
828     memset(rotx, 0, sizeof(rotx));
829     memset(rotxvel, 0, sizeof(rotxvel));
830     memset(roty, 0, sizeof(roty));
831     memset(rotyvel, 0, sizeof(rotyvel));
832     memset(possible, 0, sizeof(possible));
833     memset(model, 0, sizeof(model));
834     memset(displaymodel, 0, sizeof(displaymodel));
835     memset(friction, 0, sizeof(friction));
836     memset(scale, 0, sizeof(scale));
837     memset(messedwith, 0, sizeof(messedwith));
838     memset(checked, 0, sizeof(checked));
839     memset(shadowed, 0, sizeof(shadowed));
840     memset(occluded, 0, sizeof(occluded));
841     memset(onfire, 0, sizeof(onfire));
842     memset(flamedelay, 0, sizeof(flamedelay));
843 }
844
845 Objects::~Objects()
846 {
847     boxtextureptr.destroy();
848     treetextureptr.destroy();
849     bushtextureptr.destroy();
850     rocktextureptr.destroy();
851 };
852